-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKAnalysis.cpp
1987 lines (1722 loc) · 62.4 KB
/
KAnalysis.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 "KAnalysis.h"
using namespace std;
bool* KAnalysis::bKIonsManager;
KDatabase* KAnalysis::db;
double KAnalysis::highLinkMass;
KIons* KAnalysis::ions;
double KAnalysis::lowLinkMass;
double KAnalysis::maxMass;
double KAnalysis::minMass;
Mutex KAnalysis::mutexKIonsManager;
Mutex* KAnalysis::mutexSpecScore;
Mutex** KAnalysis::mutexSingletScore;
kParams KAnalysis::params;
KData* KAnalysis::spec;
char** KAnalysis::xlTable;
bool** KAnalysis::scanBuffer;
int KAnalysis::numIonSeries;
int* KAnalysis::pepMassSize;
double** KAnalysis::pepMass;
bool** KAnalysis::pepBin;
int* KAnalysis::pepBinSize;
bool* KAnalysis::soloLoop;
bool KAnalysis::firstPass;
int KAnalysis::skipCount;
int KAnalysis::nonSkipCount;
KDecoys KAnalysis::decoys;
KLog* KAnalysis::klog;
pair<int, int>** KAnalysis::peakMatches;
int* KAnalysis::peakMatchCount;
/*============================
Constructors & Destructors
============================*/
KAnalysis::KAnalysis(kParams& p, KDatabase* d, KData* dat){
unsigned int i;
int j,k;
//Assign pointers and structures
params=p;
db=d;
spec=dat;
xlTable = spec->getXLTable();
//Do memory allocations and initialization
bKIonsManager=NULL;
ions=NULL;
allocateMemory(params.threads);
for(j=0;j<params.threads;j++){
for(i=0;i<params.fMods->size();i++) ions[j].addFixedMod((char)params.fMods->at(i).index,params.fMods->at(i).mass);
for(i=0;i<params.mods->size();i++) ions[j].addMod((char)params.mods->at(i).index,params.mods->at(i).xl,params.mods->at(i).mass);
for(i=0;i<params.aaMass->size();i++) ions[j].setAAMass((char)params.aaMass->at(i).index, params.aaMass->at(i).mass, params.aaMass->at(i).xl);
ions[j].setMaxModCount(params.maxMods);
}
//Initalize variables
maxMass = spec->getMaxMass()+0.25;
minMass = spec->getMinMass()-0.25;
for(j=0;j<spec->sizeLink();j++){
if(spec->getLink(j).mono==0){
if(lowLinkMass==0) lowLinkMass=spec->getLink(j).mass;
if(highLinkMass==0) highLinkMass=spec->getLink(j).mass;
if(spec->getLink(j).mass<lowLinkMass) lowLinkMass=spec->getLink(j).mass;
if(spec->getLink(j).mass>highLinkMass) highLinkMass=spec->getLink(j).mass;
}
}
numIonSeries=0;
for(i=0;i<6;i++){
if(params.ionSeries[i]) numIonSeries++;
}
//Create mutexes
Threading::CreateMutex(&mutexKIonsManager);
mutexSingletScore = new Mutex*[spec->size()];
mutexSpecScore = new Mutex[spec->size()];
for(j=0;j<spec->size();j++){
Threading::CreateMutex(&mutexSpecScore[j]);
mutexSingletScore[j] = new Mutex[spec->at(j).sizePrecursor()];
for(k=0;k<spec->at(j).sizePrecursor();k++){
Threading::CreateMutex(&mutexSingletScore[j][k]);
}
}
decoys.decoySize=params.decoySize;
makePepLists();
skipCount=0;
nonSkipCount=0;
klog=NULL;
}
KAnalysis::~KAnalysis(){
int i,j;
//Destroy mutexes
Threading::DestroyMutex(mutexKIonsManager);
for(i=0;i<spec->size();i++){
Threading::DestroyMutex(mutexSpecScore[i]);
for(j=0;j<spec->at(i).sizePrecursor();j++){
Threading::DestroyMutex(mutexSingletScore[i][j]);
}
delete [] mutexSingletScore[i];
}
delete [] mutexSingletScore;
delete [] mutexSpecScore;
if (params.turbo){
for (i = 0; i < spec->getMotifCount(); i++){
delete[] pepMass[i];
delete[] pepBin[i];
}
delete[] pepMass;
delete[] pepMassSize;
delete[] pepBin;
delete[] pepBinSize;
}
//Deallocate memory and release pointers
deallocateMemory(params.threads);
db=NULL;
spec=NULL;
xlTable=NULL;
klog=NULL;
}
//============================
// Public Functions
//============================
bool KAnalysis::doPeptideAnalysis(){
size_t i;
int iPercent;
int iTmp;
vector<kPeptide>* p;
vector<int> index;
vector<kPepMod> mods;
kScoreCard sc;
firstPass=true;
ThreadPool<kAnalysisStruct*>* threadPool = new ThreadPool<kAnalysisStruct*>(analyzePeptideProc,params.threads,params.threads,1);
//Set progress meter
iPercent=0;
printf("%2d%%",iPercent);
fflush(stdout);
//Set which list of peptides to search (with and without internal lysine)
p=db->getPeptideList();
//track non-links and loops
soloLoop = new bool[p->size()];
for(i=0;i<p->size();i++) soloLoop[i]=false;
//get boundaries for first pass
double lowerBound=(spec->getMinMass()-lowLinkMass)/2-0.25;
double upperBound=spec->getMaxMass()-highLinkMass-params.minPepMass+0.25;
//Iterate the peptide for the first pass
for(i=0;i<p->size();i++){
if(p->at(i).mass>upperBound) continue;
if(p->at(i).mass<lowerBound) break;
threadPool->WaitForQueuedParams();
kAnalysisStruct* a = new kAnalysisStruct(&mutexKIonsManager,&p->at(i),(int)i);
threadPool->Launch(a);
//Update progress meter
iTmp=(int)((double)i/p->size()*100);
if(iTmp>iPercent){
iPercent=iTmp;
printf("\b\b\b%2d%%",iPercent);
fflush(stdout);
}
}
threadPool->WaitForQueuedParams();
threadPool->WaitForThreads();
//Finalize progress meter
printf("\b\b\b100%%");
cout << endl;
//Perform the second pass
firstPass=false;
if(klog!=NULL) klog->addMessage("Scoring peptides (second pass).",true);
cout << " Second pass ... ";
//get boundary for second pass
upperBound = (spec->getMaxMass() - lowLinkMass)/2;
//Set progress meter
iPercent = 0;
printf("%2d%%", iPercent);
fflush(stdout);
i=p->size() - 1;
while(true){
if (p->at(i).mass>upperBound) break;
threadPool->WaitForQueuedParams();
kAnalysisStruct* a = new kAnalysisStruct(&mutexKIonsManager, &p->at(i), (int)i);
threadPool->Launch(a);
//Update progress meter
iTmp = (int)((1.0-(double)i / p->size()) * 100);
if (iTmp>iPercent){
iPercent = iTmp;
printf("\b\b\b%2d%%", iPercent);
fflush(stdout);
}
if(i==0) break;
i--;
}
threadPool->WaitForQueuedParams();
threadPool->WaitForThreads();
//Finalize progress meter
if(iPercent<100) printf("\b\b\b100%%");
cout << endl;
//clean up memory & release pointers
delete [] soloLoop;
delete threadPool;
threadPool=NULL;
p=NULL;
return true;
}
bool KAnalysis::doEValueAnalysis(){
int i;
int iPercent;
int iTmp;
ThreadPool<KSpectrum*>* threadPool = new ThreadPool<KSpectrum*>(analyzeEValueProc, params.threads, params.threads, 1);
//Set progress meter
iPercent = 0;
printf("%2d%%", iPercent);
fflush(stdout);
//Iterate the peptide for the first pass
for (i = 0; i<spec->size(); i++){
threadPool->WaitForQueuedParams();
KSpectrum* a = &spec->at(i);
threadPool->Launch(a);
//Update progress meter
iTmp = (int)((double)i / spec->size() * 100);
if (iTmp>iPercent){
iPercent = iTmp;
printf("\b\b\b%2d%%", iPercent);
fflush(stdout);
}
}
threadPool->WaitForQueuedParams();
threadPool->WaitForThreads();
//Finalize progress meter
printf("\b\b\b100%%");
cout << endl;
//clean up memory & release pointers
delete threadPool;
threadPool = NULL;
return true;
}
//============================
// Private Functions
//============================
//============================
// Thread-Start Functions
//============================
//These functions fire off when a thread starts. They pass the variables to for
//each thread-specific analysis to the appropriate function.
void KAnalysis::analyzePeptideProc(kAnalysisStruct* s){
int i;
Threading::LockMutex(mutexKIonsManager);
for(i=0;i<params.threads;i++){
if(!bKIonsManager[i]){
bKIonsManager[i]=true;
break;
}
}
Threading::UnlockMutex(mutexKIonsManager);
if(i==params.threads){
cout << "Error in KAnalysis::analyzePeptidesProc" << endl;
exit(-1);
}
s->bKIonsMem = &bKIonsManager[i];
analyzePeptide(s->pep,s->pepIndex,i);
delete s;
s=NULL;
}
void KAnalysis::analyzeEValueProc(KSpectrum* s){
s->calcEValue(¶ms, decoys,*db);
s = NULL;
}
//============================
// Analysis Functions
//============================
//Analyzes all single peptides. Also analyzes cross-linked peptides when in full search mode,
//or stage 1 of relaxed mode analysis
bool KAnalysis::analyzePeptide(kPeptide* p, int pepIndex, int iIndex){
int j;
size_t k,k2,k3;
bool bt;
vector<int> index;
vector<kPepMod> mods;
//char str[256];
//db->getPeptideSeq(p->map->at(0).index,p->map->at(0).start,p->map->at(0).stop,str);
//if(strcmp(str,"VPSKK")==0) cout << str << "\t" << p->mass << endl;
//Set the peptide, calc the ions, and score it against the spectra
ions[iIndex].setPeptide(true,&db->at(p->map->at(0).index).sequence[p->map->at(0).start],p->map->at(0).stop-p->map->at(0).start+1,p->mass,p->nTerm,p->cTerm,p->n15);
if(!soloLoop[pepIndex]){ //if we've searched this peptide as solo in the first pass, skip doing so again
ions[iIndex].buildIons();
ions[iIndex].modIonsRec2(0,-1,0,0,false);
for(j=0;j<ions[iIndex].size();j++){
bt=spec->getBoundaries2(ions[iIndex][j].mass,params.ppmPrecursor,index,scanBuffer[iIndex]);
if(bt) scoreSpectra(index,j,ions[iIndex][j].difMass,pepIndex,-1,-1,-1,-1,iIndex,-1,-1);
}
//search non-covalent dimerization if requested by user
/* Deprecate this
if(params.dimers) {
analyzeSingletsNC(*p, pepIndex, iIndex); //this may need updating since 1.6.0
}
*/
if(p->xlSites==0) {
soloLoop[pepIndex]=true;
return true;
}
} else {
if (p->xlSites == 0) return true;
}
//Crosslinked peptides must also search singlets with reciprocol mass on each lysine
analyzeSinglets(*p,pepIndex,lowLinkMass,highLinkMass,iIndex);
if(p->xlSites==1) {
soloLoop[pepIndex] = true;
return true;
}
//also search loop-links
//check loop-links by iterating through each cross-linker mass
//if we've already searched the loop link, exit now
if(soloLoop[pepIndex]) return true;
string pepSeq;
vector<int> xlIndex;
int x;
char site1, site2;
db->getPeptideSeq(*p,pepSeq);
//iterate over every amino acid (except last - it has nothing to link)
for (k = 0; k < pepSeq.size()-1; k++){
//check if aa can be linked
if (xlTable[pepSeq[k]][0]>-1) {
//check all possible motifs at the site
for (x=0;x<20;x++){
if (xlTable[pepSeq[k]][x]==-1) break;
//site can be linked, so check remaining sites
for (k2 = k + 1; k2 < pepSeq.size(); k2++){
if (k2 == pepSeq.size() - 1){ //handle c-terminus differently
if (p->cTerm) {
checkXLMotif(xlTable[pepSeq[k]][x], xlTable['c'],xlIndex);
site1 = pepSeq[k];
site2='c';
} else continue;
} else if (xlTable[pepSeq[k2]][0] == -1) {
continue;
} else {
checkXLMotif((int)xlTable[pepSeq[k]][x], xlTable[pepSeq[k2]],xlIndex);
site1=pepSeq[k];
site2=pepSeq[k2];
}
if (xlIndex.size()>0){
for(k3=0;k3<xlIndex.size();k3++){
if(xlIndex[k3]<0) continue;
ions[iIndex].reset();
ions[iIndex].buildLoopIons(spec->getLink(xlIndex[k3]).mass, (int)k, (int)k2);
ions[iIndex].modLoopIonsRec2(0, (int)k, (int)k2, 0, 0, true);
for (j = 0; j<ions[iIndex].size(); j++){
bt = spec->getBoundaries2(ions[iIndex][j].mass, params.ppmPrecursor, index, scanBuffer[iIndex]);
if (bt) scoreSpectra(index, j, 0, pepIndex, -1, (int)k, (int)k2, xlIndex[k3], iIndex,site1,site2);
}
} //k3
}
} //k2
}//x
}
//also check the n-terminus
if (k == 0 && p->nTerm && xlTable['n'][0]>-1) {
//check all possible motifs at the site
for (x = 0; x<20; x++){
if (xlTable['n'][x] == -1) break;
//site can be linked, so check remaining sites
for (k2 = k + 1; k2 < pepSeq.size(); k2++){
if (k2 == pepSeq.size() - 1){ //handle c-terminus differently
if (p->cTerm) {
checkXLMotif(xlTable['n'][x], xlTable['c'],xlIndex);
site1='n';
site2='c';
} else continue;
} else if (xlTable[pepSeq[k2]][0] == -1) {
continue;
} else {
checkXLMotif((int)xlTable['n'][x], xlTable[pepSeq[k2]],xlIndex);
site1 = 'n';
site2 = pepSeq[k2];
}
if (xlIndex.size()>0){
for (k3 = 0; k3<xlIndex.size(); k3++){
if(xlIndex[k3]<0) continue;
ions[iIndex].reset();
ions[iIndex].buildLoopIons(spec->getLink(xlIndex[k3]).mass, (int)k, (int)k2);
ions[iIndex].modLoopIonsRec2(0, (int)k, (int)k2, 0, 0, true);
for (j = 0; j<ions[iIndex].size(); j++){
bt = spec->getBoundaries2(ions[iIndex][j].mass, params.ppmPrecursor, index, scanBuffer[iIndex]);
if (bt) scoreSpectra(index, j, 0, pepIndex, -1, (int)k, (int)k2, xlIndex[k3], iIndex,site1,site2);
}
} //k3
}
} //k2
}//x
}
}//k
soloLoop[pepIndex] = true;
return true;
}
bool KAnalysis::analyzeSinglets(kPeptide& pep, int index, double lowLinkMass, double highLinkMass, int iIndex){
int i;
size_t j;
int k;
int len;
char mot[10];
char site[10];
int m,n,c;
double minMass;
double maxMass;
vector<int> scanIndex;
string pepSeq;
bool bSearch;
int counterMotif;
int xlIndex;
double xlMass;
//get the peptide sequence
db->getPeptideSeq(pep,pepSeq);
//Set Mass boundaries
if(firstPass){
minMass = pep.mass + lowLinkMass + params.minPepMass;
maxMass = pep.mass*2 + highLinkMass; //any higher, and this would be the smaller peptide of the cross-link
} else {
minMass = pep.mass*2 + lowLinkMass; //any lower, and this would be the larger peptide of the cross-link
maxMass = pep.mass + highLinkMass + params.maxPepMass;
}
minMass-=(minMass/1000000*params.ppmPrecursor);
maxMass+=(maxMass/1000000*params.ppmPrecursor);
//Find mod mass as difference between precursor and peptide
len=(pep.map->at(0).stop-pep.map->at(0).start)+1;
ions[iIndex].setPeptide(true, &db->at(pep.map->at(0).index).sequence[pep.map->at(0).start], len, pep.mass, pep.nTerm, pep.cTerm, pep.n15);
//Iterate every link site
for(k=0;k<len;k++){
m=0; //number of motifs (linker-to-site combinations) found in the peptide
if (k == len - 1 && pep.cTerm){ //check if we are at the c-terminus on a c-terminal peptide
i=0;
while (xlTable['c'][i]>-1){ //check if c-terminus can be linked
for (n=0;n<m;n++){ //if we've seen motif(s) already, check if we are seeing them again
if (xlTable['c'][i]==mot[n]) break;
}
if (n==m) { //we have a new motif
site[m]='c'; //mark it as c-terminal
mot[m++] = xlTable['c'][i]; //mark the corresponding site
}
i++;
}
} else if (k == len - 1) { //if we are at the c-term of any other peptide, stop the analysis because it cannot be linked.
continue;
} else {
i=0;
while (xlTable[pepSeq[k]][i]>-1){ //check if amino acid can be linked
for (n = 0; n<m; n++){
if (xlTable[pepSeq[k]][i] == mot[n]) break;
}
if (n == m) {
site[m]=pepSeq[k];
mot[m++] = xlTable[pepSeq[k]][i];
}
i++;
}
if (/*m==0 &&*/ k == 0 && pep.nTerm){ // /*if it cannot be linked,*/ but is the n-terminus of a protein, check for a linker
i = 0;
while (xlTable['n'][i]>-1){
for (n = 0; n<m; n++){
if (xlTable['n'][i] == mot[n]) break;
}
if (n == m) {
site[m] = 'n';
mot[m++] = xlTable['n'][i];
}
i++;
}
}
}
if (m==0) continue; //no matching motifs, so check next site on peptide
//build fragment ions and score against all potential spectra
ions[iIndex].reset();
ions[iIndex].buildSingletIons(k);
ions[iIndex].modIonsRec2(0,k,0,0,true);
//ions[iIndex].makeIonIndex(params.binSize, params.binOffset);
//iterate through all ion sets
for(i=0;i<ions[iIndex].size();i++){
for(j=0;j<ions[iIndex][i].len;j++){
if (ions[iIndex][i].mods[j]==0) continue;
}
//Iterate all spectra from (peptide mass + low linker + minimum mass) to (peptide mass + high linker + maximum mass)
if (!spec->getBoundaries(minMass + ions[iIndex][i].difMass, maxMass + ions[iIndex][i].difMass, scanIndex, scanBuffer[iIndex])) continue;
//This set of iterations is slow because of the amount of iterating.
for (n = 0; n < m; n++){ //iterate over sites
for(c=0;c<10;c++){ //check all crosslinkers that bind to this site
counterMotif = spec->getCounterMotif(mot[n], c);
if (counterMotif>-1){ //only check peptide if it has a counterpart at this link site.
xlIndex = spec->getXLIndex((int)mot[n], c);
xlMass = spec->getLink(xlIndex).mass;
for (j = 0; j<scanIndex.size(); j++){ //iterate over all potential spectra
bSearch = scoreSingletSpectra2(scanIndex[j], i, ions[iIndex][i].mass, xlMass, counterMotif, len, index, (char)k, minMass, iIndex, site[n], xlIndex);
}
} else {
break; //no countermotif means no more crosslinkers
}
}
}
}
}
return true;
}
/* Deprecating
bool KAnalysis::analyzeSingletsNC(kPeptide& pep, int index, int iIndex){
int len;
int i;
size_t j;
double minMass;
double maxMass;
vector<int> scanIndex;
//Set Mass boundaries
minMass = pep.mass + params.minPepMass;
maxMass = pep.mass + params.maxPepMass;
minMass -= (minMass / 1000000 * params.ppmPrecursor);
maxMass += (maxMass / 1000000 * params.ppmPrecursor);
//Find mod mass as difference between precursor and peptide
len = (pep.map->at(0).stop - pep.map->at(0).start) + 1;
ions[iIndex].setPeptide(true, &db->at(pep.map->at(0).index).sequence[pep.map->at(0).start], len, pep.mass, pep.nTerm, pep.cTerm, pep.n15);
//build fragment ions and score against all potential spectra
ions[iIndex].reset();
ions[iIndex].buildIons();
ions[iIndex].modIonsRec2(0, -1, 0, 0, false);
//ions[iIndex].makeIonIndex(params.binSize, params.binOffset);
//iterate through all ion sets
for (i = 0; i<ions[iIndex].size(); i++){
//Iterate all spectra from (peptide mass + minimum mass) to (peptide mass + maximum mass)
if (!spec->getBoundaries(minMass + ions[iIndex][i].difMass, maxMass + ions[iIndex][i].difMass, scanIndex, scanBuffer[iIndex])) return false;
for (j = 0; j<scanIndex.size(); j++){
scoreSingletSpectra(scanIndex[j], i, ions[iIndex][i].mass, len, index, -1, minMass, iIndex);
}
}
return true;
}
*/
/*============================
Private Functions
============================*/
bool KAnalysis::allocateMemory(int threads){
size_t j,k;
bKIonsManager = new bool[threads];
ions = new KIons[threads];
scanBuffer = new bool*[threads];
peakMatches = new pair<int,int>*[threads];
peakMatchCount = new int[threads];
for(int i=0;i<threads;i++) {
bKIonsManager[i]=false;
ions[i].setModFlags(params.monoLinksOnXL,params.diffModsOnXL);
ions[i].setSeries(params.ionSeries[0],params.ionSeries[1],params.ionSeries[2],params.ionSeries[3],params.ionSeries[4], params.ionSeries[5]);
scanBuffer[i] = new bool[spec->size()];
peakMatches[i] = new pair<int,int>[1000];
for(j=0;j<params.xLink->size();j++){
for(k=0;k<params.xLink->at(j).motifA.size();k++){
ions[i].site[params.xLink->at(j).motifA[k]]=true;
}
for (k = 0; k<params.xLink->at(j).motifB.size(); k++){
ions[i].site[params.xLink->at(j).motifB[k]] = true;
}
}
}
return true;
}
void KAnalysis::checkXLMotif(int motifA, char* motifB, vector<int>& v){
int i, j;
int cm;
v.clear();
for (i = 0; i<10; i++){
cm = spec->getCounterMotif(motifA, i);
if (cm<0) return;
for (j = 0; j<20; j++){
if (motifB[j]<0) break;
if (motifB[j] == cm) {
v.push_back(spec->getXLIndex(motifA, i));
}
}
}
return;
}
void KAnalysis::deallocateMemory(int threads){
delete [] bKIonsManager;
delete [] ions;
for (int i = 0; i < threads; i++){
delete[] scanBuffer[i];
delete[] peakMatches[i];
}
delete[] scanBuffer;
delete[] peakMatches;
delete[] peakMatchCount;
}
int KAnalysis::findMass(kSingletScoreCardPlus* s, int sz, double mass){
int lower=0;
int mid=sz/2;
int upper=sz;
//binary search to closest mass
while(s[mid].mass!=mass){
if(lower>=upper) break;
if(mass<s[mid].mass){
upper=mid-1;
mid=(lower+upper)/2;
} else {
lower=mid+1;
mid=(lower+upper)/2;
}
if(mid==sz) {
mid--;
break;
}
}
return mid;
}
//Breakdown of the many parameters:
// index = spectrum index in data spectra object
// sIndex = ion set index
// mass = peptide mass (including all modifications, but not the linker)
// xlMass = cross-linker mass
// counterMotif = index of complementary linker site (the one on the "unknown" peptide).
// len = peptide length (amino acids)
// pep = peptide index in database array
// k = link site position on peptide (relative to peptide)
// minMass = lowest allowed mass boundary on spectrum (for spectra with multiple possible precursors)
// iIndex = thread index
// linkSite = amino acid being linked (or 'n' or 'c')
// linkIndex = motif index of linked amino acid or terminus
bool KAnalysis::scoreSingletSpectra2(int index, int sIndex, double mass, double xlMass, int counterMotif, int len, int pep, char k, double minMass, int iIndex, char linkSite, int linkIndex){
kSingletScoreCard sc;
KIonSet* iset;
kPepMod mod;
float score = 0;
float sharedScore,tss;
float alphaScore, betaScore;
float alphaUnique,betaUnique;
float alphaCP=0;
float betaCP=0;
double cpScore=0;
bool bCPScore=false;
int matches;
int conFrag;
int i,j,y;
vector<kPepMod> v;
double low,high,m;
int lowI,highI;
bool bScored=false;
bool ret;
int max = (int)((params.maxPepMass + 1000) / 0.015);
int alpha;
KSpectrum* s = spec->getSpectrum(index);
kPrecursor* p;
KTopPeps* tp;
kScoreCard protSC;
size_t x;
int sz = s->sizePrecursor();
string seq1,seq2;
int matchStart=0;
for (i = 0; i<sz; i++){
p = s->getPrecursor2(i);
if(!firstPass && (p->monoMass-spec->getLink(linkIndex).mass+0.2)/2>mass){
tp = s->getTopPeps(i);
list<kSingletScoreCard>::iterator tsc = tp->singletList.begin();
while(tsc!=tp->singletList.end()){
if(fabs((p->monoMass-tsc->mass-spec->getLink(linkIndex).mass-mass)/p->monoMass*1e6)>params.ppmPrecursor){
tsc++;
continue;
}
if(!spec->checkLink(linkSite,tsc->site,linkIndex)){
tsc++;
continue;
}
//This is inefficient because it must be recalculated for each alpha peptide; each alpha peptide has a different set of already scored peaks
sharedScore=0;
if(params.cleavageProducts->size()>0){
cpScore = kojakScoringCleavableBeta(index, sIndex, iIndex,tsc->peakMatches,tss);
sharedScore+=tss;
betaCP=(float)cpScore+tss/2;
alphaCP=tsc->cpScore-tss/2;
}
//if (tsc->pep1!=pep || tsc->k1!=k){ //Not doing special case for same peptide. This way, it still works with modifications.
score = kojakScoringBeta(index, p->monoMass - mass, sIndex, iIndex, matches, conFrag, tsc->peakMatches, tss,p->charge);
score+=(float)cpScore;
sharedScore+=tss;
protSC.simpleScore = tsc->simpleScore + score;
//split the shared score between alpha and beta
alphaScore=tsc->simpleScore-sharedScore/2;
betaScore=score+sharedScore/2;
//Keep this for now. Unique scoring for each peptide (not shared with the other) is an interesting metric that needs further exploration.
if (tsc->pep1 != pep || tsc->k1 != k){
alphaUnique=tsc->simpleScore-sharedScore;
betaUnique=score-sharedScore;
} else {
alphaUnique=betaUnique=tsc->simpleScore;
}
y = (int)(protSC.simpleScore * 10.0 + 0.5);
if (y >= HISTOSZ) y = HISTOSZ - 1;
Threading::LockMutex(mutexSpecScore[index]); //no matter how low the score, put this test in our histogram.
s->histogram[y]++;
s->histogramCount++;
//if (alphaUnique<params.minPepScore || betaUnique<params.minPepScore || protSC.simpleScore <= s->lowScore) { //maybe use unique scoring instead? Needs special case for self-peptides
if (alphaScore<params.minPepScore || betaScore<params.minPepScore || protSC.simpleScore <= s->lowScore) { //peptide needs a minimum score, and combined score should exceed bottom of best hits
Threading::UnlockMutex(mutexSpecScore[index]);
tsc++;
continue;
} else if(params.minPepUnique>0 && (alphaUnique<params.minPepUnique || betaScore<params.minPepUnique)){
Threading::UnlockMutex(mutexSpecScore[index]);
tsc++;
continue;
}
Threading::UnlockMutex(mutexSpecScore[index]);
//} else {
// //if we got here, it is because the alpha and beta peptides are the same and linked in the same place. Warning: I'm not checking modifications...
// protSC.simpleScore = tsc->simpleScore;
// score=0;
// cpScore=0;
// y = (int)(protSC.simpleScore * 10.0 + 0.5);
// if (y >= HISTOSZ) y = HISTOSZ - 1;
// Threading::LockMutex(mutexSpecScore[index]); //no matter how low the score, put this test in our histogram.
// s->histogram[y]++;
// s->histogramCount++;
// Threading::UnlockMutex(mutexSpecScore[index]);
//}
protSC.mods1.clear();
protSC.mods2.clear();
//alphabetize cross-linked peptides before storing them; this prevents confusion with duplications downstream
//instead of alphabetical, order them by mass (larger first), this has implications with downstream scoring...
//resort to alphabetical in case of equal mass
bool bSecond=false;
if(mass==tsc->mass){
if(seq2.size()==0) ret=db->getPeptideSeq(pep,seq2);
ret=db->getPeptideSeq(tsc->pep1, seq1);
alpha=seq1.compare(seq2);
if (alpha>0 || (alpha == 0 && k<tsc->k1)) bSecond=true;
}
if (mass>tsc->mass || bSecond){ //pep2 listed first
protSC.k1 = k;
protSC.k2 = tsc->k1;
protSC.site1 = linkSite;
protSC.site2 = tsc->site;
protSC.pep1 = pep;
protSC.pep2 = tsc->pep1;
protSC.score1 = betaScore; //score;
protSC.score2 = alphaScore; //tsc->simpleScore;
protSC.cpScore1 = betaCP; //(float)cpScore;
protSC.cpScore2 = alphaCP; //tsc->cpScore;
protSC.uScore1 = betaUnique;
protSC.uScore2 = alphaUnique;
protSC.mass1 = mass;
protSC.mass2 = tsc->mass;
protSC.matches1 = matches;
protSC.matches2 = tsc->matches;
protSC.conFrag1 = conFrag;
protSC.conFrag2 = tsc->conFrag;
for (x = 0; x<tsc->modLen; x++) protSC.mods2.push_back(tsc->mods[x]);
ret=true; //indicates where to put mods to pep2
} else { //pep1 listed first
protSC.k1 = tsc->k1;
protSC.k2 = k;
protSC.site1 = tsc->site;
protSC.site2 = linkSite;
protSC.pep1 = tsc->pep1;
protSC.pep2 = pep;
protSC.score1 = alphaScore; //tsc->simpleScore;
protSC.score2 = betaScore; //score;
protSC.cpScore1 = alphaCP; //tsc->cpScore;
protSC.cpScore2 = betaCP; //(float)cpScore;
protSC.uScore1 = alphaUnique;
protSC.uScore2 = betaUnique;
protSC.mass1 = tsc->mass;
protSC.mass2 = mass;
protSC.matches1 = tsc->matches;
protSC.matches2 = matches;
protSC.conFrag1 = tsc->conFrag;
protSC.conFrag2 = conFrag;
for (x = 0; x<tsc->modLen; x++) protSC.mods1.push_back(tsc->mods[x]);
ret=false; //indicates where to put mods to pep2
}
protSC.mass = tsc->mass + spec->getLink(linkIndex).mass + mass;
protSC.linkable1 = tsc->linkable;
protSC.linkable2 = false;
protSC.link = linkIndex;
protSC.precursor = (char)i;
//special case for identical peptides
//if(protSC.pep2==protSC.pep1 && protSC.k1==protSC.k2){
// protSC.score1=protSC.score2 = tsc->simpleScore/2;
// protSC.cpScore1=protSC.cpScore2=tsc->cpScore/2;
// protSC.matches1=protSC.matches2=tsc->matches;
// protSC.conFrag1=protSC.conFrag2=tsc->conFrag;
// //continue; //WARNING...JUST SKIPPING THESE...
//}
//index the mods for pep2
iset = ions[iIndex].at(sIndex);
if (iset->difMass != 0){
for (j = 0; j<ions[iIndex].getIonCount(); j++) {
if (iset->mods[j] != 0){
if(j==0){
if(iset->nTermMass!=0){
mod.pos=-1;
mod.mass=iset->nTermMass;
if (ret) protSC.mods1.push_back(mod);
else protSC.mods2.push_back(mod);
}
if(fabs(iset->mods[j]-iset->nTermMass)<0.0001) continue;
}
if (j == ions[iIndex].getIonCount() - 1){
if (iset->cTermMass != 0){
mod.pos = -2;
mod.mass = iset->cTermMass;
if (ret) protSC.mods1.push_back(mod);
else protSC.mods2.push_back(mod);
}
if (fabs(iset->mods[j] - iset->cTermMass)<0.0001) continue;
}
//if (j == 0 && iset->modNTerm) mod.term = true;
//else if (j == ions[iIndex].getIonCount() - 1 && iset->modCTerm) mod.term = true;
//else mod.term = false;
mod.pos = (char)j;
mod.mass = iset->mods[j];
if(ret) protSC.mods1.push_back(mod);
else protSC.mods2.push_back(mod);
}
}
}
Threading::LockMutex(mutexSpecScore[index]);
s->checkScore(protSC,listXL);
Threading::UnlockMutex(mutexSpecScore[index]);
tsc++;
}
}
if (firstPass && mass>(p->monoMass - spec->getLink(linkIndex).mass) / 2-0.2){
low = p->monoMass;
high = low;
m = low / 1000000 * params.ppmPrecursor;
low -= m;
high += m;
m = mass + xlMass;
low -= m;
high -= m;
lowI = (int)(low/0.015);
highI = (int)(high/0.015)+1;
if (lowI<0) lowI=0;
if (highI>max) highI=max;
while (lowI < highI){
if (pepBin[counterMotif][lowI]) break;
lowI++;
}
if (lowI >= highI) continue;
if (!bCPScore && params.cleavageProducts->size()>0){
peakMatchCount[iIndex]=matchStart;
cpScore = kojakScoringCleavableAlpha(index, sIndex, iIndex);
bCPScore = true;
matchStart=peakMatchCount[iIndex];
}
peakMatchCount[iIndex] = matchStart;
score = kojakScoringAlpha(index, p->monoMass - mass, sIndex, iIndex, matches, conFrag, p->charge);
score+=(float)cpScore;
bScored = true;
if(score<params.minPepScore || score<=0 ) continue;
Threading::LockMutex(mutexSingletScore[index][i]);
tp = s->getTopPeps(i);
if(tp->singletList.size()>=tp->singletMax && score<=tp->singletList.rbegin()->simpleScore) {
Threading::UnlockMutex(mutexSingletScore[index][i]);
continue; //don't bother with the singlet overhead if it won't make the list
}
Threading::UnlockMutex(mutexSingletScore[index][i]);
sc.len = len;