-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBullseyeSharp.cs
600 lines (540 loc) · 23.9 KB
/
BullseyeSharp.cs
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Permissions;
using pwiz.CLI.msdata;
using pwiz.CLI.data;
namespace BullseyeSharp
{
struct sPrecursor
{
public int charge;
public double monoMz;
}
class sScanInfo
{
public int msLevel;
public int scanNumber;
public double mz;
public float rTime;
public sPrecursor[] precursor;
public sScanInfo()
{
precursor = new sPrecursor[100];
}
}
class sBullseyeMatch
{
public int index;
public int scanNumber;
}
class BullseyeSharp
{
//double mean, stD;
static double ppmTolerance;
static double rtTolerance;
static bool bMatchPrecursorOnly;
static string sHKFile;
static string sDataFile;
static string sPosFile;
static string sNegFile;
static string sSumFile;
static string sID = "BullseyeSharp v1.32";
static string sDate = "Apr 21 2022";
static void Main(string[] args)
{
CKronik2 p1 = new CKronik2();
Console.WriteLine(sID+", "+sDate);
Console.WriteLine("Copyright 2008-2022 Mike Hoopmann, Ed Hsieh, Mike MacCoss");
Console.WriteLine("University of Washington");
//Set global variables
ppmTolerance = 10.0;
rtTolerance = 0.5;
bMatchPrecursorOnly = false;
sSumFile = "";
//Set default parameters for some options
double contam = 2.0;
double maxMass = 8000.0;
double minMass = 600.0;
if (args.Count() < 4)
{
usage();
return;
}
int fcount = 0;
for(int i = 0; i < args.Count(); i++)
{
if (args[i].CompareTo("-c") == 0)
{
contam = Convert.ToDouble(args[++i]);
}
else if (args[i].CompareTo("-e") == 0)
{
bMatchPrecursorOnly = true;
}
else if (args[i].CompareTo("-g") == 0)
{
p1.setGapTol(Convert.ToInt32(args[++i]));
}
else if (args[i].CompareTo("-m") == 0)
{
maxMass=Convert.ToDouble(args[++i]);
}
else if (args[i].CompareTo("-n") == 0)
{
minMass = Convert.ToDouble(args[++i]);
}
else if (args[i].CompareTo("-o") == 0)
{
sSumFile = args[++i];
}
else if (args[i].CompareTo("-p") == 0)
{
ppmTolerance = Convert.ToDouble(args[++i]);
}
else if (args[i].CompareTo("-r") == 0)
{
p1.setPPMTol(Convert.ToDouble(args[++i]));
}
else if (args[i].CompareTo("-s") == 0)
{
p1.setMatchTol(Convert.ToInt32(args[++i]));
}
else if (args[i].CompareTo("-t") == 0)
{
rtTolerance = Convert.ToDouble(args[++i]);
}
else if (args[i][0] == '-')
{
Console.WriteLine("\nInvalid flag: " + args[i] + "\n\n");
usage();
return;
} else
{
if (fcount == 0) sHKFile = args[i];
else if (fcount == 1) sDataFile = args[i];
else if (fcount == 2) sPosFile = args[i];
else if (fcount == 3) sNegFile = args[i];
else
{
Console.WriteLine("\nInvalid parameter: " + args[i] + "\n\n");
usage();
return;
}
fcount++;
}
}
p1.processHK(sHKFile,sHKFile+".bs.kro");
//Remove contaminants
p1.removeContam(contam);
Console.WriteLine("Persistent peptides after removing contaminants: " + p1.size());
//Filter based on size
p1.removeMass(minMass, maxMass);
Console.WriteLine("Persistent peptides from " + minMass + " to " + maxMass + ": " + p1.size());
matchMS2(ref p1, sDataFile, sPosFile, sNegFile);
}
static string averagine(double mass)
{
string st="";
int aa = (int)(mass / 111.2137);
int c = (int)(aa * 4.9558 + 0.5);
int h = (int)(aa * 7.8241 + 0.5);
int n = (int)(aa * 1.3571 + 0.5);
int o = (int)(aa * 1.4716 + 0.5);
int s = (int)(aa * 0.0390 + 0.5);
double tot = c * 12 + h * 1.007825 + n * 14.003074 + o * 15.9949141 + s * 31.97207;
h += (int)((mass - tot) / 1.007285 + 0.5);
st = "C" + c.ToString() + "H" + h.ToString() + "N" + n.ToString() + "O" + o.ToString();
if (s > 0) st += "S" + s.ToString();
return st;
}
static void matchMS2(ref CKronik2 p, string ms2File, string outFile, string outFile2)
{
Spectrum s;
MSDataFile.WriteConfig wcPos = getFileFormat(outFile);
MSDataFile.WriteConfig wcNeg = getFileFormat(outFile2);
SpectrumListSimple slPos = new SpectrumListSimple();
SpectrumListSimple slNeg = new SpectrumListSimple();
int i, j;
int[] lookup = new int[8001];
double lowMass, highMass, ppm;
int x, z;
int a, b;
int c = 0;
int d = 0;
int iPercent = 0;
int index;
List<int> vI = new List<int>();
List<int> vHit = new List<int>();
List<sBullseyeMatch> vMatch = new List<sBullseyeMatch>();
int[] ch = new int[10];
//Check file formats for output. Make sure the user specifies the appropriate format
if (wcPos.format == MSDataFile.Format.Format_Text)
{
Console.WriteLine("Output file format (pos set) not acceptable. Choose a different format.");
return;
}
if (wcNeg.format == MSDataFile.Format.Format_Text)
{
Console.WriteLine("Output file format (neg set) not acceptable. Choose a different format.");
return;
}
//Hardklor results are sorted and indexed to improve speed of Bullseye
Console.Write("Sorting Hardklor results...");
p.sortBasePeak();
Console.WriteLine("Done!");
Console.Write("Building lookup table...");
j = 0;
for (i = 0; i < 8001; i++)
{
while (p[j].basePeak < i)
{
if (j >= p.size() - 1) break;
j++;
}
lookup[i] = j;
}
Console.WriteLine("Done!");
//Read in the data
Console.Write("Matching MS/MS..." + iPercent);
b = 0;
z = 0;
a = 0;
MSDataFile msd = new MSDataFile(sDataFile);
Software sw = new Software("BullseyeSharp", new CVParam(pwiz.CLI.cv.CVID.MS_analysis_software), "1.32");
msd.softwareList.Add(sw);
SpectrumList sl = msd.run.spectrumList;
for (int sc = 0; sc < sl.size(); sc++)
{
s = sl.spectrum(sc, true);
sScanInfo scanInfo = processScanHeader(ref s);
if (scanInfo.msLevel != 2) continue;
j = (int)(scanInfo.mz + 0.5);
x = 0;
vHit.Clear();
//see if we can pick it up on base peak alone
for (i = lookup[j - 1]; i <= lookup[j + 1]; i++)
{
ppm = (p[i].basePeak - scanInfo.mz) / scanInfo.mz * 1000000;
if (Math.Abs(ppm) < ppmTolerance &&
scanInfo.rTime > p[i].firstRTime - rtTolerance &&
scanInfo.rTime < p[i].lastRTime + rtTolerance)
{
x++;
index = i;
vHit.Add(i);
}
}
//if base peak wasn't enough, perhaps a different peak was isolated
if (!bMatchPrecursorOnly)
{
for (i = 0; i < p.size(); i++)
{
lowMass = (p[i].monoMass + p[i].charge * 1.00727649) / p[i].charge - 0.05;
if (p[i].charge == 1) highMass = (p[i].monoMass + p[i].charge * 1.00727649) / p[i].charge + 3.10;
else if (p[i].charge == 2) highMass = (p[i].monoMass + p[i].charge * 1.00727649) / p[i].charge + 2.10;
else highMass = (p[i].monoMass + p[i].charge * 1.00727649) / p[i].charge + 4 / p[i].charge + 0.05;
if (scanInfo.mz > lowMass && scanInfo.mz < highMass &&
scanInfo.rTime > p[i].firstRTime - rtTolerance &&
scanInfo.rTime < p[i].lastRTime + rtTolerance)
{
x++;
index = i;
vHit.Add(i);
}
}
}
vI.Add(x);
if (x == 0)
{
z++;
slNeg.spectra.Add(s);
slNeg.spectra.Last().index = slNeg.spectra.Count - 1;
ch[0]++;
}
else if (x == 1)
{
a++;
slPos.spectra.Add(s);
slPos.spectra.Last().index = slPos.spectra.Count - 1;
slPos.spectra.Last().userParams.Clear();
slPos.spectra.Last().userParams.Add(new UserParam("ms2 file charge state", p[vHit[0]].charge.ToString() + " " + (p[vHit[0]].monoMass + 1.00727649).ToString()));
slPos.spectra.Last().precursors[0].selectedIons.Clear();
SelectedIon si = new SelectedIon();
si.set(pwiz.CLI.cv.CVID.MS_charge_state, p[vHit[0]].charge);
si.set(pwiz.CLI.cv.CVID.MS_accurate_mass_OBSOLETE, p[vHit[0]].monoMass + 1.00727649);
si.set(pwiz.CLI.cv.CVID.MS_selected_ion_m_z, p[vHit[0]].monoMass + 1.00727649);
slPos.spectra.Last().precursors[0].selectedIons.Add(si);
c++;
}
else
{
//Add spectra and erase original precursor estimates
slPos.spectra.Add(s);
slPos.spectra.Last().index = slPos.spectra.Count - 1;
slPos.spectra.Last().userParams.Clear();
slPos.spectra.Last().precursors[0].selectedIons.Clear();
//erase redundancies in multiple hit list
for (i = 0; i < vHit.Count - 1; i++)
{
for (j = i + 1; j < vHit.Count; j++)
{
if (p[vHit[i]].charge == p[vHit[j]].charge)
{
string str1 = Convert.ToString(Math.Round(p[vHit[i]].monoMass + 1.00727649, 2));
string str2 = Convert.ToString(Math.Round(p[vHit[j]].monoMass + 1.00727649, 2));
if (str1.CompareTo(str2) == 0)
{
if (p[vHit[i]].intensity < p[vHit[j]].intensity) vHit[i] = vHit[j];
vHit.RemoveAt(j--);
}
}
}
}
for (i = 0; i < vHit.Count; i++)
{
slPos.spectra.Last().userParams.Add(new UserParam("ms2 file charge state", p[vHit[i]].charge.ToString() + " " + (p[vHit[i]].monoMass + 1.00727649).ToString()));
SelectedIon si = new SelectedIon();
si.set(pwiz.CLI.cv.CVID.MS_charge_state, p[vHit[i]].charge);
si.set(pwiz.CLI.cv.CVID.MS_accurate_mass_OBSOLETE, p[vHit[i]].monoMass + 1.00727649);
si.set(pwiz.CLI.cv.CVID.MS_selected_ion_m_z, p[vHit[i]].monoMass + 1.00727649);
slPos.spectra.Last().precursors[0].selectedIons.Add(si);
}
if (vHit.Count == 1)
{
a++;
c++;
}
else
{
b++;
d += vHit.Count;
}
}
for (i = 0; i < vHit.Count; i++) ch[p[vHit[i]].charge]++;
//export matched features
for (i = 0; i < vHit.Count; i++)
{
vMatch.Add(new sBullseyeMatch());
vMatch.Last().index = vHit[i];
vMatch.Last().scanNumber = scanInfo.scanNumber;
}
//Update file position counter
if (sc * 100 / sl.size() > iPercent)
{
if (iPercent < 10) Console.Write("\b");
else Console.Write("\b\b");
iPercent = sc * 100 / sl.size();
Console.Write(iPercent);
}
}
Console.WriteLine(" Done!");
Console.WriteLine(z + " scans had no visible parental distribution.");
Console.WriteLine(a + " scans had a single parental distribution.");
Console.WriteLine(b + " scans had multiple possible parental distributions.");
//Some simple tables of where parental ions are found relative to scan number
i = 0;
a = 0;
x = 0;
z = 0;
b = 1;
Console.WriteLine("\nScan:\tNeg\tPos");
while (i < vI.Count)
{
if (vI[i] == 0) x++;
else z++;
a++;
if (a == 1000)
{
Console.WriteLine(b * a + "\t" + x + "\t" + z);
x = 0;
z = 0;
b++;
a = 0;
}
i++;
}
Console.WriteLine(b * 1000 + a + "\t" + x + "\t" + z);
Console.WriteLine("\nUnknown charge: " + ch[0]);
for (i = 1; i < 10; i++) Console.WriteLine("+" + i + ": " + ch[i]);
Console.WriteLine(c + " singles and " + d + " doubles total.");
vMatch.Sort((v1, v2) =>
{
int r = v1.index.CompareTo(v2.index);
if (r == 0) r = v1.scanNumber.CompareTo(v2.scanNumber);
return r;
}
);
if (sSumFile.Length > 0)
{
StreamWriter f = new StreamWriter(sSumFile);
//Preamble
f.Write(sID + "\nHarklor input: " + sHKFile + "\nMS/MS input file: " + sDataFile + "\nMS/MS spectra with new precursors: " + outFile + "\nRemaining spectra: " + outFile2 + "\n");
//Heading line
f.Write("MonoisotopicMass\tz\tCharge\tStartRT\tEndRT\tApexRT\tApexAbundance\tTotalAbundance\tAveragine\tMS2Scans\n");
for (i = 0; i < vMatch.Count; i++)
{
f.Write(p[vMatch[i].index].monoMass + "\t");
f.Write(p[vMatch[i].index].charge + "\t");
f.Write("[M+"+p[vMatch[i].index].charge+"H]" + "\t");
f.Write(p[vMatch[i].index].firstRTime + "\t");
f.Write(p[vMatch[i].index].lastRTime + "\t");
f.Write(p[vMatch[i].index].rTime + "\t");
f.Write(p[vMatch[i].index].intensity + "\t");
f.Write(p[vMatch[i].index].sumIntensity + "\t");
f.Write(averagine(p[vMatch[i].index].monoMass) + "\t");
f.Write(vMatch[i].scanNumber);
while (i < (vMatch.Count - 1) && vMatch[i + 1].index == vMatch[i].index)
{
i++;
f.Write(";" + vMatch[i].scanNumber);
}
f.Write("\n");
}
f.Close();
}
//export files
msd.run.spectrumList = slNeg;
MSDataFile.write(msd, outFile2, wcNeg);
msd.run.spectrumList = slPos;
MSDataFile.write(msd, outFile, wcPos);
Console.WriteLine("Done export");
}
static MSDataFile.WriteConfig getFileFormat(string s)
{
MSDataFile.WriteConfig wc = new MSDataFile.WriteConfig();
string[] tokens = s.Split('.');
wc.format = MSDataFile.Format.Format_Text;
wc.compression = MSDataFile.Compression.Compression_Zlib;
wc.gzipped = false;
wc.indexed = true;
wc.useWorkerThreads = false;
for (int i = tokens.Length - 1; i > 0; i--)
{
string st = tokens[i].ToLower();
switch (st)
{
case "ms2":
wc.format = MSDataFile.Format.Format_MS2;
return wc;
case "cms2":
wc.format = MSDataFile.Format.Format_CMS2;
return wc;
case "mgf":
wc.format = MSDataFile.Format.Format_MGF;
return wc;
case "mzxml":
wc.format = MSDataFile.Format.Format_mzXML;
return wc;
case "mzml":
wc.format = MSDataFile.Format.Format_mzML;
return wc;
case "gz":
wc.gzipped = true;
continue;
default:
return wc;
}
}
return wc;
}
static sScanInfo processScanHeader(ref Spectrum s)
{
sScanInfo si = new sScanInfo();
int pIndex = 0;
//find scan number...currently best for Thermo data.
string[] tokens = s.id.Split(' ', '\n', '\r');
foreach(string st in tokens)
{
if (st.Contains("scan="))
{
si.scanNumber = Convert.ToInt32(st.Substring(5));
break;
}
}
foreach (CVParam cv in s.cvParams)
{
if (cv.name.CompareTo("ms level") == 0) si.msLevel = (int)cv.value;
}
foreach (UserParam cv in s.userParams)
{
//if (cv.name.CompareTo("ms level") == 0) si.msLevel = (int)cv.value;
}
foreach(Scan sc in s.scanList.scans)
{
foreach (CVParam cv in sc.cvParams)
{
if (cv.name.CompareTo("scan start time") == 0)
{
if (cv.unitsName == "minute") si.rTime = (float)cv.value;
else si.rTime = (float)cv.value / 60; //assuming if not minutes, then seconds.
}
}
}
foreach (Precursor pr in s.precursors)
{
foreach (CVParam cv in pr.isolationWindow.cvParams)
{
if (cv.name.CompareTo("isolation window target m/z") == 0) si.mz = (double)cv.value;
}
foreach(SelectedIon sel in pr.selectedIons)
{
foreach (CVParam cv in sel.cvParams)
{
if (cv.name.CompareTo("selected ion m/z") == 0) si.precursor[pIndex].monoMz = (double)cv.value;
if (cv.name.CompareTo("charge state") == 0) si.precursor[pIndex].charge= (int)cv.value;
}
pIndex++;
}
}
return si;
}
static void usage()
{
Console.WriteLine("Usage: bullseyeSharp [flags] <HK file> <Data file> <Pos file> <Neg file>");
Console.WriteLine("\n HK files are Hardklör generated results files.");
Console.WriteLine(" http://proteome.gs.washington.edu/software/hardklor");
Console.WriteLine("\n Data files contain the MS/MS data to be used.");
Console.WriteLine(" Acceptable formats:");
Console.WriteLine(" Any ProteoWizard supported MS format (e.g. mzML, ms2, mgf, and vendor formats)");
Console.WriteLine(" http://proteome.gs.washington.edu/software/makems2/MakeMS2.zip");
Console.WriteLine("\n Pos and Neg files are output files for matches and non-matches, respectively.");
Console.WriteLine(" Acceptable formats (specify with file extension):");
Console.WriteLine(" .ms2, .cms2, .mgf, .mzXML, .mzML");
Console.WriteLine("\nExample: bullseyeSharp -p 5 Peptides.hk RawData.RAW Matches.ms2 NoMatch.ms2");
Console.WriteLine("\nFlags:");
Console.WriteLine(" -c <num> Ignore peptides that persist for this length in time.\n" +
" These peptides are considered contaminants.\n" +
" The unit of time is whatever unit is used in your data file.\n" +
" Default value: 2\n");
Console.WriteLine(" -e Use exact match to precursor ion. Rather than use wide\n" +
" precursor boundaries, this flag forces Bullseye to match\n" +
" precursors to the base isotope peak identified in Hardklor.\n" +
" The tolerance is set with the -p flag.\n");
Console.WriteLine(" -g <num> Gap size tolerance when checking for peptides across consecutive\n" +
" scans.\n" +
" Default value: 1\n");
Console.WriteLine(" -m <num> Only consider peptides below this maximum mass in daltons.\n" +
" Default value: 8000\n");
Console.WriteLine(" -n <num> Only consider peptides above this minimum mass in daltons.\n" +
" Default value: 600\n");
Console.WriteLine(" -o <file> Output tab-delimited text summary of Peptide to MS2 matches.\n");
Console.WriteLine(" -p <num> Sets the tolerance (+/- ppm) for exact match searches.\n" +
" Default value: 10\n");
Console.WriteLine(" -r <num> Sets the tolerance (+/- ppm) for finding persistent peptides.\n" +
" Default value: 5\n");
Console.WriteLine(" -s <num> Number of consecutive scans over which a peptide must be\n" +
" observed to be considered real. Gaps in persistence are allowed\n" +
" when setting the -g flag.\n" +
" Default value: 3\n");
Console.WriteLine(" -t <num> Sets the tolerance (+/- minutes) around the retention\n" +
" time over which a peptide can be matched to the MS/MS\n" +
" spectrum.\n" +
" Default value: 0.5\n");
Console.WriteLine("\nPlease read the README.txt file for more information on Bullseye.");
}
}
}