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