]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundfloatvector.cpp
check in for trip
[mothur.git] / sharedrabundfloatvector.cpp
1 /*
2  *  sharedrabundfloatvector.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 8/18/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "sharedrabundfloatvector.h"
11 #include "sharedutilities.h"
12
13 /***********************************************************************/
14
15 SharedRAbundFloatVector::SharedRAbundFloatVector() : DataVector(), maxRank(0.0), numBins(0), numSeqs(0.0) {}
16 /***********************************************************************/
17
18 SharedRAbundFloatVector::~SharedRAbundFloatVector() {}
19
20 /***********************************************************************/
21 SharedRAbundFloatVector::SharedRAbundFloatVector(int n) : DataVector(), maxRank(0.0), numBins(n), numSeqs(0.0) {
22                 
23                 individualFloat newGuy;
24                 //initialize data
25                 for (int i=0; i< n; i++) {
26                         newGuy.bin = i;
27                         newGuy.abundance = 0.0;
28                         data.push_back(newGuy);
29                 }
30 }
31 /***********************************************************************/
32 //reads a shared file
33 SharedRAbundFloatVector::SharedRAbundFloatVector(ifstream& f) : DataVector(), maxRank(0.0), numBins(0), numSeqs(0.0) {
34         try {
35                 
36                 m->clearAllGroups();
37                 vector<string> allGroups;
38                 
39                 int num, count;
40                 float inputData;
41                 count = 0;  
42                 string holdLabel, nextLabel, groupN;
43                 individualFloat newguy;
44                 
45                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
46                 lookup.clear();
47                 
48                 //are we at the beginning of the file??
49                 if (m->saveNextLabel == "") {  
50                         f >> label; 
51                         
52                         //is this a shared file that has headers
53                         if (label == "label") { 
54                                 //gets "group"
55                                 f >> label; m->gobble(f);
56                                 
57                                 //gets "numOtus"
58                                 f >> label; m->gobble(f);
59                                 
60                                 //eat rest of line
61                                 label = m->getline(f); m->gobble(f);
62                                 
63                                 //parse labels to save
64                                 istringstream iStringStream(label);
65                                 m->binLabelsInFile.clear();
66                                 while(!iStringStream.eof()){
67                                         if (m->control_pressed) { break; }
68                                         string temp;
69                                         iStringStream >> temp;  m->gobble(iStringStream);
70                                         
71                                         m->binLabelsInFile.push_back(temp);
72                                 }
73                                 
74                                 f >> label;
75                         }
76                 }else { label = m->saveNextLabel; }
77                 
78                 //reset labels, currentLabels may have gotten changed as otus were eliminated because of group choices or sampling
79                 m->currentBinLabels = m-> binLabelsInFile;
80                 
81                 //read in first row since you know there is at least 1 group.
82                 f >> groupN >> num;
83
84                 holdLabel = label;
85                 
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);
91                 
92                 allGroups.push_back(groupN);
93                 
94                 //fill vector.  data = first sharedrabund in file
95                 for(int i=0;i<num;i++){
96                         f >> inputData;
97                         
98                         lookup[0]->push_back(inputData, groupN); //abundance, bin, group
99                         push_back(inputData, groupN);
100                         
101                         if (inputData > maxRank) { maxRank = inputData; }
102                 }
103                 
104                 m->gobble(f);
105                 
106                 if (f.eof() != true) { f >> nextLabel; }
107                 
108                 //read the rest of the groups info in
109                 while ((nextLabel == holdLabel) && (f.eof() != true)) {
110                         f >> groupN >> num;
111                         count++;
112                         
113                         allGroups.push_back(groupN);
114                         
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);
120
121                         //fill vector.  
122                         for(int i=0;i<num;i++){
123                                 f >> inputData;
124                                 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
125                         }
126                         
127                         m->gobble(f);
128                                 
129                         if (f.eof() != true) { f >> nextLabel; }
130                 }
131                 
132                 m->saveNextLabel = nextLabel;
133                 m->setAllGroups(allGroups);
134         
135         }
136         catch(exception& e) {
137                 m->errorOut(e, "SharedRAbundFloatVector", "SharedRAbundFloatVector");
138                 exit(1);
139         }
140 }
141
142 /***********************************************************************/
143
144 void SharedRAbundFloatVector::set(int binNumber, float newBinSize, string groupname){
145         try {
146                 float oldBinSize = data[binNumber].abundance;
147                 data[binNumber].abundance = newBinSize;
148                 data[binNumber].group = groupname;
149                 
150                 if(newBinSize > maxRank)        {       newBinSize = newBinSize;        }
151         
152                 numSeqs += (newBinSize - oldBinSize);
153         }
154         catch(exception& e) {
155                 m->errorOut(e, "SharedRAbundFloatVector", "set");
156                 exit(1);
157         }
158 }
159 /***********************************************************************/
160
161 void SharedRAbundFloatVector::clear(){
162         numBins = 0;
163         maxRank = 0;
164         numSeqs = 0;
165         data.clear();
166         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
167         lookup.clear();
168 }
169 /***********************************************************************/
170 float SharedRAbundFloatVector::getAbundance(int index){
171         return data[index].abundance;   
172 }
173 /***********************************************************************/
174 individualFloat SharedRAbundFloatVector::get(int index){
175         return data[index];     
176 }
177 /***********************************************************************/
178 void SharedRAbundFloatVector::push_back(float binSize, string groupName){
179         try {
180                 individualFloat newGuy;
181                 newGuy.abundance = binSize;
182                 newGuy.group = groupName;
183                 newGuy.bin = data.size();
184                 
185                 data.push_back(newGuy);
186                 numBins++;
187         
188                 if(binSize > maxRank){  maxRank = binSize;      }
189         
190                 numSeqs += binSize;
191         }
192         catch(exception& e) {
193                 m->errorOut(e, "SharedRAbundFloatVector", "push_back");
194                 exit(1);
195         }
196 }
197 /***********************************************************************/
198 void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
199         try {
200                 individualFloat newGuy;
201                 newGuy.abundance = binSize;
202                 newGuy.group = groupName;
203                 newGuy.bin = otu;
204                 
205                 data.insert(data.begin()+otu, newGuy);
206                 numBins++;
207         
208                 if(binSize > maxRank){  maxRank = binSize;      }
209
210                 numSeqs += binSize;
211         }
212         catch(exception& e) {
213                 m->errorOut(e, "SharedRAbundFloatVector", "insert");
214                 exit(1);
215         }
216 }
217
218 /***********************************************************************/
219 void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
220         try {
221                 individualFloat newGuy;
222                 newGuy.abundance = binSize;
223                 newGuy.group = groupName;
224                 newGuy.bin = otu;
225                 
226                 data.insert(data.begin(), newGuy);
227                 numBins++;
228         
229                 if(binSize > maxRank){  maxRank = binSize;      }
230         
231                 numSeqs += binSize;
232         }
233         catch(exception& e) {
234                 m->errorOut(e, "SharedRAbundFloatVector", "push_front");
235                 exit(1);
236         }
237 }
238 /**********************************************************************/
239 void SharedRAbundFloatVector::pop_back(){
240         numSeqs -= data[data.size()-1].abundance;
241         numBins--;
242         data.pop_back();
243 }
244 /***********************************************************************/
245 void SharedRAbundFloatVector::resize(int size){
246         data.resize(size);
247 }
248 /**********************************************************************/
249 int SharedRAbundFloatVector::size(){
250         return data.size();
251 }
252 /***********************************************************************/
253 void SharedRAbundFloatVector::printHeaders(ostream& output){
254         try {
255                 string snumBins = toString(numBins);
256                 output << "label\tGroup\tnumOtus\t";
257                 if (m->sharedHeaderMode == "tax") {
258                         for (int i = 0; i < numBins; i++) {  
259                                 
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"; }
266                                 }
267                                 binLabel += sbinNumber;
268                                 if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
269                                 
270                                 output << binLabel << '\t'; 
271                         }
272                         output << endl;
273                 }else {
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"; }
281                                 }
282                                 binLabel += sbinNumber;
283                                 if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
284                                 
285                                 output << binLabel << '\t'; 
286                         }
287                         
288                         output << endl;
289                 }
290                 
291                 m->printedHeaders = true;
292         }
293         catch(exception& e) {
294                 m->errorOut(e, "SharedRAbundVector", "printHeaders");
295                 exit(1);
296         }
297 }
298 /***********************************************************************/
299 void SharedRAbundFloatVector::print(ostream& output){
300         try {
301                 output << numBins << '\t';
302         
303                 for(int i=0;i<data.size();i++){         output << data[i].abundance << '\t';            }
304                 output << endl;
305         }
306         catch(exception& e) {
307                 m->errorOut(e, "SharedRAbundFloatVector", "print");
308                 exit(1);
309         }
310 }
311 /***********************************************************************/
312 string SharedRAbundFloatVector::getGroup(){
313         return group;
314 }
315 /***********************************************************************/
316 void SharedRAbundFloatVector::setGroup(string groupName){
317         group = groupName;
318 }
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(){
331         return *this;                   
332 }
333 /***********************************************************************
334 SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
335         try {
336                 SharedRAbundVector rav(numBins);
337                 rav.setLabel(label);
338                 rav.setGroup(group);
339                 
340                 for (int i = 0; i < data.size(); i++) {
341                         
342                         rav.push_back(data[i].abundance);
343                 }
344                 
345         }
346         catch(exception& e) {
347                 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
348                 exit(1);
349         }               
350 }
351 /***********************************************************************/
352 vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
353         try {
354                 SharedUtil* util;
355                 util = new SharedUtil();
356                 
357                 vector<string> Groups = m->getGroups();
358                 vector<string> allGroups = m->getAllGroups();
359                 util->setGroups(Groups, allGroups);
360                 m->setGroups(Groups);
361                 
362                 bool remove = false;
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); 
368                                 i--; 
369                                 remove = true;
370                         }
371                 }
372                 
373                 delete util;
374                 
375                 if (remove) { eliminateZeroOTUS(lookup); }
376         
377                 return lookup;
378         }
379         catch(exception& e) {
380                 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
381                 exit(1);
382         }
383 }
384 /***********************************************************************/
385
386 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
387         try {
388                 RAbundVector rav(numBins);
389                 
390                 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
391                 
392                 rav.setLabel(label);
393                 return rav;
394         }
395         catch(exception& e) {
396                 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
397                 exit(1);
398         }
399 }
400 /***********************************************************************
401
402 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
403         try {
404                 SharedSAbundVector sav(maxRank+1);
405                 
406                 for(int i=0;i<data.size();i++){
407                         int abund = data[i].abundance;
408                         sav.set(abund, sav.getAbundance(abund) + 1, group);
409                 }
410                 
411                 sav.set(0, 0, group);
412                 sav.setLabel(label);
413                 sav.setGroup(group);
414                 
415                 return sav;
416         }
417         catch(exception& e) {
418                 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
419                 exit(1);
420         }
421 }
422 /***********************************************************************/
423
424 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
425         try {
426                 SAbundVector sav(ceil(maxRank)+1);
427                 
428                 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
429                 
430                 sav.set(0, 0);
431                 sav.setLabel(label);
432                 return sav;
433         }
434         catch(exception& e) {
435                 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");           
436                 exit(1);
437         }
438 }
439
440 /***********************************************************************
441
442 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
443         try {
444                 SharedOrderVector ov;
445         
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);
450                         }
451                 }
452                 random_shuffle(ov.begin(), ov.end());
453
454                 ov.setLabel(label);     
455                 ov.updateStats();
456                 
457                 return ov;
458         }
459         catch(exception& e) {
460                 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
461                 exit(1);
462         }
463 }
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) {
467         try {
468                 OrderVector ov;
469         
470                 for(int i=0;i<data.size();i++){
471                         int round = ceil(data[i].abundance);
472                         for(int j=0;j<round;j++){
473                                 ov.push_back(i);
474                         }
475                 }
476                 random_shuffle(ov.begin(), ov.end());
477
478                 ov.setLabel(label);     
479                 return ov;
480         }
481         catch(exception& e) {
482                 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
483                 exit(1);
484         }
485 }
486 //**********************************************************************************************************************
487 int SharedRAbundFloatVector::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thislookup) {
488         try {
489                 
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);
496                 }
497                 
498                 //for each bin
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; }
503                         
504                         //look at each sharedRabund and make sure they are not all zero
505                         bool allZero = true;
506                         for (int j = 0; j < thislookup.size(); j++) {
507                                 if (thislookup[j]->getAbundance(i) != 0) { allZero = false;  break;  }
508                         }
509                         
510                         //if they are not all zero add this bin
511                         if (!allZero) {
512                                 for (int j = 0; j < thislookup.size(); j++) {
513                                         newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
514                                 }
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"; }
521                                 }
522                                 binLabel += sbinNumber; 
523                                 if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
524                                 
525                                 newBinLabels.push_back(binLabel);
526                         }
527                 }
528                 
529                 for (int j = 0; j < thislookup.size(); j++) {  delete thislookup[j];  }
530                 
531                 thislookup = newLookup;
532                 m->currentBinLabels = newBinLabels;
533                 
534                 return 0;
535                 
536         }
537         catch(exception& e) {
538                 m->errorOut(e, "SharedRAbundFloatVector", "eliminateZeroOTUS");
539                 exit(1);
540         }
541 }
542 /***********************************************************************/
543