public static SrmDocument CreateNISTLibraryDocument(string textFasta, bool peptideList, string textLib,
            out LibraryManager libraryManager, out TestDocumentContainer docContainer, out int startRev)
        {
            var streamManager = new MemoryStreamManager();
            streamManager.TextFiles.Add(LibraryLoadTest.PATH_NIST_LIB, textLib);
            var librarySpec = new NistLibSpec("Yeast (NIST)", LibraryLoadTest.PATH_NIST_LIB);

            // For serialization, add the library spec to the settings
            TestLibraryList = new SpectralLibraryList { librarySpec };

            libraryManager = new LibraryManager { StreamManager = streamManager };
            docContainer = new TestDocumentContainer();

            SrmSettings settings = SrmSettingsList.GetDefault0_6();
            settings = settings.ChangePeptideLibraries(l => l.ChangeLibrarySpecs(new[] { librarySpec }));

            return CreateLibraryDocument(settings, textFasta, peptideList, docContainer, libraryManager, out startRev);
        }
        public void LibraryMultipleTest()
        {
            var streamManager = new MemoryStreamManager();
            var loader = new LibraryLoadTest.TestLibraryLoader { StreamManager = streamManager };

            // Create library files
            const string hunterText = LibraryLoadTest.TEXT_LIB_YEAST_NIST1 + "\n" + LibraryLoadTest.TEXT_LIB_YEAST_NIST2;
            LibraryLoadTest.CreateHunterFile(streamManager, loader, hunterText);
            const string biblioText = LibraryLoadTest.TEXT_LIB_YEAST_NIST2 + "\n" + LibraryLoadTest.TEXT_LIB_YEAST_NIST3;
            LibraryLoadTest.CreateBiblioFile(streamManager, loader, biblioText);
            const string nistText = LibraryLoadTest.TEXT_LIB_YEAST_NIST3 + "\n" + LibraryLoadTest.TEXT_LIB_YEAST_NIST4;
            streamManager.TextFiles.Add(LibraryLoadTest.PATH_NIST_LIB, nistText);

            var hunterSpec = new XHunterLibSpec("Yeast (X!)", LibraryLoadTest.PATH_HUNTER_LIB);
            var bilbioSpec = new BiblioSpecLibSpec("Yeast (BS)", LibraryLoadTest.PATH_BIBLIOSPEC_LIB);
            var nistSpec = new NistLibSpec("Yeast (NIST)", LibraryLoadTest.PATH_NIST_LIB);

            // For serialization, add the library spec to the settings
            TestLibraryList = new SpectralLibraryList { hunterSpec, bilbioSpec, nistSpec };

            var libraryManager = new LibraryManager { StreamManager = streamManager };
            var docContainer = new TestDocumentContainer();

            SrmSettings settings = SrmSettingsList.GetDefault();
            settings = settings.ChangePeptideLibraries(l => l.ChangeLibrarySpecs(new LibrarySpec[] { hunterSpec, bilbioSpec, nistSpec }));

            int startRev;
            SrmDocument docLoaded = CreateLibraryDocument(settings, ExampleText.TEXT_FASTA_YEAST_LIB, false,
                docContainer, libraryManager, out startRev);
            AssertEx.IsDocumentState(docLoaded, startRev, 2, 4, 12);
            Assert.IsTrue(HasLibraryInfo(docLoaded, typeof(XHunterSpectrumHeaderInfo)));
            Assert.IsTrue(HasLibraryInfo(docLoaded, typeof(BiblioSpecSpectrumHeaderInfo)));
            Assert.IsTrue(HasLibraryInfo(docLoaded, typeof(NistSpectrumHeaderInfo)));

            // Remove the rank 1 transition from each transition group
            TransitionDocNode[] transitionNodes = docLoaded.PeptideTransitions.ToArray();
            for (int i = 0; i < transitionNodes.Length; i++)
            {
                var nodeTran = transitionNodes[i];
                if (nodeTran.LibInfo.Rank != 1)
                    continue;
                var path = docLoaded.GetPathTo((int) SrmDocument.Level.TransitionGroups, i/3);
                docLoaded = (SrmDocument) docLoaded.RemoveChild(path, nodeTran);
                ++startRev;
            }
            AssertEx.IsDocumentState(docLoaded, startRev, 2, 4, 8);
            // Make sure this can be serialized and deserialized without causing
            // a recalculation of the nodes in the tree.
            AssertEx.Serializable(docLoaded, (doc1, doc2) => ValidateLibraryDocs(doc1, doc2, libraryManager));
        }
 private static void ValidateLibraryDocs(SrmDocument docTarget, SrmDocument docActual, LibraryManager libraryManager)
 {
     var docContainer = new TestDocumentContainer();
     libraryManager.Register(docContainer);
     try
     {
         AssertEx.IsDocumentState(docActual, 0, docTarget.PeptideGroupCount, docTarget.PeptideCount,
             docTarget.PeptideTransitionGroupCount, docTarget.PeptideTransitionCount);
         docActual = docActual.ChangeSettings(docActual.Settings.ConnectLibrarySpecs(FindLibrarySpec));
         Assert.IsTrue(docContainer.SetDocument(docActual, null, true));
         SrmDocument docLoaded = docContainer.Document;
         AssertEx.DocumentCloned(docTarget, docLoaded);
     //                Assert.IsTrue(ArrayUtil.ReferencesEqual(docActual.Transitions.ToArray(), docLoaded.Transitions.ToArray()));
     //                Assert.IsTrue(ArrayUtil.ReferencesEqual(docActual.TransitionGroups.ToArray(), docLoaded.TransitionGroups.ToArray()));
     //                Assert.IsTrue(ArrayUtil.ReferencesEqual(docActual.Peptides.ToArray(), docLoaded.Peptides.ToArray()));
         Assert.IsTrue(ArrayUtil.ReferencesEqual(docActual.Children, docLoaded.Children));
     }
     finally
     {
         libraryManager.Unregister(docContainer);
     }
 }
 private static SrmDocument CreateNISTLibraryDocument(out LibraryManager libraryManager,
     out TestDocumentContainer docContainer, out int startRev)
 {
     SrmDocument docLoaded = CreateNISTLibraryDocument(ExampleText.TEXT_FASTA_YEAST_LIB,
                                                       false,
                                                       LibraryLoadTest.TEXT_LIB_YEAST_NIST,
                                                       out libraryManager,
                                                       out docContainer,
                                                       out startRev);
     AssertEx.IsDocumentState(docLoaded, startRev, 2, 4, 12);
     return docLoaded;
 }
        private static SrmDocument CreateLibraryDocument(SrmSettings settings, string textFasta, bool peptideList,
            TestDocumentContainer docContainer, BackgroundLoader libraryManager, out int startRev)
        {
            startRev = 0;
            SrmDocument document = new SrmDocument(SrmSettingsList.GetDefault0_6());
            Assert.IsTrue(docContainer.SetDocument(document, null));

            // Register after first set document
            libraryManager.Register(docContainer);

            // Add libraries
            SrmDocument docLibraries = document.ChangeSettings(settings);
            ++startRev;

            // Add some FASTA
            IdentityPath path = IdentityPath.ROOT;
            SrmDocument docFasta = docLibraries.ImportFasta(new StringReader(textFasta), peptideList, path, out path);
            ++startRev;

            // Until libraries are loaded, only the sequences should appear
            if (!peptideList)
                AssertEx.IsDocumentState(docFasta, startRev, textFasta.Count(c => c == '>'), 0, 0);

            // Run the library load
            Assert.IsTrue(docContainer.SetDocument(docFasta, document, true));
            ++startRev;

            // After library load completes peptides and transitions should have expected library info
            SrmDocument docLoaded = docContainer.Document;

            // Check expected library inforamation
            foreach (var nodePeptide in docLoaded.Peptides)
                Assert.IsNull(nodePeptide.Rank);
            foreach (var nodeTran in docLoaded.PeptideTransitions)
            {
                Assert.IsTrue(nodeTran.HasLibInfo);
                Assert.IsTrue(nodeTran.LibInfo.Rank <= 3);
            }

            return docLoaded;
        }
        public void LibraryNoIonsTest()
        {
            var streamManager = new MemoryStreamManager();
            var loader = new LibraryLoadTest.TestLibraryLoader { StreamManager = streamManager };

            // Create X! Hunter library with 20 lowest intensity peaks in NIST library
            LibraryLoadTest.CreateHunterFile(streamManager, loader, LibraryLoadTest.TEXT_LIB_YEAST_NIST, true);

            var hunterSpec = new XHunterLibSpec("Yeast (X!)", LibraryLoadTest.PATH_HUNTER_LIB);

            // For serialization, add the library spec to the settings
            TestLibraryList = new SpectralLibraryList { hunterSpec };

            var libraryManager = new LibraryManager { StreamManager = streamManager };
            var docContainer = new TestDocumentContainer();

            SrmSettings settings = SrmSettingsList.GetDefault();
            settings = settings.ChangePeptideLibraries(l => l.ChangeLibrarySpecs(new LibrarySpec[] { hunterSpec }));

            int startRev;
            SrmDocument docLoaded = CreateLibraryDocument(settings, ExampleText.TEXT_FASTA_YEAST_LIB, false,
                docContainer, libraryManager, out startRev);
            // Peptides should have been chosen, but no transitions, since the spectra are garbage
            AssertEx.IsDocumentState(docLoaded, startRev, 2, 4, 0);
        }