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);
98 f >> label >> groupN >> num;
100 //read in first row since you know there is at least 1 group.
103 //make binlabels because we don't have any
104 string snumBins = toString(num);
105 m->binLabelsInFile.clear();
106 for (int i = 0; i < num; i++) {
107 //if there is a bin label use it otherwise make one
108 string binLabel = "Otu";
109 string sbinNumber = toString(i+1);
110 if (sbinNumber.length() < snumBins.length()) {
111 int diff = snumBins.length() - sbinNumber.length();
112 for (int h = 0; h < diff; h++) { binLabel += "0"; }
114 binLabel += sbinNumber;
115 m->binLabelsInFile.push_back(binLabel);
119 label = m->saveNextLabel;
121 //read in first row since you know there is at least 1 group.
125 //reset labels, currentLabels may have gotten changed as otus were eliminated because of group choices or sampling
126 m->currentBinLabels = m->binLabelsInFile;
130 //add new vector to lookup
131 SharedRAbundVector* temp = new SharedRAbundVector();
132 lookup.push_back(temp);
133 lookup[0]->setLabel(label);
134 lookup[0]->setGroup(groupN);
136 allGroups.push_back(groupN);
138 //fill vector. data = first sharedrabund in file
139 for(int i=0;i<num;i++){
142 lookup[0]->push_back(inputData, groupN); //abundance, bin, group
143 push_back(inputData, groupN);
145 if (inputData > maxRank) { maxRank = inputData; }
150 if (!(f.eof())) { f >> nextLabel; }
152 //read the rest of the groups info in
153 while ((nextLabel == holdLabel) && (f.eof() != true)) {
157 allGroups.push_back(groupN);
159 //add new vector to lookup
160 temp = new SharedRAbundVector();
161 lookup.push_back(temp);
162 lookup[count]->setLabel(label);
163 lookup[count]->setGroup(groupN);
166 for(int i=0;i<num;i++){
169 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
174 if (f.eof() != true) { f >> nextLabel; }
176 m->saveNextLabel = nextLabel;
177 m->setAllGroups(allGroups);
179 catch(exception& e) {
180 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
185 /***********************************************************************/
187 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
189 int oldBinSize = data[binNumber].abundance;
190 data[binNumber].abundance = newBinSize;
191 data[binNumber].group = groupname;
193 if(newBinSize > maxRank) { maxRank = newBinSize; }
195 numSeqs += (newBinSize - oldBinSize);
197 catch(exception& e) {
198 m->errorOut(e, "SharedRAbundVector", "set");
202 /***********************************************************************/
204 void SharedRAbundVector::setData(vector <individual> newData){
208 /***********************************************************************/
210 int SharedRAbundVector::getAbundance(int index){
211 return data[index].abundance;
214 /***********************************************************************/
216 int SharedRAbundVector::numNZ(){
218 for(int i = 1; i < numBins; i++)
219 if(data[i].abundance > 0)
223 /***********************************************************************/
225 void SharedRAbundVector::sortD(){
226 struct individual indObj;
227 sort(data.begin()+1, data.end(), indObj);
229 /***********************************************************************/
231 individual SharedRAbundVector::get(int index){
235 /***********************************************************************/
237 vector <individual> SharedRAbundVector::getData(){
240 /***********************************************************************/
242 void SharedRAbundVector::clear(){
247 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
250 /***********************************************************************/
252 void SharedRAbundVector::push_back(int binSize, string groupName){
255 newGuy.abundance = binSize;
256 newGuy.group = groupName;
257 newGuy.bin = data.size();
259 data.push_back(newGuy);
262 if(binSize > maxRank){
268 catch(exception& e) {
269 m->errorOut(e, "SharedRAbundVector", "push_back");
274 /***********************************************************************/
276 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
279 newGuy.abundance = binSize;
280 newGuy.group = groupName;
283 data.insert(data.begin()+otu, newGuy);
286 if(binSize > maxRank){
292 catch(exception& e) {
293 m->errorOut(e, "SharedRAbundVector", "insert");
298 /***********************************************************************/
300 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
303 newGuy.abundance = binSize;
304 newGuy.group = groupName;
307 data.insert(data.begin(), newGuy);
310 if(binSize > maxRank){
316 catch(exception& e) {
317 m->errorOut(e, "SharedRAbundVector", "push_front");
322 /***********************************************************************/
323 void SharedRAbundVector::pop_back(){
324 numSeqs -= data[data.size()-1].abundance;
326 return data.pop_back();
329 /***********************************************************************/
332 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
333 return data.rbegin();
336 /***********************************************************************/
338 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
342 /***********************************************************************/
343 void SharedRAbundVector::resize(int size){
348 /***********************************************************************/
350 int SharedRAbundVector::size(){
355 /***********************************************************************/
356 void SharedRAbundVector::printHeaders(ostream& output){
358 string snumBins = toString(numBins);
359 output << "label\tGroup\tnumOtus\t";
360 if (m->sharedHeaderMode == "tax") {
361 for (int i = 0; i < numBins; i++) {
363 //if there is a bin label use it otherwise make one
364 string binLabel = "PhyloType";
365 string sbinNumber = toString(i+1);
366 if (sbinNumber.length() < snumBins.length()) {
367 int diff = snumBins.length() - sbinNumber.length();
368 for (int h = 0; h < diff; h++) { binLabel += "0"; }
370 binLabel += sbinNumber;
371 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
373 output << binLabel << '\t';
377 for (int i = 0; i < numBins; i++) {
378 //if there is a bin label use it otherwise make one
379 string binLabel = "Otu";
380 string sbinNumber = toString(i+1);
381 if (sbinNumber.length() < snumBins.length()) {
382 int diff = snumBins.length() - sbinNumber.length();
383 for (int h = 0; h < diff; h++) { binLabel += "0"; }
385 binLabel += sbinNumber;
386 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
388 output << binLabel << '\t';
393 m->printedHeaders = true;
395 catch(exception& e) {
396 m->errorOut(e, "SharedRAbundVector", "printHeaders");
400 /***********************************************************************/
401 void SharedRAbundVector::print(ostream& output) {
403 output << numBins << '\t';
405 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
408 catch(exception& e) {
409 m->errorOut(e, "SharedRAbundVector", "print");
413 /***********************************************************************/
414 string SharedRAbundVector::getGroup(){
418 /***********************************************************************/
420 void SharedRAbundVector::setGroup(string groupName){
423 /***********************************************************************/
424 int SharedRAbundVector::getGroupIndex() { return index; }
425 /***********************************************************************/
426 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
427 /***********************************************************************/
428 int SharedRAbundVector::getNumBins(){
432 /***********************************************************************/
434 int SharedRAbundVector::getNumSeqs(){
438 /***********************************************************************/
440 int SharedRAbundVector::getMaxRank(){
443 /***********************************************************************/
445 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
448 /***********************************************************************/
449 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
452 util = new SharedUtil();
454 vector<string> Groups = m->getGroups();
455 vector<string> allGroups = m->getAllGroups();
456 util->setGroups(Groups, allGroups);
457 m->setGroups(Groups);
460 for (int i = 0; i < lookup.size(); i++) {
461 //if this sharedrabund is not from a group the user wants then delete it.
462 if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) {
464 delete lookup[i]; lookup[i] = NULL;
465 lookup.erase(lookup.begin()+i);
472 if (remove) { eliminateZeroOTUS(lookup); }
476 catch(exception& e) {
477 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
481 //**********************************************************************************************************************
482 int SharedRAbundVector::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
485 vector<SharedRAbundVector*> newLookup;
486 for (int i = 0; i < thislookup.size(); i++) {
487 SharedRAbundVector* temp = new SharedRAbundVector();
488 temp->setLabel(thislookup[i]->getLabel());
489 temp->setGroup(thislookup[i]->getGroup());
490 newLookup.push_back(temp);
494 vector<string> newBinLabels;
495 string snumBins = toString(thislookup[0]->getNumBins());
496 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
497 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
499 //look at each sharedRabund and make sure they are not all zero
501 for (int j = 0; j < thislookup.size(); j++) {
502 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
505 //if they are not all zero add this bin
507 for (int j = 0; j < thislookup.size(); j++) {
508 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
511 //if there is a bin label use it otherwise make one
512 string binLabel = "Otu";
513 string sbinNumber = toString(i+1);
514 if (sbinNumber.length() < snumBins.length()) {
515 int diff = snumBins.length() - sbinNumber.length();
516 for (int h = 0; h < diff; h++) { binLabel += "0"; }
518 binLabel += sbinNumber;
519 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
521 newBinLabels.push_back(binLabel);
525 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
527 thislookup = newLookup;
528 m->currentBinLabels = newBinLabels;
533 catch(exception& e) {
534 m->errorOut(e, "SharedRAbundVector", "eliminateZeroOTUS");
539 /***********************************************************************/
540 vector<SharedRAbundFloatVector*> SharedRAbundVector::getSharedRAbundFloatVectors(vector<SharedRAbundVector*> thislookup){
542 vector<SharedRAbundFloatVector*> newLookupFloat;
543 for (int i = 0; i < lookup.size(); i++) {
544 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
545 temp->setLabel(thislookup[i]->getLabel());
546 temp->setGroup(thislookup[i]->getGroup());
547 newLookupFloat.push_back(temp);
550 for (int i = 0; i < thislookup.size(); i++) {
552 for (int j = 0; j < thislookup[i]->getNumBins(); j++) {
554 if (m->control_pressed) { return newLookupFloat; }
556 int abund = thislookup[i]->getAbundance(j);
558 float relabund = abund / (float) thislookup[i]->getNumSeqs();
560 newLookupFloat[i]->push_back(relabund, thislookup[i]->getGroup());
564 return newLookupFloat;
566 catch(exception& e) {
567 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
571 /***********************************************************************/
573 RAbundVector SharedRAbundVector::getRAbundVector() {
577 for (int i = 0; i < data.size(); i++) {
578 if(data[i].abundance != 0) {
579 rav.push_back(data[i].abundance);
586 catch(exception& e) {
587 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
591 /***********************************************************************/
593 RAbundVector SharedRAbundVector::getRAbundVector2() {
596 for(int i = 0; i < numBins; i++)
597 if(data[i].abundance != 0)
598 rav.push_back(data[i].abundance-1);
601 catch(exception& e) {
602 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
606 /***********************************************************************/
608 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
610 SharedSAbundVector sav(maxRank+1);
612 for(int i=0;i<data.size();i++){
613 int abund = data[i].abundance;
614 sav.set(abund, sav.getAbundance(abund) + 1, group);
617 sav.set(0, 0, group);
623 catch(exception& e) {
624 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
628 /***********************************************************************/
630 SAbundVector SharedRAbundVector::getSAbundVector() {
632 SAbundVector sav(maxRank+1);
634 for(int i=0;i<data.size();i++){
635 int abund = data[i].abundance;
636 sav.set(abund, sav.get(abund) + 1);
642 catch(exception& e) {
643 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");
648 /***********************************************************************/
650 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
652 SharedOrderVector ov;
654 for(int i=0;i<data.size();i++){
655 for(int j=0;j<data[i].abundance;j++){
656 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
659 random_shuffle(ov.begin(), ov.end());
666 catch(exception& e) {
667 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
671 /***********************************************************************/
673 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
676 for(int i=0;i<numBins;i++){
677 for(int j=0;j<data[i].abundance;j++){
681 random_shuffle(ov.begin(), ov.end());
687 catch(exception& e) {
688 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
693 /***********************************************************************/