]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
finishing the container classes, combining read.otu and read.list commands. some...
[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 int SharedRAbundVector::getNumBins(){
229         return numBins;
230 }
231
232 /***********************************************************************/
233
234 int SharedRAbundVector::getNumSeqs(){
235         return numSeqs;
236 }
237
238 /***********************************************************************/
239
240 int SharedRAbundVector::getMaxRank(){
241         return maxRank;
242 }
243
244 /***********************************************************************/
245
246 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
247         return *this;                   
248 }
249 /***********************************************************************/
250
251 RAbundVector SharedRAbundVector::getRAbundVector() {
252         try {
253                 RAbundVector rav(data.size());
254                 
255                 for (int i = 0; i < data.size(); i++) {
256                         rav.set(i, data[i].abundance);
257                 }
258         
259                 return rav;
260         }
261         catch(exception& e) {
262                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
263                 exit(1);
264         }
265         catch(...) {
266                 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
267                 exit(1);
268         }
269 }
270
271 /***********************************************************************/
272
273 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
274         try {
275                 SharedSAbundVector sav(maxRank+1);
276                 
277                 for(int i=0;i<data.size();i++){
278                         int abund = data[i].abundance;
279                         sav.set(abund, sav.getAbundance(abund) + 1, group);
280                 }
281                 
282                 sav.set(0, 0, group);
283                 sav.setLabel(label);
284                 sav.setGroup(group);
285                 
286                 return sav;
287         }
288         catch(exception& e) {
289                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
290                 exit(1);
291         }
292         catch(...) {
293                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
294                 exit(1);
295         }
296 }
297 /***********************************************************************/
298
299 SAbundVector SharedRAbundVector::getSAbundVector() {
300         try {
301                 SAbundVector sav(maxRank+1);
302                 
303                 for(int i=0;i<data.size();i++){
304                         int abund = data[i].abundance;
305                         sav.set(abund, sav.get(abund) + 1);
306                 }
307                 sav.set(0, 0);
308                 sav.setLabel(label);
309                 return sav;
310         }
311         catch(exception& e) {
312                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
313                 exit(1);
314         }
315         catch(...) {
316                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
317                 exit(1);
318         }
319 }
320
321 /***********************************************************************/
322
323 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
324         try {
325                 SharedOrderVector ov;
326         
327                 for(int i=0;i<data.size();i++){
328                         for(int j=0;j<data[i].abundance;j++){
329                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
330                         }
331                 }
332                 random_shuffle(ov.begin(), ov.end());
333
334                 ov.setLabel(label);     
335                 return ov;
336         }
337         catch(exception& e) {
338                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
339                 exit(1);
340         }
341         catch(...) {
342                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
343                 exit(1);
344         }
345 }
346 /***********************************************************************/
347
348 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
349         try {
350                 OrderVector ov;
351         
352                 for(int i=0;i<data.size();i++){
353                         for(int j=0;j<data[i].abundance;j++){
354                                 ov.push_back(i);
355                         }
356                 }
357                 random_shuffle(ov.begin(), ov.end());
358
359                 ov.setLabel(label);     
360                 return ov;
361         }
362         catch(exception& e) {
363                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
364                 exit(1);
365         }
366         catch(...) {
367                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
368                 exit(1);
369         }
370 }
371
372 /***********************************************************************/
373