Esempio n. 1
0
        public BiblioSpecLiteBuilder(string name, string outputPath, IList <string> inputFiles, IList <string> targetSequences = null)
        {
            LibrarySpec = new BiblioSpecLiteSpec(name, outputPath);

            InputFiles      = inputFiles;
            TargetSequences = targetSequences;
        }
        private SrmDocument ConnectLibrarySpecs(SrmDocument document, string documentPath)
        {
            string docLibFile = null;

            if (!string.IsNullOrEmpty(documentPath) && document.Settings.PeptideSettings.Libraries.HasDocumentLibrary)
            {
                docLibFile = BiblioSpecLiteSpec.GetLibraryFileName(documentPath);
                if (!File.Exists(docLibFile))
                {
                    Assert.Fail(Resources.CommandLine_ConnectLibrarySpecs_Error__Could_not_find_the_spectral_library__0__for_this_document_, docLibFile);
                }
            }

            var settings = document.Settings.ConnectLibrarySpecs((library, librarySpec) =>
            {
                string name = library != null ? library.Name : librarySpec.Name;
                LibrarySpec spec;
                if (Settings.Default.SpectralLibraryList.TryGetValue(name, out spec))
                {
                    if (File.Exists(spec.FilePath))
                    {
                        return(spec);
                    }
                }

                string fileName = library != null ? library.FileNameHint : Path.GetFileName(librarySpec.FilePath);
                if (fileName != null)
                {
                    // First look for the file name in the document directory
                    string pathLibrary = PathEx.FindExistingRelativeFile(documentPath, fileName);
                    if (pathLibrary != null)
                    {
                        return(CreateLibrarySpec(library, librarySpec, pathLibrary, true));
                    }
                    // In the user's default library directory
                    pathLibrary = Path.Combine(Settings.Default.LibraryDirectory, fileName);
                    if (File.Exists(pathLibrary))
                    {
                        return(CreateLibrarySpec(library, librarySpec, pathLibrary, false));
                    }
                }
                Assert.Fail(Resources.CommandLine_ConnectLibrarySpecs_Warning__Could_not_find_the_spectral_library__0_, name);
                return(CreateLibrarySpec(library, librarySpec, null, false));
            }, docLibFile);

            if (ReferenceEquals(settings, document.Settings))
            {
                return(document);
            }

            // If the libraries were moved to disconnected state, then avoid updating
            // the document tree for this change, or it will strip all the library
            // information off the document nodes.
            if (settings.PeptideSettings.Libraries.DisconnectedLibraries != null)
            {
                return(document.ChangeSettingsNoDiff(settings));
            }

            return(document.ChangeSettings(settings));
        }
Esempio n. 3
0
        public BiblioSpecLiteBuilder GetLibBuilder(SrmDocument doc, string docFilePath, bool includeAmbiguousMatches)
        {
            string outputPath = BiblioSpecLiteSpec.GetLibraryFileName(docFilePath);

            // Check to see if the library is already there, and if it is,
            // "Append" instead of "Create"
            bool libraryExists      = File.Exists(outputPath);
            var  libraryBuildAction = LibraryBuildAction.Create;

            if (libraryExists)
            {
                if (doc.Settings.HasDocumentLibrary)
                {
                    libraryBuildAction = LibraryBuildAction.Append;
                }
                else
                {
                    // If the document does not have a document library, then delete the one that we have found
                    // CONSIDER: it may be that user is trying to re-import, in which case this file is probably in use
                    FileEx.SafeDelete(outputPath);
                    FileEx.SafeDelete(Path.ChangeExtension(outputPath, BiblioSpecLiteSpec.EXT_REDUNDANT));
                }
            }

            string name = Path.GetFileNameWithoutExtension(docFilePath);

            return(new BiblioSpecLiteBuilder(name, outputPath, SearchFilenames)
            {
                Action = libraryBuildAction,
                KeepRedundant = true,
                CutOffScore = CutoffScore,
                Id = Helpers.MakeId(name),
                IncludeAmbiguousMatches = includeAmbiguousMatches
            });
        }
        public BiblioSpecLiteBuilder(string name, string outputPath, IList<string> inputFiles, IList<string> targetSequences = null)
        {
            LibrarySpec = new BiblioSpecLiteSpec(name, outputPath);

            InputFiles = inputFiles;
            TargetSequences = targetSequences;
        }
        private bool VerifyDocumentLibrary()
        {
            string docLibPath          = BiblioSpecLiteSpec.GetLibraryFileName(DocumentPath);
            string redundantDocLibPath = BiblioSpecLiteSpec.GetRedundantName(DocumentPath);
            var    librarySettings     = SkylineWindow.Document.Settings.PeptideSettings.Libraries;

            return(File.Exists(docLibPath) && File.Exists(redundantDocLibPath) && librarySettings.HasDocumentLibrary);
        }
Esempio n. 6
0
        public void FullScanPrecursorTransitionsTest()
        {
            TestFilesDir testFilesDir = new TestFilesDir(TestContext, @"Test\FullScanPrecursor.zip");

            string      docPath = testFilesDir.GetTestPath("FullScanPrecursor.sky");
            SrmDocument doc     = ResultsUtil.DeserializeDocument(docPath);

            AssertEx.IsDocumentState(doc, 0, 1, 4, 5, 52);
            using (var docContainer = new ResultsTestDocumentContainer(doc, docPath))
            {
                var mitoLibSpec = new BiblioSpecLiteSpec("mito2", testFilesDir.GetTestPath("mito2.blib"));
                doc = docContainer.ChangeLibSpecs(new[] { mitoLibSpec });
                Assert.IsTrue(doc.IsLoaded);

                // Switch to only precursor ions
                var docPrecOnly = doc.ChangeSettings(doc.Settings.ChangeTransitionFilter(filter =>
                                                                                         filter.ChangePeptideIonTypes(new[] { IonType.precursor })));
                // All precursors should have 3 precursor transitions (M, M+1 and M+2)
                AssertEx.IsDocumentState(docPrecOnly, 3, 1, 4, 5, 15);
                Assert.IsFalse(docPrecOnly.PeptideTransitions.Any(nodeTran => nodeTran.Transition.IonType != IonType.precursor));

                // Use low resolution MS1 filtering
                var docLowMs1 = docPrecOnly.ChangeSettings(docPrecOnly.Settings.ChangeTransitionFullScan(fs =>
                                                                                                         fs.ChangePrecursorIsotopes(FullScanPrecursorIsotopes.Count, 1, null)
                                                                                                         .ChangePrecursorResolution(FullScanMassAnalyzerType.qit, 0.5, null)));
                // All precursors should have one precursor transition
                AssertEx.IsDocumentState(docLowMs1, 4, 1, 4, 5, 5);

                // Add y-ions to low resolution filtering
                var docLowMs1Y = docLowMs1.ChangeSettings(docLowMs1.Settings.ChangeTransitionFilter(filter =>
                                                                                                    filter.ChangePeptideIonTypes(new[] { IonType.precursor, IonType.y })));
                AssertEx.IsDocumentState(docLowMs1Y, 5, 1, 4, 5, 33);

                // Turn off MS1 filtering
                var docNoMs1 = docPrecOnly.ChangeSettings(docPrecOnly.Settings.ChangeTransitionFullScan(fs =>
                                                                                                        fs.ChangePrecursorIsotopes(FullScanPrecursorIsotopes.None, null, null)));
                // One of the precursors should have no transitions, since its spectrum has no precursor match
                AssertEx.IsDocumentState(docNoMs1, 4, 1, 4, 5, 4);

                // Turn off MS/MS library matching
                var docNoLibMatch = docNoMs1.ChangeSettings(docNoMs1.Settings.ChangeTransitionLibraries(lib =>
                                                                                                        lib.ChangePick(TransitionLibraryPick.none)));
                // All precursors should have a single precursor transition
                AssertEx.IsDocumentState(docNoLibMatch, 5, 1, 4, 5, 5);

                // Use library plus filter matching
                var docLibPlusMatch = docNoMs1.ChangeSettings(docNoMs1.Settings.ChangeTransitionLibraries(lib =>
                                                                                                          lib.ChangePick(TransitionLibraryPick.all_plus)));
                // All precursors should have a single precursor transition
                AssertEx.IsDocumentState(docLibPlusMatch, 5, 1, 4, 5, 5);

                // Release the library stream, and dispose of the directory
                docContainer.ChangeLibSpecs(new LibrarySpec[0]);
            }
            testFilesDir.Dispose();
        }
        public void TestBlibDriftTimes()
        {
            var testFilesDir = new TestFilesDir(TestContext, @"Test\Results\BlibDriftTimeTest.zip");
            // Open document with some peptides but no results
            var         docPath      = testFilesDir.GetTestPath("BlibDriftTimeTest.sky");
            SrmDocument docOriginal  = ResultsUtil.DeserializeDocument(docPath);
            var         docContainer = new ResultsTestDocumentContainer(docOriginal, docPath);
            var         doc          = docContainer.Document;

            // Use the bare drift times in the spectral library
            var librarySpec = new BiblioSpecLiteSpec("drift test",
                                                     testFilesDir.GetTestPath("BlibDriftTimeTest.blib"));

            doc = doc.ChangeSettings(
                doc.Settings.ChangePeptideLibraries(lib => lib.ChangeLibrarySpecs(new[] { librarySpec })).
                ChangePeptidePrediction(p => p.ChangeLibraryDriftTimesResolvingPower(20)).
                ChangePeptidePrediction(p => p.ChangeUseLibraryDriftTimes(true))
                );

            // Import an mz5 file that needs drift info that's in the original data set,
            // but preserved in the .blib file associated with a different raw source
            // Without the bugfix this won't get any drift time filtering.
            const string replicateName = "ID12692_01_UCA168_3727_040714";
            var          chromSets     = new[]
            {
                new ChromatogramSet(replicateName, new[]
                                    { new MsDataFilePath(testFilesDir.GetTestPath("ID12692_01_UCA168_3727_040714.mz5")), }),
            };
            var docResults = doc.ChangeMeasuredResults(new MeasuredResults(chromSets));

            Assert.IsTrue(docContainer.SetDocument(docResults, docOriginal, true));
            docContainer.AssertComplete();
            var document = docContainer.Document;

            float  tolerance = (float)document.Settings.TransitionSettings.Instrument.MzMatchTolerance;
            double maxHeight = 0;
            var    results   = document.Settings.MeasuredResults;

            Assert.AreEqual(2, document.PeptidePrecursorPairs.Count());
            var pair = document.PeptidePrecursorPairs.ToArray()[1];

            ChromatogramGroupInfo[] chromGroupInfo;
            Assert.IsTrue(results.TryLoadChromatogram(0, pair.NodePep, pair.NodeGroup,
                                                      tolerance, true, out chromGroupInfo));
            Assert.AreEqual(1, chromGroupInfo.Length);
            var chromGroup = chromGroupInfo[0];

            Assert.AreEqual(2, chromGroup.NumPeaks);  // This will be higher if we don't filter on DT
            foreach (var tranInfo in chromGroup.TransitionPointSets)
            {
                maxHeight = Math.Max(maxHeight, tranInfo.MaxIntensity);
            }
            Assert.AreEqual(278, maxHeight, 1);   // Without DT filtering, this will be much greater - about 996

            docContainer.Release();
        }
Esempio n. 8
0
        private void VerifyDocumentLibraryBuilt(string path)
        {
            // Verify document library was built
            string docLibPath          = BiblioSpecLiteSpec.GetLibraryFileName(GetTestPath(path));
            string redundantDocLibPath = BiblioSpecLiteSpec.GetRedundantName(docLibPath);

            Assert.IsTrue(File.Exists(docLibPath) && File.Exists(redundantDocLibPath));
            var librarySettings = SkylineWindow.Document.Settings.PeptideSettings.Libraries;

            Assert.IsTrue(librarySettings.HasDocumentLibrary);
        }
Esempio n. 9
0
 private void IncludeRedundantBlib(LibrarySpec librarySpec, ZipFileShare zip, string blibPath)
 {
     if (librarySpec is BiblioSpecLiteSpec)
     {
         var redundantBlibPath = BiblioSpecLiteSpec.GetRedundantName(blibPath);
         if (File.Exists(redundantBlibPath))
         {
             zip.AddFile(redundantBlibPath);
         }
     }
 }
Esempio n. 10
0
        public void FullScanPrecursorTransitionsTest()
        {
            TestFilesDir testFilesDir = new TestFilesDir(TestContext, @"TestA\FullScanPrecursor.zip");

            string docPath = testFilesDir.GetTestPath("FullScanPrecursor.sky");
            SrmDocument doc = ResultsUtil.DeserializeDocument(docPath);
            AssertEx.IsDocumentState(doc, 0, 1, 4, 5, 52);
            var docContainer = new ResultsTestDocumentContainer(doc, docPath);
            var mitoLibSpec = new BiblioSpecLiteSpec("mito2", testFilesDir.GetTestPath("mito2.blib"));
            doc = docContainer.ChangeLibSpecs(new [] { mitoLibSpec });
            Assert.IsTrue(doc.IsLoaded);

            // Switch to only precursor ions
            var docPrecOnly = doc.ChangeSettings(doc.Settings.ChangeTransitionFilter(filter =>
                filter.ChangeIonTypes(new[] { IonType.precursor })));
            // All precursors should have 3 precursor transitions (M, M+1 and M+2)
            AssertEx.IsDocumentState(docPrecOnly, 3, 1, 4, 5, 15);
            Assert.IsFalse(docPrecOnly.PeptideTransitions.Any(nodeTran => nodeTran.Transition.IonType != IonType.precursor));

            // Use low resolution MS1 filtering
            var docLowMs1 = docPrecOnly.ChangeSettings(docPrecOnly.Settings.ChangeTransitionFullScan(fs =>
                fs.ChangePrecursorIsotopes(FullScanPrecursorIsotopes.Count, 1, null)
                  .ChangePrecursorResolution(FullScanMassAnalyzerType.qit, 0.5, null)));
            // All precursors should have one precursor transition
            AssertEx.IsDocumentState(docLowMs1, 4, 1, 4, 5, 5);

            // Add y-ions to low resolution filtering
            var docLowMs1Y = docLowMs1.ChangeSettings(docLowMs1.Settings.ChangeTransitionFilter(filter =>
                filter.ChangeIonTypes(new[] { IonType.precursor, IonType.y })));
            AssertEx.IsDocumentState(docLowMs1Y, 5, 1, 4, 5, 33);

            // Turn off MS1 filtering
            var docNoMs1 = docPrecOnly.ChangeSettings(docPrecOnly.Settings.ChangeTransitionFullScan(fs =>
                fs.ChangePrecursorIsotopes(FullScanPrecursorIsotopes.None, null, null)));
            // One of the precursors should have no transitions, since its spectrum has no precursor match
            AssertEx.IsDocumentState(docNoMs1, 4, 1, 4, 5, 4);

            // Turn off MS/MS library matching
            var docNoLibMatch = docNoMs1.ChangeSettings(docNoMs1.Settings.ChangeTransitionLibraries(lib =>
                lib.ChangePick(TransitionLibraryPick.none)));
            // All precursors should have a single precursor transition
            AssertEx.IsDocumentState(docNoLibMatch, 5, 1, 4, 5, 5);

            // Use library plus filter matching
            var docLibPlusMatch = docNoMs1.ChangeSettings(docNoMs1.Settings.ChangeTransitionLibraries(lib =>
                lib.ChangePick(TransitionLibraryPick.all_plus)));
            // All precursors should have a single precursor transition
            AssertEx.IsDocumentState(docLibPlusMatch, 5, 1, 4, 5, 5);

            // Release the library stream, and dispose of the directory
            docContainer.ChangeLibSpecs(new LibrarySpec[0]);
            testFilesDir.Dispose();
        }
Esempio n. 11
0
        public BiblioSpecLiteBuilder(string name, string outputPath, IList <string> inputFiles, IList <Target> targetSequences = null)
        {
            LibrarySpec = new BiblioSpecLiteSpec(name, outputPath);

            InputFiles = inputFiles;

            // FUTURE(bspratt) small molecule workflows
            if (targetSequences != null)
            {
                TargetSequences = targetSequences.Where(t => t.IsProteomic).Select(t => t.Sequence).ToList();
            }
        }
Esempio n. 12
0
        public void TestBlibDriftTimes()
        {
            var testFilesDir = new TestFilesDir(TestContext, @"Test\Results\BlibDriftTimeTest.zip");
            // Open document with some peptides but no results
            var docPath = testFilesDir.GetTestPath("BlibDriftTimeTest.sky");
            SrmDocument docOriginal = ResultsUtil.DeserializeDocument(docPath);
            var docContainer = new ResultsTestDocumentContainer(docOriginal, docPath);
            var doc = docContainer.Document;

            // Use the bare drift times in the spectral library
            var librarySpec = new BiblioSpecLiteSpec("drift test",
                                                testFilesDir.GetTestPath("BlibDriftTimeTest.blib"));
            doc = doc.ChangeSettings(
                doc.Settings.ChangePeptideLibraries(lib => lib.ChangeLibrarySpecs(new[] { librarySpec })).
                ChangePeptidePrediction(p => p.ChangeLibraryDriftTimesResolvingPower(20)).
                ChangePeptidePrediction(p => p.ChangeUseLibraryDriftTimes(true))
                );

            // Import an mz5 file that needs drift info that's in the original data set,
            // but preserved in the .blib file associated with a different raw source
            // Without the bugfix this won't get any drift time filtering.
            const string replicateName = "ID12692_01_UCA168_3727_040714";
            var chromSets = new[]
                                {
                                    new ChromatogramSet(replicateName, new[]
                                        { new MsDataFilePath(testFilesDir.GetTestPath("ID12692_01_UCA168_3727_040714.mz5")),  }),
                                };
            var docResults = doc.ChangeMeasuredResults(new MeasuredResults(chromSets));
            Assert.IsTrue(docContainer.SetDocument(docResults, docOriginal, true));
            docContainer.AssertComplete();
            var document = docContainer.Document;

            float tolerance = (float)document.Settings.TransitionSettings.Instrument.MzMatchTolerance;
            double maxHeight = 0;
            var results = document.Settings.MeasuredResults;
            Assert.AreEqual(2, document.PeptidePrecursorPairs.Count());
            var pair = document.PeptidePrecursorPairs.ToArray()[1];
            ChromatogramGroupInfo[] chromGroupInfo;
            Assert.IsTrue(results.TryLoadChromatogram(0, pair.NodePep, pair.NodeGroup,
                tolerance, true, out chromGroupInfo));
            Assert.AreEqual(1, chromGroupInfo.Length);
            var chromGroup = chromGroupInfo[0];
            Assert.AreEqual(2 , chromGroup.NumPeaks); // This will be higher if we don't filter on DT
            foreach (var tranInfo in chromGroup.TransitionPointSets)
            {
                maxHeight = Math.Max(maxHeight, tranInfo.MaxIntensity);
            }
            Assert.AreEqual(278 , maxHeight, 1);  // Without DT filtering, this will be much greater - about 996

            docContainer.Release();
        }
Esempio n. 13
0
 public PrositLibraryBuilder(SrmDocument doc, string name, string outPath, Func <bool> replaceLibrary,
                             IrtStandard irtStandard, IList <PeptideDocNode> peptides, IList <TransitionGroupDocNode> precursors, int nce)
 {
     _prositClient   = PrositPredictionClient.Current;
     _intensityModel = PrositIntensityModel.Instance;
     _rtModel        = PrositRetentionTimeModel.Instance;
     _peptides       = peptides;
     _precursors     = precursors;
     _document       = doc;
     LibrarySpec     = new BiblioSpecLiteSpec(name, outPath); // Needs to be created before building
     _replaceLibrary = replaceLibrary;
     IrtStandard     = irtStandard;
     _nce            = nce;
 }
Esempio n. 14
0
        /// <summary>
        /// Added to show <see cref="SpectrumLibraryInfoDlg"/>
        /// </summary>
        private void TestLibraryInfo()
        {
            var libspec = new BiblioSpecLiteSpec("ManageLibraryRunsTestScores", TestFilesDir.GetTestPath("ManageLibraryRunsTestScores.blib"));
            var newLib  = BiblioSpecLiteLibrary.Load(libspec, new DefaultFileLoadMonitor(new SilentProgressMonitor()));

            AddLibrary(libspec, newLib);
            WaitForDocumentLoaded();

            var libExplore = ShowDialog <ViewLibraryDlg>(SkylineWindow.ViewSpectralLibraries);

            var specLibInfoDlg = ShowDialog <SpectrumLibraryInfoDlg>(libExplore.ShowLibDetails);

            IList <SpectrumSourceFileDetails> datafiles = specLibInfoDlg.GetGridView();

            Assert.AreEqual(datafiles.Count, 5);
            foreach (var file in datafiles)
            {
                Assert.AreEqual(0, file.BestSpectrum);
                Assert.AreEqual(0, file.MatchedSpectrum);
                Assert.AreEqual(file.CutoffScores.Count, 0);
            }

            OkDialog(specLibInfoDlg, specLibInfoDlg.OkDialog);

            var modDlg = ShowDialog <AddModificationsDlg>(() => libExplore.ChangeSelectedLibrary("ManageLibraryRunsTestScores"));

            OkDialog(modDlg, modDlg.OkDialog);
            WaitForConditionUI(() => libExplore.HasSelectedLibrary);
            specLibInfoDlg = ShowDialog <SpectrumLibraryInfoDlg>(libExplore.ShowLibDetails);
            datafiles      = specLibInfoDlg.GetGridView();
            Assert.AreEqual(datafiles.Count, 4);
            Assert.AreEqual(datafiles[0].CutoffScores.Count, 1);
            Assert.AreEqual(datafiles[0].MatchedSpectrum, 1);
            Assert.AreEqual(datafiles[0].BestSpectrum, 1);
            Assert.AreEqual(datafiles[1].CutoffScores.Count, 1);
            Assert.AreEqual(datafiles[1].MatchedSpectrum, 10);
            Assert.AreEqual(datafiles[1].BestSpectrum, 9);
            Assert.AreEqual(datafiles[1].CutoffScores.Count, 1);
            Assert.AreEqual(datafiles[2].MatchedSpectrum, 3);
            Assert.AreEqual(datafiles[2].BestSpectrum, 3);
            Assert.AreEqual(datafiles[3].MatchedSpectrum, 1);
            Assert.AreEqual(datafiles[3].BestSpectrum, 1);
            OkDialog(specLibInfoDlg, specLibInfoDlg.OkDialog);
            OkDialog(libExplore, libExplore.CancelDialog);
        }
Esempio n. 15
0
        private static double driftTimeMax = 13.799765403988133; // Known max drift time for this file - use to mimic resolving power logic for test purposes

        private static SrmDocument InitWatersImsMseDocument(TestFilesDir testFilesDir,
                                                            IonMobilityWindowWidthCalculator.IonMobilityPeakWidthType driftPeakWidthCalcType,
                                                            bool withDriftTimeFilter, bool withDriftTimePredictor,
                                                            out string docPath)
        {
            var skyFile = withDriftTimePredictor ? "single_with_driftinfo.sky" : "single_no_driftinfo.sky";

            docPath = testFilesDir.GetTestPath(skyFile);
            var cmdline = new CommandLine();

            Assert.IsTrue(cmdline.OpenSkyFile(docPath)); // Handles any path shifts in database files, like our .imdb file
            SrmDocument doc = cmdline.Document;

            // Cause library load and subsequent document update
            using (var docContainer = new ResultsTestDocumentContainer(null, docPath))
            {
                docContainer.SetDocument(doc, null, true);
                docContainer.AssertComplete();
                doc = docContainer.Document;

                double resolvingPower = 100; // Test was originally written with resolving power 100
                double widthAtDtMax   = 2 * driftTimeMax / resolvingPower;
                var    driftTimeWindowWidthCalculator = new IonMobilityWindowWidthCalculator(driftPeakWidthCalcType, resolvingPower, 0, widthAtDtMax);

                if (withDriftTimeFilter && !withDriftTimePredictor)
                {
                    // Use the bare drift times in the spectral library
                    var librarySpec = new BiblioSpecLiteSpec("drift test",
                                                             testFilesDir.GetTestPath("waters-mobility.filtered-scaled.blib"));
                    doc = doc.ChangeSettings(
                        doc.Settings.ChangePeptideLibraries(lib => lib.ChangeLibrarySpecs(new[] { librarySpec })).
                        ChangePeptidePrediction(p => p.ChangeLibraryDriftTimesWindowWidthCalculator(driftTimeWindowWidthCalculator)).
                        ChangePeptidePrediction(p => p.ChangeUseLibraryIonMobilityValues(true))
                        );
                }
                else if (withDriftTimeFilter)
                {
                    doc = doc.ChangeSettings(
                        doc.Settings.ChangePeptideSettings(ps => ps.ChangePrediction(
                                                               ps.Prediction.ChangeDriftTimePredictor(ps.Prediction.IonMobilityPredictor.ChangeDriftTimeWindowWidthCalculator(driftTimeWindowWidthCalculator)))));
                }
            }
            return(doc);
        }
Esempio n. 16
0
        public void ExportSpectralLibrary(string path, IProgressMonitor progressMonitor)
        {
            const string name    = "exported";
            var          spectra = new Dictionary <LibKey, SpectrumMzInfo>();

            foreach (var nodePepGroup in Document.MoleculeGroups)
            {
                foreach (var nodePep in nodePepGroup.Molecules)
                {
                    foreach (var nodeTranGroup in nodePep.TransitionGroups)
                    {
                        for (var i = 0; i < Document.Settings.MeasuredResults.Chromatograms.Count; i++)
                        {
                            ProcessTransitionGroup(spectra, nodePepGroup, nodePep, nodeTranGroup, i);
                        }
                    }
                }
            }

            var rCalcIrt = Document.Settings.HasRTPrediction
                ? Document.Settings.PeptideSettings.Prediction.RetentionTime.Calculator as RCalcIrt
                : null;
            IProgressStatus status = new ProgressStatus();

            if (rCalcIrt != null && progressMonitor != null)
            {
                progressMonitor.UpdateProgress(status = status.ChangeSegments(0, 2));
            }

            using (var blibDb = BlibDb.CreateBlibDb(path))
            {
                var libSpec = new BiblioSpecLiteSpec(name, path);
                blibDb.CreateLibraryFromSpectra(libSpec, spectra.Values.ToList(), name, progressMonitor, ref status);
            }

            if (rCalcIrt != null)
            {
                IrtDb.CreateIrtDb(path).AddPeptides(progressMonitor, rCalcIrt.GetDbIrtPeptides().ToList(), ref status);
            }
        }
