@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");
RawHapMapFeature hapmap = (RawHapMapFeature)input;
int index = hapmap.getStart() - ref.getWindow().getStart();
if ( index < 0 )
return null; // we weren't given enough reference context to create the VariantContext
HashSet<Allele> alleles = new HashSet<Allele>();
Allele refSNPAllele = Allele.create(ref.getBase(), true);
int deletionLength = -1;
Map<String, Allele> alleleMap = hapmap.getActualAlleles();
// use the actual alleles, if available
if ( alleleMap != null ) {
alleles.addAll(alleleMap.values());
Allele deletionAllele = alleleMap.get(RawHapMapFeature.INSERTION); // yes, use insertion here (since we want the reference bases)
if ( deletionAllele != null && deletionAllele.isReference() )
deletionLength = deletionAllele.length();
} else {
// add the reference allele for SNPs
alleles.add(refSNPAllele);
}
// make a mapping from sample to genotype
String[] samples = hapmap.getSampleIDs();
String[] genotypeStrings = hapmap.getGenotypes();
GenotypesContext genotypes = GenotypesContext.create(samples.length);
for ( int i = 0; i < samples.length; i++ ) {
// ignore bad genotypes
if ( genotypeStrings[i].contains("N") )
continue;
String a1 = genotypeStrings[i].substring(0,1);
String a2 = genotypeStrings[i].substring(1);
ArrayList<Allele> myAlleles = new ArrayList<Allele>(2);
// use the mapping to actual alleles, if available
if ( alleleMap != null ) {
myAlleles.add(alleleMap.get(a1));
myAlleles.add(alleleMap.get(a2));
} else {
// ignore indels (which we can't handle without knowing the alleles)
if ( genotypeStrings[i].contains("I") || genotypeStrings[i].contains("D") )
continue;
Allele allele1 = Allele.create(a1, refSNPAllele.basesMatch(a1));
Allele allele2 = Allele.create(a2, refSNPAllele.basesMatch(a2));
myAlleles.add(allele1);
myAlleles.add(allele2);
alleles.add(allele1);
alleles.add(allele2);
}
Genotype g = GenotypeBuilder.create(samples[i], myAlleles);
genotypes.add(g);
}
long end = hapmap.getEnd();
if ( deletionLength > 0 )
end += (deletionLength - 1);
VariantContext vc = new VariantContextBuilder(name, hapmap.getChr(), hapmap.getStart(), end, alleles).id(hapmap.getName()).genotypes(genotypes).make();
return vc;
}