-
Notifications
You must be signed in to change notification settings - Fork 596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds a class to represent expected insert-size distribution (normal a… #4827
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f8c54d0
Adds a class to represent expected insert-size distribution (normal a…
vruano f04ffd8
Addressed comments, added support for empirical distribution in read-…
vruano 32cbb11
Adds the hability of reading serialized forms of the read-metadata da…
vruano b5a198a
Added the Empirical Insert-Size-Distribution Shape (arbitrary fractio…
vruano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/main/java/org/broadinstitute/hellbender/tools/spark/sv/InsertSizeDistribution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package org.broadinstitute.hellbender.tools.spark.sv; | ||
|
||
import org.apache.commons.math3.distribution.AbstractIntegerDistribution; | ||
import org.apache.commons.math3.distribution.IntegerDistribution; | ||
import org.broadinstitute.hellbender.exceptions.UserException; | ||
|
||
import java.io.Serializable; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Holds the information characterizing and insert size distribution. | ||
*/ | ||
public class InsertSizeDistribution implements Serializable { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private static Pattern DESCRIPTION_PATTERN = | ||
Pattern.compile("^\\s*(?<name>[^\\s\\(\\)]+)\\s*\\((?<mean>[^,\\(\\)]+?)\\s*(?:,\\s*(?<stddev>[^,\\(\\)]+?)\\s*)?\\)\\s*"); | ||
|
||
private final String description; | ||
|
||
private transient AbstractIntegerDistribution dist; | ||
|
||
private AbstractIntegerDistribution dist() { | ||
initializeDistribution(); | ||
return dist; | ||
} | ||
|
||
public double mean() { | ||
return dist().getNumericalMean(); | ||
} | ||
|
||
public double variance() { return dist().getNumericalVariance(); } | ||
|
||
public double stddev() { return Math.sqrt(variance()); } | ||
|
||
public InsertSizeDistribution(final String distrString) { | ||
this.description = distrString; | ||
initializeDistribution(); | ||
} | ||
|
||
private void initializeDistribution() { | ||
if (!description.matches(DESCRIPTION_PATTERN.pattern())) { | ||
throw new UserException.BadInput("unsupported insert size distribution description format: " + description); | ||
} | ||
final Matcher matcher = DESCRIPTION_PATTERN.matcher(description); | ||
if (!matcher.find()) { | ||
throw new UserException.BadInput("the insert-size distribution spec is not up to standard: " + description); | ||
} | ||
final String nameString = matcher.group("name"); | ||
final String meanString = matcher.group("mean"); | ||
final String stddevString = matcher.group("stddev"); | ||
final InsertSizeDistributionShape type = extractDistributionShape(nameString, description); | ||
if (stddevString != null) { | ||
final double mean = extractDoubleParameter("mean", description, meanString, 0, Double.MAX_VALUE); | ||
final double stddev = extractDoubleParameter("stddev", description, stddevString, 0, Double.MAX_VALUE); | ||
dist = type.fromMeanAndStdDeviation(mean, stddev); | ||
} else { | ||
dist = type.fromReadMetadataFile(meanString); | ||
} | ||
} | ||
|
||
private static InsertSizeDistributionShape extractDistributionShape(final String nameString, final String description) { | ||
final InsertSizeDistributionShape result = InsertSizeDistributionShape.decode(nameString); | ||
if (result == null) { | ||
throw new UserException.BadInput("unsupported insert size distribution name '" + nameString | ||
+ "' in description: " + description); | ||
} | ||
return result; | ||
} | ||
|
||
private static double extractDoubleParameter(final String name, final String description, | ||
final String valueString, final double min, | ||
final double max) { | ||
final double value; | ||
try { | ||
value = Double.parseDouble(valueString); | ||
} catch (final NumberFormatException ex) { | ||
throw new UserException.BadInput("bad " + name + " string '" + valueString + "' in insert size distribution description: " + description); | ||
} | ||
if (value < min || Double.isInfinite(value) || Double.isNaN(value) || value > max) { | ||
throw new UserException.BadInput("bad " + name + " value '" + value + "' in insert size distribution description: " + description); | ||
} | ||
return value; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return description; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return dist().hashCode(); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object obj) { | ||
if (!(obj instanceof InsertSizeDistribution)) { | ||
return false; | ||
} else { | ||
return ((InsertSizeDistribution)obj).dist().equals(dist()); | ||
} | ||
} | ||
|
||
public int minimum() { | ||
return Math.max(0, dist().getSupportLowerBound()); | ||
} | ||
|
||
public int maximum() { | ||
return Math.min(Integer.MAX_VALUE, dist().getSupportUpperBound()); | ||
} | ||
|
||
public double probability(final int size) { | ||
return dist().probability(size); | ||
} | ||
|
||
public double logProbability(final int size) { | ||
return dist().logProbability(size); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we had a convention for class layout that put all fields first, then constructors, then other stuff. I could be wrong.