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) {
37 vector<string> allGroups;
42 string holdLabel, nextLabel, groupN;
43 individualFloat newguy;
45 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
48 //are we at the beginning of the file??
49 if (m->saveNextLabel == "") {
52 //is this a shared file that has headers
53 if (label == "label") {
55 f >> label; m->gobble(f);
58 f >> label; m->gobble(f);
61 label = m->getline(f); m->gobble(f);
63 //parse labels to save
64 istringstream iStringStream(label);
65 m->binLabelsInFile.clear();
66 while(!iStringStream.eof()){
67 if (m->control_pressed) { break; }
69 iStringStream >> temp; m->gobble(iStringStream);
71 m->binLabelsInFile.push_back(temp);
76 }else { label = m->saveNextLabel; }
78 //reset labels, currentLabels may have gotten changed as otus were eliminated because of group choices or sampling
79 m->currentBinLabels = m-> binLabelsInFile;
81 //read in first row since you know there is at least 1 group.
86 //add new vector to lookup
87 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
88 lookup.push_back(temp);
89 lookup[0]->setLabel(label);
90 lookup[0]->setGroup(groupN);
92 allGroups.push_back(groupN);
94 //fill vector. data = first sharedrabund in file
95 for(int i=0;i<num;i++){
98 lookup[0]->push_back(inputData, groupN); //abundance, bin, group
99 push_back(inputData, groupN);
101 if (inputData > maxRank) { maxRank = inputData; }
106 if (f.eof() != true) { f >> nextLabel; }
108 //read the rest of the groups info in
109 while ((nextLabel == holdLabel) && (f.eof() != true)) {
113 allGroups.push_back(groupN);
115 //add new vector to lookup
116 temp = new SharedRAbundFloatVector();
117 lookup.push_back(temp);
118 lookup[count]->setLabel(label);
119 lookup[count]->setGroup(groupN);
122 for(int i=0;i<num;i++){
124 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
129 if (f.eof() != true) { f >> nextLabel; }
132 m->saveNextLabel = nextLabel;
133 m->setAllGroups(allGroups);
136 catch(exception& e) {
137 m->errorOut(e, "SharedRAbundFloatVector", "SharedRAbundFloatVector");
142 /***********************************************************************/
144 void SharedRAbundFloatVector::set(int binNumber, float newBinSize, string groupname){
146 float oldBinSize = data[binNumber].abundance;
147 data[binNumber].abundance = newBinSize;
148 data[binNumber].group = groupname;
150 if(newBinSize > maxRank) { newBinSize = newBinSize; }
152 numSeqs += (newBinSize - oldBinSize);
154 catch(exception& e) {
155 m->errorOut(e, "SharedRAbundFloatVector", "set");
159 /***********************************************************************/
161 void SharedRAbundFloatVector::clear(){
166 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
169 /***********************************************************************/
170 float SharedRAbundFloatVector::getAbundance(int index){
171 return data[index].abundance;
173 /***********************************************************************/
174 individualFloat SharedRAbundFloatVector::get(int index){
177 /***********************************************************************/
178 void SharedRAbundFloatVector::push_back(float binSize, string groupName){
180 individualFloat newGuy;
181 newGuy.abundance = binSize;
182 newGuy.group = groupName;
183 newGuy.bin = data.size();
185 data.push_back(newGuy);
188 if(binSize > maxRank){ maxRank = binSize; }
192 catch(exception& e) {
193 m->errorOut(e, "SharedRAbundFloatVector", "push_back");
197 /***********************************************************************/
198 void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
200 individualFloat newGuy;
201 newGuy.abundance = binSize;
202 newGuy.group = groupName;
205 data.insert(data.begin()+otu, newGuy);
208 if(binSize > maxRank){ maxRank = binSize; }
212 catch(exception& e) {
213 m->errorOut(e, "SharedRAbundFloatVector", "insert");
218 /***********************************************************************/
219 void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
221 individualFloat newGuy;
222 newGuy.abundance = binSize;
223 newGuy.group = groupName;
226 data.insert(data.begin(), newGuy);
229 if(binSize > maxRank){ maxRank = binSize; }
233 catch(exception& e) {
234 m->errorOut(e, "SharedRAbundFloatVector", "push_front");
238 /**********************************************************************/
239 void SharedRAbundFloatVector::pop_back(){
240 numSeqs -= data[data.size()-1].abundance;
244 /***********************************************************************/
245 void SharedRAbundFloatVector::resize(int size){
248 /**********************************************************************/
249 int SharedRAbundFloatVector::size(){
252 /***********************************************************************/
253 void SharedRAbundFloatVector::printHeaders(ostream& output){
255 string snumBins = toString(numBins);
256 output << "label\tGroup\tnumOtus\t";
257 if (m->sharedHeaderMode == "tax") {
258 for (int i = 0; i < numBins; i++) {
260 //if there is a bin label use it otherwise make one
261 string binLabel = "PhyloType";
262 string sbinNumber = toString(i+1);
263 if (sbinNumber.length() < snumBins.length()) {
264 int diff = snumBins.length() - sbinNumber.length();
265 for (int h = 0; h < diff; h++) { binLabel += "0"; }
267 binLabel += sbinNumber;
268 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
270 output << binLabel << '\t';
274 for (int i = 0; i < numBins; i++) {
275 //if there is a bin label use it otherwise make one
276 string binLabel = "Otu";
277 string sbinNumber = toString(i+1);
278 if (sbinNumber.length() < snumBins.length()) {
279 int diff = snumBins.length() - sbinNumber.length();
280 for (int h = 0; h < diff; h++) { binLabel += "0"; }
282 binLabel += sbinNumber;
283 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
285 output << binLabel << '\t';
291 m->printedHeaders = true;
293 catch(exception& e) {
294 m->errorOut(e, "SharedRAbundVector", "printHeaders");
298 /***********************************************************************/
299 void SharedRAbundFloatVector::print(ostream& output){
301 output << numBins << '\t';
303 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
306 catch(exception& e) {
307 m->errorOut(e, "SharedRAbundFloatVector", "print");
311 /***********************************************************************/
312 string SharedRAbundFloatVector::getGroup(){
315 /***********************************************************************/
316 void SharedRAbundFloatVector::setGroup(string groupName){
319 /***********************************************************************/
320 int SharedRAbundFloatVector::getGroupIndex() { return index; }
321 /***********************************************************************/
322 void SharedRAbundFloatVector::setGroupIndex(int vIndex) { index = vIndex; }
323 /***********************************************************************/
324 int SharedRAbundFloatVector::getNumBins(){ return numBins; }
325 /***********************************************************************/
326 float SharedRAbundFloatVector::getNumSeqs(){ return numSeqs; }
327 /***********************************************************************/
328 float SharedRAbundFloatVector::getMaxRank(){ return maxRank; }
329 /***********************************************************************/
330 SharedRAbundFloatVector SharedRAbundFloatVector::getSharedRAbundFloatVector(){
333 /***********************************************************************
334 SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
336 SharedRAbundVector rav(numBins);
340 for (int i = 0; i < data.size(); i++) {
342 rav.push_back(data[i].abundance);
346 catch(exception& e) {
347 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
351 /***********************************************************************/
352 vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
355 util = new SharedUtil();
357 vector<string> Groups = m->getGroups();
358 vector<string> allGroups = m->getAllGroups();
359 util->setGroups(Groups, allGroups);
360 m->setGroups(Groups);
363 for (int i = 0; i < lookup.size(); i++) {
364 //if this sharedrabund is not from a group the user wants then delete it.
365 if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) {
366 delete lookup[i]; lookup[i] = NULL;
367 lookup.erase(lookup.begin()+i);
375 if (remove) { eliminateZeroOTUS(lookup); }
379 catch(exception& e) {
380 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
384 /***********************************************************************/
386 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
388 RAbundVector rav(numBins);
390 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
395 catch(exception& e) {
396 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
400 /***********************************************************************
402 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
404 SharedSAbundVector sav(maxRank+1);
406 for(int i=0;i<data.size();i++){
407 int abund = data[i].abundance;
408 sav.set(abund, sav.getAbundance(abund) + 1, group);
411 sav.set(0, 0, group);
417 catch(exception& e) {
418 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
422 /***********************************************************************/
424 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
426 SAbundVector sav(ceil(maxRank)+1);
428 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
434 catch(exception& e) {
435 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");
440 /***********************************************************************
442 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
444 SharedOrderVector ov;
446 for(int i=0;i<data.size();i++){
447 int round = ceil(data[i].abundance);
448 for(int j=0;j<round;j++){
449 ov.push_back(data[i].bin, round, data[i].group);
452 random_shuffle(ov.begin(), ov.end());
459 catch(exception& e) {
460 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
464 /***********************************************************************/
465 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
466 OrderVector SharedRAbundFloatVector::getOrderVector(map<string,int>* nameMap = NULL) {
470 for(int i=0;i<data.size();i++){
471 int round = ceil(data[i].abundance);
472 for(int j=0;j<round;j++){
476 random_shuffle(ov.begin(), ov.end());
481 catch(exception& e) {
482 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
486 //**********************************************************************************************************************
487 int SharedRAbundFloatVector::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thislookup) {
490 vector<SharedRAbundFloatVector*> newLookup;
491 for (int i = 0; i < thislookup.size(); i++) {
492 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
493 temp->setLabel(thislookup[i]->getLabel());
494 temp->setGroup(thislookup[i]->getGroup());
495 newLookup.push_back(temp);
499 vector<string> newBinLabels;
500 string snumBins = toString(thislookup[0]->getNumBins());
501 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
502 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
504 //look at each sharedRabund and make sure they are not all zero
506 for (int j = 0; j < thislookup.size(); j++) {
507 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
510 //if they are not all zero add this bin
512 for (int j = 0; j < thislookup.size(); j++) {
513 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
515 //if there is a bin label use it otherwise make one
516 string binLabel = "Otu";
517 string sbinNumber = toString(i+1);
518 if (sbinNumber.length() < snumBins.length()) {
519 int diff = snumBins.length() - sbinNumber.length();
520 for (int h = 0; h < diff; h++) { binLabel += "0"; }
522 binLabel += sbinNumber;
523 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
525 newBinLabels.push_back(binLabel);
529 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
531 thislookup = newLookup;
532 m->currentBinLabels = newBinLabels;
537 catch(exception& e) {
538 m->errorOut(e, "SharedRAbundFloatVector", "eliminateZeroOTUS");
542 /***********************************************************************/