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)) {
132 if (num != 1000) { break; }
135 allGroups.push_back(groupN);
137 //add new vector to lookup
138 temp = new SharedRAbundFloatVector();
139 lookup.push_back(temp);
140 lookup[count]->setLabel(label);
141 lookup[count]->setGroup(groupN);
144 for(int i=0;i<num;i++){
146 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
151 if (f.eof() != true) { f >> nextLabel; }
154 m->saveNextLabel = nextLabel;
155 m->setAllGroups(allGroups);
158 catch(exception& e) {
159 m->errorOut(e, "SharedRAbundFloatVector", "SharedRAbundFloatVector");
164 /***********************************************************************/
166 void SharedRAbundFloatVector::set(int binNumber, float newBinSize, string groupname){
168 float oldBinSize = data[binNumber].abundance;
169 data[binNumber].abundance = newBinSize;
170 data[binNumber].group = groupname;
172 if(newBinSize > maxRank) { newBinSize = newBinSize; }
174 numSeqs += (newBinSize - oldBinSize);
176 catch(exception& e) {
177 m->errorOut(e, "SharedRAbundFloatVector", "set");
181 /***********************************************************************/
183 void SharedRAbundFloatVector::clear(){
188 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
191 /***********************************************************************/
192 float SharedRAbundFloatVector::getAbundance(int index){
193 return data[index].abundance;
195 /***********************************************************************/
196 individualFloat SharedRAbundFloatVector::get(int index){
199 /***********************************************************************/
200 void SharedRAbundFloatVector::push_back(float binSize, string groupName){
202 individualFloat newGuy;
203 newGuy.abundance = binSize;
204 newGuy.group = groupName;
205 newGuy.bin = data.size();
207 data.push_back(newGuy);
210 if(binSize > maxRank){ maxRank = binSize; }
214 catch(exception& e) {
215 m->errorOut(e, "SharedRAbundFloatVector", "push_back");
219 /***********************************************************************/
220 void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
222 individualFloat newGuy;
223 newGuy.abundance = binSize;
224 newGuy.group = groupName;
227 data.insert(data.begin()+otu, newGuy);
230 if(binSize > maxRank){ maxRank = binSize; }
234 catch(exception& e) {
235 m->errorOut(e, "SharedRAbundFloatVector", "insert");
240 /***********************************************************************/
241 void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
243 individualFloat newGuy;
244 newGuy.abundance = binSize;
245 newGuy.group = groupName;
248 data.insert(data.begin(), newGuy);
251 if(binSize > maxRank){ maxRank = binSize; }
255 catch(exception& e) {
256 m->errorOut(e, "SharedRAbundFloatVector", "push_front");
260 /**********************************************************************/
261 void SharedRAbundFloatVector::pop_back(){
262 numSeqs -= data[data.size()-1].abundance;
266 /***********************************************************************/
267 void SharedRAbundFloatVector::resize(int size){
270 /**********************************************************************/
271 int SharedRAbundFloatVector::size(){
274 /***********************************************************************/
275 void SharedRAbundFloatVector::printHeaders(ostream& output){
277 string snumBins = toString(numBins);
278 output << "label\tGroup\tnumOtus\t";
279 if (m->sharedHeaderMode == "tax") {
280 for (int i = 0; i < numBins; i++) {
282 //if there is a bin label use it otherwise make one
283 string binLabel = "PhyloType";
284 string sbinNumber = toString(i+1);
285 if (sbinNumber.length() < snumBins.length()) {
286 int diff = snumBins.length() - sbinNumber.length();
287 for (int h = 0; h < diff; h++) { binLabel += "0"; }
289 binLabel += sbinNumber;
290 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
292 output << binLabel << '\t';
296 for (int i = 0; i < numBins; i++) {
297 //if there is a bin label use it otherwise make one
298 string binLabel = "Otu";
299 string sbinNumber = toString(i+1);
300 if (sbinNumber.length() < snumBins.length()) {
301 int diff = snumBins.length() - sbinNumber.length();
302 for (int h = 0; h < diff; h++) { binLabel += "0"; }
304 binLabel += sbinNumber;
305 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
307 output << binLabel << '\t';
313 m->printedHeaders = true;
315 catch(exception& e) {
316 m->errorOut(e, "SharedRAbundVector", "printHeaders");
320 /***********************************************************************/
321 void SharedRAbundFloatVector::print(ostream& output){
323 output << numBins << '\t';
325 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
328 catch(exception& e) {
329 m->errorOut(e, "SharedRAbundFloatVector", "print");
333 /***********************************************************************/
334 string SharedRAbundFloatVector::getGroup(){
337 /***********************************************************************/
338 void SharedRAbundFloatVector::setGroup(string groupName){
341 /***********************************************************************/
342 int SharedRAbundFloatVector::getGroupIndex() { return index; }
343 /***********************************************************************/
344 void SharedRAbundFloatVector::setGroupIndex(int vIndex) { index = vIndex; }
345 /***********************************************************************/
346 int SharedRAbundFloatVector::getNumBins(){ return numBins; }
347 /***********************************************************************/
348 float SharedRAbundFloatVector::getNumSeqs(){ return numSeqs; }
349 /***********************************************************************/
350 float SharedRAbundFloatVector::getMaxRank(){ return maxRank; }
351 /***********************************************************************/
352 SharedRAbundFloatVector SharedRAbundFloatVector::getSharedRAbundFloatVector(){
355 /***********************************************************************
356 SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
358 SharedRAbundVector rav(numBins);
362 for (int i = 0; i < data.size(); i++) {
364 rav.push_back(data[i].abundance);
368 catch(exception& e) {
369 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
373 ***********************************************************************/
374 vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
377 util = new SharedUtil();
379 vector<string> Groups = m->getGroups();
380 vector<string> allGroups = m->getAllGroups();
381 util->setGroups(Groups, allGroups);
382 m->setGroups(Groups);
385 for (int i = 0; i < lookup.size(); i++) {
386 //if this sharedrabund is not from a group the user wants then delete it.
387 if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) {
388 delete lookup[i]; lookup[i] = NULL;
389 lookup.erase(lookup.begin()+i);
397 if (remove) { eliminateZeroOTUS(lookup); }
401 catch(exception& e) {
402 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
406 /***********************************************************************/
408 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
410 RAbundVector rav(numBins);
412 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
417 catch(exception& e) {
418 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
422 /***********************************************************************
424 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
426 SharedSAbundVector sav(maxRank+1);
428 for(int i=0;i<data.size();i++){
429 int abund = data[i].abundance;
430 sav.set(abund, sav.getAbundance(abund) + 1, group);
433 sav.set(0, 0, group);
439 catch(exception& e) {
440 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
444 ***********************************************************************/
446 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
448 SAbundVector sav(ceil(maxRank)+1);
450 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
456 catch(exception& e) {
457 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");
462 /***********************************************************************
464 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
466 SharedOrderVector ov;
468 for(int i=0;i<data.size();i++){
469 int round = ceil(data[i].abundance);
470 for(int j=0;j<round;j++){
471 ov.push_back(data[i].bin, round, data[i].group);
474 random_shuffle(ov.begin(), ov.end());
481 catch(exception& e) {
482 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
486 ***********************************************************************/
487 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
488 OrderVector SharedRAbundFloatVector::getOrderVector(map<string,int>* nameMap = NULL) {
492 for(int i=0;i<data.size();i++){
493 int round = ceil(data[i].abundance);
494 for(int j=0;j<round;j++){
498 random_shuffle(ov.begin(), ov.end());
503 catch(exception& e) {
504 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
508 //**********************************************************************************************************************
509 int SharedRAbundFloatVector::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thislookup) {
512 vector<SharedRAbundFloatVector*> newLookup;
513 for (int i = 0; i < thislookup.size(); i++) {
514 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
515 temp->setLabel(thislookup[i]->getLabel());
516 temp->setGroup(thislookup[i]->getGroup());
517 newLookup.push_back(temp);
521 vector<string> newBinLabels;
522 string snumBins = toString(thislookup[0]->getNumBins());
523 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
524 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
526 //look at each sharedRabund and make sure they are not all zero
528 for (int j = 0; j < thislookup.size(); j++) {
529 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
532 //if they are not all zero add this bin
534 for (int j = 0; j < thislookup.size(); j++) {
535 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
537 //if there is a bin label use it otherwise make one
538 string binLabel = "Otu";
539 string sbinNumber = toString(i+1);
540 if (sbinNumber.length() < snumBins.length()) {
541 int diff = snumBins.length() - sbinNumber.length();
542 for (int h = 0; h < diff; h++) { binLabel += "0"; }
544 binLabel += sbinNumber;
545 if (i < m->currentBinLabels.size()) { binLabel = m->currentBinLabels[i]; }
547 newBinLabels.push_back(binLabel);
551 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
553 thislookup = newLookup;
554 m->currentBinLabels = newBinLabels;
559 catch(exception& e) {
560 m->errorOut(e, "SharedRAbundFloatVector", "eliminateZeroOTUS");
564 /***********************************************************************/