BedRecordTest.scala 6.59 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1
2
package nl.lumc.sasc.biopet.utils.intervals

Peter van 't Hof's avatar
Peter van 't Hof committed
3
import htsjdk.samtools.util.Interval
Peter van 't Hof's avatar
Peter van 't Hof committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test

/**
 * Created by pjvanthof on 24/08/15.
 */
class BedRecordTest extends TestNGSuite with Matchers {
  @Test def testLineParse: Unit = {
    BedRecord("chrQ", 0, 4) shouldBe BedRecord("chrQ", 0, 4)
    BedRecord.fromLine("chrQ\t0\t4") shouldBe BedRecord("chrQ", 0, 4)
    BedRecord.fromLine("chrQ\t0\t4\tname\t3\t+") shouldBe BedRecord("chrQ", 0, 4, Some("name"), Some(3.0), Some(true))
    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
19
      BedRecord("chrQ", 0, 4, Some("name"), Some(3.0), Some(true), Some(1), Some(3), Some((255, 0, 0)))
Peter van 't Hof's avatar
Peter van 't Hof committed
20
    BedRecord.fromLine("chrQ\t0\t100\tname\t3\t+\t1\t3\t255,0,0\t2\t10,20\t20,50") shouldBe
21
22
      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))
Peter van 't Hof's avatar
Peter van 't Hof committed
23
24
25
26
27
  }

  @Test def testLineOutput: Unit = {
    BedRecord("chrQ", 0, 4).toString shouldBe "chrQ\t0\t4"
    BedRecord.fromLine("chrQ\t0\t4").toString shouldBe "chrQ\t0\t4"
28
    BedRecord.fromLine("chrQ\t0\t100\tname\t3\t+\t1\t3\t255,0,0\t2\t10,20\t20,50").toString shouldBe "chrQ\t0\t100\tname\t3.0\t+\t1\t3\t255,0,0\t2\t10,20\t20,50"
Peter van 't Hof's avatar
Peter van 't Hof committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  }

  @Test def testOverlap: Unit = {
    BedRecord("chrQ", 0, 4).overlapWith(BedRecord("chrQ", 0, 4)) shouldBe true
    BedRecord("chrQ", 0, 4).overlapWith(BedRecord("chrX", 0, 4)) shouldBe false
    BedRecord("chrQ", 0, 4).overlapWith(BedRecord("chrQ", 4, 8)) shouldBe false
    BedRecord("chrQ", 0, 4).overlapWith(BedRecord("chrQ", 3, 8)) shouldBe true
    BedRecord("chrQ", 4, 8).overlapWith(BedRecord("chrQ", 0, 4)) shouldBe false
    BedRecord("chrQ", 3, 4).overlapWith(BedRecord("chrQ", 0, 4)) shouldBe true
    BedRecord("chrQ", 3, 4).overlapWith(BedRecord("chrQ", 4, 5)) shouldBe false
  }

  @Test def testLength: Unit = {
    BedRecord("chrQ", 0, 4).length shouldBe 4
    BedRecord("chrQ", 0, 1).length shouldBe 1
    BedRecord("chrQ", 3, 4).length shouldBe 1
45
46
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
47
48
49
50
51
  @Test def testToSamInterval: Unit = {
    BedRecord("chrQ", 0, 4).toSamInterval shouldBe new Interval("chrQ", 1, 4)
    BedRecord("chrQ", 0, 4, Some("name"), Some(0.0), Some(true)).toSamInterval shouldBe new Interval("chrQ", 1, 4, false, "name")
  }

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  @Test def testExons: Unit = {
    BedRecord.fromLine("chrQ\t0\t100\tname\t3\t+\t1\t3\t255,0,0").exons shouldBe None

    val record = BedRecord.fromLine("chrQ\t0\t100\tname\t3\t+\t1\t3\t255,0,0\t2\t10,20\t0,80")
    val exons = record.exons
    exons should not be None
    exons.get(0).originals()(0) shouldBe record
    exons.get(0).originals().size shouldBe 1
    exons.get(1).originals()(0) shouldBe record
    exons.get(1).originals().size shouldBe 1
    exons.get(0).start shouldBe 0
    exons.get(0).end shouldBe 10
    exons.get(1).start shouldBe 80
    exons.get(1).end shouldBe 100
    exons.get.foldLeft(0)(_ + _.length) shouldBe 30
  }

  @Test def testIntrons: Unit = {
    BedRecord.fromLine("chrQ\t0\t100\tname\t3\t+\t1\t3\t255,0,0").introns shouldBe None

    val record = BedRecord.fromLine("chrQ\t0\t100\tname\t3\t+\t1\t3\t255,0,0\t2\t10,20\t0,80")
    val introns = record.introns
    introns should not be None
    introns.get(0).originals()(0) shouldBe record
    introns.get(0).originals().size shouldBe 1
    introns.get(0).start shouldBe 10
    introns.get(0).end shouldBe 80
    introns.get.foldLeft(0)(_ + _.length) shouldBe 70
  }

82
83
84
85
86
87
88
89
90
  @Test def testExonIntronOverlap: Unit = {
    val record = BedRecord.fromLine("chrQ\t0\t100\tname\t3\t+\t1\t3\t255,0,0\t2\t10,20\t0,80")
    val exons = record.exons
    val introns = record.introns
    for (exon <- exons.get; intron <- introns.get) {
      exon.overlapWith(intron) shouldBe false
    }
  }

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  @Test def testUtrsPositive: Unit = {
    BedRecord.fromLine("chrQ\t0\t100\tname\t3\t+").utr3 shouldBe None
    BedRecord.fromLine("chrQ\t0\t100\tname\t3\t+").utr5 shouldBe None

    val record = BedRecord.fromLine("chrQ\t0\t100\tname\t3\t+\t3\t93\t255,0,0\t2\t10,20\t0,80")
    val utr5 = record.utr5
    val utr3 = record.utr3
    utr5 should not be None
    utr3 should not be None
    utr5.get.length shouldBe 3
    utr3.get.length shouldBe 7

  }

  @Test def testUtrsNegative: Unit = {
    BedRecord.fromLine("chrQ\t0\t100\tname\t3\t-").utr3 shouldBe None
    BedRecord.fromLine("chrQ\t0\t100\tname\t3\t-").utr5 shouldBe None

    val record = BedRecord.fromLine("chrQ\t0\t100\tname\t3\t-\t3\t93\t255,0,0\t2\t10,20\t0,80")
    val utr5 = record.utr5
    val utr3 = record.utr3
    utr5 should not be None
    utr3 should not be None
    utr5.get.length shouldBe 7
    utr3.get.length shouldBe 3
116
  }
117

118
119
120
121
122
123
124
125
126
127
  @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)
Peter van 't Hof's avatar
Peter van 't Hof committed
128
129
  }

