Пример #1
0
        public bool CalcRegressionWith(IRetentionTimeProvider retentionTimes, IEnumerable <DbIrtPeptide> standardPeptideList, DbIrtPeptide[] items)
        {
            if (items.Any())
            {
                // Attempt to get a regression based on shared peptides
                var calculator    = new CurrentCalculator(standardPeptideList, items);
                var peptidesTimes = retentionTimes.PeptideRetentionTimes.ToArray();
                var regression    = RetentionTimeRegression.FindThreshold(RCalcIrt.MIN_IRT_TO_TIME_CORRELATION,
                                                                          RetentionTimeRegression.ThresholdPrecision,
                                                                          peptidesTimes,
                                                                          new MeasuredRetentionTime[0],
                                                                          peptidesTimes, null,
                                                                          calculator,
                                                                          RegressionMethodRT.linear,
                                                                          () => false);

                var startingCount   = peptidesTimes.Length;
                var regressionCount = regression != null ? regression.PeptideTimes.Count : 0;
                if (regression != null && RCalcIrt.IsAcceptableStandardCount(startingCount, regressionCount))
                {
                    // Finally must recalculate the regression, because it is transposed from what
                    // we want.
                    var statTimes = new Statistics(regression.PeptideTimes.Select(pt => pt.RetentionTime));
                    var statIrts  = new Statistics(regression.PeptideTimes.Select(pt =>
                                                                                  calculator.ScoreSequence(pt.PeptideSequence) ?? calculator.UnknownScore));

                    RegressionRefined = new RegressionLine(statIrts.Slope(statTimes), statIrts.Intercept(statTimes));
                    RegressionSuccess = true;
                    return(true);
                }
            }
            return(false);
        }
Пример #2
0
        /// <summary>
        /// Align retention times with a target.
        /// For the MS2 Id's that are found in both the target and the timesToAlign, the MS2 id's
        /// are plotted against each other, and a linear regression is performed.
        /// In cases where there is more than one MS2 id in either file, only the earliest MS2 id from
        /// each file is used.
        /// </summary>
        public static AlignedRetentionTimes AlignLibraryRetentionTimes(IDictionary <Target, double> target, IDictionary <Target, double> originalTimes, double refinementThreshhold, RegressionMethodRT regressionMethod,
                                                                       Func <bool> isCanceled)
        {
            var calculator      = new DictionaryRetentionScoreCalculator("alignment", originalTimes); // Not L10N
            var targetTimesList = new List <MeasuredRetentionTime>();

            foreach (var entry in calculator.RetentionTimes)
            {
                double targetTime;
                if (!target.TryGetValue(entry.Key, out targetTime))
                {
                    continue;
                }
                MeasuredRetentionTime measuredRetentionTime;
                try
                {
                    measuredRetentionTime = new MeasuredRetentionTime(entry.Key, targetTime);
                }
                catch
                {
                    continue;
                }
                targetTimesList.Add(measuredRetentionTime);
            }
            RetentionTimeStatistics regressionStatistics;
            var regression = RetentionTimeRegression.CalcRegression(XmlNamedElement.NAME_INTERNAL, new[] { calculator }, regressionMethod, targetTimesList, out regressionStatistics);

            if (regression == null)
            {
                return(null);
            }
            RetentionTimeRegression regressionRefined;
            RetentionTimeStatistics regressionRefinedStatistics = regressionStatistics;
            HashSet <int>           outIndexes = new HashSet <int>();

            if (regressionStatistics.R >= refinementThreshhold)
            {
                regressionRefined = regression;
            }
            else
            {
                var cache = new RetentionTimeScoreCache(new[] { calculator }, new MeasuredRetentionTime[0], null);
                regressionRefined = regression.FindThreshold(refinementThreshhold, null, 0,
                                                             targetTimesList.Count, new MeasuredRetentionTime[0], targetTimesList, null, regressionStatistics,
                                                             calculator, regressionMethod, cache, isCanceled, ref regressionRefinedStatistics,
                                                             ref outIndexes);
            }

            return(new AlignedRetentionTimes
            {
                TargetTimes = target,
                OriginalTimes = originalTimes,
                Regression = regression,
                RegressionStatistics = regressionStatistics,
                RegressionRefined = regressionRefined,
                RegressionRefinedStatistics = regressionRefinedStatistics,
                OutlierIndexes = outIndexes,
                Calculator = calculator
            });
        }
Пример #3
0
        private static FileRetentionTimeAlignments CalculateFileRetentionTimeAlignments(
            string dataFileName, ResultNameMap <IDictionary <Target, double> > libraryRetentionTimes, IProgressMonitor progressMonitor)
        {
            var targetTimes = libraryRetentionTimes.Find(dataFileName);

            if (targetTimes == null)
            {
                return(null);
            }
            var alignments = new List <RetentionTimeAlignment>();

            foreach (var entry in libraryRetentionTimes)
            {
                if (dataFileName == entry.Key)
                {
                    continue;
                }
                var alignedFile = AlignedRetentionTimes.AlignLibraryRetentionTimes(targetTimes, entry.Value, REFINEMENT_THRESHHOLD, RegressionMethodRT.linear, new CustomCancellationToken(CancellationToken.None, () => progressMonitor.IsCanceled));
                if (alignedFile == null || alignedFile.RegressionRefinedStatistics == null ||
                    !RetentionTimeRegression.IsAboveThreshold(alignedFile.RegressionRefinedStatistics.R, REFINEMENT_THRESHHOLD))
                {
                    continue;
                }
                var regressionLine = alignedFile.RegressionRefined.Conversion as RegressionLineElement;
                if (regressionLine != null)
                {
                    alignments.Add(new RetentionTimeAlignment(entry.Key, regressionLine));
                }
            }
            return(new FileRetentionTimeAlignments(dataFileName, alignments));
        }
 private bool PointIsOverEx(RTLinearRegressionGraphPane graphPane, PointF point,
                            RetentionTimeRegression regression, double x, double y)
 {
     if (regression != null && regression.IsUsable)
     {
         y = GetResidual(regression, x, y);
     }
     return(graphPane.PointIsOver(point, x, y));
 }
            private double[] GetResiduals(RetentionTimeRegression regression, double[] scores, double[] times)
            {
                var residualsRefined = new double[times.Length];

                for (int i = 0; i < residualsRefined.Length; i++)
                {
                    residualsRefined[i] = GetResidual(regression, scores[i], times[i]);
                }
                return(residualsRefined);
            }
Пример #6
0
        private RetentionScoreCalculatorSpec RecalcRegression(IList <RetentionScoreCalculatorSpec> calculators, IList <MeasuredRetentionTime> peptidesTimes)
        {
            RetentionScoreCalculatorSpec calculatorSpec;
            RetentionTimeStatistics      statistics;

            var regression = RetentionTimeRegression.CalcRegression("Recalc", // Not L10N
                                                                    calculators,
                                                                    RegressionMethodRT.linear,
                                                                    peptidesTimes,
                                                                    out statistics);

            double r = 0;

            if (regression == null)
            {
                if (calculators.Count() > 1)
                {
                    textSlope.Text                = string.Empty;
                    textIntercept.Text            = string.Empty;
                    textTimeWindow.Text           = string.Empty;
                    comboCalculator.SelectedIndex = -1;

                    return(null);
                }
                calculatorSpec = calculators.First();
            }
            else
            {
                var regressionLine = regression.Conversion as RegressionLineElement;
                if (regressionLine != null)
                {
                    textSlope.Text     = string.Format("{0}", regressionLine.Slope);     // Not L10N
                    textIntercept.Text = string.Format("{0}", regressionLine.Intercept); // Not L10N
                }
                textTimeWindow.Text = string.Format("{0:F01}", regression.TimeWindow);   // Not L10N

                // Select best calculator match.
                calculatorSpec = regression.Calculator;

                // Save statistics to show in RTDetails form.
                _statistics = statistics;
                r           = statistics.R;
            }

            int minCount;
            var pepCount = calculatorSpec.ChooseRegressionPeptides(peptidesTimes.Select(mrt => mrt.PeptideSequence), out minCount).Count();

            labelRValue.Text = string.Format(Resources.EditRTDlg_RecalcRegression__0__peptides_R__1__, pepCount,
                                             Math.Round(r, RetentionTimeRegression.ThresholdPrecision));
            // Right align with the peptide grid.
            labelRValue.Left = gridPeptides.Right - labelRValue.Width;

            return(calculatorSpec);
        }
 public bool IsRefined()
 {
     // If refinement has been performed, or it doesn't need to be.
     if (_regressionRefined != null)
     {
         return(true);
     }
     if (_statisticsAll == null)
     {
         return(false);
     }
     return(RetentionTimeRegression.IsAboveThreshold(_statisticsAll.R, _threshold));
 }
Пример #8
0
        private RetentionScoreCalculatorSpec RecalcRegression(IList <RetentionScoreCalculatorSpec> calculators, IList <MeasuredRetentionTime> peptidesTimes)
        {
            var summary = RetentionTimeRegression.CalcBestRegressionLongOperationRunner(XmlNamedElement.NAME_INTERNAL, calculators, peptidesTimes,
                                                                                        null, false, RegressionMethodRT.linear, CustomCancellationToken.NONE);
            var regression     = summary.Best.Regression;
            var statistics     = summary.Best.Statistics;
            var calculatorSpec = summary.Best.Calculator;

            double r = 0;

            if (regression == null)
            {
                if (calculators.Count > 1)
                {
                    textSlope.Text                = string.Empty;
                    textIntercept.Text            = string.Empty;
                    textTimeWindow.Text           = string.Empty;
                    comboCalculator.SelectedIndex = -1;

                    return(null);
                }
                calculatorSpec = calculators.First();
            }
            else
            {
                var regressionLine = regression.Conversion as RegressionLineElement;
                if (regressionLine != null)
                {
                    textSlope.Text     = string.Format(@"{0}", regressionLine.Slope);
                    textIntercept.Text = string.Format(@"{0}", regressionLine.Intercept);
                }
                textTimeWindow.Text = string.Format(@"{0:F01}", regression.TimeWindow);

                // Select best calculator match.
                calculatorSpec = regression.Calculator;

                // Save statistics to show in RTDetails form.
                _statistics = statistics;
                r           = statistics.R;
            }

            int minCount;
            var pepCount = calculatorSpec.ChooseRegressionPeptides(peptidesTimes.Select(mrt => mrt.PeptideSequence), out minCount).Count();

            labelRValue.Text = string.Format(Resources.EditRTDlg_RecalcRegression__0__peptides_R__1__, pepCount,
                                             Math.Round(r, RetentionTimeRegression.ThresholdPrecision));
            // Right align with the peptide grid.
            labelRValue.Left = gridPeptides.Right - labelRValue.Width;

            return(calculatorSpec);
        }
