]> git.donarmstrong.com Git - mothur.git/commitdiff
added summary file to classify.otu
authorwestcott <westcott>
Mon, 13 Dec 2010 20:33:34 +0000 (20:33 +0000)
committerwestcott <westcott>
Mon, 13 Dec 2010 20:33:34 +0000 (20:33 +0000)
classifyotucommand.cpp
classifyotucommand.h
mothur
mothur.h
phylosummary.cpp
phylosummary.h

index 3469c82c0c1507604e87ea05efc30ca74927e09f..d3d574e470c352d046c07d52a7f1a30d02d4a902 100644 (file)
@@ -9,11 +9,12 @@
 
 #include "classifyotucommand.h"
 #include "phylotree.h"
+#include "phylosummary.h"
 
 //**********************************************************************************************************************
 vector<string> ClassifyOtuCommand::getValidParameters(){       
        try {
-               string AlignArray[] =  {"list","label","name","taxonomy","cutoff","probs","outputdir","inputdir"};
+               string AlignArray[] =  {"list","label","name","taxonomy","basis","cutoff","probs","group","reftaxonomy","outputdir","inputdir"};
                vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
                return myArray;
        }
@@ -29,6 +30,7 @@ ClassifyOtuCommand::ClassifyOtuCommand(){
                //initialize outputTypes
                vector<string> tempOutNames;
                outputTypes["constaxonomy"] = tempOutNames;
+               outputTypes["taxsummary"] = tempOutNames;
        }
        catch(exception& e) {
                m->errorOut(e, "ClassifyOtuCommand", "ClassifyOtuCommand");
@@ -70,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);
@@ -87,6 +89,7 @@ ClassifyOtuCommand::ClassifyOtuCommand(string option)  {
                        //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);              
@@ -116,6 +119,22 @@ ClassifyOtuCommand::ClassifyOtuCommand(string option)  {
                                        //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;            }
+                               }
                        }
 
                        
@@ -129,12 +148,20 @@ 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);                   
@@ -144,6 +171,11 @@ ClassifyOtuCommand::ClassifyOtuCommand(string option)  {
                                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); 
                        
@@ -165,8 +197,15 @@ ClassifyOtuCommand::ClassifyOtuCommand(string option)  {
 
 void ClassifyOtuCommand::help(){
        try {
-               m->mothurOut("The classify.otu command parameters are list, taxonomy, name, cutoff, label and probs.  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");
@@ -348,10 +387,11 @@ 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;
 
@@ -387,6 +427,7 @@ string ClassifyOtuCommand::findConsensusTaxonomy(int bin, ListVector* thisList,
                                                //add seq to tree
                                                phylo->addSeqToTree(names[i], it->second);
                                                size++;
+                                               allNames.push_back(names[i]);
                                        }
                                }
                                
@@ -400,11 +441,12 @@ string ClassifyOtuCommand::findConsensusTaxonomy(int bin, ListVector* thisList,
                                        //add seq to tree
                                        phylo->addSeqToTree(names[i], it->second);
                                        size++;
+                                       allNames.push_back(names[i]);
                                }
                        }
 
                        
-                       if (m->control_pressed) { delete phylo; return conTax; }
+                       if (m->control_pressed) { delete phylo; return allNames; }
                        
                }
                
@@ -454,7 +496,7 @@ string ClassifyOtuCommand::findConsensusTaxonomy(int bin, ListVector* thisList,
                
                delete phylo;   
                
-               return conTax;
+               return allNames;
                        
        }
        catch(exception& e) {
@@ -477,21 +519,52 @@ int ClassifyOtuCommand::process(ListVector* processList) {
                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;
 
        }
index 60bfb29a900dbabedcb49a23523b4b704e98352e..e81c246201f36b7904d26b702748e1ba653dff66 100644 (file)
@@ -32,7 +32,7 @@ private:
 
        ListVector* list;
        InputData* input;
-       string listfile, namefile, taxfile, label, outputDir;
+       string listfile, namefile, taxfile, label, outputDir, refTaxonomy, groupfile, basis;
        bool abort, allLines, probs;
        int cutoff;
        set<string> labels; //holds labels to be used
@@ -45,7 +45,7 @@ private:
        int readTaxonomyFile();
        void removeConfidences(string&);
        int process(ListVector*);
-       string findConsensusTaxonomy(int, ListVector*, int&);   // returns the name of the "representative" taxonomy of given bin
+       vector<string> findConsensusTaxonomy(int, ListVector*, int&, string&);  // returns the name of the "representative" taxonomy of given bin
        
                                                                                                
 };
