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