5 // Created by Sarah Westcott on 4/2/12.
6 // Copyright (c) 2012 Schloss Lab. All rights reserved.
11 //**********************************************************************************************************************
12 vector<SharedRAbundVector*> SubSample::getSamplePreserve(vector<SharedRAbundVector*>& thislookup, vector<string>& newLabels, int size) {
15 vector<SharedRAbundVector*> newlookup; newlookup.resize(thislookup.size(), NULL);
17 //save mothurOut's binLabels to restore for next label
18 vector<string> saveBinLabels = m->currentBinLabels;
20 int numBins = thislookup[0]->getNumBins();
21 for (int i = 0; i < thislookup.size(); i++) {
22 int thisSize = thislookup[i]->getNumSeqs();
24 if (thisSize != size) {
26 string thisgroup = thislookup[i]->getGroup();
29 for(int p=0;p<numBins;p++){
30 for(int j=0;j<thislookup[i]->getAbundance(p);j++){
34 random_shuffle(order.begin(), order.end());
36 SharedRAbundVector* temp = new SharedRAbundVector(numBins);
37 temp->setLabel(thislookup[i]->getLabel());
38 temp->setGroup(thislookup[i]->getGroup());
42 for (int j = 0; j < size; j++) {
44 if (m->control_pressed) { return newlookup; }
46 int bin = order.get(j);
48 int abund = newlookup[i]->getAbundance(bin);
49 newlookup[i]->set(bin, (abund+1), thisgroup);
54 //subsampling may have created some otus with no sequences in them
55 eliminateZeroOTUS(newlookup);
57 if (m->control_pressed) { return newlookup; }
59 //save mothurOut's binLabels to restore for next label
60 newLabels = m->currentBinLabels;
61 m->currentBinLabels = saveBinLabels;
67 m->errorOut(e, "SubSample", "getSamplePreserve");
71 //**********************************************************************************************************************
72 vector<string> SubSample::getSample(vector<SharedRAbundVector*>& thislookup, int size) {
75 //save mothurOut's binLabels to restore for next label
76 vector<string> saveBinLabels = m->currentBinLabels;
78 int numBins = thislookup[0]->getNumBins();
79 for (int i = 0; i < thislookup.size(); i++) {
80 int thisSize = thislookup[i]->getNumSeqs();
82 if (thisSize != size) {
84 string thisgroup = thislookup[i]->getGroup();
87 for(int p=0;p<numBins;p++){
88 for(int j=0;j<thislookup[i]->getAbundance(p);j++){
92 random_shuffle(order.begin(), order.end());
94 SharedRAbundVector* temp = new SharedRAbundVector(numBins);
95 temp->setLabel(thislookup[i]->getLabel());
96 temp->setGroup(thislookup[i]->getGroup());
102 for (int j = 0; j < size; j++) {
104 if (m->control_pressed) { return m->currentBinLabels; }
106 int bin = order.get(j);
108 int abund = thislookup[i]->getAbundance(bin);
109 thislookup[i]->set(bin, (abund+1), thisgroup);
114 //subsampling may have created some otus with no sequences in them
115 eliminateZeroOTUS(thislookup);
117 if (m->control_pressed) { return m->currentBinLabels; }
119 //save mothurOut's binLabels to restore for next label
120 vector<string> subsampleBinLabels = m->currentBinLabels;
121 m->currentBinLabels = saveBinLabels;
123 return subsampleBinLabels;
126 catch(exception& e) {
127 m->errorOut(e, "SubSample", "getSample");
131 //**********************************************************************************************************************
132 int SubSample::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
135 vector<SharedRAbundVector*> newLookup;
136 for (int i = 0; i < thislookup.size(); i++) {
137 SharedRAbundVector* temp = new SharedRAbundVector();
138 temp->setLabel(thislookup[i]->getLabel());
139 temp->setGroup(thislookup[i]->getGroup());
140 newLookup.push_back(temp);
144 vector<string> newBinLabels;
145 string snumBins = toString(thislookup[0]->getNumBins());
146 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
147 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
149 //look at each sharedRabund and make sure they are not all zero
151 for (int j = 0; j < thislookup.size(); j++) {
152 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
155 //if they are not all zero add this bin
157 for (int j = 0; j < thislookup.size(); j++) {
158 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
160 //if there is a bin label use it otherwise make one
161 string binLabel = "Otu";
162 string sbinNumber = toString(i+1);
163 if (sbinNumber.length() < snumBins.length()) {
164 int diff = snumBins.length() - sbinNumber.length();
165 for (int h = 0; h < diff; h++) { binLabel += "0"; }
167 binLabel += sbinNumber;
168 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
170 newBinLabels.push_back(binLabel);
174 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
177 thislookup = newLookup;
178 m->currentBinLabels = newBinLabels;
183 catch(exception& e) {
184 m->errorOut(e, "SubSample", "eliminateZeroOTUS");
190 //**********************************************************************************************************************