]> git.donarmstrong.com Git - mothur.git/blobdiff - classifyotucommand.cpp
added summary file to classify.otu
[mothur.git] / classifyotucommand.cpp
index 94a60d4db9ff198423a401b74cbb7e78e9c010a5..d3d574e470c352d046c07d52a7f1a30d02d4a902 100644 (file)
@@ -9,8 +9,57 @@
 
 #include "classifyotucommand.h"
 #include "phylotree.h"
+#include "phylosummary.h"
 
-
+//**********************************************************************************************************************
+vector<string> ClassifyOtuCommand::getValidParameters(){       
+       try {
+               string AlignArray[] =  {"list","label","name","taxonomy","basis","cutoff","probs","group","reftaxonomy","outputdir","inputdir"};
+               vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ClassifyOtuCommand", "getValidParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+ClassifyOtuCommand::ClassifyOtuCommand(){      
+       try {
+               abort = true;
+               //initialize outputTypes
+               vector<string> tempOutNames;
+               outputTypes["constaxonomy"] = tempOutNames;
+               outputTypes["taxsummary"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ClassifyOtuCommand", "ClassifyOtuCommand");
+               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(){ 
+       try {
+               vector<string> myArray;
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ClassifyOtuCommand", "getRequiredFiles");
+               exit(1);
+       }
+}
 //**********************************************************************************************************************
 ClassifyOtuCommand::ClassifyOtuCommand(string option)  {
        try{
@@ -23,7 +72,7 @@ ClassifyOtuCommand::ClassifyOtuCommand(string option)  {
                        help(); abort = true;
                } else {
                        //valid paramters for this command
-                       string Array[] =  {"list","label","name","taxonomy","cutoff","probs","outputdir","inputdir"};
+                       string Array[] =  {"list","label","name","taxonomy","cutoff","probs","basis","reftaxonomy","group","outputdir","inputdir"};
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
                        OptionParser parser(option);
@@ -37,6 +86,11 @@ ClassifyOtuCommand::ClassifyOtuCommand(string option)  {
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["constaxonomy"] = tempOutNames;
+                       outputTypes["taxsummary"] = tempOutNames;
+               
                        //if the user changes the input directory command factory will send this info to us in the output parameter 
                        string inputDir = validParameter.validFile(parameters, "inputdir", false);              
                        if (inputDir == "not found"){   inputDir = "";          }
@@ -45,7 +99,7 @@ ClassifyOtuCommand::ClassifyOtuCommand(string option)  {
                                it = parameters.find("list");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       path = m->hasPath(it->second);
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["list"] = inputDir + it->second;             }
                                }
@@ -53,7 +107,7 @@ ClassifyOtuCommand::ClassifyOtuCommand(string option)  {
                                it = parameters.find("name");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       path = m->hasPath(it->second);
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["name"] = inputDir + it->second;             }
                                }
@@ -61,10 +115,26 @@ ClassifyOtuCommand::ClassifyOtuCommand(string option)  {
                                it = parameters.find("taxonomy");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       path = m->hasPath(it->second);
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["taxonomy"] = inputDir + it->second;         }
                                }
+                               
+                               it = parameters.find("reftaxonomy");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = m->hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["reftaxonomy"] = inputDir + it->second;              }
+                               }
+                               
+                               it = parameters.find("group");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = m->hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["group"] = inputDir + it->second;            }
+                               }
                        }
 
                        
@@ -78,26 +148,39 @@ ClassifyOtuCommand::ClassifyOtuCommand(string option)  {
                        
                        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; }
-                       else if (taxfile == "not open") { abort = true; }       
+                       else if (taxfile == "not open") { abort = true; }
+                       
+                       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(); }
+                       else if (refTaxonomy == "not open") { abort = true; }
        
                        namefile = validParameter.validFile(parameters, "name", true);
                        if (namefile == "not open") { abort = true; }   
                        else if (namefile == "not found") { namefile = ""; }
                        
+                       groupfile = validParameter.validFile(parameters, "group", true);
+                       if (groupfile == "not open") { abort = true; }  
+                       else if (groupfile == "not found") { groupfile = ""; }
+                       
                        //check for optional parameter and set defaults
                        // ...at some point should added some additional type checking...
                        label = validParameter.validFile(parameters, "label", false);                   
                        if (label == "not found") { label = ""; allLines = 1;  }
                        else { 
-                               if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
+                               if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
                                else { allLines = 1;  }
                        }
                        
+                       basis = validParameter.validFile(parameters, "basis", false);
+                       if (basis == "not found") { basis = "otu"; }    
+                       
+                       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); 
                        
                        temp = validParameter.validFile(parameters, "probs", false);                                    if (temp == "not found"){       temp = "true";                  }
-                       probs = isTrue(temp);
+                       probs = m->isTrue(temp);
                        
                        
                        if ((cutoff < 51) || (cutoff > 100)) { m->mothurOut("cutoff must be above 50, and no greater than 100."); m->mothurOutEndLine(); abort = true;  }
