]> git.donarmstrong.com Git - mothur.git/blob - rabundvector.cpp
started shared utilities, updates to venn and heatmap added tree.groups command
[mothur.git] / rabundvector.cpp
1 /*
2  *  rabundvector.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 8/8/08.
6  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
7  *
8  */
9 using namespace std;
10  
11 #include "rabundvector.hpp"
12 #include "sabundvector.hpp"
13 #include "ordervector.hpp"
14 #include "calculator.h"
15
16
17 /***********************************************************************/
18
19 RAbundVector::RAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {};
20
21 /***********************************************************************/
22
23 RAbundVector::RAbundVector(int n) : DataVector(), data(n,0) , maxRank(0), numBins(0), numSeqs(0) {};
24
25 /***********************************************************************/
26
27 //RAbundVector::RAbundVector(const RAbundVector& rav) : DataVector(rav), data(rav.data), (rav.label),  (rav.maxRank), (rav.numBins), (rav.numSeqs){};
28
29
30 /***********************************************************************/
31
32 RAbundVector::RAbundVector(string id, vector<int> rav) : DataVector(id), data(rav) {
33         try {
34                 numBins = 0;
35                 maxRank = 0;
36                 numSeqs = 0;
37                 
38                 for(int i=0;i<data.size();i++){
39                         if(data[i] != 0)                {       numBins = i+1;          }
40                         if(data[i] > maxRank)   {       maxRank = data[i];      }
41                         numSeqs += data[i];
42                 }
43         }
44         catch(exception& e) {
45                 cout << "Standard Error: " << e.what() << " has occurred in the RAbundVector class Function RAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
46                 exit(1);
47         }
48         catch(...) {
49                 cout << "An unknown error has occurred in the RAbundVector class function RAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
50                 exit(1);
51         }
52 }
53
54 /***********************************************************************/
55
56 RAbundVector::RAbundVector(vector<int> rav, int mr, int nb, int ns) {
57         try {
58                 numBins = nb;
59                 maxRank = mr;
60                 numSeqs = ns;
61                 data = rav;
62         }
63         catch(exception& e) {
64                 cout << "Standard Error: " << e.what() << " has occurred in the RAbundVector class Function RAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
65                 exit(1);
66         }
67         catch(...) {
68                 cout << "An unknown error has occurred in the RAbundVector class function RAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
69                 exit(1);
70         }
71 }
72
73
74
75 /***********************************************************************/
76
77
78 RAbundVector::RAbundVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
79         try {
80                 int hold;
81                 f >> label >> hold;
82         
83                 data.assign(hold, 0);
84                 int inputData;
85         
86                 for(int i=0;i<hold;i++){
87                         f >> inputData;
88                         set(i, inputData);
89                 }
90         }
91         catch(exception& e) {
92                 cout << "Standard Error: " << e.what() << " has occurred in the RAbundVector class Function RAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
93                 exit(1);
94         }
95         catch(...) {
96                 cout << "An unknown error has occurred in the RAbundVector class function RAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
97                 exit(1);
98         }
99 }
100
101 /***********************************************************************/
102
103 RAbundVector::~RAbundVector() {
104
105 }
106
107 /***********************************************************************/
108
109 void RAbundVector::set(int binNumber, int newBinSize){
110         try {
111                 int oldBinSize = data[binNumber];
112                 data[binNumber] = newBinSize;
113         
114                 if(oldBinSize == 0)                     {       numBins++;                              }
115                 if(newBinSize == 0)                     {       numBins--;                              }
116                 if(newBinSize > maxRank)        {       maxRank = newBinSize;   }
117         
118                 numSeqs += (newBinSize - oldBinSize);
119         }
120         catch(exception& e) {
121                 cout << "Standard Error: " << e.what() << " has occurred in the RAbundVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
122                 exit(1);
123         }
124         catch(...) {
125                 cout << "An unknown error has occurred in the RAbundVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
126                 exit(1);
127         }
128 }
129
130 /***********************************************************************/
131
132 int RAbundVector::get(int index){
133         return data[index];
134         
135 }
136
137 /***********************************************************************/
138
139 void RAbundVector::push_back(int binSize){
140         try {
141                 data.push_back(binSize);
142                 numBins++;
143         
144                 if(binSize > maxRank){
145                         maxRank = binSize;
146                 }
147         
148                 numSeqs += binSize;
149         }
150         catch(exception& e) {
151                 cout << "Standard Error: " << e.what() << " has occurred in the RAbundVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
152                 exit(1);
153         }
154         catch(...) {
155                 cout << "An unknown error has occurred in the RAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
156                 exit(1);
157         }
158 }
159
160 /***********************************************************************/
161
162 void RAbundVector::pop_back(){
163
164         return data.pop_back();
165 }
166
167 /***********************************************************************/
168
169 void RAbundVector::resize(int size){
170         
171         data.resize(size);
172 }
173
174 /***********************************************************************/
175
176 int RAbundVector::size(){
177         return data.size();
178 }
179
180 /***********************************************************************/
181
182 void RAbundVector::quicksort(){
183         sort(data.rbegin(), data.rend());
184 }
185
186 /***********************************************************************/
187
188 int RAbundVector::sum(){
189         VecCalc vecCalc;
190         return vecCalc.sumElements(data);
191 }
192
193 /***********************************************************************/
194
195 int RAbundVector::sum(int index){
196         VecCalc vecCalc;
197         return vecCalc.sumElements(data, index);
198 }
199
200 /***********************************************************************/
201
202 int RAbundVector::numNZ(){
203         VecCalc vecCalc;
204         return vecCalc.numNZ(data);
205 }
206
207 /***********************************************************************/
208
209 vector<int>::reverse_iterator RAbundVector::rbegin(){
210         return data.rbegin();                           
211 }
212
213 /***********************************************************************/
214
215 vector<int>::reverse_iterator RAbundVector::rend(){
216         return data.rend();                                     
217 }
218
219 /***********************************************************************/
220 void RAbundVector::print(string prefix, ostream& output){
221         try {   
222                 output << prefix << '\t' << numBins << '\t';
223         
224                 vector<int> hold = data;
225                 sort(hold.rbegin(), hold.rend());
226         
227                 for(int i=0;i<numBins;i++){             output << hold[i] << '\t';              }
228                 output << endl;
229         }
230         catch(exception& e) {
231                 cout << "Standard Error: " << e.what() << " has occurred in the RAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
232                 exit(1);
233         }
234         catch(...) {
235                 cout << "An unknown error has occurred in the RAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
236                 exit(1);
237         }
238 }
239
240 /***********************************************************************/
241 void RAbundVector::print(ostream& output){
242         try {
243                 output << label << '\t' << numBins << '\t';
244         
245                 vector<int> hold = data;
246                 sort(hold.rbegin(), hold.rend());
247                 
248                 for(int i=0;i<numBins;i++){             output << hold[i] << '\t';              }
249                 output << endl;
250         }
251         catch(exception& e) {
252                 cout << "Standard Error: " << e.what() << " has occurred in the RAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
253                 exit(1);
254         }
255         catch(...) {
256                 cout << "An unknown error has occurred in the RAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
257                 exit(1);
258         }
259 }
260
261 /***********************************************************************/
262 int RAbundVector::getNumBins(){
263         return numBins;
264 }
265
266 /***********************************************************************/
267
268 int RAbundVector::getNumSeqs(){
269         return numSeqs;
270 }
271
272 /***********************************************************************/
273
274 int RAbundVector::getMaxRank(){
275         return maxRank;
276 }
277
278 /***********************************************************************/
279
280 RAbundVector RAbundVector::getRAbundVector(){
281         return *this;                   
282 }
283
284 /***********************************************************************/
285
286 SAbundVector RAbundVector::getSAbundVector() {
287         try {
288                 SAbundVector sav(maxRank+1);
289                 
290                 for(int i=0;i<data.size();i++){
291                         int abund = data[i];
292                         sav.set(abund, sav.get(abund) + 1);
293                 }
294                 sav.set(0, 0);
295                 sav.setLabel(label);
296                 return sav;
297         }
298         catch(exception& e) {
299                 cout << "Standard Error: " << e.what() << " has occurred in the RAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
300                 exit(1);
301         }
302         catch(...) {
303                 cout << "An unknown error has occurred in the RAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
304                 exit(1);
305         }
306 }
307
308 /***********************************************************************/
309
310 OrderVector RAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
311         try {
312                 OrderVector ov;
313         
314                 for(int i=0;i<data.size();i++){
315                         for(int j=0;j<data[i];j++){
316                                 ov.push_back(i);
317                         }
318                 }
319                 random_shuffle(ov.begin(), ov.end());
320
321                 ov.setLabel(label);     
322                 return ov;
323         }
324         catch(exception& e) {
325                 cout << "Standard Error: " << e.what() << " has occurred in the RAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
326                 exit(1);
327         }
328         catch(...) {
329                 cout << "An unknown error has occurred in the RAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
330                 exit(1);
331         }
332 }
333
334 /***********************************************************************/