X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=subsamplecommand.cpp;h=35441013919fbfe55452532e735b5bd1d7673f2f;hb=ce8794490ab1d83adcdb2b92e0302a1e43e17adf;hp=6b0e4f991f01f7d3a6e323f2670819e00ded79f9;hpb=c69e2e9749626cfbf1d6cb0125ae94f869e00b18;p=mothur.git diff --git a/subsamplecommand.cpp b/subsamplecommand.cpp index 6b0e4f9..3544101 100644 --- a/subsamplecommand.cpp +++ b/subsamplecommand.cpp @@ -9,75 +9,118 @@ #include "subsamplecommand.h" #include "sharedutilities.h" +#include "deconvolutecommand.h" +#include "subsample.h" //********************************************************************************************************************** -vector SubSampleCommand::getValidParameters(){ - try { - string Array[] = {"fasta", "group", "list","shared","rabund","persample", "name","sabund","size","groups","label","outputdir","inputdir"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); +vector SubSampleCommand::setParameters(){ + try { + CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "FLSSR", "none",false,false); parameters.push_back(pfasta); + CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname); + CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup); + CommandParameter plist("list", "InputTypes", "", "", "none", "FLSSR", "none",false,false); parameters.push_back(plist); + CommandParameter pshared("shared", "InputTypes", "", "", "none", "FLSSR", "none",false,false); parameters.push_back(pshared); + CommandParameter prabund("rabund", "InputTypes", "", "", "none", "FLSSR", "none",false,false); parameters.push_back(prabund); + CommandParameter psabund("sabund", "InputTypes", "", "", "none", "FLSSR", "none",false,false); parameters.push_back(psabund); + CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel); + CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups); + CommandParameter psize("size", "Number", "", "0", "", "", "",false,false); parameters.push_back(psize); + CommandParameter ppersample("persample", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(ppersample); + CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir); + CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir); + + vector myArray; + for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); } return myArray; } catch(exception& e) { - m->errorOut(e, "SubSampleCommand", "getValidParameters"); + m->errorOut(e, "SubSampleCommand", "setParameters"); exit(1); } } //********************************************************************************************************************** -SubSampleCommand::SubSampleCommand(){ +string SubSampleCommand::getHelpString(){ try { - abort = true; calledHelp = true; - vector tempOutNames; - outputTypes["shared"] = tempOutNames; - outputTypes["list"] = tempOutNames; - outputTypes["rabund"] = tempOutNames; - outputTypes["sabund"] = tempOutNames; - outputTypes["fasta"] = tempOutNames; - outputTypes["name"] = tempOutNames; - outputTypes["group"] = tempOutNames; + string helpString = ""; + helpString += "The sub.sample command is designed to be used as a way to normalize your data, or create a smaller set from your original set.\n"; + helpString += "The sub.sample command parameters are fasta, name, list, group, rabund, sabund, shared, groups, size, persample and label. You must provide a fasta, list, sabund, rabund or shared file as an input file.\n"; + helpString += "The namefile is only used with the fasta file, not with the listfile, because the list file should contain all sequences.\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 size parameter allows you indicate the size of your subsample.\n"; + helpString += "The persample parameter allows you indicate you want to select subsample of the same size from each of your groups, default=false. It is only used with the list and fasta files if a groupfile is given.\n"; + helpString += "persample=false will select a random set of sequences of the size you select, but the number of seqs from each group may differ.\n"; + helpString += "The size parameter is not set: with shared file size=number of seqs in smallest sample, with all other files if a groupfile is given and persample=true, then size=number of seqs in smallest sample, otherwise size=10% of number of seqs.\n"; + helpString += "The sub.sample command should be in the following format: sub.sample(list=yourListFile, group=yourGroupFile, groups=yourGroups, label=yourLabels).\n"; + helpString += "Example sub.sample(list=abrecovery.fn.list, group=abrecovery.groups, groups=B-C, size=20).\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 sub.sample command outputs a .subsample file.\n"; + helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n"; + return helpString; } catch(exception& e) { - m->errorOut(e, "SubSampleCommand", "GetRelAbundCommand"); + m->errorOut(e, "SubSampleCommand", "getHelpString"); exit(1); } } //********************************************************************************************************************** -vector SubSampleCommand::getRequiredParameters(){ +string SubSampleCommand::getOutputFileNameTag(string type, string inputName=""){ try { - string Array[] = {"fasta","list","shared","rabund", "sabund","or"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); - return myArray; + 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 == "fasta") { outputFileName = "subsample" + m->getExtension(inputName); } + else if (type == "sabund") { outputFileName = "subsample" + m->getExtension(inputName); } + else if (type == "name") { outputFileName = "subsample" + m->getExtension(inputName); } + else if (type == "group") { outputFileName = "subsample" + m->getExtension(inputName); } + else if (type == "list") { outputFileName = "subsample" + m->getExtension(inputName); } + else if (type == "rabund") { outputFileName = "subsample" + m->getExtension(inputName); } + else if (type == "shared") { outputFileName = "subsample" + m->getExtension(inputName); } + 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, "SubSampleCommand", "getRequiredParameters"); + m->errorOut(e, "SubSampleCommand", "getOutputFileNameTag"); exit(1); } } + //********************************************************************************************************************** -vector SubSampleCommand::getRequiredFiles(){ +SubSampleCommand::SubSampleCommand(){ try { - vector myArray; - return myArray; + abort = true; calledHelp = true; + setParameters(); + vector tempOutNames; + outputTypes["shared"] = tempOutNames; + outputTypes["list"] = tempOutNames; + outputTypes["rabund"] = tempOutNames; + outputTypes["sabund"] = tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["name"] = tempOutNames; + outputTypes["group"] = tempOutNames; } catch(exception& e) { - m->errorOut(e, "SubSampleCommand", "getRequiredFiles"); + m->errorOut(e, "SubSampleCommand", "GetRelAbundCommand"); exit(1); } } //********************************************************************************************************************** SubSampleCommand::SubSampleCommand(string option) { try { - globaldata = GlobalData::getInstance(); abort = false; calledHelp = false; allLines = 1; - labels.clear(); //allow user to run help if(option == "help") { help(); abort = true; calledHelp = true; } + else if(option == "citation") { citation(); abort = true; calledHelp = true;} else { - //valid paramters for this command - string Array[] = {"fasta", "group", "list","shared","rabund","persample", "sabund","name","size","groups","label","outputdir","inputdir"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + vector myArray = setParameters(); OptionParser parser(option); map parameters = parser.getParameters(); @@ -168,32 +211,38 @@ SubSampleCommand::SubSampleCommand(string option) { //check for required parameters listfile = validParameter.validFile(parameters, "list", true); if (listfile == "not open") { listfile = ""; abort = true; } - else if (listfile == "not found") { listfile = ""; } + else if (listfile == "not found") { listfile = ""; } + else { m->setListFile(listfile); } sabundfile = validParameter.validFile(parameters, "sabund", true); if (sabundfile == "not open") { sabundfile = ""; abort = true; } else if (sabundfile == "not found") { sabundfile = ""; } + else { m->setSabundFile(sabundfile); } rabundfile = validParameter.validFile(parameters, "rabund", true); if (rabundfile == "not open") { rabundfile = ""; abort = true; } else if (rabundfile == "not found") { rabundfile = ""; } + else { m->setRabundFile(rabundfile); } fastafile = validParameter.validFile(parameters, "fasta", true); if (fastafile == "not open") { fastafile = ""; abort = true; } else if (fastafile == "not found") { fastafile = ""; } + else { m->setFastaFile(fastafile); } sharedfile = validParameter.validFile(parameters, "shared", true); if (sharedfile == "not open") { sharedfile = ""; abort = true; } else if (sharedfile == "not found") { sharedfile = ""; } + else { m->setSharedFile(sharedfile); } namefile = validParameter.validFile(parameters, "name", true); if (namefile == "not open") { namefile = ""; abort = true; } else if (namefile == "not found") { namefile = ""; } + else { m->setNameFile(namefile); } groupfile = validParameter.validFile(parameters, "group", true); if (groupfile == "not open") { groupfile = ""; abort = true; } else if (groupfile == "not found") { groupfile = ""; } - + else { m->setGroupFile(groupfile); } //check for optional parameter and set defaults // ...at some point should added some additional type checking... @@ -209,11 +258,11 @@ SubSampleCommand::SubSampleCommand(string option) { else { pickedGroups = true; m->splitAtDash(groups, Groups); - globaldata->Groups = Groups; + m->setGroups(Groups); } string temp = validParameter.validFile(parameters, "size", false); if (temp == "not found"){ temp = "0"; } - convert(temp, size); + m->mothurConvert(temp, size); temp = validParameter.validFile(parameters, "persample", false); if (temp == "not found"){ temp = "f"; } persample = m->isTrue(temp); @@ -234,6 +283,10 @@ SubSampleCommand::SubSampleCommand(string option) { if ((groupfile != "") && ((fastafile != "") && (listfile != ""))) { m->mothurOut("A new group file can only be made from the subsample of a listfile or fastafile, not both. Please correct."); m->mothurOutEndLine(); abort = true; } + if ((fastafile != "") && (namefile == "")) { + vector files; files.push_back(fastafile); + parser.getNameFile(files); + } } } @@ -242,37 +295,6 @@ SubSampleCommand::SubSampleCommand(string option) { exit(1); } } - -//********************************************************************************************************************** - -void SubSampleCommand::help(){ - try { - m->mothurOut("The sub.sample command is designed to be used as a way to normalize your data, or create a smaller set from your original set.\n"); - m->mothurOut("The sub.sample command parameters are fasta, name, list, group, rabund, sabund, shared, groups, size, persample and label. You must provide a fasta, list, sabund, rabund or shared file as an input file.\n"); - m->mothurOut("The namefile is only used with the fasta file, not with the listfile, because the list file should contain all sequences.\n"); - m->mothurOut("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"); - m->mothurOut("The label parameter allows you to select what distance levels you would like, and are also separated by dashes.\n"); - m->mothurOut("The size parameter allows you indicate the size of your subsample.\n"); - m->mothurOut("The persample parameter allows you indicate you want to select subsample of the same size from each of your groups, default=false. It is only used with the list and fasta files if a groupfile is given.\n"); - m->mothurOut("persample=false will select a random set of sequences of the size you select, but the number of seqs from each group may differ.\n"); - m->mothurOut("The size parameter is not set: with shared file size=number of seqs in smallest sample, with all other files if a groupfile is given and persample=true, then size=number of seqs in smallest sample, otherwise size=10% of number of seqs.\n"); - m->mothurOut("The sub.sample command should be in the following format: sub.sample(list=yourListFile, group=yourGroupFile, groups=yourGroups, label=yourLabels).\n"); - m->mothurOut("Example sub.sample(list=abrecovery.fn.list, group=abrecovery.groups, groups=B-C, size=20).\n"); - m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n"); - m->mothurOut("The sub.sample command outputs a .subsample file.\n"); - m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n"); - - } - catch(exception& e) { - m->errorOut(e, "SubSampleCommand", "help"); - exit(1); - } -} - -//********************************************************************************************************************** - -SubSampleCommand::~SubSampleCommand(){} - //********************************************************************************************************************** int SubSampleCommand::execute(){ @@ -281,21 +303,58 @@ int SubSampleCommand::execute(){ if (abort == true) { if (calledHelp) { return 0; } return 2; } if (sharedfile != "") { getSubSampleShared(); } - if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); return 0; } } + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); return 0; } } if (listfile != "") { getSubSampleList(); } - if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); return 0; } } + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); return 0; } } if (rabundfile != "") { getSubSampleRabund(); } - if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); return 0; } } + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); return 0; } } if (sabundfile != "") { getSubSampleSabund(); } - if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); return 0; } } + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); return 0; } } if (fastafile != "") { getSubSampleFasta(); } - if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); return 0; } } + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); return 0; } } - + //set fasta file as new current fastafile + string current = ""; + itTypes = outputTypes.find("fasta"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); } + } + + itTypes = outputTypes.find("name"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); } + } + + itTypes = outputTypes.find("group"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); } + } + + itTypes = outputTypes.find("list"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); } + } + + itTypes = outputTypes.find("shared"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); } + } + + itTypes = outputTypes.find("rabund"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); } + } + + itTypes = outputTypes.find("sabund"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); } + } + + m->mothurOutEndLine(); m->mothurOut("Output File Names: "); m->mothurOutEndLine(); for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); } @@ -323,7 +382,8 @@ int SubSampleCommand::getSubSampleFasta() { //takes care of user setting groupNames that are invalid or setting groups=all SharedUtil* util = new SharedUtil(); - util->setGroups(Groups, groupMap->namesOfGroups); + vector namesGroups = groupMap->getNamesOfGroups(); + util->setGroups(Groups, namesGroups); delete util; //file mismatch quit @@ -337,13 +397,6 @@ int SubSampleCommand::getSubSampleFasta() { if (m->control_pressed) { return 0; } - string thisOutputDir = outputDir; - if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); } - string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "subsample" + m->getExtension(fastafile); - - ofstream out; - m->openOutputFile(outputFileName, out); - outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName); //make sure that if your picked groups size is not too big int thisSize = names.size(); @@ -356,13 +409,14 @@ int SubSampleCommand::getSubSampleFasta() { if (thisSize < size) { size = thisSize; } } }else { //make sure size is not too large - int smallestSize = groupMap->getNumSeqs(Groups[0]); - for (int i = 1; i < Groups.size(); i++) { + vector newGroups; + for (int i = 0; i < Groups.size(); i++) { int thisSize = groupMap->getNumSeqs(Groups[i]); - if (thisSize < smallestSize) { smallestSize = thisSize; } + if (thisSize >= size) { newGroups.push_back(Groups[i]); } + else { m->mothurOut("You have selected a size that is larger than " + Groups[i] + " number of sequences, removing " + Groups[i] + "."); m->mothurOutEndLine(); } } - if (smallestSize < size) { size = smallestSize; m->mothurOut("You have selected a size that is larger than your smallest sample, using your samllest sample size, " + toString(smallestSize) + "."); m->mothurOutEndLine(); } + Groups = newGroups; } m->mothurOut("Sampling " + toString(size) + " from each group."); m->mothurOutEndLine(); @@ -402,61 +456,60 @@ int SubSampleCommand::getSubSampleFasta() { set subset; //dont want repeat sequence names added if (persample) { - for (int i = 0; i < Groups.size(); i++) { - - //randomly select a subset of those names from this group to include in the subsample - for (int j = 0; j < size; j++) { - - if (m->control_pressed) { return 0; } + //initialize counts + map groupCounts; + map::iterator itGroupCounts; + for (int i = 0; i < Groups.size(); i++) { groupCounts[Groups[i]] = 0; } + + for (int j = 0; j < names.size(); j++) { - //get random sequence to add, making sure we have not already added it - bool done = false; - int myrand; - while (!done) { - myrand = (int)((float)(rand()) / (RAND_MAX / (thisSize-1) + 1)); - - if (subset.count(names[myrand]) == 0) { - - string group = groupMap->getGroup(names[myrand]); - if (group == "not found") { m->mothurOut("[ERROR]: " + names[myrand] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; } - - if (group == Groups[i]) { subset.insert(names[myrand]); break; } - } + if (m->control_pressed) { return 0; } + + string group = groupMap->getGroup(names[j]); + if (group == "not found") { m->mothurOut("[ERROR]: " + names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; } + else{ + itGroupCounts = groupCounts.find(group); + if (itGroupCounts != groupCounts.end()) { + if (groupCounts[group] < size) { subset.insert(names[j]); groupCounts[group]++; } } - } + } } }else { + //randomly select a subset of those names to include in the subsample - for (int j = 0; j < size; j++) { + //since names was randomly shuffled just grab the next one + for (int j = 0; j < names.size(); j++) { if (m->control_pressed) { return 0; } - //get random sequence to add, making sure we have not already added it - bool done = false; - int myrand; - while (!done) { - myrand = (int)((float)(rand()) / (RAND_MAX / (thisSize-1) + 1)); + if (groupfile != "") { //if there is a groupfile given fill in group info + string group = groupMap->getGroup(names[j]); + if (group == "not found") { m->mothurOut("[ERROR]: " + names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; } - if (subset.count(names[myrand]) == 0) { - - if (groupfile != "") { //if there is a groupfile given fill in group info - string group = groupMap->getGroup(names[myrand]); - if (group == "not found") { m->mothurOut("[ERROR]: " + names[myrand] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; } - - if (pickedGroups) { //if hte user picked groups, we only want to keep the names of sequences from those groups - if (m->inUsersGroups(group, Groups)) { - subset.insert(names[myrand]); break; - } - }else{ - subset.insert(names[myrand]); break; - } - }else{ //save everyone, group - subset.insert(names[myrand]); break; - } + if (pickedGroups) { //if hte user picked groups, we only want to keep the names of sequences from those groups + if (m->inUsersGroups(group, Groups)) { + subset.insert(names[j]); + } + }else{ + subset.insert(names[j]); } - } + }else{ //save everyone, group + subset.insert(names[j]); + } + + //do we have enough?? + if (subset.size() == size) { break; } } } + + if (subset.size() == 0) { m->mothurOut("The size you selected is too large, skipping fasta file."); m->mothurOutEndLine(); return 0; } + + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + getOutputFileNameTag("fasta", fastafile); + ofstream out; + m->openOutputFile(outputFileName, out); + //read through fasta file outputting only the names on the subsample list ifstream in; m->openInputFile(fastafile, in); @@ -498,12 +551,42 @@ int SubSampleCommand::getSubSampleFasta() { m->mothurOut("[ERROR]: The subset selected contained " + toString(subset.size()) + " sequences, but I only found " + toString(count) + " of those in the fastafile."); m->mothurOutEndLine(); } + if (namefile != "") { + m->mothurOut("Deconvoluting subsampled fasta file... "); m->mothurOutEndLine(); + + string outputNameFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + getOutputFileNameTag("name", namefile); + //use unique.seqs to create new name and fastafile + string inputString = "fasta=" + outputFileName; + m->mothurOut("/******************************************/"); m->mothurOutEndLine(); + m->mothurOut("Running command: unique.seqs(" + inputString + ")"); m->mothurOutEndLine(); + m->mothurCalling = true; + + Command* uniqueCommand = new DeconvoluteCommand(inputString); + uniqueCommand->execute(); + + map > filenames = uniqueCommand->getOutputFiles(); + + delete uniqueCommand; + m->mothurCalling = false; + + m->renameFile(filenames["name"][0], outputNameFileName); + m->renameFile(filenames["fasta"][0], outputFileName); + + outputTypes["name"].push_back(outputNameFileName); outputNames.push_back(outputNameFileName); + + m->mothurOut("/******************************************/"); m->mothurOutEndLine(); + + m->mothurOut("Done."); m->mothurOutEndLine(); + } + + outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName); + //if a groupfile is provided read through the group file only outputting the names on the subsample list if (groupfile != "") { string groupOutputDir = outputDir; if (outputDir == "") { groupOutputDir += m->hasPath(groupfile); } - string groupOutputFileName = groupOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "subsample" + m->getExtension(groupfile); + string groupOutputFileName = groupOutputDir + m->getRootName(m->getSimpleName(groupfile)) + getOutputFileNameTag("group", groupfile); ofstream outGroup; m->openOutputFile(groupOutputFileName, outGroup); @@ -584,34 +667,13 @@ int SubSampleCommand::getNames() { int SubSampleCommand::readNames() { try { - ifstream in; - m->openInputFile(namefile, in); - - string thisname, repnames; - map >::iterator it; - - while(!in.eof()){ - - if (m->control_pressed) { in.close(); return 0; } - - in >> thisname; m->gobble(in); //read from first column - in >> repnames; //read from second column - - it = nameMap.find(thisname); - if (it == nameMap.end()) { - - vector splitRepNames; - m->splitAtComma(repnames, splitRepNames); - - nameMap[thisname] = splitRepNames; - for (int i = 0; i < splitRepNames.size(); i++) { names.push_back(splitRepNames[i]); } - - }else{ m->mothurOut(thisname + " is already in namesfile. I will use first definition."); m->mothurOutEndLine(); } - - m->gobble(in); - } - in.close(); - + nameMap.clear(); + m->readNames(namefile, nameMap); + + //save names of all sequences + map >::iterator it; + for (it = nameMap.begin(); it != nameMap.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { names.push_back((it->second)[i]); } } + return 0; } @@ -624,14 +686,6 @@ int SubSampleCommand::readNames() { int SubSampleCommand::getSubSampleShared() { try { - string thisOutputDir = outputDir; - if (outputDir == "") { thisOutputDir += m->hasPath(sharedfile); } - string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + "subsample" + m->getExtension(sharedfile); - - ofstream out; - m->openOutputFile(outputFileName, out); - outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName); - InputData* input = new InputData(sharedfile, "sharedfile"); vector lookup = input->getSharedRAbundVectors(); string lastLabel = lookup[0]->getLabel(); @@ -647,19 +701,36 @@ int SubSampleCommand::getSubSampleShared() { if (thisSize < size) { size = thisSize; } } + }else { + m->clearGroups(); + Groups.clear(); + vector temp; + for (int i = 0; i < lookup.size(); i++) { + if (lookup[i]->getNumSeqs() < size) { + m->mothurOut(lookup[i]->getGroup() + " contains " + toString(lookup[i]->getNumSeqs()) + ". Eliminating."); m->mothurOutEndLine(); + delete lookup[i]; + }else { + Groups.push_back(lookup[i]->getGroup()); + temp.push_back(lookup[i]); + } + } + lookup = temp; + m->setGroups(Groups); } - m->mothurOut("Sampling " + toString(size) + " from " + toString(lookup[0]->getNumSeqs()) + "."); m->mothurOutEndLine(); + if (lookup.size() == 0) { m->mothurOut("The size you selected is too large, skipping shared file."); m->mothurOutEndLine(); delete input; return 0; } + + m->mothurOut("Sampling " + toString(size) + " from each group."); m->mothurOutEndLine(); //as long as you are not at the end of the file or done wih the lines you want while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) { - if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; } out.close(); return 0; } + if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; } return 0; } if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){ m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); - processShared(lookup, out); + processShared(lookup); processedLabels.insert(lookup[0]->getLabel()); userLabels.erase(lookup[0]->getLabel()); @@ -673,7 +744,7 @@ int SubSampleCommand::getSubSampleShared() { lookup = input->getSharedRAbundVectors(lastLabel); m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); - processShared(lookup, out); + processShared(lookup); processedLabels.insert(lookup[0]->getLabel()); userLabels.erase(lookup[0]->getLabel()); @@ -691,7 +762,7 @@ int SubSampleCommand::getSubSampleShared() { } - if (m->control_pressed) { out.close(); return 0; } + if (m->control_pressed) { return 0; } //output error messages about any remaining user labels set::iterator it; @@ -713,13 +784,12 @@ int SubSampleCommand::getSubSampleShared() { m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); - processShared(lookup, out); + processShared(lookup); for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } } delete input; - out.close(); return 0; @@ -730,58 +800,37 @@ int SubSampleCommand::getSubSampleShared() { } } //********************************************************************************************************************** -int SubSampleCommand::processShared(vector& thislookup, ofstream& out) { +int SubSampleCommand::processShared(vector& thislookup) { try { - int numBins = thislookup[0]->getNumBins(); - for (int i = 0; i < thislookup.size(); i++) { - int thisSize = thislookup[i]->getNumSeqs(); - - if (thisSize != size) { - - string thisgroup = thislookup[i]->getGroup(); - - OrderVector* order = new OrderVector(); - for(int p=0;pgetAbundance(p);j++){ - order->push_back(p); - } - } - random_shuffle(order->begin(), order->end()); - - SharedRAbundVector* temp = new SharedRAbundVector(numBins); - temp->setLabel(thislookup[i]->getLabel()); - temp->setGroup(thislookup[i]->getGroup()); - - delete thislookup[i]; - thislookup[i] = temp; - - - for (int j = 0; j < size; j++) { - - if (m->control_pressed) { delete order; return 0; } - - //get random number to sample from order between 0 and thisSize-1. - int myrand = (int)((float)(rand()) / (RAND_MAX / (thisSize-1) + 1)); - - int bin = order->get(myrand); - - int abund = thislookup[i]->getAbundance(bin); - thislookup[i]->set(bin, (abund+1), thisgroup); - } - delete order; - } - } + //save mothurOut's binLabels to restore for next label + vector saveBinLabels = m->currentBinLabels; - //subsampling may have created some otus with no sequences in them - eliminateZeroOTUS(thislookup); + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(sharedfile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + thislookup[0]->getLabel() + getOutputFileNameTag("shared", sharedfile); + SubSample sample; + vector subsampledLabels = sample.getSample(thislookup, size); + + if (m->control_pressed) { return 0; } + + ofstream out; + m->openOutputFile(outputFileName, out); + outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName); - if (m->control_pressed) { return 0; } + m->currentBinLabels = subsampledLabels; + + thislookup[0]->printHeaders(out); for (int i = 0; i < thislookup.size(); i++) { out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t'; thislookup[i]->print(out); } + out.close(); + + + //save mothurOut's binLabels to restore for next label + m->currentBinLabels = saveBinLabels; return 0; @@ -797,8 +846,7 @@ int SubSampleCommand::getSubSampleList() { string thisOutputDir = outputDir; if (outputDir == "") { thisOutputDir += m->hasPath(listfile); } - string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "subsample" + m->getExtension(listfile); - + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + getOutputFileNameTag("list", listfile); ofstream out; m->openOutputFile(outputFileName, out); outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName); @@ -820,7 +868,8 @@ int SubSampleCommand::getSubSampleList() { //takes care of user setting groupNames that are invalid or setting groups=all SharedUtil* util = new SharedUtil(); - util->setGroups(Groups, groupMap->namesOfGroups); + vector namesGroups = groupMap->getNamesOfGroups(); + util->setGroups(Groups, namesGroups); delete util; //create outputfiles @@ -854,13 +903,14 @@ int SubSampleCommand::getSubSampleList() { if (thisSize < size) { size = thisSize; } } }else { //make sure size is not too large - int smallestSize = groupMap->getNumSeqs(Groups[0]); - for (int i = 1; i < Groups.size(); i++) { + vector newGroups; + for (int i = 0; i < Groups.size(); i++) { int thisSize = groupMap->getNumSeqs(Groups[i]); - if (thisSize < smallestSize) { smallestSize = thisSize; } + if (thisSize >= size) { newGroups.push_back(Groups[i]); } + else { m->mothurOut("You have selected a size that is larger than " + Groups[i] + " number of sequences, removing " + Groups[i] + "."); m->mothurOutEndLine(); } } - if (smallestSize < size) { size = smallestSize; m->mothurOut("You have selected a size that is larger than your smallest sample, using your samllest sample size, " + toString(smallestSize) + "."); m->mothurOutEndLine(); } + Groups = newGroups; } m->mothurOut("Sampling " + toString(size) + " from each group."); m->mothurOutEndLine(); @@ -949,37 +999,30 @@ int SubSampleCommand::getSubSampleList() { //randomly select a subset of those names to include in the subsample set subset; //dont want repeat sequence names added if (persample) { - for (int i = 0; i < Groups.size(); i++) { + //initialize counts + map groupCounts; + map::iterator itGroupCounts; + for (int i = 0; i < Groups.size(); i++) { groupCounts[Groups[i]] = 0; } + + for (int j = 0; j < names.size(); j++) { - for (int j = 0; j < size; j++) { - - if (m->control_pressed) { break; } - - //get random sequence to add, making sure we have not already added it - bool done = false; - int myrand; - while (!done) { - myrand = (int)((float)(rand()) / (RAND_MAX / (names.size()-1) + 1)); - - if (subset.count(names[myrand]) == 0) { //you are not already added - if (groupMap->getGroup(names[myrand]) == Groups[i]) { subset.insert(names[myrand]); break; } - } + if (m->control_pressed) { return 0; } + + string group = groupMap->getGroup(names[j]); + if (group == "not found") { m->mothurOut("[ERROR]: " + names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; } + else{ + itGroupCounts = groupCounts.find(group); + if (itGroupCounts != groupCounts.end()) { + if (groupCounts[group] < size) { subset.insert(names[j]); groupCounts[group]++; } } - } + } } }else{ for (int j = 0; j < size; j++) { if (m->control_pressed) { break; } - //get random sequence to add, making sure we have not already added it - bool done = false; - int myrand; - while (!done) { - myrand = (int)((float)(rand()) / (RAND_MAX / (names.size()-1) + 1)); - - if (subset.count(names[myrand]) == 0) { subset.insert(names[myrand]); break; } - } + subset.insert(names[j]); } } @@ -1103,7 +1146,7 @@ int SubSampleCommand::processList(ListVector*& list, ofstream& out, set& individual += binnames[j]; } } - if (subset.count(individual) != 0) { newNames += individual; } + if (subset.count(individual) != 0) { newNames += individual + ","; } //if there are names in this bin add to new list @@ -1131,15 +1174,6 @@ int SubSampleCommand::processList(ListVector*& list, ofstream& out, set& //********************************************************************************************************************** int SubSampleCommand::getSubSampleRabund() { try { - - string thisOutputDir = outputDir; - if (outputDir == "") { thisOutputDir += m->hasPath(rabundfile); } - string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(rabundfile)) + "subsample" + m->getExtension(rabundfile); - - ofstream out; - m->openOutputFile(outputFileName, out); - outputTypes["rabund"].push_back(outputFileName); outputNames.push_back(outputFileName); - InputData* input = new InputData(rabundfile, "rabund"); RAbundVector* rabund = input->getRAbundVector(); string lastLabel = rabund->getLabel(); @@ -1150,10 +1184,17 @@ int SubSampleCommand::getSubSampleRabund() { if (size == 0) { //user has not set size, set size = 10% size = int((rabund->getNumSeqs()) * 0.10); - } + }else if (size > rabund->getNumSeqs()) { m->mothurOut("The size you selected is too large, skipping rabund file."); m->mothurOutEndLine(); delete input; delete rabund; return 0; } m->mothurOut("Sampling " + toString(size) + " from " + toString(rabund->getNumSeqs()) + "."); m->mothurOutEndLine(); + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(rabundfile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(rabundfile)) + getOutputFileNameTag("rabund", rabundfile); + ofstream out; + m->openOutputFile(outputFileName, out); + outputTypes["rabund"].push_back(outputFileName); outputNames.push_back(outputFileName); + //as long as you are not at the end of the file or done wih the lines you want while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) { if (m->control_pressed) { delete input; delete rabund; out.close(); return 0; } @@ -1261,10 +1302,7 @@ int SubSampleCommand::processRabund(RAbundVector*& rabund, ofstream& out) { if (m->control_pressed) { delete order; return 0; } - //get random number to sample from order between 0 and thisSize-1. - int myrand = (int)((float)(rand()) / (RAND_MAX / (thisSize-1) + 1)); - - int bin = order->get(myrand); + int bin = order->get(j); int abund = rabund->get(bin); rabund->set(bin, (abund+1)); @@ -1288,15 +1326,7 @@ int SubSampleCommand::processRabund(RAbundVector*& rabund, ofstream& out) { //********************************************************************************************************************** int SubSampleCommand::getSubSampleSabund() { try { - - string thisOutputDir = outputDir; - if (outputDir == "") { thisOutputDir += m->hasPath(sabundfile); } - string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sabundfile)) + "subsample" + m->getExtension(sabundfile); - - ofstream out; - m->openOutputFile(outputFileName, out); - outputTypes["sabund"].push_back(outputFileName); outputNames.push_back(outputFileName); - + InputData* input = new InputData(sabundfile, "sabund"); SAbundVector* sabund = input->getSAbundVector(); string lastLabel = sabund->getLabel(); @@ -1307,10 +1337,19 @@ int SubSampleCommand::getSubSampleSabund() { if (size == 0) { //user has not set size, set size = 10% size = int((sabund->getNumSeqs()) * 0.10); - } + }else if (size > sabund->getNumSeqs()) { m->mothurOut("The size you selected is too large, skipping sabund file."); m->mothurOutEndLine(); delete input; delete sabund; return 0; } + m->mothurOut("Sampling " + toString(size) + " from " + toString(sabund->getNumSeqs()) + "."); m->mothurOutEndLine(); + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(sabundfile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sabundfile)) + getOutputFileNameTag("sabund", sabundfile); + ofstream out; + m->openOutputFile(outputFileName, out); + outputTypes["sabund"].push_back(outputFileName); outputNames.push_back(outputFileName); + + //as long as you are not at the end of the file or done wih the lines you want while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) { if (m->control_pressed) { delete input; delete sabund; out.close(); return 0; } @@ -1421,10 +1460,7 @@ int SubSampleCommand::processSabund(SAbundVector*& sabund, ofstream& out) { if (m->control_pressed) { delete order; return 0; } - //get random number to sample from order between 0 and thisSize-1. - int myrand = (int)((float)(rand()) / (RAND_MAX / (thisSize-1) + 1)); - - int bin = order->get(myrand); + int bin = order->get(j); int abund = rabund->get(bin); rabund->set(bin, (abund+1)); @@ -1451,50 +1487,6 @@ int SubSampleCommand::processSabund(SAbundVector*& sabund, ofstream& out) { } } //********************************************************************************************************************** -int SubSampleCommand::eliminateZeroOTUS(vector& thislookup) { - try { - - vector newLookup; - for (int i = 0; i < thislookup.size(); i++) { - SharedRAbundVector* temp = new SharedRAbundVector(); - temp->setLabel(thislookup[i]->getLabel()); - temp->setGroup(thislookup[i]->getGroup()); - newLookup.push_back(temp); - } - - //for each bin - for (int i = 0; i < thislookup[0]->getNumBins(); i++) { - if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; } - - //look at each sharedRabund and make sure they are not all zero - bool allZero = true; - for (int j = 0; j < thislookup.size(); j++) { - if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; } - } - - //if they are not all zero add this bin - if (!allZero) { - for (int j = 0; j < thislookup.size(); j++) { - newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup()); - } - } - } - - for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; } - thislookup.clear(); - - thislookup = newLookup; - - return 0; - - } - catch(exception& e) { - m->errorOut(e, "SubSampleCommand", "eliminateZeroOTUS"); - exit(1); - } -} - -//**********************************************************************************************************************