From 6cc84c3ce28ed8b4511a3fe3dd5bd19dfaff8f32 Mon Sep 17 00:00:00 2001 From: hanna Date: Wed, 1 Jun 2011 02:54:55 +0000 Subject: [PATCH] Make the set of VariantContextAdaptors dynamic so that Andrey's MafFeature can continue to exist and live in playground (and thus outside of the normal release / git release branch). git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@5909 348d0f76-0448-11de-a6fe-93d51630548a --- .../gatk/refdata/VariantContextAdaptors.java | 185 +++++------------- .../gatk/features/maf/MafFeature.java | 128 ++++++++++++ .../utils/classloader/PluginManager.java | 14 +- 3 files changed, 187 insertions(+), 140 deletions(-) diff --git a/java/src/org/broadinstitute/sting/gatk/refdata/VariantContextAdaptors.java b/java/src/org/broadinstitute/sting/gatk/refdata/VariantContextAdaptors.java index 13bd05d95..d63a0e76d 100755 --- a/java/src/org/broadinstitute/sting/gatk/refdata/VariantContextAdaptors.java +++ b/java/src/org/broadinstitute/sting/gatk/refdata/VariantContextAdaptors.java @@ -1,5 +1,6 @@ package org.broadinstitute.sting.gatk.refdata; +import org.broad.tribble.Feature; import org.broad.tribble.dbsnp.DbSNPFeature; import org.broad.tribble.gelitext.GeliTextFeature; import org.broad.tribble.hapmap.HapMapFeature; @@ -12,6 +13,7 @@ import org.broadinstitute.sting.gatk.contexts.ReferenceContext; import org.broadinstitute.sting.gatk.contexts.variantcontext.VariantContextUtils; import org.broadinstitute.sting.gatk.refdata.utils.helpers.DbSNPHelper; import org.broadinstitute.sting.playground.gatk.features.maf.MafFeature; +import org.broadinstitute.sting.utils.classloader.PluginManager; import java.util.*; @@ -36,14 +38,13 @@ public class VariantContextAdaptors { // // -------------------------------------------------------------------------------------------------------------- - private static Map adaptors = new HashMap(); + private static Map,VCAdaptor> adaptors = new HashMap,VCAdaptor>(); static { - adaptors.put(DbSNPFeature.class, new DBSnpAdaptor()); - adaptors.put(HapMapFeature.class, new HapMapAdaptor()); - adaptors.put(GeliTextFeature.class, new GeliTextAdaptor()); - adaptors.put(VariantContext.class, new VariantContextAdaptor()); - adaptors.put(MafFeature.class, new MafAdaptor()); + PluginManager vcAdaptorManager = new PluginManager(VCAdaptor.class); + List adaptorInstances = vcAdaptorManager.createAllTypes(); + for(VCAdaptor adaptor: adaptorInstances) + adaptors.put(adaptor.getAdaptableFeatureType(),adaptor); } public static boolean canBeConvertedToVariantContext(Object variantContainingObject) { @@ -51,8 +52,13 @@ public class VariantContextAdaptors { } /** generic superclass */ - private static abstract class VCAdaptor { - abstract VariantContext convert(String name, Object input, ReferenceContext ref); + public interface VCAdaptor { + /** + * Gets the type of feature that this adaptor can 'adapt' into a VariantContext. + * @return Type of adaptable feature. Must be a Tribble feature class. + */ + Class getAdaptableFeatureType(); + VariantContext convert(String name, Object input, ReferenceContext ref); } public static VariantContext toVariantContext(String name, Object variantContainingObject, ReferenceContext ref) { @@ -68,10 +74,17 @@ public class VariantContextAdaptors { // From here below you can add adaptor classes for new rods (or other types) to convert to VC // // -------------------------------------------------------------------------------------------------------------- + private static class VariantContextAdaptor implements VCAdaptor { + /** + * 'Null' adaptor; adapts variant contexts to variant contexts. + * @return VariantContext. + */ + @Override + public Class getAdaptableFeatureType() { return VariantContext.class; } - private static class VariantContextAdaptor extends VCAdaptor { // already a VC, just cast and return it - VariantContext convert(String name, Object input, ReferenceContext ref) { + @Override + public VariantContext convert(String name, Object input, ReferenceContext ref) { return (VariantContext)input; } } @@ -82,8 +95,16 @@ public class VariantContextAdaptors { // // -------------------------------------------------------------------------------------------------------------- - private static class DBSnpAdaptor extends VCAdaptor { - VariantContext convert(String name, Object input, ReferenceContext ref) { + private static class DBSnpAdaptor implements VCAdaptor { + /** + * Converts non-VCF formatted dbSNP records to VariantContext. + * @return DbSNPFeature. + */ + @Override + public Class getAdaptableFeatureType() { return DbSNPFeature.class; } + + @Override + public VariantContext convert(String name, Object input, ReferenceContext ref) { DbSNPFeature dbsnp = (DbSNPFeature)input; if ( ! Allele.acceptableAlleleBases(DbSNPHelper.getReference(dbsnp)) ) return null; @@ -128,7 +149,14 @@ public class VariantContextAdaptors { // // -------------------------------------------------------------------------------------------------------------- - private static class GeliTextAdaptor extends VCAdaptor { + private static class GeliTextAdaptor implements VCAdaptor { + /** + * Converts Geli text records to VariantContext. + * @return GeliTextFeature. + */ + @Override + public Class getAdaptableFeatureType() { return GeliTextFeature.class; } + /** * convert to a Variant Context, given: * @param name the name of the ROD @@ -146,7 +174,8 @@ public class VariantContextAdaptors { * @param ref the reference context * @return a VariantContext object */ - VariantContext convert(String name, Object input, ReferenceContext ref) { + @Override + public VariantContext convert(String name, Object input, ReferenceContext ref) { GeliTextFeature geli = (GeliTextFeature)input; if ( ! Allele.acceptableAlleleBases(String.valueOf(geli.getRefBase())) ) return null; @@ -195,7 +224,14 @@ public class VariantContextAdaptors { // // -------------------------------------------------------------------------------------------------------------- - private static class HapMapAdaptor extends VCAdaptor { + private static class HapMapAdaptor implements VCAdaptor { + /** + * Converts HapMap records to VariantContext. + * @return HapMapFeature. + */ + @Override + public Class getAdaptableFeatureType() { return HapMapFeature.class; } + /** * convert to a Variant Context, given: * @param name the name of the ROD @@ -213,7 +249,8 @@ public class VariantContextAdaptors { * @param ref the reference context * @return a VariantContext object */ - VariantContext convert(String name, Object input, ReferenceContext ref) { + @Override + public VariantContext convert(String name, Object input, ReferenceContext ref) { if ( ref == null ) throw new UnsupportedOperationException("Conversion from HapMap to VariantContext requires a reference context"); @@ -281,120 +318,4 @@ public class VariantContextAdaptors { return vc; } } - - private static class MafAdaptor extends VCAdaptor { - /** - * convert to a Variant Context, given: - * @param name the name of the ROD - * @param input the Rod object, in this case a MafFeature - * @return a VariantContext object - */ -// VariantContext convert(String name, Object input) { -// return convert(name, input, null); -// } - - /** - * convert to a Variant Context, given: - * @param name the name of the ROD - * @param input the Rod object, in this case a MafFeature - * @param ref the reference context - * @return a VariantContext object - */ - VariantContext convert(String name, Object input, ReferenceContext ref) { - - if ( ref == null ) - throw new UnsupportedOperationException("Conversion from MAF to VariantContext requires a reference context, null received"); - - MafFeature maf = (MafFeature)input; - if ( ! Allele.acceptableAlleleBases(maf.getRefBases()) ) - return null; - - List alleles = new ArrayList(); - - Allele refAllele = Allele.create(maf.getRefBases(), true); - // add the reference allele: - alleles.add(refAllele); - - // add all of the alt alleles - for ( String alt : maf.getAllNonRefAlleleList() ) { - if ( ! Allele.acceptableAlleleBases(alt) ) { - //System.out.printf("Excluding dbsnp record %s%n", dbsnp); - return null; - } - alleles.add(Allele.create(alt, false)); - } - - // make a mapping from sample to genotype - - String normalSample = maf.getNormalSampleId(); - String tumorSample = maf.getTumorSampleId(); - -// String[] genotypeStrings = hapmap.getGenotypes(); - - Map genotypes = new HashMap(2); - - addGenotype(genotypes, normalSample, maf.getObservedNormalAlleleList(),maf.getRefBases()); - addGenotype(genotypes,tumorSample,maf.getObservedTumorAlleleList(),maf.getRefBases()); - - - HashMap attrs = new HashMap(10); - // fill attributes: - if ( maf.getHugoGeneSymbol() != null && ! maf.getHugoGeneSymbol().equals("Unknown")) - attrs.put("Gene",maf.getHugoGeneSymbol()); - - if ( maf.isSomatic() ) { - attrs.put(VCFConstants.SOMATIC_KEY,true); - attrs.put("SS","Somatic"); - } else { - attrs.put("SS","Germline"); - } - - if ( maf.getVariantClassification() != null ) { - switch(maf.getVariantClassification()) { - case Intergenic: attrs.put("VC","Genomic"); break; - case Intron: attrs.put("VC","Intron"); break; - case Noncoding_transcript: attrs.put("VC","Noncoding_transcript"); break; - case UTR3: attrs.put("VC","3'UTR"); break; - case UTR5: attrs.put("VC","5'UTR"); break; - case Flank5: attrs.put("VC","5'flank"); break; - case Promoter: attrs.put("VC","5'flank"); break; - case De_novo_start: attrs.put("VC","De_novo_start"); break; - case De_novo_start_out_of_frame: attrs.put("VC","De_novo_start_out_of_frame"); break; - case Silent: attrs.put("VC","Silent"); break; - case Missense: attrs.put("VC","Missense"); break; - case Nonsense: attrs.put("VC","Nonsense"); break; - case Splice_site: attrs.put("VC","Splice_site"); break; - case miRNA: attrs.put("VC","miRNA"); break; - case Frameshift: attrs.put("VC","Frameshift"); break; - case Inframe: attrs.put("VC","Inframe"); break; - case Stop_deletion: attrs.put("VC","Stop_codon_deletion"); - case Unclassified: attrs.put("VC","Unclassified"); - default: - } - } - - attrs.put("VT",maf.getType()); - -// attrs.put(VariantContext.ID_KEY, hapmap.getName()); - int end = maf.getEnd(); - VariantContext vc = new VariantContext(name, maf.getChr(), maf.getStart(), end, alleles, - genotypes, VariantContext.NO_NEG_LOG_10PERROR, null, attrs); - return vc; - } - - private void addGenotype(Map dest, String sampleId, List alleles, String refAllele) { - List myAlleles = new ArrayList(2); - - boolean success = true; - - for ( String a : alleles ) { - if ( a.isEmpty() || a.contains("N") || a.contains(".")) return; // bad allele found - myAlleles.add(Allele.create(a,refAllele.equals(a))); - } - dest.put(sampleId, new Genotype(sampleId,myAlleles)); - } - - } - - } diff --git a/java/src/org/broadinstitute/sting/playground/gatk/features/maf/MafFeature.java b/java/src/org/broadinstitute/sting/playground/gatk/features/maf/MafFeature.java index dd64e9176..e66f1c2b0 100644 --- a/java/src/org/broadinstitute/sting/playground/gatk/features/maf/MafFeature.java +++ b/java/src/org/broadinstitute/sting/playground/gatk/features/maf/MafFeature.java @@ -26,6 +26,12 @@ package org.broadinstitute.sting.playground.gatk.features.maf; import org.broad.tribble.Feature; +import org.broad.tribble.util.variantcontext.Allele; +import org.broad.tribble.util.variantcontext.Genotype; +import org.broad.tribble.util.variantcontext.VariantContext; +import org.broad.tribble.vcf.VCFConstants; +import org.broadinstitute.sting.gatk.contexts.ReferenceContext; +import org.broadinstitute.sting.gatk.refdata.VariantContextAdaptors; import org.broadinstitute.sting.utils.exceptions.StingException; import org.broadinstitute.sting.utils.exceptions.UserException; @@ -264,3 +270,125 @@ public class MafFeature implements Feature { } } + +class MafAdaptor implements VariantContextAdaptors.VCAdaptor { + /** + * Converts Maf features to VariantContext. + * @return MafFeature. + */ + @Override + public Class getAdaptableFeatureType() { return MafFeature.class; } + + /** + * convert to a Variant Context, given: + * @param name the name of the ROD + * @param input the Rod object, in this case a MafFeature + * @return a VariantContext object + */ +// VariantContext convert(String name, Object input) { +// return convert(name, input, null); +// } + + /** + * convert to a Variant Context, given: + * @param name the name of the ROD + * @param input the Rod object, in this case a MafFeature + * @param ref the reference context + * @return a VariantContext object + */ + @Override + public VariantContext convert(String name, Object input, ReferenceContext ref) { + + if ( ref == null ) + throw new UnsupportedOperationException("Conversion from MAF to VariantContext requires a reference context, null received"); + + MafFeature maf = (MafFeature)input; + if ( ! Allele.acceptableAlleleBases(maf.getRefBases()) ) + return null; + + List alleles = new ArrayList(); + + Allele refAllele = Allele.create(maf.getRefBases(), true); + // add the reference allele: + alleles.add(refAllele); + + // add all of the alt alleles + for ( String alt : maf.getAllNonRefAlleleList() ) { + if ( ! Allele.acceptableAlleleBases(alt) ) { + //System.out.printf("Excluding dbsnp record %s%n", dbsnp); + return null; + } + alleles.add(Allele.create(alt, false)); + } + + // make a mapping from sample to genotype + + String normalSample = maf.getNormalSampleId(); + String tumorSample = maf.getTumorSampleId(); + +// String[] genotypeStrings = hapmap.getGenotypes(); + + Map genotypes = new HashMap(2); + + addGenotype(genotypes, normalSample, maf.getObservedNormalAlleleList(),maf.getRefBases()); + addGenotype(genotypes,tumorSample,maf.getObservedTumorAlleleList(),maf.getRefBases()); + + + HashMap attrs = new HashMap(10); + // fill attributes: + if ( maf.getHugoGeneSymbol() != null && ! maf.getHugoGeneSymbol().equals("Unknown")) + attrs.put("Gene",maf.getHugoGeneSymbol()); + + if ( maf.isSomatic() ) { + attrs.put(VCFConstants.SOMATIC_KEY,true); + attrs.put("SS","Somatic"); + } else { + attrs.put("SS","Germline"); + } + + if ( maf.getVariantClassification() != null ) { + switch(maf.getVariantClassification()) { + case Intergenic: attrs.put("VC","Genomic"); break; + case Intron: attrs.put("VC","Intron"); break; + case Noncoding_transcript: attrs.put("VC","Noncoding_transcript"); break; + case UTR3: attrs.put("VC","3'UTR"); break; + case UTR5: attrs.put("VC","5'UTR"); break; + case Flank5: attrs.put("VC","5'flank"); break; + case Promoter: attrs.put("VC","5'flank"); break; + case De_novo_start: attrs.put("VC","De_novo_start"); break; + case De_novo_start_out_of_frame: attrs.put("VC","De_novo_start_out_of_frame"); break; + case Silent: attrs.put("VC","Silent"); break; + case Missense: attrs.put("VC","Missense"); break; + case Nonsense: attrs.put("VC","Nonsense"); break; + case Splice_site: attrs.put("VC","Splice_site"); break; + case miRNA: attrs.put("VC","miRNA"); break; + case Frameshift: attrs.put("VC","Frameshift"); break; + case Inframe: attrs.put("VC","Inframe"); break; + case Stop_deletion: attrs.put("VC","Stop_codon_deletion"); + case Unclassified: attrs.put("VC","Unclassified"); + default: + } + } + + attrs.put("VT",maf.getType()); + +// attrs.put(VariantContext.ID_KEY, hapmap.getName()); + int end = maf.getEnd(); + VariantContext vc = new VariantContext(name, maf.getChr(), maf.getStart(), end, alleles, + genotypes, VariantContext.NO_NEG_LOG_10PERROR, null, attrs); + return vc; + } + + private void addGenotype(Map dest, String sampleId, List alleles, String refAllele) { + List myAlleles = new ArrayList(2); + + boolean success = true; + + for ( String a : alleles ) { + if ( a.isEmpty() || a.contains("N") || a.contains(".")) return; // bad allele found + myAlleles.add(Allele.create(a,refAllele.equals(a))); + } + dest.put(sampleId, new Genotype(sampleId,myAlleles)); + } + +} \ No newline at end of file diff --git a/java/src/org/broadinstitute/sting/utils/classloader/PluginManager.java b/java/src/org/broadinstitute/sting/utils/classloader/PluginManager.java index b9f5a2421..8d37ff573 100644 --- a/java/src/org/broadinstitute/sting/utils/classloader/PluginManager.java +++ b/java/src/org/broadinstitute/sting/utils/classloader/PluginManager.java @@ -35,6 +35,7 @@ import org.reflections.scanners.SubTypesScanner; import org.reflections.util.ConfigurationBuilder; import org.slf4j.LoggerFactory; +import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; @@ -249,10 +250,11 @@ public class PluginManager { * @param pluginType type of the plugin to create. * @return The plugin object if created; null otherwise. */ - @SuppressWarnings("unchecked") - public PluginType createByType(Class pluginType) { + public PluginType createByType(Class pluginType) { try { - return ((Class) pluginType).newInstance(); + Constructor noArgsConstructor = pluginType.getDeclaredConstructor((Class[])null); + noArgsConstructor.setAccessible(true); + return noArgsConstructor.newInstance(); } catch (Exception e) { throw new DynamicClassResolutionException(pluginType, e); } @@ -265,11 +267,7 @@ public class PluginManager { public List createAllTypes() { List instances = new ArrayList(); for ( Class c : getPlugins() ) { - try { - instances.add(c.newInstance()); - } catch (Exception e) { - throw new DynamicClassResolutionException(c, e); - } + instances.add(createByType(c)); } return instances; }