]> git.donarmstrong.com Git - mothur.git/blobdiff - listseqscommand.cpp
Merge remote-tracking branch 'origin/master'
[mothur.git] / listseqscommand.cpp
index c8c82971a1cf53d0eb2ba4834c281cc310d8984b..bfbb0788c5092382f22bea643c5405674361dd67 100644 (file)
 #include "sequence.hpp"
 #include "listvector.hpp"
 
+
 //**********************************************************************************************************************
-vector<string> ListSeqsCommand::getValidParameters(){  
+vector<string> ListSeqsCommand::setParameters(){       
        try {
-               string Array[] =  {"fasta","name", "group", "alignreport","list","taxonomy","outputdir","inputdir"};
-               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               CommandParameter pfasta("fasta", "InputTypes", "", "", "FNGLT", "FNGLT", "none",false,false); parameters.push_back(pfasta);
+               CommandParameter pname("name", "InputTypes", "", "", "FNGLT", "FNGLT", "none",false,false); parameters.push_back(pname);
+               CommandParameter pgroup("group", "InputTypes", "", "", "FNGLT", "FNGLT", "none",false,false); parameters.push_back(pgroup);
+               CommandParameter plist("list", "InputTypes", "", "", "FNGLT", "FNGLT", "none",false,false); parameters.push_back(plist);
+               CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "FNGLT", "FNGLT", "none",false,false); parameters.push_back(ptaxonomy);
+               CommandParameter palignreport("alignreport", "InputTypes", "", "", "FNGLT", "FNGLT", "none",false,false); parameters.push_back(palignreport);
+               CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
+               CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
+               
+               vector<string> myArray;
+               for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
                return myArray;
        }
        catch(exception& e) {
-               m->errorOut(e, "ListSeqsCommand", "getValidParameters");
+               m->errorOut(e, "ListSeqsCommand", "setParameters");
                exit(1);
        }
 }
 //**********************************************************************************************************************
