From 5eac510b2f0d922d8cf7e976f8a3cffd9dffd0b2 Mon Sep 17 00:00:00 2001 From: hanna Date: Thu, 10 Dec 2009 18:57:54 +0000 Subject: [PATCH] Refactor the code I gave Eric yesterday to output command line arguments. Convert it from a completely wonky solution to a slightly less wonky solution that will work in more cases. git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@2310 348d0f76-0448-11de-a6fe-93d51630548a --- .../walkers/genotyper/UnifiedGenotyper.java | 10 ++--- .../broadinstitute/sting/utils/JVMUtils.java | 19 ++++++++ .../sting/utils/cmdLine/CommandLineUtils.java | 45 +++++++++++++++++++ .../sting/utils/cmdLine/ParsingEngine.java | 2 +- 4 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 java/src/org/broadinstitute/sting/utils/cmdLine/CommandLineUtils.java diff --git a/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyper.java b/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyper.java index a3ba22b14..168dffd95 100755 --- a/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyper.java +++ b/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyper.java @@ -177,13 +177,9 @@ public class UnifiedGenotyper extends LocusWalker adList = atd.createArgumentDefinitions(new ArgumentSource(field.getType(), field)); - for ( ArgumentDefinition ad : adList ) - headerInfo.add("UG_" + ad.fullName + "=" + JVMUtils.getFieldValue(field, UAC)); - } + Map commandLineArgs = CommandLineUtils.getApproximateCommandLineArguments(Collections.singleton(UAC)); + for(Map.Entry commandLineArg: commandLineArgs.entrySet()) + headerInfo.add(String.format("UG_%s=%s",commandLineArg.getKey(),commandLineArg.getValue())); return headerInfo; } diff --git a/java/src/org/broadinstitute/sting/utils/JVMUtils.java b/java/src/org/broadinstitute/sting/utils/JVMUtils.java index 7ba576a6c..da42ed60b 100755 --- a/java/src/org/broadinstitute/sting/utils/JVMUtils.java +++ b/java/src/org/broadinstitute/sting/utils/JVMUtils.java @@ -4,6 +4,10 @@ import java.lang.reflect.Modifier; import java.lang.reflect.Field; import java.io.File; import java.io.IOException; +import java.util.List; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Arrays; /** * Created by IntelliJ IDEA. @@ -49,6 +53,21 @@ public class JVMUtils { !Modifier.isInterface(clazz.getModifiers()); } + /** + * Retrieve all fields available in this object, regardless of where they are declared or + * whether they're accessible. + * @param type Type to inspect for fields. + * @return A list of all available fields. + */ + public static List getAllFields(Class type) { + List allFields = new ArrayList(); + while( type != null ) { + allFields.addAll(Arrays.asList(type.getDeclaredFields())); + type = type.getSuperclass(); + } + return allFields; + } + /** * Find the field with the given name in the class. Will inspect all fields, independent * of access level. diff --git a/java/src/org/broadinstitute/sting/utils/cmdLine/CommandLineUtils.java b/java/src/org/broadinstitute/sting/utils/cmdLine/CommandLineUtils.java new file mode 100644 index 000000000..069446694 --- /dev/null +++ b/java/src/org/broadinstitute/sting/utils/cmdLine/CommandLineUtils.java @@ -0,0 +1,45 @@ +package org.broadinstitute.sting.utils.cmdLine; + +import org.broadinstitute.sting.utils.JVMUtils; + +import java.util.Map; +import java.util.List; +import java.util.LinkedHashMap; +import java.util.Collection; +import java.lang.reflect.Field; + +/** + * Static utility methods for working with command-line arguments. + * + * @author mhanna + * @version 0.1 + */ +public class CommandLineUtils { + /** + * Returns a key-value mapping of the command-line arguments passed into the GATK. + * Will be approximate; this class doesn't have all the required data to completely + * reconstruct the list of command-line arguments from the given objects. + * + * @param argumentProviders The providers of command-line arguments. + * @return A key-value mapping of argument full names to argument values. Produces best string representation + * possible given the information available. + */ + public static Map getApproximateCommandLineArguments(Collection argumentProviders) { + Map commandLineArguments = new LinkedHashMap(); + + for(Object argumentProvider: argumentProviders) { + List argumentSources = ParsingEngine.extractArgumentSources(argumentProvider.getClass()); + for(ArgumentSource argumentSource: argumentSources) { + Object argumentValue = JVMUtils.getFieldValue(argumentSource.field,argumentProvider); + String argumentValueString = argumentValue != null ? argumentValue.toString() : null; + + for(ArgumentDefinition definition: argumentSource.createArgumentDefinitions()) { + String argumentName = definition.fullName; + commandLineArguments.put(argumentName,argumentValueString); + } + } + } + + return commandLineArguments; + } +} diff --git a/java/src/org/broadinstitute/sting/utils/cmdLine/ParsingEngine.java b/java/src/org/broadinstitute/sting/utils/cmdLine/ParsingEngine.java index 92d31ca25..f75f14da0 100755 --- a/java/src/org/broadinstitute/sting/utils/cmdLine/ParsingEngine.java +++ b/java/src/org/broadinstitute/sting/utils/cmdLine/ParsingEngine.java @@ -298,7 +298,7 @@ public class ParsingEngine { * @param sourceClass class to act as sources for other arguments. * @return A list of sources associated with this object and its aggregated objects. */ - private List extractArgumentSources(Class sourceClass) { + protected static List extractArgumentSources(Class sourceClass) { List argumentSources = new ArrayList(); while( sourceClass != null ) { Field[] fields = sourceClass.getDeclaredFields();