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