]> git.donarmstrong.com Git - mothur.git/blobdiff - getseqscommand.cpp
bugs fixes while testing for 1.5 release
[mothur.git] / getseqscommand.cpp
index e58cc883b26a33d2dec9526bb52bcbfb7de7741e..04b254599f46ef00081a3f153c0e097fea665cf4 100644 (file)
@@ -21,7 +21,7 @@ GetSeqsCommand::GetSeqsCommand(string option){
                
                else {
                        //valid paramters for this command
-                       string Array[] =  {"fasta","name", "group", "align", "accnos" };
+                       string Array[] =  {"fasta","name", "group", "alignreport", "accnos" };
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
                        OptionParser parser(option);
@@ -51,13 +51,13 @@ GetSeqsCommand::GetSeqsCommand(string option){
                        if (groupfile == "not open") { abort = true; }
                        else if (groupfile == "not found") {  groupfile = "";  }        
                        
-                       alignfile = validParameter.validFile(parameters, "align", true);
+                       alignfile = validParameter.validFile(parameters, "alignreport", true);
                        if (alignfile == "not open") { abort = true; }
                        else if (alignfile == "not found") {  alignfile = "";  }
                        
-                       if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == ""))  { mothurOut("You must provide one of the following: fasta, name, group, align."); mothurOutEndLine(); abort = true; }
+                       if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == ""))  { mothurOut("You must provide one of the following: fasta, name, group, alignreport."); mothurOutEndLine(); abort = true; }
                        
-                       if (parameters.size() > 2) { mothurOut("You may only enter one of the following: fasta, name, group, align."); mothurOutEndLine(); abort = true;  }
+                       if (parameters.size() > 2) { mothurOut("You may only enter one of the following: fasta, name, group, alignreport."); mothurOutEndLine(); abort = true;  }
                }
 
        }
