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