-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKSpectrum.cpp
1564 lines (1345 loc) · 46.7 KB
/
KSpectrum.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 "KSpectrum.h"
#include <iostream>
using namespace std;
/*============================
Constructors & Destructors
============================*/
KSpectrum::KSpectrum(const int& i, const double& bs, const double& os){
binOffset=os;
binSize=bs;
instrumentPrecursor=false;
invBinSize=1.0/binSize;
charge = 0;
maxIntensity=0;
mz = 0;
precursor = new vector<kPrecursor>;
singlets = new vector<KTopPeps>;
spec = new vector<kSpecPoint>;
scanNumber = 0;
rTime = 0;
xCorrArraySize=0;
xCorrSparseArraySize=0;
xCorrSparseArray=NULL;
singletCount=0;
singletMax=i;
kojakSparseArray=NULL;
kojakBins=0;
//singletList=NULL;
singletBins=0;
lowScore=0;
nativeID.clear();
int j;
for (j = 0; j<HISTOSZ; j++) histogram[j] = 0;
histogramCount = 0;
histoMaxIndex = 0;
//for (j = 0; j<HISTOSZ; j++) histogramSinglet[j] = 0;
//histogramSingletCount = 0;
//diagnostics - probably temporary
tmpIntercept=0;
tmpSlope=0;
tmpIStartCorr=0;
tmpINextCorr=0;
tmpIMaxCorr=0;
tmpRSquare=0;
tmpSingletIntercept = 0;
tmpSingletSlope = 0;
tmpSingletIStartCorr = 0;
tmpSingletINextCorr = 0;
tmpSingletIMaxCorr = 0;
tmpSingletRSquare = 0;
//tmpSingCount=0;
tmpHistCount=0;
cc=0;
sc=0;
peakCounts=0;
}
KSpectrum::KSpectrum(const KSpectrum& p){
unsigned int i;
int j;
spec = new vector<kSpecPoint>(*p.spec);
for(i=0;i<20;i++) topHit[i]=p.topHit[i];
precursor = new vector<kPrecursor>(*p.precursor);
singlets = new vector<KTopPeps>(*p.singlets);
binOffset = p.binOffset;
binSize = p.binSize;
instrumentPrecursor = p.instrumentPrecursor;
invBinSize = p.invBinSize;
charge= p.charge;
maxIntensity = p.maxIntensity;
mz = p.mz;
scanNumber = p.scanNumber;
rTime = p.rTime;
xCorrArraySize = p.xCorrArraySize;
xCorrSparseArraySize = p.xCorrSparseArraySize;
lowScore=p.lowScore;
nativeID=p.nativeID;
for (i = 0; i<HISTOSZ; i++) histogram[i] = p.histogram[i];
histogramCount = p.histogramCount;
histoMaxIndex = p.histoMaxIndex;
//for (i = 0; i<HISTOSZ; i++) histogramSinglet[i] = p.histogramSinglet[i];
//histogramSingletCount = p.histogramSingletCount;
//diagnostics - probably temporary
tmpIntercept = p.tmpIntercept;
tmpSlope = p.tmpSlope;
tmpIStartCorr = p.tmpIStartCorr;
tmpINextCorr = p.tmpINextCorr;
tmpIMaxCorr = p.tmpIMaxCorr;
tmpRSquare = p.tmpRSquare;
tmpSingletIntercept = p.tmpSingletIntercept;
tmpSingletSlope = p.tmpSingletSlope;
tmpSingletIStartCorr = p.tmpSingletIStartCorr;
tmpSingletINextCorr = p.tmpSingletINextCorr;
tmpSingletIMaxCorr = p.tmpSingletIMaxCorr;
tmpSingletRSquare = p.tmpSingletRSquare;
//tmpSingCount = p.tmpSingCount;
tmpHistCount = p.tmpHistCount;
cc=p.cc;
sc=p.sc;
singletCount=p.singletCount;
singletMax=p.singletMax;
if(p.xCorrSparseArray==NULL){
xCorrSparseArray=NULL;
} else {
xCorrSparseArray = (kSparseMatrix *)calloc((size_t)xCorrSparseArraySize, (size_t)sizeof(kSparseMatrix));
for(j=0;j<xCorrSparseArraySize;j++) xCorrSparseArray[j]=p.xCorrSparseArray[j];
}
kojakBins=p.kojakBins;
if(p.kojakSparseArray==NULL){
kojakSparseArray=NULL;
} else {
for(j=0;j<kojakBins;j++){
if(p.kojakSparseArray[j]==NULL){
kojakSparseArray[j]=NULL;
} else {
kojakSparseArray[j] = new char[(int)invBinSize+1];
for(i=0;i<(unsigned int)invBinSize+1;i++) kojakSparseArray[j][i]=p.kojakSparseArray[j][i];
}
}
}
}
KSpectrum::~KSpectrum(){
delete spec;
delete precursor;
delete singlets;
if(xCorrSparseArray!=NULL) free(xCorrSparseArray);
int j;
if(kojakSparseArray!=NULL){
for(j=0;j<kojakBins;j++){
if(kojakSparseArray[j]!=NULL) delete [] kojakSparseArray[j];
}
delete [] kojakSparseArray;
}
}
/*============================
Operators
============================*/
KSpectrum& KSpectrum::operator=(const KSpectrum& p){
if(this!=&p){
unsigned int i;
int j;
delete spec;
spec = new vector<kSpecPoint>(*p.spec);
for(i=0;i<20;i++) topHit[i]=p.topHit[i];
delete precursor;
precursor = new vector<kPrecursor>(*p.precursor);
delete singlets;
singlets = new vector<KTopPeps>(*p.singlets);
binOffset = p.binOffset;
binSize = p.binSize;
instrumentPrecursor = p.instrumentPrecursor;
invBinSize = p.invBinSize;
charge = p.charge;
maxIntensity = p.maxIntensity;
mz = p.charge;
scanNumber = p.scanNumber;
rTime = p.rTime;
xCorrArraySize = p.xCorrArraySize;
xCorrSparseArraySize = p.xCorrSparseArraySize;
lowScore = p.lowScore;
nativeID = p.nativeID;
for (i = 0; i<HISTOSZ; i++) histogram[i] = p.histogram[i];
histogramCount = p.histogramCount;
histoMaxIndex = p.histoMaxIndex;
//diagnostics - probably temporary
tmpIntercept = p.tmpIntercept;
tmpSlope = p.tmpSlope;
tmpIStartCorr = p.tmpIStartCorr;
tmpINextCorr = p.tmpINextCorr;
tmpIMaxCorr = p.tmpIMaxCorr;
tmpRSquare = p.tmpRSquare;
tmpSingletIntercept = p.tmpSingletIntercept;
tmpSingletSlope = p.tmpSingletSlope;
tmpSingletIStartCorr = p.tmpSingletIStartCorr;
tmpSingletINextCorr = p.tmpSingletINextCorr;
tmpSingletIMaxCorr = p.tmpSingletIMaxCorr;
tmpSingletRSquare = p.tmpSingletRSquare;
//tmpSingCount = p.tmpSingCount;
tmpHistCount = p.tmpHistCount;
cc = p.cc;
sc = p.sc;
singletCount=p.singletCount;
singletMax=p.singletMax;
if(xCorrSparseArray!=NULL) free(xCorrSparseArray);
if(p.xCorrSparseArray==NULL){
xCorrSparseArray=NULL;
} else {
xCorrSparseArray = (kSparseMatrix *)calloc((size_t)xCorrSparseArraySize, (size_t)sizeof(kSparseMatrix));
for(j=0;j<xCorrSparseArraySize;j++) xCorrSparseArray[j]=p.xCorrSparseArray[j];
}
if(kojakSparseArray!=NULL){
for(j=0;j<kojakBins;j++){
if(kojakSparseArray[j]!=NULL) delete [] kojakSparseArray[j];
}
delete [] kojakSparseArray;
}
kojakBins=p.kojakBins;
if(p.kojakSparseArray==NULL){
kojakSparseArray=NULL;
} else {
for(j=0;j<kojakBins;j++){
if(p.kojakSparseArray[j]==NULL){
kojakSparseArray[j]=NULL;
} else {
kojakSparseArray[j] = new char[(int)invBinSize+1];
for(i=0;i<(unsigned int)invBinSize+1;i++) kojakSparseArray[j][i]=p.kojakSparseArray[j][i];
}
}
}
}
return *this;
}
kSpecPoint& KSpectrum::operator [](const int &i){
return spec->at(i);
}
/*============================
Accessors
============================*/
double KSpectrum::getBinOffset(){
return binOffset;
}
int KSpectrum::getCharge(){
return charge;
}
bool KSpectrum::getInstrumentPrecursor(){
return instrumentPrecursor;
}
double KSpectrum::getInvBinSize(){
return invBinSize;
}
float KSpectrum::getMaxIntensity(){
return maxIntensity;
}
double KSpectrum::getMZ(){
return mz;
}
string KSpectrum::getNativeID(){
return nativeID;
}
kPrecursor& KSpectrum::getPrecursor(int i){
return precursor->at(i);
}
kPrecursor* KSpectrum::getPrecursor2(int i){
return &precursor->at(i);
}
float KSpectrum::getRTime(){
return rTime;
}
int KSpectrum::getScanNumber(){
return scanNumber;
}
kScoreCard& KSpectrum::getScoreCard(int i){
return topHit[i];
}
int KSpectrum::getSingletCount(){
return singletCount;
}
KTopPeps* KSpectrum::getTopPeps(int i){
return &singlets->at(i);
}
int KSpectrum::size(){
return (int)spec->size();
}
int KSpectrum::sizePrecursor(){
return (int)precursor->size();
}
/*============================
Modifiers
============================*/
void KSpectrum::addPoint(kSpecPoint& s){
spec->push_back(s);
}
void KSpectrum::addPrecursor(kPrecursor& p, int sz){
precursor->push_back(p);
KTopPeps tp;
tp.singletMax=sz;
singlets->push_back(tp);
}
void KSpectrum::clear(){
spec->clear();
precursor->clear();
singlets->clear();
}
void KSpectrum::clearPrecursors(){
precursor->clear();
singlets->clear();
}
void KSpectrum::erasePrecursor(int i){
precursor->erase(precursor->begin()+i);
singlets->erase(singlets->begin()+i);
}
void KSpectrum::setCharge(int i){
charge=i;
}
void KSpectrum::setInstrumentPrecursor(bool b){
instrumentPrecursor=b;
}
void KSpectrum::setMaxIntensity(float f){
maxIntensity=f;
}
void KSpectrum::setMZ(double d){
mz=d;
}
void KSpectrum::setNativeID(string s){
nativeID=s;
}
void KSpectrum::setRTime(float f){
rTime=f;
}
void KSpectrum::setScanNumber(int i){
scanNumber=i;
}
/*============================
Functions
============================*/
bool KSpectrum::calcEValue(kParams* params, KDecoys& decoys, KDatabase& db) {
int i;
int iLoopCount;
int iMaxCorr;
int iStartCorr;
int iNextCorr;
double dSlope;
double dIntercept;
double dRSquare;
bool bSkipXL=false;
bool bSingletFail=false;
double topScore=0;
//tmpSingCount = histogramSingletCount;
tmpHistCount = histogramCount;
for(int a=0;a<sizePrecursor();a++){
list<kScoreCard>* sc;
for (int b = 0; b < 3; b++) {
if (b == 0) sc = &getPrecursor2(a)->topSingle;
else if (b == 1) sc = &getPrecursor2(a)->topLoop;
else sc = &getPrecursor2(a)->topXL;
if(sc->size()==0) continue;
list<kScoreCard>::iterator it = sc->begin();
if(b==0){
if(it->simpleScore<=topSingle.simpleScore) continue;
} else if (b == 1){
if(it->simpleScore <= topLoop.simpleScore) continue;
} else {
if (it->simpleScore <= topXL.simpleScore) continue;
}
//always calculate the delta score of this first hit
if(sc->size()>1){
list<kScoreCard>::iterator it2=it;
it2++;
it->dScore=it->simpleScore - it2->simpleScore;
} else it->dScore=it->simpleScore;
//put this PSM on top
if(b==0) topSingle=*it;
else if(b==1) topLoop=*it;
else topXL=*it;
//record highest score of all
if(it->simpleScore>topScore) topScore=it->simpleScore;
}
}
if (topScore == 0) return true; //no need to do any of this if there are no PSMs...
//precompute which ion series to use
decoyIonSz=0;
for (i = 0; i<6; i++){
if (params->ionSeries[i]) {
if (i<3) {
decoyIons[decoyIonSz].b = true;
if (i == 0) decoyIons[decoyIonSz++].mass = -27.9949141;
else if (i == 1) decoyIons[decoyIonSz++].mass = 0;
else decoyIons[decoyIonSz++].mass = 17.026547;
} else {
decoyIons[decoyIonSz].b = false;
if (i == 3) decoyIons[decoyIonSz++].mass = 25.9792649;
else if (i == 4) decoyIons[decoyIonSz++].mass = 0;
else decoyIons[decoyIonSz++].mass = -16.0187224;
}
}
}
if (histogramCount < decoys.decoySize) {
if (!generateXcorrDecoys(params, decoys)) return false;
}
linearRegression2(dSlope, dIntercept, iMaxCorr, iStartCorr, iNextCorr,dRSquare);
histoMaxIndex = iMaxCorr;
//diagnostics - probably temporary
tmpIntercept = (float)dIntercept; // b
tmpSlope = (float)dSlope; // m
tmpIStartCorr = (float)iStartCorr;
tmpINextCorr = (float)iNextCorr;
tmpIMaxCorr = (short)iMaxCorr;
tmpRSquare = dRSquare;
dSlope *= 10.0;
//Make the tophit list
int iTop=0;
if(topSingle.simpleScore==topScore) {
topHit[iTop++]=topSingle;
for(size_t a=0;a<topSingle.alternate.size();a++) {
if(iTop==20) break;
topHit[iTop]=topSingle.alternate[a];
topHit[iTop++].dScore=topSingle.dScore;
}
}
if(iTop<20 && topLoop.simpleScore==topScore) {
topHit[iTop++]=topLoop;
for (size_t a = 0; a < topLoop.alternate.size(); a++) {
if (iTop == 20) break;
topHit[iTop] = topLoop.alternate[a];
topHit[iTop++].dScore = topLoop.dScore;
}
}
if(iTop<20 && topXL.simpleScore==topScore) {
topHit[iTop++]=topXL;
for (size_t a = 0; a < topXL.alternate.size(); a++) {
if (iTop == 20) break;
topHit[iTop] = topXL.alternate[a];
topHit[iTop++].dScore = topXL.dScore;
}
}
////This code is meant to build the top-20 list of PSMs from all PSMs identified.
////it is an alternative to the block of code above.
//vector<list<kScoreCard>*> psmLists;
//vector<list<kScoreCard>::iterator> psmIterators;
//vector<int> psmTypes;
//for (int a = 0; a < sizePrecursor(); a++) {
// list<kScoreCard>* sc;
// for (int b = 0; b < 3; b++) {
// if (b == 0) sc = &getPrecursor2(a)->topSingle;
// else if (b == 1) sc = &getPrecursor2(a)->topLoop;
// else sc = &getPrecursor2(a)->topXL;
// if (sc->size() > 0) {
// psmLists.push_back(sc);
// psmIterators.push_back(psmLists.back()->begin());
// psmTypes.push_back(b);
// }
// }
//}
//int iTopCount = 0;
//size_t topPSM = 0;
//float topScore = 0;
//while (true) {
// topScore = 0;
// for (size_t a = 0; a < psmIterators.size(); a++) {
// if (psmIterators[a] == psmLists[a]->end()) continue;
// if (psmIterators[a]->simpleScore > topScore) {
// topPSM = a;
// topScore = psmIterators[a]->simpleScore;
// }
// }
// if (topScore == 0) break;
// topHit[iTopCount++] = *psmIterators[topPSM];
// for (size_t a = 0; a < psmIterators[topPSM]->alternate.size(); a++) {
// if (iTopCount == 20) break;
// topHit[iTopCount] = psmIterators[topPSM]->alternate[a];
// topHit[iTopCount++].dScore = psmIterators[topPSM]->dScore;
// }
// if (iTopCount == 20) break;
//}
//reorder top scoring peptide so that ties always appear in the same order instead
//of the order in which the search threads finished (which can change from run to run).
string dStr = params->decoy;
refreshScore(db,dStr);
iLoopCount=20;
for (i = 0; i < iLoopCount; i++) {
if (topHit[i].simpleScore == 0) break; //score all e-values among top hits?
if (dSlope >= 0.0) {
topHit[i].eVal = 1e12;
} else {
topHit[i].eVal = pow(10.0, dSlope * topHit[i].simpleScore + dIntercept);
if (topHit[i].eVal>1e12) topHit[i].eVal = 1e12;
}
//score individual peptides
if(topHit[i].score2>0){
for (size_t c = 0; c < topHit[i].alternate.size(); c++) {
topHit[i].alternate[c].eVal= topHit[i].eVal;
}
//only generate these histograms for the first peptides
topHit[i].eVal1 = generateSingletDecoys2(params, decoys, topHit[i].score1, topHit[i].mass1, (int)topHit[i].precursor);
topHit[i].eVal2 = generateSingletDecoys2(params, decoys, topHit[i].score2, topHit[i].mass2, (int)topHit[i].precursor);
//Use same histogram for alternate peptides (ties)
//Note: there are slight differences if the alternate peptide in a tie score has a slightly different mass, resulting in a different decoy
//distribution should the order of peptides change in the next run.
for(size_t c=0;c< topHit[i].alternate.size();c++){
if(topHit[i].alternate[c].score1== topHit[i].score1) topHit[i].alternate[c].eVal1= topHit[i].eVal1;
else topHit[i].alternate[c].eVal1 = generateSingletDecoys2(params, decoys, topHit[i].alternate[c].score1, topHit[i].alternate[c].mass1, (int)topHit[i].alternate[c].precursor);
if(topHit[i].alternate[c].score2== topHit[i].score2) topHit[i].alternate[c].eVal2= topHit[i].eVal2;
else topHit[i].alternate[c].eVal2 = generateSingletDecoys2(params, decoys, topHit[i].alternate[c].score2, topHit[i].alternate[c].mass2, (int)topHit[i].alternate[c].precursor);
}
} else {
if (dSlope >= 0.0) {
topHit[i].eVal1 = 1e12;
} else {
topHit[i].eVal1 = pow(10.0, dSlope * topHit[i].score1 + dIntercept);
if (topHit[i].eVal1>1e12) topHit[i].eVal1 = 1e12;
}
topHit[i].eVal2 = 1e12;
}
}
//score topSingle,topLoop,topXL
if(topSingle.simpleScore>0){
topSingle.eVal = pow(10.0, dSlope * topSingle.simpleScore + dIntercept);
if (topSingle.eVal > 1e12 || dSlope >= 0.0) topSingle.eVal = 1e12;
} else topSingle.eVal=1e12;
if (topLoop.simpleScore > 0) {
topLoop.eVal = pow(10.0, dSlope * topLoop.simpleScore + dIntercept);
if (topLoop.eVal > 1e12 || dSlope >= 0.0) topLoop.eVal = 1e12;
} else topLoop.eVal = 1e12;
if (topXL.simpleScore > 0) {
topXL.eVal = pow(10.0, dSlope * topXL.simpleScore + dIntercept);
if (topXL.eVal > 1e12 || dSlope >= 0.0) topXL.eVal = 1e12;
topXL.eVal1 = generateSingletDecoys2(params, decoys, topXL.score1, topXL.mass1, (int)topXL.precursor);
topXL.eVal2 = generateSingletDecoys2(params, decoys, topXL.score2, topXL.mass2, (int)topXL.precursor);
} else topXL.eVal = 1e12;
return true;
}
bool KSpectrum::checkDecoy(KDatabase& db, string& dStr, kScoreCard& hit){
bool bDecoy = false;
size_t i;
kPeptide pep;
pep = db.getPeptide(hit.pep1);
for (i = 0; i<pep.map->size(); i++){
if (db[pep.map->at(i).index].name.find(dStr) != string::npos) bDecoy = true;
}
if (!bDecoy && hit.pep2>-1){ //only check second peptide if necessary
pep = db.getPeptide(hit.pep2);
for (i = 0; i<pep.map->size(); i++){
if (db[pep.map->at(i).index].name.find(dStr) != string::npos) bDecoy = true;
}
}
return bDecoy;
}
void KSpectrum::checkScore(kScoreCard& s, ePSMList listID){
kPrecursor* pre=&precursor->at(s.precursor);
list<kScoreCard>* psmList;
if(listID==listSingle) psmList=&pre->topSingle;
else if(listID==listLoop) psmList=&pre->topLoop;
else psmList=&pre->topXL;
//check bottom of list first
list<kScoreCard>::iterator it=psmList->end();
if(it== psmList->begin()){
//list is empty, add the item.
psmList->push_front(s);
return;
} else {
it--;
if(s.simpleScore<it->simpleScore){
if(psmList->size()<5) psmList->push_back(s);
return;
}
}
//check from top of list
it=psmList->begin();
while(it!= psmList->end()){
if(s.simpleScore==it->simpleScore){
it->alternate.push_back(s);
return;
} else if(s.simpleScore>it->simpleScore) {
psmList->insert(it,s);
if(psmList->size()>5) {
psmList->pop_back();
if(listID==listXL) lowScore=psmList->back().simpleScore;
} else if(psmList->size()==5 && listID==listXL){
lowScore = psmList->back().simpleScore;
}
return;
}
it++;
}
// if (s.simpleScore == it->simpleScore) {
// it->alternate.push_back(s);
// return;
// }
// psmList->push_front(s);
// if (psmList->size() > 5) {
// psmList->pop_back();
// if (listID == listXL) lowScore = psmList->back().simpleScore;
// } else if (psmList->size() == 5 && listID == listXL) {
// lowScore = psmList->back().simpleScore;
// }
//}
//unsigned int i;
//unsigned int j;
//if(iList>0){
// if(iList==1){
// if(s.simpleScore>topSingle.simpleScore) {
// s.dScore=s.simpleScore-topSingle.simpleScore;
// topSingle=s;
// } else {
// double d=topSingle.simpleScore-s.simpleScore;
// if(d>0 && d<topSingle.dScore) topSingle.dScore=d;
// }
// } else if(iList==2){
// if(s.simpleScore>topLoop.simpleScore) {
// s.dScore = s.simpleScore - topLoop.simpleScore;
// topLoop=s;
// } else {
// double d = topLoop.simpleScore - s.simpleScore;
// if (d > 0 && d < topLoop.dScore) topLoop.dScore = d;
// }
// } else if(iList==3){
// if(s.simpleScore>topXL.simpleScore) {
// s.dScore = s.simpleScore - topXL.simpleScore;
// topXL=s;
// } else {
// double d = topXL.simpleScore - s.simpleScore;
// if (d > 0 && d < topXL.dScore) topXL.dScore = d;
// }
// }
//}
////edge case for "reversible" cross-links: check if already matches top hit identically
////note that such duplications still occur below the top score, but shouldn't influence the final result to the user
//int k=0;
//while(k<20 && s.simpleScore==topHit[k].simpleScore){
// if(s.pep1==topHit[k].pep1 && s.pep2==topHit[k].pep2 && s.k1==topHit[k].k1 && s.k2==topHit[k].k2){
// if(s.mods1.size()==topHit[k].mods1.size() && s.mods2.size()==topHit[k].mods2.size()){
// for(i=0;i<s.mods1.size();i++){
// if(s.mods1[i].mass!=topHit[k].mods1[i].mass || s.mods1[i].pos!=topHit[k].mods1[i].pos) break;
// }
// for(j=0;j<s.mods2.size();j++){
// if(s.mods2[j].mass!=topHit[k].mods2[j].mass || s.mods2[j].pos!=topHit[k].mods2[j].pos) break;
// }
// if(i==s.mods1.size() && j==s.mods2.size()) return;
// }
// }
// k++;
//}
//for(i=0;i<20;i++){
// if(s.simpleScore > topHit[i].simpleScore) {
// for(j=19;j>i;j--) {
// topHit[j]=topHit[j-1];
// }
// topHit[i] = s;
// lowScore=topHit[19].simpleScore;
// return;
// }
//}
}
//from Comet
// Make synthetic decoy spectra to fill out correlation histogram by going
// through each candidate peptide and rotating spectra in m/z space.
double KSpectrum::generateSingletDecoys2(kParams* params, KDecoys& decoys, double xcorr, double mass, int preIndex) {
int i;
int n;
int j;
int k;
int maxZ;
int z;
int key;
int pos;
int xlSite=0;
int xlLen;
double dXcorr;
double dFragmentIonMass = 0.0;
double diffMass;
double preMass;
//int tempHistogram[HISTOSZ];
for(i=0;i<HISTOSZ;i++) tempHistogram[i]=0;
int seed = (int)(scanNumber*mass);
if (seed<0) seed = -seed;
seed = seed % DECOY_SIZE; //don't always start at the top, but not random either; remains reproducible across threads
int decoyIndex;
//compute modification mass
maxZ = precursor->at(preIndex).charge;
if (maxZ>4) maxZ = 4;
preMass = precursor->at(preIndex).monoMass;
diffMass=preMass-mass;
//Does this function need as many DECOY_SIZE as the other? Can this be shortened?
for (i = 0; i<decoys.decoySize; i++) { // iterate through required # decoys
dXcorr = 0.0;
decoyIndex = (seed + i) % DECOY_SIZE;
//find link site - somewhat wasted cycles
for (j = 0; j<MAX_DECOY_PEP_LEN; j++) {
if (decoys.decoyIons[decoyIndex].pdIonsN[j]>mass) break;
}
if(j<1) return 1e12;
xlSite++;
if(xlSite>=(j-1)) xlSite=0;
xlLen = j;
for (n = 0; n<decoyIonSz; n++) { //iterate over each ion series
for (j = 0; j<MAX_DECOY_PEP_LEN; j++) { // iterate through decoy fragment ions
bool bCleave=false;
if (decoyIons[n].b) {
dFragmentIonMass = decoys.decoyIons[decoyIndex].pdIonsN[j] + decoyIons[n].mass;
if (j >= xlSite) {
dFragmentIonMass+=diffMass;
bCleave=true;
}
} else {
dFragmentIonMass = decoys.decoyIons[decoyIndex].pdIonsC[j] + decoyIons[n].mass;
if (j >= xlLen - xlSite) {
dFragmentIonMass += diffMass;
bCleave=true;
}
}
if (dFragmentIonMass>preMass) break;
for (z = 1; z<maxZ; z++) {
mz = (dFragmentIonMass + (z - 1)*1.007276466) / z;
mz = params->binSize * (int)(mz*invBinSize + params->binOffset);
key = (int)mz;
if (key >= kojakBins) break;
if (kojakSparseArray[key] == NULL) continue;
pos = (int)((mz - key)*invBinSize);
dXcorr += kojakSparseArray[key][pos];
}
//iterate over cleavage products (if any)
if(bCleave){
for (size_t a = 0; a<params->cleavageProducts->size(); a++){
if (decoyIons[n].b) dFragmentIonMass = decoys.decoyIons[decoyIndex].pdIonsN[j] + decoyIons[n].mass;
else dFragmentIonMass = decoys.decoyIons[decoyIndex].pdIonsC[j] + decoyIons[n].mass;
dFragmentIonMass += params->cleavageProducts->at(a);
if (dFragmentIonMass>preMass) break;
for (z = 1; z<maxZ; z++) {
mz = (dFragmentIonMass + (z - 1)*1.007276466) / z;
mz = params->binSize * (int)(mz*invBinSize + params->binOffset);
key = (int)mz;
if (key >= kojakBins) break;
if (kojakSparseArray[key] == NULL) continue;
pos = (int)((mz - key)*invBinSize);
dXcorr += kojakSparseArray[key][pos];
}
}
}
}
}
//score the cleavage product ions
if(params->cleavageProducts->size()>0){
for (n = 0; n<decoyIonSz; n++) { //iterate over each ion series
for (j = 0; j<MAX_DECOY_PEP_LEN; j++) { // iterate through decoy fragment ions
if (decoyIons[n].b) {
if (j < xlSite) continue;
} else {
if (j < xlLen - xlSite) continue;
}
for (size_t a = 0; a<params->cleavageProducts->size();a++){
if (decoyIons[n].b) {
dFragmentIonMass = decoys.decoyIons[decoyIndex].pdIonsN[j] + decoyIons[n].mass + params->cleavageProducts->at(a);
} else {
dFragmentIonMass = decoys.decoyIons[decoyIndex].pdIonsC[j] + decoyIons[n].mass + params->cleavageProducts->at(a);
}
if (dFragmentIonMass>preMass) break;
mz = dFragmentIonMass + 1.007276466; //only check 1+ ions
mz = params->binSize * (int)(mz*invBinSize + params->binOffset);
key = (int)mz;
if (key >= kojakBins) break;
if (kojakSparseArray[key] == NULL) continue;
pos = (int)((mz - key)*invBinSize);
dXcorr += kojakSparseArray[key][pos];
}
}
}
}
if (dXcorr <= 0.0) dXcorr = 0.0;
k = (int)(dXcorr*0.05+ 0.5); // 0.05=0.005*10; see KAnalysis::kojakScoring
if (k < 0) k = 0;
else if (k >= HISTOSZ) k = HISTOSZ - 1;
tempHistogram[k]++;
}
//Do linear regression and compute e-value
double dSlope,dIntercept,dRSquare;
int iMaxCorr,iStartCorr,iNextCorr;
linearRegression4(tempHistogram, dSlope, dIntercept, iMaxCorr, iStartCorr, iNextCorr, dRSquare);
double eVal = pow(10.0, dSlope * 10 * xcorr + dIntercept);
if(eVal>1e12) eVal=1e12;
return eVal;
}
//from Comet
// Make synthetic decoy spectra to fill out correlation histogram by going
// through each candidate peptide and rotating spectra in m/z space.
bool KSpectrum::generateXcorrDecoys(kParams* params, KDecoys& decoys) {
int i;
int n;
int j;
int k;
int maxZ;
int z;
int r;
int key;
int pos;
double dXcorr;
double dFragmentIonMass = 0.0;
int myCount=0;
//tmpSingCount = histogramSingletCount;
tmpHistCount = histogramCount;
// DECOY_SIZE is the minimum # of decoys required or else this function isn't
// called. So need to generate iLoopMax more xcorr scores for the histogram.
int iLoopMax = decoys.decoySize - histogramCount;
int seed = (scanNumber*histogramCount);
if(seed<0) seed=-seed;
seed = seed % decoys.decoySize; //don't always start at the top, but not random either; remains reproducible across threads
int decoyIndex;
size_t maxPre = precursor->size();
r=0;
for (i = 0; i<iLoopMax; i++) { // iterate through required # decoys
dXcorr = 0.0;
decoyIndex = (seed + i) % decoys.decoySize;
//iterate over precursors
r++;
if(r>=(int)precursor->size()) r=0;
maxZ = precursor->at(r).charge;
if (maxZ>4) maxZ = 4;
for (n = 0; n<decoyIonSz; n++) { //iterate over each ion series
for (j = 0; j<MAX_DECOY_PEP_LEN; j++) { // iterate through decoy fragment ions
if (decoyIons[n].b) dFragmentIonMass = decoys.decoyIons[decoyIndex].pdIonsN[j] + decoyIons[n].mass;
else dFragmentIonMass = decoys.decoyIons[decoyIndex].pdIonsC[j] + decoyIons[n].mass;
if (dFragmentIonMass>precursor->at(r).monoMass) break;
for (z = 1; z<maxZ; z++) {
mz = (dFragmentIonMass + (z - 1)*1.007276466) / z;
mz = params->binSize * (int)(mz*invBinSize + params->binOffset);
key = (int)mz;
if (key >= kojakBins) break;
if (kojakSparseArray[key] == NULL) continue;
pos = (int)((mz - key)*invBinSize);
dXcorr += kojakSparseArray[key][pos];
}
//iterate over cleavage products (if any)
if(dFragmentIonMass<1000){ //just assume every mass under this value can have a cleavage product on it
for (size_t a = 0; a<params->cleavageProducts->size(); a++){
dFragmentIonMass += params->cleavageProducts->at(a);
for (z = 1; z<maxZ; z++) {
mz = (dFragmentIonMass + (z - 1)*1.007276466) / z;
mz = params->binSize * (int)(mz*invBinSize + params->binOffset);
key = (int)mz;
if (key >= kojakBins) break;
if (kojakSparseArray[key] == NULL) continue;
pos = (int)((mz - key)*invBinSize);
dXcorr += kojakSparseArray[key][pos];
}
}
}
}
}
if (dXcorr <= 0.0) dXcorr = 0.0;
k = (int)(dXcorr*0.05 + 0.5); // 0.05=0.005*10; see KAnalysis::kojakScoring
if (k < 0) k = 0;
else if (k >= HISTOSZ) k = HISTOSZ - 1;
histogram[k]++;
histogramCount++;
myCount++;
}
return true;
}
//from Comet
void KSpectrum::linearRegression2(double& slope, double& intercept, int& iMaxXcorr, int& iStartXcorr, int& iNextXcorr, double& rSquared) {
double Sx, Sxy; // Sum of square distances.
double Mx, My; // means
double dx, dy;
double b, a;
double SumX, SumY; // Sum of X and Y values to calculate mean.
double SST, SSR;
double rsq;
double bestRSQ;
double bestSlope;
double bestInt;
//double dCummulative[HISTOSZ]; // Cummulative frequency at each xcorr value.