]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
fixed metastats, added resize to cluster.classic, added code to kill children if...
[mothur.git] / sharedrabundvector.cpp
1 /*
2  *  sharedvector.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 12/5/08.
6  *  Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "sharedrabundvector.h" 
11 #include "sabundvector.hpp"
12 #include "ordervector.hpp"
13 #include "sharedutilities.h"
14
15
16 /***********************************************************************/
17
18 SharedRAbundVector::SharedRAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {globaldata = GlobalData::getInstance();}
19 /***********************************************************************/
20
21 SharedRAbundVector::~SharedRAbundVector() {
22         //for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
23
24 }
25
26 /***********************************************************************/
27
28 SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
29                 globaldata = GlobalData::getInstance();
30                 individual newGuy;
31                 //initialize data
32                 for (int i=0; i< n; i++) {
33                         newGuy.bin = i;
34                         newGuy.abundance = 0;
35                         data.push_back(newGuy);
36                 }
37 }
38
39 /***********************************************************************
40
41 SharedRAbundVector::SharedRAbundVector(string id, vector<individual> rav) : DataVector(id), data(rav) {
42         try {
43                 numBins = 0;
44                 maxRank = 0;
45                 numSeqs = 0;
46                 
47                 for(int i=0;i<data.size();i++){
48                         if(data[i].abundance != 0)              {       numBins = i+1;          }
49                         if(data[i].abundance > maxRank) {       maxRank = data[i].abundance;    }
50                         numSeqs += data[i].abundance;
51                 }
52         }
53         catch(exception& e) {
54                 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
55                 exit(1);
56         }
57 }
58
59
60 /***********************************************************************/
61 //reads a shared file
62 SharedRAbundVector::SharedRAbundVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
63         try {
64                 globaldata = GlobalData::getInstance();
65                 
66                 if (globaldata->gGroupmap == NULL) {  groupmap = new GroupMap(); }
67                 
68                 int num, inputData, count;
69                 count = 0;  
70                 string holdLabel, nextLabel, groupN;
71                 individual newguy;
72                 
73                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
74                 lookup.clear();
75                 
76                 if (globaldata->saveNextLabel == "") {  f >> label;  }
77                 else { label = globaldata->saveNextLabel; }
78                 
79                 //read in first row since you know there is at least 1 group.
80                 f >> groupN >> num;
81
82                 holdLabel = label;
83                 
84                 //add new vector to lookup
85                 SharedRAbundVector* temp = new SharedRAbundVector();
86                 lookup.push_back(temp);
87                 lookup[0]->setLabel(label);
88                 lookup[0]->setGroup(groupN);
89                 
90                 if (globaldata->gGroupmap == NULL) { 
91                         //save group in groupmap
92                         groupmap->namesOfGroups.push_back(groupN);
93                         groupmap->groupIndex[groupN] = 0;
94                 }
95                 
96                 //fill vector.  data = first sharedrabund in file
97                 for(int i=0;i<num;i++){
98                         f >> inputData;
99                         
100                         lookup[0]->push_back(inputData, groupN); //abundance, bin, group
101                         push_back(inputData, groupN);
102                         //numSeqs += inputData;
103                         //numBins++;
104                         if (inputData > maxRank) { maxRank = inputData; }
105                 }
106                 
107                 m->gobble(f);
108                 
109                 if (!(f.eof())) { f >> nextLabel; }
110         
111                 //read the rest of the groups info in
112                 while ((nextLabel == holdLabel) && (f.eof() != true)) {
113                         f >> groupN >> num;
114                         count++;
115                         
116                         if (globaldata->gGroupmap == NULL) { 
117                                 //save group in groupmap
118         
119                                 groupmap->namesOfGroups.push_back(groupN);
120                                 groupmap->groupIndex[groupN] = count;
121                         }
122                         
123                         //add new vector to lookup
124                         temp = new SharedRAbundVector();
125                         lookup.push_back(temp);
126                         lookup[count]->setLabel(label);
127                         lookup[count]->setGroup(groupN);
128
129                         //fill vector.  
130                         for(int i=0;i<num;i++){
131                                 f >> inputData;
132                                 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
133                         }
134                         
135                         m->gobble(f);
136                                 
137                         if (f.eof() != true) { f >> nextLabel; }
138                 }
139         
140                 globaldata->saveNextLabel = nextLabel;
141         
142                 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap;  }
143                 
144         }
145         catch(exception& e) {
146                 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
147                 exit(1);
148         }
149 }
150
151 /***********************************************************************/
152
153 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
154         try {
155                 int oldBinSize = data[binNumber].abundance;
156                 data[binNumber].abundance = newBinSize;
157                 data[binNumber].group = groupname;
158         
159                 if(newBinSize > maxRank)        {       maxRank = newBinSize;   }
160         
161                 numSeqs += (newBinSize - oldBinSize);
162         }
163         catch(exception& e) {
164                 m->errorOut(e, "SharedRAbundVector", "set");
165                 exit(1);
166         }
167 }
168 /***********************************************************************/
169
170 void SharedRAbundVector::setData(vector <individual> newData){
171         data = newData;
172 }
173
174 /***********************************************************************/
175
176 int SharedRAbundVector::getAbundance(int index){
177         return data[index].abundance;
178         
179 }
180 /***********************************************************************/
181
182 int SharedRAbundVector::numNZ(){
183         int sum = 0;
184         for(int i = 1; i < numBins; i++)
185                 if(data[i].abundance > 0)
186                         sum++;
187         return sum;
188 }
189 /***********************************************************************/
190
191 void SharedRAbundVector::sortD(){
192         struct individual indObj;
193         sort(data.begin()+1, data.end(), indObj);
194 }
195 /***********************************************************************/
196
197 individual SharedRAbundVector::get(int index){
198         return data[index];
199         
200 }
201 /***********************************************************************/
202
203 vector <individual> SharedRAbundVector::getData(){
204         return data;
205 }
206 /***********************************************************************/
207
208 void SharedRAbundVector::clear(){
209         numBins = 0;
210         maxRank = 0;
211         numSeqs = 0;
212         data.clear();
213         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
214         lookup.clear();
215 }
216 /***********************************************************************/
217
218 void SharedRAbundVector::push_back(int binSize, string groupName){
219         try {
220                 individual newGuy;
221                 newGuy.abundance = binSize;
222                 newGuy.group = groupName;
223                 newGuy.bin = data.size();
224                 
225                 data.push_back(newGuy);
226                 numBins++;
227         
228                 if(binSize > maxRank){
229                         maxRank = binSize;
230                 }
231         
232                 numSeqs += binSize;
233         }
234         catch(exception& e) {
235                 m->errorOut(e, "SharedRAbundVector", "push_back");
236                 exit(1);
237         }
238 }
239
240 /***********************************************************************/
241
242 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
243         try {
244                 individual newGuy;
245                 newGuy.abundance = binSize;
246                 newGuy.group = groupName;
247                 newGuy.bin = otu;
248                 
249                 data.insert(data.begin()+otu, newGuy);
250                 numBins++;
251         
252                 if(binSize > maxRank){
253                         maxRank = binSize;
254                 }
255         
256                 numSeqs += binSize;
257         }
258         catch(exception& e) {
259                 m->errorOut(e, "SharedRAbundVector", "insert");
260                 exit(1);
261         }
262 }
263
264 /***********************************************************************/
265
266 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
267         try {
268                 individual newGuy;
269                 newGuy.abundance = binSize;
270                 newGuy.group = groupName;
271                 newGuy.bin = otu;
272                 
273                 data.insert(data.begin(), newGuy);
274                 numBins++;
275         
276                 if(binSize > maxRank){
277                         maxRank = binSize;
278                 }
279         
280                 numSeqs += binSize;
281         }
282         catch(exception& e) {
283                 m->errorOut(e, "SharedRAbundVector", "push_front");
284                 exit(1);
285         }
286 }
287
288 /***********************************************************************/
289 void SharedRAbundVector::pop_back(){
290         numSeqs -= data[data.size()-1].abundance;
291         numBins--;
292         return data.pop_back();
293 }
294
295 /***********************************************************************/
296
297
298 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
299         return data.rbegin();                           
300 }
301
302 /***********************************************************************/
303
304 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
305         return data.rend();                                     
306 }
307
308 /***********************************************************************/
309 void SharedRAbundVector::resize(int size){
310         
311         data.resize(size);
312 }
313
314 /***********************************************************************/
315
316 int SharedRAbundVector::size(){
317         return data.size();
318 }
319
320 /***********************************************************************/
321 void SharedRAbundVector::print(ostream& output){
322         try {
323                 output << numBins << '\t';
324         
325                 for(int i=0;i<data.size();i++){         output << data[i].abundance << '\t';            }
326                 output << endl;
327         }
328         catch(exception& e) {
329                 m->errorOut(e, "SharedRAbundVector", "print");
330                 exit(1);
331         }
332 }
333 /***********************************************************************/
334 string SharedRAbundVector::getGroup(){
335         return group;
336 }
337
338 /***********************************************************************/
339
340 void SharedRAbundVector::setGroup(string groupName){
341         group = groupName;
342 }
343 /***********************************************************************/
344 int SharedRAbundVector::getGroupIndex()  { return index; }
345 /***********************************************************************/
346 void SharedRAbundVector::setGroupIndex(int vIndex)      { index = vIndex; }
347 /***********************************************************************/
348 int SharedRAbundVector::getNumBins(){
349         return numBins;
350 }
351
352 /***********************************************************************/
353
354 int SharedRAbundVector::getNumSeqs(){
355         return numSeqs;
356 }
357
358 /***********************************************************************/
359
360 int SharedRAbundVector::getMaxRank(){
361         return maxRank;
362 }
363 /***********************************************************************/
364
365 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
366         return *this;                   
367 }
368 /***********************************************************************/
369 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
370         try {
371                 SharedUtil* util;
372                 util = new SharedUtil();
373                 
374                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
375
376                 for (int i = 0; i < lookup.size(); i++) {
377                         //if this sharedrabund is not from a group the user wants then delete it.
378                         if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) { 
379                                 delete lookup[i]; lookup[i] = NULL;
380                                 lookup.erase(lookup.begin()+i); 
381                                 i--; 
382                         }
383                 }
384                 
385                 delete util;
386         
387                 return lookup;
388         }
389         catch(exception& e) {
390                 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
391                 exit(1);
392         }
393 }
394 /***********************************************************************/
395
396 RAbundVector SharedRAbundVector::getRAbundVector() {
397         try {
398                 RAbundVector rav;
399                 
400                 for (int i = 0; i < data.size(); i++) {
401                         if(data[i].abundance != 0) {
402                                 rav.push_back(data[i].abundance);
403                         }
404                 }
405                 
406                 rav.setLabel(label);
407                 return rav;
408         }
409         catch(exception& e) {
410                 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
411                 exit(1);
412         }
413 }
414 /***********************************************************************/
415
416 RAbundVector SharedRAbundVector::getRAbundVector2() {
417         try {
418                 RAbundVector rav;
419                 for(int i = 0; i < numBins; i++)
420                         if(data[i].abundance != 0)
421                                 rav.push_back(data[i].abundance-1);
422                 return rav;
423         }
424         catch(exception& e) {
425                 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
426                 exit(1);
427         }
428 }
429 /***********************************************************************/
430
431 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
432         try {
433                 SharedSAbundVector sav(maxRank+1);
434                 
435                 for(int i=0;i<data.size();i++){
436                         int abund = data[i].abundance;
437                         sav.set(abund, sav.getAbundance(abund) + 1, group);
438                 }
439                 
440                 sav.set(0, 0, group);
441                 sav.setLabel(label);
442                 sav.setGroup(group);
443                 
444                 return sav;
445         }
446         catch(exception& e) {
447                 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
448                 exit(1);
449         }
450 }
451 /***********************************************************************/
452
453 SAbundVector SharedRAbundVector::getSAbundVector() {
454         try {
455                 SAbundVector sav(maxRank+1);
456                 
457                 for(int i=0;i<data.size();i++){
458                         int abund = data[i].abundance;
459                         sav.set(abund, sav.get(abund) + 1);
460                 }
461                 sav.set(0, 0);
462                 sav.setLabel(label);
463                 return sav;
464         }
465         catch(exception& e) {
466                 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");                
467                 exit(1);
468         }
469 }
470
471 /***********************************************************************/
472
473 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
474         try {
475                 SharedOrderVector ov;
476         
477                 for(int i=0;i<data.size();i++){
478                         for(int j=0;j<data[i].abundance;j++){
479                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
480                         }
481                 }
482                 random_shuffle(ov.begin(), ov.end());
483
484                 ov.setLabel(label);     
485                 ov.updateStats();
486                 
487                 return ov;
488         }
489         catch(exception& e) {
490                 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
491                 exit(1);
492         }
493 }
494 /***********************************************************************/
495
496 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
497         try {
498                 OrderVector ov;
499                 for(int i=0;i<numBins;i++){
500                         for(int j=0;j<data[i].abundance;j++){
501                                 ov.push_back(i);
502                         }
503                 }
504                 random_shuffle(ov.begin(), ov.end());
505                 
506                 ov.setLabel(label);     
507
508                 return ov;
509         }
510         catch(exception& e) {
511                 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
512                 exit(1);
513         }
514 }
515
516 /***********************************************************************/
517