2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ext.htsjdk;
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertFalse;
25 import static org.testng.Assert.assertNotNull;
26 import static org.testng.Assert.assertTrue;
27 import static org.testng.Assert.fail;
29 import jalview.datamodel.SequenceI;
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.StandardCopyOption;
36 import org.testng.annotations.Test;
42 public class TestHtsContigDb
44 @Test(groups = "Functional")
45 public final void testGetSequenceProxy() throws Exception
47 String pathname = "test/jalview/ext/htsjdk/pgmb.fasta";
48 HtsContigDb db = new HtsContigDb("ADB", new File(pathname));
50 assertTrue(db.isValid());
51 assertTrue(db.isIndexed()); // htsjdk opens the .fai file
53 SequenceI sq = db.getSequenceProxy("Deminut");
55 assertEquals(sq.getLength(), 606);
58 * read a sequence earlier in the file
60 sq = db.getSequenceProxy("PPL_06716");
62 assertEquals(sq.getLength(), 602);
64 // dict = db.getDictionary(f, truncate))
68 * Trying to open a .fai file directly results in IllegalArgumentException -
69 * have to provide the unindexed file name instead
72 groups = "Functional",
73 expectedExceptions = java.lang.IllegalArgumentException.class)
74 public final void testGetSequenceProxy_indexed()
76 String pathname = "test/jalview/ext/htsjdk/pgmb.fasta.fai";
77 new HtsContigDb("ADB", new File(pathname));
78 fail("Expected exception opening .fai file");
84 * <li>opening an unindexed fasta file</li>
85 * <li>creating a .fai index</li>
86 * <li>opening the fasta file, now using the index</li>
87 * <li>error on creating index if overwrite not allowed</li>
92 @Test(groups = "Functional")
93 public void testCreateFastaSequenceIndex() throws IOException
95 File fasta = new File("test/jalview/ext/htsjdk/pgmb.fasta");
98 * create .fai with no overwrite fails if it exists
101 HtsContigDb.createFastaSequenceIndex(fasta.toPath(), false);
102 fail("Expected exception");
103 } catch (IOException e)
109 * create a copy of the .fasta (as a temp file)
111 File copyFasta = File.createTempFile("copyFasta", ".fasta");
112 copyFasta.deleteOnExit();
113 assertTrue(copyFasta.exists());
114 Files.copy(fasta.toPath(), copyFasta.toPath(),
115 StandardCopyOption.REPLACE_EXISTING);
118 * open the Fasta file - not indexed, as no .fai file yet exists
120 HtsContigDb db = new HtsContigDb("ADB", copyFasta);
121 assertTrue(db.isValid());
122 assertFalse(db.isIndexed());
126 * create the .fai index, re-open the .fasta file - now indexed
128 HtsContigDb.createFastaSequenceIndex(copyFasta.toPath(), true);
129 db = new HtsContigDb("ADB", copyFasta);
130 assertTrue(db.isValid());
131 assertTrue(db.isIndexed());
136 * A convenience 'test' that may be run to create a .fai file for any given
139 * @throws IOException
141 @Test(enabled = false)
142 public void testCreateIndex() throws IOException
145 File fasta = new File("test/jalview/io/vcf/contigs.fasta");
146 HtsContigDb.createFastaSequenceIndex(fasta.toPath(), true);