diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/compression/reducereads/CompressionStash.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/compression/reducereads/CompressionStash.java index 9f2a7f270..ebc5ef8ed 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/compression/reducereads/CompressionStash.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/compression/reducereads/CompressionStash.java @@ -46,7 +46,7 @@ package org.broadinstitute.sting.gatk.walkers.compression.reducereads; -import org.broadinstitute.sting.utils.GenomeLocComparator; +import org.broadinstitute.sting.utils.*; import java.util.Collection; import java.util.TreeSet; @@ -61,22 +61,22 @@ import java.util.TreeSet; * Date: 10/15/12 * Time: 4:08 PM */ -public class CompressionStash extends TreeSet { +public class CompressionStash extends TreeSet { public CompressionStash() { super(new GenomeLocComparator()); } /** - * Adds a SimpleGenomeLoc to the stash and merges it with any overlapping (and contiguous) existing loc + * Adds a UnvalidatingGenomeLoc to the stash and merges it with any overlapping (and contiguous) existing loc * in the stash. * * @param insertLoc the new loc to be inserted * @return true if the loc, or it's merged version, wasn't present in the list before. */ @Override - public boolean add(SimpleGenomeLoc insertLoc) { - TreeSet removedLocs = new TreeSet(); - for (SimpleGenomeLoc existingLoc : this) { + public boolean add(FinishedGenomeLoc insertLoc) { + TreeSet removedLocs = new TreeSet(); + for (FinishedGenomeLoc existingLoc : this) { if (existingLoc.isPast(insertLoc)) { break; // if we're past the loc we're done looking for overlaps. } @@ -87,17 +87,17 @@ public class CompressionStash extends TreeSet { removedLocs.add(existingLoc); // list the original loc for merging } } - for (SimpleGenomeLoc loc : removedLocs) { + for (GenomeLoc loc : removedLocs) { this.remove(loc); // remove all locs that will be merged } removedLocs.add(insertLoc); // add the new loc to the list of locs that will be merged - return super.add(SimpleGenomeLoc.merge(removedLocs)); // merge them all into one loc and add to the stash + return super.add(new FinishedGenomeLoc(GenomeLoc.merge(removedLocs), insertLoc.isFinished())); } @Override - public boolean addAll(Collection locs) { + public boolean addAll(Collection locs) { boolean result = false; - for (SimpleGenomeLoc loc : locs) { + for (final FinishedGenomeLoc loc : locs) { result |= this.add(loc); } return result; diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/compression/reducereads/SimpleGenomeLoc.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/compression/reducereads/FinishedGenomeLoc.java similarity index 81% rename from protected/java/src/org/broadinstitute/sting/gatk/walkers/compression/reducereads/SimpleGenomeLoc.java rename to protected/java/src/org/broadinstitute/sting/gatk/walkers/compression/reducereads/FinishedGenomeLoc.java index 85a1f8df1..13010f905 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/compression/reducereads/SimpleGenomeLoc.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/compression/reducereads/FinishedGenomeLoc.java @@ -46,11 +46,8 @@ package org.broadinstitute.sting.gatk.walkers.compression.reducereads; -import com.google.java.contract.Requires; import org.broadinstitute.sting.utils.GenomeLoc; -import org.broadinstitute.sting.utils.exceptions.ReviewedStingException; - -import java.util.SortedSet; +import org.broadinstitute.sting.utils.UnvalidatingGenomeLoc; /** * GenomeLocs are very useful objects to keep track of genomic locations and perform set operations @@ -59,70 +56,27 @@ import java.util.SortedSet; * However, GenomeLocs are bound to strict validation through the GenomeLocParser and cannot * be created easily for small tasks that do not require the rigors of the GenomeLocParser validation * - * SimpleGenomeLoc is a simple utility to create GenomeLocs without going through the parser. Should + * UnvalidatingGenomeLoc is a simple utility to create GenomeLocs without going through the parser. Should * only be used outside of the engine. * * User: carneiro * Date: 10/16/12 * Time: 2:07 PM */ -public class SimpleGenomeLoc extends GenomeLoc { +public class FinishedGenomeLoc extends UnvalidatingGenomeLoc { private boolean finished; - public SimpleGenomeLoc(String contigName, int contigIndex, int start, int stop, boolean finished) { - super(contigName, contigIndex, start, stop); + public FinishedGenomeLoc(final String contigName, final int contigIndex, final int start, final int stop, final boolean finished) { + super(contigName, contigIndex, start, stop); + this.finished = finished; + } + + public FinishedGenomeLoc(final GenomeLoc loc, final boolean finished) { + super(loc.getContig(), loc.getContigIndex(), loc.getStart(), loc.getStop()); this.finished = finished; } public boolean isFinished() { return finished; } - - /** - * Merges 2 *contiguous* locs into 1 - * - * @param a SimpleGenomeLoc #1 - * @param b SimpleGenomeLoc #2 - * @return one merged loc - */ - @Requires("a != null && b != null") - public static SimpleGenomeLoc merge(SimpleGenomeLoc a, SimpleGenomeLoc b) throws ReviewedStingException { - if(GenomeLoc.isUnmapped(a) || GenomeLoc.isUnmapped(b)) { - throw new ReviewedStingException("Tried to merge unmapped genome locs"); - } - - if (!(a.contiguousP(b))) { - throw new ReviewedStingException("The two genome locs need to be contiguous"); - } - - return new SimpleGenomeLoc(a.getContig(), a.contigIndex, - Math.min(a.getStart(), b.getStart()), - Math.max(a.getStop(), b.getStop()), - a.isFinished()); - } - - /** - * Merges a list of *sorted* *contiguous* locs into one - * - * @param sortedLocs a sorted list of contiguous locs - * @return one merged loc - */ - @Requires("sortedLocs != null") - public static SimpleGenomeLoc merge(SortedSet sortedLocs) { - SimpleGenomeLoc result = null; - - for ( SimpleGenomeLoc loc : sortedLocs ) { - if ( loc.isUnmapped() ) - throw new ReviewedStingException("Tried to merge unmapped genome locs"); - - if ( result == null ) - result = loc; - else if ( !result.contiguousP(loc) ) - throw new ReviewedStingException("The genome locs need to be contiguous"); - else - result = merge(result, loc); - } - - return result; - } } diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/compression/reducereads/SlidingWindow.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/compression/reducereads/SlidingWindow.java index 7ce606f20..e2f8b6682 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/compression/reducereads/SlidingWindow.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/compression/reducereads/SlidingWindow.java @@ -53,6 +53,7 @@ import net.sf.samtools.CigarElement; import net.sf.samtools.CigarOperator; import net.sf.samtools.SAMFileHeader; import org.broadinstitute.sting.gatk.downsampling.ReservoirDownsampler; +import org.broadinstitute.sting.utils.GenomeLoc; import org.broadinstitute.sting.utils.collections.Pair; import org.broadinstitute.sting.utils.exceptions.ReviewedStingException; import org.broadinstitute.sting.utils.recalibration.EventType; @@ -213,7 +214,7 @@ public class SlidingWindow { * @return null if nothing is variant, start/stop if there is a complete variant region, start/-1 if there is an incomplete variant region. All coordinates returned are global. */ @Requires({"from >= 0", "from <= to", "to <= variantSite.length"}) - private SimpleGenomeLoc findNextVariantRegion(int from, int to, boolean[] variantSite, boolean closeLastRegion) { + private FinishedGenomeLoc findNextVariantRegion(int from, int to, boolean[] variantSite, boolean closeLastRegion) { boolean foundStart = false; final int windowHeaderStart = getStartLocation(windowHeader); int variantRegionStartIndex = 0; @@ -223,12 +224,12 @@ public class SlidingWindow { foundStart = true; } else if(!variantSite[i] && foundStart) { - return(new SimpleGenomeLoc(contig, contigIndex, windowHeaderStart + variantRegionStartIndex, windowHeaderStart + i - 1, true)); + return(new FinishedGenomeLoc(contig, contigIndex, windowHeaderStart + variantRegionStartIndex, windowHeaderStart + i - 1, true)); } } final int refStart = windowHeaderStart + variantRegionStartIndex; final int refStop = windowHeaderStart + to - 1; - return (foundStart && closeLastRegion) ? new SimpleGenomeLoc(contig, contigIndex, refStart, refStop, true) : null; + return (foundStart && closeLastRegion) ? new FinishedGenomeLoc(contig, contigIndex, refStart, refStop, true) : null; } /** @@ -248,7 +249,7 @@ public class SlidingWindow { int index = from; while(index < to) { // returns results in global coordinates - SimpleGenomeLoc result = findNextVariantRegion(index, to, variantSite, closeLastRegion); + FinishedGenomeLoc result = findNextVariantRegion(index, to, variantSite, closeLastRegion); if (result == null) break; @@ -699,8 +700,8 @@ public class SlidingWindow { int lastStop = -1; int windowHeaderStart = getStartLocation(windowHeader); - for (SimpleGenomeLoc region : regions) { - if (region.isFinished() && region.getContig() == contig && region.getStart() >= windowHeaderStart && region.getStop() < windowHeaderStart + windowHeader.size()) { + for (GenomeLoc region : regions) { + if (((FinishedGenomeLoc)region).isFinished() && region.getContig() == contig && region.getStart() >= windowHeaderStart && region.getStop() < windowHeaderStart + windowHeader.size()) { int start = region.getStart() - windowHeaderStart; int stop = region.getStop() - windowHeaderStart; diff --git a/protected/java/test/org/broadinstitute/sting/gatk/walkers/compression/reducereads/SimpleGenomeLocUnitTest.java b/protected/java/test/org/broadinstitute/sting/gatk/walkers/compression/reducereads/SimpleGenomeLocUnitTest.java deleted file mode 100644 index cbac7e3c1..000000000 --- a/protected/java/test/org/broadinstitute/sting/gatk/walkers/compression/reducereads/SimpleGenomeLocUnitTest.java +++ /dev/null @@ -1,106 +0,0 @@ -/* -* 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.compression.reducereads; - - -import org.broadinstitute.sting.BaseTest; -import org.broadinstitute.sting.utils.exceptions.ReviewedStingException; -import org.testng.Assert; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; - -import java.util.*; - -public class SimpleGenomeLocUnitTest extends BaseTest { - - private static final SimpleGenomeLoc loc1 = new SimpleGenomeLoc("1", 0, 10, 20, false); - private static final SimpleGenomeLoc loc2 = new SimpleGenomeLoc("1", 0, 21, 30, false); - private static final SimpleGenomeLoc loc3 = new SimpleGenomeLoc("1", 0, 31, 40, false); - - private class SGLTest { - public List locs; - - private SGLTest(final List locs) { - this.locs = locs; - } - } - - @DataProvider(name = "SGLtest") - public Object[][] createFindVariantRegionsData() { - List tests = new ArrayList(); - - tests.add(new Object[]{new SGLTest(Arrays.asList(loc1))}); - tests.add(new Object[]{new SGLTest(Arrays.asList(loc1, loc2))}); - tests.add(new Object[]{new SGLTest(Arrays.asList(loc1, loc2, loc3))}); - - return tests.toArray(new Object[][]{}); - } - - @Test(dataProvider = "SGLtest", enabled = true) - public void testSimpleGenomeLoc(SGLTest test) { - testMerge(test.locs); - } - - @Test(expectedExceptions = ReviewedStingException.class) - public void testNotContiguousLocs() { - final List locs = new ArrayList(1); - locs.add(loc1); - locs.add(loc3); - testMerge(locs); - } - - private void testMerge(final List locs) { - SimpleGenomeLoc result1 = locs.get(0); - for ( int i = 1; i < locs.size(); i++ ) - result1 = SimpleGenomeLoc.merge(result1, locs.get(i)); - - SimpleGenomeLoc result2 = SimpleGenomeLoc.merge(new TreeSet(locs)); - Assert.assertEquals(result1, result2); - Assert.assertEquals(result1.getStart(), locs.get(0).getStart()); - Assert.assertEquals(result1.getStop(), locs.get(locs.size() - 1).getStop()); - } -} diff --git a/protected/java/test/org/broadinstitute/sting/gatk/walkers/compression/reducereads/SlidingWindowUnitTest.java b/protected/java/test/org/broadinstitute/sting/gatk/walkers/compression/reducereads/SlidingWindowUnitTest.java index 3d3c4ad24..d9b55963d 100644 --- a/protected/java/test/org/broadinstitute/sting/gatk/walkers/compression/reducereads/SlidingWindowUnitTest.java +++ b/protected/java/test/org/broadinstitute/sting/gatk/walkers/compression/reducereads/SlidingWindowUnitTest.java @@ -46,14 +46,7 @@ package org.broadinstitute.sting.gatk.walkers.compression.reducereads; -import net.sf.samtools.SAMFileHeader; -import net.sf.samtools.SAMReadGroupRecord; -import net.sf.samtools.SAMRecord; import org.broadinstitute.sting.BaseTest; -import org.broadinstitute.sting.gatk.GenomeAnalysisEngine; -import org.broadinstitute.sting.utils.sam.ArtificialSAMUtils; -import org.broadinstitute.sting.utils.sam.ArtificialSingleSampleReadStream; -import org.broadinstitute.sting.utils.sam.ArtificialSingleSampleReadStreamAnalyzer; import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @@ -70,31 +63,31 @@ public class SlidingWindowUnitTest extends BaseTest { private static final int variantRegionLength = 1000; private static final int globalStartPosition = 1000000; - private static final SimpleGenomeLoc loc90to95 = new SimpleGenomeLoc("1", 0, 1000090, 1000095, false); - private static final SimpleGenomeLoc loc96to99 = new SimpleGenomeLoc("1", 0, 1000096, 1000099, false); - private static final SimpleGenomeLoc loc100to110 = new SimpleGenomeLoc("1", 0, 1000100, 1000110, false); - private static final SimpleGenomeLoc loc999 = new SimpleGenomeLoc("1", 0, 1000999, 1000999, false); + private static final FinishedGenomeLoc loc90to95 = new FinishedGenomeLoc("1", 0, 1000090, 1000095, false); + private static final FinishedGenomeLoc loc96to99 = new FinishedGenomeLoc("1", 0, 1000096, 1000099, false); + private static final FinishedGenomeLoc loc100to110 = new FinishedGenomeLoc("1", 0, 1000100, 1000110, false); + private static final FinishedGenomeLoc loc999 = new FinishedGenomeLoc("1", 0, 1000999, 1000999, false); private class FindVariantRegionsTest { - public List locs, expectedResult; + public List locs, expectedResult; public boolean[] variantRegionBitset; - private FindVariantRegionsTest(final List locs) { + private FindVariantRegionsTest(final List locs) { this.locs = locs; this.expectedResult = locs; variantRegionBitset = createBitset(locs); } - private FindVariantRegionsTest(final List locs, final List expectedResult) { + private FindVariantRegionsTest(final List locs, final List expectedResult) { this.locs = locs; this.expectedResult = expectedResult; variantRegionBitset = createBitset(locs); } } - private static boolean[] createBitset(final List locs) { + private static boolean[] createBitset(final List locs) { boolean[] variantRegionBitset = new boolean[variantRegionLength]; - for ( SimpleGenomeLoc loc : locs ) { + for ( FinishedGenomeLoc loc : locs ) { final int stop = loc.getStop() - globalStartPosition; for ( int i = loc.getStart() - globalStartPosition; i <= stop; i++ ) variantRegionBitset[i] = true; @@ -106,11 +99,11 @@ public class SlidingWindowUnitTest extends BaseTest { public Object[][] createFindVariantRegionsData() { List tests = new ArrayList(); - tests.add(new Object[]{new FindVariantRegionsTest(Arrays.asList(loc90to95))}); - tests.add(new Object[]{new FindVariantRegionsTest(Arrays.asList(loc90to95, loc100to110))}); - tests.add(new Object[]{new FindVariantRegionsTest(Arrays.asList(loc90to95, loc96to99, loc100to110), Arrays.asList(new SimpleGenomeLoc("1", 0, 1000090, 1000110, false)))}); - tests.add(new Object[]{new FindVariantRegionsTest(Arrays.asList(loc90to95, loc999))}); - tests.add(new Object[]{new FindVariantRegionsTest(Arrays.asList(loc999))}); + tests.add(new Object[]{new FindVariantRegionsTest(Arrays.asList(loc90to95))}); + tests.add(new Object[]{new FindVariantRegionsTest(Arrays.asList(loc90to95, loc100to110))}); + tests.add(new Object[]{new FindVariantRegionsTest(Arrays.asList(loc90to95, loc96to99, loc100to110), Arrays.asList(new FinishedGenomeLoc("1", 0, 1000090, 1000110, false)))}); + tests.add(new Object[]{new FindVariantRegionsTest(Arrays.asList(loc90to95, loc999))}); + tests.add(new Object[]{new FindVariantRegionsTest(Arrays.asList(loc999))}); return tests.toArray(new Object[][]{}); } @@ -120,7 +113,7 @@ public class SlidingWindowUnitTest extends BaseTest { final SlidingWindow slidingWindow = new SlidingWindow("1", 0, globalStartPosition); final CompressionStash locs = slidingWindow.findVariantRegions(0, variantRegionLength, test.variantRegionBitset, true); int index = 0; - for ( final SimpleGenomeLoc loc : locs ) { + for ( final FinishedGenomeLoc loc : locs ) { Assert.assertTrue(loc.equals(test.expectedResult.get(index++))); } } @@ -128,7 +121,7 @@ public class SlidingWindowUnitTest extends BaseTest { @Test(enabled = true) public void testNoClosingRegions() { final SlidingWindow slidingWindow = new SlidingWindow("1", 0, globalStartPosition); - final CompressionStash locs = slidingWindow.findVariantRegions(0, variantRegionLength, createBitset(Arrays.asList(loc90to95, loc999)), false); + final CompressionStash locs = slidingWindow.findVariantRegions(0, variantRegionLength, createBitset(Arrays.asList(loc90to95, loc999)), false); Assert.assertEquals(locs.size(), 1); Assert.assertEquals(locs.iterator().next(), loc90to95); } diff --git a/public/java/src/org/broadinstitute/sting/utils/GenomeLoc.java b/public/java/src/org/broadinstitute/sting/utils/GenomeLoc.java index 4f1b35f62..0a271b64c 100644 --- a/public/java/src/org/broadinstitute/sting/utils/GenomeLoc.java +++ b/public/java/src/org/broadinstitute/sting/utils/GenomeLoc.java @@ -30,10 +30,7 @@ import com.google.java.contract.Requires; import org.broadinstitute.sting.utils.exceptions.ReviewedStingException; import java.io.Serializable; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; /** * Created by IntelliJ IDEA. @@ -581,4 +578,49 @@ public class GenomeLoc implements Comparable, Serializable, HasGenome public GenomeLoc incPos(GenomeLoc loc, int by) { return new GenomeLoc(loc.getContig(), loc.getContigIndex(), loc.start + by, loc.stop + by); } + + /** + * Merges 2 *contiguous* locs into 1 + * + * @param a GenomeLoc #1 + * @param b GenomeLoc #2 + * @return one merged loc + */ + @Requires("a != null && b != null") + public static GenomeLoc merge(final T a, final T b) { + if ( isUnmapped(a) || isUnmapped(b) ) { + throw new ReviewedStingException("Tried to merge unmapped genome locs"); + } + + if ( !(a.contiguousP(b)) ) { + throw new ReviewedStingException("The two genome locs need to be contiguous"); + } + + return new GenomeLoc(a.getContig(), a.contigIndex, Math.min(a.getStart(), b.getStart()), Math.max(a.getStop(), b.getStop())); + } + + /** + * Merges a list of *sorted* *contiguous* locs into 1 + * + * @param sortedLocs a sorted list of contiguous locs + * @return one merged loc + */ + @Requires("sortedLocs != null") + public static GenomeLoc merge(final SortedSet sortedLocs) { + GenomeLoc result = null; + + for ( GenomeLoc loc : sortedLocs ) { + if ( loc.isUnmapped() ) + throw new ReviewedStingException("Tried to merge unmapped genome locs"); + + if ( result == null ) + result = loc; + else if ( !result.contiguousP(loc) ) + throw new ReviewedStingException("The genome locs need to be contiguous"); + else + result = merge(result, loc); + } + + return result; + } } diff --git a/public/java/src/org/broadinstitute/sting/utils/UnvalidatingGenomeLoc.java b/public/java/src/org/broadinstitute/sting/utils/UnvalidatingGenomeLoc.java new file mode 100644 index 000000000..5a611c21e --- /dev/null +++ b/public/java/src/org/broadinstitute/sting/utils/UnvalidatingGenomeLoc.java @@ -0,0 +1,50 @@ +/* +* Copyright (c) 2012 The Broad Institute +* +* Permission is hereby granted, free of charge, to any person +* obtaining a copy of this software and associated documentation +* files (the "Software"), to deal in the Software without +* restriction, including without limitation the rights to use, +* copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following +* conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR +* THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +package org.broadinstitute.sting.utils; + +import com.google.java.contract.Requires; + +/** + * GenomeLocs are very useful objects to keep track of genomic locations and perform set operations + * with them. + * + * However, GenomeLocs are bound to strict validation through the GenomeLocParser and cannot + * be created easily for small tasks that do not require the rigors of the GenomeLocParser validation + * + * UnvalidatingGenomeLoc is a simple utility to create GenomeLocs without going through the parser. + * + * WARNING: SHOULD BE USED ONLY BY EXPERT USERS WHO KNOW WHAT THEY ARE DOING! + * + * User: carneiro + * Date: 10/16/12 + * Time: 2:07 PM + */ +public class UnvalidatingGenomeLoc extends GenomeLoc { + + public UnvalidatingGenomeLoc(String contigName, int contigIndex, int start, int stop) { + super(contigName, contigIndex, start, stop); + } +} diff --git a/public/java/test/org/broadinstitute/sting/utils/GenomeLocUnitTest.java b/public/java/test/org/broadinstitute/sting/utils/GenomeLocUnitTest.java index b5d495ddc..ed91114a4 100644 --- a/public/java/test/org/broadinstitute/sting/utils/GenomeLocUnitTest.java +++ b/public/java/test/org/broadinstitute/sting/utils/GenomeLocUnitTest.java @@ -29,6 +29,7 @@ package org.broadinstitute.sting.utils; // the imports for unit testing. +import org.broadinstitute.sting.utils.exceptions.ReviewedStingException; import org.broadinstitute.sting.utils.interval.IntervalMergingRule; import org.broadinstitute.sting.utils.interval.IntervalUtils; import org.testng.Assert; @@ -40,10 +41,7 @@ import org.broadinstitute.sting.utils.fasta.CachingIndexedFastaSequenceFile; import java.io.File; import java.io.FileNotFoundException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; import net.sf.picard.reference.ReferenceSequenceFile; import net.sf.picard.reference.IndexedFastaSequenceFile; @@ -291,4 +289,58 @@ public class GenomeLocUnitTest extends BaseTest { if ( expected == ComparisonResult.EQUALS ) Assert.assertEquals(g1.hashCode(), g2.hashCode(), "Equal genome locs don't have the same hash code"); } + + // ------------------------------------------------------------------------------------- + // + // testing merging functionality + // + // ------------------------------------------------------------------------------------- + + private static final GenomeLoc loc1 = new GenomeLoc("1", 0, 10, 20); + private static final GenomeLoc loc2 = new GenomeLoc("1", 0, 21, 30); + private static final GenomeLoc loc3 = new GenomeLoc("1", 0, 31, 40); + + private class MergeTest { + public List locs; + + private MergeTest(final List locs) { + this.locs = locs; + } + } + + @DataProvider(name = "SGLtest") + public Object[][] createFindVariantRegionsData() { + List tests = new ArrayList(); + + tests.add(new Object[]{new MergeTest(Arrays.asList(loc1))}); + tests.add(new Object[]{new MergeTest(Arrays.asList(loc1, loc2))}); + tests.add(new Object[]{new MergeTest(Arrays.asList(loc1, loc2, loc3))}); + + return tests.toArray(new Object[][]{}); + } + + @Test(dataProvider = "SGLtest", enabled = true) + public void testSimpleGenomeLoc(MergeTest test) { + testMerge(test.locs); + } + + @Test(expectedExceptions = ReviewedStingException.class) + public void testNotContiguousLocs() { + final List locs = new ArrayList(1); + locs.add(loc1); + locs.add(loc3); + testMerge(locs); + } + + private void testMerge(final List locs) { + GenomeLoc result1 = locs.get(0); + for ( int i = 1; i < locs.size(); i++ ) + result1 = GenomeLoc.merge(result1, locs.get(i)); + + GenomeLoc result2 = GenomeLoc.merge(new TreeSet(locs)); + Assert.assertEquals(result1, result2); + Assert.assertEquals(result1.getStart(), locs.get(0).getStart()); + Assert.assertEquals(result1.getStop(), locs.get(locs.size() - 1).getStop()); + } + }