BedRecordListTest.scala 5.57 KB
Newer Older
1
2
package nl.lumc.sasc.biopet.utils.intervals

3
import java.io.{ PrintWriter, File }
4

5
import htsjdk.samtools.util.Interval
6
7
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
8
import org.testng.annotations.{ Test, AfterClass, BeforeClass }
9
10
11
12
13
14
15
16
17
18
19
20
21
22

import scala.io.Source

/**
 * Created by pjvan_thof on 8/25/15.
 */
class BedRecordListTest extends TestNGSuite with Matchers {
  @BeforeClass
  def start: Unit = {
    {
      val writer = new PrintWriter(BedRecordListTest.bedFile)
      writer.print(BedRecordListTest.bedContent)
      writer.close()
    }
23
24
25
26
27
    {
      val writer = new PrintWriter(BedRecordListTest.corruptBedFile)
      writer.print(BedRecordListTest.corruptBedContent)
      writer.close()
    }
28
29
30
31
32
33
34
35
    {
      val writer = new PrintWriter(BedRecordListTest.bedFileUcscHeader)
      writer.print(BedRecordListTest.ucscHeader)
      writer.print(BedRecordListTest.bedContent)
      writer.close()
    }
  }

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
82
83
84
85
86
87
88
89
90
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
116
117
118
119
120
121
122
123
124
  @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)
    }
125
126
  }

127
128
129
130
131
132
  @Test def testScatter: Unit = {
    val list = BedRecordList.fromList(List(BedRecord("chrQ", 0, 1000), BedRecord("chrQ", 3000, 3500)))
    list.scatter(100).allRecords.size shouldBe 15
    list.scatter(100).length shouldBe 1500
  }

133
134
135
  @AfterClass
  def end: Unit = {
    BedRecordListTest.bedFile.delete()
136
    BedRecordListTest.corruptBedFile.delete()
137
138
139
140
141
142
143
144
145
    BedRecordListTest.bedFileUcscHeader.delete()
  }
}

object BedRecordListTest {
  val ucscHeader = """browser position chr7:127471196-127495720
                     |browser hide all
                     |track name="ItemRGBDemo" description="Item RGB demonstration" visibility=2 itemRgb="On"
                     |""".stripMargin
146
147
148
149
150
  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

151
  val bedFile = File.createTempFile("regions", ".bed")
152
  val corruptBedFile = File.createTempFile("regions", ".bed")
153
154
  val bedFileUcscHeader = File.createTempFile("regions", ".bed")
}