]> git.donarmstrong.com Git - mothur.git/blobdiff - getseqscommand.cpp
changed groupfile in classify.seqs to reflect multiple fasta files
[mothur.git] / getseqscommand.cpp
index d63dc79974ac007f504f7f1928ed8d0f031dcfe3..7fdf6ed7084463b4e875c8fa01a4e4f9b13db513 100644 (file)
 
 //**********************************************************************************************************************
 
-GetSeqsCommand::GetSeqsCommand(string option){
+GetSeqsCommand::GetSeqsCommand(string option)  {
        try {
                abort = false;
-               
+                               
                //allow user to run help
                if(option == "help") { help(); abort = true; }
                
@@ -97,7 +97,7 @@ GetSeqsCommand::GetSeqsCommand(string option){
                        //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; }
@@ -119,17 +119,18 @@ 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 (inputDir != "")     { okay++; }
                        
-                       if (parameters.size() > okay) { 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);
        }
 }
@@ -137,15 +138,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);
        }
 }
@@ -160,6 +161,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();             }
@@ -167,23 +170,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 {
                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;
@@ -191,6 +204,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();
                        
@@ -210,18 +226,20 @@ 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 {
                if (outputDir == "") { outputDir += hasPath(listfile); }
                string outputFileName = outputDir + getRootName(getSimpleName(listfile)) + "pick" +  getExtension(listfile);
@@ -234,6 +252,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);
                        
@@ -257,10 +278,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
@@ -275,23 +299,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 {
                if (outputDir == "") { outputDir += hasPath(namefile); }
                string outputFileName = outputDir + getRootName(getSimpleName(namefile)) + "pick" +  getExtension(namefile);
                ofstream out;
                openOutputFile(outputFileName, out);
+               
 
                ifstream in;
                openInputFile(namefile, in);
@@ -301,6 +328,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;                        
@@ -357,24 +386,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 {
                if (outputDir == "") { outputDir += hasPath(groupfile); }
                string outputFileName = outputDir + getRootName(getSimpleName(groupfile)) + "pick" + getExtension(groupfile);
                ofstream out;
                openOutputFile(outputFileName, out);
+               
 
                ifstream in;
                openInputFile(groupfile, in);
@@ -384,6 +416,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
                        
@@ -402,25 +437,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 {
                if (outputDir == "") { outputDir += hasPath(alignfile); }
                string outputFileName = outputDir + getRootName(getSimpleName(alignfile)) + "pick.align.report";
                ofstream out;
                openOutputFile(outputFileName, out);
+               
 
                ifstream in;
                openInputFile(alignfile, in);
@@ -436,6 +474,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
                        
@@ -468,19 +509,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;
@@ -494,11 +537,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);
        }
 }