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