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 /***********************************************************************/
18 SharedRAbundVector::SharedRAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {globaldata = GlobalData::getInstance();}
19 /***********************************************************************/
21 SharedRAbundVector::~SharedRAbundVector() {
22 //for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
26 /***********************************************************************/
28 SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
29 globaldata = GlobalData::getInstance();
32 for (int i=0; i< n; i++) {
35 data.push_back(newGuy);
39 /***********************************************************************
41 SharedRAbundVector::SharedRAbundVector(string id, vector<individual> rav) : DataVector(id), data(rav) {
47 for(int i=0;i<data.size();i++){
48 if(data[i].abundance != 0) { numBins = i+1; }
49 if(data[i].abundance > maxRank) { maxRank = data[i].abundance; }
50 numSeqs += data[i].abundance;
54 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
60 /***********************************************************************/
62 SharedRAbundVector::SharedRAbundVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
64 globaldata = GlobalData::getInstance();
66 if (globaldata->gGroupmap == NULL) { groupmap = new GroupMap(); }
68 int num, inputData, count;
70 string holdLabel, nextLabel, groupN;
73 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; } lookup.clear();
75 if (globaldata->saveNextLabel == "") { f >> label; }
76 else { label = globaldata->saveNextLabel; }
78 //read in first row since you know there is at least 1 group.
83 //add new vector to lookup
84 SharedRAbundVector* temp = new SharedRAbundVector();
85 lookup.push_back(temp);
86 lookup[0]->setLabel(label);
87 lookup[0]->setGroup(groupN);
89 if (globaldata->gGroupmap == NULL) {
90 //save group in groupmap
91 groupmap->namesOfGroups.push_back(groupN);
92 groupmap->groupIndex[groupN] = 0;
95 //fill vector. data = first sharedrabund in file
96 for(int i=0;i<num;i++){
99 lookup[0]->push_back(inputData, groupN); //abundance, bin, group
100 push_back(inputData, groupN);
101 //numSeqs += inputData;
103 if (inputData > maxRank) { maxRank = inputData; }
108 if (!(f.eof())) { f >> nextLabel; }
110 //read the rest of the groups info in
111 while ((nextLabel == holdLabel) && (f.eof() != true)) {
115 if (globaldata->gGroupmap == NULL) {
116 //save group in groupmap
118 groupmap->namesOfGroups.push_back(groupN);
119 groupmap->groupIndex[groupN] = count;
122 //add new vector to lookup
123 temp = new SharedRAbundVector();
124 lookup.push_back(temp);
125 lookup[count]->setLabel(label);
126 lookup[count]->setGroup(groupN);
129 for(int i=0;i<num;i++){
131 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
136 if (f.eof() != true) { f >> nextLabel; }
139 globaldata->saveNextLabel = nextLabel;
141 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
144 catch(exception& e) {
145 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
150 /***********************************************************************/
152 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
154 int oldBinSize = data[binNumber].abundance;
155 data[binNumber].abundance = newBinSize;
156 data[binNumber].group = groupname;
158 if(newBinSize > maxRank) { maxRank = newBinSize; }
160 numSeqs += (newBinSize - oldBinSize);
162 catch(exception& e) {
163 m->errorOut(e, "SharedRAbundVector", "set");
167 /***********************************************************************/
169 void SharedRAbundVector::setData(vector <individual> newData){
173 /***********************************************************************/
175 int SharedRAbundVector::getAbundance(int index){
176 return data[index].abundance;
179 /***********************************************************************/
181 int SharedRAbundVector::numNZ(){
183 for(int i = 1; i < numBins; i++)
184 if(data[i].abundance > 0)
188 /***********************************************************************/
190 void SharedRAbundVector::sortD(){
191 struct individual indObj;
192 sort(data.begin()+1, data.end(), indObj);
194 /***********************************************************************/
196 individual SharedRAbundVector::get(int index){
200 /***********************************************************************/
202 vector <individual> SharedRAbundVector::getData(){
205 /***********************************************************************/
207 void SharedRAbundVector::clear(){
212 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
215 /***********************************************************************/
217 void SharedRAbundVector::push_back(int binSize, string groupName){
220 newGuy.abundance = binSize;
221 newGuy.group = groupName;
222 newGuy.bin = data.size();
224 data.push_back(newGuy);
227 if(binSize > maxRank){
233 catch(exception& e) {
234 m->errorOut(e, "SharedRAbundVector", "push_back");
239 /***********************************************************************/
241 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
244 newGuy.abundance = binSize;
245 newGuy.group = groupName;
248 data.insert(data.begin()+otu, newGuy);
251 if(binSize > maxRank){
257 catch(exception& e) {
258 m->errorOut(e, "SharedRAbundVector", "insert");
263 /***********************************************************************/
265 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
268 newGuy.abundance = binSize;
269 newGuy.group = groupName;
272 data.insert(data.begin(), newGuy);
275 if(binSize > maxRank){
281 catch(exception& e) {
282 m->errorOut(e, "SharedRAbundVector", "push_front");
287 /***********************************************************************/
288 void SharedRAbundVector::pop_back(){
289 numSeqs -= data[data.size()-1].abundance;
291 return data.pop_back();
294 /***********************************************************************/
297 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
298 return data.rbegin();
301 /***********************************************************************/
303 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
307 /***********************************************************************/
308 void SharedRAbundVector::resize(int size){
313 /***********************************************************************/
315 int SharedRAbundVector::size(){
319 /***********************************************************************/
320 void SharedRAbundVector::print(ostream& output){
322 output << numBins << '\t';
324 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
327 catch(exception& e) {
328 m->errorOut(e, "SharedRAbundVector", "print");
332 /***********************************************************************/
333 string SharedRAbundVector::getGroup(){
337 /***********************************************************************/
339 void SharedRAbundVector::setGroup(string groupName){
342 /***********************************************************************/
343 int SharedRAbundVector::getGroupIndex() { return index; }
344 /***********************************************************************/
345 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
346 /***********************************************************************/
347 int SharedRAbundVector::getNumBins(){
351 /***********************************************************************/
353 int SharedRAbundVector::getNumSeqs(){
357 /***********************************************************************/
359 int SharedRAbundVector::getMaxRank(){
362 /***********************************************************************/
364 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
367 /***********************************************************************/
368 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
371 util = new SharedUtil();
373 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
376 for (int i = 0; i < lookup.size(); i++) {
377 //if this sharedrabund is not from a group the user wants then delete it.
378 if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) {
380 delete lookup[i]; lookup[i] = NULL;
381 lookup.erase(lookup.begin()+i);
388 if (remove) { eliminateZeroOTUS(lookup); }
392 catch(exception& e) {
393 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
397 //**********************************************************************************************************************
398 int SharedRAbundVector::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
401 vector<SharedRAbundVector*> newLookup;
402 for (int i = 0; i < thislookup.size(); i++) {
403 SharedRAbundVector* temp = new SharedRAbundVector();
404 temp->setLabel(thislookup[i]->getLabel());
405 temp->setGroup(thislookup[i]->getGroup());
406 newLookup.push_back(temp);
410 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
411 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
413 //look at each sharedRabund and make sure they are not all zero
415 for (int j = 0; j < thislookup.size(); j++) {
416 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
419 //if they are not all zero add this bin
421 for (int j = 0; j < thislookup.size(); j++) {
422 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
427 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
429 thislookup = newLookup;
434 catch(exception& e) {
435 m->errorOut(e, "SharedRAbundVector", "eliminateZeroOTUS");
440 /***********************************************************************/
441 vector<SharedRAbundFloatVector*> SharedRAbundVector::getSharedRAbundFloatVectors(vector<SharedRAbundVector*> thislookup){
443 vector<SharedRAbundFloatVector*> newLookupFloat;
444 for (int i = 0; i < lookup.size(); i++) {
445 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
446 temp->setLabel(thislookup[i]->getLabel());
447 temp->setGroup(thislookup[i]->getGroup());
448 newLookupFloat.push_back(temp);
451 for (int i = 0; i < thislookup.size(); i++) {
453 for (int j = 0; j < thislookup[i]->getNumBins(); j++) {
455 if (m->control_pressed) { return newLookupFloat; }
457 int abund = thislookup[i]->getAbundance(j);
459 float relabund = abund / (float) thislookup[i]->getNumSeqs();
461 newLookupFloat[i]->push_back(relabund, thislookup[i]->getGroup());
465 return newLookupFloat;
467 catch(exception& e) {
468 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
472 /***********************************************************************/
474 RAbundVector SharedRAbundVector::getRAbundVector() {
478 for (int i = 0; i < data.size(); i++) {
479 if(data[i].abundance != 0) {
480 rav.push_back(data[i].abundance);
487 catch(exception& e) {
488 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
492 /***********************************************************************/
494 RAbundVector SharedRAbundVector::getRAbundVector2() {
497 for(int i = 0; i < numBins; i++)
498 if(data[i].abundance != 0)
499 rav.push_back(data[i].abundance-1);
502 catch(exception& e) {
503 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
507 /***********************************************************************/
509 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
511 SharedSAbundVector sav(maxRank+1);
513 for(int i=0;i<data.size();i++){
514 int abund = data[i].abundance;
515 sav.set(abund, sav.getAbundance(abund) + 1, group);
518 sav.set(0, 0, group);
524 catch(exception& e) {
525 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
529 /***********************************************************************/
531 SAbundVector SharedRAbundVector::getSAbundVector() {
533 SAbundVector sav(maxRank+1);
535 for(int i=0;i<data.size();i++){
536 int abund = data[i].abundance;
537 sav.set(abund, sav.get(abund) + 1);
543 catch(exception& e) {
544 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");
549 /***********************************************************************/
551 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
553 SharedOrderVector ov;
555 for(int i=0;i<data.size();i++){
556 for(int j=0;j<data[i].abundance;j++){
557 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
560 random_shuffle(ov.begin(), ov.end());
567 catch(exception& e) {
568 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
572 /***********************************************************************/
574 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
577 for(int i=0;i<numBins;i++){
578 for(int j=0;j<data[i].abundance;j++){
582 random_shuffle(ov.begin(), ov.end());
588 catch(exception& e) {
589 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
594 /***********************************************************************/