public PeptideGroupDocNode(PeptideGroup id, Annotations annotations, ProteinMetadata proteinMetadata,
     PeptideDocNode[] children, bool autoManageChildren)
     : base(id, annotations, children, autoManageChildren)
 {
     if (Equals(id.Name, proteinMetadata.Name))
         proteinMetadata = proteinMetadata.ChangeName(null);  // Make it clear that the name hasn't been altered
     if (Equals(id.Description, proteinMetadata.Description))
         proteinMetadata = proteinMetadata.ChangeDescription(null);  // Make it clear that the description hasn't been altered
     _proteinMetadata = proteinMetadata;
 }
Example #2
0
 private PeptideGroupDocNode GetMoleculePeptideGroup(SrmDocument document, DataGridViewRow row, bool requireProductInfo)
 {
     var pepGroup = new PeptideGroup();
     var pep = GetMoleculePeptide(document, row, pepGroup, requireProductInfo);
     if (pep == null)
         return null;
     var name = Convert.ToString(row.Cells[INDEX_MOLECULE_GROUP].Value);
     if (string.IsNullOrEmpty(name))
         name = document.GetPeptideGroupId(true);
     var metadata = new ProteinMetadata(name, string.Empty).SetWebSearchCompleted();  // FUTURE: some kind of lookup for small molecules
     return new PeptideGroupDocNode(pepGroup, metadata, new[] {pep});
 }
