]> git.donarmstrong.com Git - mothur.git/blob - readotucommand.cpp
67575561721451bfe172c7235bb172ed24606155
[mothur.git] / readotucommand.cpp
1 /*
2  *  readotu.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 1/20/09.
6  *  Copyright 2009 Schloss Lab UMASS AMherst. All rights reserved.
7  *
8  */
9
10 #include "readotucommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> ReadOtuCommand::getValidParameters(){    
14         try {
15                 string Array[] =  {"list","order","shared","relabund","label","group","sabund", "rabund","groups","ordergroup","outputdir","inputdir"};
16                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
17                 return myArray;
18         }
19         catch(exception& e) {
20                 m->errorOut(e, "ReadOtuCommand", "getValidParameters");
21                 exit(1);
22         }
23 }
24 //**********************************************************************************************************************
25 ReadOtuCommand::ReadOtuCommand(){       
26         try {
27                 abort = true; calledHelp = true; 
28                 vector<string> tempOutNames;
29                 outputTypes["rabund"] = tempOutNames;
30                 outputTypes["shared"] = tempOutNames;
31         }
32         catch(exception& e) {
33                 m->errorOut(e, "ReadOtuCommand", "ReadOtuCommand");
34                 exit(1);
35         }
36 }
37 //**********************************************************************************************************************
38 vector<string> ReadOtuCommand::getRequiredParameters(){ 
39         try {
40                 string Array[] =  {"list","shared","relabund","sabund","rabund","or"};
41                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
42                 return myArray;
43         }
44         catch(exception& e) {
45                 m->errorOut(e, "ChopSeqsCommand", "getRequiredParameters");
46                 exit(1);
47         }
48 }
49 //**********************************************************************************************************************
50 vector<string> ReadOtuCommand::getRequiredFiles(){      
51         try {
52                 vector<string> myArray;
53                 return myArray;
54         }
55         catch(exception& e) {
56                 m->errorOut(e, "ReadOtuCommand", "getRequiredFiles");
57                 exit(1);
58         }
59 }
60 //**********************************************************************************************************************
61 ReadOtuCommand::ReadOtuCommand(string option)  {
62         try {
63                 globaldata = GlobalData::getInstance();
64                 abort = false; calledHelp = false;   
65                 allLines = 1;
66                 
67                 //allow user to run help
68                 if(option == "help") { help(); abort = true; calledHelp = true; }
69                 
70                 else {
71                         //valid paramters for this command
72                         string Array[] =  {"list","order","shared","relabund","label","group","sabund", "rabund","groups","ordergroup","outputdir","inputdir"};
73                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
74                         
75                         OptionParser parser(option);
76                         map<string, string> parameters = parser.getParameters();
77                         
78                         ValidParameters validParameter;
79                         map<string, string>::iterator it;
80                 
81                         //check to make sure all parameters are valid for command
82                         for (it = parameters.begin(); it != parameters.end(); it++) { 
83                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
84                         }
85                         
86                         //initialize outputTypes
87                         vector<string> tempOutNames;
88                         outputTypes["rabund"] = tempOutNames;
89                         outputTypes["shared"] = tempOutNames;
90                         
91                         globaldata->newRead();
92                         
93                         //if the user changes the input directory command factory will send this info to us in the output parameter 
94                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
95                         if (inputDir == "not found"){   inputDir = "";          }
96                         else {
97                                 string path;
98                                 it = parameters.find("list");
99                                 //user has given a template file
100                                 if(it != parameters.end()){ 
101                                         path = m->hasPath(it->second);
102                                         //if the user has not given a path then, add inputdir. else leave path alone.
103                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
104                                 }
105                                 
106                                 it = parameters.find("order");
107                                 //user has given a template file
108                                 if(it != parameters.end()){ 
109                                         path = m->hasPath(it->second);
110                                         //if the user has not given a path then, add inputdir. else leave path alone.
111                                         if (path == "") {       parameters["order"] = inputDir + it->second;            }
112                                 }
113                                 
114                                 it = parameters.find("shared");
115                                 //user has given a template file
116                                 if(it != parameters.end()){ 
117                                         path = m->hasPath(it->second);
118                                         //if the user has not given a path then, add inputdir. else leave path alone.
119                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
120                                 }
121                                 
122                                 it = parameters.find("group");
123                                 //user has given a template file
124                                 if(it != parameters.end()){ 
125                                         path = m->hasPath(it->second);
126                                         //if the user has not given a path then, add inputdir. else leave path alone.
127                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
128                                 }
129                                 
130                                 it = parameters.find("sabund");
131                                 //user has given a template file
132                                 if(it != parameters.end()){ 
133                                         path = m->hasPath(it->second);
134                                         //if the user has not given a path then, add inputdir. else leave path alone.
135                                         if (path == "") {       parameters["sabund"] = inputDir + it->second;           }
136                                 }
137                                 
138                                 it = parameters.find("rabund");
139                                 //user has given a template file
140                                 if(it != parameters.end()){ 
141                                         path = m->hasPath(it->second);
142                                         //if the user has not given a path then, add inputdir. else leave path alone.
143                                         if (path == "") {       parameters["rabund"] = inputDir + it->second;           }
144                                 }
145                                 
146                                 it = parameters.find("ordergroup");
147                                 //user has given a template file
148                                 if(it != parameters.end()){ 
149                                         path = m->hasPath(it->second);
150                                         //if the user has not given a path then, add inputdir. else leave path alone.
151                                         if (path == "") {       parameters["ordergroup"] = inputDir + it->second;               }
152                                 }
153                                 
154                                 it = parameters.find("relabund");
155                                 //user has given a template file
156                                 if(it != parameters.end()){ 
157                                         path = m->hasPath(it->second);
158                                         //if the user has not given a path then, add inputdir. else leave path alone.
159                                         if (path == "") {       parameters["relabund"] = inputDir + it->second;         }
160                                 }
161                         }
162
163                         
164                         //if the user changes the output directory command factory will send this info to us in the output parameter 
165                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = ""; }
166
167                         //check for required parameters
168                         listfile = validParameter.validFile(parameters, "list", true);
169                         if (listfile == "not open") { abort = true; }
170                         else if (listfile == "not found") { listfile = ""; }    
171                         else {  globaldata->setListFile(listfile);  globaldata->setFormat("list");      }
172                         
173                         sabundfile = validParameter.validFile(parameters, "sabund", true);
174                         if (sabundfile == "not open") { abort = true; } 
175                         else if (sabundfile == "not found") { sabundfile = ""; }
176                         else {  globaldata->setSabundFile(sabundfile); globaldata->setFormat("sabund"); }
177
178                         rabundfile = validParameter.validFile(parameters, "rabund", true);
179                         if (rabundfile == "not open") { abort = true; } 
180                         else if (rabundfile == "not found") { rabundfile = ""; }
181                         else {  globaldata->setRabundFile(rabundfile);  globaldata->setFormat("rabund");}
182                         
183                         ordergroupfile = validParameter.validFile(parameters, "ordergroup", true);
184                         if (ordergroupfile == "not open") { abort = true; }     
185                         else if (ordergroupfile == "not found") { ordergroupfile = ""; }
186                         else {  globaldata->setOrderGroupFile(ordergroupfile);  }
187                         
188                         sharedfile = validParameter.validFile(parameters, "shared", true);
189                         if (sharedfile == "not open") { abort = true; } 
190                         else if (sharedfile == "not found") { sharedfile = ""; }
191                         else {  globaldata->setSharedFile(sharedfile); globaldata->setFormat("sharedfile");     }
192                         
193                         relAbundfile = validParameter.validFile(parameters, "relabund", true);
194                         if (relAbundfile == "not open") { abort = true; }       
195                         else if (relAbundfile == "not found") { relAbundfile = ""; }
196                         else {  globaldata->setRelAbundFile(relAbundfile); globaldata->setFormat("relabund");   }
197
198                         
199                         groupfile = validParameter.validFile(parameters, "group", true);
200                         if (groupfile == "not open") { abort = true; }  
201                         else if (groupfile == "not found") { groupfile = ""; }
202                         else {  
203                                 globaldata->setGroupFile(groupfile); 
204                                 groupMap = new GroupMap(groupfile);
205                                 
206                                 int error = groupMap->readMap();
207                                 if (error == 1) { abort = true; }
208                                 
209                                 globaldata->gGroupmap = groupMap;
210                         }
211                         
212                         groups = validParameter.validFile(parameters, "groups", false);                 
213                         if (groups == "not found") { groups = ""; }
214                         else { 
215                                 m->splitAtDash(groups, Groups);
216                                 globaldata->Groups = Groups;
217                         }
218
219                         //you are doing a list and group shared
220                         if ((listfile != "") && (groupfile != "")) { globaldata->setFormat("shared"); }
221                         
222                         //you have not given a file
223                         if ((listfile == "") && (sharedfile == "") && (rabundfile == "") && (sabundfile == "") && (relAbundfile == "")) {
224                                 m->mothurOut("You must enter either a listfile, rabundfile, sabundfile, relabund or a sharedfile with the read.otu command. "); m->mothurOutEndLine(); abort = true; 
225                         }
226                 
227                         //check for optional parameter and set defaults
228                         // ...at some point should added some additional type checking...
229                         label = validParameter.validFile(parameters, "label", false);                   
230                         if (label == "not found") { label = ""; }
231                         else { 
232                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
233                                 else { allLines = 1;  }
234                                 globaldata->labels = labels;
235                         }
236                         
237                         globaldata->allLines = allLines;
238                 
239                         orderfile = validParameter.validFile(parameters, "order", true);
240                         if (orderfile == "not open") { abort = true; }  
241                         else if (orderfile == "not found") { orderfile = ""; }
242                         else {  globaldata->setOrderFile(orderfile);    }
243                         
244                                 
245                         if (abort == false) {
246                                 //gets whichever one of the above is set
247                                 filename = globaldata->inputFileName;
248                         }
249                 }
250
251         }
252         catch(exception& e) {
253                 m->errorOut(e, "ReadOtuCommand", "ReadOtuCommand");
254                 exit(1);
255         }
256 }
257 //**********************************************************************************************************************
258
259 void ReadOtuCommand::help(){
260         try {
261                 m->mothurOut("The read.otu command must be run before you execute a collect.single, rarefaction.single, summary.single, \n");
262                 m->mothurOut("collect.shared, rarefaction.shared, summary.shared heatmap.bin, heatmap.sim or venn command.   Mothur will generate a .list, .rabund and .sabund upon completion of the cluster command \n");
263                 m->mothurOut("or you may use your own. The read.otu command parameter options are list, rabund, sabund, shared, relabund, group, order, ordergroup, label and groups.\n");
264                 m->mothurOut("The read.otu command can be used in two ways.  The first is to read a list, rabund or sabund and run the collect.single, rarefaction.single or summary.single.\n");
265                 m->mothurOut("For this use the read.otu command should be in the following format: read.otu(list=yourListFile, order=yourOrderFile, label=yourLabels).\n");
266                 m->mothurOut("The list, rabund or sabund parameter is required, but you may only use one of them.\n");
267                 m->mothurOut("The label parameter is used to read specific labels in your input.\n");
268                 m->mothurOut("The second way to use the read.otu command is to read a list and a group, or a shared so you can use the collect.shared, rarefaction.shared or summary.shared commands.\n");
269                 m->mothurOut("In this case the read.otu command should be in the following format: read.otu(list=yourListFile, group=yourGroupFile) or read.otu(shared=yourSharedFile).  \n");
270                 m->mothurOut("The list parameter and group paramaters or the shared paremeter is required. When using the command the second way with a list and group file read.otu command parses the .list file\n");
271                 m->mothurOut("and separates it into groups.  It outputs a .shared file containing the OTU information for each group. The read.otu command also outputs a .rabund file for each group. \n");
272                 m->mothurOut("You can use the groups parameter to choose only specific groups to be used in the .shared and .rabund files. \n");
273                 m->mothurOut("You can use the ordergroup parameter to provide a file containing a list of group names in the order you would like them to appear in your shared file. \n");
274                 m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile).\n\n");
275
276         }
277         catch(exception& e) {
278                 m->errorOut(e, "ReadOtuCommand", "help");
279                 exit(1);
280         }
281 }
282
283
284
285 //**********************************************************************************************************************
286 ReadOtuCommand::~ReadOtuCommand(){}
287 //**********************************************************************************************************************
288
289 int ReadOtuCommand::execute(){
290         try {
291         
292                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
293         
294                 if (globaldata->getFormat() == "shared") {
295                         
296                         shared = new SharedCommand(outputDir);
297                         int okay = shared->execute();
298                         
299                         //problem with shared
300                         if (okay == 1) {
301                                 globaldata->setListFile("");
302                                 globaldata->setGroupFile("");
303                                 globaldata->setSharedFile("");
304                         }else { //shared command outputs the filenames
305                                 //m->mothurOutEndLine();
306                                 //m->mothurOut("Output File Name: "); m->mothurOutEndLine();
307                                 //m->mothurOut(globaldata->getSharedFile()); m->mothurOutEndLine();     
308                                 //m->mothurOutEndLine();
309                         }
310                         
311                         outputTypes = shared->getOutputFiles();
312                         
313                         delete shared;
314                 }
315                 return 0;
316         }
317         catch(exception& e) {
318                 m->errorOut(e, "ReadOtuCommand", "execute");
319                 exit(1);
320         }
321 }
322 //**********************************************************************************************************************