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