BamUtilsTest.scala 4.98 KB
Newer Older
Wai Yi Leung's avatar
Wai Yi Leung committed
1
2
3
4
5
package nl.lumc.sasc.biopet.utils

import java.io.File

import htsjdk.samtools._
6
import org.mockito.Mockito.{ inOrder => inOrd }
Wai Yi Leung's avatar
Wai Yi Leung committed
7
8
9
import org.scalatest.Matchers
import org.scalatest.mock.MockitoSugar
import org.scalatest.testng.TestNGSuite
10
import org.testng.annotations.{ BeforeClass, Test }
Wai Yi Leung's avatar
Wai Yi Leung committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

/**
 * Created by wyleung on 22-2-16.
 * Create samfile and records are borrowed from WipeReadsTest by @bow
 */
class BamUtilsTest extends TestNGSuite with MockitoSugar with Matchers {

  import BamUtils._

  private val samHeaderTemplate: SAMLineParser = {
    val samh = new SAMFileHeader
    samh.setSortOrder(SAMFileHeader.SortOrder.coordinate)
    samh.addSequence(new SAMSequenceRecord("chrQ", 10000))
    samh.addSequence(new SAMSequenceRecord("chrR", 10000))
    val readGroup1 = new SAMReadGroupRecord("001")
    readGroup1.setSample("sample01")
    samh.addReadGroup(readGroup1)
    val readGroup2 = new SAMReadGroupRecord("002")
    readGroup2.setSample("sample01")
    samh.addReadGroup(readGroup2)
    new SAMLineParser(samh)
  }

  private def makeSams(raws: String*): Seq[SAMRecord] =
    raws.map(s => samHeaderTemplate.parseLine(s))

  private def makeSamReader(f: File): SamReader = SamReaderFactory
    .make()
    .validationStringency(ValidationStringency.LENIENT)
    .open(f)

  val sBamRecs1 = makeSams(
    "r02\t0\tchrQ\t50\t60\t10M\t*\t0\t0\tTACGTACGTA\tEEFFGGHHII\tRG:Z:001",
    "r01\t16\tchrQ\t190\t60\t10M\t*\t0\t0\tTACGTACGTA\tEEFFGGHHII\tRG:Z:001",
    "r01\t16\tchrQ\t290\t60\t10M\t*\t0\t0\tGGGGGAAAAA\tGGGGGGGGGG\tRG:Z:001",
    "r04\t0\tchrQ\t450\t60\t10M\t*\t0\t0\tCGTACGTACG\tEEFFGGHHII\tRG:Z:001",
    "r03\t16\tchrQ\t690\t60\t10M\t*\t0\t0\tCCCCCTTTTT\tHHHHHHHHHH\tRG:Z:001",
    "r05\t0\tchrQ\t890\t60\t5M200N5M\t*\t0\t0\tGATACGATAC\tFEFEFEFEFE\tRG:Z:001",
    "r06\t4\t*\t0\t0\t*\t*\t0\t0\tATATATATAT\tHIHIHIHIHI\tRG:Z:001"
  )

