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