@@ -72,7 +72,7 @@ void GetSeqsCommand::help(){
        try {
                mothurOut("The get.seqs command reads an .accnos file and one of the following file types: fasta, name, group 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 and align.  You must provide accnos and one of the other parameters.\n");
+               mothurOut("The get.seqs command parameters are accnos, fasta, name, group 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");
@@ -111,7 +111,7 @@ int GetSeqsCommand::execute(){
 //**********************************************************************************************************************
 void GetSeqsCommand::readFasta(){
        try {
-               string outputFileName = getRootName(fastafile) + "pick";
+               string outputFileName = getRootName(fastafile) + "pick" +  getExtension(fastafile);
                ofstream out;
                openOutputFile(outputFileName, out);
                
@@ -119,19 +119,30 @@ void GetSeqsCommand::readFasta(){
                openInputFile(fastafile, in);
                string name;
                
+               bool wroteSomething = false;
+               
                while(!in.eof()){
                        Sequence currSeq(in);
                        name = currSeq.getName();
                        
                        //if this name is in the accnos file
                        if (names.count(name) == 1) {
+                               wroteSomething = true;
+                               
                                currSeq.printSequence(out);
+                               
+                               names.erase(name);
                        }
                        
                        gobble(in);
                }
                in.close();     
-               out.close();    
+               out.close();
+               
+               if (wroteSomething == false) {
+                       mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
+                       remove(outputFileName.c_str()); 
+               }
 
        }
        catch(exception& e) {
@@ -144,7 +155,7 @@ void GetSeqsCommand::readFasta(){
 void GetSeqsCommand::readName(){
        try {
        
-               string outputFileName = getRootName(namefile) + "pick";
+               string outputFileName = getRootName(namefile) + "pick" +  getExtension(namefile);
                ofstream out;
                openOutputFile(outputFileName, out);
 
@@ -152,6 +163,8 @@ void GetSeqsCommand::readName(){
                openInputFile(namefile, in);
                string name, firstCol, secondCol;
                
+               bool wroteSomething = false;
+               
                
                while(!in.eof()){
 
@@ -179,16 +192,23 @@ void GetSeqsCommand::readName(){
                        
                        //if the name in the first column is in the set then print it and any other names in second column also in set
                        if (names.count(firstCol) == 1) {
+                       
+                               wroteSomething = true;
+                               
                                out << firstCol << '\t';
                                
                                //you know you have at least one valid second since first column is valid
                                for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
                                out << validSecond[validSecond.size()-1] << endl;
+                               
                        
                        //make first name in set you come to first column and then add the remaining names to second column
                        }else {
                                //you want part of this row
                                if (validSecond.size() != 0) {
+                               
+                                       wroteSomething = true;
+                                       
                                        out << validSecond[0] << '\t';
                                
                                        //you know you have at least one valid second since first column is valid
@@ -202,6 +222,11 @@ void GetSeqsCommand::readName(){
                in.close();
                out.close();
                
+               if (wroteSomething == false) {
+                       mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
+                       remove(outputFileName.c_str()); 
+               }
+               
        }
        catch(exception& e) {
                errorOut(e, "GetSeqsCommand", "readName");
@@ -213,7 +238,7 @@ void GetSeqsCommand::readName(){
 void GetSeqsCommand::readGroup(){
        try {
        
-               string outputFileName = getRootName(groupfile) + "pick";
+               string outputFileName = getRootName(groupfile) + "pick" + getExtension(groupfile);
                ofstream out;
                openOutputFile(outputFileName, out);
 
@@ -221,6 +246,8 @@ void GetSeqsCommand::readGroup(){
                openInputFile(groupfile, in);
                string name, group;
                
+               bool wroteSomething = false;
+               
                while(!in.eof()){
 
                        in >> name;                             //read from first column
@@ -228,13 +255,22 @@ void GetSeqsCommand::readGroup(){
                        
                        //if this name is in the accnos file
                        if (names.count(name) == 1) {
+                               wroteSomething = true;
+                               
                                out << name << '\t' << group << endl;
+                               
+                               names.erase(name);
                        }
                                        
                        gobble(in);
                }
                in.close();
                out.close();
+               
+               if (wroteSomething == false) {
+                       mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
+                       remove(outputFileName.c_str()); 
+               }
 
        }
        catch(exception& e) {
@@ -247,7 +283,7 @@ void GetSeqsCommand::readGroup(){
 //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(){
        try {
-               string outputFileName = getRootName(alignfile) + "pick";
+               string outputFileName = getRootName(getRootName(alignfile)) + "pick.align.report";
                ofstream out;
                openOutputFile(outputFileName, out);
 
@@ -255,6 +291,8 @@ void GetSeqsCommand::readAlign(){
                openInputFile(alignfile, in);
                string name, junk;
                
+               bool wroteSomething = false;
+               
                //read column headers
                for (int i = 0; i < 16; i++) {  
                        if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
@@ -268,7 +306,8 @@ void GetSeqsCommand::readAlign(){
                        
                        //if this name is in the accnos file
                        if (names.count(name) == 1) {
-
+                               wroteSomething = true;
+                               
                                out << name << '\t';
                                
                                //read rest
@@ -278,6 +317,8 @@ void GetSeqsCommand::readAlign(){
                                }
                                out << endl;
                                
+                               names.erase(name);
+                               
                        }else {//still read just don't do anything with it
                                //read rest
                                for (int i = 0; i < 15; i++) {  
@@ -290,7 +331,11 @@ void GetSeqsCommand::readAlign(){
                }
                in.close();
                out.close();
-
+               
+               if (wroteSomething == false) {
+                       mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
+                       remove(outputFileName.c_str()); 
+               }
                
        }
        catch(exception& e) {
@@ -299,6 +344,7 @@ void GetSeqsCommand::readAlign(){
        }
 }
 //**********************************************************************************************************************
+
 void GetSeqsCommand::readAccnos(){
        try {