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 label = m->getline(f); m->gobble(f);
81 }else { label = m->saveNextLabel; }
83 //read in first row since you know there is at least 1 group.
88 //add new vector to lookup
89 SharedRAbundVector* temp = new SharedRAbundVector();
90 lookup.push_back(temp);
91 lookup[0]->setLabel(label);
92 lookup[0]->setGroup(groupN);
94 m->namesOfGroups.push_back(groupN);
96 //fill vector. data = first sharedrabund in file
97 for(int i=0;i<num;i++){
100 lookup[0]->push_back(inputData, groupN); //abundance, bin, group
101 push_back(inputData, groupN);
102 //numSeqs += inputData;
104 if (inputData > maxRank) { maxRank = inputData; }
109 if (!(f.eof())) { f >> nextLabel; }
111 //read the rest of the groups info in
112 while ((nextLabel == holdLabel) && (f.eof() != true)) {
116 m->namesOfGroups.push_back(groupN);
118 //add new vector to lookup
119 temp = new SharedRAbundVector();
120 lookup.push_back(temp);
121 lookup[count]->setLabel(label);
122 lookup[count]->setGroup(groupN);
125 for(int i=0;i<num;i++){
127 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
132 if (f.eof() != true) { f >> nextLabel; }
134 m->saveNextLabel = nextLabel;
137 catch(exception& e) {
138 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
143 /***********************************************************************/
145 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
147 int oldBinSize = data[binNumber].abundance;
148 data[binNumber].abundance = newBinSize;
149 data[binNumber].group = groupname;
151 if(newBinSize > maxRank) { maxRank = newBinSize; }
153 numSeqs += (newBinSize - oldBinSize);
155 catch(exception& e) {
156 m->errorOut(e, "SharedRAbundVector", "set");
160 /***********************************************************************/
162 void SharedRAbundVector::setData(vector <individual> newData){
166 /***********************************************************************/
168 int SharedRAbundVector::getAbundance(int index){
169 return data[index].abundance;
172 /***********************************************************************/
174 int SharedRAbundVector::numNZ(){
176 for(int i = 1; i < numBins; i++)
177 if(data[i].abundance > 0)
181 /***********************************************************************/
183 void SharedRAbundVector::sortD(){
184 struct individual indObj;
185 sort(data.begin()+1, data.end(), indObj);
187 /***********************************************************************/
189 individual SharedRAbundVector::get(int index){
193 /***********************************************************************/
195 vector <individual> SharedRAbundVector::getData(){
198 /***********************************************************************/
200 void SharedRAbundVector::clear(){
205 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
208 /***********************************************************************/
210 void SharedRAbundVector::push_back(int binSize, string groupName){
213 newGuy.abundance = binSize;
214 newGuy.group = groupName;
215 newGuy.bin = data.size();
217 data.push_back(newGuy);
220 if(binSize > maxRank){
226 catch(exception& e) {
227 m->errorOut(e, "SharedRAbundVector", "push_back");
232 /***********************************************************************/
234 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
237 newGuy.abundance = binSize;
238 newGuy.group = groupName;
241 data.insert(data.begin()+otu, newGuy);
244 if(binSize > maxRank){
250 catch(exception& e) {
251 m->errorOut(e, "SharedRAbundVector", "insert");
256 /***********************************************************************/
258 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
261 newGuy.abundance = binSize;
262 newGuy.group = groupName;
265 data.insert(data.begin(), newGuy);
268 if(binSize > maxRank){
274 catch(exception& e) {
275 m->errorOut(e, "SharedRAbundVector", "push_front");
280 /***********************************************************************/
281 void SharedRAbundVector::pop_back(){
282 numSeqs -= data[data.size()-1].abundance;
284 return data.pop_back();
287 /***********************************************************************/
290 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
291 return data.rbegin();
294 /***********************************************************************/
296 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
300 /***********************************************************************/
301 void SharedRAbundVector::resize(int size){
306 /***********************************************************************/
308 int SharedRAbundVector::size(){
313 /***********************************************************************/
314 void SharedRAbundVector::printHeaders(ostream& output){
316 output << "label\tGroup\tnumOtus\t";
317 if (m->sharedHeaderMode == "tax") {
318 for (int i = 0; i < numBins; i++) { output << "PhyloType" << (i+1) << '\t'; }
321 for (int i = 0; i < numBins; i++) { output << "Otu" << (i+1) << '\t'; }
324 m->printedHeaders = true;
326 catch(exception& e) {
327 m->errorOut(e, "SharedRAbundVector", "printHeaders");
331 /***********************************************************************/
332 void SharedRAbundVector::print(ostream& output) {
334 output << numBins << '\t';
336 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
339 catch(exception& e) {
340 m->errorOut(e, "SharedRAbundVector", "print");
344 /***********************************************************************/
345 string SharedRAbundVector::getGroup(){
349 /***********************************************************************/
351 void SharedRAbundVector::setGroup(string groupName){
354 /***********************************************************************/
355 int SharedRAbundVector::getGroupIndex() { return index; }
356 /***********************************************************************/
357 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
358 /***********************************************************************/
359 int SharedRAbundVector::getNumBins(){
363 /***********************************************************************/
365 int SharedRAbundVector::getNumSeqs(){
369 /***********************************************************************/
371 int SharedRAbundVector::getMaxRank(){
374 /***********************************************************************/
376 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
379 /***********************************************************************/
380 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
383 util = new SharedUtil();
385 util->setGroups(m->Groups, m->namesOfGroups);
388 for (int i = 0; i < lookup.size(); i++) {
389 //if this sharedrabund is not from a group the user wants then delete it.
390 if (util->isValidGroup(lookup[i]->getGroup(), m->Groups) == false) {
392 delete lookup[i]; lookup[i] = NULL;
393 lookup.erase(lookup.begin()+i);
400 if (remove) { eliminateZeroOTUS(lookup); }
404 catch(exception& e) {
405 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
409 //**********************************************************************************************************************
410 int SharedRAbundVector::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
413 vector<SharedRAbundVector*> newLookup;
414 for (int i = 0; i < thislookup.size(); i++) {
415 SharedRAbundVector* temp = new SharedRAbundVector();
416 temp->setLabel(thislookup[i]->getLabel());
417 temp->setGroup(thislookup[i]->getGroup());
418 newLookup.push_back(temp);
422 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
423 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
425 //look at each sharedRabund and make sure they are not all zero
427 for (int j = 0; j < thislookup.size(); j++) {
428 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
431 //if they are not all zero add this bin
433 for (int j = 0; j < thislookup.size(); j++) {
434 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
439 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
441 thislookup = newLookup;
446 catch(exception& e) {
447 m->errorOut(e, "SharedRAbundVector", "eliminateZeroOTUS");
452 /***********************************************************************/
453 vector<SharedRAbundFloatVector*> SharedRAbundVector::getSharedRAbundFloatVectors(vector<SharedRAbundVector*> thislookup){
455 vector<SharedRAbundFloatVector*> newLookupFloat;
456 for (int i = 0; i < lookup.size(); i++) {
457 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
458 temp->setLabel(thislookup[i]->getLabel());
459 temp->setGroup(thislookup[i]->getGroup());
460 newLookupFloat.push_back(temp);
463 for (int i = 0; i < thislookup.size(); i++) {
465 for (int j = 0; j < thislookup[i]->getNumBins(); j++) {
467 if (m->control_pressed) { return newLookupFloat; }
469 int abund = thislookup[i]->getAbundance(j);
471 float relabund = abund / (float) thislookup[i]->getNumSeqs();
473 newLookupFloat[i]->push_back(relabund, thislookup[i]->getGroup());
477 return newLookupFloat;
479 catch(exception& e) {
480 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
484 /***********************************************************************/
486 RAbundVector SharedRAbundVector::getRAbundVector() {
490 for (int i = 0; i < data.size(); i++) {
491 if(data[i].abundance != 0) {
492 rav.push_back(data[i].abundance);
499 catch(exception& e) {
500 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
504 /***********************************************************************/
506 RAbundVector SharedRAbundVector::getRAbundVector2() {
509 for(int i = 0; i < numBins; i++)
510 if(data[i].abundance != 0)
511 rav.push_back(data[i].abundance-1);
514 catch(exception& e) {
515 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
519 /***********************************************************************/
521 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
523 SharedSAbundVector sav(maxRank+1);
525 for(int i=0;i<data.size();i++){
526 int abund = data[i].abundance;
527 sav.set(abund, sav.getAbundance(abund) + 1, group);
530 sav.set(0, 0, group);
536 catch(exception& e) {
537 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
541 /***********************************************************************/
543 SAbundVector SharedRAbundVector::getSAbundVector() {
545 SAbundVector sav(maxRank+1);
547 for(int i=0;i<data.size();i++){
548 int abund = data[i].abundance;
549 sav.set(abund, sav.get(abund) + 1);
555 catch(exception& e) {
556 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");
561 /***********************************************************************/
563 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
565 SharedOrderVector ov;
567 for(int i=0;i<data.size();i++){
568 for(int j=0;j<data[i].abundance;j++){
569 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
572 random_shuffle(ov.begin(), ov.end());
579 catch(exception& e) {
580 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
584 /***********************************************************************/
586 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
589 for(int i=0;i<numBins;i++){
590 for(int j=0;j<data[i].abundance;j++){
594 random_shuffle(ov.begin(), ov.end());
600 catch(exception& e) {
601 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
606 /***********************************************************************/