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