diff --git a/mothur b/mothur
index 0812c5e78d5e1ecd1dc75495548ba531ee812bc7..750a30cdb2a9f0b606751ae5bea573a2d518ff92 100755 (executable)
Binary files a/mothur and b/mothur differ
index 2e2299ec2ad6fa1938a2f49e1f8601993b232cbd..b0ad51e6736159e738d83d08a418f6cdecaac767 100644 (file)
--- a/mothur.h
+++ b/mothur.h
@@ -36,6 +36,7 @@
 #include <map>
 #include <string>
 #include <list>
+#include <string.h>
 
 //math
 #include <cmath>
index 4e94605e47caf6e09d099a4df32dfdd7677be676..a9a170b86c77dff145079df269fd6b36813848ee 100644 (file)
@@ -15,6 +15,7 @@ PhyloSummary::PhyloSummary(string refTfile, string groupFile){
        try {
                m = MothurOut::getInstance();
                maxLevel = 0;
+               ignore = false;
                
                if (groupFile != "") {
                        groupmap = new GroupMap(groupFile);
@@ -42,6 +43,32 @@ PhyloSummary::PhyloSummary(string refTfile, string groupFile){
                exit(1);
        }
 }
+
+/**************************************************************************************************/
+
+PhyloSummary::PhyloSummary(string groupFile){
+       try {
+               m = MothurOut::getInstance();
+               maxLevel = 0;
+               ignore = true;
+               
+               if (groupFile != "") {
+                       groupmap = new GroupMap(groupFile);
+                       groupmap->readMap();
+               }else{
+                       groupmap = NULL;
+               }
+               
+               tree.push_back(rawTaxNode("Root"));
+               tree[0].rank = "0";
+               
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "PhyloSummary", "PhyloSummary");
+               exit(1);
+       }
+}
 /**************************************************************************************************/
 
 void PhyloSummary::summarize(string userTfile){
@@ -128,9 +155,43 @@ int PhyloSummary::addSeqToTree(string seqName, string seqTaxonomy){
                                tree[childPointer->second].total++;
 
                                currentNode = childPointer->second;
-                       }else{  //otherwise, error
-                               m->mothurOut("Warning: cannot find taxon " + taxon + " in reference taxonomy tree at level " + toString(tree[currentNode].level) + " for " + seqName + ". This may cause totals of daughter levels not to add up in summary file."); m->mothurOutEndLine();
-                               break;
+                       }else{  
+                               if (ignore) {
+                                               
+                                       tree.push_back(rawTaxNode(taxon));
+                                       int index = tree.size() - 1;
+                               
+                                       tree[index].parent = currentNode;
+                                       tree[index].level = (level+1);
+                                       tree[index].total = 1;
+                                       tree[currentNode].children[taxon] = index;
+                                       
+                                       //initialize groupcounts
+                                       if (groupmap != NULL) {
+                                               for (int j = 0; j < groupmap->namesOfGroups.size(); j++) {
+                                                       tree[index].groupCount[groupmap->namesOfGroups[j]] = 0;
+                                               }
+                                               
+                                               //find out the sequences group
+                                               string group = groupmap->getGroup(seqName);
+                                               
+                                               if (group == "not found") {  m->mothurOut(seqName + " is not in your groupfile, and will be included in the overall total, but not any group total."); m->mothurOutEndLine();  }
+                                               
+                                               //do you have a count for this group?
+                                               map<string, int>::iterator itGroup = tree[index].groupCount.find(group);
+                                               
+                                               //if yes, increment it - there should not be a case where we can't find it since we load group in read
+                                               if (itGroup != tree[index].groupCount.end()) {
+                                                       tree[index].groupCount[group]++;
+                                               }                                               
+                                       }
+                                       
+                                       currentNode = index;
+                                       
+                               }else{ //otherwise, error
+                                       m->mothurOut("Warning: cannot find taxon " + taxon + " in reference taxonomy tree at level " + toString(tree[currentNode].level) + " for " + seqName + ". This may cause totals of daughter levels not to add up in summary file."); m->mothurOutEndLine();
+                                       break;
+                               }
                        }
                        
                        level++;
@@ -148,6 +209,117 @@ int PhyloSummary::addSeqToTree(string seqName, string seqTaxonomy){
 }
 /**************************************************************************************************/
 
+int PhyloSummary::addSeqToTree(string seqTaxonomy, vector<string> names){
+       try {
+               numSeqs++;
+               
+               map<string, int>::iterator childPointer;
+               
+               int currentNode = 0;
+               string taxon;
+               
+               int level = 0;
+               
+               while (seqTaxonomy != "") {
+                       
+                       if (m->control_pressed) { return 0; }
+                       
+                       //somehow the parent is getting one too many accnos
+                       //use print to reassign the taxa id
+                       taxon = getNextTaxon(seqTaxonomy);
+                       
+                       childPointer = tree[currentNode].children.find(taxon);
+                       
+                       if(childPointer != tree[currentNode].children.end()){   //if the node already exists, update count and move on
+                               if (groupmap != NULL) {
+                                       
+                                       map<string, bool> containsGroup; 
+                                       for (int j = 0; j < groupmap->namesOfGroups.size(); j++) {
+                                               containsGroup[groupmap->namesOfGroups[j]] = false;
+                                       }
+                                       
+                                       for (int k = 0; k < names.size(); k++) {
+                                               //find out the sequences group
+                                               string group = groupmap->getGroup(names[k]);
+                                       
+                                               if (group == "not found") {  m->mothurOut(names[k] + " is not in your groupfile, and will be included in the overall total, but not any group total."); m->mothurOutEndLine();  }
+                                               else {
+                                                       containsGroup[group] = true;
+                                               }
+                                       }
+                                       
+                                       for (map<string, bool>::iterator itGroup = containsGroup.begin(); itGroup != containsGroup.end(); itGroup++) {
+                                               if (itGroup->second == true) {
+                                                       tree[childPointer->second].groupCount[itGroup->first]++;
+                                               }
+                                       }
+                                       
+                               }
+                               
+                               tree[childPointer->second].total++;
+                               
+                               currentNode = childPointer->second;
+                       }else{  
+                               if (ignore) {
+                                       
+                                       tree.push_back(rawTaxNode(taxon));
+                                       int index = tree.size() - 1;
+                                       
+                                       tree[index].parent = currentNode;
+                                       tree[index].level = (level+1);
+                                       tree[index].total = 1;
+                                       tree[currentNode].children[taxon] = index;
+                                       
+                                       //initialize groupcounts
+                                       if (groupmap != NULL) {
+                                               map<string, bool> containsGroup; 
+                                               for (int j = 0; j < groupmap->namesOfGroups.size(); j++) {
+                                                       tree[index].groupCount[groupmap->namesOfGroups[j]] = 0;
+                                                       containsGroup[groupmap->namesOfGroups[j]] = false;
+                                               }
+                                               
+                                               
+                                               for (int k = 0; k < names.size(); k++) {
+                                                       //find out the sequences group
+                                                       string group = groupmap->getGroup(names[k]);
+                                                       
+                                                       if (group == "not found") {  m->mothurOut(names[k] + " is not in your groupfile, and will be included in the overall total, but not any group total."); m->mothurOutEndLine();  }
+                                                       else {
+                                                               containsGroup[group] = true;
+                                                       }
+                                               }
+                                               
+                                               for (map<string, bool>::iterator itGroup = containsGroup.begin(); itGroup != containsGroup.end(); itGroup++) {
+                                                       if (itGroup->second == true) {
+                                                               tree[index].groupCount[itGroup->first]++;
+                                                       }
+                                               }
+                                       }
+                                       
+                                       currentNode = index;
+                                       
+                               }else{ //otherwise, error
+                                       m->mothurOut("Warning: cannot find taxon " + taxon + " in reference taxonomy tree at level " + toString(tree[currentNode].level) + ". This may cause totals of daughter levels not to add up in summary file."); m->mothurOutEndLine();
+                                       break;
+                               }
+                       }
+                       
+                       level++;
+                       
+                       if ((seqTaxonomy == "") && (level < maxLevel)) {  //if you think you are done and you are not.
+                               for (int k = level; k < maxLevel; k++) {  seqTaxonomy += "unclassified;";   }
+                       }
+               }
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "PhyloSummary", "addSeqToTree");
+               exit(1);
+       }
+}
+
+/**************************************************************************************************/
+
 void PhyloSummary::assignRank(int index){
        try {
                map<string,int>::iterator it;
@@ -169,6 +341,9 @@ void PhyloSummary::assignRank(int index){
 
 void PhyloSummary::print(ofstream& out){
        try {
+               
+               if (ignore) { assignRank(0); }
+               
                //print labels
                out << "taxlevel\t rankID\t taxon\t daughterlevels\t total\t";
                if (groupmap != NULL) {
index 9ebdf81ac4fbd06490f05d7be7c15b6f6a5d1179..04ba65f8ad6c93a5db879a323a2a6b6ed890a3c5 100644 (file)
@@ -32,11 +32,13 @@ struct rawTaxNode {
 class PhyloSummary {
 
 public:
+       PhyloSummary(string);
        PhyloSummary(string, string);
        ~PhyloSummary() { if (groupmap != NULL)  {  delete groupmap;  }  }
        
        void summarize(string);  //pass it a taxonomy file and a group file and it makes the tree
        int addSeqToTree(string, string);
+       int addSeqToTree(string, vector<string>);
        void print(ofstream&);
        int getMaxLevel() { return maxLevel; }
        
@@ -47,6 +49,7 @@ private:
        void assignRank(int);
        void readTreeStruct(ifstream&);
        GroupMap* groupmap;
+       bool ignore;
        
        int numNodes;
        int numSeqs;