Beispiel #1
0
        /// <summary>
        ///     Gets a list of the peptide matches from the path provided.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private List <PeptideMatch> GetPeptideMatches(string path)
        {
            var lines = File.ReadAllLines(path).ToList();

            var matches = new List <PeptideMatch>();

            foreach (var line in lines)
            {
                var data = line.Split(',');
                if (data.Length < 6)
                {
                    continue;
                }

                try
                {
                    var match = new PeptideMatch();
                    match.Peptide = data[5];
                    match.ScanX   = Convert.ToInt32(data[1]);
                    match.ScanY   = Convert.ToInt32(data[3]);
                    matches.Add(match);
                }
                catch
                {
                }
            }

            return(matches);
        }
Beispiel #2
0
        /// <summary>
        /// Adds all peptides which can be matched to a background proteome to the
        /// proteins in the background proteins, and returns a new document with those
        /// proteins and peptides added.
        /// </summary>
        /// <param name="document">The starting document</param>
        /// <param name="dictCopy">A dictionary of peptides to peptide matches. All added
        /// peptides are removed</param>
        /// <param name="broker">For reporting long wait status</param>
        /// <param name="toPath">Path to the location in the document to add new items</param>
        /// <param name="selectedPath">Path to item in the document that should be selected
        /// after this operation is complete</param>
        /// <returns>A new document with matching peptides and their proteins addded</returns>
        private SrmDocument AddProteomePeptides(SrmDocument document,
                                                Dictionary <PeptideSequenceModKey, PeptideMatch> dictCopy,
                                                ILongWaitBroker broker,
                                                IdentityPath toPath,
                                                out IdentityPath selectedPath)
        {
            // Build a list of new PeptideGroupDocNodes to add to the document.
            var dictPeptideGroupsNew = new Dictionary <string, PeptideGroupDocNode>();

            // Get starting progress values
            int startPercent     = (broker != null ? broker.ProgressValue : 0);
            int processedPercent = 0;
            int processedCount   = 0;
            int totalMatches     = dictCopy.Count;

            // Just to make sure this is set
            selectedPath = toPath;

            foreach (PeptideMatch pepMatch in dictCopy.Values)
            {
                // Show progress, if in a long wait
                if (broker != null)
                {
                    if (broker.IsCanceled)
                    {
                        selectedPath = toPath;
                        return(document);
                    }
                    // All peptides with protein get processed in this loop.  Peptides
                    // without proteins get added later.
                    if (pepMatch.Proteins != null)
                    {
                        processedCount++;
                    }
                    int processPercentNow = processedCount * (100 - startPercent) / totalMatches;
                    if (processedPercent != processPercentNow)
                    {
                        processedPercent     = processPercentNow;
                        broker.ProgressValue = startPercent + processedPercent;
                    }
                }
                // Peptide should be added to the document,
                // unless the NoDuplicates radio was selected and the peptide has more than 1 protein associated with it.
                if (pepMatch.Proteins == null ||
                    (FilterMultipleProteinMatches == BackgroundProteome.DuplicateProteinsFilter.NoDuplicates && pepMatch.Proteins.Count > 1))
                {
                    continue;
                }


                foreach (ProteinInfo protein in pepMatch.Proteins)
                {
                    // Look for the protein in the document.
                    string name = protein.ProteinMetadata.Name;
                    var    peptideGroupDocNode = FindPeptideGroupDocNode(document, name);
                    bool   foundInDoc          = peptideGroupDocNode != null;
                    bool   foundInList         = false;
                    if (!foundInDoc)
                    {
                        // If the protein is not already in the document,
                        // check to see if we have already created a PeptideGroupDocNode for it.
                        if (name != null && dictPeptideGroupsNew.TryGetValue(name, out peptideGroupDocNode))
                        {
                            foundInList = true;
                        }
                        // If not, create a new PeptideGroupDocNode.
                        else
                        {
                            List <ProteinMetadata> alternativeProteins = new List <ProteinMetadata>(protein.Alternatives);
                            peptideGroupDocNode = new PeptideGroupDocNode(
                                new FastaSequence(name, protein.ProteinMetadata.Description, alternativeProteins, protein.Sequence),
                                null, null, new PeptideDocNode[0]);
                        }
                    }
                    // Create a new peptide that matches this protein.
                    var fastaSequence   = peptideGroupDocNode.PeptideGroup as FastaSequence;
                    var peptideSequence = pepMatch.NodePep.Peptide.Target.Sequence;
                    // ReSharper disable PossibleNullReferenceException
                    var begin = fastaSequence.Sequence.IndexOf(peptideSequence, StringComparison.Ordinal);
                    // ReSharper restore PossibleNullReferenceException
                    // Create a new PeptideDocNode using this peptide.
                    var newPeptide = new Peptide(fastaSequence, peptideSequence, begin, begin + peptideSequence.Length,
                                                 Settings.PeptideSettings.Enzyme.CountCleavagePoints(peptideSequence));
                    // Make sure we keep the same children.
                    PeptideMatch match      = pepMatch;
                    var          newNodePep = ((PeptideDocNode) new PeptideDocNode(newPeptide, pepMatch.NodePep.ExplicitMods, pepMatch.NodePep.ExplicitRetentionTime)
                                               .ChangeChildren(pepMatch.NodePep.Children.ToList().ConvertAll(nodeGroup =>
                    {
                        // Create copies of the children in order to prevent transition groups with the same
                        // global indices.
                        var nodeTranGroup = (TransitionGroupDocNode)nodeGroup;
                        if (match.Proteins != null && match.Proteins.Count > 1)
                        {
                            nodeTranGroup = (TransitionGroupDocNode)nodeTranGroup.CopyId();
                            nodeTranGroup = (TransitionGroupDocNode)nodeTranGroup.ChangeChildren(
                                nodeTranGroup.Children.ToList().ConvertAll(nodeTran => nodeTran.CopyId()));
                        }
                        return((DocNode)nodeTranGroup);
                    })).ChangeAutoManageChildren(false)).ChangeSettings(document.Settings, SrmSettingsDiff.ALL);
                    // If this PeptideDocNode is already a child of the PeptideGroupDocNode,
                    // ignore it.
                    if (peptideGroupDocNode.Children.Contains(nodePep => Equals(((PeptideDocNode)nodePep).Key, newNodePep.Key)))
                    {
                        Console.WriteLine(Resources.ViewLibraryPepMatching_AddProteomePeptides_Skipping__0__already_present, newNodePep.Peptide.Target);
                        continue;
                    }
                    // Otherwise, add it to the list of children for the PeptideGroupNode.
                    var newChildren = peptideGroupDocNode.Children.Cast <PeptideDocNode>().ToList();
                    newChildren.Add(newNodePep);
                    newChildren.Sort(FastaSequence.ComparePeptides);

                    // Store modified proteins by global index in a HashSet for second pass.
                    var newPeptideGroupDocNode = peptideGroupDocNode.ChangeChildren(newChildren.Cast <DocNode>().ToArray())
                                                 .ChangeAutoManageChildren(false);
                    // If the protein was already in the document, replace with the new PeptideGroupDocNode.
                    if (foundInDoc)
                    {
                        document = (SrmDocument)document.ReplaceChild(newPeptideGroupDocNode);
                    }
                    // Otherwise, update the list of new PeptideGroupDocNodes to add.
                    else
                    {
                        if (foundInList)
                        {
                            dictPeptideGroupsNew.Remove(peptideGroupDocNode.Name);
                        }
                        dictPeptideGroupsNew.Add(peptideGroupDocNode.Name, (PeptideGroupDocNode)newPeptideGroupDocNode);
                    }
                    // If we are only adding a single node, select it.
                    if (PeptideMatches.Count == 1)
                    {
                        selectedPath = new IdentityPath(new[] { peptideGroupDocNode.Id, newNodePep.Peptide });
                    }
                    // If the user only wants to add the first protein found,
                    // we break the foreach loop after peptide has been added to its first protein.)
                    if (FilterMultipleProteinMatches == BackgroundProteome.DuplicateProteinsFilter.FirstOccurence)
                    {
                        break;
                    }
                }
            }

            if (dictPeptideGroupsNew.Count == 0)
            {
                return(document);
            }

            // Sort the peptides.
            var nodePepGroupsSortedChildren = new List <PeptideGroupDocNode>();

            foreach (PeptideGroupDocNode nodePepGroup in dictPeptideGroupsNew.Values)
            {
                var newChildren = nodePepGroup.Children.ToList();
                // Have to cast all children to PeptideDocNodes in order to sort.
                var newChildrenNodePeps = newChildren.Cast <PeptideDocNode>().ToList();
                newChildrenNodePeps.Sort(FastaSequence.ComparePeptides);
                nodePepGroupsSortedChildren.Add((PeptideGroupDocNode)
                                                nodePepGroup.ChangeChildren(newChildrenNodePeps.Cast <DocNode>().ToArray()));
            }
            // Sort the proteins.
            nodePepGroupsSortedChildren.Sort((node1, node2) => Comparer <string> .Default.Compare(node1.Name, node2.Name));
            IdentityPath selPathTemp = selectedPath, nextAdd;

            document = document.AddPeptideGroups(nodePepGroupsSortedChildren, false,
                                                 toPath, out selectedPath, out nextAdd);
            selectedPath = PeptideMatches.Count == 1 ? selPathTemp : selectedPath;
            return(document);
        }