Esempio n. 17
0
        /// <summary>
        /// Minimizes all libraries in a document to produce a new document with
        /// just the library information necessary for the spectra referenced by
        /// the nodes in the document.
        /// </summary>
        /// <param name="document">Document for which to minimize library information</param>
        /// <param name="pathDirectory">Directory into which new minimized libraries are built</param>
        /// <param name="nameModifier">A name modifier to append to existing names for
        ///     full libraries to create new library names</param>
        /// <param name="progressMonitor">Broker to communicate status and progress</param>
        /// <returns>A new document instance with minimized libraries</returns>
        public static SrmDocument MinimizeLibraries(SrmDocument document,
            string pathDirectory, string nameModifier, IProgressMonitor progressMonitor)
        {
            var settings = document.Settings;
            var pepLibraries = settings.PeptideSettings.Libraries;
            if (!pepLibraries.HasLibraries)
                return document;
            if (!pepLibraries.IsLoaded)
                throw new InvalidOperationException(Resources.BlibDb_MinimizeLibraries_Libraries_must_be_fully_loaded_before_they_can_be_minimzed);

            // Separate group nodes by the libraries to which they refer
            var setUsedLibrarySpecs = new HashSet<LibrarySpec>();
            foreach (var librarySpec in pepLibraries.LibrarySpecs)
            {
                string libraryName = librarySpec.Name;
                if (document.MoleculeTransitionGroups.Contains(nodeGroup =>
                        nodeGroup.HasLibInfo && Equals(nodeGroup.LibInfo.LibraryName, libraryName)))
                {
                    setUsedLibrarySpecs.Add(librarySpec);
                }
            }

            var listLibraries = new List<Library>();
            var listLibrarySpecs = new List<LibrarySpec>();
            var dictOldNameToNew = new Dictionary<string, string>();
            if (setUsedLibrarySpecs.Count > 0)
            {
                Directory.CreateDirectory(pathDirectory);

                var usedNames = new HashSet<string>();
                for (int i = 0; i < pepLibraries.LibrarySpecs.Count; i++)
                {
                    var librarySpec = pepLibraries.LibrarySpecs[i];
                    if (!setUsedLibrarySpecs.Contains(librarySpec))
                        continue;

                    string baseName = Path.GetFileNameWithoutExtension(librarySpec.FilePath);
                    string fileName = GetUniqueName(baseName, usedNames) + BiblioSpecLiteSpec.EXT;
                    using (var blibDb = CreateBlibDb(Path.Combine(pathDirectory, fileName)))
                    {
                        blibDb.ProgressMonitor = progressMonitor;
                        var librarySpecMin = librarySpec as BiblioSpecLiteSpec;
                        if (librarySpecMin == null || !librarySpecMin.IsDocumentLibrary)
                        {
                            string nameMin = librarySpec.Name;
                            // Avoid adding the modifier a second time, if it has
                            // already been done once.
                            if (!nameMin.EndsWith(nameModifier + ")")) // Not L10N
                                nameMin = string.Format("{0} ({1})", librarySpec.Name, nameModifier); // Not L10N
                            librarySpecMin = new BiblioSpecLiteSpec(nameMin, blibDb.FilePath);
                        }

                        listLibraries.Add(blibDb.MinimizeLibrary(librarySpecMin,
                            pepLibraries.Libraries[i], document));

                        // Terminate if user canceled
                        if (progressMonitor != null && progressMonitor.IsCanceled)
                            return document;

                        listLibrarySpecs.Add(librarySpecMin);
                        dictOldNameToNew.Add(librarySpec.Name, librarySpecMin.Name);
                    }
                }

                document = (SrmDocument) document.ChangeAll(node =>
                    {
                        var nodeGroup = node as TransitionGroupDocNode;
                        if (nodeGroup == null || !nodeGroup.HasLibInfo)
                            return node;

                        string libName = nodeGroup.LibInfo.LibraryName;
                        string libNameNew = dictOldNameToNew[libName];
                        if (Equals(libName, libNameNew))
                            return node;
                        var libInfo = nodeGroup.LibInfo.ChangeLibraryName(libNameNew);
                        return nodeGroup.ChangeLibInfo(libInfo);
                    },
                    (int) SrmDocument.Level.TransitionGroups);
            }

            return document.ChangeSettingsNoDiff(settings.ChangePeptideLibraries(
                lib => lib.ChangeLibraries(listLibrarySpecs, listLibraries)));
        }
        public void TestModificationMatcher()
        {
            InitSeqs();

            var carbC = StaticModList.GetDefaultsOn()[0];

            // Test exception thrown if unable to match - mass.
            UpdateMatcherFail(STR_FAIL_MASS);
            UpdateMatcherFail(STR_FAIL_NOT_A_NUMBER);
            // Test exception thrown if unable to match - name.
            UpdateMatcherFail(STR_FAIL_NAME);
            // Can't match empty modifications.
            UpdateMatcherFail(STR_FAIL_EMPTY_MOD);
            UpdateMatcherFail(STR_FAIL_EMPTY_MOD2);
            // Can't match double modifications.
            UpdateMatcherFail(STR_FAIL_DOUBLE_MOD);
            // Test exception thrown if unimod not specified correctly
            UpdateMatcherFail(STR_FAIL_UNIMOD);
            UpdateMatcherFail(STR_UNKNOWN_UNIMOD);
            // Can't phosphorylate tryptophan
            UpdateMatcherFail(STR_FAIL_WRONG_AA_UNIMOD);
            // Can't put C-terminal modification in middle of peptide
            UpdateMatcherFail(STR_FAIL_UNIMOD_TERMINUS);

            // Test mods in UniMod match correctly.
            UpdateMatcher(StaticModList.GetDefaultsOn(), HeavyModList.GetDefaultsOn(), null, null);
            // A sequence with no modifications should not be explicitly modified.
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_NO_MODS).HasExplicitMods);
            var nodeCysOxi = MATCHER.GetModifiedNode(STR_CYS_AND_OXI);
            Assert.IsTrue(nodeCysOxi.HasExplicitMods);
            Assert.IsFalse(nodeCysOxi.ExplicitMods.HasHeavyModifications);
            // Modifications should match by name.
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_MOD_BY_NAME).ExplicitMods.StaticModifications.Contains(mod =>
                Equals(mod.Modification.Name,  "Phospho (ST)")));
            // Test can find terminal modification
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_TERM_ONLY).ExplicitMods.HeavyModifications.Contains(mod =>
                mod.Modification.EquivalentAll(UniMod.GetModification("Label:13C(6) (C-term R)", false))));
            // Test can find matches on terminus that are not terminal
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_MOD_BY_NAME).ExplicitMods.StaticModifications.Contains(mod =>
                mod.Modification.Terminus == null));
            // Test matching negative masses
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_AMMONIA_LOSS).ExplicitMods.StaticModifications.Contains(mod =>
                mod.Modification.EquivalentAll(UniMod.GetModification("Ammonia-loss (N-term C)", true))));

            // General and specific
            // If all AAs modified, try for most general modification.
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_HEAVY_15)
                .ExplicitMods.HeavyModifications.Contains(mod => mod.Modification.Equivalent(LABEL15_N)));

            // Updating the settings.
            // Peptide settings should change to include new mods.
            var docNew = new SrmDocument(SrmSettingsList.GetDefault());
            IdentityPath firstAdded;
            IdentityPath nextAdded;
            docNew = docNew.AddPeptideGroups(new[] { new PeptideGroupDocNode(new PeptideGroup(), "PepGroup1", "",
                new[] {MATCHER.GetModifiedNode(STR_MOD_BY_NAME)})}, true, null, out firstAdded, out nextAdded);
            var pepSetNew = MATCHER.GetDocModifications(docNew);
            Assert.IsTrue(pepSetNew.StaticModifications.Contains(UniMod.GetModification("Phospho (ST)", true).ChangeExplicit(true)));
            // Update the document to the new settings.
            var pepSetNew1 = pepSetNew;
            var settingsNew2 = docNew.Settings.ChangePeptideModifications(mods => pepSetNew1);
            var lightGlobalMods = new MappedList<string, StaticMod>();
            lightGlobalMods.AddRange(settingsNew2.PeptideSettings.Modifications.StaticModifications);
            var heavyGlobalMods = new MappedList<string, StaticMod>();
            heavyGlobalMods.AddRange(settingsNew2.PeptideSettings.Modifications.HeavyModifications);
            // Match again. Test FoundMatches string should now be empty.
            MATCHER.CreateMatches(docNew.Settings.ChangePeptideModifications(mods => pepSetNew1),
                new List<string> { STR_MOD_BY_NAME }, lightGlobalMods, heavyGlobalMods);
            Assert.IsTrue(string.IsNullOrEmpty(MATCHER.FoundMatches));

            // Adding 15N to the settings.
            UpdateMatcher(new[] { carbC }, new[] { LABEL15_N }, null, null);
            // Test sequences with only explicit heavy mods should not have explicit light mods
            Assert.IsNull(MATCHER.GetModifiedNode(STR_HEAVY_ONLY).ExplicitMods.StaticModifications);

            // Test sequences with only explicit light mods should not have explicit heavy mods
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_LIGHT_ONLY).ExplicitMods.HasHeavyModifications);

            // Test global mods take precendence over UniMod
            UpdateMatcher(new[] { carbC }, null, new[] { OXIDATION_M_GLOBAL }, new[] { LABEL15_N });
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_CYS_AND_OXI).ExplicitMods.StaticModifications
                .Contains(mod => Equals(mod.Modification, OXIDATION_M_GLOBAL)));

            // Test document mods take precendence over UniMod
            UpdateMatcher(new[] { carbC, METHIONINE_OXIDATION }, null, new[] { OXIDATION_M_GLOBAL }, new[] { LABEL15_N });
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_CYS_AND_OXI).HasExplicitMods);

            // Test exception thrown if match doesn't make sense - wrong AA.
            UpdateMatcherFail(STR_FAIL_OX_ON_D);
            // Test exception thrown if match doesn't make sense - wrong terminus.
            _seqs.Add(STR_FAIL_OX_TERM);
            AssertEx.ThrowsException<FormatException>(() => UpdateMatcher(new[] {OXIDATION_M_C_TERM}, null, null, null));
            _seqs.Remove(STR_FAIL_OX_TERM);

            // Heavy 15N - All AAs.
            UpdateMatcher(new[] { carbC, METHIONINE_OXIDATION }, new[] {LABEL15_N}, null, null);
            // Node should be created from document settings if possible.
            Assert.IsNull(MATCHER.GetModifiedNode(STR_HEAVY_15).ExplicitMods);
            // Heavy 15N - specific AA.
            // If only a specific AA is modified, there must be an explicit mod.
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_HEAVY_15_F).HasExplicitMods);

            // Test variable mods match correctly.
            // Put variable mod in global mod and not on doc - make sure don't get variable mod,
            // should get explicit mod in that case.
            var variableMetOx = METHIONINE_OXIDATION.ChangeVariable(true);
            UpdateMatcher(new[] { carbC }, null, new[] { variableMetOx }, null);
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_CYS_AND_OXI).HasExplicitMods);
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_CYS_OXI_PHOS).ExplicitMods.IsVariableStaticMods);
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_CYS_OXI_PHOS_CAP).ExplicitMods.IsVariableStaticMods);
            // Add variable mod to doc
            UpdateMatcher(new[] { carbC, variableMetOx }, null, null, null);
            // Mod can be created by the settings.
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_CYS_AND_OXI).HasExplicitMods);
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_CYS_AND_OXI).ExplicitMods.IsVariableStaticMods);
            // Mod cannot be created by the settings.
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_CYS_OXI_PHOS).ExplicitMods.IsVariableStaticMods);
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_CYS_OXI_PHOS_CAP).ExplicitMods.IsVariableStaticMods);

            // Add Met Ox to global. Test: +16 finds it.
            UpdateMatcher(new[] {carbC}, null, new[] {MET_OX_ROUNDED}, null);
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_CYS_AND_OXI).
                ExplicitMods.StaticModifications.Contains(mod => Equals(mod.Modification, MET_OX_ROUNDED)));
            // Test: +15.99 finds UniMod.
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_HEAVY_15).
                ExplicitMods.StaticModifications.Contains(mod => Equals(mod.Modification, MET_OX_ROUNDED)));
            // Add Methionine Oxidation before Met Ox. Test: +16 finds it.
            UpdateMatcher(new[] { carbC }, null, new[] { METHIONINE_OXIDATION, MET_OX_ROUNDED }, null);
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_CYS_AND_OXI).
                ExplicitMods.StaticModifications.Contains(mod => Equals(mod.Modification, MET_OX_ROUNDED)));
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_CYS_AND_OXI).
                ExplicitMods.StaticModifications.Contains(mod => Equals(mod.Modification, METHIONINE_OXIDATION)));
            // Test long masses rounded.
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_METOX_LONG_MASS).ExplicitMods.StaticModifications.Contains(mod =>
                Equals(mod.Modification, METHIONINE_OXIDATION)));
            // Test UniMod label types
            var node = MATCHER.GetModifiedNode(STR_UNIMOD_LABEL);
            Assert.IsNotNull(node);
            Assert.IsNull(node.ExplicitMods.StaticModifications);
            Assert.IsTrue(node.ExplicitMods.HeavyModifications.Contains(mod =>
                Equals(mod.Modification, N_TERM_LABEL)));
            UpdateMatcherWithNoSequences(new[] { carbC }, new[] { N_TERM_LABEL }, new[] { METHIONINE_OXIDATION, MET_OX_ROUNDED }, null);
            var nodeNew = MATCHER.GetModifiedNode(STR_UNIMOD_LABEL);
            Assert.IsNotNull(nodeNew);
            Assert.IsTrue(nodeNew.TransitionGroups.Any(group => Equals(group.TransitionGroup.LabelType, IsotopeLabelType.heavy)));
            UpdateMatcher(new[] { carbC }, null, new[] { METHIONINE_OXIDATION, MET_OX_ROUNDED }, null);

            // Test case where there are lots of unimod labels
            var nodeUniAll = MATCHER.GetModifiedNode(STR_UNIMOD_ALL);
            Assert.AreEqual(nodeUniAll.ExplicitMods.HeavyModifications.Count, 10);
            Assert.IsNull(nodeUniAll.ExplicitMods.StaticModifications);
            foreach (var mod in nodeUniAll.ExplicitMods.HeavyModifications)
            {
                Assert.AreEqual(mod.Modification.ShortName, "+01");
                Assert.AreEqual(mod.Modification.UnimodId, 994);
            }

            // Test unimod terminal label
            var nodeUniTerm = MATCHER.GetModifiedNode(STR_UNIMOD_TERMINUS);
            Assert.AreEqual(nodeUniTerm.ExplicitMods.HeavyModifications.Count, 1);
            Assert.IsNull(nodeUniTerm.ExplicitMods.StaticModifications);
            Assert.AreEqual(nodeUniTerm.ExplicitMods.HeavyModifications[0].Modification.Terminus, ModTerminus.C);
            Assert.AreEqual(nodeUniTerm.ExplicitMods.HeavyModifications[0].Modification.UnimodId, 298);

            // Basic multi-label test
            var heavyLabelType2 = new IsotopeLabelType("Heavy2", 1);
            var typedMod = new TypedModifications(heavyLabelType2, new List<StaticMod> { LABEL15_N });
            var peptideMods = new PeptideModifications(new List<StaticMod>(), new List<TypedModifications> { typedMod });
            var settingsMultiLabel = SrmSettingsList.GetDefault().ChangePeptideModifications(mods => peptideMods);
            var defSetSetLight = new MappedList<string, StaticMod>();
            defSetSetLight.AddRange(StaticModList.GetDefaultsOn());
            var defSetHeavy = new MappedList<string, StaticMod>();
            defSetHeavy.AddRange(HeavyModList.GetDefaultsOn());
            defSetHeavy.Add( LABEL15_N );
            MATCHER.CreateMatches(settingsMultiLabel, new List<string> { STR_HEAVY_15_F }, defSetSetLight, defSetHeavy);
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_HEAVY_15_F).ExplicitMods.GetHeavyModifications().Contains(mod => Equals(mod.LabelType, heavyLabelType2)));
            // Peptide settings should not change.
            var docNew0 = new SrmDocument(settingsMultiLabel).AddPeptideGroups(new[] { new PeptideGroupDocNode(new PeptideGroup(),
                "PepGroup1", "", new[] {MATCHER.GetModifiedNode(STR_HEAVY_15_F)})}, true, null, out firstAdded, out nextAdded);
            var settingsNew = MATCHER.GetDocModifications(docNew0);
            Assert.AreEqual(settingsMultiLabel.PeptideSettings.Modifications, settingsNew);

            // Finding specific modifications.
            // If only specific AA modified, try for most specific modification.
            UpdateMatcher(null, null, null, null, new[] { STR_HEAVY_15_F});
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_HEAVY_15_F)
                .ExplicitMods.HeavyModifications.Contains(mod =>
                    mod.Modification.AminoAcids.Contains(c => c == 'F')));
            // If only some AAs modified, try for most specific modifications.
            UpdateMatcher(null, null, null, null, new[] { STR_HEAVY_15_NOT_ALL });
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_HEAVY_15_NOT_ALL)
                .ExplicitMods.HeavyModifications.Contains(mod =>
                mod.Modification.AminoAcids.Contains(c => c == 'I')));

            using (var testDir = new TestFilesDir(TestContext, ZIP_FILE))
            {
                var modMatchDocContainer = InitMatchDocContainer(testDir);
                var libkeyModMatcher = new LibKeyModificationMatcher();
                var anlLibSpec = new BiblioSpecLiteSpec("ANL_Combo", testDir.GetTestPath("ANL_Combined.blib"));
                var yeastLibSpec = new BiblioSpecLiteSpec("Yeast", testDir.GetTestPath("Yeast_atlas_small.blib"));
                modMatchDocContainer.ChangeLibSpecs(new[] { anlLibSpec, yeastLibSpec });
                var docLibraries = modMatchDocContainer.Document.Settings.PeptideSettings.Libraries.Libraries;
                int anlLibIndex = docLibraries.IndexOf(library => Equals(library.Name, anlLibSpec.Name));
                int yeastLibIndex = docLibraries.IndexOf(library => Equals(library.Name, yeastLibSpec.Name));

                libkeyModMatcher.CreateMatches(modMatchDocContainer.Document.Settings,
                    docLibraries[anlLibIndex].Keys, defSetSetLight, defSetHeavy);

                // Test can match 15N
                Assert.IsTrue(libkeyModMatcher.Matches.Values.Contains(match =>
                    match.HeavyMod != null && match.HeavyMod.Equivalent(LABEL15_N)));

                var uniModMetOx = UniMod.GetModification("Oxidation (M)", true);

                // Test can match Met Ox
                Assert.IsTrue(libkeyModMatcher.Matches.Values.Contains(match =>
                    match.StructuralMod != null && match.StructuralMod.Equivalent(uniModMetOx)));

                // Test can match 15N and Met ox!
                Assert.IsTrue(libkeyModMatcher.Matches.Contains(match => match.Key.Mass == 17
                    && match.Value.StructuralMod != null && match.Value.StructuralMod.Equivalent(uniModMetOx)
                    && match.Value.HeavyMod != null && match.Value.HeavyMod.Equivalent(LABEL15_N)));

                // Test can match Cysteine (Implicit) and Met Ox (variable)
                libkeyModMatcher.CreateMatches(modMatchDocContainer.Document.Settings,
                    docLibraries[yeastLibIndex].Keys, defSetSetLight, defSetHeavy);
                Assert.IsTrue(libkeyModMatcher.MatcherPepMods.StaticModifications.Contains(mod =>
                    mod.Formula.Equals(UniMod.GetModification(StaticModList.DEFAULT_NAME, true).Formula) && !mod.IsVariable));
                Assert.IsTrue(libkeyModMatcher.MatcherPepMods.StaticModifications.Contains(mod =>
                    mod.Formula.Equals("O") && mod.IsVariable));
            }
        }
Esempio n. 19
0
        /* TODO bspratt drift time libs for small molecules
         *
         * [TestMethod]
         * public void WatersImsMsePredictedDriftTimesChromatogramTestAsSmallMolecules()
         * {
         *  WatersImsMseChromatogramTest(DriftFilterType.predictor, true);
         * }
         *
         * [TestMethod]
         * public void WatersImsMseLibraryDriftTimesChromatogramTestAsSmallMolecules()
         * {
         *  WatersImsMseChromatogramTest(DriftFilterType.library, true);
         * }
         *
         */

        private void WatersImsMseChromatogramTest(DriftFilterType mode,
                                                  RefinementSettings.ConvertToSmallMoleculesMode asSmallMolecules = RefinementSettings.ConvertToSmallMoleculesMode.none)
        {
            string subdir       = (asSmallMolecules == RefinementSettings.ConvertToSmallMoleculesMode.none) ? null : asSmallMolecules.ToString();
            var    testFilesDir = new TestFilesDir(TestContext, ZIP_FILE, subdir);

            TestSmallMolecules = false;                                               // Don't need that extra magic node

            bool        withDriftTimePredictor = (mode == DriftFilterType.predictor); // Load the doc that has a drift time predictor?
            bool        withDriftTimeFilter    = (mode != DriftFilterType.none);      // Perform drift time filtering?  (either with predictor, or with bare times in blib file)
            string      docPath;
            SrmDocument document = InitWatersImsMseDocument(testFilesDir, withDriftTimePredictor ? "single_with_driftinfo.sky" : "single_no_driftinfo.sky", asSmallMolecules, out docPath);

            AssertEx.IsDocumentState(document, (withDriftTimePredictor || (asSmallMolecules != RefinementSettings.ConvertToSmallMoleculesMode.none)) ? 1 : 0, 1, 1, 1, 8); // Drift time lib load bumps the doc version
            var docContainer = new ResultsTestDocumentContainer(document, docPath);
            var doc          = docContainer.Document;
            var docOriginal  = doc;

            string testModeStr = withDriftTimePredictor ? "with drift time predictor" : "without drift time info";

            if (withDriftTimeFilter && !withDriftTimePredictor)
            {
                // Use the bare drift times in the spectral library
                var librarySpec = new BiblioSpecLiteSpec("drift test",
                                                         testFilesDir.GetTestPath("mse-mobility.filtered-scaled.blib"));
                doc = doc.ChangeSettings(
                    doc.Settings.ChangePeptideLibraries(lib => lib.ChangeLibrarySpecs(new[] { librarySpec })).
                    ChangePeptidePrediction(p => p.ChangeLibraryDriftTimesResolvingPower(100)).
                    ChangePeptidePrediction(p => p.ChangeUseLibraryDriftTimes(true))
                    );
                testModeStr = "with drift times from spectral library";
            }

            var listChromatograms = new List <ChromatogramSet>();
            // A small subset of the QC_HDMSE_02_UCA168_3495_082213 data set (RT 21.5-22.5) from Will Thompson
            const string path = @"waters-mobility.mz5";

            listChromatograms.Add(AssertResult.FindChromatogramSet(doc, new MsDataFilePath(path)) ??
                                  new ChromatogramSet(Path.GetFileName(path).Replace('.', '_'), new[] { path }));
            var docResults = doc.ChangeMeasuredResults(new MeasuredResults(listChromatograms));

            Assert.IsTrue(docContainer.SetDocument(docResults, docOriginal, true));
            docContainer.AssertComplete();
            document = docContainer.Document;

            float  tolerance = (float)document.Settings.TransitionSettings.Instrument.MzMatchTolerance;
            double maxHeight = 0;
            var    results   = document.Settings.MeasuredResults;

            Assert.AreEqual(1, document.MoleculePrecursorPairs.Count());
            foreach (var pair in document.MoleculePrecursorPairs)
            {
                ChromatogramGroupInfo[] chromGroupInfo;
                Assert.IsTrue(results.TryLoadChromatogram(0, pair.NodePep, pair.NodeGroup,
                                                          tolerance, true, out chromGroupInfo));
                Assert.AreEqual(1, chromGroupInfo.Length, testModeStr);
                var chromGroup    = chromGroupInfo[0];
                var expectedPeaks = ((asSmallMolecules == RefinementSettings.ConvertToSmallMoleculesMode.masses_only) ? 6 : 5);
                Assert.AreEqual(withDriftTimeFilter ? 3 : expectedPeaks, chromGroup.NumPeaks, testModeStr); // This will be higher if we don't filter on DT
                foreach (var tranInfo in chromGroup.TransitionPointSets)
                {
                    maxHeight = Math.Max(maxHeight, tranInfo.MaxIntensity);
                }
            }
            Assert.AreEqual(withDriftTimeFilter? 5226 : 20075, maxHeight, 1, testModeStr);   // Without DT filtering, this will be much greater

            // now drill down for specific values
            int nPeptides = 0;

            foreach (var nodePep in document.Molecules.Where(nodePep => nodePep.Results[0] != null))
            {
                // expecting just one peptide result in this small data set
                if (nodePep.Results[0].Sum(chromInfo => chromInfo.PeakCountRatio > 0 ? 1 : 0) > 0)
                {
                    Assert.AreEqual(21.94865, (double)nodePep.GetMeasuredRetentionTime(0), .0001, testModeStr);
                    Assert.AreEqual(1.0, (double)nodePep.GetPeakCountRatio(0), 0.0001, testModeStr);
                    nPeptides++;
                }
            }
            Assert.AreEqual(1, nPeptides);

            if (withDriftTimePredictor || withDriftTimeFilter)
            {
                // Verify that the .imdb pr .blib file goes out in the share zipfile
                for (int complete = 0; complete <= 1; complete++)
                {
                    var sharePath = testFilesDir.GetTestPath(complete == 1?"share_complete.zip":"share_minimized.zip");
                    var share     = new SrmDocumentSharing(document, docPath, sharePath, complete == 1);
                    using (var longWaitDlg = new LongWaitDlg
                    {
                        // ReSharper disable once LocalizableElement
                        Text = "unit test WatersImsTest -- sharing document",
                    })
                    {
                        longWaitDlg.PerformWork(null, 1000, share.Share);
                        Assert.IsFalse(longWaitDlg.IsCanceled);
                    }

                    var files = share.ListEntries().ToArray();
                    Assert.IsTrue(files.Contains(withDriftTimePredictor ? "scaled.imdb" : "mse-mobility.filtered-scaled.blib"));
                    // And round trip it to make sure we haven't left out any new features in minimized imdb or blib files
                    using (var longWaitDlg = new LongWaitDlg
                    {
                        // ReSharper disable once LocalizableElement
                        Text = "unit test WatersImsTest",
                    })
                    {
                        longWaitDlg.PerformWork(null, 1000, share.Extract);
                        Assert.IsFalse(longWaitDlg.IsCanceled);
                    }
                    using (TextReader reader = new StreamReader(share.DocumentPath))
                    {
                        XmlSerializer documentSerializer = new XmlSerializer(typeof(SrmDocument));
                        var           document2          = (SrmDocument)documentSerializer.Deserialize(reader);
                        Assert.IsNotNull(document2);
                        var im  = document.Settings.GetIonMobilities(new MsDataFilePath(path));
                        var pep = document2.Molecules.First();
                        foreach (TransitionGroupDocNode nodeGroup in pep.Children)
                        {
                            double windowDT;
                            var    centerDriftTime = document.Settings.PeptideSettings.Prediction.GetDriftTime(
                                pep, nodeGroup, im, out windowDT);
                            Assert.AreEqual(3.86124, centerDriftTime.DriftTimeMsec(false) ?? 0, .0001, testModeStr);
                            Assert.AreEqual(0.077224865797235934, windowDT, .0001, testModeStr);
                        }
                    }
                }
            }

            // Release file handles
            docContainer.Release();
            testFilesDir.Dispose();
            string cachePath = ChromatogramCache.FinalPathForName(docPath, null);

            FileEx.SafeDelete(cachePath);
        }
Esempio n. 20
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);
        }
Esempio n. 21
0
        public bool BuildPeptideSearchLibrary(CancelEventArgs e)
        {
            // Nothing to build, if now search files were specified
            if (!SearchFilenames.Any())
            {
                var libraries = SkylineWindow.Document.Settings.PeptideSettings.Libraries;
                if (!libraries.HasLibraries)
                {
                    return(false);
                }
                var libSpec = libraries.LibrarySpecs.FirstOrDefault(s => s.IsDocumentLibrary);
                return(libSpec != null && LoadPeptideSearchLibrary(libSpec));
            }

            double           cutOffScore;
            MessageBoxHelper helper = new MessageBoxHelper(WizardForm);

            if (!helper.ValidateDecimalTextBox(textCutoff, 0, 1.0, out cutOffScore))
            {
                e.Cancel = true;
                return(false);
            }
            ImportPeptideSearch.CutoffScore = cutOffScore;

            BiblioSpecLiteBuilder builder;

            try
            {
                builder = ImportPeptideSearch.GetLibBuilder(SkylineWindow.Document, SkylineWindow.DocumentFilePath, cbIncludeAmbiguousMatches.Checked);
            }
            catch (FileEx.DeleteException de)
            {
                MessageDlg.ShowException(this, de);
                return(false);
            }

            using (var longWaitDlg = new LongWaitDlg
            {
                Text = Resources.BuildPeptideSearchLibraryControl_BuildPeptideSearchLibrary_Building_Peptide_Search_Library,
                Message = Resources.BuildPeptideSearchLibraryControl_BuildPeptideSearchLibrary_Building_document_library_for_peptide_search_,
            })
            {
                // Disable the wizard, because the LongWaitDlg does not
                try
                {
                    ImportPeptideSearch.ClosePeptideSearchLibraryStreams(SkylineWindow.DocumentUI);
                    var status = longWaitDlg.PerformWork(WizardForm, 800,
                                                         monitor => LibraryManager.BuildLibraryBackground(SkylineWindow, builder, monitor, new LibraryManager.BuildState(null, null)));
                    if (status.IsError)
                    {
                        MessageDlg.ShowException(WizardForm, status.ErrorException);
                        return(false);
                    }
                }
                catch (Exception x)
                {
                    MessageDlg.ShowWithException(WizardForm, TextUtil.LineSeparate(string.Format(Resources.BuildPeptideSearchLibraryControl_BuildPeptideSearchLibrary_Failed_to_build_the_library__0__,
                                                                                                 Path.GetFileName(BiblioSpecLiteSpec.GetLibraryFileName(SkylineWindow.DocumentFilePath))), x.Message), x);
                    return(false);
                }
            }

            var docLibSpec = builder.LibrarySpec.ChangeDocumentLibrary(true);

            Settings.Default.SpectralLibraryList.Insert(0, docLibSpec);

            // Go ahead and load the library - we'll need it for
            // the modifications and chromatograms page.
            if (!LoadPeptideSearchLibrary(docLibSpec))
            {
                return(false);
            }

            var selectedIrtStandard = comboStandards.SelectedItem as IrtStandard;
            var addedIrts           = false;

            if (selectedIrtStandard != null && selectedIrtStandard != IrtStandard.NULL)
            {
                addedIrts = AddIrtLibraryTable(docLibSpec.FilePath, selectedIrtStandard);
            }

            var docNew = ImportPeptideSearch.AddDocumentSpectralLibrary(SkylineWindow.Document, docLibSpec);

            if (docNew == null)
            {
                return(false);
            }

            if (addedIrts)
            {
                docNew = ImportPeptideSearch.AddRetentionTimePredictor(docNew, docLibSpec);
            }

            SkylineWindow.ModifyDocument(Resources.BuildPeptideSearchLibraryControl_BuildPeptideSearchLibrary_Add_document_spectral_library, doc => docNew);

            if (!string.IsNullOrEmpty(builder.AmbiguousMatchesMessage))
            {
                MessageDlg.Show(WizardForm, builder.AmbiguousMatchesMessage);
            }
            return(true);
        }
