]> git.donarmstrong.com Git - mothur.git/blob - unifracweightedcommand.cpp
added pipeline commands which involved change to command factory and command class...
[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                                 vector<double> sums = weighted->getBranchLengthSums(T[i]); 
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(), sums, rScores);
249                                                 }else{
250                                                         createProcesses(T[i],  namesOfGroupCombos, sums, rScores);
251                                                 }
252                                         #else
253                                                 driver(T[i], namesOfGroupCombos, 0, namesOfGroupCombos.size(), sums, 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<double>& sums, 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, sums, 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 { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
353                 }
354                 
355                 driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, sums, scores);
356                 
357                 //force parent to wait until all the processes are done
358                 for (int i=0;i<(processors-1);i++) { 
359                         int temp = processIDS[i];
360                         wait(&temp);
361                 }
362                 
363                 //get data created by processes
364                 for (int i=0;i<(processors-1);i++) { 
365         
366                         ifstream in;
367                         string s = outputDir + toString(processIDS[i]) + ".weightedcommand.results.temp";
368                         m->openInputFile(s, in);
369                         
370                         double tempScore;
371                         for (int j = lines[(i+1)].start; j < (lines[(i+1)].start + lines[(i+1)].num); j++) { in >> tempScore; scores[j].push_back(tempScore); }
372                         in.close();
373                         remove(s.c_str());
374                 }
375                 
376                 return 0;
377 #endif          
378         }
379         catch(exception& e) {
380                 m->errorOut(e, "UnifracWeightedCommand", "createProcesses");
381                 exit(1);
382         }
383 }
384
385 /**************************************************************************************************/
386 int UnifracWeightedCommand::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, vector<double>& sums, vector< vector<double> >& scores) { 
387  try {
388                 Tree* randT = new Tree();
389
390                 for (int h = start; h < (start+num); h++) {
391         
392                         if (m->control_pressed) { return 0; }
393                 
394                         //initialize weighted score
395                         string groupA = namesOfGroupCombos[h][0]; 
396                         string groupB = namesOfGroupCombos[h][1];
397                         
398                         //copy T[i]'s info.
399                         randT->getCopy(t);
400                          
401                         //create a random tree with same topology as T[i], but different labels
402                         randT->assembleRandomUnifracTree(groupA, groupB);
403                         
404                         if (m->control_pressed) { delete randT;  return 0;  }
405
406                         //get wscore of random tree
407                         EstOutput randomData = weighted->getValues(randT, groupA, groupB, sums);
408                 
409                         if (m->control_pressed) { delete randT;  return 0;  }
410                                                                                 
411                         //save scores
412                         scores[h].push_back(randomData[0]);
413                 }
414         
415                 delete randT;
416         
417                 return 0;
418
419         }
420         catch(exception& e) {
421                 m->errorOut(e, "UnifracWeightedCommand", "driver");
422                 exit(1);
423         }
424 }
425 /***********************************************************/
426 void UnifracWeightedCommand::printWeightedFile() {
427         try {
428                 vector<double> data;
429                 vector<string> tags;
430                 tags.push_back("Score"); tags.push_back("RandFreq"); tags.push_back("RandCumul");
431                 
432                 for(int a = 0; a < numComp; a++) {
433                         output->initFile(groupComb[a], tags);
434                         //print each line
435                         for (map<float,float>::iterator it = validScores.begin(); it != validScores.end(); it++) { 
436                                 data.push_back(it->first);  data.push_back(rScoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]); 
437                                 output->output(data);
438                                 data.clear();
439                         } 
440                         output->resetFile();
441                 }
442         }
443         catch(exception& e) {
444                 m->errorOut(e, "UnifracWeightedCommand", "printWeightedFile");
445                 exit(1);
446         }
447 }
448
449
450 /***********************************************************/
451 void UnifracWeightedCommand::printWSummaryFile() {
452         try {
453                 //column headers
454                 outSum << "Tree#" << '\t' << "Groups" << '\t' << "WScore" << '\t' << "WSig" <<  endl;
455                 m->mothurOut("Tree#\tGroups\tWScore\tWSig"); m->mothurOutEndLine(); 
456                 
457                 //format output
458                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
459                 
460                 //print each line
461                 int count = 0;
462                 for (int i = 0; i < T.size(); i++) { 
463                         for (int j = 0; j < numComp; j++) {
464                                 if (random) {
465                                         if (WScoreSig[count] > (1/(float)iters)) {
466                                                 outSum << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(itersString.length()) << WScoreSig[count] << endl; 
467                                                 cout << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(itersString.length()) << WScoreSig[count] << endl; 
468                                                 m->mothurOutJustToLog(toString(i+1) +"\t" + groupComb[j] +"\t" + toString(utreeScores[count]) +"\t" +  toString(WScoreSig[count]) + "\n");   
469                                         }else{
470                                                 outSum << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(itersString.length()) << "<" << (1/float(iters)) << endl; 
471                                                 cout << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(itersString.length()) << "<" << (1/float(iters)) << endl; 
472                                                 m->mothurOutJustToLog(toString(i+1) +"\t" + groupComb[j] +"\t" + toString(utreeScores[count]) +"\t<" +  toString((1/float(iters))) + "\n");  
473                                         }
474                                 }else{
475                                         outSum << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << "0.00" << endl; 
476                                         cout << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << "0.00" << endl; 
477                                         m->mothurOutJustToLog(toString(i+1) +"\t" + groupComb[j] +"\t" + toString(utreeScores[count]) +"\t0.00\n"); 
478                                 }
479                                 count++;
480                         }
481                 }
482                 outSum.close();
483         }
484         catch(exception& e) {
485                 m->errorOut(e, "UnifracWeightedCommand", "printWSummaryFile");
486                 exit(1);
487         }
488 }
489 /***********************************************************/
490 void UnifracWeightedCommand::createPhylipFile() {
491         try {
492                 int count = 0;
493                 //for each tree
494                 for (int i = 0; i < T.size(); i++) { 
495                 
496                         string phylipFileName = outputDir + m->getSimpleName(globaldata->getTreeFile())  + toString(i+1) + ".weighted.dist";
497                         outputNames.push_back(phylipFileName);
498                         outputTypes["phylip"].push_back(phylipFileName);
499                         ofstream out;
500                         m->openOutputFile(phylipFileName, out);
501                         
502                         //output numSeqs
503                         out << globaldata->Groups.size() << endl;
504                         
505                         //make matrix with scores in it
506                         vector< vector<float> > dists;  dists.resize(globaldata->Groups.size());
507                         for (int i = 0; i < globaldata->Groups.size(); i++) {
508                                 dists[i].resize(globaldata->Groups.size(), 0.0);
509                         }
510                         
511                         //flip it so you can print it
512                         for (int r=0; r<globaldata->Groups.size(); r++) { 
513                                 for (int l = r+1; l < globaldata->Groups.size(); l++) {
514                                         dists[r][l] = utreeScores[count];
515                                         dists[l][r] = utreeScores[count];
516                                         count++;
517                                 }
518                         }
519
520                         //output to file
521                         for (int r=0; r<globaldata->Groups.size(); r++) { 
522                                 //output name
523                                 string name = globaldata->Groups[r];
524                                 if (name.length() < 10) { //pad with spaces to make compatible
525                                         while (name.length() < 10) {  name += " ";  }
526                                 }
527                                 out << name << '\t';
528                                 
529                                 //output distances
530                                 for (int l = 0; l < r; l++) {   out  << dists[r][l] << '\t';  }
531                                 out << endl;
532                         }
533                         out.close();
534                 }
535         }
536         catch(exception& e) {
537                 m->errorOut(e, "UnifracWeightedCommand", "createPhylipFile");
538                 exit(1);
539         }
540 }
541 /***********************************************************/
542 int UnifracWeightedCommand::findIndex(float score, int index) {
543         try{
544                 for (int i = 0; i < rScores[index].size(); i++) {
545                         if (rScores[index][i] >= score) {       return i;       }
546                 }
547                 return rScores[index].size();
548         }
549         catch(exception& e) {
550                 m->errorOut(e, "UnifracWeightedCommand", "findIndex");
551                 exit(1);
552         }
553 }
554
555 /***********************************************************/
556
557 void UnifracWeightedCommand::calculateFreqsCumuls() {
558         try {
559                 //clear out old tree values
560                 rScoreFreq.clear();
561                 rScoreFreq.resize(numComp);
562                 rCumul.clear();
563                 rCumul.resize(numComp);
564                 validScores.clear();
565         
566                 //calculate frequency
567                 for (int f = 0; f < numComp; f++) {
568                         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...
569                                 validScores[rScores[f][i]] = rScores[f][i];
570                                 map<float,float>::iterator it = rScoreFreq[f].find(rScores[f][i]);
571                                 if (it != rScoreFreq[f].end()) {
572                                         rScoreFreq[f][rScores[f][i]]++;
573                                 }else{
574                                         rScoreFreq[f][rScores[f][i]] = 1;
575                                 }
576                         }
577                 }
578                 
579                 //calculate rcumul
580                 for(int a = 0; a < numComp; a++) {
581                         float rcumul = 1.0000;
582                         //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
583                         for (map<float,float>::iterator it = validScores.begin(); it != validScores.end(); it++) {
584                                 //make rscoreFreq map and rCumul
585                                 map<float,float>::iterator it2 = rScoreFreq[a].find(it->first);
586                                 rCumul[a][it->first] = rcumul;
587                                 //get percentage of random trees with that info
588                                 if (it2 != rScoreFreq[a].end()) {  rScoreFreq[a][it->first] /= iters; rcumul-= it2->second;  }
589                                 else { rScoreFreq[a][it->first] = 0.0000; } //no random trees with that score
590                         }
591                 }
592
593         }
594         catch(exception& e) {
595                 m->errorOut(e, "UnifracWeightedCommand", "calculateFreqsCums");
596                 exit(1);
597         }
598 }
599
600 /***********************************************************/
601
602
603
604
605