private void GetSmallMoleculeFragments(LibKey key, TransitionGroupDocNode nodeGroupMatched, SpectrumPeaksInfo spectrum,
                                               IList <DocNode> transitionDocNodes)
        {
            // We usually don't know actual charge of fragments in the library, so just note + or - if
            // there are no peak annotations containing that info
            var fragmentCharge = key.Adduct.AdductCharge < 0 ? Adduct.M_MINUS : Adduct.M_PLUS;
            // Get list of possible transitions based on library spectrum
            var transitionsUnranked = new List <DocNode>();

            foreach (var peak in spectrum.Peaks)
            {
                transitionsUnranked.Add(TransitionFromPeakAndAnnotations(key, nodeGroupMatched, fragmentCharge, peak, null));
            }
            var nodeGroupUnranked = (TransitionGroupDocNode)nodeGroupMatched.ChangeChildren(transitionsUnranked);
            // Filter again, retain only those with rank info,  or at least an interesting name
            SpectrumHeaderInfo groupLibInfo = null;
            var transitionRanks             = new Dictionary <double, LibraryRankedSpectrumInfo.RankedMI>();

            nodeGroupUnranked.GetLibraryInfo(Settings, ExplicitMods.EMPTY, true, ref groupLibInfo, transitionRanks);
            foreach (var ranked in transitionRanks)
            {
                transitionDocNodes.Add(TransitionFromPeakAndAnnotations(key, nodeGroupMatched, fragmentCharge, ranked.Value.MI, ranked.Value.Rank));
            }
            // And add any unranked that have names to display
            foreach (var unrankedT in nodeGroupUnranked.Transitions)
            {
                var unranked = unrankedT;
                if (!string.IsNullOrEmpty(unranked.Transition.CustomIon.Name) &&
                    !transitionDocNodes.Any(t => t is TransitionDocNode && unranked.Transition.Equivalent(((TransitionDocNode)t).Transition)))
                {
                    transitionDocNodes.Add(unranked);
                }
            }
        }
Ejemplo n.º 2
0
        private TransitionGroupDocNode MakeTransitionGroupWithAllPossibleChildren(SrmSettings settings, IsotopeLabelType labelType)
        {
            var peptide                = new Peptide(TargetInfoObj.LookupSequence);
            var transitionGroup        = new TransitionGroup(peptide, TargetInfoObj.TransitionGroupDocNode.PrecursorAdduct, labelType);
            var transitionGroupDocNode = new TransitionGroupDocNode(transitionGroup, Annotations.EMPTY, settings, TargetInfoObj.LookupMods, null, ExplicitTransitionGroupValues.EMPTY, null, null, false);
            var children               = transitionGroupDocNode.GetTransitions(settings, TargetInfoObj.LookupMods,
                                                                               transitionGroupDocNode.PrecursorMz, null, null, null, FragmentFilterObj.UseFilter).Cast <DocNode>().ToList();

            return((TransitionGroupDocNode)transitionGroupDocNode.ChangeChildren(children));
        }
