X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fext%2Fhtsjdk%2FTestHtsContigDb.java;h=69634a91163d2c99e70a542bed4903731a440ecd;hb=refs%2Fheads%2Fjalview-headless;hp=f8c53b08fe1619daf8f7fb7ca7e462372f262ab4;hpb=604cbee405a837565ba1a74aa9bddd62aed685ab;p=jalview.git diff --git a/test/jalview/ext/htsjdk/TestHtsContigDb.java b/test/jalview/ext/htsjdk/TestHtsContigDb.java index f8c53b0..69634a9 100644 --- a/test/jalview/ext/htsjdk/TestHtsContigDb.java +++ b/test/jalview/ext/htsjdk/TestHtsContigDb.java @@ -20,11 +20,19 @@ */ package jalview.ext.htsjdk; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.fail; + import jalview.datamodel.SequenceI; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; -import org.testng.Assert; import org.testng.annotations.Test; /** @@ -33,17 +41,110 @@ import org.testng.annotations.Test; */ public class TestHtsContigDb { - @Test - public final void testHTSReferenceSequence() throws Exception + @Test(groups = "Functional") + public final void testGetSequenceProxy() throws Exception + { + String pathname = "test/jalview/ext/htsjdk/pgmB.fasta"; + HtsContigDb db = new HtsContigDb("ADB", new File(pathname)); + + assertTrue(db.isValid()); + assertTrue(db.isIndexed()); // htsjdk opens the .fai file + + SequenceI sq = db.getSequenceProxy("Deminut"); + assertNotNull(sq); + assertEquals(sq.getLength(), 606); + + /* + * read a sequence earlier in the file + */ + sq = db.getSequenceProxy("PPL_06716"); + assertNotNull(sq); + assertEquals(sq.getLength(), 602); + + // dict = db.getDictionary(f, truncate)) + } + + /** + * Trying to open a .fai file directly results in IllegalArgumentException - + * have to provide the unindexed file name instead + */ + @Test( + groups = "Functional", + expectedExceptions = java.lang.IllegalArgumentException.class) + public final void testGetSequenceProxy_indexed() { - HtsContigDb remmadb = new HtsContigDb("REEMADB", new File( - "test/jalview/ext/htsjdk/pgmb.fasta")); + String pathname = "test/jalview/ext/htsjdk/pgmB.fasta.fai"; + new HtsContigDb("ADB", new File(pathname)); + fail("Expected exception opening .fai file"); + } + + /** + * Tests that exercise + * + * + * @throws IOException + */ + @Test(groups = "Functional") + public void testCreateFastaSequenceIndex() throws IOException + { + File fasta = new File("test/jalview/ext/htsjdk/pgmB.fasta"); + + /* + * create .fai with no overwrite fails if it exists + */ + try + { + HtsContigDb.createFastaSequenceIndex(fasta.toPath(), false); + fail("Expected exception"); + } catch (IOException e) + { + // we expect an IO Exception because the pgmB.fasta.fai exists, since it + // was checked it in. + } - Assert.assertTrue(remmadb.isValid()); + /* + * create a copy of the .fasta (as a temp file) + */ + File copyFasta = File.createTempFile("copyFasta", ".fasta"); + copyFasta.deleteOnExit(); + assertTrue(copyFasta.exists()); + Files.copy(fasta.toPath(), copyFasta.toPath(), + StandardCopyOption.REPLACE_EXISTING); - SequenceI sq = remmadb.getSequenceProxy("Deminut"); - Assert.assertNotNull(sq); - Assert.assertNotEquals(0, sq.getLength()); + /* + * open the Fasta file - not indexed, as no .fai file yet exists + */ + HtsContigDb db = new HtsContigDb("ADB", copyFasta); + assertTrue(db.isValid()); + assertFalse(db.isIndexed()); + db.close(); + + /* + * create the .fai index, re-open the .fasta file - now indexed + */ + HtsContigDb.createFastaSequenceIndex(copyFasta.toPath(), true); + db = new HtsContigDb("ADB", copyFasta); + assertTrue(db.isValid()); + assertTrue(db.isIndexed()); + db.close(); } + /** + * A convenience 'test' that may be run to create a .fai file for any given + * fasta file + * + * @throws IOException + */ + @Test(enabled = false) + public void testCreateIndex() throws IOException + { + + File fasta = new File("test/jalview/io/vcf/contigs.fasta"); + HtsContigDb.createFastaSequenceIndex(fasta.toPath(), true); + } }