/// <summary> /// Callback method from input selection model which will actually do the selected operation. /// </summary> /// <param name="e">Event Argument</param> private void DoBEDSubtract(InputSequenceRangeSelectionEventArg e) { this.ScreenUpdate(false); Workbook currentWorkbook = Globals.ThisAddIn.Application.ActiveWorkbook; var subtractOutputType = SubtractOutputType.NonOverlappingPiecesOfIntervals; SequenceRangeGrouping result = null; if ((bool)e.Parameter[InputSelection.OVERLAP]) { subtractOutputType = SubtractOutputType.IntervalsWithNoOverlap; } result = e.Sequences[0].Subtract( e.Sequences[1], (long)e.Parameter[InputSelection.MINIMUMOVERLAP], subtractOutputType, true); string sheetName = Resources.SUBTRACT_SHEET + this.subtractSheetNumber.ToString(CultureInfo.CurrentCulture); this.subtractSheetNumber++; this.WriteSequenceRange(currentWorkbook, sheetName, result, e.Data, true, false); this.ScreenUpdate(true); }
/// <summary> /// Callback method from input selection model which will actually do the selected operation. /// </summary> /// <param name="e">Event Argument</param> private void DoBEDIntersect(InputSequenceRangeSelectionEventArg e) { this.ScreenUpdate(false); Workbook currentWorkbook = Globals.ThisAddIn.Application.ActiveWorkbook; var intersectOutputType = IntersectOutputType.OverlappingIntervals; var result = new List<SequenceRangeGrouping>(); if ((bool)e.Parameter[InputSelection.OVERLAP]) { intersectOutputType = IntersectOutputType.OverlappingPiecesOfIntervals; } for (int i = 1; i < e.Sequences.Count; i++) { if (0 == result.Count) { result.Add( e.Sequences[i - 1].Intersect( e.Sequences[i], (long)e.Parameter[InputSelection.MINIMUMOVERLAP], intersectOutputType, true)); } else { result.Add( result[result.Count - 1].Intersect( e.Sequences[i], (long)e.Parameter[InputSelection.MINIMUMOVERLAP], intersectOutputType, true)); } } string sheetName = Resources.INTERSECT_SHEET + this.intersectSheetNumber.ToString(CultureInfo.CurrentCulture); this.intersectSheetNumber++; this.WriteSequenceRange(currentWorkbook, sheetName, result[result.Count - 1], e.Data, true, false); this.ScreenUpdate(true); }
/// <summary> /// Method which will export a parsed BED file /// </summary> /// <param name="e">Event Arguments</param> private void DoExportRangeSequence(InputSequenceRangeSelectionEventArg e) { Stream sequenceWriter = null; //todo: Below line used hard coded BEDFormatter, have to get it from args later on ISequenceRangeFormatter formatter = new BedFormatter(); var saveDialog = new SaveFileDialog { Filter = "BED Files|*.BED" }; try { if (e.Sequences[0].GroupIDs.Any()) { if (saveDialog.ShowDialog() == DialogResult.OK) { // Get a textwriter to the file which will append text to it. sequenceWriter = File.OpenWrite(saveDialog.FileName); formatter.Format(sequenceWriter, e.Sequences[0]); } } else { MessageBox.Show( Resources.EMPTY_SEQUENCE_RANGE, Resources.CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception ex) { MessageBox.Show(ex.Message, Resources.CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (sequenceWriter != null) { sequenceWriter.Close(); } } }
/// <summary> /// Call venn diagram module to create the diagran and display it using NodeXL /// </summary> private void DoDrawVenn(InputSequenceRangeSelectionEventArg e) { // Call VennToNodeXL which will do the calculations and add the NodeXL workbook to the application object passed. if (e.Sequences.Count == 2) { VennToNodeXL.CreateNodeXLVennDiagramWorkbookFromSequenceRangeGroupings( Globals.ThisAddIn.Application, e.Sequences[0], e.Sequences[1]); } else // if sequence count is not 2, its 3. InputSequenceDialog guarentees it as we set min=2 and max=3. { VennToNodeXL.CreateNodeXLVennDiagramWorkbookFromSequenceRangeGroupings( Globals.ThisAddIn.Application, e.Sequences[0], e.Sequences[1], e.Sequences[2]); } }
/// <summary> /// Callback method from input selection model which will actually do the selected operation. /// </summary> /// <param name="e">Event Argument</param> private void DoBEDMerge(InputSequenceRangeSelectionEventArg e) { this.ScreenUpdate(false); Workbook currentWorkbook = Globals.ThisAddIn.Application.ActiveWorkbook; if (e.Sequences.Count > 1) { SequenceRangeGrouping referenceGrouping = e.Sequences[0]; // First item is mentioned as reference sequence when calling the dialog. for (int i = 1; i < e.Sequences.Count; i++) { SequenceRangeGrouping rangeGrouping = e.Sequences[i]; referenceGrouping = referenceGrouping.MergeOverlaps( rangeGrouping, (long)e.Parameter[InputSelection.MINIMUMOVERLAP], true); } string sheetName = Resources.MERGED_SHEET + this.currentMergeSheetNumber.ToString(CultureInfo.CurrentCulture); this.currentMergeSheetNumber++; this.WriteSequenceRange(currentWorkbook, sheetName, referenceGrouping, e.Data, false, true); } else { SequenceRangeGrouping mergedOverlap = e.Sequences[0]; if (mergedOverlap != null) { string sheetName = Resources.MERGED_SHEET + this.currentMergeSheetNumber.ToString(CultureInfo.CurrentCulture); this.currentMergeSheetNumber++; mergedOverlap = mergedOverlap.MergeOverlaps((long)e.Parameter[InputSelection.MINIMUMOVERLAP], true); this.WriteSequenceRange(currentWorkbook, sheetName, mergedOverlap, e.Data, false, true); } } this.ScreenUpdate(true); }
/// <summary> /// Method called when the user clicks Ok button on InputSelectionDialog. /// Takes care of parsing the selections and returning the result to the user. /// In case there was an error parsing, it will show the input selection dialog again with the sequence highlighted. /// </summary> /// <param name="dialog">InputSequenceDialog object which raised this event</param> private void OnInputSequenceRangeDialogSubmit(ISelectionDialog dialog) { InputSelectionDialog selectionDialog = dialog as InputSelectionDialog; GroupData cachedData = null; // maps sheet to its column-mapping Dictionary<string, Dictionary<int, string>> columnMappedSheets = new Dictionary<string, Dictionary<int, string>>(); // Goes in the cache and is the output of this method as well. Dictionary<SequenceRangeGrouping, GroupData> groupsData = new Dictionary<SequenceRangeGrouping, GroupData>(); List<SequenceRangeGrouping> parsedSequences = new List<SequenceRangeGrouping>(); SequenceRangeGrouping sequenceRangeGroup = null; Dictionary<string, Dictionary<ISequenceRange, string>> sheetData = null; Dictionary<ISequenceRange, string> rangeData = null; List<ISequenceRange> sequenceRanges = null; List<Range> rangesInCurrentSequenceItem; // Regular expression to read the sheet name from address Regex regexSheetname = new Regex(@"(?<Sheetname>^.[^!]*)", RegexOptions.IgnoreCase); Match matchSheetname = null; string sheetName = string.Empty; try { foreach (InputSequenceItem currentSequenceItem in selectionDialog.GetSequences()) { try { rangesInCurrentSequenceItem = GetRanges(currentSequenceItem.SequenceAddress); // get from cache cachedData = SequenceCache.TryGetSequence(rangesInCurrentSequenceItem, selectionDialog.InputParamsAsKey) as GroupData; if (cachedData != null) { // got from cache cachedData.Name = currentSequenceItem.SequenceName; // Set ID if (currentSequenceItem.IsUseMetadataSelected) { parsedSequences.Insert(0, cachedData.Group); } else { parsedSequences.Add(cachedData.Group); } if (!groupsData.ContainsKey(cachedData.Group)) { groupsData.Add(cachedData.Group, cachedData); } } else { // parse it as its not in cache sheetData = new Dictionary<string, Dictionary<ISequenceRange, string>>(); sequenceRanges = new List<ISequenceRange>(); foreach (Range currentRange in rangesInCurrentSequenceItem) { bool firstRowIsHeader = false; // See if the sheet in which this range is, has a column mapping if (!columnMappedSheets.ContainsKey(GetMappingKey(currentRange))) { (currentRange.Worksheet as _Worksheet).Activate(); currentRange.Select(); Dictionary<int, string> mapping = GetMappingForRange(currentRange, out firstRowIsHeader); if (mapping == null) { // Could not get a proper mapping. So redirect to previous window. selectionDialog.ShowDialog(); return; } if (firstRowIsHeader) { UpdateColumnHeaders(currentRange, mapping); } columnMappedSheets.Add(GetMappingKey(currentRange), mapping); } // If range has a header, remove first row from it before sending it for parsing. Range rangeToParse; if (firstRowIsHeader) { if (currentRange.Rows.Count == 1) // only one row which is marked as header, throw error { throw new InvalidOperationException(Resources.SelectionModel_ParsingFailed); } rangeToParse = currentRange.get_Offset(1, 0); rangeToParse = rangeToParse.get_Resize(currentRange.Rows.Count - 1, currentRange.Columns.Count); } else { rangeToParse = currentRange; } Dictionary<ISequenceRange, string> srCollection = ExcelSelectionParser.RangeToSequenceRange( rangeToParse, columnMappedSheets[GetMappingKey(currentRange)]); foreach (KeyValuePair<ISequenceRange, string> sr in srCollection) { matchSheetname = regexSheetname.Match(sr.Value); if (matchSheetname.Success) { sheetName = matchSheetname.Groups["Sheetname"].Value; if (sheetData.TryGetValue(sheetName, out rangeData)) { rangeData.Add(sr.Key, sr.Value); } else { rangeData = new Dictionary<ISequenceRange, string>(); sheetData.Add(sheetName, rangeData); rangeData.Add(sr.Key, sr.Value); } sequenceRanges.Add(sr.Key); } } } sequenceRangeGroup = new SequenceRangeGrouping(sequenceRanges); cachedData = new GroupData(sequenceRangeGroup, currentSequenceItem.SequenceName, sheetData); SequenceCache.Add(rangesInCurrentSequenceItem, cachedData, selectionDialog.InputParamsAsKey); if (currentSequenceItem.IsUseMetadataSelected) { parsedSequences.Insert(0, cachedData.Group); } else { parsedSequences.Add(cachedData.Group); } groupsData.Add(cachedData.Group, cachedData); } } catch { // Set error status on the current item and re-throw the error currentSequenceItem.SetErrorStatus(true); throw; } } Dictionary<string, object> parameters = new Dictionary<string, object>(); parameters.Add(InputSelection.OVERLAP, selectionDialog.OverlappingBasePairs); parameters.Add(InputSelection.MINIMUMOVERLAP, selectionDialog.MinOverLap); // On successful completion of parsing... if (inputSequenceRangeSelectionComplete != null) { InputSequenceRangeSelectionEventArg eventArg = new InputSequenceRangeSelectionEventArg(groupsData, parsedSequences, parameters, argsForCallback); inputSequenceRangeSelectionComplete(eventArg); } selectionDialog.InputSelectionDialogSubmitting -= OnInputSequenceDialogSubmit; selectionDialog.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, Resources.CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error); selectionDialog.ShowDialog(); } }