Пример #9
0
        public void CalcRetentionTime(RetentionTimeRegression rtRegression)
        {
            double?rt = null;

            if (rtRegression != null)
            {
                double?score = rtRegression.Calculator.ScoreSequence(Sequence);
                if (score.HasValue)
                {
                    rt = rtRegression.Conversion.GetY(score.Value);
                }
            }
            PredictedRetentionTime = rt;
        }
Пример #10
0
        public static SrmDocument AddRetentionTimePredictor(SrmDocument doc, LibrarySpec libSpec)
        {
            var calc = new RCalcIrt(
                Helpers.GetUniqueName(libSpec.Name, Settings.Default.RTScoreCalculatorList.Select(lib => lib.Name).ToArray()),
                libSpec.FilePath);
            var predictor = new RetentionTimeRegression(
                Helpers.GetUniqueName(libSpec.Name, Settings.Default.RetentionTimeList.Select(rt => rt.Name).ToArray()),
                calc, null, null, DEFAULT_RT_WINDOW, new List <MeasuredRetentionTime>());

            Settings.Default.RTScoreCalculatorList.Add(calc);
            Settings.Default.RetentionTimeList.Add(predictor);
            return(doc.ChangeSettings(
                       doc.Settings.ChangePeptideSettings(
                           doc.Settings.PeptideSettings.ChangePrediction(
                               doc.Settings.PeptideSettings.Prediction.ChangeRetentionTime(predictor)))));
        }
            private static float AddRegressionLabel(PaneBase graphPane, Graphics g, double score, double time,
                                                    RetentionTimeRegression regression, RetentionTimeStatistics statistics, Color color)
            {
                string label;

                if (regression == null || regression.Conversion == null || statistics == null)
                {
                    label = String.Format("{0} = ?, {1} = ?\n" + "{2} = ?\n" + "r = ?", // Not L10N
                                          Resources.Regression_slope,
                                          Resources.Regression_intercept,
                                          Resources.GraphData_AddRegressionLabel_window);
                }
                else
                {
                    label = String.Format("{0} = {1:F02}, {2} = {3:F02}\n" + "{4} = {5:F01}\n" + "r = {6}",   // Not L10N
                                          Resources.Regression_slope,
                                          regression.Conversion.Slope,
                                          Resources.Regression_intercept,
                                          regression.Conversion.Intercept,
                                          Resources.GraphData_AddRegressionLabel_window,
                                          regression.TimeWindow,
                                          Math.Round(statistics.R, RetentionTimeRegression.ThresholdPrecision));
                }

                TextObj text = new TextObj(label, score, time,
                                           CoordType.AxisXYScale, AlignH.Left, AlignV.Top)
                {
                    IsClippedToChartRect = true,
                    ZOrder   = ZOrder.E_BehindCurves,
                    FontSpec = GraphSummary.CreateFontSpec(color),
                };

                graphPane.GraphObjList.Add(text);

                // Measure the text just added, and return its height
                SizeF sizeLabel = text.FontSpec.MeasureString(g, label, graphPane.CalcScaleFactor());

                return(sizeLabel.Height + 3);
            }
Пример #12
0
        public void OkDialog()
        {
            var    helper = new MessageBoxHelper(this);
            double window;

            if (!helper.ValidateDecimalTextBox(txtWindow, out window))
            {
                return;
            }

            if (Settings.Default.RetentionTimeList.Any(regression => regression.Name == txtName.Text))
            {
                MessageDlg.Show(this, Resources.AddRetentionTimePredictorDlg_OkDialog_A_retention_time_predictor_with_that_name_already_exists__Please_choose_a_new_name_);
                txtName.Focus();
                return;
            }

            Regression = new RetentionTimeRegression(
                txtName.Text, Calculator, null, null, window, new List <MeasuredRetentionTime>());

            DialogResult = DialogResult.OK;
        }
Пример #13
0
        public void SettingsChangeNotDoc()
        {
            SrmDocument docFasta = CreateMixedDoc();
            SrmSettings settings = docFasta.Settings;

            // Change declustering potential, collision energy, and retention time
            var regressions = new DeclusterPotentialList();

            regressions.AddDefaults();
            var dpRegress  = regressions["SCIEX"];
            var collisions = new CollisionEnergyList();

            collisions.AddDefaults();
            var ceRegress = collisions["SCIEX"];
            var calc      = Settings.Default.RTScoreCalculatorList.GetDefaults().First();
            var rtRegress = new RetentionTimeRegression("Test", calc, 3.5, 10.4, 12.8,
                                                        new MeasuredRetentionTime[0]);

            SrmSettings settings2 = settings.ChangePeptidePrediction(p => p.ChangeRetentionTime(rtRegress)).
                                    ChangeTransitionPrediction(p => p.ChangeCollisionEnergy(ceRegress).ChangeDeclusteringPotential(dpRegress));

            SrmDocument docFasta2 = docFasta.ChangeSettings(settings2);

            AssertEx.IsDocumentState(docFasta2, docFasta.RevisionIndex + 1, 3, 111, 352);
            Assert.AreSame(docFasta.Children, docFasta2.Children);
            Assert.AreNotEqual(docFasta.Settings, docFasta2.Settings);

            // Change auto-select toggles
            SrmSettings settings3 = settings.ChangePeptideFilter(f => f.ChangeAutoSelect(false)).
                                    ChangeTransitionFilter(f => f.ChangeAutoSelect(false));

            SrmDocument docFasta3 = docFasta.ChangeSettings(settings3);

            AssertEx.IsDocumentState(docFasta3, docFasta.RevisionIndex + 1, 3, 111, 352);
            Assert.AreSame(docFasta.Children, docFasta3.Children);
            Assert.AreNotEqual(docFasta.Settings, docFasta3.Settings);
        }
 private double GetResidual(RetentionTimeRegression regression, double score, double time)
 {
     return(time - regression.Conversion.GetY(score));
 }
Пример #15
0
        public PeptidePrediction(RetentionTimeRegression retentionTime, DriftTimePredictor driftTimePredictor, bool useMeasuredRTs, double? measuredRTWindow,
            bool useLibraryDriftTimes, double? libraryDriftTimesResolvingPower)
        {
            RetentionTime = retentionTime;
            DriftTimePredictor = driftTimePredictor;
            UseMeasuredRTs = useMeasuredRTs;
            MeasuredRTWindow = measuredRTWindow;
            UseLibraryDriftTimes = useLibraryDriftTimes;
            LibraryDriftTimesResolvingPower = libraryDriftTimesResolvingPower;

            DoValidate();
        }
Пример #16
0
 public PeptidePrediction ChangeRetentionTime(RetentionTimeRegression prop)
 {
     return ChangeProp(ImClone(this), im => im.RetentionTime = prop);
 }
Пример #17
0
        private static RetentionTimeRegression AutoCalcRegressions(IDocumentContainer container,
            RetentionTimeRegression rtRegression)
        {
            var document = container.Document;
            var dictSeqToPeptide = new Dictionary<string, PeptideDocNode>();
            foreach (var nodePep in document.Peptides)
            {
                if (nodePep.IsDecoy)
                    continue;

                string seqMod = document.Settings.GetSourceTextId(nodePep);
                if (!dictSeqToPeptide.ContainsKey(seqMod))
                    dictSeqToPeptide.Add(seqMod, nodePep);
            }
            int minCount = 0;
            try
            {
                var regressionPeps = rtRegression.Calculator.ChooseRegressionPeptides(dictSeqToPeptide.Keys, out minCount);
                var setRegression = new HashSet<string>(regressionPeps);
                dictSeqToPeptide = dictSeqToPeptide.Where(p => setRegression.Contains(p.Key))
                                                   .ToDictionary(p => p.Key, p => p.Value);
            }
            catch (IncompleteStandardException)
            {
                // Without a full set of regression peptides, no auto-calculation is possible
                dictSeqToPeptide.Clear();
            }

            var dictStandardPeptides = dictSeqToPeptide.ToDictionary(p => p.Value.Peptide.GlobalIndex, p => p.Value);

            // Must have standard peptides, all with results
            if (dictSeqToPeptide.Count == 0)
                return rtRegression.ClearEquations();
            else if (dictSeqToPeptide.Values.Any(nodePep => !nodePep.HasResults))
                return rtRegression.ClearEquations(dictStandardPeptides);

            var calculator = rtRegression.Calculator;
            var dictSeqToScore = dictSeqToPeptide.ToDictionary(p => p.Key,
                p => calculator.ScoreSequence(p.Key) ?? calculator.UnknownScore);
            var dictFileIdToCorr = new Dictionary<int, IList<TimeScorePair>>();
            var listPepCorr = new List<TimeScorePair>();
            foreach (var seqToPeptide in dictSeqToPeptide)
            {
                var nodePep = seqToPeptide.Value;
                double? time = nodePep.SchedulingTime;
                if (!time.HasValue)
                    continue;
                double score = dictSeqToScore[seqToPeptide.Key];
                listPepCorr.Add(new TimeScorePair(time.Value, score));

                foreach (var fileId in nodePep.Results.Where(r => r != null)
                                                      .SelectMany(r => r)
                                                      .Select(chromInfo => chromInfo.FileId))
                {
                    IList<TimeScorePair> listTimeScores;
                    if (!dictFileIdToCorr.TryGetValue(fileId.GlobalIndex, out listTimeScores))
                        listTimeScores = dictFileIdToCorr[fileId.GlobalIndex] = new List<TimeScorePair>();
                    time = nodePep.GetSchedulingTime(fileId);
                    if (!time.HasValue)
                        continue;
                    listTimeScores.Add(new TimeScorePair(time.Value, score));
                }
            }

            // If not all standard peptides have at least some retention time value, fail prediction
            if (listPepCorr.Count != dictSeqToPeptide.Count)
                return rtRegression.ClearEquations(dictStandardPeptides);

            // Only calculate regressions for files with retention times for all of the standards
            var fileIdToConversions = from p in dictFileIdToCorr
                                       where p.Value.Count == dictSeqToPeptide.Count
                                       select new KeyValuePair<int, RegressionLine>(p.Key, CalcConversion(p.Value, minCount));

            return rtRegression.ChangeEquations(new RegressionLineElement(CalcConversion(listPepCorr, minCount)),
                                                fileIdToConversions,
                                                dictStandardPeptides);
        }
            private static float AddRegressionLabel(PaneBase graphPane, Graphics g, double score, double time,
                RetentionTimeRegression regression, RetentionTimeStatistics statistics, Color color)
            {
                string label;
                if (regression == null || regression.Conversion == null || statistics == null)
                {
                    label = String.Format("{0} = ?, {1} = ?\n" + "{2} = ?\n" + "r = ?", // Not L10N
                                          Resources.Regression_slope,
                                          Resources.Regression_intercept,
                                          Resources.GraphData_AddRegressionLabel_window);
                }
                else
                {
                    label = String.Format("{0} = {1:F02}, {2} = {3:F02}\n" + "{4} = {5:F01}\n" + "r = {6}",   // Not L10N
                                          Resources.Regression_slope,
                                          regression.Conversion.Slope,
                                          Resources.Regression_intercept,
                                          regression.Conversion.Intercept,
                                          Resources.GraphData_AddRegressionLabel_window,
                                          regression.TimeWindow,
                                          Math.Round(statistics.R, RetentionTimeRegression.ThresholdPrecision));
                }

                TextObj text = new TextObj(label, score, time,
                                           CoordType.AxisXYScale, AlignH.Left, AlignV.Top)
                                   {
                                       IsClippedToChartRect = true,
                                       ZOrder = ZOrder.E_BehindCurves,
                                       FontSpec = GraphSummary.CreateFontSpec(color),
                                   };
                graphPane.GraphObjList.Add(text);

                // Measure the text just added, and return its height
                SizeF sizeLabel = text.FontSpec.MeasureString(g, label, graphPane.CalcScaleFactor());
                return sizeLabel.Height + 3;
            }
