]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
changed defaults in allign.seqs command
[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
11 using namespace std;
12
13 #include "sharedrabundvector.h" 
14 #include "sabundvector.hpp"
15 #include "ordervector.hpp"
16 #include "sharedutilities.h"
17
18
19 /***********************************************************************/
20
21 SharedRAbundVector::SharedRAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {};
22
23 /***********************************************************************/
24
25 SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
26                 individual newGuy;
27                 //initialize data
28                 for (int i=0; i< n; i++) {
29                         newGuy.bin = i;
30                         newGuy.abundance = 0;
31                         data.push_back(newGuy);
32                 }
33 };
34
35 /***********************************************************************
36
37 SharedRAbundVector::SharedRAbundVector(string id, vector<individual> rav) : DataVector(id), data(rav) {
38         try {
39                 numBins = 0;
40                 maxRank = 0;
41                 numSeqs = 0;
42                 
43                 for(int i=0;i<data.size();i++){
44                         if(data[i].abundance != 0)              {       numBins = i+1;          }
45                         if(data[i].abundance > maxRank) {       maxRank = data[i].abundance;    }
46                         numSeqs += data[i].abundance;
47                 }
48         }
49         catch(exception& e) {
50                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
51                 exit(1);
52         }
53         catch(...) {
54                 cout << "An unknown error has occurred in the SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
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, pos, 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                 //save position in file in case next line is a new label.
105                 pos = f.tellg();
106                 
107                 if (f.eof() != true) { f >> nextLabel; }
108                 
109                 //read the rest of the groups info in
110                 while ((nextLabel == holdLabel) && (f.eof() != true)) {
111                         f >> groupN >> num;
112                         count++;
113                         
114                         if (globaldata->gGroupmap == NULL) { 
115                                 //save group in groupmap
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, i, groupN); //abundance, bin, group
130                         }
131                         
132                         //save position in file in case next line is a new label.
133                         pos = f.tellg();
134         
135                         if (f.eof() != true) { f >> nextLabel; }
136                 }
137                 
138                 //put file pointer back since you are now at a new distance label
139                 f.seekg(pos, ios::beg);
140         
141                 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
142                 
143         }
144         catch(exception& e) {
145                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
146                 exit(1);
147         }
148         catch(...) {
149                 cout << "An unknown error has occurred in the SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
150                 exit(1);
151         }
152 }
153
154 /***********************************************************************/
155
156 SharedRAbundVector::~SharedRAbundVector() {
157
158 }
159
160 /***********************************************************************/
161
162 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
163         try {
164                 int oldBinSize = data[binNumber].abundance;
165                 data[binNumber].abundance = newBinSize;
166                 data[binNumber].group = groupname;
167         
168                 if(newBinSize > maxRank)        {       maxRank = newBinSize;   }
169         
170                 numSeqs += (newBinSize - oldBinSize);
171         }
172         catch(exception& e) {
173                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
174                 exit(1);
175         }
176         catch(...) {
177                 cout << "An unknown error has occurred in the SharedRAbundVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
178                 exit(1);
179         }
180 }
181 /***********************************************************************/
182
183 void SharedRAbundVector::setData(vector <individual> newData){
184         data = newData;
185 }
186
187 /***********************************************************************/
188
189 int SharedRAbundVector::getAbundance(int index){
190         return data[index].abundance;
191         
192 }
193 /***********************************************************************/
194
195 int SharedRAbundVector::numNZ(){
196         int sum = 0;
197         for(int i = 1; i < numBins; i++)
198                 if(data[i].abundance > 0)
199                         sum++;
200         return sum;
201 }
202 /***********************************************************************/
203
204 void SharedRAbundVector::sortD(){
205         struct individual indObj;
206         sort(data.begin()+1, data.end(), indObj);
207 }
208 /***********************************************************************/
209
210 individual SharedRAbundVector::get(int index){
211         return data[index];
212         
213 }
214 /***********************************************************************/
215
216 vector <individual> SharedRAbundVector::getData(){
217         return data;
218 }
219 /***********************************************************************/
220
221 void SharedRAbundVector::push_back(int binSize, int otu, string groupName){
222         try {
223                 individual newGuy;
224                 newGuy.abundance = binSize;
225                 newGuy.group = groupName;
226                 newGuy.bin = otu;
227                 
228                 data.push_back(newGuy);
229                 numBins++;
230         
231                 if(binSize > maxRank){
232                         maxRank = binSize;
233                 }
234         
235                 numSeqs += binSize;
236         }
237         catch(exception& e) {
238                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
239                 exit(1);
240         }
241         catch(...) {
242                 cout << "An unknown error has occurred in the SharedRAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
243                 exit(1);
244         }
245 }
246
247 /***********************************************************************/
248
249 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
250         try {
251                 individual newGuy;
252                 newGuy.abundance = binSize;
253                 newGuy.group = groupName;
254                 newGuy.bin = otu;
255                 
256                 data.insert(data.begin()+otu, newGuy);
257                 numBins++;
258         
259                 if(binSize > maxRank){
260                         maxRank = binSize;
261                 }
262         
263                 numSeqs += binSize;
264         }
265         catch(exception& e) {
266                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
267                 exit(1);
268         }
269         catch(...) {
270                 cout << "An unknown error has occurred in the SharedRAbundVector class function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
271                 exit(1);
272         }
273 }
274
275 /***********************************************************************/
276
277 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
278         try {
279                 individual newGuy;
280                 newGuy.abundance = binSize;
281                 newGuy.group = groupName;
282                 newGuy.bin = otu;
283                 
284                 data.insert(data.begin(), newGuy);
285                 numBins++;
286         
287                 if(binSize > maxRank){
288                         maxRank = binSize;
289                 }
290         
291                 numSeqs += binSize;
292         }
293         catch(exception& e) {
294                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
295                 exit(1);
296         }
297         catch(...) {
298                 cout << "An unknown error has occurred in the SharedRAbundVector class function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
299                 exit(1);
300         }
301 }
302
303 /***********************************************************************/
304 void SharedRAbundVector::pop_back(){
305         numSeqs -= data[data.size()-1].abundance;
306         numBins--;
307         return data.pop_back();
308 }
309
310 /***********************************************************************/
311
312
313 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
314         return data.rbegin();                           
315 }
316
317 /***********************************************************************/
318
319 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
320         return data.rend();                                     
321 }
322
323 /***********************************************************************/
324 void SharedRAbundVector::resize(int size){
325         
326         data.resize(size);
327 }
328
329 /***********************************************************************/
330
331 int SharedRAbundVector::size(){
332         return data.size();
333 }
334
335 /***********************************************************************/
336 void SharedRAbundVector::print(ostream& output){
337         try {
338                 output << numBins << '\t';
339         
340                 for(int i=0;i<data.size();i++){         output << data[i].abundance << '\t';            }
341                 output << endl;
342         }
343         catch(exception& e) {
344                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
345                 exit(1);
346         }
347         catch(...) {
348                 cout << "An unknown error has occurred in the SharedRAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
349                 exit(1);
350         }
351 }
352 /***********************************************************************/
353 string SharedRAbundVector::getGroup(){
354         return group;
355 }
356
357 /***********************************************************************/
358
359 void SharedRAbundVector::setGroup(string groupName){
360         group = groupName;
361 }
362 /***********************************************************************/
363 int SharedRAbundVector::getGroupIndex()  { return index; }
364 /***********************************************************************/
365 void SharedRAbundVector::setGroupIndex(int vIndex)      { index = vIndex; }
366 /***********************************************************************/
367 int SharedRAbundVector::getNumBins(){
368         return numBins;
369 }
370
371 /***********************************************************************/
372
373 int SharedRAbundVector::getNumSeqs(){
374         return numSeqs;
375 }
376
377 /***********************************************************************/
378
379 int SharedRAbundVector::getMaxRank(){
380         return maxRank;
381 }
382 /***********************************************************************/
383
384 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
385         return *this;                   
386 }
387 /***********************************************************************/
388 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
389         try {
390                 SharedUtil* util;
391                 util = new SharedUtil();
392                 
393                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
394
395                 for (int i = 0; i < lookup.size(); i++) {
396                         //if this sharedrabund is not from a group the user wants then delete it.
397                         if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) { 
398                                 delete lookup[i]; 
399                                 lookup.erase(lookup.begin()+i); 
400                                 i--; 
401                         }
402                 }
403
404                 return lookup;
405         }
406         catch(exception& e) {
407                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
408                 exit(1);
409         }
410         catch(...) {
411                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
412                 exit(1);
413         }
414 }
415 /***********************************************************************/
416
417 RAbundVector SharedRAbundVector::getRAbundVector() {
418         try {
419                 RAbundVector rav(data.size());
420                 
421                 for (int i = 0; i < data.size(); i++) {
422                         rav.set(i, data[i].abundance);
423                 }
424         
425                 return rav;
426         }
427         catch(exception& e) {
428                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
429                 exit(1);
430         }
431         catch(...) {
432                 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
433                 exit(1);
434         }
435 }
436 /***********************************************************************/
437
438 RAbundVector SharedRAbundVector::getRAbundVector2() {
439         try {
440                 RAbundVector rav;
441                 for(int i = 0; i < numBins; i++)
442                         if(data[i].abundance != 0)
443                                 rav.push_back(data[i].abundance-1);
444                 return rav;
445         }
446         catch(exception& e) {
447                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
448                 exit(1);
449         }
450         catch(...) {
451                 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
452                 exit(1);
453         }
454 }
455 /***********************************************************************/
456
457 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
458         try {
459                 SharedSAbundVector sav(maxRank+1);
460                 
461                 for(int i=0;i<data.size();i++){
462                         int abund = data[i].abundance;
463                         sav.set(abund, sav.getAbundance(abund) + 1, group);
464                 }
465                 
466                 sav.set(0, 0, group);
467                 sav.setLabel(label);
468                 sav.setGroup(group);
469                 
470                 return sav;
471         }
472         catch(exception& e) {
473                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
474                 exit(1);
475         }
476         catch(...) {
477                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
478                 exit(1);
479         }
480 }
481 /***********************************************************************/
482
483 SAbundVector SharedRAbundVector::getSAbundVector() {
484         try {
485                 SAbundVector sav(maxRank+1);
486                 
487                 for(int i=0;i<data.size();i++){
488                         int abund = data[i].abundance;
489                         sav.set(abund, sav.get(abund) + 1);
490                 }
491                 sav.set(0, 0);
492                 sav.setLabel(label);
493                 return sav;
494         }
495         catch(exception& e) {
496                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
497                 exit(1);
498         }
499         catch(...) {
500                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
501                 exit(1);
502         }
503 }
504
505 /***********************************************************************/
506
507 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
508         try {
509                 SharedOrderVector ov;
510         
511                 for(int i=0;i<data.size();i++){
512                         for(int j=0;j<data[i].abundance;j++){
513                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
514                         }
515                 }
516                 random_shuffle(ov.begin(), ov.end());
517
518                 ov.setLabel(label);     
519                 ov.updateStats();
520                 
521                 return ov;
522         }
523         catch(exception& e) {
524                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
525                 exit(1);
526         }
527         catch(...) {
528                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
529                 exit(1);
530         }
531 }
532 /***********************************************************************/
533
534 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
535         try {
536                 OrderVector ov;
537         
538                 for(int i=0;i<data.size();i++){
539                         for(int j=0;j<data[i].abundance;j++){
540                                 ov.push_back(i);
541                         }
542                 }
543                 random_shuffle(ov.begin(), ov.end());
544
545                 ov.setLabel(label);     
546                 return ov;
547         }
548         catch(exception& e) {
549                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
550                 exit(1);
551         }
552         catch(...) {
553                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
554                 exit(1);
555         }
556 }
557
558 /***********************************************************************/
559