diff --git a/ivy.xml b/ivy.xml index f1655344a..c68ecc8e1 100644 --- a/ivy.xml +++ b/ivy.xml @@ -7,7 +7,6 @@ - diff --git a/java/src/org/broadinstitute/sting/gatk/GenomeAnalysisTK.java b/java/src/org/broadinstitute/sting/gatk/GenomeAnalysisTK.java index 9c19c5cb7..e3e5b3a12 100644 --- a/java/src/org/broadinstitute/sting/gatk/GenomeAnalysisTK.java +++ b/java/src/org/broadinstitute/sting/gatk/GenomeAnalysisTK.java @@ -4,8 +4,6 @@ import edu.mit.broad.picard.reference.ReferenceSequenceFile; import edu.mit.broad.picard.reference.ReferenceSequenceFileFactory; import net.sf.samtools.SAMFileReader; import net.sf.samtools.SAMFileReader.ValidationStringency; -import org.apache.commons.cli.Option; -import org.apache.commons.cli.OptionBuilder; import org.apache.log4j.Logger; import org.broadinstitute.sting.gatk.executive.MicroScheduler; import org.broadinstitute.sting.gatk.refdata.ReferenceOrderedData; diff --git a/java/src/org/broadinstitute/sting/utils/cmdLine/ArgumentParser.java b/java/src/org/broadinstitute/sting/utils/cmdLine/ArgumentParser.java deleted file mode 100644 index 2dc3a88b7..000000000 --- a/java/src/org/broadinstitute/sting/utils/cmdLine/ArgumentParser.java +++ /dev/null @@ -1,617 +0,0 @@ -package org.broadinstitute.sting.utils.cmdLine; - -import org.apache.commons.cli.*; -import org.apache.log4j.Logger; - -import java.lang.reflect.Array; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Modifier; -import java.lang.reflect.ParameterizedType; -import java.util.Arrays; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.HashSet; -import java.util.Set; - -import org.broadinstitute.sting.utils.Pair; - -/** - * User: aaron - * Date: Mar 19, 2009 - * Time: 6:54:15 PM - *

- * The Broad Institute - * SOFTWARE COPYRIGHT NOTICE AGREEMENT - * This software and its documentation are copyright 2009 by the - * Broad Institute/Massachusetts Institute of Technology. All rights are reserved. - *

- * This software is supplied without any warranty or guaranteed support whatsoever. Neither - * the Broad Institute nor MIT can be responsible for its use, misuse, or functionality. - */ -public class ArgumentParser { - - // what program are we parsing for - private String programName; - - // the command-line options received. - private CommandLine cmd; - - // where we eventually want the values to land - private HashMap m_storageLocations = new HashMap(); - - // create Options object - protected Options m_options = new Options(); - - /** - * our log, which we want to capture anything from org.broadinstitute.sting - */ - protected static Logger logger = Logger.getLogger(ArgumentParser.class); - - // the reference to the command line program to fill in - Object prog; - - public ArgumentParser(String programName, Object prog) { - this.programName = programName; - this.prog = prog; - } - - - /** - * print out the help information - */ - public void printHelp() { - // automatically generate the help statement - org.apache.commons.cli.HelpFormatter formatter = new org.apache.commons.cli.HelpFormatter(); - formatter.printHelp(100, - "java -Xmx4096m -jar dist/GenomeAnalysisTK.jar", - "", - m_options, - "", - true); - } - - - /** - * addOptionalArg - *

- * Adds an optional argument to check on the command line - * - * @param name the name of the argument, the long name - * @param letterform the short form - * @param description the description of the argument - * @param fieldname the field to set when we've parsed this option - */ - public void addOptionalArg(String name, String letterform, String description, String fieldname) { - - // we always want the help option to be available - Option opt = OptionBuilder.withLongOpt(name).withArgName(name) - .hasArg() - .withDescription(description) - .create(letterform); - - // add it to the option - AddToOptionStorage(opt, fieldname); - - - } - - public void addOptionalArg(Option opt, String fieldname) { - // add it to the option - AddToOptionStorage(opt, fieldname); - } - - /** - * Used locally to add to the options storage we have, for latter processing - * - * @param opt the option - * @param fieldname what field it should be stuck into on the calling class - */ - private void AddToOptionStorage(Option opt, String fieldname) { - AddToOptionStorage( opt, getField(prog, fieldname) ); - } - - /** - * Used locally to add to the options storage we have, for latter processing - * - * @param opt the option - * @param field what field it should be stuck into on the calling class - */ - private void AddToOptionStorage(Option opt, Field field ) { - // first check to see if we've already added an option with the same name - if (m_options.hasOption( opt.getOpt() )) - throw new IllegalArgumentException(opt.getOpt() + " was already added as an option"); - - // Doesn't make much sense to have a single (ungrouped) required option. Force to unrequired. - if( !opt.hasArg() && opt.isRequired() ) - opt.setRequired(false); - - // add to the option list - m_options.addOption(opt); - - // add the object with it's name to the storage location - m_storageLocations.put( opt.getLongOpt(), field ); - } - - /** - * Used locally to add a group of mutually exclusive options to options storage. - * @param options A list of pairs of param, field to add. - */ - private void AddToOptionStorage( List> options ) { - // Create an option group and mark it 'required'. If any of its constituent parameters - // are NOT required, they'll unset the required bit for the whole group. - OptionGroup optionGroup = new OptionGroup(); - optionGroup.setRequired(true); - - for( Pair option: options ) { - if (m_options.hasOption(option.first.getOpt()) ) - throw new IllegalArgumentException(option.first.getOpt() + " was already added as an option"); - - optionGroup.addOption(option.first); - m_storageLocations.put( option.first.getLongOpt(), option.second ); - optionGroup.setRequired( optionGroup.isRequired() & option.first.isRequired() ); - } - - m_options.addOptionGroup(optionGroup); - } - - private Field getField( Object obj, String fieldName ) { - try { - return obj.getClass().getField(fieldName); - } catch (NoSuchFieldException e) { - logger.fatal("Failed to find the field specified by the fieldname parameter."); - throw new RuntimeException(e.getMessage()); - } - } - - /** - * addRequiredArg - *

- * Adds a required argument to check on the command line - * - * @param name the name of the argument, the long name - * @param letterform the short form - * @param description the description of the argument - * @param fieldname what field it should be stuck into on the calling class - */ - public void addRequiredArg(String name, String letterform, String description, String fieldname) { - // we always want the help option to be available - Option opt = OptionBuilder.isRequired() - .withLongOpt(name) - .withArgName(name) - .hasArg() - .withDescription("(Required Option) " + description) - .create(letterform); - - // add it to the option - AddToOptionStorage( opt, fieldname ); - - } - - /** - * addOptionalArg - *

- * Adds an optional argument to check on the command line - * - * @param name the name of the argument, the long name - * @param letterform the short form - * @param description the description of the argument - * @param fieldname what field it should be stuck into on the calling class - */ - public void addOptionalArgList(String name, String letterform, String description, String fieldname) { - // we always want the help option to be available - Option opt = OptionBuilder.withLongOpt(name).withArgName(name) - .hasArgs() - .withDescription(description) - .create(letterform); - // add it to the option - AddToOptionStorage( opt, fieldname ); - } - - - - /** - * addRequiredArg - *

- * Adds a required argument to check on the command line - * - * @param name the name of the argument, the long name - * @param letterform the short form - * @param description the description of the argument - * @param fieldname what field it should be stuck into on the calling class - */ - public void addRequiredArgList(String name, String letterform, String description, String fieldname) { - - // we always want the help option to be available - Option opt = OptionBuilder.isRequired() - .withLongOpt(name) - .withArgName(name) - .hasArgs() - .withDescription("(Required Option) " + description) - .create(letterform); - // add it to the option - AddToOptionStorage(opt, fieldname); - - } - - /** - * addOptionalFlag - *

- * Adds an optional argument to check on the command line - * - * @param name the name of the argument, the long name - * @param letterform the short form - * @param description the description of the argument - * @param fieldname what field it should be stuck into on the calling class - */ - public void addOptionalFlag(String name, String letterform, String description, String fieldname) { - - // if they've passed a non-Boolean as a object, beat them - try { - if (!(prog.getClass().getField(fieldname).getType() == Boolean.class)) { - throw new IllegalArgumentException("Fields to addOptionalFlag must be of type Boolean"); - } - } catch (NoSuchFieldException e) { - throw new IllegalArgumentException("Fields to addOptionalFlag must exist!"); - } - - // we always want the help option to be available - Option opt = OptionBuilder.withLongOpt(name) - .withDescription(description) - .create(letterform); - - - // add it to the option - AddToOptionStorage( opt, fieldname ); - } - - /** - * This function is called to validate all the arguments to the program. - * If a required Arg isn't found, we generate the help message, and - * exit the program - * - * @param args the command line arguments we recieved - * @param allowIncomplete whether to allow incomplete command-line arguments - */ - public void processArgs(String[] args, boolean allowIncomplete) throws ParseException { - OurPosixParser parser = new OurPosixParser(); - - try { - parser.parse(m_options, args, false); - } - catch( ParseException e ) { - boolean isIncomplete = e instanceof org.apache.commons.cli.MissingArgumentException || - e instanceof MissingOptionException || - e instanceof UnrecognizedOptionException; - - if( !(allowIncomplete && isIncomplete) ) { - logger.warn(e.getMessage()); - throw e; - } - } - - // Apache CLI can ignore unrecognized arguments with a boolean flag, but - // you can't get to the unparsed args. Override PosixParser with a class - // that can reach in and extract the protected command line. - this.cmd = parser.getCmd(); - } - - public void loadArgumentsIntoObject( Object obj ) { - Collection

- * The basic idea of a test workflow in Junit 4 is: - *

- * 1) run the method tagged @BeforeClass - * 2) for each method tagged with @Test { - * run all methods tagged with @Before - * run the @Test tagged method - * run all methods tagged with @After - * 3) run the method tagged with @AfterClass - *

- * You should use the methods like - */ -public class ArgumentParserTest extends BaseTest { - - // our argument parser - private ArgumentParser m_parser = null; - - public Boolean testBool = false; - public String testString = ""; - public Integer testInt = 0; - public Float testFloat = 0.0f; - - /** - * This function (because of the @BeforeClass tag) gets called only once ever, - * before any tests are run - */ - @BeforeClass - public static void doBeforeAnyTests() { - - } - - /** - * Tears down the test fixture after each call. - *

- * Called after every test case method. - */ - @AfterClass - public static void doAfterAllTests() { - - } - - /** - * This function does the setup of our parser, before each method call. - *

- * Called before every test case method. - */ - @Before - public void doForEachTest() { - // we don't need something done to setup each test - - // setup the parser - m_parser = new ArgumentParser("Test Program", this); - m_parser.addOptionalFlag("testBool", "B", "our test bool", "testBool"); - m_parser.addRequiredArg("testString", "S", "our test string", "testString"); - m_parser.addRequiredArg("testInt", "I", "our test int", "testInt"); - m_parser.addRequiredArg("testFloat", "F", "our test float", "testFloat"); - - - } - - /** - * Tears down the test fixture after each call. - *

- * Called after every test case method. - */ - @After - public void undoForEachTest() { - // release objects under test here, if necessary - m_parser = null; - } - - /** - * Tests that we got a string parameter in correctly - */ - @Test - public void testStringParameter() { - logger.warn("Executing testStringParameter"); - // setup the parameter list - String[] params = {"-B", "-S", "String", "-I", "100", "-F", "100.0"}; - - try { - // process the arguments - m_parser.processArgs(params,false); - m_parser.loadArgumentsIntoObject(this); - } catch (ParseException e) { - fail("We received an unexpected parsing exception"); - } - - // assert that none of the parameters are still null - org.junit.Assert.assertNotNull(testString); - assertEquals(testString.equals("String"), true); - - } - - /** - * Tests that we got a Boolean parameter in correctly - */ - @Test - public void testBooleanParameter() { - logger.warn("Executing testBooleanParameter"); - // setup the parameter list - String[] params = {"-B", "-S", "String", "-I", "100", "-F", "100.0"}; - - try { - // process the arguments - m_parser.processArgs(params,false); - m_parser.loadArgumentsIntoObject(this); - } catch (ParseException e) { - fail("We received an unexpected parsing exception"); - } - - assertEquals((boolean) testBool, true); - } - - /** - * Tests that we got a Boolean parameter in correctly - */ - @Test - public void testFloatParameter() { - logger.warn("Executing testFloatParameter"); - - // setup the parameter list - String[] params = {"-B", "-S", "String", "-I", "100", "-F", "100.0"}; - - try { - // process the arguments - m_parser.processArgs(params,false); - m_parser.loadArgumentsIntoObject(this); - } catch (ParseException e) { - fail("We received an unexpected parsing exception"); - } - - assertEquals((testFloat.compareTo(100.0f)), 0); - } - - /** - * Tests that we got a Integer parameter in correctly - */ - @Test - public void testIntegerParameter() { - logger.warn("Executing testIntegerParameter"); - - // setup the parameter list - String[] params = {"-B", "-S", "String", "-I", "100", "-F", "100.0"}; - - try { - // process the arguments - m_parser.processArgs(params,false); - m_parser.loadArgumentsIntoObject(this); - } catch (ParseException e) { - fail("We received an unexpected parsing exception"); - } - - - assertEquals(testInt.compareTo(100), 0); - } - - /** - * Tests that if we dont pass a required parameter we get an exception - */ - @Test - public void testForUnpassedParameter() { - logger.warn("Executing testForUnpassedParameter"); - - - // add a new required flag we won't send it - m_parser.addRequiredArg("testNotTHere", "N", "our should be provided test", "testFloat"); - - // setup the parameter list - String[] params = {"-B", "-S", "String", "-I", "100", "-F", "100.0"}; - - try { - // process the arguments - m_parser.processArgs(params,false); - fail("We should have received a missing argument exception"); - } catch (ParseException e) { - // do nothing but consume the exception - } - } - - /** - * test to see if we pass a bad field name we get an runtime exception - */ - @Test - public void testForBadArgFieldName() { - logger.warn("Executing testForBadArgFieldName"); - - try { - // add a new required flag we won't send it - m_parser.addRequiredArg("testNotTHere", "N", "our should be provided test", "testDoesNotExist"); - fail("We should of recieved a runtime exception, add unavailable fields is Baaad"); - } catch (RuntimeException e) { - // do nothing but consume the exception - } - } - -} -