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