1010import org .testng .annotations .DataProvider ;
1111import org .testng .annotations .Test ;
1212
13- import java .io .ByteArrayOutputStream ;
1413import java .io .IOException ;
1514import java .io .InputStream ;
1615import java .nio .ByteBuffer ;
2019import java .util .ArrayList ;
2120import java .util .List ;
2221
22+ import static htsjdk .samtools .cram .CRAMInteropTestUtils .filterEmbeddedNewlines ;
23+ import static htsjdk .samtools .cram .CRAMInteropTestUtils .getInteropCompressedFilePaths ;
24+ import static htsjdk .samtools .cram .CRAMInteropTestUtils .getParamsFormatFlags ;
25+ import static htsjdk .samtools .cram .CRAMInteropTestUtils .getUnCompressedFilePath ;
26+
2327public class RangeInteropTest extends HtsjdkTest {
2428 public static final String COMPRESSED_RANGE_DIR = "arith" ;
2529
@@ -30,13 +34,13 @@ public Object[][] getRoundTripTestCases() throws IOException {
3034 // compressed testfile path, uncompressed testfile path,
3135 // Range encoder, Range decoder, Range params
3236 final List <Object []> testCases = new ArrayList <>();
33- for (Path path : getInteropRangeCompressedFilePaths (COMPRESSED_RANGE_DIR )) {
37+ for (Path path : getInteropCompressedFilePaths (COMPRESSED_RANGE_DIR )) {
3438 Object [] objects = new Object []{
3539 path ,
36- getRangeUnCompressedFilePath (path ),
40+ getUnCompressedFilePath (path ),
3741 new RangeEncode (),
3842 new RangeDecode (),
39- getRangeParams ( path )
43+ new RangeParams ( getParamsFormatFlags ( path ) )
4044 };
4145 testCases .add (objects );
4246 }
@@ -109,68 +113,4 @@ public void testDecodeOnly(
109113 }
110114 }
111115
112- // the input files have embedded newlines that the test remove before round-tripping...
113- private final byte [] filterEmbeddedNewlines (final byte [] rawBytes ) throws IOException {
114- // 1. filters new lines if any.
115- // 2. "q40+dir" file has an extra column delimited by tab. This column provides READ1 vs READ2 flag.
116- // This file is also new-line separated. The extra column, '\t' and '\n' are filtered.
117- try (final ByteArrayOutputStream baos = new ByteArrayOutputStream ()) {
118- int skip = 0 ;
119- for (final byte b : rawBytes ) {
120- if (b == '\t' ){
121- skip = 1 ;
122- }
123- if (b == '\n' ) {
124- skip = 0 ;
125- }
126- if (skip == 0 && b !='\n' ) {
127- baos .write (b );
128- }
129- }
130- return baos .toByteArray ();
131- }
132- }
133-
134- // return a list of all encoded test data files in the htscodecs/tests/dat/<compressedDir> directory
135- private List <Path > getInteropRangeCompressedFilePaths (final String compressedDir ) throws IOException {
136- final List <Path > paths = new ArrayList <>();
137- Files .newDirectoryStream (
138- CRAMInteropTestUtils .getInteropTestDataLocation ().resolve ("dat/" +compressedDir ),
139- path -> Files .isRegularFile (path ))
140- .forEach (path -> paths .add (path ));
141- return paths ;
142- }
143-
144- // Given a compressed test file path, return the corresponding uncompressed file path
145- public static final Path getRangeUnCompressedFilePath (final Path compressedInteropPath ) {
146- String uncompressedFileName = getUncompressedFileName (compressedInteropPath .getFileName ().toString ());
147- // Example compressedInteropPath: ../dat/r4x8/q4.1 => unCompressedFilePath: ../dat/q4
148- return compressedInteropPath .getParent ().getParent ().resolve (uncompressedFileName );
149- }
150-
151- public static final String getUncompressedFileName (final String compressedFileName ) {
152- // Returns original filename from compressed file name
153- int lastDotIndex = compressedFileName .lastIndexOf ("." );
154- if (lastDotIndex >= 0 ) {
155- return compressedFileName .substring (0 , lastDotIndex );
156- } else {
157- throw new CRAMException ("The format of the compressed File Name is not as expected. " +
158- "The name of the compressed file should contain a period followed by a number that" +
159- "indicates the order of compression. Actual compressed file name = " + compressedFileName );
160- }
161- }
162-
163- public static final RangeParams getRangeParams (final Path compressedInteropPath ){
164- // Returns RangeParams from compressed file path
165- final String compressedFileName = compressedInteropPath .getFileName ().toString ();
166- final int lastDotIndex = compressedFileName .lastIndexOf ("." );
167- if (lastDotIndex >= 0 && lastDotIndex < compressedFileName .length () - 1 ) {
168- return new RangeParams (Integer .parseInt (compressedFileName .substring (lastDotIndex + 1 )));
169- } else {
170- throw new CRAMException ("The format of the compressed File Name is not as expected. " +
171- "The name of the compressed file should contain a period followed by a number that" +
172- "indicates the order of compression. Actual compressed file name = " + compressedFileName );
173- }
174- }
175-
176116}
0 commit comments