]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
Initial revision
[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 "datavector.hpp"
15 #include "utilities.hpp"
16 #include <exception>
17 #include "sharedrabundvector.h"
18 #include "sabundvector.hpp"
19 #include "ordervector.hpp"
20
21
22 /***********************************************************************/
23
24 SharedRAbundVector::SharedRAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {};
25
26 /***********************************************************************/
27
28 SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
29                 individual newGuy;
30                 //initialize data
31                 for (int i=0; i< n; i++) {
32                         newGuy.bin = i;
33                         newGuy.abundance = 0;
34                         data.push_back(newGuy);
35                 }
36 };
37
38 /***********************************************************************
39
40 SharedRAbundVector::SharedRAbundVector(string id, vector<individual> rav) : DataVector(id), data(rav) {
41         try {
42                 numBins = 0;
43                 maxRank = 0;
44                 numSeqs = 0;
45                 
46                 for(int i=0;i<data.size();i++){
47                         if(data[i].abundance != 0)              {       numBins = i+1;          }
48                         if(data[i].abundance > maxRank) {       maxRank = data[i].abundance;    }
49                         numSeqs += data[i].abundance;
50                 }
51         }
52         catch(exception& e) {
53                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. 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 SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
58                 exit(1);
59         }
60 }
61
62
63 /***********************************************************************
64
65
66 SharedRAbundVector::SharedRAbundVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
67         try {
68                 int i, hold;
69                 individual newGuy;
70                 
71                 f >> label >> hold;
72                 
73                 //initialize data
74                 for (i=0; i<hold; i++) {
75                         newGuy = new individual;
76                         newGuy.abundance = 0;
77                         newGuy.bin = i;
78                         data.push_back(newGuy);
79                 }
80                 int inputData;
81         
82                 for(int i=0;i<hold;i++){
83                         f >> inputData;
84                         set(i, inputData);
85                 }
86         }
87         catch(exception& e) {
88                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. 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 SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
93                 exit(1);
94         }
95 }
96
97 /***********************************************************************/
98
99 SharedRAbundVector::~SharedRAbundVector() {
100
101 }
102
103 /***********************************************************************/
104
105 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
106         try {
107                 int oldBinSize = data[binNumber].abundance;
108                 data[binNumber].abundance = newBinSize;
109                 data[binNumber].group = groupname;
110         
111                 if(newBinSize > maxRank)        {       maxRank = newBinSize;   }
112         
113                 numSeqs += (newBinSize - oldBinSize);
114         }
115         catch(exception& e) {
116                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
117                 exit(1);
118         }
119         catch(...) {
120                 cout << "An unknown error has occurred in the SharedRAbundVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
121                 exit(1);
122         }
123 }
124
125 /***********************************************************************/
126
127 int SharedRAbundVector::getAbundance(int index){
128         return data[index].abundance;
129         
130 }
131
132 /***********************************************************************/
133
134 individual SharedRAbundVector::get(int index){
135         return data[index];
136         
137 }
138
139 /***********************************************************************/
140
141 void SharedRAbundVector::push_back(int binSize, int otu, string groupName){
142         try {
143                 individual newGuy;
144                 newGuy.abundance = binSize;
145                 newGuy.group = groupName;
146                 newGuy.bin = otu;
147                 
148                 data.push_back(newGuy);
149                 numBins++;
150         
151                 if(binSize > maxRank){
152                         maxRank = binSize;
153                 }
154         
155                 numSeqs += binSize;
156         }
157         catch(exception& e) {
158                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
159                 exit(1);
160         }
161         catch(...) {
162                 cout << "An unknown error has occurred in the SharedRAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
163                 exit(1);
164         }
165 }
166
167 /***********************************************************************/
168
169 void SharedRAbundVector::pop_back(){
170
171         return data.pop_back();
172 }
173
174 /***********************************************************************/
175
176
177 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
178         return data.rbegin();                           
179 }
180
181 /***********************************************************************/
182
183 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
184         return data.rend();                                     
185 }
186
187 /***********************************************************************/
188 void SharedRAbundVector::resize(int size){
189         
190         data.resize(size);
191 }
192
193 /***********************************************************************/
194
195 int SharedRAbundVector::size(){
196         return data.size();
197 }
198
199 /***********************************************************************/
200 void SharedRAbundVector::print(ostream& output){
201         try {
202                 output << numBins << '\t';
203         
204                 for(int i=0;i<numBins;i++){             output << data[i].abundance << '\t';            }
205                 output << endl;
206         }
207         catch(exception& e) {
208                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
209                 exit(1);
210         }
211         catch(...) {
212                 cout << "An unknown error has occurred in the SharedRAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
213                 exit(1);
214         }
215 }
216 /***********************************************************************/
217 string SharedRAbundVector::getGroup(){
218         return group;
219 }
220
221 /***********************************************************************/
222
223 void SharedRAbundVector::setGroup(string groupName){
224         group = groupName;
225 }
226
227
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
253 SAbundVector SharedRAbundVector::getSAbundVector() {
254         try {
255                 SAbundVector sav(maxRank+1);
256                 
257                 for(int i=0;i<data.size();i++){
258                         int abund = data[i].abundance;
259                         sav.set(abund, sav.get(abund) + 1);
260                 }
261                 sav.set(0, 0);
262                 sav.setLabel(label);
263                 return sav;
264         }
265         catch(exception& e) {
266                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
267                 exit(1);
268         }
269         catch(...) {
270                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
271                 exit(1);
272         }
273 }
274
275 /***********************************************************************/
276
277 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
278         try {
279                 SharedOrderVector ov;
280         
281                 for(int i=0;i<data.size();i++){
282                         for(int j=0;j<data[i].abundance;j++){
283                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
284                         }
285                 }
286                 random_shuffle(ov.begin(), ov.end());
287
288                 ov.setLabel(label);     
289                 return ov;
290         }
291         catch(exception& e) {
292                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector 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 SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
297                 exit(1);
298         }
299 }
300 /***********************************************************************/
301
302 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
303         try {
304                 OrderVector ov;
305         
306                 for(int i=0;i<data.size();i++){
307                         for(int j=0;j<data[i].abundance;j++){
308                                 ov.push_back(i);
309                         }
310                 }
311                 random_shuffle(ov.begin(), ov.end());
312
313                 ov.setLabel(label);     
314                 return ov;
315         }
316         catch(exception& e) {
317                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
318                 exit(1);
319         }
320         catch(...) {
321                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
322                 exit(1);
323         }
324 }
325
326 /***********************************************************************/
327