Example #3
0
 private PeptideDocNode GetMoleculePeptide(SrmDocument document, DataGridViewRow row, PeptideGroup group, bool requireProductInfo)
 {
     DocNodeCustomIon ion;
     MoleculeInfo moleculeInfo;
     try
     {
         moleculeInfo = ReadPrecursorOrProductColumns(document, row, true); // Re-read the precursor columns
         if (moleculeInfo == null)
             return null; // Some failure, but exception was already handled
         ion = new DocNodeCustomIon(moleculeInfo.Formula, moleculeInfo.MonoMass, moleculeInfo.AverageMass,
             Convert.ToString(row.Cells[INDEX_MOLECULE_NAME].Value)); // Short name
     }
     catch (ArgumentException e)
     {
         ShowTransitionError(new PasteError
         {
             Column    = INDEX_MOLECULE_FORMULA,
             Line = row.Index,
             Message = e.Message
         });
         return null;
     }
     try
     {
         var pep = new Peptide(ion);
         var tranGroup = GetMoleculeTransitionGroup(document, row, pep, requireProductInfo);
         if (tranGroup == null)
             return null;
         return new PeptideDocNode(pep, document.Settings, null, null, moleculeInfo.ExplicitRetentionTime, new[] { tranGroup }, true);
     }
     catch (InvalidOperationException e)
     {
         ShowTransitionError(new PasteError
         {
             Column = INDEX_MOLECULE_FORMULA,
             Line = row.Index,
             Message = e.Message
         });
         return null;
     }
 }
        public PeptideGroupDocNode ChangeSettings(SrmSettings settingsNew, SrmSettingsDiff diff)
        {
            if (diff.Monitor != null)
            {
                diff.Monitor.ProcessGroup(this);
            }

            if (diff.DiffPeptides && settingsNew.PeptideSettings.Filter.AutoSelect && AutoManageChildren)
            {
                IList <DocNode> childrenNew = new List <DocNode>();

                int countPeptides = 0;
                int countIons     = 0;

                Dictionary <int, DocNode>           mapIndexToChild = CreateGlobalIndexToChildMap();
                Dictionary <PeptideModKey, DocNode> mapIdToChild    = CreatePeptideModToChildMap();

                foreach (PeptideDocNode nodePep in GetPeptideNodes(settingsNew, true))
                {
                    PeptideDocNode  nodePepResult = nodePep;
                    SrmSettingsDiff diffNode      = SrmSettingsDiff.ALL;

                    DocNode existing;
                    // Add values that existed before the change. First check for exact match by
                    // global index, which will happen when explicit modifications are added,
                    // and then by content identity.
                    if (mapIndexToChild.TryGetValue(nodePep.Id.GlobalIndex, out existing) ||
                        mapIdToChild.TryGetValue(nodePep.Key, out existing))
                    {
                        nodePepResult = (PeptideDocNode)existing;
                        diffNode      = diff;
                    }

                    if (nodePepResult != null)
                    {
                        // Materialize children of the peptide.
                        nodePepResult = nodePepResult.ChangeSettings(settingsNew, diffNode);

                        childrenNew.Add(nodePepResult);

                        // Make sure a single peptide group does not exceed document limits.
                        countPeptides++;
                        countIons += nodePepResult.TransitionCount;
                        if (countIons > SrmDocument.MAX_TRANSITION_COUNT)
                        {
                            throw new InvalidDataException(String.Format(
                                                               Resources.PeptideGroupDocNode_ChangeSettings_The_current_document_settings_would_cause_the_number_of_targeted_transitions_to_exceed__0_n0___The_document_settings_must_be_more_restrictive_or_add_fewer_proteins_,
                                                               SrmDocument.MAX_TRANSITION_COUNT));
                        }
                        if (countPeptides > SrmDocument.MAX_PEPTIDE_COUNT)
                        {
                            throw new InvalidDataException(String.Format(
                                                               Resources.PeptideGroupDocNode_ChangeSettings_The_current_document_settings_would_cause_the_number_of_peptides_to_exceed__0_n0___The_document_settings_must_be_more_restrictive_or_add_fewer_proteins_,
                                                               SrmDocument.MAX_PEPTIDE_COUNT));
                        }
                    }
                }

                if (PeptideGroup.Sequence != null)
                {
                    childrenNew = PeptideGroup.RankPeptides(childrenNew, settingsNew, true);
                }

                return((PeptideGroupDocNode)ChangeChildrenChecked(childrenNew));
            }
            else
            {
                var nodeResult = this;

                if (diff.DiffPeptides && diff.SettingsOld != null)
                {
                    // If variable modifications changed, remove all peptides with variable
                    // modifications which are no longer possible.
                    var modsNew    = settingsNew.PeptideSettings.Modifications;
                    var modsVarNew = modsNew.VariableModifications.ToArray();
                    var modsOld    = diff.SettingsOld.PeptideSettings.Modifications;
                    var modsVarOld = modsOld.VariableModifications.ToArray();
                    if (modsNew.MaxVariableMods < modsOld.MaxVariableMods ||
                        !ArrayUtil.EqualsDeep(modsVarNew, modsVarOld))
                    {
                        IList <DocNode> childrenNew = new List <DocNode>();
                        foreach (PeptideDocNode nodePeptide in nodeResult.Children)
                        {
                            if (nodePeptide.AreVariableModsPossible(modsNew.MaxVariableMods, modsVarNew))
                            {
                                childrenNew.Add(nodePeptide);
                            }
                        }

                        nodeResult = (PeptideGroupDocNode)nodeResult.ChangeChildrenChecked(childrenNew);
                    }
                }

                // Check for changes affecting children
                if (diff.DiffPeptideProps || diff.DiffExplicit ||
                    diff.DiffTransitionGroups || diff.DiffTransitionGroupProps ||
                    diff.DiffTransitions || diff.DiffTransitionProps ||
                    diff.DiffResults)
                {
                    IList <DocNode> childrenNew = new List <DocNode>();

                    // Enumerate the nodes making necessary changes.
                    foreach (PeptideDocNode nodePeptide in nodeResult.Children)
                    {
                        childrenNew.Add(nodePeptide.ChangeSettings(settingsNew, diff));
                    }

                    childrenNew = RankChildren(settingsNew, childrenNew);

                    nodeResult = (PeptideGroupDocNode)nodeResult.ChangeChildrenChecked(childrenNew);
                }
                return(nodeResult);
            }
        }
        public SrmDocument ConvertToSmallMolecules(SrmDocument document, 
            ConvertToSmallMoleculesMode mode = ConvertToSmallMoleculesMode.formulas,
            bool invertCharges = false,
            bool ignoreDecoys=false)
        {
            if (mode == ConvertToSmallMoleculesMode.none)
                return document;
            var newdoc = new SrmDocument(document.Settings);
            var note = new Annotations(TestingConvertedFromProteomic, null, 1); // Mark this as a testing node so we don't sort it

            newdoc = (SrmDocument)newdoc.ChangeIgnoreChangingChildren(true); // Retain copied results

            foreach (var peptideGroupDocNode in document.MoleculeGroups)
            {
                if (!peptideGroupDocNode.IsProteomic)
                {
                    newdoc = (SrmDocument)newdoc.Add(peptideGroupDocNode); // Already a small molecule
                }
                else
                {
                    var newPeptideGroup = new PeptideGroup();
                    var newPeptideGroupDocNode = new PeptideGroupDocNode(newPeptideGroup,
                        peptideGroupDocNode.Annotations.Merge(note), peptideGroupDocNode.Name,
                        peptideGroupDocNode.Description, new PeptideDocNode[0],
                        peptideGroupDocNode.AutoManageChildren);
                    foreach (var mol in peptideGroupDocNode.Molecules)
                    {
                        var peptideSequence = mol.Peptide.Sequence;
                        // Create a PeptideDocNode with the presumably baseline charge and label
                        var precursorCharge = (mol.TransitionGroups.Any() ? mol.TransitionGroups.First().TransitionGroup.PrecursorCharge : 0) * (invertCharges ? -1 : 1);
                        var isotopeLabelType = mol.TransitionGroups.Any() ? mol.TransitionGroups.First().TransitionGroup.LabelType : IsotopeLabelType.light;
                        var moleculeCustomIon = ConvertToSmallMolecule(mode, document, mol, precursorCharge, isotopeLabelType);
                        var precursorCustomIon = moleculeCustomIon;
                        var newPeptide = new Peptide(moleculeCustomIon);
                        var newPeptideDocNode = new PeptideDocNode(newPeptide, newdoc.Settings, null, null,
                            null, null, mol.ExplicitRetentionTime, note, mol.Results, new TransitionGroupDocNode[0],
                            mol.AutoManageChildren);

                        foreach (var transitionGroupDocNode in mol.TransitionGroups)
                        {
                            if (transitionGroupDocNode.IsDecoy)
                            {
                                if (ignoreDecoys)
                                    continue;
                                throw new Exception("There is no translation from decoy to small molecules"); // Not L10N
                            }

                            if (transitionGroupDocNode.TransitionGroup.PrecursorCharge != Math.Abs(precursorCharge) ||
                                !Equals(isotopeLabelType, transitionGroupDocNode.TransitionGroup.LabelType))
                            {
                                // Different charges or labels mean different ion formulas
                                precursorCharge = transitionGroupDocNode.TransitionGroup.PrecursorCharge * (invertCharges ? -1 : 1);
                                isotopeLabelType = transitionGroupDocNode.TransitionGroup.LabelType;
                                precursorCustomIon = ConvertToSmallMolecule(mode, document, mol, precursorCharge, isotopeLabelType);
                            }

                            var newTransitionGroup = new TransitionGroup(newPeptide, precursorCustomIon, precursorCharge, isotopeLabelType);
                            // Remove any library info, since for the moment at least small molecules don't support this and it won't roundtrip
                            var resultsNew = RemoveTransitionGroupChromInfoLibraryInfo(transitionGroupDocNode);
                            var newTransitionGroupDocNode = new TransitionGroupDocNode(newTransitionGroup,
                                transitionGroupDocNode.Annotations.Merge(note), document.Settings,
                                null, null, transitionGroupDocNode.ExplicitValues, resultsNew, null,
                                transitionGroupDocNode.AutoManageChildren);
                            var mzShift = invertCharges ? 2.0 * BioMassCalc.MassProton : 0;  // We removed hydrogen rather than added
                            Assume.IsTrue((Math.Abs(newTransitionGroupDocNode.PrecursorMz + mzShift - transitionGroupDocNode.PrecursorMz) - Math.Abs(transitionGroupDocNode.TransitionGroup.PrecursorCharge * BioMassCalc.MassElectron)) <= 1E-5);

                            foreach (var transition in transitionGroupDocNode.Transitions)
                            {
                                double mass = 0;
                                var transitionCharge = transition.Transition.Charge * (invertCharges ? -1 : 1);
                                var ionType = IonType.custom;
                                CustomIon transitionCustomIon;
                                double mzShiftTransition = 0;
                                if (transition.Transition.IonType == IonType.precursor)
                                {
                                    ionType = IonType.precursor;
                                    transitionCustomIon = new DocNodeCustomIon(precursorCustomIon.Formula,
                                        string.IsNullOrEmpty(precursorCustomIon.Formula) ? precursorCustomIon.MonoisotopicMass : (double?) null,
                                        string.IsNullOrEmpty(precursorCustomIon.Formula) ? precursorCustomIon.AverageMass : (double?) null,
                                        SmallMoleculeNameFromPeptide(peptideSequence, transitionCharge));
                                    mzShiftTransition = invertCharges ? 2.0 * BioMassCalc.MassProton : 0;  // We removed hydrogen rather than added
                                }
                                else if (transition.Transition.IonType == IonType.custom)
                                {
                                    transitionCustomIon = transition.Transition.CustomIon;
                                    mass = transitionCustomIon.MonoisotopicMass;
                                }
                                else
                                {
                                    // TODO - try to get fragment formula?
                                    mass = BioMassCalc.CalculateIonMassFromMz(transition.Mz, transition.Transition.Charge);
                                    transitionCustomIon = new DocNodeCustomIon(mass, mass,// We can't really get at mono vs average mass from m/z, but for test purposes this is fine
                                        transition.Transition.FragmentIonName);
                                }
                                if (mode == ConvertToSmallMoleculesMode.masses_and_names)
                                {
                                    // Discard the formula if we're testing the use of mass-with-names (for matching in ratio calcs) target specification
                                    transitionCustomIon = new DocNodeCustomIon(transitionCustomIon.MonoisotopicMass, transitionCustomIon.AverageMass,
                                        transition.Transition.FragmentIonName);
                                }
                                else if (mode == ConvertToSmallMoleculesMode.masses_only)
                                {
                                    // Discard the formula and name if we're testing the use of mass-only target specification
                                    transitionCustomIon = new DocNodeCustomIon(transitionCustomIon.MonoisotopicMass, transitionCustomIon.AverageMass);
                                }

                                var newTransition = new Transition(newTransitionGroup, ionType,
                                    null, transition.Transition.MassIndex, transition.Transition.Charge * (invertCharges ? -1 : 1), null,
                                    transitionCustomIon);
                                if (ionType == IonType.precursor)
                                {
                                    mass = document.Settings.GetFragmentMass(transitionGroupDocNode.TransitionGroup.LabelType, null, newTransition, newTransitionGroupDocNode.IsotopeDist);
                                }
                                var newTransitionDocNode = new TransitionDocNode(newTransition, transition.Annotations.Merge(note),
                                    null, mass, transition.IsotopeDistInfo, null,
                                    transition.Results);
                                Assume.IsTrue((Math.Abs(newTransitionDocNode.Mz + mzShiftTransition - transition.Mz) - Math.Abs(transitionGroupDocNode.TransitionGroup.PrecursorCharge * BioMassCalc.MassElectron)) <= 1E-5, String.Format("unexpected mz difference {0}-{1}={2}", newTransitionDocNode.Mz , transition.Mz, newTransitionDocNode.Mz - transition.Mz)); // Not L10N
                                newTransitionGroupDocNode =
                                    (TransitionGroupDocNode)newTransitionGroupDocNode.Add(newTransitionDocNode);
                            }
                            if (newPeptideDocNode != null)
                                newPeptideDocNode = (PeptideDocNode)newPeptideDocNode.Add(newTransitionGroupDocNode);
                        }
                        newPeptideGroupDocNode =
                            (PeptideGroupDocNode)newPeptideGroupDocNode.Add(newPeptideDocNode);
                    }
                    newdoc = (SrmDocument)newdoc.Add(newPeptideGroupDocNode);
                }
            }

            // No retention time prediction for small molecules (yet?)
            newdoc = newdoc.ChangeSettings(newdoc.Settings.ChangePeptideSettings(newdoc.Settings.PeptideSettings.ChangePrediction(
                        newdoc.Settings.PeptideSettings.Prediction.ChangeRetentionTime(null))));

            return newdoc;
        }
