]> git.donarmstrong.com Git - mothur.git/blob - rabundvector.cpp
added logfile feature
[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                 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                 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                 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                 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                 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::print(string prefix, ostream& output){
198         try {   
199                 output << prefix << '\t' << numBins << '\t';
200         
201                 vector<int> hold = data;
202                 sort(hold.rbegin(), hold.rend());
203         
204                 for(int i=0;i<numBins;i++){             output << hold[i] << '\t';              }
205                 output << endl;
206         }
207         catch(exception& e) {
208                 errorOut(e, "RAbundVector", "print");
209                 exit(1);
210         }
211 }
212
213 /***********************************************************************/
214 void RAbundVector::print(ostream& output){
215         try {
216                 output << label << '\t' << numBins << '\t';
217         
218                 vector<int> hold = data;
219                 sort(hold.rbegin(), hold.rend());
220                 
221                 for(int i=0;i<numBins;i++){             output << hold[i] << '\t';              }
222                 output << endl;
223         }
224         catch(exception& e) {
225                 errorOut(e, "RAbundVector", "print");
226                 exit(1);
227         }
228 }
229
230 /***********************************************************************/
231 int RAbundVector::getNumBins(){
232         return numBins;
233 }
234
235 /***********************************************************************/
236
237 int RAbundVector::getNumSeqs(){
238         return numSeqs;
239 }
240
241 /***********************************************************************/
242
243 int RAbundVector::getMaxRank(){
244         return maxRank;
245 }
246
247 /***********************************************************************/
248
249 RAbundVector RAbundVector::getRAbundVector(){
250         return *this;                   
251 }
252
253 /***********************************************************************/
254
255 SAbundVector RAbundVector::getSAbundVector() {
256         try {
257                 SAbundVector sav(maxRank+1);
258                 
259                 for(int i=0;i<data.size();i++){
260                         int abund = data[i];
261                         sav.set(abund, sav.get(abund) + 1);
262                 }
263                 sav.set(0, 0);
264                 sav.setLabel(label);
265                 return sav;
266         }
267         catch(exception& e) {
268                 errorOut(e, "RAbundVector", "getSAbundVector");
269                 exit(1);
270         }
271 }
272
273 /***********************************************************************/
274
275 OrderVector RAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
276         try {
277                 OrderVector ov;
278         
279                 for(int i=0;i<data.size();i++){
280                         for(int j=0;j<data[i];j++){
281                                 ov.push_back(i);
282                         }
283                 }
284                 random_shuffle(ov.begin(), ov.end());
285
286                 ov.setLabel(label);     
287                 return ov;
288         }
289         catch(exception& e) {
290                 errorOut(e, "RAbundVector", "getOrderVector");
291                 exit(1);
292         }
293 }
294
295 /***********************************************************************/