]> git.donarmstrong.com Git - mothur.git/blob - summarytaxcommand.cpp
fixed bug with shhh.flow from file path name in write functions, added "smart" featur...
[mothur.git] / summarytaxcommand.cpp
1 /*
2  *  summarytaxcommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 9/23/11.
6  *  Copyright 2011 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "summarytaxcommand.h"
11 #include "phylosummary.h"
12
13 //**********************************************************************************************************************
14 vector<string> SummaryTaxCommand::setParameters(){      
15         try {
16                 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(ptaxonomy);
17                 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
18                 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
19                 CommandParameter preftaxonomy("reftaxonomy", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(preftaxonomy);
20                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
21                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
22                 
23                 vector<string> myArray;
24                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
25                 return myArray;
26         }
27         catch(exception& e) {
28                 m->errorOut(e, "SummaryTaxCommand", "setParameters");
29                 exit(1);
30         }
31 }
32 //**********************************************************************************************************************
33 string SummaryTaxCommand::getHelpString(){      
34         try {
35                 string helpString = "";
36                 helpString += "The summary.tax command reads a taxonomy file and an optional name file, and summarizes the taxonomy information.\n";
37                 helpString += "The summary.tax command parameters are taxonomy, group and name. taxonomy is required, unless you have a valid current taxonomy file.\n";
38                 helpString += "The name parameter allows you to enter a name file associated with your taxonomy file. \n";
39                 helpString += "The group parameter allows you add a group file so you can have the summary totals broken up by group.\n";
40                 helpString += "The reftaxonomy parameter allows you give the name of the reference taxonomy file used when you classified your sequences. It is not required, but providing it will keep the rankIDs in the summary file static.\n";
41                 helpString += "The summary.tax command should be in the following format: \n";
42                 helpString += "summary.tax(taxonomy=yourTaxonomyFile) \n";
43                 helpString += "Note: No spaces between parameter labels (i.e. taxonomy), '=' and parameters (i.e.yourTaxonomyFile).\n"; 
44                 return helpString;
45         }
46         catch(exception& e) {
47                 m->errorOut(e, "SummaryTaxCommand", "getHelpString");
48                 exit(1);
49         }
50 }
51
52 //**********************************************************************************************************************
53 SummaryTaxCommand::SummaryTaxCommand(){ 
54         try {
55                 abort = true; calledHelp = true; 
56                 setParameters();
57                 vector<string> tempOutNames;
58                 outputTypes["summary"] = tempOutNames;
59         }
60         catch(exception& e) {
61                 m->errorOut(e, "SummaryTaxCommand", "SummaryTaxCommand");
62                 exit(1);
63         }
64 }
65 //***************************************************************************************************************
66
67 SummaryTaxCommand::SummaryTaxCommand(string option)  {
68         try {
69                 abort = false; calledHelp = false;   
70                 
71                 //allow user to run help
72                 if(option == "help") { help(); abort = true; calledHelp = true; }
73                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
74                 
75                 else {
76                         vector<string> myArray = setParameters();
77                         
78                         OptionParser parser(option);
79                         map<string,string> parameters = parser.getParameters();
80                         
81                         ValidParameters validParameter;
82                         map<string,string>::iterator it;
83                         
84                         //check to make sure all parameters are valid for command
85                         for (it = parameters.begin(); it != parameters.end(); it++) { 
86                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
87                         }
88                         
89                         //if the user changes the input directory command factory will send this info to us in the output parameter 
90                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
91                         if (inputDir == "not found"){   inputDir = "";          }
92                         else {
93                                 string path;
94                                 it = parameters.find("taxonomy");
95                                 //user has given a template file
96                                 if(it != parameters.end()){ 
97                                         path = m->hasPath(it->second);
98                                         //if the user has not given a path then, add inputdir. else leave path alone.
99                                         if (path == "") {       parameters["taxonomy"] = inputDir + it->second;         }
100                                 }
101                                 
102                                 it = parameters.find("name");
103                                 //user has given a template file
104                                 if(it != parameters.end()){ 
105                                         path = m->hasPath(it->second);
106                                         //if the user has not given a path then, add inputdir. else leave path alone.
107                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
108                                 }
109                                 
110                                 it = parameters.find("group");
111                                 //user has given a template file
112                                 if(it != parameters.end()){ 
113                                         path = m->hasPath(it->second);
114                                         //if the user has not given a path then, add inputdir. else leave path alone.
115                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
116                                 }
117                                 
118                                 it = parameters.find("reftaxonomy");
119                                 //user has given a template file
120                                 if(it != parameters.end()){ 
121                                         path = m->hasPath(it->second);
122                                         //if the user has not given a path then, add inputdir. else leave path alone.
123                                         if (path == "") {       parameters["reftaxonomy"] = inputDir + it->second;              }
124                                 }
125                                 
126                         }
127                         
128                         //initialize outputTypes
129                         vector<string> tempOutNames;
130                         outputTypes["summary"] = tempOutNames;
131                         
132                         //check for required parameters
133                         taxfile = validParameter.validFile(parameters, "taxonomy", true);
134                         if (taxfile == "not open") { abort = true; }
135                         else if (taxfile == "not found") {                              
136                                 taxfile = m->getTaxonomyFile(); 
137                                 if (taxfile != "") { m->mothurOut("Using " + taxfile + " as input file for the taxonomy parameter."); m->mothurOutEndLine(); }
138                                 else {  m->mothurOut("You have no current taxonomy file and the taxonomy parameter is required."); m->mothurOutEndLine(); abort = true; }
139                         }else { m->setTaxonomyFile(taxfile); }  
140                         
141                         namefile = validParameter.validFile(parameters, "name", true);
142                         if (namefile == "not open") { namefile = ""; abort = true; }
143                         else if (namefile == "not found") { namefile = "";  }   
144                         else { m->setNameFile(namefile); }
145                         
146                         groupfile = validParameter.validFile(parameters, "group", true);
147                         if (groupfile == "not open") { groupfile = ""; abort = true; }
148                         else if (groupfile == "not found") { groupfile = ""; }
149                         else { m->setGroupFile(groupfile); }
150                         
151                         refTaxonomy = validParameter.validFile(parameters, "reftaxonomy", true);
152                         if (refTaxonomy == "not found") { refTaxonomy = ""; m->mothurOut("reftaxonomy is not required, but if given will keep the rankIDs in the summary file static."); m->mothurOutEndLine(); }
153                         else if (refTaxonomy == "not open") { refTaxonomy = ""; abort = true; }
154                         
155                         //if the user changes the output directory command factory will send this info to us in the output parameter 
156                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
157                                 outputDir = ""; 
158                                 outputDir += m->hasPath(taxfile); //if user entered a file with a path then preserve it 
159                         }
160                         
161                         if (namefile == "") {
162                                 vector<string> files; files.push_back(taxfile);
163                                 parser.getNameFile(files);
164                         }
165                         
166                 }
167         }
168         catch(exception& e) {
169                 m->errorOut(e, "SummaryTaxCommand", "SummaryTaxCommand");
170                 exit(1);
171         }
172 }
173 //***************************************************************************************************************
174
175 int SummaryTaxCommand::execute(){
176         try{
177                 
178                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
179                 int start = time(NULL);
180                 
181                 PhyloSummary* taxaSum;
182                 if (refTaxonomy != "") {
183                         taxaSum = new PhyloSummary(refTaxonomy, groupfile);
184                 }else {
185                         taxaSum = new PhyloSummary(groupfile);
186                 }
187                 
188                 if (m->control_pressed) { delete taxaSum; return 0; }
189                 
190                 int numSeqs = 0;
191                 if (namefile == "") { numSeqs = taxaSum->summarize(taxfile);  }
192                 else {
193                         map<string, vector<string> > nameMap;
194                         map<string, vector<string> >::iterator itNames;
195                         m->readNames(namefile, nameMap);
196                         
197                         if (m->control_pressed) { delete taxaSum; return 0; }
198                         
199                         ifstream in;
200                         m->openInputFile(taxfile, in);
201                         
202                         //read in users taxonomy file and add sequences to tree
203                         string name, taxon;
204                         
205                         while(!in.eof()){
206                                 in >> name >> taxon; m->gobble(in);
207                                 
208                                 itNames = nameMap.find(name);
209                                 
210                                 if (itNames == nameMap.end()) { 
211                                         m->mothurOut("[ERROR]: " + name + " is not in your name file please correct."); m->mothurOutEndLine(); exit(1);
212                                 }else{
213                                         for (int i = 0; i < itNames->second.size(); i++) { 
214                                                 numSeqs++;
215                                                 taxaSum->addSeqToTree(itNames->second[i], taxon);  //add it as many times as there are identical seqs
216                                         }
217                                         itNames->second.clear();
218                                         nameMap.erase(itNames->first);
219                                 }
220                         }
221                         in.close();
222                 }
223                 
224                 if (m->control_pressed) {  delete taxaSum; return 0; }
225                 
226                 //print summary file
227                 ofstream outTaxTree;
228                 string summaryFile = outputDir + m->getRootName(m->getSimpleName(taxfile)) + "tax.summary";
229                 m->openOutputFile(summaryFile, outTaxTree);
230                 taxaSum->print(outTaxTree);
231                 outTaxTree.close();
232                 
233                 delete taxaSum;
234                 
235                 if (m->control_pressed) {  m->mothurRemove(summaryFile); return 0; }
236                 
237                 m->mothurOutEndLine();
238                 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to create the summary file for " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
239                 m->mothurOutEndLine();
240                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
241                 m->mothurOut(summaryFile); m->mothurOutEndLine();       outputNames.push_back(summaryFile); outputTypes["summary"].push_back(summaryFile);
242                 m->mothurOutEndLine();
243                                         
244                 return 0;
245         }
246         catch(exception& e) {
247                 m->errorOut(e, "SummaryTaxCommand", "execute");
248                 exit(1);
249         }
250 }
251 /**************************************************************************************/
252
253