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);
74 f >> label >> groupN >> num;
76 //read in first row since you know there is at least 1 group.
79 //make binlabels because we don't have any
80 string snumBins = toString(num);
81 m->binLabelsInFile.clear();
82 for (int i = 0; i < num; i++) {
83 //if there is a bin label use it otherwise make one
84 string binLabel = "Otu";
85 string sbinNumber = toString(i+1);
86 if (sbinNumber.length() < snumBins.length()) {
87 int diff = snumBins.length() - sbinNumber.length();
88 for (int h = 0; h < diff; h++) { binLabel += "0"; }
90 binLabel += sbinNumber;
91 m->binLabelsInFile.push_back(binLabel);
95 label = m->saveNextLabel;
97 //read in first row since you know there is at least 1 group.
101 //reset labels, currentLabels may have gotten changed as otus were eliminated because of group choices or sampling
102 m->currentBinLabels = m->binLabelsInFile;
106 //add new vector to lookup
107 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
108 lookup.push_back(temp);
109 lookup[0]->setLabel(label);
110 lookup[0]->setGroup(groupN);
112 allGroups.push_back(groupN);
114 //fill vector. data = first sharedrabund in file
115 for(int i=0;i<num;i++){
118 lookup[0]->push_back(inputData, groupN); //abundance, bin, group
119 push_back(inputData, groupN);
121 if (inputData > maxRank) { maxRank = inputData; }
126 if (f.eof() != true) { f >> nextLabel; }
128 //read the rest of the groups info in
129 while ((nextLabel == holdLabel) && (f.eof() != true)) {
134 allGroups.push_back(groupN);
136 //add new vector to lookup
137 temp = new SharedRAbundFloatVector();
138 lookup.push_back(temp);
139 lookup[count]->setLabel(label);
140 lookup[count]->setGroup(groupN);
143 for(int i=0;i<num;i++){
145 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
150 if (f.eof() != true) { f >> nextLabel; }
153 m->saveNextLabel = nextLabel;
154 m->setAllGroups(allGroups);
156 catch(exception& e) {
157 m->errorOut(e, "SharedRAbundFloatVector", "SharedRAbundFloatVector");
162 /***********************************************************************/
164 void SharedRAbundFloatVector::set(int binNumber, float newBinSize, string groupname){
166 float oldBinSize = data[binNumber].abundance;
167 data[binNumber].abundance = newBinSize;
168 data[binNumber].group = groupname;
170 if(newBinSize > maxRank) { newBinSize = newBinSize; }
172 numSeqs += (newBinSize - oldBinSize);
174 catch(exception& e) {
175 m->errorOut(e, "SharedRAbundFloatVector", "set");
179 /***********************************************************************/
181 void SharedRAbundFloatVector::clear(){
186 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
189 /***********************************************************************/
190 float SharedRAbundFloatVector::getAbundance(int index){
191 return data[index].abundance;
193 /***********************************************************************/
194 //returns vector of abundances
195 vector<float> SharedRAbundFloatVector::getAbundances(){
196 vector<float> abunds;
197 for (int i = 0; i < data.size(); i++) {
198 abunds.push_back(data[i].abundance);
203 /***********************************************************************/
204 individualFloat SharedRAbundFloatVector::get(int index){
207 /***********************************************************************/
208 void SharedRAbundFloatVector::push_back(float binSize, string groupName){
210 individualFloat newGuy;
211 newGuy.abundance = binSize;
212 newGuy.group = groupName;
213 newGuy.bin = data.size();
215 data.push_back(newGuy);
218 if(binSize > maxRank){ maxRank = binSize; }
222 catch(exception& e) {
223 m->errorOut(e, "SharedRAbundFloatVector", "push_back");
227 /***********************************************************************/
228 void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
230 individualFloat newGuy;
231 newGuy.abundance = binSize;
232 newGuy.group = groupName;
235 data.insert(data.begin()+otu, newGuy);
238 if(binSize > maxRank){ maxRank = binSize; }
242 catch(exception& e) {
243 m->errorOut(e, "SharedRAbundFloatVector", "insert");
248 /***********************************************************************/
249 void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
251 individualFloat newGuy;
252 newGuy.abundance = binSize;
253 newGuy.group = groupName;
256 data.insert(data.begin(), newGuy);
259 if(binSize > maxRank){ maxRank = binSize; }
263 catch(exception& e) {
264 m->errorOut(e, "SharedRAbundFloatVector", "push_front");
268 /**********************************************************************/
269 void SharedRAbundFloatVector::pop_back(){
270 numSeqs -= data[data.size()-1].abundance;
274 /***********************************************************************/
275 void SharedRAbundFloatVector::resize(int size){
278 /**********************************************************************/
279 int SharedRAbundFloatVector::size(){
282 /***********************************************************************/
283 void SharedRAbundFloatVector::printHeaders(ostream& output){
285 string snumBins = toString(numBins);
286 output << "label\tGroup\tnumOtus\t";
287 if (m->sharedHeaderMode == "tax") {
288 for (int i = 0; i < numBins; i++) {
290 //if there is a bin label use it otherwise make one
291 string binLabel = "PhyloType";
292 string sbinNumber = toString(i+1);
293 if (sbinNumber.length() < snumBins.length()) {
294 int diff = snumBins.length() - sbinNumber.length();
295 for (int h = 0; h < diff; h++) { binLabel += "0"; }
297 binLabel += sbinNumber;
298 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
300 output << binLabel << '\t';
304 for (int i = 0; i < numBins; i++) {
305 //if there is a bin label use it otherwise make one
306 string binLabel = "Otu";
307 string sbinNumber = toString(i+1);
308 if (sbinNumber.length() < snumBins.length()) {
309 int diff = snumBins.length() - sbinNumber.length();
310 for (int h = 0; h < diff; h++) { binLabel += "0"; }
312 binLabel += sbinNumber;
313 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
315 output << binLabel << '\t';
321 m->printedHeaders = true;
323 catch(exception& e) {
324 m->errorOut(e, "SharedRAbundVector", "printHeaders");
328 /***********************************************************************/
329 void SharedRAbundFloatVector::print(ostream& output){
331 output << numBins << '\t';
333 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
336 catch(exception& e) {
337 m->errorOut(e, "SharedRAbundFloatVector", "print");
341 /***********************************************************************/
342 string SharedRAbundFloatVector::getGroup(){
345 /***********************************************************************/
346 void SharedRAbundFloatVector::setGroup(string groupName){
349 /***********************************************************************/
350 int SharedRAbundFloatVector::getGroupIndex() { return index; }
351 /***********************************************************************/
352 void SharedRAbundFloatVector::setGroupIndex(int vIndex) { index = vIndex; }
353 /***********************************************************************/
354 int SharedRAbundFloatVector::getNumBins(){ return numBins; }
355 /***********************************************************************/
356 float SharedRAbundFloatVector::getNumSeqs(){ return numSeqs; }
357 /***********************************************************************/
358 float SharedRAbundFloatVector::getMaxRank(){ return maxRank; }
359 /***********************************************************************/
360 SharedRAbundFloatVector SharedRAbundFloatVector::getSharedRAbundFloatVector(){
363 /***********************************************************************
364 SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
366 SharedRAbundVector rav(numBins);
370 for (int i = 0; i < data.size(); i++) {
372 rav.push_back(data[i].abundance);
376 catch(exception& e) {
377 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
381 ***********************************************************************/
382 vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
385 util = new SharedUtil();
387 vector<string> Groups = m->getGroups();
388 vector<string> allGroups = m->getAllGroups();
389 util->setGroups(Groups, allGroups);
390 m->setGroups(Groups);
393 for (int i = 0; i < lookup.size(); i++) {
394 //if this sharedrabund is not from a group the user wants then delete it.
395 if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) {
396 delete lookup[i]; lookup[i] = NULL;
397 lookup.erase(lookup.begin()+i);
405 if (remove) { eliminateZeroOTUS(lookup); }
409 catch(exception& e) {
410 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
414 /***********************************************************************/
416 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
418 RAbundVector rav(numBins);
420 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
425 catch(exception& e) {
426 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
430 /***********************************************************************
432 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
434 SharedSAbundVector sav(maxRank+1);
436 for(int i=0;i<data.size();i++){
437 int abund = data[i].abundance;
438 sav.set(abund, sav.getAbundance(abund) + 1, group);
441 sav.set(0, 0, group);
447 catch(exception& e) {
448 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
452 ***********************************************************************/
454 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
456 SAbundVector sav(ceil(maxRank)+1);
458 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
464 catch(exception& e) {
465 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");
470 /***********************************************************************
472 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
474 SharedOrderVector ov;
476 for(int i=0;i<data.size();i++){
477 int round = ceil(data[i].abundance);
478 for(int j=0;j<round;j++){
479 ov.push_back(data[i].bin, round, data[i].group);
482 random_shuffle(ov.begin(), ov.end());
489 catch(exception& e) {
490 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
494 ***********************************************************************/
495 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
496 OrderVector SharedRAbundFloatVector::getOrderVector(map<string,int>* nameMap = NULL) {
500 for(int i=0;i<data.size();i++){
501 int round = ceil(data[i].abundance);
502 for(int j=0;j<round;j++){
506 random_shuffle(ov.begin(), ov.end());
511 catch(exception& e) {
512 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
516 //**********************************************************************************************************************
517 int SharedRAbundFloatVector::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thislookup) {
520 vector<SharedRAbundFloatVector*> newLookup;
521 for (int i = 0; i < thislookup.size(); i++) {
522 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
523 temp->setLabel(thislookup[i]->getLabel());
524 temp->setGroup(thislookup[i]->getGroup());
525 newLookup.push_back(temp);
529 vector<string> newBinLabels;
530 string snumBins = toString(thislookup[0]->getNumBins());
531 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
532 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
534 //look at each sharedRabund and make sure they are not all zero
536 for (int j = 0; j < thislookup.size(); j++) {
537 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
540 //if they are not all zero add this bin
542 for (int j = 0; j < thislookup.size(); j++) {
543 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
545 //if there is a bin label use it otherwise make one
546 string binLabel = "Otu";
547 string sbinNumber = toString(i+1);
548 if (sbinNumber.length() < snumBins.length()) {
549 int diff = snumBins.length() - sbinNumber.length();
550 for (int h = 0; h < diff; h++) { binLabel += "0"; }
552 binLabel += sbinNumber;
553 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
555 newBinLabels.push_back(binLabel);
559 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
561 thislookup = newLookup;
562 m->currentBinLabels = newBinLabels;
567 catch(exception& e) {
568 m->errorOut(e, "SharedRAbundFloatVector", "eliminateZeroOTUS");
572 /***********************************************************************/