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