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