]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundfloatvector.cpp
fixed metastats, added resize to cluster.classic, added code to kill children if...
[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                 for (int i = 0; i < lookup.size(); i++) {
293                         //if this sharedrabund is not from a group the user wants then delete it.
294                         if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) { 
295                                 delete lookup[i]; lookup[i] = NULL;
296                                 lookup.erase(lookup.begin()+i); 
297                                 i--; 
298                         }
299                 }
300                 
301                 delete util;
302         
303                 return lookup;
304         }
305         catch(exception& e) {
306                 m->errorOut(e, "SharedRAbundFloatVector", "getSharedRAbundFloatVectors");
307                 exit(1);
308         }
309 }
310 /***********************************************************************/
311
312 RAbundVector SharedRAbundFloatVector::getRAbundVector() {
313         try {
314                 RAbundVector rav(numBins);
315                 
316                 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
317                 
318                 rav.setLabel(label);
319                 return rav;
320         }
321         catch(exception& e) {
322                 m->errorOut(e, "SharedRAbundFloatVector", "getRAbundVector");
323                 exit(1);
324         }
325 }
326 /***********************************************************************
327
328 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
329         try {
330                 SharedSAbundVector sav(maxRank+1);
331                 
332                 for(int i=0;i<data.size();i++){
333                         int abund = data[i].abundance;
334                         sav.set(abund, sav.getAbundance(abund) + 1, group);
335                 }
336                 
337                 sav.set(0, 0, group);
338                 sav.setLabel(label);
339                 sav.setGroup(group);
340                 
341                 return sav;
342         }
343         catch(exception& e) {
344                 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
345                 exit(1);
346         }
347 }
348 /***********************************************************************/
349
350 SAbundVector SharedRAbundFloatVector::getSAbundVector() {
351         try {
352                 SAbundVector sav(ceil(maxRank)+1);
353                 
354                 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
355                 
356                 sav.set(0, 0);
357                 sav.setLabel(label);
358                 return sav;
359         }
360         catch(exception& e) {
361                 m->errorOut(e, "SharedRAbundFloatVector", "getSAbundVector");           
362                 exit(1);
363         }
364 }
365
366 /***********************************************************************
367
368 SharedOrderVector SharedRAbundFloatVector::getSharedOrderVector() {
369         try {
370                 SharedOrderVector ov;
371         
372                 for(int i=0;i<data.size();i++){
373                         int round = ceil(data[i].abundance);
374                         for(int j=0;j<round;j++){
375                                 ov.push_back(data[i].bin, round, data[i].group);
376                         }
377                 }
378                 random_shuffle(ov.begin(), ov.end());
379
380                 ov.setLabel(label);     
381                 ov.updateStats();
382                 
383                 return ov;
384         }
385         catch(exception& e) {
386                 m->errorOut(e, "SharedRAbundFloatVector", "getSharedOrderVector");
387                 exit(1);
388         }
389 }
390 /***********************************************************************/
391 //this is not functional, not sure how to handle it yet, but I need the stub because it is a pure function
392 OrderVector SharedRAbundFloatVector::getOrderVector(map<string,int>* nameMap = NULL) {
393         try {
394                 OrderVector ov;
395         
396                 for(int i=0;i<data.size();i++){
397                         int round = ceil(data[i].abundance);
398                         for(int j=0;j<round;j++){
399                                 ov.push_back(i);
400                         }
401                 }
402                 random_shuffle(ov.begin(), ov.end());
403
404                 ov.setLabel(label);     
405                 return ov;
406         }
407         catch(exception& e) {
408                 m->errorOut(e, "SharedRAbundFloatVector", "getOrderVector");
409                 exit(1);
410         }
411 }
412
413 /***********************************************************************/
414