Пример #19
0
 public void CalcRetentionTime(RetentionTimeRegression rtRegression)
 {
     double? rt = null;
     if (rtRegression != null)
     {
         double? score = rtRegression.Calculator.ScoreSequence(Sequence);
         if (score.HasValue)
             rt = rtRegression.Conversion.GetY(score.Value);
     }
     PredictedRetentionTime = rt;
 }
Пример #20
0
 public PeptidePrediction(RetentionTimeRegression retentionTime, DriftTimePredictor driftTimePredictor = null)
     : this(retentionTime, driftTimePredictor, true, DEFAULT_MEASURED_RT_WINDOW, false, null)
 {
 }
        public void OkDialog()
        {
            var helper = new MessageBoxHelper(this);
            double window;
            if (!helper.ValidateDecimalTextBox(txtWindow, out window))
                return;

            if (Settings.Default.RetentionTimeList.Any(regression => regression.Name == txtName.Text))
            {
                MessageDlg.Show(this, Resources.AddRetentionTimePredictorDlg_OkDialog_A_retention_time_predictor_with_that_name_already_exists__Please_choose_a_new_name_);
                txtName.Focus();
                return;
            }

            Regression = new RetentionTimeRegression(
                txtName.Text, Calculator, null, null, window, new List<MeasuredRetentionTime>());

            DialogResult = DialogResult.OK;
        }
Пример #22
0
        protected void TestUsePredictedTime()
        {
            const double FILTER_LENGTH = 2.7;
            RunUI(() => SkylineWindow.OpenFile(TestFilesDir.GetTestPath("RetentionTimeFilterTest.sky")));
            WaitForDocumentLoaded();
            RunUI(() => SkylineWindow.SaveDocument(TestFilesDir.GetTestPath("TestUsePredictedTime.sky")));
            SetUiDocument(ChangeFullScanSettings(SkylineWindow.Document, SkylineWindow.Document.Settings.TransitionSettings.FullScan
                .ChangeRetentionTimeFilter(RetentionTimeFilterType.scheduling_windows, FILTER_LENGTH)));
            Assert.IsNull(SkylineWindow.Document.Settings.PeptideSettings.Prediction.RetentionTime);
            Assert.IsFalse(SkylineWindow.Document.Settings.PeptideSettings.Prediction.UseMeasuredRTs);
            // When we try to import a file, we should get an error about not having a peptide prediction algorithm
            var messageDlg = ShowDialog<MessageDlg>(() => SkylineWindow.ImportResults());
            RunUI(() =>
            {
                Assert.AreEqual(Resources.SkylineWindow_CheckRetentionTimeFilter_NoPredictionAlgorithm, messageDlg.Message);
                messageDlg.Close();
            });

            var ssrCalcRegression = new RetentionTimeRegression("SSRCALC_FOR_RtFilterTest",
                new RetentionScoreCalculator(RetentionTimeRegression.SSRCALC_100_A), .63,
                5.8, 1.4, new MeasuredRetentionTime[0]);
            // Now give the document a prediction algorithm
            SetUiDocument(ChangePeptidePrediction(SkylineWindow.Document, SkylineWindow.Document.Settings.PeptideSettings.
                Prediction.ChangeRetentionTime(ssrCalcRegression)));
            // Now import two result files
            {
                var importResultsDlg = ShowDialog<ImportResultsDlg>(SkylineWindow.ImportResults);
                var openDataSourceDialog = ShowDialog<OpenDataSourceDialog>(importResultsDlg.OkDialog);
                RunUI(() =>
                {
                    openDataSourceDialog.SelectFile("200fmol" + extension);
                    openDataSourceDialog.SelectFile("20fmol" + extension);
                });
                OkDialog(openDataSourceDialog, openDataSourceDialog.Open);
            }
            WaitForResultsImport();
            {
                var document = WaitForDocumentLoaded();
                foreach (var chromatogramSet in document.Settings.MeasuredResults.Chromatograms)
                {
                    foreach (var tuple in LoadAllChromatograms(document, chromatogramSet))
                    {
                        var peptide = tuple.Item1;
                        if (!peptide.IsProteomic)
                            continue;
                        var transitionGroup = tuple.Item2;
                        var predictedRetentionTime = ssrCalcRegression.GetRetentionTime(
                            document.Settings.GetModifiedSequence(peptide.Peptide.Sequence,
                                transitionGroup.TransitionGroup.LabelType, peptide.ExplicitMods)).Value;
                        AssertChromatogramWindow(document, chromatogramSet,
                            predictedRetentionTime - FILTER_LENGTH,
                            predictedRetentionTime + FILTER_LENGTH, tuple.Item3);
                    }
                }
            }
            ChromatogramSet chromSetForScheduling = SkylineWindow.Document.Settings.MeasuredResults.Chromatograms[1];
            // Create a SrmDocument with just the one ChromatogramSet that we are going to use for scheduling, so that
            // we can assert later that the chromatogram windows are where this document says they should be.
            SrmDocument documentForScheduling =
                SkylineWindow.Document.ChangeMeasuredResults(
                    SkylineWindow.Document.Settings.MeasuredResults.ChangeChromatograms(new[] {chromSetForScheduling}));
            SetUiDocument(ChangePeptidePrediction(SkylineWindow.Document, SkylineWindow.Document.Settings.PeptideSettings
                .Prediction.ChangeUseMeasuredRTs(true).ChangeRetentionTime(null)));
            {
                var chooseSchedulingReplicatesDlg = ShowDialog<ChooseSchedulingReplicatesDlg>(SkylineWindow.ImportResults);
                // Choose a scheduling replicate (the one saved above)
                RunUI(() => Assert.IsTrue(chooseSchedulingReplicatesDlg.TrySetReplicateChecked(
                    chromSetForScheduling, true)));
                var importResultsDlg = ShowDialog<ImportResultsDlg>(chooseSchedulingReplicatesDlg.OkDialog);
                var openDataSourceDialog = ShowDialog<OpenDataSourceDialog>(importResultsDlg.OkDialog);
                RunUI(()=>openDataSourceDialog.SelectFile("40fmol" + extension));
                OkDialog(openDataSourceDialog, openDataSourceDialog.Open);
            }
            WaitForResultsImport();
            {
                var document = WaitForDocumentLoaded();
                var chromatogramSet = document.Settings.MeasuredResults.Chromatograms.First(cs => cs.Name == "40fmol");
                int countNull = 0;
                foreach (var tuple in LoadAllChromatograms(document, chromatogramSet))
                {
                    var prediction = new PeptidePrediction(null, null, true, 1, false, 0);
                    double windowRtIgnored;

                    var schedulingPeptide =
                        documentForScheduling.Molecules.First(pep => ReferenceEquals(pep.Peptide, tuple.Item1.Peptide));
                    var schedulingTransitionGroup = (TransitionGroupDocNode) schedulingPeptide.FindNode(tuple.Item2.TransitionGroup);
                    double? predictedRt = prediction.PredictRetentionTime(documentForScheduling,
                        schedulingPeptide,
                        schedulingTransitionGroup,
                        null, ExportSchedulingAlgorithm.Average, true, out windowRtIgnored);
                    if (!predictedRt.HasValue)
                    {
                        countNull++;
                        continue;
                    }
                    AssertChromatogramWindow(document, chromatogramSet, predictedRt.Value - FILTER_LENGTH, predictedRt.Value + FILTER_LENGTH, tuple.Item3);
                }
                Assert.AreEqual((TestSmallMolecules ? 1 : 0), countNull);
            }

            // Test using iRT with auto-calculated regression
            {
                const string calcName = "TestCalculator";
                const string regressionName = "TestCalculatorAutoCalcRegression";
                var peptideSettingsDlg = ShowDialog<PeptideSettingsUI>(SkylineWindow.ShowPeptideSettingsUI);
                var editIrtDlg = ShowDialog<EditIrtCalcDlg>(peptideSettingsDlg.AddCalculator);
                RunUI(() =>
                {
                    editIrtDlg.OpenDatabase(TestFilesDir.GetTestPath("RetentionTimeFilterTest.irtdb"));
                    editIrtDlg.CalcName = calcName;
                });

                SkylineWindow.BeginInvoke(new Action(editIrtDlg.OkDialog));
                var multiButtonMsgDlg = WaitForOpenForm<MultiButtonMsgDlg>();
                OkDialog(multiButtonMsgDlg, ()=>multiButtonMsgDlg.DialogResult = DialogResult.Yes);
                var editRtDlg = ShowDialog<EditRTDlg>(peptideSettingsDlg.AddRTRegression);
                RunUI(() =>
                {
                    editRtDlg.ChooseCalculator(calcName);
                    editRtDlg.SetAutoCalcRegression(true);
                    editRtDlg.SetRegressionName(regressionName);
                    editRtDlg.SetTimeWindow(1.0);
                });
                OkDialog(editRtDlg, editRtDlg.OkDialog);
                RunUI(() =>
                {
                    peptideSettingsDlg.ChooseRegression(regressionName);
                    peptideSettingsDlg.UseMeasuredRT(false);
                });
                OkDialog(peptideSettingsDlg, peptideSettingsDlg.OkDialog);
                Assert.IsFalse(SkylineWindow.Document.Settings.PeptideSettings.Prediction.UseMeasuredRTs);
                var importResultsDlg = ShowDialog<ImportResultsDlg>(SkylineWindow.ImportResults);
                var openDataSourceDialog = ShowDialog<OpenDataSourceDialog>(importResultsDlg.OkDialog);
                RunUI(() => openDataSourceDialog.SelectFile("8fmol" + extension));
                OkDialog(openDataSourceDialog, openDataSourceDialog.Open);
                WaitForResultsImport();
                var document = WaitForDocumentLoaded();
                var chromatogramSet = document.Settings.MeasuredResults.Chromatograms.First(cs => cs.Name == "8fmol");

                var regressionLine =
                    document.Settings.PeptideSettings.Prediction.RetentionTime.GetConversion(
                        chromatogramSet.MSDataFileInfos.First().FileId);
                var calculator = document.Settings.PeptideSettings.Prediction.RetentionTime.Calculator;
                double fullGradientStartTime;
                double fullGradientEndTime;
                using (var msDataFile = new MsDataFileImpl(TestFilesDir.GetTestPath("8fmol" + extension)))
                {
                    fullGradientStartTime = msDataFile.GetSpectrum(0).RetentionTime.Value;
                    fullGradientEndTime = msDataFile.GetSpectrum(msDataFile.SpectrumCount - 1).RetentionTime.Value;
                }

                foreach (var tuple in LoadAllChromatograms(document, chromatogramSet))
                {
                    if (tuple.Item1.GlobalStandardType != PeptideDocNode.STANDARD_TYPE_IRT)
                    {
                        double? score =
                            calculator.ScoreSequence(document.Settings.GetModifiedSequence(tuple.Item1.Peptide.Sequence,
                                tuple.Item2.TransitionGroup.LabelType, tuple.Item1.ExplicitMods));
                        if (score.HasValue)
                        {
                            double? predictedRt = regressionLine.GetY(score.Value);
                            AssertChromatogramWindow(document, chromatogramSet, predictedRt.Value - FILTER_LENGTH,
                                predictedRt.Value + FILTER_LENGTH, tuple.Item3);
                        }
                    }
                    else
                    {
                        // IRT Standards get extracted for the full gradient
                        AssertChromatogramWindow(document, chromatogramSet, fullGradientStartTime, fullGradientEndTime, tuple.Item3);
                    }
                }
            }
        }
 private double[] GetResiduals(RetentionTimeRegression regression, double[] scores, double[] times)
 {
     var residualsRefined = new double[times.Length];
     for (int i = 0; i < residualsRefined.Length; i++)
         residualsRefined[i] = GetResidual(regression, scores[i], times[i]);
     return residualsRefined;
 }
