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