]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundfloatvector.cpp
adding labels to list file.
[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->sharedBinLabelsInFile.clear();
66                                 while(!iStringStream.eof()){
67                                         if (m->control_pressed) { break; }
68                                         string temp;
69                                         iStringStream >> temp;  m->gobble(iStringStream);
70                     
71                                         m->sharedBinLabelsInFile.push_back(temp);
72                                 }
73                                 
74                                 f >> label >> groupN >> num;
75                         }else {
76                 //read in first row since you know there is at least 1 group.
77                 f >> groupN >> num;
78                 
79                 //make binlabels because we don't have any
80                 string snumBins = toString(num);
81                 m->sharedBinLabelsInFile.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"; }
89                     }
90                     binLabel += sbinNumber;
91                     m->sharedBinLabelsInFile.push_back(binLabel);
92                 }
93             }
94                 }else { 
95             label = m->saveNextLabel; 
96             
97             //read in first row since you know there is at least 1 group.
98             f >> groupN >> num;
99         }
100                 
101                 //reset labels, currentLabels may have gotten changed as otus were eliminated because of group choices or sampling
102                 m->currentSharedBinLabels = m->sharedBinLabelsInFile;
103                 
104                 holdLabel = label;
105                 
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);
111                 
112                 allGroups.push_back(groupN);
113                 
114                 //fill vector.  data = first sharedrabund in file
115                 for(int i=0;i<num;i++){
116                         f >> inputData;
117                         
118                         lookup[0]->push_back(inputData, groupN); //abundance, bin, group
119                         push_back(inputData, groupN);
120                         
121                         if (inputData > maxRank) { maxRank = inputData; }
122                 }
123                 
124                 m->gobble(f);
125                 
126                 if (f.eof() != true) { f >> nextLabel; }
127                 
128                 //read the rest of the groups info in
129                 while ((nextLabel == holdLabel) && (f.eof() != true)) {
130                         f >> groupN >> num;
131             
132                         count++;
133                         
134                         allGroups.push_back(groupN);
135                         
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);
141
142                         //fill vector.  
143                         for(int i=0;i<num;i++){
144                                 f >> inputData;
145                                 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
146                         }
147                         
148                         m->gobble(f);
149                                 
150                         if (f.eof() != true) { f >> nextLabel; }
151                 }
152                 
153                 m->saveNextLabel = nextLabel;
154                 m->setAllGroups(allGroups);
155         }
156         catch(exception& e) {
157                 m->errorOut(e, "SharedRAbundFloatVector", "SharedRAbundFloatVector");
158                 exit(1);
159         }
160 }
161
162 /***********************************************************************/
163
164 void SharedRAbundFloatVector::set(int binNumber, float newBinSize, string groupname){
165         try {
166                 float oldBinSize = data[binNumber].abundance;
167                 data[binNumber].abundance = newBinSize;
168                 data[binNumber].group = groupname;
169                 
170                 if(newBinSize > maxRank)        {       newBinSize = newBinSize;        }
171         
172                 numSeqs += (newBinSize - oldBinSize);
173         }
174         catch(exception& e) {
175                 m->errorOut(e, "SharedRAbundFloatVector", "set");
176                 exit(1);
177         }
178 }
179 /***********************************************************************/
180
181 void SharedRAbundFloatVector::clear(){
182         numBins = 0;
183         maxRank = 0;
184         numSeqs = 0;
185         data.clear();
186         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
187         lookup.clear();
188 }
189 /***********************************************************************/
190 float SharedRAbundFloatVector::getAbundance(int index){
191         return data[index].abundance;   
192 }
193 /***********************************************************************/
194 //returns vector of abundances
195 vector<float> SharedRAbundFloatVector::getAbundances(){
196     vector<float> abunds;
197     for (int i = 0; i < data.size(); i++) {
198         abunds.push_back(data[i].abundance);
199     }
200     
201         return abunds;
202 }
203 /***********************************************************************/
204 individualFloat SharedRAbundFloatVector::get(int index){
205         return data[index];     
206 }
207 /***********************************************************************/
208 void SharedRAbundFloatVector::push_back(float binSize, string groupName){
209         try {
210                 individualFloat newGuy;
211                 newGuy.abundance = binSize;
212                 newGuy.group = groupName;
213                 newGuy.bin = data.size();
214                 
215                 data.push_back(newGuy);
216                 numBins++;
217         
218                 if(binSize > maxRank){  maxRank = binSize;      }
219         
220                 numSeqs += binSize;
221         }
222         catch(exception& e) {
223                 m->errorOut(e, "SharedRAbundFloatVector", "push_back");
224                 exit(1);
225         }
226 }
227 /***********************************************************************/
228 void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
229         try {
230                 individualFloat newGuy;
231                 newGuy.abundance = binSize;
232                 newGuy.group = groupName;
233                 newGuy.bin = otu;
234                 
235                 data.insert(data.begin()+otu, newGuy);
236                 numBins++;
237         
238                 if(binSize > maxRank){  maxRank = binSize;      }
239
240                 numSeqs += binSize;
241         }
242         catch(exception& e) {
243                 m->errorOut(e, "SharedRAbundFloatVector", "insert");
244                 exit(1);
245         }
246 }
247
248 /***********************************************************************/
249 void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
250         try {
251                 individualFloat newGuy;
252                 newGuy.abundance = binSize;
253                 newGuy.group = groupName;
254                 newGuy.bin = otu;
255                 
256                 data.insert(data.begin(), newGuy);
257                 numBins++;
258         
259                 if(binSize > maxRank){  maxRank = binSize;      }
260         
261                 numSeqs += binSize;
262         }
263         catch(exception& e) {
264                 m->errorOut(e, "SharedRAbundFloatVector", "push_front");
265                 exit(1);
266         }
267 }
268 /**********************************************************************/
269 void SharedRAbundFloatVector::pop_back(){
270         numSeqs -= data[data.size()-1].abundance;
271         numBins--;
272         data.pop_back();
273 }
274 /***********************************************************************/
275 void SharedRAbundFloatVector::resize(int size){
276         data.resize(size);
277 }
278 /**********************************************************************/
279 int SharedRAbundFloatVector::size(){
280         return data.size();
281 }
282 /***********************************************************************/
283 void SharedRAbundFloatVector::printHeaders(ostream& output){
284         try {
285                 string snumBins = toString(numBins);
286                 output << "label\tGroup\tnumOtus\t";
287                 if (m->sharedHeaderMode == "tax") {
288                         for (int i = 0; i < numBins; i++) {  
289                                 
290                                 //if there is a bin label use it otherwise make one
291                                 string binLabel = "PhyloType";
292                                 string sbinNumber = toString(i+1);
293                                 if (sbinNumber.length() < snumBins.length()) { 
294                                         int diff = snumBins.length() - sbinNumber.length();
295                                         for (int h = 0; h < diff; h++) { binLabel += "0"; }
296                                 }
297                                 binLabel += sbinNumber;
298                                 if (i < m->currentSharedBinLabels.size()) {  binLabel = m->currentSharedBinLabels[i]; }
299                                 
300                                 output << binLabel << '\t'; 
301                         }
302                         output << endl;
303                 }else {
304                         for (int i = 0; i < numBins; i++) {  
305                                 //if there is a bin label use it otherwise make one
306                                 string binLabel = "Otu";
307                                 string sbinNumber = toString(i+1);
308                                 if (sbinNumber.length() < snumBins.length()) { 
309                                         int diff = snumBins.length() - sbinNumber.length();
310                                         for (int h = 0; h < diff; h++) { binLabel += "0"; }
311                                 }
312                                 binLabel += sbinNumber;
313                                 if (i < m->currentSharedBinLabels.size()) {  binLabel = m->currentSharedBinLabels[i]; }
314                                 
315                                 output << binLabel << '\t'; 
316                         }
317                         
318                         output << endl;
319                 }
320                 
321                 m->printedSharedHeaders = true;
322         }
323         catch(exception& e) {
324                 m->errorOut(e, "SharedRAbundVector", "printHeaders");
325                 exit(1);
326         }
327 }
328 /***********************************************************************/
329 void SharedRAbundFloatVector::print(ostream& output){
330         try {
331                 output << numBins << '\t';
332         
333                 for(int i=0;i<data.size();i++){         output << data[i].abundance << '\t';            }
334                 output << endl;
335         }
336         catch(exception& e) {
337                 m->errorOut(e, "SharedRAbundFloatVector", "print");
338                 exit(1);
339         }
340 }
341 /***********************************************************************/
342 string SharedRAbundFloatVector::getGroup(){
343         return group;
344 }
345 /***********************************************************************/
346 void SharedRAbundFloatVector::setGroup(string groupName){
347         group = groupName;
348 }
349 /***********************************************************************/
350 int SharedRAbundFloatVector::getGroupIndex()  { return index; }
351 /***********************************************************************/
352 void SharedRAbundFloatVector::setGroupIndex(int vIndex) { index = vIndex; }
353 /***********************************************************************/
354 int SharedRAbundFloatVector::getNumBins(){      return numBins; }
355 /***********************************************************************/
356 float SharedRAbundFloatVector::getNumSeqs(){    return numSeqs; }
357 /***********************************************************************/
358 float SharedRAbundFloatVector::getMaxRank(){    return maxRank; }
359 /***********************************************************************/
360 SharedRAbundFloatVector SharedRAbundFloatVector::getSharedRAbundFloatVector(){
361         return *this;                   
362 }
363 /***********************************************************************
364 SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
365         try {
366                 SharedRAbundVector rav(numBins);
367                 rav.setLabel(label);
368                 rav.setGroup(group);
369                 
370                 for (int i = 0; i < data.size(); i++) {
371                         
372                         rav.push_back(data[i].abundance);
373                 }
374                 
375         }
376         catch(exception& e) {
377                 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
378                 exit(1);
379         }               
380 }
381 ***********************************************************************/
382 vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
383         try {
384                 SharedUtil* util;
385                 util = new SharedUtil();
386                 
387                 vector<string> Groups = m->getGroups();
388                 vector<string> allGroups = m->getAllGroups();
389                 util->setGroups(Groups, allGroups);
390                 m->setGroups(Groups);
391                 
392                 bool remove = false;
393                 for (int i = 0; i < lookup.size(); i++) {
394                         //if this sharedrabund is not from a group the user wants then delete it.
395                         if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) { 
396                                 delete lookup[i]; lookup[i] = NULL;
397                                 lookup.erase(lookup.begin()+i); 
398                                 i--; 
399                                 remove = true;
400                         }
401                 }
402                 
403                 delete util;
404                 
405                 if (remove) { eliminateZeroOTUS(lookup); }
406         
407                 return lookup;
408         }
409         catch(exception& e) {
410                 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
411                 exit(1);
412         }
413 }
414 /***********************************************************************/
415
416 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
417         try {
418                 RAbundVector rav(numBins);
419                 
420                 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
421                 
422                 rav.setLabel(label);
423                 return rav;
424         }
425         catch(exception& e) {
426                 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
427                 exit(1);
428         }
429 }
430 /***********************************************************************
431
432 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
433         try {
434                 SharedSAbundVector sav(maxRank+1);
435                 
436                 for(int i=0;i<data.size();i++){
437                         int abund = data[i].abundance;
438                         sav.set(abund, sav.getAbundance(abund) + 1, group);
439                 }
440                 
441                 sav.set(0, 0, group);
442                 sav.setLabel(label);
443                 sav.setGroup(group);
444                 
445                 return sav;
446         }
447         catch(exception& e) {
448                 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
449                 exit(1);
450         }
451 }
452 ***********************************************************************/
453
454 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
455         try {
456                 SAbundVector sav(ceil(maxRank)+1);
457                 
458                 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
459                 
460                 sav.set(0, 0);
461                 sav.setLabel(label);
462                 return sav;
463         }
464         catch(exception& e) {
465                 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");           
466                 exit(1);
467         }
468 }
469
470 /***********************************************************************
471
472 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
473         try {
474                 SharedOrderVector ov;
475         
476                 for(int i=0;i<data.size();i++){
477                         int round = ceil(data[i].abundance);
478                         for(int j=0;j<round;j++){
479                                 ov.push_back(data[i].bin, round, data[i].group);
480                         }
481                 }
482                 random_shuffle(ov.begin(), ov.end());
483
484                 ov.setLabel(label);     
485                 ov.updateStats();
486                 
487                 return ov;
488         }
489         catch(exception& e) {
490                 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
491                 exit(1);
492         }
493 }
494 ***********************************************************************/
495 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
496 OrderVector SharedRAbundFloatVector::getOrderVector(map<string,int>* nameMap = NULL) {
497         try {
498                 OrderVector ov;
499         
500                 for(int i=0;i<data.size();i++){
501                         int round = ceil(data[i].abundance);
502                         for(int j=0;j<round;j++){
503                                 ov.push_back(i);
504                         }
505                 }
506                 random_shuffle(ov.begin(), ov.end());
507
508                 ov.setLabel(label);     
509                 return ov;
510         }
511         catch(exception& e) {
512                 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
513                 exit(1);
514         }
515 }
516 //**********************************************************************************************************************
517 int SharedRAbundFloatVector::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thislookup) {
518         try {
519                 
520                 vector<SharedRAbundFloatVector*> newLookup;
521                 for (int i = 0; i < thislookup.size(); i++) {
522                         SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
523                         temp->setLabel(thislookup[i]->getLabel());
524                         temp->setGroup(thislookup[i]->getGroup());
525                         newLookup.push_back(temp);
526                 }
527                 
528                 //for each bin
529                 vector<string> newBinLabels;
530                 string snumBins = toString(thislookup[0]->getNumBins());
531                 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
532                         if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) {  delete newLookup[j];  } return 0; }
533                         
534                         //look at each sharedRabund and make sure they are not all zero
535                         bool allZero = true;
536                         for (int j = 0; j < thislookup.size(); j++) {
537                                 if (thislookup[j]->getAbundance(i) != 0) { allZero = false;  break;  }
538                         }
539                         
540                         //if they are not all zero add this bin
541                         if (!allZero) {
542                                 for (int j = 0; j < thislookup.size(); j++) {
543                                         newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
544                                 }
545                                 //if there is a bin label use it otherwise make one
546                                 string binLabel = "Otu";
547                                 string sbinNumber = toString(i+1);
548                                 if (sbinNumber.length() < snumBins.length()) { 
549                                         int diff = snumBins.length() - sbinNumber.length();
550                                         for (int h = 0; h < diff; h++) { binLabel += "0"; }
551                                 }
552                                 binLabel += sbinNumber; 
553                                 if (i < m->currentSharedBinLabels.size()) {  binLabel = m->currentSharedBinLabels[i]; }
554                                 
555                                 newBinLabels.push_back(binLabel);
556                         }
557                 }
558                 
559                 for (int j = 0; j < thislookup.size(); j++) {  delete thislookup[j];  }
560                 
561                 thislookup = newLookup;
562                 m->currentSharedBinLabels = newBinLabels;
563                 
564                 return 0;
565                 
566         }
567         catch(exception& e) {
568                 m->errorOut(e, "SharedRAbundFloatVector", "eliminateZeroOTUS");
569                 exit(1);
570         }
571 }
572 /***********************************************************************/
573