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