]> git.donarmstrong.com Git - mothur.git/blob - summarytaxcommand.cpp
Merge remote-tracking branch 'mothur/master'
[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 string SummaryTaxCommand::getOutputFileNameTag(string type, string inputName=""){       
53         try {
54         string outputFileName = "";
55                 map<string, vector<string> >::iterator it;
56         
57         //is this a type this command creates
58         it = outputTypes.find(type);
59         if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
60         else {
61             if (type == "summary")            {   outputFileName =  "tax.summary";   }
62             else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
63         }
64         return outputFileName;
65         }
66         catch(exception& e) {
67                 m->errorOut(e, "SummaryTaxCommand", "getOutputFileNameTag");
68                 exit(1);
69         }
70 }
71 //**********************************************************************************************************************
72 SummaryTaxCommand::SummaryTaxCommand(){ 
73         try {
74                 abort = true; calledHelp = true; 
75                 setParameters();
76                 vector<string> tempOutNames;
77                 outputTypes["summary"] = tempOutNames;
78         }
79         catch(exception& e) {
80                 m->errorOut(e, "SummaryTaxCommand", "SummaryTaxCommand");
81                 exit(1);
82         }
83 }
84 //***************************************************************************************************************
85
86 SummaryTaxCommand::SummaryTaxCommand(string option)  {
87         try {
88                 abort = false; calledHelp = false;   
89                 
90                 //allow user to run help
91                 if(option == "help") { help(); abort = true; calledHelp = true; }
92                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
93                 
94                 else {
95                         vector<string> myArray = setParameters();
96                         
97                         OptionParser parser(option);
98                         map<string,string> parameters = parser.getParameters();
99                         
100                         ValidParameters validParameter;
101                         map<string,string>::iterator it;
102                         
103                         //check to make sure all parameters are valid for command
104                         for (it = parameters.begin(); it != parameters.end(); it++) { 
105                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
106                         }
107                         
108                         //if the user changes the input directory command factory will send this info to us in the output parameter 
109                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
110                         if (inputDir == "not found"){   inputDir = "";          }
111                         else {
112                                 string path;
113                                 it = parameters.find("taxonomy");
114                                 //user has given a template file
115                                 if(it != parameters.end()){ 
116                                         path = m->hasPath(it->second);
117                                         //if the user has not given a path then, add inputdir. else leave path alone.
118                                         if (path == "") {       parameters["taxonomy"] = inputDir + it->second;         }
119                                 }
120                                 
121                                 it = parameters.find("name");
122                                 //user has given a template file
123                                 if(it != parameters.end()){ 
124                                         path = m->hasPath(it->second);
125                                         //if the user has not given a path then, add inputdir. else leave path alone.
126                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
127                                 }
128                                 
129                                 it = parameters.find("group");
130                                 //user has given a template file
131                                 if(it != parameters.end()){ 
132                                         path = m->hasPath(it->second);
133                                         //if the user has not given a path then, add inputdir. else leave path alone.
134                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
135                                 }
136                                 
137                                 it = parameters.find("reftaxonomy");
138                                 //user has given a template file
139                                 if(it != parameters.end()){ 
140                                         path = m->hasPath(it->second);
141                                         //if the user has not given a path then, add inputdir. else leave path alone.
142                                         if (path == "") {       parameters["reftaxonomy"] = inputDir + it->second;              }
143                                 }
144                                 
145                         }
146                         
147                         //initialize outputTypes
148                         vector<string> tempOutNames;
149                         outputTypes["summary"] = tempOutNames;
150                         
151                         //check for required parameters
152                         taxfile = validParameter.validFile(parameters, "taxonomy", true);
153                         if (taxfile == "not open") { abort = true; }
154                         else if (taxfile == "not found") {                              
155                                 taxfile = m->getTaxonomyFile(); 
156                                 if (taxfile != "") { m->mothurOut("Using " + taxfile + " as input file for the taxonomy parameter."); m->mothurOutEndLine(); }
157                                 else {  m->mothurOut("You have no current taxonomy file and the taxonomy parameter is required."); m->mothurOutEndLine(); abort = true; }
158                         }else { m->setTaxonomyFile(taxfile); }  
159                         
160                         namefile = validParameter.validFile(parameters, "name", true);
161                         if (namefile == "not open") { namefile = ""; abort = true; }
162                         else if (namefile == "not found") { namefile = "";  }   
163                         else { m->setNameFile(namefile); }
164                         
165                         groupfile = validParameter.validFile(parameters, "group", true);
166                         if (groupfile == "not open") { groupfile = ""; abort = true; }
167                         else if (groupfile == "not found") { groupfile = ""; }
168                         else { m->setGroupFile(groupfile); }
169                         
170                         refTaxonomy = validParameter.validFile(parameters, "reftaxonomy", true);
171                         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(); }
172                         else if (refTaxonomy == "not open") { refTaxonomy = ""; abort = true; }
173                         
174                         //if the user changes the output directory command factory will send this info to us in the output parameter 
175                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
176                                 outputDir = ""; 
177                                 outputDir += m->hasPath(taxfile); //if user entered a file with a path then preserve it 
178                         }
179                         
180                         if (namefile == "") {
181                                 vector<string> files; files.push_back(taxfile);
182                                 parser.getNameFile(files);
183                         }
184                         
185                 }
186         }
187         catch(exception& e) {
188                 m->errorOut(e, "SummaryTaxCommand", "SummaryTaxCommand");
189                 exit(1);
190         }
191 }
192 //***************************************************************************************************************
193
194 int SummaryTaxCommand::execute(){
195         try{
196                 
197                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
198                 int start = time(NULL);
199                 
200                 PhyloSummary* taxaSum;
201                 if (refTaxonomy != "") {
202                         taxaSum = new PhyloSummary(refTaxonomy, groupfile);
203                 }else {
204                         taxaSum = new PhyloSummary(groupfile);
205                 }
206                 
207                 if (m->control_pressed) { delete taxaSum; return 0; }
208                 
209                 int numSeqs = 0;
210                 if (namefile == "") { numSeqs = taxaSum->summarize(taxfile);  }
211                 else {
212                         map<string, vector<string> > nameMap;
213                         map<string, vector<string> >::iterator itNames;
214                         m->readNames(namefile, nameMap);
215                         
216                         if (m->control_pressed) { delete taxaSum; return 0; }
217                         
218                         ifstream in;
219                         m->openInputFile(taxfile, in);
220                         
221                         //read in users taxonomy file and add sequences to tree
222                         string name, taxon;
223                         
224                         while(!in.eof()){
225                                 in >> name >> taxon; m->gobble(in);
226                                 
227                                 itNames = nameMap.find(name);
228                                 
229                                 if (itNames == nameMap.end()) { 
230                                         m->mothurOut("[ERROR]: " + name + " is not in your name file please correct."); m->mothurOutEndLine(); exit(1);
231                                 }else{
232                                         for (int i = 0; i < itNames->second.size(); i++) { 
233                                                 numSeqs++;
234                                                 taxaSum->addSeqToTree(itNames->second[i], taxon);  //add it as many times as there are identical seqs
235                                         }
236                                         itNames->second.clear();
237                                         nameMap.erase(itNames->first);
238                                 }
239                         }
240                         in.close();
241                 }
242                 
243                 if (m->control_pressed) {  delete taxaSum; return 0; }
244                 
245                 //print summary file
246                 ofstream outTaxTree;
247                 string summaryFile = outputDir + m->getRootName(m->getSimpleName(taxfile)) + getOutputFileNameTag("summary");
248                 m->openOutputFile(summaryFile, outTaxTree);
249                 taxaSum->print(outTaxTree);
250                 outTaxTree.close();
251                 
252                 delete taxaSum;
253                 
254                 if (m->control_pressed) {  m->mothurRemove(summaryFile); return 0; }
255                 
256                 m->mothurOutEndLine();
257                 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to create the summary file for " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
258                 m->mothurOutEndLine();
259                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
260                 m->mothurOut(summaryFile); m->mothurOutEndLine();       outputNames.push_back(summaryFile); outputTypes["summary"].push_back(summaryFile);
261                 m->mothurOutEndLine();
262                                         
263                 return 0;
264         }
265         catch(exception& e) {
266                 m->errorOut(e, "SummaryTaxCommand", "execute");
267                 exit(1);
268         }
269 }
270 /**************************************************************************************/
271
272