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