]> git.donarmstrong.com Git - mothur.git/blob - rabundvector.cpp
created mothurOut class to handle logfiles
[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
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                 m->errorOut(e, "RAbundVector", "push_back");
133                 exit(1);
134         }
135 }
136
137 /***********************************************************************/
138
139 void RAbundVector::pop_back(){
140
141         return data.pop_back();
142 }
143
144 /***********************************************************************/
145
146 void RAbundVector::resize(int size){
147         
148         data.resize(size);
149 }
150
151 /***********************************************************************/
152
153 int RAbundVector::size(){
154         return data.size();
155 }
156
157 /***********************************************************************/
158
159 void RAbundVector::quicksort(){
160         sort(data.rbegin(), data.rend());
161 }
162
163 /***********************************************************************/
164
165 int RAbundVector::sum(){
166         VecCalc vecCalc;
167         return vecCalc.sumElements(data);
168 }
169
170 /***********************************************************************/
171
172 int RAbundVector::sum(int index){
173         VecCalc vecCalc;
174         return vecCalc.sumElements(data, index);
175 }
176
177 /***********************************************************************/
178
179 int RAbundVector::numNZ(){
180         VecCalc vecCalc;
181         return vecCalc.numNZ(data);
182 }
183
184 /***********************************************************************/
185
186 vector<int>::reverse_iterator RAbundVector::rbegin(){
187         return data.rbegin();                           
188 }
189
190 /***********************************************************************/
191
192 vector<int>::reverse_iterator RAbundVector::rend(){
193         return data.rend();                                     
194 }
195
196 /***********************************************************************/
197 void RAbundVector::nonSortedPrint(ostream& output){
198         try {   
199                 output << label << '\t' << numBins << '\t';
200         
201                 for(int i=0;i<numBins;i++){             output << data[i] << '\t';              }
202                 output << endl;
203         }
204         catch(exception& e) {
205                 m->errorOut(e, "RAbundVector", "nonSortedPrint");
206                 exit(1);
207         }
208 }
209 /***********************************************************************/
210 void RAbundVector::print(string prefix, ostream& output){
211         try {   
212                 output << prefix << '\t' << numBins << '\t';
213         
214                 vector<int> hold = data;
215                 sort(hold.rbegin(), hold.rend());
216         
217                 for(int i=0;i<numBins;i++){             output << hold[i] << '\t';              }
218                 output << endl;
219         }
220         catch(exception& e) {
221                 m->errorOut(e, "RAbundVector", "print");
222                 exit(1);
223         }
224 }
225
226
227 /***********************************************************************/
228 void RAbundVector::print(ostream& output){
229         try {
230                 output << label << '\t' << numBins << '\t';
231         
232                 vector<int> hold = data;
233                 sort(hold.rbegin(), hold.rend());
234                 
235                 for(int i=0;i<numBins;i++){             output << hold[i] << '\t';              }
236                 output << endl;
237         }
238         catch(exception& e) {
239                 m->errorOut(e, "RAbundVector", "print");
240                 exit(1);
241         }
242 }
243
244 /***********************************************************************/
245 int RAbundVector::getNumBins(){
246         return numBins;
247 }
248
249 /***********************************************************************/
250
251 int RAbundVector::getNumSeqs(){
252         return numSeqs;
253 }
254
255 /***********************************************************************/
256
257 int RAbundVector::getMaxRank(){
258         return maxRank;
259 }
260
261 /***********************************************************************/
262
263 RAbundVector RAbundVector::getRAbundVector(){
264         return *this;                   
265 }
266
267 /***********************************************************************/
268
269 SAbundVector RAbundVector::getSAbundVector() {
270         try {
271                 SAbundVector sav(maxRank+1);
272                 
273                 for(int i=0;i<data.size();i++){
274                         int abund = data[i];
275                         sav.set(abund, sav.get(abund) + 1);
276                 }
277                 sav.set(0, 0);
278                 sav.setLabel(label);
279                 return sav;
280         }
281         catch(exception& e) {
282                 m->errorOut(e, "RAbundVector", "getSAbundVector");
283                 exit(1);
284         }
285 }
286
287 /***********************************************************************/
288
289 OrderVector RAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
290         try {
291                 OrderVector ov;
292
293                 for(int i=0;i<data.size();i++){
294                         for(int j=0;j<data[i];j++){
295                                 ov.push_back(i);
296                         }
297                 }
298                 random_shuffle(ov.begin(), ov.end());
299                 ov.setLabel(label);     
300                 ov.getNumBins();
301
302                 return ov;
303         }
304         catch(exception& e) {
305                 m->errorOut(e, "RAbundVector", "getOrderVector");
306                 exit(1);
307         }
308 }
309
310 /***********************************************************************/