/// <summary> /// Tries to match each library peptide to document settings. /// </summary> public void MatchAllPeptides(ILongWaitBroker broker) { _chargeSettingsMap = new AdductMap <SrmSettings>(); // Build a dictionary mapping sequence to proteins because getting this information is slow. var dictSequenceProteins = new Dictionary <string, IList <ProteinInfo> >(); var dictNewNodePeps = new Dictionary <PeptideSequenceModKey, PeptideMatch>(); PeptideMatches = null; MatchedPeptideCount = 0; int peptides = 0; int totalPeptides = _libraryPepInfos.Count; ProteomeDb proteomeDb = null; IStatelessSession session = null; try { foreach (ViewLibraryPepInfo pepInfo in _libraryPepInfos) { if (broker.IsCanceled) { return; } var charge = pepInfo.Key.Adduct; // Find the matching peptide. var nodePepMatched = AssociateMatchingPeptide(pepInfo, charge).PeptideNode; if (nodePepMatched != null) { MatchedPeptideCount++; PeptideMatch peptideMatchInDict; // If peptide is already in the dictionary of peptides to add, merge the children. if (!dictNewNodePeps.TryGetValue(nodePepMatched.SequenceKey, out peptideMatchInDict)) { IList <ProteinInfo> matchedProteins = null; var target = nodePepMatched.Peptide.Target; // This is only set if the user has checked the associate peptide box. if (target.IsProteomic && _backgroundProteome != null) { var sequence = target.Sequence; // We want to query the background proteome as little as possible, // so sequences are mapped to protein lists in a dictionary. if (!dictSequenceProteins.TryGetValue(sequence, out matchedProteins)) { if (proteomeDb == null) { proteomeDb = _backgroundProteome.OpenProteomeDb(broker.CancellationToken); session = proteomeDb.OpenStatelessSession(false); } var digestion = proteomeDb.GetDigestion(); if (sequence.Length >= MIN_PEPTIDE_LENGTH) { matchedProteins = digestion.GetProteinsWithSequence(session, sequence) .Select(protein => new ProteinInfo(protein)).ToList(); } if (matchedProteins == null || matchedProteins.Count > MAX_PROTEIN_MATCHES) { // If the peptide was too short, or matched too many proteins, then // treat it as if it was not found in any proteins. matchedProteins = new List <ProteinInfo>(); } dictSequenceProteins.Add(sequence, matchedProteins); } } dictNewNodePeps.Add(nodePepMatched.SequenceKey, new PeptideMatch(nodePepMatched, matchedProteins, MatchesFilter(target, charge))); } else { PeptideDocNode nodePepInDictionary = peptideMatchInDict.NodePep; if (!nodePepInDictionary.HasChildCharge(charge)) { List <DocNode> newChildren = nodePepInDictionary.Children.ToList(); newChildren.AddRange(nodePepMatched.Children); newChildren.Sort(Peptide.CompareGroups); var key = nodePepMatched.SequenceKey; dictNewNodePeps.Remove(key); dictNewNodePeps.Add(key, new PeptideMatch((PeptideDocNode)nodePepInDictionary.ChangeChildren(newChildren), peptideMatchInDict.Proteins, peptideMatchInDict.MatchesFilterSettings)); } } } peptides++; int progressValue = (int)((peptides + 0.0) / totalPeptides * PERCENT_PEPTIDE_MATCH); broker.ProgressValue = progressValue; } PeptideMatches = dictNewNodePeps; } finally { using (proteomeDb) using (session) { } } }