annotate java-genomics-toolkit/gui/edu/unc/genomics/Job.java @ 0:1daf3026d231

Upload alpha version
author timpalpant
date Mon, 13 Feb 2012 21:55:55 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
1 package edu.unc.genomics;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
2
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
3 import java.io.StringWriter;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
4 import java.util.ArrayList;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
5 import java.util.HashMap;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
6 import java.util.Iterator;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
7 import java.util.List;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
8 import java.util.Map;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
9
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
10 import org.apache.log4j.Logger;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
11 import org.apache.log4j.PatternLayout;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
12 import org.apache.log4j.WriterAppender;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
13
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
14 import com.beust.jcommander.JCommander;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
15 import com.beust.jcommander.ParameterDescription;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
16
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
17 /**
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
18 * A Job represents an instance of a tool configured with specific arguments
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
19 * Attempting to run a Job with invalid arguments will throw a RuntimeException
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
20 * If tool execution fails, a RuntimeException will also be thrown
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
21 * UncheckedExceptions should be managed by setting the Job's UncheckedExceptionManager
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
22 * or providing a ThreadGroup / default UncheckedExceptionManager
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
23 *
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
24 * @author timpalpant
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
25 *
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
26 */
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
27 public class Job implements Iterable<ParameterDescription>, Runnable {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
28
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
29 private static final Logger log = Logger.getLogger(Job.class);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
30
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
31 private final Class<? extends CommandLineTool> tool;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
32 private final CommandLineTool app;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
33 private final List<ParameterDescription> parameters;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
34 private final String usageText;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
35 private boolean isRunning = false;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
36 private StringWriter writer = new StringWriter();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
37
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
38 /**
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
39 * Arguments for running this Job
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
40 */
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
41 private Map<ParameterDescription,String> args = new HashMap<>();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
42
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
43 /**
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
44 * Creates a new Job model for the specified tool
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
45 * @param tool
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
46 * @throws IllegalAccessException
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
47 * @throws InstantiationException
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
48 */
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
49 public Job(final Class<? extends CommandLineTool> tool) throws InstantiationException, IllegalAccessException {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
50 this.tool = tool;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
51
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
52 // Attempt to instantiate the tool and extract parameter information
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
53 app = tool.newInstance();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
54 JCommander jc = new JCommander(app);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
55 jc.setProgramName(tool.getSimpleName());
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
56 parameters = jc.getParameters();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
57 StringBuilder sbuilder = new StringBuilder();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
58 jc.usage(sbuilder);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
59 usageText = sbuilder.toString();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
60
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
61 // Set default arguments
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
62 for (ParameterDescription param : parameters) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
63 if (param.getDefault() != null) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
64 setArgument(param, String.valueOf(param.getDefault()));
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
65 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
66 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
67 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
68
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
69 /**
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
70 * Copy-constructor
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
71 * @param job
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
72 */
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
73 public Job(final Job job) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
74 this.tool = job.tool;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
75 this.app = job.app;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
76 this.parameters = job.parameters;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
77 this.args = job.args;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
78 this.usageText = job.usageText;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
79 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
80
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
81 @Override
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
82 public void run() {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
83 // Load the arguments for running the tool
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
84 String[] args;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
85 try {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
86 args = getArguments();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
87 } catch (JobException e) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
88 // TODO Auto-generated catch block
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
89 e.printStackTrace();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
90 throw new CommandLineToolException("Job arguments are not valid");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
91 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
92
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
93 // Attempt to instantiate and run the tool
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
94 WriterAppender appender = new WriterAppender(new PatternLayout(), writer);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
95 appender.addFilter(new ThreadFilter(Thread.currentThread().getName()));
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
96 Logger.getRootLogger().addAppender(appender);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
97 isRunning = true;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
98 app.toolRunnerMain(args);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
99 isRunning = false;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
100 Logger.getRootLogger().removeAppender(appender);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
101 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
102
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
103 /**
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
104 * Render the arguments for running an instance of this Job
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
105 * @return
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
106 * @throws JobException
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
107 */
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
108 public String[] getArguments() throws JobException {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
109 if (!validateArguments()) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
110 throw new JobException("Job Arguments are not valid");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
111 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
112
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
113 List<String> cmdArgs = new ArrayList<>();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
114 for (ParameterDescription p : args.keySet()) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
115 cmdArgs.add(p.getLongestName());
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
116 cmdArgs.add(args.get(p));
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
117 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
118
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
119 String[] ret = new String[cmdArgs.size()];
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
120 return cmdArgs.toArray(ret);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
121 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
122
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
123 public String getArgument(final ParameterDescription p) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
124 return args.get(p);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
125 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
126
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
127 /**
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
128 * Set a value for the given parameter
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
129 * @param p
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
130 * @param value
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
131 */
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
132 public void setArgument(final ParameterDescription p, final String value) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
133 if (value.length() == 0) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
134 args.remove(p);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
135 } else {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
136 args.put(p, value);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
137 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
138 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
139
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
140 /**
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
141 * Remove all set arguments for this Job
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
142 */
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
143 public void resetArguments() {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
144 args.clear();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
145 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
146
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
147 /**
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
148 * Is this parameter set?
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
149 * @param p
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
150 * @return
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
151 */
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
152 public boolean isSet(final ParameterDescription p) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
153 return args.containsKey(p);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
154 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
155
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
156 /**
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
157 * Validate that this job has all of its parameters set
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
158 * and that they are all valid
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
159 * @return
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
160 */
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
161 public boolean validateArguments() {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
162 // TODO: Better validation based on parameter type
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
163 boolean hasAllRequiredParams = true;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
164 for (ParameterDescription param : parameters) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
165 if (param.getParameter().required() && !isSet(param)) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
166 log.debug("Job is missing required argument: " + param.getLongestName());
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
167 hasAllRequiredParams = false;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
168 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
169 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
170
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
171 return hasAllRequiredParams;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
172 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
173
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
174 public int numParameters() {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
175 return parameters.size();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
176 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
177
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
178 @Override
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
179 public Iterator<ParameterDescription> iterator() {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
180 return parameters.iterator();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
181 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
182
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
183 public String getName() {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
184 return tool.getSimpleName();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
185 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
186
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
187 public boolean isRunning() {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
188 return isRunning;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
189 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
190
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
191 /**
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
192 * @return the usageText
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
193 */
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
194 public String getUsageText() {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
195 return usageText;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
196 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
197
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
198 @Override
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
199 public String toString() {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
200 return getName();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
201 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
202
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
203 public String getOutput() {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
204 return writer.toString();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
205 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
206
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
207 }