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