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