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, "SharedRAbundVector", "set");
139 /***********************************************************************/
140 float SharedRAbundFloatVector::getAbundance(int index){
141 return data[index].abundance;
143 /***********************************************************************/
144 individualFloat SharedRAbundFloatVector::get(int index){
147 /***********************************************************************/
148 void SharedRAbundFloatVector::push_back(float binSize, string groupName){
150 individualFloat newGuy;
151 newGuy.abundance = binSize;
152 newGuy.group = groupName;
153 newGuy.bin = data.size();
155 data.push_back(newGuy);
158 if(binSize > maxRank){ maxRank = binSize; }
162 catch(exception& e) {
163 m->errorOut(e, "SharedRAbundFloatVector", "push_back");
167 /***********************************************************************/
168 void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
170 individualFloat newGuy;
171 newGuy.abundance = binSize;
172 newGuy.group = groupName;
175 data.insert(data.begin()+otu, newGuy);
178 if(binSize > maxRank){ maxRank = binSize; }
182 catch(exception& e) {
183 m->errorOut(e, "SharedRAbundFloatVector", "insert");
188 /***********************************************************************/
189 void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
191 individualFloat newGuy;
192 newGuy.abundance = binSize;
193 newGuy.group = groupName;
196 data.insert(data.begin(), newGuy);
199 if(binSize > maxRank){ maxRank = binSize; }
203 catch(exception& e) {
204 m->errorOut(e, "SharedRAbundFloatVector", "push_front");
208 /**********************************************************************/
209 void SharedRAbundFloatVector::pop_back(){
210 numSeqs -= data[data.size()-1].abundance;
214 /***********************************************************************/
215 void SharedRAbundFloatVector::resize(int size){
218 /**********************************************************************/
219 int SharedRAbundFloatVector::size(){
222 /***********************************************************************/
223 void SharedRAbundFloatVector::print(ostream& output){
225 output << numBins << '\t';
227 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
230 catch(exception& e) {
231 m->errorOut(e, "SharedRAbundFloatVector", "print");
235 /***********************************************************************/
236 string SharedRAbundFloatVector::getGroup(){
239 /***********************************************************************/
240 void SharedRAbundFloatVector::setGroup(string groupName){
243 /***********************************************************************/
244 int SharedRAbundFloatVector::getGroupIndex() { return index; }
245 /***********************************************************************/
246 void SharedRAbundFloatVector::setGroupIndex(int vIndex) { index = vIndex; }
247 /***********************************************************************/
248 int SharedRAbundFloatVector::getNumBins(){ return numBins; }
249 /***********************************************************************/
250 float SharedRAbundFloatVector::getNumSeqs(){ return numSeqs; }
251 /***********************************************************************/
252 float SharedRAbundFloatVector::getMaxRank(){ return maxRank; }
253 /***********************************************************************/
254 SharedRAbundFloatVector SharedRAbundFloatVector::getSharedRAbundFloatVector(){
257 /***********************************************************************
258 SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
260 SharedRAbundVector rav(numBins);
264 for (int i = 0; i < data.size(); i++) {
266 rav.push_back(data[i].abundance);
270 catch(exception& e) {
271 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
275 /***********************************************************************/
276 vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
279 util = new SharedUtil();
281 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
283 for (int i = 0; i < lookup.size(); i++) {
284 //if this sharedrabund is not from a group the user wants then delete it.
285 if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) {
286 delete lookup[i]; lookup[i] = NULL;
287 lookup.erase(lookup.begin()+i);
296 catch(exception& e) {
297 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
301 /***********************************************************************/
303 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
305 RAbundVector rav(numBins);
307 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
312 catch(exception& e) {
313 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
317 /***********************************************************************
319 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
321 SharedSAbundVector sav(maxRank+1);
323 for(int i=0;i<data.size();i++){
324 int abund = data[i].abundance;
325 sav.set(abund, sav.getAbundance(abund) + 1, group);
328 sav.set(0, 0, group);
334 catch(exception& e) {
335 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
339 /***********************************************************************/
341 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
343 SAbundVector sav(ceil(maxRank)+1);
345 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
351 catch(exception& e) {
352 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");
357 /***********************************************************************
359 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
361 SharedOrderVector ov;
363 for(int i=0;i<data.size();i++){
364 int round = ceil(data[i].abundance);
365 for(int j=0;j<round;j++){
366 ov.push_back(data[i].bin, round, data[i].group);
369 random_shuffle(ov.begin(), ov.end());
376 catch(exception& e) {
377 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
381 /***********************************************************************/
382 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
383 OrderVector SharedRAbundFloatVector::getOrderVector(map<string,int>* nameMap = NULL) {
387 for(int i=0;i<data.size();i++){
388 int round = ceil(data[i].abundance);
389 for(int j=0;j<round;j++){
393 random_shuffle(ov.begin(), ov.end());
398 catch(exception& e) {
399 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
404 /***********************************************************************/