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; }
126 m->saveNextLabel = nextLabel;
129 catch(exception& e) {
130 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
135 /***********************************************************************/
137 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
139 int oldBinSize = data[binNumber].abundance;
140 data[binNumber].abundance = newBinSize;
141 data[binNumber].group = groupname;
143 if(newBinSize > maxRank) { maxRank = newBinSize; }
145 numSeqs += (newBinSize - oldBinSize);
147 catch(exception& e) {
148 m->errorOut(e, "SharedRAbundVector", "set");
152 /***********************************************************************/
154 void SharedRAbundVector::setData(vector <individual> newData){
158 /***********************************************************************/
160 int SharedRAbundVector::getAbundance(int index){
161 return data[index].abundance;
164 /***********************************************************************/
166 int SharedRAbundVector::numNZ(){
168 for(int i = 1; i < numBins; i++)
169 if(data[i].abundance > 0)
173 /***********************************************************************/
175 void SharedRAbundVector::sortD(){
176 struct individual indObj;
177 sort(data.begin()+1, data.end(), indObj);
179 /***********************************************************************/
181 individual SharedRAbundVector::get(int index){
185 /***********************************************************************/
187 vector <individual> SharedRAbundVector::getData(){
190 /***********************************************************************/
192 void SharedRAbundVector::clear(){
197 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
200 /***********************************************************************/
202 void SharedRAbundVector::push_back(int binSize, string groupName){
205 newGuy.abundance = binSize;
206 newGuy.group = groupName;
207 newGuy.bin = data.size();
209 data.push_back(newGuy);
212 if(binSize > maxRank){
218 catch(exception& e) {
219 m->errorOut(e, "SharedRAbundVector", "push_back");
224 /***********************************************************************/
226 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
229 newGuy.abundance = binSize;
230 newGuy.group = groupName;
233 data.insert(data.begin()+otu, newGuy);
236 if(binSize > maxRank){
242 catch(exception& e) {
243 m->errorOut(e, "SharedRAbundVector", "insert");
248 /***********************************************************************/
250 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
253 newGuy.abundance = binSize;
254 newGuy.group = groupName;
257 data.insert(data.begin(), newGuy);
260 if(binSize > maxRank){
266 catch(exception& e) {
267 m->errorOut(e, "SharedRAbundVector", "push_front");
272 /***********************************************************************/
273 void SharedRAbundVector::pop_back(){
274 numSeqs -= data[data.size()-1].abundance;
276 return data.pop_back();
279 /***********************************************************************/
282 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
283 return data.rbegin();
286 /***********************************************************************/
288 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
292 /***********************************************************************/
293 void SharedRAbundVector::resize(int size){
298 /***********************************************************************/
300 int SharedRAbundVector::size(){
304 /***********************************************************************/
305 void SharedRAbundVector::print(ostream& output){
307 output << numBins << '\t';
309 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
312 catch(exception& e) {
313 m->errorOut(e, "SharedRAbundVector", "print");
317 /***********************************************************************/
318 string SharedRAbundVector::getGroup(){
322 /***********************************************************************/
324 void SharedRAbundVector::setGroup(string groupName){
327 /***********************************************************************/
328 int SharedRAbundVector::getGroupIndex() { return index; }
329 /***********************************************************************/
330 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
331 /***********************************************************************/
332 int SharedRAbundVector::getNumBins(){
336 /***********************************************************************/
338 int SharedRAbundVector::getNumSeqs(){
342 /***********************************************************************/
344 int SharedRAbundVector::getMaxRank(){
347 /***********************************************************************/
349 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
352 /***********************************************************************/
353 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
356 util = new SharedUtil();
358 util->setGroups(m->Groups, m->namesOfGroups);
361 for (int i = 0; i < lookup.size(); i++) {
362 //if this sharedrabund is not from a group the user wants then delete it.
363 if (util->isValidGroup(lookup[i]->getGroup(), m->Groups) == false) {
365 delete lookup[i]; lookup[i] = NULL;
366 lookup.erase(lookup.begin()+i);
373 if (remove) { eliminateZeroOTUS(lookup); }
377 catch(exception& e) {
378 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
382 //**********************************************************************************************************************
383 int SharedRAbundVector::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
386 vector<SharedRAbundVector*> newLookup;
387 for (int i = 0; i < thislookup.size(); i++) {
388 SharedRAbundVector* temp = new SharedRAbundVector();
389 temp->setLabel(thislookup[i]->getLabel());
390 temp->setGroup(thislookup[i]->getGroup());
391 newLookup.push_back(temp);
395 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
396 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
398 //look at each sharedRabund and make sure they are not all zero
400 for (int j = 0; j < thislookup.size(); j++) {
401 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
404 //if they are not all zero add this bin
406 for (int j = 0; j < thislookup.size(); j++) {
407 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
412 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
414 thislookup = newLookup;
419 catch(exception& e) {
420 m->errorOut(e, "SharedRAbundVector", "eliminateZeroOTUS");
425 /***********************************************************************/
426 vector<SharedRAbundFloatVector*> SharedRAbundVector::getSharedRAbundFloatVectors(vector<SharedRAbundVector*> thislookup){
428 vector<SharedRAbundFloatVector*> newLookupFloat;
429 for (int i = 0; i < lookup.size(); i++) {
430 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
431 temp->setLabel(thislookup[i]->getLabel());
432 temp->setGroup(thislookup[i]->getGroup());
433 newLookupFloat.push_back(temp);
436 for (int i = 0; i < thislookup.size(); i++) {
438 for (int j = 0; j < thislookup[i]->getNumBins(); j++) {
440 if (m->control_pressed) { return newLookupFloat; }
442 int abund = thislookup[i]->getAbundance(j);
444 float relabund = abund / (float) thislookup[i]->getNumSeqs();
446 newLookupFloat[i]->push_back(relabund, thislookup[i]->getGroup());
450 return newLookupFloat;
452 catch(exception& e) {
453 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
457 /***********************************************************************/
459 RAbundVector SharedRAbundVector::getRAbundVector() {
463 for (int i = 0; i < data.size(); i++) {
464 if(data[i].abundance != 0) {
465 rav.push_back(data[i].abundance);
472 catch(exception& e) {
473 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
477 /***********************************************************************/
479 RAbundVector SharedRAbundVector::getRAbundVector2() {
482 for(int i = 0; i < numBins; i++)
483 if(data[i].abundance != 0)
484 rav.push_back(data[i].abundance-1);
487 catch(exception& e) {
488 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
492 /***********************************************************************/
494 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
496 SharedSAbundVector sav(maxRank+1);
498 for(int i=0;i<data.size();i++){
499 int abund = data[i].abundance;
500 sav.set(abund, sav.getAbundance(abund) + 1, group);
503 sav.set(0, 0, group);
509 catch(exception& e) {
510 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
514 /***********************************************************************/
516 SAbundVector SharedRAbundVector::getSAbundVector() {
518 SAbundVector sav(maxRank+1);
520 for(int i=0;i<data.size();i++){
521 int abund = data[i].abundance;
522 sav.set(abund, sav.get(abund) + 1);
528 catch(exception& e) {
529 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");
534 /***********************************************************************/
536 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
538 SharedOrderVector ov;
540 for(int i=0;i<data.size();i++){
541 for(int j=0;j<data[i].abundance;j++){
542 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
545 random_shuffle(ov.begin(), ov.end());
552 catch(exception& e) {
553 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
557 /***********************************************************************/
559 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
562 for(int i=0;i<numBins;i++){
563 for(int j=0;j<data[i].abundance;j++){
567 random_shuffle(ov.begin(), ov.end());
573 catch(exception& e) {
574 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
579 /***********************************************************************/