Search in sources :

Example 1 with CommandLineProgram

use of org.broadinstitute.hellbender.cmdline.CommandLineProgram in project gatk by broadinstitute.

the class AbstractMarkDuplicatesCommandLineProgramTest method testMDOrderImpl.

protected void testMDOrderImpl(final File input, final File expectedOutput, final String extraArgs) throws Exception {
    final File metricsFile = createTempFile("markdups_metrics", ".txt");
    final File outputFile = createTempFile("markdups", ".bam");
    ArgumentsBuilder args = new ArgumentsBuilder();
    args.add("-" + StandardArgumentDefinitions.INPUT_SHORT_NAME);
    args.add(input.getPath());
    args.add("-" + StandardArgumentDefinitions.OUTPUT_SHORT_NAME);
    args.add(outputFile.getAbsolutePath());
    args.add("--METRICS_FILE");
    args.add(metricsFile.getAbsolutePath());
    args.add(extraArgs);
    final CommandLineProgram markDuplicates = getCommandLineProgramInstance();
    markDuplicates.instanceMain(args.getArgsArray());
    SamAssertionUtils.assertEqualBamFiles(outputFile, expectedOutput, false, ValidationStringency.SILENT);
}
Also used : CommandLineProgram(org.broadinstitute.hellbender.cmdline.CommandLineProgram) ArgumentsBuilder(org.broadinstitute.hellbender.utils.test.ArgumentsBuilder) File(java.io.File)

Example 2 with CommandLineProgram

use of org.broadinstitute.hellbender.cmdline.CommandLineProgram in project gatk by broadinstitute.

the class Main method mainEntry.

/**
     * The entry point to the toolkit from commandline: it uses {@link #instanceMain(String[])} to run the command line
     * program and handle the returned object with {@link #handleResult(Object)}, and exit with 0.
     * If any error occurs, it handles the exception (if non-user exception, through {@link #handleNonUserException(Exception)})
     * and exit with the concrete error exit value.
     *
     * Note: this is the only method that is allowed to call System.exit (because gatk tools may be run from test harness etc)
     */
protected final void mainEntry(final String[] args) {
    final CommandLineProgram program = extractCommandLineProgram(args, getPackageList(), getClassList(), getCommandLineName());
    try {
        final Object result = runCommandLineProgram(program, args);
        handleResult(result);
        System.exit(0);
    } catch (final CommandLineException e) {
        System.err.println(program.getUsage());
        handleUserException(e);
        System.exit(COMMANDLINE_EXCEPTION_EXIT_VALUE);
    } catch (final UserException e) {
        handleUserException(e);
        System.exit(USER_EXCEPTION_EXIT_VALUE);
    } catch (final StorageException e) {
        handleStorageException(e);
        System.exit(ANY_OTHER_EXCEPTION_EXIT_VALUE);
    } catch (final Exception e) {
        handleNonUserException(e);
        System.exit(ANY_OTHER_EXCEPTION_EXIT_VALUE);
    }
}
Also used : CommandLineProgram(org.broadinstitute.hellbender.cmdline.CommandLineProgram) UserException(org.broadinstitute.hellbender.exceptions.UserException) StorageException(com.google.cloud.storage.StorageException) CommandLineException(org.broadinstitute.barclay.argparser.CommandLineException) UserException(org.broadinstitute.hellbender.exceptions.UserException) StorageException(com.google.cloud.storage.StorageException) CommandLineException(org.broadinstitute.barclay.argparser.CommandLineException)

Example 3 with CommandLineProgram

use of org.broadinstitute.hellbender.cmdline.CommandLineProgram in project gatk by broadinstitute.

the class Main method extractCommandLineProgram.

/**
     * Returns the command line program specified, or prints the usage and exits with exit code 1 *
     */
