]> git.donarmstrong.com Git - mothur.git/blobdiff - splitabundcommand.cpp
fixed clearcut version bug, added group count output to get.groups and remove.groups
[mothur.git] / splitabundcommand.cpp
index 9679642d9b62fb4a621701d21ad4c0d7cadb7281..bdfbeae8f845fc235ac3d115367a13ac61ff883e 100644 (file)
@@ -9,14 +9,65 @@
 
 #include "splitabundcommand.h"
 
+//**********************************************************************************************************************
+vector<string> SplitAbundCommand::getValidParameters(){        
+       try {
+               string Array[] =  {"name","group","list","label","accnos","groups","fasta","cutoff","outputdir","inputdir"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SplitAbundCommand", "getValidParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+SplitAbundCommand::SplitAbundCommand(){        
+       try {
+               abort = true; calledHelp = true; 
+               vector<string> tempOutNames;
+               outputTypes["list"] = tempOutNames;
+               outputTypes["name"] = tempOutNames;
+               outputTypes["accnos"] = tempOutNames;
+               outputTypes["group"] = tempOutNames;
+               outputTypes["fasta"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SplitAbundCommand", "SplitAbundCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> SplitAbundCommand::getRequiredParameters(){     
+       try {
+               string Array[] =  {"fasta","list","name","or"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SplitAbundCommand", "getRequiredParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> SplitAbundCommand::getRequiredFiles(){  
+       try {
+               vector<string> myArray;
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SplitAbundCommand", "getRequiredFiles");
+               exit(1);
+       }
+}
 //**********************************************************************************************************************
 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
@@ -33,7 +84,15 @@ SplitAbundCommand::SplitAbundCommand(string option)  {
                        for (it = parameters.begin(); it != parameters.end(); it++) { 
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
-                                               
+                       
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["list"] = tempOutNames;
+                       outputTypes["name"] = tempOutNames;
+                       outputTypes["accnos"] = tempOutNames;
+                       outputTypes["group"] = tempOutNames;
+                       outputTypes["fasta"] = tempOutNames;                    
+                                                                                               
                        //if the user changes the input directory command factory will send this info to us in the output parameter 
                        string inputDir = validParameter.validFile(parameters, "inputdir", false);              
                        if (inputDir == "not found"){   inputDir = "";          }
@@ -173,28 +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"));
-                       }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"));
-                               }
-                       }
                        
                        //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
                        set<string> processedLabels;
@@ -290,7 +331,34 @@ int SplitAbundCommand::execute(){
                        if (accnos)                                                     {  writeAccnos(tag);    }
                        if (fastafile != "")                            {  parseFasta(tag);             }
                }
-
+               
+               //set fasta file as new current fastafile
+               string current = "";
+               itTypes = outputTypes.find("fasta");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
+               }
+               
+               itTypes = outputTypes.find("name");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
+               }
+               
+               itTypes = outputTypes.find("group");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
+               }
+               
+               itTypes = outputTypes.find("list");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
+               }
+               
+               itTypes = outputTypes.find("accnos");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
+               }
+               
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
                for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
@@ -326,9 +394,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);             }
@@ -342,7 +412,7 @@ int SplitAbundCommand::splitList(ListVector* thisList) {
        }
 }
 /**********************************************************************************************************************/
