]> git.donarmstrong.com Git - mothur.git/blobdiff - splitabundcommand.cpp
fixed bug in unifrac commands with unrooted trees
[mothur.git] / splitabundcommand.cpp
index 607009129b3c13ec8b7adfafd66e4d3fb006c486..d0f925eb0ae26d98cd5f8098eca3ade34fc9e5d4 100644 (file)
@@ -24,7 +24,7 @@ vector<string> SplitAbundCommand::getValidParameters(){
 //**********************************************************************************************************************
 SplitAbundCommand::SplitAbundCommand(){        
        try {
-               //initialize outputTypes
+               abort = true; calledHelp = true; 
                vector<string> tempOutNames;
                outputTypes["list"] = tempOutNames;
                outputTypes["name"] = tempOutNames;
@@ -63,11 +63,11 @@ vector<string> SplitAbundCommand::getRequiredFiles(){
 //**********************************************************************************************************************
 SplitAbundCommand::SplitAbundCommand(string option)  {
        try {
-               abort = false;
+               abort = false; calledHelp = false;   
                allLines = 1;
                        
                //allow user to run help
-               if(option == "help") { help(); abort = true; }
+               if(option == "help") { help(); abort = true; calledHelp = true; }
                
                else {
                        //valid paramters for this command
@@ -232,32 +232,10 @@ SplitAbundCommand::~SplitAbundCommand(){
 int SplitAbundCommand::execute(){
        try {
        
-               if (abort == true) {    return 0;       }
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
                if (listfile != "") { //you are using a listfile to determine abundance
                        if (outputDir == "") { outputDir = m->hasPath(listfile); }
-               
-                       //remove old files so you can append later....
-                       string fileroot = outputDir + m->getRootName(m->getSimpleName(listfile));
-                       if (Groups.size() == 0) {
-                               remove((fileroot + "rare.list").c_str());
-                               remove((fileroot + "abund.list").c_str());
-                               
-                               outputNames.push_back((fileroot + "rare.list"));
-                               outputNames.push_back((fileroot + "abund.list"));
-                               outputTypes["list"].push_back((fileroot + "rare.list"));
-                               outputTypes["list"].push_back((fileroot + "abund.list"));
-                       }else{
-                               for (int i=0; i<Groups.size(); i++) {
-                                       remove((fileroot + Groups[i] + ".rare.list").c_str());
-                                       remove((fileroot + Groups[i] + ".abund.list").c_str());
-                                       
-                                       outputNames.push_back((fileroot + Groups[i] + ".rare.list"));
-                                       outputNames.push_back((fileroot + Groups[i] + ".abund.list"));
-                                       outputTypes["list"].push_back((fileroot + Groups[i] + ".rare.list"));
-                                       outputTypes["list"].push_back((fileroot + Groups[i] + ".abund.list"));
-                               }
-                       }
                        
                        //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
                        set<string> processedLabels;
@@ -353,7 +331,7 @@ int SplitAbundCommand::execute(){
                        if (accnos)                                                     {  writeAccnos(tag);    }
                        if (fastafile != "")                            {  parseFasta(tag);             }
                }
-
+               
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
                for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
@@ -389,9 +367,11 @@ int SplitAbundCommand::splitList(ListVector* thisList) {
                        }
                }//end for
 
-               writeList(thisList);
                
                string tag = thisList->getLabel() + ".";
+               
+               writeList(thisList, tag);
+               
                if (groupfile != "")                            {  parseGroup(tag);             }
                if (accnos)                                                     {  writeAccnos(tag);    }
                if (fastafile != "")                            {  parseFasta(tag);             }
@@ -405,7 +385,7 @@ int SplitAbundCommand::splitList(ListVector* thisList) {
        }
 }
 /**********************************************************************************************************************/
-int SplitAbundCommand::writeList(ListVector* thisList) { 
+int SplitAbundCommand::writeList(ListVector* thisList, string tag) { 
        try {
                
                map<string, ofstream*> filehandles;
@@ -427,13 +407,13 @@ int SplitAbundCommand::writeList(ListVector* thisList) {
                        ofstream aout;
                        ofstream rout;
                        
-                       string rare = outputDir + m->getRootName(m->getSimpleName(listfile))  + "rare.list";
-                       m->openOutputFileAppend(rare, rout);
-                       //outputNames.push_back(rare);
+                       string rare = outputDir + m->getRootName(m->getSimpleName(listfile)) + tag + "rare.list";
+                       m->openOutputFile(rare, rout);
+                       outputNames.push_back(rare); outputTypes["list"].push_back(rare);
                        
-                       string abund = outputDir + m->getRootName(m->getSimpleName(listfile))  + "abund.list";
-                       m->openOutputFileAppend(abund, aout);
-                       //outputNames.push_back(abund);
+                       string abund = outputDir + m->getRootName(m->getSimpleName(listfile)) + tag + "abund.list";
+                       m->openOutputFile(abund, aout);
+                       outputNames.push_back(abund); outputTypes["list"].push_back(abund);
 
                        if (rareNames.size() != 0)      {  rout << thisList->getLabel() << '\t' << numRareBins << '\t';         }
                        if (abundNames.size() != 0) {   aout << thisList->getLabel() << '\t' << numAbundBins << '\t';   }
@@ -469,8 +449,10 @@ int SplitAbundCommand::writeList(ListVector* thisList) {
                                temp2 = new ofstream;
                                filehandles[Groups[i]+".abund"] = temp2;
                                
-                               m->openOutputFileAppend(fileroot + Groups[i] + ".rare.list", *(filehandles[Groups[i]+".rare"]));
-                               m->openOutputFileAppend(fileroot + Groups[i] + ".abund.list", *(filehandles[Groups[i]+".abund"]));
+                               m->openOutputFile(fileroot + Groups[i] + tag + ".rare.list", *(filehandles[Groups[i]+".rare"]));
+                               m->openOutputFile(fileroot + Groups[i] + tag + ".abund.list", *(filehandles[Groups[i]+".abund"]));
+                               outputNames.push_back(fileroot + Groups[i] + tag + ".rare.list"); outputTypes["list"].push_back(fileroot + Groups[i] + tag + ".rare.list");
+                               outputNames.push_back(fileroot + Groups[i] + tag + ".abund.list"); outputTypes["list"].push_back(fileroot + Groups[i] + tag + ".abund.list");
                        }
                        
                        map<string, string> groupVector;