1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/*
* summarytaxcommand.cpp
* Mothur
*
* Created by westcott on 9/23/11.
* Copyright 2011 Schloss Lab. All rights reserved.
*
*/
#include "summarytaxcommand.h"
#include "phylosummary.h"
//**********************************************************************************************************************
vector<string> SummaryTaxCommand::setParameters(){
try {
CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "none", "none","summary",false,true,true); parameters.push_back(ptaxonomy);
CommandParameter pname("name", "InputTypes", "", "", "NameCount", "none", "none","",false,false,true); parameters.push_back(pname);
CommandParameter pcount("count", "InputTypes", "", "", "NameCount-CountGroup", "none", "none","",false,false,true); parameters.push_back(pcount);
CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "none", "none","",false,false,true); parameters.push_back(pgroup);
CommandParameter prelabund("relabund", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(prelabund);
CommandParameter poutput("output", "Multiple", "simple-detail", "detail", "", "", "","",false,false, true); parameters.push_back(poutput);
CommandParameter pthreshold("threshold", "Number", "", "0", "", "", "","",false,true); parameters.push_back(pthreshold);
CommandParameter pprintlevel("printlevel", "Number", "", "-1", "", "", "","",false,false); parameters.push_back(pprintlevel);
CommandParameter pseed("seed", "Number", "", "0", "", "", "","",false,false); parameters.push_back(pseed);
CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
vector<string> tempOutNames;
outputTypes["summary"] = tempOutNames;
abort = false; calledHelp = false;
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, "SummaryTaxCommand", "setParameters");
exit(1);
}
}
//**********************************************************************************************************************
string SummaryTaxCommand::getHelpString(){
try {
string helpString = "";
helpString += "The summary.tax command reads a taxonomy file and an optional name file, and summarizes the taxonomy information.\n";
helpString += "The summary.tax command parameters are taxonomy, count, group, name and relabund. taxonomy is required, unless you have a valid current taxonomy file.\n";
helpString += "The name parameter allows you to enter a name file associated with your taxonomy file. \n";
helpString += "The group parameter allows you add a group file so you can have the summary totals broken up by group.\n";
helpString += "The count parameter allows you add a count file so you can have the summary totals broken up by group.\n";
helpString += "The threshold parameter allows you to specify a cutoff for the taxonomy file that is being inputted. Once the classification falls below the threshold the mothur will refer to it as unclassified when calculating the concensus. This feature is similar to adjusting the cutoff in classify.seqs. Default=0.\n";
helpString += "The output parameter allows you to specify format of your summary file. Options are simple and detail. The default is detail.\n";
helpString += "The printlevel parameter allows you to specify taxlevel of your summary file to print to. Options are 1 to the maz level in the file. The default is -1, meaning max level. If you select a level greater than the level your sequences classify to, mothur will print to the level your max level. \n";
helpString += "The relabund parameter allows you to indicate you want the summary file values to be relative abundances rather than raw abundances. Default=F. \n";
helpString += "The summary.tax command should be in the following format: \n";
helpString += "summary.tax(taxonomy=yourTaxonomyFile) \n";
return helpString;
}
catch(exception& e) {
m->errorOut(e, "SummaryTaxCommand", "getHelpString");
exit(1);
}
}
//**********************************************************************************************************************
string SummaryTaxCommand::getOutputPattern(string type) {
try {
string pattern = "";
if (type == "summary") { pattern = "[filename],tax.summary"; }
else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->setControl_pressed(true); }
return pattern;
}
catch(exception& e) {
m->errorOut(e, "SummaryTaxCommand", "getOutputPattern");
exit(1);
}
}
//***************************************************************************************************************
SummaryTaxCommand::SummaryTaxCommand(string option) : Command() {
try {
if(option == "help") { help(); abort = true; calledHelp = true; }
else if(option == "citation") { citation(); abort = true; calledHelp = true;}
else if(option == "category") { abort = true; calledHelp = true; }
else {
OptionParser parser(option, setParameters());
map<string,string> parameters = parser.getParameters();
ValidParameters validParameter;
taxfile = validParameter.validFile(parameters, "taxonomy");
if (taxfile == "not open") { abort = true; }
else if (taxfile == "not found") {
taxfile = current->getTaxonomyFile();
if (taxfile != "") { m->mothurOut("Using " + taxfile + " as input file for the taxonomy parameter.\n"); }
else { m->mothurOut("You have no current taxonomy file and the taxonomy parameter is required.\n"); abort = true; }
}else { current->setTaxonomyFile(taxfile); }
namefile = validParameter.validFile(parameters, "name");
if (namefile == "not open") { abort = true; }
else if (namefile == "not found") { namefile = ""; }
else { current->setNameFile(namefile); }
groupfile = validParameter.validFile(parameters, "group");
if (groupfile == "not open") { groupfile = ""; abort = true; }
else if (groupfile == "not found") { groupfile = ""; }
else { current->setGroupFile(groupfile); }
countfile = validParameter.validFile(parameters, "count");
if (countfile == "not open") { abort = true; }
else if (countfile == "not found") { countfile = ""; }
else { current->setCountFile(countfile); }
if ((namefile != "") && (countfile != "")) {
m->mothurOut("[ERROR]: you may only use one of the following: name or count.\n"); abort = true;
}
if ((groupfile != "") && (countfile != "")) {
m->mothurOut("[ERROR]: you may only use one of the following: group or count.\n"); abort=true;
}
if (outputdir == ""){ outputdir += util.hasPath(taxfile); }
string temp = validParameter.valid(parameters, "relabund"); if (temp == "not found"){ temp = "false"; }
relabund = util.isTrue(temp);
temp = validParameter.valid(parameters, "printlevel"); if (temp == "not found"){ temp = "-1"; }
util.mothurConvert(temp, printlevel);
output = validParameter.valid(parameters, "output"); if(output == "not found"){ output = "detail"; }
if ((output != "simple") && (output != "detail")) { m->mothurOut(output + " is not a valid output form. Options are simple and detail. I will use detail.\n"); output = "detail"; }
temp = validParameter.valid(parameters, "threshold"); if (temp == "not found") { temp = "0"; }
util.mothurConvert(temp, threshold);
}
}
catch(exception& e) {
m->errorOut(e, "SummaryTaxCommand", "SummaryTaxCommand");
exit(1);
}
}
//***************************************************************************************************************
int SummaryTaxCommand::execute(){
try{
if (abort) { if (calledHelp) { return 0; } return 2; }
int maxLevel = findMaxLevel(taxfile);
long start = time(nullptr);
GroupMap* groupMap = nullptr;
CountTable* ct = nullptr;
if (groupfile != "") {
groupMap = new GroupMap(groupfile);
groupMap->readMap();
}else if (countfile != "") {
ct = new CountTable();
ct->readTable(countfile, true, false);
}
PhyloSummary* taxaSum;
if (countfile != "") { taxaSum = new PhyloSummary(ct, relabund, printlevel);
}else { taxaSum = new PhyloSummary(groupMap, relabund, printlevel); }
if (m->getControl_pressed()) { if (groupMap != nullptr) { delete groupMap; } if (ct != nullptr) { delete ct; } delete taxaSum; return 0; }
int numSeqs = 0;
map<string, vector<string> > nameMap;
map<string, vector<string> >::iterator itNames;
if (namefile != "") { util.readNames(namefile, nameMap); }
ifstream in; util.openInputFile(taxfile, in);
string name, taxon;
while(!in.eof()){
if (m->getControl_pressed()) { break; }
in >> name; gobble(in);
taxon = util.getline(in); gobble(in);
string newTax = util.addUnclassifieds(taxon, maxLevel, true);
if (threshold != 0) { newTax = processTaxMap(newTax); }
//add sequence to summary, countfile info included from Phylosummary constructor
if (namefile != "") {
itNames = nameMap.find(name);
if (itNames == nameMap.end()) {
m->mothurOut(name + " is not in your name file please correct.\n"); exit(1);
}else{
for (int i = 0; i < itNames->second.size(); i++) {
taxaSum->addSeqToTree(itNames->second[i], newTax); //add it as many times as there are identical seqs
numSeqs++;
}
itNames->second.clear();
nameMap.erase(itNames->first);
}
}else {
taxaSum->addSeqToTree(name, newTax);
numSeqs++;
}
}
in.close();
if (m->getControl_pressed()) { if (groupMap != nullptr) { delete groupMap; } if (ct != nullptr) { delete ct; } delete taxaSum; return 0; }
//print summary file
ofstream outTaxTree;
map<string, string> variables;
variables["[filename]"] = outputdir + util.getRootName(util.getSimpleName(taxfile));
string summaryFile = getOutputFileName("summary",variables);
util.openOutputFile(summaryFile, outTaxTree);
taxaSum->print(outTaxTree, output);
outTaxTree.close();
delete taxaSum;
if (groupMap != nullptr) { delete groupMap; } if (ct != nullptr) { numSeqs = ct->getNumSeqs(); delete ct; }
if (m->getControl_pressed()) { util.mothurRemove(summaryFile); return 0; }
m->mothurOutEndLine();
m->mothurOut("It took " + toString(time(nullptr) - start) + " secs to create the summary file for " + toString(numSeqs) + " sequences.\n"); m->mothurOutEndLine();
m->mothurOut("\nOutput File Names: \n");
m->mothurOut(summaryFile); m->mothurOutEndLine(); outputNames.push_back(summaryFile); outputTypes["summary"].push_back(summaryFile);
m->mothurOutEndLine();
return 0;
}
catch(exception& e) {
m->errorOut(e, "SummaryTaxCommand", "execute");
exit(1);
}
}
/**************************************************************************************************/
string SummaryTaxCommand::processTaxMap(string tax) {
try{
string newTax = tax;
vector<string> taxons;
int taxLength = tax.length();
string taxon = "";
int spot = 0;
for(int i=0;i<taxLength;i++){
if(tax[i] == ';'){
int openParen = taxon.find_last_of('(');
int closeParen = taxon.find_last_of(')');
string newtaxon, confidence;
if ((openParen != string::npos) && (closeParen != string::npos)) {
string confidenceScore = taxon.substr(openParen+1, (closeParen-(openParen+1)));
if (util.isNumeric1(confidenceScore)) { //its a confidence
newtaxon = taxon.substr(0, openParen); //rip off confidence
confidence = taxon.substr((openParen+1), (closeParen-openParen-1));
}else { //its part of the taxon
newtaxon = taxon;
confidence = "0";
}
}else{
newtaxon = taxon;
confidence = "-1";
}
float con = 0; util.mothurConvert(confidence, con);
if (util.isEqual(con, -1)) { i += taxLength; } //not a confidence score, no confidence scores on this taxonomy
else if ( con < threshold) { spot = i; break; } //below threshold, set all to unclassified
else {} //acceptable, move on
taxons.push_back(taxon);
taxon = "";
}
else{
taxon += tax[i];
}
}
if (spot != 0) {
newTax = "";
for (int i = 0; i < taxons.size(); i++) { newTax += taxons[i] + ";"; }
//for (int i = spot; i < taxLength; i++) {
//if(tax[i] == ';'){ newTax += "unclassified;"; }
util.removeConfidences(newTax);
//}
}else { util.removeConfidences(tax); newTax = tax; } //leave tax alone
return newTax;
}
catch(exception& e) {
m->errorOut(e, "SummaryTaxCommand", "processTaxMap");
exit(1);
}
}
/**************************************************************************************************/
int SummaryTaxCommand::findMaxLevel(string file) {
try{
GroupMap* groupMap = nullptr;
PhyloSummary taxaSum(groupMap, false, -1);
taxaSum.summarize(file);
return taxaSum.getMaxLevel();
}
catch(exception& e) {
m->errorOut(e, "SummaryTaxCommand", "findMaxLevel");
exit(1);
}
}
/**************************************************************************************/
|