]> git.donarmstrong.com Git - mothur.git/blob - unifracweightedcommand.cpp
cluster.split fix
[mothur.git] / unifracweightedcommand.cpp
1 /*
2  *  unifracweightedcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 2/9/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "unifracweightedcommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> UnifracWeightedCommand::getValidParameters(){    
14         try {
15                 string Array[] =  {"groups","iters","distance","random","processors","outputdir","inputdir"};
16                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
17                 return myArray;
18         }
19         catch(exception& e) {
20                 m->errorOut(e, "UnifracWeightedCommand", "getValidParameters");
21                 exit(1);
22         }
23 }
24 //**********************************************************************************************************************
25 UnifracWeightedCommand::UnifracWeightedCommand(){       
26         try {
27                 abort = true;
28                 //initialize outputTypes
29                 vector<string> tempOutNames;
30                 outputTypes["weighted"] = tempOutNames;
31                 outputTypes["wsummary"] = tempOutNames;
32                 outputTypes["phylip"] = tempOutNames;
33         }
34         catch(exception& e) {
35                 m->errorOut(e, "UnifracWeightedCommand", "UnifracWeightedCommand");
36                 exit(1);
37         }
38 }
39 //**********************************************************************************************************************
40 vector<string> UnifracWeightedCommand::getRequiredParameters(){ 
41         try {
42                 vector<string> myArray;
43                 return myArray;
44         }
45         catch(exception& e) {
46                 m->errorOut(e, "UnifracWeightedCommand", "getRequiredParameters");
47                 exit(1);
48         }
49 }
50 //**********************************************************************************************************************
51 vector<string> UnifracWeightedCommand::getRequiredFiles(){      
52         try {
53                 string Array[] =  {"tree","group"};
54                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
55
56                 return myArray;
57         }
58         catch(exception& e) {
59                 m->errorOut(e, "UnifracWeightedCommand", "getRequiredFiles");
60                 exit(1);
61         }
62 }
63 /***********************************************************/
64 UnifracWeightedCommand::UnifracWeightedCommand(string option) {
65         try {
66                 globaldata = GlobalData::getInstance();
67                 abort = false;
68                 Groups.clear();
69                         
70                 //allow user to run help
71                 if(option == "help") { help(); abort = true; }
72                 
73                 else {
74                         //valid paramters for this command
75                         string Array[] =  {"groups","iters","distance","random","processors","outputdir","inputdir"};
76                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
77                         
78                         OptionParser parser(option);
79                         map<string,string> parameters=parser.getParameters();
80                         
81                         ValidParameters validParameter;
82                 
83                         //check to make sure all parameters are valid for command
84                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
85                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
86                         }
87                         
88                         //initialize outputTypes
89                         vector<string> tempOutNames;
90                         outputTypes["weighted"] = tempOutNames;
91                         outputTypes["wsummary"] = tempOutNames;
92                         outputTypes["phylip"] = tempOutNames;
93                         
94                         if (globaldata->gTree.size() == 0) {//no trees were read
95                                 m->mothurOut("You must execute the read.tree command, before you may execute the unifrac.weighted command."); m->mothurOutEndLine(); abort = true;  }
96                         
97                         //if the user changes the output directory command factory will send this info to us in the output parameter 
98                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
99                                 outputDir = ""; 
100                                 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it       
101                         }
102                                                                                                                                         
103                         //check for optional parameter and set defaults
104                         // ...at some point should added some additional type checking...
105                         groups = validParameter.validFile(parameters, "groups", false);                 
106                         if (groups == "not found") { groups = ""; }
107                         else { 
108                                 m->splitAtDash(groups, Groups);
109                                 globaldata->Groups = Groups;
110                         }
111                                 
112                         itersString = validParameter.validFile(parameters, "iters", false);                     if (itersString == "not found") { itersString = "1000"; }
113                         convert(itersString, iters); 
114                         
115                         string temp = validParameter.validFile(parameters, "distance", false);                  if (temp == "not found") { temp = "false"; }
116                         phylip = m->isTrue(temp);
117                 
118                         temp = validParameter.validFile(parameters, "random", false);                                   if (temp == "not found") { temp = "F"; }
119                         random = m->isTrue(temp);
120                         
121                         temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = "1";                             }
122                         convert(temp, processors); 
123                         
124                         if (!random) {  iters = 0;  } //turn off random calcs
125
126                         
127                         if (abort == false) {
128                                 T = globaldata->gTree;
129                                 tmap = globaldata->gTreemap;
130                                 sumFile = outputDir + m->getSimpleName(globaldata->getTreeFile()) + ".wsummary";
131                                 m->openOutputFile(sumFile, outSum);
132                                 outputNames.push_back(sumFile);  outputTypes["wsummary"].push_back(sumFile);
133                                 
134                                 util = new SharedUtil();
135                                 string s; //to make work with setgroups
136                                 util->setGroups(globaldata->Groups, tmap->namesOfGroups, s, numGroups, "weighted");     //sets the groups the user wants to analyze
137                                 util->getCombos(groupComb, globaldata->Groups, numComp);
138                                 
139                                 weighted = new Weighted(tmap);
140                                 
141                         }
142                 }
143                 
144                 
145         }
146         catch(exception& e) {
147                 m->errorOut(e, "UnifracWeightedCommand", "UnifracWeightedCommand");
148                 exit(1);
149         }
150 }
151 //**********************************************************************************************************************
152
153 void UnifracWeightedCommand::help(){
154         try {
155                 m->mothurOut("The unifrac.weighted command can only be executed after a successful read.tree command.\n");
156                 m->mothurOut("The unifrac.weighted command parameters are groups, iters, distance, processors and random.  No parameters are required.\n");
157                 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed.  You must enter at least 2 valid groups.\n");
158                 m->mothurOut("The group names are separated by dashes.  The iters parameter allows you to specify how many random trees you would like compared to your tree.\n");
159                 m->mothurOut("The distance parameter allows you to create a distance file from the results. The default is false.\n");
160                 m->mothurOut("The random parameter allows you to shut off the comparison to random trees. The default is false, meaning don't compare your trees with randomly generated trees.\n");
161                 m->mothurOut("The processors parameter allows you to specify the number of processors to use. The default is 1.\n");
162                 m->mothurOut("The unifrac.weighted command should be in the following format: unifrac.weighted(groups=yourGroups, iters=yourIters).\n");
163                 m->mothurOut("Example unifrac.weighted(groups=A-B-C, iters=500).\n");
164                 m->mothurOut("The default value for groups is all the groups in your groupfile, and iters is 1000.\n");
165                 m->mothurOut("The unifrac.weighted command output two files: .weighted and .wsummary their descriptions are in the manual.\n");
166                 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
167         }
168         catch(exception& e) {
169                 m->errorOut(e, "UnifracWeightedCommand", "help");
170                 exit(1);
171         }
172 }
173
174 /***********************************************************/
175 int UnifracWeightedCommand::execute() {
176         try {
177         
178                 if (abort == true) { return 0; }
179                 
180                 int start = time(NULL);
181                 
182                 //get weighted for users tree
183                 userData.resize(numComp,0);  //data[0] = weightedscore AB, data[1] = weightedscore AC...
184                 randomData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
185                                 
186                 //get weighted scores for users trees
187                 for (int i = 0; i < T.size(); i++) {
188                         
189                         if (m->control_pressed) { outSum.close(); for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
190
191                         counter = 0;
192                         rScores.resize(numComp);  //data[0] = weightedscore AB, data[1] = weightedscore AC...
193                         uScores.resize(numComp);  //data[0] = weightedscore AB, data[1] = weightedscore AC...
194                         
195                         if (random) {  
196                                 output = new ColumnFile(outputDir + m->getSimpleName(globaldata->getTreeFile())  + toString(i+1) + ".weighted", itersString);  
197                                 outputNames.push_back(outputDir + m->getSimpleName(globaldata->getTreeFile())  + toString(i+1) + ".weighted");
198                                 outputTypes["weighted"].push_back(outputDir + m->getSimpleName(globaldata->getTreeFile())  + toString(i+1) + ".weighted");
199                         } 
200
201                         userData = weighted->getValues(T[i], processors, outputDir);  //userData[0] = weightedscore
202                         
203                         if (m->control_pressed) { if (random) { delete output; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str());  } return 0; }
204                         
205                         //save users score
206                         for (int s=0; s<numComp; s++) {
207                                 //add users score to vector of user scores
208                                 uScores[s].push_back(userData[s]);
209                                 
210                                 //save users tree score for summary file
211                                 utreeScores.push_back(userData[s]);
212                         }
213                         
214                         if (random) { 
215                         
216                                 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
217                                 vector< vector<string> > namesOfGroupCombos;
218                                 for (int a=0; a<numGroups; a++) { 
219                                         for (int l = 0; l < a; l++) {   
220                                                 vector<string> groups; groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
221                                                 namesOfGroupCombos.push_back(groups);
222                                         }
223                                 }
224                                 
225                                 lines.clear();
226                                 
227                                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
228                                         if(processors != 1){
229                                                 int numPairs = namesOfGroupCombos.size();
230                                                 int numPairsPerProcessor = numPairs / processors;
231                                         
232                                                 for (int i = 0; i < processors; i++) {
233                                                         int startPos = i * numPairsPerProcessor;
234                                                         if(i == processors - 1){
235                                                                 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
236                                                         }
237                                                         lines.push_back(linePair(startPos, numPairsPerProcessor));
238                                                 }
239                                         }
240                                 #endif
241
242                                 
243                                 //get scores for random trees
244                                 for (int j = 0; j < iters; j++) {
245                                 
246                                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
247                                                 if(processors == 1){
248                                                         driver(T[i],  namesOfGroupCombos, 0, namesOfGroupCombos.size(),  rScores);
249                                                 }else{
250                                                         createProcesses(T[i],  namesOfGroupCombos, rScores);
251                                                 }
252                                         #else
253                                                 driver(T[i], namesOfGroupCombos, 0, namesOfGroupCombos.size(), rScores);
254                                         #endif
255                                         
256                                         if (m->control_pressed) { delete output; outSum.close(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str());  } return 0; }
257                                         
258                                         //report progress
259                                         m->mothurOut("Iter: " + toString(j+1)); m->mothurOutEndLine();          
260                                 }
261                                 lines.clear();
262                         
263                                 //find the signifigance of the score for summary file
264                                 for (int f = 0; f < numComp; f++) {
265                                         //sort random scores
266                                         sort(rScores[f].begin(), rScores[f].end());
267                                         
268                                         //the index of the score higher than yours is returned 
269                                         //so if you have 1000 random trees the index returned is 100 
270                                         //then there are 900 trees with a score greater then you. 
271                                         //giving you a signifigance of 0.900
272                                         int index = findIndex(userData[f], f);    if (index == -1) { m->mothurOut("error in UnifracWeightedCommand"); m->mothurOutEndLine(); exit(1); } //error code
273                                         
274                                         //the signifigance is the number of trees with the users score or higher 
275                                         WScoreSig.push_back((iters-index)/(float)iters);
276                                 }
277                                 
278                                 //out << "Tree# " << i << endl;
279                                 calculateFreqsCumuls();
280                                 printWeightedFile();
281                                 
282                                 delete output;
283                         
284                         }
285                         
286                         //clear data
287                         rScores.clear();
288                         uScores.clear();
289                         validScores.clear();
290                 }
291                 
292                 
293                 if (m->control_pressed) { outSum.close(); for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0;  }
294                 
295                 printWSummaryFile();
296                 
297                 if (phylip) {   createPhylipFile();             }
298
299                 //clear out users groups
300                 globaldata->Groups.clear();
301                 
302                 
303                 if (m->control_pressed) { 
304                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  }
305                         return 0; 
306                 }
307                 
308                 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to run unifrac.weighted."); m->mothurOutEndLine();
309                 
310                 m->mothurOutEndLine();
311                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
312                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
313                 m->mothurOutEndLine();
314                 
315                 return 0;
316                 
317         }
318         catch(exception& e) {
319                 m->errorOut(e, "UnifracWeightedCommand", "execute");
320                 exit(1);
321         }
322 }
323 /**************************************************************************************************/
324
325 int UnifracWeightedCommand::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, vector< vector<double> >& scores) {
326         try {
327 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
328                 int process = 1;
329                 int num = 0;
330                 vector<int> processIDS;
331                 
332                 EstOutput results;
333                 
334                 //loop through and create all the processes you want
335                 while (process != processors) {
336                         int pid = fork();
337                         
338                         if (pid > 0) {
339                                 processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
340                                 process++;
341                         }else if (pid == 0){
342                                 driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, scores);
343                         
344                                 //pass numSeqs to parent
345                                 ofstream out;
346                                 string tempFile = outputDir + toString(getpid()) + ".weightedcommand.results.temp";
347                                 m->openOutputFile(tempFile, out);
348                                 for (int i = lines[process].start; i < (lines[process].start + lines[process].num); i++) { out << scores[i][(scores[i].size()-1)] << '\t';  } out << endl;
349                                 out.close();
350                                 
351                                 exit(0);
352                         }else { 
353                                 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
354                                 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
355                                 exit(0);
356                         }
357                 }
358                 
359                 driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, scores);
360                 
361                 //force parent to wait until all the processes are done
362                 for (int i=0;i<(processors-1);i++) { 
363                         int temp = processIDS[i];
364                         wait(&temp);
365                 }
366                 
367                 //get data created by processes
368                 for (int i=0;i<(processors-1);i++) { 
369         
370                         ifstream in;
371                         string s = outputDir + toString(processIDS[i]) + ".weightedcommand.results.temp";
372                         m->openInputFile(s, in);
373                         
374                         double tempScore;
375                         for (int j = lines[(i+1)].start; j < (lines[(i+1)].start + lines[(i+1)].num); j++) { in >> tempScore; scores[j].push_back(tempScore); }
376                         in.close();
377                         remove(s.c_str());
378                 }
379                 
380                 return 0;
381 #endif          
382         }
383         catch(exception& e) {
384                 m->errorOut(e, "UnifracWeightedCommand", "createProcesses");
385                 exit(1);
386         }
387 }
388
389 /**************************************************************************************************/
390 int UnifracWeightedCommand::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, vector< vector<double> >& scores) { 
391  try {
392                 Tree* randT = new Tree();
393
394                 for (int h = start; h < (start+num); h++) {
395         
396                         if (m->control_pressed) { return 0; }
397                 
398                         //initialize weighted score
399                         string groupA = namesOfGroupCombos[h][0]; 
400                         string groupB = namesOfGroupCombos[h][1];
401                         
402                         //copy T[i]'s info.
403                         randT->getCopy(t);
404                          
405                         //create a random tree with same topology as T[i], but different labels
406                         randT->assembleRandomUnifracTree(groupA, groupB);
407                         
408                         if (m->control_pressed) { delete randT;  return 0;  }
409
410                         //get wscore of random tree
411                         EstOutput randomData = weighted->getValues(randT, groupA, groupB);
412                 
413                         if (m->control_pressed) { delete randT;  return 0;  }
414                                                                                 
415                         //save scores
416                         scores[h].push_back(randomData[0]);
417                 }
418         
419                 delete randT;
420         
421                 return 0;
422
423         }
424         catch(exception& e) {
425                 m->errorOut(e, "UnifracWeightedCommand", "driver");
426                 exit(1);
427         }
428 }
429 /***********************************************************/
430 void UnifracWeightedCommand::printWeightedFile() {
431         try {
432                 vector<double> data;
433                 vector<string> tags;
434                 tags.push_back("Score"); tags.push_back("RandFreq"); tags.push_back("RandCumul");
435                 
436                 for(int a = 0; a < numComp; a++) {
437                         output->initFile(groupComb[a], tags);
438                         //print each line
439                         for (map<float,float>::iterator it = validScores.begin(); it != validScores.end(); it++) { 
440                                 data.push_back(it->first);  data.push_back(rScoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]); 
441                                 output->output(data);
442                                 data.clear();
443                         } 
444                         output->resetFile();
445                 }
446         }
447         catch(exception& e) {
448                 m->errorOut(e, "UnifracWeightedCommand", "printWeightedFile");
449                 exit(1);
450         }
451 }
452
453
454 /***********************************************************/
455 void UnifracWeightedCommand::printWSummaryFile() {
456         try {
457                 //column headers
458                 outSum << "Tree#" << '\t' << "Groups" << '\t' << "WScore" << '\t' << "WSig" <<  endl;
459                 m->mothurOut("Tree#\tGroups\tWScore\tWSig"); m->mothurOutEndLine(); 
460                 
461                 //format output
462                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
463                 
464                 //print each line
465                 int count = 0;
466                 for (int i = 0; i < T.size(); i++) { 
467                         for (int j = 0; j < numComp; j++) {
468                                 if (random) {
469                                         if (WScoreSig[count] > (1/(float)iters)) {
470                                                 outSum << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(itersString.length()) << WScoreSig[count] << endl; 
471                                                 cout << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(itersString.length()) << WScoreSig[count] << endl; 
472                                                 m->mothurOutJustToLog(toString(i+1) +"\t" + groupComb[j] +"\t" + toString(utreeScores[count]) +"\t" +  toString(WScoreSig[count]) + "\n");   
473                                         }else{
474                                                 outSum << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(itersString.length()) << "<" << (1/float(iters)) << endl; 
475                                                 cout << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(itersString.length()) << "<" << (1/float(iters)) << endl; 
476                                                 m->mothurOutJustToLog(toString(i+1) +"\t" + groupComb[j] +"\t" + toString(utreeScores[count]) +"\t<" +  toString((1/float(iters))) + "\n");  
477                                         }
478                                 }else{
479                                         outSum << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << "0.00" << endl; 
480                                         cout << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << "0.00" << endl; 
481                                         m->mothurOutJustToLog(toString(i+1) +"\t" + groupComb[j] +"\t" + toString(utreeScores[count]) +"\t0.00\n"); 
482                                 }
483                                 count++;
484                         }
485                 }
486                 outSum.close();
487         }
488         catch(exception& e) {
489                 m->errorOut(e, "UnifracWeightedCommand", "printWSummaryFile");
490                 exit(1);
491         }
492 }
493 /***********************************************************/
494 void UnifracWeightedCommand::createPhylipFile() {
495         try {
496                 int count = 0;
497                 //for each tree
498                 for (int i = 0; i < T.size(); i++) { 
499                 
500                         string phylipFileName = outputDir + m->getSimpleName(globaldata->getTreeFile())  + toString(i+1) + ".weighted.dist";
501                         outputNames.push_back(phylipFileName);
502                         outputTypes["phylip"].push_back(phylipFileName);
503                         ofstream out;
504                         m->openOutputFile(phylipFileName, out);
505                         
506                         //output numSeqs
507                         out << globaldata->Groups.size() << endl;
508                         
509                         //make matrix with scores in it
510                         vector< vector<float> > dists;  dists.resize(globaldata->Groups.size());
511                         for (int i = 0; i < globaldata->Groups.size(); i++) {
512                                 dists[i].resize(globaldata->Groups.size(), 0.0);
513                         }
514                         
515                         //flip it so you can print it
516                         for (int r=0; r<globaldata->Groups.size(); r++) { 
517                                 for (int l = r+1; l < globaldata->Groups.size(); l++) {
518                                         dists[r][l] = utreeScores[count];
519                                         dists[l][r] = utreeScores[count];
520                                         count++;
521                                 }
522                         }
523
524                         //output to file
525                         for (int r=0; r<globaldata->Groups.size(); r++) { 
526                                 //output name
527                                 string name = globaldata->Groups[r];
528                                 if (name.length() < 10) { //pad with spaces to make compatible
529                                         while (name.length() < 10) {  name += " ";  }
530                                 }
531                                 out << name << '\t';
532                                 
533                                 //output distances
534                                 for (int l = 0; l < r; l++) {   out  << dists[r][l] << '\t';  }
535                                 out << endl;
536                         }
537                         out.close();
538                 }
539         }
540         catch(exception& e) {
541                 m->errorOut(e, "UnifracWeightedCommand", "createPhylipFile");
542                 exit(1);
543         }
544 }
545 /***********************************************************/
546 int UnifracWeightedCommand::findIndex(float score, int index) {
547         try{
548                 for (int i = 0; i < rScores[index].size(); i++) {
549                         if (rScores[index][i] >= score) {       return i;       }
550                 }
551                 return rScores[index].size();
552         }
553         catch(exception& e) {
554                 m->errorOut(e, "UnifracWeightedCommand", "findIndex");
555                 exit(1);
556         }
557 }
558
559 /***********************************************************/
560
561 void UnifracWeightedCommand::calculateFreqsCumuls() {
562         try {
563                 //clear out old tree values
564                 rScoreFreq.clear();
565                 rScoreFreq.resize(numComp);
566                 rCumul.clear();
567                 rCumul.resize(numComp);
568                 validScores.clear();
569         
570                 //calculate frequency
571                 for (int f = 0; f < numComp; f++) {
572                         for (int i = 0; i < rScores[f].size(); i++) { //looks like 0,0,1,1,1,2,4,7...  you want to make a map that say rScoreFreq[0] = 2, rScoreFreq[1] = 3...
573                                 validScores[rScores[f][i]] = rScores[f][i];
574                                 map<float,float>::iterator it = rScoreFreq[f].find(rScores[f][i]);
575                                 if (it != rScoreFreq[f].end()) {
576                                         rScoreFreq[f][rScores[f][i]]++;
577                                 }else{
578                                         rScoreFreq[f][rScores[f][i]] = 1;
579                                 }
580                         }
581                 }
582                 
583                 //calculate rcumul
584                 for(int a = 0; a < numComp; a++) {
585                         float rcumul = 1.0000;
586                         //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
587                         for (map<float,float>::iterator it = validScores.begin(); it != validScores.end(); it++) {
588                                 //make rscoreFreq map and rCumul
589                                 map<float,float>::iterator it2 = rScoreFreq[a].find(it->first);
590                                 rCumul[a][it->first] = rcumul;
591                                 //get percentage of random trees with that info
592                                 if (it2 != rScoreFreq[a].end()) {  rScoreFreq[a][it->first] /= iters; rcumul-= it2->second;  }
593                                 else { rScoreFreq[a][it->first] = 0.0000; } //no random trees with that score
594                         }
595                 }
596
597         }
598         catch(exception& e) {
599                 m->errorOut(e, "UnifracWeightedCommand", "calculateFreqsCums");
600                 exit(1);
601         }
602 }
603
604 /***********************************************************/
605
606
607
608
609