Ejemplo n.º 1
0
        // find matches using the background proteome
        public void UseBackgroundProteome()
        {
            if (_parent.Document.Settings.PeptideSettings.BackgroundProteome.Equals(BackgroundProteome.NONE))
            {
                MessageDlg.Show(this, Resources.AssociateProteinsDlg_UseBackgroundProteome_No_background_proteome_defined);
                return;
            }
            _isFasta  = false;
            _fileName = _parent.Document.Settings.PeptideSettings.BackgroundProteome.DatabasePath;
            checkBoxListMatches.Items.Clear();
            BackgroundProteome proteome = _parent.Document.Settings.PeptideSettings.BackgroundProteome;
            var proteinAssociations     = new List <KeyValuePair <FastaSequence, List <PeptideDocNode> > >();
            var peptidesForMatching     = ListPeptidesForMatching();

            IDictionary <String, List <Protein> > proteinsWithSequences = null;

            using (var longWaitDlg = new LongWaitDlg())
            {
                longWaitDlg.PerformWork(this, 1000, longWaitBroker =>
                {
                    using (var proteomeDb = proteome.OpenProteomeDb(longWaitBroker.CancellationToken))
                    {
                        proteinsWithSequences = proteomeDb.GetDigestion()
                                                .GetProteinsWithSequences(peptidesForMatching.Select(pep => pep.Peptide.Target.Sequence));
                    }
                });
            }
            if (proteinsWithSequences == null)
            {
                return;
            }
            HashSet <String> processedProteinSequence = new HashSet <string>();

            foreach (var entry in proteinsWithSequences)
            {
                foreach (var protein in entry.Value)
                {
                    if (!processedProteinSequence.Add(protein.Sequence))
                    {
                        continue;
                    }
                    var matches = peptidesForMatching.Where(pep => protein.Sequence.Contains(pep.Peptide.Target.Sequence)).ToList();
                    if (matches.Count == 0)
                    {
                        continue;
                    }
                    FastaSequence fastaSequence = proteome.MakeFastaSequence(protein);
                    if (fastaSequence != null)
                    {
                        proteinAssociations.Add(new KeyValuePair <FastaSequence, List <PeptideDocNode> >(fastaSequence, matches));
                    }
                }
            }
            SetCheckBoxListItems(proteinAssociations,
                                 Resources.AssociateProteinsDlg_UseBackgroundProteome_No_matches_were_found_using_the_background_proteome_);
        }
Ejemplo n.º 2
0
        /// <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)
                    {
                    }
            }
        }
        /// <summary>
        /// Tries to match each library peptide to document settings.
        /// </summary>
        public void MatchAllPeptides(ILongWaitBroker broker)
        {
            _chargeSettingsMap = new SrmSettings[128];

            // 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.Length;

            foreach (ViewLibraryPepInfo pepInfo in _libraryPepInfos)
            {
                if (broker.IsCanceled)
                {
                    return;
                }

                int charge = pepInfo.Key.Charge;
                // 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 sequence = nodePepMatched.Peptide.Sequence;
                        // This is only set if the user has checked the associate peptide box.
                        if (_backgroundProteome != null)
                        {
                            // 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))
                            {
                                using (var proteomeDb = _backgroundProteome.OpenProteomeDb())
                                {
                                    var digestion = _backgroundProteome.GetDigestion(proteomeDb, Settings.PeptideSettings);
                                    if (digestion != null)
                                    {
                                        matchedProteins = digestion.GetProteinsWithSequence(sequence).Select(protein => new ProteinInfo(protein)).ToList();
                                        dictSequenceProteins.Add(sequence, matchedProteins);
                                    }
                                }
                            }
                        }
                        dictNewNodePeps.Add(nodePepMatched.SequenceKey,
                                            new PeptideMatch(nodePepMatched, matchedProteins,
                                                             MatchesFilter(sequence, 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;
        }