Пример #24
0
        public void OkDialog()
        {
            var helper = new MessageBoxHelper(this);

            string name;

            if (!helper.ValidateNameTextBox(textName, out name))
            {
                return;
            }

            if (_existing.Contains(r => !ReferenceEquals(_regression, r) && Equals(name, r.Name)))
            {
                helper.ShowTextBoxError(textName, Resources.EditRTDlg_OkDialog_The_retention_time_regression__0__already_exists, name);
                return;
            }

            double?slope     = null;
            double?intercept = null;

            if (!cbAutoCalc.Checked)
            {
                double slopeTmp;
                if (!helper.ValidateDecimalTextBox(textSlope, out slopeTmp))
                {
                    return;
                }
                slope = slopeTmp;

                double interceptTmp;
                if (!helper.ValidateDecimalTextBox(textIntercept, out interceptTmp))
                {
                    return;
                }
                intercept = interceptTmp;
            }

            double window;

            if (!helper.ValidateDecimalTextBox(textTimeWindow, out window))
            {
                return;
            }

            if (window <= 0)
            {
                helper.ShowTextBoxError(textTimeWindow, Resources.EditRTDlg_OkDialog__0__must_be_greater_than_0);
                return;
            }

            if (comboCalculator.SelectedIndex == -1)
            {
                MessageBox.Show(this, Resources.EditRTDlg_OkDialog_Retention_time_prediction_requires_a_calculator_algorithm,
                                Program.Name);
                comboCalculator.Focus();
                return;
            }
            var calculator = _driverCalculators.SelectedItem;

            RetentionTimeRegression regression =
                new RetentionTimeRegression(name, calculator, slope, intercept, window, GetTablePeptides());

            _regression = regression;

            DialogResult = DialogResult.OK;
        }
Пример #25
0
        protected void TestUsePredictedTime()
        {
            const double FILTER_LENGTH = 2.7;

            RunUI(() => SkylineWindow.OpenFile(TestFilesDir.GetTestPath("RetentionTimeFilterTest.sky")));
            var docStart = WaitForDocumentLoaded();

            RunUI(() => SkylineWindow.SaveDocument(TestFilesDir.GetTestPath("TestUsePredictedTime.sky")));
            SetUiDocument(docStart.ChangeSettings(docStart.Settings.ChangeTransitionFullScan(f =>
                                                                                             f.ChangeRetentionTimeFilter(RetentionTimeFilterType.scheduling_windows, FILTER_LENGTH))));
            Assert.IsNull(SkylineWindow.Document.Settings.PeptideSettings.Prediction.RetentionTime);
            Assert.IsFalse(SkylineWindow.Document.Settings.PeptideSettings.Prediction.UseMeasuredRTs);
            // When we try to import a file, we should get an error about not having a peptide prediction algorithm
            var messageDlg = ShowDialog <MessageDlg>(() => SkylineWindow.ImportResults());

            RunUI(() =>
            {
                Assert.AreEqual(Resources.SkylineWindow_CheckRetentionTimeFilter_NoPredictionAlgorithm, messageDlg.Message);
                messageDlg.Close();
            });

            var ssrCalcRegression = new RetentionTimeRegression("SSRCALC_FOR_RtFilterTest",
                                                                new RetentionScoreCalculator(RetentionTimeRegression.SSRCALC_100_A), .63,
                                                                5.8, 1.4, new MeasuredRetentionTime[0]);
            // Now give the document a prediction algorithm
            var docBeforeImport = SkylineWindow.Document;

            docBeforeImport = docBeforeImport.ChangeSettings(docBeforeImport.Settings.ChangePeptidePrediction(p =>
                                                                                                              p.ChangeRetentionTime(ssrCalcRegression)));
            SetUiDocument(docBeforeImport);
            // Now import two result files
            {
                var importResultsDlg     = ShowDialog <ImportResultsDlg>(SkylineWindow.ImportResults);
                var openDataSourceDialog = ShowDialog <OpenDataSourceDialog>(importResultsDlg.OkDialog);
                RunUI(() =>
                {
                    openDataSourceDialog.SelectFile("200fmol" + extension);
                    openDataSourceDialog.SelectFile("20fmol" + extension);
                });
                OkDialog(openDataSourceDialog, openDataSourceDialog.Open);
            }
            {
                var document = WaitForDocumentChangeLoaded(docBeforeImport);
                foreach (var chromatogramSet in document.Settings.MeasuredResults.Chromatograms)
                {
                    foreach (var tuple in LoadAllChromatograms(document, chromatogramSet))
                    {
                        var peptide = tuple.Item1;
                        if (!peptide.IsProteomic)
                        {
                            continue;
                        }
                        var transitionGroup        = tuple.Item2;
                        var predictedRetentionTime = ssrCalcRegression.GetRetentionTime(
                            document.Settings.GetModifiedSequence(peptide.Peptide.Sequence,
                                                                  transitionGroup.TransitionGroup.LabelType, peptide.ExplicitMods)).Value;
                        AssertChromatogramWindow(document, chromatogramSet,
                                                 predictedRetentionTime - FILTER_LENGTH,
                                                 predictedRetentionTime + FILTER_LENGTH, tuple.Item3);
                    }
                }
            }
            ChromatogramSet chromSetForScheduling = SkylineWindow.Document.Settings.MeasuredResults.Chromatograms[1];
            // Create a SrmDocument with just the one ChromatogramSet that we are going to use for scheduling, so that
            // we can assert later that the chromatogram windows are where this document says they should be.
            var docForScheduling = SkylineWindow.Document.ChangeMeasuredResults(
                SkylineWindow.Document.Settings.MeasuredResults.ChangeChromatograms(new[] { chromSetForScheduling }));

            docForScheduling = docForScheduling.ChangeSettings(docForScheduling.Settings.ChangePeptidePrediction(p =>
                                                                                                                 p.ChangeUseMeasuredRTs(true).ChangeRetentionTime(null)));
            SetUiDocument(docForScheduling);
            {
                var chooseSchedulingReplicatesDlg = ShowDialog <ChooseSchedulingReplicatesDlg>(SkylineWindow.ImportResults);
                // Choose a scheduling replicate (the one saved above)
                RunUI(() => Assert.IsTrue(chooseSchedulingReplicatesDlg.TrySetReplicateChecked(
                                              chromSetForScheduling, true)));
                var importResultsDlg     = ShowDialog <ImportResultsDlg>(chooseSchedulingReplicatesDlg.OkDialog);
                var openDataSourceDialog = ShowDialog <OpenDataSourceDialog>(importResultsDlg.OkDialog);
                RunUI(() => openDataSourceDialog.SelectFile("40fmol" + extension));
                OkDialog(openDataSourceDialog, openDataSourceDialog.Open);
            }
            {
                var document        = WaitForDocumentChangeLoaded(docForScheduling);
                var chromatogramSet = document.Settings.MeasuredResults.Chromatograms.First(cs => cs.Name == "40fmol");
                int countNull       = 0;
                foreach (var tuple in LoadAllChromatograms(document, chromatogramSet))
                {
                    var    prediction = new PeptidePrediction(null, null, true, 1, false, 0);
                    double windowRtIgnored;

                    var schedulingPeptide =
                        docForScheduling.Molecules.First(pep => ReferenceEquals(pep.Peptide, tuple.Item1.Peptide));
                    var    schedulingTransitionGroup = (TransitionGroupDocNode)schedulingPeptide.FindNode(tuple.Item2.TransitionGroup);
                    double?predictedRt = prediction.PredictRetentionTime(docForScheduling,
                                                                         schedulingPeptide,
                                                                         schedulingTransitionGroup,
                                                                         null, ExportSchedulingAlgorithm.Average, true, out windowRtIgnored);
                    if (!predictedRt.HasValue)
                    {
                        countNull++;
                        continue;
                    }
                    AssertChromatogramWindow(document, chromatogramSet, predictedRt.Value - FILTER_LENGTH, predictedRt.Value + FILTER_LENGTH, tuple.Item3);
                }
                Assert.AreEqual((TestSmallMolecules ? 1 : 0), countNull);
            }

            // Test using iRT with auto-calculated regression
            {
                const string calcName           = "TestCalculator";
                const string regressionName     = "TestCalculatorAutoCalcRegression";
                var          peptideSettingsDlg = ShowDialog <PeptideSettingsUI>(SkylineWindow.ShowPeptideSettingsUI);
                var          editIrtDlg         = ShowDialog <EditIrtCalcDlg>(peptideSettingsDlg.AddCalculator);
                RunUI(() =>
                {
                    editIrtDlg.OpenDatabase(TestFilesDir.GetTestPath("RetentionTimeFilterTest.irtdb"));
                    editIrtDlg.CalcName = calcName;
                });

                SkylineWindow.BeginInvoke(new Action(editIrtDlg.OkDialog));
                var multiButtonMsgDlg = WaitForOpenForm <MultiButtonMsgDlg>();
                OkDialog(multiButtonMsgDlg, () => multiButtonMsgDlg.DialogResult = DialogResult.Yes);
                var editRtDlg = ShowDialog <EditRTDlg>(peptideSettingsDlg.AddRTRegression);
                RunUI(() =>
                {
                    editRtDlg.ChooseCalculator(calcName);
                    editRtDlg.SetAutoCalcRegression(true);
                    editRtDlg.SetRegressionName(regressionName);
                    editRtDlg.SetTimeWindow(1.0);
                });
                OkDialog(editRtDlg, editRtDlg.OkDialog);
                RunUI(() =>
                {
                    peptideSettingsDlg.ChooseRegression(regressionName);
                    peptideSettingsDlg.UseMeasuredRT(false);
                });
                OkDialog(peptideSettingsDlg, peptideSettingsDlg.OkDialog);
                Assert.IsFalse(SkylineWindow.Document.Settings.PeptideSettings.Prediction.UseMeasuredRTs);
                docBeforeImport = SkylineWindow.Document;
                var importResultsDlg     = ShowDialog <ImportResultsDlg>(SkylineWindow.ImportResults);
                var openDataSourceDialog = ShowDialog <OpenDataSourceDialog>(importResultsDlg.OkDialog);
                RunUI(() => openDataSourceDialog.SelectFile("8fmol" + extension));
                OkDialog(openDataSourceDialog, openDataSourceDialog.Open);
                var document        = WaitForDocumentChangeLoaded(docBeforeImport);
                var chromatogramSet = document.Settings.MeasuredResults.Chromatograms.First(cs => cs.Name == "8fmol");

                var regressionLine =
                    document.Settings.PeptideSettings.Prediction.RetentionTime.GetConversion(
                        chromatogramSet.MSDataFileInfos.First().FileId);
                var    calculator = document.Settings.PeptideSettings.Prediction.RetentionTime.Calculator;
                double fullGradientStartTime;
                double fullGradientEndTime;
                using (var msDataFile = new MsDataFileImpl(TestFilesDir.GetTestPath("8fmol" + extension)))
                {
                    fullGradientStartTime = msDataFile.GetSpectrum(0).RetentionTime.Value;
                    fullGradientEndTime   = msDataFile.GetSpectrum(msDataFile.SpectrumCount - 1).RetentionTime.Value;
                }

                foreach (var tuple in LoadAllChromatograms(document, chromatogramSet))
                {
                    if (tuple.Item1.GlobalStandardType != PeptideDocNode.STANDARD_TYPE_IRT)
                    {
                        double?score =
                            calculator.ScoreSequence(document.Settings.GetModifiedSequence(tuple.Item1.Peptide.Sequence,
                                                                                           tuple.Item2.TransitionGroup.LabelType, tuple.Item1.ExplicitMods));
                        if (score.HasValue)
                        {
                            double?predictedRt = regressionLine.GetY(score.Value);
                            AssertChromatogramWindow(document, chromatogramSet, predictedRt.Value - FILTER_LENGTH,
                                                     predictedRt.Value + FILTER_LENGTH, tuple.Item3);
                        }
                    }
                    else
                    {
                        // IRT Standards get extracted for the full gradient
                        AssertChromatogramWindow(document, chromatogramSet, fullGradientStartTime, fullGradientEndTime, tuple.Item3);
                    }
                }
            }
        }
 private bool PointIsOverEx(RTLinearRegressionGraphPane graphPane, PointF point,
     RetentionTimeRegression regression, double x, double y)
 {
     if (regression != null && regression.IsUsable)
         y = GetResidual(regression, x, y);
     return graphPane.PointIsOver(point, x, y);
 }
