]> git.donarmstrong.com Git - mothur.git/blobdiff - binsequencecommand.cpp
added get.otus and remove.otus commands
[mothur.git] / binsequencecommand.cpp
index 274a8901db3c8f17ad7e70519916c009e5613193..bf14d6b07b3b6ae64c20d3072abb3c3603a9e42c 100644 (file)
@@ -9,6 +9,55 @@
 
 #include "binsequencecommand.h"
 
+//**********************************************************************************************************************
+vector<string> BinSeqCommand::getValidParameters(){    
+       try {
+               string AlignArray[] =  {"fasta","label","name", "group","outputdir","inputdir"};
+               vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "BinSeqCommand", "getValidParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> BinSeqCommand::getRequiredParameters(){ 
+       try {
+               string AlignArray[] =  {"fasta"};
+               vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "BinSeqCommand", "getRequiredParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> BinSeqCommand::getRequiredFiles(){      
+       try {
+               string AlignArray[] =  {"list"};
+               vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "BinSeqCommand", "getRequiredFiles");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+BinSeqCommand::BinSeqCommand(){        
+       try {
+               abort = true;
+               //initialize outputTypes
+               vector<string> tempOutNames;
+               outputTypes["fasta"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "BinSeqCommand", "BinSeqCommand");
+               exit(1);
+       }
+}
 //**********************************************************************************************************************
 BinSeqCommand::BinSeqCommand(string option) {
        try {
@@ -36,10 +85,14 @@ BinSeqCommand::BinSeqCommand(string option) {
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["fasta"] = tempOutNames;
+                       
                        //if the user changes the output directory command factory will send this info to us in the output parameter 
                        outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
                                outputDir = ""; 
-                               outputDir += hasPath(globaldata->getListFile()); //if user entered a file with a path then preserve it  
+                               outputDir += m->hasPath(globaldata->getListFile()); //if user entered a file with a path then preserve it       
                        }
 
                        
@@ -58,7 +111,7 @@ BinSeqCommand::BinSeqCommand(string option) {
                                it = parameters.find("fasta");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       path = m->hasPath(it->second);
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
                                }
@@ -66,7 +119,7 @@ BinSeqCommand::BinSeqCommand(string option) {
                                it = parameters.find("name");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       path = m->hasPath(it->second);
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["name"] = inputDir + it->second;             }
                                }
@@ -74,7 +127,7 @@ BinSeqCommand::BinSeqCommand(string option) {
                                it = parameters.find("group");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       path = m->hasPath(it->second);
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["group"] = inputDir + it->second;            }
                                }
@@ -92,7 +145,7 @@ BinSeqCommand::BinSeqCommand(string option) {
                        label = validParameter.validFile(parameters, "label", false);                   
                        if (label == "not found") { label = ""; }
                        else { 
-                               if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
+                               if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
                                else { allLines = 1;  }
                        }
                        
@@ -111,7 +164,7 @@ BinSeqCommand::BinSeqCommand(string option) {
                        else if (groupfile == "not found") { groupfile = ""; }
                        
                        if (abort == false) { 
-//                             openInputFile(fastafile, in);
+//                             m->openInputFile(fastafile, in);
                                fasta = new FastaMap();
                                if (groupfile != "") {
                                        groupMap = new GroupMap(groupfile);
@@ -189,6 +242,8 @@ int BinSeqCommand::execute(){
                list = globaldata->gListVector;
                string lastLabel = list->getLabel();
                
+               if (m->control_pressed) {  return 0; }
+               
                //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
                set<string> processedLabels;
                set<string> userLabels = labels;
@@ -196,23 +251,25 @@ int BinSeqCommand::execute(){
                                
                while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
                        
+                       if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str());         } return 0; }   
+                       
                        if(allLines == 1 || labels.count(list->getLabel()) == 1){
                                
                                error = process(list);  
-                               if (error == 1) { return 0; }   
+                               if (error == 1) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());         } return 0; }   
                                                        
                                processedLabels.insert(list->getLabel());
                                userLabels.erase(list->getLabel());
                        }
                        
-                       if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
+                       if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
                                string saveLabel = list->getLabel();
                                
                                delete list;
                                list = input->getListVector(lastLabel);
                                
                                error = process(list);  
-                               if (error == 1) { return 0; }
+                               if (error == 1) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());         } return 0; }
                                                                                                        
                                processedLabels.insert(list->getLabel());
                                userLabels.erase(list->getLabel());
