]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
added mothur.h and fixed includes in many files
[mothur.git] / sharedrabundvector.cpp
1 /*
2  *  sharedvector.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 12/5/08.
6  *  Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10
11 using namespace std;
12
13 #include "sharedrabundvector.h" 
14 #include "utilities.hpp"
15 #include "sabundvector.hpp"
16 #include "ordervector.hpp"
17
18
19 /***********************************************************************/
20
21 SharedRAbundVector::SharedRAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {};
22
23 /***********************************************************************/
24
25 SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
26                 individual newGuy;
27                 //initialize data
28                 for (int i=0; i< n; i++) {
29                         newGuy.bin = i;
30                         newGuy.abundance = 0;
31                         data.push_back(newGuy);
32                 }
33 };
34
35 /***********************************************************************
36
37 SharedRAbundVector::SharedRAbundVector(string id, vector<individual> rav) : DataVector(id), data(rav) {
38         try {
39                 numBins = 0;
40                 maxRank = 0;
41                 numSeqs = 0;
42                 
43                 for(int i=0;i<data.size();i++){
44                         if(data[i].abundance != 0)              {       numBins = i+1;          }
45                         if(data[i].abundance > maxRank) {       maxRank = data[i].abundance;    }
46                         numSeqs += data[i].abundance;
47                 }
48         }
49         catch(exception& e) {
50                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. 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 SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
55                 exit(1);
56         }
57 }
58
59
60 /***********************************************************************
61
62
63 SharedRAbundVector::SharedRAbundVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
64         try {
65                 int i, num;
66                 string holdLabel, group
67                 individual newGuy;
68                 
69                 f >> label >> group >> num;
70                 
71                 //initialize data
72                 for (i=0; i<hold; i++) {
73                         newGuy = new individual;
74                         newGuy.abundance = 0;
75                         newGuy.bin = i;
76                         data.push_back(newGuy);
77                 }
78                 int inputData;
79         
80                 for(int i=0;i<hold;i++){
81                         f >> inputData;
82                         set(i, inputData);
83                 }
84         }
85         catch(exception& e) {
86                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. 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 SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
91                 exit(1);
92         }
93 }
94
95 /***********************************************************************/
96
97 SharedRAbundVector::~SharedRAbundVector() {
98
99 }
100
101 /***********************************************************************/
102
103 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
104         try {
105                 int oldBinSize = data[binNumber].abundance;
106                 data[binNumber].abundance = newBinSize;
107                 data[binNumber].group = groupname;
108         
109                 if(newBinSize > maxRank)        {       maxRank = newBinSize;   }
110         
111                 numSeqs += (newBinSize - oldBinSize);
112         }
113         catch(exception& e) {
114                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
115                 exit(1);
116         }
117         catch(...) {
118                 cout << "An unknown error has occurred in the SharedRAbundVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
119                 exit(1);
120         }
121 }
122
123 /***********************************************************************/
124
125 int SharedRAbundVector::getAbundance(int index){
126         return data[index].abundance;
127         
128 }
129
130 /***********************************************************************/
131
132 individual SharedRAbundVector::get(int index){
133         return data[index];
134         
135 }
136
137 /***********************************************************************/
138
139 void SharedRAbundVector::push_back(int binSize, int otu, string groupName){
140         try {
141                 individual newGuy;
142                 newGuy.abundance = binSize;
143                 newGuy.group = groupName;
144                 newGuy.bin = otu;
145                 
146                 data.push_back(newGuy);
147                 numBins++;
148         
149                 if(binSize > maxRank){
150                         maxRank = binSize;
151                 }
152         
153                 numSeqs += binSize;
154         }
155         catch(exception& e) {
156                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
157                 exit(1);
158         }
159         catch(...) {
160                 cout << "An unknown error has occurred in the SharedRAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
161                 exit(1);
162         }
163 }
164
165 /***********************************************************************/
166
167 void SharedRAbundVector::pop_back(){
168
169         return data.pop_back();
170 }
171
172 /***********************************************************************/
173
174
175 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
176         return data.rbegin();                           
177 }
178
179 /***********************************************************************/
180
181 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
182         return data.rend();                                     
183 }
184
185 /***********************************************************************/
186 void SharedRAbundVector::resize(int size){
187         
188         data.resize(size);
189 }
190
191 /***********************************************************************/
192
193 int SharedRAbundVector::size(){
194         return data.size();
195 }
196
197 /***********************************************************************/
198 void SharedRAbundVector::print(ostream& output){
199         try {
200                 output << numBins << '\t';
201         
202                 for(int i=0;i<numBins;i++){             output << data[i].abundance << '\t';            }
203                 output << endl;
204         }
205         catch(exception& e) {
206                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
207                 exit(1);
208         }
209         catch(...) {
210                 cout << "An unknown error has occurred in the SharedRAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
211                 exit(1);
212         }
213 }
214 /***********************************************************************/
215 string SharedRAbundVector::getGroup(){
216         return group;
217 }
218
219 /***********************************************************************/
220
221 void SharedRAbundVector::setGroup(string groupName){
222         group = groupName;
223 }
224 /***********************************************************************/
225 int SharedRAbundVector::getGroupIndex()  { return index; }
226 /***********************************************************************/
227 void SharedRAbundVector::setGroupIndex(int vIndex)      { index = vIndex; }
228 /***********************************************************************/
229 int SharedRAbundVector::getNumBins(){
230         return numBins;
231 }
232
233 /***********************************************************************/
234
235 int SharedRAbundVector::getNumSeqs(){
236         return numSeqs;
237 }
238
239 /***********************************************************************/
240
241 int SharedRAbundVector::getMaxRank(){
242         return maxRank;
243 }
244
245 /***********************************************************************/
246
247 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
248         return *this;                   
249 }
250 /***********************************************************************/
251
252 RAbundVector SharedRAbundVector::getRAbundVector() {
253         try {
254                 RAbundVector rav(data.size());
255                 
256                 for (int i = 0; i < data.size(); i++) {
257                         rav.set(i, data[i].abundance);
258                 }
259         
260                 return rav;
261         }
262         catch(exception& e) {
263                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
264                 exit(1);
265         }
266         catch(...) {
267                 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
268                 exit(1);
269         }
270 }
271
272 /***********************************************************************/
273
274 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
275         try {
276                 SharedSAbundVector sav(maxRank+1);
277                 
278                 for(int i=0;i<data.size();i++){
279                         int abund = data[i].abundance;
280                         sav.set(abund, sav.getAbundance(abund) + 1, group);
281                 }
282                 
283                 sav.set(0, 0, group);
284                 sav.setLabel(label);
285                 sav.setGroup(group);
286                 
287                 return sav;
288         }
289         catch(exception& e) {
290                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedSAbundVector. 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 SharedRAbundVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
295                 exit(1);
296         }
297 }
298 /***********************************************************************/
299
300 SAbundVector SharedRAbundVector::getSAbundVector() {
301         try {
302                 SAbundVector sav(maxRank+1);
303                 
304                 for(int i=0;i<data.size();i++){
305                         int abund = data[i].abundance;
306                         sav.set(abund, sav.get(abund) + 1);
307                 }
308                 sav.set(0, 0);
309                 sav.setLabel(label);
310                 return sav;
311         }
312         catch(exception& e) {
313                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
314                 exit(1);
315         }
316         catch(...) {
317                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
318                 exit(1);
319         }
320 }
321
322 /***********************************************************************/
323
324 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
325         try {
326                 SharedOrderVector ov;
327         
328                 for(int i=0;i<data.size();i++){
329                         for(int j=0;j<data[i].abundance;j++){
330                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
331                         }
332                 }
333                 random_shuffle(ov.begin(), ov.end());
334
335                 ov.setLabel(label);     
336                 return ov;
337         }
338         catch(exception& e) {
339                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
340                 exit(1);
341         }
342         catch(...) {
343                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
344                 exit(1);
345         }
346 }
347 /***********************************************************************/
348
349 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
350         try {
351                 OrderVector ov;
352         
353                 for(int i=0;i<data.size();i++){
354                         for(int j=0;j<data[i].abundance;j++){
355                                 ov.push_back(i);
356                         }
357                 }
358                 random_shuffle(ov.begin(), ov.end());
359
360                 ov.setLabel(label);     
361                 return ov;
362         }
363         catch(exception& e) {
364                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
365                 exit(1);
366         }
367         catch(...) {
368                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
369                 exit(1);
370         }
371 }
372
373 /***********************************************************************/
374