Example #6
0
        public PeptideGroupDocNode ChangeSettings(SrmSettings settingsNew, SrmSettingsDiff diff,
                                                  DocumentSettingsContext context = null)
        {
            if (diff.Monitor != null)
            {
                diff.Monitor.ProcessGroup(this);
            }

            if (diff.DiffPeptides && settingsNew.PeptideSettings.Filter.AutoSelect && AutoManageChildren)
            {
                IList <DocNode> childrenNew = new List <DocNode>();

                int countPeptides = 0;
                int countIons     = 0;

                Dictionary <int, DocNode>           mapIndexToChild = CreateGlobalIndexToChildMap();
                Dictionary <PeptideModKey, DocNode> mapIdToChild    = CreatePeptideModToChildMap();

                IEnumerable <PeptideDocNode> peptideDocNodes;
                if (settingsNew.PeptideSettings.Filter.PeptideUniqueness == PeptideFilter.PeptideUniquenessConstraint.none ||
                    settingsNew.PeptideSettings.NeedsBackgroundProteomeUniquenessCheckProcessing)
                {
                    peptideDocNodes = GetPeptideNodes(settingsNew, true).ToList();
                }
                else
                {
                    // Checking peptide uniqueness against the background proteome can be expensive.
                    // Do all the regular processing, then filter those results at the end when we
                    // can do it in aggregate for best speed.
                    IEnumerable <PeptideDocNode> peptideDocNodesUnique;
                    var peptideDocNodesPrecalculatedForUniquenessCheck = context == null ? null : context.PeptideDocNodesPrecalculatedForUniquenessCheck;
                    var uniquenessDict = context == null ? null : context.UniquenessDict;
                    if (peptideDocNodesPrecalculatedForUniquenessCheck != null)
                    {
                        // Already processed, and a global list of peptides provided
                        Assume.IsNotNull(uniquenessDict);
                        peptideDocNodesUnique = peptideDocNodesPrecalculatedForUniquenessCheck;
                    }
                    else
                    {
                        // We'll have to do the processing for this node, and work with
                        // just the peptides on this node.  With luck the background proteome
                        // will already have those cached for uniqueness checks.
                        var settingsNoUniquenessFilter =
                            settingsNew.ChangePeptideFilter(f => f.ChangePeptideUniqueness(PeptideFilter.PeptideUniquenessConstraint.none));
                        var nodes     = GetPeptideNodes(settingsNoUniquenessFilter, true).ToList();
                        var sequences = new List <string>(from p in nodes select p.Peptide.Sequence);
                        peptideDocNodesUnique = nodes;  // Avoid ReSharper multiple enumeration warning
                        uniquenessDict        = settingsNew.PeptideSettings.Filter.CheckPeptideUniqueness(settingsNew, sequences, diff.Monitor);
                    }
                    // ReSharper disable once PossibleNullReferenceException
                    peptideDocNodes = peptideDocNodesUnique.Where(p =>
                    {
                        // It's possible during document load for uniqueness dict to get out of synch, so be
                        // cautious with lookup and just return false of not found. Final document change will clean that up.
                        bool isUnique;
                        return(uniquenessDict.TryGetValue(p.Peptide.Sequence, out isUnique) && isUnique);
                    });
                }

                foreach (var nodePep in peptideDocNodes)
                {
                    PeptideDocNode  nodePepResult = nodePep;
                    SrmSettingsDiff diffNode      = SrmSettingsDiff.ALL;

                    DocNode existing;
                    // Add values that existed before the change. First check for exact match by
                    // global index, which will happen when explicit modifications are added,
                    // and then by content identity.
                    if (mapIndexToChild.TryGetValue(nodePep.Id.GlobalIndex, out existing) ||
                        mapIdToChild.TryGetValue(nodePep.Key, out existing))
                    {
                        nodePepResult = (PeptideDocNode)existing;
                        diffNode      = diff;
                    }

                    if (nodePepResult != null)
                    {
                        // Materialize children of the peptide.
                        nodePepResult = nodePepResult.ChangeSettings(settingsNew, diffNode);

                        childrenNew.Add(nodePepResult);

                        // Make sure a single peptide group does not exceed document limits.
                        countPeptides++;
                        countIons += nodePepResult.TransitionCount;
                        if (countIons > SrmDocument.MAX_TRANSITION_COUNT)
                        {
                            throw new InvalidDataException(String.Format(
                                                               Resources.PeptideGroupDocNode_ChangeSettings_The_current_document_settings_would_cause_the_number_of_targeted_transitions_to_exceed__0_n0___The_document_settings_must_be_more_restrictive_or_add_fewer_proteins_,
                                                               SrmDocument.MAX_TRANSITION_COUNT));
                        }
                        if (countPeptides > SrmDocument.MAX_PEPTIDE_COUNT)
                        {
                            throw new InvalidDataException(String.Format(
                                                               Resources.PeptideGroupDocNode_ChangeSettings_The_current_document_settings_would_cause_the_number_of_peptides_to_exceed__0_n0___The_document_settings_must_be_more_restrictive_or_add_fewer_proteins_,
                                                               SrmDocument.MAX_PEPTIDE_COUNT));
                        }
                    }
                }

                if (PeptideGroup.Sequence != null)
                {
                    childrenNew = PeptideGroup.RankPeptides(childrenNew, settingsNew, true);
                }

                return((PeptideGroupDocNode)ChangeChildrenChecked(childrenNew));
            }
            else
            {
                var nodeResult = this;

                if (diff.DiffPeptides && diff.SettingsOld != null)
                {
                    // If variable modifications changed, remove all peptides with variable
                    // modifications which are no longer possible.
                    var modsNew    = settingsNew.PeptideSettings.Modifications;
                    var modsVarNew = modsNew.VariableModifications.ToArray();
                    var modsOld    = diff.SettingsOld.PeptideSettings.Modifications;
                    var modsVarOld = modsOld.VariableModifications.ToArray();
                    if (modsNew.MaxVariableMods < modsOld.MaxVariableMods ||
                        !ArrayUtil.EqualsDeep(modsVarNew, modsVarOld))
                    {
                        IList <DocNode> childrenNew = new List <DocNode>();
                        foreach (PeptideDocNode nodePeptide in nodeResult.Children)
                        {
                            if (nodePeptide.AreVariableModsPossible(modsNew.MaxVariableMods, modsVarNew))
                            {
                                childrenNew.Add(nodePeptide);
                            }
                        }

                        nodeResult = (PeptideGroupDocNode)nodeResult.ChangeChildrenChecked(childrenNew);
                    }
                }

                // Check for changes affecting children
                if (diff.DiffPeptideProps || diff.DiffExplicit ||
                    diff.DiffTransitionGroups || diff.DiffTransitionGroupProps ||
                    diff.DiffTransitions || diff.DiffTransitionProps ||
                    diff.DiffResults)
                {
                    IList <DocNode> childrenNew = new List <DocNode>();

                    // Enumerate the nodes making necessary changes.
                    foreach (PeptideDocNode nodePeptide in nodeResult.Children)
                    {
                        childrenNew.Add(nodePeptide.ChangeSettings(settingsNew, diff));
                    }

                    childrenNew = RankChildren(settingsNew, childrenNew);

                    nodeResult = (PeptideGroupDocNode)nodeResult.ChangeChildrenChecked(childrenNew);
                }
                return(nodeResult);
            }
        }
 public PeptideGroupDocNode(PeptideGroup id, Annotations annotations, string name, string description,
     PeptideDocNode[] children, bool autoManageChildren)
     : this(id, annotations, new ProteinMetadata(name, description), children, autoManageChildren)
 {
 }
 public PeptideGroupDocNode(PeptideGroup id, Annotations annotations, string name, string description,
     PeptideDocNode[] children)
     : this(id, annotations, name, description, children, true)
 {
 }
 public PeptideGroupDocNode(PeptideGroup id, ProteinMetadata proteinMetadata, PeptideDocNode[] children)
     : this(id, Annotations.EMPTY, proteinMetadata, children, true)
 {
 }
        private ProteinMetadata _proteinMetadata; // name, description, accession, gene, etc

        #endregion Fields

        #region Constructors

        public PeptideGroupDocNode(PeptideGroup id, string name, string description, PeptideDocNode[] children)
            : this(id, Annotations.EMPTY, name, description, children)
        {
        }
Example #11
0
 public PeptideGroupDocNode(PeptideGroup id, Annotations annotations, ProteinMetadata proteinMetadata,
                            PeptideDocNode[] children, bool autoManageChildren)
     : this(id, annotations, proteinMetadata, children, autoManageChildren, null)
 {
 }