]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundfloatvector.cpp
working on testing
[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, "SharedRAbundVector", "set");
135                 exit(1);
136         }
137 }
138
139 /***********************************************************************/
140 float SharedRAbundFloatVector::getAbundance(int index){
141         return data[index].abundance;   
142 }
143 /***********************************************************************/
144 individualFloat SharedRAbundFloatVector::get(int index){
145         return data[index];     
146 }
147 /***********************************************************************/
148 void SharedRAbundFloatVector::push_back(float binSize, string groupName){
149         try {
150                 individualFloat newGuy;
151                 newGuy.abundance = binSize;
152                 newGuy.group = groupName;
153                 newGuy.bin = data.size();
154                 
155                 data.push_back(newGuy);
156                 numBins++;
157         
158                 if(binSize > maxRank){  maxRank = binSize;      }
159         
160                 numSeqs += binSize;
161         }
162         catch(exception& e) {
163                 m->errorOut(e, "SharedRAbundFloatVector", "push_back");
164                 exit(1);
165         }
166 }
167 /***********************************************************************/
168 void SharedRAbundFloatVector::insert(float binSize, int otu, string groupName){
169         try {
170                 individualFloat newGuy;
171                 newGuy.abundance = binSize;
172                 newGuy.group = groupName;
173                 newGuy.bin = otu;
174                 
175                 data.insert(data.begin()+otu, newGuy);
176                 numBins++;
177         
178                 if(binSize > maxRank){  maxRank = binSize;      }
179
180                 numSeqs += binSize;
181         }
182         catch(exception& e) {
183                 m->errorOut(e, "SharedRAbundFloatVector", "insert");
184                 exit(1);
185         }
186 }
187
188 /***********************************************************************/
189 void SharedRAbundFloatVector::push_front(float binSize, int otu, string groupName){
190         try {
191                 individualFloat newGuy;
192                 newGuy.abundance = binSize;
193                 newGuy.group = groupName;
194                 newGuy.bin = otu;
195                 
196                 data.insert(data.begin(), newGuy);
197                 numBins++;
198         
199                 if(binSize > maxRank){  maxRank = binSize;      }
200         
201                 numSeqs += binSize;
202         }
203         catch(exception& e) {
204                 m->errorOut(e, "SharedRAbundFloatVector", "push_front");
205                 exit(1);
206         }
207 }
208 /**********************************************************************/
209 void SharedRAbundFloatVector::pop_back(){
210         numSeqs -= data[data.size()-1].abundance;
211         numBins--;
212         data.pop_back();
213 }
214 /***********************************************************************/
215 void SharedRAbundFloatVector::resize(int size){
216         data.resize(size);
217 }
218 /**********************************************************************/
219 int SharedRAbundFloatVector::size(){
220         return data.size();
221 }
222 /***********************************************************************/
223 void SharedRAbundFloatVector::print(ostream& output){
224         try {
225                 output << numBins << '\t';
226         
227                 for(int i=0;i<data.size();i++){         output << data[i].abundance << '\t';            }
228                 output << endl;
229         }
230         catch(exception& e) {
231                 m->errorOut(e, "SharedRAbundFloatVector", "print");
232                 exit(1);
233         }
234 }
235 /***********************************************************************/
236 string SharedRAbundFloatVector::getGroup(){
237         return group;
238 }
239 /***********************************************************************/
240 void SharedRAbundFloatVector::setGroup(string groupName){
241         group = groupName;
242 }
243 /***********************************************************************/
244 int SharedRAbundFloatVector::getGroupIndex()  { return index; }
245 /***********************************************************************/
246 void SharedRAbundFloatVector::setGroupIndex(int vIndex) { index = vIndex; }
247 /***********************************************************************/
248 int SharedRAbundFloatVector::getNumBins(){      return numBins; }
249 /***********************************************************************/
250 float SharedRAbundFloatVector::getNumSeqs(){    return numSeqs; }
251 /***********************************************************************/
252 float SharedRAbundFloatVector::getMaxRank(){    return maxRank; }
253 /***********************************************************************/
254 SharedRAbundFloatVector SharedRAbundFloatVector::getSharedRAbundFloatVector(){
255         return *this;                   
256 }
257 /***********************************************************************
258 SharedRAbundVector SharedRAbundFloatVector::getSharedRAbundVector(){
259         try {
260                 SharedRAbundVector rav(numBins);
261                 rav.setLabel(label);
262                 rav.setGroup(group);
263                 
264                 for (int i = 0; i < data.size(); i++) {
265                         
266                         rav.push_back(data[i].abundance);
267                 }
268                 
269         }
270         catch(exception& e) {
271                 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundVector");
272                 exit(1);
273         }               
274 }
275 /***********************************************************************/
276 vector<SharedRAbundFloatVector*> SharedRAbundFloatVector::getSharedRAbundFloatVectors(){
277         try {
278                 SharedUtil* util;
279                 util = new SharedUtil();
280                 
281                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
282
283                 for (int i = 0; i < lookup.size(); i++) {
284                         //if this sharedrabund is not from a group the user wants then delete it.
285                         if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) { 
286                                 delete lookup[i]; lookup[i] = NULL;
287                                 lookup.erase(lookup.begin()+i); 
288                                 i--; 
289                         }
290                 }
291                 
292                 delete util;
293         
294                 return lookup;
295         }
296         catch(exception& e) {
297                 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
298                 exit(1);
299         }
300 }
301 /***********************************************************************/
302
303 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
304         try {
305                 RAbundVector rav(numBins);
306                 
307                 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
308                 
309                 rav.setLabel(label);
310                 return rav;
311         }
312         catch(exception& e) {
313                 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
314                 exit(1);
315         }
316 }
317 /***********************************************************************
318
319 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
320         try {
321                 SharedSAbundVector sav(maxRank+1);
322                 
323                 for(int i=0;i<data.size();i++){
324                         int abund = data[i].abundance;
325                         sav.set(abund, sav.getAbundance(abund) + 1, group);
326                 }
327                 
328                 sav.set(0, 0, group);
329                 sav.setLabel(label);
330                 sav.setGroup(group);
331                 
332                 return sav;
333         }
334         catch(exception& e) {
335                 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
336                 exit(1);
337         }
338 }
339 /***********************************************************************/
340
341 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
342         try {
343                 SAbundVector sav(ceil(maxRank)+1);
344                 
345                 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
346                 
347                 sav.set(0, 0);
348                 sav.setLabel(label);
349                 return sav;
350         }
351         catch(exception& e) {
352                 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");           
353                 exit(1);
354         }
355 }
356
357 /***********************************************************************
358
359 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
360         try {
361                 SharedOrderVector ov;
362         
363                 for(int i=0;i<data.size();i++){
364                         int round = ceil(data[i].abundance);
365                         for(int j=0;j<round;j++){
366                                 ov.push_back(data[i].bin, round, data[i].group);
367                         }
368                 }
369                 random_shuffle(ov.begin(), ov.end());
370
371                 ov.setLabel(label);     
372                 ov.updateStats();
373                 
374                 return ov;
375         }
376         catch(exception& e) {
377                 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
378                 exit(1);
379         }
380 }
381 /***********************************************************************/
382 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
383 OrderVector SharedRAbundFloatVector::getOrderVector(map<string,int>* nameMap = NULL) {
384         try {
385                 OrderVector ov;
386         
387                 for(int i=0;i<data.size();i++){
388                         int round = ceil(data[i].abundance);
389                         for(int j=0;j<round;j++){
390                                 ov.push_back(i);
391                         }
392                 }
393                 random_shuffle(ov.begin(), ov.end());
394
395                 ov.setLabel(label);     
396                 return ov;
397         }
398         catch(exception& e) {
399                 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
400                 exit(1);
401         }
402 }
403
404 /***********************************************************************/
405