-int SplitAbundCommand::writeList(ListVector* thisList) { 
+int SplitAbundCommand::writeList(ListVector* thisList, string tag) { 
        try {
                
                map<string, ofstream*> filehandles;
@@ -364,13 +434,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';   }
@@ -406,8 +476,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;
@@ -572,11 +644,11 @@ int SplitAbundCommand::writeNames() { //namefile
                        
                        string rare = outputDir + m->getRootName(m->getSimpleName(namefile))  + "rare.names";
                        m->openOutputFile(rare, rout);
-                       outputNames.push_back(rare);
+                       outputNames.push_back(rare); outputTypes["name"].push_back(rare);
                        
                        string abund = outputDir + m->getRootName(m->getSimpleName(namefile))  + "abund.names";
                        m->openOutputFile(abund, aout);
-                       outputNames.push_back(abund);
+                       outputNames.push_back(abund); outputTypes["name"].push_back(abund);
                        
                        if (rareNames.size() != 0) {
                                for (set<string>::iterator itRare = rareNames.begin(); itRare != rareNames.end(); itRare++) {
@@ -642,7 +714,7 @@ int SplitAbundCommand::writeNames() { //namefile
                        
                        for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { 
                                (*(filehandles[it3->first])).close();
-                               outputNames.push_back(fileroot + it3->first + ".names");  
+                               outputNames.push_back(fileroot + it3->first + ".names");  outputTypes["name"].push_back(fileroot + it3->first + ".names");
                                delete it3->second;
                        }
                }
@@ -669,7 +741,7 @@ int SplitAbundCommand::writeAccnos(string tag) {
                        
                        string rare = outputDir + m->getRootName(m->getSimpleName(inputFile))  + tag + "rare.accnos";
                        m->openOutputFile(rare, rout);
-                       outputNames.push_back(rare);
+                       outputNames.push_back(rare); outputTypes["accnos"].push_back(rare); 
                        
                        for (set<string>::iterator itRare = rareNames.begin(); itRare != rareNames.end(); itRare++) {
                                rout << (*itRare) << endl;
@@ -678,7 +750,7 @@ int SplitAbundCommand::writeAccnos(string tag) {
                
                        string abund = outputDir + m->getRootName(m->getSimpleName(inputFile)) + tag  + "abund.accnos";
                        m->openOutputFile(abund, aout);
-                       outputNames.push_back(abund);
+                       outputNames.push_back(abund); outputTypes["accnos"].push_back(abund);
                        
                        for (set<string>::iterator itAbund = abundNames.begin(); itAbund != abundNames.end(); itAbund++) {
                                aout << (*itAbund) << endl;
@@ -723,7 +795,7 @@ int SplitAbundCommand::writeAccnos(string tag) {
                        //close files
                        for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { 
                                (*(filehandles[it3->first])).close();
-                               outputNames.push_back(fileroot + tag + it3->first + ".accnos");  
+                               outputNames.push_back(fileroot + tag + it3->first + ".accnos");  outputTypes["accnos"].push_back(fileroot + tag + it3->first + ".accnos");
                                delete it3->second;
                        }
                }
@@ -748,11 +820,11 @@ int SplitAbundCommand::parseGroup(string tag) { //namefile
                        
                        string rare = outputDir + m->getRootName(m->getSimpleName(groupfile))  + tag + "rare.groups";
                        m->openOutputFile(rare, rout);
-                       outputNames.push_back(rare);
+                       outputNames.push_back(rare); outputTypes["group"].push_back(rare);
                
                        string abund = outputDir + m->getRootName(m->getSimpleName(groupfile))  + tag + "abund.groups";
                        m->openOutputFile(abund, aout);
-                       outputNames.push_back(abund);
+                       outputNames.push_back(abund); outputTypes["group"].push_back(abund);
                        
                        for (map<string, string>::iterator itName = nameMap.begin(); itName != nameMap.end(); itName++) {                               
                                vector<string> names;
@@ -817,7 +889,7 @@ int SplitAbundCommand::parseGroup(string tag) { //namefile
                        
                        for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { 
                                (*(filehandles[it3->first])).close();
-                               outputNames.push_back(fileroot + tag + it3->first + ".groups");  
+                               outputNames.push_back(fileroot + tag + it3->first + ".groups");  outputTypes["group"].push_back(fileroot + tag + it3->first + ".groups");
                                delete it3->second;
                        }
                }
@@ -842,11 +914,11 @@ int SplitAbundCommand::parseFasta(string tag) { //namefile
                        
                        string rare = outputDir + m->getRootName(m->getSimpleName(fastafile))  + tag + "rare.fasta";
                        m->openOutputFile(rare, rout);
-                       outputNames.push_back(rare);
+                       outputNames.push_back(rare); outputTypes["fasta"].push_back(rare);
                
                        string abund = outputDir + m->getRootName(m->getSimpleName(fastafile))  + tag + "abund.fasta";
                        m->openOutputFile(abund, aout);
-                       outputNames.push_back(abund);
+                       outputNames.push_back(abund); outputTypes["fasta"].push_back(abund);
                
                        //open input file
                        ifstream in;
@@ -937,7 +1009,7 @@ int SplitAbundCommand::parseFasta(string tag) { //namefile
                        
                        for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { 
                                (*(filehandles[it3->first])).close();
-                               outputNames.push_back(fileroot + tag + it3->first + ".fasta");  
+                               outputNames.push_back(fileroot + tag + it3->first + ".fasta");  outputTypes["fasta"].push_back(fileroot + tag + it3->first + ".fasta");
                                delete it3->second;
                        }
                }