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
This commit is contained in:
hanna 2009-12-10 18:57:54 +00:00
parent 74b8055b6a
commit 5eac510b2f
4 changed files with 68 additions and 8 deletions

View File

@ -177,13 +177,9 @@ public class UnifiedGenotyper extends LocusWalker<Pair<VariationCall, List<Genot
headerInfo.addAll(VCFGenotypeRecord.getSupportedHeaderStrings());
// all of the arguments from the argument collection
Field[] fields = UAC.getClass().getFields();
for ( Field field: fields ) {
ArgumentTypeDescriptor atd = ArgumentTypeDescriptor.create(field.getType());
List<ArgumentDefinition> adList = atd.createArgumentDefinitions(new ArgumentSource(field.getType(), field));
for ( ArgumentDefinition ad : adList )
headerInfo.add("UG_" + ad.fullName + "=" + JVMUtils.getFieldValue(field, UAC));
}
Map<String,String> commandLineArgs = CommandLineUtils.getApproximateCommandLineArguments(Collections.<Object>singleton(UAC));
for(Map.Entry<String,String> commandLineArg: commandLineArgs.entrySet())
headerInfo.add(String.format("UG_%s=%s",commandLineArg.getKey(),commandLineArg.getValue()));
return headerInfo;
}

View File

@ -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<Field> getAllFields(Class type) {
List<Field> allFields = new ArrayList<Field>();
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.

View File

@ -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<String,String> getApproximateCommandLineArguments(Collection<Object> argumentProviders) {
Map<String,String> commandLineArguments = new LinkedHashMap<String,String>();
for(Object argumentProvider: argumentProviders) {
List<ArgumentSource> 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;
}
}

View File

@ -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<ArgumentSource> extractArgumentSources(Class sourceClass) {
protected static List<ArgumentSource> extractArgumentSources(Class sourceClass) {
List<ArgumentSource> argumentSources = new ArrayList<ArgumentSource>();
while( sourceClass != null ) {
Field[] fields = sourceClass.getDeclaredFields();