Alleles now hash correctly.
Special thanks to Matt & Aaron. git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@2965 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
parent
e5475a7ba9
commit
0e360ea8af
|
|
@ -121,23 +121,33 @@ public class Allele implements Comparable<Allele> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Do the bases represent the null allele?
|
* @param bases bases representing an allele
|
||||||
|
* @return true if the bases represent the null allele
|
||||||
*/
|
*/
|
||||||
public static boolean wouldBeNullAllele(byte[] bases) {
|
public static boolean wouldBeNullAllele(byte[] bases) {
|
||||||
return (bases.length == 1 && bases[0] == '-') || bases.length == 0;
|
return (bases.length == 1 && bases[0] == '-') || bases.length == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Do the bases represent the NO_CALL allele? */
|
/**
|
||||||
|
* @param bases bases representing an allele
|
||||||
|
* @return true if the bases represent the NO_CALL allele
|
||||||
|
*/
|
||||||
public static boolean wouldBeNoCallAllele(byte[] bases) {
|
public static boolean wouldBeNoCallAllele(byte[] bases) {
|
||||||
return bases.length == 1 && bases[0] == '.';
|
return bases.length == 1 && bases[0] == '.';
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Do the bases represent the null allele? */
|
/**
|
||||||
|
* @param bases bases representing an allele
|
||||||
|
* @return true if the bases represent the well formatted allele
|
||||||
|
*/
|
||||||
public static boolean acceptableAlleleBases(String bases) {
|
public static boolean acceptableAlleleBases(String bases) {
|
||||||
return acceptableAlleleBases(bases.getBytes());
|
return acceptableAlleleBases(bases.getBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Can we create an allele from bases, including NO_CALL and Null alleles? */
|
/**
|
||||||
|
* @param bases bases representing an allele
|
||||||
|
* @return true if the bases represent the well formatted allele
|
||||||
|
*/
|
||||||
public static boolean acceptableAlleleBases(byte[] bases) {
|
public static boolean acceptableAlleleBases(byte[] bases) {
|
||||||
if ( wouldBeNullAllele(bases) || wouldBeNoCallAllele(bases) )
|
if ( wouldBeNullAllele(bases) || wouldBeNoCallAllele(bases) )
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -154,8 +164,8 @@ public class Allele implements Comparable<Allele> {
|
||||||
/**
|
/**
|
||||||
* @see Allele(byte[], boolean)
|
* @see Allele(byte[], boolean)
|
||||||
*
|
*
|
||||||
* @param bases
|
* @param bases bases representing an allele
|
||||||
* @param isRef
|
* @param isRef is this the reference allele?
|
||||||
*/
|
*/
|
||||||
public Allele(String bases, boolean isRef) {
|
public Allele(String bases, boolean isRef) {
|
||||||
this(bases.getBytes(), isRef);
|
this(bases.getBytes(), isRef);
|
||||||
|
|
@ -164,14 +174,14 @@ public class Allele implements Comparable<Allele> {
|
||||||
/**
|
/**
|
||||||
* Creates a non-Ref allele. @see Allele(byte[], boolean) for full information
|
* Creates a non-Ref allele. @see Allele(byte[], boolean) for full information
|
||||||
*
|
*
|
||||||
* @param bases
|
* @param bases bases representing an allele
|
||||||
*/
|
*/
|
||||||
public Allele(String bases) { this(bases, false); }
|
public Allele(String bases) { this(bases, false); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a non-Ref allele. @see Allele(byte[], boolean) for full information
|
* Creates a non-Ref allele. @see Allele(byte[], boolean) for full information
|
||||||
*
|
*
|
||||||
* @param bases
|
* @param bases bases representing an allele
|
||||||
*/
|
*/
|
||||||
public Allele(byte[] bases) { this(bases, false); }
|
public Allele(byte[] bases) { this(bases, false); }
|
||||||
|
|
||||||
|
|
@ -181,22 +191,22 @@ public class Allele implements Comparable<Allele> {
|
||||||
//
|
//
|
||||||
// ---------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
/** Returns true if this is the null allele */
|
//Returns true if this is the null allele
|
||||||
public boolean isNull() { return isNull; }
|
public boolean isNull() { return isNull; }
|
||||||
/** Returns true if this is not the null allele */
|
// Returns true if this is not the null allele
|
||||||
public boolean isNonNull() { return ! isNull(); }
|
public boolean isNonNull() { return ! isNull(); }
|
||||||
|
|
||||||
/** Returns true if this is the NO_CALL allele */
|
// Returns true if this is the NO_CALL allele
|
||||||
public boolean isNoCall() { return isNoCall; }
|
public boolean isNoCall() { return isNoCall; }
|
||||||
/** Returns true if this is the not the NO_CALL allele */
|
// Returns true if this is the not the NO_CALL allele
|
||||||
public boolean isCalled() { return ! isNoCall(); }
|
public boolean isCalled() { return ! isNoCall(); }
|
||||||
|
|
||||||
/** Returns true if this Allele is the reference allele */
|
// Returns true if this Allele is the reference allele
|
||||||
public boolean isReference() { return isRef; }
|
public boolean isReference() { return isRef; }
|
||||||
/** Returns true if this Allele is not the reference allele */
|
// Returns true if this Allele is not the reference allele
|
||||||
public boolean isNonReference() { return ! isReference(); }
|
public boolean isNonReference() { return ! isReference(); }
|
||||||
|
|
||||||
/** Returns a nice string representation of this object */
|
// Returns a nice string representation of this object
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return (isNull() ? "-" : ( isNoCall() ? "." : new String(getBases()))) + (isReference() ? "*" : "");
|
return (isNull() ? "-" : ( isNoCall() ? "." : new String(getBases()))) + (isReference() ? "*" : "");
|
||||||
}
|
}
|
||||||
|
|
@ -214,52 +224,55 @@ public class Allele implements Comparable<Allele> {
|
||||||
*
|
*
|
||||||
* @return true if these alleles are equal
|
* @return true if these alleles are equal
|
||||||
*/
|
*/
|
||||||
public boolean equals(Allele other) {
|
public boolean equals(Object other) {
|
||||||
return equals(other, false);
|
return ( ! (other instanceof Allele) ? false : equals((Allele)other, false) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return hash code
|
||||||
|
*/
|
||||||
|
public int hashCode() {
|
||||||
|
int hash = 1;
|
||||||
|
for (int i = 0; i < bases.length; i++)
|
||||||
|
hash += (i+1) * bases[i];
|
||||||
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if this and other are equal. If ignoreRefState is true, then doesn't require both alleles has the
|
* Returns true if this and other are equal. If ignoreRefState is true, then doesn't require both alleles has the
|
||||||
* same ref tag
|
* same ref tag
|
||||||
*
|
*
|
||||||
* @param other
|
* @param other allele to compare to
|
||||||
* @param ignoreRefState
|
* @param ignoreRefState if true, ignore ref state in comparison
|
||||||
* @return
|
* @return true if this and other are equal
|
||||||
*/
|
*/
|
||||||
public boolean equals(Allele other, boolean ignoreRefState) {
|
public boolean equals(Allele other, boolean ignoreRefState) {
|
||||||
return (isRef == other.isRef || ignoreRefState) && isNull == other.isNull && isNoCall == other.isNoCall && this.basesMatch(other.getBases());
|
return (isRef == other.isRef || ignoreRefState) && isNull == other.isNull && isNoCall == other.isNoCall && basesMatch(other.getBases());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if this Alelle contains the same bases as test, regardless of its reference status. Also handles
|
* @param test bases to test against
|
||||||
* Null and NO_CALL alleles
|
|
||||||
*
|
*
|
||||||
* @param test
|
* @return true if this Alelle contains the same bases as test, regardless of its reference status; handles Null and NO_CALL alleles
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public boolean basesMatch(byte[] test) { return bases == test || Arrays.equals(bases, test); }
|
public boolean basesMatch(byte[] test) { return bases == test || Arrays.equals(bases, test); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if this Alelle contains the same bases as test, regardless of its reference status. Also handles
|
* @param test bases to test against
|
||||||
* Null and NO_CALL alleles
|
|
||||||
*
|
*
|
||||||
* @param test
|
* @return true if this Alelle contains the same bases as test, regardless of its reference status; handles Null and NO_CALL alleles
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public boolean basesMatch(String test) { return basesMatch(test.toUpperCase().getBytes()); }
|
public boolean basesMatch(String test) { return basesMatch(test.toUpperCase().getBytes()); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if this Alelle contains the same bases as test, regardless of its reference status. Also handles
|
* @param test allele to test against
|
||||||
* Null and NO_CALL alleles
|
|
||||||
*
|
*
|
||||||
* @param test
|
* @return true if this Alelle contains the same bases as test, regardless of its reference status; handles Null and NO_CALL alleles
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public boolean basesMatch(Allele test) { return basesMatch(test.getBases()); }
|
public boolean basesMatch(Allele test) { return basesMatch(test.getBases()); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the length of this allele. Null and NO_CALL alleles have 0 length.
|
* @return the length of this allele. Null and NO_CALL alleles have 0 length.
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public int length() {
|
public int length() {
|
||||||
return bases.length;
|
return bases.length;
|
||||||
|
|
|
||||||
|
|
@ -227,9 +227,8 @@ public class VariantContextAdaptors {
|
||||||
|
|
||||||
String filters = vc.isFiltered() ? Utils.join(";", Utils.sorted(vc.getFilters())) : (filtersWereAppliedToContext ? VCFRecord.PASSES_FILTERS : VCFRecord.UNFILTERED);
|
String filters = vc.isFiltered() ? Utils.join(";", Utils.sorted(vc.getFilters())) : (filtersWereAppliedToContext ? VCFRecord.PASSES_FILTERS : VCFRecord.UNFILTERED);
|
||||||
|
|
||||||
// TODO - fixme: temporarily using Strings as keys because Alleles aren't hashing correctly
|
Map<Allele, VCFGenotypeEncoding> alleleMap = new HashMap<Allele, VCFGenotypeEncoding>();
|
||||||
Map<String, VCFGenotypeEncoding> alleleMap = new HashMap<String, VCFGenotypeEncoding>();
|
alleleMap.put(Allele.NO_CALL, new VCFGenotypeEncoding(VCFGenotypeRecord.EMPTY_ALLELE)); // convenience for lookup
|
||||||
alleleMap.put(Allele.NO_CALL.toString(), new VCFGenotypeEncoding(VCFGenotypeRecord.EMPTY_ALLELE)); // convenience for lookup
|
|
||||||
List<VCFGenotypeEncoding> vcfAltAlleles = new ArrayList<VCFGenotypeEncoding>();
|
List<VCFGenotypeEncoding> vcfAltAlleles = new ArrayList<VCFGenotypeEncoding>();
|
||||||
for ( Allele a : vc.getAlleles() ) {
|
for ( Allele a : vc.getAlleles() ) {
|
||||||
|
|
||||||
|
|
@ -271,7 +270,7 @@ public class VariantContextAdaptors {
|
||||||
vcfAltAlleles.add(encoding);
|
vcfAltAlleles.add(encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
alleleMap.put(a.toString(), encoding);
|
alleleMap.put(a, encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( vcfGenotypeAttributeKeys == null ) {
|
if ( vcfGenotypeAttributeKeys == null ) {
|
||||||
|
|
@ -288,7 +287,7 @@ public class VariantContextAdaptors {
|
||||||
List<VCFGenotypeEncoding> encodings = new ArrayList<VCFGenotypeEncoding>(g.getPloidy());
|
List<VCFGenotypeEncoding> encodings = new ArrayList<VCFGenotypeEncoding>(g.getPloidy());
|
||||||
|
|
||||||
for ( Allele a : g.getAlleles() ) {
|
for ( Allele a : g.getAlleles() ) {
|
||||||
encodings.add(alleleMap.get(a.toString()));
|
encodings.add(alleleMap.get(a));
|
||||||
}
|
}
|
||||||
|
|
||||||
VCFGenotypeRecord.PHASE phasing = g.genotypesArePhased() ? VCFGenotypeRecord.PHASE.PHASED : VCFGenotypeRecord.PHASE.UNPHASED;
|
VCFGenotypeRecord.PHASE phasing = g.genotypesArePhased() ? VCFGenotypeRecord.PHASE.PHASED : VCFGenotypeRecord.PHASE.UNPHASED;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue