]> git.donarmstrong.com Git - mothur.git/blob - ordervector.cpp
fixed some bugs
[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                 cout << "Standard Error: " << e.what() << " has occurred in the OrderVector class Function OrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
51                 exit(1);
52         }
53         catch(...) {
54                 cout << "An unknown error has occurred in the OrderVector class function OrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
55                 exit(1);
56         }
57 }
58
59 /***********************************************************************/
60
61
62 int OrderVector::getNumBins(){
63         if(needToUpdate == 1){  updateStats();  }
64         return numBins;
65 }
66
67 /***********************************************************************/
68
69 int OrderVector::getNumSeqs(){
70         if(needToUpdate == 1){  updateStats();  }
71         return numSeqs;
72 }
73
74 /***********************************************************************/
75
76 int OrderVector::getMaxRank(){
77         if(needToUpdate == 1){  updateStats();  }
78         return maxRank;
79 }
80
81 /***********************************************************************/
82
83
84
85 void OrderVector::set(int index, int binNumber){
86         
87         data[index] = binNumber;
88         needToUpdate = 1;
89         
90 }
91
92 /***********************************************************************/
93
94 int OrderVector::get(int index){
95         return data[index];                     
96 }
97
98 /***********************************************************************/
99
100 void OrderVector::push_back(int index){
101
102         data.push_back(index);
103         needToUpdate = 1;
104         
105 }
106
107 /***********************************************************************/
108
109 void OrderVector::print(ostream& output){
110         try {
111                 output << label << '\t' << numSeqs << '\t';
112         
113                 for(int i=0;i<data.size();i++){
114                         output << data[i] << '\t';
115                 }
116                 output << endl;
117         }
118         catch(exception& e) {
119                 cout << "Standard Error: " << e.what() << " has occurred in the OrderVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
120                 exit(1);
121         }
122         catch(...) {
123                 cout << "An unknown error has occurred in the OrderVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
124                 exit(1);
125         }
126 }
127
128 /***********************************************************************/
129
130 void OrderVector::print(string prefix, ostream& output){
131         try {
132                 output << prefix << '\t' << numSeqs << '\t';
133         
134                 for(int i=0;i<numSeqs;i++){
135                         output << data[i] << '\t';
136                 }
137                 output << endl;
138         }
139         catch(exception& e) {
140                 cout << "Standard Error: " << e.what() << " has occurred in the OrderVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
141                 exit(1);
142         }
143         catch(...) {
144                 cout << "An unknown error has occurred in the OrderVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
145                 exit(1);
146         }       
147 }
148
149 /***********************************************************************/
150
151 void OrderVector::resize(int){
152         cout << "resize() did nothing in class OrderVector";
153 }
154
155 /***********************************************************************/
156
157 int OrderVector::size(){
158         return data.size();                                     
159 }
160
161 /***********************************************************************/
162
163 vector<int>::iterator OrderVector::begin(){
164         return data.begin();    
165 }
166
167 /***********************************************************************/
168
169 vector<int>::iterator OrderVector::end(){
170         return data.end();              
171 }
172
173 /***********************************************************************/
174
175 RAbundVector OrderVector::getRAbundVector(){
176         try {
177                 RAbundVector rav(data.size());
178         
179                 for(int i=0;i<numSeqs;i++){
180                         rav.set(data[i], rav.get(data[i]) + 1);
181                 }       
182                 sort(rav.rbegin(), rav.rend());
183                 for(int i=numSeqs-1;i>=0;i--){
184                         if(rav.get(i) == 0){    rav.pop_back(); }
185                         else{
186                                 break;
187                         }
188                 }
189                 rav.setLabel(label);
190
191                 return rav;
192         }
193         catch(exception& e) {
194                 cout << "Standard Error: " << e.what() << " has occurred in the OrderVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
195                 exit(1);
196         }
197         catch(...) {
198                 cout << "An unknown error has occurred in the OrderVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
199                 exit(1);
200         }       
201 }
202
203 /***********************************************************************/
204
205 SAbundVector OrderVector::getSAbundVector(){
206         
207         RAbundVector rav(this->getRAbundVector());
208         return rav.getSAbundVector();
209
210 }
211
212 /***********************************************************************/
213
214 OrderVector OrderVector::getOrderVector(map<string,int>* hold = 0){
215         return *this;                   
216 }
217
218 /***********************************************************************/
219
220 void OrderVector::updateStats(){
221         try {
222                 needToUpdate = 0;
223         //      int maxBinVectorLength = 0;
224                 numSeqs = 0;
225                 numBins = 0;
226                 maxRank = 0;
227         
228                 for(int i=0;i<data.size();i++){
229                         if(data[i] != -1){
230                                 numSeqs++;
231                         }
232                 }
233         
234                 vector<int> hold(numSeqs);
235         
236                 for(int i=0;i<numSeqs;i++){
237                         if(data[i] != -1){
238                                 hold[data[i]] = hold[data[i]]+1;
239                         }
240                 }       
241                 for(int i=0;i<numSeqs;i++){
242                         if(hold[i] > 0)                 {       numBins++;                      }
243                         if(hold[i] > maxRank)   {       maxRank = hold[i];      }
244                 }
245         }
246         catch(exception& e) {
247                 cout << "Standard Error: " << e.what() << " has occurred in the OrderVector class Function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
248                 exit(1);
249         }
250         catch(...) {
251                 cout << "An unknown error has occurred in the OrderVector class function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
252                 exit(1);
253         }       
254 }
255
256 /***********************************************************************/
257