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) {
37 vector<string> allGroups;
42 string holdLabel, nextLabel, groupN;
43 individualFloat newguy;
45 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
48 //are we at the beginning of the file??
49 if (m->saveNextLabel == "") {
52 //is this a shared file that has headers
53 if (label == "label") {
55 f >> label; m->gobble(f);
58 f >> label; m->gobble(f);
61 label = m->getline(f); m->gobble(f);
63 //parse labels to save
64 istringstream iStringStream(label);
65 m->binLabelsInFile.clear();
66 while(!iStringStream.eof()){
67 if (m->control_pressed) { break; }
69 iStringStream >> temp; m->gobble(iStringStream);
71 m->binLabelsInFile.push_back(temp);
76 }else { label = m->saveNextLabel; }
78 //reset labels, currentLabels may have gotten changed as otus were eliminated because of group choices or sampling
79 m->currentBinLabels = m->binLabelsInFile;
81 //read in first row since you know there is at least 1 group.
86 //add new vector to lookup
87 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
88 lookup.push_back(temp);
89 lookup[0]->setLabel(label);
90 lookup[0]->setGroup(groupN);
92 allGroups.push_back(groupN);
94 //fill vector. data = first sharedrabund in file
95 for(int i=0;i<num;i++){
98 lookup[0]->push_back(inputData, groupN); //abundance, bin, group
99 push_back(inputData, groupN);
101 if (inputData > maxRank) { maxRank = inputData; }
106 if (f.eof() != true) { f >> nextLabel; }
108 //read the rest of the groups info in
109 while ((nextLabel == holdLabel) && (f.eof() != true)) {
112 if (num != 1000) { break; }
115 allGroups.push_back(groupN);
117 //add new vector to lookup
118 temp = new SharedRAbundFloatVector();
119 lookup.push_back(temp);
120 lookup[count]->setLabel(label);
121 lookup[count]->setGroup(groupN);
124 for(int i=0;i<num;i++){
126 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
131 if (f.eof() != true) { f >> nextLabel; }
134 m->saveNextLabel = nextLabel;
135 m->setAllGroups(allGroups);
138 catch(exception& e) {
139 m->errorOut(e, "SharedRAbundFloatVector", "SharedRAbundFloatVector");
144 /***********************************************************************/
146 void SharedRAbundFloatVector::set(int binNumber, float newBinSize, string groupname){
148 float oldBinSize = data[binNumber].abundance;
149 data[binNumber].abundance = newBinSize;
150 data[binNumber].group = groupname;
152 if(newBinSize > maxRank) { newBinSize = newBinSize; }
154 numSeqs += (newBinSize - oldBinSize);
156 catch(exception& e) {
157 m->errorOut(e, "SharedRAbundFloatVector", "set");
161 /***********************************************************************/
163 void SharedRAbundFloatVector::clear(){
168 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
171 /***********************************************************************/
172 float SharedRAbundFloatVector::getAbundance(int index){
173 return data[index].abundance;
175 /***********************************************************************/
176 individualFloat SharedRAbundFloatVector::get(int index){
179 /***********************************************************************/
180 void SharedRAbundFloatVector::push_back(float binSize, string groupName){
182 individualFloat newGuy;
183 newGuy.abundance = binSize;
184 newGuy.group = groupName;
185 newGuy.bin = data.size();
187 data.push_back(newGuy);
190 if(binSize > maxRank){ maxRank = binSize; }
194 catch(exception& e) {
195 m->errorOut(e, "SharedRAbundFloatVector", "push_back");
199 /***********************************************************************/
200 void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
202 individualFloat newGuy;
203 newGuy.abundance = binSize;
204 newGuy.group = groupName;
207 data.insert(data.begin()+otu, newGuy);
210 if(binSize > maxRank){ maxRank = binSize; }
214 catch(exception& e) {
215 m->errorOut(e, "SharedRAbundFloatVector", "insert");
220 /***********************************************************************/
221 void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
223 individualFloat newGuy;
224 newGuy.abundance = binSize;
225 newGuy.group = groupName;
228 data.insert(data.begin(), newGuy);
231 if(binSize > maxRank){ maxRank = binSize; }
235 catch(exception& e) {
236 m->errorOut(e, "SharedRAbundFloatVector", "push_front");
240 /**********************************************************************/
241 void SharedRAbundFloatVector::pop_back(){
242 numSeqs -= data[data.size()-1].abundance;
246 /***********************************************************************/
247 void SharedRAbundFloatVector::resize(int size){
250 /**********************************************************************/
251 int SharedRAbundFloatVector::size(){
254 /***********************************************************************/
255 void SharedRAbundFloatVector::printHeaders(ostream& output){
257 string snumBins = toString(numBins);
258 output << "label\tGroup\tnumOtus\t";
259 if (m->sharedHeaderMode == "tax") {
260 for (int i = 0; i < numBins; i++) {
262 //if there is a bin label use it otherwise make one
263 string binLabel = "PhyloType";
264 string sbinNumber = toString(i+1);
265 if (sbinNumber.length() < snumBins.length()) {
266 int diff = snumBins.length() - sbinNumber.length();
267 for (int h = 0; h < diff; h++) { binLabel += "0"; }
269 binLabel += sbinNumber;
270 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
272 output << binLabel << '\t';
276 for (int i = 0; i < numBins; i++) {
277 //if there is a bin label use it otherwise make one
278 string binLabel = "Otu";
279 string sbinNumber = toString(i+1);
280 if (sbinNumber.length() < snumBins.length()) {
281 int diff = snumBins.length() - sbinNumber.length();
282 for (int h = 0; h < diff; h++) { binLabel += "0"; }
284 binLabel += sbinNumber;
285 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
287 output << binLabel << '\t';
293 m->printedHeaders = true;
295 catch(exception& e) {
296 m->errorOut(e, "SharedRAbundVector", "printHeaders");
300 /***********************************************************************/
301 void SharedRAbundFloatVector::print(ostream& output){
303 output << numBins << '\t';
305 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
308 catch(exception& e) {
309 m->errorOut(e, "SharedRAbundFloatVector", "print");
313 /***********************************************************************/
314 string SharedRAbundFloatVector::getGroup(){
317 /***********************************************************************/
318 void SharedRAbundFloatVector::setGroup(string groupName){
321 /***********************************************************************/
322 int SharedRAbundFloatVector::getGroupIndex() { return index; }
323 /***********************************************************************/
324 void SharedRAbundFloatVector::setGroupIndex(int vIndex) { index = vIndex; }
325 /***********************************************************************/
326 int SharedRAbundFloatVector::getNumBins(){ return numBins; }
327 /***********************************************************************/
328 float SharedRAbundFloatVector::getNumSeqs(){ return numSeqs; }
329 /***********************************************************************/
330 float SharedRAbundFloatVector::getMaxRank(){ return maxRank; }
331 /***********************************************************************/
332 SharedRAbundFloatVector SharedRAbundFloatVector::getSharedRAbundFloatVector(){
335 /***********************************************************************
336 SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
338 SharedRAbundVector rav(numBins);
342 for (int i = 0; i < data.size(); i++) {
344 rav.push_back(data[i].abundance);
348 catch(exception& e) {
349 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
353 ***********************************************************************/
354 vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
357 util = new SharedUtil();
359 vector<string> Groups = m->getGroups();
360 vector<string> allGroups = m->getAllGroups();
361 util->setGroups(Groups, allGroups);
362 m->setGroups(Groups);
365 for (int i = 0; i < lookup.size(); i++) {
366 //if this sharedrabund is not from a group the user wants then delete it.
367 if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) {
368 delete lookup[i]; lookup[i] = NULL;
369 lookup.erase(lookup.begin()+i);
377 if (remove) { eliminateZeroOTUS(lookup); }
381 catch(exception& e) {
382 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
386 /***********************************************************************/
388 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
390 RAbundVector rav(numBins);
392 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
397 catch(exception& e) {
398 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
402 /***********************************************************************
404 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
406 SharedSAbundVector sav(maxRank+1);
408 for(int i=0;i<data.size();i++){
409 int abund = data[i].abundance;
410 sav.set(abund, sav.getAbundance(abund) + 1, group);
413 sav.set(0, 0, group);
419 catch(exception& e) {
420 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
424 ***********************************************************************/
426 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
428 SAbundVector sav(ceil(maxRank)+1);
430 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
436 catch(exception& e) {
437 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");
442 /***********************************************************************
444 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
446 SharedOrderVector ov;
448 for(int i=0;i<data.size();i++){
449 int round = ceil(data[i].abundance);
450 for(int j=0;j<round;j++){
451 ov.push_back(data[i].bin, round, data[i].group);
454 random_shuffle(ov.begin(), ov.end());
461 catch(exception& e) {
462 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
466 ***********************************************************************/
467 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
468 OrderVector SharedRAbundFloatVector::getOrderVector(map<string,int>* nameMap = NULL) {
472 for(int i=0;i<data.size();i++){
473 int round = ceil(data[i].abundance);
474 for(int j=0;j<round;j++){
478 random_shuffle(ov.begin(), ov.end());
483 catch(exception& e) {
484 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
488 //**********************************************************************************************************************
489 int SharedRAbundFloatVector::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thislookup) {
492 vector<SharedRAbundFloatVector*> newLookup;
493 for (int i = 0; i < thislookup.size(); i++) {
494 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
495 temp->setLabel(thislookup[i]->getLabel());
496 temp->setGroup(thislookup[i]->getGroup());
497 newLookup.push_back(temp);
501 vector<string> newBinLabels;
502 string snumBins = toString(thislookup[0]->getNumBins());
503 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
504 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
506 //look at each sharedRabund and make sure they are not all zero
508 for (int j = 0; j < thislookup.size(); j++) {
509 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
512 //if they are not all zero add this bin
514 for (int j = 0; j < thislookup.size(); j++) {
515 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
517 //if there is a bin label use it otherwise make one
518 string binLabel = "Otu";
519 string sbinNumber = toString(i+1);
520 if (sbinNumber.length() < snumBins.length()) {
521 int diff = snumBins.length() - sbinNumber.length();
522 for (int h = 0; h < diff; h++) { binLabel += "0"; }
524 binLabel += sbinNumber;
525 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
527 newBinLabels.push_back(binLabel);
531 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
533 thislookup = newLookup;
534 m->currentBinLabels = newBinLabels;
539 catch(exception& e) {
540 m->errorOut(e, "SharedRAbundFloatVector", "eliminateZeroOTUS");
544 /***********************************************************************/