]> git.donarmstrong.com Git - mothur.git/blob - summarycommand.cpp
5adf44ff8621e5ea6e5231abc9417cdb3ff93e80
[mothur.git] / summarycommand.cpp
1 /*
2  *  summarycommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "summarycommand.h"
11 #include "ace.h"
12 #include "sobs.h"
13 #include "nseqs.h"
14 #include "chao1.h"
15 #include "bootstrap.h"
16 #include "simpson.h"
17 #include "npshannon.h"
18 #include "shannon.h"
19 #include "jackknife.h"
20
21 //**********************************************************************************************************************
22
23 SummaryCommand::SummaryCommand(){
24         try {
25                 globaldata = GlobalData::getInstance();
26                 validCalculator = new ValidCalculators();
27                 int i;
28                 
29                 for (i=0; i<globaldata->Estimators.size(); i++) {
30                         if (validCalculator->isValidCalculator("summary", globaldata->Estimators[i]) == true) { 
31                                 if(globaldata->Estimators[i] == "sobs"){
32                                         sumCalculators.push_back(new Sobs());
33                                 }else if(globaldata->Estimators[i] == "chao"){
34                                         sumCalculators.push_back(new Chao1());
35                                 }else if(globaldata->Estimators[i] == "ace"){
36                                         convert(globaldata->getAbund(), abund);
37                                         if(abund < 5)
38                                                 abund = 10;
39                                         sumCalculators.push_back(new Ace(abund));
40                                 }else if(globaldata->Estimators[i] == "jack"){
41                                         sumCalculators.push_back(new Jackknife());
42                                 }else if(globaldata->Estimators[i] == "shannon"){
43                                         sumCalculators.push_back(new Shannon());
44                                 }else if(globaldata->Estimators[i] == "npshannon"){
45                                         sumCalculators.push_back(new NPShannon());
46                                 }else if(globaldata->Estimators[i] == "simpson"){
47                                         sumCalculators.push_back(new Simpson());
48                                 }else if(globaldata->Estimators[i] == "bootstrap"){
49                                         sumCalculators.push_back(new Bootstrap());
50                                 }else if (globaldata->Estimators[i] == "nseqs") { 
51                                         sumCalculators.push_back(new NSeqs());
52                                 }
53                         }
54                 }
55                 
56                 //reset calc for next command
57                 globaldata->setCalc("");
58
59         }
60         catch(exception& e) {
61                 cout << "Standard Error: " << e.what() << " has occurred in the SummaryCommand class Function SummaryCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
62                 exit(1);
63         }
64         catch(...) {
65                 cout << "An unknown error has occurred in the SummaryCommand class function SummaryCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
66                 exit(1);
67         }       
68 }
69 //**********************************************************************************************************************
70
71 SummaryCommand::~SummaryCommand(){
72         delete sabund;
73         delete input;
74         delete read;
75 }
76
77 //**********************************************************************************************************************
78
79 int SummaryCommand::execute(){
80         try {
81         
82                 //if the users entered no valid calculators don't execute command
83                 if (sumCalculators.size() == 0) { return 0; }
84
85                 outputFileName = ((getRootName(globaldata->inputFileName)) + "summary");
86                 openOutputFile(outputFileName, outputFileHandle);
87                 outputFileHandle << "label";
88         
89                 read = new ReadPhilFile(globaldata->inputFileName);     
90                 read->read(&*globaldata); 
91
92                 for(int i=0;i<sumCalculators.size();i++){
93                         if(sumCalculators[i]->getCols() == 1){
94                                 outputFileHandle << '\t' << sumCalculators[i]->getName();
95                         }
96                         else{
97                                 outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
98                         }
99                 }
100                 outputFileHandle << endl;
101                 
102                 sabund = globaldata->sabund;
103                 input = globaldata->ginput;
104                 int count = 1;
105                 while(sabund != NULL){
106                 
107                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(sabund->getLabel()) == 1){                      
108         
109                                 cout << sabund->getLabel() << '\t' << count << endl;
110                         
111                                 outputFileHandle << sabund->getLabel();
112                                 for(int i=0;i<sumCalculators.size();i++){
113                                         vector<double> data = sumCalculators[i]->getValues(sabund);
114                                         outputFileHandle << '\t';
115                                         sumCalculators[i]->print(outputFileHandle);
116                                 }
117                                 outputFileHandle << endl;
118                         }
119                 
120                         sabund = input->getSAbundVector();
121                         count++;
122                 }
123         
124                 return 0;
125         }
126         catch(exception& e) {
127                 cout << "Standard Error: " << e.what() << " has occurred in the SummaryCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
128                 exit(1);
129         }
130         catch(...) {
131                 cout << "An unknown error has occurred in the SummaryCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
132                 exit(1);
133         }               
134 }
135
136 //**********************************************************************************************************************