//**********************************************************************************************************************
vector<string> UnifracUnweightedCommand::setParameters(){
try {
- CommandParameter ptree("tree", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(ptree);
- CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
- CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
- CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
- CommandParameter piters("iters", "Number", "", "1000", "", "", "",false,false); parameters.push_back(piters);
- CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
- CommandParameter prandom("random", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(prandom);
- CommandParameter pdistance("distance", "Multiple", "column-lt-square-phylip", "column", "", "", "",false,false); parameters.push_back(pdistance);
- CommandParameter psubsample("subsample", "String", "", "", "", "", "",false,false); parameters.push_back(psubsample);
- CommandParameter pconsensus("consensus", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pconsensus);
- CommandParameter proot("root", "Boolean", "F", "", "", "", "",false,false); parameters.push_back(proot);
- CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
- CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
+ CommandParameter ptree("tree", "InputTypes", "", "", "none", "none", "none","unweighted-uwsummary",false,true,true); parameters.push_back(ptree);
+ 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 pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
+ CommandParameter piters("iters", "Number", "", "1000", "", "", "","",false,false); parameters.push_back(piters);
+ CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors);
+ CommandParameter prandom("random", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(prandom);
+ CommandParameter pdistance("distance", "Multiple", "column-lt-square-phylip", "column", "", "", "","phylip-column",false,false); parameters.push_back(pdistance);
+ CommandParameter psubsample("subsample", "String", "", "", "", "", "","",false,false); parameters.push_back(psubsample);
+ CommandParameter pconsensus("consensus", "Boolean", "", "F", "", "", "","tree",false,false); parameters.push_back(pconsensus);
+ CommandParameter proot("root", "Boolean", "F", "", "", "", "","",false,false); parameters.push_back(proot);
+ CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
+ CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
vector<string> myArray;
for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
string UnifracUnweightedCommand::getHelpString(){
try {
string helpString = "";
- helpString += "The unifrac.unweighted command parameters are tree, group, name, groups, iters, distance, processors, root and random. tree parameter is required unless you have valid current tree file.\n";
+ helpString += "The unifrac.unweighted command parameters are tree, group, name, count, groups, iters, distance, processors, root and random. tree parameter is required unless you have valid current tree file.\n";
helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed. You must enter at least 1 valid group.\n";
helpString += "The group names are separated by dashes. The iters parameter allows you to specify how many random trees you would like compared to your tree.\n";
helpString += "The distance parameter allows you to create a distance file from the results. The default is false. You may set distance to lt, square or column.\n";
}
}
//**********************************************************************************************************************
-string UnifracUnweightedCommand::getOutputFileNameTag(string type, string inputName=""){
- try {
- string outputFileName = "";
- map<string, vector<string> >::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 == "unweighted") { outputFileName = "unweighted"; }
- else if (type == "uwsummary") { outputFileName = "uwsummary"; }
- else if (type == "phylip") { outputFileName = "dist"; }
- else if (type == "column") { outputFileName = "dist"; }
- else if (type == "tree") { outputFileName = "tre"; }
- 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, "UnifracUnweightedCommand", "getOutputFileNameTag");
- exit(1);
- }
+string UnifracUnweightedCommand::getOutputPattern(string type) {
+ try {
+ string pattern = "";
+ if (type == "unweighted") { pattern = "[filename],unweighted-[filename],[tag],unweighted"; }
+ else if (type == "uwsummary") { pattern = "[filename],uwsummary"; }
+ else if (type == "phylip") { pattern = "[filename],[tag],[tag2],dist"; }
+ else if (type == "column") { pattern = "[filename],[tag],[tag2],dist"; }
+ else if (type == "tree") { pattern = "[filename],[tag],[tag2],tre"; }
+ else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
+
+ return pattern;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "UnifracUnweightedCommand", "getOutputPattern");
+ exit(1);
+ }
}
-
//**********************************************************************************************************************
UnifracUnweightedCommand::UnifracUnweightedCommand(){
try {
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["name"] = inputDir + it->second; }
}
+
+ it = parameters.find("count");
+ //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["count"] = inputDir + it->second; }
+ }
}
//check for required parameters
if (namefile == "not open") { namefile = ""; abort = true; }
else if (namefile == "not found") { namefile = ""; }
else { m->setNameFile(namefile); }
+
+ countfile = validParameter.validFile(parameters, "count", true);
+ if (countfile == "not open") { countfile = ""; abort = true; }
+ else if (countfile == "not found") { countfile = ""; }
+ else { m->setCountTableFile(countfile); }
+
+ if ((namefile != "") && (countfile != "")) {
+ m->mothurOut("[ERROR]: you may only use one of the following: name or count."); m->mothurOutEndLine(); abort = true;
+ }
+
+ if ((groupfile != "") && (countfile != "")) {
+ m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
+ }
outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(treefile); }
consensus = m->isTrue(temp);
if (subsample && random) { m->mothurOut("[ERROR]: random must be false, if subsample=t.\n"); abort=true; }
- if (subsample && (groupfile == "")) { m->mothurOut("[ERROR]: if subsample=t, a group file must be provided.\n"); abort=true; }
+ if (countfile == "") { if (subsample && (groupfile == "")) { m->mothurOut("[ERROR]: if subsample=t, a group file must be provided.\n"); abort=true; } }
+ else {
+ CountTable testCt;
+ if ((!testCt.testGroups(countfile)) && (subsample)) {
+ m->mothurOut("[ERROR]: if subsample=t, a count file with group info must be provided.\n"); abort=true;
+ }
+ }
if (subsample && (!phylip)) { phylip=true; outputForm = "lt"; }
if (consensus && (!subsample)) { m->mothurOut("[ERROR]: you cannot use consensus without subsample.\n"); abort=true; }
m->setGroups(Groups);
}
- if (namefile == "") {
- vector<string> files; files.push_back(treefile);
- parser.getNameFile(files);
- }
+ if (countfile=="") {
+ if (namefile == "") {
+ vector<string> files; files.push_back(treefile);
+ parser.getNameFile(files);
+ }
+ }
}
}
m->setTreeFile(treefile);
- TreeReader* reader = new TreeReader(treefile, groupfile, namefile);
+ TreeReader* reader;
+ if (countfile == "") { reader = new TreeReader(treefile, groupfile, namefile); }
+ else { reader = new TreeReader(treefile, countfile); }
T = reader->getTrees();
- tmap = T[0]->getTreeMap();
- map<string, string> nameMap = reader->getNames();
- map<string, string> unique2Dup = reader->getNameMap();
- delete reader;
+ ct = T[0]->getCountTable();
+ delete reader;
- sumFile = outputDir + m->getRootName(m->getSimpleName(treefile)) + getOutputFileNameTag("uwsummary");
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(treefile));
+ sumFile = getOutputFileName("uwsummary",variables);
outputNames.push_back(sumFile); outputTypes["uwsummary"].push_back(sumFile);
m->openOutputFile(sumFile, outSum);
SharedUtil util;
Groups = m->getGroups();
- vector<string> namesGroups = tmap->getNamesOfGroups();
+ vector<string> namesGroups = ct->getNamesOfGroups();
util.setGroups(Groups, namesGroups, allGroups, numGroups, "unweighted"); //sets the groups the user wants to analyze
Unweighted unweighted(includeRoot);
//user has not set size, set size = smallest samples size
if (subsampleSize == -1) {
vector<string> temp; temp.push_back(Groups[0]);
- subsampleSize = (tmap->getNamesSeqs(temp)).size(); //num in first group
+ subsampleSize = ct->getGroupCount(Groups[0]); //num in first group
for (int i = 1; i < Groups.size(); i++) {
- temp.clear(); temp.push_back(Groups[i]);
- int thisSize = (tmap->getNamesSeqs(temp)).size();
+ int thisSize = ct->getGroupCount(Groups[i]);
if (thisSize < subsampleSize) { subsampleSize = thisSize; }
}
m->mothurOut("\nSetting subsample size to " + toString(subsampleSize) + ".\n\n");
vector<string> newGroups = Groups;
Groups.clear();
for (int i = 0; i < newGroups.size(); i++) {
- vector<string> thisGroup; thisGroup.push_back(newGroups[i]);
- vector<string> thisGroupsSeqs = tmap->getNamesSeqs(thisGroup);
- int thisSize = thisGroupsSeqs.size();
+ int thisSize = ct->getGroupCount(newGroups[i]);
if (thisSize >= subsampleSize) { Groups.push_back(newGroups[i]); }
else { m->mothurOut("You have selected a size that is larger than "+newGroups[i]+" number of sequences, removing "+newGroups[i]+".\n"); }
//get pscores for users trees
for (int i = 0; i < T.size(); i++) {
- if (m->control_pressed) { delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }outSum.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
+ if (m->control_pressed) { delete ct; for (int i = 0; i < T.size(); i++) { delete T[i]; }outSum.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
counter = 0;
if (random) {
- output = new ColumnFile(outputDir + m->getSimpleName(treefile) + toString(i+1) + "." + getOutputFileNameTag("unweighted"), itersString);
- outputNames.push_back(outputDir + m->getSimpleName(treefile) + toString(i+1) + "." + getOutputFileNameTag("unweighted"));
- outputTypes["unweighted"].push_back(outputDir + m->getSimpleName(treefile) + toString(i+1) + "." + getOutputFileNameTag("unweighted"));
+ variables["[filename]"] = outputDir + m->getSimpleName(treefile);
+ variables["[tag]"] = toString(i+1);
+ string unFileName = getOutputFileName("unweighted", variables);
+ output = new ColumnFile(unFileName, itersString);
+ outputNames.push_back(unFileName); outputTypes["unweighted"].push_back(unFileName);
}
userData = unweighted.getValues(T[i], processors, outputDir); //userData[0] = unweightedscore
- if (m->control_pressed) { delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }if (random) { delete output; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }return 0; }
+ if (m->control_pressed) { delete ct; for (int i = 0; i < T.size(); i++) { delete T[i]; }if (random) { delete output; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }return 0; }
//output scores for each combination
for(int k = 0; k < numComp; k++) {
if (random) { runRandomCalcs(T[i], userData); }
- if (m->control_pressed) { delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }if (random) { delete output; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
+ if (m->control_pressed) { delete ct; for (int i = 0; i < T.size(); i++) { delete T[i]; }if (random) { delete output; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
int startSubsample = time(NULL);
if (m->control_pressed) { break; }
//copy to preserve old one - would do this in subsample but memory cleanup becomes messy.
- TreeMap* newTmap = new TreeMap();
- //newTmap->getCopy(*tmap);
-
- //SubSample sample;
- //Tree* subSampleTree = sample.getSample(T[i], newTmap, nameMap, subsampleSize);
-
+ CountTable* newCt = new CountTable();
+
//uses method of setting groups to doNotIncludeMe
+ int sampleTime = 0;
+ if (m->debug) { sampleTime = time(NULL); }
SubSample sample;
- Tree* subSampleTree = sample.getSample(T[i], tmap, newTmap, subsampleSize, unique2Dup);
+ Tree* subSampleTree = sample.getSample(T[i], ct, newCt, subsampleSize);
+
+ if (m->debug) { m->mothurOut("[DEBUG]: iter " + toString(thisIter) + " took " + toString(time(NULL) - sampleTime) + " seconds to sample tree.\n"); }
//call new weighted function
vector<double> iterData; iterData.resize(numComp,0);
Unweighted thisUnweighted(includeRoot);
iterData = thisUnweighted.getValues(subSampleTree, processors, outputDir); //userData[0] = weightedscore
-
+
//save data to make ave dist, std dist
calcDistsTotals.push_back(iterData);
- delete newTmap;
+ delete newCt;
delete subSampleTree;
- if((thisIter+1) % 100 == 0){ m->mothurOut(toString(thisIter+1)); m->mothurOutEndLine(); }
+ if((thisIter+1) % 100 == 0){ m->mothurOutJustToScreen(toString(thisIter+1)+"\n"); }
}
- m->mothurOut("It took " + toString(time(NULL) - startSubsample) + " secs to run the subsampling."); m->mothurOutEndLine();
+ if (subsample) { m->mothurOut("It took " + toString(time(NULL) - startSubsample) + " secs to run the subsampling."); m->mothurOutEndLine(); }
- if (m->control_pressed) { delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; }if (random) { delete output; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
+ if (m->control_pressed) { delete ct; for (int i = 0; i < T.size(); i++) { delete T[i]; }if (random) { delete output; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
if (subsample) { getAverageSTDMatrices(calcDistsTotals, i); }
if (consensus) { getConsensusTrees(calcDistsTotals, i); }
outSum.close();
- delete tmap;
+ delete ct;
for (int i = 0; i < T.size(); i++) { delete T[i]; }
if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
int UnifracUnweightedCommand::getAverageSTDMatrices(vector< vector<double> >& dists, int treeNum) {
try {
//we need to find the average distance and standard deviation for each groups distance
-
//finds sum
- vector<double> averages; averages.resize(numComp, 0);
- for (int thisIter = 0; thisIter < subsampleIters; thisIter++) {
- for (int i = 0; i < dists[thisIter].size(); i++) {
- averages[i] += dists[thisIter][i];
- }
- }
-
- //finds average.
- for (int i = 0; i < averages.size(); i++) { averages[i] /= (float) subsampleIters; }
+ vector<double> averages = m->getAverages(dists);
//find standard deviation
- vector<double> stdDev; stdDev.resize(numComp, 0);
-
- for (int thisIter = 0; thisIter < iters; thisIter++) { //compute the difference of each dist from the mean, and square the result of each
- for (int j = 0; j < dists[thisIter].size(); j++) {
- stdDev[j] += ((dists[thisIter][j] - averages[j]) * (dists[thisIter][j] - averages[j]));
- }
- }
- for (int i = 0; i < stdDev.size(); i++) {
- stdDev[i] /= (float) subsampleIters;
- stdDev[i] = sqrt(stdDev[i]);
- }
+ vector<double> stdDev = m->getStandardDeviation(dists, averages);
//make matrix with scores in it
- vector< vector<double> > avedists; avedists.resize(m->getNumGroups());
+ vector< vector<double> > avedists; //avedists.resize(m->getNumGroups());
for (int i = 0; i < m->getNumGroups(); i++) {
- avedists[i].resize(m->getNumGroups(), 0.0);
+ vector<double> temp;
+ for (int j = 0; j < m->getNumGroups(); j++) { temp.push_back(0.0); }
+ avedists.push_back(temp);
}
//make matrix with scores in it
- vector< vector<double> > stddists; stddists.resize(m->getNumGroups());
+ vector< vector<double> > stddists; //stddists.resize(m->getNumGroups());
for (int i = 0; i < m->getNumGroups(); i++) {
- stddists[i].resize(m->getNumGroups(), 0.0);
+ vector<double> temp;
+ for (int j = 0; j < m->getNumGroups(); j++) { temp.push_back(0.0); }
+ //stddists[i].resize(m->getNumGroups(), 0.0);
+ stddists.push_back(temp);
}
+ if (m->debug) { m->mothurOut("[DEBUG]: about to fill matrix.\n"); }
+
//flip it so you can print it
int count = 0;
for (int r=0; r<m->getNumGroups(); r++) {
}
}
- string aveFileName = outputDir + m->getSimpleName(treefile) + toString(treeNum+1) + ".unweighted.ave." + getOutputFileNameTag("phylip");
+ if (m->debug) { m->mothurOut("[DEBUG]: done filling matrix.\n"); }
+
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(treefile));
+ variables["[tag]"] = toString(treeNum+1);
+ variables["[tag2]"] = "unweighted.ave";
+ string aveFileName = getOutputFileName("phylip",variables);
if (outputForm != "column") { outputNames.push_back(aveFileName); outputTypes["phylip"].push_back(aveFileName); }
else { outputNames.push_back(aveFileName); outputTypes["column"].push_back(aveFileName); }
ofstream out;
m->openOutputFile(aveFileName, out);
- string stdFileName = outputDir + m->getSimpleName(treefile) + toString(treeNum+1) + ".unweighted.std." + getOutputFileNameTag("phylip");
+ variables["[tag2]"] = "unweighted.std";
+ string stdFileName = getOutputFileName("phylip",variables);
if (outputForm != "column") { outputNames.push_back(stdFileName); outputTypes["phylip"].push_back(stdFileName); }
else { outputNames.push_back(stdFileName); outputTypes["column"].push_back(stdFileName); }
ofstream outStd;
m->runParse = false;
//create treemap class from groupmap for tree class to use
- TreeMap newTmap;
- newTmap.makeSim(m->getGroups());
+ CountTable newCt;
+ set<string> nameMap;
+ map<string, string> groupMap;
+ set<string> gps;
+ for (int i = 0; i < m->getGroups().size(); i++) {
+ nameMap.insert(m->getGroups()[i]);
+ gps.insert(m->getGroups()[i]);
+ groupMap[m->getGroups()[i]] = m->getGroups()[i];
+ }
+ newCt.createTable(nameMap, groupMap, gps);
//clear old tree names if any
m->Treenames.clear();
//fills globaldatas tree names
m->Treenames = m->getGroups();
- vector<Tree*> newTrees = buildTrees(dists, treeNum, newTmap); //also creates .all.tre file containing the trees created
+ vector<Tree*> newTrees = buildTrees(dists, treeNum, newCt); //also creates .all.tre file containing the trees created
if (m->control_pressed) { return 0; }
Tree* conTree = con.getTree(newTrees);
//create a new filename
- string conFile = outputDir + m->getRootName(m->getSimpleName(treefile)) + toString(treeNum+1) + ".unweighted.cons." + getOutputFileNameTag("tree");
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(treefile));
+ variables["[tag]"] = toString(treeNum+1);
+ variables["[tag2]"] = "unweighted.cons";
+ string conFile = getOutputFileName("tree",variables);
outputNames.push_back(conFile); outputTypes["tree"].push_back(conFile);
ofstream outTree;
m->openOutputFile(conFile, outTree);
}
/**************************************************************************************************/
-vector<Tree*> UnifracUnweightedCommand::buildTrees(vector< vector<double> >& dists, int treeNum, TreeMap& mytmap) {
+vector<Tree*> UnifracUnweightedCommand::buildTrees(vector< vector<double> >& dists, int treeNum, CountTable& myct) {
try {
vector<Tree*> trees;
//create a new filename
- string outputFile = outputDir + m->getRootName(m->getSimpleName(treefile)) + toString(treeNum+1) + ".unweighted.all." + getOutputFileNameTag("tree");
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(treefile));
+ variables["[tag]"] = toString(treeNum+1);
+ variables["[tag2]"] = "unweighted.all";
+ string outputFile = getOutputFileName("tree",variables);
outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
ofstream outAll;
}
//create tree
- Tree* tempTree = new Tree(&mytmap, sims);
- map<string, string> empty;
- tempTree->assembleTree(empty);
+ Tree* tempTree = new Tree(&myct, sims);
+ tempTree->assembleTree();
trees.push_back(tempTree);
void UnifracUnweightedCommand::createPhylipFile(int i) {
try {
string phylipFileName;
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getSimpleName(treefile);
+ variables["[tag]"] = toString(i+1);
if ((outputForm == "lt") || (outputForm == "square")) {
- phylipFileName = outputDir + m->getSimpleName(treefile) + toString(i+1) + ".unweighted.phylip." + getOutputFileNameTag("phylip");
+ variables["[tag2]"] = "unweighted.phylip";
+ phylipFileName = getOutputFileName("phylip",variables);
outputNames.push_back(phylipFileName); outputTypes["phylip"].push_back(phylipFileName);
}else { //column
- phylipFileName = outputDir + m->getSimpleName(treefile) + toString(i+1) + ".unweighted.column." + getOutputFileNameTag("column");
+ variables["[tag2]"] = "unweighted.column";
+ phylipFileName = getOutputFileName("column",variables);
outputNames.push_back(phylipFileName); outputTypes["column"].push_back(phylipFileName);
}