Minor interface change for VCFGenotypeRecord.

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@2537 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
ebanks 2010-01-07 16:48:09 +00:00
parent 431e9c2c8b
commit b643a513bb
7 changed files with 58 additions and 55 deletions

View File

@ -137,16 +137,18 @@ public class VariantsToVCF extends RefWalker<Integer, Integer> {
str.put("GQ", lod);
if (depth > 0) str.put("DP", String.valueOf(depth));
gt.add(new VCFGenotypeRecord(name, alleles, VCFGenotypeRecord.PHASE.UNPHASED, str));
VCFGenotypeRecord rec = new VCFGenotypeRecord(name, alleles, VCFGenotypeRecord.PHASE.UNPHASED);
for ( Map.Entry<String, String> entry : str.entrySet() )
rec.setField(entry.getKey(), entry.getValue());
gt.add(rec);
numSNPs++;
snpQual += av.getNegLog10PError();
} else if (BaseUtils.simpleBaseToBaseIndex(ref.getBase()) != -1) {
Map<String, String> str = new HashMap<String, String>();
List<VCFGenotypeEncoding> alleles = new ArrayList<VCFGenotypeEncoding>();
alleles.add(new VCFGenotypeEncoding(String.valueOf(ref.getBase())));
alleles.add(new VCFGenotypeEncoding(String.valueOf(ref.getBase())));
gt.add(new VCFGenotypeRecord(name, alleles, VCFGenotypeRecord.PHASE.UNPHASED, str));
gt.add(new VCFGenotypeRecord(name, alleles, VCFGenotypeRecord.PHASE.UNPHASED));
numRefs++;
}

View File

@ -58,20 +58,12 @@ public class VCFGenotypeRecord implements Genotype, SampleBacked {
* @param sampleName sample name
* @param genotypes list of genotypes
* @param phasing phasing
* @param otherFlags other flags
*/
public VCFGenotypeRecord(String sampleName, List<VCFGenotypeEncoding> genotypes, PHASE phasing, Map<String, String> otherFlags) {
public VCFGenotypeRecord(String sampleName, List<VCFGenotypeEncoding> genotypes, PHASE phasing) {
mSampleName = sampleName;
if (genotypes != null) this.mGenotypeAlleles.addAll(genotypes);
if (genotypes != null)
this.mGenotypeAlleles.addAll(genotypes);
mPhaseType = phasing;
if (otherFlags != null) {
// we need to be backwards compatible
if ( otherFlags.containsKey(OLD_DEPTH_KEY) ) {
otherFlags.put(DEPTH_KEY, otherFlags.get(OLD_DEPTH_KEY));
otherFlags.remove(OLD_DEPTH_KEY);
}
mFields.putAll(otherFlags);
}
}
public void setVCFRecord(VCFRecord record) {
@ -82,6 +74,23 @@ public class VCFGenotypeRecord implements Genotype, SampleBacked {
mSampleName = name;
}
/**
* Adds a field to the genotype record.
* Throws an exception if the key is GT, as that's computed internally.
*
* @param key the field name (use static variables above for common fields)
* @param value the field value
*/
public void setField(String key, String value) {
// make sure the GT field isn't being set
if ( key.equals(GENOTYPE_KEY) )
throw new IllegalArgumentException("Setting the GT field is not allowed as that's done internally");
// we need to be backwards compatible
if ( key.equals(OLD_DEPTH_KEY) )
key = DEPTH_KEY;
mFields.put(key, value);
}
/**
* determine the phase of the genotype
*
@ -217,7 +226,8 @@ public class VCFGenotypeRecord implements Genotype, SampleBacked {
}
@Override public String toString() {
@Override
public String toString() {
return String.format("[VCFGenotype %s %s %s %s]", getLocation(), mSampleName, this.mGenotypeAlleles, mFields);
}

View File

@ -192,18 +192,12 @@ public class VCFGenotypeWriterAdapter implements VCFGenotypeWriter {
* @return a VCFGenotypeRecord for the no call situation
*/
private VCFGenotypeRecord createNoCallRecord(String sampleName) {
Map<String, String> map = new HashMap<String, String>();
List<VCFGenotypeEncoding> alleles = new ArrayList<VCFGenotypeEncoding>();
alleles.add(new VCFGenotypeEncoding(VCFGenotypeRecord.EMPTY_ALLELE));
alleles.add(new VCFGenotypeEncoding(VCFGenotypeRecord.EMPTY_ALLELE));
VCFGenotypeRecord record = new VCFGenotypeRecord(sampleName,
alleles,
VCFGenotypeRecord.PHASE.UNPHASED,
map);
return record;
return new VCFGenotypeRecord(sampleName, alleles, VCFGenotypeRecord.PHASE.UNPHASED);
}
/** @return true if we support multisample, false otherwise */

View File

@ -256,7 +256,7 @@ public class VCFReader implements Iterator<VCFRecord>, Iterable<VCFRecord> {
*/
public static VCFGenotypeRecord getVCFGenotype(String sampleName, String[] keyStrings, String genotypeString, String altAlleles[], char referenceBase) {
// parameters to create the VCF genotype record
Map<String, String> tagToValue = new HashMap<String, String>();
HashMap<String, String> tagToValue = new HashMap<String, String>();
VCFGenotypeRecord.PHASE phase = VCFGenotypeRecord.PHASE.UNKNOWN;
List<VCFGenotypeEncoding> bases = new ArrayList<VCFGenotypeEncoding>();
@ -294,7 +294,11 @@ public class VCFReader implements Iterator<VCFRecord>, Iterable<VCFRecord> {
else if ( genotypeString.length() > 0 )
throw new RuntimeException("VCFReader: genotype string contained additional unprocessed fields: " + genotypeString
+ ". This most likely means that the format string is shorter then the value fields.");
return new VCFGenotypeRecord(sampleName, bases, phase, tagToValue);
VCFGenotypeRecord rec = new VCFGenotypeRecord(sampleName, bases, phase);
for ( Map.Entry<String, String> entry : tagToValue.entrySet() )
rec.setField(entry.getKey(), entry.getValue());
return rec;
}

View File

@ -210,24 +210,21 @@ public class VCFUtils {
* @return a VCFGenotypeRecord
*/
public static VCFGenotypeRecord createVCFGenotypeRecord(VCFParameters params, VCFGenotypeRecord gtype, VCFRecord vcfrecord) {
Map<String, String> map = new HashMap<String, String>();
// calculate the genotype quality and the read depth
map.put(VCFGenotypeRecord.DEPTH_KEY, String.valueOf(gtype.getReadCount()));
params.addFormatItem(VCFGenotypeRecord.DEPTH_KEY);
double qual = Math.min(10.0 * gtype.getNegLog10PError(), VCFGenotypeRecord.MAX_QUAL_VALUE);
map.put(VCFGenotypeRecord.GENOTYPE_QUALITY_KEY, String.format("%.2f", qual));
params.addFormatItem(VCFGenotypeRecord.GENOTYPE_QUALITY_KEY);
List<VCFGenotypeEncoding> alleles = createAlleleArray(gtype);
for (VCFGenotypeEncoding allele : alleles) {
params.addAlternateBase(allele);
}
VCFGenotypeRecord record = new VCFGenotypeRecord(gtype.getSampleName(),
alleles,
VCFGenotypeRecord.PHASE.UNPHASED,
map);
VCFGenotypeRecord record = new VCFGenotypeRecord(gtype.getSampleName(), alleles, VCFGenotypeRecord.PHASE.UNPHASED);
// calculate the genotype quality and the read depth
record.setField(VCFGenotypeRecord.DEPTH_KEY, String.valueOf(gtype.getReadCount()));
params.addFormatItem(VCFGenotypeRecord.DEPTH_KEY);
double qual = Math.min(10.0 * gtype.getNegLog10PError(), VCFGenotypeRecord.MAX_QUAL_VALUE);
record.setField(VCFGenotypeRecord.GENOTYPE_QUALITY_KEY, String.format("%.2f", qual));
params.addFormatItem(VCFGenotypeRecord.GENOTYPE_QUALITY_KEY);
record.setVCFRecord(vcfrecord);
return record;
}
@ -241,24 +238,21 @@ public class VCFUtils {
* @return a VCFGenotypeRecord
*/
public static VCFGenotypeRecord createVCFGenotypeRecord(VCFParameters params, VCFGenotypeCall gtype) {
Map<String, String> map = new HashMap<String, String>();
// calculate the RMS mapping qualities and the read depth
map.put(VCFGenotypeRecord.DEPTH_KEY, String.valueOf(gtype.getReadCount()));
params.addFormatItem(VCFGenotypeRecord.DEPTH_KEY);
double qual = Math.min(10.0 * gtype.getNegLog10PError(), VCFGenotypeRecord.MAX_QUAL_VALUE);
map.put(VCFGenotypeRecord.GENOTYPE_QUALITY_KEY, String.format("%.2f", qual));
params.addFormatItem(VCFGenotypeRecord.GENOTYPE_QUALITY_KEY);
List<VCFGenotypeEncoding> alleles = createAlleleArray(gtype);
for (VCFGenotypeEncoding allele : alleles) {
params.addAlternateBase(allele);
}
VCFGenotypeRecord record = new VCFGenotypeRecord(gtype.getSampleName(),
alleles,
VCFGenotypeRecord.PHASE.UNPHASED,
map);
VCFGenotypeRecord record = new VCFGenotypeRecord(gtype.getSampleName(), alleles, VCFGenotypeRecord.PHASE.UNPHASED);
// calculate the RMS mapping qualities and the read depth
record.setField(VCFGenotypeRecord.DEPTH_KEY, String.valueOf(gtype.getReadCount()));
params.addFormatItem(VCFGenotypeRecord.DEPTH_KEY);
double qual = Math.min(10.0 * gtype.getNegLog10PError(), VCFGenotypeRecord.MAX_QUAL_VALUE);
record.setField(VCFGenotypeRecord.GENOTYPE_QUALITY_KEY, String.format("%.2f", qual));
params.addFormatItem(VCFGenotypeRecord.GENOTYPE_QUALITY_KEY);
return record;
}

View File

@ -56,12 +56,12 @@ public class VCFRecordTest extends BaseTest {
* @return a VCFGenotypeRecord
*/
private static VCFGenotypeRecord createGenotype(String name, String Allele1, String Allele2) {
Map<String, String> keyValues = new HashMap<String, String>();
keyValues.put("AA", "2");
List<VCFGenotypeEncoding> Alleles = new ArrayList<VCFGenotypeEncoding>();
Alleles.add(new VCFGenotypeEncoding(Allele1));
Alleles.add(new VCFGenotypeEncoding(Allele2));
return new VCFGenotypeRecord(name, Alleles, VCFGenotypeRecord.PHASE.PHASED, keyValues);
VCFGenotypeRecord rec = new VCFGenotypeRecord(name, Alleles, VCFGenotypeRecord.PHASE.PHASED);
rec.setField("AA", "2");
return rec;
}
@Test

View File

@ -85,13 +85,12 @@ public class VCFWriterTest extends BaseTest {
List<VCFGenotypeRecord> gt = new ArrayList<VCFGenotypeRecord>();
for (String name : header.getGenotypeSamples()) {
Map<String,String> str = new HashMap<String,String>();
str.put("bb","0");
List<VCFGenotypeEncoding> myAlleles = new ArrayList<VCFGenotypeEncoding>();
myAlleles.add(new VCFGenotypeEncoding("C"));
myAlleles.add(new VCFGenotypeEncoding("D1"));
gt.add(new VCFGenotypeRecord(name, myAlleles, VCFGenotypeRecord.PHASE.PHASED, str));
VCFGenotypeRecord rec = new VCFGenotypeRecord(name, myAlleles, VCFGenotypeRecord.PHASE.PHASED);
rec.setField("bb", "0");
gt.add(rec);
}
return new VCFRecord('A',"chr1",1,"RANDOM",altBases,0,".",infoFields, "GT:AA",gt);