diff --git a/java/src/org/broadinstitute/sting/gatk/dataSources/providers/ShardDataProvider.java b/java/src/org/broadinstitute/sting/gatk/dataSources/providers/ShardDataProvider.java index f24acc281..ec949b269 100755 --- a/java/src/org/broadinstitute/sting/gatk/dataSources/providers/ShardDataProvider.java +++ b/java/src/org/broadinstitute/sting/gatk/dataSources/providers/ShardDataProvider.java @@ -141,7 +141,7 @@ public class ShardDataProvider { Collection> conflicts = registeredView.getConflictingViews(); for( Class conflict: conflicts ) { if( conflict.isInstance(view) ) - throw new StingException(String.format("Tried to registered two conflicting views: %s and %s", + throw new StingException(String.format("Tried to register two conflicting views: %s and %s", registeredView.getClass().getSimpleName(), view.getClass().getSimpleName())); } @@ -151,7 +151,7 @@ public class ShardDataProvider { for( Class conflict: view.getConflictingViews() ) { for( View registeredView: registeredViews ) { if( conflict.isInstance(registeredView) ) - throw new StingException(String.format("Tried to registered two conflicting views: %s and %s", + throw new StingException(String.format("Tried to register two conflicting views: %s and %s", registeredView.getClass().getSimpleName(), view.getClass().getSimpleName())); } diff --git a/java/test/org/broadinstitute/sting/gatk/dataSources/providers/ReferenceOrderedViewTest.java b/java/test/org/broadinstitute/sting/gatk/dataSources/providers/ReferenceOrderedViewTest.java new file mode 100755 index 000000000..8c718f19c --- /dev/null +++ b/java/test/org/broadinstitute/sting/gatk/dataSources/providers/ReferenceOrderedViewTest.java @@ -0,0 +1,116 @@ +package org.broadinstitute.sting.gatk.dataSources.providers; + +import org.junit.Test; +import org.junit.BeforeClass; +import org.junit.Assert; +import org.broadinstitute.sting.utils.fasta.IndexedFastaSequenceFile; +import org.broadinstitute.sting.utils.GenomeLoc; +import org.broadinstitute.sting.gatk.refdata.ReferenceOrderedData; +import org.broadinstitute.sting.gatk.refdata.TabularROD; +import org.broadinstitute.sting.gatk.refdata.RefMetaDataTracker; +import org.broadinstitute.sting.gatk.dataSources.shards.Shard; +import org.broadinstitute.sting.gatk.dataSources.shards.LocusShard; +import org.broadinstitute.sting.gatk.dataSources.simpleDataSources.ReferenceOrderedDataSource; +import org.broadinstitute.sting.BaseTest; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Collections; +import java.util.Arrays; +/** + * User: hanna + * Date: May 27, 2009 + * Time: 3:07:23 PM + * BROAD INSTITUTE SOFTWARE COPYRIGHT NOTICE AND AGREEMENT + * Software and documentation are copyright 2005 by the Broad Institute. + * All rights are reserved. + * + * Users acknowledge that this software is supplied without any warranty or support. + * The Broad Institute is not responsible for its use, misuse, or + * functionality. + */ + +/** + * Test the transparent view into the reference-ordered data. At the moment, just do some basic bindings and make + * sure the data comes through correctly. + */ +public class ReferenceOrderedViewTest extends BaseTest { + /** + * Sequence file. + */ + private static IndexedFastaSequenceFile seq; + + @BeforeClass + public static void init() throws FileNotFoundException { + // sequence + seq = new IndexedFastaSequenceFile(new File(seqLocation + "/references/Homo_sapiens_assembly18/v0/Homo_sapiens_assembly18.fasta")); + GenomeLoc.setupRefContigOrdering(seq); + } + + /** + * Make sure binding to an empty list produces an empty tracker. + */ + @Test + public void testNoBindings() { + Shard shard = new LocusShard(new GenomeLoc("chrM",1,30)); + ShardDataProvider provider = new ShardDataProvider(shard, null, seq, Collections.emptyList()); + ReferenceOrderedView view = new ReferenceOrderedView( provider ); + + RefMetaDataTracker tracker = view.getReferenceOrderedDataAtLocus(new GenomeLoc("chrM",10)); + Assert.assertNull("The tracker should not have produced any data", tracker.lookup("tableTest",null)); + } + + /** + * Test a single ROD binding. + */ + @Test + public void testSingleBinding() { + File file = new File(testDir + "TabularDataTest.dat"); + ReferenceOrderedData rod = new ReferenceOrderedData("tableTest", file, TabularROD.class); + ReferenceOrderedDataSource dataSource = new ReferenceOrderedDataSource(rod); + + Shard shard = new LocusShard(new GenomeLoc("chrM",1,30)); + + ShardDataProvider provider = new ShardDataProvider(shard, null, seq, Collections.singletonList(dataSource)); + ReferenceOrderedView view = new ReferenceOrderedView( provider ); + + RefMetaDataTracker tracker = view.getReferenceOrderedDataAtLocus(new GenomeLoc("chrM",20)); + TabularROD datum = (TabularROD)tracker.lookup("tableTest",null); + + Assert.assertEquals("datum parameter for COL1 is incorrect", "C", datum.get("COL1")); + Assert.assertEquals("datum parameter for COL2 is incorrect", "D", datum.get("COL2")); + Assert.assertEquals("datum parameter for COL3 is incorrect", "E", datum.get("COL3")); + } + + /** + * Make sure multiple bindings are visible from the view. + */ + @Test + public void testMultipleBinding() { + File file = new File(testDir + "TabularDataTest.dat"); + + ReferenceOrderedData rod1 = new ReferenceOrderedData("tableTest1", file, TabularROD.class); + ReferenceOrderedDataSource dataSource1 = new ReferenceOrderedDataSource(rod1); + ReferenceOrderedData rod2 = new ReferenceOrderedData("tableTest2", file, TabularROD.class); + ReferenceOrderedDataSource dataSource2 = new ReferenceOrderedDataSource(rod2); + + + Shard shard = new LocusShard(new GenomeLoc("chrM",1,30)); + + ShardDataProvider provider = new ShardDataProvider(shard, null, seq, Arrays.asList(dataSource1,dataSource2)); + ReferenceOrderedView view = new ReferenceOrderedView( provider ); + + RefMetaDataTracker tracker = view.getReferenceOrderedDataAtLocus(new GenomeLoc("chrM",20)); + TabularROD datum1 = (TabularROD)tracker.lookup("tableTest1",null); + + Assert.assertEquals("datum1 parameter for COL1 is incorrect", "C", datum1.get("COL1")); + Assert.assertEquals("datum1 parameter for COL2 is incorrect", "D", datum1.get("COL2")); + Assert.assertEquals("datum1 parameter for COL3 is incorrect", "E", datum1.get("COL3")); + + TabularROD datum2 = (TabularROD)tracker.lookup("tableTest2",null); + + Assert.assertEquals("datum2 parameter for COL1 is incorrect", "C", datum2.get("COL1")); + Assert.assertEquals("datum2 parameter for COL2 is incorrect", "D", datum2.get("COL2")); + Assert.assertEquals("datum2 parameter for COL3 is incorrect", "E", datum2.get("COL3")); + } +} diff --git a/java/test/org/broadinstitute/sting/gatk/dataSources/providers/ShardDataProviderTest.java b/java/test/org/broadinstitute/sting/gatk/dataSources/providers/ShardDataProviderTest.java new file mode 100755 index 000000000..192a38643 --- /dev/null +++ b/java/test/org/broadinstitute/sting/gatk/dataSources/providers/ShardDataProviderTest.java @@ -0,0 +1,125 @@ +package org.broadinstitute.sting.gatk.dataSources.providers; + +import org.junit.Before; +import org.junit.Assert; +import org.junit.Test; +import org.broadinstitute.sting.BaseTest; +import org.broadinstitute.sting.utils.StingException; + +import java.util.Collection; +import java.util.Collections; +import java.util.Arrays; +/** + * User: hanna + * Date: May 27, 2009 + * Time: 1:56:02 PM + * BROAD INSTITUTE SOFTWARE COPYRIGHT NOTICE AND AGREEMENT + * Software and documentation are copyright 2005 by the Broad Institute. + * All rights are reserved. + * + * Users acknowledge that this software is supplied without any warranty or support. + * The Broad Institute is not responsible for its use, misuse, or + * functionality. + */ + +/** + * Test basic functionality of the shard data provider. + */ + +public class ShardDataProviderTest extends BaseTest { + /** + * Provider to test. Should be recreated for every test. + */ + private ShardDataProvider provider = null; + + @Before + public void createProvider() { + provider = new ShardDataProvider( null,null,null,null ); + } + + /** + * Test whether views are closed when the provider closes. + */ + @Test + public void testClose() { + TestView testView = new TestView( provider ); + Assert.assertFalse("View is currently closed but should be open", testView.closed); + + provider.close(); + Assert.assertTrue("View is currently open but should be closed", testView.closed); + } + + /** + * Test whether multiple of the same view can be registered and all get a close method. + */ + @Test + public void testMultipleClose() { + Collection testViews = Arrays.asList(new TestView(provider),new TestView(provider)); + for( TestView testView: testViews ) + Assert.assertFalse("View is currently closed but should be open", testView.closed); + + provider.close(); + for( TestView testView: testViews ) + Assert.assertTrue("View is currently open but should be closed", testView.closed); + } + + /** + * Try adding a view which conflicts with some other view that's already been registered. + */ + @Test(expected=StingException.class) + public void testAddViewWithExistingConflict() { + View initial = new ConflictingTestView( provider ); + View conflictsWithInitial = new TestView( provider ); + } + + /** + * Try adding a view which has a conflict with a previously registered view. + */ + @Test(expected=StingException.class) + public void testAddViewWithNewConflict() { + View conflictsWithInitial = new TestView( provider ); + View initial = new ConflictingTestView( provider ); + } + + /** + * A simple view for testing interactions between views attached to the ShardDataProvider. + */ + private class TestView implements View { + /** + * Is the test view currently closed. + */ + private boolean closed = false; + + /** + * Create a new test view wrapping the given provider. + * @param provider + */ + public TestView( ShardDataProvider provider ) { + provider.register(this); + } + + /** + * Gets conflicting views. In this case, none conflict. + * @return + */ + public Collection> getConflictingViews() { return Collections.emptyList(); } + + /** + * Close this view. + */ + public void close() { this.closed = true; } + } + + /** + * Another view that conflicts with the one above. + */ + private class ConflictingTestView implements View { + public ConflictingTestView( ShardDataProvider provider ) { provider.register(this); } + + public Collection> getConflictingViews() { + return Collections.>singleton(TestView.class); + } + + public void close() {} + } +}