]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
working on testing
[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::push_back(int binSize, string groupName){
209         try {
210                 individual newGuy;
211                 newGuy.abundance = binSize;
212                 newGuy.group = groupName;
213                 newGuy.bin = data.size();
214                 
215                 data.push_back(newGuy);
216                 numBins++;
217         
218                 if(binSize > maxRank){
219                         maxRank = binSize;
220                 }
221         
222                 numSeqs += binSize;
223         }
224         catch(exception& e) {
225                 m->errorOut(e, "SharedRAbundVector", "push_back");
226                 exit(1);
227         }
228 }
229
230 /***********************************************************************/
231
232 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
233         try {
234                 individual newGuy;
235                 newGuy.abundance = binSize;
236                 newGuy.group = groupName;
237                 newGuy.bin = otu;
238                 
239                 data.insert(data.begin()+otu, newGuy);
240                 numBins++;
241         
242                 if(binSize > maxRank){
243                         maxRank = binSize;
244                 }
245         
246                 numSeqs += binSize;
247         }
248         catch(exception& e) {
249                 m->errorOut(e, "SharedRAbundVector", "insert");
250                 exit(1);
251         }
252 }
253
254 /***********************************************************************/
255
256 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
257         try {
258                 individual newGuy;
259                 newGuy.abundance = binSize;
260                 newGuy.group = groupName;
261                 newGuy.bin = otu;
262                 
263                 data.insert(data.begin(), newGuy);
264                 numBins++;
265         
266                 if(binSize > maxRank){
267                         maxRank = binSize;
268                 }
269         
270                 numSeqs += binSize;
271         }
272         catch(exception& e) {
273                 m->errorOut(e, "SharedRAbundVector", "push_front");
274                 exit(1);
275         }
276 }
277
278 /***********************************************************************/
279 void SharedRAbundVector::pop_back(){
280         numSeqs -= data[data.size()-1].abundance;
281         numBins--;
282         return data.pop_back();
283 }
284
285 /***********************************************************************/
286
287
288 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
289         return data.rbegin();                           
290 }
291
292 /***********************************************************************/
293
294 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
295         return data.rend();                                     
296 }
297
298 /***********************************************************************/
299 void SharedRAbundVector::resize(int size){
300         
301         data.resize(size);
302 }
303
304 /***********************************************************************/
305
306 int SharedRAbundVector::size(){
307         return data.size();
308 }
309
310 /***********************************************************************/
311 void SharedRAbundVector::print(ostream& output){
312         try {
313                 output << numBins << '\t';
314         
315                 for(int i=0;i<data.size();i++){         output << data[i].abundance << '\t';            }
316                 output << endl;
317         }
318         catch(exception& e) {
319                 m->errorOut(e, "SharedRAbundVector", "print");
320                 exit(1);
321         }
322 }
323 /***********************************************************************/
324 string SharedRAbundVector::getGroup(){
325         return group;
326 }
327
328 /***********************************************************************/
329
330 void SharedRAbundVector::setGroup(string groupName){
331         group = groupName;
332 }
333 /***********************************************************************/
334 int SharedRAbundVector::getGroupIndex()  { return index; }
335 /***********************************************************************/
336 void SharedRAbundVector::setGroupIndex(int vIndex)      { index = vIndex; }
337 /***********************************************************************/
338 int SharedRAbundVector::getNumBins(){
339         return numBins;
340 }
341
342 /***********************************************************************/
343
344 int SharedRAbundVector::getNumSeqs(){
345         return numSeqs;
346 }
347
348 /***********************************************************************/
349
350 int SharedRAbundVector::getMaxRank(){
351         return maxRank;
352 }
353 /***********************************************************************/
354
355 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
356         return *this;                   
357 }
358 /***********************************************************************/
359 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
360         try {
361                 SharedUtil* util;
362                 util = new SharedUtil();
363                 
364                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
365
366                 for (int i = 0; i < lookup.size(); i++) {
367                         //if this sharedrabund is not from a group the user wants then delete it.
368                         if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) { 
369                                 delete lookup[i]; lookup[i] = NULL;
370                                 lookup.erase(lookup.begin()+i); 
371                                 i--; 
372                         }
373                 }
374                 
375                 delete util;
376         
377                 return lookup;
378         }
379         catch(exception& e) {
380                 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
381                 exit(1);
382         }
383 }
384 /***********************************************************************/
385
386 RAbundVector SharedRAbundVector::getRAbundVector() {
387         try {
388                 RAbundVector rav;
389                 
390                 for (int i = 0; i < data.size(); i++) {
391                         if(data[i].abundance != 0) {
392                                 rav.push_back(data[i].abundance);
393                         }
394                 }
395                 
396                 rav.setLabel(label);
397                 return rav;
398         }
399         catch(exception& e) {
400                 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
401                 exit(1);
402         }
403 }
404 /***********************************************************************/
405
406 RAbundVector SharedRAbundVector::getRAbundVector2() {
407         try {
408                 RAbundVector rav;
409                 for(int i = 0; i < numBins; i++)
410                         if(data[i].abundance != 0)
411                                 rav.push_back(data[i].abundance-1);
412                 return rav;
413         }
414         catch(exception& e) {
415                 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
416                 exit(1);
417         }
418 }
419 /***********************************************************************/
420
421 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
422         try {
423                 SharedSAbundVector sav(maxRank+1);
424                 
425                 for(int i=0;i<data.size();i++){
426                         int abund = data[i].abundance;
427                         sav.set(abund, sav.getAbundance(abund) + 1, group);
428                 }
429                 
430                 sav.set(0, 0, group);
431                 sav.setLabel(label);
432                 sav.setGroup(group);
433                 
434                 return sav;
435         }
436         catch(exception& e) {
437                 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
438                 exit(1);
439         }
440 }
441 /***********************************************************************/
442
443 SAbundVector SharedRAbundVector::getSAbundVector() {
444         try {
445                 SAbundVector sav(maxRank+1);
446                 
447                 for(int i=0;i<data.size();i++){
448                         int abund = data[i].abundance;
449                         sav.set(abund, sav.get(abund) + 1);
450                 }
451                 sav.set(0, 0);
452                 sav.setLabel(label);
453                 return sav;
454         }
455         catch(exception& e) {
456                 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");                
457                 exit(1);
458         }
459 }
460
461 /***********************************************************************/
462
463 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
464         try {
465                 SharedOrderVector ov;
466         
467                 for(int i=0;i<data.size();i++){
468                         for(int j=0;j<data[i].abundance;j++){
469                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
470                         }
471                 }
472                 random_shuffle(ov.begin(), ov.end());
473
474                 ov.setLabel(label);     
475                 ov.updateStats();
476                 
477                 return ov;
478         }
479         catch(exception& e) {
480                 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
481                 exit(1);
482         }
483 }
484 /***********************************************************************/
485
486 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
487         try {
488                 OrderVector ov;
489         
490                 for(int i=0;i<data.size();i++){
491                         for(int j=0;j<data[i].abundance;j++){
492                                 ov.push_back(i);
493                         }
494                 }
495                 random_shuffle(ov.begin(), ov.end());
496
497                 ov.setLabel(label);     
498                 return ov;
499         }
500         catch(exception& e) {
501                 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
502                 exit(1);
503         }
504 }
505
506 /***********************************************************************/
507