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