]> git.donarmstrong.com Git - mothur.git/blobdiff - parsimonycommand.cpp
started shared utilities, updates to venn and heatmap added tree.groups command
[mothur.git] / parsimonycommand.cpp
index 474f732ef9b3c448e6bfe27753df7d59c4dec300..4fa4ec5ed0943614bf4da9d5a1f03b5d8f706a42 100644 (file)
@@ -28,12 +28,22 @@ ParsimonyCommand::ParsimonyCommand() {
                }else { //user wants random distribution
                        savetmap = globaldata->gTreemap;
                        getUserInput();
-                       parsFile = randomtree + ".rd_parsimony";
-                       openOutputFile(parsFile, out);
+                       parsFile = randomtree;
+                       parsFileout = globaldata->getTreeFile() + "temp";
                }
                
                //set users groups to analyze
-               setGroups();
+               util = new SharedUtil();
+               util->setGroups(globaldata->Groups, tmap->namesOfGroups, allGroups, numGroups, "unweighted");   //sets the groups the user wants to analyze
+               util->getCombos(groupComb, globaldata->Groups, numComp);
+               globaldata->setGroups("");
+               
+               //ABC
+               if (numComp != 1) {
+                       groupComb.push_back(allGroups);
+                       numComp++;
+               }
+
                convert(globaldata->getIters(), iters);  //how many random trees to generate
                pars = new Parsimony(tmap);
                counter = 0;