Esempio n. 22
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;
        }
Esempio n. 23
0
        public SrmSettings ConnectLibrarySpecs(Func<Library, LibrarySpec> findLibrarySpec, string docLibPath = null)
        {
            var libraries = PeptideSettings.Libraries;
            bool hasDocLib = libraries.HasDocumentLibrary && null != docLibPath;
            if (!libraries.HasLibraries && !hasDocLib)
                return this;

            int len = libraries.Libraries.Count;
            int docLibShift = hasDocLib ? 1 : 0;
            LibrarySpec[] librarySpecs = new LibrarySpec[len + docLibShift];
            for (int i = 0; i < len; i++)
            {
                int iSpec = i + docLibShift;
                var library = libraries.Libraries[i];
                if (library == null)
                {
                    librarySpecs[iSpec] = libraries.LibrarySpecs[i];
                    if (librarySpecs[iSpec] == null)
                        throw new InvalidDataException(Resources.SrmSettings_ConnectLibrarySpecs_Settings_missing_library_spec);
                    continue;
                }

                librarySpecs[iSpec] = findLibrarySpec(library);
                if (librarySpecs[iSpec] == null)
                    return null;    // Canceled
                if (librarySpecs[iSpec].FilePath == null)
                {
                    // Disconnect the libraries, if not canceled, but no path
                    // specified.
                    return ChangePeptideSettings(PeptideSettings.ChangeLibraries(libraries.Disconnect()));
                }
            }

            if (hasDocLib)
            {
                string docLibName = Path.GetFileNameWithoutExtension(docLibPath);
                librarySpecs[0] = new BiblioSpecLiteSpec(docLibName, docLibPath).ChangeDocumentLibrary(true);
            }

            if (ArrayUtil.EqualsDeep(librarySpecs, libraries.LibrarySpecs))
                return this;

            libraries = libraries.ChangeLibrarySpecs(librarySpecs);
            return ChangePeptideSettings(PeptideSettings.ChangeLibraries(libraries));
        }
Esempio n. 24
0
        private void ImportMassList(MassListInputs inputs, string description)
        {
            SrmTreeNode nodePaste = SequenceTree.SelectedNode as SrmTreeNode;
            IdentityPath insertPath = nodePaste != null ? nodePaste.Path : null;
            IdentityPath selectPath = null;
            List<MeasuredRetentionTime> irtPeptides = null;
            List<SpectrumMzInfo> librarySpectra = null;
            List<TransitionImportErrorInfo> errorList = null;
            List<PeptideGroupDocNode> peptideGroups = null;
            RetentionTimeRegression retentionTimeRegressionStore = null;
            var docCurrent = DocumentUI;
            SrmDocument docNew = null;
            var retentionTimeRegression = docCurrent.Settings.PeptideSettings.Prediction.RetentionTime;
            RCalcIrt calcIrt = retentionTimeRegression != null ? (retentionTimeRegression.Calculator as RCalcIrt) : null;
            using (var longWaitDlg = new LongWaitDlg(this) {Text = description})
            {
                longWaitDlg.PerformWork(this, 1000, longWaitBroker =>
                {
                    docNew = docCurrent.ImportMassList(inputs, longWaitBroker,
                        insertPath, out selectPath, out irtPeptides, out librarySpectra, out errorList, out peptideGroups);
                });
            }
            bool isDocumentSame = ReferenceEquals(docNew, docCurrent);
            // If nothing was imported (e.g. operation was canceled or zero error-free transitions) and also no errors, just return
            if (isDocumentSame && !errorList.Any())
                return;
            // Show the errors, giving the option to accept the transitions without errors,
            // if there are any
            if (errorList.Any())
            {
                using (var errorDlg = new ImportTransitionListErrorDlg(errorList, isDocumentSame))
                {
                    if (errorDlg.ShowDialog(this) == DialogResult.Cancel || isDocumentSame)
                    {
                        return;
                    }
                }
            }

            var dbIrtPeptides = irtPeptides.Select(rt => new DbIrtPeptide(rt.PeptideSequence, rt.RetentionTime, false, TimeSource.scan)).ToList();
            var dbIrtPeptidesFilter = ImportAssayLibraryHelper.GetUnscoredIrtPeptides(dbIrtPeptides, calcIrt);
            bool overwriteExisting = false;
            MassListInputs irtInputs = null;
            // If there are no iRT peptides or none with different values than the database, don't import any iRT's
            if (dbIrtPeptidesFilter.Any())
            {
                // Ask whether or not to include iRT peptides in the paste
                string useIrtMessage = calcIrt == null
                    ? Resources.SkylineWindow_ImportMassList_The_transition_list_appears_to_contain_iRT_values__but_the_document_does_not_have_an_iRT_calculator___Create_a_new_calculator_and_add_these_iRT_values_
                    : Resources.SkylineWindow_ImportMassList_The_transition_list_appears_to_contain_iRT_library_values___Add_these_iRT_values_to_the_iRT_calculator_;
                string yesButton = calcIrt == null
                    ? Resources.SkylineWindow_ImportMassList__Create___
                    : Resources.SkylineWindow_ImportMassList_Add;
                var useIrtResult = MultiButtonMsgDlg.Show(this, useIrtMessage,
                    yesButton, Resources.SkylineWindow_ImportMassList__Skip, true);
                if (useIrtResult == DialogResult.Cancel)
                {
                    return;
                }
                if (useIrtResult == DialogResult.Yes)
                {
                    if (calcIrt == null)
                    {
                        // If there is no iRT calculator, ask the user to create one
                        using (var dlg = new CreateIrtCalculatorDlg(docNew, DocumentFilePath, Settings.Default.RTScoreCalculatorList, peptideGroups))
                        {
                            if (dlg.ShowDialog(this) != DialogResult.OK)
                            {
                                return;
                            }

                            docNew = dlg.Document;
                            calcIrt = (RCalcIrt)docNew.Settings.PeptideSettings.Prediction.RetentionTime.Calculator;
                            dlg.UpdateLists(librarySpectra, dbIrtPeptidesFilter);
                            if (!string.IsNullOrEmpty(dlg.IrtFile))
                                irtInputs = new MassListInputs(dlg.IrtFile);
                        }
                    }
                    string dbPath = calcIrt.DatabasePath;
                    IrtDb db = File.Exists(dbPath) ? IrtDb.GetIrtDb(dbPath, null) : IrtDb.CreateIrtDb(dbPath);
                    var oldPeptides = db.GetPeptides().ToList();
                    IList<DbIrtPeptide.Conflict> conflicts;
                    dbIrtPeptidesFilter = DbIrtPeptide.MakeUnique(dbIrtPeptidesFilter);
                    DbIrtPeptide.FindNonConflicts(oldPeptides, dbIrtPeptidesFilter, null, out conflicts);
                    // Ask whether to keep or overwrite peptides that are present in the import and already in the database
                    if (conflicts.Any())
                    {
                        string messageOverwrite = string.Format(Resources.SkylineWindow_ImportMassList_The_iRT_calculator_already_contains__0__of_the_imported_peptides_,
                                                                conflicts.Count);
                        var overwriteResult = MultiButtonMsgDlg.Show(this,
                            TextUtil.LineSeparate(messageOverwrite, conflicts.Count == 1
                                ? Resources.SkylineWindow_ImportMassList_Keep_the_existing_iRT_value_or_overwrite_with_the_imported_value_
                                : Resources.SkylineWindow_ImportMassList_Keep_the_existing_iRT_values_or_overwrite_with_imported_values_),
                            Resources.SkylineWindow_ImportMassList__Keep, Resources.SkylineWindow_ImportMassList__Overwrite, true);
                        if (overwriteResult == DialogResult.Cancel)
                        {
                            return;
                        }
                        overwriteExisting = overwriteResult == DialogResult.No;
                    }
                    using (var longWaitDlg = new LongWaitDlg(this) { Text = Resources.SkylineWindow_ImportMassList_Adding_iRT_values_})
                    {
                        longWaitDlg.PerformWork(this, 100, progressMonitor =>
                            docNew = docNew.AddIrtPeptides(dbIrtPeptidesFilter, overwriteExisting, progressMonitor));
                    }
                    if (docNew == null)
                        return;
                    retentionTimeRegressionStore = docNew.Settings.PeptideSettings.Prediction.RetentionTime;
                }
            }
            BiblioSpecLiteSpec docLibrarySpec = null;
            BiblioSpecLiteLibrary docLibrary = null;
            int indexOldLibrary = -1;
            if (librarySpectra.Any())
            {
                string addLibraryMessage = Resources.SkylineWindow_ImportMassList_The_transition_list_appears_to_contain_spectral_library_intensities___Create_a_document_library_from_these_intensities_;
                var addLibraryResult = MultiButtonMsgDlg.Show(this, addLibraryMessage,
                    Resources.SkylineWindow_ImportMassList__Create___, Resources.SkylineWindow_ImportMassList__Skip,
                    true);
                if (addLibraryResult == DialogResult.Cancel)
                {
                    return;
                }
                if (addLibraryResult == DialogResult.Yes)
                {
                    // Can't name a library after the document if the document is unsaved
                    // In this case, prompt to save
                    if (DocumentFilePath == null)
                    {
                        string saveDocumentMessage = Resources.SkylineWindow_ImportMassList_You_must_save_the_Skyline_document_in_order_to_create_a_spectral_library_from_a_transition_list_;
                        var saveDocumentResult = MultiButtonMsgDlg.Show(this, saveDocumentMessage, MultiButtonMsgDlg.BUTTON_OK);
                        if (saveDocumentResult == DialogResult.Cancel)
                        {
                            return;
                        }
                        else if (!SaveDocumentAs())
                        {
                            return;
                        }
                    }

                    librarySpectra = SpectrumMzInfo.RemoveDuplicateSpectra(librarySpectra);

                    string documentLibrary = BiblioSpecLiteSpec.GetLibraryFileName(DocumentFilePath);
            // ReSharper disable once AssignNullToNotNullAttribute
                    string outputPath = Path.Combine(Path.GetDirectoryName(documentLibrary),
                                                     Path.GetFileNameWithoutExtension(documentLibrary) + BiblioSpecLiteSpec.ASSAY_NAME + BiblioSpecLiteSpec.EXT);
                    bool libraryExists = File.Exists(outputPath);
                    string name = Path.GetFileNameWithoutExtension(DocumentFilePath) + BiblioSpecLiteSpec.ASSAY_NAME;
                    indexOldLibrary = docNew.Settings.PeptideSettings.Libraries.LibrarySpecs.IndexOf(spec => spec != null && spec.FilePath == outputPath);
                    bool libraryLinkedToDoc = indexOldLibrary != -1;
                    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 (libraryExists && !libraryLinkedToDoc)
                    {
                        string replaceLibraryMessage = string.Format(Resources.SkylineWindow_ImportMassList_There_is_an_existing_library_with_the_same_name__0__as_the_document_library_to_be_created___Overwrite_this_library_or_skip_import_of_library_intensities_,
                                          name);
                        // If the document does not have an assay library linked to it, then ask if user wants to delete the one that we have found
                        var replaceLibraryResult = MultiButtonMsgDlg.Show(this, replaceLibraryMessage,
                            Resources.SkylineWindow_ImportMassList__Overwrite, Resources.SkylineWindow_ImportMassList__Skip, true);
                        if (replaceLibraryResult == DialogResult.Cancel)
                            return;
                        if (replaceLibraryResult == DialogResult.No)
                            librarySpectra.Clear();
                    }
                    if (librarySpectra.Any())
                    {
                        // Delete the existing library; either it's not tied to the document or we've already extracted the spectra
                        if (libraryExists)
                        {
                            FileEx.SafeDelete(outputPath);
                            FileEx.SafeDelete(Path.ChangeExtension(outputPath, BiblioSpecLiteSpec.EXT_REDUNDANT));
                        }
                        using (var blibDb = BlibDb.CreateBlibDb(outputPath))
                        {
                            docLibrarySpec = new BiblioSpecLiteSpec(name, outputPath);
                            using (var longWaitDlg = new LongWaitDlg(this) { Text = Resources.SkylineWindow_ImportMassList_Creating_Spectral_Library })
                            {
                                longWaitDlg.PerformWork(this, 1000, progressMonitor =>
                                {
                                    docLibrary = blibDb.CreateLibraryFromSpectra(docLibrarySpec, librarySpectra, name, progressMonitor);
                                    if (docLibrary == null)
                                        return;
                                    var newSettings = docNew.Settings.ChangePeptideLibraries(libs => libs.ChangeLibrary(docLibrary, docLibrarySpec, indexOldLibrary));
                                    var status = new ProgressStatus(Resources.SkylineWindow_ImportMassList_Finishing_up_import);
                                    progressMonitor.UpdateProgress(status);
                                    progressMonitor.UpdateProgress(status.ChangePercentComplete(100));
                                    docNew = docNew.ChangeSettings(newSettings);
                                });
                                if (docLibrary == null)
                                    return;
                            }
                        }
                    }
                }
            }

            ModifyDocument(description, doc =>
            {
                if (ReferenceEquals(doc, docCurrent))
                    return docNew;
                try
                {
                    // If the document was changed during the operation, try all the changes again
                    // using the information given by the user.
                    docCurrent = DocumentUI;
                    doc = doc.ImportMassList(inputs, insertPath, out selectPath);
                    if (irtInputs != null)
                    {
                        doc = doc.ImportMassList(irtInputs, null, out selectPath);
                    }
                    var newSettings = doc.Settings;
                    if (retentionTimeRegressionStore != null)
                    {
                        newSettings = newSettings.ChangePeptidePrediction(prediction =>
                            prediction.ChangeRetentionTime(retentionTimeRegressionStore));
                    }
                    if (docLibrarySpec != null)
                    {
                        newSettings = newSettings.ChangePeptideLibraries(libs =>
                            libs.ChangeLibrary(docLibrary, docLibrarySpec, indexOldLibrary));
                    }
                    if (!ReferenceEquals(doc.Settings, newSettings))
                        doc = doc.ChangeSettings(newSettings);
                }
                catch (Exception x)
                {
                    throw new InvalidDataException(string.Format(Resources.SkylineWindow_ImportMassList_Unexpected_document_change_during_operation___0_, x.Message, x));
                }
                return doc;
            });

            if (selectPath != null)
                SequenceTree.SelectedPath = selectPath;
            if (retentionTimeRegressionStore != null)
            {
                Settings.Default.RetentionTimeList.Add(retentionTimeRegressionStore);
                Settings.Default.RTScoreCalculatorList.Add(retentionTimeRegressionStore.Calculator);
            }
            if (docLibrarySpec != null)
            {
                Settings.Default.SpectralLibraryList.Insert(0, docLibrarySpec);
            }
        }
Esempio n. 25
0
        /// <summary>
        /// Make a BiblioSpec SQLite library from a list of spectra and their intensities.
        /// </summary>
        /// <param name="librarySpec">Library spec for which the new library is created</param>
        /// <param name="listSpectra">List of existing spectra, by LibKey</param>
        /// <param name="libraryName">Name of the library to be created</param>
        /// <param name="progressMonitor">Progress monitor to display progress in creating library</param>
        /// <returns>A library of type <see cref="BiblioSpecLiteLibrary"/></returns>
        public BiblioSpecLiteLibrary CreateLibraryFromSpectra(BiblioSpecLiteSpec librarySpec,
            List<SpectrumMzInfo> listSpectra,
            string libraryName,
            IProgressMonitor progressMonitor)
        {
            const string libAuthority = BiblioSpecLiteLibrary.DEFAULT_AUTHORITY;
            const int majorVer = 1;
            const int minorVer = 0;
            string libId = libraryName;
            // Use a very specific LSID, since it really only matches this document.
            string libLsid = string.Format("urn:lsid:{0}:spectral_libary:bibliospec:nr:minimal:{1}:{2}:{3}.{4}", // Not L10N
                libAuthority, libId, Guid.NewGuid(), majorVer, minorVer);

            var dictLibrary = new Dictionary<LibKey, BiblioLiteSpectrumInfo>();

            using (ISession session = OpenWriteSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                int progressPercent = 0;
                int i = 0;
                var status = new ProgressStatus(Resources.BlibDb_CreateLibraryFromSpectra_Creating_spectral_library_for_imported_transition_list);
                foreach (var spectrum in listSpectra)
                {
                    ++i;
                    var dbRefSpectrum = RefSpectrumFromPeaks(spectrum);
                    session.Save(dbRefSpectrum);
                    dictLibrary.Add(spectrum.Key,
                                    new BiblioLiteSpectrumInfo(spectrum.Key, dbRefSpectrum.Copies,
                                                                dbRefSpectrum.NumPeaks,
                                                                (int)(dbRefSpectrum.Id ?? 0),
                                                                default(IndexedRetentionTimes),
                                                                default(IndexedIonMobilities)));
                    if (progressMonitor != null)
                    {
                        if (progressMonitor.IsCanceled)
                            return null;
                        int progressNew = (i * 100 / listSpectra.Count);
                        if (progressPercent != progressNew)
                        {
                            progressMonitor.UpdateProgress(status = status.ChangePercentComplete(progressNew));
                            progressPercent = progressNew;
                        }
                    }
                }

                session.Flush();
                session.Clear();
                // Simulate ctime(d), which is what BlibBuild uses.
                string createTime = string.Format("{0:ddd MMM dd HH:mm:ss yyyy}", DateTime.Now); // Not L10N? different date/time format in different countries
                DbLibInfo libInfo = new DbLibInfo
                {
                    LibLSID = libLsid,
                    CreateTime = createTime,
                    NumSpecs = dictLibrary.Count,
                    MajorVersion = majorVer,
                    MinorVersion = minorVer
                };

                session.Save(libInfo);
                session.Flush();
                session.Clear();
                transaction.Commit();
            }

            var libraryEntries = dictLibrary.Values.ToArray();
            return new BiblioSpecLiteLibrary(librarySpec, libLsid, majorVer, minorVer,
                libraryEntries, FileStreamManager.Default);
        }
