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