]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
added read.shared, broke up globaldata a bit
[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, num;
69                 string holdLabel, group
70                 individual newGuy;
71                 
72                 f >> label >> group >> num;
73                 
74                 //initialize data
75                 for (i=0; i<hold; i++) {
76                         newGuy = new individual;
77                         newGuy.abundance = 0;
78                         newGuy.bin = i;
79                         data.push_back(newGuy);
80                 }
81                 int inputData;
82         
83                 for(int i=0;i<hold;i++){
84                         f >> inputData;
85                         set(i, inputData);
86                 }
87         }
88         catch(exception& e) {
89                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. 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 SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
94                 exit(1);
95         }
96 }
97
98 /***********************************************************************/
99
100 SharedRAbundVector::~SharedRAbundVector() {
101
102 }
103
104 /***********************************************************************/
105
106 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
107         try {
108                 int oldBinSize = data[binNumber].abundance;
109                 data[binNumber].abundance = newBinSize;
110                 data[binNumber].group = groupname;
111         
112                 if(newBinSize > maxRank)        {       maxRank = newBinSize;   }
113         
114                 numSeqs += (newBinSize - oldBinSize);
115         }
116         catch(exception& e) {
117                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
118                 exit(1);
119         }
120         catch(...) {
121                 cout << "An unknown error has occurred in the SharedRAbundVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
122                 exit(1);
123         }
124 }
125
126 /***********************************************************************/
127
128 int SharedRAbundVector::getAbundance(int index){
129         return data[index].abundance;
130         
131 }
132
133 /***********************************************************************/
134
135 individual SharedRAbundVector::get(int index){
136         return data[index];
137         
138 }
139
140 /***********************************************************************/
141
142 void SharedRAbundVector::push_back(int binSize, int otu, string groupName){
143         try {
144                 individual newGuy;
145                 newGuy.abundance = binSize;
146                 newGuy.group = groupName;
147                 newGuy.bin = otu;
148                 
149                 data.push_back(newGuy);
150                 numBins++;
151         
152                 if(binSize > maxRank){
153                         maxRank = binSize;
154                 }
155         
156                 numSeqs += binSize;
157         }
158         catch(exception& e) {
159                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
160                 exit(1);
161         }
162         catch(...) {
163                 cout << "An unknown error has occurred in the SharedRAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
164                 exit(1);
165         }
166 }
167
168 /***********************************************************************/
169
170 void SharedRAbundVector::pop_back(){
171
172         return data.pop_back();
173 }
174
175 /***********************************************************************/
176
177
178 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
179         return data.rbegin();                           
180 }
181
182 /***********************************************************************/
183
184 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
185         return data.rend();                                     
186 }
187
188 /***********************************************************************/
189 void SharedRAbundVector::resize(int size){
190         
191         data.resize(size);
192 }
193
194 /***********************************************************************/
195
196 int SharedRAbundVector::size(){
197         return data.size();
198 }
199
200 /***********************************************************************/
201 void SharedRAbundVector::print(ostream& output){
202         try {
203                 output << numBins << '\t';
204         
205                 for(int i=0;i<numBins;i++){             output << data[i].abundance << '\t';            }
206                 output << endl;
207         }
208         catch(exception& e) {
209                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
210                 exit(1);
211         }
212         catch(...) {
213                 cout << "An unknown error has occurred in the SharedRAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
214                 exit(1);
215         }
216 }
217 /***********************************************************************/
218 string SharedRAbundVector::getGroup(){
219         return group;
220 }
221
222 /***********************************************************************/
223
224 void SharedRAbundVector::setGroup(string groupName){
225         group = groupName;
226 }
227 /***********************************************************************/
228 int SharedRAbundVector::getGroupIndex()  { return index; }
229 /***********************************************************************/
230 void SharedRAbundVector::setGroupIndex(int vIndex)      { index = vIndex; }
231 /***********************************************************************/
232 int SharedRAbundVector::getNumBins(){
233         return numBins;
234 }
235
236 /***********************************************************************/
237
238 int SharedRAbundVector::getNumSeqs(){
239         return numSeqs;
240 }
241
242 /***********************************************************************/
243
244 int SharedRAbundVector::getMaxRank(){
245         return maxRank;
246 }
247
248 /***********************************************************************/
249
250 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
251         return *this;                   
252 }
253 /***********************************************************************/
254
255 RAbundVector SharedRAbundVector::getRAbundVector() {
256         try {
257                 RAbundVector rav(data.size());
258                 
259                 for (int i = 0; i < data.size(); i++) {
260                         rav.set(i, data[i].abundance);
261                 }
262         
263                 return rav;
264         }
265         catch(exception& e) {
266                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. 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 getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
271                 exit(1);
272         }
273 }
274
275 /***********************************************************************/
276
277 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
278         try {
279                 SharedSAbundVector sav(maxRank+1);
280                 
281                 for(int i=0;i<data.size();i++){
282                         int abund = data[i].abundance;
283                         sav.set(abund, sav.getAbundance(abund) + 1, group);
284                 }
285                 
286                 sav.set(0, 0, group);
287                 sav.setLabel(label);
288                 sav.setGroup(group);
289                 
290                 return sav;
291         }
292         catch(exception& e) {
293                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
294                 exit(1);
295         }
296         catch(...) {
297                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
298                 exit(1);
299         }
300 }
301 /***********************************************************************/
302
303 SAbundVector SharedRAbundVector::getSAbundVector() {
304         try {
305                 SAbundVector sav(maxRank+1);
306                 
307                 for(int i=0;i<data.size();i++){
308                         int abund = data[i].abundance;
309                         sav.set(abund, sav.get(abund) + 1);
310                 }
311                 sav.set(0, 0);
312                 sav.setLabel(label);
313                 return sav;
314         }
315         catch(exception& e) {
316                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
317                 exit(1);
318         }
319         catch(...) {
320                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
321                 exit(1);
322         }
323 }
324
325 /***********************************************************************/
326
327 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
328         try {
329                 SharedOrderVector ov;
330         
331                 for(int i=0;i<data.size();i++){
332                         for(int j=0;j<data[i].abundance;j++){
333                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
334                         }
335                 }
336                 random_shuffle(ov.begin(), ov.end());
337
338                 ov.setLabel(label);     
339                 return ov;
340         }
341         catch(exception& e) {
342                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
343                 exit(1);
344         }
345         catch(...) {
346                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
347                 exit(1);
348         }
349 }
350 /***********************************************************************/
351
352 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
353         try {
354                 OrderVector ov;
355         
356                 for(int i=0;i<data.size();i++){
357                         for(int j=0;j<data[i].abundance;j++){
358                                 ov.push_back(i);
359                         }
360                 }
361                 random_shuffle(ov.begin(), ov.end());
362
363                 ov.setLabel(label);     
364                 return ov;
365         }
366         catch(exception& e) {
367                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
368                 exit(1);
369         }
370         catch(...) {
371                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
372                 exit(1);
373         }
374 }
375
376 /***********************************************************************/
377