]> git.donarmstrong.com Git - mothur.git/blobdiff - getlistcountcommand.cpp
made get.oturep more memory efficient by breaking the work into 2 pieces.
[mothur.git] / getlistcountcommand.cpp
index 94437fddfa766efe0bf25233914fe59befd9f204..7757122776136ce25edf778709db6c4102a37b9a 100644 (file)
 #include "getlistcountcommand.h"
 
 //**********************************************************************************************************************
-GetListCountCommand::GetListCountCommand(string option){
+GetListCountCommand::GetListCountCommand(string option)  {
        try {
                globaldata = GlobalData::getInstance();
                abort = false;
                allLines = 1;
                labels.clear();
-               
+                               
                //allow user to run help
                if(option == "help") { help(); abort = true; }
                
                else {
                        //valid paramters for this command
-                       string AlignArray[] =  {"list","label"};
+                       string AlignArray[] =  {"list","label","outputdir","inputdir"};
                        vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
                        
                        OptionParser parser(option);
                        map<string, string> parameters = parser.getParameters();
                        
                        ValidParameters validParameter;
-               
+                       map<string, string>::iterator it;
+                       
                        //check to make sure all parameters are valid for command
-                       for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
+                       for (it = parameters.begin(); it != parameters.end(); it++) { 
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
                        string ranRead = globaldata->getListFile();
                        
+                       //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 = "";          }
+                       else {
+                               string path;
+                               it = parameters.find("list");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["list"] = inputDir + it->second;             }
+                               }
+                       }
+
+                       //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 = "";         }
+                       
                        //check for required parameters
                        listfile = validParameter.validFile(parameters, "list", true);
-                       if ((listfile == "not found") && (globaldata->getListFile() == ""))  { mothurOut("You must read a listfile before running the get.listcount command.");  mothurOutEndLine(); abort = true; }
+                       if ((listfile == "not found") && (globaldata->getListFile() == ""))  { m->mothurOut("You must read a listfile before running the get.listcount command.");  m->mothurOutEndLine(); abort = true; }
                        else if ((listfile == "not found") && (globaldata->getListFile() != "")) { listfile = globaldata->getListFile(); }
                        else if (listfile == "not open") { abort = true; }      
                        else { globaldata->setListFile(listfile); }
@@ -62,7 +80,7 @@ GetListCountCommand::GetListCountCommand(string option){
                }
        }
        catch(exception& e) {
-               errorOut(e, "GetListCountCommand", "GetListCountCommand");
+               m->errorOut(e, "GetListCountCommand", "GetListCountCommand");
                exit(1);
        }
 }
@@ -70,17 +88,17 @@ GetListCountCommand::GetListCountCommand(string option){
 
 void GetListCountCommand::help(){
        try {
-               mothurOut("The get.listcount command can only be executed after a successful read.otu command of a listfile or providing a list file using the list parameter.\n");
-               mothurOut("The get.listcount command parameters are list and label.  No parameters are required.\n");
-               mothurOut("The label parameter allows you to select what distance levels you would like a output files created for, and are separated by dashes.\n");
-               mothurOut("The get.listcount command should be in the following format: get.listcount(list=yourlistFile, label=yourLabels).\n");
-               mothurOut("Example get.listcount(list=amazon.fn.list, label=0.10).\n");
-               mothurOut("The default value for label is all lines in your inputfile.\n");
-               mothurOut("The get.listcount command outputs a .otu file for each distance you specify listing the bin number and the names of the sequences in that bin.\n");
-               mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
+               m->mothurOut("The get.listcount command can only be executed after a successful read.otu command of a listfile or providing a list file using the list parameter.\n");
+               m->mothurOut("The get.listcount command parameters are list and label.  No parameters are required.\n");
+               m->mothurOut("The label parameter allows you to select what distance levels you would like a output files created for, and are separated by dashes.\n");
+               m->mothurOut("The get.listcount command should be in the following format: get.listcount(list=yourlistFile, label=yourLabels).\n");
+               m->mothurOut("Example get.listcount(list=amazon.fn.list, label=0.10).\n");
+               m->mothurOut("The default value for label is all lines in your inputfile.\n");
+               m->mothurOut("The get.listcount command outputs a .otu file for each distance you specify listing the bin number and the names of the sequences in that bin.\n");
+               m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
        }
        catch(exception& e) {
-               errorOut(e, "GetListCountCommand", "help");
+               m->errorOut(e, "GetListCountCommand", "help");
                exit(1);
        }
 }
@@ -108,12 +126,30 @@ int GetListCountCommand::execute(){
                //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;
-
+               
+               if (m->control_pressed) { 
+                       delete read;
+                       delete input;
+                       delete list;
+                       globaldata->gListVector = NULL;  
+                       for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); }
+                       return 0; 
+               }
+               
                while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
                        
                        if(allLines == 1 || labels.count(list->getLabel()) == 1){
                        
                                process(list);
+                               
+                               if (m->control_pressed) { 
+                                       delete read;
+                                       delete input;
+                                       delete list;
+                                       globaldata->gListVector = NULL;  
+                                       for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); }
+                                       return 0; 
+                               }
                                                        
                                processedLabels.insert(list->getLabel());
                                userLabels.erase(list->getLabel());
