]> git.donarmstrong.com Git - mothur.git/blobdiff - getseqscommand.cpp
paralellized screen.seqs and added mpi code to it. fixed bug with all mpi commands...
[mothur.git] / getseqscommand.cpp
index 03ccb82f6ef5c9c437942420163ac286fa35c203..f40c89b3fb3eff837729bc203055d11ef1a3c5a5 100644 (file)
 
 //**********************************************************************************************************************
 
-GetSeqsCommand::GetSeqsCommand(string option){
+GetSeqsCommand::GetSeqsCommand(string option)  {
        try {
                abort = false;
-               
+                               
                //allow user to run help
                if(option == "help") { help(); abort = true; }
                
                else {
                        //valid paramters for this command
-                       string Array[] =  {"fasta","name", "group", "alignreport", "accnos", "list"};
+                       string Array[] =  {"fasta","name", "group", "alignreport", "accnos", "list","outputdir","inputdir"};
                        vector<string> myArray (Array, Array+(sizeof(Array)/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;  }
                        }
                        
+                       //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 = "";         }
+                       
+                       //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("alignreport");
+                               //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["alignreport"] = inputDir + it->second;              }
+                               }
+                               
+                               it = parameters.find("fasta");
+                               //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["fasta"] = inputDir + it->second;            }
+                               }
+                               
+                               it = parameters.find("accnos");
+                               //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["accnos"] = inputDir + it->second;           }
+                               }
+                               
+                               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;             }
+                               }
+                               
+                               it = parameters.find("name");
+                               //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["name"] = inputDir + it->second;             }
+                               }
+                               
+                               it = parameters.find("group");
+                               //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["group"] = inputDir + it->second;            }
+                               }
+                       }
+
+                       
                        //check for required parameters
                        accnosfile = validParameter.validFile(parameters, "accnos", true);
                        if (accnosfile == "not open") { abort = true; }
-                       else if (accnosfile == "not found") {  accnosfile = "";  mothurOut("You must provide an accnos file."); mothurOutEndLine(); abort = true; }     
+                       else if (accnosfile == "not found") {  accnosfile = "";  m->mothurOut("You must provide an accnos file."); m->mothurOutEndLine(); abort = true; }       
                        
                        fastafile = validParameter.validFile(parameters, "fasta", true);
                        if (fastafile == "not open") { abort = true; }
@@ -60,14 +119,17 @@ GetSeqsCommand::GetSeqsCommand(string option){
                        if (listfile == "not open") { abort = true; }
                        else if (listfile == "not found") {  listfile = "";  }
                        
-                       if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == ""))  { mothurOut("You must provide one of the following: fasta, name, group, alignreport or listfile."); mothurOutEndLine(); abort = true; }
+                       if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == ""))  { m->mothurOut("You must provide one of the following: fasta, name, group, alignreport or listfile."); m->mothurOutEndLine(); abort = true; }
+                       
+                       int okay = 2;
+                       if (outputDir != "") { okay++; }
                        
-                       if (parameters.size() > 2) { mothurOut("You may only enter one of the following: fasta, name, group, alignreport or listfile."); mothurOutEndLine(); abort = true;  }
+                       if (parameters.size() > okay) { m->mothurOut("You may only enter one of the following: fasta, name, group, alignreport or listfile."); m->mothurOutEndLine(); abort = true;  }
                }
 
        }
        catch(exception& e) {
-               errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
+               m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
                exit(1);
        }
 }
