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