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