  val pBamRecs1 = makeSams(
    "r02\t99\tchrQ\t50\t60\t10M\t=\t90\t50\tTACGTACGTA\tEEFFGGHHII\tRG:Z:001",
    "r02\t147\tchrQ\t90\t60\t10M\t=\t50\t-50\tATGCATGCAT\tEEFFGGHHII\tRG:Z:001",
    "r01\t163\tchrQ\t150\t60\t10M\t=\t190\t50\tAAAAAGGGGG\tGGGGGGGGGG\tRG:Z:001",
    "r01\t83\tchrQ\t190\t60\t10M\t=\t150\t-50\tGGGGGAAAAA\tGGGGGGGGGG\tRG:Z:001",
    "r01\t163\tchrQ\t250\t60\t10M\t=\t290\t50\tAAAAAGGGGG\tGGGGGGGGGG\tRG:Z:001",
    "r01\t83\tchrQ\t290\t60\t10M\t=\t250\t-50\tGGGGGAAAAA\tGGGGGGGGGG\tRG:Z:001",
    "r04\t99\tchrQ\t450\t60\t10M\t=\t490\t50\tCGTACGTACG\tEEFFGGHHII\tRG:Z:001",
    "r04\t147\tchrQ\t490\t60\t10M\t=\t450\t-50\tGCATGCATGC\tEEFFGGHHII\tRG:Z:001",
    "r03\t163\tchrQ\t650\t60\t10M\t=\t690\t50\tTTTTTCCCCC\tHHHHHHHHHH\tRG:Z:001",
    "r03\t83\tchrQ\t690\t60\t10M\t=\t650\t-50\tCCCCCTTTTT\tHHHHHHHHHH\tRG:Z:001",
    "r05\t99\tchrQ\t890\t60\t5M200N5M\t=\t1140\t50\tTACGTACGTA\tEEFFGGHHII\tRG:Z:001",
    "r05\t147\tchrQ\t1140\t60\t10M\t=\t890\t-50\tATGCATGCAT\tEEFFGGHHII\tRG:Z:001",
    "r06\t4\t*\t0\t0\t*\t*\t0\t0\tATATATATAT\tHIHIHIHIHI\tRG:Z:001",
    "r06\t4\t*\t0\t0\t*\t*\t0\t0\tGCGCGCGCGC\tHIHIHIHIHI\tRG:Z:001"
  )

  @BeforeClass
  def start: Unit = {
    createTestFileFrom(sBamRecs1, BamUtilsTest.singleEndBam01)
    createTestFileFrom(pBamRecs1, BamUtilsTest.pairedEndBam01)
  }

  private def createTestFileFrom(records: Seq[SAMRecord], output: File): File = {
    output.getParentFile.createNewFile()
    val outBam = new SAMFileWriterFactory()
      .setCreateIndex(true)
      .setUseAsyncIo(true)
      .makeBAMWriter(samHeaderTemplate.getFileHeader, true, output)
    writeBam(records, outBam)
    output
  }

  private def writeBam(records: Seq[SAMRecord], outBam: SAMFileWriter): Unit = {
    try {
      for (rec <- records) {
        outBam.addAlignment(rec)
      }
    } finally {
      outBam.close()
    }
  }

  @Test def testInputSingleEndOK() = {
    sBamRecs1.size shouldBe 7
  }

  @Test def testInputPairedEndOK() = {
    pBamRecs1.size shouldBe 14
  }

  @Test def testPairedRecords = {
    sampleBamInsertSize(BamUtilsTest.pairedEndBam01) shouldBe 50
  }

  @Test def testContigInsertsize = {
    contigInsertSize(BamUtilsTest.pairedEndBam01, "chrQ", 1, 10000) shouldBe Some(50)
    contigInsertSize(BamUtilsTest.pairedEndBam01, "chrR", 1, 10000) shouldBe None
  }

  @Test def testContigInsertsizeContigNotFound = {
    intercept(contigInsertSize(BamUtilsTest.pairedEndBam01, "chrW", 1, 10000))

  }

  @Test def testSingleRecords = {
    sampleBamInsertSize(BamUtilsTest.singleEndBam01) shouldBe 0
  }

  @Test def testMultiBamInsertsizes = {
    val result = sampleBamsInsertSize(List(BamUtilsTest.singleEndBam01, BamUtilsTest.pairedEndBam01))
    result shouldEqual Map(
      BamUtilsTest.singleEndBam01 -> 0,
      BamUtilsTest.pairedEndBam01 -> 50
    )
  }

  @Test def testSampleBamNames = {
    sampleBamMap(List(BamUtilsTest.singleEndBam01)) shouldEqual Map(
      "sample01" -> BamUtilsTest.singleEndBam01
    )
  }

}

object BamUtilsTest {
  var singleEndBam01: File = File.createTempFile("bamutils", "single01.bam")
  singleEndBam01.deleteOnExit()
  var pairedEndBam01: File = File.createTempFile("bamutils", "paired01.bam")
  singleEndBam01.deleteOnExit()
}