Beispiel #1
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));
        }
Beispiel #2
0
        private void AlignDataRow(int index, CancellationToken cancellationToken)
        {
            var dataRow = _dataRows[index];

            if (dataRow.TargetTimes == null || dataRow.SourceTimes == null)
            {
                return;
            }
            Task.Factory.StartNew(
                () => {
                try
                {
                    return(AlignedRetentionTimes.AlignLibraryRetentionTimes(
                               dataRow.TargetTimes, dataRow.SourceTimes,
                               DocumentRetentionTimes.REFINEMENT_THRESHHOLD,
                               RegressionMethodRT.linear,
                               () => cancellationToken.IsCancellationRequested));
                }
                catch (OperationCanceledException operationCanceledException)
                {
                    throw new OperationCanceledException(operationCanceledException.Message, operationCanceledException, cancellationToken);
                }
            }, cancellationToken)
            .ContinueWith(alignedTimesTask => UpdateDataRow(index, alignedTimesTask), TaskScheduler.FromCurrentSynchronizationContext());
        }
Beispiel #3
0
        private void AlignDataRowAsync(DataRow dataRow, int index, CancellationToken cancellationToken)
        {
            try
            {
                var alignedTimes = AlignedRetentionTimes.AlignLibraryRetentionTimes(
                    dataRow.TargetTimes, dataRow.SourceTimes,
                    DocumentRetentionTimes.REFINEMENT_THRESHHOLD,
                    RegressionMethodRT.linear,
                    new CustomCancellationToken(cancellationToken));

                if (!cancellationToken.IsCancellationRequested)
                {
                    RunUI(() => UpdateDataRow(index, alignedTimes, cancellationToken));
                }
            }
            catch (OperationCanceledException operationCanceledException)
            {
                throw new OperationCanceledException(operationCanceledException.Message, operationCanceledException, cancellationToken);
            }
        }