Esempio n. 26
0
        protected override void DoTest()
        {
            TestSmallMolecules = false; // The presence of the extra test node without any results is incompatible with what's being tested here.

            // Clean-up before running the test
            RunUI(() => SkylineWindow.ModifyDocument("Set default settings",
                                                     d => d.ChangeSettings(SrmSettingsList.GetDefault())));

            SrmDocument doc = SkylineWindow.Document;

            const string documentBaseName = "Ms1FilterTutorial";
            string       documentFile     = GetTestPath(documentBaseName + SrmDocument.EXT);

            RunUI(() => SkylineWindow.SaveDocument(documentFile));

            // show the empty Transition Setting dialog
            var transitionSettingsDlg = ShowDialog <TransitionSettingsUI>(SkylineWindow.ShowTransitionSettingsUI);

            RunUI(() => transitionSettingsDlg.SelectedTab = TransitionSettingsUI.TABS.FullScan);
            PauseForScreenShot <TransitionSettingsUI.FullScanTab>("Transition Settings - Full-Scan tab nothing set", 2);
            OkDialog(transitionSettingsDlg, transitionSettingsDlg.OkDialog);

            // Launch the wizard
            var importPeptideSearchDlg = ShowDialog <ImportPeptideSearchDlg>(SkylineWindow.ShowImportPeptideSearchDlg);

            // We're on the "Build Spectral Library" page of the wizard.
            // Add the test xml file to the search files list and try to
            // build the document library.
            string[] searchFiles =
            {
                GetTestPath("100803_0001_MCF7_TiB_L.group.xml"),      // Not L10N
                GetTestPath("100803_0005b_MCF7_TiTip3.group.xml")     // Not L10N
            };
            PauseForScreenShot <ImportPeptideSearchDlg.SpectraPage>("Import Peptide Search - Build Spectral Library empty page", 3);

            RunUI(() =>
            {
                Assert.IsTrue(importPeptideSearchDlg.CurrentPage ==
                              ImportPeptideSearchDlg.Pages.spectra_page);
                importPeptideSearchDlg.BuildPepSearchLibControl.AddSearchFiles(searchFiles);
            });
            PauseForScreenShot <ImportPeptideSearchDlg.SpectraPage>("Import Peptide Search - Build Spectral Library populated page", 4);

            var ambiguousDlg = ShowDialog <MessageDlg>(importPeptideSearchDlg.ClickNextButtonNoCheck);

            OkDialog(ambiguousDlg, ambiguousDlg.OkDialog);

            // Verify document library was built
            string docLibPath          = BiblioSpecLiteSpec.GetLibraryFileName(documentFile);
            string redundantDocLibPath = BiblioSpecLiteSpec.GetRedundantName(docLibPath);

            Assert.IsTrue(File.Exists(docLibPath) && File.Exists(redundantDocLibPath));
            var librarySettings = SkylineWindow.Document.Settings.PeptideSettings.Libraries;

            Assert.IsTrue(librarySettings.HasDocumentLibrary);

            // We're on the "Extract Chromatograms" page of the wizard.
            // All the test results files are in the same directory as the
            // document file, so all the files should be found, and we should
            // just be able to move to the next page.
            WaitForConditionUI(() => importPeptideSearchDlg.CurrentPage == ImportPeptideSearchDlg.Pages.chromatograms_page);
            PauseForScreenShot <ImportPeptideSearchDlg.ChromatogramsPage>("Import Peptide Search - Extract Chromatograms page", 5);

            var importResultsNameDlg = ShowDialog <ImportResultsNameDlg>(() => importPeptideSearchDlg.ClickNextButton());

            PauseForScreenShot <ImportResultsNameDlg>("Import Results - Common prefix form", 6);

            OkDialog(importResultsNameDlg, importResultsNameDlg.YesDialog);

            // Wait for the "Add Modifications" page of the wizard.
            WaitForConditionUI(() => importPeptideSearchDlg.CurrentPage == ImportPeptideSearchDlg.Pages.match_modifications_page);

            List <string> modsToCheck = new List <string> {
                "Phospho (ST)", "Phospho (Y)", "Oxidation (M)"
            };                                                                                              // Not L10N

            RunUI(() =>
            {
                importPeptideSearchDlg.MatchModificationsControl.CheckedModifications = modsToCheck;
            });
            PauseForScreenShot <ImportPeptideSearchDlg.MatchModsPage>("Import Peptide Search - Add Modifications page", 7);
            RunUI(() => Assert.IsTrue(importPeptideSearchDlg.ClickNextButton()));

            // We're on the "Configure MS1 Full-Scan Settings" page of the wizard.
            RunUI(() =>
            {
                Assert.IsTrue(importPeptideSearchDlg.CurrentPage == ImportPeptideSearchDlg.Pages.full_scan_settings_page);
                importPeptideSearchDlg.FullScanSettingsControl.PrecursorCharges      = new[] { 2, 3, 4 };
                importPeptideSearchDlg.FullScanSettingsControl.PrecursorMassAnalyzer = FullScanMassAnalyzerType.tof;
                importPeptideSearchDlg.FullScanSettingsControl.PrecursorRes          = 10 * 1000;

                Assert.AreEqual(importPeptideSearchDlg.FullScanSettingsControl.PrecursorIsotopesCurrent, FullScanPrecursorIsotopes.Count);
                Assert.AreEqual(FullScanMassAnalyzerType.tof, importPeptideSearchDlg.FullScanSettingsControl.PrecursorMassAnalyzer);
                Assert.AreEqual(10 * 1000, importPeptideSearchDlg.FullScanSettingsControl.PrecursorRes);
                Assert.AreEqual(3, importPeptideSearchDlg.FullScanSettingsControl.Peaks);
                Assert.AreEqual(RetentionTimeFilterType.ms2_ids, importPeptideSearchDlg.FullScanSettingsControl.RetentionTimeFilterType);
                Assert.AreEqual(5, importPeptideSearchDlg.FullScanSettingsControl.TimeAroundMs2Ids);
            });
            PauseForScreenShot <ImportPeptideSearchDlg.Ms1FullScanPage>("Import Peptide Search - Configure MS1 Full-Scan Settings page", 8);

            RunUI(() => Assert.IsTrue(importPeptideSearchDlg.ClickNextButton()));

            // Last page of wizard - Import Fasta.
            string fastaPath = GetTestPath("12_proteins.062011.fasta");

            RunUI(() =>
            {
                Assert.IsTrue(importPeptideSearchDlg.CurrentPage == ImportPeptideSearchDlg.Pages.import_fasta_page);
                Assert.AreEqual("Trypsin [KR | P]", importPeptideSearchDlg.ImportFastaControl.Enzyme.GetKey());
                importPeptideSearchDlg.ImportFastaControl.MaxMissedCleavages = 2;
                importPeptideSearchDlg.ImportFastaControl.SetFastaContent(fastaPath);
                Assert.IsFalse(importPeptideSearchDlg.ImportFastaControl.DecoyGenerationEnabled);
            });
            PauseForScreenShot <ImportPeptideSearchDlg.FastaPage>("Import Peptide Search - Import FASTA page", 10);

            var peptidesPerProteinDlg = ShowDialog <PeptidesPerProteinDlg>(() => importPeptideSearchDlg.ClickNextButton());

            WaitForCondition(() => peptidesPerProteinDlg.DocumentFinalCalculated);
            RunUI(() =>
            {
                int proteinCount, peptideCount, precursorCount, transitionCount;
                peptidesPerProteinDlg.NewTargetsAll(out proteinCount, out peptideCount, out precursorCount, out transitionCount);
                Assert.AreEqual(11, proteinCount);
                Assert.AreEqual(51, peptideCount);
                Assert.AreEqual(52, precursorCount);
                Assert.AreEqual(156, transitionCount);
                peptidesPerProteinDlg.NewTargetsFinal(out proteinCount, out peptideCount, out precursorCount, out transitionCount);
                Assert.AreEqual(11, proteinCount);
                Assert.AreEqual(51, peptideCount);
                Assert.AreEqual(52, precursorCount);
                Assert.AreEqual(156, transitionCount);
            });
            OkDialog(peptidesPerProteinDlg, peptidesPerProteinDlg.OkDialog);
            PauseForScreenShot <AllChromatogramsGraph>("Loading chromatograms window", 11);
            WaitForDocumentChangeLoaded(doc, 8 * 60 * 1000); // 10 minutes

            var libraryExplorer   = ShowDialog <ViewLibraryDlg>(() => SkylineWindow.OpenLibraryExplorer(documentBaseName));
            var matchedPepModsDlg = WaitForOpenForm <AddModificationsDlg>();

            PauseForScreenShot <MultiButtonMsgDlg>("Add mods alert", 12);
            RunUI(() =>
            {
                Assert.AreEqual(13, matchedPepModsDlg.NumMatched);
                Assert.AreEqual(0, matchedPepModsDlg.NumUnmatched);
                matchedPepModsDlg.CancelDialog();
            });
            RunUI(() =>
            {
                libraryExplorer.GraphSettings.ShowBIons        = true;
                libraryExplorer.GraphSettings.ShowYIons        = true;
                libraryExplorer.GraphSettings.ShowCharge1      = true;
                libraryExplorer.GraphSettings.ShowCharge2      = true;
                libraryExplorer.GraphSettings.ShowPrecursorIon = true;
            });

            PauseForScreenShot <ViewLibraryDlg>("Spectral Library Explorer", 13);
            RunUI(() =>
            {
                const string sourceFirst = "100803_0005b_MCF7_TiTip3.wiff";
                const double timeFirst   = 35.2128;
                Assert.AreEqual(sourceFirst, libraryExplorer.SourceFile);
                Assert.AreEqual(timeFirst, libraryExplorer.RetentionTime, 0.0001);
                libraryExplorer.SelectedIndex++;
                Assert.AreNotEqual(sourceFirst, libraryExplorer.SourceFile);
                Assert.AreNotEqual(timeFirst, libraryExplorer.RetentionTime, 0.0001);
            });
            OkDialog(libraryExplorer, libraryExplorer.CancelDialog);

            const int TIB_L = 0; // index for Tib_L
            const int TIP3  = 1; // index for Tip3

            AssertEx.IsDocumentState(SkylineWindow.Document, null, 11, 51, 52, 156);
            AssertResult.IsDocumentResultsState(SkylineWindow.Document, GetFileNameWithoutExtension(searchFiles[TIB_L]), 51, 52, 0, 156, 0);
            AssertResult.IsDocumentResultsState(SkylineWindow.Document, GetFileNameWithoutExtension(searchFiles[TIP3]), 51, 52, 0, 156, 0);
            string Tib_LFilename = searchFiles[TIB_L].Replace(".group.xml", PreferedExtAbWiff);
            string Tip3Filename  = searchFiles[TIP3].Replace(".group.xml", PreferedExtAbWiff);

            // Select the first transition group.
            RunUI(() =>
            {
                SkylineWindow.SequenceTree.SelectedPath =
                    SkylineWindow.Document.GetPathTo((int)SrmDocument.Level.Molecules, 0);
                SkylineWindow.GraphSpectrumSettings.ShowAIons        = true;
                SkylineWindow.GraphSpectrumSettings.ShowBIons        = true;
                SkylineWindow.GraphSpectrumSettings.ShowYIons        = true;
                SkylineWindow.GraphSpectrumSettings.ShowPrecursorIon = true;
                SkylineWindow.ExpandPrecursors();
                SkylineWindow.ChangeTextSize(TreeViewMS.LRG_TEXT_FACTOR);
            });
            RunDlg <SpectrumChartPropertyDlg>(SkylineWindow.ShowSpectrumProperties, dlg =>
            {
                dlg.FontSize = GraphFontSize.NORMAL;
                dlg.OkDialog();
            });
            RunDlg <ChromChartPropertyDlg>(SkylineWindow.ShowChromatogramProperties, dlg =>
            {
                dlg.FontSize = GraphFontSize.NORMAL;
                dlg.OkDialog();
            });
            RunUI(() =>
            {
                // Make window screenshot size
                if (IsPauseForScreenShots && SkylineWindow.WindowState != FormWindowState.Maximized)
                {
                    SkylineWindow.Width  = 1160;
                    SkylineWindow.Height = 792;
                }
            });
            RestoreViewOnScreen(13);
            PauseForScreenShot("Main window with imported data", 14);

//            RunUIWithDocumentWait(() =>
//            {
//                SkylineWindow.ToggleIntegrateAll(); // TODO: No longer necessary.  Change in tutorial
//            });
            RunUI(() =>
            {
                SkylineWindow.ShowGraphPeakArea(true);
                SkylineWindow.ShowPeakAreaReplicateComparison();
                SkylineWindow.NormalizeAreaGraphTo(AreaNormalizeToView.none);
                Settings.Default.ShowDotProductPeakArea = true;
                Settings.Default.ShowLibraryPeakArea    = true;
            });
            PauseForScreenShot <GraphSummary.AreaGraphView>("Peak Areas view (show context menu)", 16);

            RestoreViewOnScreen(15);
            RunUI(() =>
            {
                SkylineWindow.AutoZoomBestPeak();
                SkylineWindow.ArrangeGraphsTiled();
                SkylineWindow.ShowChromatogramLegends(false);
            });
            PauseForScreenShot("Main window layout", 17);

            int atest = 0;

            CheckAnnotations(TIB_L, 0, atest++);
            int pepIndex = 3;

            RunUI(() => SkylineWindow.CollapsePeptides());
            RunUI(() => SkylineWindow.ShowAlignedPeptideIDTimes(true));
            ChangePeakBounds(TIB_L, pepIndex, 38.79, 39.385);
            PauseForScreenShot("Chromatogram graphs clipped from main window", 19);
            CheckAnnotations(TIB_L, pepIndex, atest++);

            var alignmentForm = ShowDialog <AlignmentForm>(() => SkylineWindow.ShowRetentionTimeAlignmentForm());

            RunUI(() =>
            {
                alignmentForm.Width  = 711;
                alignmentForm.Height = 561;
                alignmentForm.ComboAlignAgainst.SelectedIndex = 0;     // to match what's in the tutorial doc
            });
            PauseForScreenShot <AlignmentForm>("Retention time alignment form", 20);

            OkDialog(alignmentForm, alignmentForm.Close);
            PauseForScreenShot("Status bar clipped from main window - 4/51 pep 4/52 prec 10/156 tran", 21);

            pepIndex = JumpToPeptide("SSKASLGSLEGEAEAEASSPK");
            RunUI(() => SkylineWindow.ShowChromatogramLegends(true));
            Assert.IsTrue(8 == pepIndex);
            PauseForScreenShot("Chromatogram graph metafiles for 9th peptide", 21);
            CheckAnnotations(TIB_L, pepIndex, atest++);

            ZoomSingle(TIP3, 32.6, 41.4); // simulate the wheel scroll described in tutorial
            PauseForScreenShot("Chromatogram graph metafile showing all peaks for 1_MCF_TiB_L", 22);
            CheckAnnotations(TIB_L, pepIndex, atest++);

            // current TIB_L peak should have idotp .87 and ppm -6.9
            Assert.AreEqual(0.87, GetTransitionGroupChromInfo(TIB_L, pepIndex).IsotopeDotProduct ?? -1, .005);
            Assert.AreEqual(-10.8, GetTransitionChromInfo(TIB_L, pepIndex, 0).MassError ?? -1, .05);

            ChangePeakBounds(TIB_L, pepIndex, 36.5, 38.0);

            // now current TIB_L peak should have idotp .9 and ppm -6.5
            Assert.AreEqual(0.9, GetTransitionGroupChromInfo(TIB_L, pepIndex).IsotopeDotProduct ?? -1, .005);
            Assert.AreEqual(-9.4, GetTransitionChromInfo(TIB_L, pepIndex, 0).MassError ?? -1, .05);
            CheckAnnotations(TIB_L, pepIndex, atest++);

            var undoIndex = SkylineWindow.Document.RevisionIndex; // preserve for simulating ctrl-z

            PickPeakBoth(pepIndex, 40.471035, 40.8134);           // select peak for both chromatograms at these respective retention times
            PauseForScreenShot <GraphSummary.AreaGraphView>("Peak Areas graph metafile", 23);

            int[] m1Thru4 = { 1, 2, 3, 4, 5 };
            PickTransitions(pepIndex, m1Thru4, "Transition pick list filtered", 24, "Transition pick list unfiltered", 24); // turn on chromatograms
            PickPeakBoth(pepIndex, 36.992836, 37.3896027);                                                                  // select peak for both chromatograms at these respective retention times
            ZoomSingle(TIP3, 32.4, 39.6);                                                                                   // set the view for screenshot
            PauseForScreenShot("Chromatogram graph metafile comparing 33 and 37 minute peaks", 25);
            CheckAnnotations(TIB_L, pepIndex, atest++);
            CheckAnnotations(TIP3, pepIndex, atest++);

            RevertDoc(undoIndex);                             // undo changes
            pepIndex = JumpToPeptide("ASLGSLEGEAEAEASSPKGK"); // Not L10N
            Assert.IsTrue(10 == pepIndex);
            PauseForScreenShot("Chhromatogram graph meta files for peptide ASLGSLEGEAEAEASSPKGK", 26);
            CheckAnnotations(TIB_L, pepIndex, atest++);
            CheckAnnotations(TIP3, pepIndex, atest++);

            PickTransitions(pepIndex, m1Thru4); // turn on M+3 and M+4
            ChangePeakBounds(TIP3, pepIndex, 37.35, 38.08);
            ZoomSingle(TIP3, 36.65, 39.11);     // simulate the wheel scroll described in tutorial
            PauseForScreenShot("upper - Chromatogram graph metafile for peptide ASLGSLEGEAEAEASSPKGK with adjusted integration", 27);
            CheckAnnotations(TIP3, pepIndex, atest++);

            RevertDoc(undoIndex);                              // undo changes
            pepIndex = JumpToPeptide("AEGEWEDQEALDYFSDKESGK"); // Not L10N
            PauseForScreenShot("lower - Chromatogram graph metafiles for peptide AEGEWEDQEALDYFSDKESGK", 27);
            CheckAnnotations(TIB_L, pepIndex, atest++);
            CheckAnnotations(TIP3, pepIndex, atest++);

            int[] m1Thru5 = { 1, 2, 3, 4, 5, 6 };
            PickTransitions(pepIndex, m1Thru5); // turn on M+3 M+4 and M+5
            PauseForScreenShot("Chromatogram graph metafiles with M+3, M+4 and M+5 added", 28);
            CheckAnnotations(TIB_L, pepIndex, atest++);
            CheckAnnotations(TIP3, pepIndex, atest++);

            JumpToPeptide("ALVEFESNPEETREPGSPPSVQR"); // Not L10N
            PauseForScreenShot("Chromatogram graph metafiles for peptide ALVEFESNPEETREPGSPPSVQR", 29);

            pepIndex = JumpToPeptide("YGPADVEDTTGSGATDSKDDDDIDLFGSDDEEESEEAKR"); // Not L10N
            PauseForScreenShot("upper - Peak Areas graph metafile for peptide YGPADVEDTTGSGATDSKDDDDIDLFGSDDEEESEEAKR", 30);

            int[] m1Thru7 = { 1, 2, 3, 4, 5, 6, 7, 8 };
            PickTransitions(pepIndex, m1Thru7); // enable [M+3] [M+4] [M+5] [M+6] [M+7]
            PauseForScreenShot("lower - Peak Areas graph metafile with M+3 through M+7 added", 30);
            CheckAnnotations(TIB_L, pepIndex, atest++);
            CheckAnnotations(TIP3, pepIndex, atest++);

            // page 32 zooming setup
            RunUI(() =>
            {
                SkylineWindow.SynchronizeZooming(true);
                SkylineWindow.LockYChrom(false);
                SkylineWindow.AlignToFile = SkylineWindow.GraphChromatograms.ToArray()[TIP3].GetChromFileInfoId(); // align to Tip3
            });
            ZoomBoth(36.5, 39.5, 1600);                                                                            // simulate the wheel scroll described in tutorial
            RunUI(() => SkylineWindow.ShowChromatogramLegends(false));
            PauseForScreenShot("Chromatogram graphs clipped from main window with synchronized zooming", 32);

            RestoreViewOnScreen(33);                                                                                                      // float the  Library Match window TODO this causes a crash at next call to ChangePeakBounds, in pwiz.Skyline.Controls.Graphs.GraphChromatogram.ChromGroupInfos.get() Line 492 , why?
            RunUI(() => SkylineWindow.GraphSpectrum.SelectSpectrum(new SpectrumIdentifier(MsDataFileUri.Parse(Tip3Filename), 37.6076f))); // set the Library Match view
            PauseForScreenShot <GraphSpectrum>("Library Match graph metafile - 5b_MCF7_TiTip3 (37.61 Min)", 33);

            RunUI(() => SkylineWindow.GraphSpectrum.SelectSpectrum(new SpectrumIdentifier(MsDataFileUri.Parse(Tib_LFilename), 37.0335f))); // set the Library Match view
            PauseForScreenShot <GraphSpectrum>("Library Match graph metafile - 1_MCF_TiB_L (37.03 min)", 33);

            RestoreViewOnScreen(34);                    // back to normal view

            pepIndex = JumpToPeptide("GVVDSEDLPLNISR"); // Not L10N
            RunUI(() => SkylineWindow.ShowChromatogramLegends(true));
            PauseForScreenShot("upper - Chromatogram graph metafiles for peptide GVVDSEDLPLNISR", 34);

            ZoomBoth(35.05, 36.9, 480);
            PauseForScreenShot("lower - Chromatogram graph metafile - effect of zoom ", 34);
            ChangePeakBounds(TIP3, pepIndex, 35.7, 36.5);  // adjust integration per tutorial
            CheckAnnotations(TIP3, pepIndex, atest++);     // check the new idotp values

            /* pepIndex = */ JumpToPeptide("DQVANSAFVER"); // Not L10N
            PauseForScreenShot("Chromatogram graph metafiles for peptide DQVANSAFVER", 35);

//            int[] m1 = {2};
//            PickTransitions(pepIndex, m1); // enable [M+1] only
//            // Measured times in TIB_L are different from displayed times, because of alignment
//            ChangePeakBounds(TIB_L, pepIndex, 23.99, 25.29);
//            ChangePeakBounds(TIP3, pepIndex, 23.81, 25.21);
//            // First transition selected for screenshot
//            RunUI(() =>
//            {
//                var pathPep = SkylineWindow.SelectedPath;
//                var nodePep = ((PeptideTreeNode)SkylineWindow.SelectedNode).DocNode;
//                var nodeGroup = nodePep.TransitionGroups.First();
//                var nodeTran = nodeGroup.Transitions.First();
//                SkylineWindow.SelectedPath = new IdentityPath(
//                    new IdentityPath(pathPep, nodeGroup.TransitionGroup), nodeTran.Transition);
//            });
//            PauseForScreenShot("page 36 - M+1 only, with adjusted integration");
//            CheckAnnotations(TIB_L, pepIndex, atest++);
//            CheckAnnotations(TIP3, pepIndex, EXPECTED_ANNOTATIONS[atest]);

            var docAfter = WaitForProteinMetadataBackgroundLoaderCompletedUI();

            // Minimizing a chromatogram cache file.
            RunUI(SkylineWindow.CollapsePeptides);
            for (int i = 0; i < 5; i++) // just do the first 5
            {
                int iPeptide = i;
                var path     = docAfter.GetPathTo((int)SrmDocument.Level.Molecules, iPeptide);
                RunUI(() =>
                {
                    SkylineWindow.SelectedPath = path;
                });
                WaitForGraphs();
            }

            // Eliminate extraneous chromatogram data.
            doc = WaitForProteinMetadataBackgroundLoaderCompletedUI();
            var minimizedFile = GetTestPath("Ms1FilteringTutorial-2min.sky"); // Not L10N
            var cacheFile     = Path.ChangeExtension(minimizedFile, ChromatogramCache.EXT);

            {
                // TODO: Figure out why the minimize fails to unlock the .skyd file, if not minimized to current file
                RunUI(() => SkylineWindow.SaveDocument(minimizedFile));

                var manageResultsDlg   = ShowDialog <ManageResultsDlg>(SkylineWindow.ManageResults);
                var minimizeResultsDlg = ShowDialog <MinimizeResultsDlg>(manageResultsDlg.MinimizeResults);
                RunUI(() =>
                {
                    minimizeResultsDlg.LimitNoiseTime = true;
                    minimizeResultsDlg.NoiseTimeRange = 2;                                                        // Not L10N
                });
                PauseForScreenShot <MinimizeResultsDlg>("Minimize Results form (percentages vary slightly)", 36); // old p. 23

                OkDialog(minimizeResultsDlg, () => minimizeResultsDlg.MinimizeToFile(minimizedFile));
                WaitForCondition(() => File.Exists(cacheFile));
                WaitForClosedForm(manageResultsDlg);
            }
            WaitForDocumentChange(doc);

            // Inclusion list method export for MS1 filtering
            doc = SkylineWindow.Document;
            RunDlg <PeptideSettingsUI>(() => SkylineWindow.ShowPeptideSettingsUI(PeptideSettingsUI.TABS.Prediction), dlg =>
            {
                dlg.IsUseMeasuredRT = true;
                dlg.TimeWindow      = 10;
                dlg.OkDialog();
            });
            WaitForDocumentChangeLoaded(doc);

            // Now deviating from the tutorial script for a moment to make sure we can choose a Scheduled export method.
            RunDlg <RefineDlg>(SkylineWindow.ShowRefineDlg, dlg =>
            {
                dlg.MinPeptides = 1; // Not L10N
                const double minPeakFoundRatio = 0.1;
                dlg.MinPeakFoundRatio          = minPeakFoundRatio;
                dlg.OkDialog();
            });

            // Ready to export, although we will just cancel out of the dialog.
            var exportMethodDlg = ShowDialog <ExportMethodDlg>(() => SkylineWindow.ShowExportMethodDialog(ExportFileType.Method));

            RunUI(() =>
            {
                exportMethodDlg.InstrumentType = ExportInstrumentType.ABI_TOF; // Not L10N
                exportMethodDlg.MethodType     = ExportMethodType.Scheduled;
                exportMethodDlg.CancelButton.PerformClick();
            });
            WaitForClosedForm(exportMethodDlg);

            RunUI(() => SkylineWindow.SaveDocument());
            RunUI(SkylineWindow.NewDocument);
        }
Esempio n. 27
0
        /// <summary>
        /// Minimizes all libraries in a document to produce a new document with
        /// just the library information necessary for the spectra referenced by
        /// the nodes in the document.
        /// </summary>
        /// <param name="document">Document for which to minimize library information</param>
        /// <param name="pathDirectory">Directory into which new minimized libraries are built</param>
        /// <param name="nameModifier">A name modifier to append to existing names for
        ///     full libraries to create new library names</param>
        /// <param name="progressMonitor">Broker to communicate status and progress</param>
        /// <returns>A new document instance with minimized libraries</returns>
        public static SrmDocument MinimizeLibraries(SrmDocument document,
                                                    string pathDirectory, string nameModifier, IProgressMonitor progressMonitor)
        {
            var settings     = document.Settings;
            var pepLibraries = settings.PeptideSettings.Libraries;

            if (!pepLibraries.HasLibraries)
            {
                return(document);
            }
            if (!pepLibraries.IsLoaded)
            {
                throw new InvalidOperationException(Resources.BlibDb_MinimizeLibraries_Libraries_must_be_fully_loaded_before_they_can_be_minimzed);
            }

            // Separate group nodes by the libraries to which they refer
            var setUsedLibrarySpecs = new HashSet <LibrarySpec>();

            foreach (var librarySpec in pepLibraries.LibrarySpecs)
            {
                string libraryName = librarySpec.Name;
                if (document.MoleculeTransitionGroups.Contains(nodeGroup =>
                                                               nodeGroup.HasLibInfo && Equals(nodeGroup.LibInfo.LibraryName, libraryName)))
                {
                    setUsedLibrarySpecs.Add(librarySpec);
                }
            }

            var listLibraries    = new List <Library>();
            var listLibrarySpecs = new List <LibrarySpec>();
            var dictOldNameToNew = new Dictionary <string, string>();

            if (setUsedLibrarySpecs.Count > 0)
            {
                Directory.CreateDirectory(pathDirectory);

                var usedNames = new HashSet <string>();
                for (int i = 0; i < pepLibraries.LibrarySpecs.Count; i++)
                {
                    var librarySpec = pepLibraries.LibrarySpecs[i];
                    if (!setUsedLibrarySpecs.Contains(librarySpec))
                    {
                        continue;
                    }

                    string baseName = Path.GetFileNameWithoutExtension(librarySpec.FilePath);
                    string fileName = GetUniqueName(baseName, usedNames) + BiblioSpecLiteSpec.EXT;
                    using (var blibDb = CreateBlibDb(Path.Combine(pathDirectory, fileName)))
                    {
                        blibDb.ProgressMonitor = progressMonitor;
                        var librarySpecMin = librarySpec as BiblioSpecLiteSpec;
                        if (librarySpecMin == null || !librarySpecMin.IsDocumentLibrary)
                        {
                            string nameMin = librarySpec.Name;
                            // Avoid adding the modifier a second time, if it has
                            // already been done once.
                            if (!nameMin.EndsWith(nameModifier + ")"))                                // Not L10N
                            {
                                nameMin = string.Format("{0} ({1})", librarySpec.Name, nameModifier); // Not L10N
                            }
                            librarySpecMin = new BiblioSpecLiteSpec(nameMin, blibDb.FilePath);
                        }

                        listLibraries.Add(blibDb.MinimizeLibrary(librarySpecMin,
                                                                 pepLibraries.Libraries[i], document));

                        // Terminate if user canceled
                        if (progressMonitor != null && progressMonitor.IsCanceled)
                        {
                            return(document);
                        }

                        listLibrarySpecs.Add(librarySpecMin);
                        dictOldNameToNew.Add(librarySpec.Name, librarySpecMin.Name);
                    }
                }

                document = (SrmDocument)document.ChangeAll(node =>
                {
                    var nodeGroup = node as TransitionGroupDocNode;
                    if (nodeGroup == null || !nodeGroup.HasLibInfo)
                    {
                        return(node);
                    }

                    string libName    = nodeGroup.LibInfo.LibraryName;
                    string libNameNew = dictOldNameToNew[libName];
                    if (Equals(libName, libNameNew))
                    {
                        return(node);
                    }
                    var libInfo = nodeGroup.LibInfo.ChangeLibraryName(libNameNew);
                    return(nodeGroup.ChangeLibInfo(libInfo));
                },
                                                           (int)SrmDocument.Level.TransitionGroups);
            }

            return(document.ChangeSettingsNoDiff(settings.ChangePeptideLibraries(
                                                     lib => lib.ChangeLibraries(listLibrarySpecs, listLibraries))));
        }
Esempio n. 28
0
        /// <summary>
        /// Minimize any library type to a fully functional BiblioSpec SQLite library.
        /// </summary>
        /// <param name="librarySpec">Library spec for which the new library is created</param>
        /// <param name="library">Existing library to minimize</param>
        /// <param name="document">Document for which only used spectra are included in the new library</param>
        /// <returns>A new minimized <see cref="BiblioSpecLiteLibrary"/></returns>
        public BiblioSpecLiteLibrary MinimizeLibrary(BiblioSpecLiteSpec librarySpec,
                                                     Library library, SrmDocument document)
        {
            if (!UpdateProgressMessage(string.Format(Resources.BlibDb_MinimizeLibrary_Minimizing_library__0__, library.Name)))
            {
                return(null);
            }

            string libAuthority = "unknown.org"; // Not L10N
            string libId        = library.Name;
            // CONSIDER: Use version numbers of the original library?
            int libraryRevision = DbLibInfo.INITIAL_LIBRARY_REVISION;
            int schemaVersion   = 0;

            bool saveRetentionTimes = false;
            bool saveRedundantLib   = false;

            var blibLib = library as BiblioSpecLiteLibrary;

            if (blibLib != null)
            {
                string libraryLsid = blibLib.Lsid;
                Match  matchLsid   = REGEX_LSID.Match(libraryLsid);
                if (matchLsid.Success)
                {
                    libAuthority = matchLsid.Groups[1].Value;
                    libId        = matchLsid.Groups[2].Value;
                }
                else
                {
                    libAuthority = BiblioSpecLiteLibrary.DEFAULT_AUTHORITY;
                }

                // We will have a RetentionTimes table if schemaVersion if 1 or greater.
                saveRetentionTimes = blibLib.SchemaVersion >= 1;
                libraryRevision    = blibLib.Revision;
                schemaVersion      = Math.Min(blibLib.SchemaVersion, DbLibInfo.SCHEMA_VERSION_CURRENT);

                // If the document has MS1 filtering enabled we will save a minimized version
                // of the redundant library, if available.
                if (document.Settings.TransitionSettings.FullScan.IsEnabledMs)
                {
                    String redundantLibPath = blibLib.FilePathRedundant;
                    if (File.Exists(redundantLibPath))
                    {
                        string path = BiblioSpecLiteSpec.GetRedundantName(FilePath);
                        CreateSessionFactory_Redundant(path);
                        saveRedundantLib = true;
                    }
                }
            }
            else if (library is BiblioSpecLibrary)
            {
                libAuthority = BiblioSpecLiteLibrary.DEFAULT_AUTHORITY;
            }
            else if (library is XHunterLibrary)
            {
                libAuthority = XHunterLibrary.DEFAULT_AUTHORITY;
            }
            else
            {
                var nistLibrary = library as NistLibrary;
                if (nistLibrary != null)
                {
                    libAuthority = NistLibrary.DEFAULT_AUTHORITY;
                    libId        = nistLibrary.Id ?? libId;
                }
            }
            // Use a very specific LSID, since it really only matches this document.
            string libLsid = string.Format("urn:lsid:{0}:spectral_libary:bibliospec:nr:minimal:{1}:{2}:{3}.{4}", // Not L10N
                                           libAuthority, libId, Guid.NewGuid(), libraryRevision, schemaVersion);

            var dictLibrary = new Dictionary <LibKey, BiblioLiteSpectrumInfo>();

            // Hash table to store the database IDs of any source files in the library
            // Source file information is available only in Bibliospec libraries, schema version >= 1
            var dictFiles          = new Dictionary <string, long>();
            var dictFilesRedundant = new Dictionary <string, long>();

            ISession     redundantSession      = null;
            ITransaction redundantTransaction  = null;
            int          redundantSpectraCount = 0;

            try
            {
                using (ISession session = OpenWriteSession())
                    using (ITransaction transaction = session.BeginTransaction())
                    {
                        var settings = document.Settings;

                        int peptideCount = document.PeptideCount;
                        int savedCount   = 0;

                        foreach (var nodePep in document.Peptides)
                        {
                            var mods = nodePep.ExplicitMods;

                            foreach (TransitionGroupDocNode nodeGroup in nodePep.Children)
                            {
                                // Only get library info from precursors that use the desired library
                                if (!nodeGroup.HasLibInfo || !Equals(nodeGroup.LibInfo.LibraryName, library.Name))
                                {
                                    continue;
                                }

                                TransitionGroup  group           = nodeGroup.TransitionGroup;
                                string           peptideSeq      = group.Peptide.Sequence;
                                int              precursorCharge = group.PrecursorCharge;
                                IsotopeLabelType labelType       = nodeGroup.TransitionGroup.LabelType;


                                var calcPre       = settings.GetPrecursorCalc(labelType, mods);
                                var peptideModSeq = calcPre.GetModifiedSequence(peptideSeq, false);
                                var libKey        = new LibKey(peptideModSeq, precursorCharge);

                                if (dictLibrary.ContainsKey(libKey))
                                {
                                    continue;
                                }


                                // saveRetentionTimes will be false unless this is a BiblioSpec(schemaVersion >=1) library.
                                if (!saveRetentionTimes)
                                {
                                    // get the best spectra
                                    foreach (var spectrumInfo in library.GetSpectra(libKey, labelType, LibraryRedundancy.best))
                                    {
                                        DbRefSpectra refSpectra = MakeRefSpectrum(spectrumInfo,
                                                                                  peptideSeq,
                                                                                  peptideModSeq,
                                                                                  nodeGroup.PrecursorMz,
                                                                                  precursorCharge);

                                        session.Save(refSpectra);

                                        dictLibrary.Add(libKey,
                                                        new BiblioLiteSpectrumInfo(libKey, refSpectra.Copies,
                                                                                   refSpectra.NumPeaks,
                                                                                   (int)(refSpectra.Id ?? 0),
                                                                                   default(IndexedRetentionTimes),
                                                                                   default(IndexedIonMobilities)));
                                    }

                                    session.Flush();
                                    session.Clear();
                                }
                                // This is a BiblioSpec(schemaVersion >=1) library.
                                else
                                {
                                    // get all the spectra, including the redundant ones if this library has any
                                    var spectra = library.GetSpectra(libKey, labelType, LibraryRedundancy.all_redundant).ToArray();
                                    // Avoid saving to the RefSpectra table for isotope label types that have no spectra
                                    if (spectra.Length == 0)
                                    {
                                        continue;
                                    }

                                    DbRefSpectra refSpectra = new DbRefSpectra
                                    {
                                        PeptideSeq      = peptideSeq,
                                        PrecursorMZ     = nodeGroup.PrecursorMz,
                                        PrecursorCharge = precursorCharge,
                                        PeptideModSeq   = peptideModSeq
                                    };

                                    // Get all the information for this reference spectrum.
                                    // For BiblioSpec (schema ver >= 1), this can include retention time information
                                    // for this spectrum as well as any redundant spectra for the peptide.
                                    // Ids of spectra in the redundant library, where available, are also returned.
                                    var redundantSpectraKeys = new List <SpectrumKeyTime>();
                                    BuildRefSpectra(document, session, refSpectra, spectra, dictFiles, redundantSpectraKeys);

                                    session.Save(refSpectra);
                                    session.Flush();
                                    session.Clear();

                                    // TODO(nicksh): preserve retention time information.
                                    var retentionTimesByFileId = default(IndexedRetentionTimes);
                                    var driftTimesByFileId     = default(IndexedIonMobilities);
                                    dictLibrary.Add(libKey,
                                                    new BiblioLiteSpectrumInfo(libKey,
                                                                               refSpectra.Copies,
                                                                               refSpectra.NumPeaks,
                                                                               (int)(refSpectra.Id ?? 0),
                                                                               retentionTimesByFileId,
                                                                               driftTimesByFileId));

                                    // Save entries in the redundant library.
                                    if (saveRedundantLib && redundantSpectraKeys.Count > 0)
                                    {
                                        if (redundantSession == null)
                                        {
                                            redundantSession     = OpenWriteSession_Redundant();
                                            redundantTransaction = redundantSession.BeginTransaction();
                                        }
                                        SaveRedundantSpectra(redundantSession, redundantSpectraKeys, dictFilesRedundant, refSpectra, library);
                                        redundantSpectraCount += redundantSpectraKeys.Count;
                                    }
                                }
                            }

                            savedCount++;
                            if (!UpdateProgress(peptideCount, savedCount))
                            {
                                return(null);
                            }
                        }

                        // Simulate ctime(d), which is what BlibBuild uses.
                        string    createTime = string.Format("{0:ddd MMM dd HH:mm:ss yyyy}", DateTime.Now); // Not L10N? different date/time format in different countries
                        DbLibInfo libInfo    = new DbLibInfo
                        {
                            LibLSID      = libLsid,
                            CreateTime   = createTime,
                            NumSpecs     = dictLibrary.Count,
                            MajorVersion = libraryRevision,
                            MinorVersion = schemaVersion
                        };

                        session.Save(libInfo);
                        session.Flush();
                        session.Clear();

                        transaction.Commit();

                        if (redundantTransaction != null)
                        {
                            var scoreType = new DbScoreTypes {
                                Id = 0, ScoreType = "UNKNOWN"
                            };                                                            // Not L10N
                            redundantSession.Save(scoreType);

                            libInfo = new DbLibInfo
                            {
                                LibLSID      = libLsid.Replace(":nr:", ":redundant:"),      // Not L10N
                                CreateTime   = createTime,
                                NumSpecs     = redundantSpectraCount,
                                MajorVersion = libraryRevision,
                                MinorVersion = schemaVersion
                            };
                            redundantSession.Save(libInfo);
                            redundantSession.Flush();
                            redundantSession.Clear();

                            redundantTransaction.Commit();
                        }
                    }
            }
            finally
            {
                if (redundantTransaction != null)
                {
                    redundantTransaction.Dispose();
                }
                if (redundantSession != null)
                {
                    redundantSession.Dispose();
                }
            }

            var libraryEntries = dictLibrary.Values.ToArray();

            return(new BiblioSpecLiteLibrary(librarySpec, libLsid, libraryRevision, schemaVersion,
                                             libraryEntries, FileStreamManager.Default));
        }
