Skip to content
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
merged 4 commits into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scripts/sv/run_whole_pipeline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ case ${GATK_SV_TOOL} in
--breakpoint-intervals ${PROJECT_OUTPUT_DIR}/intervals \
--high-coverage-intervals "${PROJECT_OUTPUT_DIR}/highCoverageIntervals.bed" \
--fastq-dir ${PROJECT_OUTPUT_DIR}/fastq \
--read-metadata ${PROJECT_OUTPUT_DIR}/read-metadata.txt \
--contig-sam-file ${PROJECT_OUTPUT_DIR}/assemblies.bam \
--target-link-file ${PROJECT_OUTPUT_DIR}/target_links.bedpe \
--exp-interpret"
Expand Down
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 {
Copy link
Contributor

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.


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);
}
}
Loading