]> git.donarmstrong.com Git - mothur.git/blob - ordervector.cpp
created mothurOut class to handle logfiles
[mothur.git] / ordervector.cpp
1 /*
2  *  order.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 "ordervector.hpp"
11
12
13 /***********************************************************************/
14
15 OrderVector::OrderVector() : DataVector() {}
16
17 /***********************************************************************/
18
19 //OrderVector::OrderVector(int ns) : DataVector(), data(ns, -1) {}
20
21 /***********************************************************************/
22
23 OrderVector::OrderVector(string id, vector<int> ov) : 
24                                                                                         DataVector(id), data(ov)
25 {
26         updateStats();  
27 }
28
29 /***********************************************************************/
30
31 OrderVector::OrderVector(ifstream& f) : DataVector() {
32         try {
33                 int hold;
34         
35                 f >> label;
36                 f >> hold;
37         
38                 data.assign(hold, -1);
39         
40                 int inputData;
41         
42                 for(int i=0;i<hold;i++){
43                         f >> inputData;
44                         set(i, inputData);
45                 }
46         
47                 updateStats();
48         }
49         catch(exception& e) {
50                 m->errorOut(e, "OrderVector", "OrderVector");
51                 exit(1);
52         }
53 }
54
55 /***********************************************************************/
56
57
58 int OrderVector::getNumBins(){
59         if(needToUpdate == 1){  updateStats();  }
60         return numBins;
61 }
62
63 /***********************************************************************/
64
65 int OrderVector::getNumSeqs(){
66         if(needToUpdate == 1){  updateStats();  }
67         return numSeqs;
68 }
69
70 /***********************************************************************/
71
72 int OrderVector::getMaxRank(){
73         if(needToUpdate == 1){  updateStats();  }
74         return maxRank;
75 }
76
77 /***********************************************************************/
78
79
80
81 void OrderVector::set(int index, int binNumber){
82         
83         data[index] = binNumber;
84         needToUpdate = 1;
85         
86 }
87
88 /***********************************************************************/
89
90 int OrderVector::get(int index){
91         return data[index];                     
92 }
93
94 /***********************************************************************/
95
96 void OrderVector::push_back(int index){
97
98         data.push_back(index);
99         needToUpdate = 1;
100         
101 }
102
103 /***********************************************************************/
104
105 void OrderVector::print(ostream& output){
106         try {
107                 output << label << '\t' << numSeqs << '\t';
108         
109                 for(int i=0;i<data.size();i++){
110                         output << data[i] << '\t';
111                 }
112                 output << endl;
113         }
114         catch(exception& e) {
115                 m->errorOut(e, "OrderVector", "print");
116                 exit(1);
117         }
118 }
119
120 /***********************************************************************/
121
122 void OrderVector::print(string prefix, ostream& output){
123         try {
124                 output << prefix << '\t' << numSeqs << '\t';
125         
126                 for(int i=0;i<numSeqs;i++){
127                         output << data[i] << '\t';
128                 }
129                 output << endl;
130         }
131         catch(exception& e) {
132                 m->errorOut(e, "OrderVector", "print");
133                 exit(1);
134         }
135 }
136
137 /***********************************************************************/
138
139 void OrderVector::resize(int){
140         m->mothurOut("resize() did nothing in class OrderVector");
141 }
142
143 /***********************************************************************/
144
145 int OrderVector::size(){
146         return data.size();                                     
147 }
148
149 /***********************************************************************/
150
151 vector<int>::iterator OrderVector::begin(){
152         return data.begin();    
153 }
154
155 /***********************************************************************/
156
157 vector<int>::iterator OrderVector::end(){
158         return data.end();              
159 }
160
161 /***********************************************************************/
162
163 RAbundVector OrderVector::getRAbundVector(){
164         try {
165                 RAbundVector rav(data.size());
166         
167                 for(int i=0;i<numSeqs;i++){
168                         rav.set(data[i], rav.get(data[i]) + 1);
169                 }       
170                 sort(rav.rbegin(), rav.rend());
171                 for(int i=numSeqs-1;i>=0;i--){
172                         if(rav.get(i) == 0){    rav.pop_back(); }
173                         else{
174                                 break;
175                         }
176                 }
177                 rav.setLabel(label);
178
179                 return rav;
180         }
181         catch(exception& e) {
182                 m->errorOut(e, "OrderVector", "getRAbundVector");
183                 exit(1);
184         }
185 }
186
187 /***********************************************************************/
188
189 SAbundVector OrderVector::getSAbundVector(){
190         
191         RAbundVector rav(this->getRAbundVector());
192         return rav.getSAbundVector();
193
194 }
195
196 /***********************************************************************/
197
198 OrderVector OrderVector::getOrderVector(map<string,int>* hold = 0){
199         return *this;                   
200 }
201
202 /***********************************************************************/
203
204 void OrderVector::updateStats(){
205         try {
206                 needToUpdate = 0;
207         //      int maxBinVectorLength = 0;
208                 numSeqs = 0;
209                 numBins = 0;
210                 maxRank = 0;
211         
212                 for(int i=0;i<data.size();i++){
213                         if(data[i] != -1){
214                                 numSeqs++;
215                         }
216                 }
217         
218                 vector<int> hold(numSeqs);
219         
220                 for(int i=0;i<numSeqs;i++){
221                         if(data[i] != -1){
222                                 hold[data[i]] = hold[data[i]]+1;
223                         }
224                 }       
225                 for(int i=0;i<numSeqs;i++){
226                         if(hold[i] > 0)                 {       numBins++;                      }
227                         if(hold[i] > maxRank)   {       maxRank = hold[i];      }
228                 }
229         }
230         catch(exception& e) {
231                 m->errorOut(e, "OrderVector", "updateStats");
232                 exit(1);
233         }
234 }
235
236 /***********************************************************************/
237