Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions gortools/src/test/java/gorsat/parser/UTestGenomicFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ public void testRefBases() {
Assert.assertEquals("cct", TestUtils.getCalculatedWithArgs("refbases('chr1', 10101, 10103)", new String[]{"-config", "../tests/data/ref_mini/gor_config.txt"}));
}

@Test
public void testRefBasesBelowPositionOne() {
String[] args = new String[]{"gorrow chr17,0 | calc r refbases(chrom,pos,pos+3)", "-config", "../tests/data/ref_mini/gor_config.txt"};
String lines = TestUtils.runGorPipe(args);
Assert.assertEquals("chrom\tpos\tr\nchr17\t0\tNAAG\n", lines);
}

@Test
public void testRefBasesWithBuildBelowPositionOne() {
String lines = TestUtils.runGorPipe("gorrow chr17,0 | calc r refbases_with_build(chrom,pos,pos+3,'../tests/data/ref_mini/chromSeq')");
Assert.assertEquals("chrom\tpos\tr\nchr17\t0\tNAAG\n", lines);
}

@Test
public void testRefBasesWithBuildUnQuoted() {
TestUtils.assertCalculated("refbases_with_build('chr1', 10101, 10103, ../tests/data/ref_mini/chromSeq)", "cct");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ class RefSeqFromChromSeq(ipath : String, fileReader : FileReader) extends RefSeq

def getBase(chr: String, pos: Int): Char = {
if (noReferenceBuildFound) return 'N'
// Positions below 1 are outside the chromosome, same as positions past its end.
if (pos < 1) return 'N'
try {
val (buffKey, offset) = getKeyAndOffset(chr, pos)

Expand Down Expand Up @@ -165,6 +167,12 @@ class RefSeqFromChromSeq(ipath : String, fileReader : FileReader) extends RefSeq
}

def getBases(chr: String, pos1: Int, pos2: Int): String = {
// Positions below 1 are outside the chromosome and read as 'N', same as positions past its end.
// Handle them before the buffer lookup: (pos - 1) / buffLength truncates toward zero, so pos 0 maps to buffer 0.
if (pos1 < 1) {
val leading = "N" * (math.min(pos2, 0) - pos1 + 1)
return if (pos2 < 1) leading else leading + getBases(chr, 1, pos2)
}
if (pos1 == pos2) return getBase(chr, pos1).toString
if ((pos1 - 1) / buffLength == (pos2 - 1) / buffLength) {
val (buffKey, offset) = getKeyAndOffset(chr, pos1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,46 @@ public void testGetRefbases() {

}

// Positions below 1 (e.g. pos 0 from an unmapped liftover row) read as 'N', like positions past the chromosome end.
@Test
public void testGetRefbasesBelowPositionOne() {
String path = "../tests/data/ref_mini/chromSeq";

// chr17 in ref_mini starts with real bases (AAGC...).
RefSeqFromChromSeq refseq = new RefSeqFromChromSeq(path, new DriverBackedFileReader(""));
Assert.assertEquals("NAAGC", refseq.getBases("chr17", 0, 4));

// Same, with the buffer already loaded.
Assert.assertEquals("NAAGC", refseq.getBases("chr17", 0, 4));

// Fresh refseq, no buffer loaded yet.
refseq = new RefSeqFromChromSeq(path, new DriverBackedFileReader(""));
Assert.assertEquals("NNNNN", refseq.getBases("chr17", -5, -1));

Assert.assertEquals("N", refseq.getBases("chr17", 0, 0));
Assert.assertEquals('N', refseq.getBase("chr17", 0));
Assert.assertEquals('N', refseq.getBase("chr17", -1));
Assert.assertEquals('N', refseq.getBase("chr17", -10001));

// Starting below 1 and crossing the buffer boundary (10000).
String expectedTail = new RefSeqFromChromSeq(path, new DriverBackedFileReader("")).getBases("chr17", 1, 10002);
refseq = new RefSeqFromChromSeq(path, new DriverBackedFileReader(""));
String bases = refseq.getBases("chr17", -2, 10002);
Assert.assertEquals(10005, bases.length());
Assert.assertEquals("NNN" + expectedTail, bases);
Assert.assertTrue(bases.startsWith("NNNAAGC"));
Assert.assertTrue(bases.endsWith("aactctt"));

// Starting more than a buffer below 1.
refseq = new RefSeqFromChromSeq(path, new DriverBackedFileReader(""));
Assert.assertEquals("N".repeat(10006) + "AA", refseq.getBases("chr17", -10005, 2));

// Positive positions are unchanged.
Assert.assertEquals("AAGC", refseq.getBases("chr17", 1, 4));
Assert.assertEquals('A', refseq.getBase("chr17", 1));
Assert.assertEquals("aactcttgac", refseq.getBases("chr17", 9996, 10005));
}

@Ignore("Run manually to test from same buffer optimization")
@Test
public void testGetRefbasesPerformance() {
Expand Down
Loading