@@ -51,7 +61,9 @@ ParsimonyCommand::ParsimonyCommand() {
 /***********************************************************/
 int ParsimonyCommand::execute() {
        try {
-       
+               Progress* reading;
+               reading = new Progress("Comparing to random:", iters);
+               
                //get pscore for users tree
                userData.resize(numComp,0);  //data = AB, AC, BC, ABC.
                randomData.resize(numComp,0);  //data = AB, AC, BC, ABC.
@@ -66,9 +78,10 @@ int ParsimonyCommand::execute() {
                        //get pscores for users trees
                        for (int i = 0; i < T.size(); i++) {
                                userData = pars->getValues(T[i]);  //data = AB, AC, BC, ABC.
-                               
+
                                //output scores for each combination
                                for(int k = 0; k < numComp; k++) {
+
                                        //update uscoreFreq
                                        it = uscoreFreq[k].find(userData[k]);
                                        if (it == uscoreFreq[k].end()) {//new score
@@ -87,11 +100,13 @@ int ParsimonyCommand::execute() {
                        for (int j = 0; j < iters; j++) {
                                //create new tree with same num nodes and leaves as users
                                randT = new Tree();
+
                                //create random relationships between nodes
                                randT->assembleRandomTree();
+
                                //get pscore of random tree
                                randomData = pars->getValues(randT);
-                               
+                                       
                                for(int r = 0; r < numComp; r++) {
                                        //add trees pscore to map of scores
                                        it2 = rscoreFreq[r].find(randomData[r]);
@@ -105,18 +120,24 @@ int ParsimonyCommand::execute() {
                                        validScores[randomData[r]] = randomData[r];
                                }
                                
+                               //update progress bar
+                               reading->update(j);
+                               
                                delete randT;
                        }
+
                }else {
                        //get pscores for random trees
                        for (int j = 0; j < iters; j++) {
                                //create new tree with same num nodes and leaves as users
                                randT = new Tree();
                                //create random relationships between nodes
+
                                randT->assembleRandomTree();
+
                                //get pscore of random tree
                                randomData = pars->getValues(randT);
-                               
+                       
                                for(int r = 0; r < numComp; r++) {
                                        //add trees pscore to map of scores
                                        it2 = rscoreFreq[r].find(randomData[r]);
@@ -130,10 +151,13 @@ int ParsimonyCommand::execute() {
                                        validScores[randomData[r]] = randomData[r];
                                }
                                
+                               //update progress bar
+                               reading->update(j);
+                               
                                delete randT;
                        }
                }
-               
+
                for(int a = 0; a < numComp; a++) {
                        float rcumul = 0.0000;
                        float ucumul = 0.0000;
@@ -162,16 +186,25 @@ int ParsimonyCommand::execute() {
                        }
                }
                
+               //finish progress bar
+               reading->finish();
+               delete reading;
+
+               
                printParsimonyFile();
                if (randomtree == "") { printUSummaryFile(); }
                
                //reset globaldata's treemap if you just did random distrib
-               if (randomtree != "") { globaldata->gTreemap = savetmap; }
+               if (randomtree != "") {
+                       //memory leak prevention
+                       //if (globaldata->gTreemap != NULL) { delete globaldata->gTreemap;  }
+                       globaldata->gTreemap = savetmap;
+               }
                
                //reset randomTree parameter to ""
                globaldata->setRandomTree("");
                //reset groups parameter
-               globaldata->Groups.clear();  globaldata->setGroups("");
+               globaldata->Groups.clear(); 
                
                return 0;
                
@@ -226,8 +259,8 @@ void ParsimonyCommand::printParsimonyFile() {
 void ParsimonyCommand::printUSummaryFile() {
        try {
                //column headers
-               outSum << "Tree#" << '\t' << "Comb" << '\t'  <<  "ParsScore" << '\t' << '\t' << "ParsSig" <<  endl;
-               cout << "Tree#" << '\t' << "Comb" << '\t'  <<  "ParsScore" << '\t' << '\t' << "ParsSig" <<  endl;
+               outSum << "Tree#" << '\t' << "Groups" << '\t'  <<  "ParsScore" << '\t' << "ParsSig" <<  endl;
+               cout << "Tree#" << '\t' << "Groups" << '\t'  <<  "ParsScore" << '\t' << "ParsSig" <<  endl;
                
                //format output
                outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
@@ -237,11 +270,11 @@ void ParsimonyCommand::printUSummaryFile() {
                for (int i = 0; i< T.size(); i++) {
                        for(int a = 0; a < numComp; a++) {
                                if (UScoreSig[a][i] > (1/(float)iters)) {
-                                       outSum << setprecision(globaldata->getIters().length()) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << UScoreSig[a][i] << endl;
-                                       cout << setprecision(globaldata->getIters().length()) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << UScoreSig[a][i] << endl;
+                                       outSum << setprecision(6) << i+1 << '\t' << groupComb[a]  << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << UScoreSig[a][i] << endl;
+                                       cout << setprecision(6) << i+1 << '\t' << groupComb[a]  << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << UScoreSig[a][i] << endl;
                                }else {
-                                       outSum << setprecision(globaldata->getIters().length()) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << "<" << (1/float(iters)) << endl;
-                                       cout << setprecision(globaldata->getIters().length()) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << "<" << (1/float(iters)) << endl;
+                                       outSum << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length())  << '\t' << "<" << (1/float(iters)) << endl;
+                                       cout << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << "<" << (1/float(iters)) << endl;
                                }
                        }
                }
@@ -293,6 +326,8 @@ void ParsimonyCommand::getUserInput() {
                getline(cin, s);
                
                //save tmap for later
+               //memory leak prevention
+               //if (globaldata->gTreemap != NULL) { delete globaldata->gTreemap;  }
                globaldata->gTreemap = tmap;
                
        }
@@ -305,83 +340,6 @@ void ParsimonyCommand::getUserInput() {
                exit(1);
        }
 }
-/***********************************************************/
-
-void ParsimonyCommand::setGroups() {
-       try {
-               string allGroups = "";
-               numGroups = 0;
-               //if the user has not entered specific groups to analyze then do them all
-               if (globaldata->Groups.size() != 0) {
-                       if (globaldata->Groups[0] != "all") {
-                               //check that groups are valid
-                               for (int i = 0; i < globaldata->Groups.size(); i++) {
-                                       if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
-                                               cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
-                                               // erase the invalid group from globaldata->Groups
-                                               globaldata->Groups.erase(globaldata->Groups.begin()+i);
-                                       }
-                               }
-                       
-                               //if the user only entered invalid groups
-                               if (globaldata->Groups.size() == 0) { 
-                                       cout << "When using the groups parameter you must have at least 1 valid group. I will run the command using all the groups in your groupfile." << endl; 
-                                       for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
-                                               globaldata->Groups.push_back(tmap->namesOfGroups[i]);
-                                               numGroups++;
-                                               allGroups += tmap->namesOfGroups[i] + "-";
-                                       }
-                                       allGroups = allGroups.substr(0, allGroups.length()-1);
-                               }else {
-                                       for (int i = 0; i < globaldata->Groups.size(); i++) {
-                                               allGroups += globaldata->Groups[i] + "-";
-                                               numGroups++;
-                                       }
-                                       allGroups = allGroups.substr(0, allGroups.length()-1);
-                               }
-                       }else{//user has enter "all" and wants the default groups
-                               for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
-                                       globaldata->Groups.push_back(tmap->namesOfGroups[i]);
-                                       numGroups++;
-                                       allGroups += tmap->namesOfGroups[i] + "-";
-                               }
-                               allGroups = allGroups.substr(0, allGroups.length()-1);
-                               globaldata->setGroups("");
-                       }
-               }else {
-                       for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
-                               allGroups += tmap->namesOfGroups[i] + "-";
-                       }
-                       allGroups = allGroups.substr(0, allGroups.length()-1);
-                       numGroups = 1;
-               }
-               
-               //calculate number of comparsions
-               numComp = 0;
-               for (int r=0; r<numGroups; r++) { 
-                       for (int l = r+1; l < numGroups; l++) {
-                               groupComb.push_back(globaldata->Groups[r]+ "-" +globaldata->Groups[l]);
-                               numComp++;
-                       }
-               }
-               
-               //ABC
-               if (numComp != 1) {
-                       groupComb.push_back(allGroups);
-                       numComp++;
-               }
-               
-       }
-       catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-       catch(...) {
-               cout << "An unknown error has occurred in the ParsimonyCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }               
-
-}
 /*****************************************************************/
 
 void ParsimonyCommand::initFile(string label){
@@ -396,7 +354,7 @@ void ParsimonyCommand::initFile(string label){
                        if (randomtree == "") {
                                out <<  inputBuffer << '\t' << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
                        }else {
-                               out <<  inputBuffer << '\t' << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
+                               out <<  inputBuffer << '\t' << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
                        }
                }else{
                        openOutputFile(parsFileout, out);
@@ -404,7 +362,7 @@ void ParsimonyCommand::initFile(string label){
                        if (randomtree == "") {
                                out << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
                        }else {
-                               out << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
+                               out << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
                        }
                }
 
@@ -430,16 +388,16 @@ void ParsimonyCommand::output(vector<double> data){
                        getline(inFile, inputBuffer);
                
                        if (randomtree == "") {
-                               out << inputBuffer << '\t' << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
+                               out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
                        }else{
-                               out << inputBuffer << '\t' << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
+                               out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << endl;
                        }
                }
                else{
                        if (randomtree == "") {
-                               out << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
+                               out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
                        }else{
-                               out << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
+                               out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << endl;
                        }
                }