コード例 #1
0
        /// <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="selectionDialog">InputSequenceDialog object which raised this event</param>
        private void OnInputSequenceDialogSubmit(ISelectionDialog dialog)
        {
            InputSelectionDialog     selectionDialog = dialog as InputSelectionDialog;
            List <ISequence>         parsedSequences = new List <ISequence>();
            List <Range>             rangesInCurrentSequenceItem;
            List <InputSequenceItem> sequenceItems = selectionDialog.GetSequences();

            try
            {
                foreach (InputSequenceItem currentSequenceItem in sequenceItems)
                {
                    try
                    {
                        rangesInCurrentSequenceItem = GetRanges(currentSequenceItem.SequenceAddress);
                        if (rangesInCurrentSequenceItem.Count > 0)
                        {
                            ISequence sequenceForCurrentItem;
                            sequenceForCurrentItem = SequenceCache.TryGetSequence(rangesInCurrentSequenceItem, selectionDialog.InputParamsAsKey) as ISequence; // get from cache
                            if (sequenceForCurrentItem == null)                                                                                                // if not in cache
                            {
                                sequenceForCurrentItem = ExcelSelectionParser.RangeToSequence(rangesInCurrentSequenceItem, selectionDialog.TreatBlankCellsAsGaps, selectionDialog.MoleculeType, currentSequenceItem.SequenceName);

                                SequenceCache.Add(rangesInCurrentSequenceItem, sequenceForCurrentItem, selectionDialog.InputParamsAsKey);
                            }
                            else
                            {
                                // Set the ID
                                sequenceForCurrentItem = SetSequenceID(sequenceForCurrentItem, currentSequenceItem.SequenceName);
                            }

                            parsedSequences.Add(sequenceForCurrentItem);
                        }
                        else
                        {
                            currentSequenceItem.SetErrorStatus(false);
                        }
                    }
                    catch
                    {
                        // Set error status on item and re-throw
                        currentSequenceItem.SetErrorStatus(true);
                        throw;
                    }
                }

                // On successful parsing...
                if (inputSequenceSelectionComplete != null)
                {
                    inputSequenceSelectionComplete(parsedSequences, this.argsForCallback);
                }
                selectionDialog.InputSelectionDialogSubmitting -= OnInputSequenceDialogSubmit;
                selectionDialog.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Resources.CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                selectionDialog.ShowDialog();
            }
        }
コード例 #2
0
        /// <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();
            }
        }