CandidateSearch 1.1.2
Proof-of-concept implementation of a search engine that uses sparse matrix multiplication to identify the best peptide candidates for a given mass spectrum.
Loading...
Searching...
No Matches
ChemicalUtils.cs
Go to the documentation of this file.
2{
3 public class ChemicalElement
4 {
5 public readonly string Name;
6 public readonly string FullName;
7 public readonly double MonoMass;
8 public readonly double AvgMass;
9
10 public ChemicalElement(string name, string fullName, double monoMass, double avgMass)
11 {
12 Name = name;
13 FullName = fullName;
14 MonoMass = monoMass;
15 AvgMass = avgMass;
16 }
17 }
18
19 public class AminoAcid
20 {
21 public readonly string Name;
22 public readonly string ThreeLetterCode;
23 public readonly char OneLetterCode;
24 private readonly double AverageMass;
25 private readonly double MonoisotopicMass;
26 public readonly double ImmoniumMass;
27
28 public double Mass(bool mono)
29 {
30 return mono ? MonoisotopicMass : AverageMass;
31 }
32
33 public AminoAcid(char oneLetter, string threeLetter, string name, double monoisotopicMass, double avgMass, double immoniumMass)
34 {
35 AverageMass = avgMass;
36 MonoisotopicMass = monoisotopicMass;
37 Name = name;
38 ThreeLetterCode = threeLetter;
39 OneLetterCode = oneLetter;
40 ImmoniumMass = immoniumMass;
41 }
42
43 public AminoAcid(char oneLetter, string threeLetter, string name, double monoisotopicMass, double avgMass, bool mono = true)
44 {
45 AverageMass = avgMass;
46 MonoisotopicMass = monoisotopicMass;
47 Name = name;
48 ThreeLetterCode = threeLetter;
49 OneLetterCode = oneLetter;
50 if (mono)
51 ImmoniumMass = MonoisotopicMass + ChemicalUtils.Chemicals["H"].MonoMass -
52 ChemicalUtils.Chemicals["C"].MonoMass - ChemicalUtils.Chemicals["O"].MonoMass;
53 else
54 ImmoniumMass = AverageMass + ChemicalUtils.Chemicals["H"].AvgMass -
55 ChemicalUtils.Chemicals["C"].AvgMass - ChemicalUtils.Chemicals["O"].AvgMass;
56 }
57 }
58
59 public class Permutations : IEquatable<Permutations>
60 {
61 public int N { get; set; }
62 public int K { get; set; }
63 public List<int[]> Elements { get; set; }
64
65 public Permutations(int n, int k)
66 {
67 N = n;
68 K = k;
69 Elements = new List<int[]>();
70 int[] data = new int[k];
71 for (int i = 0; i < k; ++i)
72 data[i] = i;
73 Elements.Add(data);
74
76 }
77
78 public Permutations(string key, List<int[]> elems)
79 {
80 string[] s = key.Split('_');
81 N = Int32.Parse(s[0]);
82 K = Int32.Parse(s[1]);
83 Elements = elems;
84 }
85
86 public int[] Successor(int[] previous)
87 {
88 if (previous.Length == 0 || previous[0] == this.N - this.K)
89 return null;
90
91 int[] next = new int[K];
92
93 int i;
94 for (i = 0; i < this.K; ++i)
95 next[i] = previous[i];
96
97 for (i = this.K - 1; i > 0 && next[i] == this.N - this.K + i; --i) { }
98
99 ++next[i];
100
101 for (int j = i; j < this.K - 1; ++j)
102 next[j + 1] = next[j] + 1;
103
104 return next;
105 }
106
108 {
109 int[] last = Elements[0];
110 int[] next = Successor(last);
111 while (next != null)
112 {
113 Elements.Add(next);
114 last = next;
115 next = Successor(last);
116 }
117 }
118
119 public bool Equals(Permutations other)
120 {
121 return (N == other.N && K == other.K);
122 }
123 }
124
125 public static class ChemicalUtils
126 {
127 public static Dictionary<string, ChemicalElement> Chemicals = new Dictionary<string, ChemicalElement>() {
128 {"H", new ChemicalElement("H", "Hydrogen", 1.007825035, 1.00794)},
129 {"C", new ChemicalElement("C", "Carbon", 12.0, 12.0107)},
130 {"N", new ChemicalElement("N", "Nitrogen", 14.003074, 14.0067)},
131 {"O", new ChemicalElement("O", "Oxygen", 15.99491463, 15.9994)},
132 {"p", new ChemicalElement("p", "Proton", 1.007276466812, 1.007276466812)},
133 {"i", new ChemicalElement("i", "C13IsotopeDiff", 1.00335, 1.00335)}
134 };
135
136 public static Dictionary<char, AminoAcid> AminoAcids = new Dictionary<char, AminoAcid>() {
137 {'A', new AminoAcid('A', "Ala", "Alanine", 71.037114, 71.0779)}, //71.03712
138 {'R', new AminoAcid('R', "Arg", "Arginine", 156.101111, 156.1857)}, //156.10112
139 {'N', new AminoAcid('N', "Asn", "Asparagine", 114.042927, 114.1026)}, //114.04293
140 {'D', new AminoAcid('D', "Asp", "Aspartic acid", 115.026943, 115.0874)}, //115.02695
141 {'C', new AminoAcid('C', "Cys", "Cysteine", 103.009185, 103.1429)}, //103.00919
142 {'E', new AminoAcid('E', "Glu", "Glutamic acid", 129.042593, 129.114)}, //129.0426
143 {'Q', new AminoAcid('Q', "Gln", "Glutamine", 128.058578, 128.1292)}, //128.05858
144 {'G', new AminoAcid('G', "Gly", "Glycine", 57.021464, 57.0513)}, //57.02147
145 {'H', new AminoAcid('H', "His", "Histidine", 137.058912, 137.1393)}, //137.05891
146 {'I', new AminoAcid('I', "Ile", "Isoleucine", 113.084064, 113.1576)}, //113.08407
147 {'L', new AminoAcid('L', "Leu", "Leucine", 113.084064, 113.1576)},
148 {'K', new AminoAcid('K', "Lys", "Lysine", 128.094963, 128.1723)}, //128.09497
149 {'M', new AminoAcid('M', "Met", "Methionine", 131.040485, 131.1961)}, //131.0405
150 {'F', new AminoAcid('F', "Phe", "Phenylalanine", 147.068414, 147.1739)}, //147.06842
151 {'P', new AminoAcid('P', "Pro", "Proline", 97.052764, 97.1152)}, //97.05277
152 {'S', new AminoAcid('S', "Ser", "Serine", 87.032028, 87.0773)}, //87.03203
153 {'T', new AminoAcid('T', "Thr", "Threonine", 101.047679, 101.1039)}, //101.04768
154 {'U', new AminoAcid('U', "Sec", "Selenocysteine", 150.95363, 150.0379)},
155 {'W', new AminoAcid('W', "Trp", "Tryptophane", 186.079313, 186.2099)}, //186.07932
156 {'Y', new AminoAcid('Y', "Tyr", "Tyrosine", 163.06332, 163.1733)}, //163.06332
157 {'V', new AminoAcid('V', "Val", "Valine", 99.068414, 99.1311)}, //99.06842
158 {'J', new AminoAcid('J', "LeI", "Leu_Isoleu", 113.084064, 113.1576)},
159 {'O', new AminoAcid('O', "Pyl", "Pyrrolysin", 237.14772677, 237.300713363271)},
160 {'$', new AminoAcid('$', "", "N-Term", 0.0, 0.0)},
161 {'^', new AminoAcid('^', "", "C-Term", 0.0, 0.0)}
162 };
163
164
165 public static Dictionary<string, Permutations> Combinations = new Dictionary<string, Permutations>();
166
167 public static int GetMassIndex(double mass)
168 {
169 return (int)(mass * 10000);
170 }
171
172 public static double GetMassDoubleKey(double moverz)
173 {
174 return Math.Round(moverz, 7);
175 }
176
177 public static double CalculatePeptideMass(string sequence, bool mono)
178 {
179 double pepMass = Chemicals["H"].MonoMass * 2 + Chemicals["O"].MonoMass;
180 if (!mono)
181 pepMass = Chemicals["H"].AvgMass * 2 + Chemicals["O"].AvgMass;
182 for (int i = 0; i < sequence.Length; ++i)
183 {
184 pepMass += AminoAcids[sequence[i]].Mass(mono);
185 }
186 return pepMass;
187 }
188
189 //public static double CalculatePeptideMass(string sequence, bool mono)
190 //{
191 // return CalculatePeptideMass(sequence, 0, sequence.Length, mono);
192 //}
193
194 public static double CalculatePrecursorMassToCharge(int charge, double mass, bool mono)
195 {
196 if (mono)
197 return (mass + charge * Chemicals["p"].MonoMass) / charge;
198 else
199 return (mass + charge * Chemicals["p"].AvgMass) / charge;
200 }
201
202 public static double CalculateUnchargedMass(double mass, int charge, bool mono)
203 {
204 if (mono)
205 return ((mass * charge) - (charge) * Chemicals["p"].MonoMass);
206 else
207 return ((mass * charge) - (charge) * Chemicals["p"].AvgMass);
208 }
209
210 public static Permutations GetAscendingPermutations(int n, int k)
211 {
212 string key = n + "_" + k;
213 lock (Combinations)
214 {
215 if (Combinations.ContainsKey(key))
216 return Combinations[key];
217 else
218 {
219 Permutations m = new Permutations(n, k);
220 lock (Combinations)
221 {
222 if (!Combinations.ContainsKey(key))
223 {
224 Combinations.Add(key, m);
225 }
226 }
227 return m;
228 }
229 }
230 }
231
232 public static double CalculateMassDiffInPPM(double expMass, double theoMass)
233 {
234 double ppm = (Math.Abs(expMass - theoMass) / theoMass) * (1000000.0);
235 return ppm;
236 }
237 }
238}
AminoAcid(char oneLetter, string threeLetter, string name, double monoisotopicMass, double avgMass, bool mono=true)
AminoAcid(char oneLetter, string threeLetter, string name, double monoisotopicMass, double avgMass, double immoniumMass)
ChemicalElement(string name, string fullName, double monoMass, double avgMass)
Permutations(string key, List< int[]> elems)
bool Equals(Permutations other)