5 // Created by Sarah Westcott on 4/2/12.
6 // Copyright (c) 2012 Schloss Lab. All rights reserved.
11 //**********************************************************************************************************************
12 vector<string> SubSample::getSample(vector<SharedRAbundVector*>& thislookup, int size) {
15 //save mothurOut's binLabels to restore for next label
16 vector<string> saveBinLabels = m->currentBinLabels;
18 int numBins = thislookup[0]->getNumBins();
19 for (int i = 0; i < thislookup.size(); i++) {
20 int thisSize = thislookup[i]->getNumSeqs();
22 if (thisSize != size) {
24 string thisgroup = thislookup[i]->getGroup();
27 for(int p=0;p<numBins;p++){
28 for(int j=0;j<thislookup[i]->getAbundance(p);j++){
32 random_shuffle(order.begin(), order.end());
34 SharedRAbundVector* temp = new SharedRAbundVector(numBins);
35 temp->setLabel(thislookup[i]->getLabel());
36 temp->setGroup(thislookup[i]->getGroup());
42 for (int j = 0; j < size; j++) {
44 if (m->control_pressed) { return m->currentBinLabels; }
46 int bin = order.get(j);
48 int abund = thislookup[i]->getAbundance(bin);
49 thislookup[i]->set(bin, (abund+1), thisgroup);
54 //subsampling may have created some otus with no sequences in them
55 eliminateZeroOTUS(thislookup);
57 if (m->control_pressed) { return m->currentBinLabels; }
59 //save mothurOut's binLabels to restore for next label
60 vector<string> subsampleBinLabels = m->currentBinLabels;
61 m->currentBinLabels = saveBinLabels;
63 return subsampleBinLabels;
67 m->errorOut(e, "SubSample", "getSample");
71 //**********************************************************************************************************************
72 int SubSample::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
75 vector<SharedRAbundVector*> newLookup;
76 for (int i = 0; i < thislookup.size(); i++) {
77 SharedRAbundVector* temp = new SharedRAbundVector();
78 temp->setLabel(thislookup[i]->getLabel());
79 temp->setGroup(thislookup[i]->getGroup());
80 newLookup.push_back(temp);
84 vector<string> newBinLabels;
85 string snumBins = toString(thislookup[0]->getNumBins());
86 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
87 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
89 //look at each sharedRabund and make sure they are not all zero
91 for (int j = 0; j < thislookup.size(); j++) {
92 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
95 //if they are not all zero add this bin
97 for (int j = 0; j < thislookup.size(); j++) {
98 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
100 //if there is a bin label use it otherwise make one
101 string binLabel = "Otu";
102 string sbinNumber = toString(i+1);
103 if (sbinNumber.length() < snumBins.length()) {
104 int diff = snumBins.length() - sbinNumber.length();
105 for (int h = 0; h < diff; h++) { binLabel += "0"; }
107 binLabel += sbinNumber;
108 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
110 newBinLabels.push_back(binLabel);
114 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
117 thislookup = newLookup;
118 m->currentBinLabels = newBinLabels;
123 catch(exception& e) {
124 m->errorOut(e, "SubSample", "eliminateZeroOTUS");
130 //**********************************************************************************************************************