| 1 |
package org.broadinstitute.hellbender.utils.bwa; |
| 2 |
|
| 3 |
import org.testng.Assert; |
| 4 |
import org.testng.Reporter; |
| 5 |
import org.testng.annotations.AfterClass; |
| 6 |
import org.testng.annotations.BeforeClass; |
| 7 |
import org.testng.annotations.DataProvider; |
| 8 |
import org.testng.annotations.Test; |
| 9 |
|
| 10 |
import java.io.File; |
| 11 |
import java.io.FileWriter; |
| 12 |
import java.io.IOException; |
| 13 |
import java.io.PrintWriter; |
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.Collections; |
| 16 |
import java.util.List; |
| 17 |
import java.util.Random; |
| 18 |
import java.util.*; |
| 19 |
import java.util.stream.Stream; |
| 20 |
|
| 21 |
@Test |
| 22 |
public final class BwaMemIndexTest { |
| 23 |
private static BwaMemIndex index; |
| 24 |
|
| 25 |
private static final String[] INDEX_EXTENSIONS = {".amb", ".ann", ".bwt", ".pac", ".sa" }; |
| 26 |
|
| 27 |
@BeforeClass |
| 28 |
void openIndex() { |
| 29 |
final String indexImageFile = "src/test/resources/ref.fa.img"; |
| 30 |
new File(indexImageFile).deleteOnExit(); |
| 31 |
BwaMemIndex.createIndexImageFromIndexFiles("src/test/resources/ref.fa", indexImageFile ); |
| 32 |
index = new BwaMemIndex(indexImageFile); |
| 33 |
} |
| 34 |
|
| 35 |
@AfterClass |
| 36 |
void closeIndex() { index.close(); index = null; } |
| 37 |
|
| 38 |
@Test |
| 39 |
void testOptsSize() { |
| 40 |
try ( final BwaMemAligner aligner = new BwaMemAligner(index) ) { |
| 41 |
Assert.assertEquals(aligner.getOptsSize(), aligner.getExpectedOptsSize()); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
@Test |
| 46 |
void testSimple() { |
| 47 |
final BwaMemAligner aligner = new BwaMemAligner(index); |
| 48 |
final List<List<BwaMemAlignment>> alignments = aligner.alignSeqs(Collections.singletonList( |
| 49 |
// first line in ref.fa |
| 50 |
"GGCTTTTAATGCTTTTCAGTGGTTGCTGCTCAAGATGGAGTCTACTCAGCAGATGGTAAGCTCTATTATT".getBytes() |
| 51 |
)); |
| 52 |
Assert.assertNotNull(alignments); |
| 53 |
Assert.assertEquals(alignments.size(), 1); |
| 54 |
final List<BwaMemAlignment> alignmentList = alignments.get(0); |
| 55 |
Assert.assertNotNull(alignmentList); |
| 56 |
Assert.assertEquals(alignmentList.size(), 1); |
| 57 |
testAlignment(alignmentList.get(0), 0, 70, 0, 70, "70M", 0, 0); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
void testMulti() { |
| 62 |
final List<String> seqs = new ArrayList<>(); |
| 63 |
seqs.add("GGCTTTTAATGCTTTTCAGTGCTAGGTGCTCAAGATGGAGTCTACTCAGCAGATGGTAAGCTCTATTATT"); // 3 snvs |
| 64 |
seqs.add("AATAATAGAGCTTACCATCTGCTGAGTAGACTCCATCTTGAGCAGCAACCACTGAAAAGCATTAAAAGCC"); // rc |
| 65 |
seqs.add("AATACTTCTTTTGAAGCTGCAGTTGTTGCTGCCTTCAACATTAGAATTAATGGGTATTCAATATGATT"); // 2-base deletion |
| 66 |
final BwaMemAligner aligner = new BwaMemAligner(index); |
| 67 |
final List<List<BwaMemAlignment>> alignments = aligner.alignSeqs(seqs,String::getBytes); |
| 68 |
Assert.assertNotNull(alignments); |
| 69 |
Assert.assertEquals(alignments.size(), 3); |
| 70 |
List<BwaMemAlignment> alignmentList = alignments.get(0); |
| 71 |
Assert.assertNotNull(alignmentList); |
| 72 |
Assert.assertEquals(alignmentList.size(), 1); |
| 73 |
testAlignment(alignmentList.get(0), 0, 70, 0, 70, "70M", 3, 0); // 3 snvs |
| 74 |
alignmentList = alignments.get(1); |
| 75 |
Assert.assertNotNull(alignmentList); |
| 76 |
Assert.assertEquals(alignmentList.size(), 1); |
| 77 |
testAlignment(alignmentList.get(0), 0, 70, 0, 70, "70M", 0, 0x10); // rc |
| 78 |
alignmentList = alignments.get(2); |
| 79 |
Assert.assertNotNull(alignmentList); |
| 80 |
Assert.assertEquals(alignmentList.size(), 1); |
| 81 |
testAlignment(alignmentList.get(0), 70, 140, 0, 68, "32M2D36M", 2, 0); // 2-base deletion |
| 82 |
} |
| 83 |
|
| 84 |
@Test |
| 85 |
void testPair() { |
| 86 |
final List<String> seqs = new ArrayList<>(); |
| 87 |
seqs.add("GGCTTTTAATGCTTTTCAGTGGTTGCTGCTCAAGATGGAGTCTACTCAGCAGATGGTAAGCTCTATTATT"); // ref.fa line 1 |
| 88 |
seqs.add("TTGTTTTTAACACCAGAGTCATCCATCACATAATCAAATTTACTTTTAACTCTGGTAAATACTTCATTGT"); // rc ref.fa line 3 |
| 89 |
final BwaMemAligner aligner = new BwaMemAligner(index); |
| 90 |
aligner.alignPairs(); |
| 91 |
final List<List<BwaMemAlignment>> alignments = aligner.alignSeqs(seqs,String::getBytes); |
| 92 |
Assert.assertNotNull(alignments); |
| 93 |
Assert.assertEquals(alignments.size(), 2); |
| 94 |
List<BwaMemAlignment> alignmentList = alignments.get(0); |
| 95 |
Assert.assertNotNull(alignmentList); |
| 96 |
Assert.assertEquals(alignmentList.size(), 1); |
| 97 |
BwaMemAlignment alignment = alignmentList.get(0); |
| 98 |
testAlignment(alignment, 0, 70, 0, 70, "70M", 0, 0x61); |
| 99 |
Assert.assertEquals(alignment.getMateRefStart(), 140); |
| 100 |
Assert.assertEquals(alignment.getTemplateLen(), 210); |
| 101 |
alignmentList = alignments.get(1); |
| 102 |
Assert.assertNotNull(alignmentList); |
| 103 |
Assert.assertEquals(alignmentList.size(), 1); |
| 104 |
alignment = alignmentList.get(0); |
| 105 |
testAlignment(alignment, 140, 210, 0, 70, "70M", 0, 0x91); |
| 106 |
Assert.assertEquals(alignment.getMateRefStart(), 0); |
| 107 |
Assert.assertEquals(alignment.getTemplateLen(), -210); |
| 108 |
} |
| 109 |
|
| 110 |
void testAlignment( final BwaMemAlignment alignment, |
| 111 |
final int refStart, final int refEnd, final int seqStart, final int seqEnd, |
| 112 |
final String cigar, final int nMismatches, final int samFlag ) { |
| 113 |
Assert.assertEquals(alignment.getRefStart(), refStart); |
| 114 |
Assert.assertEquals(alignment.getRefEnd(), refEnd); |
| 115 |
Assert.assertEquals(alignment.getSeqStart(), seqStart); |
| 116 |
Assert.assertEquals(alignment.getSeqEnd(), seqEnd); |
| 117 |
Assert.assertEquals(alignment.getCigar(), cigar); |
| 118 |
Assert.assertEquals(alignment.getNMismatches(), nMismatches); |
| 119 |
Assert.assertEquals(alignment.getRefId(), 0); |
| 120 |
Assert.assertEquals(alignment.getSamFlag(), samFlag); |
| 121 |
} |
| 122 |
|
| 123 |
@Test(dataProvider = "algorithmsData") |
| 124 |
void testIndexReference(final BwaMemIndex.Algorithm alg) throws IOException, IOException { |
| 125 |
final char[] refSeq1 = new char[45212]; |
| 126 |
final char[] refSeq2 = new char[13415]; |
| 127 |
final char[] nucletotides = new char[] { 'A', 'C', 'G', 'T'}; |
| 128 |
final Random rdn = new Random(13); |
| 129 |
for (int i = 0; i < refSeq1.length; i++) { |
| 130 |
refSeq1[i] = nucletotides[rdn.nextInt(nucletotides.length)]; |
| 131 |
} |
| 132 |
for (int i = 0; i < refSeq2.length; i++) { |
| 133 |
refSeq2[i] = nucletotides[rdn.nextInt(nucletotides.length)]; |
| 134 |
} |
| 135 |
|
| 136 |
final File fastaFile = File.createTempFile("test", ".fasta"); |
| 137 |
//fastaFile.deleteOnExit(); |
| 138 |
|
| 139 |
final int basesPerLine = 60; |
| 140 |
try (final PrintWriter fastaWriter = new PrintWriter(new FileWriter(fastaFile))) { |
| 141 |
fastaWriter.println(">seq1"); |
| 142 |
for (int i = 0; i < refSeq1.length; i += basesPerLine) { |
| 143 |
fastaWriter.println(new String(refSeq1, i, Math.min(basesPerLine, refSeq1.length - i))); |
| 144 |
} |
| 145 |
fastaWriter.println(">seq2"); |
| 146 |
for (int i = 0; i < refSeq2.length; i += basesPerLine) { |
| 147 |
fastaWriter.println(new String(refSeq2, i, Math.min(basesPerLine, refSeq2.length - i))); |
| 148 |
} |
| 149 |
} |
| 150 |
final File imageFile = new File(fastaFile.getPath() + ".idx"); |
| 151 |
imageFile.deleteOnExit(); |
| 152 |
BwaMemIndex.createIndexImageFromFastaFile(fastaFile.getPath(), imageFile.getPath()); |
| 153 |
final BwaMemIndex index = new BwaMemIndex(imageFile.getPath() ); |
| 154 |
Assert.assertEquals(index.getReferenceContigNames(), Arrays.asList("seq1", "seq2")); |
| 155 |
index.close(); |
| 156 |
fastaFile.delete(); |
| 157 |
imageFile.delete(); |
| 158 |
} |
| 159 |
|
| 160 |
private static void deleteOnExit(final String prefix) { |
| 161 |
Stream.of(INDEX_EXTENSIONS) |
| 162 |
.map(ext -> prefix + ext) |
| 163 |
.map(File::new) |
| 164 |
.forEach(File::deleteOnExit); |
| 165 |
} |
| 166 |
private static void delete(final String prefix) { |
| 167 |
Stream.of(INDEX_EXTENSIONS) |
| 168 |
.map(ext -> prefix + ext) |
| 169 |
.map(File::new) |
| 170 |
.forEach(File::deleteOnExit); |
| 171 |
} |
| 172 |
|
| 173 |
private static void assertNonEmptyReadableIndexFiles(final String prefix) { |
| 174 |
Stream.of(INDEX_EXTENSIONS) |
| 175 |
.map(ext -> prefix + ext) |
| 176 |
.map(File::new) |
| 177 |
.forEach(f -> { |
| 178 |
Assert.assertTrue(f.exists()); |
| 179 |
Assert.assertTrue(f.isFile()); |
| 180 |
Assert.assertTrue(f.canRead()); |
| 181 |
Assert.assertTrue(f.length() > 0); |
| 182 |
}); |
| 183 |
} |
| 184 |
|
| 185 |
@DataProvider(name = "algorithmsData") |
| 186 |
Object[][] algorithmsData() { |
| 187 |
return Stream.of(BwaMemIndex.Algorithm.values()) |
| 188 |
.map(a -> new Object[] { a }).toArray(Object[][]::new); |
| 189 |
} |
| 190 |
} |