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 //read in first row since you know there is at least 1 group.
49 f >> label >> groupN >> num;
52 //add new vector to lookup
53 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
54 lookup.push_back(temp);
55 lookup[0]->setLabel(label);
56 lookup[0]->setGroup(groupN);
58 if (globaldata->gGroupmap == NULL) {
59 //save group in groupmap
60 groupmap->namesOfGroups.push_back(groupN);
61 groupmap->groupIndex[groupN] = 0;
64 //fill vector. data = first sharedrabund in file
65 for(int i=0;i<num;i++){
68 lookup[0]->push_back(inputData, groupN); //abundance, bin, group
69 push_back(inputData, groupN);
71 if (inputData > maxRank) { maxRank = inputData; }
76 if (f.eof() != true) { f >> nextLabel; }
78 //read the rest of the groups info in
79 while ((nextLabel == holdLabel) && (f.eof() != true)) {
83 if (globaldata->gGroupmap == NULL) {
84 //save group in groupmap
85 groupmap->namesOfGroups.push_back(groupN);
86 groupmap->groupIndex[groupN] = count;
89 //add new vector to lookup
90 temp = new SharedRAbundFloatVector();
91 lookup.push_back(temp);
92 lookup[count]->setLabel(label);
93 lookup[count]->setGroup(groupN);
96 for(int i=0;i<num;i++){
98 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
103 if (f.eof() != true) { f >> nextLabel; }
106 //put file pointer back since you are now at a new distance label
107 for (int i = 0; i < nextLabel.length(); i++) { f.unget(); }
109 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
112 catch(exception& e) {
113 m->errorOut(e, "SharedRAbundFloatVector", "SharedRAbundFloatVector");
118 /***********************************************************************/
120 void SharedRAbundFloatVector::set(int binNumber, float newBinSize, string groupname){
122 float oldBinSize = data[binNumber].abundance;
123 data[binNumber].abundance = newBinSize;
124 data[binNumber].group = groupname;
126 if(newBinSize > maxRank) { newBinSize = newBinSize; }
128 numSeqs += (newBinSize - oldBinSize);
130 catch(exception& e) {
131 m->errorOut(e, "SharedRAbundVector", "set");
136 /***********************************************************************/
137 float SharedRAbundFloatVector::getAbundance(int index){
138 return data[index].abundance;
140 /***********************************************************************/
141 individualFloat SharedRAbundFloatVector::get(int index){
144 /***********************************************************************/
145 void SharedRAbundFloatVector::push_back(float binSize, string groupName){
147 individualFloat newGuy;
148 newGuy.abundance = binSize;
149 newGuy.group = groupName;
150 newGuy.bin = data.size();
152 data.push_back(newGuy);
155 if(binSize > maxRank){ maxRank = binSize; }
159 catch(exception& e) {
160 m->errorOut(e, "SharedRAbundFloatVector", "push_back");
164 /***********************************************************************/
165 void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
167 individualFloat newGuy;
168 newGuy.abundance = binSize;
169 newGuy.group = groupName;
172 data.insert(data.begin()+otu, newGuy);
175 if(binSize > maxRank){ maxRank = binSize; }
179 catch(exception& e) {
180 m->errorOut(e, "SharedRAbundFloatVector", "insert");
185 /***********************************************************************/
186 void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
188 individualFloat newGuy;
189 newGuy.abundance = binSize;
190 newGuy.group = groupName;
193 data.insert(data.begin(), newGuy);
196 if(binSize > maxRank){ maxRank = binSize; }
200 catch(exception& e) {
201 m->errorOut(e, "SharedRAbundFloatVector", "push_front");
205 /**********************************************************************/
206 void SharedRAbundFloatVector::pop_back(){
207 numSeqs -= data[data.size()-1].abundance;
211 /***********************************************************************/
212 void SharedRAbundFloatVector::resize(int size){
215 /**********************************************************************/
216 int SharedRAbundFloatVector::size(){
219 /***********************************************************************/
220 void SharedRAbundFloatVector::print(ostream& output){
222 output << numBins << '\t';
224 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
227 catch(exception& e) {
228 m->errorOut(e, "SharedRAbundFloatVector", "print");
232 /***********************************************************************/
233 string SharedRAbundFloatVector::getGroup(){
236 /***********************************************************************/
237 void SharedRAbundFloatVector::setGroup(string groupName){
240 /***********************************************************************/
241 int SharedRAbundFloatVector::getGroupIndex() { return index; }
242 /***********************************************************************/
243 void SharedRAbundFloatVector::setGroupIndex(int vIndex) { index = vIndex; }
244 /***********************************************************************/
245 int SharedRAbundFloatVector::getNumBins(){ return numBins; }
246 /***********************************************************************/
247 float SharedRAbundFloatVector::getNumSeqs(){ return numSeqs; }
248 /***********************************************************************/
249 float SharedRAbundFloatVector::getMaxRank(){ return maxRank; }
250 /***********************************************************************/
251 SharedRAbundFloatVector SharedRAbundFloatVector::getSharedRAbundFloatVector(){
254 /***********************************************************************
255 SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
257 SharedRAbundVector rav(numBins);
261 for (int i = 0; i < data.size(); i++) {
263 rav.push_back(data[i].abundance);
267 catch(exception& e) {
268 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
272 /***********************************************************************/
273 vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
276 util = new SharedUtil();
278 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
280 for (int i = 0; i < lookup.size(); i++) {
281 //if this sharedrabund is not from a group the user wants then delete it.
282 if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) {
283 delete lookup[i]; lookup[i] = NULL;
284 lookup.erase(lookup.begin()+i);
293 catch(exception& e) {
294 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
298 /***********************************************************************/
300 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
302 RAbundVector rav(numBins);
304 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
309 catch(exception& e) {
310 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
314 /***********************************************************************
316 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
318 SharedSAbundVector sav(maxRank+1);
320 for(int i=0;i<data.size();i++){
321 int abund = data[i].abundance;
322 sav.set(abund, sav.getAbundance(abund) + 1, group);
325 sav.set(0, 0, group);
331 catch(exception& e) {
332 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
336 /***********************************************************************/
338 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
340 SAbundVector sav(ceil(maxRank)+1);
342 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
348 catch(exception& e) {
349 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");
354 /***********************************************************************
356 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
358 SharedOrderVector ov;
360 for(int i=0;i<data.size();i++){
361 int round = ceil(data[i].abundance);
362 for(int j=0;j<round;j++){
363 ov.push_back(data[i].bin, round, data[i].group);
366 random_shuffle(ov.begin(), ov.end());
373 catch(exception& e) {
374 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
378 /***********************************************************************/
379 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
380 OrderVector SharedRAbundFloatVector::getOrderVector(map<string,int>* nameMap = NULL) {
384 for(int i=0;i<data.size();i++){
385 int round = ceil(data[i].abundance);
386 for(int j=0;j<round;j++){
390 random_shuffle(ov.begin(), ov.end());
395 catch(exception& e) {
396 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
401 /***********************************************************************/