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