From 8f8f339e4bd4106f95552d86c6b0368d228729b8 Mon Sep 17 00:00:00 2001 From: Mauricio Carneiro Date: Tue, 23 Apr 2013 18:02:27 -0400 Subject: [PATCH] Abstract class for the statistics Addressing the code duplication issue raised by Mark. --- .../diagnosetargets/AbstractStatistics.java | 150 ++++++++++++++++++ .../diagnosetargets/DiagnoseTargets.java | 8 +- .../diagnostics/diagnosetargets/Interval.java | 4 +- .../diagnosetargets/IntervalStatistics.java | 53 ++----- .../diagnostics/diagnosetargets/Locus.java | 4 +- .../diagnosetargets/LocusCoverageGap.java | 3 +- .../LocusExcessiveCoverage.java | 3 +- .../diagnosetargets/LocusLowCoverage.java | 5 +- .../diagnosetargets/LocusPoorQuality.java | 3 +- .../diagnosetargets/LocusStatistics.java | 28 ++-- .../diagnosetargets/PluginUtils.java | 2 +- .../diagnostics/diagnosetargets/Sample.java | 4 +- .../diagnosetargets/SampleBadMates.java | 3 +- .../diagnosetargets/SampleNoReads.java | 9 +- .../diagnosetargets/SampleStatistics.java | 132 +++++---------- .../diagnosetargets/Statistic.java | 57 +++++++ .../diagnosetargets/ThresHolder.java | 6 +- 17 files changed, 304 insertions(+), 170 deletions(-) create mode 100644 protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/AbstractStatistics.java create mode 100644 protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/Statistic.java diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/AbstractStatistics.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/AbstractStatistics.java new file mode 100644 index 000000000..0ac083bb6 --- /dev/null +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/AbstractStatistics.java @@ -0,0 +1,150 @@ +/* +* By downloading the PROGRAM you agree to the following terms of use: +* +* BROAD INSTITUTE - SOFTWARE LICENSE AGREEMENT - FOR ACADEMIC NON-COMMERCIAL RESEARCH PURPOSES ONLY +* +* This Agreement is made between the Broad Institute, Inc. with a principal address at 7 Cambridge Center, Cambridge, MA 02142 (BROAD) and the LICENSEE and is effective at the date the downloading is completed (EFFECTIVE DATE). +* +* WHEREAS, LICENSEE desires to license the PROGRAM, as defined hereinafter, and BROAD wishes to have this PROGRAM utilized in the public interest, subject only to the royalty-free, nonexclusive, nontransferable license rights of the United States Government pursuant to 48 CFR 52.227-14; and +* WHEREAS, LICENSEE desires to license the PROGRAM and BROAD desires to grant a license on the following terms and conditions. +* NOW, THEREFORE, in consideration of the promises and covenants made herein, the parties hereto agree as follows: +* +* 1. DEFINITIONS +* 1.1 PROGRAM shall mean copyright in the object code and source code known as GATK2 and related documentation, if any, as they exist on the EFFECTIVE DATE and can be downloaded from http://www.broadinstitute/GATK on the EFFECTIVE DATE. +* +* 2. LICENSE +* 2.1 Grant. Subject to the terms of this Agreement, BROAD hereby grants to LICENSEE, solely for academic non-commercial research purposes, a non-exclusive, non-transferable license to: (a) download, execute and display the PROGRAM and (b) create bug fixes and modify the PROGRAM. +* The LICENSEE may apply the PROGRAM in a pipeline to data owned by users other than the LICENSEE and provide these users the results of the PROGRAM provided LICENSEE does so for academic non-commercial purposes only. For clarification purposes, academic sponsored research is not a commercial use under the terms of this Agreement. +* 2.2 No Sublicensing or Additional Rights. LICENSEE shall not sublicense or distribute the PROGRAM, in whole or in part, without prior written permission from BROAD. LICENSEE shall ensure that all of its users agree to the terms of this Agreement. LICENSEE further agrees that it shall not put the PROGRAM on a network, server, or other similar technology that may be accessed by anyone other than the LICENSEE and its employees and users who have agreed to the terms of this agreement. +* 2.3 License Limitations. Nothing in this Agreement shall be construed to confer any rights upon LICENSEE by implication, estoppel, or otherwise to any computer software, trademark, intellectual property, or patent rights of BROAD, or of any other entity, except as expressly granted herein. LICENSEE agrees that the PROGRAM, in whole or part, shall not be used for any commercial purpose, including without limitation, as the basis of a commercial software or hardware product or to provide services. LICENSEE further agrees that the PROGRAM shall not be copied or otherwise adapted in order to circumvent the need for obtaining a license for use of the PROGRAM. +* +* 3. OWNERSHIP OF INTELLECTUAL PROPERTY +* LICENSEE acknowledges that title to the PROGRAM shall remain with BROAD. The PROGRAM is marked with the following BROAD copyright notice and notice of attribution to contributors. LICENSEE shall retain such notice on all copies. LICENSEE agrees to include appropriate attribution if any results obtained from use of the PROGRAM are included in any publication. +* Copyright 2012 Broad Institute, Inc. +* Notice of attribution: The GATK2 program was made available through the generosity of Medical and Population Genetics program at the Broad Institute, Inc. +* LICENSEE shall not use any trademark or trade name of BROAD, or any variation, adaptation, or abbreviation, of such marks or trade names, or any names of officers, faculty, students, employees, or agents of BROAD except as states above for attribution purposes. +* +* 4. INDEMNIFICATION +* LICENSEE shall indemnify, defend, and hold harmless BROAD, and their respective officers, faculty, students, employees, associated investigators and agents, and their respective successors, heirs and assigns, (Indemnitees), against any liability, damage, loss, or expense (including reasonable attorneys fees and expenses) incurred by or imposed upon any of the Indemnitees in connection with any claims, suits, actions, demands or judgments arising out of any theory of liability (including, without limitation, actions in the form of tort, warranty, or strict liability and regardless of whether such action has any factual basis) pursuant to any right or license granted under this Agreement. +* +* 5. NO REPRESENTATIONS OR WARRANTIES +* THE PROGRAM IS DELIVERED AS IS. BROAD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE PROGRAM OR THE COPYRIGHT, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, WHETHER OR NOT DISCOVERABLE. BROAD EXTENDS NO WARRANTIES OF ANY KIND AS TO PROGRAM CONFORMITY WITH WHATEVER USER MANUALS OR OTHER LITERATURE MAY BE ISSUED FROM TIME TO TIME. +* IN NO EVENT SHALL BROAD OR ITS RESPECTIVE DIRECTORS, OFFICERS, EMPLOYEES, AFFILIATED INVESTIGATORS AND AFFILIATES BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, INCLUDING, WITHOUT LIMITATION, ECONOMIC DAMAGES OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF WHETHER BROAD SHALL BE ADVISED, SHALL HAVE OTHER REASON TO KNOW, OR IN FACT SHALL KNOW OF THE POSSIBILITY OF THE FOREGOING. +* +* 6. ASSIGNMENT +* This Agreement is personal to LICENSEE and any rights or obligations assigned by LICENSEE without the prior written consent of BROAD shall be null and void. +* +* 7. MISCELLANEOUS +* 7.1 Export Control. LICENSEE gives assurance that it will comply with all United States export control laws and regulations controlling the export of the PROGRAM, including, without limitation, all Export Administration Regulations of the United States Department of Commerce. Among other things, these laws and regulations prohibit, or require a license for, the export of certain types of software to specified countries. +* 7.2 Termination. LICENSEE shall have the right to terminate this Agreement for any reason upon prior written notice to BROAD. If LICENSEE breaches any provision hereunder, and fails to cure such breach within thirty (30) days, BROAD may terminate this Agreement immediately. Upon termination, LICENSEE shall provide BROAD with written assurance that the original and all copies of the PROGRAM have been destroyed, except that, upon prior written authorization from BROAD, LICENSEE may retain a copy for archive purposes. +* 7.3 Survival. The following provisions shall survive the expiration or termination of this Agreement: Articles 1, 3, 4, 5 and Sections 2.2, 2.3, 7.3, and 7.4. +* 7.4 Notice. Any notices under this Agreement shall be in writing, shall specifically refer to this Agreement, and shall be sent by hand, recognized national overnight courier, confirmed facsimile transmission, confirmed electronic mail, or registered or certified mail, postage prepaid, return receipt requested. All notices under this Agreement shall be deemed effective upon receipt. +* 7.5 Amendment and Waiver; Entire Agreement. This Agreement may be amended, supplemented, or otherwise modified only by means of a written instrument signed by all parties. Any waiver of any rights or failure to act in a specific instance shall relate only to such instance and shall not be construed as an agreement to waive any rights or fail to act in any other instance, whether or not similar. This Agreement constitutes the entire agreement among the parties with respect to its subject matter and supersedes prior agreements or understandings between the parties relating to its subject matter. +* 7.6 Binding Effect; Headings. This Agreement shall be binding upon and inure to the benefit of the parties and their respective permitted successors and assigns. All headings are for convenience only and shall not affect the meaning of any provision of this Agreement. +* 7.7 Governing Law. This Agreement shall be construed, governed, interpreted and applied in accordance with the internal laws of the Commonwealth of Massachusetts, U.S.A., without regard to conflict of laws principles. +*/ + +package org.broadinstitute.sting.gatk.walkers.diagnostics.diagnosetargets; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +/** + * Generic code for Diagnose Target Statistics + * + * @author Mauricio Carneiro + * @since 4/23/13 + */ +abstract class AbstractStatistics { + + private long preComputedTotalCoverage = -1; + private Map statusTally = null; + protected ThresHolder thresholds; + + /** + * Calculates the average "good" coverage of this sample. Good means "passes the base and + * mapping quality requirements. + * + * @return the average "good" coverage + */ + public double averageCoverage(final int size) { + if (preComputedTotalCoverage < 0) + preComputedTotalCoverage = calculateTotalCoverage(getElements()); + return (double) preComputedTotalCoverage / size; + } + + /** + * Calculates the total "good" coverage of this sample. Good means "passes the base and + * mapping quality requirements. + * + * @return the total "good" coverage across the interval for this sample + */ + public long getCoverage() { + if (preComputedTotalCoverage < 0) + preComputedTotalCoverage = calculateTotalCoverage(getElements()); + return preComputedTotalCoverage; + } + + + /** + * This is how the extending class will calculate it's own total coverage + * + * @return the total coverage + */ + private long calculateTotalCoverage(Iterable elements) { + long cov = 0; + for (AbstractStatistics element : elements) { + cov += element.getCoverage(); + } + return cov; + } + + /** + * What are the list of elements in your class? For example: + * + * IntervalStatistics => List + * SampleStatistics => List + * + * @return the corresponding list of elements of the extending class + */ + public abstract Iterable getElements(); + + /** + * Calculates the Callable statuses for the statistic as a whole (interval, sample or locus) + * + * @return the callable status(es) for the whole object + */ + public abstract Iterable callableStatuses(); + + + /** + * Tally up all the callable status of all the loci in this sample. + * + * @return a map of callable status and counts + */ + public Map getStatusTally() { + if (statusTally == null) { + statusTally = new HashMap(CallableStatus.values().length); + for (AbstractStatistics stats : getElements()) { + for (CallableStatus status : stats.callableStatuses()) { + statusTally.put(status, !statusTally.containsKey(status) ? 1 : statusTally.get(status) + 1); + } + } + } + return statusTally; + } + + public static List queryStatus(List statList, AbstractStatistics stratification) { + List output = new LinkedList(); + for (Statistic stat : statList) { + final CallableStatus status = stat.status(stratification); + if (status != null) { + output.add(status); + } + } + return output; + } + +} diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/DiagnoseTargets.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/DiagnoseTargets.java index 7ecbe2f21..78a30a7bd 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/DiagnoseTargets.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/DiagnoseTargets.java @@ -259,14 +259,14 @@ public class DiagnoseTargets extends LocusWalker { vcb.filters(new LinkedHashSet(statusToStrings(stats.callableStatuses(), true))); attributes.put(VCFConstants.END_KEY, interval.getStop()); - attributes.put(AVG_INTERVAL_DP_KEY, stats.averageCoverage()); + attributes.put(AVG_INTERVAL_DP_KEY, stats.averageCoverage(interval.size())); vcb = vcb.attributes(attributes); for (String sample : samples) { final GenotypeBuilder gb = new GenotypeBuilder(sample); SampleStatistics sampleStat = stats.getSampleStatistics(sample); - gb.attribute(AVG_INTERVAL_DP_KEY, sampleStat.averageCoverage()); + gb.attribute(AVG_INTERVAL_DP_KEY, sampleStat.averageCoverage(interval.size())); gb.filters(statusToStrings(stats.getSampleStatistics(sample).callableStatuses(), false)); @@ -283,8 +283,8 @@ public class DiagnoseTargets extends LocusWalker { * @param statuses the set of statuses to be converted * @return a matching set of strings */ - private List statusToStrings(List statuses, final boolean isInfoField) { - List output = new ArrayList(statuses.size()); + private List statusToStrings(Iterable statuses, final boolean isInfoField) { + List output = new LinkedList(); for (CallableStatus status : statuses) if ( isInfoField || status != CallableStatus.PASS ) diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/Interval.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/Interval.java index 75f41edf9..bd8307f89 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/Interval.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/Interval.java @@ -53,7 +53,5 @@ package org.broadinstitute.sting.gatk.walkers.diagnostics.diagnosetargets; * Time: 11:30 PM * To change this template use File | Settings | File Templates. */ -interface Interval { - public void initialize(ThresHolder thresholds); - public CallableStatus status (IntervalStatistics intervalStatistics); +interface Interval extends Statistic { } diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/IntervalStatistics.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/IntervalStatistics.java index 30cca8c5a..1580ce9ac 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/IntervalStatistics.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/IntervalStatistics.java @@ -53,23 +53,21 @@ import org.broadinstitute.sting.utils.pileup.ReadBackedPileup; import java.util.*; -final class IntervalStatistics { - private final Map samples; +final class IntervalStatistics extends AbstractStatistics{ + private final Map samples; private final GenomeLoc interval; private final ThresHolder thresholds; - private int preComputedTotalCoverage = -1; - public IntervalStatistics(Set samples, GenomeLoc interval, ThresHolder thresholds) { this.interval = interval; this.thresholds = thresholds; - this.samples = new HashMap(samples.size()); + this.samples = new HashMap(samples.size()); for (String sample : samples) this.samples.put(sample, new SampleStatistics(interval, thresholds)); } public SampleStatistics getSampleStatistics(String sample) { - return samples.get(sample); + return (SampleStatistics) samples.get(sample); } public GenomeLoc getInterval() { @@ -94,7 +92,7 @@ final class IntervalStatistics { for (Map.Entry entry : samplePileups.entrySet()) { String sample = entry.getKey(); ReadBackedPileup samplePileup = entry.getValue(); - SampleStatistics sampleStatistics = samples.get(sample); + SampleStatistics sampleStatistics = (SampleStatistics) samples.get(sample); if (sampleStatistics == null) throw new ReviewedStingException(String.format("Trying to add locus statistics to a sample (%s) that doesn't exist in the Interval.", sample)); @@ -104,49 +102,30 @@ final class IntervalStatistics { } - public double averageCoverage() { - if (preComputedTotalCoverage < 0) - calculateTotalCoverage(); - return (double) preComputedTotalCoverage / interval.size(); - } - - private void calculateTotalCoverage() { - preComputedTotalCoverage = 0; - for (SampleStatistics sample : samples.values()) - preComputedTotalCoverage += sample.totalCoverage(); + /** + * {@inheritDoc} + */ + @Override + public Iterable getElements() { + return samples.values(); } /** - * Return the Callable statuses for the interval as a whole - * - * @return the callable status(es) for the whole interval + * {@inheritDoc} */ - public List callableStatuses() { + @Override + public Iterable callableStatuses() { final List output = new LinkedList(); - // sum up all the callable status for each sample - final Map sampleStatusTally = new HashMap(CallableStatus.values().length); - for (SampleStatistics sampleStatistics : samples.values()) { - for (CallableStatus status : sampleStatistics.callableStatuses()) { - sampleStatusTally.put(status, !sampleStatusTally.containsKey(status) ? 1 : sampleStatusTally.get(status) + 1); - } - } - // check if any of the votes pass the threshold final int nSamples = getNSamples(); - for (Map.Entry entry : sampleStatusTally.entrySet()) { + for (Map.Entry entry : getStatusTally().entrySet()) { if ((double) entry.getValue() / nSamples > thresholds.votePercentageThreshold) { output.add(entry.getKey()); } } - // add the interval specific statitics statuses - for (Interval intervalStat : thresholds.intervalStatisticList) { - final CallableStatus status = intervalStat.status(this); - if (status != null) { - output.add(status); - } - } + output.addAll(queryStatus(thresholds.intervalStatisticList, this)); return output; } diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/Locus.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/Locus.java index 5e6162fb6..f04a093fd 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/Locus.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/Locus.java @@ -53,8 +53,6 @@ package org.broadinstitute.sting.gatk.walkers.diagnostics.diagnosetargets; * Time: 11:29 PM * To change this template use File | Settings | File Templates. */ -interface Locus { - public void initialize(ThresHolder thresholds); - public CallableStatus status (LocusStatistics locusStatistics); +interface Locus extends Statistic { public CallableStatus sampleStatus (SampleStatistics sampleStatistics); } diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusCoverageGap.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusCoverageGap.java index d78109a86..d40816a34 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusCoverageGap.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusCoverageGap.java @@ -61,7 +61,8 @@ final class LocusCoverageGap implements Locus { } @Override - public CallableStatus status(LocusStatistics locusStatistics) { + public CallableStatus status(AbstractStatistics statistics) { + final LocusStatistics locusStatistics = (LocusStatistics) statistics; return locusStatistics.getRawCoverage() == 0 ? CALL : null; } diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusExcessiveCoverage.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusExcessiveCoverage.java index 3bbb6b2d8..ef1d7ffde 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusExcessiveCoverage.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusExcessiveCoverage.java @@ -63,7 +63,8 @@ final class LocusExcessiveCoverage implements Locus { } @Override - public CallableStatus status(LocusStatistics locusStatistics) { + public CallableStatus status(AbstractStatistics statistics) { + final LocusStatistics locusStatistics = (LocusStatistics) statistics; return locusStatistics.getCoverage() > excessiveCoverage ? CALL : null; } diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusLowCoverage.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusLowCoverage.java index 0f7d481c9..f421f5142 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusLowCoverage.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusLowCoverage.java @@ -63,8 +63,9 @@ final class LocusLowCoverage implements Locus { } @Override - public CallableStatus status(LocusStatistics locusStatistics) { - final int raw = locusStatistics.getRawCoverage(); + public CallableStatus status(AbstractStatistics statistics) { + final LocusStatistics locusStatistics = (LocusStatistics) statistics; + final long raw = locusStatistics.getRawCoverage(); return raw > 0 && raw < minCoverage ? CALL: null; } diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusPoorQuality.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusPoorQuality.java index 3caf467ec..042ebcbf8 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusPoorQuality.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusPoorQuality.java @@ -63,7 +63,8 @@ final class LocusPoorQuality implements Locus { } @Override - public CallableStatus status(LocusStatistics locusStatistics) { + public CallableStatus status(AbstractStatistics statistics) { + final LocusStatistics locusStatistics = (LocusStatistics) statistics; return locusStatistics.getCoverage() < minCoverage && locusStatistics.getRawCoverage() >= minCoverage ? CALL: null; } diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusStatistics.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusStatistics.java index 543b126b4..b5a9373d5 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusStatistics.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/LocusStatistics.java @@ -49,10 +49,10 @@ package org.broadinstitute.sting.gatk.walkers.diagnostics.diagnosetargets; import java.util.LinkedList; import java.util.List; -final class LocusStatistics { - private int coverage; - private int rawCoverage; - private final List locusStatisticsList; +final class LocusStatistics extends AbstractStatistics{ + private long coverage; + private long rawCoverage; + private final List locusStatisticsList; public LocusStatistics(ThresHolder thresholds) { this(0,0,thresholds); @@ -64,12 +64,13 @@ final class LocusStatistics { this.locusStatisticsList = thresholds.locusStatisticList; } - public int getCoverage() { - return coverage; - } + @Override + public long getCoverage() {return coverage;} + public long getRawCoverage() {return rawCoverage;} - public int getRawCoverage() { - return rawCoverage; + public void addLocus(final int coverage, final int rawCoverage) { + this.coverage = coverage; + this.rawCoverage = rawCoverage; } /** @@ -79,7 +80,7 @@ final class LocusStatistics { */ public List callableStatuses() { List output = new LinkedList(); - for (Locus stats : locusStatisticsList) { + for (Statistic stats : locusStatisticsList) { CallableStatus status = stats.status(this); if (status != null) { output.add(status); @@ -88,8 +89,9 @@ final class LocusStatistics { return output; } - public void set(final int coverage, final int rawCoverage) { - this.coverage = coverage; - this.rawCoverage = rawCoverage; + @Override + public Iterable getElements() { + return null; } + } diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/PluginUtils.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/PluginUtils.java index 2343b637e..cb28e0ac5 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/PluginUtils.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/PluginUtils.java @@ -55,7 +55,7 @@ import java.util.Map; */ final class PluginUtils { public static CallableStatus genericSampleStatus (final SampleStatistics sampleStatistics, final CallableStatus CALL, final double threshold) { - final Map totals = sampleStatistics.getLocusStatusTally(); + final Map totals = sampleStatistics.getStatusTally(); final int size = sampleStatistics.getIntervalSize(); final int statusCount = totals.containsKey(CALL) ? totals.get(CALL) : 0; return ( (double) statusCount / size) >= threshold ? CALL: null; diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/Sample.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/Sample.java index 3b4e55347..52b24520e 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/Sample.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/Sample.java @@ -53,7 +53,5 @@ package org.broadinstitute.sting.gatk.walkers.diagnostics.diagnosetargets; * Time: 11:30 PM * To change this template use File | Settings | File Templates. */ -interface Sample { - public void initialize(ThresHolder thresholds); - public CallableStatus status (SampleStatistics sampleStatistics); +interface Sample extends Statistic { } diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/SampleBadMates.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/SampleBadMates.java index 9c56858f6..483f63c06 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/SampleBadMates.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/SampleBadMates.java @@ -64,7 +64,8 @@ final class SampleBadMates implements Sample { } @Override - public CallableStatus status(SampleStatistics sampleStatistics) { + public CallableStatus status(AbstractStatistics statistics) { + final SampleStatistics sampleStatistics = (SampleStatistics) statistics; final int nReads = sampleStatistics.getnReads(); return nReads > 0 && (double) sampleStatistics.getnBadMates() / nReads > threshold ? CALL : null; } diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/SampleNoReads.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/SampleNoReads.java index 95d66a555..1c6d3deb7 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/SampleNoReads.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/SampleNoReads.java @@ -53,16 +53,13 @@ package org.broadinstitute.sting.gatk.walkers.diagnostics.diagnosetargets; */ final class SampleNoReads implements Sample { private static final CallableStatus CALL = CallableStatus.NO_READS; - - private double votingThreshold; - - @Override +@Override public void initialize(ThresHolder thresholds) { - votingThreshold = thresholds.votePercentageThreshold; } @Override - public CallableStatus status(SampleStatistics sampleStatistics) { + public CallableStatus status(AbstractStatistics statistics) { + final SampleStatistics sampleStatistics = (SampleStatistics) statistics; return sampleStatistics.getnReads() == 0 ? CALL : null; } diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/SampleStatistics.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/SampleStatistics.java index 6c8481b0e..4d41fea16 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/SampleStatistics.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/SampleStatistics.java @@ -56,20 +56,18 @@ import java.util.*; /** * The statistics calculator for a specific sample given the interval */ -final class SampleStatistics { +final class SampleStatistics extends AbstractStatistics { private final GenomeLoc interval; - private final ArrayList loci; + private final ArrayList loci; private final ThresHolder thresholds; - // avoids re-calculating these sums over loci - private int preComputedTotalCoverage = -1; private Map locusStatusTally = null; private int nReads = -1; private int nBadMates = -1; public SampleStatistics(final GenomeLoc interval, final ThresHolder thresholds) { this.interval = interval; - this.loci = new ArrayList(interval.size()); + this.loci = new ArrayList(interval.size()); this.thresholds = thresholds; nReads = 0; nBadMates = 0; @@ -77,69 +75,61 @@ final class SampleStatistics { // Initialize every loci (this way we don't have to worry about non-existent loci in the object for (int i = 0; i < interval.size(); i++) this.loci.add(new LocusStatistics(thresholds)); - } /** - * Calculates the total "good" coverage of this sample. Good means "passes the base and - * mapping quality requirements. - * - * @return the total "good" coverage across the interval for this sample + * Simple Getters */ - public long totalCoverage() { - if (preComputedTotalCoverage < 0) - calculateTotalCoverage(); - return preComputedTotalCoverage; - } + public int getIntervalSize() {return interval.size();} + public int getnReads() {return nReads;} + public int getnBadMates() {return nBadMates;} /** - * Calculates the average "good" coverage of this sample. Good means "passes the base and - * mapping quality requirements. - * - * @return the average "good" coverage - */ - public double averageCoverage() { - return (double) totalCoverage() / loci.size(); - } - - /** - * Tally up all the callable status of all the loci in this sample. - * - * @return a map of callable status and counts - */ - public Map getLocusStatusTally() { - if (locusStatusTally == null) { - locusStatusTally = new HashMap(CallableStatus.values().length); - - // sum up all the callable statuses for each locus - for (int i = 0; i < interval.size(); i++) { - LocusStatistics locus = loci.get(i); - for (CallableStatus status : locus.callableStatuses()) { - locusStatusTally.put(status, !locusStatusTally.containsKey(status) ? 1 : locusStatusTally.get(status) + 1); - } - } - } - return locusStatusTally; - } - - /** - * Calculates the callable statuses of the entire sample + * Adds a locus to the interval wide stats * - * @return the callable statuses of the entire sample + * @param locus The locus given as a GenomeLoc + * @param pileup The pileup of that locus, this exclusively contains the sample */ - public List callableStatuses() { + public void addLocus(GenomeLoc locus, ReadBackedPileup pileup) { + if (!interval.containsP(locus)) + throw new ReviewedStingException(String.format("Locus %s is not part of the Interval %s", locus, interval)); + + // a null pileup means there nothing to add + if (pileup != null) { + final int locusIndex = locus.getStart() - interval.getStart(); + final int rawCoverage = pileup.depthOfCoverage(); + final int coverage = pileup.getBaseAndMappingFilteredPileup(thresholds.minimumBaseQuality, thresholds.minimumMappingQuality).depthOfCoverage(); + final LocusStatistics locusData = (LocusStatistics) loci.get(locusIndex); + locusData.addLocus(coverage, rawCoverage); + + // process all the reads in this pileup (tallying number of reads and bad mates) + for (GATKSAMRecord read : pileup.getReads()) + processRead(read); + } + } + + @Override + public Iterable getElements() { + return loci; + } + + /** + * {@inheritDoc} + */ + @Override + public Iterable callableStatuses() { final List output = new LinkedList(); // get the tally of all the locus callable statuses - for (Locus locusStat : thresholds.locusStatisticList) { - final CallableStatus status = locusStat.sampleStatus(this); + for (Statistic locusStat : thresholds.locusStatisticList) { + final CallableStatus status = ((Locus) locusStat).sampleStatus(this); if (status != null) { output.add(status); } } // get the sample specific statitics statuses - for (Sample sampleStat : thresholds.sampleStatisticList) { + for (Statistic sampleStat : thresholds.sampleStatisticList) { final CallableStatus status = sampleStat.status(this); if (status != null) { output.add(status); @@ -153,28 +143,6 @@ final class SampleStatistics { return output; } - /** - * Adds a locus to the interval wide stats - * - * @param locus The locus given as a GenomeLoc - * @param pileup The pileup of that locus, this exclusively contains the sample - */ - public void addLocus(GenomeLoc locus, ReadBackedPileup pileup) { - if (!interval.containsP(locus)) - throw new ReviewedStingException(String.format("Locus %s is not part of the Interval %s", locus, interval)); - - // a null pileup means there nothing ot add - if (pileup != null) { - final int locusIndex = locus.getStart() - interval.getStart(); - final int rawCoverage = pileup.depthOfCoverage(); - final int coverage = pileup.getBaseAndMappingFilteredPileup(thresholds.minimumBaseQuality, thresholds.minimumMappingQuality).depthOfCoverage(); - final LocusStatistics locusData = loci.get(locusIndex); - locusData.set(coverage, rawCoverage); - - for (GATKSAMRecord read : pileup.getReads()) - processRead(read); - } - } /** * Account for the read and check it for any statistics necessary. Reads are marked in the temporary @@ -190,22 +158,4 @@ final class SampleStatistics { read.setTemporaryAttribute("seen", true); } } - - private void calculateTotalCoverage() { - preComputedTotalCoverage = 0; - for (LocusStatistics locus : loci) - preComputedTotalCoverage += locus.getCoverage(); - } - - public int getIntervalSize() { - return interval.size(); - } - - public int getnReads() { - return nReads; - } - - public int getnBadMates() { - return nBadMates; - } } diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/Statistic.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/Statistic.java new file mode 100644 index 000000000..c43b00a65 --- /dev/null +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/Statistic.java @@ -0,0 +1,57 @@ +/* +* By downloading the PROGRAM you agree to the following terms of use: +* +* BROAD INSTITUTE - SOFTWARE LICENSE AGREEMENT - FOR ACADEMIC NON-COMMERCIAL RESEARCH PURPOSES ONLY +* +* This Agreement is made between the Broad Institute, Inc. with a principal address at 7 Cambridge Center, Cambridge, MA 02142 (BROAD) and the LICENSEE and is effective at the date the downloading is completed (EFFECTIVE DATE). +* +* WHEREAS, LICENSEE desires to license the PROGRAM, as defined hereinafter, and BROAD wishes to have this PROGRAM utilized in the public interest, subject only to the royalty-free, nonexclusive, nontransferable license rights of the United States Government pursuant to 48 CFR 52.227-14; and +* WHEREAS, LICENSEE desires to license the PROGRAM and BROAD desires to grant a license on the following terms and conditions. +* NOW, THEREFORE, in consideration of the promises and covenants made herein, the parties hereto agree as follows: +* +* 1. DEFINITIONS +* 1.1 PROGRAM shall mean copyright in the object code and source code known as GATK2 and related documentation, if any, as they exist on the EFFECTIVE DATE and can be downloaded from http://www.broadinstitute/GATK on the EFFECTIVE DATE. +* +* 2. LICENSE +* 2.1 Grant. Subject to the terms of this Agreement, BROAD hereby grants to LICENSEE, solely for academic non-commercial research purposes, a non-exclusive, non-transferable license to: (a) download, execute and display the PROGRAM and (b) create bug fixes and modify the PROGRAM. +* The LICENSEE may apply the PROGRAM in a pipeline to data owned by users other than the LICENSEE and provide these users the results of the PROGRAM provided LICENSEE does so for academic non-commercial purposes only. For clarification purposes, academic sponsored research is not a commercial use under the terms of this Agreement. +* 2.2 No Sublicensing or Additional Rights. LICENSEE shall not sublicense or distribute the PROGRAM, in whole or in part, without prior written permission from BROAD. LICENSEE shall ensure that all of its users agree to the terms of this Agreement. LICENSEE further agrees that it shall not put the PROGRAM on a network, server, or other similar technology that may be accessed by anyone other than the LICENSEE and its employees and users who have agreed to the terms of this agreement. +* 2.3 License Limitations. Nothing in this Agreement shall be construed to confer any rights upon LICENSEE by implication, estoppel, or otherwise to any computer software, trademark, intellectual property, or patent rights of BROAD, or of any other entity, except as expressly granted herein. LICENSEE agrees that the PROGRAM, in whole or part, shall not be used for any commercial purpose, including without limitation, as the basis of a commercial software or hardware product or to provide services. LICENSEE further agrees that the PROGRAM shall not be copied or otherwise adapted in order to circumvent the need for obtaining a license for use of the PROGRAM. +* +* 3. OWNERSHIP OF INTELLECTUAL PROPERTY +* LICENSEE acknowledges that title to the PROGRAM shall remain with BROAD. The PROGRAM is marked with the following BROAD copyright notice and notice of attribution to contributors. LICENSEE shall retain such notice on all copies. LICENSEE agrees to include appropriate attribution if any results obtained from use of the PROGRAM are included in any publication. +* Copyright 2012 Broad Institute, Inc. +* Notice of attribution: The GATK2 program was made available through the generosity of Medical and Population Genetics program at the Broad Institute, Inc. +* LICENSEE shall not use any trademark or trade name of BROAD, or any variation, adaptation, or abbreviation, of such marks or trade names, or any names of officers, faculty, students, employees, or agents of BROAD except as states above for attribution purposes. +* +* 4. INDEMNIFICATION +* LICENSEE shall indemnify, defend, and hold harmless BROAD, and their respective officers, faculty, students, employees, associated investigators and agents, and their respective successors, heirs and assigns, (Indemnitees), against any liability, damage, loss, or expense (including reasonable attorneys fees and expenses) incurred by or imposed upon any of the Indemnitees in connection with any claims, suits, actions, demands or judgments arising out of any theory of liability (including, without limitation, actions in the form of tort, warranty, or strict liability and regardless of whether such action has any factual basis) pursuant to any right or license granted under this Agreement. +* +* 5. NO REPRESENTATIONS OR WARRANTIES +* THE PROGRAM IS DELIVERED AS IS. BROAD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE PROGRAM OR THE COPYRIGHT, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, WHETHER OR NOT DISCOVERABLE. BROAD EXTENDS NO WARRANTIES OF ANY KIND AS TO PROGRAM CONFORMITY WITH WHATEVER USER MANUALS OR OTHER LITERATURE MAY BE ISSUED FROM TIME TO TIME. +* IN NO EVENT SHALL BROAD OR ITS RESPECTIVE DIRECTORS, OFFICERS, EMPLOYEES, AFFILIATED INVESTIGATORS AND AFFILIATES BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, INCLUDING, WITHOUT LIMITATION, ECONOMIC DAMAGES OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF WHETHER BROAD SHALL BE ADVISED, SHALL HAVE OTHER REASON TO KNOW, OR IN FACT SHALL KNOW OF THE POSSIBILITY OF THE FOREGOING. +* +* 6. ASSIGNMENT +* This Agreement is personal to LICENSEE and any rights or obligations assigned by LICENSEE without the prior written consent of BROAD shall be null and void. +* +* 7. MISCELLANEOUS +* 7.1 Export Control. LICENSEE gives assurance that it will comply with all United States export control laws and regulations controlling the export of the PROGRAM, including, without limitation, all Export Administration Regulations of the United States Department of Commerce. Among other things, these laws and regulations prohibit, or require a license for, the export of certain types of software to specified countries. +* 7.2 Termination. LICENSEE shall have the right to terminate this Agreement for any reason upon prior written notice to BROAD. If LICENSEE breaches any provision hereunder, and fails to cure such breach within thirty (30) days, BROAD may terminate this Agreement immediately. Upon termination, LICENSEE shall provide BROAD with written assurance that the original and all copies of the PROGRAM have been destroyed, except that, upon prior written authorization from BROAD, LICENSEE may retain a copy for archive purposes. +* 7.3 Survival. The following provisions shall survive the expiration or termination of this Agreement: Articles 1, 3, 4, 5 and Sections 2.2, 2.3, 7.3, and 7.4. +* 7.4 Notice. Any notices under this Agreement shall be in writing, shall specifically refer to this Agreement, and shall be sent by hand, recognized national overnight courier, confirmed facsimile transmission, confirmed electronic mail, or registered or certified mail, postage prepaid, return receipt requested. All notices under this Agreement shall be deemed effective upon receipt. +* 7.5 Amendment and Waiver; Entire Agreement. This Agreement may be amended, supplemented, or otherwise modified only by means of a written instrument signed by all parties. Any waiver of any rights or failure to act in a specific instance shall relate only to such instance and shall not be construed as an agreement to waive any rights or fail to act in any other instance, whether or not similar. This Agreement constitutes the entire agreement among the parties with respect to its subject matter and supersedes prior agreements or understandings between the parties relating to its subject matter. +* 7.6 Binding Effect; Headings. This Agreement shall be binding upon and inure to the benefit of the parties and their respective permitted successors and assigns. All headings are for convenience only and shall not affect the meaning of any provision of this Agreement. +* 7.7 Governing Law. This Agreement shall be construed, governed, interpreted and applied in accordance with the internal laws of the Commonwealth of Massachusetts, U.S.A., without regard to conflict of laws principles. +*/ + +package org.broadinstitute.sting.gatk.walkers.diagnostics.diagnosetargets; + +/** + * + * @author Mauricio Carneiro + * @since 4/23/13 + */ +interface Statistic { + public void initialize(ThresHolder thresholds); + public CallableStatus status (AbstractStatistics statistic); +} diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/ThresHolder.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/ThresHolder.java index c45c2d9ff..42c09dda1 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/ThresHolder.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/diagnosetargets/ThresHolder.java @@ -114,9 +114,9 @@ final class ThresHolder { @Argument(fullName = "quality_status_threshold", shortName = "stQ", doc = "The proportion of the loci needed for calling POOR_QUALITY", required = false) public double qualityStatusThreshold = 0.50; - public final List locusStatisticList = new LinkedList(); - public final List sampleStatisticList = new LinkedList(); - public final List intervalStatisticList = new LinkedList(); + public final List locusStatisticList = new LinkedList(); + public final List sampleStatisticList = new LinkedList(); + public final List intervalStatisticList = new LinkedList(); public ThresHolder() {}