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) {
62 m->namesOfGroups.clear();
64 int num, inputData, count;
66 string holdLabel, nextLabel, groupN;
69 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; } lookup.clear();
71 //are we at the beginning of the file??
72 if (m->saveNextLabel == "") {
75 //is this a shared file that has headers
76 if (label == "label") {
78 f >> label; m->gobble(f);
81 f >> label; m->gobble(f);
84 label = m->getline(f); m->gobble(f);
86 //parse labels to save
87 istringstream iStringStream(label);
88 m->binLabelsInFile.clear();
89 while(!iStringStream.eof()){
90 if (m->control_pressed) { break; }
92 iStringStream >> temp; m->gobble(iStringStream);
94 m->binLabelsInFile.push_back(temp);
99 }else { label = m->saveNextLabel; }
101 //reset labels, currentLabels may have gotten changed as otus were eliminated because of group choices or sampling
102 m->currentBinLabels = m->binLabelsInFile;
104 //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 m->namesOfGroups.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 m->namesOfGroups.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;
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){
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" + toString(i+1);
344 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
346 output << binLabel << '\t';
350 for (int i = 0; i < numBins; i++) {
351 //if there is a bin label use it otherwise make one
352 string mybinLabel = "Otu" + toString(i+1);
353 if (i < m->currentBinLabels.size()) { mybinLabel = m->currentBinLabels[i]; }
355 output << mybinLabel << '\t';
360 m->printedHeaders = true;
362 catch(exception& e) {
363 m->errorOut(e, "SharedRAbundVector", "printHeaders");
367 /***********************************************************************/
368 void SharedRAbundVector::print(ostream& output) {
370 output << numBins << '\t';
372 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
375 catch(exception& e) {
376 m->errorOut(e, "SharedRAbundVector", "print");
380 /***********************************************************************/
381 string SharedRAbundVector::getGroup(){
385 /***********************************************************************/
387 void SharedRAbundVector::setGroup(string groupName){
390 /***********************************************************************/
391 int SharedRAbundVector::getGroupIndex() { return index; }
392 /***********************************************************************/
393 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
394 /***********************************************************************/
395 int SharedRAbundVector::getNumBins(){
399 /***********************************************************************/
401 int SharedRAbundVector::getNumSeqs(){
405 /***********************************************************************/
407 int SharedRAbundVector::getMaxRank(){
410 /***********************************************************************/
412 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
415 /***********************************************************************/
416 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
419 util = new SharedUtil();
421 util->setGroups(m->Groups, m->namesOfGroups);
424 for (int i = 0; i < lookup.size(); i++) {
425 //if this sharedrabund is not from a group the user wants then delete it.
426 if (util->isValidGroup(lookup[i]->getGroup(), m->Groups) == false) {
428 delete lookup[i]; lookup[i] = NULL;
429 lookup.erase(lookup.begin()+i);
436 if (remove) { eliminateZeroOTUS(lookup); }
440 catch(exception& e) {
441 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
445 //**********************************************************************************************************************
446 int SharedRAbundVector::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
449 vector<SharedRAbundVector*> newLookup;
450 for (int i = 0; i < thislookup.size(); i++) {
451 SharedRAbundVector* temp = new SharedRAbundVector();
452 temp->setLabel(thislookup[i]->getLabel());
453 temp->setGroup(thislookup[i]->getGroup());
454 newLookup.push_back(temp);
458 vector<string> newBinLabels;
459 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
460 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
462 //look at each sharedRabund and make sure they are not all zero
464 for (int j = 0; j < thislookup.size(); j++) {
465 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
468 //if they are not all zero add this bin
470 for (int j = 0; j < thislookup.size(); j++) {
471 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
474 //if there is a bin label use it otherwise make one
475 string binLabel = "Otu" + (i+1);
476 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
478 newBinLabels.push_back(binLabel);
482 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
484 thislookup = newLookup;
485 m->currentBinLabels = newBinLabels;
490 catch(exception& e) {
491 m->errorOut(e, "SharedRAbundVector", "eliminateZeroOTUS");
496 /***********************************************************************/
497 vector<SharedRAbundFloatVector*> SharedRAbundVector::getSharedRAbundFloatVectors(vector<SharedRAbundVector*> thislookup){
499 vector<SharedRAbundFloatVector*> newLookupFloat;
500 for (int i = 0; i < lookup.size(); i++) {
501 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
502 temp->setLabel(thislookup[i]->getLabel());
503 temp->setGroup(thislookup[i]->getGroup());
504 newLookupFloat.push_back(temp);
507 for (int i = 0; i < thislookup.size(); i++) {
509 for (int j = 0; j < thislookup[i]->getNumBins(); j++) {
511 if (m->control_pressed) { return newLookupFloat; }
513 int abund = thislookup[i]->getAbundance(j);
515 float relabund = abund / (float) thislookup[i]->getNumSeqs();
517 newLookupFloat[i]->push_back(relabund, thislookup[i]->getGroup());
521 return newLookupFloat;
523 catch(exception& e) {
524 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
528 /***********************************************************************/
530 RAbundVector SharedRAbundVector::getRAbundVector() {
534 for (int i = 0; i < data.size(); i++) {
535 if(data[i].abundance != 0) {
536 rav.push_back(data[i].abundance);
543 catch(exception& e) {
544 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
548 /***********************************************************************/
550 RAbundVector SharedRAbundVector::getRAbundVector2() {
553 for(int i = 0; i < numBins; i++)
554 if(data[i].abundance != 0)
555 rav.push_back(data[i].abundance-1);
558 catch(exception& e) {
559 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
563 /***********************************************************************/
565 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
567 SharedSAbundVector sav(maxRank+1);
569 for(int i=0;i<data.size();i++){
570 int abund = data[i].abundance;
571 sav.set(abund, sav.getAbundance(abund) + 1, group);
574 sav.set(0, 0, group);
580 catch(exception& e) {
581 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
585 /***********************************************************************/
587 SAbundVector SharedRAbundVector::getSAbundVector() {
589 SAbundVector sav(maxRank+1);
591 for(int i=0;i<data.size();i++){
592 int abund = data[i].abundance;
593 sav.set(abund, sav.get(abund) + 1);
599 catch(exception& e) {
600 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");
605 /***********************************************************************/
607 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
609 SharedOrderVector ov;
611 for(int i=0;i<data.size();i++){
612 for(int j=0;j<data[i].abundance;j++){
613 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
616 random_shuffle(ov.begin(), ov.end());
623 catch(exception& e) {
624 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
628 /***********************************************************************/
630 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
633 for(int i=0;i<numBins;i++){
634 for(int j=0;j<data[i].abundance;j++){
638 random_shuffle(ov.begin(), ov.end());
644 catch(exception& e) {
645 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
650 /***********************************************************************/