PageRenderTime 91ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/picard/cmdline/CommandLineProgram.java

https://gitlab.com/zhuravelchik/picard
Java | 333 lines | 192 code | 44 blank | 97 comment | 31 complexity | e0b93a37c4c1acddb913c22a12ad978e MD5 | raw file
  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2009 The Broad Institute
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package picard.cmdline;
  25. import htsjdk.samtools.Defaults;
  26. import htsjdk.samtools.SAMFileWriterFactory;
  27. import htsjdk.samtools.SAMFileWriterImpl;
  28. import htsjdk.samtools.SamReaderFactory;
  29. import htsjdk.samtools.ValidationStringency;
  30. import htsjdk.samtools.metrics.Header;
  31. import htsjdk.samtools.metrics.MetricBase;
  32. import htsjdk.samtools.metrics.MetricsFile;
  33. import htsjdk.samtools.metrics.StringHeader;
  34. import htsjdk.samtools.util.BlockCompressedOutputStream;
  35. import htsjdk.samtools.util.BlockCompressedStreamConstants;
  36. import htsjdk.samtools.util.IOUtil;
  37. import htsjdk.samtools.util.Log;
  38. import htsjdk.samtools.util.zip.DeflaterFactory;
  39. import htsjdk.variant.variantcontext.writer.Options;
  40. import htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder;
  41. import java.io.File;
  42. import java.lang.annotation.Annotation;
  43. import java.lang.reflect.Field;
  44. import java.net.InetAddress;
  45. import java.text.DecimalFormat;
  46. import java.util.ArrayList;
  47. import java.util.Date;
  48. import java.util.List;
  49. import java.util.Map;
  50. /**
  51. * Abstract class to facilitate writing command-line programs.
  52. *
  53. * To use:
  54. *
  55. * 1. Extend this class with a concrete class that has data members annotated with @Option, @PositionalArguments
  56. * and/or @Usage annotations.
  57. *
  58. * 2. If there is any custom command-line validation, override customCommandLineValidation(). When this method is
  59. * called, the command line has been parsed and set into the data members of the concrete class.
  60. *
  61. * 3. Implement a method doWork(). This is called after successful command-line processing. The value it returns is
  62. * the exit status of the program. It is assumed that the concrete class emits any appropriate error message before
  63. * returning non-zero. doWork() may throw unchecked exceptions, which are caught and reported appropriately.
  64. *
  65. * 4. Implement the following static method in the concrete class:
  66. *
  67. * public static void main(String[] argv) {
  68. new MyConcreteClass().instanceMain(argv);
  69. }
  70. */
  71. public abstract class CommandLineProgram {
  72. @Option(common=true, optional=true)
  73. public List<File> TMP_DIR = new ArrayList<File>();
  74. @Option(doc = "Control verbosity of logging.", common=true)
  75. public Log.LogLevel VERBOSITY = Log.LogLevel.INFO;
  76. @Option(doc = "Whether to suppress job-summary info on System.err.", common=true)
  77. public Boolean QUIET = false;
  78. @Option(doc = "Validation stringency for all SAM files read by this program. Setting stringency to SILENT " +
  79. "can improve performance when processing a BAM file in which variable-length data (read, qualities, tags) " +
  80. "do not otherwise need to be decoded.", common=true)
  81. public ValidationStringency VALIDATION_STRINGENCY = ValidationStringency.DEFAULT_STRINGENCY;
  82. @Option(doc = "Compression level for all compressed files created (e.g. BAM and GELI).", common=true)
  83. public int COMPRESSION_LEVEL = BlockCompressedStreamConstants.DEFAULT_COMPRESSION_LEVEL;
  84. @Option(doc = "When writing SAM files that need to be sorted, this will specify the number of records stored in RAM before spilling to disk. Increasing this number reduces the number of file handles needed to sort a SAM file, and increases the amount of RAM needed.", optional=true, common=true)
  85. public Integer MAX_RECORDS_IN_RAM = SAMFileWriterImpl.getDefaultMaxRecordsInRam();
  86. @Option(doc = "Whether to create a BAM index when writing a coordinate-sorted BAM file.", common=true)
  87. public Boolean CREATE_INDEX = Defaults.CREATE_INDEX;
  88. @Option(doc="Whether to create an MD5 digest for any BAM or FASTQ files created. ", common=true)
  89. public boolean CREATE_MD5_FILE = Defaults.CREATE_MD5;
  90. @Option(shortName = StandardOptionDefinitions.REFERENCE_SHORT_NAME, doc = "Reference sequence file.", common = true, optional = true, overridable = true)
  91. public File REFERENCE_SEQUENCE = Defaults.REFERENCE_FASTA;
  92. @Option(doc="Google Genomics API client_secrets.json file path.", common = true)
  93. public String GA4GH_CLIENT_SECRETS="client_secrets.json";
  94. private final String standardUsagePreamble = CommandLineParser.getStandardUsagePreamble(getClass());
  95. static {
  96. // Register custom reader factory for reading data from Google Genomics
  97. // implementation of GA4GH API.
  98. // With this it will be possible to pass these urls as INPUT params.
  99. // E.g. java -jar dist/picard.jar ViewSam \
  100. // INPUT=https://www.googleapis.com/genomics/v1beta2/readgroupsets/CK256frpGBD44IWHwLP22R4/ \
  101. // GA4GH_CLIENT_SECRETS=../client_secrets.json
  102. if (System.getProperty("samjdk.custom_reader") == null) {
  103. System.setProperty("samjdk.custom_reader",
  104. "https://www.googleapis.com/genomics," +
  105. "com.google.cloud.genomics.gatk.htsjdk.GA4GHReaderFactory");
  106. }
  107. }
  108. /**
  109. * Initialized in parseArgs. Subclasses may want to access this to do their
  110. * own validation, and then print usage using commandLineParser.
  111. */
  112. private CommandLineParser commandLineParser;
  113. private final List<Header> defaultHeaders = new ArrayList<Header>();
  114. /**
  115. * The reconstructed commandline used to run this program. Used for logging
  116. * and debugging.
  117. */
  118. private String commandLine;
  119. /**
  120. * Do the work after command line has been parsed. RuntimeException may be
  121. * thrown by this method, and are reported appropriately.
  122. * @return program exit status.
  123. */
  124. protected abstract int doWork();
  125. public void instanceMainWithExit(final String[] argv) {
  126. System.exit(instanceMain(argv));
  127. }
  128. public int instanceMain(final String[] argv) {
  129. if (!parseArgs(argv)) {
  130. return 1;
  131. }
  132. // Provide one temp directory if the caller didn't
  133. if (this.TMP_DIR == null) this.TMP_DIR = new ArrayList<File>();
  134. if (this.TMP_DIR.isEmpty()) TMP_DIR.add(IOUtil.getDefaultTmpDir());
  135. // Build the default headers
  136. final Date startDate = new Date();
  137. this.defaultHeaders.add(new StringHeader(commandLine));
  138. this.defaultHeaders.add(new StringHeader("Started on: " + startDate));
  139. Log.setGlobalLogLevel(VERBOSITY);
  140. if (System.getProperty("ga4gh.client_secrets") == null) {
  141. System.setProperty("ga4gh.client_secrets", GA4GH_CLIENT_SECRETS);
  142. }
  143. SamReaderFactory.setDefaultValidationStringency(VALIDATION_STRINGENCY);
  144. BlockCompressedOutputStream.setDefaultCompressionLevel(COMPRESSION_LEVEL);
  145. if (VALIDATION_STRINGENCY != ValidationStringency.STRICT) VariantContextWriterBuilder.setDefaultOption(Options.ALLOW_MISSING_FIELDS_IN_HEADER);
  146. if (MAX_RECORDS_IN_RAM != null) {
  147. SAMFileWriterImpl.setDefaultMaxRecordsInRam(MAX_RECORDS_IN_RAM);
  148. }
  149. if (CREATE_INDEX){
  150. SAMFileWriterFactory.setDefaultCreateIndexWhileWriting(true);
  151. }
  152. SAMFileWriterFactory.setDefaultCreateMd5File(CREATE_MD5_FILE);
  153. for (final File f : TMP_DIR) {
  154. // Intentially not checking the return values, because it may be that the program does not
  155. // need a tmp_dir. If this fails, the problem will be discovered downstream.
  156. if (!f.exists()) f.mkdirs();
  157. f.setReadable(true, false);
  158. f.setWritable(true, false);
  159. System.setProperty("java.io.tmpdir", f.getAbsolutePath()); // in loop so that last one takes effect
  160. }
  161. if (!QUIET) {
  162. System.err.println("[" + new Date() + "] " + commandLine);
  163. // Output a one liner about who/where and what software/os we're running on
  164. try {
  165. System.err.println("[" + new Date() + "] Executing as " +
  166. System.getProperty("user.name") + "@" + InetAddress.getLocalHost().getHostName() +
  167. " on " + System.getProperty("os.name") + " " + System.getProperty("os.version") +
  168. " " + System.getProperty("os.arch") + "; " + System.getProperty("java.vm.name") +
  169. " " + System.getProperty("java.runtime.version") +
  170. "; Picard version: " + commandLineParser.getVersion());
  171. }
  172. catch (Exception e) { /* Unpossible! */ }
  173. }
  174. int ret = -1;
  175. try {
  176. ret = doWork();
  177. } finally {
  178. try {
  179. // Emit the time even if program throws
  180. if (!QUIET) {
  181. final Date endDate = new Date();
  182. final double elapsedMinutes = (endDate.getTime() - startDate.getTime()) / (1000d * 60d);
  183. final String elapsedString = new DecimalFormat("#,##0.00").format(elapsedMinutes);
  184. System.err.println("[" + endDate + "] " + getClass().getName() + " done. Elapsed time: " + elapsedString + " minutes.");
  185. System.err.println("Runtime.totalMemory()=" + Runtime.getRuntime().totalMemory());
  186. if (ret != 0 && CommandLineParser.hasWebDocumentation(this.getClass())) System.err.println(CommandLineParser.getFaqLink());
  187. }
  188. }
  189. catch (Throwable e) {
  190. // do nothing
  191. }
  192. }
  193. return ret;
  194. }
  195. /**
  196. * Put any custom command-line validation in an override of this method.
  197. * clp is initialized at this point and can be used to print usage and access argv.
  198. * Any options set by command-line parser can be validated.
  199. * @return null if command line is valid. If command line is invalid, returns an array of error message
  200. * to be written to the appropriate place.
  201. */
  202. protected String[] customCommandLineValidation() {
  203. final List<String> ret = new ArrayList<String>();
  204. for (final Object childOptionsObject : getNestedOptions().values()) {
  205. if (childOptionsObject instanceof CommandLineProgram) {
  206. final CommandLineProgram childClp = (CommandLineProgram)childOptionsObject;
  207. final String[] childErrors = childClp.customCommandLineValidation();
  208. if (childErrors != null) {
  209. for (final String error: childErrors) {
  210. ret.add(error);
  211. }
  212. }
  213. }
  214. }
  215. if (!ret.isEmpty()) {
  216. ret.toArray(new String[ret.size()]);
  217. }
  218. return null;
  219. }
  220. /**
  221. *
  222. * @return true if command line is valid
  223. */
  224. protected boolean parseArgs(final String[] argv) {
  225. commandLineParser = new CommandLineParser(this);
  226. final boolean ret = commandLineParser.parseOptions(System.err, argv);
  227. commandLine = commandLineParser.getCommandLine();
  228. if (!ret) {
  229. return false;
  230. }
  231. final String[] customErrorMessages = customCommandLineValidation();
  232. if (customErrorMessages != null) {
  233. for (final String msg : customErrorMessages) {
  234. System.err.println(msg);
  235. }
  236. commandLineParser.usage(System.err, false);
  237. return false;
  238. }
  239. return true;
  240. }
  241. /** Gets a MetricsFile with default headers already written into it. */
  242. protected <A extends MetricBase,B extends Comparable<?>> MetricsFile<A,B> getMetricsFile() {
  243. final MetricsFile<A,B> file = new MetricsFile<A,B>();
  244. for (final Header h : this.defaultHeaders) {
  245. file.addHeader(h);
  246. }
  247. return file;
  248. }
  249. public String getStandardUsagePreamble() {
  250. return standardUsagePreamble;
  251. }
  252. public CommandLineParser getCommandLineParser() {
  253. return commandLineParser;
  254. }
  255. /**
  256. * @return Version stored in the manifest of the jarfile.
  257. */
  258. public String getVersion() {
  259. return getCommandLineParser().getVersion();
  260. }
  261. public String getCommandLine() {
  262. return commandLine;
  263. }
  264. public void setDefaultHeaders(final List<Header> headers) {
  265. this.defaultHeaders.clear();
  266. this.defaultHeaders.addAll(headers);
  267. }
  268. public List<Header> getDefaultHeaders() {
  269. return this.defaultHeaders;
  270. }
  271. /**
  272. * @return Map of nested options, where the key is the prefix to be used when specifying Options inside of a nested
  273. * options object, and the value is the nested options object itself. Default implementation is to return a
  274. * map of all the fields annotated with @NestedOptions, with key being the field name.
  275. */
  276. public Map<String, Object> getNestedOptions() {
  277. return CommandLineParser.getNestedOptions(this);
  278. }
  279. /**
  280. * @return Map of nested options, where the key is the prefix to be used when specifying Options inside of a nested
  281. * options object, and the value is the nested options object itself, for the purpose of generating help.
  282. * Default implementation is to return the same map as getNestedOptions().
  283. */
  284. public Map<String, Object> getNestedOptionsForHelp() {
  285. return getNestedOptions();
  286. }
  287. }