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