Пример #27
0
        public void SettingsChangeNotDoc()
        {
            SrmDocument docFasta = CreateMixedDoc();
            SrmSettings settings = docFasta.Settings;

            // Change declustering potential, collision energy, and retention time
            var regressions = new DeclusterPotentialList();
            regressions.AddDefaults();
            var dpRegress = regressions["ABI"];
            var collisions = new CollisionEnergyList();
            collisions.AddDefaults();
            var ceRegress = collisions["ABI 4000 QTrap"];
            var calc = Settings.Default.RTScoreCalculatorList.GetDefaults().First();
            var rtRegress = new RetentionTimeRegression("Test", calc, 3.5, 10.4, 12.8,
                new MeasuredRetentionTime[0]);

            SrmSettings settings2 = settings.ChangePeptidePrediction(p => p.ChangeRetentionTime(rtRegress)).
                ChangeTransitionPrediction(p => p.ChangeCollisionEnergy(ceRegress).ChangeDeclusteringPotential(dpRegress));

            SrmDocument docFasta2 = docFasta.ChangeSettings(settings2);
            AssertEx.IsDocumentState(docFasta2, docFasta.RevisionIndex + 1, 3, 111, 352);
            Assert.AreSame(docFasta.Children, docFasta2.Children);
            Assert.AreNotEqual(docFasta.Settings, docFasta2.Settings);

            // Change auto-select toggles
            SrmSettings settings3 = settings.ChangePeptideFilter(f => f.ChangeAutoSelect(false)).
                ChangeTransitionFilter(f => f.ChangeAutoSelect(false));

            SrmDocument docFasta3 = docFasta.ChangeSettings(settings3);
            AssertEx.IsDocumentState(docFasta3, docFasta.RevisionIndex + 1, 3, 111, 352);
            Assert.AreSame(docFasta.Children, docFasta3.Children);
            Assert.AreNotEqual(docFasta.Settings, docFasta3.Settings);
        }
Пример #28
0
 public RegressionUnconversion(RetentionTimeRegression retentionTimeRegression)
 {
     _retentionTimeRegression = retentionTimeRegression;
 }
