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