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