Esempio n. 29
0
        /// <summary>
        /// Make a BiblioSpec SQLite library from a list of spectra and their intensities.
        /// </summary>
        /// <param name="librarySpec">Library spec for which the new library is created</param>
        /// <param name="listSpectra">List of existing spectra, by LibKey</param>
        /// <param name="libraryName">Name of the library to be created</param>
        /// <param name="progressMonitor">Progress monitor to display progress in creating library</param>
        /// <returns>A library of type <see cref="BiblioSpecLiteLibrary"/></returns>
        public BiblioSpecLiteLibrary CreateLibraryFromSpectra(BiblioSpecLiteSpec librarySpec,
                                                              List <SpectrumMzInfo> listSpectra,
                                                              string libraryName,
                                                              IProgressMonitor progressMonitor)
        {
            const string libAuthority = BiblioSpecLiteLibrary.DEFAULT_AUTHORITY;
            const int    majorVer     = 1;
            const int    minorVer     = 0;
            string       libId        = libraryName;
            // Use a very specific LSID, since it really only matches this document.
            string libLsid = string.Format("urn:lsid:{0}:spectral_libary:bibliospec:nr:minimal:{1}:{2}:{3}.{4}", // Not L10N
                                           libAuthority, libId, Guid.NewGuid(), majorVer, minorVer);

            var dictLibrary = new Dictionary <LibKey, BiblioLiteSpectrumInfo>();

            using (ISession session = OpenWriteSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    int progressPercent = 0;
                    int i      = 0;
                    var status = new ProgressStatus(Resources.BlibDb_CreateLibraryFromSpectra_Creating_spectral_library_for_imported_transition_list);
                    foreach (var spectrum in listSpectra)
                    {
                        ++i;
                        var dbRefSpectrum = RefSpectrumFromPeaks(spectrum);
                        session.Save(dbRefSpectrum);
                        dictLibrary.Add(spectrum.Key,
                                        new BiblioLiteSpectrumInfo(spectrum.Key, dbRefSpectrum.Copies,
                                                                   dbRefSpectrum.NumPeaks,
                                                                   (int)(dbRefSpectrum.Id ?? 0),
                                                                   default(IndexedRetentionTimes),
                                                                   default(IndexedIonMobilities)));
                        if (progressMonitor != null)
                        {
                            if (progressMonitor.IsCanceled)
                            {
                                return(null);
                            }
                            int progressNew = (i * 100 / listSpectra.Count);
                            if (progressPercent != progressNew)
                            {
                                progressMonitor.UpdateProgress(status = status.ChangePercentComplete(progressNew));
                                progressPercent = progressNew;
                            }
                        }
                    }

                    session.Flush();
                    session.Clear();
                    // Simulate ctime(d), which is what BlibBuild uses.
                    string    createTime = string.Format("{0:ddd MMM dd HH:mm:ss yyyy}", DateTime.Now); // Not L10N? different date/time format in different countries
                    DbLibInfo libInfo    = new DbLibInfo
                    {
                        LibLSID      = libLsid,
                        CreateTime   = createTime,
                        NumSpecs     = dictLibrary.Count,
                        MajorVersion = majorVer,
                        MinorVersion = minorVer
                    };

                    session.Save(libInfo);
                    session.Flush();
                    session.Clear();
                    transaction.Commit();
                }

            var libraryEntries = dictLibrary.Values.ToArray();

            return(new BiblioSpecLiteLibrary(librarySpec, libLsid, majorVer, minorVer,
                                             libraryEntries, FileStreamManager.Default));
        }
Esempio n. 30
0
        protected override void DoTest()
        {
            string skyfile = TestFilesDir.GetTestPath("test_pasef_mascot.sky");

            RunUI(() =>
            {
                SkylineWindow.NewDocument(true);
                SkylineWindow.SaveDocument(skyfile);
            });



            Stopwatch loadStopwatch = new Stopwatch();

            loadStopwatch.Start();

            // Enable use of drift times in spectral library
            var peptideSettingsUI = ShowDialog <PeptideSettingsUI>(SkylineWindow.ShowPeptideSettingsUI);

            RunUI(() =>
            {
                // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                peptideSettingsUI.IsUseSpectralLibraryDriftTimes         = true;
                peptideSettingsUI.SpectralLibraryDriftTimeResolvingPower = 50;
            });
            OkDialog(peptideSettingsUI, peptideSettingsUI.OkDialog);

            // Launch import peptide search wizard
            var importPeptideSearchDlg = ShowDialog <ImportPeptideSearchDlg>(SkylineWindow.ShowImportPeptideSearchDlg);
            var nextFile      = "FZGC A 100 ng_Slot1-46_01_440.d";
            var searchResults = GetTestPath("F264099.dat");

            var doc = SkylineWindow.Document;

            var searchResultsList = new[] { searchResults };

            RunUI(() =>
            {
                Assert.IsTrue(importPeptideSearchDlg.CurrentPage ==
                              ImportPeptideSearchDlg.Pages.spectra_page);
                importPeptideSearchDlg.BuildPepSearchLibControl.AddSearchFiles(searchResultsList);
                importPeptideSearchDlg.BuildPepSearchLibControl.CutOffScore = 0.95;
                importPeptideSearchDlg.BuildPepSearchLibControl.FilterForDocumentPeptides = false;
            });
            var ambiguousDlg = ShowDialog <MessageDlg>(importPeptideSearchDlg.ClickNextButtonNoCheck); // Expect the ambiguous matches dialog

            OkDialog(ambiguousDlg, ambiguousDlg.OkDialog);
            doc = WaitForDocumentChange(doc);

            // Verify document library was built
            string docLibPath          = BiblioSpecLiteSpec.GetLibraryFileName(skyfile);
            string redundantDocLibPath = BiblioSpecLiteSpec.GetRedundantName(docLibPath);

            Assert.IsTrue(File.Exists(docLibPath) && File.Exists(redundantDocLibPath));
            var librarySettings = SkylineWindow.Document.Settings.PeptideSettings.Libraries;

            Assert.IsTrue(librarySettings.HasDocumentLibrary);
            // We're on the "Extract Chromatograms" page of the wizard.
            // All the files should be found, and we should
            // just be able to move to the next page.
            RunUI(() => Assert.IsTrue(importPeptideSearchDlg.CurrentPage == ImportPeptideSearchDlg.Pages.chromatograms_page));
            RunUI(() =>
            {
                var importResultsControl = (ImportResultsControl)importPeptideSearchDlg.ImportResultsControl;
                importResultsControl.ExcludeSpectrumSourceFiles = false;
                importResultsControl.UpdateResultsFiles(new [] { TestFilesDirs[0].PersistentFilesDir }, true); // Go look in the persistent files dir
            });
            if (searchResultsList.Length > 1)
            {
                // Deal with the common name start dialog
                var importResultsNameDlg = ShowDialog <ImportResultsNameDlg>(importPeptideSearchDlg.ClickNextButtonNoCheck);
                RunUI(() =>
                {
                    importResultsNameDlg.NoDialog();
                });
                WaitForClosedForm(importResultsNameDlg);
            }
            else
            {
                RunUI(() => importPeptideSearchDlg.ClickNextButtonNoCheck());
            }
            // Modifications are already set up, so that page should get skipped.
            RunUI(() => importPeptideSearchDlg.FullScanSettingsControl.PrecursorCharges      = new [] { 2, 3, 4, 5 });
            RunUI(() => importPeptideSearchDlg.FullScanSettingsControl.PrecursorMassAnalyzer = FullScanMassAnalyzerType.tof);
            RunUI(() => importPeptideSearchDlg.FullScanSettingsControl.UseSpectralLibraryIonMobilityValuesControl.SetUseSpectralLibraryDriftTimes(true));

            // Verify error handling in ion mobility control
            RunUI(() => importPeptideSearchDlg.FullScanSettingsControl.UseSpectralLibraryIonMobilityValuesControl.SetResolvingPowerText("fish"));
            var errDlg = ShowDialog <MessageDlg>(importPeptideSearchDlg.ClickNextButtonNoCheck);

            RunUI(() => errDlg.OkDialog());

            RunUI(() => importPeptideSearchDlg.FullScanSettingsControl.UseSpectralLibraryIonMobilityValuesControl.SetResolvingPower(40));

            RunUI(() => importPeptideSearchDlg.ClickNextButton()); // Accept the full scan settings
            // We're on the "Import FASTA" page of the wizard.
            RunUI(() =>
            {
                Assert.IsTrue(importPeptideSearchDlg.CurrentPage == ImportPeptideSearchDlg.Pages.import_fasta_page);
                importPeptideSearchDlg.ImportFastaControl.SetFastaContent(GetTestPath("human_and_yeast.fasta"));
            });
            var peptidesPerProteinDlg = ShowDialog <PeptidesPerProteinDlg>(importPeptideSearchDlg.ClickNextButtonNoCheck);

            WaitForConditionUI(() => peptidesPerProteinDlg.DocumentFinalCalculated);
            OkDialog(peptidesPerProteinDlg, peptidesPerProteinDlg.OkDialog);

            WaitForClosedForm(importPeptideSearchDlg);
            var doc1 = WaitForDocumentChangeLoaded(doc, 15 * 60 * 1000); // 15 minutes

            AssertEx.IsDocumentState(doc1, null, 7906, 28510, 28510, 85530);
            loadStopwatch.Stop();
            DebugLog.Info("load time = {0}", loadStopwatch.ElapsedMilliseconds);
            var errmsg = "";

            LibraryIonMobilityInfo libraryIonMobilityInfo;

            doc1.Settings.PeptideSettings.Libraries.Libraries.First().TryGetIonMobilityInfos(0, out libraryIonMobilityInfo);
            var driftInfoExplicitDT = libraryIonMobilityInfo;
            var instrumentInfo      = new DataFileInstrumentInfo(new MsDataFileImpl(GetTestPath(nextFile)));
            var dictExplicitDT      = driftInfoExplicitDT.GetIonMobilityDict();

            foreach (var pep in doc1.Peptides)
            {
                foreach (var nodeGroup in pep.TransitionGroups)
                {
                    double windowDT;
                    var    calculatedDriftTime = doc1.Settings.GetIonMobility(
                        pep, nodeGroup, libraryIonMobilityInfo, instrumentInfo, 0, out windowDT);
                    var libKey = new LibKey(pep.ModifiedSequence, nodeGroup.PrecursorAdduct);
                    IonMobilityAndCCS[] infoValueExplicitDT;
                    if (!dictExplicitDT.TryGetValue(libKey, out infoValueExplicitDT))
                    {
                        errmsg += "No ionMobility value found for " + libKey + "\n";
                    }
                    else
                    {
                        var ionMobilityInfo = infoValueExplicitDT[0];
                        var delta           = Math.Abs(ionMobilityInfo.IonMobility.Mobility.Value - calculatedDriftTime.IonMobility.Mobility.Value);
                        var acceptableDelta = 1;
                        if (delta > acceptableDelta)
                        {
                            errmsg += String.Format("calculated DT ({0}) and explicit DT ({1}, CCS={4}) do not agree (abs delta = {2}) for {3}\n",
                                                    calculatedDriftTime.IonMobility, ionMobilityInfo.IonMobility,
                                                    delta, libKey,
                                                    ionMobilityInfo.CollisionalCrossSectionSqA ?? 0);
                        }
                    }
                }
            }

            float  tolerance = (float)doc1.Settings.TransitionSettings.Instrument.MzMatchTolerance;
            double maxHeight = 0;
            var    results   = doc1.Settings.MeasuredResults;

            foreach (var pair in doc1.PeptidePrecursorPairs)
            {
                Assert.IsTrue(results.TryLoadChromatogram(0, pair.NodePep, pair.NodeGroup,
                                                          tolerance, true, out var chromGroupInfo));

                foreach (var chromGroup in chromGroupInfo)
                {
                    foreach (var tranInfo in chromGroup.TransitionPointSets)
                    {
                        maxHeight = Math.Max(maxHeight, tranInfo.MaxIntensity);
                    }
                }
            }
            Assert.IsTrue(errmsg.Length == 0, errmsg);
            Assert.AreEqual(1093425, maxHeight, 1);

            // Does CCS show up in reports?
            TestReports(doc1);
        }
Esempio n. 31
0
        public void UpdateLists(string documentFilePath)
        {
            Settings defSet = Settings.Default;

            // Make sure all settings are contained in the appropriate lists.
            // CONSIDER: Simple List.Contains() checks mean that values with the same name
            //           but differing values will be overwritten.
            if (!defSet.EnzymeList.Contains(PeptideSettings.Enzyme))
                defSet.EnzymeList.Add(PeptideSettings.Enzyme);
            // Extra null checks to avoid ReSharper warnings.
            if (PeptideSettings.Prediction != null)
            {
                if (PeptideSettings.Prediction.RetentionTime != null)
                {
                    if (!defSet.RetentionTimeList.Contains(PeptideSettings.Prediction.RetentionTime))
                        defSet.RetentionTimeList.Add(PeptideSettings.Prediction.RetentionTime);
                    if (!defSet.RTScoreCalculatorList.Contains(PeptideSettings.Prediction.RetentionTime.Calculator))
                        defSet.RTScoreCalculatorList.Add(PeptideSettings.Prediction.RetentionTime.Calculator);
                }
                if (PeptideSettings.Prediction.DriftTimePredictor != null)
                {
                    if (!defSet.DriftTimePredictorList.Contains(PeptideSettings.Prediction.DriftTimePredictor))
                        defSet.DriftTimePredictorList.Add(PeptideSettings.Prediction.DriftTimePredictor);
                    if (PeptideSettings.Prediction.DriftTimePredictor.IonMobilityLibrary != null &&
                        !defSet.IonMobilityLibraryList.Contains(PeptideSettings.Prediction.DriftTimePredictor.IonMobilityLibrary))
                    {
                        defSet.IonMobilityLibraryList.Add(PeptideSettings.Prediction.DriftTimePredictor.IonMobilityLibrary);
                    }
                }
            }
            if (PeptideSettings.Filter != null)
            {
                foreach (PeptideExcludeRegex exclude in PeptideSettings.Filter.Exclusions)
                {
                    if (!defSet.PeptideExcludeList.Contains(exclude))
                        defSet.PeptideExcludeList.Add(exclude);
                }
            }
            // First remove all old document local specs.
            defSet.SpectralLibraryList.RemoveDocumentLocalLibraries();
            // Then add any specs belonging to this document.
            if (PeptideSettings.Libraries.HasLibraries)
            {
                foreach (LibrarySpec librarySpec in PeptideSettings.Libraries.LibrarySpecs)
                {
                    if (librarySpec != null && !defSet.SpectralLibraryList.Contains(librarySpec))
                        defSet.SpectralLibraryList.Add(librarySpec);
                }
            }

            // If this document has a document library, add it to the specs list
            if (PeptideSettings.Libraries.HasDocumentLibrary)
            {
                string outputPath = Path.ChangeExtension(documentFilePath, BiblioSpecLiteSpec.EXT);
                if (File.Exists(outputPath))
                {
                    string docFileName = Path.GetFileNameWithoutExtension(documentFilePath);
                    var documentLibSpec = new BiblioSpecLiteSpec(docFileName, outputPath);
                    if (!defSet.SpectralLibraryList.Contains(documentLibSpec))
                    {
                        defSet.SpectralLibraryList.Add(documentLibSpec.ChangeDocumentLibrary(true));
                    }
                }
            }

            if (PeptideSettings.Integration.PeakScoringModel != null &&
                !Equals(PeptideSettings.Integration.PeakScoringModel.Name, LegacyScoringModel.DEFAULT_NAME))
            {
                defSet.PeakScoringModelList.Add(PeptideSettings.Integration.PeakScoringModel);
            }

            UpdateDefaultModifications(true, true);
            if (TransitionSettings.Prediction != null)
            {
                TransitionPrediction prediction = TransitionSettings.Prediction;
                if (!defSet.CollisionEnergyList.Contains(prediction.CollisionEnergy))
                    defSet.CollisionEnergyList.Add(prediction.CollisionEnergy);
                if (prediction.DeclusteringPotential != null &&
                        !defSet.DeclusterPotentialList.Contains(prediction.DeclusteringPotential))
                    defSet.DeclusterPotentialList.Add(prediction.DeclusteringPotential);
                if (prediction.CompensationVoltage != null &&
                        !defSet.CompensationVoltageList.Contains(prediction.CompensationVoltage))
                    defSet.CompensationVoltageList.Add(prediction.CompensationVoltage);
                if (!Equals(prediction.OptimizedLibrary, OptimizationLibrary.NONE) &&
                        Equals(defSet.GetOptimizationLibraryByName(prediction.OptimizedLibrary.Name), OptimizationLibrary.NONE))
                    defSet.OptimizationLibraryList.Add(prediction.OptimizedLibrary);
            }
            if (TransitionSettings.Filter != null)
            {
                foreach (var measuredIon in TransitionSettings.Filter.MeasuredIons)
                {
                    if (!defSet.MeasuredIonList.Contains(measuredIon))
                        defSet.MeasuredIonList.Add(measuredIon);
                }
            }
            if (TransitionSettings.FullScan.IsotopeEnrichments != null)
            {
                if (!defSet.IsotopeEnrichmentsList.ContainsKey(TransitionSettings.FullScan.IsotopeEnrichments.Name))
                {
                    defSet.IsotopeEnrichmentsList.Add(TransitionSettings.FullScan.IsotopeEnrichments);
                }
            }
            if (TransitionSettings.FullScan.IsolationScheme != null)
            {
                if (!defSet.IsolationSchemeList.ContainsKey(TransitionSettings.FullScan.IsolationScheme.Name))
                {
                    defSet.IsolationSchemeList.Add(TransitionSettings.FullScan.IsolationScheme);
                }
            }
            foreach (var annotationDef in DataSettings.AnnotationDefs)
            {
                if (!defSet.AnnotationDefList.Contains(annotationDef))
                {
                    defSet.AnnotationDefList.Add(annotationDef);
                }
            }
            foreach (var groupComparisonDef in DataSettings.GroupComparisonDefs)
            {
                if (!defSet.GroupComparisonDefList.Contains(groupComparisonDef))
                {
                    defSet.GroupComparisonDefList.Add(groupComparisonDef);
                }
            }
            var mainViewSpecList = defSet.PersistedViews.GetViewSpecList(PersistedViews.MainGroup.Id);
            foreach (var viewSpec in DataSettings.ViewSpecList.ViewSpecs)
            {
                mainViewSpecList = mainViewSpecList.ReplaceView(viewSpec.Name, viewSpec);
            }
            defSet.PersistedViews.SetViewSpecList(PersistedViews.MainGroup.Id, mainViewSpecList);
            if (!PeptideSettings.BackgroundProteome.IsNone)
            {
                if (!defSet.BackgroundProteomeList.ContainsKey(PeptideSettings.BackgroundProteome.Name))
                {
                    defSet.BackgroundProteomeList.Add(PeptideSettings.BackgroundProteome);
                }
            }
        }
Esempio n. 32
0
        public bool BuildLibrary(IProgressMonitor progress)
        {
            _ambiguousMatches = null;
            IProgressStatus status = new ProgressStatus(Resources.BiblioSpecLiteBuilder_BuildLibrary_Preparing_to_build_library);

            progress.UpdateProgress(status);
            if (InputFiles.Any(f => f.EndsWith(EXT_PILOT)))
            {
                try
                {
                    InputFiles = VendorIssueHelper.ConvertPilotFiles(InputFiles, progress, status);
                    if (progress.IsCanceled)
                    {
                        return(false);
                    }
                }
                catch (Exception x)
                {
                    progress.UpdateProgress(status.ChangeErrorException(x));
                    return(false);
                }
            }

            string message = string.Format(Resources.BiblioSpecLiteBuilder_BuildLibrary_Building__0__library,
                                           Path.GetFileName(OutputPath));

            progress.UpdateProgress(status = status.ChangeMessage(message));
            string redundantLibrary = BiblioSpecLiteSpec.GetRedundantName(OutputPath);
            var    blibBuilder      = new BlibBuild(redundantLibrary, InputFiles, TargetSequences)
            {
                IncludeAmbiguousMatches = IncludeAmbiguousMatches,
                CutOffScore             = CutOffScore,
                Id = Id,
                PreferEmbeddedSpectra = PreferEmbeddedSpectra,
                DebugMode             = DebugMode,
            };

            bool retry = true;

            do
            {
                try
                {
                    if (!blibBuilder.BuildLibrary(Action, progress, ref status,
                                                  out _buildCommandArgs, out _buildOutput, out _ambiguousMatches))
                    {
                        return(false);
                    }

                    retry = false;
                }
                catch (IOException x)
                {
                    if (IsLibraryMissingExternalSpectraError(x, out string spectrumFilename, out string resultsFilepath))
                    {
                        // replace the relative path to the results file (e.g. msms.txt) with the absolute path
                        string fullResultsFilepath = InputFiles.SingleOrDefault(o => o.EndsWith(resultsFilepath)) ??
                                                     throw new InvalidDataException(@"no results filepath from BiblioSpec error message", x);

                        // TODO: this will break if BiblioSpec output is translated to other languages
                        string messageWithFullFilepath =
                            x.Message.Replace(@"search results file '" + resultsFilepath,
                                              @"search results file '" + fullResultsFilepath);

                        var response =
                            progress.UpdateProgress(
                                status.ChangeErrorException(new IOException(messageWithFullFilepath, x)));
                        if (response == UpdateProgressResponse.cancel)
                        {
                            return(false);
                        }
                        else if (response == UpdateProgressResponse.normal)
                        {
                            blibBuilder.PreferEmbeddedSpectra = true;
                        }
                        continue;
                    }

                    progress.UpdateProgress(status.ChangeErrorException(x));
                    return(false);
                }
                catch (Exception x)
                {
                    Console.WriteLine(x.Message);
                    progress.UpdateProgress(status.ChangeErrorException(
                                                new Exception(string.Format(Resources.BiblioSpecLiteBuilder_BuildLibrary_Failed_trying_to_build_the_redundant_library__0__,
                                                                            redundantLibrary))));
                    return(false);
                }
            } while (retry);

            var blibFilter = new BlibFilter();

            status = new ProgressStatus(message);
            progress.UpdateProgress(status);
            // Write the non-redundant library to a temporary file first
            try
            {
                using (var saver = new FileSaver(OutputPath))
                {
                    if (!blibFilter.Filter(redundantLibrary, saver.SafeName, progress, ref status))
                    {
                        return(false);
                    }

                    saver.Commit();
                }
            }
            catch (IOException x)
            {
                progress.UpdateProgress(status.ChangeErrorException(x));
                return(false);
            }
            catch
            {
                progress.UpdateProgress(status.ChangeErrorException(
                                            new Exception(string.Format(Resources.BiblioSpecLiteBuilder_BuildLibrary_Failed_trying_to_build_the_library__0__,
                                                                        OutputPath))));
                return(false);
            }
            finally
            {
                if (!KeepRedundant)
                {
                    FileEx.SafeDelete(redundantLibrary, true);
                }
            }

            return(true);
        }
