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