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 /***********************************************************************/
215 //returns vector of abundances
216 vector<int> SharedRAbundVector::getAbundances(){
218 for (int i = 0; i < data.size(); i++) {
219 abunds.push_back(data[i].abundance);
226 /***********************************************************************/
228 int SharedRAbundVector::numNZ(){
230 for(int i = 1; i < numBins; i++)
231 if(data[i].abundance > 0)
235 /***********************************************************************/
237 void SharedRAbundVector::sortD(){
238 struct individual indObj;
239 sort(data.begin()+1, data.end(), indObj);
241 /***********************************************************************/
243 individual SharedRAbundVector::get(int index){
247 /***********************************************************************/
249 vector <individual> SharedRAbundVector::getData(){
252 /***********************************************************************/
254 void SharedRAbundVector::clear(){
259 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
262 /***********************************************************************/
264 void SharedRAbundVector::push_back(int binSize, string groupName){
267 newGuy.abundance = binSize;
268 newGuy.group = groupName;
269 newGuy.bin = data.size();
271 data.push_back(newGuy);
274 if(binSize > maxRank){
280 catch(exception& e) {
281 m->errorOut(e, "SharedRAbundVector", "push_back");
286 /***********************************************************************/
288 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
291 newGuy.abundance = binSize;
292 newGuy.group = groupName;
295 data.insert(data.begin()+otu, newGuy);
298 if(binSize > maxRank){
304 catch(exception& e) {
305 m->errorOut(e, "SharedRAbundVector", "insert");
310 /***********************************************************************/
312 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
315 newGuy.abundance = binSize;
316 newGuy.group = groupName;
319 data.insert(data.begin(), newGuy);
322 if(binSize > maxRank){
328 catch(exception& e) {
329 m->errorOut(e, "SharedRAbundVector", "push_front");
334 /***********************************************************************/
335 void SharedRAbundVector::pop_back(){
336 numSeqs -= data[data.size()-1].abundance;
338 return data.pop_back();
341 /***********************************************************************/
344 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
345 return data.rbegin();
348 /***********************************************************************/
350 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
354 /***********************************************************************/
355 void SharedRAbundVector::resize(int size){
360 /***********************************************************************/
362 int SharedRAbundVector::size(){
367 /***********************************************************************/
368 void SharedRAbundVector::printHeaders(ostream& output){
370 string snumBins = toString(numBins);
371 output << "label\tGroup\tnumOtus\t";
372 if (m->sharedHeaderMode == "tax") {
373 for (int i = 0; i < numBins; i++) {
375 //if there is a bin label use it otherwise make one
376 string binLabel = "PhyloType";
377 string sbinNumber = toString(i+1);
378 if (sbinNumber.length() < snumBins.length()) {
379 int diff = snumBins.length() - sbinNumber.length();
380 for (int h = 0; h < diff; h++) { binLabel += "0"; }
382 binLabel += sbinNumber;
383 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
385 output << binLabel << '\t';
389 for (int i = 0; i < numBins; i++) {
390 //if there is a bin label use it otherwise make one
391 string binLabel = "Otu";
392 string sbinNumber = toString(i+1);
393 if (sbinNumber.length() < snumBins.length()) {
394 int diff = snumBins.length() - sbinNumber.length();
395 for (int h = 0; h < diff; h++) { binLabel += "0"; }
397 binLabel += sbinNumber;
398 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
400 output << binLabel << '\t';
405 m->printedHeaders = true;
407 catch(exception& e) {
408 m->errorOut(e, "SharedRAbundVector", "printHeaders");
412 /***********************************************************************/
413 void SharedRAbundVector::print(ostream& output) {
415 output << numBins << '\t';
417 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
420 catch(exception& e) {
421 m->errorOut(e, "SharedRAbundVector", "print");
425 /***********************************************************************/
426 string SharedRAbundVector::getGroup(){
430 /***********************************************************************/
432 void SharedRAbundVector::setGroup(string groupName){
435 /***********************************************************************/
436 int SharedRAbundVector::getGroupIndex() { return index; }
437 /***********************************************************************/
438 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
439 /***********************************************************************/
440 int SharedRAbundVector::getNumBins(){
444 /***********************************************************************/
446 int SharedRAbundVector::getNumSeqs(){
450 /***********************************************************************/
452 int SharedRAbundVector::getMaxRank(){
455 /***********************************************************************/
457 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
460 /***********************************************************************/
461 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
464 util = new SharedUtil();
466 vector<string> Groups = m->getGroups();
467 vector<string> allGroups = m->getAllGroups();
468 util->setGroups(Groups, allGroups);
469 m->setGroups(Groups);
472 for (int i = 0; i < lookup.size(); i++) {
473 //if this sharedrabund is not from a group the user wants then delete it.
474 if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) {
476 delete lookup[i]; lookup[i] = NULL;
477 lookup.erase(lookup.begin()+i);
484 if (remove) { eliminateZeroOTUS(lookup); }
488 catch(exception& e) {
489 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
493 //**********************************************************************************************************************
494 int SharedRAbundVector::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
497 vector<SharedRAbundVector*> newLookup;
498 for (int i = 0; i < thislookup.size(); i++) {
499 SharedRAbundVector* temp = new SharedRAbundVector();
500 temp->setLabel(thislookup[i]->getLabel());
501 temp->setGroup(thislookup[i]->getGroup());
502 newLookup.push_back(temp);
506 vector<string> newBinLabels;
507 string snumBins = toString(thislookup[0]->getNumBins());
508 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
509 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
511 //look at each sharedRabund and make sure they are not all zero
513 for (int j = 0; j < thislookup.size(); j++) {
514 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
517 //if they are not all zero add this bin
519 for (int j = 0; j < thislookup.size(); j++) {
520 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
523 //if there is a bin label use it otherwise make one
524 string binLabel = "Otu";
525 string sbinNumber = toString(i+1);
526 if (sbinNumber.length() < snumBins.length()) {
527 int diff = snumBins.length() - sbinNumber.length();
528 for (int h = 0; h < diff; h++) { binLabel += "0"; }
530 binLabel += sbinNumber;
531 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
533 newBinLabels.push_back(binLabel);
537 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
539 thislookup = newLookup;
540 m->currentBinLabels = newBinLabels;
545 catch(exception& e) {
546 m->errorOut(e, "SharedRAbundVector", "eliminateZeroOTUS");
551 /***********************************************************************/
552 vector<SharedRAbundFloatVector*> SharedRAbundVector::getSharedRAbundFloatVectors(vector<SharedRAbundVector*> thislookup){
554 vector<SharedRAbundFloatVector*> newLookupFloat;
555 for (int i = 0; i < lookup.size(); i++) {
556 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
557 temp->setLabel(thislookup[i]->getLabel());
558 temp->setGroup(thislookup[i]->getGroup());
559 newLookupFloat.push_back(temp);
562 for (int i = 0; i < thislookup.size(); i++) {
564 for (int j = 0; j < thislookup[i]->getNumBins(); j++) {
566 if (m->control_pressed) { return newLookupFloat; }
568 int abund = thislookup[i]->getAbundance(j);
570 float relabund = abund / (float) thislookup[i]->getNumSeqs();
572 newLookupFloat[i]->push_back(relabund, thislookup[i]->getGroup());
576 return newLookupFloat;
578 catch(exception& e) {
579 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
583 /***********************************************************************/
585 RAbundVector SharedRAbundVector::getRAbundVector() {
589 for (int i = 0; i < data.size(); i++) {
590 if(data[i].abundance != 0) {
591 rav.push_back(data[i].abundance);
598 catch(exception& e) {
599 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
603 /***********************************************************************/
605 RAbundVector SharedRAbundVector::getRAbundVector2() {
608 for(int i = 0; i < numBins; i++)
609 if(data[i].abundance != 0)
610 rav.push_back(data[i].abundance-1);
613 catch(exception& e) {
614 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
618 /***********************************************************************/
620 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
622 SharedSAbundVector sav(maxRank+1);
624 for(int i=0;i<data.size();i++){
625 int abund = data[i].abundance;
626 sav.set(abund, sav.getAbundance(abund) + 1, group);
629 sav.set(0, 0, group);
635 catch(exception& e) {
636 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
640 /***********************************************************************/
642 SAbundVector SharedRAbundVector::getSAbundVector() {
644 SAbundVector sav(maxRank+1);
646 for(int i=0;i<data.size();i++){
647 int abund = data[i].abundance;
648 sav.set(abund, sav.get(abund) + 1);
654 catch(exception& e) {
655 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");
660 /***********************************************************************/
662 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
664 SharedOrderVector ov;
666 for(int i=0;i<data.size();i++){
667 for(int j=0;j<data[i].abundance;j++){
668 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
671 random_shuffle(ov.begin(), ov.end());
678 catch(exception& e) {
679 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
683 /***********************************************************************/
685 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
688 for(int i=0;i<numBins;i++){
689 for(int j=0;j<data[i].abundance;j++){
693 random_shuffle(ov.begin(), ov.end());
699 catch(exception& e) {
700 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
705 /***********************************************************************/