Esempio n. 33
0
        public bool SetLibrary(string name, string path, bool append = true)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                _out.WriteLine(Resources.CommandLine_SetLibrary_Error__Cannot_set_library_name_without_path_);
                return false;
            }
            else if (!File.Exists(path))
            {
                _out.WriteLine(Resources.CommandLine_SetLibrary_Error__The_file__0__does_not_exist_, path);
                return false;
            }
            else if (path.EndsWith(BiblioSpecLiteSpec.EXT_REDUNDANT))
            {
                _out.WriteLine(Resources.CommandLine_SetLibrary_Error__The_file__0__appears_to_be_a_redundant_library_);
                return false;
            }

            if (string.IsNullOrWhiteSpace(name))
                name = Path.GetFileNameWithoutExtension(path);

            LibrarySpec librarySpec;

            string ext = Path.GetExtension(path);
            if (Equals(ext, BiblioSpecLiteSpec.EXT))
                librarySpec = new BiblioSpecLiteSpec(name, path);
            else if (Equals(ext, BiblioSpecLibSpec.EXT))
                librarySpec = new BiblioSpecLibSpec(name, path);
            else if (Equals(ext, XHunterLibSpec.EXT))
                librarySpec = new XHunterLibSpec(name, path);
            else if (Equals(ext, NistLibSpec.EXT))
                librarySpec = new NistLibSpec(name, path);
            else if (Equals(ext, SpectrastSpec.EXT))
                librarySpec = new SpectrastSpec(name, path);
            else
            {
                _out.WriteLine(Resources.CommandLine_SetLibrary_Error__The_file__0__is_not_a_supported_spectral_library_file_format_, path);
                return false;
            }

            // Check for conflicting names
            foreach (var docLibrarySpec in _doc.Settings.PeptideSettings.Libraries.LibrarySpecs)
            {
                if (docLibrarySpec.Name == librarySpec.Name || docLibrarySpec.FilePath == librarySpec.FilePath)
                {
                    _out.WriteLine(Resources.CommandLine_SetLibrary_Error__The_library_you_are_trying_to_add_conflicts_with_a_library_already_in_the_file_);
                    return false;
                }
            }

            var librarySpecs = append ?
                new List<LibrarySpec>(_doc.Settings.PeptideSettings.Libraries.LibrarySpecs) { librarySpec } :
                new List<LibrarySpec>{ librarySpec };

            SrmSettings newSettings = _doc.Settings.ChangePeptideLibraries(l => l.ChangeLibrarySpecs(librarySpecs));
            _doc = _doc.ChangeSettings(newSettings);

            return true;
        }
        protected override void DoTest()
        {
            // Create the modification and library spec used in this test
            var phosphoLossMod = new StaticMod("Phospho Loss", "S, T", null, true, "HPO3",
                                              LabelAtoms.None, RelativeRT.Matching, null, null,  new[] { new FragmentLoss("H3PO4"), });
            var multipleLossMod = new StaticMod("Multiple Loss-only", "D", null, false, null,
                LabelAtoms.None, RelativeRT.Matching, null, null, new[]
                {
                    new FragmentLoss("NH3"),
                    new FragmentLoss("H2O"),
                    new FragmentLoss(null, 20, 25)
                });
            var heavyKMod = new StaticMod("Heavy K", "K", ModTerminus.C, null, LabelAtoms.C13 | LabelAtoms.N15, null, null);
            var librarySpec = new BiblioSpecLiteSpec("Phospho Library",
                                                     TestFilesDir.GetTestPath("phospho_30882_v2.blib"));

            // Prepare settings for this test
            Settings.Default.StaticModList.Clear();
            Settings.Default.StaticModList.AddRange(StaticModList.GetDefaultsOn());
            Settings.Default.StaticModList.Add(phosphoLossMod);
            Settings.Default.HeavyModList.Clear();
            Settings.Default.HeavyModList.Add(heavyKMod);
            Settings.Default.SpectralLibraryList.Clear();
            Settings.Default.SpectralLibraryList.Add(librarySpec);

            // Prepare document settings for this test
            const int countIons = 6;
            var settings = SrmSettingsList.GetDefault()
                .ChangePeptideModifications(mods => mods.ChangeStaticModifications(new List<StaticMod>(mods.StaticModifications) { phosphoLossMod }))
                .ChangePeptideLibraries(lib => lib.ChangeLibrarySpecs(new[] { librarySpec }))
                .ChangeTransitionLibraries(tlib => tlib.ChangeIonCount(countIons));

            RunUI(() => SkylineWindow.ModifyDocument("Set test settings",
                                                     doc => doc.ChangeSettings(settings)));

            WaitForDocumentLoaded();

            // Add FASTA sequence
            RunUI(() => SkylineWindow.Paste(TEXT_FASTA_SPROT));

            var docLibLoss = SkylineWindow.Document;
            AssertEx.IsDocumentState(docLibLoss, null, 3, 4, 24);
            Assert.AreEqual(7, GetLossCount(docLibLoss, 1));

            string lossLabel = "-" + Math.Round(phosphoLossMod.Losses[0].MonoisotopicMass, 1);
            for (int i = 0; i < docLibLoss.PeptideTransitionGroupCount; i++)
            {
                var pathTranGroup = docLibLoss.GetPathTo((int) SrmDocument.Level.TransitionGroups, i);
                var nodeGroup = (TransitionGroupDocNode) docLibLoss.FindNode(pathTranGroup);
                if (!nodeGroup.Children.Contains(child => ((TransitionDocNode) child).HasLoss))
                    continue;

                // Select the transition groups that contain loss ions
                SelectNode(SrmDocument.Level.TransitionGroups, i);
                WaitForGraphs();

                // Make sure the spectrum graph contains some -98 ions
                RunUI(() => Assert.IsTrue(SkylineWindow.GraphSpectrum.IonLabels.Contains(label => label.Contains(lossLabel)),
                    string.Format("Missing loss labels in spectrum graph for {0}", nodeGroup.TransitionGroup.Peptide.Sequence)));

                // Make sure the transition tree nodes contain -98 ions
                RunUI(() => Assert.IsTrue(GetChildLabels(SkylineWindow.SelectedNode).Contains(label => label.Contains(lossLabel)),
                    string.Format("Missing loss labels in transition tree nodes for {0}", nodeGroup.TransitionGroup.Peptide.Sequence)));
            }

            // Make the settings significantly more complex
            // - 2 neutral losses
            // - a second modification with 3 potential neutral losses
            // - a heavy labeling modification
            // - only ions between precursor m/z and last ion
            // - allow both y- and b- ions
            // - no proline peaks
            // - only use filtered ions to match library
            RunUI(() => SkylineWindow.ModifyDocument("Set test settings", doc =>
                doc.ChangeSettings(doc.Settings.ChangePeptideModifications(mod =>
                        mod.ChangeMaxNeutralLosses(2)
                            .ChangeStaticModifications(new List<StaticMod>(mod.StaticModifications) {multipleLossMod})
                            .ChangeHeavyModifications(new[] { heavyKMod }))
                    .ChangeTransitionFilter(filter =>
                        filter.ChangeFragmentRangeFirstName("m/z > precursor")
                            .ChangeFragmentRangeLastName("last ion")
                            .ChangeIonTypes(new[] { IonType.y, IonType.b })
                            .ChangeMeasuredIons(new MeasuredIon[0]))
                    .ChangeTransitionLibraries(lib =>
                        lib.ChangePick(TransitionLibraryPick.filter)))));

            var docComplex = WaitForDocumentChange(docLibLoss);

            // The document has 2 variable mod peptides at the protein terminus
            Assert.AreEqual(docLibLoss.PeptideTransitionGroupCount*2 - 2, docComplex.PeptideTransitionGroupCount);
            Assert.AreEqual(4, GetLossCount(docComplex, 2));
            Assert.AreEqual(16, GetLossCount(docComplex, 1));
            foreach (var nodePep in docComplex.Peptides)
            {
                if (nodePep.Children.Count != 2)
                    continue;
                var nodeGroup1 = (TransitionGroupDocNode)nodePep.Children[0];
                var nodeGroup2 = (TransitionGroupDocNode)nodePep.Children[1];

                Assert.IsTrue(nodeGroup1.EquivalentChildren(nodeGroup2));
            }
            // CONSIDER: Can't check cloned because of libraries.  Maybe add a new method for this
            // AssertEx.Serializable(docComplex, AssertEx.DocumentCloned);

            SelectNode(SrmDocument.Level.Molecules, 1);
            WaitForGraphs();

            // Verify that the ion labels in the graph match those in the tree view
            RunUI(() =>
                      {
                          var nodePepSelected = (PeptideDocNode) docComplex.FindNode(SkylineWindow.SelectedPath);
                          string[] ionLabels = SkylineWindow.GraphSpectrum.IonLabels.ToArray();

                          foreach (TransitionGroupDocNode nodeGroup in nodePepSelected.Children)
                          {
                              var setSeenRanks = new HashSet<int>();
                              foreach (TransitionDocNode nodeTran in nodeGroup.Children)
                              {
                                  Assert.IsTrue(nodeTran.HasLibInfo);
                                  // Make sure the rank has not been seen
                                  int rankTran = nodeTran.LibInfo.Rank;
                                  Assert.IsFalse(setSeenRanks.Contains(rankTran));
                                  setSeenRanks.Add(rankTran);
                                  // And it is within the expected range
                                  Assert.IsTrue(1 <= rankTran && rankTran <= countIons);
                                  // Make sure there is a matching label in the spectrum graph
                                  var regexRank = nodeTran.HasLoss ?
                                      new Regex(string.Format(@"(\w)(\d+) -([^ +]+).* \({0}\)", string.Format(Resources.AbstractSpectrumGraphItem_GetLabel_rank__0__, rankTran))) :
                                      new Regex(string.Format(@"(\w)(\d+).* \({0}\)", string.Format(Resources.AbstractSpectrumGraphItem_GetLabel_rank__0__, rankTran)));
                                  int iLabel = ionLabels.IndexOf(regexRank.IsMatch);
                                  Assert.IsTrue(iLabel != -1);
                                  var match = regexRank.Match(ionLabels[iLabel]);
                                  Assert.AreEqual(nodeTran.Transition.IonType.ToString(), match.Groups[1].Value);
                                  Assert.AreEqual(nodeTran.Transition.Ordinal, int.Parse(match.Groups[2].Value));
                                  if (nodeTran.HasLoss)
                                      Assert.AreEqual(nodeTran.Losses.Mass, double.Parse(match.Groups[3].Value), 0.1);
                              }
                          }
                      });

            // Make sure setting losses as included Never works
            {
                var docBeforeNever = SkylineWindow.Document;
                var peptideSettingsUINever = ShowDialog<PeptideSettingsUI>(() =>
                    SkylineWindow.ShowPeptideSettingsUI(PeptideSettingsUI.TABS.Modifications));
                var editModsDlgNever = ShowEditStaticModsDlg(peptideSettingsUINever);
                RunUI(() => editModsDlgNever.SelectItem(phosphoLossMod.Name));
                var editModDlgNever = ShowDialog<EditStaticModDlg>(editModsDlgNever.EditItem);
                RunUI(() => editModDlgNever.LossSelectedIndex = 0);
                RunDlg<EditFragmentLossDlg>(editModDlgNever.EditLoss, dlg =>
                {
                    dlg.Inclusion = LossInclusion.Never;
                    dlg.OkDialog();
                });
                RunDlg<MultiButtonMsgDlg>(editModDlgNever.OkDialog, dlg => dlg.Btn1Click());
                OkDialog(editModsDlgNever, editModsDlgNever.OkDialog);
                OkDialog(peptideSettingsUINever, peptideSettingsUINever.OkDialog);
                var docAfterNever = WaitForDocumentChange(docBeforeNever);
                Assert.AreEqual(0, GetLossCount(docAfterNever, 1));
            }
        }
        private bool BuildPeptideSearchLibrary(CancelEventArgs e)
        {
            // Nothing to build, if now search files were specified
            if (!SearchFilenames.Any())
            {
                var libraries = DocumentContainer.Document.Settings.PeptideSettings.Libraries;
                if (!libraries.HasLibraries)
                {
                    return(false);
                }
                var libSpec = libraries.LibrarySpecs.FirstOrDefault(s => s.IsDocumentLibrary);
                return(libSpec != null && LoadPeptideSearchLibrary(libSpec));
            }

            double           cutOffScore;
            MessageBoxHelper helper = new MessageBoxHelper(WizardForm);

            if (!helper.ValidateDecimalTextBox(textCutoff, 0, 1.0, out cutOffScore))
            {
                e.Cancel = true;
                return(false);
            }
            ImportPeptideSearch.CutoffScore = cutOffScore;

            BiblioSpecLiteBuilder builder;

            try
            {
                builder = ImportPeptideSearch.GetLibBuilder(DocumentContainer.Document, DocumentContainer.DocumentFilePath, cbIncludeAmbiguousMatches.Checked);
                builder.PreferEmbeddedSpectra = PreferEmbeddedSpectra;
            }
            catch (FileEx.DeleteException de)
            {
                MessageDlg.ShowException(this, de);
                return(false);
            }

            bool retry = false;

            do
            {
                using (var longWaitDlg = new LongWaitDlg
                {
                    Text = Resources.BuildPeptideSearchLibraryControl_BuildPeptideSearchLibrary_Building_Peptide_Search_Library,
                    Message = Resources.BuildPeptideSearchLibraryControl_BuildPeptideSearchLibrary_Building_document_library_for_peptide_search_,
                })
                {
                    // Disable the wizard, because the LongWaitDlg does not
                    try
                    {
                        ImportPeptideSearch.ClosePeptideSearchLibraryStreams(DocumentContainer.Document);
                        var status = longWaitDlg.PerformWork(WizardForm, 800,
                                                             monitor => LibraryManager.BuildLibraryBackground(DocumentContainer, builder, monitor, new LibraryManager.BuildState(null, null)));
                        if (status.IsError)
                        {
                            // E.g. could not find external raw data for MaxQuant msms.txt; ask user if they want to retry with "prefer embedded spectra" option
                            if (BiblioSpecLiteBuilder.IsLibraryMissingExternalSpectraError(status.ErrorException))
                            {
                                var response = ShowLibraryMissingExternalSpectraError(WizardForm, status.ErrorException);
                                if (response == UpdateProgressResponse.cancel)
                                {
                                    return(false);
                                }
                                else if (response == UpdateProgressResponse.normal)
                                {
                                    builder.PreferEmbeddedSpectra = true;
                                }

                                retry = true;
                            }
                            else
                            {
                                MessageDlg.ShowException(WizardForm, status.ErrorException);
                                return(false);
                            }
                        }
                    }
                    catch (Exception x)
                    {
                        MessageDlg.ShowWithException(WizardForm, TextUtil.LineSeparate(string.Format(Resources.BuildPeptideSearchLibraryControl_BuildPeptideSearchLibrary_Failed_to_build_the_library__0__,
                                                                                                     Path.GetFileName(BiblioSpecLiteSpec.GetLibraryFileName(DocumentContainer.DocumentFilePath))), x.Message), x);
                        return(false);
                    }
                }
            } while (retry);

            var docLibSpec = builder.LibrarySpec.ChangeDocumentLibrary(true);

            Settings.Default.SpectralLibraryList.Insert(0, docLibSpec);

            // Go ahead and load the library - we'll need it for
            // the modifications and chromatograms page.
            if (!LoadPeptideSearchLibrary(docLibSpec))
            {
                return(false);
            }

            var selectedIrtStandard = comboStandards.SelectedItem as IrtStandard;
            var addedIrts           = false;

            if (selectedIrtStandard != null && selectedIrtStandard != IrtStandard.EMPTY)
            {
                addedIrts = AddIrtLibraryTable(docLibSpec.FilePath, selectedIrtStandard);
            }

            var docNew = ImportPeptideSearch.AddDocumentSpectralLibrary(DocumentContainer.Document, docLibSpec);

            if (docNew == null)
            {
                return(false);
            }

            if (addedIrts)
            {
                docNew = ImportPeptideSearch.AddRetentionTimePredictor(docNew, docLibSpec);
            }

            DocumentContainer.ModifyDocumentNoUndo(doc => docNew);

            if (!string.IsNullOrEmpty(builder.AmbiguousMatchesMessage))
            {
                MessageDlg.Show(WizardForm, builder.AmbiguousMatchesMessage);
            }
            return(true);
        }
Esempio n. 36
0
        protected override void DoTest()
        {
            // Create the modification and library spec used in this test
            var phosphoLossMod = new StaticMod("Phospho Loss", "S, T", null, true, "HPO3",
                                               LabelAtoms.None, RelativeRT.Matching, null, null, new[] { new FragmentLoss("H3PO4"), });
            var multipleLossMod = new StaticMod("Multiple Loss-only", "D", null, false, null,
                                                LabelAtoms.None, RelativeRT.Matching, null, null, new[]
            {
                new FragmentLoss("NH3"),
                new FragmentLoss("H2O"),
                new FragmentLoss(null, 20, 25)
            });
            var heavyKMod   = new StaticMod("Heavy K", "K", ModTerminus.C, null, LabelAtoms.C13 | LabelAtoms.N15, null, null);
            var librarySpec = new BiblioSpecLiteSpec("Phospho Library",
                                                     TestFilesDir.GetTestPath("phospho_30882_v2.blib"));

            // Prepare settings for this test
            Settings.Default.StaticModList.Clear();
            Settings.Default.StaticModList.AddRange(StaticModList.GetDefaultsOn());
            Settings.Default.StaticModList.Add(phosphoLossMod);
            Settings.Default.HeavyModList.Clear();
            Settings.Default.HeavyModList.Add(heavyKMod);
            Settings.Default.SpectralLibraryList.Clear();
            Settings.Default.SpectralLibraryList.Add(librarySpec);

            // Prepare document settings for this test
            const int countIons = 6;
            var       settings  = SrmSettingsList.GetDefault()
                                  .ChangePeptideModifications(mods => mods.ChangeStaticModifications(new List <StaticMod>(mods.StaticModifications)
            {
                phosphoLossMod
            }))
                                  .ChangePeptideLibraries(lib => lib.ChangeLibrarySpecs(new[] { librarySpec }))
                                  .ChangeTransitionLibraries(tlib => tlib.ChangeIonCount(countIons));

            RunUI(() => SkylineWindow.ModifyDocument("Set test settings",
                                                     doc => doc.ChangeSettings(settings)));

            WaitForDocumentLoaded();

            // Add FASTA sequence
            RunUI(() => SkylineWindow.Paste(TEXT_FASTA_SPROT));

            var docLibLoss = SkylineWindow.Document;

            AssertEx.IsDocumentState(docLibLoss, null, 3, 4, 24);
            Assert.AreEqual(7, GetLossCount(docLibLoss, 1));

            string lossLabel = "-" + Math.Round(phosphoLossMod.Losses[0].MonoisotopicMass, 1);

            for (int i = 0; i < docLibLoss.PeptideTransitionGroupCount; i++)
            {
                var pathTranGroup = docLibLoss.GetPathTo((int)SrmDocument.Level.TransitionGroups, i);
                var nodeGroup     = (TransitionGroupDocNode)docLibLoss.FindNode(pathTranGroup);
                if (!nodeGroup.Children.Contains(child => ((TransitionDocNode)child).HasLoss))
                {
                    continue;
                }

                // Select the transition groups that contain loss ions
                SelectNode(SrmDocument.Level.TransitionGroups, i);
                WaitForGraphs();

                // Make sure the spectrum graph contains some -98 ions
                RunUI(() => Assert.IsTrue(SkylineWindow.GraphSpectrum.IonLabels.Contains(label => label.Contains(lossLabel)),
                                          string.Format("Missing loss labels in spectrum graph for {0}", nodeGroup.TransitionGroup.Peptide.Target)));

                // Make sure the transition tree nodes contain -98 ions
                RunUI(() => Assert.IsTrue(GetChildLabels(SkylineWindow.SelectedNode).Contains(label => label.Contains(lossLabel)),
                                          string.Format("Missing loss labels in transition tree nodes for {0}", nodeGroup.TransitionGroup.Peptide.Target)));
            }

            // Make the settings significantly more complex
            // - 2 neutral losses
            // - a second modification with 3 potential neutral losses
            // - a heavy labeling modification
            // - only ions between precursor m/z and last ion
            // - allow both y- and b- ions
            // - no proline peaks
            // - only use filtered ions to match library
            RunUI(() => SkylineWindow.ModifyDocument("Set test settings", doc =>
                                                     doc.ChangeSettings(doc.Settings.ChangePeptideModifications(mod =>
                                                                                                                mod.ChangeMaxNeutralLosses(2)
                                                                                                                .ChangeStaticModifications(new List <StaticMod>(mod.StaticModifications)
            {
                multipleLossMod
            })
                                                                                                                .ChangeModifications(IsotopeLabelType.heavy, new[] { heavyKMod }))
                                                                        .ChangeTransitionFilter(filter =>
                                                                                                filter.ChangeFragmentRangeFirstName("m/z > precursor")
                                                                                                .ChangeFragmentRangeLastName("last ion")
                                                                                                .ChangePeptideIonTypes(new[] { IonType.y, IonType.b })
                                                                                                .ChangeMeasuredIons(new MeasuredIon[0]))
                                                                        .ChangeTransitionLibraries(lib =>
                                                                                                   lib.ChangePick(TransitionLibraryPick.filter)))));

            var docComplex = WaitForDocumentChange(docLibLoss);

            // The document has 2 variable mod peptides at the protein terminus
            Assert.AreEqual(docLibLoss.PeptideTransitionGroupCount * 2 - 2, docComplex.PeptideTransitionGroupCount);
            Assert.AreEqual(4, GetLossCount(docComplex, 2));
            Assert.AreEqual(16, GetLossCount(docComplex, 1));
            foreach (var nodePep in docComplex.Peptides)
            {
                if (nodePep.Children.Count != 2)
                {
                    continue;
                }
                var nodeGroup1 = (TransitionGroupDocNode)nodePep.Children[0];
                var nodeGroup2 = (TransitionGroupDocNode)nodePep.Children[1];

                Assert.IsTrue(nodeGroup1.EquivalentChildren(nodeGroup2));
            }
            // CONSIDER: Can't check cloned because of libraries.  Maybe add a new method for this
            // AssertEx.Serializable(docComplex, AssertEx.DocumentCloned);

            SelectNode(SrmDocument.Level.Molecules, 1);
            WaitForGraphs();

            // Verify that the ion labels in the graph match those in the tree view
            RunUI(() =>
            {
                var nodePepSelected = (PeptideDocNode)docComplex.FindNode(SkylineWindow.SelectedPath);
                string[] ionLabels  = SkylineWindow.GraphSpectrum.IonLabels.ToArray();

                foreach (TransitionGroupDocNode nodeGroup in nodePepSelected.Children)
                {
                    var setSeenRanks = new HashSet <int>();
                    foreach (TransitionDocNode nodeTran in nodeGroup.Children)
                    {
                        Assert.IsTrue(nodeTran.HasLibInfo);
                        // Make sure the rank has not been seen
                        int rankTran = nodeTran.LibInfo.Rank;
                        Assert.IsFalse(setSeenRanks.Contains(rankTran));
                        setSeenRanks.Add(rankTran);
                        // And it is within the expected range
                        Assert.IsTrue(1 <= rankTran && rankTran <= countIons);
                        // Make sure there is a matching label in the spectrum graph
                        var regexRank = nodeTran.HasLoss ?
                                        new Regex(string.Format(@"(\w)(\d+) -([^ +]+).* \({0}\)", string.Format(Resources.AbstractSpectrumGraphItem_GetLabel_rank__0__, rankTran))) :
                                        new Regex(string.Format(@"(\w)(\d+).* \({0}\)", string.Format(Resources.AbstractSpectrumGraphItem_GetLabel_rank__0__, rankTran)));
                        int iLabel = ionLabels.IndexOf(regexRank.IsMatch);
                        Assert.IsTrue(iLabel != -1);
                        var match = regexRank.Match(ionLabels[iLabel]);
                        Assert.AreEqual(nodeTran.Transition.IonType.ToString(), match.Groups[1].Value);
                        Assert.AreEqual(nodeTran.Transition.Ordinal, int.Parse(match.Groups[2].Value));
                        if (nodeTran.HasLoss)
                        {
                            Assert.AreEqual(nodeTran.Losses.Mass, double.Parse(match.Groups[3].Value), 0.1);
                        }
                    }
                }
            });

            // Make sure setting losses as included Never works
            {
                var docBeforeNever         = SkylineWindow.Document;
                var peptideSettingsUINever = ShowDialog <PeptideSettingsUI>(() =>
                                                                            SkylineWindow.ShowPeptideSettingsUI(PeptideSettingsUI.TABS.Modifications));
                var editModsDlgNever = ShowEditStaticModsDlg(peptideSettingsUINever);
                RunUI(() => editModsDlgNever.SelectItem(phosphoLossMod.Name));
                var editModDlgNever = ShowDialog <EditStaticModDlg>(editModsDlgNever.EditItem);
                RunUI(() => editModDlgNever.LossSelectedIndex = 0);
                RunDlg <EditFragmentLossDlg>(editModDlgNever.EditLoss, dlg =>
                {
                    dlg.Inclusion = LossInclusion.Never;
                    dlg.OkDialog();
                });
                RunDlg <MultiButtonMsgDlg>(editModDlgNever.OkDialog, dlg => dlg.Btn1Click());
                OkDialog(editModsDlgNever, editModsDlgNever.OkDialog);
                OkDialog(peptideSettingsUINever, peptideSettingsUINever.OkDialog);
                var docAfterNever = WaitForDocumentChange(docBeforeNever);
                Assert.AreEqual(0, GetLossCount(docAfterNever, 1));
            }
        }
