Esempio n. 1
0
        /// <summary>
        /// Parses a string sequence of amino acids characters into a peptide object
        /// </summary>
        /// <param name="sequence"></param>
        /// <returns></returns>
        private void ParseSequence(string sequence)
        {
            bool inMod        = false;
            bool cterminalMod = false; // n or c terminal modification
            int  index        = 0;

            double monoMass = 0;

            StringBuilder modSb = new StringBuilder(10);

            foreach (char letter in sequence)
            {
                if (inMod)
                {
                    if (letter == ']')
                    {
                        inMod = false; // end the modification phase

                        string modString = modSb.ToString();
                        modSb.Clear();
                        IHasMass modification;
                        try
                        {
                            modification = new OldSchoolChemicalFormulaModification(ChemicalFormula.ParseFormula(modString));
                        }
                        catch (MzLibException)
                        {
                            if (double.TryParse(modString, out double mass))
                            {
                                modification = new ModWithOnlyMass(mass);
                            }
                            else
                            {
                                throw new MzLibException("Unable to correctly parse the following modification: " + modString);
                            }
                        }

                        monoMass += modification.MonoisotopicMass;

                        if (_modifications == null)
                        {
                            _modifications = new IHasMass[Length + 2];
                        }

                        if (cterminalMod)
                        {
                            _modifications[index + 1] = modification;
                        }
                        else
                        {
                            _modifications[index] = modification;
                        }

                        cterminalMod = false;
                    }
                    else
                    {
                        modSb.Append(letter);
                    }
                }
                else
                {
                    //char upperletter = char.ToUpper(letter); // moved to amino acid dictionary
                    if (Residue.TryGetResidue(letter, out Residue residue))
                    {
                        residues[index++] = residue;
                        monoMass         += residue.MonoisotopicMass;
                    }
                    else
                    {
                        switch (letter)
                        {
                        case '[':     // start of a modification
                            inMod = true;
                            break;

                        case '-':     // End of an n-terminus mod or start of a c-terminus mod
                            cterminalMod = (index > 0);
                            break;

                        default:
                            throw new MzLibException(string.Format(CultureInfo.InvariantCulture, "Amino Acid Letter {0} does not exist in the Amino Acid Dictionary. {0} is also not a valid character", letter));
                        }
                    }
                }
            }

            if (inMod)
            {
                throw new MzLibException("Couldn't find the closing ] for a modification in this sequence: " + sequence);
            }

            Length            = index;
            MonoisotopicMass += monoMass;
            Array.Resize(ref residues, Length);
            if (_modifications != null)
            {
                Array.Resize(ref _modifications, Length + 2);
            }
        }
 public OldSchoolChemicalFormulaModification(OldSchoolChemicalFormulaModification other)
     : this(ChemicalFormula.ParseFormula(other.ThisChemicalFormula.Formula), other.Name, other.Sites)
 {
 }