-
Notifications
You must be signed in to change notification settings - Fork 0
/
MData.cpp
2529 lines (2229 loc) · 70 KB
/
MData.cpp
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2014, Michael R. Hoopmann, Institute for Systems Biology
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "MData.h"
using namespace std;
using namespace MSToolkit;
Mutex MData::mutexMemoryPool;
bool* MData::memoryPool;
double** MData::tempRawData;
double** MData::tmpFastXcorrData;
float** MData::fastXcorrData;
mPreprocessStruct** MData::preProcess;
mParams* MData::params;
MLog* MData::mlog;
deque<Spectrum*> MData::dMS1;
vector<Spectrum*> MData::vMS1Buffer;
Mutex MData::mutexLockMS1;
CHardklor2** MData::h;
CAveragine** MData::averagine;
CMercury8** MData::mercury;
CModelLibrary* MData::models;
Mutex* MData::mutexHardklor;
CHardklorSetting MData::hs;
bool* MData::bHardklor;
int MData::maxPrecursorMass;
/*============================
Constructors
============================*/
MData::MData(){
bScans=NULL;
params=NULL;
for(int i=0;i<128;i++) adductSite[i]=false;
pepXMLindex=0;
}
MData::MData(mParams* p){
bScans=NULL;
params=p;
size_t i;
for(i=0;i<p->fMods.size();i++) aa.addFixedMod((char)p->fMods[i].index,p->fMods[i].mass);
for (i = 0; i<128; i++) adductSite[i] = false;
pepXMLindex = 0;
}
MData::~MData(){
params=NULL;
parObj=NULL;
if(bScans!=NULL) delete[] bScans;
}
/*============================
Operators
============================*/
MSpectrum& MData::operator [](const int& i){
return *spec[i];
}
/*============================
Functions
============================*/
bool MData::createDiag(FILE*& f){
string fName=params->outFile;
fName+=".diag.xml";
f = fopen(fName.c_str(), "wt");
if (f == NULL) {
cout << "ERROR: Cannot open: " << fName << endl;
return false;
}
return true;
}
NeoPepXMLParser* MData::createPepXML(string& str, MDatabase& db){
str = params->outFile;
str += ".pep.xml";
FILE* f = fopen(str.c_str(), "wt");
if (f == NULL) {
cout << "ERROR: Cannot open: " << str << endl;
return NULL;
}
fclose(f);
NeoPepXMLParser* p = new NeoPepXMLParser();
CnpxMSMSPipelineAnalysis pa;
time_t timeNow;
time(&timeNow);
tm local_tm = *localtime(&timeNow);
pa.date.date.year = local_tm.tm_year+1900;
pa.date.date.month = local_tm.tm_mon + 1;
pa.date.date.day = local_tm.tm_mday;
pa.date.time.hour = local_tm.tm_hour;
pa.date.time.minute = local_tm.tm_min;
pa.date.time.second = local_tm.tm_sec;
pa.summary_xml=str;
CnpxMSMSRunSummary rs;
rs.base_name=params->inFileNoExt; //params->inFile.substr(0,params->inFile.find_last_of('.'));
rs.raw_data_type="raw";
rs.raw_data=params->ext;
CnpxSampleEnzyme se;
se.name = params->enzymeName;
CnpxSpecificity sp;
mEnzymeRules enzyme = db.getEnzymeRules();
for (int i = 65; i < 90; i++){
if (enzyme.cutC[i]) sp.cut += (char)i;
if (enzyme.exceptN[i]) sp.no_cut += (char)i;
}
if (sp.cut.size()>0) sp.sense = "C";
else {
sp.sense = "N";
for (int i = 65; i < 90; i++){
if (enzyme.cutN[i]) sp.cut += (char)i;
if (enzyme.exceptC[i]) sp.no_cut += (char)i;
}
}
se.specificity.push_back(sp);
rs.sample_enzyme.push_back(se);
CnpxSearchSummary ss;
ss.base_name=params->outFile;
ss.search_engine="Magnum";
ss.search_engine_version=version;
ss.precursor_mass_type="monoisotopic";
ss.fragment_mass_type="monoisotopic";
ss.search_id=1;
for(size_t a=0;a<params->mods.size();a++){
CnpxAminoAcidModification aam;
aam.aminoacid=(char)params->mods[a].index;
aam.massdiff=params->mods[a].mass;
aam.mass = aam.massdiff + aa.getAAMass(params->mods[a].index);
aam.variable="Y";
ss.aminoacid_modification.push_back(aam);
}
for (size_t a = 0; a<params->fMods.size(); a++){
CnpxAminoAcidModification aam;
aam.aminoacid = (char)params->fMods[a].index;
aam.massdiff = params->fMods[a].mass;
aam.mass = aa.getAAMass(params->fMods[a].index);
aam.variable = "N";
ss.aminoacid_modification.push_back(aam);
}
for (size_t a = 0; a<parObj->xmlParams.size(); a++){
CnpxParameter p;
p.name=parObj->xmlParams[a].name;
p.value=parObj->xmlParams[a].value;
ss.parameter.push_back(p);
}
CnpxSearchDatabase sdb;
sdb.local_path=params->dbPath;
sdb.type="AA";
ss.search_database.push_back(sdb);
rs.search_summary.push_back(ss);
pa.msms_run_summary.push_back(rs);
p->msms_pipeline_analysis.push_back(pa);
pepXMLindex=0;
return p;
}
bool MData::createPercolator(FILE*& f, FILE*& f2){
string sPercName = params->outFile;
if(params->splitPercolator){
string file1= sPercName+ ".standard.perc.txt";
f = fopen(file1.c_str(), "wt");
if (f == NULL) {
cout << "ERROR: Cannot open: " << file1 << endl;
return false;
}
fprintf(f, "SpecId\tLabel\tscannr\tMScore\tDScore");
fprintf(f, "\tnLog10Eval\tCharge1\tCharge2\tCharge3");
fprintf(f, "\tCharge4\tCharge5\tCharge6plus");
fprintf(f, "\tMass\tPPM\tLength\tOpenMod\tOpenMass\tAdjMass\tMC");
fprintf(f, "\tPeptide\tProteins\n");
string file2 = sPercName + ".open.perc.txt";
f2 = fopen(file2.c_str(), "wt");
if (f2 == NULL) {
cout << "ERROR: Cannot open: " << file2 << endl;
return false;
}
fprintf(f2, "SpecId\tLabel\tscannr\tMScore\tDScore");
fprintf(f2, "\tnLog10Eval\tCharge1\tCharge2\tCharge3");
fprintf(f2, "\tCharge4\tCharge5\tCharge6plus");
fprintf(f2, "\tMass\tPPM\tLength\tOpenMod\tOpenMass\tAdjMass\tMC");
fprintf(f2, "\tPeptide\tProteins\n");
} else {
sPercName += ".perc.txt";
f = fopen(sPercName.c_str(), "wt");
if (f == NULL) {
cout << "ERROR: Cannot open: " << sPercName << endl;
return false;
}
fprintf(f, "SpecId\tLabel\tscannr\tMScore\tDScore");
fprintf(f, "\tnLog10Eval\tCharge1\tCharge2\tCharge3");
fprintf(f, "\tCharge4\tCharge5\tCharge6plus");
fprintf(f, "\tMass\tPPM\tLength\tOpenMod\tOpenMass\tAdjMass\tMC");
fprintf(f, "\tPeptide\tProteins\n");
}
return true;
}
bool MData::createTXT(FILE*& f){
string sTXTName = params->outFile;
sTXTName += ".magnum.txt";
f = fopen(sTXTName.c_str(), "wt");
if (f == NULL) {
cout << "ERROR: Cannot open: " << sTXTName << endl;
return false;
}
fprintf(f,"ScanNumber\tPSMID\tRTsec\tSelectedMZ\tMonoisotopicMass");
fprintf(f,"\tCharge\tPSMmass\tPPM\tEvalue");
fprintf(f,"\tMscore\tDscore\tReporterIons");
fprintf(f,"\tModifications\tOpenMod\tPeptide");
fprintf(f,"\tPeptideA\tDecoy\tProteins\n");
return true;
}
bool* MData::getAdductSites(){
return &adductSite[0];
}
MSpectrum* MData::getSpectrum(const int& i){
return spec[i];
}
void MData::initHardklor(){
CHardklorVariant hv;
hv.clear();
hs.winSize = 10;
hs.peptide = 4;
hs.sn = 0;
hs.depth = 3;
hs.minCharge = 2;
hs.maxCharge = 8;
hs.algorithm = Version2;
if (params->instrument == 1) hs.msType = FTICR;
else hs.msType = OrbiTrap;
hs.res400 = params->ms1Resolution;
hs.corr = 0.875;
hs.centroid = true;
strcpy(hs.inFile, "PLTmp.ms1");
hs.fileFormat = ms1;
CHardklorVariant hkv;
vector<CHardklorVariant> pepVariants;
pepVariants.clear();
pepVariants.push_back(hkv);
averagine = new CAveragine*[params->threads]();
mercury = new CMercury8*[params->threads]();
h = new CHardklor2*[params->threads]();
mutexHardklor = new Mutex[params->threads]();
bHardklor = new bool[params->threads]();
for (int a = 0; a<params->threads; a++){
averagine[a] = new CAveragine(NULL, NULL);
mercury[a] = new CMercury8(NULL);
if (a == 0) models = new CModelLibrary(averagine[a], mercury[a]);
h[a] = new CHardklor2(averagine[a], mercury[a], models);
h[a]->Echo(false);
h[a]->SetResultsToMemory(true);
Threading::CreateMutex(&mutexHardklor[a]);
bHardklor[a] = false;
}
models->eraseLibrary();
models->buildLibrary(2, 8, pepVariants);
}
MSpectrum& MData::at(const int& i){
return *spec[i];
}
//Places centroided peaks in bins, then finds average. Has potential for error when accuracy drifts into neighboring bins.
void MData::averageScansCentroid(vector<Spectrum*>& s, Spectrum& avg, double min, double max){
unsigned int i;
int j, k;
double binWidth;
double offset = -1.0;
float* bin;
float intensity;
int binCount;
int* pos;
double lowMZ = -1.0;
mScanBin sb;
vector<mScanBin> topList;
avg.clear();
//if vector is just one scan, then simply copy the peaks
if (s.size() == 1){
int index = findPeak(s[0], min);
if (s[0]->at(index).mz<min) index++;
while (index<s[0]->size() && s[0]->at(index).mz<max){
avg.add(s[0]->at(index++));
}
return;
}
pos = new int[s.size()];
//Set some really small bin width related to the mz region being summed.
binWidth = 0.0001;
binCount = (int)((max - min) / binWidth + 1);
bin = new float[binCount];
for (j = 0; j<binCount; j++) bin[j] = 0;
//align all spectra to point closest to min and set offset
for (i = 0; i<s.size(); i++) {
pos[i] = findPeak(s[i], min);
if (s[i]->at(pos[i]).mz<min) pos[i]++;
if (offset<0) offset = s[i]->at(pos[i]).mz;
else if (s[i]->at(pos[i]).mz<offset) offset = s[i]->at(pos[i]).mz;
}
//Iterate all spectra and add peaks to bins
for (i = 0; i<s.size(); i++) {
while (pos[i]<s[i]->size() && s[i]->at(pos[i]).mz<max){
j = (int)((s[i]->at(pos[i]).mz - offset) / binWidth);
if (j<0){
pos[i]++;
continue;
}
if (j >= binCount) break;
bin[j] += s[i]->at(pos[i]).intensity;
pos[i]++;
}
}
//Unsure of current efficiency. Finds bin of tallest peak, then combines with neighboring bins
//to produce an average. Thus summing of neighboring bins allows flexibility when dealing with mass accuracy drift.
//Using larger bins has the same effect, but perhaps fewer significant digits in the final result.
for (j = 0; j<binCount; j++){
if (bin[j]>0) {
sb.index = j;
sb.intensity = bin[j];
topList.push_back(sb);
}
}
if (topList.size()>0) {
qsort(&topList[0], topList.size(), sizeof(mScanBin), compareScanBinRev2);
for (i = 0; i<topList.size(); i++){
if (bin[topList[i].index] == 0) continue;
intensity = 0;
j = topList[i].index - 50; //This will sum bins within 0.05 m/z... might be too wide...
k = topList[i].index + 51;
if (j<0) j = 0;
if (k>binCount) k = binCount;
for (j = j; j<k; j++){
intensity += bin[j];
bin[j] = 0;
}
intensity /= s.size();
avg.add(offset + topList[i].index*binWidth, intensity);
}
avg.sortMZ();
}
//clean up memory
delete[] bin;
delete[] pos;
}
void MData::diagSinglet(){
int oddCount = 0;
double maxScore;
int bigCount = 0;
int twoCount = 0;
double bigScore = 0;
double bigMass;
double unknownMass;
for (size_t b = 0; b<spec.size(); b++){
//if(spec[b].getScoreCard(0).simpleScore==0) continue;
maxScore = 0;
unknownMass = 0;
for (int q = 0; q<spec[b]->sizePrecursor(); q++){
if (spec[b]->getTopPeps(q)->peptideCount == 0) continue;
if (spec[b]->getTopPeps(q)->peptideFirst->simpleScore>spec[b]->getScoreCard(0).simpleScore){
if (spec[b]->getTopPeps(q)->peptideFirst->simpleScore>maxScore) {
maxScore = spec[b]->getTopPeps(q)->peptideFirst->simpleScore;
unknownMass = spec[b]->getPrecursor(q).monoMass - spec[b]->getTopPeps(q)->peptideFirst->mass;
}
}
}
if (maxScore>0) oddCount++;
if (maxScore>3) bigCount++;
if (maxScore>3 && spec[b]->getScoreCard(0).simpleScore>0 && maxScore>spec[b]->getScoreCard(0).simpleScore * 2) twoCount++;
if (maxScore>bigScore) {
bigScore = maxScore;
bigMass = unknownMass;
}
}
cout << " Diagnostics:" << endl;
cout << " Odd counts " << oddCount << " of " << spec.size() << endl;
cout << " Big counts " << bigCount << " of " << oddCount << endl;
cout << " Two fold " << twoCount << " of " << bigCount << endl;
cout << " Biggest Score: " << bigScore << " Mass: " << bigMass << endl;
cout << endl;
}
void MData::exportPercolator(FILE*& f, vector<mResults>& r){
for (size_t a = 0; a<r.size(); a++){
if (r[a].decoy) {
fprintf(f, "D-%d-%.2f", r[a].scanNumber, r[a].rTimeSec / 60);
if(a>0) fprintf(f,"-%d",(int)a+1);
fprintf(f, "\t-1");
} else {
fprintf(f, "T-%d-%.2f", r[a].scanNumber, r[a].rTimeSec / 60);
if (a>0) fprintf(f, "-%d", (int)a + 1);
fprintf(f, "\t1");
}
fprintf(f, "\t%d", r[a].scanNumber);
fprintf(f, "\t%.4lf", r[a].scoreMagnum);
fprintf(f, "\t%.4lf", r[a].scoreDelta);
fprintf(f, "\t%.6lf", -log10(r[a].eValue));
int z=r[a].charge;
if(z>6) z=6;
for(int b=1;b<z;b++) fprintf(f,"\t0");
fprintf(f,"\t1");
for (int b = z + 1; b<7; b++) fprintf(f, "\t0");
fprintf(f, "\t%.4lf", r[a].psmMass);
fprintf(f, "\t%.4lf", r[a].ppm);
fprintf(f, "\t%d", (int)r[a].peptide.size());
//OpenMod\tOpenMass\tAdjMass\tMC
if(r[a].openMod.empty()) fprintf(f, "\t0\t0");
else fprintf(f,"\t1\t%.4lf",r[a].openModMass);
fprintf(f,"\t%.4lf",r[a].psmMass/r[a].peptide.size());
int mc=0;
for(size_t b=0;b<r[a].peptide.size()-1;b++){
if((r[a].peptide[b]=='R' || r[a].peptide[b]=='K') && r[a].peptide[b+1]!='P') mc++;
}
fprintf(f,"\t%d",mc);
fprintf(f, "\t-.%s.-", r[a].modPeptide[0].c_str());
for(size_t b=0;b<r[a].proteins.size();b++){
fprintf(f, "\t%s", r[a].proteins[b].protein.c_str());
}
fprintf(f, "\n");
break; //only output first result
}
}
void MData::exportTXT(FILE*& f, vector<mResults>& r){
for(size_t a=0;a<r.size();a++){
fprintf(f,"%d",r[a].scanNumber);
fprintf(f, "\t%d", r[a].psmID);
fprintf(f, "\t%.2f", r[a].rTimeSec);
fprintf(f,"\t%.6lf",r[a].selectedMZ);
fprintf(f, "\t%.6lf", r[a].monoMass);
fprintf(f,"\t%d",r[a].charge);
fprintf(f, "\t%.6lf", r[a].psmMass);
fprintf(f, "\t%.4lf", r[a].ppm);
fprintf(f, "\t%.4e", r[a].eValue);
fprintf(f, "\t%.2lf", r[a].scoreMagnum);
fprintf(f, "\t%.2lf", r[a].scoreDelta);
if(r[a].rIon.size()==0) fprintf(f,"\t");
else {
fprintf(f,"\t%.3lf",r[a].rIon[0]);
for(size_t b=1;b<r[a].rIon.size();b++) fprintf(f,";%.3lf",r[a].rIon[b]);
}
fprintf(f, "\t%s", r[a].mods.c_str());
fprintf(f, "\t%s", r[a].openMod.c_str());
fprintf(f, "\t%s", r[a].peptide.c_str());
fprintf(f, "\t%s", r[a].modPeptide[0].c_str());
if(r[a].decoy) fprintf(f, "\t1");
else fprintf(f,"\t0");
fprintf(f, "\t%s", r[a].proteins[0].protein.c_str());
for(size_t b=1;b<r[a].proteins.size();b++){
fprintf(f, ";%s", r[a].proteins[b].protein.c_str());
}
fprintf(f,"\n");
}
}
void MData::exportPepXML(NeoPepXMLParser*& p, vector<mResults>& r){
if(r.size()==0) return;
CnpxSpectrumQuery s;
char str[256];
sprintf(str,"%s.%d.%d.%d",params->msBase.c_str(),r[0].scanNumber,r[0].scanNumber,r[0].charge);
s.spectrum=str;
s.start_scan=r[0].scanNumber;
s.end_scan=r[0].scanNumber;
s.precursor_neutral_mass=r[0].monoMass;
s.assumed_charge=r[0].charge;
s.retention_time_sec=r[0].rTimeSec;
s.index=++pepXMLindex;
CnpxSearchResult sr;
for (size_t a = 0; a<r.size(); a++){
for(size_t x=0;x<r[a].vMods.size();x++){ //to export EVERY alternate arrangement of mods for this PSM
CnpxSearchHit sh;
sh.hit_rank=1;
sh.peptide=r[a].peptide;
sh.protein=r[a].proteins[0].protein;
sh.peptide_prev_aa=r[a].proteins[0].prevAA;
sh.peptide_next_aa=r[a].proteins[0].nextAA;
sh.peptide_start_pos = r[a].proteins[0].startPos;
for(size_t b=1;b<r[a].proteins.size();b++){
CnpxAlternativeProtein ap;
ap.protein=r[a].proteins[b].protein;
ap.peptide_prev_aa = r[a].proteins[b].prevAA;
ap.peptide_next_aa = r[a].proteins[b].nextAA;
ap.peptide_start_pos = r[a].proteins[b].startPos;
sh.alternative_protein.push_back(ap);
}
sh.num_tot_proteins=(int)r[a].proteins.size();
sh.calc_neutral_pep_mass=r[a].psmMass;
sh.massdiff=s.precursor_neutral_mass-sh.calc_neutral_pep_mass;
if(fabs(sh.massdiff<0.000001)) sh.massdiff=0;
//Add scores
sprintf(str,"%.4lf",r[a].scoreMagnum);
sh.addSearchScore("Mscore",string(str));
sprintf(str, "%.4lf", r[a].scoreDelta);
sh.addSearchScore("Dscore", string(str));
sprintf(str, "%.4lf", r[a].ppm);
sh.addSearchScore("ppm_error", string(str));
sprintf(str, "%.3e", r[a].eValue);
sh.addSearchScore("Evalue", string(str));
for(size_t b=0;b<r[a].rIon.size(); b++){
sprintf(str, "%.4lf", r[a].rIon[b]);
sh.addSearchScore("reporter_ion", string(str));
}
//check modifications
CnpxModificationInfo mi;
//check n-term
for(size_t b=0;b<r[a].vMods[x].mods.size();b++){
if (r[a].vMods[x].mods[b].term && r[a].vMods[x].mods[b].pos == 0 && !r[a].vMods[x].mods[b].adduct){
sprintf(str, "n[%d]", (int)(r[a].vMods[x].mods[b].mass + 0.5));
mi.modified_peptide+=str;
mi.mod_nterm_mass = r[a].vMods[x].mods[b].mass + 1.00782503;
break;
}
}
//check all amino acids
for(size_t b=0;b<r[a].peptide.size();b++){
mi.modified_peptide+=r[a].peptide[b];
//check fixed mods
if(aa.getFixedModMass(r[a].peptide[b])!=0){
char c = r[a].peptide[b];
sprintf(str, "[%d]", (int)(aa.getAAMass(c) + 0.5));
mi.modified_peptide += str;
CnpxModAminoAcidMass mam;
mam.position = (int)b + 1;
mam.mass = aa.getAAMass(r[a].peptide[b]);
mam.staticMass = aa.getFixedModMass(c);
mam.source = "param";
mi.mod_aminoacid_mass.push_back(mam);
}
//check variable mods
for (size_t c = 0; c<r[a].vMods[x].mods.size(); c++){
if (r[a].vMods[x].mods[c].pos == b && !r[a].vMods[x].mods[c].term){
sprintf(str, "[%d]", (int)(r[a].vMods[x].mods[c].mass + 0.5));
mi.modified_peptide += str;
CnpxModAminoAcidMass mam;
mam.position = (int)r[a].vMods[x].mods[c].pos + 1;
mam.mass = aa.getAAMass(r[a].peptide[b]) + r[a].vMods[x].mods[c].mass;
if (r[a].vMods[x].mods[c].variable) mam.variable = r[a].vMods[x].mods[c].mass;
else mam.staticMass = r[a].vMods[x].mods[c].mass;
if (r[a].vMods[x].mods[c].adduct) mam.source = "adduct";
else mam.source="param";
mi.mod_aminoacid_mass.push_back(mam);
break;
}
}
}
//check c-term
for (size_t b = 0; b<r[a].vMods[x].mods.size(); b++){
if (r[a].vMods[x].mods[b].term && r[a].vMods[x].mods[b].pos > 0 && !r[a].vMods[x].mods[b].adduct){
sprintf(str, "c[%d]", (int)(r[a].vMods[x].mods[b].mass + 0.5));
mi.modified_peptide += str;
mi.mod_cterm_mass = r[a].vMods[x].mods[b].mass + 17.00273963;
break;
}
}
if(mi.mod_aminoacid_mass.size()>0 || mi.mod_cterm_mass!=0 || mi.mod_nterm_mass!=0) sh.modification_info.push_back(mi);
//check for open modifications (adducts)
for (size_t b = 0; b<r[a].vMods[x].mods.size(); b++){
if (r[a].vMods[x].mods[b].adduct && r[a].vMods[x].mods[b].term){
sh.massdiff = r[a].vMods[x].mods[b].mass;
}
}
sr.search_hit.push_back(sh);
}
}
s.search_result.push_back(sr);
p->msms_pipeline_analysis.back().msms_run_summary.back().spectrum_query.push_back(s);
}
//Returns closet mz value to desired point
int MData::findPeak(Spectrum* s, double mass){
int sz = s->size();
int lower = 0;
int mid = sz / 2;
int upper = sz;
//binary search to closest mass
while (s->at(mid).mz != mass){
if (lower >= upper) break;
if (mass<s->at(mid).mz){
upper = mid - 1;
mid = (lower + upper) / 2;
} else {
lower = mid + 1;
mid = (lower + upper) / 2;
}
if (mid == sz) {
mid--;
break;
}
}
//Check that mass is closest
if (mid>0 && fabs(s->at(mid - 1).mz - mass)<fabs(s->at(mid).mz - mass)) return mid - 1;
if (mid<s->size() - 1 && fabs(s->at(mid + 1).mz - mass)<fabs(s->at(mid).mz - mass)) return mid + 1;
return mid;
}
//Returns point within precision or -1 if doesn't exist
int MData::findPeak(Spectrum* s, double mass, double prec){
int sz = s->size();
int lower = 0;
int mid = sz / 2;
int upper = sz;
double minMass = mass - (mass / 1000000 * prec);
double maxMass = mass + (mass / 1000000 * prec);
//binary search to closest mass
while (s->at(mid).mz<minMass || s->at(mid).mz>maxMass){
if (lower >= upper) break;
if (mass<s->at(mid).mz){
upper = mid - 1;
mid = (lower + upper) / 2;
} else {
lower = mid + 1;
mid = (lower + upper) / 2;
}
if (mid == sz) {
mid--;
break;
}
}
//Check that mass is correct
if (s->at(mid).mz>minMass && s->at(mid).mz<maxMass) return mid;
return -1;
}
void MData::formatMS2(MSToolkit::Spectrum* s, MSpectrum* pls){
char nStr[256];
string sStr;
pls->setRTime(s->getRTime());
pls->setScanNumber(s->getScanNumber());
s->getNativeID(nStr, 256);
sStr = nStr;
pls->setNativeID(sStr);
bool doCentroid = false;
switch (s->getCentroidStatus()){
case 0:
if (params->ms2Centroid) {
char tmpStr[256];
sprintf(tmpStr, "Magnum parameter indicates MS/MS data are centroid, but spectrum %d labeled as profile.", s->getScanNumber());
mlog->addError(string(tmpStr));
} else doCentroid = true;
break;
case 1:
if (!params->ms2Centroid) {
mlog->addWarning(0, "Spectrum is labeled as centroid, but Magnum parameter indicates data are profile. Ignoring Magnum parameter.");
}
break;
default:
if (!params->ms2Centroid) doCentroid = true;
break;
}
//If not centroided, do so now.
int totalPeaks = 0;
if (doCentroid){
centroid(s, pls, params->ms2Resolution, params->instrument);
totalPeaks += pls->size();
} else {
mSpecPoint sp;
for (int i = 0; i<s->size(); i++){
sp.mass = s->at(i).mz;
sp.intensity = s->at(i).intensity;
pls->addPoint(sp);
}
}
////remove precursor if requested
//if (params->removePrecursor>0){
// double pMin = s->getMZ() - params->removePrecursor;
// double pMax = s->getMZ() + params->removePrecursor;
// for (int i = 0; i<pls->size(); i++){
// if ((*pls)[i].mass>pMin && (*pls)[i].mass<pMax) (*pls)[i].intensity = 0;
// }
//}
//Collapse the isotope peaks
int collapsedPeaks = 0;
if (params->specProcess == 1 && pls->size()>1) {
collapseSpectrum(*pls);
collapsedPeaks += pls->size();
}
//If user limits number of peaks to analyze, sort by intensity and take top N
if (params->maxPeaks>0 && pls->size()>params->maxPeaks){
if (pls->size()>1) pls->sortIntensityRev();
vector<mSpecPoint> v;
for (int i = 0; i<params->maxPeaks; i++) v.push_back((*pls)[i]);
pls->clear();
for (int i = 0; i<params->maxPeaks; i++) pls->addPoint(v[i]);
pls->sortMZ();
pls->setMaxIntensity(v[0].intensity);
} else {
float max = 0;
for (int i = 0; i<pls->size(); i++){
if ((*pls)[i].intensity>max) max = (*pls)[i].intensity;
}
pls->setMaxIntensity(max);
}
//Get any additional information user requested
pls->setCharge(s->getCharge());
pls->setMZ(s->getMZ());
if (params->preferPrecursor>0){
if (s->getMonoMZ()>0 && s->getCharge()>0){
mPrecursor pre;
pre.monoMass = s->getMonoMZ()*s->getCharge() - s->getCharge()*1.007276466;
pre.charge = s->getCharge();
pre.corr = 0;
pre.type=1;
pre.offset=0;
pls->addPrecursor(pre, params->topCount);
for (int px = 1; px <= params->isotopeError; px++){
if (px == 4) break;
pre.monoMass -= 1.00335483;
pre.corr -= 0.1;
pre.offset-=1;
pls->addPrecursor(pre, params->topCount);
}
pls->setInstrumentPrecursor(true);
}
}
}
bool MData::getBoundaries(double mass1, double mass2, vector<int>& index, bool* buffer){
int sz=(int)massList.size();
if(mass1>massList[sz-1].mass) return false;
int lower=0;
int mid=sz/2;
int upper=sz;
int i;
int low;
int high;
vector<int> v;
//binary search to closest mass
while(massList[mid].mass!=mass1){
if(lower>=upper) break;
if(mass1<massList[mid].mass){
upper=mid-1;
mid=(lower+upper)/2;
} else {
lower=mid+1;
mid=(lower+upper)/2;
}
if(mid==sz) {
mid--;
break;
}
}
//Adjust if needed
if(massList[mid].mass<mass1) mid++;
if(mid>0 && massList[mid-1].mass>mass1) mid--;
if(massList[mid].mass>mass2) return false;
if(mid==sz) return false;
low=mid;
//binary search to next mass
lower=0;
mid=sz/2;
upper=sz;
while(massList[mid].mass!=mass2){
if(lower>=upper) break;
if(mass2<massList[mid].mass){
upper=mid-1;
mid=(lower+upper)/2;
} else {
lower=mid+1;
mid=(lower+upper)/2;
}
if(mid==sz) {
mid--;
break;
}
}
//Adjust if needed
if(massList[mid].mass>mass2) mid--;
if(mid<sz-1 && massList[mid+1].mass<mass2) mid++;
if(mid<0) return false;
high=mid;
sz=(int)spec.size();
memset(buffer,false,sz);
for (i = low; i <= high; i++) buffer[massList[i].index]=true;
index.clear();
for (i = 0; i < sz; i++) {
if(buffer[i]) index.push_back(i);
}
return true;
}
//Get the list of spectrum array indexes to search based on desired mass
bool MData::getBoundaries2(double mass, double prec, vector<int>& index, bool* buffer){
int sz=(int)massList.size();
int lower=0;
int mid=sz/2;
int upper=sz;
int i;
vector<int> v;
double minMass = mass - (mass/1000000*prec);
double maxMass = mass + (mass/1000000*prec);
//binary search to closest mass
while(massList[mid].mass<minMass || massList[mid].mass>maxMass){
if(lower>=upper) break;
if(mass<massList[mid].mass){
upper=mid-1;
mid=(lower+upper)/2;
} else {
lower=mid+1;
mid=(lower+upper)/2;
}
if(mid==sz) {
mid--;
break;
}
}
//Check that mass is correct
if(massList[mid].mass<minMass || massList[mid].mass>maxMass) return false;
v.push_back(massList[mid].index);
//check left
i=mid;
while(i>0){
i--;
if(massList[i].mass<minMass) break;
v.push_back(massList[i].index);
}
//check right
i=mid;
while(i<(sz-1)){
i++;
if(massList[i].mass>maxMass) break;
v.push_back(massList[i].index);
}
sz = (int)spec.size();
memset(buffer, false, sz);
for (i = 0; i < (int)v.size(); i++) buffer[v[i]] = true;
index.clear();
for (i = 0; i < sz; i++) {
if (buffer[i]) index.push_back(i);
}
return true;
//Sort indexes and copy to final array, removing duplicates.
//This may be a potentially slow step and should be profiled
qsort(&v[0],v.size(),sizeof(int),compareInt);
index.clear();
index.push_back(v[0]);
mid=(int)v.size();
for(i=1;i<mid;i++){
if(v[i]!=v[i-1]) index.push_back(v[i]);
}
return true;
}
double MData::getMaxMass(){
if(massList.size()==0) return 0;
else return massList[massList.size()-1].mass;
}
double MData::getMinMass(){
if(massList.size()==0) return 0;
else return massList[0].mass;
}
void MData::memoryAllocate(){
//find largest possible array for a spectrum
int threads = params->threads;
double td = params->maxPepMass + params->maxAdductMass + 1;
maxPrecursorMass = (int)td;
int xCorrArraySize = (int)((params->maxPepMass + params->maxAdductMass + 100.0) / params->binSize);
//Mark all arrays as available
memoryPool = new bool[threads];
for (int a = 0; a<threads; a++) memoryPool[a] = false;
//Allocate arrays
tempRawData = new double*[threads]();
for (int a = 0; a<threads; a++) tempRawData[a] = new double[xCorrArraySize]();
tmpFastXcorrData = new double*[threads]();
for (int a = 0; a<threads; a++) tmpFastXcorrData[a] = new double[xCorrArraySize]();
fastXcorrData = new float*[threads]();
for (int a = 0; a<threads; a++) fastXcorrData[a] = new float[xCorrArraySize]();
preProcess = new mPreprocessStruct*[threads]();
for (int a = 0; a<threads; a++) {
preProcess[a] = new mPreprocessStruct();
preProcess[a]->pdCorrelationData = new mSpecPoint[xCorrArraySize]();
preProcess[a]->iMaxXCorrArraySize=xCorrArraySize;
}
//Create mutex
Threading::CreateMutex(&mutexMemoryPool);
}
void MData::memoryFree(){
delete[] memoryPool;
for (int a = 0; a<params->threads; a++){
delete[] tempRawData[a];
delete[] tmpFastXcorrData[a];
delete[] fastXcorrData[a];
delete[] preProcess[a]->pdCorrelationData;
delete preProcess[a];
}
delete[] tempRawData;
delete[] tmpFastXcorrData;
delete[] fastXcorrData;
delete[] preProcess;
//Destroy mutexes
Threading::DestroyMutex(mutexMemoryPool);
}
void MData::outputDiagnostics(FILE* f, MSpectrum& s, MDatabase& db){
size_t i,x;
int j,k;
char strs[256];