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.
109 //add new vector to lookup
110 SharedRAbundVector* temp = new SharedRAbundVector();
111 lookup.push_back(temp);
112 lookup[0]->setLabel(label);
113 lookup[0]->setGroup(groupN);
115 allGroups.push_back(groupN);
117 //fill vector. data = first sharedrabund in file
118 for(int i=0;i<num;i++){
121 lookup[0]->push_back(inputData, groupN); //abundance, bin, group
122 push_back(inputData, groupN);
124 if (inputData > maxRank) { maxRank = inputData; }
129 if (!(f.eof())) { f >> nextLabel; }
131 //read the rest of the groups info in
132 while ((nextLabel == holdLabel) && (f.eof() != true)) {
136 allGroups.push_back(groupN);
138 //add new vector to lookup
139 temp = new SharedRAbundVector();
140 lookup.push_back(temp);
141 lookup[count]->setLabel(label);
142 lookup[count]->setGroup(groupN);
145 for(int i=0;i<num;i++){
148 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
153 if (f.eof() != true) { f >> nextLabel; }
155 m->saveNextLabel = nextLabel;
156 m->setAllGroups(allGroups);
158 catch(exception& e) {
159 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
164 /***********************************************************************/
166 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
168 int oldBinSize = data[binNumber].abundance;
169 data[binNumber].abundance = newBinSize;
170 data[binNumber].group = groupname;
172 if(newBinSize > maxRank) { maxRank = newBinSize; }
174 numSeqs += (newBinSize - oldBinSize);
176 catch(exception& e) {
177 m->errorOut(e, "SharedRAbundVector", "set");
181 /***********************************************************************/
183 void SharedRAbundVector::setData(vector <individual> newData){
187 /***********************************************************************/
189 int SharedRAbundVector::getAbundance(int index){
190 return data[index].abundance;
193 /***********************************************************************/
195 int SharedRAbundVector::numNZ(){
197 for(int i = 1; i < numBins; i++)
198 if(data[i].abundance > 0)
202 /***********************************************************************/
204 void SharedRAbundVector::sortD(){
205 struct individual indObj;
206 sort(data.begin()+1, data.end(), indObj);
208 /***********************************************************************/
210 individual SharedRAbundVector::get(int index){
214 /***********************************************************************/
216 vector <individual> SharedRAbundVector::getData(){
219 /***********************************************************************/
221 void SharedRAbundVector::clear(){
226 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
229 /***********************************************************************/
231 void SharedRAbundVector::push_back(int binSize, string groupName){
234 newGuy.abundance = binSize;
235 newGuy.group = groupName;
236 newGuy.bin = data.size();
238 data.push_back(newGuy);
241 if(binSize > maxRank){
247 catch(exception& e) {
248 m->errorOut(e, "SharedRAbundVector", "push_back");
253 /***********************************************************************/
255 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
258 newGuy.abundance = binSize;
259 newGuy.group = groupName;
262 data.insert(data.begin()+otu, newGuy);
265 if(binSize > maxRank){
271 catch(exception& e) {
272 m->errorOut(e, "SharedRAbundVector", "insert");
277 /***********************************************************************/
279 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
282 newGuy.abundance = binSize;
283 newGuy.group = groupName;
286 data.insert(data.begin(), newGuy);
289 if(binSize > maxRank){
295 catch(exception& e) {
296 m->errorOut(e, "SharedRAbundVector", "push_front");
301 /***********************************************************************/
302 void SharedRAbundVector::pop_back(){
303 numSeqs -= data[data.size()-1].abundance;
305 return data.pop_back();
308 /***********************************************************************/
311 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
312 return data.rbegin();
315 /***********************************************************************/
317 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
321 /***********************************************************************/
322 void SharedRAbundVector::resize(int size){
327 /***********************************************************************/
329 int SharedRAbundVector::size(){
334 /***********************************************************************/
335 void SharedRAbundVector::printHeaders(ostream& output){
337 string snumBins = toString(numBins);
338 output << "label\tGroup\tnumOtus\t";
339 if (m->sharedHeaderMode == "tax") {
340 for (int i = 0; i < numBins; i++) {
342 //if there is a bin label use it otherwise make one
343 string binLabel = "PhyloType";
344 string sbinNumber = toString(i+1);
345 if (sbinNumber.length() < snumBins.length()) {
346 int diff = snumBins.length() - sbinNumber.length();
347 for (int h = 0; h < diff; h++) { binLabel += "0"; }
349 binLabel += sbinNumber;
350 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
352 output << binLabel << '\t';
356 for (int i = 0; i < numBins; i++) {
357 //if there is a bin label use it otherwise make one
358 string binLabel = "Otu";
359 string sbinNumber = toString(i+1);
360 if (sbinNumber.length() < snumBins.length()) {
361 int diff = snumBins.length() - sbinNumber.length();
362 for (int h = 0; h < diff; h++) { binLabel += "0"; }
364 binLabel += sbinNumber;
365 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
367 output << binLabel << '\t';
372 m->printedHeaders = true;
374 catch(exception& e) {
375 m->errorOut(e, "SharedRAbundVector", "printHeaders");
379 /***********************************************************************/
380 void SharedRAbundVector::print(ostream& output) {
382 output << numBins << '\t';
384 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
387 catch(exception& e) {
388 m->errorOut(e, "SharedRAbundVector", "print");
392 /***********************************************************************/
393 string SharedRAbundVector::getGroup(){
397 /***********************************************************************/
399 void SharedRAbundVector::setGroup(string groupName){
402 /***********************************************************************/
403 int SharedRAbundVector::getGroupIndex() { return index; }
404 /***********************************************************************/
405 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
406 /***********************************************************************/
407 int SharedRAbundVector::getNumBins(){
411 /***********************************************************************/
413 int SharedRAbundVector::getNumSeqs(){
417 /***********************************************************************/
419 int SharedRAbundVector::getMaxRank(){
422 /***********************************************************************/
424 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
427 /***********************************************************************/
428 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
431 util = new SharedUtil();
433 vector<string> Groups = m->getGroups();
434 vector<string> allGroups = m->getAllGroups();
435 util->setGroups(Groups, allGroups);
436 m->setGroups(Groups);
439 for (int i = 0; i < lookup.size(); i++) {
440 //if this sharedrabund is not from a group the user wants then delete it.
441 if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) {
443 delete lookup[i]; lookup[i] = NULL;
444 lookup.erase(lookup.begin()+i);
451 if (remove) { eliminateZeroOTUS(lookup); }
455 catch(exception& e) {
456 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
460 //**********************************************************************************************************************
461 int SharedRAbundVector::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
464 vector<SharedRAbundVector*> newLookup;
465 for (int i = 0; i < thislookup.size(); i++) {
466 SharedRAbundVector* temp = new SharedRAbundVector();
467 temp->setLabel(thislookup[i]->getLabel());
468 temp->setGroup(thislookup[i]->getGroup());
469 newLookup.push_back(temp);
473 vector<string> newBinLabels;
474 string snumBins = toString(thislookup[0]->getNumBins());
475 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
476 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
478 //look at each sharedRabund and make sure they are not all zero
480 for (int j = 0; j < thislookup.size(); j++) {
481 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
484 //if they are not all zero add this bin
486 for (int j = 0; j < thislookup.size(); j++) {
487 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
490 //if there is a bin label use it otherwise make one
491 string binLabel = "Otu";
492 string sbinNumber = toString(i+1);
493 if (sbinNumber.length() < snumBins.length()) {
494 int diff = snumBins.length() - sbinNumber.length();
495 for (int h = 0; h < diff; h++) { binLabel += "0"; }
497 binLabel += sbinNumber;
498 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
500 newBinLabels.push_back(binLabel);
504 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
506 thislookup = newLookup;
507 m->currentBinLabels = newBinLabels;
512 catch(exception& e) {
513 m->errorOut(e, "SharedRAbundVector", "eliminateZeroOTUS");
518 /***********************************************************************/
519 vector<SharedRAbundFloatVector*> SharedRAbundVector::getSharedRAbundFloatVectors(vector<SharedRAbundVector*> thislookup){
521 vector<SharedRAbundFloatVector*> newLookupFloat;
522 for (int i = 0; i < lookup.size(); i++) {
523 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
524 temp->setLabel(thislookup[i]->getLabel());
525 temp->setGroup(thislookup[i]->getGroup());
526 newLookupFloat.push_back(temp);
529 for (int i = 0; i < thislookup.size(); i++) {
531 for (int j = 0; j < thislookup[i]->getNumBins(); j++) {
533 if (m->control_pressed) { return newLookupFloat; }
535 int abund = thislookup[i]->getAbundance(j);
537 float relabund = abund / (float) thislookup[i]->getNumSeqs();
539 newLookupFloat[i]->push_back(relabund, thislookup[i]->getGroup());
543 return newLookupFloat;
545 catch(exception& e) {
546 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
550 /***********************************************************************/
552 RAbundVector SharedRAbundVector::getRAbundVector() {
556 for (int i = 0; i < data.size(); i++) {
557 if(data[i].abundance != 0) {
558 rav.push_back(data[i].abundance);
565 catch(exception& e) {
566 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
570 /***********************************************************************/
572 RAbundVector SharedRAbundVector::getRAbundVector2() {
575 for(int i = 0; i < numBins; i++)
576 if(data[i].abundance != 0)
577 rav.push_back(data[i].abundance-1);
580 catch(exception& e) {
581 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
585 /***********************************************************************/
587 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
589 SharedSAbundVector sav(maxRank+1);
591 for(int i=0;i<data.size();i++){
592 int abund = data[i].abundance;
593 sav.set(abund, sav.getAbundance(abund) + 1, group);
596 sav.set(0, 0, group);
602 catch(exception& e) {
603 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
607 /***********************************************************************/
609 SAbundVector SharedRAbundVector::getSAbundVector() {
611 SAbundVector sav(maxRank+1);
613 for(int i=0;i<data.size();i++){
614 int abund = data[i].abundance;
615 sav.set(abund, sav.get(abund) + 1);
621 catch(exception& e) {
622 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");
627 /***********************************************************************/
629 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
631 SharedOrderVector ov;
633 for(int i=0;i<data.size();i++){
634 for(int j=0;j<data[i].abundance;j++){
635 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
638 random_shuffle(ov.begin(), ov.end());
645 catch(exception& e) {
646 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
650 /***********************************************************************/
652 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
655 for(int i=0;i<numBins;i++){
656 for(int j=0;j<data[i].abundance;j++){
660 random_shuffle(ov.begin(), ov.end());
666 catch(exception& e) {
667 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
672 /***********************************************************************/