X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=makebiomcommand.cpp;h=6385adef0fd1c36f73e509604f10b082a75e54a5;hb=006601d68abe8d0061f77e8d28323b160750e343;hp=eb46b97b9566773d5e02b3d5e4c38e50fae94719;hpb=aa23898d895a8c04cc4582a8809343df0aef6622;p=mothur.git diff --git a/makebiomcommand.cpp b/makebiomcommand.cpp index eb46b97..6385ade 100644 --- a/makebiomcommand.cpp +++ b/makebiomcommand.cpp @@ -9,6 +9,7 @@ #include "makebiomcommand.h" #include "sharedrabundvector.h" #include "inputdata.h" +#include "sharedutilities.h" //taken from http://biom-format.org/documentation/biom_format.html /* Minimal Sparse @@ -93,6 +94,7 @@ vector MakeBiomCommand::setParameters(){ try { CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pshared); CommandParameter pcontaxonomy("contaxonomy", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pcontaxonomy); + CommandParameter pmetadata("metadata", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pmetadata); CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups); CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel); CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir); @@ -112,12 +114,13 @@ vector MakeBiomCommand::setParameters(){ string MakeBiomCommand::getHelpString(){ try { string helpString = ""; - helpString += "The make.biom command parameters are shared, groups, matrixtype and label. shared is required, unless you have a valid current file.\n"; + helpString += "The make.biom command parameters are shared, contaxonomy, metadata, groups, matrixtype and label. shared is required, unless you have a valid current file.\n"; helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like included. The group names are separated by dashes.\n"; helpString += "The label parameter allows you to select what distance levels you would like, and are also separated by dashes.\n"; helpString += "The matrixtype parameter allows you to select what type you would like to make. Choices are sparse and dense, default is sparse.\n"; + helpString += "The contaxonomy file is the taxonomy file outputted by classify.otu(list=yourListfile, taxonomy=yourTaxonomyFile). Be SURE that the you are the constaxonomy file distance matches the shared file distance. ie, for *.0.03.cons.taxonomy set label=0.03. Mothur is smart enough to handle shared files that have been subsampled. It is used to assign taxonomy information to the metadata of rows.\n"; + helpString += "The metadata parameter is used to provide experimental parameters to the columns. Things like 'sample1 gut human_gut'. \n"; helpString += "The make.biom command should be in the following format: make.biom(shared=yourShared, groups=yourGroups, label=yourLabels).\n"; - helpString += "The contaxonomy file is the taxonomy file outputted by classify.otu(list=yourListfile, taxonomy=yourTaxonomyFile). Be SURE that the you are the constaxonomy file distance matches the shared file distance. ie, for *.0.03.cons.taxonomy set label=0.03. Mothur is smart enough to handle shared files that have been subsampled after the classify.otu command was run on the list and taxonomy file.\n"; helpString += "Example make.biom(shared=abrecovery.an.shared, groups=A-B-C).\n"; helpString += "The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n"; helpString += "The make.biom command outputs a .biom file.\n"; @@ -129,6 +132,27 @@ string MakeBiomCommand::getHelpString(){ exit(1); } } +//********************************************************************************************************************** +string MakeBiomCommand::getOutputFileNameTag(string type, string inputName=""){ + try { + string outputFileName = ""; + map >::iterator it; + + //is this a type this command creates + it = outputTypes.find(type); + if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); } + else { + if (type == "biom") { outputFileName = "biom"; } + else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; } + } + return outputFileName; + } + catch(exception& e) { + m->errorOut(e, "MakeBiomCommand", "getOutputFileNameTag"); + exit(1); + } +} + //********************************************************************************************************************** MakeBiomCommand::MakeBiomCommand(){ try { @@ -165,10 +189,10 @@ MakeBiomCommand::MakeBiomCommand(string option) { for (it = parameters.begin(); it != parameters.end(); it++) { if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } } - + //initialize outputTypes vector tempOutNames; - outputTypes["relabund"] = tempOutNames; + outputTypes["biom"] = 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); @@ -190,6 +214,14 @@ MakeBiomCommand::MakeBiomCommand(string option) { //if the user has not given a path then, add inputdir. else leave path alone. if (path == "") { parameters["contaxonomy"] = inputDir + it->second; } } + + it = parameters.find("metadata"); + //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["metadata"] = inputDir + it->second; } + } } //get shared file @@ -210,6 +242,9 @@ MakeBiomCommand::MakeBiomCommand(string option) { if (contaxonomyfile == "not found") { contaxonomyfile = ""; } else if (contaxonomyfile == "not open") { contaxonomyfile = ""; abort = true; } + metadatafile = validParameter.validFile(parameters, "metadata", true); + if (metadatafile == "not found") { metadatafile = ""; } + else if (metadatafile == "not open") { metadatafile = ""; abort = true; } //check for optional parameter and set defaults // ...at some point should added some additional type checking... @@ -259,6 +294,8 @@ int MakeBiomCommand::execute(){ labels.insert(lastLabel); } + getSampleMetaData(lookup); + //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label. set processedLabels; set userLabels = labels; @@ -328,6 +365,14 @@ int MakeBiomCommand::execute(){ if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } + //set sabund file as new current sabundfile + string current = ""; + itTypes = outputTypes.find("biom"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setBiomFile(current); } + } + + m->mothurOutEndLine(); m->mothurOut("Output File Names: "); m->mothurOutEndLine(); for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); } @@ -344,7 +389,7 @@ int MakeBiomCommand::execute(){ int MakeBiomCommand::getBiom(vector& lookup){ try { - string outputFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + lookup[0]->getLabel() + ".biom"; + string outputFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + lookup[0]->getLabel() + "." + getOutputFileNameTag("biom"); ofstream out; m->openOutputFile(outputFileName, out); outputNames.push_back(outputFileName); outputTypes["biom"].push_back(outputFileName); @@ -394,13 +439,13 @@ int MakeBiomCommand::getBiom(vector& lookup){ {"id":"Sample6", "metadata":null} ],*/ - string colBack = "\", \"metadata\":null}"; + string colBack = "\", \"metadata\":"; out << spaces + "\"columns\":[\n"; for (int i = 0; i < lookup.size()-1; i++) { if (m->control_pressed) { out.close(); return 0; } - out << rowFront << lookup[i]->getGroup() << colBack << ",\n"; + out << rowFront << lookup[i]->getGroup() << colBack << sampleMetadata[i] << "},\n"; } - out << rowFront << lookup[(lookup.size()-1)]->getGroup() << colBack << "\n" + spaces + "],\n"; + out << rowFront << lookup[(lookup.size()-1)]->getGroup() << colBack << sampleMetadata[lookup.size()-1] << "}\n" + spaces + "],\n"; out << spaces + "\"matrix_type\": \"" << format << "\",\n" + spaces + "\"matrix_element_type\": \"int\",\n"; out << spaces + "\"shape\": [" << m->currentBinLabels.size() << "," << lookup.size() << "],\n"; @@ -513,26 +558,23 @@ vector MakeBiomCommand::getMetaData(vector& lookup) //convert list file bin labels to shared file bin labels //parse tax strings //save in map - map > labelTaxMap; + map labelTaxMap; string snumBins = toString(otuLabels.size()); for (int i = 0; i < otuLabels.size(); i++) { if (m->control_pressed) { return metadata; } //if there is a bin label use it otherwise make one - string binLabel = binTag; - string sbinNumber = otuLabels[i]; - if (sbinNumber.length() < snumBins.length()) { - int diff = snumBins.length() - sbinNumber.length(); - for (int h = 0; h < diff; h++) { binLabel += "0"; } - } - binLabel += sbinNumber; - - vector taxString; - m->splitAtChar(taxs[i], taxString, ';'); - taxString.pop_back(); //adds blank string because taxonomies end in ; - - labelTaxMap[binLabel] = taxString; + if (m->isContainingOnlyDigits(otuLabels[i])) { + string binLabel = binTag; + string sbinNumber = otuLabels[i]; + if (sbinNumber.length() < snumBins.length()) { + int diff = snumBins.length() - sbinNumber.length(); + for (int h = 0; h < diff; h++) { binLabel += "0"; } + } + binLabel += sbinNumber; + labelTaxMap[binLabel] = taxs[i]; + }else { labelTaxMap[otuLabels[i]] = taxs[i]; } } @@ -540,7 +582,7 @@ vector MakeBiomCommand::getMetaData(vector& lookup) //traverse the binLabels forming the metadata strings and saving them //make sure to sanity check - map >::iterator it; + map::iterator it; for (int i = 0; i < m->currentBinLabels.size(); i++) { if (m->control_pressed) { return metadata; } @@ -549,37 +591,24 @@ vector MakeBiomCommand::getMetaData(vector& lookup) if (it == labelTaxMap.end()) { m->mothurOut("[ERROR]: can't find taxonomy information for " + m->currentBinLabels[i] + ".\n"); m->control_pressed = true; } else { + vector bootstrapValues; string data = "{\"taxonomy\":["; - for (int j = 0; j < (it->second).size()-1; j ++) { - - string taxon = (it->second)[j]; - - //strip "" if they are there - int pos = taxon.find("\""); - if (pos != string::npos) { - string newTax = ""; - for (int k = 0; k < taxon.length(); k++) { - if (taxon[k] != '\"') { newTax += taxon[k]; } - } - taxon = newTax; - } - - data += "\"" + taxon + "\", "; - } + + vector scores; + vector taxonomies = parseTax(it->second, scores); - string taxon = (it->second)[(it->second).size()-1]; + for (int j = 0; j < taxonomies.size()-1; j ++) { data += "\"" + taxonomies[j] + "\", "; } + data += "\"" + taxonomies[taxonomies.size()-1] + "\"]"; - //strip "" if they are there - int pos = taxon.find("\""); - if (pos != string::npos) { - string newTax = ""; - for (int k = 0; k < taxon.length(); k++) { - if (taxon[k] != '\"') { newTax += taxon[k]; } - } - taxon = newTax; + //add bootstrap values if available + if (scores[0] != "null") { + data += ", \"bootstrap\":["; + + for (int j = 0; j < scores.size()-1; j ++) { data += scores[j] + ", "; } + data += scores[scores.size()-1] + "]"; + } - - data += "\"" + taxon + "\"]} "; + data += "}"; metadata.push_back(data); } @@ -594,7 +623,133 @@ vector MakeBiomCommand::getMetaData(vector& lookup) exit(1); } -} +} +//********************************************************************************************************************** +int MakeBiomCommand::getSampleMetaData(vector& lookup){ + try { + + if (metadatafile == "") { for (int i = 0; i < lookup.size(); i++) { sampleMetadata.push_back("null"); } } + else { + ifstream in; + m->openInputFile(metadatafile, in); + + vector groupNames, metadataLabels; + map > lines; + + string headerLine = m->getline(in); m->gobble(in); + vector pieces = m->splitWhiteSpace(headerLine); + + //save names of columns you are reading + for (int i = 1; i < pieces.size(); i++) { + metadataLabels.push_back(pieces[i]); + } + int count = metadataLabels.size(); + + vector groups = m->getGroups(); + + //read rest of file + while (!in.eof()) { + + if (m->control_pressed) { in.close(); return 0; } + + string group = ""; + in >> group; m->gobble(in); + groupNames.push_back(group); + + string line = m->getline(in); m->gobble(in); + vector thisPieces = m->splitWhiteSpaceWithQuotes(line); + + if (thisPieces.size() != count) { m->mothurOut("[ERROR]: expected " + toString(count) + " items of data for sample " + group + " read " + toString(thisPieces.size()) + ", quitting.\n"); } + else { if (m->inUsersGroups(group, groups)) { lines[group] = thisPieces; } } + + m->gobble(in); + } + in.close(); + + map >::iterator it; + for (int i = 0; i < lookup.size(); i++) { + + if (m->control_pressed) { return 0; } + + it = lines.find(lookup[i]->getGroup()); + + if (it == lines.end()) { m->mothurOut("[ERROR]: can't find metadata information for " + lookup[i]->getGroup() + ", quitting.\n"); m->control_pressed = true; } + else { + vector values = it->second; + + string data = "{"; + for (int j = 0; j < metadataLabels.size()-1; j++) { + values[j] = m->removeQuotes(values[j]); + data += "\"" + metadataLabels[j] + "\":\"" + values[j] + "\", "; + } + values[metadataLabels.size()-1] = m->removeQuotes(values[metadataLabels.size()-1]); + data += "\"" + metadataLabels[metadataLabels.size()-1] + "\":\"" + values[metadataLabels.size()-1] + "\"}"; + sampleMetadata.push_back(data); + } + } + } + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "MakeBiomCommand", "getSampleMetaData"); + exit(1); + } + +} + +/**************************************************************************************************/ +//returns {Bacteria, Bacteroidetes, ..} and scores is filled with {100, 98, ...} or {null, null, null} +vector MakeBiomCommand::parseTax(string tax, vector& scores) { + try { + + string taxon; + vector taxs; + + while (tax.find_first_of(';') != -1) { + + if (m->control_pressed) { return taxs; } + + //get taxon + taxon = tax.substr(0,tax.find_first_of(';')); + + int pos = taxon.find_last_of('('); + if (pos != -1) { + //is it a number? + int pos2 = taxon.find_last_of(')'); + if (pos2 != -1) { + string confidenceScore = taxon.substr(pos+1, (pos2-(pos+1))); + if (m->isNumeric1(confidenceScore)) { + taxon = taxon.substr(0, pos); //rip off confidence + scores.push_back(confidenceScore); + }else{ scores.push_back("null"); } + } + }else{ scores.push_back("null"); } + + //strip "" if they are there + pos = taxon.find("\""); + if (pos != string::npos) { + string newTax = ""; + for (int k = 0; k < taxon.length(); k++) { + if (taxon[k] != '\"') { newTax += taxon[k]; } + } + taxon = newTax; + } + + //look for bootstrap value + taxs.push_back(taxon); + tax = tax.substr(tax.find_first_of(';')+1, tax.length()); + } + + return taxs; + } + catch(exception& e) { + m->errorOut(e, "MakeBiomCommand", "parseTax"); + exit(1); + } +} + //**********************************************************************************************************************