Cleaned up some duplicate code in preparation for making plugin dir configurable.

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@1270 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
hanna 2009-07-16 22:02:21 +00:00
parent 31f3f466ca
commit 7c30c30d26
4 changed files with 66 additions and 159 deletions

View File

@ -1,7 +1,8 @@
package org.broadinstitute.sting.gatk; package org.broadinstitute.sting.gatk;
import org.broadinstitute.sting.utils.cmdLine.CommandLineProgram; import org.broadinstitute.sting.utils.cmdLine.CommandLineProgram;
import org.broadinstitute.sting.utils.cmdLine.ArgumentException; import org.broadinstitute.sting.utils.cmdLine.ArgumentFactory;
import org.broadinstitute.sting.utils.cmdLine.ArgumentCollection;
import org.broadinstitute.sting.utils.StingException; import org.broadinstitute.sting.utils.StingException;
import org.broadinstitute.sting.utils.xReadLines; import org.broadinstitute.sting.utils.xReadLines;
import org.broadinstitute.sting.gatk.walkers.Walker; import org.broadinstitute.sting.gatk.walkers.Walker;
@ -11,6 +12,8 @@ import java.io.FileNotFoundException;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import net.sf.samtools.SAMFileReader;
/* /*
* Copyright (c) 2009 The Broad Institute * Copyright (c) 2009 The Broad Institute
@ -44,20 +47,16 @@ import java.util.ArrayList;
*/ */
public abstract class CommandLineExecutable extends CommandLineProgram { public abstract class CommandLineExecutable extends CommandLineProgram {
// our argument collection, the collection of command line args we accept
protected GATKArgumentCollection argCollection = new GATKArgumentCollection();
/** the type of analysis to run - switch to the type of analysis */
private String analysisName = "SomaticCoverageWalker";
// our genome analysis engine // our genome analysis engine
private GenomeAnalysisEngine GATKEngine = new GenomeAnalysisEngine(); private GenomeAnalysisEngine GATKEngine = new GenomeAnalysisEngine();
// get the analysis name // get the analysis name
protected abstract String getAnalysisName(); protected abstract String getAnalysisName();
protected abstract GATKArgumentCollection getArgumentCollection();
// override select arguments // override select arguments
protected abstract void overrideArguments(); protected void overrideArguments() { }
/** /**
* this is the function that the inheriting class can expect to have called * this is the function that the inheriting class can expect to have called
@ -66,18 +65,20 @@ public abstract class CommandLineExecutable extends CommandLineProgram {
* @return the return code to exit the program with * @return the return code to exit the program with
*/ */
protected int execute() { protected int execute() {
Walker<?,?> mWalker = GATKEngine.getWalkerByName(analysisName); Walker<?,?> mWalker = GATKEngine.getWalkerByName(getAnalysisName());
GATKArgumentCollection arguments = getArgumentCollection();
// load the arguments into the walkers // load the arguments into the walkers
loadArgumentsIntoObject(argCollection); loadArgumentsIntoObject(arguments);
loadArgumentsIntoObject(mWalker); loadArgumentsIntoObject(mWalker);
// process any arguments that need a second pass // process any arguments that need a second pass
processArguments(argCollection); processArguments(arguments);
// set the analysis name in the argument collection // set the analysis name in the argument collection
this.argCollection.analysisName = this.analysisName; arguments.analysisName = getAnalysisName();
GATKEngine.execute(argCollection, mWalker); GATKEngine.execute(arguments, mWalker);
return 0; return 0;
} }
@ -93,17 +94,20 @@ public abstract class CommandLineExecutable extends CommandLineProgram {
} }
/** /**
* GATK provides the walker as an argument source. As a side-effect, initializes the walker variable. * GATK provides the walker as an argument source.
*
* @return List of walkers to load dynamically. * @return List of walkers to load dynamically.
*/ */
@Override @Override
protected Class[] getArgumentSources() { protected Class[] getArgumentSources() {
// No walker info? No plugins. // No walker info? No plugins.
if (analysisName == null) return new Class[] {}; if (getAnalysisName() == null) return new Class[] {};
return new Class[] { GATKEngine.getWalkerByName(analysisName).getClass() }; return new Class[] { GATKEngine.getWalkerByName(getAnalysisName()).getClass() };
} }
@Override
protected String getArgumentSourceName( Class argumentSource ) {
return WalkerManager.getWalkerName((Class<Walker>) argumentSource);
}
/** /**
* Preprocess the arguments before submitting them to the GATK engine. * Preprocess the arguments before submitting them to the GATK engine.
@ -111,7 +115,7 @@ public abstract class CommandLineExecutable extends CommandLineProgram {
* @param argCollection Collection of arguments to preprocess. * @param argCollection Collection of arguments to preprocess.
*/ */
private void processArguments( GATKArgumentCollection argCollection ) { private void processArguments( GATKArgumentCollection argCollection ) {
argCollection.samFiles = unpackReads(argCollection.samFiles); argCollection.samFiles = unpackReads( argCollection.samFiles );
} }
/** /**
@ -124,33 +128,37 @@ public abstract class CommandLineExecutable extends CommandLineProgram {
*/ */
private List<File> unpackReads( List<File> inputFiles ) { private List<File> unpackReads( List<File> inputFiles ) {
List<File> unpackedReads = new ArrayList<File>(); List<File> unpackedReads = new ArrayList<File>();
for (File inputFile : inputFiles) { for( File inputFile: inputFiles ) {
if (inputFile.getName().endsWith(".list")) { if (inputFile.getName().endsWith(".list") ) {
try { try {
for (String fileName : new xReadLines(inputFile)) for( String fileName : new xReadLines(inputFile) )
unpackedReads.add(new File(fileName)); unpackedReads.add( new File(fileName) );
} }
catch (FileNotFoundException ex) { catch( FileNotFoundException ex ) {
throw new StingException("Unable to find file while unpacking reads", ex); throw new StingException("Unable to find file while unpacking reads", ex);
} }
} else }
unpackedReads.add(inputFile); else
unpackedReads.add( inputFile );
} }
return unpackedReads; return unpackedReads;
} }
/**
* Get a custom factory for instantiating specialty GATK arguments.
* @return An instance of the command-line argument of the specified type.
*/
@Override @Override
protected String getArgumentSourceName( Class argumentSource ) { protected ArgumentFactory getCustomArgumentFactory() {
return WalkerManager.getWalkerName((Class<Walker>) argumentSource); return new ArgumentFactory() {
public Object createArgument( Class type, List<String> repr ) {
if (type == SAMFileReader.class && repr.size() == 1) {
SAMFileReader samFileReader = new SAMFileReader(new File(repr.get(0)),true);
samFileReader.setValidationStringency(getArgumentCollection().strictnessLevel);
return samFileReader;
} }
return null;
public GATKArgumentCollection getArgCollection() {
return argCollection;
} }
};
public void setArgCollection( GATKArgumentCollection argCollection ) {
this.argCollection = argCollection;
} }
} }

