]> git.donarmstrong.com Git - mothur.git/blobdiff - weighted.cpp
fixed order of unifrac commands so that it matches the summary commands
[mothur.git] / weighted.cpp
index d068369cca57d73c260e56e522b51fc5eef97fea..52d90c8ac4487d83acd1587b1d0f5c30b0c4cfe0 100644 (file)
@@ -30,7 +30,7 @@ EstOutput Weighted::getValues(Tree* t, int p, string o) {
                //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
                vector< vector<string> > namesOfGroupCombos;
                for (int i=0; i<numGroups; i++) { 
-                       for (int l = i+1; l < numGroups; l++) { 
+                       for (int l = 0; l < i; l++) {   
                                //initialize weighted scores
                                //WScore[globaldata->Groups[i]+globaldata->Groups[l]] = 0.0;
                                vector<string> groups; groups.push_back(globaldata->Groups[i]); groups.push_back(globaldata->Groups[l]);
@@ -51,12 +51,12 @@ EstOutput Weighted::getValues(Tree* t, int p, string o) {
                                        if(i == processors - 1){
                                                numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
                                        }
-                                       lines.push_back(new linePair(startPos, numPairsPerProcessor));
+                                       lines.push_back(linePair(startPos, numPairsPerProcessor));
                                }
 
                                data = createProcesses(t, namesOfGroupCombos, sums);
                                
-                               for (int i = 0; i < lines.size(); i++) {  delete lines[i];  }  lines.clear();
+                               lines.clear();
                        }
                #else
                        data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), sums);
@@ -74,7 +74,7 @@ EstOutput Weighted::getValues(Tree* t, int p, string o) {
 EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, vector<double>& sums) {
        try {
 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
-               int process = 0;
+               int process = 1;
                int num = 0;
                vector<int> processIDS;
                
@@ -88,64 +88,59 @@ EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGro
                                processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
                                process++;
                        }else if (pid == 0){
-                               results = driver(t, namesOfGroupCombos, lines[process]->start, lines[process]->num, sums);
-                               
-                               if (m->control_pressed) { exit(0); }
-                               
+       
+                               EstOutput Myresults;
+                               Myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, sums);
+                       
                                m->mothurOut("Merging results."); m->mothurOutEndLine();
                                
                                //pass numSeqs to parent
                                ofstream out;
-                               string tempFile = outputDir + toString(getpid()) + ".results.temp";
+
+                               string tempFile = outputDir + toString(getpid()) + ".weighted.results.temp";
+       
                                m->openOutputFile(tempFile, out);
-                               out << results.size() << endl;
-                               for (int i = 0; i < results.size(); i++) {  out << results[i] << '\t';  } out << endl;
+       
+                               out << Myresults.size() << endl;
+                               for (int i = 0; i < Myresults.size(); i++) {  out << Myresults[i] << '\t';  } out << endl;
                                out.close();
                                
                                exit(0);
                        }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
                }
-               
+       
+               results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, sums);
+       
                //force parent to wait until all the processes are done
-               for (int i=0;i<processors;i++) { 
+               for (int i=0;i<(processors-1);i++) { 
                        int temp = processIDS[i];
                        wait(&temp);
                }
-               
+       
                if (m->control_pressed) { return results; }
                
                //get data created by processes
-               for (int i=0;i<processors;i++) { 
+               for (int i=0;i<(processors-1);i++) { 
                        ifstream in;
-                       string s = outputDir + toString(processIDS[i]) + ".results.temp";
+                       string s = outputDir + toString(processIDS[i]) + ".weighted.results.temp";
                        m->openInputFile(s, in);
                        
-                       vector<double> r;
-                       
                        //get quantiles
                        while (!in.eof()) {
                                int num;
-                               in >> num; 
+                               in >> num; m->gobble(in);
                                
                                if (m->control_pressed) { break; }
-                               
-                               m->gobble(in);
 
                                double w; 
                                for (int j = 0; j < num; j++) {
                                        in >> w;
-                                       r.push_back(w);
+                                       results.push_back(w);
                                }
                                m->gobble(in);
                        }
                        in.close();
                        remove(s.c_str());
-       
-                       //save quan in quantiles
-                       for (int j = 0; j < r.size(); j++) {
-                               //put all values of r into results
-                               results.push_back(r[j]);   
-                       }
                }
                
                m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
@@ -161,15 +156,10 @@ EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGro
 /**************************************************************************************************/
 EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, vector<double>& sums) { 
  try {
-               globaldata = GlobalData::getInstance();
-               
                EstOutput results;
                vector<double> D;
                
                int count = 0;
-               int total = start+num;
-               int twentyPercent = (total * 0.20);
-
                for (int h = start; h < (start+num); h++) {
                
                        if (m->control_pressed) { return results; }
@@ -201,14 +191,12 @@ EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos,
                                D[count] += weightedSum;
                        }
                        count++;
-                       
-                       //report progress
-                       if((h) % twentyPercent == 0){   m->mothurOut("Percentage complete: " + toString(int((h / (float)total) * 100.0))); m->mothurOutEndLine();               }
                }
                
-               m->mothurOut("Percentage complete: 100"); m->mothurOutEndLine();
-               
                //calculate u for the group comb 
+               int total = t->getNumNodes();
+               int twentyPercent = (total * 0.20);
+               
                for(int i=0;i<t->getNumNodes();i++){
                        for (int h = start; h < (start+num); h++) {
                                
@@ -238,10 +226,13 @@ EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos,
                                //save groupcombs u value
                                WScore[(groupA+groupB)] += u;
                        }
+                       //report progress
+                       if((i) % twentyPercent == 0){   m->mothurOut("Percentage complete: " + toString(int((i / (float)total) * 100.0))); m->mothurOutEndLine();               }
                }
                
-               /********************************************************/
+               m->mothurOut("Percentage complete: 100"); m->mothurOutEndLine();
                
+               /********************************************************/
                //calculate weighted score for the group combination
                double UN;      
                count = 0;