Пример #29
0
        public void OkDialog()
        {
            IrtType irtType = GetIrtType();

            if (textCalculatorName.Text.Length == 0)
            {
                MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_Calculator_name_cannot_be_empty);
                return;
            }
            if (_existing.Select(spec => spec.Name).Contains(textCalculatorName.Text))
            {
                var replaceResult = MultiButtonMsgDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_A_calculator_with_that_name_already_exists___Do_you_want_to_replace_it_,
                                                           MultiButtonMsgDlg.BUTTON_YES,
                                                           MultiButtonMsgDlg.BUTTON_NO,
                                                           false);
                if (replaceResult == DialogResult.No)
                {
                    return;
                }
            }
            if (irtType == IrtType.existing)
            {
                try
                {
                    if (!File.Exists(textOpenDatabase.Text))
                    {
                        MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_iRT_database_field_must_contain_a_path_to_a_valid_file_);
                        textOpenDatabase.Focus();
                        return;
                    }
                    var db = IrtDb.GetIrtDb(textOpenDatabase.Text, null);
                    if (db == null)
                    {
                        throw new DatabaseOpeningException(string.Format(Resources.CreateIrtCalculatorDlg_OkDialog_Cannot_read_the_database_file__0_, textOpenDatabase.Text));
                    }
                }
                catch (Exception x)
                {
                    MessageDlg.ShowWithException(this, string.Format(Resources.CreateIrtCalculatorDlg_OkDialog_Failed_to_open_the_database_file___0_, x.Message), x);
                    return;
                }
            }
            else if (irtType == IrtType.separate_list)
            {
                if (textNewDatabase.Text.Length == 0)
                {
                    MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_iRT_database_field_must_not_be_empty_);
                    textNewDatabase.Focus();
                    return;
                }
                if (!CreateIrtDatabase(textNewDatabase.Text))
                {
                    return;
                }
            }
            else
            {
                if (textNewDatabaseProteins.Text.Length == 0)
                {
                    MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_iRT_database_field_must_not_be_empty_);
                    textNewDatabaseProteins.Focus();
                    return;
                }
                if (comboBoxProteins.SelectedIndex == -1)
                {
                    MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_Please_select_a_protein_containing_the_list_of_standard_peptides_for_the_iRT_calculator_);
                    comboBoxProteins.Focus();
                    return;
                }
                if (!CreateIrtDatabase(textNewDatabaseProteins.Text))
                {
                    return;
                }
            }
            // Make a version of the document with the new calculator in it
            var databaseFileName = irtType == IrtType.existing ? textOpenDatabase.Text :
                                   irtType == IrtType.separate_list ? textNewDatabase.Text :
                                   textNewDatabaseProteins.Text;
            var calculator = new RCalcIrt(textCalculatorName.Text, databaseFileName);
            // CONSIDER: Probably can't use just a static default like 10 below
            var retentionTimeRegression = new RetentionTimeRegression(calculator.Name, calculator, null, null, RetentionTimeRegression.DEFAULT_WINDOW, new List <MeasuredRetentionTime>());
            var docNew = Document.ChangeSettings(Document.Settings.ChangePeptidePrediction(prediction =>
                                                                                           prediction.ChangeRetentionTime(retentionTimeRegression)));

            // Import transition list of standards, if applicable
            if (irtType == IrtType.separate_list)
            {
                try
                {
                    if (!File.Exists(textImportText.Text))
                    {
                        MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_Transition_list_field_must_contain_a_path_to_a_valid_file_);
                        return;
                    }
                    IdentityPath selectPath;
                    List <MeasuredRetentionTime>     irtPeptides;
                    List <TransitionImportErrorInfo> errorList;
                    var inputs = new MassListInputs(textImportText.Text);
                    docNew = docNew.ImportMassList(inputs, null, out selectPath, out irtPeptides, out _librarySpectra, out errorList);
                    if (errorList.Any())
                    {
                        throw new InvalidDataException(errorList[0].ErrorMessage);
                    }
                    _dbIrtPeptides = irtPeptides.Select(rt => new DbIrtPeptide(rt.PeptideSequence, rt.RetentionTime, true, TimeSource.scan)).ToList();
                    IrtFile        = textImportText.Text;
                }
                catch (Exception x)
                {
                    MessageDlg.ShowWithException(this, string.Format(Resources.CreateIrtCalculatorDlg_OkDialog_Error_reading_iRT_standards_transition_list___0_, x.Message), x);
                    return;
                }
            }
            else if (irtType == IrtType.protein)
            {
                PeptideGroupDocNode selectedGroup = comboBoxProteins.SelectedItem as PeptideGroupDocNode;
// ReSharper disable PossibleNullReferenceException
                _irtPeptideSequences = new HashSet <Target>(selectedGroup.Peptides.Select(pep => pep.ModifiedTarget));
// ReSharper restore PossibleNullReferenceException
            }
            Document     = docNew;
            DialogResult = DialogResult.OK;
        }
            public GraphData(SrmDocument document, GraphData dataPrevious,
                int resultIndex, double threshold, int? thresholdPrecision, bool refine, bool bestResult)
            {
                _document = document;
                _resultIndex = resultIndex;
                _bestResult = bestResult;
                _threshold = threshold;
                _thresholdPrecision = thresholdPrecision;
                _peptidesIndexes = new List<PeptideDocumentIndex>();
                _peptidesTimes = new List<MeasuredRetentionTime>();
                int index = -1;
                // CONSIDER: Retention time prediction for small molecules?
                foreach (var nodePeptide in document.Peptides)
                {
                    index++;
                    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();
            }
Пример #31
0
        private static RetentionTimeRegression AutoCalcRegressions(IDocumentContainer container,
                                                                   RetentionTimeRegression rtRegression)
        {
            var document         = container.Document;
            var dictSeqToPeptide = new Dictionary <string, PeptideDocNode>();

            foreach (var nodePep in document.Peptides)
            {
                if (nodePep.IsDecoy)
                {
                    continue;
                }

                string seqMod = document.Settings.GetSourceTextId(nodePep);
                if (!dictSeqToPeptide.ContainsKey(seqMod))
                {
                    dictSeqToPeptide.Add(seqMod, nodePep);
                }
            }
            int minCount = 0;

            try
            {
                var regressionPeps = rtRegression.Calculator.ChooseRegressionPeptides(dictSeqToPeptide.Keys, out minCount);
                var setRegression  = new HashSet <string>(regressionPeps);
                dictSeqToPeptide = dictSeqToPeptide.Where(p => setRegression.Contains(p.Key))
                                   .ToDictionary(p => p.Key, p => p.Value);
            }
            catch (IncompleteStandardException)
            {
                // Without a full set of regression peptides, no auto-calculation is possible
                dictSeqToPeptide.Clear();
            }

            var dictStandardPeptides = dictSeqToPeptide.ToDictionary(p => p.Value.Peptide.GlobalIndex, p => p.Value);

            // Must have standard peptides, all with results
            if (dictSeqToPeptide.Count == 0)
            {
                return(rtRegression.ClearEquations());
            }
            else if (dictSeqToPeptide.Values.Any(nodePep => !nodePep.HasResults))
            {
                return(rtRegression.ClearEquations(dictStandardPeptides));
            }

            var calculator     = rtRegression.Calculator;
            var dictSeqToScore = dictSeqToPeptide.ToDictionary(p => p.Key,
                                                               p => calculator.ScoreSequence(p.Key) ?? calculator.UnknownScore);
            var dictFileIdToCorr = new Dictionary <int, IList <TimeScorePair> >();
            var listPepCorr      = new List <TimeScorePair>();

            foreach (var seqToPeptide in dictSeqToPeptide)
            {
                var    nodePep = seqToPeptide.Value;
                double?time    = nodePep.SchedulingTime;
                if (!time.HasValue)
                {
                    continue;
                }
                double score = dictSeqToScore[seqToPeptide.Key];
                listPepCorr.Add(new TimeScorePair(time.Value, score));

                foreach (var fileId in nodePep.Results.Where(r => r != null)
                         .SelectMany(r => r)
                         .Select(chromInfo => chromInfo.FileId))
                {
                    IList <TimeScorePair> listTimeScores;
                    if (!dictFileIdToCorr.TryGetValue(fileId.GlobalIndex, out listTimeScores))
                    {
                        listTimeScores = dictFileIdToCorr[fileId.GlobalIndex] = new List <TimeScorePair>();
                    }
                    time = nodePep.GetSchedulingTime(fileId);
                    if (!time.HasValue)
                    {
                        continue;
                    }
                    listTimeScores.Add(new TimeScorePair(time.Value, score));
                }
            }

            // If not all standard peptides have at least some retention time value, fail prediction
            if (listPepCorr.Count != dictSeqToPeptide.Count)
            {
                return(rtRegression.ClearEquations(dictStandardPeptides));
            }

            // Only calculate regressions for files with retention times for all of the standards
            var fileIdToConversions = from p in dictFileIdToCorr
                                      where p.Value.Count == dictSeqToPeptide.Count
                                      select new KeyValuePair <int, RegressionLine>(p.Key, CalcConversion(p.Value, minCount));

            var line = CalcConversion(listPepCorr, minCount);

            return(line != null
                ? rtRegression.ChangeEquations(new RegressionLineElement(line), fileIdToConversions, dictStandardPeptides)
                : rtRegression.ChangeEquations(null, fileIdToConversions, dictStandardPeptides).ChangeInsufficientCorrelation(true));
        }
 private double GetResidual(RetentionTimeRegression regression, double score, double time)
 {
     return time - regression.Conversion.GetY(score);
 }
Пример #33
0
        public bool BuildLibrary(IProgressMonitor progress)
        {
            RetentionTimeRegression regr = null;
            var standardSpectra          = new List <SpectrumMzInfo>();

            if (IrtStandard != null && !ReferenceEquals(IrtStandard, IrtStandard.EMPTY))
            {
                // Align Prosit iRTs with iRT standard
                var standardPeptidesToAdd = SkylineWindow.ReadStandardPeptides(IrtStandard);

                if (standardPeptidesToAdd != null && standardPeptidesToAdd.Count > 0)
                {
                    // Get iRTs
                    var standardIRTMap = _rtModel.Predict(_prositClient, _document.Settings,
                                                          standardPeptidesToAdd.Select(p => (PrositRetentionTimeModel.PeptideDocNodeWrapper)p.NodePep).ToArray(),
                                                          CancellationToken.None);

                    var original = standardIRTMap.ToDictionary(p => p.Key.ModifiedTarget, p => p.Value);
                    var target   = IrtStandard.Peptides.ToDictionary(p => p.ModifiedTarget, p => p.Irt);

                    var aligned = AlignedRetentionTimes.AlignLibraryRetentionTimes(target, original, 0.0, RegressionMethodRT.linear,
                                                                                   CancellationToken.None);
                    regr = aligned.Regression;

                    // Get spectra
                    var standardMS = _intensityModel.PredictBatches(_prositClient, progress, _document.Settings,
                                                                    standardPeptidesToAdd.Select(p => p.WithNCE(_nce)).ToArray(),
                                                                    CancellationToken.None);

                    // Merge iRT and MS2 into SpecMzInfos
                    standardSpectra = standardMS.Spectra.Select(m => m.SpecMzInfo).ToList();
                    for (var i = 0; i < standardSpectra.Count; ++i)
                    {
                        if (standardIRTMap.TryGetValue(standardMS.Spectra[i].PeptidePrecursorNCE.NodePep, out var iRT))
                        {
                            standardSpectra[i].RetentionTime = iRT;
                        }
                    }
                }
            }

            // Predict fragment intensities
            PrositMS2Spectra ms = _intensityModel.PredictBatches(_prositClient, progress, _document.Settings,
                                                                 _peptides.Zip(_precursors,
                                                                               (pep, prec) =>
                                                                               new PrositIntensityModel.PeptidePrecursorNCE(pep, prec, _nce)).ToArray(),
                                                                 CancellationToken.None);

            var specMzInfo = ms.Spectra.Select(m => m.SpecMzInfo).ToList();

            // Predict iRTs for peptides
            var distinctPeps = _peptides.Select(p => (PrositRetentionTimeModel.PeptideDocNodeWrapper)p).Distinct(
                new SystemLinqExtensionMethods.FuncEqualityComparer <PrositRetentionTimeModel.PeptideDocNodeWrapper>(
                    (p1, p2) => p1.Node.ModifiedSequence == p2.Node.ModifiedSequence)).ToArray();
            var iRTMap = _rtModel.PredictBatches(_prositClient, progress, _document.Settings,
                                                 distinctPeps, CancellationToken.None);

            for (var i = 0; i < specMzInfo.Count; ++i)
            {
                if (iRTMap.TryGetValue(ms.Spectra[i].PeptidePrecursorNCE.NodePep, out var iRT))
                {
                    specMzInfo[i].RetentionTime = regr?.Conversion?.GetY(iRT) ?? iRT;
                }
            }

            // Build library
            var librarySpectra = SpectrumMzInfo.RemoveDuplicateSpectra(standardSpectra.Concat(specMzInfo).ToList());

            // Delete if already exists, no merging with Prosit
            var libraryExists = File.Exists(LibrarySpec.FilePath);

            if (libraryExists)
            {
                var replace = _replaceLibrary();
                if (!replace)
                {
                    return(false);
                }
                FileEx.SafeDelete(LibrarySpec.FilePath);
            }

            if (!librarySpectra.Any())
            {
                return(true);
            }

            // Build the library
            using (var blibDb = BlibDb.CreateBlibDb(LibrarySpec.FilePath))
            {
                var docLibrarySpec = new BiblioSpecLiteSpec(LibrarySpec.Name, LibrarySpec.FilePath);
                BiblioSpecLiteLibrary docLibraryNew = null;
                var docLibrarySpec2 = docLibrarySpec;

                docLibraryNew =
                    blibDb.CreateLibraryFromSpectra(docLibrarySpec2, librarySpectra, LibrarySpec.Name, progress);
                if (docLibraryNew == null)
                {
                    return(false);
                }
            }

            return(true);
        }
            private GraphData RefineCloned(double threshold, int?precision, Func <bool> isCanceled)
            {
                // Create list of deltas between predicted and measured times
                _outlierIndexes = new HashSet <int>();
                // Start with anything assigned a zero retention time as outliers
                for (int i = 0; i < _peptidesTimes.Count; i++)
                {
                    if (_peptidesTimes[i].RetentionTime == 0)
                    {
                        _outlierIndexes.Add(i);
                    }
                }

                // Now that we have added iRT calculators, RecalcRegression
                // cannot go and mark as outliers peptides at will anymore. It must know which peptides, if any,
                // are required by the calculator for a regression. With iRT calcs, the standard is required.
                if (!_calculator.IsUsable)
                {
                    return(null);
                }

                HashSet <string> standardNames;

                try
                {
                    var names = _calculator.GetStandardPeptides(_peptidesTimes.Select(pep => pep.PeptideSequence));
                    standardNames = new HashSet <string>(names);
                }
                catch (CalculatorException)
                {
                    standardNames = new HashSet <string>();
                }

                var standardPeptides = _peptidesTimes.Where(pep => standardNames.Contains(pep.PeptideSequence)).ToArray();
                var variablePeptides = _peptidesTimes.Where(pep => !standardNames.Contains(pep.PeptideSequence)).ToArray();

                //Throws DatabaseNotConnectedException
                _regressionRefined = (_regressionAll == null
                                          ? null
                                          : _regressionAll.FindThreshold(threshold,
                                                                         precision,
                                                                         0,
                                                                         variablePeptides.Length,
                                                                         standardPeptides,
                                                                         variablePeptides,
                                                                         _statisticsAll,
                                                                         _calculator,
                                                                         _scoreCache,
                                                                         isCanceled,
                                                                         ref _statisticsRefined,
                                                                         ref _outlierIndexes));

                if (ReferenceEquals(_regressionRefined, _regressionAll))
                {
                    return(null);
                }

                // Separate lists into acceptable and outliers
                var listScoresRefined  = new List <double>();
                var listTimesRefined   = new List <double>();
                var listScoresOutliers = new List <double>();
                var listTimesOutliers  = new List <double>();

                for (int i = 0; i < _scoresRefined.Length; i++)
                {
                    if (_outlierIndexes.Contains(i))
                    {
                        listScoresOutliers.Add(_scoresRefined[i]);
                        listTimesOutliers.Add(_timesRefined[i]);
                    }
                    else
                    {
                        listScoresRefined.Add(_scoresRefined[i]);
                        listTimesRefined.Add(_timesRefined[i]);
                    }
                }
                _scoresRefined  = listScoresRefined.ToArray();
                _timesRefined   = listTimesRefined.ToArray();
                _scoresOutliers = listScoresOutliers.ToArray();
                _timesOutliers  = listTimesOutliers.ToArray();

                return(this);
            }
Пример #35
0
 public void OkDialog()
 {
     IrtType irtType = GetIrtType();
     if (textCalculatorName.Text.Length == 0)
     {
         MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_Calculator_name_cannot_be_empty);
         return;
     }
     if (_existing.Select(spec => spec.Name).Contains(textCalculatorName.Text))
     {
         var replaceResult = MultiButtonMsgDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_A_calculator_with_that_name_already_exists___Do_you_want_to_replace_it_,
                                MultiButtonMsgDlg.BUTTON_YES,
                                MultiButtonMsgDlg.BUTTON_NO,
                                false);
         if (replaceResult == DialogResult.No)
             return;
     }
     if (irtType == IrtType.existing)
     {
         try
         {
             if (!File.Exists(textOpenDatabase.Text))
             {
                 MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_iRT_database_field_must_contain_a_path_to_a_valid_file_);
                 textOpenDatabase.Focus();
                 return;
             }
             var db = IrtDb.GetIrtDb(textOpenDatabase.Text, null);
             if (db == null)
             {
                 throw new DatabaseOpeningException(string.Format(Resources.CreateIrtCalculatorDlg_OkDialog_Cannot_read_the_database_file__0_, textOpenDatabase.Text));
             }
         }
         catch (Exception x)
         {
             MessageDlg.ShowWithException(this, string.Format(Resources.CreateIrtCalculatorDlg_OkDialog_Failed_to_open_the_database_file___0_, x.Message), x);
             return;
         }
     }
     else if (irtType == IrtType.separate_list)
     {
         if (textNewDatabase.Text.Length == 0)
         {
             MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_iRT_database_field_must_not_be_empty_);
             textNewDatabase.Focus();
             return;
         }
         if (!CreateIrtDatabase(textNewDatabase.Text))
             return;
     }
     else
     {
         if (textNewDatabaseProteins.Text.Length == 0)
         {
             MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_iRT_database_field_must_not_be_empty_);
             textNewDatabaseProteins.Focus();
             return;
         }
         if (comboBoxProteins.SelectedIndex == -1)
         {
             MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_Please_select_a_protein_containing_the_list_of_standard_peptides_for_the_iRT_calculator_);
             comboBoxProteins.Focus();
             return;
         }
         if (!CreateIrtDatabase(textNewDatabaseProteins.Text))
             return;
     }
     // Make a version of the document with the new calculator in it
     var databaseFileName = irtType == IrtType.existing ? textOpenDatabase.Text :
                            irtType == IrtType.separate_list ? textNewDatabase.Text :
                             textNewDatabaseProteins.Text;
     var calculator = new RCalcIrt(textCalculatorName.Text, databaseFileName);
     // CONSIDER: Probably can't use just a static default like 10 below
     var retentionTimeRegression = new RetentionTimeRegression(calculator.Name, calculator, null, null, 10, new List<MeasuredRetentionTime>());
     var docNew = Document.ChangeSettings(Document.Settings.ChangePeptidePrediction(prediction =>
         prediction.ChangeRetentionTime(retentionTimeRegression)));
     // Import transition list of standards, if applicable
     if (irtType == IrtType.separate_list)
     {
         try
         {
             if (!File.Exists(textImportText.Text))
             {
                 MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_Transition_list_field_must_contain_a_path_to_a_valid_file_);
                 return;
             }
             IdentityPath selectPath;
             List<MeasuredRetentionTime> irtPeptides;
             List<TransitionImportErrorInfo> errorList;
             var inputs = new MassListInputs(textImportText.Text);
             docNew = docNew.ImportMassList(inputs, null, out selectPath, out irtPeptides, out _librarySpectra, out errorList);
             if (errorList.Any())
             {
                 throw new InvalidDataException(errorList[0].ErrorMessage);
             }
             _dbIrtPeptides = irtPeptides.Select(rt => new DbIrtPeptide(rt.PeptideSequence, rt.RetentionTime, true, TimeSource.scan)).ToList();
             IrtFile = textImportText.Text;
         }
         catch (Exception x)
         {
             MessageDlg.ShowWithException(this, string.Format(Resources.CreateIrtCalculatorDlg_OkDialog_Error_reading_iRT_standards_transition_list___0_, x.Message), x);
             return;
         }
     }
     else if (irtType == IrtType.protein)
     {
         PeptideGroupDocNode selectedGroup = comboBoxProteins.SelectedItem as PeptideGroupDocNode;
     // ReSharper disable PossibleNullReferenceException
         _irtPeptideSequences = new HashSet<string>(selectedGroup.Peptides.Select(pep => pep.ModifiedSequence));
     // ReSharper restore PossibleNullReferenceException
     }
     Document = docNew;
     DialogResult = DialogResult.OK;
 }
            private GraphData RefineCloned(double threshold, int? precision, Func<bool> isCanceled)
            {
                // Create list of deltas between predicted and measured times
                _outlierIndexes = new HashSet<int>();
                // Start with anything assigned a zero retention time as outliers
                for (int i = 0; i < _peptidesTimes.Count; i++)
                {
                    if (_peptidesTimes[i].RetentionTime == 0)
                        _outlierIndexes.Add(i);
                }

                // Now that we have added iRT calculators, RecalcRegression
                // cannot go and mark as outliers peptides at will anymore. It must know which peptides, if any,
                // are required by the calculator for a regression. With iRT calcs, the standard is required.
                if(!_calculator.IsUsable)
                    return null;

                HashSet<string> standardNames;
                try
                {
                    var names = _calculator.GetStandardPeptides(_peptidesTimes.Select(pep => pep.PeptideSequence));
                    standardNames = new HashSet<string>(names);
                }
                catch (CalculatorException)
                {
                    standardNames = new HashSet<string>();
                }

                var standardPeptides = _peptidesTimes.Where(pep => standardNames.Contains(pep.PeptideSequence)).ToArray();
                var variablePeptides = _peptidesTimes.Where(pep => !standardNames.Contains(pep.PeptideSequence)).ToArray();

                //Throws DatabaseNotConnectedException
                _regressionRefined = (_regressionAll == null
                                          ? null
                                          : _regressionAll.FindThreshold(threshold,
                                                                         precision,
                                                                         0,
                                                                         variablePeptides.Length,
                                                                         standardPeptides,
                                                                         variablePeptides,
                                                                         _statisticsAll,
                                                                         _calculator,
                                                                         _scoreCache,
                                                                         isCanceled,
                                                                         ref _statisticsRefined,
                                                                         ref _outlierIndexes));

                if (ReferenceEquals(_regressionRefined, _regressionAll))
                    return null;

                // Separate lists into acceptable and outliers
                var listScoresRefined = new List<double>();
                var listTimesRefined = new List<double>();
                var listScoresOutliers = new List<double>();
                var listTimesOutliers = new List<double>();
                for (int i = 0; i < _scoresRefined.Length; i++)
                {
                    if (_outlierIndexes.Contains(i))
                    {
                        listScoresOutliers.Add(_scoresRefined[i]);
                        listTimesOutliers.Add(_timesRefined[i]);
                    }
                    else
                    {
                        listScoresRefined.Add(_scoresRefined[i]);
                        listTimesRefined.Add(_timesRefined[i]);
                    }
                }
                _scoresRefined = listScoresRefined.ToArray();
                _timesRefined = listTimesRefined.ToArray();
                _scoresOutliers = listScoresOutliers.ToArray();
                _timesOutliers = listTimesOutliers.ToArray();

                return this;
            }
