For calculating peptide and fragment masses.
using System;
using System.Collections.Generic;
using matrix_science.msparser;
using System.Text;
namespace MsParserExamples
{
class tools_aahelper
{
public static int Main(string[] argv)
{
if (argv.Length < 2)
{
Console.Error.WriteLine("Location of enzymes file and mod_file have to be specified as parameters");
return 1;
}
ms_enzymefile enzymefile;
if (!openEnzymefile(argv[0], out enzymefile))
{
return 1;
}
ms_enzyme trypsin = enzymefile.getEnzymeByName("Trypsin");
if (trypsin == null)
{
Console.Error.WriteLine("Cannot find 'Trypsin' in the enzyme file. Cannot continue.");
return 1;
}
ms_modfile modfile;
if(!openModfile(argv[1], out modfile)) {
return 1;
}
ms_aahelper aahelper = new ms_aahelper();
aahelper.setMasses(modfile.getMassFile());
aahelper.setEnzyme(trypsin);
string proteinStr = "MAIFRIDEIRNMSSEELEEELRKLEVELIRERGAVRAGGAPEKPGRIREIRRTIARMKTVQRERVRK";
aahelper.startIteratePeptides(proteinStr, proteinStr.Length, 0);
Console.WriteLine("List of peptides");
while (aahelper.getNextPeptide())
{
int start = aahelper.getPepStart(), len = aahelper.getPepEnd() - aahelper.getPepStart() + 1;
string peptideStr = proteinStr.Substring(start - 1, len);
Console.WriteLine(peptideStr);
}
Console.WriteLine("End of list\n");
ms_modvector vecFixed = new ms_modvector();
vecFixed.appendModification(modfile.getModificationByName("Phospho (Y)"));
ms_modvector vecVariable = new ms_modvector();
vecVariable.appendModification(modfile.getModificationByName("Oxidation (M)"));
vecVariable.appendModification(modfile.getModificationByName("Acetyl (N-term)"));
aahelper.setAvailableModifications(vecFixed, vecVariable);
if (!aahelper.isValid())
{
Console.WriteLine("Error while setting available modifications: {0}", aahelper.getLastErrorString());
return 1;
}
ms_errs err = new ms_errs();
vectori numThatMustBeModded = new vectori();
numThatMustBeModded.Add(1);
numThatMustBeModded.Add(1);
double mr = aahelper.calcPeptideMZ(proteinStr,
proteinStr.Length,
1,
10,
numThatMustBeModded,
0,
MASS_TYPE.MASS_TYPE_MONO,
err);
if (!err.isValid())
{
Console.Error.WriteLine("Error while calculating peptide mass: {0}", err.getLastErrorString());
err.clearAllErrors();
}
else
{
Console.WriteLine("Peptide mass calculated using 'calcPeptideMZ' is {0:########.###}", mr);
}
vectori numModded = new vectori()
{
{2},
{1},
{0},
{0},
{0},
{0},
{0},
{0},
{0},
{0},
{0},
{0}
};
vectori whichNL = new vectori() {
{0},
{1},
{0},
{0},
{0},
{0},
{0},
{0},
{0},
{0},
{0},
{0}
};
ms_peptide peptide = aahelper.createPeptide(
proteinStr,
proteinStr.Length,
1,
10,
numModded,
whichNL,
0,
MASS_TYPE.MASS_TYPE_MONO,
err);
if (!err.isValid())
{
Console.Error.WriteLine("Error while creating a peptide: {0}", err.getLastErrorString());
err.clearAllErrors();
}
else
{
Console.WriteLine("\n\nPeptide has been created successfully: {0}", peptide.getPeptideStr());
}
ms_fragmentvector allFragments = new ms_fragmentvector();
ms_fragmentvector bions = fragmentPeptide(aahelper,
peptide,
(int) ms_fragmentationrules.FRAG_SERIES_TYPE.FRAG_B_SERIES,
"b-ions series",
false,
mr
);
allFragments.copyFrom(bions);
ms_fragmentvector fragments = fragmentPeptide(aahelper,
peptide,
(int) ms_fragmentationrules.FRAG_SERIES_TYPE.FRAG_Y_SERIES,
"y-ion series",
false,
mr
);
for (uint i = 0; i < fragments.getNumberOfFragments(); i++)
{
allFragments.appendFragment(fragments.getFragmentByNumber(i));
}
fragments = fragmentPeptide(aahelper,
peptide,
(int)ms_fragmentationrules.FRAG_SERIES_TYPE.FRAG_INTERNAL_YB,
"internal yb-ion series",
false,
700
);
for(uint i = 0; i < fragments.getNumberOfFragments(); i++) {
allFragments.appendFragment(fragments.getFragmentByNumber(i));
}
Console.WriteLine("Paste the following into a Mascot search query window to verify this output:");
displayMascotTestSearch(vecFixed, vecVariable, trypsin, peptide.getMrCalc(),
bions);
return 0;
}
private static void displayMascotTestSearch(ms_modvector vecFixed, ms_modvector vecVariable, ms_enzyme enzyme, double mr, ms_fragmentvector fragments)
{
for (int i = 0; i < vecFixed.getNumberOfModifications(); i++)
{
Console.WriteLine("MODS={0}", vecFixed.getModificationByNumber(i).getTitle());
}
for (int i = 0; i < vecVariable.getNumberOfModifications(); i++)
{
Console.WriteLine("IT_MODS={0}", vecVariable.getModificationByNumber(i).getTitle());
}
Console.WriteLine("CHARGE=Mr");
Console.WriteLine("CLE={0}", enzyme.getTitle());
Console.Write("{0:#.###} ions(", mr);
StringBuilder sbIons = new StringBuilder();
for (uint i = 0; i < fragments.getNumberOfFragments(); i++)
{
if (i > 0) sbIons.Append(", ");
sbIons.Append(string.Format("{0:0.###}", fragments.getFragmentByNumber(i).getMass()));
}
Console.Write(sbIons);
Console.WriteLine(")");
}
private static ms_fragmentvector fragmentPeptide(ms_aahelper aahelper, ms_peptide peptide, int series, string seriesLabel, bool doubleCharged, double massMax)
{
ms_fragmentvector fragments = new ms_fragmentvector();
ms_errs err = new ms_errs();
aahelper.calcFragments(
peptide,
series,
doubleCharged,
100d,
massMax,
MASS_TYPE.MASS_TYPE_MONO,
fragments,
err);
if (!err.isValid())
{
Console.Error.WriteLine("Error while creating peptide {0} fragments: {1}", seriesLabel, err.getLastErrorString());
err.clearAllErrors();
}
Console.WriteLine("{0} fragments", seriesLabel);
printFragmentsTable(fragments);
return fragments;
}
private static void printFragmentsTable(ms_fragmentvector fragments)
{
Console.WriteLine("Number of fragments: {0}", fragments.getNumberOfFragments());
string fmt = "{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}";
Console.WriteLine(fmt, "Col".PadLeft(5),
"Start".PadLeft(5),
"End".PadLeft(5),
"Label".PadLeft(10),
"Mass".PadLeft(10),
"NL".PadLeft(10),
"Name",
"Immon",
"Intern",
"Reg".PadLeft(4));
for (uint i = 0; i < fragments.getNumberOfFragments(); i++)
{
ms_fragment frag = fragments.getFragmentByNumber(i);
Console.WriteLine(fmt,
frag.getColumn().ToString().PadLeft(5),
frag.getStart().ToString().PadLeft(5),
frag.getEnd().ToString().PadLeft(5),
frag.getLabel().PadLeft(10),
string.Format("{0:######0.00}", frag.getMass()).PadLeft(10),
string.Format("{0:######0.00}", frag.getNeutralLoss()).PadLeft(10),
frag.getSeriesName().PadLeft(4),
(frag.isImmonium() ? "1" : "").PadLeft(5),
(frag.isInternal() ? "1" : "").PadLeft(6),
(frag.isRegular() ? "1" : "").PadLeft(4));
}
Console.WriteLine();
}
private static bool openModfile(string filename, out ms_modfile modfile)
{
ms_masses masses = new ms_masses();
modfile = new ms_modfile(filename, masses);
if (!modfile.isValid())
{
Console.Error.WriteLine("Error while opening mod file: {0}", modfile.getLastErrorString());
return false;
}
string[] aTargetMods = new string[]{"Oxidation (M)", "Acetyl (N-term)", "Phospho (Y)"};
foreach(string mod in aTargetMods) {
if (modfile.getModificationByName(mod) == null)
{
Console.Error.WriteLine("Cannot find '{0}' in the mod file. Cannot continue.", mod);
return false;
}
}
return true;
}
private static bool openEnzymefile(string filename, out ms_enzymefile enzymefile)
{
enzymefile = new ms_enzymefile(filename);
if (!enzymefile.isValid())
{
Console.Error.WriteLine("Error while opening enzyme file: {0}", enzymefile.getLastErrorString());
return false;
}
return true;
}
}
}