2 * sharedrabundfloatvector.cpp
5 * Created by westcott on 8/18/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "sharedrabundfloatvector.h"
11 #include "sharedutilities.h"
13 /***********************************************************************/
15 SharedRAbundFloatVector::SharedRAbundFloatVector() : DataVector(), maxRank(0.0), numBins(0), numSeqs(0.0) {globaldata = GlobalData::getInstance();}
16 /***********************************************************************/
18 SharedRAbundFloatVector::~SharedRAbundFloatVector() {}
20 /***********************************************************************/
21 SharedRAbundFloatVector::SharedRAbundFloatVector(int n) : DataVector(), maxRank(0.0), numBins(n), numSeqs(0.0) {
22 globaldata = GlobalData::getInstance();
23 individualFloat newGuy;
25 for (int i=0; i< n; i++) {
27 newGuy.abundance = 0.0;
28 data.push_back(newGuy);
31 /***********************************************************************/
33 SharedRAbundFloatVector::SharedRAbundFloatVector(ifstream& f) : DataVector(), maxRank(0.0), numBins(0), numSeqs(0.0) {
35 globaldata = GlobalData::getInstance();
37 if (globaldata->gGroupmap == NULL) { groupmap = new GroupMap(); }
42 string holdLabel, nextLabel, groupN;
43 individualFloat newguy;
45 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
48 if (globaldata->saveNextLabel == "") { f >> label; }
49 else { label = globaldata->saveNextLabel; }
51 //read in first row since you know there is at least 1 group.
56 //add new vector to lookup
57 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
58 lookup.push_back(temp);
59 lookup[0]->setLabel(label);
60 lookup[0]->setGroup(groupN);
62 if (globaldata->gGroupmap == NULL) {
63 //save group in groupmap
64 groupmap->namesOfGroups.push_back(groupN);
65 groupmap->groupIndex[groupN] = 0;
68 //fill vector. data = first sharedrabund in file
69 for(int i=0;i<num;i++){
72 lookup[0]->push_back(inputData, groupN); //abundance, bin, group
73 push_back(inputData, groupN);
75 if (inputData > maxRank) { maxRank = inputData; }
80 if (f.eof() != true) { f >> nextLabel; }
82 //read the rest of the groups info in
83 while ((nextLabel == holdLabel) && (f.eof() != true)) {
87 if (globaldata->gGroupmap == NULL) {
88 //save group in groupmap
89 groupmap->namesOfGroups.push_back(groupN);
90 groupmap->groupIndex[groupN] = count;
93 //add new vector to lookup
94 temp = new SharedRAbundFloatVector();
95 lookup.push_back(temp);
96 lookup[count]->setLabel(label);
97 lookup[count]->setGroup(groupN);
100 for(int i=0;i<num;i++){
102 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
107 if (f.eof() != true) { f >> nextLabel; }
110 globaldata->saveNextLabel = nextLabel;
112 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
115 catch(exception& e) {
116 m->errorOut(e, "SharedRAbundFloatVector", "SharedRAbundFloatVector");
121 /***********************************************************************/
123 void SharedRAbundFloatVector::set(int binNumber, float newBinSize, string groupname){
125 float oldBinSize = data[binNumber].abundance;
126 data[binNumber].abundance = newBinSize;
127 data[binNumber].group = groupname;
129 if(newBinSize > maxRank) { newBinSize = newBinSize; }
131 numSeqs += (newBinSize - oldBinSize);
133 catch(exception& e) {
134 m->errorOut(e, "SharedRAbundFloatVector", "set");
138 /***********************************************************************/
140 void SharedRAbundFloatVector::clear(){
145 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
148 /***********************************************************************/
149 float SharedRAbundFloatVector::getAbundance(int index){
150 return data[index].abundance;
152 /***********************************************************************/
153 individualFloat SharedRAbundFloatVector::get(int index){
156 /***********************************************************************/
157 void SharedRAbundFloatVector::push_back(float binSize, string groupName){
159 individualFloat newGuy;
160 newGuy.abundance = binSize;
161 newGuy.group = groupName;
162 newGuy.bin = data.size();
164 data.push_back(newGuy);
167 if(binSize > maxRank){ maxRank = binSize; }
171 catch(exception& e) {
172 m->errorOut(e, "SharedRAbundFloatVector", "push_back");
176 /***********************************************************************/
177 void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
179 individualFloat newGuy;
180 newGuy.abundance = binSize;
181 newGuy.group = groupName;
184 data.insert(data.begin()+otu, newGuy);
187 if(binSize > maxRank){ maxRank = binSize; }
191 catch(exception& e) {
192 m->errorOut(e, "SharedRAbundFloatVector", "insert");
197 /***********************************************************************/
198 void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
200 individualFloat newGuy;
201 newGuy.abundance = binSize;
202 newGuy.group = groupName;
205 data.insert(data.begin(), newGuy);
208 if(binSize > maxRank){ maxRank = binSize; }
212 catch(exception& e) {
213 m->errorOut(e, "SharedRAbundFloatVector", "push_front");
217 /**********************************************************************/
218 void SharedRAbundFloatVector::pop_back(){
219 numSeqs -= data[data.size()-1].abundance;
223 /***********************************************************************/
224 void SharedRAbundFloatVector::resize(int size){
227 /**********************************************************************/
228 int SharedRAbundFloatVector::size(){
231 /***********************************************************************/
232 void SharedRAbundFloatVector::print(ostream& output){
234 output << numBins << '\t';
236 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
239 catch(exception& e) {
240 m->errorOut(e, "SharedRAbundFloatVector", "print");
244 /***********************************************************************/
245 string SharedRAbundFloatVector::getGroup(){
248 /***********************************************************************/
249 void SharedRAbundFloatVector::setGroup(string groupName){
252 /***********************************************************************/
253 int SharedRAbundFloatVector::getGroupIndex() { return index; }
254 /***********************************************************************/
255 void SharedRAbundFloatVector::setGroupIndex(int vIndex) { index = vIndex; }
256 /***********************************************************************/
257 int SharedRAbundFloatVector::getNumBins(){ return numBins; }
258 /***********************************************************************/
259 float SharedRAbundFloatVector::getNumSeqs(){ return numSeqs; }
260 /***********************************************************************/
261 float SharedRAbundFloatVector::getMaxRank(){ return maxRank; }
262 /***********************************************************************/
263 SharedRAbundFloatVector SharedRAbundFloatVector::getSharedRAbundFloatVector(){
266 /***********************************************************************
267 SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
269 SharedRAbundVector rav(numBins);
273 for (int i = 0; i < data.size(); i++) {
275 rav.push_back(data[i].abundance);
279 catch(exception& e) {
280 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
284 /***********************************************************************/
285 vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
288 util = new SharedUtil();
290 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
293 for (int i = 0; i < lookup.size(); i++) {
294 //if this sharedrabund is not from a group the user wants then delete it.
295 if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) {
296 delete lookup[i]; lookup[i] = NULL;
297 lookup.erase(lookup.begin()+i);
305 if (remove) { eliminateZeroOTUS(lookup); }
309 catch(exception& e) {
310 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
314 /***********************************************************************/
316 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
318 RAbundVector rav(numBins);
320 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
325 catch(exception& e) {
326 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
330 /***********************************************************************
332 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
334 SharedSAbundVector sav(maxRank+1);
336 for(int i=0;i<data.size();i++){
337 int abund = data[i].abundance;
338 sav.set(abund, sav.getAbundance(abund) + 1, group);
341 sav.set(0, 0, group);
347 catch(exception& e) {
348 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
352 /***********************************************************************/
354 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
356 SAbundVector sav(ceil(maxRank)+1);
358 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
364 catch(exception& e) {
365 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");
370 /***********************************************************************
372 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
374 SharedOrderVector ov;
376 for(int i=0;i<data.size();i++){
377 int round = ceil(data[i].abundance);
378 for(int j=0;j<round;j++){
379 ov.push_back(data[i].bin, round, data[i].group);
382 random_shuffle(ov.begin(), ov.end());
389 catch(exception& e) {
390 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
394 /***********************************************************************/
395 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
396 OrderVector SharedRAbundFloatVector::getOrderVector(map<string,int>* nameMap = NULL) {
400 for(int i=0;i<data.size();i++){
401 int round = ceil(data[i].abundance);
402 for(int j=0;j<round;j++){
406 random_shuffle(ov.begin(), ov.end());
411 catch(exception& e) {
412 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
416 //**********************************************************************************************************************
417 int SharedRAbundFloatVector::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thislookup) {
420 vector<SharedRAbundFloatVector*> newLookup;
421 for (int i = 0; i < thislookup.size(); i++) {
422 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
423 temp->setLabel(thislookup[i]->getLabel());
424 temp->setGroup(thislookup[i]->getGroup());
425 newLookup.push_back(temp);
429 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
430 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
432 //look at each sharedRabund and make sure they are not all zero
434 for (int j = 0; j < thislookup.size(); j++) {
435 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
438 //if they are not all zero add this bin
440 for (int j = 0; j < thislookup.size(); j++) {
441 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
446 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
448 thislookup = newLookup;
453 catch(exception& e) {
454 m->errorOut(e, "SharedRAbundFloatVector", "eliminateZeroOTUS");
458 /***********************************************************************/