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);
159 catch(exception& e) {
160 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
165 /***********************************************************************/
167 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
169 int oldBinSize = data[binNumber].abundance;
170 data[binNumber].abundance = newBinSize;
171 data[binNumber].group = groupname;
173 if(newBinSize > maxRank) { maxRank = newBinSize; }
175 numSeqs += (newBinSize - oldBinSize);
177 catch(exception& e) {
178 m->errorOut(e, "SharedRAbundVector", "set");
182 /***********************************************************************/
184 void SharedRAbundVector::setData(vector <individual> newData){
188 /***********************************************************************/
190 int SharedRAbundVector::getAbundance(int index){
191 return data[index].abundance;
194 /***********************************************************************/
196 int SharedRAbundVector::numNZ(){
198 for(int i = 1; i < numBins; i++)
199 if(data[i].abundance > 0)
203 /***********************************************************************/
205 void SharedRAbundVector::sortD(){
206 struct individual indObj;
207 sort(data.begin()+1, data.end(), indObj);
209 /***********************************************************************/
211 individual SharedRAbundVector::get(int index){
215 /***********************************************************************/
217 vector <individual> SharedRAbundVector::getData(){
220 /***********************************************************************/
222 void SharedRAbundVector::clear(){
227 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
230 /***********************************************************************/
232 void SharedRAbundVector::push_back(int binSize, string groupName){
235 newGuy.abundance = binSize;
236 newGuy.group = groupName;
237 newGuy.bin = data.size();
239 data.push_back(newGuy);
242 if(binSize > maxRank){
248 catch(exception& e) {
249 m->errorOut(e, "SharedRAbundVector", "push_back");
254 /***********************************************************************/
256 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
259 newGuy.abundance = binSize;
260 newGuy.group = groupName;
263 data.insert(data.begin()+otu, newGuy);
266 if(binSize > maxRank){
272 catch(exception& e) {
273 m->errorOut(e, "SharedRAbundVector", "insert");
278 /***********************************************************************/
280 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
283 newGuy.abundance = binSize;
284 newGuy.group = groupName;
287 data.insert(data.begin(), newGuy);
290 if(binSize > maxRank){
296 catch(exception& e) {
297 m->errorOut(e, "SharedRAbundVector", "push_front");
302 /***********************************************************************/
303 void SharedRAbundVector::pop_back(){
304 numSeqs -= data[data.size()-1].abundance;
306 return data.pop_back();
309 /***********************************************************************/
312 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
313 return data.rbegin();
316 /***********************************************************************/
318 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
322 /***********************************************************************/
323 void SharedRAbundVector::resize(int size){
328 /***********************************************************************/
330 int SharedRAbundVector::size(){
335 /***********************************************************************/
336 void SharedRAbundVector::printHeaders(ostream& output){
338 string snumBins = toString(numBins);
339 output << "label\tGroup\tnumOtus\t";
340 if (m->sharedHeaderMode == "tax") {
341 for (int i = 0; i < numBins; i++) {
343 //if there is a bin label use it otherwise make one
344 string binLabel = "PhyloType";
345 string sbinNumber = toString(i+1);
346 if (sbinNumber.length() < snumBins.length()) {
347 int diff = snumBins.length() - sbinNumber.length();
348 for (int h = 0; h < diff; h++) { binLabel += "0"; }
350 binLabel += sbinNumber;
351 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
353 output << binLabel << '\t';
357 for (int i = 0; i < numBins; i++) {
358 //if there is a bin label use it otherwise make one
359 string binLabel = "Otu";
360 string sbinNumber = toString(i+1);
361 if (sbinNumber.length() < snumBins.length()) {
362 int diff = snumBins.length() - sbinNumber.length();
363 for (int h = 0; h < diff; h++) { binLabel += "0"; }
365 binLabel += sbinNumber;
366 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
368 output << binLabel << '\t';
373 m->printedHeaders = true;
375 catch(exception& e) {
376 m->errorOut(e, "SharedRAbundVector", "printHeaders");
380 /***********************************************************************/
381 void SharedRAbundVector::print(ostream& output) {
383 output << numBins << '\t';
385 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
388 catch(exception& e) {
389 m->errorOut(e, "SharedRAbundVector", "print");
393 /***********************************************************************/
394 string SharedRAbundVector::getGroup(){
398 /***********************************************************************/
400 void SharedRAbundVector::setGroup(string groupName){
403 /***********************************************************************/
404 int SharedRAbundVector::getGroupIndex() { return index; }
405 /***********************************************************************/
406 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
407 /***********************************************************************/
408 int SharedRAbundVector::getNumBins(){
412 /***********************************************************************/
414 int SharedRAbundVector::getNumSeqs(){
418 /***********************************************************************/
420 int SharedRAbundVector::getMaxRank(){
423 /***********************************************************************/
425 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
428 /***********************************************************************/
429 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
432 util = new SharedUtil();
434 vector<string> Groups = m->getGroups();
435 vector<string> allGroups = m->getAllGroups();
436 util->setGroups(Groups, allGroups);
437 m->setGroups(Groups);
440 for (int i = 0; i < lookup.size(); i++) {
441 //if this sharedrabund is not from a group the user wants then delete it.
442 if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) {
444 delete lookup[i]; lookup[i] = NULL;
445 lookup.erase(lookup.begin()+i);
452 if (remove) { eliminateZeroOTUS(lookup); }
456 catch(exception& e) {
457 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
461 //**********************************************************************************************************************
462 int SharedRAbundVector::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
465 vector<SharedRAbundVector*> newLookup;
466 for (int i = 0; i < thislookup.size(); i++) {
467 SharedRAbundVector* temp = new SharedRAbundVector();
468 temp->setLabel(thislookup[i]->getLabel());
469 temp->setGroup(thislookup[i]->getGroup());
470 newLookup.push_back(temp);
474 vector<string> newBinLabels;
475 string snumBins = toString(thislookup[0]->getNumBins());
476 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
477 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
479 //look at each sharedRabund and make sure they are not all zero
481 for (int j = 0; j < thislookup.size(); j++) {
482 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
485 //if they are not all zero add this bin
487 for (int j = 0; j < thislookup.size(); j++) {
488 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
491 //if there is a bin label use it otherwise make one
492 string binLabel = "Otu";
493 string sbinNumber = toString(i+1);
494 if (sbinNumber.length() < snumBins.length()) {
495 int diff = snumBins.length() - sbinNumber.length();
496 for (int h = 0; h < diff; h++) { binLabel += "0"; }
498 binLabel += sbinNumber;
499 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
501 newBinLabels.push_back(binLabel);
505 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
507 thislookup = newLookup;
508 m->currentBinLabels = newBinLabels;
513 catch(exception& e) {
514 m->errorOut(e, "SharedRAbundVector", "eliminateZeroOTUS");
519 /***********************************************************************/
520 vector<SharedRAbundFloatVector*> SharedRAbundVector::getSharedRAbundFloatVectors(vector<SharedRAbundVector*> thislookup){
522 vector<SharedRAbundFloatVector*> newLookupFloat;
523 for (int i = 0; i < lookup.size(); i++) {
524 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
525 temp->setLabel(thislookup[i]->getLabel());
526 temp->setGroup(thislookup[i]->getGroup());
527 newLookupFloat.push_back(temp);
530 for (int i = 0; i < thislookup.size(); i++) {
532 for (int j = 0; j < thislookup[i]->getNumBins(); j++) {
534 if (m->control_pressed) { return newLookupFloat; }
536 int abund = thislookup[i]->getAbundance(j);
538 float relabund = abund / (float) thislookup[i]->getNumSeqs();
540 newLookupFloat[i]->push_back(relabund, thislookup[i]->getGroup());
544 return newLookupFloat;
546 catch(exception& e) {
547 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
551 /***********************************************************************/
553 RAbundVector SharedRAbundVector::getRAbundVector() {
557 for (int i = 0; i < data.size(); i++) {
558 if(data[i].abundance != 0) {
559 rav.push_back(data[i].abundance);
566 catch(exception& e) {
567 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
571 /***********************************************************************/
573 RAbundVector SharedRAbundVector::getRAbundVector2() {
576 for(int i = 0; i < numBins; i++)
577 if(data[i].abundance != 0)
578 rav.push_back(data[i].abundance-1);
581 catch(exception& e) {
582 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
586 /***********************************************************************/
588 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
590 SharedSAbundVector sav(maxRank+1);
592 for(int i=0;i<data.size();i++){
593 int abund = data[i].abundance;
594 sav.set(abund, sav.getAbundance(abund) + 1, group);
597 sav.set(0, 0, group);
603 catch(exception& e) {
604 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
608 /***********************************************************************/
610 SAbundVector SharedRAbundVector::getSAbundVector() {
612 SAbundVector sav(maxRank+1);
614 for(int i=0;i<data.size();i++){
615 int abund = data[i].abundance;
616 sav.set(abund, sav.get(abund) + 1);
622 catch(exception& e) {
623 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");
628 /***********************************************************************/
630 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
632 SharedOrderVector ov;
634 for(int i=0;i<data.size();i++){
635 for(int j=0;j<data[i].abundance;j++){
636 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
639 random_shuffle(ov.begin(), ov.end());
646 catch(exception& e) {
647 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
651 /***********************************************************************/
653 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
656 for(int i=0;i<numBins;i++){
657 for(int j=0;j<data[i].abundance;j++){
661 random_shuffle(ov.begin(), ov.end());
667 catch(exception& e) {
668 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
673 /***********************************************************************/