public ViewLibraryPepInfo AssociateMatchingPeptide(ViewLibraryPepInfo pepInfo, int charge, SrmSettingsDiff settingsDiff)
        {
            var key = pepInfo.Key;

            var settings = _chargeSettingsMap[charge];

            // Change current document settings to match the current library and change the charge filter to
            // match the current peptide.
            if (settings == null)
            {
                settings = _document.Settings;
                var rankId = settings.PeptideSettings.Libraries.RankId;
                if (rankId != null && !_selectedSpec.PeptideRankIds.Contains(rankId))
                {
                    settings = settings.ChangePeptideLibraries(lib => lib.ChangeRankId(null));
                }

                settings = settings.ChangePeptideLibraries(
                    lib => lib.ChangeLibraries(new[] { _selectedSpec }, new[] { _selectedLibrary })
                    .ChangePick(PeptidePick.library))
                           .ChangeTransitionFilter(
                    filter => filter.ChangePrecursorCharges(new[] { charge }).ChangeAutoSelect(true))
                           .ChangeMeasuredResults(null);

                _chargeSettingsMap[charge] = settings;
            }

            var nodePep = _matcher.GetModifiedNode(key, pepInfo.GetAASequence(_lookupPool), settings, settingsDiff);

            if (nodePep != null)
            {
                pepInfo.PeptideNode = nodePep;
            }
            return(pepInfo);
        }
        public PeptideDocNode MatchSinglePeptide(ViewLibraryPepInfo pepInfo)
        {
            _chargeSettingsMap = new SrmSettings[128];
            var nodePep = AssociateMatchingPeptide(pepInfo, pepInfo.Key.Charge).PeptideNode;

            if (nodePep == null)
            {
                return(null);
            }

            IList <ProteinInfo> matchedProteins = null;

            // This is only set if the user has checked the associate peptide box.
            var sequence = nodePep.Peptide.Sequence;

            if (_backgroundProteome != null)
            {
                using (var proteomeDb = _backgroundProteome.OpenProteomeDb())
                {
                    var digestion = _backgroundProteome.GetDigestion(proteomeDb, Settings.PeptideSettings);
                    if (digestion != null)
                    {
                        matchedProteins = digestion.GetProteinsWithSequence(sequence).Select(protein => new ProteinInfo(protein)).ToArray();
                    }
                }
            }

            PeptideMatches = new Dictionary <PeptideSequenceModKey, PeptideMatch>
            {
                { nodePep.SequenceKey, new PeptideMatch(nodePep, matchedProteins,
                                                        MatchesFilter(sequence, pepInfo.Key.Charge)) }
            };
            return(nodePep);
        }
示例#3
0
        public ViewLibraryPepInfo AssociateMatchingPeptide(ViewLibraryPepInfo pepInfo, Adduct charge, SrmSettingsDiff settingsDiff)
        {
            var key = pepInfo.Key;

            if (key.IsPrecursorKey)
            {
                return(pepInfo);
            }

            var settings = _chargeSettingsMap[charge];

            // Change current document settings to match the current library and change the charge filter to
            // match the current peptide.
            if (settings == null)
            {
                settings = _document.Settings;
                var rankId = settings.PeptideSettings.Libraries.RankId;
                if (rankId != null && !_selectedSpec.PeptideRankIds.Contains(rankId))
                {
                    settings = settings.ChangePeptideLibraries(lib => lib.ChangeRankId(null));
                }
                var isProteomic = pepInfo.Target.IsProteomic;
                settings = settings.ChangePeptideLibraries(
                    lib => lib.ChangeLibraries(new[] { _selectedSpec }, new[] { _selectedLibrary })
                    .ChangePick(PeptidePick.library))
                           .ChangeTransitionFilter(
                    filter => (isProteomic ?
                               filter.ChangePeptidePrecursorCharges(new[] { charge }) :
                               filter.ChangeSmallMoleculePrecursorAdducts(new[] { charge }))
                    .ChangeAutoSelect(true))
                           .ChangeMeasuredResults(null);
                var staticMods = settings.PeptideSettings.Modifications.StaticModifications.ToList();
                foreach (var crosslinkMod in Properties.Settings.Default.StaticModList.Where(mod =>
                                                                                             null != mod.CrosslinkerSettings))
                {
                    if (!staticMods.Any(mod => mod.Name == crosslinkMod.Name))
                    {
                        staticMods.Add(crosslinkMod);
                    }
                }

                settings = settings.ChangePeptideSettings(
                    settings.PeptideSettings.ChangeModifications(
                        settings.PeptideSettings.Modifications.ChangeStaticModifications(staticMods)));


                _chargeSettingsMap[charge] = settings;
            }

            var nodePep = _matcher.GetModifiedNode(key, settings, settingsDiff);

            if (nodePep != null)
            {
                pepInfo = pepInfo.ChangePeptideNode(nodePep);
            }
            return(pepInfo);
        }
 public ViewLibraryPepMatching(SrmDocument document,
     Library library,
     LibrarySpec spec,
     byte[] lookupPool,
     LibKeyModificationMatcher matcher,
     ViewLibraryPepInfo[] peptides)
 {
     _document = document;
     _selectedLibrary = library;
     _selectedSpec = spec;
     _lookupPool = lookupPool;
     _matcher = matcher;
     _libraryPepInfos = peptides;
     _chargeSettingsMap = new SrmSettings[128];
 }
