Clover icon

Coverage Report

  1. Project Clover database Wed Nov 13 2024 18:27:33 GMT
  2. Package jalview.ext.htsjdk

File TestHtsContigDb.java

 

Code metrics

0
32
4
1
150
70
5
0.16
8
4
1.25

Classes

Class Line # Actions
TestHtsContigDb 42 32 5
0.861111186.1%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.ext.htsjdk;
22   
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;
28   
29    import jalview.datamodel.SequenceI;
30   
31    import java.io.File;
32    import java.io.IOException;
33    import java.nio.file.Files;
34    import java.nio.file.StandardCopyOption;
35   
36    import org.testng.annotations.Test;
37   
38    /**
39    * @author jprocter
40    *
41    */
 
42    public class TestHtsContigDb
43    {
 
44  1 toggle @Test(groups = "Functional")
45    public final void testGetSequenceProxy() throws Exception
46    {
47  1 String pathname = "test/jalview/ext/htsjdk/pgmB.fasta";
48  1 HtsContigDb db = new HtsContigDb("ADB", new File(pathname));
49   
50  1 assertTrue(db.isValid());
51  1 assertTrue(db.isIndexed()); // htsjdk opens the .fai file
52   
53  1 SequenceI sq = db.getSequenceProxy("Deminut");
54  1 assertNotNull(sq);
55  1 assertEquals(sq.getLength(), 606);
56   
57    /*
58    * read a sequence earlier in the file
59    */
60  1 sq = db.getSequenceProxy("PPL_06716");
61  1 assertNotNull(sq);
62  1 assertEquals(sq.getLength(), 602);
63   
64    // dict = db.getDictionary(f, truncate))
65    }
66   
67    /**
68    * Trying to open a .fai file directly results in IllegalArgumentException -
69    * have to provide the unindexed file name instead
70    */
 
71  1 toggle @Test(
72    groups = "Functional",
73    expectedExceptions = java.lang.IllegalArgumentException.class)
74    public final void testGetSequenceProxy_indexed()
75    {
76  1 String pathname = "test/jalview/ext/htsjdk/pgmB.fasta.fai";
77  1 new HtsContigDb("ADB", new File(pathname));
78  0 fail("Expected exception opening .fai file");
79    }
80   
81    /**
82    * Tests that exercise
83    * <ul>
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>
88    * </ul>
89    *
90    * @throws IOException
91    */
 
92  1 toggle @Test(groups = "Functional")
93    public void testCreateFastaSequenceIndex() throws IOException
94    {
95  1 File fasta = new File("test/jalview/ext/htsjdk/pgmB.fasta");
96   
97    /*
98    * create .fai with no overwrite fails if it exists
99    */
100  1 try
101    {
102  1 HtsContigDb.createFastaSequenceIndex(fasta.toPath(), false);
103  0 fail("Expected exception");
104    } catch (IOException e)
105    {
106    // we expect an IO Exception because the pgmB.fasta.fai exists, since it
107    // was checked it in.
108    }
109   
110    /*
111    * create a copy of the .fasta (as a temp file)
112    */
113  1 File copyFasta = File.createTempFile("copyFasta", ".fasta");
114  1 copyFasta.deleteOnExit();
115  1 assertTrue(copyFasta.exists());
116  1 Files.copy(fasta.toPath(), copyFasta.toPath(),
117    StandardCopyOption.REPLACE_EXISTING);
118   
119    /*
120    * open the Fasta file - not indexed, as no .fai file yet exists
121    */
122  1 HtsContigDb db = new HtsContigDb("ADB", copyFasta);
123  1 assertTrue(db.isValid());
124  1 assertFalse(db.isIndexed());
125  1 db.close();
126   
127    /*
128    * create the .fai index, re-open the .fasta file - now indexed
129    */
130  1 HtsContigDb.createFastaSequenceIndex(copyFasta.toPath(), true);
131  1 db = new HtsContigDb("ADB", copyFasta);
132  1 assertTrue(db.isValid());
133  1 assertTrue(db.isIndexed());
134  1 db.close();
135    }
136   
137    /**
138    * A convenience 'test' that may be run to create a .fai file for any given
139    * fasta file
140    *
141    * @throws IOException
142    */
 
143  0 toggle @Test(enabled = false)
144    public void testCreateIndex() throws IOException
145    {
146   
147  0 File fasta = new File("test/jalview/io/vcf/contigs.fasta");
148  0 HtsContigDb.createFastaSequenceIndex(fasta.toPath(), true);
149    }
150    }