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