]> git.donarmstrong.com Git - mothur.git/blobdiff - unifracunweightedcommand.cpp
fixed memory leak in parsimony calculator and added progress bars to parsimony and...
[mothur.git] / unifracunweightedcommand.cpp
index 5a7cc367cd12212743f836cda998a10c339b5c09..65f30ebc949f4945023fd295224d00d52827d82b 100644 (file)
@@ -36,36 +36,36 @@ UnifracUnweightedCommand::UnifracUnweightedCommand() {
 /***********************************************************/
 int UnifracUnweightedCommand::execute() {
        try {
-       
+
                userData.resize(numComp,0);  //data[0] = unweightedscore 
                randomData.resize(numComp,0); //data[0] = unweightedscore
                //create new tree with same num nodes and leaves as users
-                               
+               
+               outSum << "Tree#" << '\t' << "Groups" << '\t'  <<  "UWScore" <<'\t' << "UWSig" <<  endl;
+               cout << "Tree#" << '\t' << "Groups" << '\t'  <<  "UWScore" << '\t' << "UWSig" <<  endl;
+               
                //get pscores for users trees
                for (int i = 0; i < T.size(); i++) {
+                       counter = 0;
                        unweightedFile = globaldata->getTreeFile()  + toString(i+1) + ".unweighted";
                        unweightedFileout = globaldata->getTreeFile() + "temp." + toString(i+1) + ".unweighted";
-                                               //column headers
-                       outSum << "Tree# " << i+1 << endl;
-                       outSum << "Comb" << '\t'  <<  "UWScore" << '\t' << '\t' << "UWSig" <<  endl;
-
+                       
+                       outSum << i+1 << '\t';
+                       cout << i+1 << '\t';
+                       
                        //get unweighted for users tree
                        rscoreFreq.resize(numComp);  
                        rCumul.resize(numComp);  
-                       validScores.resize(numComp); 
                        utreeScores.resize(numComp);  
                        UWScoreSig.resize(numComp); 
 
-                       cout << "Processing tree " << i+1 << endl;
                        userData = unweighted->getValues(T[i]);  //userData[0] = unweightedscore
                        
                        //output scores for each combination
                        for(int k = 0; k < numComp; k++) {
-                               //add users score to valid scores
-                               validScores[k][userData[k]] = userData[k];
-                       
                                //saves users score
                                utreeScores[k].push_back(userData[k]);
+
                        }
                        
                        //get unweighted scores for random trees
@@ -74,7 +74,6 @@ int UnifracUnweightedCommand::execute() {
                                randomData = unweighted->getValues(T[i], "", "");
                                
                                for(int k = 0; k < numComp; k++) {      
-//cout << "iter " << j << " comp " << k << " = " << randomData[k] << endl;
                                        //add trees unweighted score to map of scores
                                        it2 = rscoreFreq[k].find(randomData[k]);
                                        if (it2 != rscoreFreq[k].end()) {//already have that score
@@ -84,14 +83,15 @@ int UnifracUnweightedCommand::execute() {
                                        }
                                
                                        //add randoms score to validscores
-                                       validScores[k][randomData[k]] = randomData[k];
+                                       validScores[randomData[k]] = randomData[k];
                                }
+                               
                        }
                
                for(int a = 0; a < numComp; a++) {
                        float rcumul = 1.0000;
                        //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
-                       for (it = validScores[a].begin(); it != validScores[a].end(); it++) { 
+                       for (it = validScores.begin(); it != validScores.end(); it++) { 
                                //make rscoreFreq map and rCumul
                                it2 = rscoreFreq[a].find(it->first);
                                rCumul[a][it->first] = rcumul;
@@ -102,6 +102,8 @@ int UnifracUnweightedCommand::execute() {
                        UWScoreSig[a].push_back(rCumul[a][userData[a]]);
                }
                
+               
+               
                printUnweightedFile();
                printUWSummaryFile();
                
@@ -135,7 +137,7 @@ void UnifracUnweightedCommand::printUnweightedFile() {
                for(int a = 0; a < numComp; a++) {
                        initFile(groupComb[a]);
                        //print each line
-                       for (it = validScores[a].begin(); it != validScores[a].end(); it++) { 
+                       for (it = validScores.begin(); it != validScores.end(); it++) { 
                                data.push_back(it->first);  data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]); 
                                output(data);
                                data.clear();
@@ -164,11 +166,17 @@ void UnifracUnweightedCommand::printUWSummaryFile() {
                                
                //format output
                outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
-               
+                       
                //print each line
+
                for(int a = 0; a < numComp; a++) {
-                       outSum << setprecision(6) << groupComb[a] << '\t' << '\t' << utreeScores[a][0] << '\t' << UWScoreSig[a][0] << endl;
-                       cout << setprecision(6)  << groupComb[a] << '\t' << '\t' << utreeScores[a][0] << '\t' << UWScoreSig[a][0] << endl; 
+                       if (UWScoreSig[a][0] > (1/(float)iters)) {
+                               outSum << setprecision(6) << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl;
+                               cout << setprecision(6)  << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl; 
+                       }else {
+                               outSum << setprecision(6) << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
+                               cout << setprecision(6)  << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl; 
+                       }
                }
                
        }
@@ -205,26 +213,31 @@ void UnifracUnweightedCommand::setGroups() {
                                        for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
                                                globaldata->Groups.push_back(tmap->namesOfGroups[i]);
                                                numGroups++;
-                                               allGroups += tmap->namesOfGroups[i];
+                                               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];
+                                               allGroups += globaldata->Groups[i] + "-";
                                                numGroups++;
                                        }
+                                       allGroups = allGroups.substr(0, allGroups.length()-1);
                                }
                        }else{//user has enter "all" and wants the default groups
+                               globaldata->Groups.clear();
                                for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
                                        globaldata->Groups.push_back(tmap->namesOfGroups[i]);
                                        numGroups++;
-                                       allGroups += tmap->namesOfGroups[i];
+                                       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 += tmap->namesOfGroups[i] + "-";
                        }
+                       allGroups = allGroups.substr(0, allGroups.length()-1);
                        numGroups = 1;
                }
                
@@ -232,7 +245,7 @@ void UnifracUnweightedCommand::setGroups() {
                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]);
+                               groupComb.push_back(globaldata->Groups[r]+ "-" + globaldata->Groups[l]);
                                numComp++;
                        }
                }
@@ -264,7 +277,7 @@ void UnifracUnweightedCommand::initFile(string label){
                        string inputBuffer;
                        getline(inFile, inputBuffer);
                
-                       out     <<  inputBuffer << '\t' << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;                
+                       out     <<  inputBuffer << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;           
                }else{
                        openOutputFile(unweightedFileout, out);
                        out     << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
@@ -290,11 +303,12 @@ void UnifracUnweightedCommand::output(vector<double> data){
                if(counter != 0){               
                        string inputBuffer;
                        getline(inFile, inputBuffer);
+//                     out     <<  inputBuffer << setprecision(6) << '\t' << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
                
-                       out     <<  inputBuffer << setprecision(6) << '\t' << 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{
-                       out << setprecision(6) << 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;
                }
 
        }