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);
74 f >> label >> groupN >> num;
76 //read in first row since you know there is at least 1 group.
79 //make binlabels because we don't have any
80 string snumBins = toString(num);
81 m->binLabelsInFile.clear();
82 for (int i = 0; i < num; i++) {
83 //if there is a bin label use it otherwise make one
84 string binLabel = "Otu";
85 string sbinNumber = toString(i+1);
86 if (sbinNumber.length() < snumBins.length()) {
87 int diff = snumBins.length() - sbinNumber.length();
88 for (int h = 0; h < diff; h++) { binLabel += "0"; }
90 binLabel += sbinNumber;
91 m->binLabelsInFile.push_back(binLabel);
95 label = m->saveNextLabel;
97 //read in first row since you know there is at least 1 group.
101 //reset labels, currentLabels may have gotten changed as otus were eliminated because of group choices or sampling
102 m->currentBinLabels = m->binLabelsInFile;
106 //add new vector to lookup
107 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
108 lookup.push_back(temp);
109 lookup[0]->setLabel(label);
110 lookup[0]->setGroup(groupN);
112 allGroups.push_back(groupN);
114 //fill vector. data = first sharedrabund in file
115 for(int i=0;i<num;i++){
118 lookup[0]->push_back(inputData, groupN); //abundance, bin, group
119 push_back(inputData, groupN);
121 if (inputData > maxRank) { maxRank = inputData; }
126 if (f.eof() != true) { f >> nextLabel; }
128 //read the rest of the groups info in
129 while ((nextLabel == holdLabel) && (f.eof() != true)) {
134 allGroups.push_back(groupN);
136 //add new vector to lookup
137 temp = new SharedRAbundFloatVector();
138 lookup.push_back(temp);
139 lookup[count]->setLabel(label);
140 lookup[count]->setGroup(groupN);
143 for(int i=0;i<num;i++){
145 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
150 if (f.eof() != true) { f >> nextLabel; }
153 m->saveNextLabel = nextLabel;
154 m->setAllGroups(allGroups);
156 catch(exception& e) {
157 m->errorOut(e, "SharedRAbundFloatVector", "SharedRAbundFloatVector");
162 /***********************************************************************/
164 void SharedRAbundFloatVector::set(int binNumber, float newBinSize, string groupname){
166 float oldBinSize = data[binNumber].abundance;
167 data[binNumber].abundance = newBinSize;
168 data[binNumber].group = groupname;
170 if(newBinSize > maxRank) { newBinSize = newBinSize; }
172 numSeqs += (newBinSize - oldBinSize);
174 catch(exception& e) {
175 m->errorOut(e, "SharedRAbundFloatVector", "set");
179 /***********************************************************************/
181 void SharedRAbundFloatVector::clear(){
186 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
189 /***********************************************************************/
190 float SharedRAbundFloatVector::getAbundance(int index){
191 return data[index].abundance;
193 /***********************************************************************/
194 individualFloat SharedRAbundFloatVector::get(int index){
197 /***********************************************************************/
198 void SharedRAbundFloatVector::push_back(float binSize, string groupName){
200 individualFloat newGuy;
201 newGuy.abundance = binSize;
202 newGuy.group = groupName;
203 newGuy.bin = data.size();
205 data.push_back(newGuy);
208 if(binSize > maxRank){ maxRank = binSize; }
212 catch(exception& e) {
213 m->errorOut(e, "SharedRAbundFloatVector", "push_back");
217 /***********************************************************************/
218 void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
220 individualFloat newGuy;
221 newGuy.abundance = binSize;
222 newGuy.group = groupName;
225 data.insert(data.begin()+otu, newGuy);
228 if(binSize > maxRank){ maxRank = binSize; }
232 catch(exception& e) {
233 m->errorOut(e, "SharedRAbundFloatVector", "insert");
238 /***********************************************************************/
239 void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
241 individualFloat newGuy;
242 newGuy.abundance = binSize;
243 newGuy.group = groupName;
246 data.insert(data.begin(), newGuy);
249 if(binSize > maxRank){ maxRank = binSize; }
253 catch(exception& e) {
254 m->errorOut(e, "SharedRAbundFloatVector", "push_front");
258 /**********************************************************************/
259 void SharedRAbundFloatVector::pop_back(){
260 numSeqs -= data[data.size()-1].abundance;
264 /***********************************************************************/
265 void SharedRAbundFloatVector::resize(int size){
268 /**********************************************************************/
269 int SharedRAbundFloatVector::size(){
272 /***********************************************************************/
273 void SharedRAbundFloatVector::printHeaders(ostream& output){
275 string snumBins = toString(numBins);
276 output << "label\tGroup\tnumOtus\t";
277 if (m->sharedHeaderMode == "tax") {
278 for (int i = 0; i < numBins; i++) {
280 //if there is a bin label use it otherwise make one
281 string binLabel = "PhyloType";
282 string sbinNumber = toString(i+1);
283 if (sbinNumber.length() < snumBins.length()) {
284 int diff = snumBins.length() - sbinNumber.length();
285 for (int h = 0; h < diff; h++) { binLabel += "0"; }
287 binLabel += sbinNumber;
288 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
290 output << binLabel << '\t';
294 for (int i = 0; i < numBins; i++) {
295 //if there is a bin label use it otherwise make one
296 string binLabel = "Otu";
297 string sbinNumber = toString(i+1);
298 if (sbinNumber.length() < snumBins.length()) {
299 int diff = snumBins.length() - sbinNumber.length();
300 for (int h = 0; h < diff; h++) { binLabel += "0"; }
302 binLabel += sbinNumber;
303 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
305 output << binLabel << '\t';
311 m->printedHeaders = true;
313 catch(exception& e) {
314 m->errorOut(e, "SharedRAbundVector", "printHeaders");
318 /***********************************************************************/
319 void SharedRAbundFloatVector::print(ostream& output){
321 output << numBins << '\t';
323 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
326 catch(exception& e) {
327 m->errorOut(e, "SharedRAbundFloatVector", "print");
331 /***********************************************************************/
332 string SharedRAbundFloatVector::getGroup(){
335 /***********************************************************************/
336 void SharedRAbundFloatVector::setGroup(string groupName){
339 /***********************************************************************/
340 int SharedRAbundFloatVector::getGroupIndex() { return index; }
341 /***********************************************************************/
342 void SharedRAbundFloatVector::setGroupIndex(int vIndex) { index = vIndex; }
343 /***********************************************************************/
344 int SharedRAbundFloatVector::getNumBins(){ return numBins; }
345 /***********************************************************************/
346 float SharedRAbundFloatVector::getNumSeqs(){ return numSeqs; }
347 /***********************************************************************/
348 float SharedRAbundFloatVector::getMaxRank(){ return maxRank; }
349 /***********************************************************************/
350 SharedRAbundFloatVector SharedRAbundFloatVector::getSharedRAbundFloatVector(){
353 /***********************************************************************
354 SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
356 SharedRAbundVector rav(numBins);
360 for (int i = 0; i < data.size(); i++) {
362 rav.push_back(data[i].abundance);
366 catch(exception& e) {
367 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
371 ***********************************************************************/
372 vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
375 util = new SharedUtil();
377 vector<string> Groups = m->getGroups();
378 vector<string> allGroups = m->getAllGroups();
379 util->setGroups(Groups, allGroups);
380 m->setGroups(Groups);
383 for (int i = 0; i < lookup.size(); i++) {
384 //if this sharedrabund is not from a group the user wants then delete it.
385 if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) {
386 delete lookup[i]; lookup[i] = NULL;
387 lookup.erase(lookup.begin()+i);
395 if (remove) { eliminateZeroOTUS(lookup); }
399 catch(exception& e) {
400 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
404 /***********************************************************************/
406 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
408 RAbundVector rav(numBins);
410 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
415 catch(exception& e) {
416 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
420 /***********************************************************************
422 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
424 SharedSAbundVector sav(maxRank+1);
426 for(int i=0;i<data.size();i++){
427 int abund = data[i].abundance;
428 sav.set(abund, sav.getAbundance(abund) + 1, group);
431 sav.set(0, 0, group);
437 catch(exception& e) {
438 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
442 ***********************************************************************/
444 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
446 SAbundVector sav(ceil(maxRank)+1);
448 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
454 catch(exception& e) {
455 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");
460 /***********************************************************************
462 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
464 SharedOrderVector ov;
466 for(int i=0;i<data.size();i++){
467 int round = ceil(data[i].abundance);
468 for(int j=0;j<round;j++){
469 ov.push_back(data[i].bin, round, data[i].group);
472 random_shuffle(ov.begin(), ov.end());
479 catch(exception& e) {
480 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
484 ***********************************************************************/
485 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
486 OrderVector SharedRAbundFloatVector::getOrderVector(map<string,int>* nameMap = NULL) {
490 for(int i=0;i<data.size();i++){
491 int round = ceil(data[i].abundance);
492 for(int j=0;j<round;j++){
496 random_shuffle(ov.begin(), ov.end());
501 catch(exception& e) {
502 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
506 //**********************************************************************************************************************
507 int SharedRAbundFloatVector::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thislookup) {
510 vector<SharedRAbundFloatVector*> newLookup;
511 for (int i = 0; i < thislookup.size(); i++) {
512 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
513 temp->setLabel(thislookup[i]->getLabel());
514 temp->setGroup(thislookup[i]->getGroup());
515 newLookup.push_back(temp);
519 vector<string> newBinLabels;
520 string snumBins = toString(thislookup[0]->getNumBins());
521 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
522 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
524 //look at each sharedRabund and make sure they are not all zero
526 for (int j = 0; j < thislookup.size(); j++) {
527 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
530 //if they are not all zero add this bin
532 for (int j = 0; j < thislookup.size(); j++) {
533 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
535 //if there is a bin label use it otherwise make one
536 string binLabel = "Otu";
537 string sbinNumber = toString(i+1);
538 if (sbinNumber.length() < snumBins.length()) {
539 int diff = snumBins.length() - sbinNumber.length();
540 for (int h = 0; h < diff; h++) { binLabel += "0"; }
542 binLabel += sbinNumber;
543 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
545 newBinLabels.push_back(binLabel);
549 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
551 thislookup = newLookup;
552 m->currentBinLabels = newBinLabels;
557 catch(exception& e) {
558 m->errorOut(e, "SharedRAbundFloatVector", "eliminateZeroOTUS");
562 /***********************************************************************/