]> git.donarmstrong.com Git - mothur.git/blob - sharedsabundvector.cpp
finishing the container classes, combining read.otu and read.list commands. some...
[mothur.git] / sharedsabundvector.cpp
1 /*
2  *  sharedSharedSAbundVector.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 12/10/08.
6  *  Copyright 2008 __MyCompanyName__. All rights reserved.
7  *
8  */
9
10 #include "sharedsabundvector.h"
11 #include "sabundvector.hpp"
12 #include "datavector.hpp"
13 #include "utilities.hpp"
14 #include <exception>
15
16 using namespace std;
17
18
19 /***********************************************************************/
20
21 SharedSAbundVector::SharedSAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){};
22
23 /***********************************************************************/
24
25 SharedSAbundVector::SharedSAbundVector(int size) :      DataVector(), maxRank(0), numBins(0), numSeqs(0) {
26                 individual newGuy;
27                 //initialize data
28                 for (int i=0; i< size; i++) {
29                         newGuy.bin = i;
30                         newGuy.abundance = 0;
31                         data.push_back(newGuy);
32                 }
33         //      for(int i=0;i<data.size();i++){
34         //              if(data[i].abundance != 0){     maxRank = i;    }
35         //              numSeqs += i*data[i].abundance;
36         //              numBins += data[i].abundance;
37         //      }
38
39 };
40
41 /***********************************************************************
42
43 SharedSAbundVector::SharedSAbundVector(string id, vector<int> sav) : DataVector(id), data(sav) {
44         try {
45                 
46                 for(int i=0;i<sav.size();i++){
47                         if(data[i] != 0){       maxRank = i;    }
48                         numSeqs += i*data[i];
49                         numBins += data[i];
50                 }
51         }
52         catch(exception& e) {
53                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function SharedSAbundVector. 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 SharedSAbundVector class function SharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
58                 exit(1);
59         }
60 }
61
62 /***********************************************************************
63
64 SharedSAbundVector::SharedSAbundVector(ifstream& f): DataVector(), maxRank(0), numBins(0), numSeqs(0) {
65         try {
66                 int hold;
67                 f >> label >> hold;
68         
69                 data.assign(hold+1, 0);
70                 int inputData;
71         
72                 for(int i=1;i<=hold;i++){
73                         f >> inputData;
74                         set(i, inputData);
75                 }
76         }
77         catch(exception& e) {
78                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function SharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
79                 exit(1);
80         }
81         catch(...) {
82                 cout << "An unknown error has occurred in the SharedSAbundVector class function SharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
83                 exit(1);
84         }
85 }
86
87 /***********************************************************************/
88
89 void SharedSAbundVector::set(int bin, int abundance, string groupName){
90         try {
91
92                 int initSize = data[bin].abundance;
93                 data[bin].abundance = abundance;
94                 data[bin].group = groupName;
95         
96                 if(bin != 0){
97                         numBins += (abundance - initSize);
98                 }
99         
100                 numSeqs += bin * (abundance - initSize);
101         
102                 if(bin > maxRank)       {       maxRank = bin;          }
103         }
104         catch(exception& e) {
105                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
106                 exit(1);
107         }
108         catch(...) {
109                 cout << "An unknown error has occurred in the SharedSAbundVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
110                 exit(1);
111         }       
112 }
113
114 /***********************************************************************/
115
116 individual SharedSAbundVector::get(int index){
117         return data[index];
118 }
119 /***********************************************************************/
120
121 int SharedSAbundVector::getAbundance(int index){
122         return data[index].abundance;
123 }
124
125 /***********************************************************************/
126
127 void SharedSAbundVector::push_back(int abundance, int bin, string groupName){
128         try {
129                 individual newGuy;
130                 newGuy.abundance = abundance;
131                 newGuy.bin = bin;
132                 newGuy.group = groupName;
133                 
134                 data.push_back(newGuy);
135         
136                 maxRank++;      
137         
138                 numBins += abundance;
139         
140                 numSeqs += (maxRank * abundance);
141         }
142         catch(exception& e) {
143                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
144                 exit(1);
145         }
146         catch(...) {
147                 cout << "An unknown error has occurred in the SharedSAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
148                 exit(1);
149         }               
150 }
151
152 /***********************************************************************/
153
154 void SharedSAbundVector::resize(int size){
155         data.resize(size);
156 }
157
158 /***********************************************************************/
159
160 int SharedSAbundVector::size(){
161         return data.size();             
162 }
163
164 /***********************************************************************/
165 void SharedSAbundVector::print(ostream& output){
166         try {
167                 output << label << '\t' << maxRank << '\t';
168         
169                 for(int i=1;i<=maxRank;i++){
170                         output << data[i].abundance << '\t';
171                 }
172                 output << endl;
173         }
174         catch(exception& e) {
175                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
176                 exit(1);
177         }
178         catch(...) {
179                 cout << "An unknown error has occurred in the SharedSAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
180                 exit(1);
181         }               
182 }
183 /***********************************************************************/
184 string SharedSAbundVector::getGroup(){
185         return group;
186 }
187
188 /***********************************************************************/
189
190 void SharedSAbundVector::setGroup(string groupName){
191         group = groupName;
192 }
193
194 /**********************************************************************/
195 int SharedSAbundVector::getNumBins(){
196         return numBins;
197 }
198
199 /***********************************************************************/
200
201 int SharedSAbundVector::getNumSeqs(){
202         return numSeqs;
203 }
204
205 /***********************************************************************/
206
207 int SharedSAbundVector::getMaxRank(){
208         return maxRank;
209 }
210 /***********************************************************************/
211 RAbundVector SharedSAbundVector::getRAbundVector(){
212         try {
213                 RAbundVector rav;
214         
215                 for(int i=1;i<=data.size();i++){                
216                         for(int j=0;j<data[i].abundance;j++){
217                                 rav.push_back(i);
218                         }
219                 }
220                 sort(rav.rbegin(), rav.rend());
221         
222                 rav.setLabel(label);
223                 return rav;
224         }
225         catch(exception& e) {
226                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
227                 exit(1);
228         }
229         catch(...) {
230                 cout << "An unknown error has occurred in the SharedSAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
231                 exit(1);
232         }               
233 }
234 /***********************************************************************/
235 SAbundVector SharedSAbundVector::getSAbundVector(){
236         try {
237                 RAbundVector rav;
238                 SAbundVector sav;
239                 
240                 rav = getRAbundVector();
241                 sav = rav.getSAbundVector();
242                 return sav;
243         
244         }
245         catch(exception& e) {
246                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
247                 exit(1);
248         }
249         catch(...) {
250                 cout << "An unknown error has occurred in the SharedSAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
251                 exit(1);
252         }               
253 }
254
255 /***********************************************************************/
256
257 bool compareMembers (individual member, individual member2){
258
259   if(member.abundance < member2.abundance){
260     return true;   }   
261   else{
262         return false; 
263   }
264 }
265
266 /***********************************************************************/
267 SharedRAbundVector SharedSAbundVector::getSharedRAbundVector(){
268         try {
269                 SharedRAbundVector rav;
270                 
271                 int binNumber = 0;
272                 for(int i=1;i<=data.size();i++){                
273                         for(int j=0;j<data[i].abundance;j++){
274                                 rav.push_back(i, binNumber, data[i].group);
275                                 binNumber++;
276                         }
277                 }
278                 sort(rav.rbegin(), rav.rend(), compareMembers);
279         
280                 rav.setLabel(label);
281                 rav.setGroup(group);
282                 
283                 return rav;
284         }
285         catch(exception& e) {
286                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function getSharedVector. 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 SharedSAbundVector class function getSharedVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
291                 exit(1);
292         }               
293 }
294
295
296 /***********************************************************************/
297
298 SharedSAbundVector SharedSAbundVector::getSharedSAbundVector(){
299         return *this;                   
300 }
301
302 /***********************************************************************/
303 SharedOrderVector SharedSAbundVector::getSharedOrderVector() {
304         try {
305                 SharedRAbundVector rav;
306                 SharedOrderVector ov;
307                 
308                 rav = this->getSharedRAbundVector();
309                 ov = rav.getSharedOrderVector();
310                 
311                 return ov;
312         }
313         catch(exception& e) {
314                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
315                 exit(1);
316         }
317         catch(...) {
318                 cout << "An unknown error has occurred in the SharedSAbundVector class function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
319                 exit(1);
320         }               
321 }
322 /***********************************************************************/
323 OrderVector SharedSAbundVector::getOrderVector(map<string,int>* hold = NULL){
324         try {
325                 OrderVector ov;
326         
327                 int binIndex = 0;
328         
329                 for(int i=1;i<data.size();i++){
330                         for(int j=0;j<data[i].abundance;j++){
331                                 for(int k=0;k<i;k++){
332                                         ov.push_back(binIndex);
333                                 }
334                                 binIndex++;
335                         }
336                 }
337         
338                 random_shuffle(ov.begin(), ov.end());
339
340                 ov.setLabel(label);
341                 ov.getNumBins();
342                 return ov;
343         }
344         catch(exception& e) {
345                 cout << "Standard Error: " << e.what() << " has occurred in the SharedSAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
346                 exit(1);
347         }
348         catch(...) {
349                 cout << "An unknown error has occurred in the SharedSAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
350                 exit(1);
351         }               
352 }
353
354 /***********************************************************************/
355