]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
started shared utilities, updates to venn and heatmap added tree.groups 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
17
18 /***********************************************************************/
19
20 SharedRAbundVector::SharedRAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {};
21
22 /***********************************************************************/
23
24 SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
25                 individual newGuy;
26                 //initialize data
27                 for (int i=0; i< n; i++) {
28                         newGuy.bin = i;
29                         newGuy.abundance = 0;
30                         data.push_back(newGuy);
31                 }
32 };
33
34 /***********************************************************************
35
36 SharedRAbundVector::SharedRAbundVector(string id, vector<individual> rav) : DataVector(id), data(rav) {
37         try {
38                 numBins = 0;
39                 maxRank = 0;
40                 numSeqs = 0;
41                 
42                 for(int i=0;i<data.size();i++){
43                         if(data[i].abundance != 0)              {       numBins = i+1;          }
44                         if(data[i].abundance > maxRank) {       maxRank = data[i].abundance;    }
45                         numSeqs += data[i].abundance;
46                 }
47         }
48         catch(exception& e) {
49                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
50                 exit(1);
51         }
52         catch(...) {
53                 cout << "An unknown error has occurred in the SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
54                 exit(1);
55         }
56 }
57
58
59 /***********************************************************************
60
61
62 SharedRAbundVector::SharedRAbundVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
63         try {
64                 int i, num;
65                 string holdLabel, group
66                 individual newGuy;
67                 
68                 f >> label >> group >> num;
69                 
70                 //initialize data
71                 for (i=0; i<hold; i++) {
72                         newGuy = new individual;
73                         newGuy.abundance = 0;
74                         newGuy.bin = i;
75                         data.push_back(newGuy);
76                 }
77                 int inputData;
78         
79                 for(int i=0;i<hold;i++){
80                         f >> inputData;
81                         set(i, inputData);
82                 }
83         }
84         catch(exception& e) {
85                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
86                 exit(1);
87         }
88         catch(...) {
89                 cout << "An unknown error has occurred in the SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
90                 exit(1);
91         }
92 }
93
94 /***********************************************************************/
95
96 SharedRAbundVector::~SharedRAbundVector() {
97
98 }
99
100 /***********************************************************************/
101
102 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
103         try {
104                 int oldBinSize = data[binNumber].abundance;
105                 data[binNumber].abundance = newBinSize;
106                 data[binNumber].group = groupname;
107         
108                 if(newBinSize > maxRank)        {       maxRank = newBinSize;   }
109         
110                 numSeqs += (newBinSize - oldBinSize);
111         }
112         catch(exception& e) {
113                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
114                 exit(1);
115         }
116         catch(...) {
117                 cout << "An unknown error has occurred in the SharedRAbundVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
118                 exit(1);
119         }
120 }
121 /***********************************************************************/
122
123 void SharedRAbundVector::setData(vector <individual> newData){
124         data = newData;
125 }
126
127 /***********************************************************************/
128
129 int SharedRAbundVector::getAbundance(int index){
130         return data[index].abundance;
131         
132 }
133 /***********************************************************************/
134
135 int SharedRAbundVector::numNZ(){
136         int sum = 0;
137         for(int i = 1; i < numBins; i++)
138                 if(data[i].abundance > 0)
139                         sum++;
140         return sum;
141 }
142 /***********************************************************************/
143
144 void SharedRAbundVector::sortD(){
145         struct individual indObj;
146         sort(data.begin()+1, data.end(), indObj);
147 }
148 /***********************************************************************/
149
150 individual SharedRAbundVector::get(int index){
151         return data[index];
152         
153 }
154 /***********************************************************************/
155
156 vector <individual> SharedRAbundVector::getData(){
157         return data;
158 }
159 /***********************************************************************/
160
161 void SharedRAbundVector::push_back(int binSize, int otu, string groupName){
162         try {
163                 individual newGuy;
164                 newGuy.abundance = binSize;
165                 newGuy.group = groupName;
166                 newGuy.bin = otu;
167                 
168                 data.push_back(newGuy);
169                 numBins++;
170         
171                 if(binSize > maxRank){
172                         maxRank = binSize;
173                 }
174         
175                 numSeqs += binSize;
176         }
177         catch(exception& e) {
178                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
179                 exit(1);
180         }
181         catch(...) {
182                 cout << "An unknown error has occurred in the SharedRAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
183                 exit(1);
184         }
185 }
186
187 /***********************************************************************/
188
189 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
190         try {
191                 individual newGuy;
192                 newGuy.abundance = binSize;
193                 newGuy.group = groupName;
194                 newGuy.bin = otu;
195                 
196                 data.insert(data.begin()+otu, newGuy);
197                 numBins++;
198         
199                 if(binSize > maxRank){
200                         maxRank = binSize;
201                 }
202         
203                 numSeqs += binSize;
204         }
205         catch(exception& e) {
206                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
207                 exit(1);
208         }
209         catch(...) {
210                 cout << "An unknown error has occurred in the SharedRAbundVector class function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
211                 exit(1);
212         }
213 }
214
215 /***********************************************************************/
216
217 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
218         try {
219                 individual newGuy;
220                 newGuy.abundance = binSize;
221                 newGuy.group = groupName;
222                 newGuy.bin = otu;
223                 
224                 data.insert(data.begin(), newGuy);
225                 numBins++;
226         
227                 if(binSize > maxRank){
228                         maxRank = binSize;
229                 }
230         
231                 numSeqs += binSize;
232         }
233         catch(exception& e) {
234                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
235                 exit(1);
236         }
237         catch(...) {
238                 cout << "An unknown error has occurred in the SharedRAbundVector class function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
239                 exit(1);
240         }
241 }
242
243 /***********************************************************************/
244 void SharedRAbundVector::pop_back(){
245
246         return data.pop_back();
247 }
248
249 /***********************************************************************/
250
251
252 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
253         return data.rbegin();                           
254 }
255
256 /***********************************************************************/
257
258 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
259         return data.rend();                                     
260 }
261
262 /***********************************************************************/
263 void SharedRAbundVector::resize(int size){
264         
265         data.resize(size);
266 }
267
268 /***********************************************************************/
269
270 int SharedRAbundVector::size(){
271         return data.size();
272 }
273
274 /***********************************************************************/
275 void SharedRAbundVector::print(ostream& output){
276         try {
277                 output << numBins << '\t';
278         
279                 for(int i=0;i<numBins;i++){             output << data[i].abundance << '\t';            }
280                 output << endl;
281         }
282         catch(exception& e) {
283                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
284                 exit(1);
285         }
286         catch(...) {
287                 cout << "An unknown error has occurred in the SharedRAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
288                 exit(1);
289         }
290 }
291 /***********************************************************************/
292 string SharedRAbundVector::getGroup(){
293         return group;
294 }
295
296 /***********************************************************************/
297
298 void SharedRAbundVector::setGroup(string groupName){
299         group = groupName;
300 }
301 /***********************************************************************/
302 int SharedRAbundVector::getGroupIndex()  { return index; }
303 /***********************************************************************/
304 void SharedRAbundVector::setGroupIndex(int vIndex)      { index = vIndex; }
305 /***********************************************************************/
306 int SharedRAbundVector::getNumBins(){
307         return numBins;
308 }
309
310 /***********************************************************************/
311
312 int SharedRAbundVector::getNumSeqs(){
313         return numSeqs;
314 }
315
316 /***********************************************************************/
317
318 int SharedRAbundVector::getMaxRank(){
319         return maxRank;
320 }
321 /***********************************************************************/
322
323 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
324         return *this;                   
325 }
326 /***********************************************************************/
327
328 RAbundVector SharedRAbundVector::getRAbundVector() {
329         try {
330                 RAbundVector rav(data.size());
331                 
332                 for (int i = 0; i < data.size(); i++) {
333                         rav.set(i, data[i].abundance);
334                 }
335         
336                 return rav;
337         }
338         catch(exception& e) {
339                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
340                 exit(1);
341         }
342         catch(...) {
343                 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
344                 exit(1);
345         }
346 }
347 /***********************************************************************/
348
349 RAbundVector SharedRAbundVector::getRAbundVector2() {
350         try {
351                 RAbundVector rav;
352                 for(int i = 0; i < numBins; i++)
353                         if(data[i].abundance != 0)
354                                 rav.push_back(data[i].abundance-1);
355                 return rav;
356         }
357         catch(exception& e) {
358                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
359                 exit(1);
360         }
361         catch(...) {
362                 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
363                 exit(1);
364         }
365 }
366 /***********************************************************************/
367
368 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
369         try {
370                 SharedSAbundVector sav(maxRank+1);
371                 
372                 for(int i=0;i<data.size();i++){
373                         int abund = data[i].abundance;
374                         sav.set(abund, sav.getAbundance(abund) + 1, group);
375                 }
376                 
377                 sav.set(0, 0, group);
378                 sav.setLabel(label);
379                 sav.setGroup(group);
380                 
381                 return sav;
382         }
383         catch(exception& e) {
384                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
385                 exit(1);
386         }
387         catch(...) {
388                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
389                 exit(1);
390         }
391 }
392 /***********************************************************************/
393
394 SAbundVector SharedRAbundVector::getSAbundVector() {
395         try {
396                 SAbundVector sav(maxRank+1);
397                 
398                 for(int i=0;i<data.size();i++){
399                         int abund = data[i].abundance;
400                         sav.set(abund, sav.get(abund) + 1);
401                 }
402                 sav.set(0, 0);
403                 sav.setLabel(label);
404                 return sav;
405         }
406         catch(exception& e) {
407                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSAbundVector. 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 getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
412                 exit(1);
413         }
414 }
415
416 /***********************************************************************/
417
418 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
419         try {
420                 SharedOrderVector ov;
421         
422                 for(int i=0;i<data.size();i++){
423                         for(int j=0;j<data[i].abundance;j++){
424                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
425                         }
426                 }
427                 random_shuffle(ov.begin(), ov.end());
428
429                 ov.setLabel(label);     
430                 return ov;
431         }
432         catch(exception& e) {
433                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
434                 exit(1);
435         }
436         catch(...) {
437                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
438                 exit(1);
439         }
440 }
441 /***********************************************************************/
442
443 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
444         try {
445                 OrderVector ov;
446         
447                 for(int i=0;i<data.size();i++){
448                         for(int j=0;j<data[i].abundance;j++){
449                                 ov.push_back(i);
450                         }
451                 }
452                 random_shuffle(ov.begin(), ov.end());
453
454                 ov.setLabel(label);     
455                 return ov;
456         }
457         catch(exception& e) {
458                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
459                 exit(1);
460         }
461         catch(...) {
462                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
463                 exit(1);
464         }
465 }
466
467 /***********************************************************************/
468