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"; }
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; }
261 catch(exception& e) {
262 m->errorOut(e, "SubSampleCommand", "SubSampleCommand");
266 //**********************************************************************************************************************
268 int SubSampleCommand::execute(){
271 if (abort == true) { if (calledHelp) { return 0; } return 2; }
273 if (sharedfile != "") { getSubSampleShared(); }
274 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); return 0; } }
276 if (listfile != "") { getSubSampleList(); }
277 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); return 0; } }
279 if (rabundfile != "") { getSubSampleRabund(); }
280 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); return 0; } }
282 if (sabundfile != "") { getSubSampleSabund(); }
283 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); return 0; } }
285 if (fastafile != "") { getSubSampleFasta(); }
286 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); return 0; } }
288 //set fasta file as new current fastafile
290 itTypes = outputTypes.find("fasta");
291 if (itTypes != outputTypes.end()) {
292 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
295 itTypes = outputTypes.find("name");
296 if (itTypes != outputTypes.end()) {
297 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
300 itTypes = outputTypes.find("group");
301 if (itTypes != outputTypes.end()) {
302 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
305 itTypes = outputTypes.find("list");
306 if (itTypes != outputTypes.end()) {
307 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
310 itTypes = outputTypes.find("shared");
311 if (itTypes != outputTypes.end()) {
312 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
315 itTypes = outputTypes.find("rabund");
316 if (itTypes != outputTypes.end()) {
317 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
320 itTypes = outputTypes.find("sabund");
321 if (itTypes != outputTypes.end()) {
322 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
326 m->mothurOutEndLine();
327 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
328 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
329 m->mothurOutEndLine();
333 catch(exception& e) {
334 m->errorOut(e, "SubSampleCommand", "execute");
338 //**********************************************************************************************************************
339 int SubSampleCommand::getSubSampleFasta() {
342 if (namefile != "") { readNames(); } //fills names with all names in namefile.
343 else { getNames(); }//no name file, so get list of names to pick from
346 if (groupfile != "") {
348 groupMap = new GroupMap(groupfile);
351 //takes care of user setting groupNames that are invalid or setting groups=all
352 SharedUtil* util = new SharedUtil();
353 vector<string> namesGroups = groupMap->getNamesOfGroups();
354 util->setGroups(Groups, namesGroups);
358 if (names.size() != groupMap->getNumSeqs()) {
359 m->mothurOut("[ERROR]: your fasta file contains " + toString(names.size()) + " sequences, and your groupfile contains " + toString(groupMap->getNumSeqs()) + ", please correct.");
360 m->mothurOutEndLine();
366 if (m->control_pressed) { return 0; }
369 //make sure that if your picked groups size is not too big
370 int thisSize = names.size();
372 if (size == 0) { //user has not set size, set size = smallest samples size
373 size = groupMap->getNumSeqs(Groups[0]);
374 for (int i = 1; i < Groups.size(); i++) {
375 int thisSize = groupMap->getNumSeqs(Groups[i]);
377 if (thisSize < size) { size = thisSize; }
379 }else { //make sure size is not too large
380 vector<string> newGroups;
381 for (int i = 0; i < Groups.size(); i++) {
382 int thisSize = groupMap->getNumSeqs(Groups[i]);
384 if (thisSize >= size) { newGroups.push_back(Groups[i]); }
385 else { m->mothurOut("You have selected a size that is larger than " + Groups[i] + " number of sequences, removing " + Groups[i] + "."); m->mothurOutEndLine(); }
390 m->mothurOut("Sampling " + toString(size) + " from each group."); m->mothurOutEndLine();
394 for(int i = 0; i < Groups.size(); i++) {
395 total += groupMap->getNumSeqs(Groups[i]);
398 if (size == 0) { //user has not set size, set size = 10% samples size
399 size = int (total * 0.10);
404 m->mothurOut("Your size is too large for the number of groups you selected. Adjusting to " + toString(int (total * 0.10)) + "."); m->mothurOutEndLine();
406 size = int (total * 0.10);
409 m->mothurOut("Sampling " + toString(size) + " from " + toString(total) + "."); m->mothurOutEndLine();
412 if (size == 0) { //user has not set size, set size = 10% samples size
413 size = int (names.size() * 0.10);
416 if (size > thisSize) { m->mothurOut("Your fasta file only contains " + toString(thisSize) + " sequences. Setting size to " + toString(thisSize) + "."); m->mothurOutEndLine();
420 if (!pickedGroups) { m->mothurOut("Sampling " + toString(size) + " from " + toString(thisSize) + "."); m->mothurOutEndLine(); }
423 random_shuffle(names.begin(), names.end());
425 set<string> subset; //dont want repeat sequence names added
428 map<string, int> groupCounts;
429 for (int i = 0; i < Groups.size(); i++) { groupCounts[Groups[i]] = 0; }
431 for (int j = 0; j < names.size(); j++) {
433 if (m->control_pressed) { return 0; }
435 string group = groupMap->getGroup(names[j]);
436 if (group == "not found") { m->mothurOut("[ERROR]: " + names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
438 if (groupCounts[group] < size) { subset.insert(names[j]); }
443 //randomly select a subset of those names to include in the subsample
444 //since names was randomly shuffled just grab the next one
445 for (int j = 0; j < names.size(); j++) {
447 if (m->control_pressed) { return 0; }
449 if (groupfile != "") { //if there is a groupfile given fill in group info
450 string group = groupMap->getGroup(names[j]);
451 if (group == "not found") { m->mothurOut("[ERROR]: " + names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
453 if (pickedGroups) { //if hte user picked groups, we only want to keep the names of sequences from those groups
454 if (m->inUsersGroups(group, Groups)) {
455 subset.insert(names[j]);
458 subset.insert(names[j]);
460 }else{ //save everyone, group
461 subset.insert(names[j]);
464 //do we have enough??
465 if (subset.size() == size) { break; }
469 if (subset.size() == 0) { m->mothurOut("The size you selected is too large, skipping fasta file."); m->mothurOutEndLine(); return 0; }
471 string thisOutputDir = outputDir;
472 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
473 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "subsample" + m->getExtension(fastafile);
476 m->openOutputFile(outputFileName, out);
478 //read through fasta file outputting only the names on the subsample list
480 m->openInputFile(fastafile, in);
484 map<string, vector<string> >::iterator itNameMap;
488 if (m->control_pressed) { in.close(); out.close(); return 0; }
490 Sequence currSeq(in);
491 thisname = currSeq.getName();
493 if (thisname != "") {
495 //does the subset contain a sequence that this sequence represents
496 itNameMap = nameMap.find(thisname);
497 if (itNameMap != nameMap.end()) {
498 vector<string> nameRepresents = itNameMap->second;
500 for (int i = 0; i < nameRepresents.size(); i++){
501 if (subset.count(nameRepresents[i]) != 0) {
502 out << ">" << nameRepresents[i] << endl << currSeq.getAligned() << endl;
507 m->mothurOut("[ERROR]: " + thisname + " is not in your namefile, please correct."); m->mothurOutEndLine();
515 if (count != subset.size()) {
516 m->mothurOut("[ERROR]: The subset selected contained " + toString(subset.size()) + " sequences, but I only found " + toString(count) + " of those in the fastafile."); m->mothurOutEndLine();
519 if (namefile != "") {
520 m->mothurOut("Deconvoluting subsampled fasta file... "); m->mothurOutEndLine();
522 //use unique.seqs to create new name and fastafile
523 string inputString = "fasta=" + outputFileName;
524 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
525 m->mothurOut("Running command: unique.seqs(" + inputString + ")"); m->mothurOutEndLine();
527 Command* uniqueCommand = new DeconvoluteCommand(inputString);
528 uniqueCommand->execute();
530 map<string, vector<string> > filenames = uniqueCommand->getOutputFiles();
532 delete uniqueCommand;
534 outputTypes["name"].push_back(filenames["name"][0]); outputNames.push_back(filenames["name"][0]);
535 m->mothurRemove(outputFileName);
536 outputFileName = filenames["fasta"][0];
538 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
540 m->mothurOut("Done."); m->mothurOutEndLine();
543 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
545 //if a groupfile is provided read through the group file only outputting the names on the subsample list
546 if (groupfile != "") {
548 string groupOutputDir = outputDir;
549 if (outputDir == "") { groupOutputDir += m->hasPath(groupfile); }
550 string groupOutputFileName = groupOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "subsample" + m->getExtension(groupfile);
553 m->openOutputFile(groupOutputFileName, outGroup);
554 outputTypes["group"].push_back(groupOutputFileName); outputNames.push_back(groupOutputFileName);
557 m->openInputFile(groupfile, inGroup);
560 while(!inGroup.eof()){
562 if (m->control_pressed) { inGroup.close(); outGroup.close(); return 0; }
564 inGroup >> name; m->gobble(inGroup); //read from first column
565 inGroup >> group; //read from second column
567 //if this name is in the accnos file
568 if (subset.count(name) != 0) {
569 outGroup << name << '\t' << group << endl;
579 if (subset.size() != 0) {
580 m->mothurOut("Your groupfile does not match your fasta file."); m->mothurOutEndLine();
581 for (set<string>::iterator it = subset.begin(); it != subset.end(); it++) {
582 m->mothurOut("[ERROR]: " + *it + " is missing from your groupfile."); m->mothurOutEndLine();
590 catch(exception& e) {
591 m->errorOut(e, "SubSampleCommand", "getSubSampleFasta");
595 //**********************************************************************************************************************
596 int SubSampleCommand::getNames() {
600 m->openInputFile(fastafile, in);
605 if (m->control_pressed) { in.close(); return 0; }
607 Sequence currSeq(in);
608 thisname = currSeq.getName();
610 if (thisname != "") {
611 vector<string> temp; temp.push_back(thisname);
612 nameMap[thisname] = temp;
613 names.push_back(thisname);
622 catch(exception& e) {
623 m->errorOut(e, "SubSampleCommand", "getNames");
627 //**********************************************************************************************************************
628 int SubSampleCommand::readNames() {
632 m->openInputFile(namefile, in);
634 string thisname, repnames;
635 map<string, vector<string> >::iterator it;
639 if (m->control_pressed) { in.close(); return 0; }
641 in >> thisname; m->gobble(in); //read from first column
642 in >> repnames; //read from second column
644 it = nameMap.find(thisname);
645 if (it == nameMap.end()) {
647 vector<string> splitRepNames;
648 m->splitAtComma(repnames, splitRepNames);
650 nameMap[thisname] = splitRepNames;
651 for (int i = 0; i < splitRepNames.size(); i++) { names.push_back(splitRepNames[i]); }
653 }else{ m->mothurOut(thisname + " is already in namesfile. I will use first definition."); m->mothurOutEndLine(); }
662 catch(exception& e) {
663 m->errorOut(e, "SubSampleCommand", "readNames");
667 //**********************************************************************************************************************
668 int SubSampleCommand::getSubSampleShared() {
671 InputData* input = new InputData(sharedfile, "sharedfile");
672 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
673 string lastLabel = lookup[0]->getLabel();
675 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
676 set<string> processedLabels;
677 set<string> userLabels = labels;
679 if (size == 0) { //user has not set size, set size = smallest samples size
680 size = lookup[0]->getNumSeqs();
681 for (int i = 1; i < lookup.size(); i++) {
682 int thisSize = lookup[i]->getNumSeqs();
684 if (thisSize < size) { size = thisSize; }
689 vector<SharedRAbundVector*> temp;
690 for (int i = 0; i < lookup.size(); i++) {
691 if (lookup[i]->getNumSeqs() < size) {
692 m->mothurOut(lookup[i]->getGroup() + " contains " + toString(lookup[i]->getNumSeqs()) + ". Eliminating."); m->mothurOutEndLine();
695 Groups.push_back(lookup[i]->getGroup());
696 temp.push_back(lookup[i]);
700 m->setGroups(Groups);
703 if (lookup.size() == 0) { m->mothurOut("The size you selected is too large, skipping shared file."); m->mothurOutEndLine(); delete input; return 0; }
705 m->mothurOut("Sampling " + toString(size) + " from each group."); m->mothurOutEndLine();
707 //as long as you are not at the end of the file or done wih the lines you want
708 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
709 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; } return 0; }
711 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
713 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
715 processShared(lookup);
717 processedLabels.insert(lookup[0]->getLabel());
718 userLabels.erase(lookup[0]->getLabel());
721 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
722 string saveLabel = lookup[0]->getLabel();
724 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
726 lookup = input->getSharedRAbundVectors(lastLabel);
727 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
729 processShared(lookup);
731 processedLabels.insert(lookup[0]->getLabel());
732 userLabels.erase(lookup[0]->getLabel());
734 //restore real lastlabel to save below
735 lookup[0]->setLabel(saveLabel);
738 lastLabel = lookup[0]->getLabel();
739 //prevent memory leak
740 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
742 //get next line to process
743 lookup = input->getSharedRAbundVectors();
747 if (m->control_pressed) { return 0; }
749 //output error messages about any remaining user labels
750 set<string>::iterator it;
751 bool needToRun = false;
752 for (it = userLabels.begin(); it != userLabels.end(); it++) {
753 m->mothurOut("Your file does not include the label " + *it);
754 if (processedLabels.count(lastLabel) != 1) {
755 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
758 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
762 //run last label if you need to
763 if (needToRun == true) {
764 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
765 lookup = input->getSharedRAbundVectors(lastLabel);
767 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
769 processShared(lookup);
771 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
779 catch(exception& e) {
780 m->errorOut(e, "SubSampleCommand", "getSubSampleShared");
784 //**********************************************************************************************************************
785 int SubSampleCommand::processShared(vector<SharedRAbundVector*>& thislookup) {
788 //save mothurOut's binLabels to restore for next label
789 vector<string> saveBinLabels = m->currentBinLabels;
791 string thisOutputDir = outputDir;
792 if (outputDir == "") { thisOutputDir += m->hasPath(sharedfile); }
793 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + thislookup[0]->getLabel() + ".subsample" + m->getExtension(sharedfile);
797 m->openOutputFile(outputFileName, out);
798 outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName);
800 int numBins = thislookup[0]->getNumBins();
801 for (int i = 0; i < thislookup.size(); i++) {
802 int thisSize = thislookup[i]->getNumSeqs();
804 if (thisSize != size) {
806 string thisgroup = thislookup[i]->getGroup();
808 OrderVector* order = new OrderVector();
809 for(int p=0;p<numBins;p++){
810 for(int j=0;j<thislookup[i]->getAbundance(p);j++){
814 random_shuffle(order->begin(), order->end());
816 SharedRAbundVector* temp = new SharedRAbundVector(numBins);
817 temp->setLabel(thislookup[i]->getLabel());
818 temp->setGroup(thislookup[i]->getGroup());
820 delete thislookup[i];
821 thislookup[i] = temp;
824 for (int j = 0; j < size; j++) {
826 if (m->control_pressed) { delete order; out.close(); return 0; }
828 //get random number to sample from order between 0 and thisSize-1.
829 //don't need this because of the random shuffle above
830 //int myrand = int((float)(thisSize) * (float)(rand()) / ((float)RAND_MAX+1.0));
832 int bin = order->get(j);
834 int abund = thislookup[i]->getAbundance(bin);
835 thislookup[i]->set(bin, (abund+1), thisgroup);
841 //subsampling may have created some otus with no sequences in them
842 eliminateZeroOTUS(thislookup);
844 if (m->control_pressed) { out.close(); return 0; }
846 thislookup[0]->printHeaders(out);
848 for (int i = 0; i < thislookup.size(); i++) {
849 out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
850 thislookup[i]->print(out);
855 //save mothurOut's binLabels to restore for next label
856 m->currentBinLabels = saveBinLabels;
861 catch(exception& e) {
862 m->errorOut(e, "SubSampleCommand", "processShared");
866 //**********************************************************************************************************************
867 int SubSampleCommand::getSubSampleList() {
870 string thisOutputDir = outputDir;
871 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
872 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "subsample" + m->getExtension(listfile);
875 m->openOutputFile(outputFileName, out);
876 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
878 InputData* input = new InputData(listfile, "list");
879 ListVector* list = input->getListVector();
880 string lastLabel = list->getLabel();
882 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
883 set<string> processedLabels;
884 set<string> userLabels = labels;
888 if (groupfile != "") {
890 groupMap = new GroupMap(groupfile);
893 //takes care of user setting groupNames that are invalid or setting groups=all
894 SharedUtil* util = new SharedUtil();
895 vector<string> namesGroups = groupMap->getNamesOfGroups();
896 util->setGroups(Groups, namesGroups);
900 string groupOutputDir = outputDir;
901 if (outputDir == "") { groupOutputDir += m->hasPath(groupfile); }
902 string groupOutputFileName = groupOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "subsample" + m->getExtension(groupfile);
904 m->openOutputFile(groupOutputFileName, outGroup);
905 outputTypes["group"].push_back(groupOutputFileName); outputNames.push_back(groupOutputFileName);
908 if (list->getNumSeqs() != groupMap->getNumSeqs()) {
909 m->mothurOut("[ERROR]: your list file contains " + toString(list->getNumSeqs()) + " sequences, and your groupfile contains " + toString(groupMap->getNumSeqs()) + ", please correct.");
910 m->mothurOutEndLine();
920 //make sure that if your picked groups size is not too big
922 if (size == 0) { //user has not set size, set size = smallest samples size
923 size = groupMap->getNumSeqs(Groups[0]);
924 for (int i = 1; i < Groups.size(); i++) {
925 int thisSize = groupMap->getNumSeqs(Groups[i]);
927 if (thisSize < size) { size = thisSize; }
929 }else { //make sure size is not too large
930 vector<string> newGroups;
931 for (int i = 0; i < Groups.size(); i++) {
932 int thisSize = groupMap->getNumSeqs(Groups[i]);
934 if (thisSize >= size) { newGroups.push_back(Groups[i]); }
935 else { m->mothurOut("You have selected a size that is larger than " + Groups[i] + " number of sequences, removing " + Groups[i] + "."); m->mothurOutEndLine(); }
940 m->mothurOut("Sampling " + toString(size) + " from each group."); m->mothurOutEndLine();
944 for(int i = 0; i < Groups.size(); i++) {
945 total += groupMap->getNumSeqs(Groups[i]);
948 if (size == 0) { //user has not set size, set size = 10% samples size
949 size = int (total * 0.10);
953 m->mothurOut("Your size is too large for the number of groups you selected. Adjusting to " + toString(int (total * 0.10)) + "."); m->mothurOutEndLine();
954 size = int (total * 0.10);
957 m->mothurOut("Sampling " + toString(size) + " from " + toString(total) + "."); m->mothurOutEndLine();
960 if (size == 0) { //user has not set size, set size = 10% samples size
961 size = int (list->getNumSeqs() * 0.10);
964 int thisSize = list->getNumSeqs();
965 if (size > thisSize) { m->mothurOut("Your list file only contains " + toString(thisSize) + " sequences. Setting size to " + toString(thisSize) + "."); m->mothurOutEndLine();
969 m->mothurOut("Sampling " + toString(size) + " from " + toString(list->getNumSeqs()) + "."); m->mothurOutEndLine();
975 for (int i = 0; i < list->getNumBins(); i++) {
976 string binnames = list->get(i);
979 string individual = "";
980 int length = binnames.length();
981 for(int j=0;j<length;j++){
982 if(binnames[j] == ','){
984 if (groupfile != "") { //if there is a groupfile given fill in group info
985 string group = groupMap->getGroup(individual);
986 if (group == "not found") { m->mothurOut("[ERROR]: " + individual + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
988 if (pickedGroups) { //if hte user picked groups, we only want to keep the names of sequences from those groups
989 if (m->inUsersGroups(group, Groups)) {
990 names.push_back(individual);
993 names.push_back(individual);
995 }else{ //save everyone, group
996 names.push_back(individual);
1001 individual += binnames[j];
1005 if (groupfile != "") { //if there is a groupfile given fill in group info
1006 string group = groupMap->getGroup(individual);
1007 if (group == "not found") { m->mothurOut("[ERROR]: " + individual + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
1009 if (pickedGroups) { //if hte user picked groups, we only want to keep the names of sequences from those groups
1010 if (m->inUsersGroups(group, Groups)) {
1011 names.push_back(individual);
1014 names.push_back(individual);
1016 }else{ //save everyone, group
1017 names.push_back(individual);
1021 random_shuffle(names.begin(), names.end());
1023 //randomly select a subset of those names to include in the subsample
1024 set<string> subset; //dont want repeat sequence names added
1027 map<string, int> groupCounts;
1028 for (int i = 0; i < Groups.size(); i++) { groupCounts[Groups[i]] = 0; }
1030 for (int j = 0; j < names.size(); j++) {
1032 if (m->control_pressed) { return 0; }
1034 string group = groupMap->getGroup(names[j]);
1035 if (group == "not found") { m->mothurOut("[ERROR]: " + names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
1037 if (groupCounts[group] < size) { subset.insert(names[j]); }
1041 for (int j = 0; j < size; j++) {
1043 if (m->control_pressed) { break; }
1045 subset.insert(names[j]);
1049 if (groupfile != "") {
1050 //write out new groupfile
1051 for (set<string>::iterator it = subset.begin(); it != subset.end(); it++) {
1052 string group = groupMap->getGroup(*it);
1053 if (group == "not found") { group = "NOTFOUND"; }
1055 outGroup << *it << '\t' << group << endl;
1057 outGroup.close(); delete groupMap;
1061 //as long as you are not at the end of the file or done wih the lines you want
1062 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
1064 if (m->control_pressed) { delete list; delete input; out.close(); return 0; }
1066 if(allLines == 1 || labels.count(list->getLabel()) == 1){
1068 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
1070 processList(list, out, subset);
1072 processedLabels.insert(list->getLabel());
1073 userLabels.erase(list->getLabel());
1076 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
1077 string saveLabel = list->getLabel();
1081 list = input->getListVector(lastLabel);
1082 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
1084 processList(list, out, subset);
1086 processedLabels.insert(list->getLabel());
1087 userLabels.erase(list->getLabel());
1089 //restore real lastlabel to save below
1090 list->setLabel(saveLabel);
1093 lastLabel = list->getLabel();
1095 delete list; list = NULL;
1097 //get next line to process
1098 list = input->getListVector();
1102 if (m->control_pressed) { if (list != NULL) { delete list; } delete input; out.close(); return 0; }
1104 //output error messages about any remaining user labels
1105 set<string>::iterator it;
1106 bool needToRun = false;
1107 for (it = userLabels.begin(); it != userLabels.end(); it++) {
1108 m->mothurOut("Your file does not include the label " + *it);
1109 if (processedLabels.count(lastLabel) != 1) {
1110 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
1113 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
1117 //run last label if you need to
1118 if (needToRun == true) {
1119 if (list != NULL) { delete list; }
1121 list = input->getListVector(lastLabel);
1123 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
1125 processList(list, out, subset);
1127 delete list; list = NULL;
1131 if (list != NULL) { delete list; }
1137 catch(exception& e) {
1138 m->errorOut(e, "SubSampleCommand", "getSubSampleList");
1142 //**********************************************************************************************************************
1143 int SubSampleCommand::processList(ListVector*& list, ofstream& out, set<string>& subset) {
1146 int numBins = list->getNumBins();
1148 ListVector* temp = new ListVector();
1149 temp->setLabel(list->getLabel());
1151 for (int i = 0; i < numBins; i++) {
1153 if (m->control_pressed) { break; }
1155 string binnames = list->get(i);
1158 string individual = "";
1159 string newNames = "";
1160 int length = binnames.length();
1161 for(int j=0;j<length;j++){
1162 if(binnames[j] == ','){
1163 if (subset.count(individual) != 0) { newNames += individual + ","; }
1166 individual += binnames[j];
1169 if (subset.count(individual) != 0) { newNames += individual + ","; }
1172 //if there are names in this bin add to new list
1173 if (newNames != "") {
1174 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
1175 temp->push_back(newNames);
1182 if (m->control_pressed) { return 0; }
1189 catch(exception& e) {
1190 m->errorOut(e, "SubSampleCommand", "processList");
1194 //**********************************************************************************************************************
1195 int SubSampleCommand::getSubSampleRabund() {
1197 InputData* input = new InputData(rabundfile, "rabund");
1198 RAbundVector* rabund = input->getRAbundVector();
1199 string lastLabel = rabund->getLabel();
1201 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
1202 set<string> processedLabels;
1203 set<string> userLabels = labels;
1205 if (size == 0) { //user has not set size, set size = 10%
1206 size = int((rabund->getNumSeqs()) * 0.10);
1207 }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; }
1209 m->mothurOut("Sampling " + toString(size) + " from " + toString(rabund->getNumSeqs()) + "."); m->mothurOutEndLine();
1211 string thisOutputDir = outputDir;
1212 if (outputDir == "") { thisOutputDir += m->hasPath(rabundfile); }
1213 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(rabundfile)) + "subsample" + m->getExtension(rabundfile);
1216 m->openOutputFile(outputFileName, out);
1217 outputTypes["rabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
1219 //as long as you are not at the end of the file or done wih the lines you want
1220 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
1221 if (m->control_pressed) { delete input; delete rabund; out.close(); return 0; }
1223 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
1225 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
1227 processRabund(rabund, out);
1229 processedLabels.insert(rabund->getLabel());
1230 userLabels.erase(rabund->getLabel());
1233 if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
1234 string saveLabel = rabund->getLabel();
1238 rabund = input->getRAbundVector(lastLabel);
1239 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
1241 processRabund(rabund, out);
1243 processedLabels.insert(rabund->getLabel());
1244 userLabels.erase(rabund->getLabel());
1246 //restore real lastlabel to save below
1247 rabund->setLabel(saveLabel);
1250 lastLabel = rabund->getLabel();
1252 //prevent memory leak
1253 delete rabund; rabund = NULL;
1255 //get next line to process
1256 rabund = input->getRAbundVector();
1260 if (m->control_pressed) { out.close(); return 0; }
1262 //output error messages about any remaining user labels
1263 set<string>::iterator it;
1264 bool needToRun = false;
1265 for (it = userLabels.begin(); it != userLabels.end(); it++) {
1266 m->mothurOut("Your file does not include the label " + *it);
1267 if (processedLabels.count(lastLabel) != 1) {
1268 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
1271 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
1275 //run last label if you need to
1276 if (needToRun == true) {
1277 if (rabund != NULL) { delete rabund; }
1279 rabund = input->getRAbundVector(lastLabel);
1281 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
1283 processRabund(rabund, out);
1294 catch(exception& e) {
1295 m->errorOut(e, "SubSampleCommand", "getSubSampleRabund");
1299 //**********************************************************************************************************************
1300 int SubSampleCommand::processRabund(RAbundVector*& rabund, ofstream& out) {
1303 int numBins = rabund->getNumBins();
1304 int thisSize = rabund->getNumSeqs();
1306 if (thisSize != size) {
1308 OrderVector* order = new OrderVector();
1309 for(int p=0;p<numBins;p++){
1310 for(int j=0;j<rabund->get(p);j++){
1311 order->push_back(p);
1314 random_shuffle(order->begin(), order->end());
1316 RAbundVector* temp = new RAbundVector(numBins);
1317 temp->setLabel(rabund->getLabel());
1322 for (int j = 0; j < size; j++) {
1324 if (m->control_pressed) { delete order; return 0; }
1326 int bin = order->get(j);
1328 int abund = rabund->get(bin);
1329 rabund->set(bin, (abund+1));
1335 if (m->control_pressed) { return 0; }
1342 catch(exception& e) {
1343 m->errorOut(e, "SubSampleCommand", "processRabund");
1347 //**********************************************************************************************************************
1348 int SubSampleCommand::getSubSampleSabund() {
1351 InputData* input = new InputData(sabundfile, "sabund");
1352 SAbundVector* sabund = input->getSAbundVector();
1353 string lastLabel = sabund->getLabel();
1355 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
1356 set<string> processedLabels;
1357 set<string> userLabels = labels;
1359 if (size == 0) { //user has not set size, set size = 10%
1360 size = int((sabund->getNumSeqs()) * 0.10);
1361 }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; }
1364 m->mothurOut("Sampling " + toString(size) + " from " + toString(sabund->getNumSeqs()) + "."); m->mothurOutEndLine();
1366 string thisOutputDir = outputDir;
1367 if (outputDir == "") { thisOutputDir += m->hasPath(sabundfile); }
1368 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sabundfile)) + "subsample" + m->getExtension(sabundfile);
1371 m->openOutputFile(outputFileName, out);
1372 outputTypes["sabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
1375 //as long as you are not at the end of the file or done wih the lines you want
1376 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
1377 if (m->control_pressed) { delete input; delete sabund; out.close(); return 0; }
1379 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
1381 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
1383 processSabund(sabund, out);
1385 processedLabels.insert(sabund->getLabel());
1386 userLabels.erase(sabund->getLabel());
1389 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
1390 string saveLabel = sabund->getLabel();
1394 sabund = input->getSAbundVector(lastLabel);
1395 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
1397 processSabund(sabund, out);
1399 processedLabels.insert(sabund->getLabel());
1400 userLabels.erase(sabund->getLabel());
1402 //restore real lastlabel to save below
1403 sabund->setLabel(saveLabel);
1406 lastLabel = sabund->getLabel();
1408 //prevent memory leak
1409 delete sabund; sabund = NULL;
1411 //get next line to process
1412 sabund = input->getSAbundVector();
1416 if (m->control_pressed) { out.close(); return 0; }
1418 //output error messages about any remaining user labels
1419 set<string>::iterator it;
1420 bool needToRun = false;
1421 for (it = userLabels.begin(); it != userLabels.end(); it++) {
1422 m->mothurOut("Your file does not include the label " + *it);
1423 if (processedLabels.count(lastLabel) != 1) {
1424 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
1427 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
1431 //run last label if you need to
1432 if (needToRun == true) {
1433 if (sabund != NULL) { delete sabund; }
1435 sabund = input->getSAbundVector(lastLabel);
1437 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
1439 processSabund(sabund, out);
1450 catch(exception& e) {
1451 m->errorOut(e, "SubSampleCommand", "getSubSampleSabund");
1455 //**********************************************************************************************************************
1456 int SubSampleCommand::processSabund(SAbundVector*& sabund, ofstream& out) {
1459 RAbundVector* rabund = new RAbundVector();
1460 *rabund = sabund->getRAbundVector();
1462 int numBins = rabund->getNumBins();
1463 int thisSize = rabund->getNumSeqs();
1465 if (thisSize != size) {
1467 OrderVector* order = new OrderVector();
1468 for(int p=0;p<numBins;p++){
1469 for(int j=0;j<rabund->get(p);j++){
1470 order->push_back(p);
1473 random_shuffle(order->begin(), order->end());
1475 RAbundVector* temp = new RAbundVector(numBins);
1476 temp->setLabel(rabund->getLabel());
1481 for (int j = 0; j < size; j++) {
1483 if (m->control_pressed) { delete order; return 0; }
1485 int bin = order->get(j);
1487 int abund = rabund->get(bin);
1488 rabund->set(bin, (abund+1));
1494 if (m->control_pressed) { return 0; }
1497 sabund = new SAbundVector();
1498 *sabund = rabund->getSAbundVector();
1506 catch(exception& e) {
1507 m->errorOut(e, "SubSampleCommand", "processSabund");
1511 //**********************************************************************************************************************
1512 int SubSampleCommand::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
1515 vector<SharedRAbundVector*> newLookup;
1516 for (int i = 0; i < thislookup.size(); i++) {
1517 SharedRAbundVector* temp = new SharedRAbundVector();
1518 temp->setLabel(thislookup[i]->getLabel());
1519 temp->setGroup(thislookup[i]->getGroup());
1520 newLookup.push_back(temp);
1524 vector<string> newBinLabels;
1525 string snumBins = toString(thislookup[0]->getNumBins());
1526 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
1527 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
1529 //look at each sharedRabund and make sure they are not all zero
1530 bool allZero = true;
1531 for (int j = 0; j < thislookup.size(); j++) {
1532 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
1535 //if they are not all zero add this bin
1537 for (int j = 0; j < thislookup.size(); j++) {
1538 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
1540 //if there is a bin label use it otherwise make one
1541 string binLabel = "Otu";
1542 string sbinNumber = toString(i+1);
1543 if (sbinNumber.length() < snumBins.length()) {
1544 int diff = snumBins.length() - sbinNumber.length();
1545 for (int h = 0; h < diff; h++) { binLabel += "0"; }
1547 binLabel += sbinNumber;
1548 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
1550 newBinLabels.push_back(binLabel);
1554 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
1557 thislookup = newLookup;
1558 m->currentBinLabels = newBinLabels;
1563 catch(exception& e) {
1564 m->errorOut(e, "SubSampleCommand", "eliminateZeroOTUS");
1569 //**********************************************************************************************************************