Пример #37
0
        private bool ImportTransitionList(CommandArgs commandArgs)
        {
            _out.WriteLine(Resources.CommandLine_ImportTransitionList_Importing_transiton_list__0____, Path.GetFileName(commandArgs.TransitionListPath));

            IdentityPath selectPath;
            List<MeasuredRetentionTime> irtPeptides;
            List<SpectrumMzInfo> librarySpectra;
            List<TransitionImportErrorInfo> errorList;
            List<PeptideGroupDocNode> peptideGroups;
            var retentionTimeRegression = _doc.Settings.PeptideSettings.Prediction.RetentionTime;
            RCalcIrt calcIrt = retentionTimeRegression != null ? (retentionTimeRegression.Calculator as RCalcIrt) : null;

            var progressMonitor = new CommandProgressMonitor(_out, new ProgressStatus(string.Empty));
            var inputs = new MassListInputs(commandArgs.TransitionListPath);
            var docNew = _doc.ImportMassList(inputs, progressMonitor, null,
                out selectPath, out irtPeptides, out librarySpectra, out errorList, out peptideGroups);

            // If nothing was imported (e.g. operation was canceled or zero error-free transitions) and also no errors, just return
            if (ReferenceEquals(docNew, _doc) && !errorList.Any())
                return true;
            // Show the errors or as warnings, if error transitions are ignore
            if (errorList.Any())
            {
                string messageFormat = !commandArgs.IsIgnoreTransitionErrors
                    ? Resources.CommandLine_ImportTransitionList_Error___line__0___column__1____2_
                    : Resources.CommandLine_ImportTransitionList_Warning___line__0___column__1____2_;
                foreach (var errorMessage in errorList)
                {
                    _out.WriteLine(messageFormat, errorMessage.Row, errorMessage.Column, errorMessage.ErrorMessage);
                }
                if (!commandArgs.IsIgnoreTransitionErrors)
                    return false;
            }
            if (!commandArgs.IsTransitionListAssayLibrary)
            {
                _doc = docNew;
                return true;
            }
            if (irtPeptides.Count == 0 || librarySpectra.Count == 0)
            {
                if (irtPeptides.Any())
                    _out.WriteLine(Resources.CommandLine_ImportTransitionList_Error__Imported_assay_library__0__lacks_ion_abundance_values_);
                else if (librarySpectra.Any())
                    _out.WriteLine(Resources.CommandLine_ImportTransitionList_Error__Imported_assay_library__0__lacks_iRT_values_);
                else
                    _out.WriteLine(Resources.CommandLine_ImportTransitionList_Error__Imported_assay_library__0__lacks_iRT_and_ion_abundance_values_);
                return false;
            }

            string destinationPath = commandArgs.SaveFile ?? commandArgs.SkylineFile;
            string documentLibrary = BiblioSpecLiteSpec.GetLibraryFileName(destinationPath);
            // ReSharper disable once AssignNullToNotNullAttribute
            string outputLibraryPath = Path.Combine(Path.GetDirectoryName(documentLibrary),
                Path.GetFileNameWithoutExtension(documentLibrary) + BiblioSpecLiteSpec.ASSAY_NAME +
                BiblioSpecLiteSpec.EXT);
            bool libraryExists = File.Exists(outputLibraryPath);
            string libraryName = Path.GetFileNameWithoutExtension(destinationPath) + BiblioSpecLiteSpec.ASSAY_NAME;
            int indexOldLibrary = docNew.Settings.PeptideSettings.Libraries.LibrarySpecs.IndexOf(
                    spec => spec != null && spec.FilePath == outputLibraryPath);
            bool libraryLinkedToDoc = indexOldLibrary != -1;
            if (libraryExists && !libraryLinkedToDoc)
            {
                _out.WriteLine(Resources.CommandLine_ImportTransitionList_Error__There_is_an_existing_library_with_the_same_name__0__as_the_document_library_to_be_created_,
                        libraryName);
                return false;
            }

            var dbIrtPeptides = irtPeptides.Select(rt => new DbIrtPeptide(rt.PeptideSequence, rt.RetentionTime, false, TimeSource.scan)).ToList();
            var dbIrtPeptidesFilter = ImportAssayLibraryHelper.GetUnscoredIrtPeptides(dbIrtPeptides, calcIrt);
            // If there are no iRT peptides with different values than the database, don't import any iRT's
            bool checkPeptides = false;
            if (dbIrtPeptidesFilter.Any())
            {
                if (calcIrt == null)
                {
                    string irtDatabasePath = commandArgs.IrtDatabasePath;
                    if (string.IsNullOrEmpty(irtDatabasePath))
                        irtDatabasePath = Path.ChangeExtension(destinationPath, IrtDb.EXT);
                    if (!string.IsNullOrEmpty(commandArgs.IrtStandardsPath))
                    {
                        _out.WriteLine(Resources.CommandLine_ImportTransitionList_Importing_iRT_transition_list__0_, commandArgs.IrtStandardsPath);
                        var irtInputs = new MassListInputs(commandArgs.IrtStandardsPath);
                        try
                        {
                            List<SpectrumMzInfo> irtLibrarySpectra;
                            docNew = docNew.ImportMassList(irtInputs, null, out selectPath, out irtPeptides, out irtLibrarySpectra, out errorList);
                            if (errorList.Any())
                            {
                                throw new InvalidDataException(errorList[0].ErrorMessage);
                            }
                            librarySpectra.AddRange(irtLibrarySpectra);
                            dbIrtPeptidesFilter.AddRange(irtPeptides.Select(rt => new DbIrtPeptide(rt.PeptideSequence, rt.RetentionTime, true, TimeSource.scan)));
                        }
                        catch (Exception x)
                        {
                            _out.WriteLine(Resources.CommandLine_Run_Error__Failed_importing_the_file__0____1_, commandArgs.IrtStandardsPath, x.Message);
                            return false;
                        }
                        if (!CreateIrtDatabase(irtDatabasePath, commandArgs))
                            return false;
                    }
                    else if (!string.IsNullOrEmpty(commandArgs.IrtGroupName))
                    {
                        var nodeGroupIrt = docNew.PeptideGroups.FirstOrDefault(nodeGroup => nodeGroup.Name == commandArgs.IrtGroupName);
                        if (nodeGroupIrt == null)
                        {
                            _out.WriteLine(Resources.CommandLine_ImportTransitionList_Error__The_name__0__specified_with__1__was_not_found_in_the_imported_assay_library_,
                                commandArgs.IrtGroupName, CommandArgs.ArgText(CommandArgs.ARG_IRT_STANDARDS_GROUP_NAME));
                            return false;
                        }
                        var irtPeptideSequences = new HashSet<string>(nodeGroupIrt.Peptides.Select(pep => pep.ModifiedSequence));
                        dbIrtPeptidesFilter.ForEach(pep => pep.Standard = irtPeptideSequences.Contains(pep.PeptideModSeq));
                        if (!CreateIrtDatabase(irtDatabasePath, commandArgs))
                            return false;
                    }
                    else if (!File.Exists(irtDatabasePath))
                    {
                        _out.Write(Resources.CommandLine_ImportTransitionList_Error__To_create_the_iRT_database___0___for_this_assay_library__you_must_specify_the_iRT_standards_using_either_of_the_arguments__1__or__2_,
                            irtDatabasePath, CommandArgs.ArgText(CommandArgs.ARG_IRT_STANDARDS_GROUP_NAME), CommandArgs.ArgText(CommandArgs.ARG_IRT_STANDARDS_FILE));
                        return false;
                    }
                    else
                    {
                        checkPeptides = true;
                    }
                    string irtCalcName = commandArgs.IrtCalcName ?? Path.GetFileNameWithoutExtension(destinationPath);
                    calcIrt = new RCalcIrt(irtCalcName, irtDatabasePath);

                    retentionTimeRegression = new RetentionTimeRegression(calcIrt.Name, calcIrt, null, null, 10, new List<MeasuredRetentionTime>());
                    docNew = docNew.ChangeSettings(docNew.Settings.ChangePeptidePrediction(prediction =>
                        prediction.ChangeRetentionTime(retentionTimeRegression)));
                }
                string dbPath = calcIrt.DatabasePath;
                IrtDb db = IrtDb.GetIrtDb(dbPath, null);
                if (checkPeptides)
                {
                    var standards = docNew.Molecules.Where(m => db.IsStandard(m.RawTextId)).ToArray();
                    if (standards.Length != db.StandardPeptideCount)
                    {
                        _out.WriteLine(Resources.CommandLine_ImportTransitionList_Warning__The_document_is_missing_iRT_standards);
                        foreach (var rawTextId in db.StandardPeptides.Where(s => !standards.Contains(nodePep => s == nodePep.RawTextId)))
                        {
                            _out.WriteLine("    " + rawTextId); // Not L10N
                        }
                    }
                }
                var oldPeptides = db.GetPeptides().ToList();
                IList<DbIrtPeptide.Conflict> conflicts;
                dbIrtPeptidesFilter = DbIrtPeptide.MakeUnique(dbIrtPeptidesFilter);
                DbIrtPeptide.FindNonConflicts(oldPeptides, dbIrtPeptidesFilter, null, out conflicts);
                // Warn about peptides that are present in the import and already in the database
                foreach (var conflict in conflicts)
                {
                    _out.WriteLine(Resources.CommandLine_ImportTransitionList_Warning__The_iRT_calculator_already_contains__0__with_the_value__1___Ignoring__2_,
                        conflict.ExistingPeptide.PeptideModSeq, conflict.ExistingPeptide.Irt, conflict.NewPeptide.Irt);
                }

                _out.WriteLine(Resources.CommandLine_ImportTransitionList_Importing__0__iRT_values_into_the_iRT_calculator__1_, dbIrtPeptidesFilter.Count, calcIrt.Name);
                docNew = docNew.AddIrtPeptides(dbIrtPeptidesFilter, false, progressMonitor);
                if (docNew == null)
                    return false;
            }

            librarySpectra = SpectrumMzInfo.RemoveDuplicateSpectra(librarySpectra);

            if (libraryLinkedToDoc)
            {
                string oldName = docNew.Settings.PeptideSettings.Libraries.LibrarySpecs[indexOldLibrary].Name;
                var libraryOld = docNew.Settings.PeptideSettings.Libraries.GetLibrary(oldName);
                var additionalSpectra = SpectrumMzInfo.GetInfoFromLibrary(libraryOld);
                additionalSpectra = SpectrumMzInfo.RemoveDuplicateSpectra(additionalSpectra);

                librarySpectra = SpectrumMzInfo.MergeWithOverwrite(librarySpectra, additionalSpectra);

                foreach (var stream in libraryOld.ReadStreams)
                    stream.CloseStream();
            }

            if (librarySpectra.Any())
            {
                // Delete the existing library; either it's not tied to the document or we've already extracted the spectra
                _out.WriteLine(Resources.CommandLine_ImportTransitionList_Adding__0__spectra_to_the_library__1_, librarySpectra.Count, libraryName);
                if (libraryExists)
                {
                    FileEx.SafeDelete(outputLibraryPath);
                    FileEx.SafeDelete(Path.ChangeExtension(outputLibraryPath, BiblioSpecLiteSpec.EXT_REDUNDANT));
                }
                using (var blibDb = BlibDb.CreateBlibDb(outputLibraryPath))
                {
                    var docLibrarySpec = new BiblioSpecLiteSpec(libraryName, outputLibraryPath);
                    var docLibrary = blibDb.CreateLibraryFromSpectra(docLibrarySpec, librarySpectra, libraryName,
                        progressMonitor);
                    if (docLibrary == null)
                        return false;
                    var newSettings = docNew.Settings.ChangePeptideLibraries(
                        libs => libs.ChangeLibrary(docLibrary, docLibrarySpec, indexOldLibrary));
                    docNew = docNew.ChangeSettings(newSettings, new SrmSettingsChangeMonitor(progressMonitor,
                        Resources.SkylineWindow_ImportMassList_Finishing_up_import));
                }
            }
            _doc = docNew;
            return true;
        }
            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();
            }