]> git.donarmstrong.com Git - mothur.git/blobdiff - unweighted.cpp
fixed bug in unifrac commands with unrooted trees
[mothur.git] / unweighted.cpp
index edd628d82b72a871b6595aed0d1e644a7fd89af9..fb2f0b22afa3cc2143ad95afe6c45e4e093db222 100644 (file)
@@ -66,7 +66,6 @@ EstOutput Unweighted::getValues(Tree* t, int p, string o) {
                
                                        lines.push_back(linePair(startPos, numPairsPerProcessor));
                                }
-
                                data = createProcesses(t, namesOfGroupCombos);
                                lines.clear();
                        }
@@ -87,7 +86,6 @@ EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfG
        try {
 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
                int process = 1;
-               int num = 0;
                vector<int> processIDS;
                
                EstOutput results;
@@ -116,7 +114,11 @@ EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfG
                                out.close();
                                
                                exit(0);
-                       }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
+                       }else { 
+                               m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
+                               for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
+                               exit(0); 
+                       }
                }
                
                results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
@@ -177,7 +179,7 @@ EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombo
                
                
                for (int h = start; h < (start+num); h++) {
-               
+       //cout << namesOfGroupCombos[h][0] << '\t' << namesOfGroupCombos[h][1] << endl;         
                        if (m->control_pressed) { return results; }
                
                        double UniqueBL=0.0000;  //a branch length is unique if it's chidren are from the same group
@@ -203,12 +205,14 @@ EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombo
 
                                nodePcountSize[i] = pcountSize;
                                
-                               if (pcountSize == 0) { ; }
+                               //cout << i << '\t' << t->tree[i].getName() << " br = " << abs(t->tree[i].getBranchLength()) << '\t';           
+                               if (pcountSize == 0) { }
                                else if ((t->tree[i].getBranchLength() != -1) && (pcountSize == 1)) {  UniqueBL += abs(t->tree[i].getBranchLength()); }
-                                                               
+                               
                                //if you are a leaf from a users group add to total
                                if (i < numLeaves) {
-                                       if ((t->tree[i].getBranchLength() != -1) && pcountSize != 0) {  
+                                       if ((t->tree[i].getBranchLength() != -1) && pcountSize != 0) { 
+                                       //cout << "added to total" << endl; 
                                                totalBL += abs(t->tree[i].getBranchLength()); 
                                        }
                                        tempTotals[i] = 0.0;  //we don't care about you, or we have already added you
@@ -220,17 +224,25 @@ EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombo
                                        //if yes, add your childrens tempTotals
                                        if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
                                                totalBL += tempTotals[lc] + tempTotals[rc]; 
+                                               //cout << "added to total " << tempTotals[lc] << '\t' << tempTotals[rc] << endl;
                                                if (t->tree[i].getBranchLength() != -1) {
                                                        tempTotals[i] = abs(t->tree[i].getBranchLength());
                                                }else {
                                                        tempTotals[i] = 0.0;
                                                }
+                                       }else if ((nodePcountSize[lc] == 0) && (nodePcountSize[rc] == 0)) { tempTotals[i] = 0.0;  //we don't care about you
                                        }else { //if no, your tempTotal is your childrens temp totals + your branch length
-                                               tempTotals[i] = tempTotals[lc] + tempTotals[rc] + abs(t->tree[i].getBranchLength()); 
+                                               if (t->tree[i].getBranchLength() != -1) {
+                                                       tempTotals[i] = tempTotals[lc] + tempTotals[rc] + abs(t->tree[i].getBranchLength()); 
+                                               }else {
+                                                       tempTotals[i] = tempTotals[lc] + tempTotals[rc];
+                                               }
                                        }
+                                       //cout << "temptotal = "<< tempTotals[i] << endl;
                                }
+
                        }
-       cout << UniqueBL << '\t' << totalBL << endl;            
+       //cout << UniqueBL << '\t' << totalBL << endl;          
                        UW = (UniqueBL / totalBL);  
        
                        if (isnan(UW) || isinf(UW)) { UW = 0; }
@@ -308,7 +320,7 @@ EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB, int p, st
                                        }
                                        lines.push_back(linePair(startPos, numPairsPerProcessor));
                                }
-
+                                       
                                data = createProcesses(t, namesOfGroupCombos, true);
                                
                                lines.clear();
@@ -330,7 +342,6 @@ EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfG
        try {
 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
                int process = 1;
-               int num = 0;
                vector<int> processIDS;
                
                EstOutput results;
@@ -357,7 +368,11 @@ EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfG
                                out.close();
                                
                                exit(0);
-                       }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
+                       }else { 
+                               m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
+                               for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
+                               exit(0); 
+                       }
                }
                
                results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, usingGroups);
@@ -380,12 +395,13 @@ EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfG
                        if (!in.eof()) {
                                int num;
                                in >> num; m->gobble(in);
-                               
+                                       
                                if (m->control_pressed) { break; }
                                
                                double w; 
                                for (int j = 0; j < num; j++) {
                                        in >> w;
+                                       
                                        results.push_back(w);
                                }
                                m->gobble(in);
@@ -409,8 +425,6 @@ EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombo
                EstOutput results; results.resize(num);
                
                int count = 0;
-               int total = num;
-               int twentyPercent = (total * 0.20);
                int numLeaves = t->getNumLeaves();
                
                Tree* copyTree = new Tree;
@@ -446,13 +460,13 @@ EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombo
                                }
                                
                                nodePcountSize[i] = pcountSize;
-                               
+                       
                                if (pcountSize == 0) { }
-                               else if ((copyTree->tree[i].getBranchLength() != -1) && (pcountSize == 1)) {  UniqueBL += abs(copyTree->tree[i].getBranchLength());     }
+                               else if ((copyTree->tree[i].getBranchLength() != -1) && (pcountSize == 1)) {  UniqueBL += abs(copyTree->tree[i].getBranchLength());      }
                                
                                //if you are a leaf from a users group add to total
                                if (i < numLeaves) {
-                                       if ((copyTree->tree[i].getBranchLength() != -1) && pcountSize != 0) {  
+                                       if ((copyTree->tree[i].getBranchLength() != -1) && pcountSize != 0) { 
                                                totalBL += abs(copyTree->tree[i].getBranchLength()); 
                                        }
                                        tempTotals[i] = 0.0;  //we don't care about you, or we have already added you
@@ -464,14 +478,21 @@ EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombo
                                        //if yes, add your childrens tempTotals
                                        if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
                                                totalBL += tempTotals[lc] + tempTotals[rc]; 
+                                               
                                                if (copyTree->tree[i].getBranchLength() != -1) {
                                                        tempTotals[i] = abs(copyTree->tree[i].getBranchLength());
                                                }else {
                                                        tempTotals[i] = 0.0;
                                                }
+                                       }else if ((nodePcountSize[lc] == 0) && (nodePcountSize[rc] == 0)) { tempTotals[i] = 0.0;  //we don't care about you
                                        }else { //if no, your tempTotal is your childrens temp totals + your branch length
-                                               tempTotals[i] = tempTotals[lc] + tempTotals[rc] + abs(copyTree->tree[i].getBranchLength()); 
+                                               if (t->tree[i].getBranchLength() != -1) {
+                                                       tempTotals[i] = tempTotals[lc] + tempTotals[rc] + abs(copyTree->tree[i].getBranchLength()); 
+                                               }else {
+                                                       tempTotals[i] = tempTotals[lc] + tempTotals[rc];
+                                               }
                                        }
+                                       
                                }
 
                        }