示例#5
0
        public ViewLibraryPepInfo AssociateMatchingPeptide(ViewLibraryPepInfo pepInfo, Adduct charge, SrmSettingsDiff settingsDiff)
        {
            var key = pepInfo.Key;

            if (key.IsPrecursorKey)
            {
                return(pepInfo);
            }

            var settings = _chargeSettingsMap[charge];

            // Change current document settings to match the current library and change the charge filter to
            // match the current peptide.
            if (settings == null)
            {
                settings = _document.Settings;
                var rankId = settings.PeptideSettings.Libraries.RankId;
                if (rankId != null && !_selectedSpec.PeptideRankIds.Contains(rankId))
                {
                    settings = settings.ChangePeptideLibraries(lib => lib.ChangeRankId(null));
                }
                var isProteomic = pepInfo.Target.IsProteomic;
                settings = settings.ChangePeptideLibraries(
                    lib => lib.ChangeLibraries(new[] { _selectedSpec }, new[] { _selectedLibrary })
                    .ChangePick(PeptidePick.library))
                           .ChangeTransitionFilter(
                    filter => (isProteomic ?
                               filter.ChangePeptidePrecursorCharges(new[] { charge }) :
                               filter.ChangeSmallMoleculePrecursorAdducts(new[] { charge }))
                    .ChangeAutoSelect(true))
                           .ChangeMeasuredResults(null);

                _chargeSettingsMap[charge] = settings;
            }

            var nodePep = _matcher.GetModifiedNode(key, settings, settingsDiff);

            if (nodePep != null)
            {
                pepInfo = pepInfo.ChangePeptideNode(nodePep);
            }
            return(pepInfo);
        }
示例#6
0
        public static int Compare(ViewLibraryPepInfo p1, ViewLibraryPepInfo p2, IList <byte> lookupPool)
        {
            // If they point to the same look-up index, then they are equal.
            if (p1.IndexLookup == p2.IndexLookup)
            {
                return(0);
            }

            int lenP1 = p1.SequenceLength;
            int lenP2 = p2.SequenceLength;
            // If sequences are equal length compare charge and mod count also
            int lenCompare = lenP1 == lenP2 ? p1.LengthLookup : Math.Min(lenP1, lenP2);

            // Compare bytes in the lookup pool
            for (int i = 0; i < lenCompare; i++)
            {
                byte b1 = lookupPool[p1.IndexLookup + i];
                byte b2 = lookupPool[p2.IndexLookup + i];
                // If unequal bytes are found, compare the bytes
                if (b1 != b2)
                {
                    return(b1 - b2);
                }
            }

            // If sequence length is not equal, the shorter should be first.
            if (lenP1 != lenP2)
            {
                return(lenP1 - lenP2);
            }

            // p1 and p2 have the same unmodified sequence, same number
            // of charges, and same number of modifications. Just
            // compare their display strings directly in this case.
            return(Comparer.Default.Compare(p1.DisplayString, p2.DisplayString));
        }
示例#7
0
        public static int Compare(ViewLibraryPepInfo p1, ViewLibraryPepInfo p2, IList<byte> lookupPool)
        {
            // If they point to the same look-up index, then they are equal.
            if (p1.IndexLookup == p2.IndexLookup)
                return 0;

            int lenP1 = p1.SequenceLength;
            int lenP2 = p2.SequenceLength;
            // If sequences are equal length compare charge and mod count also
            int lenCompare = lenP1 == lenP2 ? p1.LengthLookup : Math.Min(lenP1, lenP2);
            // Compare bytes in the lookup pool
            for (int i = 0; i < lenCompare; i++)
            {
                byte b1 = lookupPool[p1.IndexLookup + i];
                byte b2 = lookupPool[p2.IndexLookup + i];
                // If unequal bytes are found, compare the bytes
                if (b1 != b2)
                    return b1 - b2;
            }

            // If sequence length is not equal, the shorter should be first.
            if (lenP1 != lenP2)
                return lenP1 - lenP2;

            // p1 and p2 have the same unmodified sequence, same number
            // of charges, and same number of modifications. Just
            // compare their display strings directly in this case.
            return Comparer.Default.Compare(p1.DisplayString, p2.DisplayString);
        }
