]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
added sort to heatmap to put shared otus at the top and updated some error checking
[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
192 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
193         try {
194                 individual newGuy;
195                 newGuy.abundance = binSize;
196                 newGuy.group = groupName;
197                 newGuy.bin = otu;
198                 
199                 data.insert(data.begin(), newGuy);
200                 numBins++;
201         
202                 if(binSize > maxRank){
203                         maxRank = binSize;
204                 }
205         
206                 numSeqs += binSize;
207         }
208         catch(exception& e) {
209                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_front. 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 push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
214                 exit(1);
215         }
216 }
217
218 /***********************************************************************/
219 void SharedRAbundVector::pop_back(){
220
221         return data.pop_back();
222 }
223
224 /***********************************************************************/
225
226
227 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
228         return data.rbegin();                           
229 }
230
231 /***********************************************************************/
232
233 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
234         return data.rend();                                     
235 }
236
237 /***********************************************************************/
238 void SharedRAbundVector::resize(int size){
239         
240         data.resize(size);
241 }
242
243 /***********************************************************************/
244
245 int SharedRAbundVector::size(){
246         return data.size();
247 }
248
249 /***********************************************************************/
250 void SharedRAbundVector::print(ostream& output){
251         try {
252                 output << numBins << '\t';
253         
254                 for(int i=0;i<numBins;i++){             output << data[i].abundance << '\t';            }
255                 output << endl;
256         }
257         catch(exception& e) {
258                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
259                 exit(1);
260         }
261         catch(...) {
262                 cout << "An unknown error has occurred in the SharedRAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
263                 exit(1);
264         }
265 }
266 /***********************************************************************/
267 string SharedRAbundVector::getGroup(){
268         return group;
269 }
270
271 /***********************************************************************/
272
273 void SharedRAbundVector::setGroup(string groupName){
274         group = groupName;
275 }
276 /***********************************************************************/
277 int SharedRAbundVector::getGroupIndex()  { return index; }
278 /***********************************************************************/
279 void SharedRAbundVector::setGroupIndex(int vIndex)      { index = vIndex; }
280 /***********************************************************************/
281 int SharedRAbundVector::getNumBins(){
282         return numBins;
283 }
284
285 /***********************************************************************/
286
287 int SharedRAbundVector::getNumSeqs(){
288         return numSeqs;
289 }
290
291 /***********************************************************************/
292
293 int SharedRAbundVector::getMaxRank(){
294         return maxRank;
295 }
296 /***********************************************************************/
297
298 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
299         return *this;                   
300 }
301 /***********************************************************************/
302
303 RAbundVector SharedRAbundVector::getRAbundVector() {
304         try {
305                 RAbundVector rav(data.size());
306                 
307                 for (int i = 0; i < data.size(); i++) {
308                         rav.set(i, data[i].abundance);
309                 }
310         
311                 return rav;
312         }
313         catch(exception& e) {
314                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. 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 SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
319                 exit(1);
320         }
321 }
322 /***********************************************************************/
323
324 RAbundVector SharedRAbundVector::getRAbundVector2() {
325         try {
326                 RAbundVector rav;
327                 for(int i = 0; i < numBins; i++)
328                         if(data[i].abundance != 0)
329                                 rav.push_back(data[i].abundance-1);
330                 return rav;
331         }
332         catch(exception& e) {
333                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
334                 exit(1);
335         }
336         catch(...) {
337                 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
338                 exit(1);
339         }
340 }
341 /***********************************************************************/
342
343 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
344         try {
345                 SharedSAbundVector sav(maxRank+1);
346                 
347                 for(int i=0;i<data.size();i++){
348                         int abund = data[i].abundance;
349                         sav.set(abund, sav.getAbundance(abund) + 1, group);
350                 }
351                 
352                 sav.set(0, 0, group);
353                 sav.setLabel(label);
354                 sav.setGroup(group);
355                 
356                 return sav;
357         }
358         catch(exception& e) {
359                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
360                 exit(1);
361         }
362         catch(...) {
363                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
364                 exit(1);
365         }
366 }
367 /***********************************************************************/
368
369 SAbundVector SharedRAbundVector::getSAbundVector() {
370         try {
371                 SAbundVector sav(maxRank+1);
372                 
373                 for(int i=0;i<data.size();i++){
374                         int abund = data[i].abundance;
375                         sav.set(abund, sav.get(abund) + 1);
376                 }
377                 sav.set(0, 0);
378                 sav.setLabel(label);
379                 return sav;
380         }
381         catch(exception& e) {
382                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
383                 exit(1);
384         }
385         catch(...) {
386                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
387                 exit(1);
388         }
389 }
390
391 /***********************************************************************/
392
393 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
394         try {
395                 SharedOrderVector ov;
396         
397                 for(int i=0;i<data.size();i++){
398                         for(int j=0;j<data[i].abundance;j++){
399                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
400                         }
401                 }
402                 random_shuffle(ov.begin(), ov.end());
403
404                 ov.setLabel(label);     
405                 return ov;
406         }
407         catch(exception& e) {
408                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
409                 exit(1);
410         }
411         catch(...) {
412                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
413                 exit(1);
414         }
415 }
416 /***********************************************************************/
417
418 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
419         try {
420                 OrderVector ov;
421         
422                 for(int i=0;i<data.size();i++){
423                         for(int j=0;j<data[i].abundance;j++){
424                                 ov.push_back(i);
425                         }
426                 }
427                 random_shuffle(ov.begin(), ov.end());
428
429                 ov.setLabel(label);     
430                 return ov;
431         }
432         catch(exception& e) {
433                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
434                 exit(1);
435         }
436         catch(...) {
437                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
438                 exit(1);
439         }
440 }
441
442 /***********************************************************************/
443