-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathSpectrumHandler.cs
More file actions
426 lines (375 loc) · 19.9 KB
/
SpectrumHandler.cs
File metadata and controls
426 lines (375 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
using CompMs.Common.Algorithm.IsotopeCalc;
using CompMs.Common.Algorithm.Scoring;
using CompMs.Common.Components;
using CompMs.Common.DataObj.Property;
using CompMs.Common.Enum;
using CompMs.Common.Extension;
using CompMs.Common.FormulaGenerator.DataObj;
using CompMs.Common.Parser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CompMs.Common.Algorithm.Function {
public class IsotopeTemp {
public int WeightNumber { get; set; }
public double Mz { get; set; }
public double MzClBr { get; set; }
public double Intensity { get; set; }
public int PeakID { get; set; }
}
public sealed class SpectrumHandler {
public static List<SpectrumPeak> GetCombinedSpectrum(
List<SpectrumPeak> peaks1,
List<SpectrumPeak> peaks2,
double bin) {
var peaks = new List<SpectrumPeak>();
var range2Peaks = new Dictionary<int, List<SpectrumPeak>>();
foreach (var peak in peaks1) {
var mass = peak.Mass;
var massframe = (int)(mass / bin);
if (range2Peaks.ContainsKey(massframe))
range2Peaks[massframe].Add(peak);
else
range2Peaks[massframe] = new List<SpectrumPeak>() { peak };
}
foreach (var peak in peaks2) {
var mass = peak.Mass;
var massframe = (int)(mass / bin);
if (range2Peaks.ContainsKey(massframe))
range2Peaks[massframe].Add(peak);
else
range2Peaks[massframe] = new List<SpectrumPeak>() { peak };
}
foreach (var pair in range2Peaks) {
var maxMass = pair.Value.Argmax(n => n.Intensity).Mass;
var sumIntensity = pair.Value.Sum(n => n.Intensity) * 0.5;
peaks.Add(new SpectrumPeak(maxMass, sumIntensity));
}
return peaks;
}
public static List<SpectrumPeak> GetNormalizedPeaks(List<SpectrumPeak> spectrum, double powFactor, double maxValue) {
if (spectrum.Count == 0) return new List<SpectrumPeak>();
var maxIntensity = Math.Pow(spectrum.Max(n => n.Intensity), powFactor);
return spectrum.Select(n => new SpectrumPeak { Mass = n.Mass, Intensity = Math.Pow(n.Intensity, powFactor) / maxIntensity * maxValue }).ToList();
}
public static List<SpectrumPeak> GetNormalizedByTotalIntensityPeaks(List<SpectrumPeak> spectrum) {
var sumIntensity = spectrum.Sum(n => n.Intensity);
return spectrum.Select(n => new SpectrumPeak { Mass = n.Mass, Intensity = n.Intensity / sumIntensity }).ToList();
}
public static List<SpectrumPeak> GetBinnedSpectrum(List<SpectrumPeak> spectrum, double delta = 100, int maxPeaks = 12) {
var peaks = new List<SpectrumPeak>();
var range2Peaks = new Dictionary<int, List<SpectrumPeak>>();
foreach (var peak in spectrum) {
var mass = peak.Mass;
var massframe = (int)(mass / delta);
if (range2Peaks.ContainsKey(massframe))
range2Peaks[massframe].Add(peak);
else
range2Peaks[massframe] = new List<SpectrumPeak>() { peak };
}
foreach (var pair in range2Peaks) {
var counter = 1;
foreach (var peak in pair.Value.OrderByDescending(n => n.Intensity)) {
if (counter > maxPeaks) break;
peaks.Add(peak);
counter++;
}
}
return peaks;
}
public static List<SpectrumPeak> GetBinnedSpectrum(List<SpectrumPeak> spectrum, double bin) {
var peaks = new List<SpectrumPeak>();
var range2Peaks = new Dictionary<int, List<SpectrumPeak>>();
foreach (var peak in spectrum) {
var mass = peak.Mass;
var massframe = (int)(mass / bin);
if (range2Peaks.ContainsKey(massframe))
range2Peaks[massframe].Add(peak);
else
range2Peaks[massframe] = new List<SpectrumPeak>() { peak };
}
foreach (var pair in range2Peaks) {
var maxMass = pair.Value.Argmax(n => n.Intensity).Mass;
var sumIntensity = pair.Value.Sum(n => n.Intensity);
peaks.Add(new SpectrumPeak(maxMass, sumIntensity));
}
return peaks;
}
public static List<SpectrumPeak> GetNormalizedPeak4SpectralEntropyCalc(
List<SpectrumPeak> peaklist,
double precursorMz,
double ms2Tol = 0.05,
double relativeAbundanceCutOff = 0.1,
double absoluteAbundanceCutOff = 3,
double minMz = 0,
double maxMz = 100000) {
if (peaklist == null || peaklist.Count == 0) return new List<SpectrumPeak>();
double maxIntensity = peaklist.Max(n => n.Intensity);
var refinedPeaklist = new List<SpectrumPeak>();
foreach (var peak in peaklist) {
if (peak.Mass < minMz) continue;
if (peak.Mass > maxMz) continue;
if (peak.Mass > precursorMz + ms2Tol) continue;
if (peak.Intensity < absoluteAbundanceCutOff) continue;
if (peak.Intensity >= maxIntensity * relativeAbundanceCutOff * 0.01) {
refinedPeaklist.Add(peak);
}
}
var sumIntensity = refinedPeaklist.Sum(n => n.Intensity);
return refinedPeaklist.Select(n => new SpectrumPeak() { Mass = n.Mass, Intensity = n.Intensity / sumIntensity }).ToList();
}
public static List<SpectrumPeak> GetNormalizedPeak4SpectralEntropySimilarityCalc(
List<SpectrumPeak> peaklist,
double precursorMz,
double ms2Tol = 0.05,
double relativeAbundanceCutOff = 0.1,
double absoluteAbundanceCutOff = 3,
double minMz = 0,
double maxMz = 100000) {
if (peaklist == null || peaklist.Count == 0) return new List<SpectrumPeak>();
double maxIntensity = peaklist.Max(n => n.Intensity);
var refinedPeaklist = new List<SpectrumPeak>();
foreach (var peak in peaklist) {
if (peak.Mass < minMz) continue;
if (peak.Mass > maxMz) continue;
if (peak.Mass > precursorMz + ms2Tol) continue;
if (peak.Intensity < absoluteAbundanceCutOff) continue;
if (peak.Intensity >= maxIntensity * relativeAbundanceCutOff * 0.01) {
refinedPeaklist.Add(peak);
}
}
var entropy = MsScanMatching.GetSpectralEntropy(refinedPeaklist);
if (entropy < 3) {
foreach (var peak in refinedPeaklist) {
peak.Intensity = Math.Pow(peak.Intensity, 0.25 + entropy * 0.25);
}
}
var sumIntensity = refinedPeaklist.Sum(n => n.Intensity);
return refinedPeaklist.Select(n => new SpectrumPeak() { Mass = n.Mass, Intensity = n.Intensity / sumIntensity }).ToList();
}
public static List<SpectrumPeak> GetRefinedPeaklist(
List<SpectrumPeak> peaklist,
double relativeAbundanceCutOff,
double absoluteAbundanceCutOff,
double minMz,
double maxMz,
double precursorMz,
double ms2Tol,
MassToleranceType massTolType,
int precursorCharge,
bool isBrClConsideredForIsotopes = false,
bool isRemoveIsotopes = false,
bool removeAfterPrecursor = true) {
if (peaklist == null || peaklist.Count == 0) return new List<SpectrumPeak>();
double maxIntensity = peaklist.Max(n => n.Intensity);
var refinedPeaklist = new List<SpectrumPeak>();
foreach (var peak in peaklist) {
if (peak.Mass < minMz) continue;
if (peak.Mass > maxMz) continue;
if (removeAfterPrecursor && peak.Mass > precursorMz + ms2Tol) continue;
if (peak.Intensity < absoluteAbundanceCutOff) continue;
if (peak.Intensity >= maxIntensity * relativeAbundanceCutOff * 0.01) {
refinedPeaklist.Add(new SpectrumPeak() { Mass = peak.Mass, Intensity = peak.Intensity, Comment = string.Empty });
}
}
if (isRemoveIsotopes) {
EstimateIsotopes(refinedPeaklist, ms2Tol, isBrClConsideredForIsotopes, precursorCharge);
return refinedPeaklist.Where(n => n.IsotopeWeightNumber == 0).ToList();
}
else {
return refinedPeaklist;
}
}
/// <summary>
/// peak list must be sorted by m/z (ordering)
/// peak should be initialized by new Peak() { Mz = spec[0], Intensity = spec[1], Charge = 1, IsotopeFrag = false }
/// </summary>
public static void EstimateIsotopes(List<SpectrumPeak> peaks, double mztolerance, bool isBrClConsideredForIsotopes = false, int maxChargeNumber = 0) {
var c13_c12Diff = MassDiffDictionary.C13_C12; //1.003355F;
var br81_br79 = MassDiffDictionary.Br81_Br79; //1.9979535; also to be used for S34_S32 (1.9957959), Cl37_Cl35 (1.99704991)
var tolerance = mztolerance;
for (int i = 0; i < peaks.Count; i++) {
var peak = peaks[i];
peak.PeakID = i;
if (peak.IsotopeWeightNumber >= 0) continue;
peak.IsotopeWeightNumber = 0;
peak.IsotopeParentPeakID = i;
// charge state checking at M + 1
var predChargeNumber = 1;
for (int j = i + 1; j < peaks.Count; j++) {
var isotopePeak = peaks[j];
if (isotopePeak.Mass > peak.Mass + c13_c12Diff + tolerance) break;
if (isotopePeak.IsotopeWeightNumber >= 0) continue;
for (int k = maxChargeNumber; k >= 1; k--) {
var predIsotopeMass = (double)peak.Mass + (double)c13_c12Diff / (double)k;
var diff = Math.Abs(predIsotopeMass - isotopePeak.Mass);
if (diff < tolerance) {
predChargeNumber = k;
if (k <= 3) {
break;
}
else if (k == 4 || k == 5) {
var predNextIsotopeMass = (double)peak.Mass + (double)c13_c12Diff / (double)(k - 1);
var nextDiff = Math.Abs(predNextIsotopeMass - isotopePeak.Mass);
if (diff > nextDiff) predChargeNumber = k - 1;
break;
}
else if (k >= 6) {
var predNextIsotopeMass = (double)peak.Mass + (double)c13_c12Diff / (double)(k - 1);
var nextDiff = Math.Abs(predNextIsotopeMass - isotopePeak.Mass);
if (diff > nextDiff) {
predChargeNumber = k - 1;
diff = nextDiff;
predNextIsotopeMass = (double)peak.Mass + (double)c13_c12Diff / (double)(k - 2);
nextDiff = Math.Abs(predNextIsotopeMass - isotopePeak.Mass);
if (diff > nextDiff) {
predChargeNumber = k - 2;
diff = nextDiff;
}
}
break;
}
}
}
if (predChargeNumber != 1) break;
}
peak.Charge = predChargeNumber;
// isotope grouping till M + 8
var maxTraceNumber = 15;
var isotopeTemps = new IsotopeTemp[maxTraceNumber + 1];
isotopeTemps[0] = new IsotopeTemp() {
WeightNumber = 0,
Mz = peak.Mass, Intensity = peak.Intensity, PeakID = i, MzClBr = peak.Mass
};
for (int j = 1; j < isotopeTemps.Length; j++) {
isotopeTemps[j] = new IsotopeTemp() {
WeightNumber = j, Mz = peak.Mass + (double)j * c13_c12Diff / (double)predChargeNumber,
MzClBr = j % 2 == 0 ? peak.Mass + (double)j * c13_c12Diff / (double)predChargeNumber : peak.Mass + (double)j * br81_br79 * 0.5 / (double)predChargeNumber,
Intensity = 0, PeakID = -1
};
}
var reminderIndex = i + 1;
var isFinished = false;
var mzFocused = peak.Mass;
for (int j = 1; j <= maxTraceNumber; j++) {
var predIsotopicMass = mzFocused + (double)c13_c12Diff / (double)predChargeNumber;
var predClBrIsotopicMass = mzFocused + (double)br81_br79 * 0.5 / (double)predChargeNumber;
for (int k = reminderIndex; k < peaks.Count; k++) {
var isotopePeak = peaks[k];
if (isotopePeak.IsotopeWeightNumber >= 0) continue;
var isotopeMz = isotopePeak.Mass;
var diffMz = Math.Abs(predIsotopicMass - isotopeMz);
var diffMzClBr = Math.Abs(predClBrIsotopicMass - isotopeMz);
if (diffMz < tolerance) {
if (isotopeTemps[j].PeakID == -1) {
isotopeTemps[j] = new IsotopeTemp() {
WeightNumber = j, Mz = isotopeMz,
Intensity = isotopePeak.Intensity, PeakID = k
};
mzFocused = isotopeMz;
}
else {
if (Math.Abs(isotopeTemps[j].Mz - predIsotopicMass) > Math.Abs(isotopeMz - predIsotopicMass)) {
isotopeTemps[j].Mz = isotopeMz;
isotopeTemps[j].Intensity = isotopePeak.Intensity;
isotopeTemps[j].PeakID = k;
mzFocused = isotopeMz;
}
}
}
else if (isBrClConsideredForIsotopes && j % 2 == 0 && diffMzClBr < tolerance) {
if (isotopeTemps[j].PeakID == -1) {
isotopeTemps[j] = new IsotopeTemp() {
WeightNumber = j, Mz = isotopeMz, MzClBr = isotopeMz,
Intensity = isotopePeak.Intensity, PeakID = k
};
mzFocused = isotopeMz;
}
else {
if (Math.Abs(isotopeTemps[j].Mz - predIsotopicMass) > Math.Abs(isotopeMz - predIsotopicMass)) {
isotopeTemps[j].Mz = isotopeMz;
isotopeTemps[j].MzClBr = isotopeMz;
isotopeTemps[j].Intensity = isotopePeak.Intensity;
isotopeTemps[j].PeakID = k;
mzFocused = isotopeMz;
}
}
}
else if (isotopePeak.Mass >= predIsotopicMass + tolerance) {
if (k == peaks.Count - 1) break;
reminderIndex = k;
if (isotopeTemps[j - 1].PeakID == -1 && isotopeTemps[j].PeakID == -1) {
isFinished = true;
}
else if (isotopeTemps[j].PeakID == -1) {
mzFocused += (double)c13_c12Diff / (double)predChargeNumber;
}
break;
}
}
if (isFinished)
break;
}
// finalize and store
var monoisotopicMass = (double)peak.Mass * (double)predChargeNumber;
var simulatedFormulaByAlkane = getSimulatedFormulaByAlkane(monoisotopicMass);
//from here, simple decreasing will be expected for <= 800 Da
//simulated profiles by alkane formula will be projected to the real abundances for the peaks of more than 800 Da
IsotopeProperty simulatedIsotopicPeaks = null;
var isIsotopeDetected = false;
var iupac = IupacResourceParser.GetIupacCHData();
if (monoisotopicMass > 800)
simulatedIsotopicPeaks = IsotopeCalculator.GetNominalIsotopeProperty(simulatedFormulaByAlkane, maxTraceNumber + 1, iupac);
for (int j = 1; j <= maxTraceNumber; j++) {
if (isotopeTemps[j].PeakID == -1) continue;
if (isotopeTemps[j - 1].PeakID == -1 && isotopeTemps[j].PeakID == -1) break;
if (monoisotopicMass <= 800) {
if (isotopeTemps[j - 1].Intensity > isotopeTemps[j].Intensity && isBrClConsideredForIsotopes == false) {
peaks[isotopeTemps[j].PeakID].IsotopeParentPeakID = peak.PeakID;
peaks[isotopeTemps[j].PeakID].IsotopeWeightNumber = j;
peaks[isotopeTemps[j].PeakID].Charge = peak.Charge;
isIsotopeDetected = true;
}
else if (isBrClConsideredForIsotopes == true) {
peaks[isotopeTemps[j].PeakID].IsotopeParentPeakID = peak.PeakID;
peaks[isotopeTemps[j].PeakID].IsotopeWeightNumber = j;
peaks[isotopeTemps[j].PeakID].Charge = peak.Charge;
isIsotopeDetected = true;
}
else {
break;
}
}
else {
if (isotopeTemps[j - 1].Intensity <= 0) break;
var expRatio = isotopeTemps[j].Intensity / isotopeTemps[j - 1].Intensity;
var simRatio = simulatedIsotopicPeaks.IsotopeProfile[j].RelativeAbundance / simulatedIsotopicPeaks.IsotopeProfile[j - 1].RelativeAbundance;
if (Math.Abs(expRatio - simRatio) < 5.0) {
peaks[isotopeTemps[j].PeakID].IsotopeParentPeakID = peak.PeakID;
peaks[isotopeTemps[j].PeakID].IsotopeWeightNumber = j;
peaks[isotopeTemps[j].PeakID].Charge = peak.Charge;
isIsotopeDetected = true;
}
else {
break;
}
}
}
if (!isIsotopeDetected) {
peak.Charge = 1;
}
}
}
private static string getSimulatedFormulaByAlkane(double mass) {
var ch2Mass = 14.0;
var carbonCount = (int)(mass / ch2Mass);
var hCount = (int)(carbonCount * 2);
if (carbonCount == 0 || carbonCount == 1)
return "CH2";
else {
return "C" + carbonCount.ToString() + "H" + hCount.ToString();
}
}
}
}