130
131
132
133
  @Test def testScatter: Unit = {
    val list = BedRecord("chrQ", 0, 1000).scatter(10)
    list.size shouldBe 100
    BedRecordList.fromList(list).length shouldBe 1000
134
    for (l1 <- list; l2 <- list if l1 != l2) l1.overlapWith(l2) shouldBe false
135
136
137
138

    val list2 = BedRecord("chrQ", 0, 999).scatter(10)
    list2.size shouldBe 99
    BedRecordList.fromList(list2).length shouldBe 999
139
    for (l1 <- list2; l2 <- list2 if l1 != l2) l1.overlapWith(l2) shouldBe false
140
141
142
143

    val list3 = BedRecord("chrQ", 0, 999).scatter(9)
    list3.size shouldBe 111
    BedRecordList.fromList(list3).length shouldBe 999
144
    for (l1 <- list3; l2 <- list3 if l1 != l2) l1.overlapWith(l2) shouldBe false
145
146
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  @Test def testErrors: Unit = {
    BedRecord("chrQ", 0, 3).validate
    BedRecord.fromLine("chrQ\t0\t100\tname\t3\t+\t1\t3\t255,0,0\t2\t10,20\t20,50").validate
    intercept[IllegalArgumentException] {
      BedRecord("chrQ", 0, 0).validate
    }
    intercept[IllegalArgumentException] {
      BedRecord("chrQ", 4, 3).validate
    }
    intercept[IllegalArgumentException] {
      BedRecord.fromLine("chrQ\t0\t100\tname\t3\t+\t1\t3\t255,0,0\t2\t10\t50").validate
    }
    intercept[IllegalStateException] {
      BedRecord.fromLine("chrQ\t0\t100\tname\t3\tx\t1\t3\t255,0,0\t2\t10,20\t20,50").validate
    }
  }
}