View File

@ -1,12 +1,7 @@
package org.broadinstitute.sting.gatk; package org.broadinstitute.sting.gatk;
import org.broadinstitute.sting.gatk.walkers.Walker;
import org.broadinstitute.sting.utils.StingException;
import org.broadinstitute.sting.utils.xReadLines;
import org.broadinstitute.sting.utils.cmdLine.*; import org.broadinstitute.sting.utils.cmdLine.*;
import java.io.FileNotFoundException;
import java.io.File;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
@ -40,49 +35,18 @@ import net.sf.samtools.SAMFileReader;
* gatk all the parsed out information. Pretty much anything dealing with the underlying system should go here, * gatk all the parsed out information. Pretty much anything dealing with the underlying system should go here,
* the gatk engine should deal with any data related information. * the gatk engine should deal with any data related information.
*/ */
public class CommandLineGATK extends CommandLineProgram { public class CommandLineGATK extends CommandLineExecutable {
@Argument(fullName = "analysis_type", shortName = "T", doc = "Type of analysis to run") @Argument(fullName = "analysis_type", shortName = "T", doc = "Type of analysis to run")
public String analysisName = null; public String analysisName = null;
@ArgumentCollection // our argument collection, the collection of command line args we accept // our argument collection, the collection of command line args we accept
public GATKArgumentCollection argCollection = new GATKArgumentCollection(); @ArgumentCollection
protected GATKArgumentCollection argCollection = new GATKArgumentCollection();
/** our genome analysis engine */
GenomeAnalysisEngine GATKEngine = new GenomeAnalysisEngine();
/** Required main method implementation. */
public static void main(String[] argv) {
try {
CommandLineGATK instance = new CommandLineGATK();
start(instance, argv);
} catch (Exception e) {
exitSystemWithError(e);
}
}
/**
* this is the function that the inheriting class can expect to have called
* when the command line system has initialized.
*
* @return the return code to exit the program with
*/
protected int execute() {
Walker<?,?> mWalker = GATKEngine.getWalkerByName(analysisName);
loadArgumentsIntoObject(argCollection);
loadArgumentsIntoObject(mWalker);
processArguments(argCollection);
this.argCollection.analysisName = this.analysisName;
GATKEngine.execute(argCollection, mWalker);
return 0;
}
/** /**
* Get pleasing info about the GATK. * Get pleasing info about the GATK.
* @return * @return A list of Strings that contain pleasant info about the GATK.
*/ */
@Override @Override
protected List<String> getApplicationHeader() { protected List<String> getApplicationHeader() {
@ -95,90 +59,23 @@ public class CommandLineGATK extends CommandLineProgram {
return header; return header;
} }
/**
* GATK can add arguments dynamically based on analysis type.
*
* @return true
*/
@Override @Override
protected boolean canAddArgumentsDynamically() { protected String getAnalysisName() {
return true; return analysisName;
}
/**
* GATK provides the walker as an argument source. As a side-effect, initializes the walker variable.
*
* @return List of walkers to load dynamically.
*/
@Override
protected Class[] getArgumentSources() {
// No walker info? No plugins.
if (analysisName == null) return new Class[] {};
return new Class[] { GATKEngine.getWalkerByName(analysisName).getClass() };
} }
@Override @Override
protected String getArgumentSourceName(Class argumentSource) { protected GATKArgumentCollection getArgumentCollection() {
return WalkerManager.getWalkerName((Class<Walker>) argumentSource);
}
public GATKArgumentCollection getArgCollection() {
return argCollection; return argCollection;
} }
public void setArgCollection(GATKArgumentCollection argCollection) { /** Required main method implementation. */
this.argCollection = argCollection; public static void main(String[] argv) {
}
/**
* Get a custom factory for instantiating specialty GATK arguments.
* @return An instance of the command-line argument of the specified type.
*/
@Override
protected ArgumentFactory getCustomArgumentFactory() {
return new ArgumentFactory() {
public Object createArgument( Class type, List<String> repr ) {
if (type == SAMFileReader.class && repr.size() == 1) {
SAMFileReader samFileReader = new SAMFileReader(new File(repr.get(0)),true);
samFileReader.setValidationStringency(argCollection.strictnessLevel);
return samFileReader;
}
return null;
}
};
}
/**
* Preprocess the arguments before submitting them to the GATK engine.
* @param argCollection Collection of arguments to preprocess.
*/
private void processArguments( GATKArgumentCollection argCollection ) {
argCollection.samFiles = unpackReads( argCollection.samFiles );
}
/**
* Unpack the files to be processed, given a list of files. That list of files can
* itself contain lists of other files to be read.
* @param inputFiles
* @return
*/
private List<File> unpackReads( List<File> inputFiles ) {
List<File> unpackedReads = new ArrayList<File>();
for( File inputFile: inputFiles ) {
if( inputFile.getName().endsWith(".list") ) {
try { try {
for( String fileName : new xReadLines(inputFile) ) CommandLineGATK instance = new CommandLineGATK();
unpackedReads.add( new File(fileName) ); start(instance, argv);
} } catch (Exception e) {
catch( FileNotFoundException ex ) { exitSystemWithError(e);
throw new StingException("Unable to find file while unpacking reads", ex);
} }
} }
else
unpackedReads.add( inputFile );
}
return unpackedReads;
}
} }

View File

@ -90,6 +90,8 @@ public class WalkerManager {
public Walker createWalkerByName(String walkerName) public Walker createWalkerByName(String walkerName)
throws InstantiationException, IllegalAccessException { throws InstantiationException, IllegalAccessException {
Class<? extends Walker> walker = walkersByName.get(walkerName); Class<? extends Walker> walker = walkersByName.get(walkerName);
if( walker == null )
throw new StingException(String.format("Could not find walker with name: %s", walkerName));
return walker.newInstance(); return walker.newInstance();
} }

View File

@ -47,7 +47,6 @@ public class SomaticCoverageTool extends CommandLineExecutable {
@Argument(fullName = "bam_file", shortName = "I", doc = "The bam files, one for the tumor one for the normal", required = true) @Argument(fullName = "bam_file", shortName = "I", doc = "The bam files, one for the tumor one for the normal", required = true)
public List<File> samFiles = new ArrayList<File>(); public List<File> samFiles = new ArrayList<File>();
/** Required main method implementation. */ /** Required main method implementation. */
public static void main( String[] argv ) { public static void main( String[] argv ) {
try { try {
@ -69,8 +68,9 @@ public class SomaticCoverageTool extends CommandLineExecutable {
} }
/** override any arguments we see fit. */ /** override any arguments we see fit. */
protected void overrideArguments() { protected GATKArgumentCollection getArgumentCollection() {
this.argCollection = GATKArgumentCollection.unmarshal(getClass().getClassLoader().getResourceAsStream("SomaticCoverage.xml")); GATKArgumentCollection argCollection = GATKArgumentCollection.unmarshal(getClass().getClassLoader().getResourceAsStream("SomaticCoverage.xml"));
this.argCollection.samFiles = samFiles; argCollection.samFiles = samFiles;
return argCollection;
} }
} }