示例#8
0
 public ViewLibraryPepInfo AssociateMatchingPeptide(ViewLibraryPepInfo pepInfo, Adduct charge)
 {
     return(AssociateMatchingPeptide(pepInfo, charge, null));
 }
        private void TestBasicFunctionality()
        {
            // Launch the Library Explorer dialog
            _viewLibUI = ShowDialog<ViewLibraryDlg>(SkylineWindow.ViewSpectralLibraries);

            // Ensure the appropriate default library is selected
            ComboBox libComboBox = null;
            ListBox pepList = null;
            string libSelected = null;
            RunUI(() =>
            {
                libComboBox = (ComboBox)_viewLibUI.Controls.Find("comboLibrary", true)[0];
                Assert.IsNotNull(libComboBox);
                libSelected = libComboBox.SelectedItem.ToString();

                // Find the peptides list control
                pepList = (ListBox)_viewLibUI.Controls.Find("listPeptide", true)[0];
                Assert.IsNotNull(pepList);
            });
            Assert.AreEqual(_testLibs[0].Name, libSelected);

            // Initially, peptide with index 0 should be selected
            WaitForConditionUI(() => pepList.SelectedIndex != -1);
            var modDlg = WaitForOpenForm<MultiButtonMsgDlg>();
            RunUI(modDlg.Btn1Click);

            ViewLibraryPepInfo previousPeptide = new ViewLibraryPepInfo();
            int peptideIndex = -1;
            RunUI(() =>
            {
                previousPeptide = (ViewLibraryPepInfo)pepList.SelectedItem;
                peptideIndex = pepList.SelectedIndex;
            });
            Assert.IsNotNull(previousPeptide);
            Assert.AreEqual(0, peptideIndex);

            // Now try to select a different peptide and check to see if the
            // selection changes
            const int selectPeptideIndex = 1;
            RunUI(() =>
            {
                pepList.SelectedIndex = selectPeptideIndex;
            });

            ViewLibraryPepInfo selPeptide = new ViewLibraryPepInfo();
            RunUI(() =>
            {
                selPeptide = (ViewLibraryPepInfo)pepList.SelectedItem;
            });
            Assert.IsNotNull(selPeptide);
            if (Equals(previousPeptide, selPeptide))
                Assert.AreNotEqual(previousPeptide, selPeptide);

            // Click the "Next" link
            RunUI(() =>
                      {
                          var nextLink = (IButtonControl)_viewLibUI.Controls.Find("NextLink", true)[0];
                          Assert.IsNotNull(nextLink);

                          nextLink.PerformClick();
                      });
            RunUI(() =>
            {
                previousPeptide = (ViewLibraryPepInfo)pepList.SelectedItem;
            });

            // Click "Previous" link and ensure the peptide selected changes
            RunUI(() =>
                      {
                          var previousLink = (IButtonControl) _viewLibUI.Controls.Find("PreviousLink", true)[0];
                          Assert.IsNotNull(previousLink);

                          previousLink.PerformClick();
                      });
            RunUI(() =>
            {
                selPeptide = (ViewLibraryPepInfo)pepList.SelectedItem;
            });
            Assert.AreNotEqual(previousPeptide, selPeptide);

            // Test valid peptide search
            TextBox pepTextBox = null;
            RunUI(() =>
            {
                pepTextBox = (TextBox)_viewLibUI.Controls.Find("textPeptide", true)[0];
                Assert.IsNotNull(pepTextBox);

                pepTextBox.Focus();
                pepTextBox.Text = _testLibs[0].UniquePeptide;
            });
            int pepsCount = 0;
            RunUI(() =>
            {
                selPeptide = (ViewLibraryPepInfo)pepList.SelectedItem;
                pepsCount = pepList.Items.Count;
            });
            Assert.AreEqual(_testLibs[0].UniquePeptide, selPeptide.DisplayString);
            Assert.AreEqual(1, pepsCount);

            // Test invalid peptide search
            RunUI(() =>
            {
                pepTextBox.Focus();
                pepTextBox.Text = INVALID_SEARCH;
            });
            RunUI(() =>
            {
                pepsCount = pepList.Items.Count;
            });
            Assert.AreEqual(0, pepsCount);

            // Test clearing invalid peptide search
            RunUI(() =>
            {
                pepTextBox.Focus();
                pepTextBox.Text = "";
            });
            selPeptide = new ViewLibraryPepInfo();
            RunUI(() =>
            {
                selPeptide = (ViewLibraryPepInfo)pepList.SelectedItem;
                pepsCount = pepList.Items.Count;
            });
            Assert.IsNotNull(selPeptide);
            Assert.AreNotEqual(0, pepsCount);

            // Test selecting a different library
            previousPeptide = selPeptide;
            RunDlg<MultiButtonMsgDlg>(() => libComboBox.SelectedIndex = 1, dlg => dlg.Btn1Click());
            RunUI(() =>
            {
                libComboBox.SelectedIndex = 1;
            });
            RunUI(() =>
            {
                libSelected = libComboBox.SelectedItem.ToString();
            });
            Assert.AreEqual(libSelected, _testLibs[1].Name);
            RunUI(() =>
            {
                selPeptide = (ViewLibraryPepInfo)pepList.SelectedItem;
            });
            Assert.IsNotNull(selPeptide);
            Assert.AreNotEqual(previousPeptide, selPeptide);

            // If the library is not in the document settings, offer to add the library to the settings.
            // If the user declines, add the peptides anyways, but strip them so they do not appear to be
            // connected to any library.
            RunDlg<MultiButtonMsgDlg>(_viewLibUI.AddPeptide, msgDlg => msgDlg.Btn1Click());
            Assert.AreEqual(1, SkylineWindow.Document.PeptideCount);
            Assert.IsFalse(SkylineWindow.Document.Peptides.Contains(nodePep => nodePep.HasLibInfo));
            RunUI(() =>
                      {
                          SkylineWindow.SelectAll();
                          SkylineWindow.EditDelete();
                      });

            // Test adding peptides offers to add library if not already in settings.
            // Test add single peptide.
            using (new CheckDocumentStateWithPossibleProteinMetadataBackgroundUpdate(1, 1, 1, 3))
            {
                RunDlg<MultiButtonMsgDlg>(_viewLibUI.AddPeptide, msgDlg => msgDlg.Btn0Click());
            }
            RunUI(SkylineWindow.EditDelete);

            // Test unmatched peptides are correct.
            // One unmatched because its precursor m/z is outside the instrument measurement range
            AddAllPeptides(1, 4, 3);

            // Verify that everything matches, given a wide enough mass range
            RunUI(() => SkylineWindow.ModifyDocument("Change m/z range",
                    doc => doc.ChangeSettings(doc.Settings.ChangeTransitionInstrument(inst =>
                        inst.ChangeMaxMz(TransitionInstrument.MAX_MEASURABLE_MZ)))));
            WaitForConditionUI(() => !_viewLibUI.HasUnmatchedPeptides);
            RunUI(() => SkylineWindow.ModifyDocument("Change m/z range",
                    doc => doc.ChangeSettings(doc.Settings.ChangeTransitionInstrument(inst =>
                        inst.ChangeMaxMz(1500)))));
            WaitForConditionUI(() => _viewLibUI.HasUnmatchedPeptides);

            // Test library peptides are merged without duplicates.
            TestForDuplicatePeptides();

            // Test library peptides only get added to the document once.
            var docOriginal = SkylineWindow.Document;
            RunDlg<MessageDlg>(_viewLibUI.AddPeptide, msgDlg => msgDlg.OkDialog());
            var filterMatchedPeptidesDlg5 = ShowDialog<FilterMatchedPeptidesDlg>(_viewLibUI.AddAllPeptides);
            RunDlg<MessageDlg>(filterMatchedPeptidesDlg5.OkDialog, msgDlg => msgDlg.OkDialog());
            Assert.AreSame(docOriginal, SkylineWindow.Document);

            // Test missing peptides added.
            RunUI(() =>
            {
                var sequenceTree = SkylineWindow.SequenceTree;
                var nodePeps = sequenceTree.Nodes[0].Nodes;
                sequenceTree.SelectedNode = nodePeps[0];
                sequenceTree.KeysOverride = Keys.Control;
                for (int i = 2; i < 10; i += 2)
                {
                    sequenceTree.SelectedNode = nodePeps[i];
                }
                sequenceTree.KeysOverride = Keys.None;
                SkylineWindow.EditDelete();
            });
            AddAllPeptides();
            var docAddBack = SkylineWindow.Document;
            // Peptides will be added back in a different order
            AssertEx.IsDocumentState(docAddBack, null,
                docOriginal.PeptideGroupCount, docOriginal.PeptideCount,
                docOriginal.PeptideTransitionGroupCount, docOriginal.PeptideTransitionCount);
            TestSamePeptides(docOriginal.Peptides);

            // Test missing transition groups added correctly.
            RunUI(() =>
            {
                var sequenceTree = SkylineWindow.SequenceTree;
                sequenceTree.SelectedNode = sequenceTree.Nodes[0].Nodes[0].Nodes[0];
                sequenceTree.KeysOverride = Keys.Control;
                for (int x = 0; x < 5; x++)
                {
                    var nodePep = sequenceTree.Nodes[0].Nodes[x];
                    var nodeGroups = nodePep.Nodes;
                    for (int i = 0; i < nodeGroups.Count; i += 2)
                    {
                        sequenceTree.SelectedNode = nodeGroups[i];
                    }
                }
                SkylineWindow.EditDelete();
            });
            AddAllPeptides();
            var docAddBackGroups = SkylineWindow.Document;
            // Check all precursor charges present.
            foreach (PeptideDocNode nodePep in docOriginal.Peptides)
            {
                var key = nodePep.Key;
                foreach (TransitionGroupDocNode nodeGroup in nodePep.Children)
                {
                    var charge = nodeGroup.TransitionGroup.PrecursorCharge;
                    Assert.IsTrue(docAddBackGroups.Peptides.Contains(nodePepDoc => Equals(key, nodePepDoc.Key)
                        && nodePepDoc.HasChildCharge(charge)));
                }
            }
            // Check no duplicate TransitionGroups added.
            TestForDuplicateTransitionGroups();

            // Test peptides get heavy label modifications.
            List<StaticMod> heavyMods15N =
                new List<StaticMod> { new StaticMod("15N", null, null, null, LabelAtoms.N15, null, null) };
            RunUI(() =>
            {
                SkylineWindow.SelectAll();
                SkylineWindow.EditDelete();
                var settings = SkylineWindow.Document.Settings.ChangePeptideModifications(mods => new PeptideModifications(mods.StaticModifications,
                   new[] { new TypedModifications(IsotopeLabelType.heavy, heavyMods15N) }));
                SkylineWindow.ModifyDocument("Change heavy modifications", doc => doc.ChangeSettings(settings));
            });
            AddAllPeptides();

            // All peptides should have a heavy label transition group.
            // There should be no peptide whose children do not contain a transition group with heavy label type.
            Assert.IsFalse(SkylineWindow.Document.Peptides.Contains(nodePep =>
                !nodePep.Children.Contains(nodeGroup =>
                    ((TransitionGroupDocNode)nodeGroup).TransitionGroup.LabelType.Equals(IsotopeLabelType.heavy))));

            // Test peptide setting changes update the library explorer.
            RunUI(() => SkylineWindow.ModifyDocument("Change static modifications",
                doc => doc.ChangeSettings(SkylineWindow.Document.Settings.ChangePeptideModifications(mods =>
                mods.ChangeStaticModifications(new List<StaticMod>())))));
            AddAllPeptides(1, 8, 3);

            // Along with Heavy 15N added earlier in the test, adding this modification means that all library peptides
            // will match to the document settings.
            StaticMod varMetOxidized = new StaticMod("Methionine Oxidized", "M", null, true, "O",
                LabelAtoms.None, RelativeRT.Matching, null, null, null);
            var metOxidizedSettings = SkylineWindow.Document.Settings.ChangePeptideModifications(mods =>
                mods.ChangeStaticModifications(new[] { varMetOxidized }));

            RunUI(() =>
                      {
                          SkylineWindow.SelectAll();
                          SkylineWindow.EditDelete();
                          SkylineWindow.ModifyDocument("Change static mods",
                                                       doc => doc.ChangeSettings(metOxidizedSettings));
                      });

            // Switch to ANL_Combined library
            RunDlg<MultiButtonMsgDlg>(() => { libComboBox.SelectedIndex = 2; }, msgDlg => msgDlg.BtnCancelClick());

            // User prompted to add library since not in current settings.
            RunDlg<MultiButtonMsgDlg>(() => _viewLibUI.CheckLibraryInSettings(), msgDlg => msgDlg.Btn0Click());
            // Add single peptide to the document.
            RunUI(_viewLibUI.AddPeptide);
            WaitForProteinMetadataBackgroundLoaderCompletedUI();
            var nodePepAdded = SkylineWindow.SequenceTree.Nodes[0].Nodes[0];
            // Because document settings match the library, no duplicates should be found.
            AddAllPeptides(0);

            // Even though there are two matches in the library for the the nodePep we just added
            // to the document (one with light modifications and one with heavy), the two spectrum
            // both have the same charge. In this case, both spectrum should be ignored when Add All
            // is called.
            Assert.AreEqual(nodePepAdded, SkylineWindow.SequenceTree.Nodes[0].Nodes[0]);
            Assert.AreEqual(3, SkylineWindow.Document.PeptideCount);

            // Switch to the Phospho Loss Library
            RunDlg<MultiButtonMsgDlg>(() => libComboBox.SelectedIndex = 3, msgDlg => msgDlg.Btn1Click());

            // Add modifications to the document matching the settings of the library.
            var phosphoLossMod = new StaticMod("Phospho Loss", "S, T", null, true, "HPO3",
                                  LabelAtoms.None, RelativeRT.Matching, null, null, new[] { new FragmentLoss("H3PO4"), });
            var phosphoLossSettings =
                SkylineWindow.Document.Settings.ChangePeptideModifications(mods => mods.ChangeStaticModifications(new[] { phosphoLossMod }));
            RunUI(() =>
                SkylineWindow.ModifyDocument("Change static mods", doc => doc.ChangeSettings(phosphoLossSettings)));
            RunDlg<MultiButtonMsgDlg>(() => _viewLibUI.CheckLibraryInSettings(), msgDlg => msgDlg.Btn0Click());

            // Again, we should be able to match all peptides since the document settings match use the peptides found
            // in the library.
            RunDlg<MultiButtonMsgDlg>(_viewLibUI.AddAllPeptides, msgDlg =>
             {
                 Assert.AreEqual(0, (int)msgDlg.Tag);
                 msgDlg.Btn1Click();
             });
            // Test losses are being displayed in the graph, indicating that the spectrum have been matched correctly.
            WaitForConditionUI(() => _viewLibUI.GraphItem.IonLabels.Contains(str => str.Contains("98")));
            Assert.IsTrue(_viewLibUI.GraphItem.IonLabels.Contains(str => str.Contains("98")));

            // Associate yeast background proteome
            var peptideSettingsUI = ShowDialog<PeptideSettingsUI>(SkylineWindow.ShowPeptideSettingsUI);
            RunDlg<BuildBackgroundProteomeDlg>(peptideSettingsUI.ShowBuildBackgroundProteomeDlg,
                buildBackgroundProteomeDlg =>
            {
                buildBackgroundProteomeDlg.BackgroundProteomePath = TestFilesDir.GetTestPath("yeast_mini.protdb");
                buildBackgroundProteomeDlg.BackgroundProteomeName = "Yeast";
                buildBackgroundProteomeDlg.OkDialog();
            });
            RunUI(peptideSettingsUI.OkDialog);
            WaitForCondition(() =>
            {
                var peptideSettings = Program.ActiveDocument.Settings.PeptideSettings;
                var backgroundProteome = peptideSettings.BackgroundProteome;
                return backgroundProteome.HasDigestion(peptideSettings);
            });
            WaitForDocumentLoaded(); // Give background loader a chance to get the protein metadata too

            RunDlg<TransitionSettingsUI>(SkylineWindow.ShowTransitionSettingsUI, transitionSettingsUI =>
            {
                transitionSettingsUI.PrecursorCharges = "1,2,3";
                transitionSettingsUI.OkDialog();
            });

            // Test add all with the yeast background proteome connected.
            RunUI(() =>
            {
                SkylineWindow.SelectAll();
                SkylineWindow.EditDelete();
                // Switch to yeast library.
                libComboBox.SelectedIndex = 4;
                _viewLibUI.AssociateMatchingProteins = true;
            });
            var addLibraryDlg = ShowDialog<MultiButtonMsgDlg>(_viewLibUI.AddAllPeptides);
            // Add the library to the document.
            var filterMatchedPeptidesDlg = ShowDialog<FilterMatchedPeptidesDlg>(addLibraryDlg.Btn0Click);
            RunUI(() => filterMatchedPeptidesDlg.AddUnmatched = false);
            using (new CheckDocumentStateWithPossibleProteinMetadataBackgroundUpdate(4, 10, 13, 39))
            {
                RunDlg<MultiButtonMsgDlg>(filterMatchedPeptidesDlg.OkDialog, messageDlg => messageDlg.Btn1Click());
            }
            Assert.IsFalse(SkylineWindow.Document.Peptides.Contains(nodePep => !nodePep.HasLibInfo));

            // Test adding a single peptide that matches two different proteins using keep all.
            RunUI(() =>
                      {
                          SkylineWindow.Undo();
                          _viewLibUI.ChangeSelectedPeptide("ADTGIAVEGATDAAR+");
                      });
            using (new CheckDocumentStateWithPossibleProteinMetadataBackgroundUpdate(2, 2, 2, 6))
            {
                RunDlg<FilterMatchedPeptidesDlg>(_viewLibUI.AddPeptide,
                    filterMatchedPeptideDlg => filterMatchedPeptideDlg.OkDialog());
            }

            // Test adding a second charge state for that peptide.
            RunUI(() => _viewLibUI.ChangeSelectedPeptide("ADTGIAVEGATDAAR++"));
            using (new CheckDocumentStateWithPossibleProteinMetadataBackgroundUpdate(2, 2, 4, 12))
            {
                RunDlg<FilterMatchedPeptidesDlg>(_viewLibUI.AddPeptide,
                    filterMatchedPeptideDlg => filterMatchedPeptideDlg.OkDialog());
            }
            RunDlg<FilterMatchedPeptidesDlg>(_viewLibUI.AddPeptide,
                      filterMatchedPeptideDlg => filterMatchedPeptideDlg.OkDialog());

            // Test adding a second charge state - No Duplicates.
            RunUI(() => SkylineWindow.Undo());
            var docPrev = WaitForDocumentLoaded();
            RunDlg<FilterMatchedPeptidesDlg>(_viewLibUI.AddPeptide,
                      filterMatchedPeptideDlg =>
            {
                filterMatchedPeptideDlg.DuplicateProteinsFilter = BackgroundProteome.DuplicateProteinsFilter.NoDuplicates;
                filterMatchedPeptideDlg.OkDialog();
            });
            Assert.AreEqual(docPrev, WaitForDocumentLoaded());

            // Test adding a second charge state - First Only.
            using (new CheckDocumentStateWithPossibleProteinMetadataBackgroundUpdate(2, 2, 3, 9))
            {
                RunDlg<FilterMatchedPeptidesDlg>(_viewLibUI.AddPeptide, filterMatchedPeptideDlg =>
                {
                    filterMatchedPeptideDlg.DuplicateProteinsFilter =
                        BackgroundProteome.DuplicateProteinsFilter.FirstOccurence;
                    filterMatchedPeptideDlg.OkDialog();
                });
            }

            RunUI(() =>
            {
                SkylineWindow.Undo();
                SkylineWindow.Undo();
                // Add doubly charged state, no protein association.
                _viewLibUI.AssociateMatchingProteins = false;
                _viewLibUI.AddPeptide();
                _viewLibUI.AssociateMatchingProteins = true;
            });

            // Add doubly charged state with protein assocation.
            RunDlg<FilterMatchedPeptidesDlg>(_viewLibUI.AddPeptide,
                filterMatchedPeptideDlg => filterMatchedPeptideDlg.OkDialog());  // First only
            // Select singly charged state
            RunUI(() => _viewLibUI.ChangeSelectedPeptide("ADTGIAVEGATDAAR+"));
            // Test adding peptide associated with the background proteome does not affect any matching peptides that are
            // in peptide lists.
            using (new CheckDocumentStateWithPossibleProteinMetadataBackgroundUpdate(2, 2, 3, 9))
            {
                RunDlg<FilterMatchedPeptidesDlg>(_viewLibUI.AddPeptide,
                    filterMatchedPeptideDlg => filterMatchedPeptideDlg.OkDialog()); // First only
            }
            var peptideGroups = SkylineWindow.Document.PeptideGroups.ToArray();
            int index = peptideGroups.IndexOf(nodeGroup => nodeGroup.IsPeptideList);
            Assert.IsTrue(peptideGroups[index].Children.Count == 1);

            // Test selecting no duplicates prevents any peptide from appearing twice in the document.
            RunUI(() => SkylineWindow.Undo());
            RunUI(() => SkylineWindow.Undo());
            var filterMatchedPeptidesDlg1 = ShowDialog<FilterMatchedPeptidesDlg>(_viewLibUI.AddAllPeptides);
            RunUI(() => filterMatchedPeptidesDlg1.DuplicateProteinsFilter = BackgroundProteome.DuplicateProteinsFilter.NoDuplicates);
            RunDlg<MultiButtonMsgDlg>(filterMatchedPeptidesDlg1.OkDialog, messageDlg => messageDlg.Btn1Click());
            TestForDuplicatePeptides();

            // Test selecting first occurence prevents any peptide from appearing twice in the document.
            RunUI(() => SkylineWindow.Undo());
            var filterMatchedPeptidesDlg2 = ShowDialog<FilterMatchedPeptidesDlg>(_viewLibUI.AddAllPeptides);
            RunUI(() => filterMatchedPeptidesDlg2.DuplicateProteinsFilter = BackgroundProteome.DuplicateProteinsFilter.FirstOccurence);
            RunDlg<MultiButtonMsgDlg>(filterMatchedPeptidesDlg2.OkDialog, messageDlg => messageDlg.Btn1Click());
            TestForDuplicatePeptides();

            // Test peptides are added to "Library Peptides" peptide list if this peptide group already exists.
            using (new CheckDocumentStateWithPossibleProteinMetadataBackgroundUpdate(1, 2, 3, 9))
            {
                RunUI(() =>
                    {
                        SkylineWindow.Undo();
                        _viewLibUI.AssociateMatchingProteins = false;
                        _viewLibUI.AddPeptide();
                        _viewLibUI.ChangeSelectedPeptide("AAAP");
                        _viewLibUI.AddPeptide();
                    });
            }

            RunUI(() =>
            {
                SkylineWindow.SelectAll();
                SkylineWindow.EditDelete();
            });

            // Close the Library Explorer dialog
            OkDialog(_viewLibUI, _viewLibUI.CancelDialog);
        }
        public PeptideDocNode MatchSinglePeptide(ViewLibraryPepInfo pepInfo)
        {
            _chargeSettingsMap = new SrmSettings[128];
            var nodePep = AssociateMatchingPeptide(pepInfo, pepInfo.Key.Charge).PeptideNode;
            if (nodePep == null)
                return null;

            IList<ProteinInfo> matchedProteins = null;

            // This is only set if the user has checked the associate peptide box.
            var sequence = nodePep.Peptide.Sequence;
            if (_backgroundProteome != null)
            {
                using (var proteomeDb = _backgroundProteome.OpenProteomeDb())
                {
                    var digestion = _backgroundProteome.GetDigestion(proteomeDb, Settings.PeptideSettings);
                    if (digestion != null)
                    {
                        matchedProteins = digestion.GetProteinsWithSequence(sequence).Select(protein=>new ProteinInfo(protein)).ToArray();
                    }
                }
            }

            PeptideMatches = new Dictionary<PeptideSequenceModKey, PeptideMatch>
                                 {{nodePep.SequenceKey, new PeptideMatch(nodePep, matchedProteins,
                                     MatchesFilter(sequence, pepInfo.Key.Charge))}};
            return nodePep;
        }
        public ViewLibraryPepInfo AssociateMatchingPeptide(ViewLibraryPepInfo pepInfo, int charge, SrmSettingsDiff settingsDiff)
        {
            var key = pepInfo.Key;

            var settings = _chargeSettingsMap[charge];
            // Change current document settings to match the current library and change the charge filter to
            // match the current peptide.
            if (settings == null)
            {
                settings = _document.Settings;
                var rankId = settings.PeptideSettings.Libraries.RankId;
                if (rankId != null && !_selectedSpec.PeptideRankIds.Contains(rankId))
                    settings = settings.ChangePeptideLibraries(lib => lib.ChangeRankId(null));

                settings = settings.ChangePeptideLibraries(
                        lib => lib.ChangeLibraries(new[] { _selectedSpec }, new[] { _selectedLibrary })
                                  .ChangePick(PeptidePick.library))
                    .ChangeTransitionFilter(
                        filter => filter.ChangePrecursorCharges(new[] { charge }).ChangeAutoSelect(true))
                    .ChangeMeasuredResults(null);

                _chargeSettingsMap[charge] = settings;
            }

            var nodePep = _matcher.GetModifiedNode(key, pepInfo.GetAASequence(_lookupPool), settings, settingsDiff);
            if (nodePep != null)
            {
                pepInfo.PeptideNode = nodePep;
            }
            return pepInfo;
        }
 public ViewLibraryPepInfo AssociateMatchingPeptide(ViewLibraryPepInfo pepInfo, int charge)
 {
     return AssociateMatchingPeptide(pepInfo, charge, null);
 }