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