2 * sharedrabundfloatvector.cpp
5 * Created by westcott on 8/18/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "sharedrabundfloatvector.h"
11 #include "sharedutilities.h"
13 /***********************************************************************/
15 SharedRAbundFloatVector::SharedRAbundFloatVector() : DataVector(), maxRank(0.0), numBins(0), numSeqs(0.0) {}
16 /***********************************************************************/
18 SharedRAbundFloatVector::~SharedRAbundFloatVector() {}
20 /***********************************************************************/
21 SharedRAbundFloatVector::SharedRAbundFloatVector(int n) : DataVector(), maxRank(0.0), numBins(n), numSeqs(0.0) {
23 individualFloat newGuy;
25 for (int i=0; i< n; i++) {
27 newGuy.abundance = 0.0;
28 data.push_back(newGuy);
31 /***********************************************************************/
33 SharedRAbundFloatVector::SharedRAbundFloatVector(ifstream& f) : DataVector(), maxRank(0.0), numBins(0), numSeqs(0.0) {
36 m->namesOfGroups.clear();
41 string holdLabel, nextLabel, groupN;
42 individualFloat newguy;
44 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
47 if (m->saveNextLabel == "") { f >> label; }
48 else { label = m->saveNextLabel; }
50 //read in first row since you know there is at least 1 group.
55 //add new vector to lookup
56 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
57 lookup.push_back(temp);
58 lookup[0]->setLabel(label);
59 lookup[0]->setGroup(groupN);
61 m->namesOfGroups.push_back(groupN);
63 //fill vector. data = first sharedrabund in file
64 for(int i=0;i<num;i++){
67 lookup[0]->push_back(inputData, groupN); //abundance, bin, group
68 push_back(inputData, groupN);
70 if (inputData > maxRank) { maxRank = inputData; }
75 if (f.eof() != true) { f >> nextLabel; }
77 //read the rest of the groups info in
78 while ((nextLabel == holdLabel) && (f.eof() != true)) {
82 m->namesOfGroups.push_back(groupN);
84 //add new vector to lookup
85 temp = new SharedRAbundFloatVector();
86 lookup.push_back(temp);
87 lookup[count]->setLabel(label);
88 lookup[count]->setGroup(groupN);
91 for(int i=0;i<num;i++){
93 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
98 if (f.eof() != true) { f >> nextLabel; }
101 m->saveNextLabel = nextLabel;
104 catch(exception& e) {
105 m->errorOut(e, "SharedRAbundFloatVector", "SharedRAbundFloatVector");
110 /***********************************************************************/
112 void SharedRAbundFloatVector::set(int binNumber, float newBinSize, string groupname){
114 float oldBinSize = data[binNumber].abundance;
115 data[binNumber].abundance = newBinSize;
116 data[binNumber].group = groupname;
118 if(newBinSize > maxRank) { newBinSize = newBinSize; }
120 numSeqs += (newBinSize - oldBinSize);
122 catch(exception& e) {
123 m->errorOut(e, "SharedRAbundFloatVector", "set");
127 /***********************************************************************/
129 void SharedRAbundFloatVector::clear(){
134 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
137 /***********************************************************************/
138 float SharedRAbundFloatVector::getAbundance(int index){
139 return data[index].abundance;
141 /***********************************************************************/
142 individualFloat SharedRAbundFloatVector::get(int index){
145 /***********************************************************************/
146 void SharedRAbundFloatVector::push_back(float binSize, string groupName){
148 individualFloat newGuy;
149 newGuy.abundance = binSize;
150 newGuy.group = groupName;
151 newGuy.bin = data.size();
153 data.push_back(newGuy);
156 if(binSize > maxRank){ maxRank = binSize; }
160 catch(exception& e) {
161 m->errorOut(e, "SharedRAbundFloatVector", "push_back");
165 /***********************************************************************/
166 void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
168 individualFloat newGuy;
169 newGuy.abundance = binSize;
170 newGuy.group = groupName;
173 data.insert(data.begin()+otu, newGuy);
176 if(binSize > maxRank){ maxRank = binSize; }
180 catch(exception& e) {
181 m->errorOut(e, "SharedRAbundFloatVector", "insert");
186 /***********************************************************************/
187 void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
189 individualFloat newGuy;
190 newGuy.abundance = binSize;
191 newGuy.group = groupName;
194 data.insert(data.begin(), newGuy);
197 if(binSize > maxRank){ maxRank = binSize; }
201 catch(exception& e) {
202 m->errorOut(e, "SharedRAbundFloatVector", "push_front");
206 /**********************************************************************/
207 void SharedRAbundFloatVector::pop_back(){
208 numSeqs -= data[data.size()-1].abundance;
212 /***********************************************************************/
213 void SharedRAbundFloatVector::resize(int size){
216 /**********************************************************************/
217 int SharedRAbundFloatVector::size(){
220 /***********************************************************************/
221 void SharedRAbundFloatVector::print(ostream& output){
223 output << numBins << '\t';
225 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
228 catch(exception& e) {
229 m->errorOut(e, "SharedRAbundFloatVector", "print");
233 /***********************************************************************/
234 string SharedRAbundFloatVector::getGroup(){
237 /***********************************************************************/
238 void SharedRAbundFloatVector::setGroup(string groupName){
241 /***********************************************************************/
242 int SharedRAbundFloatVector::getGroupIndex() { return index; }
243 /***********************************************************************/
244 void SharedRAbundFloatVector::setGroupIndex(int vIndex) { index = vIndex; }
245 /***********************************************************************/
246 int SharedRAbundFloatVector::getNumBins(){ return numBins; }
247 /***********************************************************************/
248 float SharedRAbundFloatVector::getNumSeqs(){ return numSeqs; }
249 /***********************************************************************/
250 float SharedRAbundFloatVector::getMaxRank(){ return maxRank; }
251 /***********************************************************************/
252 SharedRAbundFloatVector SharedRAbundFloatVector::getSharedRAbundFloatVector(){
255 /***********************************************************************
256 SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
258 SharedRAbundVector rav(numBins);
262 for (int i = 0; i < data.size(); i++) {
264 rav.push_back(data[i].abundance);
268 catch(exception& e) {
269 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
273 /***********************************************************************/
274 vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
277 util = new SharedUtil();
279 util->setGroups(m->Groups, m->namesOfGroups);
282 for (int i = 0; i < lookup.size(); i++) {
283 //if this sharedrabund is not from a group the user wants then delete it.
284 if (util->isValidGroup(lookup[i]->getGroup(), m->Groups) == false) {
285 delete lookup[i]; lookup[i] = NULL;
286 lookup.erase(lookup.begin()+i);
294 if (remove) { eliminateZeroOTUS(lookup); }
298 catch(exception& e) {
299 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
303 /***********************************************************************/
305 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
307 RAbundVector rav(numBins);
309 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
314 catch(exception& e) {
315 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
319 /***********************************************************************
321 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
323 SharedSAbundVector sav(maxRank+1);
325 for(int i=0;i<data.size();i++){
326 int abund = data[i].abundance;
327 sav.set(abund, sav.getAbundance(abund) + 1, group);
330 sav.set(0, 0, group);
336 catch(exception& e) {
337 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
341 /***********************************************************************/
343 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
345 SAbundVector sav(ceil(maxRank)+1);
347 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
353 catch(exception& e) {
354 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");
359 /***********************************************************************
361 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
363 SharedOrderVector ov;
365 for(int i=0;i<data.size();i++){
366 int round = ceil(data[i].abundance);
367 for(int j=0;j<round;j++){
368 ov.push_back(data[i].bin, round, data[i].group);
371 random_shuffle(ov.begin(), ov.end());
378 catch(exception& e) {
379 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
383 /***********************************************************************/
384 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
385 OrderVector SharedRAbundFloatVector::getOrderVector(map<string,int>* nameMap = NULL) {
389 for(int i=0;i<data.size();i++){
390 int round = ceil(data[i].abundance);
391 for(int j=0;j<round;j++){
395 random_shuffle(ov.begin(), ov.end());
400 catch(exception& e) {
401 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
405 //**********************************************************************************************************************
406 int SharedRAbundFloatVector::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thislookup) {
409 vector<SharedRAbundFloatVector*> newLookup;
410 for (int i = 0; i < thislookup.size(); i++) {
411 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
412 temp->setLabel(thislookup[i]->getLabel());
413 temp->setGroup(thislookup[i]->getGroup());
414 newLookup.push_back(temp);
418 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
419 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
421 //look at each sharedRabund and make sure they are not all zero
423 for (int j = 0; j < thislookup.size(); j++) {
424 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
427 //if they are not all zero add this bin
429 for (int j = 0; j < thislookup.size(); j++) {
430 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
435 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
437 thislookup = newLookup;
442 catch(exception& e) {
443 m->errorOut(e, "SharedRAbundFloatVector", "eliminateZeroOTUS");
447 /***********************************************************************/