@@ -227,7 +284,8 @@ int BinSeqCommand::execute(){
                        list = input->getListVector();
                }
                
-               
+               if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str());         } return 0; }   
+
                //output error messages about any remaining user labels
                set<string>::iterator it;
                bool needToRun = false;
@@ -247,11 +305,13 @@ int BinSeqCommand::execute(){
                        list = input->getListVector(lastLabel);
                                
                        error = process(list);  
-                       if (error == 1) { return 0; }
+                       if (error == 1) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());         } return 0; }
                        
                        delete list;  
                }
                
+               if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str());         } return 0; }   
+
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
                for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
@@ -270,7 +330,7 @@ int BinSeqCommand::execute(){
 void BinSeqCommand::readNamesFile() {
        try {
                vector<string> dupNames;
-               openInputFile(namesfile, inNames);
+               m->openInputFile(namesfile, inNames);
                
                string name, names, sequence;
        
@@ -281,7 +341,7 @@ void BinSeqCommand::readNamesFile() {
                        dupNames.clear();
                        
                        //parse names into vector
-                       splitAtComma(names, dupNames);
+                       m->splitAtComma(names, dupNames);
                        
                        //store names in fasta map
                        sequence = fasta->getSequence(name);
@@ -289,7 +349,7 @@ void BinSeqCommand::readNamesFile() {
                                fasta->push_back(dupNames[i], sequence);
                        }
                
-                       gobble(inNames);
+                       m->gobble(inNames);
                }
                inNames.close();
 
@@ -305,17 +365,19 @@ int BinSeqCommand::process(ListVector* list) {
        try {
                                string binnames, name, sequence;
                                
-                               string outputFileName = outputDir + getRootName(getSimpleName(globaldata->getListFile())) + list->getLabel() + ".fasta";
-                               openOutputFile(outputFileName, out);
+                               string outputFileName = outputDir + m->getRootName(m->getSimpleName(globaldata->getListFile())) + list->getLabel() + ".fasta";
+                               m->openOutputFile(outputFileName, out);
                                
                                //save to output list of output file names
-                               outputNames.push_back(outputFileName);
+                               outputNames.push_back(outputFileName);  outputTypes["fasta"].push_back(outputFileName);
 
                                m->mothurOut(list->getLabel()); m->mothurOutEndLine();
                                
                                //for each bin in the list vector
                                for (int i = 0; i < list->size(); i++) {
-
+                                       
+                                       if (m->control_pressed) {  return 1; }
+                                       
                                        binnames = list->get(i);
                                        while (binnames.find_first_of(',') != -1) { 
                                                name = binnames.substr(0,binnames.find_first_of(','));
@@ -333,7 +395,6 @@ int BinSeqCommand::process(ListVector* list) {
                                                                string group = groupMap->getGroup(name);
                                                                if (group == "not found") {  
                                                                        m->mothurOut(name + " is missing from your group file. Please correct. ");  m->mothurOutEndLine();
-                                                                       remove(outputFileName.c_str());
                                                                        return 1;
                                                                }else{
                                                                        name = name + "|" + group + "|" + toString(i+1);
@@ -343,7 +404,6 @@ int BinSeqCommand::process(ListVector* list) {
                                                        }
                                                }else { 
                                                        m->mothurOut(name + " is missing from your fasta or name file. Please correct. "); m->mothurOutEndLine();
-                                                       remove(outputFileName.c_str());
                                                        return 1;
                                                }
                                                
@@ -361,7 +421,6 @@ int BinSeqCommand::process(ListVector* list) {
                                                        string group = groupMap->getGroup(binnames);
                                                        if (group == "not found") {  
                                                                m->mothurOut(binnames + " is missing from your group file. Please correct. "); m->mothurOutEndLine();
-                                                               remove(outputFileName.c_str());
                                                                return 1;
                                                        }else{
                                                                binnames = binnames + "|" + group + "|" + toString(i+1);
@@ -371,7 +430,6 @@ int BinSeqCommand::process(ListVector* list) {
                                                }
                                        }else { 
                                                m->mothurOut(binnames + " is missing from your fasta or name file. Please correct. "); m->mothurOutEndLine();
-                                               remove(outputFileName.c_str());
                                                return 1;
                                        }
                                }