]> git.donarmstrong.com Git - mothur.git/blobdiff - binsequencecommand.cpp
adding labels to list file.
[mothur.git] / binsequencecommand.cpp
index ad71d1065fb32904b365a7cd2be46a28718a7d98..738c3241732c814d01b87052fa3cafd899c74182 100644 (file)
 //**********************************************************************************************************************
 vector<string> BinSeqCommand::setParameters(){ 
        try {
-               CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(plist);
-               CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
-        CommandParameter pname("name", "InputTypes", "", "", "NameCount", "none", "none",false,false); parameters.push_back(pname);
-        CommandParameter pcount("count", "InputTypes", "", "", "NameCount-CountGroup", "none", "none",false,false); parameters.push_back(pcount);
-               CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "none", "none",false,false); parameters.push_back(pgroup);
-               CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
-               CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
-               CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
+               CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none","",false,true,true); parameters.push_back(plist);
+               CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none","fasta",false,true,true); parameters.push_back(pfasta);
+        CommandParameter pname("name", "InputTypes", "", "", "NameCount", "none", "none","",false,false,true); parameters.push_back(pname);
+        CommandParameter pcount("count", "InputTypes", "", "", "NameCount-CountGroup", "none", "none","",false,false,true); parameters.push_back(pcount);
+               CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "none", "none","",false,false,true); parameters.push_back(pgroup);
+               CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
+               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);          }
@@ -51,24 +51,19 @@ string BinSeqCommand::getHelpString(){
        }
 }
 //**********************************************************************************************************************
-string BinSeqCommand::getOutputFileNameTag(string type, string inputName=""){  
-       try {
-        string outputFileName = "";
-               map<string, vector<string> >::iterator it;
+string BinSeqCommand::getOutputPattern(string type) {
+    try {
+        string pattern = "";
         
-        //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 == "fasta") {  outputFileName =  "fasta"; }
-            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, "BinSeqCommand", "getOutputFileNameTag");
-               exit(1);
-       }
+        if (type == "fasta") {  pattern = "[filename],[distance],fasta"; } //makes file like: amazon.0.03.fasta
+        else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
+        
+        return pattern;
+    }
+    catch(exception& e) {
+        m->errorOut(e, "BinSeqCommand", "getOutputPattern");
+        exit(1);
+    }
 }
 //**********************************************************************************************************************
 BinSeqCommand::BinSeqCommand(){        
@@ -254,7 +249,7 @@ int BinSeqCommand::execute(){
                
                //if user gave a namesfile then use it
                if (namesfile != "") {  readNamesFile();  }
-        if (countfile != "") {  ct.readTable(countfile);  }
+        if (countfile != "") {  ct.readTable(countfile, true, false);  }
                
                input = new InputData(listfile, "list");
                list = input->getListVector();
@@ -333,6 +328,13 @@ int BinSeqCommand::execute(){
                if (groupfile != "") {  delete groupMap;   } 
                
                if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]);                }  return 0; }  
+        
+        //set align file as new current fastafile
+               string currentFasta = "";
+               itTypes = outputTypes.find("fasta");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { currentFasta = (itTypes->second)[0]; m->setFastaFile(currentFasta); }
+               }
                
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
@@ -385,13 +387,18 @@ void BinSeqCommand::readNamesFile() {
 //return 1 if error, 0 otherwise
 int BinSeqCommand::process(ListVector* list) {
        try {
-        string outputFileName = outputDir + m->getRootName(m->getSimpleName(listfile)) + list->getLabel() + "." + getOutputFileNameTag("fasta");
+        map<string, string> variables; 
+        variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(listfile));
+        variables["[distance]"] = list->getLabel();
+        string outputFileName = getOutputFileName("fasta", variables);
+        
         m->openOutputFile(outputFileName, out);
         outputNames.push_back(outputFileName);  outputTypes["fasta"].push_back(outputFileName);
         
         m->mothurOut(list->getLabel()); m->mothurOutEndLine();
         
         //for each bin in the list vector
+        vector<string> binLabels = list->getLabels();
         for (int i = 0; i < list->size(); i++) {
             
             if (m->control_pressed) {  return 1; }
@@ -415,11 +422,11 @@ int BinSeqCommand::process(ListVector* list) {
                             }
                             if (groups.size() != 0) { groupInfo += groups[groups.size()-1]; }
                             else { groupInfo = "not found";  }
-                            name = name + "\t" + groupInfo + "\t" + toString(i+1)+ "\tNumRep=" + toString(ct.getNumSeqs(name));
+                            name = name + "\t" + groupInfo + "\t" + binLabels[i] + "\tNumRep=" + toString(ct.getNumSeqs(name));
                             out << ">" << name << endl;
                             out << sequence << endl;
                         }else {
-                            name = name + "\t" + toString(i+1) + "\tNumRep=" + toString(ct.getNumSeqs(name));
+                            name = name + "\t" + binLabels[i] + "\tNumRep=" + toString(ct.getNumSeqs(name));
                             out << ">" << name << endl;
                             out << sequence << endl;
                         }
@@ -429,7 +436,7 @@ int BinSeqCommand::process(ListVector* list) {
                     if (sequence != "not found") {
                         //if you don't have groups
                         if (groupfile == "") {
-                            name = name + "\t" + toString(i+1);
+                            name = name + "\t" + binLabels[i];
                             out << ">" << name << endl;
                             out << sequence << endl;
                         }else {//if you do have groups
@@ -438,7 +445,7 @@ int BinSeqCommand::process(ListVector* list) {
                                 m->mothurOut(name + " is missing from your group file. Please correct. ");  m->mothurOutEndLine();
                                 return 1;
                             }else{
-                                name = name + "\t" + group + "\t" + toString(i+1);
+                                name = name + "\t" + group + "\t" + binLabels[i];
                                 out << ">" << name << endl;
                                 out << sequence << endl;
                             }