Commit f4e40a13 authored by Peter van 't Hof's avatar Peter van 't Hof
Browse files

Added more, now almost 100% coverage

parent cac5d7d3
......@@ -103,7 +103,8 @@ case class BedRecord(chr: String,
def toSamInterval = (name, strand) match {
case (Some(name), Some(strand)) => new Interval(chr, start + 1, end, !strand, name)
case _ => new Interval(chr, start + 1, end)
case (Some(name), _) => new Interval(chr, start + 1, end, false, name)
case _ => new Interval(chr, start + 1, end)
}
}
......@@ -116,7 +117,7 @@ object BedRecord {
values(1).toInt,
values(2).toInt,
values.lift(3),
values.lift(4).map(_.toInt),
values.lift(4).map(_.toDouble),
values.lift(5).map {
case "-" => false
case "+" => true
......
......@@ -12,11 +12,11 @@ import nl.lumc.sasc.biopet.core.Logging
/**
* Created by pjvan_thof on 8/20/15.
*/
class BedRecordList(val chrRecords: Map[String, List[BedRecord]], header: List[String] = Nil) {
case class BedRecordList(val chrRecords: Map[String, List[BedRecord]], val header: List[String] = Nil) {
def allRecords = for (chr <- chrRecords; record <- chr._2) yield record
def toSamIntervals = allRecords.map(_.toSamInterval)
lazy val sorted = {
val sorted = new BedRecordList(chrRecords.map(x => x._1 -> x._2.sortWith((a, b) => a.start < b.start)))
if (sorted.chrRecords.forall(x => x._2 == chrRecords(x._1))) this else sorted
......@@ -31,26 +31,27 @@ class BedRecordList(val chrRecords: Map[String, List[BedRecord]], header: List[S
def length = allRecords.foldLeft(0L)((a, b) => a + b.length)
def squishBed(strandSensitive: Boolean = true) = BedRecordList.fromList {
def squishBed(strandSensitive: Boolean = true, nameSensitive: Boolean = true) = BedRecordList.fromList {
(for ((chr, records) <- sorted.chrRecords; record <- records) yield {
val overlaps = overlapWith(record)
.filterNot(_ == record)
.filterNot(strandSensitive && _.strand != record.strand)
.filterNot(_.name == record.name)
.filterNot(nameSensitive && _.name == record.name)
if (overlaps.isEmpty) {
List(record)
} else {
overlaps
.foldLeft(List(record))((result, overlap) => {
(for (r <- result) yield {
(overlap.start < r.start, overlap.end > r.end) match {
case (true, true) =>
(overlap.start <= r.start, overlap.end >= r.end) match {
case (true, true) =>
Nil
case (true, false) =>
List(r.copy(start = overlap.end))
case (false, true) =>
List(r.copy(end = overlap.start))
case (true, false) =>
List(r.copy(start = overlap.end, _originals = List(r)))
case (false, true) =>
List(r.copy(end = overlap.start, _originals = List(r)))
case (false, false) =>
List(r.copy(end = overlap.start), r.copy(start = overlap.end))
List(r.copy(end = overlap.start, _originals = List(r)), r.copy(start = overlap.end, _originals = List(r)))
}
}).flatten
})
......
......@@ -2,5 +2,5 @@ chrQ 300 350 rRNA03 0 +
chrQ 350 400 rRNA03 0 +
chrQ 450 480 rRNA02 0 -
chrQ 470 475 rRNA04 0 -
chrQ 1 200 rRNA01 0 .
chrQ 150 250 rRNA01 0 .
chrQ 1 200 rRNA01 0
chrQ 150 250 rRNA01 0
package nl.lumc.sasc.biopet.utils.intervals
import java.io.{PrintWriter, File}
import java.io.{ PrintWriter, File }
import htsjdk.samtools.util.Interval
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.{Test, AfterClass, BeforeClass}
import org.testng.annotations.{ Test, AfterClass, BeforeClass }
import scala.io.Source
......@@ -19,6 +20,11 @@ class BedRecordListTest extends TestNGSuite with Matchers {
writer.print(BedRecordListTest.bedContent)
writer.close()
}
{
val writer = new PrintWriter(BedRecordListTest.corruptBedFile)
writer.print(BedRecordListTest.corruptBedContent)
writer.close()
}
{
val writer = new PrintWriter(BedRecordListTest.bedFileUcscHeader)
writer.print(BedRecordListTest.ucscHeader)
......@@ -27,15 +33,101 @@ class BedRecordListTest extends TestNGSuite with Matchers {
}
}
@Test def test {
println("test")
@Test def testReadBedFile {
val records = BedRecordList.fromFile(BedRecordListTest.bedFile)
records.allRecords.size shouldBe 2
records.header shouldBe Nil
val tempFile = File.createTempFile("region", ".bed")
records.writeToFile(tempFile)
BedRecordList.fromFile(tempFile) shouldBe records
tempFile.delete()
}
@Test def testReadBedFileUcscHeader {
val records = BedRecordList.fromFile(BedRecordListTest.bedFileUcscHeader)
records.allRecords.size shouldBe 2
records.header shouldBe BedRecordListTest.ucscHeader.split("\n").toList
val tempFile = File.createTempFile("region", ".bed")
records.writeToFile(tempFile)
BedRecordList.fromFile(tempFile) shouldBe records
tempFile.delete()
}
@Test def testSorted: Unit = {
val unsorted = BedRecordList.fromList(List(BedRecord("chrQ", 10, 20), BedRecord("chrQ", 0, 10)))
unsorted.isSorted shouldBe false
unsorted.sorted.isSorted shouldBe true
val sorted = BedRecordList.fromList(List(BedRecord("chrQ", 0, 10), BedRecord("chrQ", 10, 20)))
sorted.isSorted shouldBe true
sorted.sorted.isSorted shouldBe true
sorted.hashCode() shouldBe sorted.sorted.hashCode()
}
@Test def testOverlap: Unit = {
val list = BedRecordList.fromList(List(BedRecord("chrQ", 0, 10), BedRecord("chrQ", 10, 20)))
list.overlapWith(BedRecord("chrQ", 5, 15)).size shouldBe 2
list.overlapWith(BedRecord("chrQ", 0, 10)).size shouldBe 1
list.overlapWith(BedRecord("chrQ", 10, 20)).size shouldBe 1
list.overlapWith(BedRecord("chrQ", 19, 25)).size shouldBe 1
list.overlapWith(BedRecord("chrQ", 20, 25)).size shouldBe 0
}
@Test def testLength: Unit = {
val list = BedRecordList.fromList(List(BedRecord("chrQ", 0, 10), BedRecord("chrQ", 10, 20)))
list.length shouldBe 20
}
@Test def testCombineOverlap: Unit = {
val noOverlapList = BedRecordList.fromList(List(BedRecord("chrQ", 0, 10), BedRecord("chrQ", 10, 20)))
noOverlapList.length shouldBe 20
noOverlapList.combineOverlap.length shouldBe 20
val overlapList = BedRecordList.fromList(List(BedRecord("chrQ", 0, 10), BedRecord("chrQ", 5, 15), BedRecord("chrQ", 10, 20)))
overlapList.length shouldBe 30
overlapList.combineOverlap.length shouldBe 20
}
@Test def testSquishBed: Unit = {
val noOverlapList = BedRecordList.fromList(List(BedRecord("chrQ", 0, 10), BedRecord("chrQ", 10, 20)))
noOverlapList.length shouldBe 20
noOverlapList.squishBed().length shouldBe 20
val overlapList = BedRecordList.fromList(List(
BedRecord("chrQ", 0, 10),
BedRecord("chrQ", 5, 15),
BedRecord("chrQ", 10, 20),
BedRecord("chrQ", 25, 35),
BedRecord("chrQ", 50, 80),
BedRecord("chrQ", 60, 70)
))
overlapList.length shouldBe 80
val squishedList = overlapList.squishBed(strandSensitive = false, nameSensitive = false)
squishedList.allRecords.size shouldBe 5
squishedList.length shouldBe 40
}
@Test def testSamInterval: Unit = {
val list = BedRecordList.fromList(List(BedRecord("chrQ", 0, 10), BedRecord("chrQ", 5, 15)))
list.toSamIntervals.toList shouldBe List(new Interval("chrQ", 1, 10), new Interval("chrQ", 6, 15))
}
@Test def testTraversable: Unit = {
val list = List(BedRecord("chrQ", 0, 10))
BedRecordList.fromList(list) shouldBe BedRecordList.fromList(list.toIterator)
}
@Test def testErrors: Unit = {
intercept[IllegalArgumentException] {
val records = BedRecordList.fromFile(BedRecordListTest.corruptBedFile)
}
}
@AfterClass
def end: Unit = {
println(BedRecordListTest.bedFile)
Source.fromFile(BedRecordListTest.bedFile).getLines().foreach(println)
BedRecordListTest.bedFile.delete()
BedRecordListTest.corruptBedFile.delete()
BedRecordListTest.bedFileUcscHeader.delete()
}
}
......@@ -45,8 +137,12 @@ object BedRecordListTest {
|browser hide all
|track name="ItemRGBDemo" description="Item RGB demonstration" visibility=2 itemRgb="On"
|""".stripMargin
val bedContent = """chr22 1000 5000 cloneA 960 + 1000 5000 0 2 567,488, 0,3512
|chr22 2000 6000 cloneB 900 - 2000 6000 0 2 433,399, 0,3601""".stripMargin
val bedContent = """chr22 1000 5000 cloneA 960 + 1000 5000 0 2 567,488 0,3512
|chr22 2000 6000 cloneB 900 - 2000 6000 0 2 433,399 0,3601""".stripMargin
val corruptBedContent = """chr22 5000 1000 cloneA 960 + 1000 5000 0 2 567,488 0,3512
|chr22 2000 6000 cloneB 900 - 2000 6000 0 2 433,399 0,3601""".stripMargin
val bedFile = File.createTempFile("regions", ".bed")
val corruptBedFile = File.createTempFile("regions", ".bed")
val bedFileUcscHeader = File.createTempFile("regions", ".bed")
}
\ No newline at end of file
......@@ -16,10 +16,10 @@ class BedRecordTest extends TestNGSuite with Matchers {
BedRecord.fromLine("chrQ\t0\t4\tname\t3\t+\t1\t3") shouldBe
BedRecord("chrQ", 0, 4, Some("name"), Some(3.0), Some(true), Some(1), Some(3))
BedRecord.fromLine("chrQ\t0\t4\tname\t3\t+\t1\t3\t255,0,0") shouldBe
BedRecord("chrQ", 0, 4, Some("name"), Some(3.0), Some(true), Some(1), Some(3), Some((255,0,0)))
BedRecord("chrQ", 0, 4, Some("name"), Some(3.0), Some(true), Some(1), Some(3), Some((255, 0, 0)))
BedRecord.fromLine("chrQ\t0\t100\tname\t3\t+\t1\t3\t255,0,0\t2\t10,20\t20,50") shouldBe
BedRecord("chrQ", 0, 100, Some("name"), Some(3.0), Some(true), Some(1), Some(3), Some((255,0,0)),
Some(2), IndexedSeq(10,20), IndexedSeq(20,50))
BedRecord("chrQ", 0, 100, Some("name"), Some(3.0), Some(true), Some(1), Some(3), Some((255, 0, 0)),
Some(2), IndexedSeq(10, 20), IndexedSeq(20, 50))
}
@Test def testLineOutput: Unit = {
......@@ -113,7 +113,18 @@ class BedRecordTest extends TestNGSuite with Matchers {
utr3 should not be None
utr5.get.length shouldBe 7
utr3.get.length shouldBe 3
}
@Test def testOriginals: Unit = {
val original = BedRecord("chrQ", 1, 2)
val level1 = BedRecord("chrQ", 1, 2, _originals = List(original))
val level2 = BedRecord("chrQ", 2, 3, _originals = List(level1))
original.originals() shouldBe List(original)
original.originals(nested = false) shouldBe List(original)
level1.originals() shouldBe List(original)
level1.originals(nested = false) shouldBe List(original)
level2.originals() shouldBe List(original)
level2.originals(nested = false) shouldBe List(level1)
}
@Test def testErrors: Unit = {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment