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