Esempio n. 37
0
        private bool BuildLibraryOrThrow(IProgressMonitor progress, ref IProgressStatus progressStatus)
        {
            progressStatus = progressStatus.ChangeSegments(0, 5);
            var standardSpectra = new List <SpectrumMzInfo>();

            // First get predictions for iRT standards specified by the user which may or may not be in the document
            if (IrtStandard != null && !ReferenceEquals(IrtStandard, IrtStandard.EMPTY) && !ReferenceEquals(IrtStandard, IrtStandard.AUTO))
            {
                var standardPeptidesToAdd = 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);

                    // Get spectra
                    var standardMS = _intensityModel.PredictBatches(_prositClient, progress, ref progressStatus, _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;
                        }
                    }
                }
            }

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

            progressStatus = progressStatus.NextSegment();

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

            // Predict iRTs for peptides
            var distinctModifiedSequences = new HashSet <string>();
            var distinctPeps = new List <PrositRetentionTimeModel.PeptideDocNodeWrapper>();

            foreach (var p in _peptides)
            {
                if (distinctModifiedSequences.Add(p.ModifiedSequence))
                {
                    distinctPeps.Add(new PrositRetentionTimeModel.PeptideDocNodeWrapper(p));
                }
            }
            var iRTMap = _rtModel.PredictBatches(_prositClient, progress, ref progressStatus, _document.Settings,
                                                 distinctPeps, CancellationToken.None);

            progressStatus = progressStatus.NextSegment();

            for (var i = 0; i < specMzInfo.Count; ++i)
            {
                if (iRTMap.TryGetValue(ms.Spectra[i].PeptidePrecursorNCE.NodePep, out var iRT))
                {
                    specMzInfo[i].RetentionTime = 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);
            }

            progressStatus = progressStatus.NextSegment().ChangeMessage(Resources.SkylineWindow_SaveDocument_Saving___);
            // 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, ref progressStatus);
                if (docLibraryNew == null)
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 38
0
        /* TODO bspratt drift time libs for small molecules

        [TestMethod]
        public void WatersImsMsePredictedDriftTimesChromatogramTestAsSmallMolecules()
        {
            WatersImsMseChromatogramTest(DriftFilterType.predictor, true);
        }

        [TestMethod]
        public void WatersImsMseLibraryDriftTimesChromatogramTestAsSmallMolecules()
        {
            WatersImsMseChromatogramTest(DriftFilterType.library, true);
        }

         */
        private void WatersImsMseChromatogramTest(DriftFilterType mode,
            RefinementSettings.ConvertToSmallMoleculesMode asSmallMolecules = RefinementSettings.ConvertToSmallMoleculesMode.none)
        {
            string subdir = (asSmallMolecules == RefinementSettings.ConvertToSmallMoleculesMode.none) ? null : asSmallMolecules.ToString();
            var testFilesDir = new TestFilesDir(TestContext, ZIP_FILE, subdir);
            TestSmallMolecules = false; // Don't need that extra magic node

            bool withDriftTimePredictor = (mode == DriftFilterType.predictor); // Load the doc that has a drift time predictor?
            bool withDriftTimeFilter = (mode != DriftFilterType.none); // Perform drift time filtering?  (either with predictor, or with bare times in blib file)
            string docPath;
            SrmDocument document = InitWatersImsMseDocument(testFilesDir, withDriftTimePredictor ? "single_with_driftinfo.sky" : "single_no_driftinfo.sky", asSmallMolecules, out docPath);
            AssertEx.IsDocumentState(document, (withDriftTimePredictor || (asSmallMolecules != RefinementSettings.ConvertToSmallMoleculesMode.none)) ? 1 : 0, 1, 1, 1, 8); // Drift time lib load bumps the doc version
            var docContainer = new ResultsTestDocumentContainer(document, docPath);
            var doc = docContainer.Document;
            var docOriginal = doc;

            string testModeStr = withDriftTimePredictor ? "with drift time predictor" : "without drift time info";

            if (withDriftTimeFilter && !withDriftTimePredictor)
            {
                // Use the bare drift times in the spectral library
                var librarySpec = new BiblioSpecLiteSpec("drift test",
                                                    testFilesDir.GetTestPath("mse-mobility.filtered-scaled.blib"));
                doc = doc.ChangeSettings(
                    doc.Settings.ChangePeptideLibraries(lib => lib.ChangeLibrarySpecs(new[] { librarySpec })).
                    ChangePeptidePrediction(p => p.ChangeLibraryDriftTimesResolvingPower(100)).
                    ChangePeptidePrediction(p => p.ChangeUseLibraryDriftTimes(true))
                    );
                testModeStr = "with drift times from spectral library";
            }

            var listChromatograms = new List<ChromatogramSet>();
            // A small subset of the QC_HDMSE_02_UCA168_3495_082213 data set (RT 21.5-22.5) from Will Thompson
            const string path = @"waters-mobility.mz5";
            listChromatograms.Add(AssertResult.FindChromatogramSet(doc, new MsDataFilePath(path)) ??
                                    new ChromatogramSet(Path.GetFileName(path).Replace('.', '_'), new[] { path }));
            var docResults = doc.ChangeMeasuredResults(new MeasuredResults(listChromatograms));
            Assert.IsTrue(docContainer.SetDocument(docResults, docOriginal, true));
            docContainer.AssertComplete();
            document = docContainer.Document;

            float tolerance = (float)document.Settings.TransitionSettings.Instrument.MzMatchTolerance;
            double maxHeight = 0;
            var results = document.Settings.MeasuredResults;
            Assert.AreEqual(1, document.MoleculePrecursorPairs.Count());
            foreach (var pair in document.MoleculePrecursorPairs)
            {
                ChromatogramGroupInfo[] chromGroupInfo;
                Assert.IsTrue(results.TryLoadChromatogram(0, pair.NodePep, pair.NodeGroup,
                    tolerance, true, out chromGroupInfo));
                Assert.AreEqual(1, chromGroupInfo.Length, testModeStr);
                var chromGroup = chromGroupInfo[0];
                var expectedPeaks = ((asSmallMolecules == RefinementSettings.ConvertToSmallMoleculesMode.masses_only) ? 6 : 5);
                Assert.AreEqual(withDriftTimeFilter ? 3 : expectedPeaks, chromGroup.NumPeaks, testModeStr); // This will be higher if we don't filter on DT
                foreach (var tranInfo in chromGroup.TransitionPointSets)
                {
                    maxHeight = Math.Max(maxHeight, tranInfo.MaxIntensity);
                }
            }
            Assert.AreEqual(withDriftTimeFilter? 5226 : 20075 , maxHeight, 1, testModeStr);  // Without DT filtering, this will be much greater

            // now drill down for specific values
            int nPeptides = 0;
            foreach (var nodePep in document.Molecules.Where(nodePep => nodePep.Results[0] != null))
            {
                // expecting just one peptide result in this small data set
                if (nodePep.Results[0].Sum(chromInfo => chromInfo.PeakCountRatio > 0 ? 1 : 0) > 0)
                {
                    Assert.AreEqual(21.94865, (double)nodePep.GetMeasuredRetentionTime(0), .0001, testModeStr);
                    Assert.AreEqual(1.0, (double)nodePep.GetPeakCountRatio(0), 0.0001, testModeStr);
                    nPeptides++;
                }
            }
            Assert.AreEqual(1, nPeptides);

            if (withDriftTimePredictor || withDriftTimeFilter)
            {
                // Verify that the .imdb pr .blib file goes out in the share zipfile
                for (int complete = 0; complete <= 1; complete++)
                {
                    var sharePath = testFilesDir.GetTestPath(complete==1?"share_complete.zip":"share_minimized.zip");
                    var share = new SrmDocumentSharing(document, docPath, sharePath, complete==1);
                    using (var longWaitDlg = new LongWaitDlg
                    {
                        // ReSharper disable once LocalizableElement
                        Text = "unit test WatersImsTest -- sharing document",
                    })
                    {
                        longWaitDlg.PerformWork(null, 1000, share.Share);
                        Assert.IsFalse(longWaitDlg.IsCanceled);
                    }

                    var files = share.ListEntries().ToArray();
                    Assert.IsTrue(files.Contains(withDriftTimePredictor ? "scaled.imdb" : "mse-mobility.filtered-scaled.blib"));
                    // And round trip it to make sure we haven't left out any new features in minimized imdb or blib files
                    using (var longWaitDlg = new LongWaitDlg
                    {
                        // ReSharper disable once LocalizableElement
                        Text = "unit test WatersImsTest",
                    })
                    {
                        longWaitDlg.PerformWork(null, 1000, share.Extract);
                        Assert.IsFalse(longWaitDlg.IsCanceled);
                    }
                    using (TextReader reader = new StreamReader(share.DocumentPath))
                    {
                        XmlSerializer documentSerializer = new XmlSerializer(typeof(SrmDocument));
                        var document2 = (SrmDocument) documentSerializer.Deserialize(reader);
                        Assert.IsNotNull(document2);
                        var im = document.Settings.GetIonMobilities(new MsDataFilePath(path));
                        var pep = document2.Molecules.First();
                        foreach (TransitionGroupDocNode nodeGroup in pep.Children)
                        {
                            double windowDT;
                            var centerDriftTime = document.Settings.PeptideSettings.Prediction.GetDriftTime(
                                                       pep, nodeGroup, im, out windowDT);
                            Assert.AreEqual(3.86124, centerDriftTime.DriftTimeMsec(false) ?? 0, .0001, testModeStr);
                            Assert.AreEqual(0.077224865797235934, windowDT, .0001, testModeStr);
                        }
                    }
                }
            }

            // Release file handles
            docContainer.Release();
            testFilesDir.Dispose();
            string cachePath = ChromatogramCache.FinalPathForName(docPath, null);
            FileEx.SafeDelete(cachePath);
        }
        protected override void DoTest()
        {
            string skyfile = TestFilesDir.GetTestPath("test.sky");

            RunUI(() => SkylineWindow.SaveDocument(skyfile));

            Stopwatch loadStopwatch = new Stopwatch();

            loadStopwatch.Start();

            // Launch import peptide search wizard
            var importPeptideSearchDlg = ShowDialog <ImportPeptideSearchDlg>(SkylineWindow.ShowImportPeptideSearchDlg);

            string lo  = GetTestPath("40minG_WBP_wide_z2-3_low_BSA_5pmol_02.pep.xml");
            string mid = GetTestPath("40minG_WBP_wide_z2-3_mid_BSA_5pmol_01.pep.xml");
            string up  = GetTestPath("40minG_WBP_wide_z2-3_up_BSA_5pmol_02.pep.xml");

            string[] searchFiles = { lo, mid, up };
            var      doc         = SkylineWindow.Document;

            RunUI(() =>
            {
                Assert.IsTrue(importPeptideSearchDlg.CurrentPage ==
                              ImportPeptideSearchDlg.Pages.spectra_page);
                importPeptideSearchDlg.BuildPepSearchLibControl.AddSearchFiles(searchFiles);
                importPeptideSearchDlg.BuildPepSearchLibControl.CutOffScore = 0.95;
                importPeptideSearchDlg.BuildPepSearchLibControl.FilterForDocumentPeptides = false;
            });

            RunUI(() => Assert.IsTrue(importPeptideSearchDlg.ClickNextButton()));
            doc = WaitForDocumentChange(doc);

            // Verify document library was built
            string docLibPath          = BiblioSpecLiteSpec.GetLibraryFileName(skyfile);
            string redundantDocLibPath = BiblioSpecLiteSpec.GetRedundantName(docLibPath);

            Assert.IsTrue(File.Exists(docLibPath) && File.Exists(redundantDocLibPath));
            var librarySettings = SkylineWindow.Document.Settings.PeptideSettings.Libraries;

            Assert.IsTrue(librarySettings.HasDocumentLibrary);
            // We're on the "Extract Chromatograms" page of the wizard.
            // All the files should be found, and we should
            // just be able to move to the next page.
            RunUI(() => Assert.IsTrue(importPeptideSearchDlg.CurrentPage == ImportPeptideSearchDlg.Pages.chromatograms_page));
            RunUI(() =>
            {
                var importResultsControl = (ImportResultsControl)importPeptideSearchDlg.ImportResultsControl;
                importResultsControl.ExcludeSpectrumSourceFiles = true;
            });
            var importResultsNameDlg = ShowDialog <ImportResultsNameDlg>(importPeptideSearchDlg.ClickNextButtonNoCheck);

            RunUI(() =>
            {
                importResultsNameDlg.NoDialog();
            });
            WaitForClosedForm(importResultsNameDlg);
            // Modifications are already set up, so that page should get skipped.
            RunUI(() => importPeptideSearchDlg.FullScanSettingsControl.PrecursorCharges      = new [] { 2, 3, 4, 5 });
            RunUI(() => importPeptideSearchDlg.FullScanSettingsControl.PrecursorMassAnalyzer = FullScanMassAnalyzerType.tof);
            // Enable use of drift times in spectral library
            Assume.IsTrue(importPeptideSearchDlg.FullScanSettingsControl.IonMobilityFiltering.Visible);
            Assume.IsTrue(importPeptideSearchDlg.FullScanSettingsControl.IonMobilityFiltering.Enabled);
            var useDriftTimes = true; // For debugging convenience, if you want to see how this works without IM filtering

            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            RunUI(() => importPeptideSearchDlg.FullScanSettingsControl.IonMobilityFiltering.IsUseSpectralLibraryIonMobilities = useDriftTimes);
            RunUI(() => importPeptideSearchDlg.FullScanSettingsControl.IonMobilityFiltering.IonMobilityFilterResolvingPower   = 50);
            RunUI(() => importPeptideSearchDlg.ClickNextButton()); // Accept the full scan settings

            // We're on the "Import FASTA" page of the wizard.
            RunUI(() =>
            {
                Assert.IsTrue(importPeptideSearchDlg.CurrentPage == ImportPeptideSearchDlg.Pages.import_fasta_page);
                importPeptideSearchDlg.ImportFastaControl.SetFastaContent(GetTestPath("SwissProt.bsa-mature"));
            });
            var peptidesPerProteinDlg = ShowDialog <PeptidesPerProteinDlg>(importPeptideSearchDlg.ClickNextButtonNoCheck);

            OkDialog(peptidesPerProteinDlg, peptidesPerProteinDlg.OkDialog);
            WaitForClosedForm(importPeptideSearchDlg);
            WaitForDocumentChangeLoaded(doc, 15 * 60 * 1000); // 15 minutes
            var doc1 = WaitForDocumentLoaded(400000);

            AssertEx.IsDocumentState(doc1, null, 1, 40, 54, 162);
            loadStopwatch.Stop();
            DebugLog.Info("load time = {0}", loadStopwatch.ElapsedMilliseconds);

            float  tolerance = (float)doc1.Settings.TransitionSettings.Instrument.MzMatchTolerance;
            double maxHeight = 0;
            var    results   = doc1.Settings.MeasuredResults;

            foreach (var pair in doc1.PeptidePrecursorPairs)
            {
                ChromatogramGroupInfo[] chromGroupInfo;
                Assert.IsTrue(results.TryLoadChromatogram(0, pair.NodePep, pair.NodeGroup,
                                                          tolerance, true, out chromGroupInfo));

                foreach (var chromGroup in chromGroupInfo)
                {
                    foreach (var tranInfo in chromGroup.TransitionPointSets)
                    {
                        maxHeight = Math.Max(maxHeight, tranInfo.MaxIntensity);
                    }
                }
            }
            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            Assert.AreEqual(useDriftTimes ? 972186 : 1643104, maxHeight, 1);
        }
Esempio n. 40
0
        public bool BuildLibrary(IProgressMonitor progress)
        {
            _ambiguousMatches = null;
            IProgressStatus status = new ProgressStatus(Resources.BiblioSpecLiteBuilder_BuildLibrary_Preparing_to_build_library);

            progress.UpdateProgress(status);
            if (InputFiles.Any(f => f.EndsWith(EXT_PILOT)))
            {
                try
                {
                    InputFiles = VendorIssueHelper.ConvertPilotFiles(InputFiles, progress, status);
                    if (progress.IsCanceled)
                    {
                        return(false);
                    }
                }
                catch (Exception x)
                {
                    progress.UpdateProgress(status.ChangeErrorException(x));
                    return(false);
                }
            }

            string message = string.Format(Resources.BiblioSpecLiteBuilder_BuildLibrary_Building__0__library,
                                           Path.GetFileName(OutputPath));

            progress.UpdateProgress(status = status.ChangeMessage(message));
            string redundantLibrary = BiblioSpecLiteSpec.GetRedundantName(OutputPath);
            var    blibBuilder      = new BlibBuild(redundantLibrary, InputFiles, TargetSequences)
            {
                IncludeAmbiguousMatches = IncludeAmbiguousMatches,
                CutOffScore             = CutOffScore,
                Id = Id,
            };

            try
            {
                if (!blibBuilder.BuildLibrary(Action, progress, ref status, out _ambiguousMatches))
                {
                    return(false);
                }
            }
            catch (IOException x)
            {
                progress.UpdateProgress(status.ChangeErrorException(x));
                return(false);
            }
            catch (Exception x)
            {
                Console.WriteLine(x.Message);
                progress.UpdateProgress(status.ChangeErrorException(
                                            new Exception(string.Format(Resources.BiblioSpecLiteBuilder_BuildLibrary_Failed_trying_to_build_the_redundant_library__0__,
                                                                        redundantLibrary))));
                return(false);
            }
            var blibFilter = new BlibFilter();

            status = new ProgressStatus(message);
            progress.UpdateProgress(status);
            // Write the non-redundant library to a temporary file first
            try
            {
                using (var saver = new FileSaver(OutputPath))
                {
                    if (!blibFilter.Filter(redundantLibrary, saver.SafeName, progress, ref status))
                    {
                        return(false);
                    }

                    saver.Commit();
                }
            }
            catch (IOException x)
            {
                progress.UpdateProgress(status.ChangeErrorException(x));
                return(false);
            }
            catch
            {
                progress.UpdateProgress(status.ChangeErrorException(
                                            new Exception(string.Format(Resources.BiblioSpecLiteBuilder_BuildLibrary_Failed_trying_to_build_the_library__0__,
                                                                        OutputPath))));
                return(false);
            }
            finally
            {
                if (!KeepRedundant)
                {
                    FileEx.SafeDelete(redundantLibrary, true);
                }
            }

            return(true);
        }
Esempio n. 41
0
        private void Testit(
            bool useDriftTimes, // If false, don't use any drift information in chromatogram extraction
            ref LibraryIonMobilityInfo driftInfoExplicitDT
            )
        {
            bool   CCSonly = driftInfoExplicitDT != null; // If true, force conversion from CCS to DT
            var    ext     = useDriftTimes ? (CCSonly ? "CCS" : "DT") : "train";
            string skyfile = TestFilesDir.GetTestPath("test_" + ext + ".sky");

            RunUI(() =>
            {
                SkylineWindow.NewDocument(true);
                SkylineWindow.SaveDocument(skyfile);
            });



            Stopwatch loadStopwatch = new Stopwatch();

            loadStopwatch.Start();

            // Enable use of drift times in spectral library
            var peptideSettingsUI = ShowDialog <PeptideSettingsUI>(SkylineWindow.ShowPeptideSettingsUI);

            RunUI(() =>
            {
                // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                peptideSettingsUI.IsUseSpectralLibraryDriftTimes         = useDriftTimes;
                peptideSettingsUI.SpectralLibraryDriftTimeResolvingPower = 50;
            });
            OkDialog(peptideSettingsUI, peptideSettingsUI.OkDialog);

            // Launch import peptide search wizard
            var importPeptideSearchDlg = ShowDialog <ImportPeptideSearchDlg>(SkylineWindow.ShowImportPeptideSearchDlg);
            var basename      = _testCase == 1 ? "40minG_WBP_wide_z2-3_mid_BSA_5pmol_01" : "09_BSAtrypticdigest_5uL_IMQTOF_AltFramesdtramp_dAJS009";
            var nextFile      = _testCase == 1 ? null : "10_BSAtrypticdigest_5uL_IMQTOF_AltFramesdtramp_dAJS010.d";
            var searchResults = GetTestPath(basename + ".pep.xml");

            var doc = SkylineWindow.Document;

            if (CCSonly || !useDriftTimes)
            {
                // Hide the drift time info provided by SpectrumMill, so we have to convert from CCS
                var mzxmlFile    = searchResults.Replace("pep.xml", "mzXML");
                var fileContents = File.ReadAllText(mzxmlFile);
                fileContents = fileContents.Replace(" DT=", " xx=");
                if (!useDriftTimes)
                {
                    fileContents = fileContents.Replace(" CCS=", " xxx=");
                }
                File.WriteAllText(mzxmlFile, fileContents);
            }

            var searchResultsList = new[] { searchResults };

            RunUI(() =>
            {
                Assert.IsTrue(importPeptideSearchDlg.CurrentPage ==
                              ImportPeptideSearchDlg.Pages.spectra_page);
                importPeptideSearchDlg.BuildPepSearchLibControl.AddSearchFiles(searchResultsList);
                importPeptideSearchDlg.BuildPepSearchLibControl.CutOffScore = 0.95;
                importPeptideSearchDlg.BuildPepSearchLibControl.FilterForDocumentPeptides = false;
            });

            RunUI(() => Assert.IsTrue(importPeptideSearchDlg.ClickNextButton()));
            doc = WaitForDocumentChange(doc);

            // Verify document library was built
            string docLibPath          = BiblioSpecLiteSpec.GetLibraryFileName(skyfile);
            string redundantDocLibPath = BiblioSpecLiteSpec.GetRedundantName(docLibPath);

            Assert.IsTrue(File.Exists(docLibPath) && File.Exists(redundantDocLibPath));
            var librarySettings = SkylineWindow.Document.Settings.PeptideSettings.Libraries;

            Assert.IsTrue(librarySettings.HasDocumentLibrary);
            // We're on the "Extract Chromatograms" page of the wizard.
            // All the files should be found, and we should
            // just be able to move to the next page.
            RunUI(() => Assert.IsTrue(importPeptideSearchDlg.CurrentPage == ImportPeptideSearchDlg.Pages.chromatograms_page));
            RunUI(() =>
            {
                var importResultsControl = (ImportResultsControl)importPeptideSearchDlg.ImportResultsControl;
                importResultsControl.ExcludeSpectrumSourceFiles = true;
                importResultsControl.UpdateResultsFiles(new [] { TestFilesDirs[0].PersistentFilesDir }, true); // Go look in the persistent files dir
            });
            if (searchResultsList.Length > 1)
            {
                // Deal with the common name start dialog
                var importResultsNameDlg = ShowDialog <ImportResultsNameDlg>(importPeptideSearchDlg.ClickNextButtonNoCheck);
                RunUI(() =>
                {
                    importResultsNameDlg.NoDialog();
                });
                WaitForClosedForm(importResultsNameDlg);
            }
            else
            {
                RunUI(() => importPeptideSearchDlg.ClickNextButtonNoCheck());
            }
            // Modifications are already set up, so that page should get skipped.
            RunUI(() => importPeptideSearchDlg.FullScanSettingsControl.PrecursorCharges      = new [] { 2, 3, 4, 5 });
            RunUI(() => importPeptideSearchDlg.FullScanSettingsControl.PrecursorMassAnalyzer = FullScanMassAnalyzerType.tof);
            RunUI(() => importPeptideSearchDlg.ClickNextButton()); // Accept the full scan settings

            // We're on the "Import FASTA" page of the wizard.
            RunUI(() =>
            {
                Assert.IsTrue(importPeptideSearchDlg.CurrentPage == ImportPeptideSearchDlg.Pages.import_fasta_page);
                importPeptideSearchDlg.ImportFastaControl.SetFastaContent(GetTestPath("SwissProt.bsa-mature"));
            });
            var peptidesPerProteinDlg = ShowDialog <PeptidesPerProteinDlg>(importPeptideSearchDlg.ClickNextButtonNoCheck);

            OkDialog(peptidesPerProteinDlg, peptidesPerProteinDlg.OkDialog);

            WaitForClosedForm(importPeptideSearchDlg);
            var doc1 = WaitForDocumentChangeLoaded(doc, 15 * 60 * 1000); // 15 minutes

            if (_testCase == 1)
            {
                AssertEx.IsDocumentState(doc1, null, 1, 34, 45, 135);
            }
            else
            {
                AssertEx.IsDocumentState(doc1, null, 1, 36, 43, 129);
            }
            loadStopwatch.Stop();
            DebugLog.Info("load time = {0}", loadStopwatch.ElapsedMilliseconds);
            var errmsg = "";

            if (!useDriftTimes)
            {
                // Inspect the loaded data directly to derive DT and CCS
                // Verify ability to extract predictions from raw data
                var peptideSettingsDlg = ShowDialog <PeptideSettingsUI>(
                    () => SkylineWindow.ShowPeptideSettingsUI(PeptideSettingsUI.TABS.Prediction));

                // Simulate user picking Edit Current from the Drift Time Predictor combo control
                var driftTimePredictorDlg = ShowDialog <EditDriftTimePredictorDlg>(peptideSettingsDlg.AddDriftTimePredictor);
                RunUI(() =>
                {
                    driftTimePredictorDlg.SetPredictorName("test");
                    driftTimePredictorDlg.SetResolvingPower(50);
                    driftTimePredictorDlg.GetDriftTimesFromResults();
                    driftTimePredictorDlg.OkDialog();
                });
                WaitForClosedForm(driftTimePredictorDlg);
                RunUI(() =>
                {
                    peptideSettingsDlg.OkDialog();
                });
                WaitForClosedForm(peptideSettingsDlg);

                var document    = SkylineWindow.Document;
                var measuredDTs = document.Settings.PeptideSettings.Prediction.IonMobilityPredictor.MeasuredMobilityIons;
                Assert.IsNotNull(driftInfoExplicitDT, "driftInfoExplicitDT != null");
                var explicitDTs = driftInfoExplicitDT.GetIonMobilityDict();

                string errMsgAll = string.Empty;
                // A handful of peptides that really should have been trained on a clean sample
                // CONSIDER: or are they multiple conformers? They have multiple hits with distinct IM in the pepXML
                var expectedDiffs = LibKeyMap <double> .FromDictionary(new Dictionary <LibKey, double>
                {
                    { new PeptideLibraryKey("LC[+57.0]VLHEK", 2), 18.09 },
                    { new PeptideLibraryKey("EC[+57.0]C[+57.0]DKPLLEK", 3), 7.0 },
                    { new PeptideLibraryKey("SHC[+57.0]IAEVEK", 3), 6.0 },
                    { new PeptideLibraryKey("DDPHAC[+57.0]YSTVFDK", 2), 24.0 }
                }).AsDictionary();

                foreach (var pair in doc1.PeptidePrecursorPairs)
                {
                    string errMsg   = string.Empty;
                    var    key      = new LibKey(pair.NodePep.ModifiedSequence, pair.NodeGroup.PrecursorAdduct);
                    double tolerCCS = 5;
                    if (expectedDiffs.ContainsKey(key))
                    {
                        tolerCCS = expectedDiffs[key] + .1;
                    }
                    if (!explicitDTs.ContainsKey(key))
                    {
                        errMsg += "Could not locate explicit IMS info for " + key + "\n";
                    }
                    var given    = explicitDTs[key][0];
                    var measured = measuredDTs[key];
                    var msg      = CheckDeltaPct(given.CollisionalCrossSectionSqA ?? 0, measured.CollisionalCrossSectionSqA ?? 0, tolerCCS, "measured CCS", key.ToString());
                    if (!string.IsNullOrEmpty(msg))
                    {
                        errMsg += msg + CheckDeltaPct(given.IonMobility.Mobility.Value, measured.IonMobility.Mobility.Value, -1, "measured drift time", key.ToString());
                    }
                    else
                    {
                        errMsg += CheckDelta(given.IonMobility.Mobility.Value, measured.IonMobility.Mobility.Value, 10.0, "measured drift time", key.ToString());
                    }
                    errMsg += CheckDelta(given.HighEnergyIonMobilityValueOffset, measured.HighEnergyIonMobilityValueOffset, 2.0, "measured drift time high energy offset", key.ToString());
                    if (!string.IsNullOrEmpty(errMsg))
                    {
                        errMsgAll += "\n" + errMsg;
                    }
                }
                if (!string.IsNullOrEmpty(errMsgAll))
                {
                    Assert.Fail(errMsgAll);
                }
                return;
            }

            LibraryIonMobilityInfo libraryIonMobilityInfo;

            doc1.Settings.PeptideSettings.Libraries.Libraries.First().TryGetIonMobilityInfos(doc1.MoleculeLibKeys.ToArray(), 0, out libraryIonMobilityInfo);
            if (driftInfoExplicitDT == null)
            {
                driftInfoExplicitDT = libraryIonMobilityInfo;
            }
            else
            {
                var instrumentInfo = new DataFileInstrumentInfo(new MsDataFileImpl(GetTestPath(basename + ".d")));
                var dictExplicitDT = driftInfoExplicitDT.GetIonMobilityDict();
                foreach (var pep in doc1.Peptides)
                {
                    foreach (var nodeGroup in pep.TransitionGroups)
                    {
                        double windowDT;
                        var    calculatedDriftTime = doc1.Settings.GetIonMobility(
                            pep, nodeGroup, null, libraryIonMobilityInfo, instrumentInfo, 0, out windowDT);
                        var libKey = new LibKey(pep.ModifiedSequence, nodeGroup.PrecursorAdduct);
                        IonMobilityAndCCS[] infoValueExplicitDT;
                        if (!dictExplicitDT.TryGetValue(libKey, out infoValueExplicitDT))
                        {
                            errmsg += "No driftinfo value found for " + libKey + "\n";
                        }
                        else
                        {
                            var ionMobilityInfo = infoValueExplicitDT[0];
                            var delta           = Math.Abs(ionMobilityInfo.IonMobility.Mobility.Value - calculatedDriftTime.IonMobility.Mobility.Value);
                            var acceptableDelta = (libKey.Sequence.StartsWith("DDPHAC") || libKey.Sequence.EndsWith("VLHEK")) ? 3: 1; // These were ambiguous matches
                            if (delta > acceptableDelta)
                            {
                                errmsg += String.Format("calculated DT ({0}) and explicit DT ({1}, CCS={4}) do not agree (abs delta = {2}) for {3}\n",
                                                        calculatedDriftTime.IonMobility, ionMobilityInfo.IonMobility,
                                                        delta, libKey,
                                                        ionMobilityInfo.CollisionalCrossSectionSqA ?? 0);
                            }
                        }
                    }
                }
            }

            float  tolerance = (float)doc1.Settings.TransitionSettings.Instrument.MzMatchTolerance;
            double maxHeight = 0;
            var    results   = doc1.Settings.MeasuredResults;
            var    numPeaks  = _testCase == 1 ?
                               new[] { 8, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 10, 7, 10, 10, 10, 10, 8, 10, 10, 10, 10, 10, 10, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 10, 10, 8, 10, 10, 10, 10, 10 } :
            new[] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 8, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 };

            int npIndex = 0;

            foreach (var pair in doc1.PeptidePrecursorPairs)
            {
                ChromatogramGroupInfo[] chromGroupInfo;
                Assert.IsTrue(results.TryLoadChromatogram(0, pair.NodePep, pair.NodeGroup,
                                                          tolerance, true, out chromGroupInfo));

                foreach (var chromGroup in chromGroupInfo)
                {
                    if (numPeaks[npIndex] != chromGroup.NumPeaks)
                    {
                        errmsg += String.Format("unexpected peak count {0} instead of {1} in chromatogram {2}\r\n", chromGroup.NumPeaks, numPeaks[npIndex], npIndex);
                    }
                    npIndex++;
                    foreach (var tranInfo in chromGroup.TransitionPointSets)
                    {
                        maxHeight = Math.Max(maxHeight, tranInfo.MaxIntensity);
                    }
                }
            }
            Assert.IsTrue(errmsg.Length == 0, errmsg);
            Assert.AreEqual(_testCase == 1 ? 2265204 : 1326442, maxHeight, 1);

            // Does CCS show up in reports?
            var expectedDtWindow = _testCase == 1 ? 0.74 : 0.94;

            TestReports(doc1, 0, expectedDtWindow);

            if (nextFile != null)
            {
                // Verify that we can use library generated for one file as the default for another without its own library
                ImportResults(nextFile);
                TestReports(SkylineWindow.Document, 1, expectedDtWindow);
            }

            // And verify roundtrip of ion mobility
            AssertEx.RoundTrip(SkylineWindow.Document);
            RunUI(() =>
            {
                SkylineWindow.SaveDocument(skyfile);
                SkylineWindow.NewDocument(true);
                SkylineWindow.OpenFile(skyfile);
            });
            TestReports(SkylineWindow.Document, 1, expectedDtWindow);

            // Watch for problem with reimport after changed DT window
            var docResolvingPower  = SkylineWindow.Document;
            var peptideSettingsUI2 = ShowDialog <PeptideSettingsUI>(SkylineWindow.ShowPeptideSettingsUI);

            RunUI(() =>
            {
                // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                peptideSettingsUI2.IsUseSpectralLibraryDriftTimes         = useDriftTimes;
                peptideSettingsUI2.SpectralLibraryDriftTimeResolvingPower = 40;
            });
            OkDialog(peptideSettingsUI2, peptideSettingsUI2.OkDialog);
            var docReimport = WaitForDocumentChangeLoaded(docResolvingPower);

            // Reimport data for a replicate
            RunDlg <ManageResultsDlg>(SkylineWindow.ManageResults, dlg =>
            {
                var chromatograms         = docReimport.Settings.MeasuredResults.Chromatograms;
                dlg.SelectedChromatograms = new[] { chromatograms[0] };
                dlg.ReimportResults();
                dlg.OkDialog();
            });
            WaitForDocumentChangeLoaded(docReimport);
            var expectedDtWindow0 = _testCase == 2 ? 1.175 : 0.92;
            var expectedDtWindow1 = _testCase == 2 ? 0.94 : 0.92;

            TestReports(SkylineWindow.Document, 0, expectedDtWindow0, string.Format(" row {0} case {1} ccsOnly {2}", 0, _testCase, CCSonly));
            TestReports(SkylineWindow.Document, 1, expectedDtWindow1, string.Format(" row {0} case {1} ccsOnly {2}", 1, _testCase, CCSonly));
        }
