]> git.donarmstrong.com Git - mothur.git/blob - source/readotucommand.cpp
f39e8df2c382f02451848ca41ec6edb23e07aa49
[mothur.git] / source / 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 //**********************************************************************************************************************
14 ReadOtuCommand::ReadOtuCommand(){       
15         try {
16                 abort = true; calledHelp = true; 
17                 setParameters();
18                 vector<string> tempOutNames;
19                 outputTypes["rabund"] = tempOutNames;
20                 outputTypes["shared"] = tempOutNames;
21         }
22         catch(exception& e) {
23                 m->errorOut(e, "ReadOtuCommand", "ReadOtuCommand");
24                 exit(1);
25         }
26 }
27 //**********************************************************************************************************************
28 ReadOtuCommand::ReadOtuCommand(string option)  {
29         try {
30                 abort = false; calledHelp = false;   
31                 allLines = 1;
32                 
33                 //allow user to run help
34                 if(option == "help") { help(); abort = true; calledHelp = true; }
35                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
36                 
37                 else {
38                         /*
39                         //valid paramters for this command
40                         string Array[] =  {"list","order","shared","relabund","label","group","sabund", "rabund","groups","ordergroup","outputdir","inputdir"};
41                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
42                         
43                         OptionParser parser(option);
44                         map<string, string> parameters = parser.getParameters();
45                         
46                         ValidParameters validParameter;
47                         map<string, string>::iterator it;
48                 
49                         //check to make sure all parameters are valid for command
50                         for (it = parameters.begin(); it != parameters.end(); it++) { 
51                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
52                         }
53                         
54                         //initialize outputTypes
55                         vector<string> tempOutNames;
56                         outputTypes["rabund"] = tempOutNames;
57                         outputTypes["shared"] = tempOutNames;
58                         
59                         globaldata->newRead();
60                         
61                         //if the user changes the input directory command factory will send this info to us in the output parameter 
62                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
63                         if (inputDir == "not found"){   inputDir = "";          }
64                         else {
65                                 string path;
66                                 it = parameters.find("list");
67                                 //user has given a template file
68                                 if(it != parameters.end()){ 
69                                         path = m->hasPath(it->second);
70                                         //if the user has not given a path then, add inputdir. else leave path alone.
71                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
72                                 }
73                                 
74                                 it = parameters.find("order");
75                                 //user has given a template file
76                                 if(it != parameters.end()){ 
77                                         path = m->hasPath(it->second);
78                                         //if the user has not given a path then, add inputdir. else leave path alone.
79                                         if (path == "") {       parameters["order"] = inputDir + it->second;            }
80                                 }
81                                 
82                                 it = parameters.find("shared");
83                                 //user has given a template file
84                                 if(it != parameters.end()){ 
85                                         path = m->hasPath(it->second);
86                                         //if the user has not given a path then, add inputdir. else leave path alone.
87                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
88                                 }
89                                 
90                                 it = parameters.find("group");
91                                 //user has given a template file
92                                 if(it != parameters.end()){ 
93                                         path = m->hasPath(it->second);
94                                         //if the user has not given a path then, add inputdir. else leave path alone.
95                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
96                                 }
97                                 
98                                 it = parameters.find("sabund");
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["sabund"] = inputDir + it->second;           }
104                                 }
105                                 
106                                 it = parameters.find("rabund");
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["rabund"] = inputDir + it->second;           }
112                                 }
113                                 
114                                 it = parameters.find("ordergroup");
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["ordergroup"] = inputDir + it->second;               }
120                                 }
121                                 
122                                 it = parameters.find("relabund");
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["relabund"] = inputDir + it->second;         }
128                                 }
129                         }
130
131                         
132                         //if the user changes the output directory command factory will send this info to us in the output parameter 
133                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = ""; }
134
135                         //check for required parameters
136                         listfile = validParameter.validFile(parameters, "list", true);
137                         if (listfile == "not open") { abort = true; }
138                         else if (listfile == "not found") { listfile = ""; }    
139                         else {  globaldata->setListFile(listfile);  globaldata->setFormat("list");      }
140                         
141                         sabundfile = validParameter.validFile(parameters, "sabund", true);
142                         if (sabundfile == "not open") { abort = true; } 
143                         else if (sabundfile == "not found") { sabundfile = ""; }
144                         else {  globaldata->setSabundFile(sabundfile); globaldata->setFormat("sabund"); }
145
146                         rabundfile = validParameter.validFile(parameters, "rabund", true);
147                         if (rabundfile == "not open") { abort = true; } 
148                         else if (rabundfile == "not found") { rabundfile = ""; }
149                         else {  globaldata->setRabundFile(rabundfile);  globaldata->setFormat("rabund");}
150                         
151                         ordergroupfile = validParameter.validFile(parameters, "ordergroup", true);
152                         if (ordergroupfile == "not open") { abort = true; }     
153                         else if (ordergroupfile == "not found") { ordergroupfile = ""; }
154                         else {  globaldata->setOrderGroupFile(ordergroupfile);  }
155                         
156                         sharedfile = validParameter.validFile(parameters, "shared", true);
157                         if (sharedfile == "not open") { abort = true; } 
158                         else if (sharedfile == "not found") { sharedfile = ""; }
159                         else {  globaldata->setSharedFile(sharedfile); globaldata->setFormat("sharedfile");     }
160                         
161                         relAbundfile = validParameter.validFile(parameters, "relabund", true);
162                         if (relAbundfile == "not open") { abort = true; }       
163                         else if (relAbundfile == "not found") { relAbundfile = ""; }
164                         else {  globaldata->setRelAbundFile(relAbundfile); globaldata->setFormat("relabund");   }
165
166                         
167                         groupfile = validParameter.validFile(parameters, "group", true);
168                         if (groupfile == "not open") { abort = true; }  
169                         else if (groupfile == "not found") { groupfile = ""; }
170                         else {  
171                                 globaldata->setGroupFile(groupfile); 
172                                 groupMap = new GroupMap(groupfile);
173                                 
174                                 int error = groupMap->readMap();
175                                 if (error == 1) { abort = true; }
176                                 
177                                 globaldata->gGroupmap = groupMap;
178                         }
179                         
180                         groups = validParameter.validFile(parameters, "groups", false);                 
181                         if (groups == "not found") { groups = ""; }
182                         else { 
183                                 m->splitAtDash(groups, Groups);
184                                 globaldata->Groups = Groups;
185                         }
186
187                         //you are doing a list and group shared
188                         if ((listfile != "") && (groupfile != "")) { globaldata->setFormat("shared"); }
189                         
190                         //you have not given a file
191                         if ((listfile == "") && (sharedfile == "") && (rabundfile == "") && (sabundfile == "") && (relAbundfile == "")) {
192                                 m->mothurOut("You must enter either a listfile, rabundfile, sabundfile, relabund or a sharedfile with the read.otu command. "); m->mothurOutEndLine(); abort = true; 
193                         }
194                 
195                         //check for optional parameter and set defaults
196                         // ...at some point should added some additional type checking...
197                         label = validParameter.validFile(parameters, "label", false);                   
198                         if (label == "not found") { label = ""; }
199                         else { 
200                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
201                                 else { allLines = 1;  }
202                                 globaldata->labels = labels;
203                         }
204                         
205                         globaldata->allLines = allLines;
206                 
207                         orderfile = validParameter.validFile(parameters, "order", true);
208                         if (orderfile == "not open") { abort = true; }  
209                         else if (orderfile == "not found") { orderfile = ""; }
210                         else {  globaldata->setOrderFile(orderfile);    }
211                         
212                                 
213                         if (abort == false) {
214                                 //gets whichever one of the above is set
215                                 filename = globaldata->inputFileName;
216                         }
217                          */
218                 }
219
220         }
221         catch(exception& e) {
222                 m->errorOut(e, "ReadOtuCommand", "ReadOtuCommand");
223                 exit(1);
224         }
225 }
226 ///**********************************************************************************************************************
227
228 int ReadOtuCommand::execute(){
229         try {
230         
231                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
232                 m->mothurOut(getHelpString()); m->mothurOutEndLine();
233         
234                 /*
235                 if (globaldata->getFormat() == "shared") {
236                         
237                         shared = new SharedCommand(outputDir);
238                         int okay = shared->execute();
239                         
240                         //problem with shared
241                         if (okay == 1) {
242                                 globaldata->setListFile("");
243                                 globaldata->setGroupFile("");
244                                 globaldata->setSharedFile("");
245                         }else { //shared command outputs the filenames
246                                 //m->mothurOutEndLine();
247                                 //m->mothurOut("Output File Name: "); m->mothurOutEndLine();
248                                 //m->mothurOut(globaldata->getSharedFile()); m->mothurOutEndLine();     
249                                 //m->mothurOutEndLine();
250                         }
251                         
252                         outputTypes = shared->getOutputFiles();
253                         
254                         //set rabund file as new current rabundfile
255                         string current = "";
256                         itTypes = outputTypes.find("rabund");
257                         if (itTypes != outputTypes.end()) {
258                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
259                         }
260                         
261                         itTypes = outputTypes.find("shared");
262                         if (itTypes != outputTypes.end()) {
263                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
264                         }                       
265                         
266                         delete shared;
267                 }
268                 */
269                                 
270                 return 0;
271         }
272         catch(exception& e) {
273                 m->errorOut(e, "ReadOtuCommand", "execute");
274                 exit(1);
275         }
276 }
277 //**********************************************************************************************************************