]> git.donarmstrong.com Git - mothur.git/blobdiff - classifyotucommand.cpp
1.23.0
[mothur.git] / classifyotucommand.cpp
index d3d574e470c352d046c07d52a7f1a30d02d4a902..e28961b74888ee5bdefb9eb022fd11afe328e07f 100644 (file)
 #include "phylosummary.h"
 
 //**********************************************************************************************************************
-vector<string> ClassifyOtuCommand::getValidParameters(){       
+vector<string> ClassifyOtuCommand::setParameters(){    
        try {
-               string AlignArray[] =  {"list","label","name","taxonomy","basis","cutoff","probs","group","reftaxonomy","outputdir","inputdir"};
-               vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
+               CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(plist);
+               CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(ptaxonomy);
+               CommandParameter preftaxonomy("reftaxonomy", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(preftaxonomy);
+               CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
+               CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
+               CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
+               CommandParameter pbasis("basis", "Multiple", "otu-sequence", "otu", "", "", "",false,false); parameters.push_back(pbasis);
+               CommandParameter pcutoff("cutoff", "Number", "", "51", "", "", "",false,true); parameters.push_back(pcutoff);
+               CommandParameter pprobs("probs", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pprobs);
+               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, "ClassifyOtuCommand", "getValidParameters");
+               m->errorOut(e, "ClassifyOtuCommand", "setParameters");
                exit(1);
        }
 }
 //**********************************************************************************************************************
-ClassifyOtuCommand::ClassifyOtuCommand(){      
+string ClassifyOtuCommand::getHelpString(){    
        try {
-               abort = true;
-               //initialize outputTypes
-               vector<string> tempOutNames;
-               outputTypes["constaxonomy"] = tempOutNames;
-               outputTypes["taxsummary"] = tempOutNames;
+               string helpString = "";
+               helpString += "The classify.otu command parameters are list, taxonomy, reftaxonomy, name, group, cutoff, label, basis and probs.  The taxonomy and list parameters are required unless you have a valid current file.\n";
+               helpString += "The reftaxonomy parameter allows you give the name of the reference taxonomy file used when you classified your sequences. Providing it will keep the rankIDs in the summary file static.\n";
+               helpString += "The name parameter allows you add a names file with your taxonomy file.\n";
+               helpString += "The group parameter allows you provide a group file to use in creating the summary file breakdown.\n";
+               helpString += "The basis parameter allows you indicate what you want the summary file to represent, options are otu and sequence. Default is otu.\n";
+               helpString += "For example consider the following basis=sequence could give Clostridiales       3       105     16      43      46, where 105 is the total number of sequences whose otu classified to Clostridiales.\n";
+               helpString += "16 is the number of sequences in the otus from groupA, 43 is the number of sequences in the otus from groupB, and 46 is the number of sequences in the otus from groupC.\n";
+               helpString += "Now for basis=otu could give Clostridiales       3       7       6       1       2, where 7 is the number of otus that classified to Clostridiales.\n";
+               helpString += "6 is the number of otus containing sequences from groupA, 1 is the number of otus containing sequences from groupB, and 2 is the number of otus containing sequences from groupC.\n";
+               helpString += "The label parameter allows you to select what distance levels you would like a output files created for, and is separated by dashes.\n";
+               helpString += "The default value for label is all labels in your inputfile.\n";
+               helpString += "The cutoff parameter allows you to specify a consensus confidence threshold for your taxonomy.  The default is 51, meaning 51%. Cutoff cannot be below 51.\n";
+               helpString += "The probs parameter shuts off the outputting of the consensus confidence results. The default is true, meaning you want the confidence to be shown.\n";
+               helpString += "The classify.otu command should be in the following format: classify.otu(taxonomy=yourTaxonomyFile, list=yourListFile, name=yourNamesFile, label=yourLabels).\n";
+               helpString += "Example classify.otu(taxonomy=abrecovery.silva.full.taxonomy, list=abrecovery.fn.list, label=0.10).\n";
+               helpString += "Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n";
+               return helpString;
        }
        catch(exception& e) {
-               m->errorOut(e, "ClassifyOtuCommand", "ClassifyOtuCommand");
+               m->errorOut(e, "ClassifyOtuCommand", "getHelpString");
                exit(1);
        }
 }
 //**********************************************************************************************************************
-vector<string> ClassifyOtuCommand::getRequiredParameters(){    
-       try {
-               string Array[] =  {"list","taxonomy"};
-               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
-               return myArray;
-       }
-       catch(exception& e) {
-               m->errorOut(e, "ClassifyOtuCommand", "getRequiredParameters");
-               exit(1);
-       }
-}
-//**********************************************************************************************************************
-vector<string> ClassifyOtuCommand::getRequiredFiles(){ 
+ClassifyOtuCommand::ClassifyOtuCommand(){      
        try {
-               vector<string> myArray;
-               return myArray;
+               abort = true; calledHelp = true; 
+               setParameters();
+               vector<string> tempOutNames;
+               outputTypes["constaxonomy"] = tempOutNames;
+               outputTypes["taxsummary"] = tempOutNames;
        }
        catch(exception& e) {
-               m->errorOut(e, "ClassifyOtuCommand", "getRequiredFiles");
+               m->errorOut(e, "ClassifyOtuCommand", "ClassifyOtuCommand");
                exit(1);
        }
 }
+
 //**********************************************************************************************************************
 ClassifyOtuCommand::ClassifyOtuCommand(string option)  {
        try{
-               abort = false;
+               abort = false; calledHelp = false;   
                allLines = 1;
                labels.clear();
                                
                //allow user to run help
                if (option == "help") { 
-                       help(); abort = true;
-               } else {
-                       //valid paramters for this command
-                       string Array[] =  {"list","label","name","taxonomy","cutoff","probs","basis","reftaxonomy","group","outputdir","inputdir"};
-                       vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+                       help(); abort = true; calledHelp = true;
+               }else if(option == "citation") { citation(); abort = true; calledHelp = true;} 
+               else {
+                       vector<string> myArray = setParameters();
                        
                        OptionParser parser(option);
                        map<string, string> parameters = parser.getParameters();
@@ -143,12 +159,23 @@ ClassifyOtuCommand::ClassifyOtuCommand(string option)  {
                        
                        //check for required parameters
                        listfile = validParameter.validFile(parameters, "list", true);
-                       if (listfile == "not found") { m->mothurOut("list is a required parameter for the classify.otu command."); m->mothurOutEndLine(); abort = true; }
+                       if (listfile == "not found") {                          
+                               //if there is a current list file, use it
+                               listfile = m->getListFile(); 
+                               if (listfile != "") {  m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
+                               else {  m->mothurOut("You have no current listfile and the list parameter is required."); m->mothurOutEndLine(); abort = true; }
+                       }
                        else if (listfile == "not open") { abort = true; }      
+                       else { m->setListFile(listfile); }
                        
                        taxfile = validParameter.validFile(parameters, "taxonomy", true);
-                       if (taxfile == "not found") {  m->mothurOut("taxonomy is a required parameter for the classify.otu command."); m->mothurOutEndLine(); abort = true; }
+                       if (taxfile == "not found") {  //if there is a current list file, use it
+                               taxfile = m->getTaxonomyFile(); 
+                               if (taxfile != "") {  m->mothurOut("Using " + taxfile + " as input file for the taxonomy parameter."); m->mothurOutEndLine(); }
+                               else {  m->mothurOut("You have no current taxonomy file and the taxonomy parameter is required."); m->mothurOutEndLine(); abort = true; }
+                       }
                        else if (taxfile == "not open") { abort = true; }
+                       else { m->setTaxonomyFile(taxfile); }
                        
                        refTaxonomy = validParameter.validFile(parameters, "reftaxonomy", true);
                        if (refTaxonomy == "not found") { refTaxonomy = ""; m->mothurOut("reftaxonomy is not required, but if given will keep the rankIDs in the summary file static."); m->mothurOutEndLine(); }
@@ -157,10 +184,12 @@ ClassifyOtuCommand::ClassifyOtuCommand(string option)  {
                        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); }
                        
                        //check for optional parameter and set defaults
                        // ...at some point should added some additional type checking...
@@ -177,7 +206,7 @@ ClassifyOtuCommand::ClassifyOtuCommand(string option)  {
                        if ((basis != "otu") && (basis != "sequence")) { m->mothurOut("Invalid option for basis. basis options are otu and sequence, using otu."); m->mothurOutEndLine(); }
                        
                        string temp = validParameter.validFile(parameters, "cutoff", false);                    if (temp == "not found") { temp = "51"; }
-                       convert(temp, cutoff); 
+                       m->mothurConvert(temp, cutoff); 
                        
                        temp = validParameter.validFile(parameters, "probs", false);                                    if (temp == "not found"){       temp = "true";                  }
                        probs = m->isTrue(temp);
@@ -192,44 +221,12 @@ ClassifyOtuCommand::ClassifyOtuCommand(string option)  {
                exit(1);
        }
 }
-
-//**********************************************************************************************************************
-
-void ClassifyOtuCommand::help(){
-       try {
-               m->mothurOut("The classify.otu command parameters are list, taxonomy, reftaxonomy, name, group, cutoff, label, basis and probs.  The taxonomy and list parameters are required.\n");
-               m->mothurOut("The reftaxonomy parameter allows you give the name of the reference taxonomy file used when you classified your sequences. Providing it will keep the rankIDs in the summary file static.\n");
-               m->mothurOut("The name parameter allows you add a names file with your taxonomy file.\n");
-               m->mothurOut("The group parameter allows you provide a group file to use in creating the summary file breakdown.\n");
-               m->mothurOut("The basis parameter allows you indicate what you want the summary file to represent, options are otu and sequence. Default is otu.\n");
-               m->mothurOut("For example consider the following basis=sequence could give Clostridiales        3       105     16      43      46, where 105 is the total number of sequences whose otu classified to Clostridiales.\n");
-               m->mothurOut("16 is the number of sequences in the otus from groupA, 43 is the number of sequences in the otus from groupB, and 46 is the number of sequences in the otus from groupC.\n");
-               m->mothurOut("Now for basis=otu could give Clostridiales        3       7       6       1       2, where 7 is the number of otus that classified to Clostridiales.\n");
-               m->mothurOut("6 is the number of otus containing sequences from groupA, 1 is the number of otus containing sequences from groupB, and 2 is the number of otus containing sequences from groupC.\n");
-               m->mothurOut("The label parameter allows you to select what distance levels you would like a output files created for, and is separated by dashes.\n");
-               m->mothurOut("The default value for label is all labels in your inputfile.\n");
-               m->mothurOut("The cutoff parameter allows you to specify a consensus confidence threshold for your taxonomy.  The default is 51, meaning 51%. Cutoff cannot be below 51.\n");
-               m->mothurOut("The probs parameter shuts off the outputting of the consensus confidence results. The default is true, meaning you want the confidence to be shown.\n");
-               m->mothurOut("The classify.otu command should be in the following format: classify.otu(taxonomy=yourTaxonomyFile, list=yourListFile, name=yourNamesFile, label=yourLabels).\n");
-               m->mothurOut("Example classify.otu(taxonomy=abrecovery.silva.full.taxonomy, list=abrecovery.fn.list, label=0.10).\n");
-               m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
-       }
-       catch(exception& e) {
-               m->errorOut(e, "ClassifyOtuCommand", "help");
-               exit(1);
-       }
-}
-
-//**********************************************************************************************************************
-
-ClassifyOtuCommand::~ClassifyOtuCommand(){}
-
 //**********************************************************************************************************************
 
 int ClassifyOtuCommand::execute(){
        try {
        
-               if (abort == true) { return 0; }
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
                //if user gave a namesfile then use it
                if (namefile != "") {   readNamesFile();        }
@@ -247,7 +244,7 @@ int ClassifyOtuCommand::execute(){
                set<string> processedLabels;
                set<string> userLabels = labels;
                
-               if (m->control_pressed) { outputTypes.clear(); delete input; delete list; for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  }  return 0; }
+               if (m->control_pressed) { outputTypes.clear(); delete input; delete list; for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);  }  return 0; }
        
                while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
                        
@@ -255,7 +252,7 @@ int ClassifyOtuCommand::execute(){
                        
                                        m->mothurOut(list->getLabel() + "\t" + toString(list->size())); m->mothurOutEndLine();
                                        process(list);
-                                       if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) {   remove(outputNames[i].c_str());  } delete input; delete list; return 0; }
+                                       if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) {   m->mothurRemove(outputNames[i]);  } delete input; delete list; return 0; }
                                                                                
                                        processedLabels.insert(list->getLabel());
                                        userLabels.erase(list->getLabel());
@@ -270,7 +267,7 @@ int ClassifyOtuCommand::execute(){
                                        process(list);
                                
                                        
-                                       if (m->control_pressed) { outputTypes.clear();  for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  } delete input; delete list; return 0; }
+                                       if (m->control_pressed) { outputTypes.clear();  for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  } delete input; delete list; return 0; }
                                                                                
                                        processedLabels.insert(list->getLabel());
                                        userLabels.erase(list->getLabel());
@@ -306,12 +303,12 @@ int ClassifyOtuCommand::execute(){
                        process(list);
                        delete list;
                        
-                       if (m->control_pressed) { outputTypes.clear();  for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  } delete input; delete list; return 0; }
+                       if (m->control_pressed) { outputTypes.clear();  for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  } delete input; delete list; return 0; }
                }
                
                delete input;  
                                
-               if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) {   remove(outputNames[i].c_str());  } return 0; }
+               if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) {   m->mothurRemove(outputNames[i]);  } return 0; }
                
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
@@ -371,7 +368,7 @@ int ClassifyOtuCommand::readTaxonomyFile() {
                        m->gobble(in);
                        
                        //are there confidence scores, if so remove them
-                       if (tax.find_first_of('(') != -1) {  removeConfidences(tax);    }
+                       if (tax.find_first_of('(') != -1) {  m->removeConfidences(tax); }
                        
                        taxMap[name] = tax;
                        
@@ -454,7 +451,7 @@ vector<string> ClassifyOtuCommand::findConsensusTaxonomy(int bin, ListVector* th
                phylo->assignHeirarchyIDs(0);
                
                TaxNode currentNode = phylo->get(0);
-               
+               int myLevel = 0;        
                //at each level
                while (currentNode.children.size() != 0) { //you still have more to explore
                
@@ -483,6 +480,7 @@ vector<string> ClassifyOtuCommand::findConsensusTaxonomy(int bin, ListVector* th
                                }else{
                                        conTax += bestChild.name + ";";
                                }
+                               myLevel++;
                        }else{ //if no, quit
                                break;
                        }
@@ -491,7 +489,12 @@ vector<string> ClassifyOtuCommand::findConsensusTaxonomy(int bin, ListVector* th
                        currentNode = bestChild;
                }
                
-                               
+               if (myLevel != phylo->getMaxLevel()) {
+                       while (myLevel != phylo->getMaxLevel()) {
+                               conTax += "unclassified;";
+                               myLevel++;
+                       }
+               }               
                if (conTax == "") {  conTax = "no_consensus;";  }
                
                delete phylo;   
@@ -547,7 +550,7 @@ int ClassifyOtuCommand::process(ListVector* processList) {
                        out << (i+1) << '\t' << size << '\t' << conTax << endl;
                        
                        string noConfidenceConTax = conTax;
-                       removeConfidences(noConfidenceConTax);
+                       m->removeConfidences(noConfidenceConTax);
                        
                        //add this bins taxonomy to summary
                        if (basis == "sequence") {
@@ -574,31 +577,30 @@ int ClassifyOtuCommand::process(ListVector* processList) {
        }
 }
 /**************************************************************************************************/
-void ClassifyOtuCommand::removeConfidences(string& tax) {
-       try {
-               
-               string taxon;
-               string newTax = "";
+string ClassifyOtuCommand::addUnclassifieds(string tax, int maxlevel) {
+       try{
+               string newTax, taxon;
+               int level = 0;
                
+               //keep what you have counting the levels
                while (tax.find_first_of(';') != -1) {
                        //get taxon
-                       taxon = tax.substr(0,tax.find_first_of(';'));
-                       
-                       int pos = taxon.find_first_of('(');
-                       if (pos != -1) {
-                               taxon = taxon.substr(0, pos); //rip off confidence 
-                       }
-                       
-                       taxon += ";";
-                       
+                       taxon = tax.substr(0,tax.find_first_of(';'))+';';
                        tax = tax.substr(tax.find_first_of(';')+1, tax.length());
                        newTax += taxon;
+                       level++;
+               }
+               
+               //add "unclassified" until you reach maxLevel
+               while (level < maxlevel) {
+                       newTax += "unclassified;";
+                       level++;
                }
                
-               tax = newTax;
+               return newTax;
        }
        catch(exception& e) {
-               m->errorOut(e, "ClassifyOtuCommand", "removeConfidences");
+               m->errorOut(e, "ClassifyOtuCommand", "addUnclassifieds");
                exit(1);
        }
 }