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