5 * Created by Sarah Westcott on 12/5/08.
6 * Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "sharedrabundvector.h"
11 #include "sabundvector.hpp"
12 #include "ordervector.hpp"
13 #include "sharedutilities.h"
16 /***********************************************************************/
17 SharedRAbundVector::SharedRAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {}
18 /***********************************************************************/
20 SharedRAbundVector::~SharedRAbundVector() {
21 //for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
25 /***********************************************************************/
27 SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
30 for (int i=0; i< n; i++) {
33 data.push_back(newGuy);
37 /***********************************************************************
39 SharedRAbundVector::SharedRAbundVector(string id, vector<individual> rav) : DataVector(id), data(rav) {
45 for(int i=0;i<data.size();i++){
46 if(data[i].abundance != 0) { numBins = i+1; }
47 if(data[i].abundance > maxRank) { maxRank = data[i].abundance; }
48 numSeqs += data[i].abundance;
52 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
58 /***********************************************************************/
60 SharedRAbundVector::SharedRAbundVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
63 vector<string> allGroups;
65 int num, inputData, count;
67 string holdLabel, nextLabel, groupN;
70 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; } lookup.clear();
72 //are we at the beginning of the file??
73 if (m->saveNextLabel == "") {
76 //is this a shared file that has headers
77 if (label == "label") {
79 f >> label; m->gobble(f);
82 f >> label; m->gobble(f);
85 label = m->getline(f); m->gobble(f);
87 //parse labels to save
88 istringstream iStringStream(label);
89 m->binLabelsInFile.clear();
90 while(!iStringStream.eof()){
91 if (m->control_pressed) { break; }
93 iStringStream >> temp; m->gobble(iStringStream);
95 m->binLabelsInFile.push_back(temp);
100 }else { label = m->saveNextLabel; }
102 //reset labels, currentLabels may have gotten changed as otus were eliminated because of group choices or sampling
103 m->currentBinLabels = m->binLabelsInFile;
105 //read in first row since you know there is at least 1 group.
110 //add new vector to lookup
111 SharedRAbundVector* temp = new SharedRAbundVector();
112 lookup.push_back(temp);
113 lookup[0]->setLabel(label);
114 lookup[0]->setGroup(groupN);
116 allGroups.push_back(groupN);
118 //fill vector. data = first sharedrabund in file
119 for(int i=0;i<num;i++){
122 lookup[0]->push_back(inputData, groupN); //abundance, bin, group
123 push_back(inputData, groupN);
125 if (inputData > maxRank) { maxRank = inputData; }
130 if (!(f.eof())) { f >> nextLabel; }
132 //read the rest of the groups info in
133 while ((nextLabel == holdLabel) && (f.eof() != true)) {
137 allGroups.push_back(groupN);
139 //add new vector to lookup
140 temp = new SharedRAbundVector();
141 lookup.push_back(temp);
142 lookup[count]->setLabel(label);
143 lookup[count]->setGroup(groupN);
146 for(int i=0;i<num;i++){
149 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
154 if (f.eof() != true) { f >> nextLabel; }
156 m->saveNextLabel = nextLabel;
157 m->setAllGroups(allGroups);
160 catch(exception& e) {
161 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
166 /***********************************************************************/
168 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
170 int oldBinSize = data[binNumber].abundance;
171 data[binNumber].abundance = newBinSize;
172 data[binNumber].group = groupname;
174 if(newBinSize > maxRank) { maxRank = newBinSize; }
176 numSeqs += (newBinSize - oldBinSize);
178 catch(exception& e) {
179 m->errorOut(e, "SharedRAbundVector", "set");
183 /***********************************************************************/
185 void SharedRAbundVector::setData(vector <individual> newData){
189 /***********************************************************************/
191 int SharedRAbundVector::getAbundance(int index){
192 return data[index].abundance;
195 /***********************************************************************/
197 int SharedRAbundVector::numNZ(){
199 for(int i = 1; i < numBins; i++)
200 if(data[i].abundance > 0)
204 /***********************************************************************/
206 void SharedRAbundVector::sortD(){
207 struct individual indObj;
208 sort(data.begin()+1, data.end(), indObj);
210 /***********************************************************************/
212 individual SharedRAbundVector::get(int index){
216 /***********************************************************************/
218 vector <individual> SharedRAbundVector::getData(){
221 /***********************************************************************/
223 void SharedRAbundVector::clear(){
228 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
231 /***********************************************************************/
233 void SharedRAbundVector::push_back(int binSize, string groupName){
236 newGuy.abundance = binSize;
237 newGuy.group = groupName;
238 newGuy.bin = data.size();
240 data.push_back(newGuy);
243 if(binSize > maxRank){
249 catch(exception& e) {
250 m->errorOut(e, "SharedRAbundVector", "push_back");
255 /***********************************************************************/
257 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
260 newGuy.abundance = binSize;
261 newGuy.group = groupName;
264 data.insert(data.begin()+otu, newGuy);
267 if(binSize > maxRank){
273 catch(exception& e) {
274 m->errorOut(e, "SharedRAbundVector", "insert");
279 /***********************************************************************/
281 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
284 newGuy.abundance = binSize;
285 newGuy.group = groupName;
288 data.insert(data.begin(), newGuy);
291 if(binSize > maxRank){
297 catch(exception& e) {
298 m->errorOut(e, "SharedRAbundVector", "push_front");
303 /***********************************************************************/
304 void SharedRAbundVector::pop_back(){
305 numSeqs -= data[data.size()-1].abundance;
307 return data.pop_back();
310 /***********************************************************************/
313 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
314 return data.rbegin();
317 /***********************************************************************/
319 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
323 /***********************************************************************/
324 void SharedRAbundVector::resize(int size){
329 /***********************************************************************/
331 int SharedRAbundVector::size(){
336 /***********************************************************************/
337 void SharedRAbundVector::printHeaders(ostream& output){
339 string snumBins = toString(numBins);
340 output << "label\tGroup\tnumOtus\t";
341 if (m->sharedHeaderMode == "tax") {
342 for (int i = 0; i < numBins; i++) {
344 //if there is a bin label use it otherwise make one
345 string binLabel = "PhyloType";
346 string sbinNumber = toString(i+1);
347 if (sbinNumber.length() < snumBins.length()) {
348 int diff = snumBins.length() - sbinNumber.length();
349 for (int h = 0; h < diff; h++) { binLabel += "0"; }
351 binLabel += sbinNumber;
352 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
354 output << binLabel << '\t';
358 for (int i = 0; i < numBins; i++) {
359 //if there is a bin label use it otherwise make one
360 string binLabel = "Otu";
361 string sbinNumber = toString(i+1);
362 if (sbinNumber.length() < snumBins.length()) {
363 int diff = snumBins.length() - sbinNumber.length();
364 for (int h = 0; h < diff; h++) { binLabel += "0"; }
366 binLabel += sbinNumber;
367 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
369 output << binLabel << '\t';
374 m->printedHeaders = true;
376 catch(exception& e) {
377 m->errorOut(e, "SharedRAbundVector", "printHeaders");
381 /***********************************************************************/
382 void SharedRAbundVector::print(ostream& output) {
384 output << numBins << '\t';
386 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
389 catch(exception& e) {
390 m->errorOut(e, "SharedRAbundVector", "print");
394 /***********************************************************************/
395 string SharedRAbundVector::getGroup(){
399 /***********************************************************************/
401 void SharedRAbundVector::setGroup(string groupName){
404 /***********************************************************************/
405 int SharedRAbundVector::getGroupIndex() { return index; }
406 /***********************************************************************/
407 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
408 /***********************************************************************/
409 int SharedRAbundVector::getNumBins(){
413 /***********************************************************************/
415 int SharedRAbundVector::getNumSeqs(){
419 /***********************************************************************/
421 int SharedRAbundVector::getMaxRank(){
424 /***********************************************************************/
426 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
429 /***********************************************************************/
430 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
433 util = new SharedUtil();
435 vector<string> Groups = m->getGroups();
436 vector<string> allGroups = m->getAllGroups();
437 util->setGroups(Groups, allGroups);
438 m->setGroups(Groups);
441 for (int i = 0; i < lookup.size(); i++) {
442 //if this sharedrabund is not from a group the user wants then delete it.
443 if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) {
445 delete lookup[i]; lookup[i] = NULL;
446 lookup.erase(lookup.begin()+i);
453 if (remove) { eliminateZeroOTUS(lookup); }
457 catch(exception& e) {
458 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
462 //**********************************************************************************************************************
463 int SharedRAbundVector::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
466 vector<SharedRAbundVector*> newLookup;
467 for (int i = 0; i < thislookup.size(); i++) {
468 SharedRAbundVector* temp = new SharedRAbundVector();
469 temp->setLabel(thislookup[i]->getLabel());
470 temp->setGroup(thislookup[i]->getGroup());
471 newLookup.push_back(temp);
475 vector<string> newBinLabels;
476 string snumBins = toString(thislookup[0]->getNumBins());
477 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
478 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
480 //look at each sharedRabund and make sure they are not all zero
482 for (int j = 0; j < thislookup.size(); j++) {
483 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
486 //if they are not all zero add this bin
488 for (int j = 0; j < thislookup.size(); j++) {
489 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
492 //if there is a bin label use it otherwise make one
493 string binLabel = "Otu";
494 string sbinNumber = toString(i+1);
495 if (sbinNumber.length() < snumBins.length()) {
496 int diff = snumBins.length() - sbinNumber.length();
497 for (int h = 0; h < diff; h++) { binLabel += "0"; }
499 binLabel += sbinNumber;
500 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
502 newBinLabels.push_back(binLabel);
506 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
508 thislookup = newLookup;
509 m->currentBinLabels = newBinLabels;
514 catch(exception& e) {
515 m->errorOut(e, "SharedRAbundVector", "eliminateZeroOTUS");
520 /***********************************************************************/
521 vector<SharedRAbundFloatVector*> SharedRAbundVector::getSharedRAbundFloatVectors(vector<SharedRAbundVector*> thislookup){
523 vector<SharedRAbundFloatVector*> newLookupFloat;
524 for (int i = 0; i < lookup.size(); i++) {
525 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
526 temp->setLabel(thislookup[i]->getLabel());
527 temp->setGroup(thislookup[i]->getGroup());
528 newLookupFloat.push_back(temp);
531 for (int i = 0; i < thislookup.size(); i++) {
533 for (int j = 0; j < thislookup[i]->getNumBins(); j++) {
535 if (m->control_pressed) { return newLookupFloat; }
537 int abund = thislookup[i]->getAbundance(j);
539 float relabund = abund / (float) thislookup[i]->getNumSeqs();
541 newLookupFloat[i]->push_back(relabund, thislookup[i]->getGroup());
545 return newLookupFloat;
547 catch(exception& e) {
548 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
552 /***********************************************************************/
554 RAbundVector SharedRAbundVector::getRAbundVector() {
558 for (int i = 0; i < data.size(); i++) {
559 if(data[i].abundance != 0) {
560 rav.push_back(data[i].abundance);
567 catch(exception& e) {
568 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
572 /***********************************************************************/
574 RAbundVector SharedRAbundVector::getRAbundVector2() {
577 for(int i = 0; i < numBins; i++)
578 if(data[i].abundance != 0)
579 rav.push_back(data[i].abundance-1);
582 catch(exception& e) {
583 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
587 /***********************************************************************/
589 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
591 SharedSAbundVector sav(maxRank+1);
593 for(int i=0;i<data.size();i++){
594 int abund = data[i].abundance;
595 sav.set(abund, sav.getAbundance(abund) + 1, group);
598 sav.set(0, 0, group);
604 catch(exception& e) {
605 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
609 /***********************************************************************/
611 SAbundVector SharedRAbundVector::getSAbundVector() {
613 SAbundVector sav(maxRank+1);
615 for(int i=0;i<data.size();i++){
616 int abund = data[i].abundance;
617 sav.set(abund, sav.get(abund) + 1);
623 catch(exception& e) {
624 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");
629 /***********************************************************************/
631 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
633 SharedOrderVector ov;
635 for(int i=0;i<data.size();i++){
636 for(int j=0;j<data[i].abundance;j++){
637 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
640 random_shuffle(ov.begin(), ov.end());
647 catch(exception& e) {
648 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
652 /***********************************************************************/
654 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
657 for(int i=0;i<numBins;i++){
658 for(int j=0;j<data[i].abundance;j++){
662 random_shuffle(ov.begin(), ov.end());
668 catch(exception& e) {
669 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
674 /***********************************************************************/