forked from mothur/mothur
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ccode.cpp
1091 lines (868 loc) · 35.2 KB
/
ccode.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
/*
* ccode.cpp
* Mothur
*
* Created by westcott on 8/24/09.
* Copyright 2009 Schloss Lab. All rights reserved.
*
*/
#include "ccode.h"
#include "ignoregaps.h"
#include "eachgapdist.h"
//***************************************************************************************************************
Ccode::Ccode(string filename, string temp, bool f, string mask, int win, int numW, string o) : Chimera() {
try {
fastafile = filename;
outputDir = o;
templateFileName = temp; templateSeqs = readSeqs(temp);
setMask(mask);
filter = f;
window = win;
numWanted = numW;
distCalc = new eachGapDist();
decalc = new DeCalculator();
mapInfo = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "mapinfo";
#ifdef USE_MPI
//char* inFileName = new char[mapInfo.length()];
//memcpy(inFileName, mapInfo.c_str(), mapInfo.length());
char inFileName[1024];
strcpy(inFileName, mapInfo.c_str());
int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
MPI_File_open(MPI_COMM_WORLD, inFileName, outMode, MPI_INFO_NULL, &outMap); //comm, filename, mode, info, filepointer
//delete inFileName;
int pid;
MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
if (pid == 0) {
string outString = "Place in masked, filtered and trimmed sequence\tPlace in original alignment\n";
MPI_Status status;
int length = outString.length();
char* buf2 = new char[length];
memcpy(buf2, outString.c_str(), length);
MPI_File_write_shared(outMap, buf2, length, MPI_CHAR, &status);
delete buf2;
}
#else
ofstream out2;
m->openOutputFile(mapInfo, out2);
out2 << "Place in masked, filtered and trimmed sequence\tPlace in original alignment" << endl;
out2.close();
#endif
}
catch(exception& e) {
m->errorOut(e, "Ccode", "Ccode");
exit(1);
}
}
//***************************************************************************************************************
Ccode::~Ccode() {
delete distCalc;
delete decalc;
#ifdef USE_MPI
MPI_File_close(&outMap);
#endif
}
//***************************************************************************************************************
Sequence Ccode::print(ostream& out, ostream& outAcc) {
try {
ofstream out2;
m->openOutputFileAppend(mapInfo, out2);
out2 << querySeq->getName() << endl;
for (it = spotMap.begin(); it!= spotMap.end(); it++) {
out2 << it->first << '\t' << it->second << endl;
}
out2.close();
out << querySeq->getName() << endl << endl << "Reference sequences used and distance to query:" << endl;
for (int j = 0; j < closest.size(); j++) {
out << closest[j].seq->getName() << '\t' << closest[j].dist << endl;
}
out << endl << endl;
//for each window
//window mapping info.
out << "Mapping information: ";
//you mask and did not filter
if ((seqMask != "") && (!filter)) { out << "mask and trim."; }
//you filtered and did not mask
if ((seqMask == "") && (filter)) { out << "filter and trim."; }
//you masked and filtered
if ((seqMask != "") && (filter)) { out << "mask, filter and trim."; }
out << endl << "Window\tStartPos\tEndPos" << endl;
it = trim.begin();
for (int k = 0; k < windows.size()-1; k++) {
out << k+1 << '\t' << spotMap[windows[k]-it->first] << '\t' << spotMap[windows[k]-it->first+windowSizes] << endl;
}
out << windows.size() << '\t' << spotMap[windows[windows.size()-1]-it->first] << '\t' << spotMap[it->second-it->first-1] << endl;
out << endl;
out << "Window\tAvgQ\t(sdQ)\tAvgR\t(sdR)\tRatio\tAnova" << endl;
for (int k = 0; k < windows.size(); k++) {
float ds = averageQuery[k] / averageRef[k];
out << k+1 << '\t' << averageQuery[k] << '\t' << sdQuery[k] << '\t' << averageRef[k] << '\t'<< sdRef[k] << '\t' << ds << '\t' << anova[k] << endl;
}
out << endl;
//varRef
//varQuery
/* F test for differences among variances.
* varQuery is expected to be higher or similar than varRef */
//float fs = varQuery[query] / varRef[query]; /* F-Snedecor, test for differences of variances */
bool results = false;
//confidence limit, t - Student, anova
out << "Window\tConfidenceLimit\tt-Student\tAnova" << endl;
for (int k = 0; k < windows.size(); k++) {
string temp = "";
if (isChimericConfidence[k]) { temp += "*\t"; }
else { temp += "\t"; }
if (isChimericTStudent[k]) { temp += "*\t"; }
else { temp += "\t"; }
if (isChimericANOVA[k]) { temp += "*\t"; }
else { temp += "\t"; }
out << k+1 << '\t' << temp << endl;
if (temp == "*\t*\t*\t") { results = true; }
}
out << endl;
if (results) {
m->mothurOut(querySeq->getName() + " was found have at least one chimeric window."); m->mothurOutEndLine();
outAcc << querySeq->getName() << endl;
}
//free memory
for (int i = 0; i < closest.size(); i++) { delete closest[i].seq; }
return *querySeq;
}
catch(exception& e) {
m->errorOut(e, "Ccode", "print");
exit(1);
}
}
#ifdef USE_MPI
//***************************************************************************************************************
Sequence Ccode::print(MPI_File& out, MPI_File& outAcc) {
try {
string outMapString = "";
outMapString += querySeq->getName() + "\n";
for (it = spotMap.begin(); it!= spotMap.end(); it++) {
outMapString += toString(it->first) + "\t" + toString(it->second) + "\n";
}
printMapping(outMapString);
outMapString = "";
string outString = "";
string outAccString = "";
outString += querySeq->getName() + "\n\nReference sequences used and distance to query:\n";
for (int j = 0; j < closest.size(); j++) {
outString += closest[j].seq->getName() + "\t" + toString(closest[j].dist) + "\n";
}
outString += "\n\nMapping information: ";
//for each window
//window mapping info.
//you mask and did not filter
if ((seqMask != "") && (!filter)) { outString += "mask and trim."; }
//you filtered and did not mask
if ((seqMask == "") && (filter)) { outString += "filter and trim."; }
//you masked and filtered
if ((seqMask != "") && (filter)) { outString += "mask, filter and trim."; }
outString += "\nWindow\tStartPos\tEndPos\n";
it = trim.begin();
for (int k = 0; k < windows.size()-1; k++) {
outString += toString(k+1) + "\t" + toString(spotMap[windows[k]-it->first]) + "\t" + toString(spotMap[windows[k]-it->first+windowSizes]) + "\n";
}
outString += toString(windows.size()) + "\t" + toString(spotMap[windows[windows.size()-1]-it->first]) + "\t" + toString(spotMap[it->second-it->first-1]) + "\n\n";
outString += "Window\tAvgQ\t(sdQ)\tAvgR\t(sdR)\tRatio\tAnova\n";
for (int k = 0; k < windows.size(); k++) {
float ds = averageQuery[k] / averageRef[k];
outString += toString(k+1) + "\t" + toString(averageQuery[k]) + "\t" + toString(sdQuery[k]) + "\t" + toString(averageRef[k]) + "\t" + toString(sdRef[k]) + "\t" + toString(ds) + "\t" + toString(anova[k]) + "\n";
}
//varRef
//varQuery
/* F test for differences among variances.
* varQuery is expected to be higher or similar than varRef */
//float fs = varQuery[query] / varRef[query]; /* F-Snedecor, test for differences of variances */
bool results = false;
//confidence limit, t - Student, anova
outString += "\nWindow\tConfidenceLimit\tt-Student\tAnova\n";
for (int k = 0; k < windows.size(); k++) {
string temp = "";
if (isChimericConfidence[k]) { temp += "*\t"; }
else { temp += "\t"; }
if (isChimericTStudent[k]) { temp += "*\t"; }
else { temp += "\t"; }
if (isChimericANOVA[k]) { temp += "*\t"; }
else { temp += "\t"; }
outString += toString(k+1) + "\t" + temp + "\n";
if (temp == "*\t*\t*\t") { results = true; }
}
outString += "\n";
MPI_Status status;
int length = outString.length();
char* buf2 = new char[length];
memcpy(buf2, outString.c_str(), length);
MPI_File_write_shared(out, buf2, length, MPI_CHAR, &status);
delete buf2;
if (results) {
m->mothurOut(querySeq->getName() + " was found have at least one chimeric window."); m->mothurOutEndLine();
outAccString += querySeq->getName() + "\n";
MPI_Status statusAcc;
length = outAccString.length();
char* buf = new char[length];
memcpy(buf, outAccString.c_str(), length);
MPI_File_write_shared(outAcc, buf, length, MPI_CHAR, &statusAcc);
delete buf;
}
//free memory
for (int i = 0; i < closest.size(); i++) { delete closest[i].seq; }
return *querySeq;
}
catch(exception& e) {
m->errorOut(e, "Ccode", "print");
exit(1);
}
}
//***************************************************************************************************************
int Ccode::printMapping(string& output) {
try {
MPI_Status status;
int length = output.length();
char* buf = new char[length];
memcpy(buf, output.c_str(), length);
MPI_File_write_shared(outMap, buf, length, MPI_CHAR, &status);
delete buf;
return 0;
}
catch(exception& e) {
m->errorOut(e, "Ccode", "printMapping");
exit(1);
}
}
#endif
//***************************************************************************************************************
int Ccode::getChimeras(Sequence* query) {
try {
closest.clear();
refCombo = 0;
sumRef.clear();
varRef.clear();
varQuery.clear();
sdRef.clear();
sdQuery.clear();
sumQuery.clear();
sumSquaredRef.clear();
sumSquaredQuery.clear();
averageRef.clear();
averageQuery.clear();
anova.clear();
isChimericConfidence.clear();
isChimericTStudent.clear();
isChimericANOVA.clear();
trim.clear();
spotMap.clear();
windowSizes = window;
windows.clear();
querySeq = query;
//find closest matches to query
closest = findClosest(query, numWanted);
if (m->control_pressed) { return 0; }
//initialize spotMap
for (int i = 0; i < query->getAligned().length(); i++) { spotMap[i] = i; }
//mask sequences if the user wants to
if (seqMask != "") {
decalc->setMask(seqMask);
decalc->runMask(query);
//mask closest
for (int i = 0; i < closest.size(); i++) { decalc->runMask(closest[i].seq); }
spotMap = decalc->getMaskMap();
}
if (filter) {
vector<Sequence*> temp;
for (int i = 0; i < closest.size(); i++) { temp.push_back(closest[i].seq); }
temp.push_back(query);
createFilter(temp, 0.5);
for (int i = 0; i < temp.size(); i++) {
if (m->control_pressed) { return 0; }
runFilter(temp[i]);
}
//update spotMap
map<int, int> newMap;
int spot = 0;
for (int i = 0; i < filterString.length(); i++) {
if (filterString[i] == '1') {
//add to newMap
newMap[spot] = spotMap[i];
spot++;
}
}
spotMap = newMap;
}
//trim sequences - this follows ccodes remove_extra_gaps
trimSequences(query);
if (m->control_pressed) { return 0; }
//windows are equivalent to words - ccode paper recommends windows are between 5% and 20% on alignment length().
//Our default will be 10% and we will warn if user tries to use a window above or below these recommendations
windows = findWindows();
if (m->control_pressed) { return 0; }
//remove sequences that are more than 20% different and less than 0.5% different - may want to allow user to specify this later
removeBadReferenceSeqs(closest);
if (m->control_pressed) { return 0; }
//find the averages for each querys references
getAverageRef(closest); //fills sumRef, averageRef, sumSquaredRef and refCombo.
getAverageQuery(closest, query); //fills sumQuery, averageQuery, sumSquaredQuery.
if (m->control_pressed) { return 0; }
//find the averages for each querys references
findVarianceRef(); //fills varRef and sdRef also sets minimum error rate to 0.001 to avoid divide by 0.
if (m->control_pressed) { return 0; }
//find the averages for the query
findVarianceQuery(); //fills varQuery and sdQuery also sets minimum error rate to 0.001 to avoid divide by 0.
if (m->control_pressed) { return 0; }
determineChimeras(); //fills anova, isChimericConfidence, isChimericTStudent and isChimericANOVA.
if (m->control_pressed) { return 0; }
return 0;
}
catch(exception& e) {
m->errorOut(e, "Ccode", "getChimeras");
exit(1);
}
}
/***************************************************************************************************************/
//ccode algo says it does this to "Removes the initial and final gaps to avoid biases due to incomplete sequences."
void Ccode::trimSequences(Sequence* query) {
try {
int frontPos = 0; //should contain first position in all seqs that is not a gap character
int rearPos = query->getAligned().length();
//********find first position in closest seqs that is a non gap character***********//
//find first position all query seqs that is a non gap character
for (int i = 0; i < closest.size(); i++) {
string aligned = closest[i].seq->getAligned();
int pos = 0;
//find first spot in this seq
for (int j = 0; j < aligned.length(); j++) {
if (isalpha(aligned[j])) {
pos = j;
break;
}
}
//save this spot if it is the farthest
if (pos > frontPos) { frontPos = pos; }
}
//find first position all querySeq[query] that is a non gap character
string aligned = query->getAligned();
int pos = 0;
//find first spot in this seq
for (int j = 0; j < aligned.length(); j++) {
if (isalpha(aligned[j])) {
pos = j;
break;
}
}
//save this spot if it is the farthest
if (pos > frontPos) { frontPos = pos; }
//********find last position in closest seqs that is a non gap character***********//
for (int i = 0; i < closest.size(); i++) {
string aligned = closest[i].seq->getAligned();
int pos = aligned.length();
//find first spot in this seq
for (int j = aligned.length()-1; j >= 0; j--) {
if (isalpha(aligned[j])) {
pos = j;
break;
}
}
//save this spot if it is the farthest
if (pos < rearPos) { rearPos = pos; }
}
//find last position all querySeqs[query] that is a non gap character
aligned = query->getAligned();
pos = aligned.length();
//find first spot in this seq
for (int j = aligned.length()-1; j >= 0; j--) {
if (isalpha(aligned[j])) {
pos = j;
break;
}
}
//save this spot if it is the farthest
if (pos < rearPos) { rearPos = pos; }
//check to make sure that is not whole seq
if ((rearPos - frontPos - 1) <= 0) { m->mothurOut("Error, when I trim your sequences, the entire sequence is trimmed."); m->mothurOutEndLine(); exit(1); }
map<int, int> tempTrim;
tempTrim[frontPos] = rearPos;
//save trimmed locations
trim = tempTrim;
//update spotMask
map<int, int> newMap;
int spot = 0;
for (int i = frontPos; i < rearPos; i++) {
//add to newMap
newMap[spot] = spotMap[i];
spot++;
}
spotMap = newMap;
}
catch(exception& e) {
m->errorOut(e, "Ccode", "trimSequences");
exit(1);
}
}
/***************************************************************************************************************/
vector<int> Ccode::findWindows() {
try {
vector<int> win;
it = trim.begin();
int length = it->second - it->first;
//default is wanted = 10% of total length
if (windowSizes > length) {
m->mothurOut("You have slected a window larger than your sequence length after all filters, masks and trims have been done. I will use the default 10% of sequence length.");
windowSizes = length / 10;
}else if (windowSizes == 0) { windowSizes = length / 10; }
else if (windowSizes > (length * 0.20)) {
m->mothurOut("You have selected a window that is larger than 20% of your sequence length. This is not recommended, but I will continue anyway."); m->mothurOutEndLine();
}else if (windowSizes < (length * 0.05)) {
m->mothurOut("You have selected a window that is smaller than 5% of your sequence length. This is not recommended, but I will continue anyway."); m->mothurOutEndLine();
}
//save starting points of each window
for (int m = it->first; m < (it->second-windowSizes); m+=windowSizes) { win.push_back(m); }
//save last window
if (win[win.size()-1] < (it->first+length)) {
win.push_back(win[win.size()-1]+windowSizes); // ex. string length is 115, window is 25, without this you would get 0, 25, 50, 75
} //with this you would get 1,25,50,75,100
return win;
}
catch(exception& e) {
m->errorOut(e, "Ccode", "findWindows");
exit(1);
}
}
//***************************************************************************************************************
int Ccode::getDiff(string seqA, string seqB) {
try {
int numDiff = 0;
for (int i = 0; i < seqA.length(); i++) {
//if you are both not gaps
//if (isalpha(seqA[i]) && isalpha(seqA[i])) {
//are you different
if (seqA[i] != seqB[i]) {
int ok; /* ok=1 means equivalent base. Checks for degenerate bases */
/* the char in base_a and base_b have been checked and they are different */
if ((seqA[i] == 'N') && (seqB[i] != '-')) ok = 1;
else if ((seqB[i] == 'N') && (seqA[i] != '-')) ok = 1;
else if ((seqA[i] == 'Y') && ((seqB[i] == 'C') || (seqB[i] == 'T'))) ok = 1;
else if ((seqB[i] == 'Y') && ((seqA[i] == 'C') || (seqA[i] == 'T'))) ok = 1;
else if ((seqA[i] == 'R') && ((seqB[i] == 'G') || (seqB[i] == 'A'))) ok = 1;
else if ((seqB[i] == 'R') && ((seqA[i] == 'G') || (seqA[i] == 'A'))) ok = 1;
else if ((seqA[i] == 'S') && ((seqB[i] == 'C') || (seqB[i] == 'G'))) ok = 1;
else if ((seqB[i] == 'S') && ((seqA[i] == 'C') || (seqA[i] == 'G'))) ok = 1;
else if ((seqA[i] == 'W') && ((seqB[i] == 'T') || (seqB[i] == 'A'))) ok = 1;
else if ((seqB[i] == 'W') && ((seqA[i] == 'T') || (seqA[i] == 'A'))) ok = 1;
else if ((seqA[i] == 'M') && ((seqB[i] == 'A') || (seqB[i] == 'C'))) ok = 1;
else if ((seqB[i] == 'M') && ((seqA[i] == 'A') || (seqA[i] == 'C'))) ok = 1;
else if ((seqA[i] == 'K') && ((seqB[i] == 'T') || (seqB[i] == 'G'))) ok = 1;
else if ((seqB[i] == 'K') && ((seqA[i] == 'T') || (seqA[i] == 'G'))) ok = 1;
else if ((seqA[i] == 'V') && ((seqB[i] == 'C') || (seqB[i] == 'A') || (seqB[i] == 'G'))) ok = 1;
else if ((seqB[i] == 'V') && ((seqA[i] == 'C') || (seqA[i] == 'A') || (seqA[i] == 'G'))) ok = 1;
else if ((seqA[i] == 'H') && ((seqB[i] == 'T') || (seqB[i] == 'A') || (seqB[i] == 'C'))) ok = 1;
else if ((seqB[i] == 'H') && ((seqA[i] == 'T') || (seqA[i] == 'A') || (seqA[i] == 'C'))) ok = 1;
else if ((seqA[i] == 'D') && ((seqB[i] == 'T') || (seqB[i] == 'A') || (seqB[i] == 'G'))) ok = 1;
else if ((seqB[i] == 'D') && ((seqA[i] == 'T') || (seqA[i] == 'A') || (seqA[i] == 'G'))) ok = 1;
else if ((seqA[i] == 'B') && ((seqB[i] == 'C') || (seqB[i] == 'T') || (seqB[i] == 'G'))) ok = 1;
else if ((seqB[i] == 'B') && ((seqA[i] == 'C') || (seqA[i] == 'T') || (seqA[i] == 'G'))) ok = 1;
else ok = 0; /* the bases are different and not equivalent */
//check if they are both blanks
if ((seqA[i] == '.') && (seqB[i] == '-')) ok = 1;
else if ((seqB[i] == '.') && (seqA[i] == '-')) ok = 1;
if (ok == 0) { numDiff++; }
}
//}
}
return numDiff;
}
catch(exception& e) {
m->errorOut(e, "Ccode", "getDiff");
exit(1);
}
}
//***************************************************************************************************************
//tried to make this look most like ccode original implementation
void Ccode::removeBadReferenceSeqs(vector<SeqDist>& seqs) {
try {
vector< vector<int> > numDiffBases;
numDiffBases.resize(seqs.size());
//initialize to 0
for (int i = 0; i < numDiffBases.size(); i++) { numDiffBases[i].resize(seqs.size(),0); }
it = trim.begin();
int length = it->second - it->first;
//calc differences from each sequence to everyother seq in the set
for (int i = 0; i < seqs.size(); i++) {
string seqA = seqs[i].seq->getAligned().substr(it->first, length);
//so you don't calc i to j and j to i since they are the same
for (int j = 0; j < i; j++) {
string seqB = seqs[j].seq->getAligned().substr(it->first, length);
//compare strings
int numDiff = getDiff(seqA, seqB);
numDiffBases[i][j] = numDiff;
numDiffBases[j][i] = numDiff;
}
}
//initailize remove to 0
vector<int> remove; remove.resize(seqs.size(), 0);
float top = ((20*length) / (float) 100);
float bottom = ((0.5*length) / (float) 100);
//check each numDiffBases and if any are higher than threshold set remove to 1 so you can remove those seqs from the closest set
for (int i = 0; i < numDiffBases.size(); i++) {
for (int j = 0; j < i; j++) {
//are you more than 20% different
if (numDiffBases[i][j] > top) { remove[j] = 1; }
//are you less than 0.5% different
if (numDiffBases[i][j] < bottom) { remove[j] = 1; }
}
}
int numSeqsLeft = 0;
//count seqs that are not going to be removed
for (int i = 0; i < remove.size(); i++) {
if (remove[i] == 0) { numSeqsLeft++; }
}
//if you have enough then remove bad ones
if (numSeqsLeft >= 3) {
vector<SeqDist> goodSeqs;
//remove bad seqs
for (int i = 0; i < remove.size(); i++) {
if (remove[i] == 0) {
goodSeqs.push_back(seqs[i]);
}
}
seqs = goodSeqs;
}else { //warn, but dont remove any
m->mothurOut(querySeq->getName() + " does not have an adaquate number of reference sequences that are within 20% and 0.5% similarity. I will continue, but please check."); m->mothurOutEndLine();
}
}
catch(exception& e) {
m->errorOut(e, "Ccode", "removeBadReferenceSeqs");
exit(1);
}
}
//***************************************************************************************************************
//makes copy of templateseq for filter
vector<SeqDist> Ccode::findClosest(Sequence* q, int numWanted) {
try{
vector<SeqDist> topMatches;
Sequence query = *(q);
//calc distance to each sequence in template seqs
for (int i = 0; i < templateSeqs.size(); i++) {
Sequence ref = *(templateSeqs[i]);
//find overall dist
distCalc->calcDist(query, ref);
float dist = distCalc->getDist();
//save distance
SeqDist temp;
temp.seq = new Sequence(templateSeqs[i]->getName(), templateSeqs[i]->getAligned());
temp.dist = dist;
topMatches.push_back(temp);
}
sort(topMatches.begin(), topMatches.end(), compareSeqDist);
for (int i = numWanted; i < topMatches.size(); i++) { delete topMatches[i].seq; }
topMatches.resize(numWanted);
return topMatches;
}
catch(exception& e) {
m->errorOut(e, "Ccode", "findClosestSides");
exit(1);
}
}
/**************************************************************************************************/
//find the distances from each reference sequence to every other reference sequence for each window for this query
void Ccode::getAverageRef(vector<SeqDist> ref) {
try {
vector< vector< vector<int> > > diffs; //diffs[0][1][2] is the number of differences between ref seq 0 and ref seq 1 at window 2.
//initialize diffs vector
diffs.resize(ref.size());
for (int i = 0; i < diffs.size(); i++) {
diffs[i].resize(ref.size());
for (int j = 0; j < diffs[i].size(); j++) {
diffs[i][j].resize(windows.size(), 0);
}
}
it = trim.begin();
//find the distances from each reference sequence to every other reference sequence for each window for this query
for (int i = 0; i < ref.size(); i++) {
string refI = ref[i].seq->getAligned();
//j<i, so you don't find distances from i to j and then j to i.
for (int j = 0; j < i; j++) {
string refJ = ref[j].seq->getAligned();
for (int k = 0; k < windows.size(); k++) {
string refIWindowk, refJWindowk;
if (k < windows.size()-1) {
//get window strings
refIWindowk = refI.substr(windows[k], windowSizes);
refJWindowk = refJ.substr(windows[k], windowSizes);
}else { //last window may be smaller than rest - see findwindows
//get window strings
refIWindowk = refI.substr(windows[k], (it->second-windows[k]));
refJWindowk = refJ.substr(windows[k], (it->second-windows[k]));
}
//find differences
int diff = getDiff(refIWindowk, refJWindowk);
//save differences in [i][j][k] and [j][i][k] since they are the same
diffs[i][j][k] = diff;
diffs[j][i][k] = diff;
}//k
}//j
}//i
//initialize sumRef for this query
sumRef.resize(windows.size(), 0);
sumSquaredRef.resize(windows.size(), 0);
averageRef.resize(windows.size(), 0);
//find the sum of the differences for hte reference sequences
for (int i = 0; i < diffs.size(); i++) {
for (int j = 0; j < i; j++) {
//increment this querys reference sequences combos
refCombo++;
for (int k = 0; k < diffs[i][j].size(); k++) {
sumRef[k] += diffs[i][j][k];
sumSquaredRef[k] += (diffs[i][j][k]*diffs[i][j][k]);
}//k
}//j
}//i
//find the average of the differences for the references for each window
for (int i = 0; i < windows.size(); i++) {
averageRef[i] = sumRef[i] / (float) refCombo;
}
}
catch(exception& e) {
m->errorOut(e, "Ccode", "getAverageRef");
exit(1);
}
}
/**************************************************************************************************/
void Ccode::getAverageQuery (vector<SeqDist> ref, Sequence* query) {
try {
vector< vector<int> > diffs; //diffs[1][2] is the number of differences between querySeqs[query] and ref seq 1 at window 2.
//initialize diffs vector
diffs.resize(ref.size());
for (int j = 0; j < diffs.size(); j++) {
diffs[j].resize(windows.size(), 0);
}
it = trim.begin();
string refQuery = query->getAligned();
//j<i, so you don't find distances from i to j and then j to i.
for (int j = 0; j < ref.size(); j++) {
string refJ = ref[j].seq->getAligned();
for (int k = 0; k < windows.size(); k++) {
string QueryWindowk, refJWindowk;
if (k < windows.size()-1) {
//get window strings
QueryWindowk = refQuery.substr(windows[k], windowSizes);
refJWindowk = refJ.substr(windows[k], windowSizes);
}else { //last window may be smaller than rest - see findwindows
//get window strings
QueryWindowk = refQuery.substr(windows[k], (it->second-windows[k]));
refJWindowk = refJ.substr(windows[k], (it->second-windows[k]));
}
//find differences
int diff = getDiff(QueryWindowk, refJWindowk);
//save differences
diffs[j][k] = diff;
}//k
}//j
//initialize sumRef for this query
sumQuery.resize(windows.size(), 0);
sumSquaredQuery.resize(windows.size(), 0);
averageQuery.resize(windows.size(), 0);
//find the sum of the differences
for (int j = 0; j < diffs.size(); j++) {
for (int k = 0; k < diffs[j].size(); k++) {
sumQuery[k] += diffs[j][k];
sumSquaredQuery[k] += (diffs[j][k]*diffs[j][k]);
}//k
}//j
//find the average of the differences for the references for each window
for (int i = 0; i < windows.size(); i++) {
averageQuery[i] = sumQuery[i] / (float) ref.size();
}
}
catch(exception& e) {
m->errorOut(e, "Ccode", "getAverageQuery");
exit(1);
}
}
/**************************************************************************************************/
void Ccode::findVarianceRef() {
try {
varRef.resize(windows.size(), 0);
sdRef.resize(windows.size(), 0);
//for each window
for (int i = 0; i < windows.size(); i++) {
varRef[i] = (sumSquaredRef[i] - ((sumRef[i]*sumRef[i])/(float)refCombo)) / (float)(refCombo-1);
sdRef[i] = sqrt(varRef[i]);
//set minimum error rate to 0.001 - to avoid potential divide by zero - not sure if this is necessary but it follows ccode implementation
if (averageRef[i] < 0.001) { averageRef[i] = 0.001; }
if (sumRef[i] < 0.001) { sumRef[i] = 0.001; }
if (varRef[i] < 0.001) { varRef[i] = 0.001; }
if (sumSquaredRef[i] < 0.001) { sumSquaredRef[i] = 0.001; }
if (sdRef[i] < 0.001) { sdRef[i] = 0.001; }
}
}
catch(exception& e) {
m->errorOut(e, "Ccode", "findVarianceRef");
exit(1);
}
}
/**************************************************************************************************/
void Ccode::findVarianceQuery() {
try {
varQuery.resize(windows.size(), 0);
sdQuery.resize(windows.size(), 0);
//for each window
for (int i = 0; i < windows.size(); i++) {
varQuery[i] = (sumSquaredQuery[i] - ((sumQuery[i]*sumQuery[i])/(float) closest.size())) / (float) (closest.size()-1);
sdQuery[i] = sqrt(varQuery[i]);
//set minimum error rate to 0.001 - to avoid potential divide by zero - not sure if this is necessary but it follows ccode implementation
if (averageQuery[i] < 0.001) { averageQuery[i] = 0.001; }
if (sumQuery[i] < 0.001) { sumQuery[i] = 0.001; }
if (varQuery[i] < 0.001) { varQuery[i] = 0.001; }
if (sumSquaredQuery[i] < 0.001) { sumSquaredQuery[i] = 0.001; }
if (sdQuery[i] < 0.001) { sdQuery[i] = 0.001; }
}
}
catch(exception& e) {
m->errorOut(e, "Ccode", "findVarianceQuery");
exit(1);
}
}
/**************************************************************************************************/
void Ccode::determineChimeras() {
try {
isChimericConfidence.resize(windows.size(), false);
isChimericTStudent.resize(windows.size(), false);
isChimericANOVA.resize(windows.size(), false);
anova.resize(windows.size());
//for each window
for (int i = 0; i < windows.size(); i++) {
//get confidence limits
float t = getT(closest.size()-1); //how many seqs you are comparing to this querySeq
float dsUpper = (averageQuery[i] + (t * sdQuery[i])) / averageRef[i];
float dsLower = (averageQuery[i] - (t * sdQuery[i])) / averageRef[i];
if ((dsUpper > 1.0) && (dsLower > 1.0) && (averageQuery[i] > averageRef[i])) { /* range does not include 1 */
isChimericConfidence[i] = true; /* significantly higher at P<0.05 */
}
//student t test
int degreeOfFreedom = refCombo + closest.size() - 2;
float denomForT = (((refCombo-1) * varQuery[i] + (closest.size() - 1) * varRef[i]) / (float) degreeOfFreedom) * ((refCombo + closest.size()) / (float) (refCombo * closest.size())); /* denominator, without sqrt(), for ts calculations */
float ts = fabs((averageQuery[i] - averageRef[i]) / (sqrt(denomForT))); /* value of ts for t-student test */
t = getT(degreeOfFreedom);
if ((ts >= t) && (averageQuery[i] > averageRef[i])) {
isChimericTStudent[i] = true; /* significantly higher at P<0.05 */
}
//anova test
float value1 = sumQuery[i] + sumRef[i];
float value2 = sumSquaredQuery[i] + sumSquaredRef[i];
float value3 = ((sumQuery[i]*sumQuery[i]) / (float) (closest.size())) + ((sumRef[i] * sumRef[i]) / (float) refCombo);
float value4 = (value1 * value1) / ( (float) (closest.size() + refCombo) );
float value5 = value2 - value4;
float value6 = value3 - value4;
float value7 = value5 - value6;
float value8 = value7 / ((float) degreeOfFreedom);
float anovaValue = value6 / value8;
float f = getF(degreeOfFreedom);
if ((anovaValue >= f) && (averageQuery[i] > averageRef[i])) {
isChimericANOVA[i] = true; /* significant P<0.05 */
}
if (isnan(anovaValue) || isinf(anovaValue)) { anovaValue = 0.0; }
anova[i] = anovaValue;
}
}
catch(exception& e) {
m->errorOut(e, "Ccode", "determineChimeras");
exit(1);
}
}
/**************************************************************************************************/
float Ccode::getT(int numseq) {
try {
float tvalue = 0;
/* t-student critical values for different degrees of freedom and alpha 0.1 in one-tail tests (equivalent to 0.05) */
if (numseq > 120) tvalue = 1.645;
else if (numseq > 60) tvalue = 1.658;
else if (numseq > 40) tvalue = 1.671;
else if (numseq > 30) tvalue = 1.684;
else if (numseq > 29) tvalue = 1.697;
else if (numseq > 28) tvalue = 1.699;
else if (numseq > 27) tvalue = 1.701;
else if (numseq > 26) tvalue = 1.703;
else if (numseq > 25) tvalue = 1.706;