@@ -126,6 +162,15 @@ int GetListCountCommand::execute(){
                                list = input->getListVector(lastLabel);
                                
                                process(list);
+                               
+                               if (m->control_pressed) { 
+                                       delete read;
+                                       delete input;
+                                       delete list;
+                                       globaldata->gListVector = NULL;  
+                                       for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); }
+                                       return 0; 
+                               }
                                                                                                        
                                processedLabels.insert(list->getLabel());
                                userLabels.erase(list->getLabel());
@@ -145,12 +190,12 @@ int GetListCountCommand::execute(){
                set<string>::iterator it;
                bool needToRun = false;
                for (it = userLabels.begin(); it != userLabels.end(); it++) {  
-                       mothurOut("Your file does not include the label " + *it); 
+                       m->mothurOut("Your file does not include the label " + *it); 
                        if (processedLabels.count(lastLabel) != 1) {
-                               mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
+                               m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
                                needToRun = true;
                        }else {
-                               mothurOut(". Please refer to " + lastLabel + ".");  mothurOutEndLine();
+                               m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
                        }
                }
                
@@ -159,17 +204,33 @@ int GetListCountCommand::execute(){
                        if (list != NULL) {             delete list;    }
                        list = input->getListVector(lastLabel);
                                
-                       process(list);                  
+                       process(list);  
+                       
+                       if (m->control_pressed) { 
+                                       delete read;
+                                       delete input;
+                                       delete list;
+                                       globaldata->gListVector = NULL;  
+                                       for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); }
+                                       return 0; 
+                       }
+                       
                        delete list;  
                }
                
                delete read;
+               delete input;
                globaldata->gListVector = NULL;
                
+               m->mothurOutEndLine();
+               m->mothurOut("Output File Names: "); m->mothurOutEndLine();
+               for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
+               m->mothurOutEndLine();
+               
                return 0;
        }
        catch(exception& e) {
-               errorOut(e, "GetListCountCommand", "execute");
+               m->errorOut(e, "GetListCountCommand", "execute");
                exit(1);
        }
 }
@@ -179,13 +240,17 @@ int GetListCountCommand::execute(){
 void GetListCountCommand::process(ListVector* list) {
        try {
                string binnames, name, sequence;
-               string outputFileName = getRootName(listfile) + list->getLabel() + ".otu";
+               if (outputDir == "") { outputDir += hasPath(listfile); }
+               string outputFileName = outputDir + getRootName(getSimpleName(listfile)) + list->getLabel() + ".otu";
                openOutputFile(outputFileName, out);
+               outputNames.push_back(outputFileName);
                
-               mothurOut(list->getLabel()); mothurOutEndLine();
+               m->mothurOut(list->getLabel()); m->mothurOutEndLine();
                
                //for each bin in the list vector
                for (int i = 0; i < list->getNumBins(); i++) {
+                       if (m->control_pressed) { break; }
+                       
                        binnames = list->get(i);
                        out << i+1 << '\t' << binnames << endl;
                }
@@ -193,7 +258,7 @@ void GetListCountCommand::process(ListVector* list) {
                out.close();
        }
        catch(exception& e) {
-               errorOut(e, "GetListCountCommand", "process");
+               m->errorOut(e, "GetListCountCommand", "process");
                exit(1);
        }
 }