]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
added logfile feature
[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(data.size());
383                 
384                 for (int i = 0; i < data.size(); i++) {
385                         rav.set(i, data[i].abundance);
386                 }
387         
388                 return rav;
389         }
390         catch(exception& e) {
391                 errorOut(e, "SharedRAbundVector", "getRAbundVector");
392                 exit(1);
393         }
394 }
395 /***********************************************************************/
396
397 RAbundVector SharedRAbundVector::getRAbundVector2() {
398         try {
399                 RAbundVector rav;
400                 for(int i = 0; i < numBins; i++)
401                         if(data[i].abundance != 0)
402                                 rav.push_back(data[i].abundance-1);
403                 return rav;
404         }
405         catch(exception& e) {
406                 errorOut(e, "SharedRAbundVector", "getRAbundVector2");
407                 exit(1);
408         }
409 }
410 /***********************************************************************/
411
412 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
413         try {
414                 SharedSAbundVector sav(maxRank+1);
415                 
416                 for(int i=0;i<data.size();i++){
417                         int abund = data[i].abundance;
418                         sav.set(abund, sav.getAbundance(abund) + 1, group);
419                 }
420                 
421                 sav.set(0, 0, group);
422                 sav.setLabel(label);
423                 sav.setGroup(group);
424                 
425                 return sav;
426         }
427         catch(exception& e) {
428                 errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
429                 exit(1);
430         }
431 }
432 /***********************************************************************/
433
434 SAbundVector SharedRAbundVector::getSAbundVector() {
435         try {
436                 SAbundVector sav(maxRank+1);
437                 
438                 for(int i=0;i<data.size();i++){
439                         int abund = data[i].abundance;
440                         sav.set(abund, sav.get(abund) + 1);
441                 }
442                 sav.set(0, 0);
443                 sav.setLabel(label);
444                 return sav;
445         }
446         catch(exception& e) {
447                 errorOut(e, "SharedRAbundVector", "getSAbundVector");           
448                 exit(1);
449         }
450 }
451
452 /***********************************************************************/
453
454 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
455         try {
456                 SharedOrderVector ov;
457         
458                 for(int i=0;i<data.size();i++){
459                         for(int j=0;j<data[i].abundance;j++){
460                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
461                         }
462                 }
463                 random_shuffle(ov.begin(), ov.end());
464
465                 ov.setLabel(label);     
466                 ov.updateStats();
467                 
468                 return ov;
469         }
470         catch(exception& e) {
471                 errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
472                 exit(1);
473         }
474 }
475 /***********************************************************************/
476
477 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
478         try {
479                 OrderVector ov;
480         
481                 for(int i=0;i<data.size();i++){
482                         for(int j=0;j<data[i].abundance;j++){
483                                 ov.push_back(i);
484                         }
485                 }
486                 random_shuffle(ov.begin(), ov.end());
487
488                 ov.setLabel(label);     
489                 return ov;
490         }
491         catch(exception& e) {
492                 errorOut(e, "SharedRAbundVector", "getOrderVector");
493                 exit(1);
494         }
495 }
496
497 /***********************************************************************/
498