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