forked from mothur/mothur
/
corraxescommand.cpp
978 lines (785 loc) · 34.5 KB
/
corraxescommand.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
/*
* corraxescommand.cpp
* Mothur
*
* Created by westcott on 12/22/10.
* Copyright 2010 Schloss Lab. All rights reserved.
*
*/
#include "corraxescommand.h"
#include "sharedutilities.h"
//**********************************************************************************************************************
vector<string> CorrAxesCommand::setParameters(){
try {
CommandParameter paxes("axes", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(paxes);
CommandParameter pshared("shared", "InputTypes", "", "", "SharedRelMeta", "SharedRelMeta", "none",false,false); parameters.push_back(pshared);
CommandParameter prelabund("relabund", "InputTypes", "", "", "SharedRelMeta", "SharedRelMeta", "none",false,false); parameters.push_back(prelabund);
CommandParameter pmetadata("metadata", "InputTypes", "", "", "SharedRelMeta", "SharedRelMeta", "none",false,false); parameters.push_back(pmetadata);
CommandParameter pnumaxes("numaxes", "Number", "", "3", "", "", "",false,false); parameters.push_back(pnumaxes);
CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
CommandParameter pmethod("method", "Multiple", "pearson-spearman-kendall", "pearson", "", "", "",false,false); parameters.push_back(pmethod);
CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
vector<string> myArray;
for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
return myArray;
}
catch(exception& e) {
m->errorOut(e, "CorrAxesCommand", "setParameters");
exit(1);
}
}
//**********************************************************************************************************************
string CorrAxesCommand::getHelpString(){
try {
string helpString = "";
helpString += "The corr.axes command reads a shared, relabund or metadata file as well as an axes file and calculates the correlation coefficient.\n";
helpString += "The corr.axes command parameters are shared, relabund, axes, metadata, groups, method, numaxes and label. The shared, relabund or metadata and axes parameters are required. If shared is given the relative abundance is calculated.\n";
helpString += "The groups parameter allows you to specify which of the groups you would like included. The group names are separated by dashes.\n";
helpString += "The label parameter allows you to select what distance level you would like used, if none is given the first distance is used.\n";
helpString += "The method parameter allows you to select what method you would like to use. Options are pearson, spearman and kendall. Default=pearson.\n";
helpString += "The numaxes parameter allows you to select the number of axes you would like to use. Default=3.\n";
helpString += "The corr.axes command should be in the following format: corr.axes(axes=yourPcoaFile, shared=yourSharedFile, method=yourMethod).\n";
helpString += "Example corr.axes(axes=genus.pool.thetayc.genus.lt.pcoa, shared=genus.pool.shared, method=kendall).\n";
helpString += "The corr.axes command outputs a .corr.axes file.\n";
helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n";
return helpString;
}
catch(exception& e) {
m->errorOut(e, "CorrAxesCommand", "getHelpString");
exit(1);
}
}
//**********************************************************************************************************************
CorrAxesCommand::CorrAxesCommand(){
try {
abort = true; calledHelp = true;
setParameters();
vector<string> tempOutNames;
outputTypes["corr.axes"] = tempOutNames;
}
catch(exception& e) {
m->errorOut(e, "CorrAxesCommand", "CorrAxesCommand");
exit(1);
}
}
//**********************************************************************************************************************
CorrAxesCommand::CorrAxesCommand(string option) {
try {
abort = false; calledHelp = false;
//allow user to run help
if(option == "help") { help(); abort = true; calledHelp = true; }
else if(option == "citation") { citation(); abort = true; calledHelp = true;}
else {
vector<string> myArray = setParameters();
OptionParser parser(option);
map<string, string> parameters = parser.getParameters();
ValidParameters validParameter;
map<string, string>::iterator it;
//check to make sure all parameters are valid for command
for (it = parameters.begin(); it != parameters.end(); it++) {
if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
}
vector<string> tempOutNames;
outputTypes["corr.axes"] = tempOutNames;
//if the user changes the input directory command factory will send this info to us in the output parameter
string inputDir = validParameter.validFile(parameters, "inputdir", false);
if (inputDir == "not found"){ inputDir = ""; }
else {
string path;
it = parameters.find("axes");
//user has given a template file
if(it != parameters.end()){
path = m->hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["axes"] = inputDir + it->second; }
}
it = parameters.find("shared");
//user has given a template file
if(it != parameters.end()){
path = m->hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["shared"] = inputDir + it->second; }
}
it = parameters.find("relabund");
//user has given a template file
if(it != parameters.end()){
path = m->hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["relabund"] = inputDir + it->second; }
}
it = parameters.find("metadata");
//user has given a template file
if(it != parameters.end()){
path = m->hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["metadata"] = inputDir + it->second; }
}
}
//check for required parameters
axesfile = validParameter.validFile(parameters, "axes", true);
if (axesfile == "not open") { abort = true; }
else if (axesfile == "not found") { axesfile = ""; m->mothurOut("axes is a required parameter for the corr.axes command."); m->mothurOutEndLine(); abort = true; }
sharedfile = validParameter.validFile(parameters, "shared", true);
if (sharedfile == "not open") { abort = true; }
else if (sharedfile == "not found") { sharedfile = ""; }
else { inputFileName = sharedfile; m->setSharedFile(sharedfile); }
relabundfile = validParameter.validFile(parameters, "relabund", true);
if (relabundfile == "not open") { abort = true; }
else if (relabundfile == "not found") { relabundfile = ""; }
else { inputFileName = relabundfile; m->setRelAbundFile(relabundfile); }
metadatafile = validParameter.validFile(parameters, "metadata", true);
if (metadatafile == "not open") { abort = true; }
else if (metadatafile == "not found") { metadatafile = ""; }
else { inputFileName = metadatafile; }
groups = validParameter.validFile(parameters, "groups", false);
if (groups == "not found") { groups = ""; pickedGroups = false; }
else {
pickedGroups = true;
m->splitAtDash(groups, Groups);
}
m->setGroups(Groups);
outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputFileName); }
label = validParameter.validFile(parameters, "label", false);
if (label == "not found") { label = ""; m->mothurOut("You did not provide a label, I will use the first label in your inputfile."); m->mothurOutEndLine(); label=""; }
if ((relabundfile == "") && (sharedfile == "") && (metadatafile == "")) {
//is there are current file available for any of these?
//give priority to shared, then relabund
//if there is a current shared file, use it
sharedfile = m->getSharedFile();
if (sharedfile != "") { inputFileName = sharedfile; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
else {
relabundfile = m->getRelAbundFile();
if (relabundfile != "") { inputFileName = relabundfile; m->mothurOut("Using " + relabundfile + " as input file for the relabund parameter."); m->mothurOutEndLine(); }
else {
m->mothurOut("You must provide either a shared, relabund, or metadata file."); m->mothurOutEndLine(); abort = true;
}
}
}
if (metadatafile != "") {
if ((relabundfile != "") || (sharedfile != "")) { m->mothurOut("You may only use one of the following : shared, relabund or metadata file."); m->mothurOutEndLine(); abort = true; }
}else {
if ((relabundfile != "") && (sharedfile != "")) { m->mothurOut("You may only use one of the following : shared, relabund or metadata file."); m->mothurOutEndLine(); abort = true; }
}
string temp;
temp = validParameter.validFile(parameters, "numaxes", false); if (temp == "not found"){ temp = "3"; }
m->mothurConvert(temp, numaxes);
method = validParameter.validFile(parameters, "method", false); if (method == "not found"){ method = "pearson"; }
if ((method != "pearson") && (method != "spearman") && (method != "kendall")) { m->mothurOut(method + " is not a valid method. Valid methods are pearson, spearman, and kendall."); m->mothurOutEndLine(); abort = true; }
}
}
catch(exception& e) {
m->errorOut(e, "CorrAxesCommand", "CorrAxesCommand");
exit(1);
}
}
//**********************************************************************************************************************
int CorrAxesCommand::execute(){
try {
if (abort == true) { if (calledHelp) { return 0; } return 2; }
/*************************************************************************************/
// use smart distancing to get right sharedRabund and convert to relabund if needed //
/************************************************************************************/
if (sharedfile != "") {
InputData* input = new InputData(sharedfile, "sharedfile");
getSharedFloat(input);
delete input;
if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; }
if (lookupFloat[0] == NULL) { m->mothurOut("[ERROR] reading relabund file."); m->mothurOutEndLine(); return 0; }
}else if (relabundfile != "") {
InputData* input = new InputData(relabundfile, "relabund");
getSharedFloat(input);
delete input;
if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; }
if (lookupFloat[0] == NULL) { m->mothurOut("[ERROR] reading relabund file."); m->mothurOutEndLine(); return 0; }
}else if (metadatafile != "") {
getMetadata(); //reads metadata file and store in lookupFloat, saves column headings in metadataLabels for later
if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; }
if (lookupFloat[0] == NULL) { m->mothurOut("[ERROR] reading metadata file."); m->mothurOutEndLine(); return 0; }
if (pickedGroups) { eliminateZeroOTUS(lookupFloat); }
}else { m->mothurOut("[ERROR]: no file given."); m->mothurOutEndLine(); return 0; }
if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; }
//this is for a sanity check to make sure the axes file and shared file match
for (int i = 0; i < lookupFloat.size(); i++) { names.insert(lookupFloat[i]->getGroup()); }
/*************************************************************************************/
// read axes file and check for file mismatches with shared or relabund file //
/************************************************************************************/
//read axes file
map<string, vector<float> > axes = readAxes();
if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; }
//sanity check, the read only adds groups that are in the shared or relabund file, but we want to make sure the axes file isn't missing anyone
if (axes.size() != lookupFloat.size()) {
map<string, vector<float> >::iterator it;
for (int i = 0; i < lookupFloat.size(); i++) {
it = axes.find(lookupFloat[i]->getGroup());
if (it == axes.end()) { m->mothurOut(lookupFloat[i]->getGroup() + " is in your shared of relabund file but not in your axes file, please correct."); m->mothurOutEndLine(); }
}
m->control_pressed = true;
}
if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; }
/*************************************************************************************/
// calc the r values //
/************************************************************************************/
string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + method + ".corr.axes";
outputNames.push_back(outputFileName); outputTypes["corr.axes"].push_back(outputFileName);
ofstream out;
m->openOutputFile(outputFileName, out);
out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
//output headings
if (metadatafile == "") { out << "OTU"; }
else { out << "Feature"; }
for (int i = 0; i < numaxes; i++) { out << '\t' << "axis" << (i+1) << "\tp-value"; }
out << "\tlength" << endl;
if (method == "pearson") { calcPearson(axes, out); }
else if (method == "spearman") { calcSpearman(axes, out); }
else if (method == "kendall") { calcKendall(axes, out); }
else { m->mothurOut("[ERROR]: Invalid method."); m->mothurOutEndLine(); }
out.close();
for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
if (m->control_pressed) { return 0; }
m->mothurOutEndLine();
m->mothurOut("Output File Names: "); m->mothurOutEndLine();
for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
m->mothurOutEndLine();
return 0;
}
catch(exception& e) {
m->errorOut(e, "CorrAxesCommand", "execute");
exit(1);
}
}
//**********************************************************************************************************************
int CorrAxesCommand::calcPearson(map<string, vector<float> >& axes, ofstream& out) {
try {
//find average of each axis - X
vector<float> averageAxes; averageAxes.resize(numaxes, 0.0);
for (map<string, vector<float> >::iterator it = axes.begin(); it != axes.end(); it++) {
vector<float> temp = it->second;
for (int i = 0; i < temp.size(); i++) {
averageAxes[i] += temp[i];
}
}
for (int i = 0; i < averageAxes.size(); i++) { averageAxes[i] = averageAxes[i] / (float) axes.size(); }
//for each otu
for (int i = 0; i < lookupFloat[0]->getNumBins(); i++) {
if (metadatafile == "") { out << i+1; }
else { out << metadataLabels[i]; }
//find the averages this otu - Y
float sumOtu = 0.0;
for (int j = 0; j < lookupFloat.size(); j++) {
sumOtu += lookupFloat[j]->getAbundance(i);
}
float Ybar = sumOtu / (float) lookupFloat.size();
vector<float> rValues(averageAxes.size());
//find r value for each axis
for (int k = 0; k < averageAxes.size(); k++) {
double r = 0.0;
double numerator = 0.0;
double denomTerm1 = 0.0;
double denomTerm2 = 0.0;
for (int j = 0; j < lookupFloat.size(); j++) {
float Yi = lookupFloat[j]->getAbundance(i);
float Xi = axes[lookupFloat[j]->getGroup()][k];
numerator += ((Xi - averageAxes[k]) * (Yi - Ybar));
denomTerm1 += ((Xi - averageAxes[k]) * (Xi - averageAxes[k]));
denomTerm2 += ((Yi - Ybar) * (Yi - Ybar));
}
double denom = (sqrt(denomTerm1) * sqrt(denomTerm2));
r = numerator / denom;
if (isnan(r) || isinf(r)) { r = 0.0; }
rValues[k] = r;
out << '\t' << r;
//signifigance calc - http://faculty.vassar.edu/lowry/ch4apx.html
double temp = (1- (r*r)) / (double) (lookupFloat.size()-2);
temp = sqrt(temp);
double sig = r / temp;
if (isnan(sig) || isinf(sig)) { sig = 0.0; }
out << '\t' << sig;
}
double sum = 0;
for(int k=0;k<rValues.size();k++){
sum += rValues[k] * rValues[k];
}
out << '\t' << sqrt(sum) << endl;
}
return 0;
}
catch(exception& e) {
m->errorOut(e, "CorrAxesCommand", "calcPearson");
exit(1);
}
}
//**********************************************************************************************************************
int CorrAxesCommand::calcSpearman(map<string, vector<float> >& axes, ofstream& out) {
try {
//format data
vector< map<float, int> > tableX; tableX.resize(numaxes);
map<float, int>::iterator itTable;
vector< vector<spearmanRank> > scores; scores.resize(numaxes);
for (map<string, vector<float> >::iterator it = axes.begin(); it != axes.end(); it++) {
vector<float> temp = it->second;
for (int i = 0; i < temp.size(); i++) {
spearmanRank member(it->first, temp[i]);
scores[i].push_back(member);
//count number of repeats
itTable = tableX[i].find(temp[i]);
if (itTable == tableX[i].end()) {
tableX[i][temp[i]] = 1;
}else {
tableX[i][temp[i]]++;
}
}
}
//calc LX
//for each axis
vector<double> Lx; Lx.resize(numaxes, 0.0);
for (int i = 0; i < numaxes; i++) {
for (itTable = tableX[i].begin(); itTable != tableX[i].end(); itTable++) {
double tx = (double) itTable->second;
Lx[i] += ((pow(tx, 3.0) - tx) / 12.0);
}
}
//sort each axis
for (int i = 0; i < numaxes; i++) { sort(scores[i].begin(), scores[i].end(), compareSpearman); }
//find ranks of xi in each axis
map<string, vector<float> > rankAxes;
for (int i = 0; i < numaxes; i++) {
vector<spearmanRank> ties;
int rankTotal = 0;
for (int j = 0; j < scores[i].size(); j++) {
rankTotal += (j+1);
ties.push_back(scores[i][j]);
if (j != (scores[i].size()-1)) { // you are not the last so you can look ahead
if (scores[i][j].score != scores[i][j+1].score) { // you are done with ties, rank them and continue
for (int k = 0; k < ties.size(); k++) {
float thisrank = rankTotal / (float) ties.size();
rankAxes[ties[k].name].push_back(thisrank);
}
ties.clear();
rankTotal = 0;
}
}else { // you are the last one
for (int k = 0; k < ties.size(); k++) {
float thisrank = rankTotal / (float) ties.size();
rankAxes[ties[k].name].push_back(thisrank);
}
}
}
}
//for each otu
for (int i = 0; i < lookupFloat[0]->getNumBins(); i++) {
if (metadatafile == "") { out << i+1; }
else { out << metadataLabels[i]; }
//find the ranks of this otu - Y
vector<spearmanRank> otuScores;
map<float, int> tableY;
for (int j = 0; j < lookupFloat.size(); j++) {
spearmanRank member(lookupFloat[j]->getGroup(), lookupFloat[j]->getAbundance(i));
otuScores.push_back(member);
itTable = tableY.find(member.score);
if (itTable == tableY.end()) {
tableY[member.score] = 1;
}else {
tableY[member.score]++;
}
}
//calc Ly
double Ly = 0.0;
for (itTable = tableY.begin(); itTable != tableY.end(); itTable++) {
double ty = (double) itTable->second;
Ly += ((pow(ty, 3.0) - ty) / 12.0);
}
sort(otuScores.begin(), otuScores.end(), compareSpearman);
map<string, float> rankOtus;
vector<spearmanRank> ties;
int rankTotal = 0;
for (int j = 0; j < otuScores.size(); j++) {
rankTotal += (j+1);
ties.push_back(otuScores[j]);
if (j != (otuScores.size()-1)) { // you are not the last so you can look ahead
if (otuScores[j].score != otuScores[j+1].score) { // you are done with ties, rank them and continue
for (int k = 0; k < ties.size(); k++) {
float thisrank = rankTotal / (float) ties.size();
rankOtus[ties[k].name] = thisrank;
}
ties.clear();
rankTotal = 0;
}
}else { // you are the last one
for (int k = 0; k < ties.size(); k++) {
float thisrank = rankTotal / (float) ties.size();
rankOtus[ties[k].name] = thisrank;
}
}
}
vector<double> pValues(numaxes);
//calc spearman ranks for each axis for this otu
for (int j = 0; j < numaxes; j++) {
double di = 0.0;
for (int k = 0; k < lookupFloat.size(); k++) {
float xi = rankAxes[lookupFloat[k]->getGroup()][j];
float yi = rankOtus[lookupFloat[k]->getGroup()];
di += ((xi - yi) * (xi - yi));
}
double p = 0.0;
double n = (double) lookupFloat.size();
double SX2 = ((pow(n, 3.0) - n) / 12.0) - Lx[j];
double SY2 = ((pow(n, 3.0) - n) / 12.0) - Ly;
p = (SX2 + SY2 - di) / (2.0 * sqrt((SX2*SY2)));
if (isnan(p) || isinf(p)) { p = 0.0; }
out << '\t' << p;
pValues[j] = p;
//signifigance calc - http://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient
double temp = (lookupFloat.size()-2) / (double) (1- (p*p));
temp = sqrt(temp);
double sig = p*temp;
if (isnan(sig) || isinf(sig)) { sig = 0.0; }
out << '\t' << sig;
}
double sum = 0;
for(int k=0;k<numaxes;k++){
sum += pValues[k] * pValues[k];
}
out << '\t' << sqrt(sum) << endl;
}
return 0;
}
catch(exception& e) {
m->errorOut(e, "CorrAxesCommand", "calcSpearman");
exit(1);
}
}
//**********************************************************************************************************************
int CorrAxesCommand::calcKendall(map<string, vector<float> >& axes, ofstream& out) {
try {
//format data
vector< vector<spearmanRank> > scores; scores.resize(numaxes);
for (map<string, vector<float> >::iterator it = axes.begin(); it != axes.end(); it++) {
vector<float> temp = it->second;
for (int i = 0; i < temp.size(); i++) {
spearmanRank member(it->first, temp[i]);
scores[i].push_back(member);
}
}
//sort each axis
for (int i = 0; i < numaxes; i++) { sort(scores[i].begin(), scores[i].end(), compareSpearman); }
//convert scores to ranks of xi in each axis
for (int i = 0; i < numaxes; i++) {
vector<spearmanRank*> ties;
int rankTotal = 0;
for (int j = 0; j < scores[i].size(); j++) {
rankTotal += (j+1);
ties.push_back(&(scores[i][j]));
if (j != scores[i].size()-1) { // you are not the last so you can look ahead
if (scores[i][j].score != scores[i][j+1].score) { // you are done with ties, rank them and continue
for (int k = 0; k < ties.size(); k++) {
float thisrank = rankTotal / (float) ties.size();
(*ties[k]).score = thisrank;
}
ties.clear();
rankTotal = 0;
}
}else { // you are the last one
for (int k = 0; k < ties.size(); k++) {
float thisrank = rankTotal / (float) ties.size();
(*ties[k]).score = thisrank;
}
}
}
}
//for each otu
for (int i = 0; i < lookupFloat[0]->getNumBins(); i++) {
if (metadatafile == "") { out << i+1; }
else { out << metadataLabels[i]; }
//find the ranks of this otu - Y
vector<spearmanRank> otuScores;
for (int j = 0; j < lookupFloat.size(); j++) {
spearmanRank member(lookupFloat[j]->getGroup(), lookupFloat[j]->getAbundance(i));
otuScores.push_back(member);
}
sort(otuScores.begin(), otuScores.end(), compareSpearman);
map<string, float> rankOtus;
vector<spearmanRank> ties;
int rankTotal = 0;
for (int j = 0; j < otuScores.size(); j++) {
rankTotal += (j+1);
ties.push_back(otuScores[j]);
if (j != otuScores.size()-1) { // you are not the last so you can look ahead
if (otuScores[j].score != otuScores[j+1].score) { // you are done with ties, rank them and continue
for (int k = 0; k < ties.size(); k++) {
float thisrank = rankTotal / (float) ties.size();
rankOtus[ties[k].name] = thisrank;
}
ties.clear();
rankTotal = 0;
}
}else { // you are the last one
for (int k = 0; k < ties.size(); k++) {
float thisrank = rankTotal / (float) ties.size();
rankOtus[ties[k].name] = thisrank;
}
}
}
vector<double> pValues(numaxes);
//calc spearman ranks for each axis for this otu
for (int j = 0; j < numaxes; j++) {
int numCoor = 0;
int numDisCoor = 0;
vector<spearmanRank> otus;
vector<spearmanRank> otusTemp;
for (int l = 0; l < scores[j].size(); l++) {
spearmanRank member(scores[j][l].name, rankOtus[scores[j][l].name]);
otus.push_back(member);
}
int count = 0;
for (int l = 0; l < scores[j].size(); l++) {
int numWithHigherRank = 0;
int numWithLowerRank = 0;
float thisrank = otus[l].score;
for (int u = l+1; u < scores[j].size(); u++) {
if (otus[u].score > thisrank) { numWithHigherRank++; }
else if (otus[u].score < thisrank) { numWithLowerRank++; }
count++;
}
numCoor += numWithHigherRank;
numDisCoor += numWithLowerRank;
}
double p = (numCoor - numDisCoor) / (float) count;
if (isnan(p) || isinf(p)) { p = 0.0; }
out << '\t' << p;
pValues[j] = p;
//calc signif - zA - http://en.wikipedia.org/wiki/Kendall_tau_rank_correlation_coefficient#Significance_tests
double numer = 3.0 * (numCoor - numDisCoor);
int n = scores[j].size();
double denom = n * (n-1) * (2*n + 5) / (double) 2.0;
denom = sqrt(denom);
double sig = numer / denom;
if (isnan(sig) || isinf(sig)) { sig = 0.0; }
out << '\t' << sig;
}
double sum = 0;
for(int k=0;k<numaxes;k++){
sum += pValues[k] * pValues[k];
}
out << '\t' << sqrt(sum) << endl;
}
return 0;
}
catch(exception& e) {
m->errorOut(e, "CorrAxesCommand", "calcKendall");
exit(1);
}
}
//**********************************************************************************************************************
int CorrAxesCommand::getSharedFloat(InputData* input){
try {
lookupFloat = input->getSharedRAbundFloatVectors();
string lastLabel = lookupFloat[0]->getLabel();
if (label == "") { label = lastLabel; return 0; }
//if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
set<string> labels; labels.insert(label);
set<string> processedLabels;
set<string> userLabels = labels;
//as long as you are not at the end of the file or done wih the lines you want
while((lookupFloat[0] != NULL) && (userLabels.size() != 0)) {
if (m->control_pressed) { return 0; }
if(labels.count(lookupFloat[0]->getLabel()) == 1){
processedLabels.insert(lookupFloat[0]->getLabel());
userLabels.erase(lookupFloat[0]->getLabel());
break;
}
if ((m->anyLabelsToProcess(lookupFloat[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
string saveLabel = lookupFloat[0]->getLabel();
for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
processedLabels.insert(lookupFloat[0]->getLabel());
userLabels.erase(lookupFloat[0]->getLabel());
//restore real lastlabel to save below
lookupFloat[0]->setLabel(saveLabel);
break;
}
lastLabel = lookupFloat[0]->getLabel();
//get next line to process
//prevent memory leak
for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
lookupFloat = input->getSharedRAbundFloatVectors();
}
if (m->control_pressed) { return 0; }
//output error messages about any remaining user labels
set<string>::iterator it;
bool needToRun = false;
for (it = userLabels.begin(); it != userLabels.end(); it++) {
m->mothurOut("Your file does not include the label " + *it);
if (processedLabels.count(lastLabel) != 1) {
m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
needToRun = true;
}else {
m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
}
}
//run last label if you need to
if (needToRun == true) {
for (int i = 0; i < lookupFloat.size(); i++) { if (lookupFloat[i] != NULL) { delete lookupFloat[i]; } }
lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
}
return 0;
}
catch(exception& e) {
m->errorOut(e, "CorrAxesCommand", "getSharedFloat");
exit(1);
}
}
//**********************************************************************************************************************
int CorrAxesCommand::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thislookup) {
try {
vector<SharedRAbundFloatVector*> newLookup;
for (int i = 0; i < thislookup.size(); i++) {
SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
temp->setLabel(thislookup[i]->getLabel());
temp->setGroup(thislookup[i]->getGroup());
newLookup.push_back(temp);
}
//for each bin
vector<string> newBinLabels;
string snumBins = toString(thislookup[0]->getNumBins());
for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
//look at each sharedRabund and make sure they are not all zero
bool allZero = true;
for (int j = 0; j < thislookup.size(); j++) {
if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
}
//if they are not all zero add this bin
if (!allZero) {
for (int j = 0; j < thislookup.size(); j++) {
newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
}
//if there is a bin label use it otherwise make one
string binLabel = "Otu";
string sbinNumber = toString(i+1);
if (sbinNumber.length() < snumBins.length()) {
int diff = snumBins.length() - sbinNumber.length();
for (int h = 0; h < diff; h++) { binLabel += "0"; }
}
binLabel += sbinNumber;
if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
newBinLabels.push_back(binLabel);
}
}
for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
thislookup = newLookup;
m->currentBinLabels = newBinLabels;
return 0;
}
catch(exception& e) {
m->errorOut(e, "CorrAxesCommand", "eliminateZeroOTUS");
exit(1);
}
}
/*****************************************************************/
map<string, vector<float> > CorrAxesCommand::readAxes(){
try {
map<string, vector<float> > axes;
ifstream in;
m->openInputFile(axesfile, in);
string headerLine = m->getline(in); m->gobble(in);
//count the number of axis you are reading
bool done = false;
int count = 0;
while (!done) {
int pos = headerLine.find("axis");
if (pos != string::npos) {
count++;
headerLine = headerLine.substr(pos+4);
}else { done = true; }
}
if (numaxes > count) { m->mothurOut("You requested " + toString(numaxes) + " axes, but your file only includes " + toString(count) + ". Using " + toString(count) + "."); m->mothurOutEndLine(); numaxes = count; }
while (!in.eof()) {
if (m->control_pressed) { in.close(); return axes; }
string group = "";
in >> group; m->gobble(in);
vector<float> thisGroupsAxes;
for (int i = 0; i < count; i++) {
float temp = 0.0;
in >> temp;
//only save the axis we want
if (i < numaxes) { thisGroupsAxes.push_back(temp); }
}
//save group if its one the user selected
if (names.count(group) != 0) {
map<string, vector<float> >::iterator it = axes.find(group);
if (it == axes.end()) {
axes[group] = thisGroupsAxes;
}else {
m->mothurOut(group + " is already in your axes file, using first definition."); m->mothurOutEndLine();
}
}
m->gobble(in);
}
in.close();
return axes;
}
catch(exception& e) {
m->errorOut(e, "CorrAxesCommand", "readAxes");
exit(1);
}
}
/*****************************************************************/
int CorrAxesCommand::getMetadata(){
try {
vector<string> groupNames;
ifstream in;
m->openInputFile(metadatafile, in);
string headerLine = m->getline(in); m->gobble(in);
istringstream iss (headerLine,istringstream::in);
//read the first label, because it refers to the groups
string columnLabel;
iss >> columnLabel; m->gobble(iss);
//save names of columns you are reading
while (!iss.eof()) {
iss >> columnLabel; m->gobble(iss);
metadataLabels.push_back(columnLabel);
}
int count = metadataLabels.size();
//read rest of file
while (!in.eof()) {
if (m->control_pressed) { in.close(); return 0; }
string group = "";
in >> group; m->gobble(in);
groupNames.push_back(group);
SharedRAbundFloatVector* tempLookup = new SharedRAbundFloatVector();
tempLookup->setGroup(group);
tempLookup->setLabel("1");
for (int i = 0; i < count; i++) {
float temp = 0.0;
in >> temp;
tempLookup->push_back(temp, group);
}
lookupFloat.push_back(tempLookup);
m->gobble(in);
}
in.close();
//remove any groups the user does not want, and set globaldata->groups with only valid groups
SharedUtil* util;
util = new SharedUtil();
Groups = m->getGroups();
util->setGroups(Groups, groupNames);
m->setGroups(Groups);
for (int i = 0; i < lookupFloat.size(); i++) {
//if this sharedrabund is not from a group the user wants then delete it.
if (util->isValidGroup(lookupFloat[i]->getGroup(), m->getGroups()) == false) {
delete lookupFloat[i]; lookupFloat[i] = NULL;
lookupFloat.erase(lookupFloat.begin()+i);
i--;
}
}
delete util;
return 0;
}
catch(exception& e) {
m->errorOut(e, "CorrAxesCommand", "getMetadata");
exit(1);
}
}
/*****************************************************************/