annotate src/edu/unc/genomics/CommandLineTool.java @ 2:e16016635b2a

Uploaded
author timpalpant
date Mon, 13 Feb 2012 22:12:06 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
1 package edu.unc.genomics;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
2
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
3 import java.io.IOException;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
4
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
5 import com.beust.jcommander.JCommander;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
6 import com.beust.jcommander.ParameterException;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
7
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
8 /**
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
9 * A command-line script
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
10 * @author timpalpant
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
11 *
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
12 */
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
13 public abstract class CommandLineTool {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
14
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
15 /**
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
16 * JCommander command-line argument parser
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
17 */
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
18 private final JCommander jc = new JCommander(this);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
19
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
20 public CommandLineTool() {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
21 // Add factories for parsing Paths, Assemblies, IntervalFiles, and WigFiles
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
22 jc.addConverterFactory(new PathFactory());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
23 jc.addConverterFactory(new AssemblyFactory());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
24 jc.addConverterFactory(new IntervalFileFactory());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
25 jc.addConverterFactory(new WigFileFactory());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
26
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
27 // Set the program name to be the class name
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
28 jc.setProgramName(this.getClass().getSimpleName());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
29 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
30
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
31 /**
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
32 * The default bite-size to use for applications that process files in chunks
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
33 */
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
34 public static final int DEFAULT_CHUNK_SIZE = 500_000;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
35
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
36 /**
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
37 * Do the main computation of this tool
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
38 * @throws IOException
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
39 */
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
40 public abstract void run() throws IOException;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
41
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
42 /**
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
43 * Parse command-line arguments and run the tool
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
44 * Exit on parameter exceptions
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
45 * @param args
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
46 */
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
47 public void instanceMain(String[] args) throws CommandLineToolException {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
48 try {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
49 toolRunnerMain(args);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
50 } catch (ParameterException e) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
51 System.err.println(e.getMessage());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
52 jc.usage();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
53 System.exit(-1);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
54 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
55 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
56
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
57 /**
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
58 * Parse command-line arguments and run the tool
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
59 * @param args
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
60 * @throws ParameterException if there are invalid/missing parameters
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
61 * @throws CommandLineToolException if an exception occurs while running the tool
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
62 */
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
63 public void toolRunnerMain(String[] args) throws ParameterException, CommandLineToolException {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
64 jc.parse(args);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
65
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
66 try {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
67 run();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
68 } catch (IOException e) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
69 e.printStackTrace();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
70 throw new CommandLineToolException("IO error while running tool");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
71 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
72 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
73 }