@@ -75,15 +137,15 @@ GetSeqsCommand::GetSeqsCommand(string option){
 
 void GetSeqsCommand::help(){
        try {
-               mothurOut("The get.seqs command reads an .accnos file and one of the following file types: fasta, name, group, list or alignreport file.\n");
-               mothurOut("It outputs a file containing only the sequences in the .accnos file.\n");
-               mothurOut("The get.seqs command parameters are accnos, fasta, name, group, list and alignreport.  You must provide accnos and one of the other parameters.\n");
-               mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
-               mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
-               mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
+               m->mothurOut("The get.seqs command reads an .accnos file and one of the following file types: fasta, name, group, list or alignreport file.\n");
+               m->mothurOut("It outputs a file containing only the sequences in the .accnos file.\n");
+               m->mothurOut("The get.seqs command parameters are accnos, fasta, name, group, list and alignreport.  You must provide accnos and one of the other parameters.\n");
+               m->mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
+               m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
+               m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
        }
        catch(exception& e) {
-               errorOut(e, "GetSeqsCommand", "help");
+               m->errorOut(e, "GetSeqsCommand", "help");
                exit(1);
        }
 }
@@ -98,6 +160,8 @@ int GetSeqsCommand::execute(){
                //get names you want to keep
                readAccnos();
                
+               if (m->control_pressed) { return 0; }
+               
                //read through the correct file and output lines you want to keep
                if (fastafile != "")            {               readFasta();    }
                else if (namefile != "")        {               readName();             }
@@ -105,22 +169,33 @@ int GetSeqsCommand::execute(){
                else if (alignfile != "")       {               readAlign();    }
                else if (listfile != "")        {               readList();             }
                
+               if (m->control_pressed) { return 0; }
+               
+               if (outputNames.size() != 0) {
+                       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, "GetSeqsCommand", "execute");
+               m->errorOut(e, "GetSeqsCommand", "execute");
                exit(1);
        }
 }
 
 //**********************************************************************************************************************
-void GetSeqsCommand::readFasta(){
+int GetSeqsCommand::readFasta(){
        try {
-               string outputFileName = getRootName(fastafile) + "pick" +  getExtension(fastafile);
+               if (outputDir == "") { outputDir += hasPath(fastafile); }
+               string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "pick" +  getExtension(fastafile);
                ofstream out;
                openOutputFile(outputFileName, out);
                
+               
                ifstream in;
                openInputFile(fastafile, in);
                string name;
@@ -128,6 +203,9 @@ void GetSeqsCommand::readFasta(){
                bool wroteSomething = false;
                
                while(!in.eof()){
+               
+                       if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
+                       
                        Sequence currSeq(in);
                        name = currSeq.getName();
                        
@@ -147,20 +225,23 @@ void GetSeqsCommand::readFasta(){
                out.close();
                
                if (wroteSomething == false) {
-                       mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
+                       m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
                        remove(outputFileName.c_str()); 
-               }
+               }else {  outputNames.push_back(outputFileName); }
+               
+               return 0;
 
        }
        catch(exception& e) {
-               errorOut(e, "GetSeqsCommand", "readFasta");
+               m->errorOut(e, "GetSeqsCommand", "readFasta");
                exit(1);
        }
 }
 //**********************************************************************************************************************
-void GetSeqsCommand::readList(){
+int GetSeqsCommand::readList(){
        try {
-               string outputFileName = getRootName(listfile) + "pick" +  getExtension(listfile);
+               if (outputDir == "") { outputDir += hasPath(listfile); }
+               string outputFileName = outputDir + getRootName(getSimpleName(listfile)) + "pick" +  getExtension(listfile);
                ofstream out;
                openOutputFile(outputFileName, out);
                
@@ -170,6 +251,9 @@ void GetSeqsCommand::readList(){
                bool wroteSomething = false;
                
                while(!in.eof()){
+                       
+                       if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
+
                        //read in list vector
                        ListVector list(in);
                        
@@ -193,10 +277,13 @@ void GetSeqsCommand::readList(){
                                }
                        
                                //get last name
-                               if (names.count(binnames) == 1) {  newNames += binnames;  }
+                               if (names.count(binnames) == 1) {  newNames += binnames + ",";  }
 
                                //if there are names in this bin add to new list
-                               if (newNames != "") {  newList.push_back(newNames);     }
+                               if (newNames != "") { 
+                                       newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
+                                       newList.push_back(newNames);    
+                               }
                        }
                                
                        //print new listvector
@@ -211,23 +298,26 @@ void GetSeqsCommand::readList(){
                out.close();
                
                if (wroteSomething == false) {
-                       mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
+                       m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
                        remove(outputFileName.c_str()); 
-               }
+               }else {  outputNames.push_back(outputFileName); }
+               
+               return 0;
 
        }
        catch(exception& e) {
-               errorOut(e, "GetSeqsCommand", "readList");
+               m->errorOut(e, "GetSeqsCommand", "readList");
                exit(1);
        }
 }
 //**********************************************************************************************************************
-void GetSeqsCommand::readName(){
+int GetSeqsCommand::readName(){
        try {
-       
-               string outputFileName = getRootName(namefile) + "pick" +  getExtension(namefile);
+               if (outputDir == "") { outputDir += hasPath(namefile); }
+               string outputFileName = outputDir + getRootName(getSimpleName(namefile)) + "pick" +  getExtension(namefile);
                ofstream out;
                openOutputFile(outputFileName, out);
+               
 
                ifstream in;
                openInputFile(namefile, in);
@@ -237,6 +327,8 @@ void GetSeqsCommand::readName(){
                
                
                while(!in.eof()){
+               
+                       if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
 
                        in >> firstCol;                         
                        in >> secondCol;                        
@@ -293,24 +385,27 @@ void GetSeqsCommand::readName(){
                out.close();
                
                if (wroteSomething == false) {
-                       mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
+                       m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
                        remove(outputFileName.c_str()); 
-               }
+               }else {  outputNames.push_back(outputFileName); }
+               
+               return 0;
                
        }
        catch(exception& e) {
-               errorOut(e, "GetSeqsCommand", "readName");
+               m->errorOut(e, "GetSeqsCommand", "readName");
                exit(1);
        }
 }
 
 //**********************************************************************************************************************
-void GetSeqsCommand::readGroup(){
+int GetSeqsCommand::readGroup(){
        try {
-       
-               string outputFileName = getRootName(groupfile) + "pick" + getExtension(groupfile);
+               if (outputDir == "") { outputDir += hasPath(groupfile); }
+               string outputFileName = outputDir + getRootName(getSimpleName(groupfile)) + "pick" + getExtension(groupfile);
                ofstream out;
                openOutputFile(outputFileName, out);
+               
 
                ifstream in;
                openInputFile(groupfile, in);
@@ -320,6 +415,9 @@ void GetSeqsCommand::readGroup(){
                
                while(!in.eof()){
 
+                       if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
+
+
                        in >> name;                             //read from first column
                        in >> group;                    //read from second column
                        
@@ -338,24 +436,28 @@ void GetSeqsCommand::readGroup(){
                out.close();
                
                if (wroteSomething == false) {
-                       mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
+                       m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
                        remove(outputFileName.c_str()); 
-               }
+               }else {  outputNames.push_back(outputFileName); }
+               
+               return 0;
 
        }
        catch(exception& e) {
-               errorOut(e, "GetSeqsCommand", "readGroup");
+               m->errorOut(e, "GetSeqsCommand", "readGroup");
                exit(1);
        }
 }
 
 //**********************************************************************************************************************
 //alignreport file has a column header line then all other lines contain 16 columns.  we just want the first column since that contains the name
-void GetSeqsCommand::readAlign(){
+int GetSeqsCommand::readAlign(){
        try {
-               string outputFileName = getRootName(getRootName(alignfile)) + "pick.align.report";
+               if (outputDir == "") { outputDir += hasPath(alignfile); }
+               string outputFileName = outputDir + getRootName(getSimpleName(alignfile)) + "pick.align.report";
                ofstream out;
                openOutputFile(outputFileName, out);
+               
 
                ifstream in;
                openInputFile(alignfile, in);
@@ -371,6 +473,9 @@ void GetSeqsCommand::readAlign(){
                out << endl;
                
                while(!in.eof()){
+               
+                       if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
+
 
                        in >> name;                             //read from first column
                        
@@ -403,19 +508,21 @@ void GetSeqsCommand::readAlign(){
                out.close();
                
                if (wroteSomething == false) {
-                       mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
+                       m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
                        remove(outputFileName.c_str()); 
-               }
+               }else {  outputNames.push_back(outputFileName); }
+               
+               return 0;
                
        }
        catch(exception& e) {
-               errorOut(e, "GetSeqsCommand", "readAlign");
+               m->errorOut(e, "GetSeqsCommand", "readAlign");
                exit(1);
        }
 }
 //**********************************************************************************************************************
 
-void GetSeqsCommand::readAccnos(){
+int GetSeqsCommand::readAccnos(){
        try {
                
                ifstream in;
@@ -429,11 +536,13 @@ void GetSeqsCommand::readAccnos(){
                        
                        gobble(in);
                }
-               in.close();             
+               in.close();     
+               
+               return 0;
 
        }
        catch(exception& e) {
-               errorOut(e, "GetSeqsCommand", "readAccnos");
+               m->errorOut(e, "GetSeqsCommand", "readAccnos");
                exit(1);
        }
 }