Pre-cleanup commit of Sample and SampleDataSource

-- SampleDataSource has all reader functionality disabled
This commit is contained in:
Mark DePristo 2011-09-29 09:44:18 -04:00
parent 4d31673cc5
commit e197dcd1f3
2 changed files with 290 additions and 281 deletions

View File

@ -14,6 +14,13 @@ import java.util.Map;
* Time: 3:31:38 PM * Time: 3:31:38 PM
*/ */
public class Sample implements java.io.Serializable { public class Sample implements java.io.Serializable {
private final static String MOTHER = "mother";
private final static String FATHER = "father";
private final static String GENDER = "gender";
private final static String POPULATION = "population";
private final static String FAMILY = "familyId";
private final static String AFFECTION = "affection";
private final static String QUANT_TRAIT = "quantTrait";
private final String id; private final String id;
@ -31,6 +38,18 @@ public class Sample implements java.io.Serializable {
UNKNOWN UNKNOWN
} }
public enum Affection {
/** Status is unknown */
UNKNOWN,
/** Suffers from the disease */
AFFECTED,
/** Unaffected by the disease */
UNAFFECTED,
/** A quantitative trait: value of the trait is stored elsewhere */
QUANTITATIVE
}
public final static double UNSET_QUANTITIATIVE_TRAIT_VALUE = Double.NaN;
public Sample(String id) { public Sample(String id) {
/* if (id == null) { /* if (id == null) {
throw new StingException("Error creating sample: sample ID cannot be null"); throw new StingException("Error creating sample: sample ID cannot be null");
@ -46,30 +65,21 @@ public class Sample implements java.io.Serializable {
return properties; return properties;
} }
public void setProperties(Map<String, Object> properties) { @Deprecated
this.properties = (HashMap) properties;
}
public Map<String,Sample> getRelationships() {
return Collections.unmodifiableMap(this.relationships);
}
public void setSampleFileEntry(boolean value) { public void setSampleFileEntry(boolean value) {
this.hasSampleFileEntry = value; this.hasSampleFileEntry = value;
} }
@Deprecated
public boolean hasSAMFileEntry() { public boolean hasSAMFileEntry() {
return this.hasSAMFileEntry; return this.hasSAMFileEntry;
} }
@Deprecated
public void setSAMFileEntry(boolean value) { public void setSAMFileEntry(boolean value) {
this.hasSAMFileEntry = value; this.hasSAMFileEntry = value;
} }
public boolean hasSampleFileEntry() {
return this.hasSampleFileEntry;
}
/** /**
* Get one property * Get one property
* @param key key of property * @param key key of property
@ -91,11 +101,11 @@ public class Sample implements java.io.Serializable {
throw new StingException("The same key cannot exist as a property and a relationship"); throw new StingException("The same key cannot exist as a property and a relationship");
} }
if (key.equals("gender") && value.getClass() != Gender.class) { if (key.equals(GENDER) && value.getClass() != Gender.class) {
throw new StingException("'gender' property must be of type Sample.Gender"); throw new StingException("'gender' property must be of type Sample.Gender");
} }
if (key.equals("population") && value.getClass() != String.class) { if (key.equals(POPULATION) && value.getClass() != String.class) {
throw new StingException("'population' property must be of type String"); throw new StingException("'population' property must be of type String");
} }
@ -129,7 +139,7 @@ public class Sample implements java.io.Serializable {
* @return sample object with relationship mother, if exists, or null * @return sample object with relationship mother, if exists, or null
*/ */
public Sample getMother() { public Sample getMother() {
return getRelationship("mother"); return getRelationship(MOTHER);
} }
/** /**
@ -137,7 +147,7 @@ public class Sample implements java.io.Serializable {
* @return sample object with relationship father, if exists, or null * @return sample object with relationship father, if exists, or null
*/ */
public Sample getFather() { public Sample getFather() {
return getRelationship("father"); return getRelationship(FATHER);
} }
/** /**
@ -145,29 +155,29 @@ public class Sample implements java.io.Serializable {
* @return property of key "gender" - must be of type Gender * @return property of key "gender" - must be of type Gender
*/ */
public Gender getGender() { public Gender getGender() {
return (Gender) properties.get("gender"); return (Gender) properties.get(GENDER);
} }
public String getPopulation() { public String getPopulation() {
return (String) properties.get("population"); return (String) properties.get(POPULATION);
} }
public String getFamilyId() { public String getFamilyId() {
return (String) properties.get("familyId"); return (String) properties.get(FAMILY);
} }
/** /**
* @return True if sample is male, false if female, unknown, or null * @return True if sample is male, false if female, unknown, or null
*/ */
public boolean isMale() { public boolean isMale() {
return properties.get("gender") == Gender.MALE; return properties.get(GENDER) == Gender.MALE;
} }
/** /**
* @return True if sample is female, false if male, unknown or null * @return True if sample is female, false if male, unknown or null
*/ */
public boolean isFemale() { public boolean isFemale() {
return properties.get("gender") == Gender.MALE; return properties.get(GENDER) == Gender.MALE;
} }
/** /**

View File

@ -104,266 +104,265 @@ public class SampleDataSource {
* Parse one sample file and integrate it with samples that are already there * Parse one sample file and integrate it with samples that are already there
* Fail quickly if we find any errors in the file * Fail quickly if we find any errors in the file
*/ */
public void addFile(File sampleFile) { public void addFile(File sampleFile) {}
//
BufferedReader reader; // BufferedReader reader;
try { // try {
reader = new BufferedReader(new FileReader(sampleFile)); // reader = new BufferedReader(new FileReader(sampleFile));
} // }
catch (IOException e) { // catch (IOException e) {
throw new StingException("Could not open sample file " + sampleFile.getAbsolutePath(), e); // throw new StingException("Could not open sample file " + sampleFile.getAbsolutePath(), e);
} // }
//
// set up YAML reader - a "Constructor" creates java object from YAML and "Loader" loads the file // // set up YAML reader - a "Constructor" creates java object from YAML and "Loader" loads the file
Constructor con = new Constructor(SampleFileParser.class); // Constructor con = new Constructor(SampleFileParser.class);
TypeDescription desc = new TypeDescription(SampleFileParser.class); // TypeDescription desc = new TypeDescription(SampleFileParser.class);
desc.putListPropertyType("propertyDefinitions", PropertyDefinition.class); // desc.putListPropertyType("propertyDefinitions", PropertyDefinition.class);
desc.putListPropertyType("sampleAliases", SampleAlias.class); // desc.putListPropertyType("sampleAliases", SampleAlias.class);
con.addTypeDescription(desc); // con.addTypeDescription(desc);
Yaml yaml = new Yaml(con); // Yaml yaml = new Yaml(con);
//
// SampleFileParser stores an object representation of a sample file - this is what we'll parse // // SampleFileParser stores an object representation of a sample file - this is what we'll parse
SampleFileParser parser; // SampleFileParser parser;
try { // try {
parser = (SampleFileParser) yaml.load(reader); // parser = (SampleFileParser) yaml.load(reader);
} // }
catch (Exception e) { // catch (Exception e) {
throw new StingException("There was a syntactic error with the YAML in sample file " + sampleFile.getAbsolutePath(), e); // throw new StingException("There was a syntactic error with the YAML in sample file " + sampleFile.getAbsolutePath(), e);
} // }
//
// check to see which validation options were built into the file // // check to see which validation options were built into the file
boolean restrictProperties = parser.getAllowedProperties() != null; // boolean restrictProperties = parser.getAllowedProperties() != null;
boolean restrictRelationships = parser.getAllowedRelationships() != null; // boolean restrictRelationships = parser.getAllowedRelationships() != null;
boolean restrictPropertyValues = parser.getPropertyDefinitions() != null; // boolean restrictPropertyValues = parser.getPropertyDefinitions() != null;
//
// propertyValues stores the values that are allowed for a given property // // propertyValues stores the values that are allowed for a given property
HashMap<String, HashSet> propertyValues = null; // HashMap<String, HashSet> propertyValues = null;
if (restrictPropertyValues) { // if (restrictPropertyValues) {
propertyValues = new HashMap<String, HashSet>(); // propertyValues = new HashMap<String, HashSet>();
for (PropertyDefinition def : parser.getPropertyDefinitions()) { // for (PropertyDefinition def : parser.getPropertyDefinitions()) {
HashSet<String> set = new HashSet<String>(); // HashSet<String> set = new HashSet<String>();
for (String value : def.getValues()) { // for (String value : def.getValues()) {
set.add(value); // set.add(value);
} // }
propertyValues.put(def.getProperty(), set); // propertyValues.put(def.getProperty(), set);
} // }
} // }
//
// make sure the aliases are valid // // make sure the aliases are valid
validateAliases(parser); // validateAliases(parser);
//
// loop through each sample in the file - a SampleParser stores an object that will become a Sample // // loop through each sample in the file - a SampleParser stores an object that will become a Sample
for (SampleParser sampleParser : parser.getSamples()) { // for (SampleParser sampleParser : parser.getSamples()) {
//
try { // try {
// step 1: add the sample if it doesn't already exist // // step 1: add the sample if it doesn't already exist
Sample sample = getSampleById(sampleParser.getId()); // Sample sample = getSampleById(sampleParser.getId());
if (sample == null) { // if (sample == null) {
sample = new Sample(sampleParser.getId()); // sample = new Sample(sampleParser.getId());
} // }
addSample(sample); // addSample(sample);
sample.setSampleFileEntry(true); // sample.setSampleFileEntry(true);
//
// step 2: add the properties // // step 2: add the properties
if (sampleParser.getProperties() != null) { // if (sampleParser.getProperties() != null) {
for (String property : sampleParser.getProperties().keySet()) { // for (String property : sampleParser.getProperties().keySet()) {
//
// check that property is allowed // // check that property is allowed
if (restrictProperties) { // if (restrictProperties) {
if (!isPropertyValid(property, parser.getAllowedProperties())) { // if (!isPropertyValid(property, parser.getAllowedProperties())) {
throw new StingException(property + " is an invalid property. It is not included in the list " + // throw new StingException(property + " is an invalid property. It is not included in the list " +
"of allowed properties."); // "of allowed properties.");
} // }
} // }
//
// next check that the value is allowed // // next check that the value is allowed
if (restrictPropertyValues) { // if (restrictPropertyValues) {
if (!isValueAllowed(property, sampleParser.getProperties().get(property), propertyValues)) { // if (!isValueAllowed(property, sampleParser.getProperties().get(property), propertyValues)) {
throw new StingException("The value of property '" + property + "' is invalid. " + // throw new StingException("The value of property '" + property + "' is invalid. " +
"It is not included in the list of allowed values for this property."); // "It is not included in the list of allowed values for this property.");
} // }
} // }
//
// next check that there isn't already a conflicting property there // // next check that there isn't already a conflicting property there
if (sample.getProperty(property) != null && // if (sample.getProperty(property) != null &&
sample.getProperty(property) != sampleParser.getProperties().get(property)) // sample.getProperty(property) != sampleParser.getProperties().get(property))
{ // {
throw new StingException(property + " is a conflicting property!"); // throw new StingException(property + " is a conflicting property!");
} // }
//
// checks are passed - now add the property! // // checks are passed - now add the property!
saveProperty(sample, property, sampleParser.getProperties().get(property)); // saveProperty(sample, property, sampleParser.getProperties().get(property));
} // }
} // }
//
// step 3: add the relationships // // step 3: add the relationships
if (sampleParser.getRelationships() != null) { // if (sampleParser.getRelationships() != null) {
for (String relationship : sampleParser.getRelationships().keySet()) { // for (String relationship : sampleParser.getRelationships().keySet()) {
String relativeId = sampleParser.getRelationships().get(relationship); // String relativeId = sampleParser.getRelationships().get(relationship);
if (relativeId == null) { // if (relativeId == null) {
throw new StingException("The relationship cannot be null"); // throw new StingException("The relationship cannot be null");
} // }
//
// first check that it's not invalid // // first check that it's not invalid
if (restrictRelationships) { // if (restrictRelationships) {
if (!isRelationshipValid(relationship, parser.getAllowedRelationships())) { // if (!isRelationshipValid(relationship, parser.getAllowedRelationships())) {
throw new StingException(relationship + " is an invalid relationship"); // throw new StingException(relationship + " is an invalid relationship");
} // }
} // }
//
// next check that there isn't already a conflicting property there // // next check that there isn't already a conflicting property there
if (sample.getRelationship(relationship) != null) { // if (sample.getRelationship(relationship) != null) {
if (sample.getRelationship(relationship).getId() != sampleParser.getProperties().get(relationship)) { // if (sample.getRelationship(relationship).getId() != sampleParser.getProperties().get(relationship)) {
throw new StingException(relationship + " is a conflicting relationship!"); // throw new StingException(relationship + " is a conflicting relationship!");
} // }
// if the relationship is already set - and consistent with what we're reading now - no need to continue // // if the relationship is already set - and consistent with what we're reading now - no need to continue
else { // else {
continue; // continue;
} // }
} // }
//
// checks are passed - now save the relationship // // checks are passed - now save the relationship
saveRelationship(sample, relationship, relativeId); // saveRelationship(sample, relationship, relativeId);
} // }
} // }
} catch (Exception e) { // } catch (Exception e) {
throw new StingException("An error occurred while loading this sample from the sample file: " + // throw new StingException("An error occurred while loading this sample from the sample file: " +
sampleParser.getId(), e); // sampleParser.getId(), e);
} // }
} // }
// }
} //
// private boolean isValueAllowed(String key, Object value, HashMap<String, HashSet> valuesList) {
private boolean isValueAllowed(String key, Object value, HashMap<String, HashSet> valuesList) { //
// // if the property values weren't specified for this property, then any value is okay
// if the property values weren't specified for this property, then any value is okay // if (!valuesList.containsKey(key)) {
if (!valuesList.containsKey(key)) { // return true;
return true; // }
} //
// // if this property has enumerated values, it must be a string
// if this property has enumerated values, it must be a string // else if (value.getClass() != String.class)
else if (value.getClass() != String.class) // return false;
return false; //
// // is the value specified or not?
// is the value specified or not? // else if (!valuesList.get(key).contains(value))
else if (!valuesList.get(key).contains(value)) // return false;
return false; //
// return true;
return true; // }
} //
// /**
/** // * Makes sure that the aliases are valid
* Makes sure that the aliases are valid // * Checks that 1) no string is used as both a main ID and an alias;
* Checks that 1) no string is used as both a main ID and an alias; // * 2) no alias is used more than once
* 2) no alias is used more than once // * @param parser
* @param parser // */
*/ // private void validateAliases(SampleFileParser parser) {
private void validateAliases(SampleFileParser parser) { //
// // no aliases sure validate
// no aliases sure validate // if (parser.getSampleAliases() == null)
if (parser.getSampleAliases() == null) // return;
return; //
// HashSet<String> mainIds = new HashSet<String>();
HashSet<String> mainIds = new HashSet<String>(); // HashSet<String> otherIds = new HashSet<String>();
HashSet<String> otherIds = new HashSet<String>(); //
// for (SampleAlias sampleAlias : parser.getSampleAliases()) {
for (SampleAlias sampleAlias : parser.getSampleAliases()) { // mainIds.add(sampleAlias.getMainId());
mainIds.add(sampleAlias.getMainId()); // for (String otherId : sampleAlias.getOtherIds()) {
for (String otherId : sampleAlias.getOtherIds()) { // if (mainIds.contains(otherId))
if (mainIds.contains(otherId)) // throw new StingException(String.format("The aliases in your sample file are invalid - the alias %s cannot " +
throw new StingException(String.format("The aliases in your sample file are invalid - the alias %s cannot " + // "be both a main ID and an other ID", otherId));
"be both a main ID and an other ID", otherId)); //
// if (!otherIds.add(otherId))
if (!otherIds.add(otherId)) // throw new StingException(String.format("The aliases in your sample file are invalid - %s is listed as an " +
throw new StingException(String.format("The aliases in your sample file are invalid - %s is listed as an " + // "alias more than once.", otherId));
"alias more than once.", otherId)); // }
} // }
} // }
} //
// private boolean isPropertyValid(String property, String[] allowedProperties) {
private boolean isPropertyValid(String property, String[] allowedProperties) { //
// // is it a special property that is always allowed?
// is it a special property that is always allowed? // for (String allowedProperty : specialProperties) {
for (String allowedProperty : specialProperties) { // if (property.equals(allowedProperty))
if (property.equals(allowedProperty)) // return true;
return true; // }
} //
// // is it in the allowed properties list?
// is it in the allowed properties list? // for (String allowedProperty : allowedProperties) {
for (String allowedProperty : allowedProperties) { // if (property.equals(allowedProperty))
if (property.equals(allowedProperty)) // return true;
return true; // }
} //
// return false;
return false; // }
} //
// private boolean isRelationshipValid(String relationship, String[] allowedRelationships) {
private boolean isRelationshipValid(String relationship, String[] allowedRelationships) { //
// // is it a special relationship that is always allowed?
// is it a special relationship that is always allowed? // for (String allowedRelationship : specialRelationships) {
for (String allowedRelationship : specialRelationships) { // if (relationship.equals(allowedRelationship))
if (relationship.equals(allowedRelationship)) // return true;
return true; // }
} //
// // is it in the allowed properties list?
// is it in the allowed properties list? // for (String allowedRelationship : allowedRelationships) {
for (String allowedRelationship : allowedRelationships) { // if (relationship.equals(allowedRelationship))
if (relationship.equals(allowedRelationship)) // return true;
return true; // }
} //
// return false;
return false; // }
} //
// /**
/** // * Saves a property as the correct type
* Saves a property as the correct type // * @param key property key
* @param key property key // * @param value property value, as read from YAML parser
* @param value property value, as read from YAML parser // * @return property value to be stored
* @return property value to be stored // */
*/ // private void saveProperty(Sample sample, String key, Object value) {
private void saveProperty(Sample sample, String key, Object value) { //
// // convert gender to the right type, if it was stored as a String
// convert gender to the right type, if it was stored as a String // if (key.equals("gender")) {
if (key.equals("gender")) { // if (((String) value).toLowerCase().equals("male")) {
if (((String) value).toLowerCase().equals("male")) { // value = Sample.Gender.MALE;
value = Sample.Gender.MALE; // }
} // else if (((String) value).toLowerCase().equals("female")) {
else if (((String) value).toLowerCase().equals("female")) { // value = Sample.Gender.FEMALE;
value = Sample.Gender.FEMALE; // }
} // else if (((String) value).toLowerCase().equals("unknown")) {
else if (((String) value).toLowerCase().equals("unknown")) { // value = Sample.Gender.UNKNOWN;
value = Sample.Gender.UNKNOWN; // }
} // else if (value != null) {
else if (value != null) { // throw new StingException("'gender' property must be male, female, or unknown.");
throw new StingException("'gender' property must be male, female, or unknown."); // }
} // }
} // try {
try { // sample.setProperty(key, value);
sample.setProperty(key, value); // }
} // catch (Exception e) {
catch (Exception e) { // throw new StingException("Could not save property " + key, e);
throw new StingException("Could not save property " + key, e); // }
} // }
} //
// /**
/** // * Saves a relationship as the correct type
* Saves a relationship as the correct type // * @param key relationship key
* @param key relationship key // * @param relativeId sample ID string of the relative
* @param relativeId sample ID string of the relative // * @return relationship value to be stored
* @return relationship value to be stored // */
*/ // private void saveRelationship(Sample sample, String key, String relativeId) {
private void saveRelationship(Sample sample, String key, String relativeId) { //
// // get the reference that we'll store as the value
// get the reference that we'll store as the value // Sample relative = getSampleById(relativeId);
Sample relative = getSampleById(relativeId); //
// // create sample object for the relative, if necessary
// create sample object for the relative, if necessary // if (relative == null) {
if (relative == null) { // relative = new Sample(relativeId);
relative = new Sample(relativeId); // addSample(relative);
addSample(relative); // }
} // sample.setRelationship(key, relative);
sample.setRelationship(key, relative); // }
}