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.
124 if (m->debug) { m->mothurOut("[DEBUG]: "+ groupN + '\t' + toString(num)); }
127 //reset labels, currentLabels may have gotten changed as otus were eliminated because of group choices or sampling
128 m->currentBinLabels = m->binLabelsInFile;
132 //add new vector to lookup
133 SharedRAbundVector* temp = new SharedRAbundVector();
134 lookup.push_back(temp);
135 lookup[0]->setLabel(label);
136 lookup[0]->setGroup(groupN);
138 allGroups.push_back(groupN);
140 //fill vector. data = first sharedrabund in file
141 for(int i=0;i<num;i++){
143 if (m->debug) { m->mothurOut("[DEBUG]: OTU" + toString(i+1)+ '\t' +toString(inputData)); }
145 lookup[0]->push_back(inputData, groupN); //abundance, bin, group
146 push_back(inputData, groupN);
148 if (inputData > maxRank) { maxRank = inputData; }
153 if (!(f.eof())) { f >> nextLabel; }
155 //read the rest of the groups info in
156 while ((nextLabel == holdLabel) && (f.eof() != true)) {
158 if (m->debug) { m->mothurOut("[DEBUG]: "+ groupN + '\t' + toString(num)); }
161 allGroups.push_back(groupN);
163 //add new vector to lookup
164 temp = new SharedRAbundVector();
165 lookup.push_back(temp);
166 lookup[count]->setLabel(label);
167 lookup[count]->setGroup(groupN);
170 for(int i=0;i<num;i++){
172 if (m->debug) { m->mothurOut("[DEBUG]: OTU" + toString(i+1)+ '\t' +toString(inputData)); }
174 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
179 if (f.eof() != true) { f >> nextLabel; }
181 m->saveNextLabel = nextLabel;
182 m->setAllGroups(allGroups);
184 catch(exception& e) {
185 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
190 /***********************************************************************/
192 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
194 int oldBinSize = data[binNumber].abundance;
195 data[binNumber].abundance = newBinSize;
196 data[binNumber].group = groupname;
198 if(newBinSize > maxRank) { maxRank = newBinSize; }
200 numSeqs += (newBinSize - oldBinSize);
202 catch(exception& e) {
203 m->errorOut(e, "SharedRAbundVector", "set");
207 /***********************************************************************/
209 void SharedRAbundVector::setData(vector <individual> newData){
213 /***********************************************************************/
215 int SharedRAbundVector::getAbundance(int index){
216 return data[index].abundance;
219 /***********************************************************************/
220 //returns vector of abundances
221 vector<int> SharedRAbundVector::getAbundances(){
223 for (int i = 0; i < data.size(); i++) {
224 abunds.push_back(data[i].abundance);
231 /***********************************************************************/
233 int SharedRAbundVector::numNZ(){
235 for(int i = 1; i < numBins; i++)
236 if(data[i].abundance > 0)
240 /***********************************************************************/
242 void SharedRAbundVector::sortD(){
243 struct individual indObj;
244 sort(data.begin()+1, data.end(), indObj);
246 /***********************************************************************/
248 individual SharedRAbundVector::get(int index){
252 /***********************************************************************/
254 vector <individual> SharedRAbundVector::getData(){
257 /***********************************************************************/
259 void SharedRAbundVector::clear(){
264 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
267 /***********************************************************************/
269 void SharedRAbundVector::push_back(int binSize, string groupName){
272 newGuy.abundance = binSize;
273 newGuy.group = groupName;
274 newGuy.bin = data.size();
276 data.push_back(newGuy);
279 if(binSize > maxRank){
285 catch(exception& e) {
286 m->errorOut(e, "SharedRAbundVector", "push_back");
291 /***********************************************************************/
293 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
296 newGuy.abundance = binSize;
297 newGuy.group = groupName;
300 data.insert(data.begin()+otu, newGuy);
303 if(binSize > maxRank){
309 catch(exception& e) {
310 m->errorOut(e, "SharedRAbundVector", "insert");
315 /***********************************************************************/
317 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
320 newGuy.abundance = binSize;
321 newGuy.group = groupName;
324 data.insert(data.begin(), newGuy);
327 if(binSize > maxRank){
333 catch(exception& e) {
334 m->errorOut(e, "SharedRAbundVector", "push_front");
339 /***********************************************************************/
340 void SharedRAbundVector::pop_back(){
341 numSeqs -= data[data.size()-1].abundance;
343 return data.pop_back();
346 /***********************************************************************/
349 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
350 return data.rbegin();
353 /***********************************************************************/
355 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
359 /***********************************************************************/
360 void SharedRAbundVector::resize(int size){
365 /***********************************************************************/
367 int SharedRAbundVector::size(){
372 /***********************************************************************/
373 void SharedRAbundVector::printHeaders(ostream& output){
375 string snumBins = toString(numBins);
376 output << "label\tGroup\tnumOtus\t";
377 if (m->sharedHeaderMode == "tax") {
378 for (int i = 0; i < numBins; i++) {
380 //if there is a bin label use it otherwise make one
381 string binLabel = "PhyloType";
382 string sbinNumber = toString(i+1);
383 if (sbinNumber.length() < snumBins.length()) {
384 int diff = snumBins.length() - sbinNumber.length();
385 for (int h = 0; h < diff; h++) { binLabel += "0"; }
387 binLabel += sbinNumber;
388 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
390 output << binLabel << '\t';
394 for (int i = 0; i < numBins; i++) {
395 //if there is a bin label use it otherwise make one
396 string binLabel = "Otu";
397 string sbinNumber = toString(i+1);
398 if (sbinNumber.length() < snumBins.length()) {
399 int diff = snumBins.length() - sbinNumber.length();
400 for (int h = 0; h < diff; h++) { binLabel += "0"; }
402 binLabel += sbinNumber;
403 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
405 output << binLabel << '\t';
410 m->printedHeaders = true;
412 catch(exception& e) {
413 m->errorOut(e, "SharedRAbundVector", "printHeaders");
417 /***********************************************************************/
418 void SharedRAbundVector::print(ostream& output) {
420 output << numBins << '\t';
422 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
425 catch(exception& e) {
426 m->errorOut(e, "SharedRAbundVector", "print");
430 /***********************************************************************/
431 string SharedRAbundVector::getGroup(){
435 /***********************************************************************/
437 void SharedRAbundVector::setGroup(string groupName){
440 /***********************************************************************/
441 int SharedRAbundVector::getGroupIndex() { return index; }
442 /***********************************************************************/
443 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
444 /***********************************************************************/
445 int SharedRAbundVector::getNumBins(){
449 /***********************************************************************/
451 int SharedRAbundVector::getNumSeqs(){
455 /***********************************************************************/
457 int SharedRAbundVector::getMaxRank(){
460 /***********************************************************************/
462 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
465 /***********************************************************************/
466 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
469 util = new SharedUtil();
471 vector<string> Groups = m->getGroups();
472 vector<string> allGroups = m->getAllGroups();
473 util->setGroups(Groups, allGroups);
474 m->setGroups(Groups);
477 for (int i = 0; i < lookup.size(); i++) {
478 //if this sharedrabund is not from a group the user wants then delete it.
479 if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) {
481 delete lookup[i]; lookup[i] = NULL;
482 lookup.erase(lookup.begin()+i);
489 if (remove) { eliminateZeroOTUS(lookup); }
493 catch(exception& e) {
494 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
498 //**********************************************************************************************************************
499 int SharedRAbundVector::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
502 vector<SharedRAbundVector*> newLookup;
503 for (int i = 0; i < thislookup.size(); i++) {
504 SharedRAbundVector* temp = new SharedRAbundVector();
505 temp->setLabel(thislookup[i]->getLabel());
506 temp->setGroup(thislookup[i]->getGroup());
507 newLookup.push_back(temp);
511 vector<string> newBinLabels;
512 string snumBins = toString(thislookup[0]->getNumBins());
513 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
514 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
516 //look at each sharedRabund and make sure they are not all zero
518 for (int j = 0; j < thislookup.size(); j++) {
519 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
522 //if they are not all zero add this bin
524 for (int j = 0; j < thislookup.size(); j++) {
525 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
528 //if there is a bin label use it otherwise make one
529 string binLabel = "Otu";
530 string sbinNumber = toString(i+1);
531 if (sbinNumber.length() < snumBins.length()) {
532 int diff = snumBins.length() - sbinNumber.length();
533 for (int h = 0; h < diff; h++) { binLabel += "0"; }
535 binLabel += sbinNumber;
536 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
538 newBinLabels.push_back(binLabel);
542 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
544 thislookup = newLookup;
545 m->currentBinLabels = newBinLabels;
550 catch(exception& e) {
551 m->errorOut(e, "SharedRAbundVector", "eliminateZeroOTUS");
556 /***********************************************************************/
557 vector<SharedRAbundFloatVector*> SharedRAbundVector::getSharedRAbundFloatVectors(vector<SharedRAbundVector*> thislookup){
559 vector<SharedRAbundFloatVector*> newLookupFloat;
560 for (int i = 0; i < lookup.size(); i++) {
561 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
562 temp->setLabel(thislookup[i]->getLabel());
563 temp->setGroup(thislookup[i]->getGroup());
564 newLookupFloat.push_back(temp);
567 for (int i = 0; i < thislookup.size(); i++) {
569 for (int j = 0; j < thislookup[i]->getNumBins(); j++) {
571 if (m->control_pressed) { return newLookupFloat; }
573 int abund = thislookup[i]->getAbundance(j);
575 float relabund = abund / (float) thislookup[i]->getNumSeqs();
577 newLookupFloat[i]->push_back(relabund, thislookup[i]->getGroup());
581 return newLookupFloat;
583 catch(exception& e) {
584 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
588 /***********************************************************************/
590 RAbundVector SharedRAbundVector::getRAbundVector() {
594 for (int i = 0; i < data.size(); i++) {
595 if(data[i].abundance != 0) {
596 rav.push_back(data[i].abundance);
603 catch(exception& e) {
604 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
608 /***********************************************************************/
610 RAbundVector SharedRAbundVector::getRAbundVector2() {
613 for(int i = 0; i < numBins; i++)
614 if(data[i].abundance != 0)
615 rav.push_back(data[i].abundance-1);
618 catch(exception& e) {
619 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
623 /***********************************************************************/
625 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
627 SharedSAbundVector sav(maxRank+1);
629 for(int i=0;i<data.size();i++){
630 int abund = data[i].abundance;
631 sav.set(abund, sav.getAbundance(abund) + 1, group);
634 sav.set(0, 0, group);
640 catch(exception& e) {
641 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
645 /***********************************************************************/
647 SAbundVector SharedRAbundVector::getSAbundVector() {
649 SAbundVector sav(maxRank+1);
651 for(int i=0;i<data.size();i++){
652 int abund = data[i].abundance;
653 sav.set(abund, sav.get(abund) + 1);
659 catch(exception& e) {
660 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");
665 /***********************************************************************/
667 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
669 SharedOrderVector ov;
671 for(int i=0;i<data.size();i++){
672 for(int j=0;j<data[i].abundance;j++){
673 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
676 random_shuffle(ov.begin(), ov.end());
683 catch(exception& e) {
684 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
688 /***********************************************************************/
690 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
693 for(int i=0;i<numBins;i++){
694 for(int j=0;j<data[i].abundance;j++){
698 random_shuffle(ov.begin(), ov.end());
704 catch(exception& e) {
705 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
710 /***********************************************************************/