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