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 f >> label; m->gobble(f);
57 f >> label; m->gobble(f);
60 label = m->getline(f); m->gobble(f);
62 //parse labels to save
63 istringstream iStringStream(label);
64 m->binLabelsInFile.clear();
65 while(!iStringStream.eof()){
66 if (m->control_pressed) { break; }
68 iStringStream >> temp; m->gobble(iStringStream);
70 m->binLabelsInFile.push_back(temp);
75 }else { label = m->saveNextLabel; }
77 //reset labels, currentLabels may have gotten changed as otus were eliminated because of group choices or sampling
78 m->currentBinLabels = m-> binLabelsInFile;
80 //read in first row since you know there is at least 1 group.
85 //add new vector to lookup
86 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
87 lookup.push_back(temp);
88 lookup[0]->setLabel(label);
89 lookup[0]->setGroup(groupN);
91 m->namesOfGroups.push_back(groupN);
93 //fill vector. data = first sharedrabund in file
94 for(int i=0;i<num;i++){
97 lookup[0]->push_back(inputData, groupN); //abundance, bin, group
98 push_back(inputData, groupN);
100 if (inputData > maxRank) { maxRank = inputData; }
105 if (f.eof() != true) { f >> nextLabel; }
107 //read the rest of the groups info in
108 while ((nextLabel == holdLabel) && (f.eof() != true)) {
112 m->namesOfGroups.push_back(groupN);
114 //add new vector to lookup
115 temp = new SharedRAbundFloatVector();
116 lookup.push_back(temp);
117 lookup[count]->setLabel(label);
118 lookup[count]->setGroup(groupN);
121 for(int i=0;i<num;i++){
123 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
128 if (f.eof() != true) { f >> nextLabel; }
131 m->saveNextLabel = nextLabel;
134 catch(exception& e) {
135 m->errorOut(e, "SharedRAbundFloatVector", "SharedRAbundFloatVector");
140 /***********************************************************************/
142 void SharedRAbundFloatVector::set(int binNumber, float newBinSize, string groupname){
144 float oldBinSize = data[binNumber].abundance;
145 data[binNumber].abundance = newBinSize;
146 data[binNumber].group = groupname;
148 if(newBinSize > maxRank) { newBinSize = newBinSize; }
150 numSeqs += (newBinSize - oldBinSize);
152 catch(exception& e) {
153 m->errorOut(e, "SharedRAbundFloatVector", "set");
157 /***********************************************************************/
159 void SharedRAbundFloatVector::clear(){
164 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
167 /***********************************************************************/
168 float SharedRAbundFloatVector::getAbundance(int index){
169 return data[index].abundance;
171 /***********************************************************************/
172 individualFloat SharedRAbundFloatVector::get(int index){
175 /***********************************************************************/
176 void SharedRAbundFloatVector::push_back(float binSize, string groupName){
178 individualFloat newGuy;
179 newGuy.abundance = binSize;
180 newGuy.group = groupName;
181 newGuy.bin = data.size();
183 data.push_back(newGuy);
186 if(binSize > maxRank){ maxRank = binSize; }
190 catch(exception& e) {
191 m->errorOut(e, "SharedRAbundFloatVector", "push_back");
195 /***********************************************************************/
196 void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
198 individualFloat newGuy;
199 newGuy.abundance = binSize;
200 newGuy.group = groupName;
203 data.insert(data.begin()+otu, newGuy);
206 if(binSize > maxRank){ maxRank = binSize; }
210 catch(exception& e) {
211 m->errorOut(e, "SharedRAbundFloatVector", "insert");
216 /***********************************************************************/
217 void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
219 individualFloat newGuy;
220 newGuy.abundance = binSize;
221 newGuy.group = groupName;
224 data.insert(data.begin(), newGuy);
227 if(binSize > maxRank){ maxRank = binSize; }
231 catch(exception& e) {
232 m->errorOut(e, "SharedRAbundFloatVector", "push_front");
236 /**********************************************************************/
237 void SharedRAbundFloatVector::pop_back(){
238 numSeqs -= data[data.size()-1].abundance;
242 /***********************************************************************/
243 void SharedRAbundFloatVector::resize(int size){
246 /**********************************************************************/
247 int SharedRAbundFloatVector::size(){
250 /***********************************************************************/
251 void SharedRAbundFloatVector::printHeaders(ostream& output){
253 output << "label\tGroup\tnumOtus\t";
254 if (m->sharedHeaderMode == "tax") {
255 for (int i = 0; i < numBins; i++) {
257 //if there is a bin label use it otherwise make one
258 string binLabel = "PhyloType" + toString(i+1);
259 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
261 output << binLabel << '\t';
265 for (int i = 0; i < numBins; i++) {
266 //if there is a bin label use it otherwise make one
267 string binLabel = "Otu" + toString(i+1);
268 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
270 output << binLabel << '\t';
276 m->printedHeaders = true;
278 catch(exception& e) {
279 m->errorOut(e, "SharedRAbundVector", "printHeaders");
283 /***********************************************************************/
284 void SharedRAbundFloatVector::print(ostream& output){
286 output << numBins << '\t';
288 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
291 catch(exception& e) {
292 m->errorOut(e, "SharedRAbundFloatVector", "print");
296 /***********************************************************************/
297 string SharedRAbundFloatVector::getGroup(){
300 /***********************************************************************/
301 void SharedRAbundFloatVector::setGroup(string groupName){
304 /***********************************************************************/
305 int SharedRAbundFloatVector::getGroupIndex() { return index; }
306 /***********************************************************************/
307 void SharedRAbundFloatVector::setGroupIndex(int vIndex) { index = vIndex; }
308 /***********************************************************************/
309 int SharedRAbundFloatVector::getNumBins(){ return numBins; }
310 /***********************************************************************/
311 float SharedRAbundFloatVector::getNumSeqs(){ return numSeqs; }
312 /***********************************************************************/
313 float SharedRAbundFloatVector::getMaxRank(){ return maxRank; }
314 /***********************************************************************/
315 SharedRAbundFloatVector SharedRAbundFloatVector::getSharedRAbundFloatVector(){
318 /***********************************************************************
319 SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
321 SharedRAbundVector rav(numBins);
325 for (int i = 0; i < data.size(); i++) {
327 rav.push_back(data[i].abundance);
331 catch(exception& e) {
332 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
336 /***********************************************************************/
337 vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
340 util = new SharedUtil();
342 util->setGroups(m->Groups, m->namesOfGroups);
345 for (int i = 0; i < lookup.size(); i++) {
346 //if this sharedrabund is not from a group the user wants then delete it.
347 if (util->isValidGroup(lookup[i]->getGroup(), m->Groups) == false) {
348 delete lookup[i]; lookup[i] = NULL;
349 lookup.erase(lookup.begin()+i);
357 if (remove) { eliminateZeroOTUS(lookup); }
361 catch(exception& e) {
362 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
366 /***********************************************************************/
368 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
370 RAbundVector rav(numBins);
372 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
377 catch(exception& e) {
378 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
382 /***********************************************************************
384 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
386 SharedSAbundVector sav(maxRank+1);
388 for(int i=0;i<data.size();i++){
389 int abund = data[i].abundance;
390 sav.set(abund, sav.getAbundance(abund) + 1, group);
393 sav.set(0, 0, group);
399 catch(exception& e) {
400 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
404 /***********************************************************************/
406 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
408 SAbundVector sav(ceil(maxRank)+1);
410 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
416 catch(exception& e) {
417 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");
422 /***********************************************************************
424 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
426 SharedOrderVector ov;
428 for(int i=0;i<data.size();i++){
429 int round = ceil(data[i].abundance);
430 for(int j=0;j<round;j++){
431 ov.push_back(data[i].bin, round, data[i].group);
434 random_shuffle(ov.begin(), ov.end());
441 catch(exception& e) {
442 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
446 /***********************************************************************/
447 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
448 OrderVector SharedRAbundFloatVector::getOrderVector(map<string,int>* nameMap = NULL) {
452 for(int i=0;i<data.size();i++){
453 int round = ceil(data[i].abundance);
454 for(int j=0;j<round;j++){
458 random_shuffle(ov.begin(), ov.end());
463 catch(exception& e) {
464 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
468 //**********************************************************************************************************************
469 int SharedRAbundFloatVector::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thislookup) {
472 vector<SharedRAbundFloatVector*> newLookup;
473 for (int i = 0; i < thislookup.size(); i++) {
474 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
475 temp->setLabel(thislookup[i]->getLabel());
476 temp->setGroup(thislookup[i]->getGroup());
477 newLookup.push_back(temp);
481 vector<string> newBinLabels;
482 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
483 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
485 //look at each sharedRabund and make sure they are not all zero
487 for (int j = 0; j < thislookup.size(); j++) {
488 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
491 //if they are not all zero add this bin
493 for (int j = 0; j < thislookup.size(); j++) {
494 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
496 //if there is a bin label use it otherwise make one
497 string binLabel = "Otu" + toString(i+1);
498 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
500 newBinLabels.push_back(binLabel);
504 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
506 thislookup = newLookup;
507 m->currentBinLabels = newBinLabels;
512 catch(exception& e) {
513 m->errorOut(e, "SharedRAbundFloatVector", "eliminateZeroOTUS");
517 /***********************************************************************/