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