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) {}
16 /***********************************************************************/
18 SharedRAbundFloatVector::~SharedRAbundFloatVector() {}
20 /***********************************************************************/
21 SharedRAbundFloatVector::SharedRAbundFloatVector(int n) : DataVector(), maxRank(0.0), numBins(n), numSeqs(0.0) {
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) {
36 m->namesOfGroups.clear();
41 string holdLabel, nextLabel, groupN;
42 individualFloat newguy;
44 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
47 //are we at the beginning of the file??
48 if (m->saveNextLabel == "") {
51 //is this a shared file that has headers
52 if (label == "label") {
54 label = m->getline(f); m->gobble(f);
57 }else { label = m->saveNextLabel; }
59 //read in first row since you know there is at least 1 group.
64 //add new vector to lookup
65 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
66 lookup.push_back(temp);
67 lookup[0]->setLabel(label);
68 lookup[0]->setGroup(groupN);
70 m->namesOfGroups.push_back(groupN);
72 //fill vector. data = first sharedrabund in file
73 for(int i=0;i<num;i++){
76 lookup[0]->push_back(inputData, groupN); //abundance, bin, group
77 push_back(inputData, groupN);
79 if (inputData > maxRank) { maxRank = inputData; }
84 if (f.eof() != true) { f >> nextLabel; }
86 //read the rest of the groups info in
87 while ((nextLabel == holdLabel) && (f.eof() != true)) {
91 m->namesOfGroups.push_back(groupN);
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 m->saveNextLabel = nextLabel;
113 catch(exception& e) {
114 m->errorOut(e, "SharedRAbundFloatVector", "SharedRAbundFloatVector");
119 /***********************************************************************/
121 void SharedRAbundFloatVector::set(int binNumber, float newBinSize, string groupname){
123 float oldBinSize = data[binNumber].abundance;
124 data[binNumber].abundance = newBinSize;
125 data[binNumber].group = groupname;
127 if(newBinSize > maxRank) { newBinSize = newBinSize; }
129 numSeqs += (newBinSize - oldBinSize);
131 catch(exception& e) {
132 m->errorOut(e, "SharedRAbundFloatVector", "set");
136 /***********************************************************************/
138 void SharedRAbundFloatVector::clear(){
143 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
146 /***********************************************************************/
147 float SharedRAbundFloatVector::getAbundance(int index){
148 return data[index].abundance;
150 /***********************************************************************/
151 individualFloat SharedRAbundFloatVector::get(int index){
154 /***********************************************************************/
155 void SharedRAbundFloatVector::push_back(float binSize, string groupName){
157 individualFloat newGuy;
158 newGuy.abundance = binSize;
159 newGuy.group = groupName;
160 newGuy.bin = data.size();
162 data.push_back(newGuy);
165 if(binSize > maxRank){ maxRank = binSize; }
169 catch(exception& e) {
170 m->errorOut(e, "SharedRAbundFloatVector", "push_back");
174 /***********************************************************************/
175 void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
177 individualFloat newGuy;
178 newGuy.abundance = binSize;
179 newGuy.group = groupName;
182 data.insert(data.begin()+otu, newGuy);
185 if(binSize > maxRank){ maxRank = binSize; }
189 catch(exception& e) {
190 m->errorOut(e, "SharedRAbundFloatVector", "insert");
195 /***********************************************************************/
196 void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
198 individualFloat newGuy;
199 newGuy.abundance = binSize;
200 newGuy.group = groupName;
203 data.insert(data.begin(), newGuy);
206 if(binSize > maxRank){ maxRank = binSize; }
210 catch(exception& e) {
211 m->errorOut(e, "SharedRAbundFloatVector", "push_front");
215 /**********************************************************************/
216 void SharedRAbundFloatVector::pop_back(){
217 numSeqs -= data[data.size()-1].abundance;
221 /***********************************************************************/
222 void SharedRAbundFloatVector::resize(int size){
225 /**********************************************************************/
226 int SharedRAbundFloatVector::size(){
229 /***********************************************************************/
230 void SharedRAbundFloatVector::printHeaders(ostream& output){
232 output << "label\tGroup\tnumOtus\t";
233 if (m->sharedHeaderMode == "tax") {
234 for (int i = 0; i < numBins; i++) { output << "PhyloType" << (i+1) << '\t'; }
237 for (int i = 0; i < numBins; i++) { output << "Otu" << (i+1) << '\t'; }
240 m->printedHeaders = true;
242 catch(exception& e) {
243 m->errorOut(e, "SharedRAbundVector", "printHeaders");
247 /***********************************************************************/
248 void SharedRAbundFloatVector::print(ostream& output){
250 output << numBins << '\t';
252 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
255 catch(exception& e) {
256 m->errorOut(e, "SharedRAbundFloatVector", "print");
260 /***********************************************************************/
261 string SharedRAbundFloatVector::getGroup(){
264 /***********************************************************************/
265 void SharedRAbundFloatVector::setGroup(string groupName){
268 /***********************************************************************/
269 int SharedRAbundFloatVector::getGroupIndex() { return index; }
270 /***********************************************************************/
271 void SharedRAbundFloatVector::setGroupIndex(int vIndex) { index = vIndex; }
272 /***********************************************************************/
273 int SharedRAbundFloatVector::getNumBins(){ return numBins; }
274 /***********************************************************************/
275 float SharedRAbundFloatVector::getNumSeqs(){ return numSeqs; }
276 /***********************************************************************/
277 float SharedRAbundFloatVector::getMaxRank(){ return maxRank; }
278 /***********************************************************************/
279 SharedRAbundFloatVector SharedRAbundFloatVector::getSharedRAbundFloatVector(){
282 /***********************************************************************
283 SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
285 SharedRAbundVector rav(numBins);
289 for (int i = 0; i < data.size(); i++) {
291 rav.push_back(data[i].abundance);
295 catch(exception& e) {
296 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
300 /***********************************************************************/
301 vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
304 util = new SharedUtil();
306 util->setGroups(m->Groups, m->namesOfGroups);
309 for (int i = 0; i < lookup.size(); i++) {
310 //if this sharedrabund is not from a group the user wants then delete it.
311 if (util->isValidGroup(lookup[i]->getGroup(), m->Groups) == false) {
312 delete lookup[i]; lookup[i] = NULL;
313 lookup.erase(lookup.begin()+i);
321 if (remove) { eliminateZeroOTUS(lookup); }
325 catch(exception& e) {
326 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
330 /***********************************************************************/
332 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
334 RAbundVector rav(numBins);
336 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
341 catch(exception& e) {
342 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
346 /***********************************************************************
348 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
350 SharedSAbundVector sav(maxRank+1);
352 for(int i=0;i<data.size();i++){
353 int abund = data[i].abundance;
354 sav.set(abund, sav.getAbundance(abund) + 1, group);
357 sav.set(0, 0, group);
363 catch(exception& e) {
364 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
368 /***********************************************************************/
370 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
372 SAbundVector sav(ceil(maxRank)+1);
374 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
380 catch(exception& e) {
381 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");
386 /***********************************************************************
388 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
390 SharedOrderVector ov;
392 for(int i=0;i<data.size();i++){
393 int round = ceil(data[i].abundance);
394 for(int j=0;j<round;j++){
395 ov.push_back(data[i].bin, round, data[i].group);
398 random_shuffle(ov.begin(), ov.end());
405 catch(exception& e) {
406 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
410 /***********************************************************************/
411 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
412 OrderVector SharedRAbundFloatVector::getOrderVector(map<string,int>* nameMap = NULL) {
416 for(int i=0;i<data.size();i++){
417 int round = ceil(data[i].abundance);
418 for(int j=0;j<round;j++){
422 random_shuffle(ov.begin(), ov.end());
427 catch(exception& e) {
428 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
432 //**********************************************************************************************************************
433 int SharedRAbundFloatVector::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thislookup) {
436 vector<SharedRAbundFloatVector*> newLookup;
437 for (int i = 0; i < thislookup.size(); i++) {
438 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
439 temp->setLabel(thislookup[i]->getLabel());
440 temp->setGroup(thislookup[i]->getGroup());
441 newLookup.push_back(temp);
445 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
446 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
448 //look at each sharedRabund and make sure they are not all zero
450 for (int j = 0; j < thislookup.size(); j++) {
451 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
454 //if they are not all zero add this bin
456 for (int j = 0; j < thislookup.size(); j++) {
457 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
462 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
464 thislookup = newLookup;
469 catch(exception& e) {
470 m->errorOut(e, "SharedRAbundFloatVector", "eliminateZeroOTUS");
474 /***********************************************************************/