]> git.donarmstrong.com Git - mothur.git/blob - sharedsabundvector.cpp
fixed summary.shared bug and set jumble default to 1.
[mothur.git] / sharedsabundvector.cpp
1 /*
2  *  sharedSharedSAbundVector.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 12/10/08.
6  *  Copyright 2008 __MyCompanyName__. All rights reserved.
7  *
8  */
9
10 #include "sharedsabundvector.h"
11 #include "sabundvector.hpp"
12 #include "datavector.hpp"
13 #include "utilities.hpp"
14 #include <exception>
15
16 using namespace std;
17
18
19 /***********************************************************************/
20
21 SharedSAbundVector::SharedSAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){};
22
23 /***********************************************************************/
24
25 SharedSAbundVector::SharedSAbundVector(int size) :      DataVector(), maxRank(0), numBins(0), numSeqs(0) {
26                 individual newGuy;
27                 //initialize data
28                 for (int i=0; i< size; i++) {
29                         newGuy.bin = i;
30                         newGuy.abundance = 0;
31                         data.push_back(newGuy);
32                 }
33 };
34
35 /***********************************************************************/
36
37 void SharedSAbundVector::set(int bin, int abundance, string groupName){
38         try {
39
40                 int initSize = data[bin].abundance;
41                 data[bin].abundance = abundance;
42                 data[bin].group = groupName;
43         
44                 if(bin != 0){
45                         numBins += (abundance - initSize);
46                 }
47         
48                 numSeqs += bin * (abundance - initSize);
49         
50                 if(bin > maxRank)       {       maxRank = bin;          }
51         }
52         catch(exception& e) {
53                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function set. 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 SharedSAbundVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
58                 exit(1);
59         }       
60 }
61
62 /***********************************************************************/
63
64 individual SharedSAbundVector::get(int index){
65         return data[index];
66 }
67 /***********************************************************************/
68
69 int SharedSAbundVector::getAbundance(int index){
70         return data[index].abundance;
71 }
72
73 /***********************************************************************/
74
75 void SharedSAbundVector::push_back(int abundance, int bin, string groupName){
76         try {
77                 individual newGuy;
78                 newGuy.abundance = abundance;
79                 newGuy.bin = bin;
80                 newGuy.group = groupName;
81                 
82                 data.push_back(newGuy);
83         
84                 maxRank++;      
85         
86                 numBins += abundance;
87         
88                 numSeqs += (maxRank * abundance);
89         }
90         catch(exception& e) {
91                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
92                 exit(1);
93         }
94         catch(...) {
95                 cout << "An unknown error has occurred in the SharedSAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
96                 exit(1);
97         }               
98 }
99
100 /***********************************************************************/
101
102 void SharedSAbundVector::resize(int size){
103         data.resize(size);
104 }
105
106 /***********************************************************************/
107
108 int SharedSAbundVector::size(){
109         return data.size();             
110 }
111
112 /***********************************************************************/
113 void SharedSAbundVector::print(ostream& output){
114         try {
115                 output << label << '\t' << maxRank << '\t';
116         
117                 for(int i=1;i<=maxRank;i++){
118                         output << data[i].abundance << '\t';
119                 }
120                 output << endl;
121         }
122         catch(exception& e) {
123                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
124                 exit(1);
125         }
126         catch(...) {
127                 cout << "An unknown error has occurred in the SharedSAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
128                 exit(1);
129         }               
130 }
131 /***********************************************************************/
132 string SharedSAbundVector::getGroup(){
133         return group;
134 }
135
136 /***********************************************************************/
137
138 void SharedSAbundVector::setGroup(string groupName){
139         group = groupName;
140 }
141
142 /**********************************************************************/
143 int SharedSAbundVector::getNumBins(){
144         return numBins;
145 }
146
147 /***********************************************************************/
148
149 int SharedSAbundVector::getNumSeqs(){
150         return numSeqs;
151 }
152
153 /***********************************************************************/
154
155 int SharedSAbundVector::getMaxRank(){
156         return maxRank;
157 }
158 /***********************************************************************/
159 RAbundVector SharedSAbundVector::getRAbundVector(){
160         try {
161                 RAbundVector rav;
162         
163                 for(int i=1;i<=data.size();i++){                
164                         for(int j=0;j<data[i].abundance;j++){
165                                 rav.push_back(i);
166                         }
167                 }
168                 sort(rav.rbegin(), rav.rend());
169         
170                 rav.setLabel(label);
171                 return rav;
172         }
173         catch(exception& e) {
174                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
175                 exit(1);
176         }
177         catch(...) {
178                 cout << "An unknown error has occurred in the SharedSAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
179                 exit(1);
180         }               
181 }
182 /***********************************************************************/
183 SAbundVector SharedSAbundVector::getSAbundVector(){
184         try {
185                 RAbundVector rav;
186                 SAbundVector sav;
187                 
188                 rav = getRAbundVector();
189                 sav = rav.getSAbundVector();
190                 return sav;
191         
192         }
193         catch(exception& e) {
194                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
195                 exit(1);
196         }
197         catch(...) {
198                 cout << "An unknown error has occurred in the SharedSAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
199                 exit(1);
200         }               
201 }
202
203 /***********************************************************************/
204
205 bool compareMembers (individual member, individual member2){
206
207   if(member.abundance < member2.abundance){
208     return true;   }   
209   else{
210         return false; 
211   }
212 }
213
214 /***********************************************************************/
215 SharedRAbundVector SharedSAbundVector::getSharedRAbundVector(){
216         try {
217                 SharedRAbundVector rav;
218                 
219                 int binNumber = 0;
220                 for(int i=1;i<=data.size();i++){                
221                         for(int j=0;j<data[i].abundance;j++){
222                                 rav.push_back(i, binNumber, data[i].group);
223                                 binNumber++;
224                         }
225                 }
226                 sort(rav.rbegin(), rav.rend(), compareMembers);
227         
228                 rav.setLabel(label);
229                 rav.setGroup(group);
230                 
231                 return rav;
232         }
233         catch(exception& e) {
234                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function getSharedVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
235                 exit(1);
236         }
237         catch(...) {
238                 cout << "An unknown error has occurred in the SharedSAbundVector class function getSharedVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
239                 exit(1);
240         }               
241 }
242
243
244 /***********************************************************************/
245
246 SharedSAbundVector SharedSAbundVector::getSharedSAbundVector(){
247         return *this;                   
248 }
249
250 /***********************************************************************/
251 SharedOrderVector SharedSAbundVector::getSharedOrderVector() {
252         try {
253                 SharedRAbundVector rav;
254                 SharedOrderVector ov;
255                 
256                 rav = this->getSharedRAbundVector();
257                 ov = rav.getSharedOrderVector();
258                 
259                 return ov;
260         }
261         catch(exception& e) {
262                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
263                 exit(1);
264         }
265         catch(...) {
266                 cout << "An unknown error has occurred in the SharedSAbundVector class function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
267                 exit(1);
268         }               
269 }
270 /***********************************************************************/
271 OrderVector SharedSAbundVector::getOrderVector(map<string,int>* hold = NULL){
272         try {
273                 OrderVector ov;
274         
275                 int binIndex = 0;
276         
277                 for(int i=1;i<data.size();i++){
278                         for(int j=0;j<data[i].abundance;j++){
279                                 for(int k=0;k<i;k++){
280                                         ov.push_back(binIndex);
281                                 }
282                                 binIndex++;
283                         }
284                 }
285         
286                 random_shuffle(ov.begin(), ov.end());
287
288                 ov.setLabel(label);
289                 ov.getNumBins();
290                 return ov;
291         }
292         catch(exception& e) {
293                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
294                 exit(1);
295         }
296         catch(...) {
297                 cout << "An unknown error has occurred in the SharedSAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
298                 exit(1);
299         }               
300 }
301
302 /***********************************************************************/
303