Esempio n. 42
0
        /// <summary>
        /// Minimize any library type to a fully functional BiblioSpec SQLite library.
        /// </summary>
        /// <param name="librarySpec">Library spec for which the new library is created</param>
        /// <param name="library">Existing library to minimize</param>
        /// <param name="document">Document for which only used spectra are included in the new library</param>
        /// <returns>A new minimized <see cref="BiblioSpecLiteLibrary"/></returns>
        public BiblioSpecLiteLibrary MinimizeLibrary(BiblioSpecLiteSpec librarySpec,
            Library library, SrmDocument document)
        {
            if (!UpdateProgressMessage(string.Format(Resources.BlibDb_MinimizeLibrary_Minimizing_library__0__, library.Name)))
                return null;

            string libAuthority = "unknown.org"; // Not L10N
            string libId = library.Name;
            // CONSIDER: Use version numbers of the original library?
            int libraryRevision = DbLibInfo.INITIAL_LIBRARY_REVISION;
            int schemaVersion = 0;

            bool saveRetentionTimes = false;
            bool saveRedundantLib = false;

            var blibLib = library as BiblioSpecLiteLibrary;
            if (blibLib != null)
            {
                string libraryLsid = blibLib.Lsid;
                Match matchLsid = REGEX_LSID.Match(libraryLsid);
                if (matchLsid.Success)
                {
                    libAuthority = matchLsid.Groups[1].Value;
                    libId = matchLsid.Groups[2].Value;
                }
                else
                {
                    libAuthority = BiblioSpecLiteLibrary.DEFAULT_AUTHORITY;
                }

                // We will have a RetentionTimes table if schemaVersion if 1 or greater.
                saveRetentionTimes = blibLib.SchemaVersion >= 1;
                libraryRevision = blibLib.Revision;
                schemaVersion = Math.Min(blibLib.SchemaVersion, DbLibInfo.SCHEMA_VERSION_CURRENT);

                // If the document has MS1 filtering enabled we will save a minimized version
                // of the redundant library, if available.
                if(document.Settings.TransitionSettings.FullScan.IsEnabledMs)
                {
                    String redundantLibPath = blibLib.FilePathRedundant;
                    if(File.Exists(redundantLibPath))
                    {
                        string path = BiblioSpecLiteSpec.GetRedundantName(FilePath);
                        CreateSessionFactory_Redundant(path);
                        saveRedundantLib = true;
                    }
                }
            }
            else if (library is BiblioSpecLibrary)
                libAuthority = BiblioSpecLiteLibrary.DEFAULT_AUTHORITY;
            else if (library is XHunterLibrary)
                libAuthority = XHunterLibrary.DEFAULT_AUTHORITY;
            else
            {
                var nistLibrary = library as NistLibrary;
                if (nistLibrary != null)
                {
                    libAuthority = NistLibrary.DEFAULT_AUTHORITY;
                    libId = nistLibrary.Id ?? libId;
                }
            }
            // Use a very specific LSID, since it really only matches this document.
            string libLsid = string.Format("urn:lsid:{0}:spectral_libary:bibliospec:nr:minimal:{1}:{2}:{3}.{4}", // Not L10N
                libAuthority, libId, Guid.NewGuid(), libraryRevision, schemaVersion);

            var dictLibrary = new Dictionary<LibKey, BiblioLiteSpectrumInfo>();

            // Hash table to store the database IDs of any source files in the library
            // Source file information is available only in Bibliospec libraries, schema version >= 1
            var dictFiles = new Dictionary<string, long>();
            var dictFilesRedundant = new Dictionary<string, long>();

            ISession redundantSession = null;
            ITransaction redundantTransaction = null;
            int redundantSpectraCount = 0;

            try
            {
                using (ISession session = OpenWriteSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    var settings = document.Settings;

                    int peptideCount = document.PeptideCount;
                    int savedCount = 0;

                    foreach (var nodePep in document.Peptides)
                    {
                        var mods = nodePep.ExplicitMods;

                        foreach (TransitionGroupDocNode nodeGroup in nodePep.Children)
                        {
                            // Only get library info from precursors that use the desired library
                            if (!nodeGroup.HasLibInfo || !Equals(nodeGroup.LibInfo.LibraryName, library.Name))
                                continue;

                            TransitionGroup group = nodeGroup.TransitionGroup;
                            string peptideSeq = group.Peptide.Sequence;
                            int precursorCharge = group.PrecursorCharge;
                            IsotopeLabelType labelType = nodeGroup.TransitionGroup.LabelType;

                            var calcPre = settings.GetPrecursorCalc(labelType, mods);
                            var peptideModSeq = calcPre.GetModifiedSequence(peptideSeq, false);
                            var libKey = new LibKey(peptideModSeq, precursorCharge);

                            if (dictLibrary.ContainsKey(libKey))
                                continue;

                            // saveRetentionTimes will be false unless this is a BiblioSpec(schemaVersion >=1) library.
                            if (!saveRetentionTimes)
                            {
                                // get the best spectra
                                foreach (var spectrumInfo in library.GetSpectra(libKey, labelType, LibraryRedundancy.best))
                                {
                                    DbRefSpectra refSpectra = MakeRefSpectrum(spectrumInfo,
                                                                              peptideSeq,
                                                                              peptideModSeq,
                                                                              nodeGroup.PrecursorMz,
                                                                              precursorCharge);

                                    session.Save(refSpectra);

                                    dictLibrary.Add(libKey,
                                                    new BiblioLiteSpectrumInfo(libKey, refSpectra.Copies,
                                                                               refSpectra.NumPeaks,
                                                                               (int) (refSpectra.Id ?? 0),
                                                                               default(IndexedRetentionTimes),
                                                                               default(IndexedIonMobilities)));
                                }

                                session.Flush();
                                session.Clear();
                            }
                                // This is a BiblioSpec(schemaVersion >=1) library.
                            else
                            {
                                // get all the spectra, including the redundant ones if this library has any
                                var spectra = library.GetSpectra(libKey, labelType, LibraryRedundancy.all_redundant).ToArray();
                                // Avoid saving to the RefSpectra table for isotope label types that have no spectra
                                if (spectra.Length == 0)
                                    continue;

                                DbRefSpectra refSpectra = new DbRefSpectra
                                                              {
                                                                  PeptideSeq = peptideSeq,
                                                                  PrecursorMZ = nodeGroup.PrecursorMz,
                                                                  PrecursorCharge = precursorCharge,
                                                                  PeptideModSeq = peptideModSeq
                                                              };

                                // Get all the information for this reference spectrum.
                                // For BiblioSpec (schema ver >= 1), this can include retention time information
                                // for this spectrum as well as any redundant spectra for the peptide.
                                // Ids of spectra in the redundant library, where available, are also returned.
                                var redundantSpectraKeys = new List<SpectrumKeyTime>();
                                BuildRefSpectra(document, session, refSpectra, spectra, dictFiles, redundantSpectraKeys);

                                session.Save(refSpectra);
                                session.Flush();
                                session.Clear();

                                // TODO(nicksh): preserve retention time information.
                                var retentionTimesByFileId = default(IndexedRetentionTimes);
                                var driftTimesByFileId = default(IndexedIonMobilities);
                                dictLibrary.Add(libKey,
                                                new BiblioLiteSpectrumInfo(libKey,
                                                                           refSpectra.Copies,
                                                                           refSpectra.NumPeaks,
                                                                           (int) (refSpectra.Id ?? 0),
                                                                           retentionTimesByFileId,
                                                                           driftTimesByFileId));

                                // Save entries in the redundant library.
                                if (saveRedundantLib && redundantSpectraKeys.Count > 0)
                                {
                                    if (redundantSession == null)
                                    {
                                        redundantSession = OpenWriteSession_Redundant();
                                        redundantTransaction = redundantSession.BeginTransaction();
                                    }
                                    SaveRedundantSpectra(redundantSession, redundantSpectraKeys, dictFilesRedundant, refSpectra, library);
                                    redundantSpectraCount += redundantSpectraKeys.Count;
                                }
                            }
                        }

                        savedCount++;
                        if (!UpdateProgress(peptideCount, savedCount))
                            return null;
                    }

                    // Simulate ctime(d), which is what BlibBuild uses.
                    string createTime = string.Format("{0:ddd MMM dd HH:mm:ss yyyy}", DateTime.Now); // Not L10N? different date/time format in different countries
                    DbLibInfo libInfo = new DbLibInfo
                                            {
                                                LibLSID = libLsid,
                                                CreateTime = createTime,
                                                NumSpecs = dictLibrary.Count,
                                                MajorVersion = libraryRevision,
                                                MinorVersion = schemaVersion
                                            };

                    session.Save(libInfo);
                    session.Flush();
                    session.Clear();

                    transaction.Commit();

                    if (redundantTransaction != null)
                    {
                        var scoreType = new DbScoreTypes {Id = 0, ScoreType = "UNKNOWN"}; // Not L10N
                        redundantSession.Save(scoreType);

                        libInfo = new DbLibInfo
                                      {
                                          LibLSID = libLsid.Replace(":nr:", ":redundant:"), // Not L10N
                                          CreateTime = createTime,
                                          NumSpecs = redundantSpectraCount,
                                          MajorVersion = libraryRevision,
                                          MinorVersion = schemaVersion
                                      };
                        redundantSession.Save(libInfo);
                        redundantSession.Flush();
                        redundantSession.Clear();

                        redundantTransaction.Commit();
                    }
                }

            }
            finally
            {
                if(redundantTransaction != null)
                {
                    redundantTransaction.Dispose();
                }
                if (redundantSession != null)
                {
                    redundantSession.Dispose();
                }
            }

            var libraryEntries = dictLibrary.Values.ToArray();

            return new BiblioSpecLiteLibrary(librarySpec, libLsid, libraryRevision, schemaVersion,
                libraryEntries, FileStreamManager.Default);
        }
Esempio n. 43
0
        protected override void DoTest()
        {
            string skyfile = TestFilesDir.GetTestPath("Mix1_SkylineIMS_Test-reimport_RP50.sky");

            RunUI(() => SkylineWindow.OpenFile(skyfile));

            var doc0 = WaitForDocumentLoaded();

            AssertEx.IsDocumentState(doc0, null, 4, 218, 429, 3176);

            Stopwatch loadStopwatch = new Stopwatch();

            loadStopwatch.Start();
            // Launch import peptide search wizard
            var importPeptideSearchDlg = ShowDialog <ImportPeptideSearchDlg>(SkylineWindow.ShowImportPeptideSearchDlg);


            const string ID12692Base   = "ID12692_01_UCA168_3727_040714";
            string       ID12692Search = GetTestPath(ID12692Base + "_IA_final_fragment.csv");

            string[] searchFiles = { ID12692Search };
            var      doc         = SkylineWindow.Document;

            RunUI(() =>
            {
                Assert.IsTrue(importPeptideSearchDlg.CurrentPage ==
                              ImportPeptideSearchDlg.Pages.spectra_page);
                importPeptideSearchDlg.BuildPepSearchLibControl.AddSearchFiles(searchFiles);
                importPeptideSearchDlg.BuildPepSearchLibControl.CutOffScore = 0.95;
                importPeptideSearchDlg.BuildPepSearchLibControl.FilterForDocumentPeptides = true;
            });

            RunUI(() => Assert.IsTrue(importPeptideSearchDlg.ClickNextButton()));
            doc = WaitForDocumentChange(doc);

            // Verify document library was built
            string docLibPath          = BiblioSpecLiteSpec.GetLibraryFileName(skyfile);
            string redundantDocLibPath = BiblioSpecLiteSpec.GetRedundantName(docLibPath);

            Assert.IsTrue(File.Exists(docLibPath) && File.Exists(redundantDocLibPath));
            var librarySettings = SkylineWindow.Document.Settings.PeptideSettings.Libraries;

            Assert.IsTrue(librarySettings.HasDocumentLibrary);

            // We're on the "Extract Chromatograms" page of the wizard.
            // All the files should be found, and we should
            // just be able to move to the next page.
            RunUI(() => Assert.IsTrue(importPeptideSearchDlg.CurrentPage == ImportPeptideSearchDlg.Pages.chromatograms_page));
            RunUI(() => importPeptideSearchDlg.ClickNextButton());
            // Modifications are already set up, so that page should get skipped.
            RunUI(() => importPeptideSearchDlg.ClickNextButton());     // Accept the full scan settings

/* No lockmass correction for IMS data due to Waters DLL limitations as of Nov 2015
 *              var lockmassDlg = ShowDialog<ImportResultsLockMassDlg>(() => importPeptideSearchDlg.ClickNextButton()); // Accept the full scan settings, lockmass correction dialog should appear
 *              RunUI(() =>
 *              {
 *                  var mz = 785.8426;  // Glu-Fib ESI 2+, per Will T
 *                  lockmassDlg.LockmassPositive = mz;
 *                  lockmassDlg.LockmassNegative = mz;
 *                  lockmassDlg.LockmassTolerance = 10.0;
 *              });
 *              RunUI(lockmassDlg.OkDialog);
 *              WaitForClosedForm<ImportResultsLockMassDlg>();
 */

            // Add FASTA also skipped because filter for document peptides was chosen.

            WaitForClosedForm(importPeptideSearchDlg);
            WaitForDocumentChangeLoaded(doc, 15 * 60 * 1000);     // 15 minutes

            var doc1 = WaitForDocumentLoaded(400000);

            AssertEx.IsDocumentState(doc1, null, 4, 63, 6, 42);      // Was 4, 63, 4, 30 before drift time based charge state detection was added to final_fragments reader

            loadStopwatch.Stop();
            DebugLog.Info("load time = {0}", loadStopwatch.ElapsedMilliseconds);

            float  tolerance = (float)doc1.Settings.TransitionSettings.Instrument.MzMatchTolerance;
            double maxHeight = 0;
            var    results   = doc1.Settings.MeasuredResults;

            var numPeaks = new[] { 8, 10, 10, 10, 10, 10, 10 };
            int npIndex  = 0;
            var errmsg   = "";

            foreach (var pair in doc1.PeptidePrecursorPairs)
            {
                ChromatogramGroupInfo[] chromGroupInfo;
                Assert.IsTrue(results.TryLoadChromatogram(0, pair.NodePep, pair.NodeGroup,
                                                          tolerance, true, out chromGroupInfo));

                foreach (var chromGroup in chromGroupInfo)
                {
                    if (numPeaks[npIndex] != chromGroup.NumPeaks)
                    {
                        errmsg += String.Format("unexpected peak count {0} instead of {1} in chromatogram {2}\r\n", chromGroup.NumPeaks, numPeaks[npIndex], npIndex);
                    }
                    npIndex++;
                    foreach (var tranInfo in chromGroup.TransitionPointSets)
                    {
                        maxHeight = Math.Max(maxHeight, tranInfo.MaxIntensity);
                    }
                }
            }
            Assert.IsTrue(errmsg.Length == 0, errmsg);
            Assert.AreEqual(3617.529, maxHeight, 1);
        }
Esempio n. 44
0
        public void TestModificationMatcher()
        {
            InitSeqs();

            var carbC = StaticModList.GetDefaultsOn()[0];

            // Test exception thrown if unable to match - mass.
            UpdateMatcherFail(STR_FAIL_MASS);
            UpdateMatcherFail(STR_FAIL_NOT_A_NUMBER);
            // Test exception thrown if unable to match - name.
            UpdateMatcherFail(STR_FAIL_NAME);
            // Can't match empty modifications.
            UpdateMatcherFail(STR_FAIL_EMPTY_MOD);
            UpdateMatcherFail(STR_FAIL_EMPTY_MOD2);
            // Can't match double modifications.
            UpdateMatcherFail(STR_FAIL_DOUBLE_MOD);
            // Test exception thrown if unimod not specified correctly
            UpdateMatcherFail(STR_FAIL_UNIMOD);
            UpdateMatcherFail(STR_UNKNOWN_UNIMOD);
            // Can't phosphorylate tryptophan
            UpdateMatcherFail(STR_FAIL_WRONG_AA_UNIMOD);
            // Can't put C-terminal modification in middle of peptide
            UpdateMatcherFail(STR_FAIL_UNIMOD_TERMINUS);

            // Test mods in UniMod match correctly.
            UpdateMatcher(StaticModList.GetDefaultsOn(), HeavyModList.GetDefaultsOn(), null, null);
            // A sequence with no modifications should not be explicitly modified.
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_NO_MODS).HasExplicitMods);
            var nodeCysOxi = MATCHER.GetModifiedNode(STR_CYS_AND_OXI);

            Assert.IsTrue(nodeCysOxi.HasExplicitMods);
            Assert.IsFalse(nodeCysOxi.ExplicitMods.HasHeavyModifications);
            // Modifications should match by name.
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_MOD_BY_NAME).ExplicitMods.StaticModifications.Contains(mod =>
                                                                                                             Equals(mod.Modification.Name, "Phospho (ST)")));
            // Test can find terminal modification
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_TERM_ONLY).ExplicitMods.HeavyModifications.Contains(mod =>
                                                                                                          mod.Modification.EquivalentAll(UniMod.GetModification("Label:13C(6) (C-term R)", false))));
            // Test can find matches on terminus that are not terminal
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_MOD_BY_NAME).ExplicitMods.StaticModifications.Contains(mod =>
                                                                                                             mod.Modification.Terminus == null));
            // Test matching negative masses
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_AMMONIA_LOSS).ExplicitMods.StaticModifications.Contains(mod =>
                                                                                                              mod.Modification.EquivalentAll(UniMod.GetModification("Ammonia-loss (N-term C)", true))));

            // General and specific
            // If all AAs modified, try for most general modification.
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_HEAVY_15)
                          .ExplicitMods.HeavyModifications.Contains(mod => mod.Modification.Equivalent(LABEL15_N)));

            // Updating the settings.
            // Peptide settings should change to include new mods.
            var          docNew = new SrmDocument(SrmSettingsList.GetDefault());
            IdentityPath firstAdded;
            IdentityPath nextAdded;

            docNew = docNew.AddPeptideGroups(new[] { new PeptideGroupDocNode(new PeptideGroup(), "PepGroup1", "",
                                                                             new[] { MATCHER.GetModifiedNode(STR_MOD_BY_NAME) }) }, true, null, out firstAdded, out nextAdded);
            var pepSetNew = MATCHER.GetDocModifications(docNew);

            Assert.IsTrue(pepSetNew.StaticModifications.Contains(UniMod.GetModification("Phospho (ST)", true).ChangeExplicit(true)));
            // Update the document to the new settings.
            var pepSetNew1      = pepSetNew;
            var settingsNew2    = docNew.Settings.ChangePeptideModifications(mods => pepSetNew1);
            var lightGlobalMods = new MappedList <string, StaticMod>();

            lightGlobalMods.AddRange(settingsNew2.PeptideSettings.Modifications.StaticModifications);
            var heavyGlobalMods = new MappedList <string, StaticMod>();

            heavyGlobalMods.AddRange(settingsNew2.PeptideSettings.Modifications.AllHeavyModifications);
            // Match again. Test FoundMatches string should now be empty.
            MATCHER.CreateMatches(docNew.Settings.ChangePeptideModifications(mods => pepSetNew1),
                                  new List <string> {
                STR_MOD_BY_NAME
            }, lightGlobalMods, heavyGlobalMods);
            Assert.IsTrue(string.IsNullOrEmpty(MATCHER.FoundMatches));

            // Adding 15N to the settings.
            UpdateMatcher(new[] { carbC }, new[] { LABEL15_N }, null, null);
            // Test sequences with only explicit heavy mods should not have explicit light mods
            Assert.IsNull(MATCHER.GetModifiedNode(STR_HEAVY_ONLY).ExplicitMods.StaticModifications);

            // Test sequences with only explicit light mods should not have explicit heavy mods
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_LIGHT_ONLY).ExplicitMods.HasHeavyModifications);

            // Test global mods take precendence over UniMod
            UpdateMatcher(new[] { carbC }, null, new[] { OXIDATION_M_GLOBAL }, new[] { LABEL15_N });
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_CYS_AND_OXI).ExplicitMods.StaticModifications
                          .Contains(mod => Equals(mod.Modification, OXIDATION_M_GLOBAL)));

            // Test document mods take precendence over UniMod
            UpdateMatcher(new[] { carbC, METHIONINE_OXIDATION }, null, new[] { OXIDATION_M_GLOBAL }, new[] { LABEL15_N });
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_CYS_AND_OXI).HasExplicitMods);

            // Test exception thrown if match doesn't make sense - wrong AA.
            UpdateMatcherFail(STR_FAIL_OX_ON_D);
            // Test exception thrown if match doesn't make sense - wrong terminus.
            _seqs.Add(STR_FAIL_OX_TERM);
            AssertEx.ThrowsException <FormatException>(() => UpdateMatcher(new[] { OXIDATION_M_C_TERM }, null, null, null));
            _seqs.Remove(STR_FAIL_OX_TERM);

            // Heavy 15N - All AAs.
            UpdateMatcher(new[] { carbC, METHIONINE_OXIDATION }, new[] { LABEL15_N }, null, null);
            // Node should be created from document settings if possible.
            Assert.IsNull(MATCHER.GetModifiedNode(STR_HEAVY_15).ExplicitMods);
            // Heavy 15N - specific AA.
            // If only a specific AA is modified, there must be an explicit mod.
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_HEAVY_15_F).HasExplicitMods);

            // Test variable mods match correctly.
            // Put variable mod in global mod and not on doc - make sure don't get variable mod,
            // should get explicit mod in that case.
            var variableMetOx = METHIONINE_OXIDATION.ChangeVariable(true);

            UpdateMatcher(new[] { carbC }, null, new[] { variableMetOx }, null);
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_CYS_AND_OXI).HasExplicitMods);
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_CYS_OXI_PHOS).ExplicitMods.IsVariableStaticMods);
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_CYS_OXI_PHOS_CAP).ExplicitMods.IsVariableStaticMods);
            // Add variable mod to doc
            UpdateMatcher(new[] { carbC, variableMetOx }, null, null, null);
            // Mod can be created by the settings.
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_CYS_AND_OXI).HasExplicitMods);
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_CYS_AND_OXI).ExplicitMods.IsVariableStaticMods);
            // Mod cannot be created by the settings.
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_CYS_OXI_PHOS).ExplicitMods.IsVariableStaticMods);
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_CYS_OXI_PHOS_CAP).ExplicitMods.IsVariableStaticMods);

            // Add Met Ox to global. Test: +16 finds it.
            UpdateMatcher(new[] { carbC }, null, new[] { MET_OX_ROUNDED }, null);
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_CYS_AND_OXI).
                          ExplicitMods.StaticModifications.Contains(mod => Equals(mod.Modification, MET_OX_ROUNDED)));
            // Test: +15.99 finds UniMod.
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_HEAVY_15).
                           ExplicitMods.StaticModifications.Contains(mod => Equals(mod.Modification, MET_OX_ROUNDED)));
            // Add Methionine Oxidation before Met Ox. Test: +16 finds it.
            UpdateMatcher(new[] { carbC }, null, new[] { METHIONINE_OXIDATION, MET_OX_ROUNDED }, null);
            Assert.IsFalse(MATCHER.GetModifiedNode(STR_CYS_AND_OXI).
                           ExplicitMods.StaticModifications.Contains(mod => Equals(mod.Modification, MET_OX_ROUNDED)));
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_CYS_AND_OXI).
                          ExplicitMods.StaticModifications.Contains(mod => Equals(mod.Modification, METHIONINE_OXIDATION)));
            // Test long masses rounded.
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_METOX_LONG_MASS).ExplicitMods.StaticModifications.Contains(mod =>
                                                                                                                 Equals(mod.Modification, METHIONINE_OXIDATION)));
            // Test UniMod label types
            var node = MATCHER.GetModifiedNode(STR_UNIMOD_LABEL);

            Assert.IsNotNull(node);
            Assert.IsNull(node.ExplicitMods.StaticModifications);
            Assert.IsTrue(node.ExplicitMods.HeavyModifications.Contains(mod =>
                                                                        Equals(mod.Modification, N_TERM_LABEL)));
            UpdateMatcherWithNoSequences(new[] { carbC }, new[] { N_TERM_LABEL }, new[] { METHIONINE_OXIDATION, MET_OX_ROUNDED }, null);
            var nodeNew = MATCHER.GetModifiedNode(STR_UNIMOD_LABEL);

            Assert.IsNotNull(nodeNew);
            Assert.IsTrue(nodeNew.TransitionGroups.Any(group => Equals(group.TransitionGroup.LabelType, IsotopeLabelType.heavy)));
            UpdateMatcher(new[] { carbC }, null, new[] { METHIONINE_OXIDATION, MET_OX_ROUNDED }, null);

            // Test case where there are lots of unimod labels
            var nodeUniAll = MATCHER.GetModifiedNode(STR_UNIMOD_ALL);

            Assert.AreEqual(nodeUniAll.ExplicitMods.HeavyModifications.Count, 10);
            Assert.IsNull(nodeUniAll.ExplicitMods.StaticModifications);
            foreach (var mod in nodeUniAll.ExplicitMods.HeavyModifications)
            {
                Assert.AreEqual(mod.Modification.ShortName, "+01");
                Assert.AreEqual(mod.Modification.UnimodId, 994);
            }

            // Test unimod terminal label
            var nodeUniTerm = MATCHER.GetModifiedNode(STR_UNIMOD_TERMINUS);

            Assert.AreEqual(nodeUniTerm.ExplicitMods.HeavyModifications.Count, 1);
            Assert.IsNull(nodeUniTerm.ExplicitMods.StaticModifications);
            Assert.AreEqual(nodeUniTerm.ExplicitMods.HeavyModifications[0].Modification.Terminus, ModTerminus.C);
            Assert.AreEqual(nodeUniTerm.ExplicitMods.HeavyModifications[0].Modification.UnimodId, 298);

            // Basic multi-label test
            var heavyLabelType2 = new IsotopeLabelType("Heavy2", 1);
            var typedMod        = new TypedModifications(heavyLabelType2, new List <StaticMod> {
                LABEL15_N
            });
            var peptideMods = new PeptideModifications(new List <StaticMod>(), new List <TypedModifications> {
                typedMod
            });
            var settingsMultiLabel = SrmSettingsList.GetDefault().ChangePeptideModifications(mods => peptideMods);
            var defSetSetLight     = new MappedList <string, StaticMod>();

            defSetSetLight.AddRange(StaticModList.GetDefaultsOn());
            var defSetHeavy = new MappedList <string, StaticMod>();

            defSetHeavy.AddRange(HeavyModList.GetDefaultsOn());
            defSetHeavy.Add(LABEL15_N);
            MATCHER.CreateMatches(settingsMultiLabel, new List <string> {
                STR_HEAVY_15_F
            }, defSetSetLight, defSetHeavy);
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_HEAVY_15_F).ExplicitMods.GetHeavyModifications().Contains(mod => Equals(mod.LabelType, heavyLabelType2)));
            // Peptide settings should not change.
            var docNew0 = new SrmDocument(settingsMultiLabel).AddPeptideGroups(new[] { new PeptideGroupDocNode(new PeptideGroup(),
                                                                                                               "PepGroup1", "", new[] { MATCHER.GetModifiedNode(STR_HEAVY_15_F) }) }, true, null, out firstAdded, out nextAdded);
            var settingsNew = MATCHER.GetDocModifications(docNew0);

            Assert.AreEqual(settingsMultiLabel.PeptideSettings.Modifications.ChangeHasHeavyModifications(false),
                            settingsNew.ChangeHasHeavyModifications(false));

            // Finding specific modifications.
            // If only specific AA modified, try for most specific modification.
            UpdateMatcher(null, null, null, null, new[] { STR_HEAVY_15_F });
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_HEAVY_15_F)
                          .ExplicitMods.HeavyModifications.Contains(mod =>
                                                                    mod.Modification.AminoAcids.Contains(c => c == 'F')));
            // If only some AAs modified, try for most specific modifications.
            UpdateMatcher(null, null, null, null, new[] { STR_HEAVY_15_NOT_ALL });
            Assert.IsTrue(MATCHER.GetModifiedNode(STR_HEAVY_15_NOT_ALL)
                          .ExplicitMods.HeavyModifications.Contains(mod =>
                                                                    mod.Modification.AminoAcids.Contains(c => c == 'I')));


            using (var testDir = new TestFilesDir(TestContext, ZIP_FILE))
                using (var modMatchDocContainer = InitMatchDocContainer(testDir))
                {
                    var libkeyModMatcher = new LibKeyModificationMatcher();
                    var anlLibSpec       = new BiblioSpecLiteSpec("ANL_Combo", testDir.GetTestPath("ANL_Combined.blib"));
                    var yeastLibSpec     = new BiblioSpecLiteSpec("Yeast", testDir.GetTestPath("Yeast_atlas_small.blib"));
                    modMatchDocContainer.ChangeLibSpecs(new[] { anlLibSpec, yeastLibSpec });
                    var docLibraries  = modMatchDocContainer.Document.Settings.PeptideSettings.Libraries.Libraries;
                    int anlLibIndex   = docLibraries.IndexOf(library => Equals(library.Name, anlLibSpec.Name));
                    int yeastLibIndex = docLibraries.IndexOf(library => Equals(library.Name, yeastLibSpec.Name));

                    libkeyModMatcher.CreateMatches(modMatchDocContainer.Document.Settings,
                                                   docLibraries[anlLibIndex].Keys, defSetSetLight, defSetHeavy);

                    // Test can match 15N
                    Assert.IsTrue(libkeyModMatcher.Matches.Values.Contains(match =>
                                                                           match.HeavyMod != null && match.HeavyMod.Equivalent(LABEL15_N)));

                    var uniModMetOx = UniMod.GetModification("Oxidation (M)", true);

                    // Test can match Met Ox
                    Assert.IsTrue(libkeyModMatcher.Matches.Values.Contains(match =>
                                                                           match.StructuralMod != null && match.StructuralMod.Equivalent(uniModMetOx)));

                    // Test can match 15N and Met ox!
                    Assert.IsTrue(libkeyModMatcher.Matches.Contains(match => match.Key.Mass == 17 &&
                                                                    match.Value.StructuralMod != null && match.Value.StructuralMod.Equivalent(uniModMetOx) &&
                                                                    match.Value.HeavyMod != null && match.Value.HeavyMod.Equivalent(LABEL15_N)));

                    // Test can match Cysteine (Implicit) and Met Ox (variable)
                    libkeyModMatcher.CreateMatches(modMatchDocContainer.Document.Settings,
                                                   docLibraries[yeastLibIndex].Keys, defSetSetLight, defSetHeavy);
                    Assert.IsTrue(libkeyModMatcher.MatcherPepMods.StaticModifications.Contains(mod =>
                                                                                               mod.Formula.Equals(UniMod.GetModification(StaticModList.DEFAULT_NAME, true).Formula) && !mod.IsVariable));
                    Assert.IsTrue(libkeyModMatcher.MatcherPepMods.StaticModifications.Contains(mod =>
                                                                                               mod.Formula.Equals("O") && mod.IsVariable));
                }
        }