-ListSeqsCommand::ListSeqsCommand(){    
+string ListSeqsCommand::getHelpString(){       
        try {
-               //initialize outputTypes
-               vector<string> tempOutNames;
-               outputTypes["accnos"] = tempOutNames;
+               string helpString = "";
+               helpString += "The list.seqs command reads a fasta, name, group, list, taxonomy or alignreport file and outputs a .accnos file containing sequence names.\n";
+               helpString += "The list.seqs command parameters are fasta, name, group, list, taxonomy and alignreport.  You must provide one of these parameters.\n";
+               helpString += "The list.seqs command should be in the following format: list.seqs(fasta=yourFasta).\n";
+               helpString += "Example list.seqs(fasta=amazon.fasta).\n";
+               helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
+               return helpString;
        }
        catch(exception& e) {
-               m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
+               m->errorOut(e, "ListSeqsCommand", "getHelpString");
                exit(1);
        }
 }
 //**********************************************************************************************************************
-vector<string> ListSeqsCommand::getRequiredParameters(){       
+string ListSeqsCommand::getOutputFileNameTag(string type, string inputName=""){        
        try {
-               string Array[] =  {"fasta","name", "group", "alignreport","list","taxonomy","or"};
-               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
-               return myArray;
+        string outputFileName = "";
+               map<string, vector<string> >::iterator it;
+        
+        //is this a type this command creates
+        it = outputTypes.find(type);
+        if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
+        else {
+            if (type == "accnos")             {   outputFileName =  "accnos";       }
+            else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
+        }
+        return outputFileName;
        }
        catch(exception& e) {
-               m->errorOut(e, "ListSeqsCommand", "getRequiredParameters");
+               m->errorOut(e, "ListSeqsCommand", "getOutputFileNameTag");
                exit(1);
        }
 }
 //**********************************************************************************************************************
-vector<string> ListSeqsCommand::getRequiredFiles(){    
+ListSeqsCommand::ListSeqsCommand(){    
        try {
-               vector<string> myArray;
-               return myArray;
+               abort = true; calledHelp = true; 
+               setParameters();
+               vector<string> tempOutNames;
+               outputTypes["accnos"] = tempOutNames;
        }
        catch(exception& e) {
-               m->errorOut(e, "ListSeqsCommand", "getRequiredFiles");
+               m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
                exit(1);
        }
 }
@@ -62,15 +86,13 @@ vector<string> ListSeqsCommand::getRequiredFiles(){
 
 ListSeqsCommand::ListSeqsCommand(string option)  {
        try {
-               abort = false;
+               abort = false; calledHelp = false;   
                
                //allow user to run help
-               if(option == "help") { help(); abort = true; }
-               
+               if(option == "help") { help(); abort = true; calledHelp = true; }
+               else if(option == "citation") { citation(); abort = true; calledHelp = true;}
                else {
-                       //valid paramters for this command
-                       string Array[] =  {"fasta","name", "group", "alignreport","list","taxonomy","outputdir","inputdir"};
-                       vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+                       vector<string> myArray = setParameters();
                        
                        OptionParser parser(option);
                        map<string,string> parameters = parser.getParameters();
@@ -147,15 +169,18 @@ ListSeqsCommand::ListSeqsCommand(string option)  {
                        //check for required parameters
                        fastafile = validParameter.validFile(parameters, "fasta", true);
                        if (fastafile == "not open") { abort = true; }
-                       else if (fastafile == "not found") {  fastafile = "";  }        
+                       else if (fastafile == "not found") {  fastafile = "";  }
+                       else { m->setFastaFile(fastafile); }
                        
                        namefile = validParameter.validFile(parameters, "name", true);
                        if (namefile == "not open") { abort = true; }
                        else if (namefile == "not found") {  namefile = "";  }  
+                       else { m->setNameFile(namefile); }
                        
                        groupfile = validParameter.validFile(parameters, "group", true);
                        if (groupfile == "not open") { abort = true; }
                        else if (groupfile == "not found") {  groupfile = "";  }        
+                       else { m->setGroupFile(groupfile); }
                        
                        alignfile = validParameter.validFile(parameters, "alignreport", true);
                        if (alignfile == "not open") { abort = true; }
@@ -164,10 +189,12 @@ ListSeqsCommand::ListSeqsCommand(string option)  {
                        listfile = validParameter.validFile(parameters, "list", true);
                        if (listfile == "not open") { abort = true; }
                        else if (listfile == "not found") {  listfile = "";  }
-
+                       else { m->setListFile(listfile); }
+                       
                        taxfile = validParameter.validFile(parameters, "taxonomy", true);
                        if (taxfile == "not open") { abort = true; }
                        else if (taxfile == "not found") {  taxfile = "";  }
+                       else { m->setTaxonomyFile(taxfile); }
                        
                        if ((fastafile == "") && (namefile == "") && (listfile == "") && (groupfile == "") && (alignfile == "") && (taxfile == ""))  { m->mothurOut("You must provide a file."); m->mothurOutEndLine(); abort = true; }
                        
@@ -186,26 +213,10 @@ ListSeqsCommand::ListSeqsCommand(string option)  {
 }
 //**********************************************************************************************************************
 
-void ListSeqsCommand::help(){
-       try {
-               m->mothurOut("The list.seqs command reads a fasta, name, group, list, taxonomy or alignreport file and outputs a .accnos file containing sequence names.\n");
-               m->mothurOut("The list.seqs command parameters are fasta, name, group, list, taxonomy and alignreport.  You must provide one of these parameters.\n");
-               m->mothurOut("The list.seqs command should be in the following format: list.seqs(fasta=yourFasta).\n");
-               m->mothurOut("Example list.seqs(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) {
-               m->errorOut(e, "ListSeqsCommand", "help");
-               exit(1);
-       }
-}
-
-//**********************************************************************************************************************
-
 int ListSeqsCommand::execute(){
        try {
                
-               if (abort == true) { return 0; }
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
                //read functions fill names vector
                if (fastafile != "")            {       inputFileName = fastafile;      readFasta();    }
@@ -222,27 +233,37 @@ int ListSeqsCommand::execute(){
                
                if (outputDir == "") {  outputDir += m->hasPath(inputFileName);  }
                
-               string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + "accnos";
+               string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + getOutputFileNameTag("accnos");
 
                ofstream out;
                m->openOutputFile(outputFileName, out);
+               outputNames.push_back(outputFileName); outputTypes["accnos"].push_back(outputFileName);
                
                //output to .accnos file
                for (int i = 0; i < names.size(); i++) {
                        
-                       if (m->control_pressed) { outputTypes.clear(); out.close(); remove(outputFileName.c_str()); return 0; }
+                       if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(outputFileName); return 0; }
                        
                        out << names[i] << endl;
                }
                out.close();
                
-               if (m->control_pressed) { outputTypes.clear();  remove(outputFileName.c_str()); return 0; }
-
+               if (m->control_pressed) { outputTypes.clear();  m->mothurRemove(outputFileName); return 0; }
+               
+               m->setAccnosFile(outputFileName);
+               
                m->mothurOutEndLine();
                m->mothurOut("Output File Name: "); m->mothurOutEndLine();
-               m->mothurOut(outputFileName); m->mothurOutEndLine();    outputNames.push_back(outputFileName); outputTypes["accnos"].push_back(outputFileName);
+               m->mothurOut(outputFileName); m->mothurOutEndLine();    
                m->mothurOutEndLine();
                
+               //set accnos file as new current accnosfile
+               string current = "";
+               itTypes = outputTypes.find("accnos");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
+               }
+               
                return 0;               
        }
 
@@ -259,19 +280,33 @@ int ListSeqsCommand::readFasta(){
                ifstream in;
                m->openInputFile(fastafile, in);
                string name;
-       
+               
+               //ofstream out;
+               //string newFastaName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "numsAdded.fasta";
+               //m->openOutputFile(newFastaName, out);
+               //int count = 1;
+               //string lastName = "";
+               
                while(!in.eof()){
                        
                        if (m->control_pressed) { in.close(); return 0; }
                        
                        Sequence currSeq(in);
                        name = currSeq.getName();
+                       //if (lastName == "") { lastName = name; }
+                       //if (name != lastName) { count = 1; }
+               //      lastName = name;
+                       
+                       //Sequence newSeq(name+"_"+toString(count), currSeq.getAligned());
+                       //newSeq.printSequence(out);
                        
                        if (name != "") {  names.push_back(name);  }
                        
                        m->gobble(in);
+                       //count++;
                }
                in.close();     
+               //out.close();
                
                return 0;
 
@@ -297,13 +332,7 @@ int ListSeqsCommand::readList(){
                                
                                if (m->control_pressed) { in.close(); return 0; }
                                
-                               while (binnames.find_first_of(',') != -1) { 
-                                       string name = binnames.substr(0,binnames.find_first_of(','));
-                                       binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
-                                       names.push_back(name);
-                               }
-                       
-                               names.push_back(binnames);
+                               m->splitAtComma(binnames, names);
                        }
                }
                in.close();     
@@ -333,14 +362,7 @@ int ListSeqsCommand::readName(){
                        in >> secondCol;                        
                        
                        //parse second column saving each name
-                       while (secondCol.find_first_of(',') != -1) { 
-                               name = secondCol.substr(0,secondCol.find_first_of(','));
-                               secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
-                               names.push_back(name);
-                       }
-                       
-                       //get name after last ,
-                       names.push_back(secondCol);
+                       m->splitAtComma(secondCol, names);
                        
                        m->gobble(in);
                }
@@ -366,7 +388,7 @@ int ListSeqsCommand::readGroup(){
                        
                        if (m->control_pressed) { in.close(); return 0; }
                        
-                       in >> name;                             //read from first column
+                       in >> name;     m->gobble(in);                  //read from first column
                        in >> group;                    //read from second column
                        
                        names.push_back(name);
@@ -397,14 +419,14 @@ int ListSeqsCommand::readAlign(){
                        if (!in.eof())  {       in >> junk;             }
                        else                    {       break;                  }
                }
-               
+               //m->getline(in);
                
                while(!in.eof()){
                
                        if (m->control_pressed) { in.close(); return 0; }
 
                        in >> name;                             //read from first column
-                       
+                       //m->getline(in);
                        //read rest
                        for (int i = 0; i < 15; i++) {  
                                if (!in.eof())  {       in >> junk;             }
@@ -444,6 +466,7 @@ int ListSeqsCommand::readTax(){
                        names.push_back(firstCol);
                        
                        m->gobble(in);
+                       
                }
                in.close();