5 * Created by westcott on 10/27/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "subsamplecommand.h"
11 #include "sharedutilities.h"
12 #include "deconvolutecommand.h"
14 //**********************************************************************************************************************
15 vector<string> SubSampleCommand::setParameters(){
17 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "FLSSR", "none",false,false); parameters.push_back(pfasta);
18 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
19 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
20 CommandParameter plist("list", "InputTypes", "", "", "none", "FLSSR", "none",false,false); parameters.push_back(plist);
21 CommandParameter pshared("shared", "InputTypes", "", "", "none", "FLSSR", "none",false,false); parameters.push_back(pshared);
22 CommandParameter prabund("rabund", "InputTypes", "", "", "none", "FLSSR", "none",false,false); parameters.push_back(prabund);
23 CommandParameter psabund("sabund", "InputTypes", "", "", "none", "FLSSR", "none",false,false); parameters.push_back(psabund);
24 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
25 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
26 CommandParameter psize("size", "Number", "", "0", "", "", "",false,false); parameters.push_back(psize);
27 CommandParameter ppersample("persample", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(ppersample);
28 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
29 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
31 vector<string> myArray;
32 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
36 m->errorOut(e, "SubSampleCommand", "setParameters");
40 //**********************************************************************************************************************
41 string SubSampleCommand::getHelpString(){
43 string helpString = "";
44 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";
45 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";
46 helpString += "The namefile is only used with the fasta file, not with the listfile, because the list file should contain all sequences.\n";
47 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";
48 helpString += "The label parameter allows you to select what distance levels you would like, and are also separated by dashes.\n";
49 helpString += "The size parameter allows you indicate the size of your subsample.\n";
50 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";
51 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";
52 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";
53 helpString += "The sub.sample command should be in the following format: sub.sample(list=yourListFile, group=yourGroupFile, groups=yourGroups, label=yourLabels).\n";
54 helpString += "Example sub.sample(list=abrecovery.fn.list, group=abrecovery.groups, groups=B-C, size=20).\n";
55 helpString += "The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n";
56 helpString += "The sub.sample command outputs a .subsample file.\n";
57 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n";
61 m->errorOut(e, "SubSampleCommand", "getHelpString");
65 //**********************************************************************************************************************
66 SubSampleCommand::SubSampleCommand(){
68 abort = true; calledHelp = true;
70 vector<string> tempOutNames;
71 outputTypes["shared"] = tempOutNames;
72 outputTypes["list"] = tempOutNames;
73 outputTypes["rabund"] = tempOutNames;
74 outputTypes["sabund"] = tempOutNames;
75 outputTypes["fasta"] = tempOutNames;
76 outputTypes["name"] = tempOutNames;
77 outputTypes["group"] = tempOutNames;
80 m->errorOut(e, "SubSampleCommand", "GetRelAbundCommand");
84 //**********************************************************************************************************************
85 SubSampleCommand::SubSampleCommand(string option) {
87 abort = false; calledHelp = false;
90 //allow user to run help
91 if(option == "help") { help(); abort = true; calledHelp = true; }
92 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
95 vector<string> myArray = setParameters();
97 OptionParser parser(option);
98 map<string,string> parameters = parser.getParameters();
100 ValidParameters validParameter;
102 //check to make sure all parameters are valid for command
103 map<string,string>::iterator it;
104 for (it = parameters.begin(); it != parameters.end(); it++) {
105 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
108 //initialize outputTypes
109 vector<string> tempOutNames;
110 outputTypes["shared"] = tempOutNames;
111 outputTypes["list"] = tempOutNames;
112 outputTypes["rabund"] = tempOutNames;
113 outputTypes["sabund"] = tempOutNames;
114 outputTypes["fasta"] = tempOutNames;
115 outputTypes["name"] = tempOutNames;
116 outputTypes["group"] = tempOutNames;
118 //if the user changes the output directory command factory will send this info to us in the output parameter
119 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
121 //if the user changes the input directory command factory will send this info to us in the output parameter
122 string inputDir = validParameter.validFile(parameters, "inputdir", false);
123 if (inputDir == "not found"){ inputDir = ""; }
126 it = parameters.find("list");
127 //user has given a template file
128 if(it != parameters.end()){
129 path = m->hasPath(it->second);
130 //if the user has not given a path then, add inputdir. else leave path alone.
131 if (path == "") { parameters["list"] = inputDir + it->second; }
134 it = parameters.find("fasta");
135 //user has given a template file
136 if(it != parameters.end()){
137 path = m->hasPath(it->second);
138 //if the user has not given a path then, add inputdir. else leave path alone.
139 if (path == "") { parameters["fasta"] = inputDir + it->second; }
142 it = parameters.find("shared");
143 //user has given a template file
144 if(it != parameters.end()){
145 path = m->hasPath(it->second);
146 //if the user has not given a path then, add inputdir. else leave path alone.
147 if (path == "") { parameters["shared"] = inputDir + it->second; }
150 it = parameters.find("group");
151 //user has given a template file
152 if(it != parameters.end()){
153 path = m->hasPath(it->second);
154 //if the user has not given a path then, add inputdir. else leave path alone.
155 if (path == "") { parameters["group"] = inputDir + it->second; }
158 it = parameters.find("sabund");
159 //user has given a template file
160 if(it != parameters.end()){
161 path = m->hasPath(it->second);
162 //if the user has not given a path then, add inputdir. else leave path alone.
163 if (path == "") { parameters["sabund"] = inputDir + it->second; }
166 it = parameters.find("rabund");
167 //user has given a template file
168 if(it != parameters.end()){
169 path = m->hasPath(it->second);
170 //if the user has not given a path then, add inputdir. else leave path alone.
171 if (path == "") { parameters["rabund"] = inputDir + it->second; }
174 it = parameters.find("name");
175 //user has given a template file
176 if(it != parameters.end()){
177 path = m->hasPath(it->second);
178 //if the user has not given a path then, add inputdir. else leave path alone.
179 if (path == "") { parameters["name"] = inputDir + it->second; }
183 //check for required parameters
184 listfile = validParameter.validFile(parameters, "list", true);
185 if (listfile == "not open") { listfile = ""; abort = true; }
186 else if (listfile == "not found") { listfile = ""; }
187 else { m->setListFile(listfile); }
189 sabundfile = validParameter.validFile(parameters, "sabund", true);
190 if (sabundfile == "not open") { sabundfile = ""; abort = true; }
191 else if (sabundfile == "not found") { sabundfile = ""; }
192 else { m->setSabundFile(sabundfile); }
194 rabundfile = validParameter.validFile(parameters, "rabund", true);
195 if (rabundfile == "not open") { rabundfile = ""; abort = true; }
196 else if (rabundfile == "not found") { rabundfile = ""; }
197 else { m->setRabundFile(rabundfile); }
199 fastafile = validParameter.validFile(parameters, "fasta", true);
200 if (fastafile == "not open") { fastafile = ""; abort = true; }
201 else if (fastafile == "not found") { fastafile = ""; }
202 else { m->setFastaFile(fastafile); }
204 sharedfile = validParameter.validFile(parameters, "shared", true);
205 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
206 else if (sharedfile == "not found") { sharedfile = ""; }
207 else { m->setSharedFile(sharedfile); }
209 namefile = validParameter.validFile(parameters, "name", true);
210 if (namefile == "not open") { namefile = ""; abort = true; }
211 else if (namefile == "not found") { namefile = ""; }
212 else { m->setNameFile(namefile); }
214 groupfile = validParameter.validFile(parameters, "group", true);
215 if (groupfile == "not open") { groupfile = ""; abort = true; }
216 else if (groupfile == "not found") { groupfile = ""; }
217 else { m->setGroupFile(groupfile); }
219 //check for optional parameter and set defaults
220 // ...at some point should added some additional type checking...
221 label = validParameter.validFile(parameters, "label", false);
222 if (label == "not found") { label = ""; }
224 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
225 else { allLines = 1; }
228 groups = validParameter.validFile(parameters, "groups", false);
229 if (groups == "not found") { groups = ""; pickedGroups = false; }
232 m->splitAtDash(groups, Groups);
233 m->setGroups(Groups);
236 string temp = validParameter.validFile(parameters, "size", false); if (temp == "not found"){ temp = "0"; }
237 m->mothurConvert(temp, size);
239 temp = validParameter.validFile(parameters, "persample", false); if (temp == "not found"){ temp = "f"; }
240 persample = m->isTrue(temp);
242 if (groupfile == "") { persample = false; }
244 if ((namefile != "") && (fastafile == "")) { m->mothurOut("You may only use a namefile with a fastafile."); m->mothurOutEndLine(); abort = true; }
246 if ((fastafile == "") && (listfile == "") && (sabundfile == "") && (rabundfile == "") && (sharedfile == "")) {
247 m->mothurOut("You must provide a fasta, list, sabund, rabund or shared file as an input file."); m->mothurOutEndLine(); abort = true; }
249 if (pickedGroups && ((groupfile == "") && (sharedfile == ""))) {
250 m->mothurOut("You cannot pick groups without a valid group file or shared file."); m->mothurOutEndLine(); abort = true; }
252 if ((groupfile != "") && ((fastafile == "") && (listfile == ""))) {
253 m->mothurOut("Group file only valid with listfile or fastafile."); m->mothurOutEndLine(); abort = true; }
255 if ((groupfile != "") && ((fastafile != "") && (listfile != ""))) {
256 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; }
258 if ((fastafile != "") && (namefile == "")) {
259 vector<string> files; files.push_back(fastafile);
260 parser.getNameFile(files);
265 catch(exception& e) {
266 m->errorOut(e, "SubSampleCommand", "SubSampleCommand");
270 //**********************************************************************************************************************
272 int SubSampleCommand::execute(){
275 if (abort == true) { if (calledHelp) { return 0; } return 2; }
277 if (sharedfile != "") { getSubSampleShared(); }
278 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); return 0; } }
280 if (listfile != "") { getSubSampleList(); }
281 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); return 0; } }
283 if (rabundfile != "") { getSubSampleRabund(); }
284 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); return 0; } }
286 if (sabundfile != "") { getSubSampleSabund(); }
287 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); return 0; } }
289 if (fastafile != "") { getSubSampleFasta(); }
290 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); return 0; } }
292 //set fasta file as new current fastafile
294 itTypes = outputTypes.find("fasta");
295 if (itTypes != outputTypes.end()) {
296 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
299 itTypes = outputTypes.find("name");
300 if (itTypes != outputTypes.end()) {
301 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
304 itTypes = outputTypes.find("group");
305 if (itTypes != outputTypes.end()) {
306 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
309 itTypes = outputTypes.find("list");
310 if (itTypes != outputTypes.end()) {
311 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
314 itTypes = outputTypes.find("shared");
315 if (itTypes != outputTypes.end()) {
316 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
319 itTypes = outputTypes.find("rabund");
320 if (itTypes != outputTypes.end()) {
321 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
324 itTypes = outputTypes.find("sabund");
325 if (itTypes != outputTypes.end()) {
326 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
330 m->mothurOutEndLine();
331 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
332 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
333 m->mothurOutEndLine();
337 catch(exception& e) {
338 m->errorOut(e, "SubSampleCommand", "execute");
342 //**********************************************************************************************************************
343 int SubSampleCommand::getSubSampleFasta() {
346 if (namefile != "") { readNames(); } //fills names with all names in namefile.
347 else { getNames(); }//no name file, so get list of names to pick from
350 if (groupfile != "") {
352 groupMap = new GroupMap(groupfile);
355 //takes care of user setting groupNames that are invalid or setting groups=all
356 SharedUtil* util = new SharedUtil();
357 vector<string> namesGroups = groupMap->getNamesOfGroups();
358 util->setGroups(Groups, namesGroups);
362 if (names.size() != groupMap->getNumSeqs()) {
363 m->mothurOut("[ERROR]: your fasta file contains " + toString(names.size()) + " sequences, and your groupfile contains " + toString(groupMap->getNumSeqs()) + ", please correct.");
364 m->mothurOutEndLine();
370 if (m->control_pressed) { return 0; }
373 //make sure that if your picked groups size is not too big
374 int thisSize = names.size();
376 if (size == 0) { //user has not set size, set size = smallest samples size
377 size = groupMap->getNumSeqs(Groups[0]);
378 for (int i = 1; i < Groups.size(); i++) {
379 int thisSize = groupMap->getNumSeqs(Groups[i]);
381 if (thisSize < size) { size = thisSize; }
383 }else { //make sure size is not too large
384 vector<string> newGroups;
385 for (int i = 0; i < Groups.size(); i++) {
386 int thisSize = groupMap->getNumSeqs(Groups[i]);
388 if (thisSize >= size) { newGroups.push_back(Groups[i]); }
389 else { m->mothurOut("You have selected a size that is larger than " + Groups[i] + " number of sequences, removing " + Groups[i] + "."); m->mothurOutEndLine(); }
394 m->mothurOut("Sampling " + toString(size) + " from each group."); m->mothurOutEndLine();
398 for(int i = 0; i < Groups.size(); i++) {
399 total += groupMap->getNumSeqs(Groups[i]);
402 if (size == 0) { //user has not set size, set size = 10% samples size
403 size = int (total * 0.10);
408 m->mothurOut("Your size is too large for the number of groups you selected. Adjusting to " + toString(int (total * 0.10)) + "."); m->mothurOutEndLine();
410 size = int (total * 0.10);
413 m->mothurOut("Sampling " + toString(size) + " from " + toString(total) + "."); m->mothurOutEndLine();
416 if (size == 0) { //user has not set size, set size = 10% samples size
417 size = int (names.size() * 0.10);
420 if (size > thisSize) { m->mothurOut("Your fasta file only contains " + toString(thisSize) + " sequences. Setting size to " + toString(thisSize) + "."); m->mothurOutEndLine();
424 if (!pickedGroups) { m->mothurOut("Sampling " + toString(size) + " from " + toString(thisSize) + "."); m->mothurOutEndLine(); }
427 random_shuffle(names.begin(), names.end());
429 set<string> subset; //dont want repeat sequence names added
432 map<string, int> groupCounts;
433 map<string, int>::iterator itGroupCounts;
434 for (int i = 0; i < Groups.size(); i++) { groupCounts[Groups[i]] = 0; }
436 for (int j = 0; j < names.size(); j++) {
438 if (m->control_pressed) { return 0; }
440 string group = groupMap->getGroup(names[j]);
441 if (group == "not found") { m->mothurOut("[ERROR]: " + names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
443 itGroupCounts = groupCounts.find(group);
444 if (itGroupCounts != groupCounts.end()) {
445 if (groupCounts[group] < size) { subset.insert(names[j]); groupCounts[group]++; }
451 //randomly select a subset of those names to include in the subsample
452 //since names was randomly shuffled just grab the next one
453 for (int j = 0; j < names.size(); j++) {
455 if (m->control_pressed) { return 0; }
457 if (groupfile != "") { //if there is a groupfile given fill in group info
458 string group = groupMap->getGroup(names[j]);
459 if (group == "not found") { m->mothurOut("[ERROR]: " + names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
461 if (pickedGroups) { //if hte user picked groups, we only want to keep the names of sequences from those groups
462 if (m->inUsersGroups(group, Groups)) {
463 subset.insert(names[j]);
466 subset.insert(names[j]);
468 }else{ //save everyone, group
469 subset.insert(names[j]);
472 //do we have enough??
473 if (subset.size() == size) { break; }
477 if (subset.size() == 0) { m->mothurOut("The size you selected is too large, skipping fasta file."); m->mothurOutEndLine(); return 0; }
479 string thisOutputDir = outputDir;
480 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
481 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "subsample" + m->getExtension(fastafile);
484 m->openOutputFile(outputFileName, out);
486 //read through fasta file outputting only the names on the subsample list
488 m->openInputFile(fastafile, in);
492 map<string, vector<string> >::iterator itNameMap;
496 if (m->control_pressed) { in.close(); out.close(); return 0; }
498 Sequence currSeq(in);
499 thisname = currSeq.getName();
501 if (thisname != "") {
503 //does the subset contain a sequence that this sequence represents
504 itNameMap = nameMap.find(thisname);
505 if (itNameMap != nameMap.end()) {
506 vector<string> nameRepresents = itNameMap->second;
508 for (int i = 0; i < nameRepresents.size(); i++){
509 if (subset.count(nameRepresents[i]) != 0) {
510 out << ">" << nameRepresents[i] << endl << currSeq.getAligned() << endl;
515 m->mothurOut("[ERROR]: " + thisname + " is not in your namefile, please correct."); m->mothurOutEndLine();
523 if (count != subset.size()) {
524 m->mothurOut("[ERROR]: The subset selected contained " + toString(subset.size()) + " sequences, but I only found " + toString(count) + " of those in the fastafile."); m->mothurOutEndLine();
527 if (namefile != "") {
528 m->mothurOut("Deconvoluting subsampled fasta file... "); m->mothurOutEndLine();
530 //use unique.seqs to create new name and fastafile
531 string inputString = "fasta=" + outputFileName;
532 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
533 m->mothurOut("Running command: unique.seqs(" + inputString + ")"); m->mothurOutEndLine();
535 Command* uniqueCommand = new DeconvoluteCommand(inputString);
536 uniqueCommand->execute();
538 map<string, vector<string> > filenames = uniqueCommand->getOutputFiles();
540 delete uniqueCommand;
542 outputTypes["name"].push_back(filenames["name"][0]); outputNames.push_back(filenames["name"][0]);
543 m->mothurRemove(outputFileName);
544 outputFileName = filenames["fasta"][0];
546 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
548 m->mothurOut("Done."); m->mothurOutEndLine();
551 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
553 //if a groupfile is provided read through the group file only outputting the names on the subsample list
554 if (groupfile != "") {
556 string groupOutputDir = outputDir;
557 if (outputDir == "") { groupOutputDir += m->hasPath(groupfile); }
558 string groupOutputFileName = groupOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "subsample" + m->getExtension(groupfile);
561 m->openOutputFile(groupOutputFileName, outGroup);
562 outputTypes["group"].push_back(groupOutputFileName); outputNames.push_back(groupOutputFileName);
565 m->openInputFile(groupfile, inGroup);
568 while(!inGroup.eof()){
570 if (m->control_pressed) { inGroup.close(); outGroup.close(); return 0; }
572 inGroup >> name; m->gobble(inGroup); //read from first column
573 inGroup >> group; //read from second column
575 //if this name is in the accnos file
576 if (subset.count(name) != 0) {
577 outGroup << name << '\t' << group << endl;
587 if (subset.size() != 0) {
588 m->mothurOut("Your groupfile does not match your fasta file."); m->mothurOutEndLine();
589 for (set<string>::iterator it = subset.begin(); it != subset.end(); it++) {
590 m->mothurOut("[ERROR]: " + *it + " is missing from your groupfile."); m->mothurOutEndLine();
598 catch(exception& e) {
599 m->errorOut(e, "SubSampleCommand", "getSubSampleFasta");
603 //**********************************************************************************************************************
604 int SubSampleCommand::getNames() {
608 m->openInputFile(fastafile, in);
613 if (m->control_pressed) { in.close(); return 0; }
615 Sequence currSeq(in);
616 thisname = currSeq.getName();
618 if (thisname != "") {
619 vector<string> temp; temp.push_back(thisname);
620 nameMap[thisname] = temp;
621 names.push_back(thisname);
630 catch(exception& e) {
631 m->errorOut(e, "SubSampleCommand", "getNames");
635 //**********************************************************************************************************************
636 int SubSampleCommand::readNames() {
640 m->openInputFile(namefile, in);
642 string thisname, repnames;
643 map<string, vector<string> >::iterator it;
647 if (m->control_pressed) { in.close(); return 0; }
649 in >> thisname; m->gobble(in); //read from first column
650 in >> repnames; //read from second column
652 it = nameMap.find(thisname);
653 if (it == nameMap.end()) {
655 vector<string> splitRepNames;
656 m->splitAtComma(repnames, splitRepNames);
658 nameMap[thisname] = splitRepNames;
659 for (int i = 0; i < splitRepNames.size(); i++) { names.push_back(splitRepNames[i]); }
661 }else{ m->mothurOut(thisname + " is already in namesfile. I will use first definition."); m->mothurOutEndLine(); }
670 catch(exception& e) {
671 m->errorOut(e, "SubSampleCommand", "readNames");
675 //**********************************************************************************************************************
676 int SubSampleCommand::getSubSampleShared() {
679 InputData* input = new InputData(sharedfile, "sharedfile");
680 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
681 string lastLabel = lookup[0]->getLabel();
683 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
684 set<string> processedLabels;
685 set<string> userLabels = labels;
687 if (size == 0) { //user has not set size, set size = smallest samples size
688 size = lookup[0]->getNumSeqs();
689 for (int i = 1; i < lookup.size(); i++) {
690 int thisSize = lookup[i]->getNumSeqs();
692 if (thisSize < size) { size = thisSize; }
697 vector<SharedRAbundVector*> temp;
698 for (int i = 0; i < lookup.size(); i++) {
699 if (lookup[i]->getNumSeqs() < size) {
700 m->mothurOut(lookup[i]->getGroup() + " contains " + toString(lookup[i]->getNumSeqs()) + ". Eliminating."); m->mothurOutEndLine();
703 Groups.push_back(lookup[i]->getGroup());
704 temp.push_back(lookup[i]);
708 m->setGroups(Groups);
711 if (lookup.size() == 0) { m->mothurOut("The size you selected is too large, skipping shared file."); m->mothurOutEndLine(); delete input; return 0; }
713 m->mothurOut("Sampling " + toString(size) + " from each group."); m->mothurOutEndLine();
715 //as long as you are not at the end of the file or done wih the lines you want
716 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
717 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; } return 0; }
719 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
721 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
723 processShared(lookup);
725 processedLabels.insert(lookup[0]->getLabel());
726 userLabels.erase(lookup[0]->getLabel());
729 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
730 string saveLabel = lookup[0]->getLabel();
732 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
734 lookup = input->getSharedRAbundVectors(lastLabel);
735 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
737 processShared(lookup);
739 processedLabels.insert(lookup[0]->getLabel());
740 userLabels.erase(lookup[0]->getLabel());
742 //restore real lastlabel to save below
743 lookup[0]->setLabel(saveLabel);
746 lastLabel = lookup[0]->getLabel();
747 //prevent memory leak
748 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
750 //get next line to process
751 lookup = input->getSharedRAbundVectors();
755 if (m->control_pressed) { return 0; }
757 //output error messages about any remaining user labels
758 set<string>::iterator it;
759 bool needToRun = false;
760 for (it = userLabels.begin(); it != userLabels.end(); it++) {
761 m->mothurOut("Your file does not include the label " + *it);
762 if (processedLabels.count(lastLabel) != 1) {
763 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
766 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
770 //run last label if you need to
771 if (needToRun == true) {
772 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
773 lookup = input->getSharedRAbundVectors(lastLabel);
775 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
777 processShared(lookup);
779 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
787 catch(exception& e) {
788 m->errorOut(e, "SubSampleCommand", "getSubSampleShared");
792 //**********************************************************************************************************************
793 int SubSampleCommand::processShared(vector<SharedRAbundVector*>& thislookup) {
796 //save mothurOut's binLabels to restore for next label
797 vector<string> saveBinLabels = m->currentBinLabels;
799 string thisOutputDir = outputDir;
800 if (outputDir == "") { thisOutputDir += m->hasPath(sharedfile); }
801 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + thislookup[0]->getLabel() + ".subsample" + m->getExtension(sharedfile);
805 m->openOutputFile(outputFileName, out);
806 outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName);
808 int numBins = thislookup[0]->getNumBins();
809 for (int i = 0; i < thislookup.size(); i++) {
810 int thisSize = thislookup[i]->getNumSeqs();
812 if (thisSize != size) {
814 string thisgroup = thislookup[i]->getGroup();
816 OrderVector* order = new OrderVector();
817 for(int p=0;p<numBins;p++){
818 for(int j=0;j<thislookup[i]->getAbundance(p);j++){
822 random_shuffle(order->begin(), order->end());
824 SharedRAbundVector* temp = new SharedRAbundVector(numBins);
825 temp->setLabel(thislookup[i]->getLabel());
826 temp->setGroup(thislookup[i]->getGroup());
828 delete thislookup[i];
829 thislookup[i] = temp;
832 for (int j = 0; j < size; j++) {
834 if (m->control_pressed) { delete order; out.close(); return 0; }
836 //get random number to sample from order between 0 and thisSize-1.
837 //don't need this because of the random shuffle above
838 //int myrand = int((float)(thisSize) * (float)(rand()) / ((float)RAND_MAX+1.0));
840 int bin = order->get(j);
842 int abund = thislookup[i]->getAbundance(bin);
843 thislookup[i]->set(bin, (abund+1), thisgroup);
849 //subsampling may have created some otus with no sequences in them
850 eliminateZeroOTUS(thislookup);
852 if (m->control_pressed) { out.close(); return 0; }
854 thislookup[0]->printHeaders(out);
856 for (int i = 0; i < thislookup.size(); i++) {
857 out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
858 thislookup[i]->print(out);
863 //save mothurOut's binLabels to restore for next label
864 m->currentBinLabels = saveBinLabels;
869 catch(exception& e) {
870 m->errorOut(e, "SubSampleCommand", "processShared");
874 //**********************************************************************************************************************
875 int SubSampleCommand::getSubSampleList() {
878 string thisOutputDir = outputDir;
879 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
880 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "subsample" + m->getExtension(listfile);
883 m->openOutputFile(outputFileName, out);
884 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
886 InputData* input = new InputData(listfile, "list");
887 ListVector* list = input->getListVector();
888 string lastLabel = list->getLabel();
890 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
891 set<string> processedLabels;
892 set<string> userLabels = labels;
896 if (groupfile != "") {
898 groupMap = new GroupMap(groupfile);
901 //takes care of user setting groupNames that are invalid or setting groups=all
902 SharedUtil* util = new SharedUtil();
903 vector<string> namesGroups = groupMap->getNamesOfGroups();
904 util->setGroups(Groups, namesGroups);
908 string groupOutputDir = outputDir;
909 if (outputDir == "") { groupOutputDir += m->hasPath(groupfile); }
910 string groupOutputFileName = groupOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "subsample" + m->getExtension(groupfile);
912 m->openOutputFile(groupOutputFileName, outGroup);
913 outputTypes["group"].push_back(groupOutputFileName); outputNames.push_back(groupOutputFileName);
916 if (list->getNumSeqs() != groupMap->getNumSeqs()) {
917 m->mothurOut("[ERROR]: your list file contains " + toString(list->getNumSeqs()) + " sequences, and your groupfile contains " + toString(groupMap->getNumSeqs()) + ", please correct.");
918 m->mothurOutEndLine();
928 //make sure that if your picked groups size is not too big
930 if (size == 0) { //user has not set size, set size = smallest samples size
931 size = groupMap->getNumSeqs(Groups[0]);
932 for (int i = 1; i < Groups.size(); i++) {
933 int thisSize = groupMap->getNumSeqs(Groups[i]);
935 if (thisSize < size) { size = thisSize; }
937 }else { //make sure size is not too large
938 vector<string> newGroups;
939 for (int i = 0; i < Groups.size(); i++) {
940 int thisSize = groupMap->getNumSeqs(Groups[i]);
942 if (thisSize >= size) { newGroups.push_back(Groups[i]); }
943 else { m->mothurOut("You have selected a size that is larger than " + Groups[i] + " number of sequences, removing " + Groups[i] + "."); m->mothurOutEndLine(); }
948 m->mothurOut("Sampling " + toString(size) + " from each group."); m->mothurOutEndLine();
952 for(int i = 0; i < Groups.size(); i++) {
953 total += groupMap->getNumSeqs(Groups[i]);
956 if (size == 0) { //user has not set size, set size = 10% samples size
957 size = int (total * 0.10);
961 m->mothurOut("Your size is too large for the number of groups you selected. Adjusting to " + toString(int (total * 0.10)) + "."); m->mothurOutEndLine();
962 size = int (total * 0.10);
965 m->mothurOut("Sampling " + toString(size) + " from " + toString(total) + "."); m->mothurOutEndLine();
968 if (size == 0) { //user has not set size, set size = 10% samples size
969 size = int (list->getNumSeqs() * 0.10);
972 int thisSize = list->getNumSeqs();
973 if (size > thisSize) { m->mothurOut("Your list file only contains " + toString(thisSize) + " sequences. Setting size to " + toString(thisSize) + "."); m->mothurOutEndLine();
977 m->mothurOut("Sampling " + toString(size) + " from " + toString(list->getNumSeqs()) + "."); m->mothurOutEndLine();
983 for (int i = 0; i < list->getNumBins(); i++) {
984 string binnames = list->get(i);
987 string individual = "";
988 int length = binnames.length();
989 for(int j=0;j<length;j++){
990 if(binnames[j] == ','){
992 if (groupfile != "") { //if there is a groupfile given fill in group info
993 string group = groupMap->getGroup(individual);
994 if (group == "not found") { m->mothurOut("[ERROR]: " + individual + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
996 if (pickedGroups) { //if hte user picked groups, we only want to keep the names of sequences from those groups
997 if (m->inUsersGroups(group, Groups)) {
998 names.push_back(individual);
1001 names.push_back(individual);
1003 }else{ //save everyone, group
1004 names.push_back(individual);
1009 individual += binnames[j];
1013 if (groupfile != "") { //if there is a groupfile given fill in group info
1014 string group = groupMap->getGroup(individual);
1015 if (group == "not found") { m->mothurOut("[ERROR]: " + individual + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
1017 if (pickedGroups) { //if hte user picked groups, we only want to keep the names of sequences from those groups
1018 if (m->inUsersGroups(group, Groups)) {
1019 names.push_back(individual);
1022 names.push_back(individual);
1024 }else{ //save everyone, group
1025 names.push_back(individual);
1029 random_shuffle(names.begin(), names.end());
1031 //randomly select a subset of those names to include in the subsample
1032 set<string> subset; //dont want repeat sequence names added
1035 map<string, int> groupCounts;
1036 map<string, int>::iterator itGroupCounts;
1037 for (int i = 0; i < Groups.size(); i++) { groupCounts[Groups[i]] = 0; }
1039 for (int j = 0; j < names.size(); j++) {
1041 if (m->control_pressed) { return 0; }
1043 string group = groupMap->getGroup(names[j]);
1044 if (group == "not found") { m->mothurOut("[ERROR]: " + names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
1046 itGroupCounts = groupCounts.find(group);
1047 if (itGroupCounts != groupCounts.end()) {
1048 if (groupCounts[group] < size) { subset.insert(names[j]); groupCounts[group]++; }
1053 for (int j = 0; j < size; j++) {
1055 if (m->control_pressed) { break; }
1057 subset.insert(names[j]);
1061 if (groupfile != "") {
1062 //write out new groupfile
1063 for (set<string>::iterator it = subset.begin(); it != subset.end(); it++) {
1064 string group = groupMap->getGroup(*it);
1065 if (group == "not found") { group = "NOTFOUND"; }
1067 outGroup << *it << '\t' << group << endl;
1069 outGroup.close(); delete groupMap;
1073 //as long as you are not at the end of the file or done wih the lines you want
1074 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
1076 if (m->control_pressed) { delete list; delete input; out.close(); return 0; }
1078 if(allLines == 1 || labels.count(list->getLabel()) == 1){
1080 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
1082 processList(list, out, subset);
1084 processedLabels.insert(list->getLabel());
1085 userLabels.erase(list->getLabel());
1088 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
1089 string saveLabel = list->getLabel();
1093 list = input->getListVector(lastLabel);
1094 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
1096 processList(list, out, subset);
1098 processedLabels.insert(list->getLabel());
1099 userLabels.erase(list->getLabel());
1101 //restore real lastlabel to save below
1102 list->setLabel(saveLabel);
1105 lastLabel = list->getLabel();
1107 delete list; list = NULL;
1109 //get next line to process
1110 list = input->getListVector();
1114 if (m->control_pressed) { if (list != NULL) { delete list; } delete input; out.close(); return 0; }
1116 //output error messages about any remaining user labels
1117 set<string>::iterator it;
1118 bool needToRun = false;
1119 for (it = userLabels.begin(); it != userLabels.end(); it++) {
1120 m->mothurOut("Your file does not include the label " + *it);
1121 if (processedLabels.count(lastLabel) != 1) {
1122 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
1125 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
1129 //run last label if you need to
1130 if (needToRun == true) {
1131 if (list != NULL) { delete list; }
1133 list = input->getListVector(lastLabel);
1135 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
1137 processList(list, out, subset);
1139 delete list; list = NULL;
1143 if (list != NULL) { delete list; }
1149 catch(exception& e) {
1150 m->errorOut(e, "SubSampleCommand", "getSubSampleList");
1154 //**********************************************************************************************************************
1155 int SubSampleCommand::processList(ListVector*& list, ofstream& out, set<string>& subset) {
1158 int numBins = list->getNumBins();
1160 ListVector* temp = new ListVector();
1161 temp->setLabel(list->getLabel());
1163 for (int i = 0; i < numBins; i++) {
1165 if (m->control_pressed) { break; }
1167 string binnames = list->get(i);
1170 string individual = "";
1171 string newNames = "";
1172 int length = binnames.length();
1173 for(int j=0;j<length;j++){
1174 if(binnames[j] == ','){
1175 if (subset.count(individual) != 0) { newNames += individual + ","; }
1178 individual += binnames[j];
1181 if (subset.count(individual) != 0) { newNames += individual + ","; }
1184 //if there are names in this bin add to new list
1185 if (newNames != "") {
1186 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
1187 temp->push_back(newNames);
1194 if (m->control_pressed) { return 0; }
1201 catch(exception& e) {
1202 m->errorOut(e, "SubSampleCommand", "processList");
1206 //**********************************************************************************************************************
1207 int SubSampleCommand::getSubSampleRabund() {
1209 InputData* input = new InputData(rabundfile, "rabund");
1210 RAbundVector* rabund = input->getRAbundVector();
1211 string lastLabel = rabund->getLabel();
1213 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
1214 set<string> processedLabels;
1215 set<string> userLabels = labels;
1217 if (size == 0) { //user has not set size, set size = 10%
1218 size = int((rabund->getNumSeqs()) * 0.10);
1219 }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; }
1221 m->mothurOut("Sampling " + toString(size) + " from " + toString(rabund->getNumSeqs()) + "."); m->mothurOutEndLine();
1223 string thisOutputDir = outputDir;
1224 if (outputDir == "") { thisOutputDir += m->hasPath(rabundfile); }
1225 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(rabundfile)) + "subsample" + m->getExtension(rabundfile);
1228 m->openOutputFile(outputFileName, out);
1229 outputTypes["rabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
1231 //as long as you are not at the end of the file or done wih the lines you want
1232 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
1233 if (m->control_pressed) { delete input; delete rabund; out.close(); return 0; }
1235 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
1237 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
1239 processRabund(rabund, out);
1241 processedLabels.insert(rabund->getLabel());
1242 userLabels.erase(rabund->getLabel());
1245 if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
1246 string saveLabel = rabund->getLabel();
1250 rabund = input->getRAbundVector(lastLabel);
1251 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
1253 processRabund(rabund, out);
1255 processedLabels.insert(rabund->getLabel());
1256 userLabels.erase(rabund->getLabel());
1258 //restore real lastlabel to save below
1259 rabund->setLabel(saveLabel);
1262 lastLabel = rabund->getLabel();
1264 //prevent memory leak
1265 delete rabund; rabund = NULL;
1267 //get next line to process
1268 rabund = input->getRAbundVector();
1272 if (m->control_pressed) { out.close(); return 0; }
1274 //output error messages about any remaining user labels
1275 set<string>::iterator it;
1276 bool needToRun = false;
1277 for (it = userLabels.begin(); it != userLabels.end(); it++) {
1278 m->mothurOut("Your file does not include the label " + *it);
1279 if (processedLabels.count(lastLabel) != 1) {
1280 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
1283 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
1287 //run last label if you need to
1288 if (needToRun == true) {
1289 if (rabund != NULL) { delete rabund; }
1291 rabund = input->getRAbundVector(lastLabel);
1293 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
1295 processRabund(rabund, out);
1306 catch(exception& e) {
1307 m->errorOut(e, "SubSampleCommand", "getSubSampleRabund");
1311 //**********************************************************************************************************************
1312 int SubSampleCommand::processRabund(RAbundVector*& rabund, ofstream& out) {
1315 int numBins = rabund->getNumBins();
1316 int thisSize = rabund->getNumSeqs();
1318 if (thisSize != size) {
1320 OrderVector* order = new OrderVector();
1321 for(int p=0;p<numBins;p++){
1322 for(int j=0;j<rabund->get(p);j++){
1323 order->push_back(p);
1326 random_shuffle(order->begin(), order->end());
1328 RAbundVector* temp = new RAbundVector(numBins);
1329 temp->setLabel(rabund->getLabel());
1334 for (int j = 0; j < size; j++) {
1336 if (m->control_pressed) { delete order; return 0; }
1338 int bin = order->get(j);
1340 int abund = rabund->get(bin);
1341 rabund->set(bin, (abund+1));
1347 if (m->control_pressed) { return 0; }
1354 catch(exception& e) {
1355 m->errorOut(e, "SubSampleCommand", "processRabund");
1359 //**********************************************************************************************************************
1360 int SubSampleCommand::getSubSampleSabund() {
1363 InputData* input = new InputData(sabundfile, "sabund");
1364 SAbundVector* sabund = input->getSAbundVector();
1365 string lastLabel = sabund->getLabel();
1367 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
1368 set<string> processedLabels;
1369 set<string> userLabels = labels;
1371 if (size == 0) { //user has not set size, set size = 10%
1372 size = int((sabund->getNumSeqs()) * 0.10);
1373 }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; }
1376 m->mothurOut("Sampling " + toString(size) + " from " + toString(sabund->getNumSeqs()) + "."); m->mothurOutEndLine();
1378 string thisOutputDir = outputDir;
1379 if (outputDir == "") { thisOutputDir += m->hasPath(sabundfile); }
1380 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sabundfile)) + "subsample" + m->getExtension(sabundfile);
1383 m->openOutputFile(outputFileName, out);
1384 outputTypes["sabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
1387 //as long as you are not at the end of the file or done wih the lines you want
1388 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
1389 if (m->control_pressed) { delete input; delete sabund; out.close(); return 0; }
1391 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
1393 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
1395 processSabund(sabund, out);
1397 processedLabels.insert(sabund->getLabel());
1398 userLabels.erase(sabund->getLabel());
1401 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
1402 string saveLabel = sabund->getLabel();
1406 sabund = input->getSAbundVector(lastLabel);
1407 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
1409 processSabund(sabund, out);
1411 processedLabels.insert(sabund->getLabel());
1412 userLabels.erase(sabund->getLabel());
1414 //restore real lastlabel to save below
1415 sabund->setLabel(saveLabel);
1418 lastLabel = sabund->getLabel();
1420 //prevent memory leak
1421 delete sabund; sabund = NULL;
1423 //get next line to process
1424 sabund = input->getSAbundVector();
1428 if (m->control_pressed) { out.close(); return 0; }
1430 //output error messages about any remaining user labels
1431 set<string>::iterator it;
1432 bool needToRun = false;
1433 for (it = userLabels.begin(); it != userLabels.end(); it++) {
1434 m->mothurOut("Your file does not include the label " + *it);
1435 if (processedLabels.count(lastLabel) != 1) {
1436 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
1439 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
1443 //run last label if you need to
1444 if (needToRun == true) {
1445 if (sabund != NULL) { delete sabund; }
1447 sabund = input->getSAbundVector(lastLabel);
1449 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
1451 processSabund(sabund, out);
1462 catch(exception& e) {
1463 m->errorOut(e, "SubSampleCommand", "getSubSampleSabund");
1467 //**********************************************************************************************************************
1468 int SubSampleCommand::processSabund(SAbundVector*& sabund, ofstream& out) {
1471 RAbundVector* rabund = new RAbundVector();
1472 *rabund = sabund->getRAbundVector();
1474 int numBins = rabund->getNumBins();
1475 int thisSize = rabund->getNumSeqs();
1477 if (thisSize != size) {
1479 OrderVector* order = new OrderVector();
1480 for(int p=0;p<numBins;p++){
1481 for(int j=0;j<rabund->get(p);j++){
1482 order->push_back(p);
1485 random_shuffle(order->begin(), order->end());
1487 RAbundVector* temp = new RAbundVector(numBins);
1488 temp->setLabel(rabund->getLabel());
1493 for (int j = 0; j < size; j++) {
1495 if (m->control_pressed) { delete order; return 0; }
1497 int bin = order->get(j);
1499 int abund = rabund->get(bin);
1500 rabund->set(bin, (abund+1));
1506 if (m->control_pressed) { return 0; }
1509 sabund = new SAbundVector();
1510 *sabund = rabund->getSAbundVector();
1518 catch(exception& e) {
1519 m->errorOut(e, "SubSampleCommand", "processSabund");
1523 //**********************************************************************************************************************
1524 int SubSampleCommand::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
1527 vector<SharedRAbundVector*> newLookup;
1528 for (int i = 0; i < thislookup.size(); i++) {
1529 SharedRAbundVector* temp = new SharedRAbundVector();
1530 temp->setLabel(thislookup[i]->getLabel());
1531 temp->setGroup(thislookup[i]->getGroup());
1532 newLookup.push_back(temp);
1536 vector<string> newBinLabels;
1537 string snumBins = toString(thislookup[0]->getNumBins());
1538 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
1539 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
1541 //look at each sharedRabund and make sure they are not all zero
1542 bool allZero = true;
1543 for (int j = 0; j < thislookup.size(); j++) {
1544 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
1547 //if they are not all zero add this bin
1549 for (int j = 0; j < thislookup.size(); j++) {
1550 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
1552 //if there is a bin label use it otherwise make one
1553 string binLabel = "Otu";
1554 string sbinNumber = toString(i+1);
1555 if (sbinNumber.length() < snumBins.length()) {
1556 int diff = snumBins.length() - sbinNumber.length();
1557 for (int h = 0; h < diff; h++) { binLabel += "0"; }
1559 binLabel += sbinNumber;
1560 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
1562 newBinLabels.push_back(binLabel);
1566 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
1569 thislookup = newLookup;
1570 m->currentBinLabels = newBinLabels;
1575 catch(exception& e) {
1576 m->errorOut(e, "SubSampleCommand", "eliminateZeroOTUS");
1581 //**********************************************************************************************************************