-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCHardklor2.cpp
1523 lines (1272 loc) · 40.8 KB
/
CHardklor2.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 2007-2016, Michael R. Hoopmann, Institute for Systems Biology
Michael J. MacCoss, University of Washington
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 "CHardklor2.h"
using namespace std;
using namespace MSToolkit;
CHardklor2::CHardklor2(CAveragine *a, CMercury8 *m, CModelLibrary *lib){
averagine=a;
mercury=m;
models=lib;
bEcho=true;
bMem=false;
PT=NULL;
}
CHardklor2::~CHardklor2(){
averagine=NULL;
mercury=NULL;
models=NULL;
if(PT!=NULL) {
PT=NULL;
}
}
hkMem& CHardklor2::operator[](const int& index){
return vResults[index];
}
void CHardklor2::Echo(bool b){
bEcho=b;
}
int CHardklor2::GoHardklor(CHardklorSetting sett, Spectrum* s){
//Member variables
MSReader r;
Spectrum curSpec,c;
vector<int> v;
FILE* fout=NULL;
int TotalScans;
int manyPep, zeroPep, lowSigPep;
int iPercent;
int minutes, seconds;
int i;
vector<pepHit> vPeps;
//initialize variables
cs=sett;
loadTime=0;
analysisTime=0;
TotalScans=0;
manyPep=0;
zeroPep=0;
lowSigPep=0;
iPercent=0;
getTimerFrequency(timerFrequency);
vResults.clear();
//For noise reduction
CNoiseReduction nr(&r,cs);
//Signature
//if(bEcho) cout << "\n\nHardklor, v2.06, Mike Hoopmann, Mike MacCoss\nCopyright 2007-2012\nUniversity of Washington\n" << endl;
//Set the periodic table
if(PT==NULL) PT=averagine->getPT();
//Ouput file info to user
if(bEcho){
if(s==NULL) cout << "Reading from file: " << cs.inFile << endl;
if(!bMem) cout << "Writing to file: " << cs.outFile << endl;
}
if(cs.fileFormat==dunno) {
cout << "Unknown file format or bad extension." << endl;
return -1;
}
if (!bMem) fout = fopen(cs.outFile, "wt");
//read a spectrum
getExactTime(startTime);
//Read in the initial spectrum
r.setFilter(cs.mzXMLFilter);
r.setRawFilter(cs.rawFilter);
if(s!=NULL){
curSpec=*s;
} else {
if(cs.boxcar==0){
if (cs.scan.iLower > 0) {
int scn = cs.scan.iLower;
r.readFile(&cs.inFile[0], curSpec, scn);
while (curSpec.getScanNumber() == 0) {
scn++;
if (cs.scan.iUpper > 0) {
if (scn > cs.scan.iUpper) break;
} else {
if (scn > r.getLastScan()) break;
}
r.readFile(NULL, curSpec, scn);
}
} else r.readFile(&cs.inFile[0], curSpec);
} else {
if(cs.boxcarFilter==0){
if(!nr.DeNoiseD(curSpec)) curSpec.setScanNumber(0);
//if(!nr.DeNoise(curSpec)) curSpec.setScanNumber(0);
//do something about this...
} else {
if(!nr.DeNoiseC(curSpec)) curSpec.setScanNumber(0);
}
}
}
getExactTime(stopTime);
tmpTime1=toMicroSec(stopTime);
tmpTime2=toMicroSec(startTime);
loadTime+=(tmpTime1-tmpTime2);
//Check that file was read
if(curSpec.getScanNumber()==0) {
if(s!=NULL) {
cout << "Spectrum is invalid." << endl;
return -2;
}
if(cs.scan.iLower>0) cout << cs.inFile << " is invalid, or requested scan number is of incorrect format." << endl;
else cout << cs.inFile << " is invalid, or contains no spectrum." << endl;
return -2;
}
//Write scan information to output file.
if(!bMem){
if (cs.reducedOutput) WriteScanLine(curSpec, fout, 2);
else if (cs.xml) WriteScanLine(curSpec, fout, 1);
else WriteScanLine(curSpec, fout, 0);
} else {
currentScanNumber = curSpec.getScanNumber();
}
//Output progress indicator
if(bEcho) cout << iPercent;
//While there is still data to read in the file.
while(true){
getExactTime(startTime);
TotalScans++;
//Smooth if requested
if(cs.smooth>0) SG_Smooth(curSpec,cs.smooth,4);
//Centroid if needed; notice that this copy wastes a bit of time.
//TODO: make this more efficient
if(cs.boxcar==0 && !cs.centroid) Centroid(curSpec,c);
else c=curSpec;
//There is a bug when using noise reduction that results in out of order m/z values
//TODO: fix noise reduction so sorting isn't needed
if(c.size()>0) c.sortMZ();
//Analyze
QuickHardklor(c,vPeps);
//export results
for(i=0;i<(int)vPeps.size();i++){
if(!bMem){
if(cs.reducedOutput) WritePepLine(vPeps[i],c,fout,2);
else if(cs.xml) WritePepLine(vPeps[i],c,fout,1);
else WritePepLine(vPeps[i],c,fout,0);
} else {
ResultToMem(vPeps[i],c);
}
}
//Update progress
if(bEcho){
if (r.getPercent() > iPercent){
if(iPercent<10) cout << "\b";
else cout << "\b\b";
cout.flush();
iPercent=r.getPercent();
cout << iPercent;
cout.flush();
}
}
getExactTime(stopTime);
tmpTime1=toMicroSec(stopTime);
tmpTime2=toMicroSec(startTime);
analysisTime+=tmpTime1-tmpTime2;
if(s!=NULL) break;
//Check if any user limits were made and met
if( (cs.scan.iUpper == cs.scan.iLower) && (cs.scan.iLower != 0) ){
break;
} else if( (cs.scan.iLower < cs.scan.iUpper) && (curSpec.getScanNumber() >= cs.scan.iUpper) ){
break;
}
//Read next spectrum from file.
getExactTime(startTime);
if(cs.boxcar==0) {
r.readFile(NULL,curSpec);
} else {
if(cs.boxcarFilter==0){
//possible to not filter?
nr.DeNoiseD(curSpec);
} else {
//case 5: nr.DeNoise(curSpec); break; //this is for filtering without boxcar
nr.DeNoiseC(curSpec);
}
}
getExactTime(stopTime);
tmpTime1=toMicroSec(stopTime);
tmpTime2=toMicroSec(startTime);
loadTime+=(tmpTime1-tmpTime2);
if(curSpec.getScanNumber()!=0){
//Write scan information to output file.
if(cs.reducedOutput){
WriteScanLine(curSpec,fout,2);
} else if(cs.xml) {
fprintf(fout,"</Spectrum>\n");
WriteScanLine(curSpec,fout,1);
} else {
WriteScanLine(curSpec,fout,0);
}
} else {
break;
}
}
if(!bMem) fclose(fout);
if(bEcho) {
cout << "\n" << endl;
cout << " Total number of scans analyzed: " << TotalScans << endl;
i=(int)timeToSec(loadTime,timerFrequency);
minutes = (int)(i/60);
seconds = i - (60*minutes);
cout << "\nFile access time: " << minutes << " minutes, " << seconds << " seconds." << endl;
i=(int)timeToSec(analysisTime,timerFrequency);
minutes = (int)(i/60);
seconds = i - (60*minutes);
cout << "Analysis Time: " << minutes << " minutes, " << seconds << " seconds." << endl;
if (minutes==0 && seconds==0){
cout << "IMPOSSIBLE!!!" << endl;
} else if(minutes <=2){
cout << "HOLY FRIJOLE!!" << endl;
} else if(minutes<=5) {
cout << "Like lightning!" << endl;
} else if(minutes<=10){
cout << "That's pretty damn fast!" << endl;
} else if(minutes<=20){
cout << "Monkeys calculate faster than that!" << endl;
} else if(minutes<=30){
cout << "You should have taken a lunch break." << endl;
} else if(minutes<=40){
cout << "Oi! Too freakin' slow!!" << endl;
} else {
cout << "You might be able to eek out some better performance by adjusting your parameters." << endl;
}
}
return 1;
}
int CHardklor2::BinarySearch(Spectrum& s, double mz, bool floor){
int mid=s.size()/2;
int upper=s.size();
int lower=0;
if(mz>=s[s.size()-1].mz) return s.size()-1;
if(mz<=s[0].mz) return 0;
while(s[mid].mz!=mz){
if(lower>=upper) break;
if(s[mid].mz>mz){
upper=mid-1;
mid=(lower+upper)/2;
} else {
lower=mid+1;
mid=(lower+upper)/2;
}
}
if(floor && s[mid].mz>mz && mid>0) return mid-1;
else if(!floor && s[mid].mz<mz && mid<s.size()) return mid+1;
else return mid;
}
//Calculates the resolution (FWHM) of a peak
double CHardklor2::CalcFWHM(double mz,double res,int iType){
double deltaM;
switch(iType){
case 0: //Orbitrap
deltaM = mz * sqrt(mz) / (20*res); //sqare root of 400
break;
case 1: //TOF
deltaM = mz / res;
break;
case 2: //QIT
deltaM = res / 5000.0;
break;
case 3: //FTICR
default:
deltaM = mz * mz / (400*res);
break;
}
return deltaM;
}
//First derivative method, returns base peak intensity of the set
void CHardklor2::Centroid(Spectrum& s, Spectrum& out){
int i,j;
float maxIntensity;
int bestPeak;
bool bLastPos;
int nextBest;
double FWHM;
Peak_T centroid;
out.clear();
//Get boundaries of the spectrum. centroids must be within boundaries.
double minMZ, maxMZ;
if(s.size()>0){
minMZ = s[0].mz;
maxMZ = s[s.size()-1].mz;
}
bLastPos=false;
for(i=0;i<s.size()-1;i++){
if(s[i].intensity<s[i+1].intensity) {
bLastPos=true;
continue;
} else {
if(bLastPos){
bLastPos=false;
//find max and add peak
maxIntensity=0;
for(j=i;j<i+1;j++){
if (s[j].intensity>maxIntensity){
maxIntensity=s[j].intensity;
bestPeak = j;
}
}
//Best estimate of Gaussian centroid
//Get 2nd highest point of peak
if(bestPeak==s.size()){
nextBest=bestPeak-1;
} else if(s[bestPeak-1].intensity > s[bestPeak+1].intensity){
nextBest=bestPeak-1;
} else {
nextBest=bestPeak+1;
}
//Get FWHM
FWHM = CalcFWHM(s[bestPeak].mz,cs.res400,cs.msType);
//Calc centroid MZ (in three lines for easy reading)
centroid.mz = (FWHM*FWHM*log(s[bestPeak].intensity/s[nextBest].intensity));
centroid.mz /= (GAUSSCONST*(s[bestPeak].mz-s[nextBest].mz));
centroid.mz += ((s[bestPeak].mz+s[nextBest].mz)/2);
//Calc centroid intensity
centroid.intensity=(float)(s[bestPeak].intensity/exp(-pow((s[bestPeak].mz-centroid.mz)/FWHM,2)*GAUSSCONST));
//some peaks are funny shaped and have bad gaussian fit.
//if error is more than 10%, keep existing intensity
if( fabs((s[bestPeak].intensity - centroid.intensity) / centroid.intensity * 100) > 10 ||
//not a good check for infinity
centroid.intensity>999999999999.9 ||
centroid.intensity < 0 ) {
centroid.intensity=s[bestPeak].intensity;
}
//Centroided peaks must fall within spectrum mass range
if(centroid.mz<minMZ || centroid.mz>maxMZ) {
//do nothing if invalid mz, but perhaps find a better way to handle this one day.
} else {
out.add(centroid);
}
}
}
}
}
//returns whether or not the peak is still valid. true if peak still exists, false if peak was solved already.
bool CHardklor2::CheckForPeak(vector<Result>& vMR, Spectrum& s, int index){
double dif=100.0;
double massDif;
bool match=false;
int mid=s.size()/2;
int upper=s.size();
int lower=0;
double FWHM=CalcFWHM(vMR[index].mass,cs.res400,cs.msType);
if(vMR[index].mass>=s[s.size()-1].mz) {
mid=s.size()-1;
} else if(vMR[index].mass<=s[0].mz) {
mid=0;
} else {
while(s[mid].mz!=vMR[index].mass){
if(lower>=upper) break;
if(s[mid].mz>vMR[index].mass){
upper=mid-1;
mid=(lower+upper)/2;
} else {
lower=mid+1;
mid=(lower+upper)/2;
}
}
}
dif=fabs(s[mid].mz-vMR[index].mass);
if(mid>0){
massDif=fabs(s[mid-1].mz-vMR[index].mass);
if(massDif<dif) {
dif=massDif;
mid--;
}
}
if(mid<s.size()-1){
massDif=fabs(s[mid+1].mz-vMR[index].mass);
if(massDif<dif) {
dif=massDif;
mid++;
}
}
if(dif<FWHM){
if(mask[mid].intensity>0.1) return false;
else return true;
}
return false;
}
int CHardklor2::CompareBPI(const void *p1, const void *p2){
const pepHit d1 = *(pepHit *)p1;
const pepHit d2 = *(pepHit *)p2;
if(d1.basePeakIndex<d2.basePeakIndex) return -1;
else if(d1.basePeakIndex>d2.basePeakIndex) return 1;
else return 0;
}
double CHardklor2::LinReg(vector<float>& mer, vector<float>& obs){
int i,sz;
double sxx=0,syy=0,sxy=0;
//Cosine angle correlation
sxy=0;
sxx=0;
syy=0;
sz=(int)mer.size();
for(i=0;i<sz;i++){
sxy += (mer[i]*obs[i]);
sxx += (mer[i]*mer[i]);
syy += (obs[i]*obs[i]);
}
if(sxx>0 && syy>0 && sxy>0) return sxy/sqrt(sxx*syy);
else return 0;
}
bool CHardklor2::MatchSubSpectrum(Spectrum& s, int peakIndex, pepHit& pep){
int i,k,n;
size_t varCount;
size_t v;
float max=s[peakIndex].intensity;
int maxMercuryIndex[3];
vector<int> charges;
double dif;
vector<float> obs;
vector<float> mer;
vector<int> vMatchIndex;
vector<float> vMatchPeak;
vector<Result> vMR;
Result r;
double corr;
double da;
//keep track of best hits
double bestCorr;
double bestDA;
int bestCharge;
double bestMass;
vector<int> bestMatchIndex;
vector<float> bestMatchPeak;
int matchCount;
int bestMatchCount;
int thisMaxIndex=0;
int bestVariant;
mercuryModel* model=NULL;
double deltaM = CalcFWHM(s[peakIndex].mz,cs.res400,cs.msType);
QuickCharge(s,peakIndex,charges);
bestCorr=0.0;
bestMatchCount=0;
//Mark number of variants to analyze
if(cs.noBase) varCount=cs.variant->size();
else varCount=cs.variant->size()+1;
//iterate through all charge states
for(i=0;i<(int)charges.size();i++){
for(v=0;v<varCount;v++){
//get model from library
dif=0;
model=models->getModel(charges[i],(int)v,s[peakIndex].mz);
for(k=0; k<model->size; k++) {
if(model->peaks[k].intensity>dif){
dif = model->peaks[k].intensity;
maxMercuryIndex[0]=k;
}
}
if(k==0) maxMercuryIndex[1]=-1;
else maxMercuryIndex[1]=maxMercuryIndex[0]-1; //allow right shift
maxMercuryIndex[2]=maxMercuryIndex[0]+1; //allow left shift
//Apply shift and find mz boundaries
n=0;
while(n<3){
if(maxMercuryIndex[n]<0) {
n++;
continue;
}
//Align the mercury distribution (MD) to the observed peak. The MD can shift in
//either direction for one peak to adjust for system noise. The width of the MD
//determines the boundaries for correlation to the observed data.
double lower=5000.0;
double upper=0.0;
double shft = s[peakIndex].mz - model->peaks[maxMercuryIndex[n]].mz;
vMR.clear();
for(k=0; k<model->size; k++) {
r.data=model->peaks[k].intensity;
r.mass=model->peaks[k].mz+shft;
vMR.push_back(r);
if(model->peaks[k].intensity>99.999) thisMaxIndex=(int)vMR.size()-1;
if(r.mass<lower) lower=r.mass;
if(r.mass>upper) upper=r.mass;
}
da=model->area;
//Add a little buffer to the bounds
lower-=0.1;
upper+=0.1;
//Match predictions to the observed peaks and record them in the proper array.
corr=PeakMatcherB(vMR,s,lower,upper,deltaM/2,peakIndex,matchCount,vMatchIndex,vMatchPeak);
//cout << "\tMSS: " << s[peakIndex].mz << " " << s[peakIndex].intensity << "\t" << charges[i] << "\t" << matchCount << "\t" << corr << endl;
if(corr>bestCorr || (corr>cs.corr && corr+0.025*(matchCount-bestMatchCount)>bestCorr) ){
bestMatchIndex=vMatchIndex;
bestMatchPeak=vMatchPeak;
bestMatchCount=matchCount;
bestCorr=corr;
bestMass=model->zeroMass+shft*charges[i];
bestCharge=charges[i];
bestDA=da;
bestVariant=(int)v;
}
n++;
}//while
}//for v (variant)
}//for i (charge)
model=NULL;
//if above threshold, erase peaks.
if(bestCorr>cs.corr){
pep.area=(float)bestDA;
strcpy(pep.averagine,"");
pep.basePeakIndex=0;
pep.charge=bestCharge;
pep.corr=bestCorr;
pep.highMZ=0;
pep.lowMZ=0;
pep.massShift=0;
pep.monoMass=bestMass;
pep.intensity=s[peakIndex].intensity;
pep.variantIndex=bestVariant;
//mark which peaks contributed to this analysis
for(k=0;k<(int)bestMatchIndex.size();k++){
if(bestMatchPeak[k]*max/s[bestMatchIndex[k]].intensity>0.5) s[bestMatchIndex[k]].intensity=-s[bestMatchIndex[k]].intensity;
else s[bestMatchIndex[k]].intensity-=bestMatchPeak[k]*max;
}
return true;
}
return false;
}
double CHardklor2::PeakMatcher(vector<Result>& vMR, Spectrum& s, double lower, double upper, double deltaM, int matchIndex, int& matchCount, int& indexOverlap, vector<int>& vMatchIndex, vector<float>& vMatchIntensity){
vMatchIndex.clear();
vMatchIntensity.clear();
vector<float> obs;
vector<float> mer;
bool match;
bool bMax=false;
double corr=0.0;
double dif;
double massDif;
matchCount=0;
indexOverlap=-1;
int j,k;
for(k=0;k<(int)vMR.size();k++) {
if(vMR[k].data>99.9) bMax=true;
match=false;
dif=deltaM;
//look left
j=matchIndex;
while(j>-1 && s[j].mz>=lower){
massDif=s[j].mz-vMR[k].mass;
if(massDif<-deltaM) break;
if(fabs(massDif)<dif){
dif=fabs(massDif);
match=true;
matchIndex=j;
}
j--;
}
//look right
j=matchIndex+1;
while(j<s.size() && s[j].mz<=upper){
massDif=s[j].mz-vMR[k].mass;
if(massDif>deltaM) break;
if(fabs(massDif)<dif){
dif=fabs(massDif);
match=true;
matchIndex=j;
}
j++;
}
if(!match) {
//if expected peak is significant (above 50 rel abun) and has no match, match it to 0.
if(vMR[k].data>50.0) {
//cout << "xM: " << vMR[k].mass << "\t0" << endl;
mer.push_back((float)vMR[k].data);
obs.push_back(0.0f);
if(bMax) break;
}
} else {
mer.push_back((float)vMR[k].data);
//cout << "xM: " << vMR[k].mass << "\t" << s[matchIndex].mz << endl;
if(mask[matchIndex].intensity>0.1 && vMR[k].data>50) {
if(indexOverlap<0) indexOverlap=matchIndex;
}
if(s[matchIndex].intensity<0.1) {
obs.push_back(0.0f);
} else {
matchCount++;
obs.push_back(s[matchIndex].intensity);
}
vMatchIndex.push_back(matchIndex);
vMatchIntensity.push_back((float)vMR[k].data/100.0f);
}
}
if(matchCount<2) corr=0.0;
else corr=LinReg(mer,obs);
//for(j=0;j<mer.size();j++){
// cout << "M:" << mer[j] << "\t" << "O:" << obs[j] << endl;
//}
//cout << "Corr: " << corr << "(" << matchCount << ")" << endl;
//remove last matched peaks (possibly overlap with other peaks) but only if they are of low abundance.
int tmpCount=matchCount;
while(corr<0.90 && matchCount>2 && mer[mer.size()-1]<50.0){
mer.pop_back();
obs.pop_back();
matchCount--;
double corr2=LinReg(mer,obs);
//cout << "Old corr: " << corr << "(" << matchCount+1 << ")" << " New corr: " << corr2 << endl;
if(corr2>corr) {
corr=corr2;
tmpCount=matchCount;
}
}
matchCount=tmpCount;
return corr;
}
double CHardklor2::PeakMatcherB(vector<Result>& vMR, Spectrum& s, double lower, double upper, double deltaM, int matchIndex, int& matchCount, vector<int>& vMatchIndex, vector<float>& vMatchIntensity){
vMatchIndex.clear();
vMatchIntensity.clear();
vector<float> obs;
vector<float> mer;
bool match;
bool bMax=false;
double corr=0.0;
double dif;
double massDif;
matchCount=0;
int j,k;
for(k=0;k<(int)vMR.size();k++) {
if(vMR[k].data>99.9) bMax=true;
else bMax=false;
match=false;
dif=deltaM;
//look left
j=matchIndex;
while(j>-1 && s[j].mz>=lower){
massDif=s[j].mz-vMR[k].mass;
if(massDif<-deltaM) break;
if(fabs(massDif)<dif){
dif=fabs(massDif);
match=true;
matchIndex=j;
}
j--;
}
//look right
j=matchIndex+1;
while(j<s.size() && s[j].mz<=upper){
massDif=s[j].mz-vMR[k].mass;
if(massDif>deltaM) break;
if(fabs(massDif)<dif){
dif=fabs(massDif);
match=true;
matchIndex=j;
}
j++;
}
if(!match) {
break;
} else {
mer.push_back((float)vMR[k].data);
if(s[matchIndex].intensity<0.1) {
obs.push_back(0.0f);
} else {
matchCount++;
obs.push_back(s[matchIndex].intensity);
}
vMatchIndex.push_back(matchIndex);
vMatchIntensity.push_back((float)vMR[k].data/100.0f);
}
}
if(matchCount<2) corr=0.0;
else corr=LinReg(mer,obs);
int tmpCount=matchCount;
while(corr<0.90 && matchCount>2){
mer.pop_back();
obs.pop_back();
matchCount--;
double corr2=LinReg(mer,obs);
if(corr2>corr) {
corr=corr2;
tmpCount=matchCount;
}
}
matchCount=tmpCount;
return corr;
}
void CHardklor2::QuickCharge(Spectrum& s, int index, vector<int>& v){
int i,j;
double dif;
double rawCh;
double rawChR;
int ch;
int charge[1000];
float minIntensity=s[index].intensity/4;
for(i=cs.minCharge;i<=cs.maxCharge;i++) charge[i]=0;
//check forward
for(j=index+1;j<s.size();j++){
if(s[j].intensity<minIntensity) continue;
dif = s[j].mz - s[index].mz;
if(dif > 1.1) break;
rawCh=1/dif;
ch = (int)(rawCh+0.5);
rawChR=rawCh-(int)rawCh;
if(rawChR>0.2 && rawChR<0.8) continue;
if(ch<cs.minCharge || ch>cs.maxCharge) continue;
charge[ch]=1;
}
//if no forward charge, exit now.
bool bMatch=false;
for(i=cs.minCharge;i<=cs.maxCharge;i++){
if(charge[i]>0) {
bMatch=true;
break;
}
}
if(!bMatch) {
v.clear();
return;
}
//check backward
for(j=index-1;j>=0;j--){
if (s[j].intensity<minIntensity) continue;
dif = s[index].mz - s[j].mz;
if(dif > 1.1) break;
rawCh=1/dif;
ch = (int)(rawCh+0.5);
rawChR=rawCh-(int)rawCh;
if(rawChR>0.2 && rawChR<0.8) continue;
if(ch<cs.minCharge || ch>cs.maxCharge) continue;
charge[ch]=1;
}
v.clear();
for(i=cs.minCharge;i<=cs.maxCharge;i++){
if(charge[i]>0) v.push_back(i);
}
}
void CHardklor2::QuickHardklor(Spectrum& s, vector<pepHit>& vPeps) {
//iterators
int i,j,k,n,m,x;
size_t varCount;
size_t v;
//tracking spectrum peak intensities
float maxHeight=9999999999999.9f;
float max=0.0f;
float lowPoint=9999999999999.9f;
//Mercury storage and variables aligning mercury data (including 1 da shifts)
mercuryModel* model;
vector<Result> vMR;
Result r;
int maxIndex;
int thisMaxIndex;
int maxMercuryIndex[3];
double da;
double lower;
double upper;
double shft;
//peak variables
vector<int> charges;
double deltaM;
double dif;
double corr;
vector<float> obs;
vector<float> mer;
vector<int> vMatchIndex;
vector<float> vMatchPeak;
vector<int> vMatchIndex2;
vector<float> vMatchPeak2;
int matchCount,matchCount2;
int indexOverlap;
double top3[3];
//refinement variables
bool keepPH;
pepHit ph2;
//pepHit bestKeepPH;
int lowIndex;
int highIndex;
bool corr2;
double corr3;
//best hit variables
double bestCorr;
double bestLow;
double bestHigh;
double bestDA;
int bestCharge;
double bestMass;
vector<int> bestMatchIndex;
vector<float> bestMatchPeak;
int bestMatchCount;
pepHit bestPH;
bool bestKeepPH;
int bestOverlap;
int bestLowIndex;
int bestHighIndex;
int bestVariant;
//Results
pepHit ph;
//Spectrum variables
Spectrum origSpec=s;
Spectrum refSpec=s;
Spectrum tmpSpec;
//create mask
mask.clear();
for(i=0;i<s.size();i++) mask.add(s[i].mz,0);
//find lowest intensity;
for(i=0;i<s.size();i++){
//printf("%.6lf\t%.1f\n",s[i].mz, s[i].intensity);
if(s[i].intensity<1) continue; //zero is not allowed as a low point
if(s[i].intensity<lowPoint) lowPoint=s[i].intensity;
}
//clear results vector
vPeps.clear();
//Mark number of variants to analyze
if(cs.noBase) varCount=cs.variant->size();
else varCount=cs.variant->size()+1;
//start the loop through all peaks
while(true){
//Find most intense peak. Note that sorting is not possible because
//peaks change in intensity as they are deconvolved. Also it is advantageous
//to keep peaks in m/z order
max=0.0f;
for(i=0;i<s.size();i++){
if(s[i].intensity<maxHeight && s[i].intensity>max){
max=s[i].intensity;
maxIndex=i;
}
}
//stop searching when we reach lowest original point
//this prevents overfitting with lots of partial noise peaks
if(max<lowPoint) break;