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