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)) {
113 allGroups.push_back(groupN);
115 //add new vector to lookup
116 temp = new SharedRAbundFloatVector();
117 lookup.push_back(temp);
118 lookup[count]->setLabel(label);
119 lookup[count]->setGroup(groupN);
122 for(int i=0;i<num;i++){
124 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
129 if (f.eof() != true) { f >> nextLabel; }
132 m->saveNextLabel = nextLabel;
133 m->setAllGroups(allGroups);
136 catch(exception& e) {
137 m->errorOut(e, "SharedRAbundFloatVector", "SharedRAbundFloatVector");
142 /***********************************************************************/
144 void SharedRAbundFloatVector::set(int binNumber, float newBinSize, string groupname){
146 float oldBinSize = data[binNumber].abundance;
147 data[binNumber].abundance = newBinSize;
148 data[binNumber].group = groupname;
150 if(newBinSize > maxRank) { newBinSize = newBinSize; }
152 numSeqs += (newBinSize - oldBinSize);
154 catch(exception& e) {
155 m->errorOut(e, "SharedRAbundFloatVector", "set");
159 /***********************************************************************/
161 void SharedRAbundFloatVector::clear(){
166 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
169 /***********************************************************************/
170 float SharedRAbundFloatVector::getAbundance(int index){
171 return data[index].abundance;
173 /***********************************************************************/
174 individualFloat SharedRAbundFloatVector::get(int index){
177 /***********************************************************************/
178 void SharedRAbundFloatVector::push_back(float binSize, string groupName){
180 individualFloat newGuy;
181 newGuy.abundance = binSize;
182 newGuy.group = groupName;
183 newGuy.bin = data.size();
185 data.push_back(newGuy);
188 if(binSize > maxRank){ maxRank = binSize; }
192 catch(exception& e) {
193 m->errorOut(e, "SharedRAbundFloatVector", "push_back");
197 /***********************************************************************/
198 void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
200 individualFloat newGuy;
201 newGuy.abundance = binSize;
202 newGuy.group = groupName;
205 data.insert(data.begin()+otu, newGuy);
208 if(binSize > maxRank){ maxRank = binSize; }
212 catch(exception& e) {
213 m->errorOut(e, "SharedRAbundFloatVector", "insert");
218 /***********************************************************************/
219 void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
221 individualFloat newGuy;
222 newGuy.abundance = binSize;
223 newGuy.group = groupName;
226 data.insert(data.begin(), newGuy);
229 if(binSize > maxRank){ maxRank = binSize; }
233 catch(exception& e) {
234 m->errorOut(e, "SharedRAbundFloatVector", "push_front");
238 /**********************************************************************/
239 void SharedRAbundFloatVector::pop_back(){
240 numSeqs -= data[data.size()-1].abundance;
244 /***********************************************************************/
245 void SharedRAbundFloatVector::resize(int size){
248 /**********************************************************************/
249 int SharedRAbundFloatVector::size(){
252 /***********************************************************************/
253 void SharedRAbundFloatVector::printHeaders(ostream& output){
255 output << "label\tGroup\tnumOtus\t";
256 if (m->sharedHeaderMode == "tax") {
257 for (int i = 0; i < numBins; i++) {
259 //if there is a bin label use it otherwise make one
260 string binLabel = "PhyloType" + toString(i+1);
261 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
263 output << binLabel << '\t';
267 for (int i = 0; i < numBins; i++) {
268 //if there is a bin label use it otherwise make one
269 string binLabel = "Otu" + toString(i+1);
270 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
272 output << binLabel << '\t';
278 m->printedHeaders = true;
280 catch(exception& e) {
281 m->errorOut(e, "SharedRAbundVector", "printHeaders");
285 /***********************************************************************/
286 void SharedRAbundFloatVector::print(ostream& output){
288 output << numBins << '\t';
290 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
293 catch(exception& e) {
294 m->errorOut(e, "SharedRAbundFloatVector", "print");
298 /***********************************************************************/
299 string SharedRAbundFloatVector::getGroup(){
302 /***********************************************************************/
303 void SharedRAbundFloatVector::setGroup(string groupName){
306 /***********************************************************************/
307 int SharedRAbundFloatVector::getGroupIndex() { return index; }
308 /***********************************************************************/
309 void SharedRAbundFloatVector::setGroupIndex(int vIndex) { index = vIndex; }
310 /***********************************************************************/
311 int SharedRAbundFloatVector::getNumBins(){ return numBins; }
312 /***********************************************************************/
313 float SharedRAbundFloatVector::getNumSeqs(){ return numSeqs; }
314 /***********************************************************************/
315 float SharedRAbundFloatVector::getMaxRank(){ return maxRank; }
316 /***********************************************************************/
317 SharedRAbundFloatVector SharedRAbundFloatVector::getSharedRAbundFloatVector(){
320 /***********************************************************************
321 SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
323 SharedRAbundVector rav(numBins);
327 for (int i = 0; i < data.size(); i++) {
329 rav.push_back(data[i].abundance);
333 catch(exception& e) {
334 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
338 /***********************************************************************/
339 vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
342 util = new SharedUtil();
344 vector<string> Groups = m->getGroups();
345 vector<string> allGroups = m->getAllGroups();
346 util->setGroups(Groups, allGroups);
347 m->setGroups(Groups);
350 for (int i = 0; i < lookup.size(); i++) {
351 //if this sharedrabund is not from a group the user wants then delete it.
352 if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) {
353 delete lookup[i]; lookup[i] = NULL;
354 lookup.erase(lookup.begin()+i);
362 if (remove) { eliminateZeroOTUS(lookup); }
366 catch(exception& e) {
367 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
371 /***********************************************************************/
373 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
375 RAbundVector rav(numBins);
377 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
382 catch(exception& e) {
383 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
387 /***********************************************************************
389 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
391 SharedSAbundVector sav(maxRank+1);
393 for(int i=0;i<data.size();i++){
394 int abund = data[i].abundance;
395 sav.set(abund, sav.getAbundance(abund) + 1, group);
398 sav.set(0, 0, group);
404 catch(exception& e) {
405 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
409 /***********************************************************************/
411 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
413 SAbundVector sav(ceil(maxRank)+1);
415 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
421 catch(exception& e) {
422 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");
427 /***********************************************************************
429 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
431 SharedOrderVector ov;
433 for(int i=0;i<data.size();i++){
434 int round = ceil(data[i].abundance);
435 for(int j=0;j<round;j++){
436 ov.push_back(data[i].bin, round, data[i].group);
439 random_shuffle(ov.begin(), ov.end());
446 catch(exception& e) {
447 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
451 /***********************************************************************/
452 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
453 OrderVector SharedRAbundFloatVector::getOrderVector(map<string,int>* nameMap = NULL) {
457 for(int i=0;i<data.size();i++){
458 int round = ceil(data[i].abundance);
459 for(int j=0;j<round;j++){
463 random_shuffle(ov.begin(), ov.end());
468 catch(exception& e) {
469 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
473 //**********************************************************************************************************************
474 int SharedRAbundFloatVector::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thislookup) {
477 vector<SharedRAbundFloatVector*> newLookup;
478 for (int i = 0; i < thislookup.size(); i++) {
479 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
480 temp->setLabel(thislookup[i]->getLabel());
481 temp->setGroup(thislookup[i]->getGroup());
482 newLookup.push_back(temp);
486 vector<string> newBinLabels;
487 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
488 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
490 //look at each sharedRabund and make sure they are not all zero
492 for (int j = 0; j < thislookup.size(); j++) {
493 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
496 //if they are not all zero add this bin
498 for (int j = 0; j < thislookup.size(); j++) {
499 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
501 //if there is a bin label use it otherwise make one
502 string binLabel = "Otu" + toString(i+1);
503 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
505 newBinLabels.push_back(binLabel);
509 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
511 thislookup = newLookup;
512 m->currentBinLabels = newBinLabels;
517 catch(exception& e) {
518 m->errorOut(e, "SharedRAbundFloatVector", "eliminateZeroOTUS");
522 /***********************************************************************/