]> git.donarmstrong.com Git - mothur.git/blob - rabundvector.cpp
added shannonrange calc.
[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                 m->errorOut(e, "RAbundVector", "RAbundVector");
45                 exit(1);
46         }
47 }
48
49 /***********************************************************************/
50
51 RAbundVector::RAbundVector(vector<int> rav, int mr, int nb, int ns) {
52         try {
53                 numBins = nb;
54                 maxRank = mr;
55                 numSeqs = ns;
56                 data = rav;
57         }
58         catch(exception& e) {
59                 m->errorOut(e, "RAbundVector", "RAbundVector");
60                 exit(1);
61         }
62 }
63
64 /***********************************************************************/
65
66
67 RAbundVector::RAbundVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
68         try {
69                 int hold;
70                 f >> label >> hold;
71         
72                 data.assign(hold, 0);
73                 int inputData;
74         
75                 for(int i=0;i<hold;i++){
76                         f >> inputData;
77                         set(i, inputData);
78                 }
79         
80         }
81         catch(exception& e) {
82                 m->errorOut(e, "RAbundVector", "RAbundVector");
83                 exit(1);
84         }
85 }
86
87 /***********************************************************************/
88
89 RAbundVector::~RAbundVector() {
90
91 }
92
93 /***********************************************************************/
94
95 void RAbundVector::set(int binNumber, int newBinSize){
96         try {
97                 int oldBinSize = data[binNumber];
98                 data[binNumber] = newBinSize;
99         
100                 if(oldBinSize == 0)                     {       numBins++;                              }
101                 if(newBinSize == 0)                     {       numBins--;                              }
102                 if(newBinSize > maxRank)        {       maxRank = newBinSize;   }
103         
104                 numSeqs += (newBinSize - oldBinSize);
105         }
106         catch(exception& e) {
107                 m->errorOut(e, "RAbundVector", "set");
108                 exit(1);
109         }
110 }
111
112 /***********************************************************************/
113
114 int RAbundVector::get(int index){
115         return data[index];
116         
117 }
118 /***********************************************************************/
119
120 void RAbundVector::clear(){
121         numBins = 0;
122         maxRank = 0;
123         numSeqs = 0;
124         data.clear();
125         
126 }
127 /***********************************************************************/
128
129 void RAbundVector::push_back(int binSize){
130         try {
131                 data.push_back(binSize);
132                 numBins++;
133         
134                 if(binSize > maxRank){
135                         maxRank = binSize;
136                 }
137         
138                 numSeqs += binSize;
139         }
140         catch(exception& e) {
141                 m->errorOut(e, "RAbundVector", "push_back");
142                 exit(1);
143         }
144 }
145
146 /***********************************************************************/
147
148 void RAbundVector::pop_back(){
149
150         return data.pop_back();
151 }
152
153 /***********************************************************************/
154
155 void RAbundVector::resize(int size){
156         
157         data.resize(size);
158 }
159
160 /***********************************************************************/
161
162 int RAbundVector::size(){
163         return data.size();
164 }
165
166 /***********************************************************************/
167
168 void RAbundVector::quicksort(){
169         sort(data.rbegin(), data.rend());
170 }
171
172 /***********************************************************************/
173
174 int RAbundVector::sum(){
175         VecCalc vecCalc;
176         return vecCalc.sumElements(data);
177 }
178
179 /***********************************************************************/
180
181 int RAbundVector::sum(int index){
182         VecCalc vecCalc;
183         return vecCalc.sumElements(data, index);
184 }
185
186 /***********************************************************************/
187
188 int RAbundVector::numNZ(){
189         VecCalc vecCalc;
190         return vecCalc.numNZ(data);
191 }
192
193 /***********************************************************************/
194
195 vector<int>::reverse_iterator RAbundVector::rbegin(){
196         return data.rbegin();                           
197 }
198
199 /***********************************************************************/
200
201 vector<int>::reverse_iterator RAbundVector::rend(){
202         return data.rend();                                     
203 }
204
205 /***********************************************************************/
206 void RAbundVector::nonSortedPrint(ostream& output){
207         try {   
208                 output << label << '\t' << numBins << '\t';
209         
210                 for(int i=0;i<numBins;i++){             output << data[i] << '\t';              }
211                 output << endl;
212         }
213         catch(exception& e) {
214                 m->errorOut(e, "RAbundVector", "nonSortedPrint");
215                 exit(1);
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                 m->errorOut(e, "RAbundVector", "print");
231                 exit(1);
232         }
233 }
234
235
236 /***********************************************************************/
237 void RAbundVector::print(ostream& output){
238         try {
239                 output << label << '\t' << numBins << '\t';
240         
241                 vector<int> hold = data;
242                 sort(hold.rbegin(), hold.rend());
243                 
244                 for(int i=0;i<numBins;i++){             output << hold[i] << '\t';              }
245                 output << endl;
246         }
247         catch(exception& e) {
248                 m->errorOut(e, "RAbundVector", "print");
249                 exit(1);
250         }
251 }
252
253 /***********************************************************************/
254 int RAbundVector::getNumBins(){
255         return numBins;
256 }
257
258 /***********************************************************************/
259
260 int RAbundVector::getNumSeqs(){
261         return numSeqs;
262 }
263
264 /***********************************************************************/
265
266 int RAbundVector::getMaxRank(){
267         return maxRank;
268 }
269
270 /***********************************************************************/
271
272 RAbundVector RAbundVector::getRAbundVector(){
273         return *this;                   
274 }
275
276 /***********************************************************************/
277
278 SAbundVector RAbundVector::getSAbundVector() {
279         try {
280                 SAbundVector sav(maxRank+1);
281                 
282                 for(int i=0;i<data.size();i++){
283                         int abund = data[i];
284                         sav.set(abund, sav.get(abund) + 1);
285                 }
286                 sav.set(0, 0);
287                 sav.setLabel(label);
288                 return sav;
289         }
290         catch(exception& e) {
291                 m->errorOut(e, "RAbundVector", "getSAbundVector");
292                 exit(1);
293         }
294 }
295
296 /***********************************************************************/
297
298 OrderVector RAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
299         try {
300                 OrderVector ov;
301
302                 for(int i=0;i<data.size();i++){
303                         for(int j=0;j<data[i];j++){
304                                 ov.push_back(i);
305                         }
306                 }
307                 random_shuffle(ov.begin(), ov.end());
308                 ov.setLabel(label);     
309                 ov.getNumBins();
310
311                 return ov;
312         }
313         catch(exception& e) {
314                 m->errorOut(e, "RAbundVector", "getOrderVector");
315                 exit(1);
316         }
317 }
318
319 /***********************************************************************/