]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundfloatvector.cpp
sffinfo bug with flow grams right index when clipQualRight=0
[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 >> 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->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"; }
89                     }
90                     binLabel += sbinNumber;
91                     m->binLabelsInFile.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->currentBinLabels = m->binLabelsInFile;
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 individualFloat SharedRAbundFloatVector::get(int index){
195         return data[index];     
196 }
197 /***********************************************************************/
198 void SharedRAbundFloatVector::push_back(float binSize, string groupName){
199         try {
200                 individualFloat newGuy;
201                 newGuy.abundance = binSize;
202                 newGuy.group = groupName;
203                 newGuy.bin = data.size();
204                 
205                 data.push_back(newGuy);
206                 numBins++;
207         
208                 if(binSize > maxRank){  maxRank = binSize;      }
209         
210                 numSeqs += binSize;
211         }
212         catch(exception& e) {
213                 m->errorOut(e, "SharedRAbundFloatVector", "push_back");
214                 exit(1);
215         }
216 }
217 /***********************************************************************/
218 void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
219         try {
220                 individualFloat newGuy;
221                 newGuy.abundance = binSize;
222                 newGuy.group = groupName;
223                 newGuy.bin = otu;
224                 
225                 data.insert(data.begin()+otu, newGuy);
226                 numBins++;
227         
228                 if(binSize > maxRank){  maxRank = binSize;      }
229
230                 numSeqs += binSize;
231         }
232         catch(exception& e) {
233                 m->errorOut(e, "SharedRAbundFloatVector", "insert");
234                 exit(1);
235         }
236 }
237
238 /***********************************************************************/
239 void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
240         try {
241                 individualFloat newGuy;
242                 newGuy.abundance = binSize;
243                 newGuy.group = groupName;
244                 newGuy.bin = otu;
245                 
246                 data.insert(data.begin(), newGuy);
247                 numBins++;
248         
249                 if(binSize > maxRank){  maxRank = binSize;      }
250         
251                 numSeqs += binSize;
252         }
253         catch(exception& e) {
254                 m->errorOut(e, "SharedRAbundFloatVector", "push_front");
255                 exit(1);
256         }
257 }
258 /**********************************************************************/
259 void SharedRAbundFloatVector::pop_back(){
260         numSeqs -= data[data.size()-1].abundance;
261         numBins--;
262         data.pop_back();
263 }
264 /***********************************************************************/
265 void SharedRAbundFloatVector::resize(int size){
266         data.resize(size);
267 }
268 /**********************************************************************/
269 int SharedRAbundFloatVector::size(){
270         return data.size();
271 }
272 /***********************************************************************/
273 void SharedRAbundFloatVector::printHeaders(ostream& output){
274         try {
275                 string snumBins = toString(numBins);
276                 output << "label\tGroup\tnumOtus\t";
277                 if (m->sharedHeaderMode == "tax") {
278                         for (int i = 0; i < numBins; i++) {  
279                                 
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"; }
286                                 }
287                                 binLabel += sbinNumber;
288                                 if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
289                                 
290                                 output << binLabel << '\t'; 
291                         }
292                         output << endl;
293                 }else {
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"; }
301                                 }
302                                 binLabel += sbinNumber;
303                                 if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
304                                 
305                                 output << binLabel << '\t'; 
306                         }
307                         
308                         output << endl;
309                 }
310                 
311                 m->printedHeaders = true;
312         }
313         catch(exception& e) {
314                 m->errorOut(e, "SharedRAbundVector", "printHeaders");
315                 exit(1);
316         }
317 }
318 /***********************************************************************/
319 void SharedRAbundFloatVector::print(ostream& output){
320         try {
321                 output << numBins << '\t';
322         
323                 for(int i=0;i<data.size();i++){         output << data[i].abundance << '\t';            }
324                 output << endl;
325         }
326         catch(exception& e) {
327                 m->errorOut(e, "SharedRAbundFloatVector", "print");
328                 exit(1);
329         }
330 }
331 /***********************************************************************/
332 string SharedRAbundFloatVector::getGroup(){
333         return group;
334 }
335 /***********************************************************************/
336 void SharedRAbundFloatVector::setGroup(string groupName){
337         group = groupName;
338 }
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(){
351         return *this;                   
352 }
353 /***********************************************************************
354 SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
355         try {
356                 SharedRAbundVector rav(numBins);
357                 rav.setLabel(label);
358                 rav.setGroup(group);
359                 
360                 for (int i = 0; i < data.size(); i++) {
361                         
362                         rav.push_back(data[i].abundance);
363                 }
364                 
365         }
366         catch(exception& e) {
367                 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
368                 exit(1);
369         }               
370 }
371 ***********************************************************************/
372 vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
373         try {
374                 SharedUtil* util;
375                 util = new SharedUtil();
376                 
377                 vector<string> Groups = m->getGroups();
378                 vector<string> allGroups = m->getAllGroups();
379                 util->setGroups(Groups, allGroups);
380                 m->setGroups(Groups);
381                 
382                 bool remove = false;
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); 
388                                 i--; 
389                                 remove = true;
390                         }
391                 }
392                 
393                 delete util;
394                 
395                 if (remove) { eliminateZeroOTUS(lookup); }
396         
397                 return lookup;
398         }
399         catch(exception& e) {
400                 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
401                 exit(1);
402         }
403 }
404 /***********************************************************************/
405
406 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
407         try {
408                 RAbundVector rav(numBins);
409                 
410                 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
411                 
412                 rav.setLabel(label);
413                 return rav;
414         }
415         catch(exception& e) {
416                 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
417                 exit(1);
418         }
419 }
420 /***********************************************************************
421
422 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
423         try {
424                 SharedSAbundVector sav(maxRank+1);
425                 
426                 for(int i=0;i<data.size();i++){
427                         int abund = data[i].abundance;
428                         sav.set(abund, sav.getAbundance(abund) + 1, group);
429                 }
430                 
431                 sav.set(0, 0, group);
432                 sav.setLabel(label);
433                 sav.setGroup(group);
434                 
435                 return sav;
436         }
437         catch(exception& e) {
438                 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
439                 exit(1);
440         }
441 }
442 ***********************************************************************/
443
444 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
445         try {
446                 SAbundVector sav(ceil(maxRank)+1);
447                 
448                 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
449                 
450                 sav.set(0, 0);
451                 sav.setLabel(label);
452                 return sav;
453         }
454         catch(exception& e) {
455                 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");           
456                 exit(1);
457         }
458 }
459
460 /***********************************************************************
461
462 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
463         try {
464                 SharedOrderVector ov;
465         
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);
470                         }
471                 }
472                 random_shuffle(ov.begin(), ov.end());
473
474                 ov.setLabel(label);     
475                 ov.updateStats();
476                 
477                 return ov;
478         }
479         catch(exception& e) {
480                 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
481                 exit(1);
482         }
483 }
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) {
487         try {
488                 OrderVector ov;
489         
490                 for(int i=0;i<data.size();i++){
491                         int round = ceil(data[i].abundance);
492                         for(int j=0;j<round;j++){
493                                 ov.push_back(i);
494                         }
495                 }
496                 random_shuffle(ov.begin(), ov.end());
497
498                 ov.setLabel(label);     
499                 return ov;
500         }
501         catch(exception& e) {
502                 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
503                 exit(1);
504         }
505 }
506 //**********************************************************************************************************************
507 int SharedRAbundFloatVector::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thislookup) {
508         try {
509                 
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);
516                 }
517                 
518                 //for each bin
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; }
523                         
524                         //look at each sharedRabund and make sure they are not all zero
525                         bool allZero = true;
526                         for (int j = 0; j < thislookup.size(); j++) {
527                                 if (thislookup[j]->getAbundance(i) != 0) { allZero = false;  break;  }
528                         }
529                         
530                         //if they are not all zero add this bin
531                         if (!allZero) {
532                                 for (int j = 0; j < thislookup.size(); j++) {
533                                         newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
534                                 }
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"; }
541                                 }
542                                 binLabel += sbinNumber; 
543                                 if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
544                                 
545                                 newBinLabels.push_back(binLabel);
546                         }
547                 }
548                 
549                 for (int j = 0; j < thislookup.size(); j++) {  delete thislookup[j];  }
550                 
551                 thislookup = newLookup;
552                 m->currentBinLabels = newBinLabels;
553                 
554                 return 0;
555                 
556         }
557         catch(exception& e) {
558                 m->errorOut(e, "SharedRAbundFloatVector", "eliminateZeroOTUS");
559                 exit(1);
560         }
561 }
562 /***********************************************************************/
563