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 if (m->saveNextLabel == "") { f >> label; }
72 else { label = m->saveNextLabel; }
74 //read in first row since you know there is at least 1 group.
79 //add new vector to lookup
80 SharedRAbundVector* temp = new SharedRAbundVector();
81 lookup.push_back(temp);
82 lookup[0]->setLabel(label);
83 lookup[0]->setGroup(groupN);
85 m->namesOfGroups.push_back(groupN);
87 //fill vector. data = first sharedrabund in file
88 for(int i=0;i<num;i++){
91 lookup[0]->push_back(inputData, groupN); //abundance, bin, group
92 push_back(inputData, groupN);
93 //numSeqs += inputData;
95 if (inputData > maxRank) { maxRank = inputData; }
100 if (!(f.eof())) { f >> nextLabel; }
102 //read the rest of the groups info in
103 while ((nextLabel == holdLabel) && (f.eof() != true)) {
107 m->namesOfGroups.push_back(groupN);
109 //add new vector to lookup
110 temp = new SharedRAbundVector();
111 lookup.push_back(temp);
112 lookup[count]->setLabel(label);
113 lookup[count]->setGroup(groupN);
116 for(int i=0;i<num;i++){
118 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
123 if (f.eof() != true) { f >> nextLabel; }
125 m->saveNextLabel = nextLabel;
128 catch(exception& e) {
129 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
134 /***********************************************************************/
136 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
138 int oldBinSize = data[binNumber].abundance;
139 data[binNumber].abundance = newBinSize;
140 data[binNumber].group = groupname;
142 if(newBinSize > maxRank) { maxRank = newBinSize; }
144 numSeqs += (newBinSize - oldBinSize);
146 catch(exception& e) {
147 m->errorOut(e, "SharedRAbundVector", "set");
151 /***********************************************************************/
153 void SharedRAbundVector::setData(vector <individual> newData){
157 /***********************************************************************/
159 int SharedRAbundVector::getAbundance(int index){
160 return data[index].abundance;
163 /***********************************************************************/
165 int SharedRAbundVector::numNZ(){
167 for(int i = 1; i < numBins; i++)
168 if(data[i].abundance > 0)
172 /***********************************************************************/
174 void SharedRAbundVector::sortD(){
175 struct individual indObj;
176 sort(data.begin()+1, data.end(), indObj);
178 /***********************************************************************/
180 individual SharedRAbundVector::get(int index){
184 /***********************************************************************/
186 vector <individual> SharedRAbundVector::getData(){
189 /***********************************************************************/
191 void SharedRAbundVector::clear(){
196 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
199 /***********************************************************************/
201 void SharedRAbundVector::push_back(int binSize, string groupName){
204 newGuy.abundance = binSize;
205 newGuy.group = groupName;
206 newGuy.bin = data.size();
208 data.push_back(newGuy);
211 if(binSize > maxRank){
217 catch(exception& e) {
218 m->errorOut(e, "SharedRAbundVector", "push_back");
223 /***********************************************************************/
225 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
228 newGuy.abundance = binSize;
229 newGuy.group = groupName;
232 data.insert(data.begin()+otu, newGuy);
235 if(binSize > maxRank){
241 catch(exception& e) {
242 m->errorOut(e, "SharedRAbundVector", "insert");
247 /***********************************************************************/
249 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
252 newGuy.abundance = binSize;
253 newGuy.group = groupName;
256 data.insert(data.begin(), newGuy);
259 if(binSize > maxRank){
265 catch(exception& e) {
266 m->errorOut(e, "SharedRAbundVector", "push_front");
271 /***********************************************************************/
272 void SharedRAbundVector::pop_back(){
273 numSeqs -= data[data.size()-1].abundance;
275 return data.pop_back();
278 /***********************************************************************/
281 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
282 return data.rbegin();
285 /***********************************************************************/
287 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
291 /***********************************************************************/
292 void SharedRAbundVector::resize(int size){
297 /***********************************************************************/
299 int SharedRAbundVector::size(){
303 /***********************************************************************/
304 void SharedRAbundVector::print(ostream& output){
306 output << numBins << '\t';
308 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
311 catch(exception& e) {
312 m->errorOut(e, "SharedRAbundVector", "print");
316 /***********************************************************************/
317 string SharedRAbundVector::getGroup(){
321 /***********************************************************************/
323 void SharedRAbundVector::setGroup(string groupName){
326 /***********************************************************************/
327 int SharedRAbundVector::getGroupIndex() { return index; }
328 /***********************************************************************/
329 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
330 /***********************************************************************/
331 int SharedRAbundVector::getNumBins(){
335 /***********************************************************************/
337 int SharedRAbundVector::getNumSeqs(){
341 /***********************************************************************/
343 int SharedRAbundVector::getMaxRank(){
346 /***********************************************************************/
348 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
351 /***********************************************************************/
352 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
355 util = new SharedUtil();
357 util->setGroups(m->Groups, m->namesOfGroups);
360 for (int i = 0; i < lookup.size(); i++) {
361 //if this sharedrabund is not from a group the user wants then delete it.
362 if (util->isValidGroup(lookup[i]->getGroup(), m->Groups) == false) {
364 delete lookup[i]; lookup[i] = NULL;
365 lookup.erase(lookup.begin()+i);
372 if (remove) { eliminateZeroOTUS(lookup); }
376 catch(exception& e) {
377 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
381 //**********************************************************************************************************************
382 int SharedRAbundVector::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
385 vector<SharedRAbundVector*> newLookup;
386 for (int i = 0; i < thislookup.size(); i++) {
387 SharedRAbundVector* temp = new SharedRAbundVector();
388 temp->setLabel(thislookup[i]->getLabel());
389 temp->setGroup(thislookup[i]->getGroup());
390 newLookup.push_back(temp);
394 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
395 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
397 //look at each sharedRabund and make sure they are not all zero
399 for (int j = 0; j < thislookup.size(); j++) {
400 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
403 //if they are not all zero add this bin
405 for (int j = 0; j < thislookup.size(); j++) {
406 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
411 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
413 thislookup = newLookup;
418 catch(exception& e) {
419 m->errorOut(e, "SharedRAbundVector", "eliminateZeroOTUS");
424 /***********************************************************************/
425 vector<SharedRAbundFloatVector*> SharedRAbundVector::getSharedRAbundFloatVectors(vector<SharedRAbundVector*> thislookup){
427 vector<SharedRAbundFloatVector*> newLookupFloat;
428 for (int i = 0; i < lookup.size(); i++) {
429 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
430 temp->setLabel(thislookup[i]->getLabel());
431 temp->setGroup(thislookup[i]->getGroup());
432 newLookupFloat.push_back(temp);
435 for (int i = 0; i < thislookup.size(); i++) {
437 for (int j = 0; j < thislookup[i]->getNumBins(); j++) {
439 if (m->control_pressed) { return newLookupFloat; }
441 int abund = thislookup[i]->getAbundance(j);
443 float relabund = abund / (float) thislookup[i]->getNumSeqs();
445 newLookupFloat[i]->push_back(relabund, thislookup[i]->getGroup());
449 return newLookupFloat;
451 catch(exception& e) {
452 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
456 /***********************************************************************/
458 RAbundVector SharedRAbundVector::getRAbundVector() {
462 for (int i = 0; i < data.size(); i++) {
463 if(data[i].abundance != 0) {
464 rav.push_back(data[i].abundance);
471 catch(exception& e) {
472 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
476 /***********************************************************************/
478 RAbundVector SharedRAbundVector::getRAbundVector2() {
481 for(int i = 0; i < numBins; i++)
482 if(data[i].abundance != 0)
483 rav.push_back(data[i].abundance-1);
486 catch(exception& e) {
487 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
491 /***********************************************************************/
493 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
495 SharedSAbundVector sav(maxRank+1);
497 for(int i=0;i<data.size();i++){
498 int abund = data[i].abundance;
499 sav.set(abund, sav.getAbundance(abund) + 1, group);
502 sav.set(0, 0, group);
508 catch(exception& e) {
509 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
513 /***********************************************************************/
515 SAbundVector SharedRAbundVector::getSAbundVector() {
517 SAbundVector sav(maxRank+1);
519 for(int i=0;i<data.size();i++){
520 int abund = data[i].abundance;
521 sav.set(abund, sav.get(abund) + 1);
527 catch(exception& e) {
528 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");
533 /***********************************************************************/
535 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
537 SharedOrderVector ov;
539 for(int i=0;i<data.size();i++){
540 for(int j=0;j<data[i].abundance;j++){
541 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
544 random_shuffle(ov.begin(), ov.end());
551 catch(exception& e) {
552 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
556 /***********************************************************************/
558 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
561 for(int i=0;i<numBins;i++){
562 for(int j=0;j<data[i].abundance;j++){
566 random_shuffle(ov.begin(), ov.end());
572 catch(exception& e) {
573 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
578 /***********************************************************************/