@@ -114,8 +197,15 @@ ClassifyOtuCommand::ClassifyOtuCommand(string option)  {
 
 void ClassifyOtuCommand::help(){
        try {
-               m->mothurOut("The classify.otu command parameters are list, taxonomy, name, cutoff, and label.  The taxonomy and list parameters are required.\n");
+               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");
@@ -157,7 +247,7 @@ int ClassifyOtuCommand::execute(){
                set<string> processedLabels;
                set<string> userLabels = labels;
                
-               if (m->control_pressed) { 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++) {        remove(outputNames[i].c_str());  }  return 0; }
        
                while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
                        
@@ -165,13 +255,13 @@ int ClassifyOtuCommand::execute(){
                        
                                        m->mothurOut(list->getLabel() + "\t" + toString(list->size())); m->mothurOutEndLine();
                                        process(list);
-                                       if (m->control_pressed) { 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++) {   remove(outputNames[i].c_str());  } delete input; delete list; return 0; }
                                                                                
                                        processedLabels.insert(list->getLabel());
                                        userLabels.erase(list->getLabel());
                        }
                        
-                       if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
+                       if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
                                        string saveLabel = list->getLabel();
                                        
                                        delete list;
@@ -180,7 +270,7 @@ int ClassifyOtuCommand::execute(){
                                        process(list);
                                
                                        
-                                       if (m->control_pressed) { 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++) {  remove(outputNames[i].c_str());  } delete input; delete list; return 0; }
                                                                                
                                        processedLabels.insert(list->getLabel());
                                        userLabels.erase(list->getLabel());
@@ -216,12 +306,12 @@ int ClassifyOtuCommand::execute(){
                        process(list);
                        delete list;
                        
-                       if (m->control_pressed) { 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++) {  remove(outputNames[i].c_str());  } delete input; delete list; return 0; }
                }
                
                delete input;  
                                
-               if (m->control_pressed) {  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++) {   remove(outputNames[i].c_str());  } return 0; }
                
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
@@ -241,16 +331,20 @@ int ClassifyOtuCommand::readNamesFile() {
        try {
                
                ifstream inNames;
-               openInputFile(namefile, inNames);
+               m->openInputFile(namefile, inNames);
                
                string name, names;
        
-               while(inNames){
+               while(!inNames.eof()){
                        inNames >> name;                        //read from first column  A
                        inNames >> names;               //read from second column  A,B,C,D
-                       gobble(inNames);
+                       m->gobble(inNames);
                        
-                       nameMap[name] = names;
+                       //parse names into vector
+                       vector<string> theseNames;
+                       m->splitAtComma(names, theseNames);
+
+                       for (int i = 0; i < theseNames.size(); i++) {  nameMap[theseNames[i]] = name;  }
                        
                        if (m->control_pressed) { inNames.close(); nameMap.clear(); return 0; }
                }
@@ -268,13 +362,13 @@ int ClassifyOtuCommand::readTaxonomyFile() {
        try {
                
                ifstream in;
-               openInputFile(taxfile, in);
+               m->openInputFile(taxfile, in);
                
                string name, tax;
        
                while(!in.eof()){
                        in >> name >> tax;              
-                       gobble(in);
+                       m->gobble(in);
                        
                        //are there confidence scores, if so remove them
                        if (tax.find_first_of('(') != -1) {  removeConfidences(tax);    }
@@ -293,57 +387,67 @@ int ClassifyOtuCommand::readTaxonomyFile() {
        }
 }
 //**********************************************************************************************************************
-string ClassifyOtuCommand::findConsensusTaxonomy(int bin, ListVector* thisList, int& size) {
+vector<string> ClassifyOtuCommand::findConsensusTaxonomy(int bin, ListVector* thisList, int& size, string& conTax) {
        try{
-               string conTax = "";
+               conTax = "";
                vector<string> names;
+               vector<string> allNames;
                map<string, string>::iterator it;
                map<string, string>::iterator it2;
 
                //parse names into vector
                string binnames = thisList->get(bin);
-               splitAtComma(binnames, names);
+               m->splitAtComma(binnames, names);
 
                //create a tree containing sequences from this bin
                PhyloTree* phylo = new PhyloTree();
                
                size = 0;
                for (int i = 0; i < names.size(); i++) {
-                       
-                       if (m->control_pressed) { delete phylo; return conTax; }
-                       
-                       //is this sequence in the taxonomy file
-                       it = taxMap.find(names[i]);
-                       
-                       if (it == taxMap.end()) { //this name is not in taxonomy file, skip it
-                               m->mothurOut(names[i] + " is not in your taxonomy file.  I will not include it in the consensus."); m->mothurOutEndLine();
-                       }else{
+       
+                       //if namesfile include the names
+                       if (namefile != "") {
+       
+                               //is this sequence in the name file - namemap maps seqName -> repSeqName
+                               it2 = nameMap.find(names[i]);
                                
-                               //if namesfile include the names
-                               if (namefile != "") {
-                                       //is this sequence in the name file
-                                       it2 = nameMap.find(names[i]);
+                               if (it2 == nameMap.end()) { //this name is not in name file, skip it
+                                       m->mothurOut(names[i] + " is not in your name file.  I will not include it in the consensus."); m->mothurOutEndLine();
+                               }else{
                                        
-                                       if (it2 == nameMap.end()) { //this name is not in name file, skip it
-                                               m->mothurOut(names[i] + " is not in your name file.  I will not include it in the consensus."); m->mothurOutEndLine();
+                                       //is this sequence in the taxonomy file - look for repSeqName since we are assuming the taxonomy file is unique
+                                       it = taxMap.find(it2->second);
+                       
+                                       if (it == taxMap.end()) { //this name is not in taxonomy file, skip it
+                                       
+                                               if (names[i] != it2->second) { m->mothurOut(names[i] + " is represented by " +  it2->second + " and is not in your taxonomy file.  I will not include it in the consensus."); m->mothurOutEndLine(); }
+                                               else {  m->mothurOut(names[i] + " is not in your taxonomy file.  I will not include it in the consensus."); m->mothurOutEndLine(); }
                                        }else{
-                                               
-                                               vector<string> nameFileNames;
-                                               splitAtComma(it2->second, nameFileNames);
-                                               
-                                               for (int j = 0; j < nameFileNames.size(); j++) {
-                                                       //add seq to tree
-                                                       phylo->addSeqToTree(nameFileNames[j], it->second);
-                                                       size++;
-                                               }
+                               
+                                               //add seq to tree
+                                               phylo->addSeqToTree(names[i], it->second);
+                                               size++;
+                                               allNames.push_back(names[i]);
                                        }
-                                       
+                               }
+                               
+                       }else{
+                               //is this sequence in the taxonomy file - look for repSeqName since we are assuming the taxonomy file is unique
+                               it = taxMap.find(names[i]);
+               
+                               if (it == taxMap.end()) { //this name is not in taxonomy file, skip it
+                                       m->mothurOut(names[i] + " is not in your taxonomy file.  I will not include it in the consensus."); m->mothurOutEndLine();
                                }else{
                                        //add seq to tree
                                        phylo->addSeqToTree(names[i], it->second);
                                        size++;
+                                       allNames.push_back(names[i]);
                                }
                        }
+
+                       
+                       if (m->control_pressed) { delete phylo; return allNames; }
+                       
                }
                
                //build tree
@@ -388,11 +492,11 @@ string ClassifyOtuCommand::findConsensusTaxonomy(int bin, ListVector* thisList,
                }
                
                                
-               if (conTax == "") {  conTax = "unclassified;";  }
+               if (conTax == "") {  conTax = "no_consensus;";  }
                
                delete phylo;   
                
-               return conTax;
+               return allNames;
                        
        }
        catch(exception& e) {
@@ -408,25 +512,59 @@ int ClassifyOtuCommand::process(ListVector* processList) {
                int size;
                
                //create output file
-               if (outputDir == "") { outputDir += hasPath(listfile); }
+               if (outputDir == "") { outputDir += m->hasPath(listfile); }
                                
                ofstream out;
-               string outputFile = outputDir + getRootName(getSimpleName(listfile)) + processList->getLabel() + ".cons.taxonomy";
-               openOutputFile(outputFile, out);
-               outputNames.push_back(outputFile);
+               string outputFile = outputDir + m->getRootName(m->getSimpleName(listfile)) + processList->getLabel() + ".cons.taxonomy";
+               m->openOutputFile(outputFile, out);
+               outputNames.push_back(outputFile); outputTypes["constaxonomy"].push_back(outputFile);
+               
+               ofstream outSum;
+               string outputSumFile = outputDir + m->getRootName(m->getSimpleName(listfile)) + processList->getLabel() + ".cons.tax.summary";
+               m->openOutputFile(outputSumFile, outSum);
+               outputNames.push_back(outputSumFile); outputTypes["taxsummary"].push_back(outputSumFile);
+               
+               out << "OTU\tSize\tTaxonomy" << endl;
+               
+               PhyloSummary* taxaSum;
+               if (refTaxonomy != "") {
+                       taxaSum = new PhyloSummary(refTaxonomy, groupfile);
+               }else {
+                       taxaSum = new PhyloSummary(groupfile);
+               }
                
                //for each bin in the list vector
                for (int i = 0; i < processList->getNumBins(); i++) {
-                       conTax  = findConsensusTaxonomy(i, processList, size);
                        
+                       if (m->control_pressed) { break; }
+                       
+                       vector<string> names;
+                       names = findConsensusTaxonomy(i, processList, size, conTax);
+               
                        if (m->control_pressed) { out.close();  return 0; }
                        
                        //output to new names file
                        out << (i+1) << '\t' << size << '\t' << conTax << endl;
+                       
+                       string noConfidenceConTax = conTax;
+                       removeConfidences(noConfidenceConTax);
+                       
+                       //add this bins taxonomy to summary
+                       if (basis == "sequence") {
+                               for(int j = 0; j < names.size(); j++) {  taxaSum->addSeqToTree(names[j], noConfidenceConTax);  }
+                       }else { //otu
+                               taxaSum->addSeqToTree(noConfidenceConTax, names);
+                       }
                }
 
                out.close();
                
+               //print summary file
+               taxaSum->print(outSum);
+               outSum.close();
+               
+               delete taxaSum;
+               
                return 0;
 
        }