Ejemplo n.º 1
0
 private static void CheckPointsTypeRT(PointsTypeRT pointsType, int expectedPoints)
 {
     RunUI(() => SkylineWindow.ShowPointsType(pointsType));
     WaitForGraphs();
     RunUI(() =>
     {
         RTLinearRegressionGraphPane pane;
         Assert.IsTrue(SkylineWindow.RTGraphController.GraphSummary.TryGetGraphPane(out pane));
         Assert.AreEqual(expectedPoints, pane.StatisticsRefined.ListRetentionTimes.Count);
     });
 }
            public bool IsValidFor(SrmDocument document, int resultIndex, bool bestResult, double threshold, bool refine, PointsTypeRT pointsType)
            {
                string calculatorName = Settings.Default.RTCalculatorName;

                if (string.IsNullOrEmpty(calculatorName))
                {
                    calculatorName = _calculator.Name;
                }
                return(IsValidFor(document) &&
                       _resultIndex == resultIndex &&
                       _bestResult == bestResult &&
                       _threshold == threshold &&
                       _pointsType == pointsType &&
                       _calculatorName == Settings.Default.RTCalculatorName &&
                       ReferenceEquals(_calculator, Settings.Default.GetCalculatorByName(calculatorName)) &&
                       // Valid if refine is true, and this data requires no further refining
                       (_refine == refine || (refine && IsRefined())));
            }
            public GraphData(SrmDocument document, GraphData dataPrevious,
                             int resultIndex, double threshold, int?thresholdPrecision, bool refine, bool bestResult, PointsTypeRT pointsType)
            {
                _document           = document;
                _resultIndex        = resultIndex;
                _bestResult         = bestResult;
                _threshold          = threshold;
                _thresholdPrecision = thresholdPrecision;
                _pointsType         = pointsType;
                _peptidesIndexes    = new List <PeptideDocumentIndex>();
                _peptidesTimes      = new List <MeasuredRetentionTime>();
                int index = -1;

                var standards = new HashSet <string>();

                if (RTGraphController.PointsType == PointsTypeRT.standards)
                {
                    standards = document.GetRetentionTimeStandards();
                }

                // CONSIDER: Retention time prediction for small molecules?
                foreach (var nodePeptide in document.Peptides)
                {
                    index++;

                    switch (RTGraphController.PointsType)
                    {
                    default:
                        if (nodePeptide.IsDecoy)
                        {
                            continue;
                        }
                        break;

                    case PointsTypeRT.standards:
                        if (!standards.Contains(document.Settings.GetModifiedSequence(nodePeptide)))
                        {
                            continue;
                        }
                        break;

                    case PointsTypeRT.decoys:
                        if (!nodePeptide.IsDecoy)
                        {
                            continue;
                        }
                        break;
                    }

                    float?rt = null;
                    if (!bestResult)
                    {
                        rt = nodePeptide.GetSchedulingTime(resultIndex);
                    }
                    else
                    {
                        int iBest = nodePeptide.BestResult;
                        if (iBest != -1)
                        {
                            rt = nodePeptide.GetSchedulingTime(iBest);
                        }
                    }
                    if (!rt.HasValue)
                    {
                        rt = 0;
                    }

                    _peptidesIndexes.Add(new PeptideDocumentIndex(nodePeptide, index));
                    string modSeq = _document.Settings.GetSourceTextId(nodePeptide);
                    _peptidesTimes.Add(new MeasuredRetentionTime(modSeq, rt.Value));
                }

                _calculatorName = Settings.Default.RTCalculatorName;
                RetentionScoreCalculatorSpec calc = !string.IsNullOrEmpty(_calculatorName)
                                                        ? Settings.Default.GetCalculatorByName(Settings.Default.RTCalculatorName)
                                                        : null;

                if (calc == null)
                {
                    // Initialize all calculators
                    Settings.Default.RTScoreCalculatorList.Initialize(null);

                    //This call will pick the best calculator, disqualifying any iRT Calcs that do not have
                    //connected databases
                    _regressionAll = RetentionTimeRegression.CalcRegression(XmlNamedElement.NAME_INTERNAL,
                                                                            Settings.Default.RTScoreCalculatorList,
                                                                            _peptidesTimes,
                                                                            _scoreCache,
                                                                            true,
                                                                            out _statisticsAll,
                                                                            out _calculator);
                }
                else
                {
                    // Initialize the one calculator
                    calc = Settings.Default.RTScoreCalculatorList.Initialize(null, calc);

                    _regressionAll = RetentionTimeRegression.CalcRegression(XmlNamedElement.NAME_INTERNAL,
                                                                            new[] { calc },
                                                                            _peptidesTimes,
                                                                            _scoreCache,
                                                                            true,
                                                                            out _statisticsAll,
                                                                            out _calculator);

                    //If _regressionAll is null, it is safe to assume that the calculator is an iRT Calc with
                    //its database disconnected.
                    if (_regressionAll == null)
                    {
                        var tryIrtCalc = calc as RCalcIrt;
                        //Only show an error message if the user specifically chooses this calculator.
                        if (dataPrevious != null && !ReferenceEquals(calc, dataPrevious.Calculator) && tryIrtCalc != null)
                        {
                            throw new DatabaseNotConnectedException(tryIrtCalc);
                        }
                    }
                }

                if (_regressionAll != null)
                {
                    _scoreCache = new RetentionTimeScoreCache(new[] { _calculator }, _peptidesTimes,
                                                              dataPrevious != null ? dataPrevious._scoreCache : null);

                    if (dataPrevious != null && !ReferenceEquals(_calculator, dataPrevious._calculator))
                    {
                        _scoreCache.RecalculateCalcCache(_calculator);
                    }

                    _scoresRefined = _statisticsAll.ListHydroScores.ToArray();
                    _timesRefined  = _statisticsAll.ListRetentionTimes.ToArray();
                }

                _regressionPredict = document.Settings.PeptideSettings.Prediction.RetentionTime;
                if (_regressionPredict != null)
                {
                    if (!Equals(_calculator, _regressionPredict.Calculator))
                    {
                        _regressionPredict = null;
                    }
                    else
                    {
                        IDictionary <string, double> scoreCache = null;
                        if (_regressionAll != null && ReferenceEquals(_regressionAll.Calculator, _regressionPredict.Calculator))
                        {
                            scoreCache = _statisticsAll.ScoreCache;
                        }
                        _statisticsPredict = _regressionPredict.CalcStatistics(_peptidesTimes, scoreCache);
                    }
                }

                // Only refine, if not already exceeding the threshold
                _refine = refine && !IsRefined();
            }
        public override void UpdateGraph(bool checkData)
        {
            GraphHelper.FormatGraphPane(this);
            SrmDocument    document         = GraphSummary.DocumentUIContainer.DocumentUI;
            PeptideDocNode nodeSelected     = null;
            int            resultIndex      = (ShowReplicate == ReplicateDisplay.single ? GraphSummary.ResultsIndex : -1);
            var            results          = document.Settings.MeasuredResults;
            bool           resultsAvailable = results != null;

            if (resultsAvailable)
            {
                if (resultIndex == -1)
                {
                    resultsAvailable = results.IsLoaded;
                }
                else
                {
                    resultsAvailable = results.Chromatograms.Count > resultIndex &&
                                       results.IsChromatogramSetLoaded(resultIndex);
                }
            }

            if (!resultsAvailable)
            {
                Clear();
            }
            else
            {
                var nodeTree    = GraphSummary.StateProvider.SelectedNode as SrmTreeNode;
                var nodePeptide = nodeTree as PeptideTreeNode;
                while (nodePeptide == null && nodeTree != null)
                {
                    nodeTree    = nodeTree.Parent as SrmTreeNode;
                    nodePeptide = nodeTree as PeptideTreeNode;
                }
                if (nodePeptide != null)
                {
                    nodeSelected = nodePeptide.DocNode;
                }

                if (checkData)
                {
                    double threshold  = RTGraphController.OutThreshold;
                    bool   refine     = Settings.Default.RTRefinePeptides;
                    bool   bestResult = (ShowReplicate == ReplicateDisplay.best);

                    if ((RTGraphController.PointsType == PointsTypeRT.standards && !document.GetRetentionTimeStandards().Any()) ||
                        (RTGraphController.PointsType == PointsTypeRT.decoys &&
                         !document.PeptideGroups.Any(nodePepGroup => nodePepGroup.Children.Cast <PeptideDocNode>().Any(nodePep => nodePep.IsDecoy))))
                    {
                        RTGraphController.PointsType = PointsTypeRT.targets;
                    }
                    PointsTypeRT pointsType = RTGraphController.PointsType;

                    if (!IsValidFor(document, resultIndex, bestResult, threshold, refine, pointsType))
                    {
                        Update(document, resultIndex, threshold, refine, pointsType);
                        if (refine && !IsRefined)
                        {
                            // Do refinement on a background thread.
                            ActionUtil.RunAsync(RefineData, "Refine data"); // Not L10N
                        }
                    }
                }

                Graph(nodeSelected);
            }

            AxisChange();
            GraphSummary.GraphControl.Invalidate();
        }
        public void Update(SrmDocument document, int resultIndex, double threshold, bool refine, PointsTypeRT pointsType)
        {
            bool bestResults = (ShowReplicate == ReplicateDisplay.best);

            Data = new GraphData(document, Data, resultIndex, threshold, null, refine, bestResults, pointsType);

            XAxis.Title.Text = Data.Calculator.Name;
        }
        public bool IsValidFor(SrmDocument document, int resultIndex, bool bestResult, double threshold, bool refine, PointsTypeRT pointsType)
        {
            var data = Data;

            return(data != null && data.IsValidFor(document, resultIndex, bestResult, threshold, refine, pointsType));
        }