Ejemplo n.º 3
0
        public TransitionGroupDocNode UpdatePrecursor(IdentityPath parent, TransitionGroupDocNode precursorDocNode)
        {
            CheckCancelled();
            AnnotationUpdater updater;

            _annotationUpdaters.TryGetValue(AnnotationDef.AnnotationTarget.precursor, out updater);
            IdentityPath identityPath = new IdentityPath(parent, precursorDocNode.TransitionGroup);

            if (updater != null || _precursorResultUpdater != null)
            {
                var precursor = new Precursor(SkylineDataSchema, identityPath);
                if (updater != null)
                {
                    var annotations = updater.UpdateAnnotations(precursorDocNode.Annotations, precursor);
                    if (!Equals(annotations, precursorDocNode.Annotations))
                    {
                        precursorDocNode = (TransitionGroupDocNode)precursorDocNode.ChangeAnnotations(annotations);
                    }
                }

                if (_precursorResultUpdater != null)
                {
                    var newResults = _precursorResultUpdater.Update(precursorDocNode.Results, precursor.Results);
                    precursorDocNode = precursorDocNode.ChangeResults(newResults);
                }
            }

            if (!RecurseTransitions)
            {
                return(precursorDocNode);
            }

            var newChildren = precursorDocNode.Transitions
                              .Select(transition => UpdateTransition(identityPath, transition)).ToArray();

            if (!ArrayUtil.ReferencesEqual(precursorDocNode.Children, newChildren))
            {
                precursorDocNode = (TransitionGroupDocNode)precursorDocNode.ChangeChildren(newChildren);
            }

            return(precursorDocNode);
        }
        public PeptideDocNode CreateDocNodeFromSettings(LibKey key, Peptide peptide, SrmSettingsDiff diff, out TransitionGroupDocNode nodeGroupMatched)
        {
            if (!key.Target.IsProteomic)
            {
                // Scan the spectral lib entry for top N ranked (for now, that's just by intensity with high mz as tie breaker) fragments,
                // add those as mass-only fragments, or with more detail if peak annotations are present.
                foreach (var nodePep in peptide.CreateDocNodes(Settings, new MaxModFilter(0)))
                {
                    SpectrumHeaderInfo libInfo;
                    if (nodePep != null && Settings.PeptideSettings.Libraries.TryGetLibInfo(key, out libInfo))
                    {
                        var isotopeLabelType = key.Adduct.HasIsotopeLabels ? IsotopeLabelType.heavy : IsotopeLabelType.light;
                        var group            = new TransitionGroup(peptide, key.Adduct, isotopeLabelType);
                        nodeGroupMatched = new TransitionGroupDocNode(group, Annotations.EMPTY, Settings, null, libInfo, ExplicitTransitionGroupValues.EMPTY, null, null, false);
                        SpectrumPeaksInfo spectrum;
                        if (Settings.PeptideSettings.Libraries.TryLoadSpectrum(key, out spectrum))
                        {
                            // Add fragment and precursor transitions as needed
                            var transitionDocNodes =
                                Settings.TransitionSettings.Filter.SmallMoleculeIonTypes.Contains(IonType.precursor)
                                    ? nodeGroupMatched.GetPrecursorChoices(Settings, null, true) // Gives list of precursors
                                    : new List <DocNode>();

                            if (Settings.TransitionSettings.Filter.SmallMoleculeIonTypes.Contains(IonType.custom))
                            {
                                GetSmallMoleculeFragments(key, nodeGroupMatched, spectrum, transitionDocNodes);
                            }
                            nodeGroupMatched = (TransitionGroupDocNode)nodeGroupMatched.ChangeChildren(transitionDocNodes);
                            return((PeptideDocNode)nodePep.ChangeChildren(new List <DocNode>()
                            {
                                nodeGroupMatched
                            }));
                        }
                    }
                }
                nodeGroupMatched = null;
                return(null);
            }
            return(CreateDocNodeFromSettings(key.Target, peptide, diff, out nodeGroupMatched));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Enumerate all document peptides. If a library peptide already exists in the
        /// current document, update the transition groups for that document peptide and
        /// remove the peptide from the list to add.
        /// </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="toPath">Currently selected path.</param>
        /// <param name="selectedPath">Selected path after the nodes have been added</param>
        /// <returns>A new document with precursors for existing petides added</returns>
        private SrmDocument UpdateExistingPeptides(SrmDocument document,
                                                   Dictionary <PeptideSequenceModKey, PeptideMatch> dictCopy,
                                                   IdentityPath toPath, out IdentityPath selectedPath)
        {
            selectedPath = toPath;
            IList <DocNode> nodePepGroups         = new List <DocNode>();
            var             keysAddedWithoutMatch = new
                                                    SortedDictionary <PeptideSequenceModKey, PeptideMatch>();

            foreach (PeptideGroupDocNode nodePepGroup in document.MoleculeGroups)
            {
                IList <DocNode> nodePeps = new List <DocNode>();
                foreach (PeptideDocNode nodePep in nodePepGroup.Children)
                {
                    var          key = nodePep.SequenceKey;
                    PeptideMatch peptideMatch;
                    // If this peptide is not in our list of peptides to add,
                    // or if we are in a peptide list and this peptide has been matched to protein(s),
                    // then we don't touch this particular node.
                    if (!dictCopy.TryGetValue(key, out peptideMatch) ||
                        (nodePepGroup.IsPeptideList &&
                         (peptideMatch.Proteins != null && peptideMatch.Proteins.Any())))
                    {
                        nodePeps.Add(nodePep);
                        if (keysAddedWithoutMatch.ContainsKey(key))
                        {
                            keysAddedWithoutMatch.Add(key, new PeptideMatch(null, null, false));
                        }
                    }
                    else
                    {
                        var proteinName  = nodePepGroup.PeptideGroup.Name;
                        int indexProtein = -1;
                        if (peptideMatch.Proteins != null)
                        {
                            indexProtein =
                                peptideMatch.Proteins.IndexOf(protein => Equals(protein.ProteinMetadata.Name, proteinName));
                            // If the user has opted to filter duplicate peptides, remove this peptide from the list to
                            // add and continue.
                            if (FilterMultipleProteinMatches == BackgroundProteome.DuplicateProteinsFilter.NoDuplicates && peptideMatch.Proteins.Count > 1)
                            {
                                dictCopy.Remove(key);
                                nodePeps.Add(nodePep);
                                continue;
                            }
                            // [1] If this protein is not the first match, and the user has opted to add only the first occurence,
                            // [2] or if this protein is not one of the matches, and [2a] we are either not in a peptide list
                            // [2b] or the user has opted to filter unmatched peptides, ignore this particular node.
                            if ((indexProtein > 0 && FilterMultipleProteinMatches == BackgroundProteome.DuplicateProteinsFilter.FirstOccurence) ||
                                (indexProtein == -1 &&
                                 (!nodePepGroup.IsPeptideList || !Properties.Settings.Default.LibraryPeptidesAddUnmatched)))
                            {
                                nodePeps.Add(nodePep);
                                continue;
                            }
                        }
                        // Update the children of the peptide in the document to include the charge state of the peptide we are adding.
                        PeptideDocNode nodePepMatch      = peptideMatch.NodePep;
                        PeptideDocNode nodePepSettings   = null;
                        var            newChildren       = nodePep.Children.ToList();
                        Identity       nodeGroupChargeId = newChildren.Count > 0 ? newChildren[0].Id : null;
                        foreach (TransitionGroupDocNode nodeGroup in nodePepMatch.Children)
                        {
                            var chargeGroup = nodeGroup.TransitionGroup.PrecursorAdduct;
                            if (nodePep.HasChildCharge(chargeGroup))
                            {
                                SkippedPeptideCount++;
                            }
                            else
                            {
                                if (nodePepSettings == null)
                                {
                                    nodePepSettings = nodePepMatch.ChangeSettings(document.Settings, SrmSettingsDiff.ALL);
                                }
                                TransitionGroupDocNode nodeGroupCharge = (TransitionGroupDocNode)nodePepSettings.FindNode(nodeGroup.TransitionGroup);
                                if (nodeGroupCharge == null)
                                {
                                    continue;
                                }
                                if (peptideMatch.Proteins != null && peptideMatch.Proteins.Count > 1)
                                {
                                    // If we may be adding this specific node to the document more than once, create a copy of it so that
                                    // we don't have two nodes with the same global id.
                                    nodeGroupCharge = (TransitionGroupDocNode)nodeGroupCharge.CopyId();
                                    nodeGroupCharge = (TransitionGroupDocNode)nodeGroupCharge.ChangeChildren(
                                        nodeGroupCharge.Children.ToList().ConvertAll(child => child.CopyId()));
                                }
                                nodeGroupChargeId = nodeGroupCharge.Id;
                                newChildren.Add(nodeGroupCharge);
                            }
                        }
                        // Sort the new peptide children.
                        newChildren.Sort(Peptide.CompareGroups);
                        var nodePepAdd = nodePep.ChangeChildrenChecked(newChildren);
                        // If we have changed the children, need to set automanage children to false.
                        if (nodePep.AutoManageChildren && !ReferenceEquals(nodePep, nodePepAdd))
                        {
                            nodePepAdd = nodePepAdd.ChangeAutoManageChildren(false);
                        }
                        // Change the selected path.
                        if (PeptideMatches.Count == 1)
                        {
                            selectedPath = nodeGroupChargeId == null
                                                ? new IdentityPath(new[] { nodePepGroup.Id, nodePepAdd.Id })
                                                : new IdentityPath(new[] { nodePepGroup.Id, nodePepAdd.Id, nodeGroupChargeId });
                        }
                        nodePeps.Add(nodePepAdd);
                        // Remove this peptide from the list of peptides we need to add to the document
                        dictCopy.Remove(key);
                        if (peptideMatch.Proteins != null)
                        {
                            if (indexProtein != -1)
                            {
                                // Remove this protein from the list of proteins associated with the peptide.
                                peptideMatch.Proteins.RemoveAt(indexProtein);
                            }
                            // If this peptide has not yet been added to all matched proteins,
                            // put it back in the list of peptides to add.
                            if (peptideMatch.Proteins.Count != 0 && FilterMultipleProteinMatches != BackgroundProteome.DuplicateProteinsFilter.FirstOccurence)
                            {
                                dictCopy.Add(key, peptideMatch);
                            }
                        }
                    }
                }
                nodePepGroups.Add(nodePepGroup.ChangeChildrenChecked(nodePeps));
            }
            return((SrmDocument)document.ChangeChildrenChecked(nodePepGroups));
        }