Beispiel #4
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);
        }
        protected override void DoTest()
        {
            const string seqWithOneId  = "TFAEALR";
            const string seqWithTwoIds = "AADALLLK";

            SetPeptideSettings();
            SetTransitionSettings();
            InsertPeptides();
            RunUI(() => SkylineWindow.SaveDocument(TestFilesDir.GetTestPath("RetentionTimeAlignmentTest.sky")));
            ImportResultsFile("S_1.mzML");
            ImportResultsFile("S_10.mzML");
            WaitForDocumentLoaded();
            var document = SkylineWindow.Document;

            CollectionAssert.AreEqual(
                new[] { "S_1", "S_10" },
                document.Settings.MeasuredResults.Chromatograms.Select(c => c.Name).ToArray());
            var peptideWithOneId =
                document.Peptides.First(
                    peptideDocNode => seqWithOneId.Equals(peptideDocNode.Peptide.Sequence));
            var precursorWithOneId = peptideWithOneId.TransitionGroups.First();
            var peptideWithTwoIds  =
                document.Peptides.First(
                    peptideDocNode => seqWithTwoIds.Equals(peptideDocNode.Peptide.Sequence));
            var precursorWithTwoIds = peptideWithTwoIds.TransitionGroups.First();

            Assert.IsTrue(precursorWithOneId.Results[0][0].IsIdentified);
            Assert.IsFalse(precursorWithOneId.Results[1][0].Identified == PeakIdentification.TRUE);
            Assert.IsTrue(precursorWithOneId.Results[1][0].Identified == PeakIdentification.ALIGNED);
            Assert.IsTrue(precursorWithTwoIds.Results[0][0].IsIdentified);
            Assert.IsTrue(precursorWithTwoIds.Results[1][0].IsIdentified);

            var documentRetentionTimes = document.Settings.DocumentRetentionTimes;
            var alignedTo1             = documentRetentionTimes.FileAlignments.Find("S_1");
            var alignedTo10            = documentRetentionTimes.FileAlignments.Find("S_10");
            var af10To1 = alignedTo1.RetentionTimeAlignments.Find("S_10");
            var af1To10 = alignedTo10.RetentionTimeAlignments.Find("S_1");

            // Verify that the slopes and intercepts are reciprocals of each other.
            // We can only verify this with very coarse precision
            Assert.AreEqual(af10To1.RegressionLine.Slope, 1 / af1To10.RegressionLine.Slope, .03);
            Assert.AreEqual(af10To1.RegressionLine.Intercept, -af1To10.RegressionLine.Intercept * af10To1.RegressionLine.Slope, 1);

            var alignedRetentionTimes10To1 = AlignedRetentionTimes.AlignLibraryRetentionTimes(
                document.Settings.GetRetentionTimes("S_1").GetFirstRetentionTimes(),
                document.Settings.GetRetentionTimes("S_10").GetFirstRetentionTimes(),
                DocumentRetentionTimes.REFINEMENT_THRESHHOLD, () => false);
            var alignedRetentionTimes1To10 = AlignedRetentionTimes.AlignLibraryRetentionTimes(
                document.Settings.GetRetentionTimes("S_10").GetFirstRetentionTimes(),
                document.Settings.GetRetentionTimes("S_1").GetFirstRetentionTimes(),
                DocumentRetentionTimes.REFINEMENT_THRESHHOLD, () => false);

            Assert.AreEqual(af10To1.RegressionLine.Slope, alignedRetentionTimes10To1.RegressionRefined.Conversion.Slope);
            Assert.AreEqual(af10To1.RegressionLine.Intercept, alignedRetentionTimes10To1.RegressionRefined.Conversion.Intercept);
            Assert.AreEqual(af1To10.RegressionLine.Slope, alignedRetentionTimes1To10.RegressionRefined.Conversion.Slope);
            Assert.AreEqual(af1To10.RegressionLine.Intercept, alignedRetentionTimes1To10.RegressionRefined.Conversion.Intercept);


            // Verify that the generated chromatogram is of the expected length around the actual or aligned ID's
            var idTimes = document.Settings.GetRetentionTimes("S_1", seqWithOneId, peptideWithOneId.ExplicitMods);

            VerifyStartEndTime(document, peptideWithOneId, precursorWithOneId, 0,
                               idTimes.Min() - CHROMATOGRAM_WINDOW_LENGTH_MINUTES,
                               idTimes.Max() + CHROMATOGRAM_WINDOW_LENGTH_MINUTES);
            var alignedTimes = document.Settings.GetAllRetentionTimes("S_10", seqWithOneId, peptideWithOneId.ExplicitMods);

            Assert.AreEqual(0, document.Settings.GetRetentionTimes("S_10").GetRetentionTimes(seqWithOneId).Length);
            VerifyStartEndTime(document, peptideWithOneId, precursorWithOneId, 1,
                               alignedTimes.Min() - CHROMATOGRAM_WINDOW_LENGTH_MINUTES,
                               alignedTimes.Max() + CHROMATOGRAM_WINDOW_LENGTH_MINUTES);
            RunUI(() => SkylineWindow.ComboResults.SelectedIndex = 1);
            var alignmentForm = ShowDialog <AlignmentForm>(() => SkylineWindow.ShowRetentionTimeAlignmentForm());

            RunUI(() =>
            {
                var alignAgainstOptions = alignmentForm.ComboAlignAgainst.Items.Cast <object>()
                                          .Select(item => item.ToString())
                                          .ToArray();
                CollectionAssert.AreEqual(new[] { "S_1", "S_10" }, alignAgainstOptions);
                Assert.AreEqual("S_10", alignmentForm.ComboAlignAgainst.SelectedItem.ToString());
            });
            WaitForConditionUI(10000, () => alignmentForm.RegressionGraph.GraphPane.XAxis.Title.Text == string.Format(Resources.AlignmentForm_UpdateGraph_Time_from__0__, "S_1"));
            RunUI(() =>
            {
                var curves          = alignmentForm.RegressionGraph.GraphPane.CurveList;
                var outlierCurve    = curves.Find(curveItem => Resources.AlignmentForm_UpdateGraph_Outliers == curveItem.Label.Text);
                var goodPointsCurve = curves.Find(curveItem => curveItem.Label.Text == Resources.AlignmentForm_UpdateGraph_Peptides_Refined);
                Assert.AreEqual(alignedRetentionTimes1To10.OutlierIndexes.Count, outlierCurve.Points.Count);
                Assert.AreEqual(alignedRetentionTimes1To10.RegressionPointCount, outlierCurve.Points.Count + goodPointsCurve.Points.Count);
            });
            RunUI(alignmentForm.Close);
            RunUI(() => SkylineWindow.ComboResults.SelectedIndex = 0);
            var alignmentForm2 = ShowDialog <AlignmentForm>(() => SkylineWindow.ShowRetentionTimeAlignmentForm());

            RunUI(() => Assert.AreEqual("S_1", alignmentForm2.ComboAlignAgainst.SelectedItem.ToString()));
            RunUI(alignmentForm2.Close);
            RunUI(() => SkylineWindow.SaveDocument());
        }