From: westcott Date: Mon, 13 Dec 2010 20:33:34 +0000 (+0000) Subject: added summary file to classify.otu X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=commitdiff_plain;h=a5af3313f0221a8b41aa362d72eadb60a4dd6e27 added summary file to classify.otu --- diff --git a/classifyotucommand.cpp b/classifyotucommand.cpp index 3469c82..d3d574e 100644 --- a/classifyotucommand.cpp +++ b/classifyotucommand.cpp @@ -9,11 +9,12 @@ #include "classifyotucommand.h" #include "phylotree.h" +#include "phylosummary.h" //********************************************************************************************************************** vector 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 myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); return myArray; } @@ -29,6 +30,7 @@ ClassifyOtuCommand::ClassifyOtuCommand(){ //initialize outputTypes vector 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 myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -87,6 +89,7 @@ ClassifyOtuCommand::ClassifyOtuCommand(string option) { //initialize outputTypes vector 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 ClassifyOtuCommand::findConsensusTaxonomy(int bin, ListVector* thisList, int& size, string& conTax) { try{ - string conTax = ""; + conTax = ""; vector names; + vector allNames; map::iterator it; map::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 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; } diff --git a/classifyotucommand.h b/classifyotucommand.h index 60bfb29..e81c246 100644 --- a/classifyotucommand.h +++ b/classifyotucommand.h @@ -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 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 findConsensusTaxonomy(int, ListVector*, int&, string&); // returns the name of the "representative" taxonomy of given bin }; diff --git a/mothur b/mothur index 0812c5e..750a30c 100755 Binary files a/mothur and b/mothur differ diff --git a/mothur.h b/mothur.h index 2e2299e..b0ad51e 100644 --- a/mothur.h +++ b/mothur.h @@ -36,6 +36,7 @@ #include #include #include +#include //math #include diff --git a/phylosummary.cpp b/phylosummary.cpp index 4e94605..a9a170b 100644 --- a/phylosummary.cpp +++ b/phylosummary.cpp @@ -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::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 names){ + try { + numSeqs++; + + map::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 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::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 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::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::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) { diff --git a/phylosummary.h b/phylosummary.h index 9ebdf81..04ba65f 100644 --- a/phylosummary.h +++ b/phylosummary.h @@ -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); 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;