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