private static CommandLineProgram extractCommandLineProgram(final String[] args, final List<String> packageList, final List<Class<? extends CommandLineProgram>> classList, final String commandLineName) {
    /** Get the set of classes that are our command line programs **/
    final ClassFinder classFinder = new ClassFinder();
    for (final String pkg : packageList) {
        classFinder.find(pkg, CommandLineProgram.class);
    }
    String missingAnnotationClasses = "";
    final Set<Class<?>> toCheck = classFinder.getClasses();
    toCheck.addAll(classList);
    final Map<String, Class<?>> simpleNameToClass = new LinkedHashMap<>();
    for (final Class<?> clazz : toCheck) {
        // No interfaces, synthetic, primitive, local, or abstract classes.
        if (ClassUtils.canMakeInstances(clazz)) {
            final CommandLineProgramProperties property = getProgramProperty(clazz);
            // Check for missing annotations
            if (null == property) {
                if (missingAnnotationClasses.isEmpty())
                    missingAnnotationClasses += clazz.getSimpleName();
                else
                    missingAnnotationClasses += ", " + clazz.getSimpleName();
            } else if (!property.omitFromCommandLine()) {
                /** We should check for missing annotations later **/
                if (simpleNameToClass.containsKey(clazz.getSimpleName())) {
                    throw new RuntimeException("Simple class name collision: " + clazz.getSimpleName());
                }
                simpleNameToClass.put(clazz.getSimpleName(), clazz);
            }
        }
    }
    if (!missingAnnotationClasses.isEmpty()) {
        throw new RuntimeException("The following classes are missing the required CommandLineProgramProperties annotation: " + missingAnnotationClasses);
    }
    final Set<Class<?>> classes = new LinkedHashSet<>();
    classes.addAll(simpleNameToClass.values());
    if (args.length < 1) {
        printUsage(classes, commandLineName);
    } else {
        if (args[0].equals("-h") || args[0].equals("--help")) {
            printUsage(classes, commandLineName);
        } else {
            if (simpleNameToClass.containsKey(args[0])) {
                final Class<?> clazz = simpleNameToClass.get(args[0]);
                try {
                    return (CommandLineProgram) clazz.newInstance();
                } catch (final InstantiationException | IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
            printUsage(classes, commandLineName);
            throw new UserException(getUnknownCommandMessage(classes, args[0]));
        }
    }
    return null;
}
Also used : CommandLineProgram(org.broadinstitute.hellbender.cmdline.CommandLineProgram) CommandLineProgramProperties(org.broadinstitute.barclay.argparser.CommandLineProgramProperties) ClassFinder(org.broadinstitute.hellbender.cmdline.ClassFinder) UserException(org.broadinstitute.hellbender.exceptions.UserException)

Example 4 with CommandLineProgram

use of org.broadinstitute.hellbender.cmdline.CommandLineProgram in project gatk by broadinstitute.

the class AbstractMarkDuplicatesCommandLineProgramTest method testDuplicateDetectionDataProviderWithMetrics.

@Test(dataProvider = "testDuplicateDetectionDataProviderWithMetrics")
public void testDuplicateDetectionDataProviderWithMetrics(final File sam, final File expectedMetrics) throws IOException {
    final File outputDir = BaseTest.createTempDir("MarkDups");
    final File outputSam = new File(outputDir, "MarkDups" + ".sam");
    outputSam.deleteOnExit();
    final File metricsFile = new File(outputDir, "MarkDups" + ".duplicate_metrics");
    metricsFile.deleteOnExit();
    final CommandLineProgram markDuplicates = getCommandLineProgramInstance();
    final ArgumentsBuilder args = new ArgumentsBuilder();
    args.add("-I");
    args.add(sam.getAbsolutePath());
    args.add("-O");
    args.add(outputSam.getAbsolutePath());
    args.add("--METRICS_FILE");
    args.add(metricsFile.getAbsolutePath());
    markDuplicates.instanceMain(args.getArgsArray());
    //this compares the values but not headers
    IntegrationTestSpec.assertEqualTextFiles(metricsFile, expectedMetrics, "#");
    //Note: headers need to be compares not by exact values because they include times and class names
    final List<String> lines = FileUtils.readLines(metricsFile);
    Assert.assertTrue(lines.get(0).startsWith("##"), lines.get(0));
    Assert.assertTrue(lines.get(1).startsWith("#"), lines.get(1));
    //Note: lowercase because picard uses INPUT and GATK uses input for full name
    Assert.assertTrue(lines.get(1).toLowerCase().contains("--input"), lines.get(1));
    Assert.assertTrue(lines.get(2).startsWith("##"), lines.get(2));
    Assert.assertTrue(lines.get(3).startsWith("# Started on:"), lines.get(3));
    Assert.assertTrue(lines.get(4).trim().isEmpty());
    Assert.assertTrue(lines.get(4).trim().isEmpty());
    Assert.assertTrue(lines.get(5).startsWith("## METRICS CLASS"), lines.get(5));
}
Also used : CommandLineProgram(org.broadinstitute.hellbender.cmdline.CommandLineProgram) ArgumentsBuilder(org.broadinstitute.hellbender.utils.test.ArgumentsBuilder) File(java.io.File) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

Aggregations

CommandLineProgram (org.broadinstitute.hellbender.cmdline.CommandLineProgram)4 File (java.io.File)2 UserException (org.broadinstitute.hellbender.exceptions.UserException)2 ArgumentsBuilder (org.broadinstitute.hellbender.utils.test.ArgumentsBuilder)2 StorageException (com.google.cloud.storage.StorageException)1 CommandLineException (org.broadinstitute.barclay.argparser.CommandLineException)1 CommandLineProgramProperties (org.broadinstitute.barclay.argparser.CommandLineProgramProperties)1 CommandLineProgramTest (org.broadinstitute.hellbender.CommandLineProgramTest)1 ClassFinder (org.broadinstitute.hellbender.cmdline.ClassFinder)1 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)1 Test (org.testng.annotations.Test)1