BamUtilsTest.scala 9.17 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._
Peter van 't Hof's avatar
Peter van 't Hof committed
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
Peter van 't Hof's avatar
Peter van 't Hof committed
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

/**
 * 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)
  }

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  private val samHeaderTemplateDoubleSample: 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("sampleX")
    samh.addReadGroup(readGroup1)
    val readGroup2 = new SAMReadGroupRecord("002")
    readGroup2.setSample("sampleY")
    samh.addReadGroup(readGroup2)
    new SAMLineParser(samh)
  }

  private val samHeaderTemplateErrornousNoReadgroup: SAMLineParser = {
    val samh = new SAMFileHeader
    samh.setSortOrder(SAMFileHeader.SortOrder.coordinate)
    samh.addSequence(new SAMSequenceRecord("chrQ", 10000))
    samh.addSequence(new SAMSequenceRecord("chrR", 10000))
    new SAMLineParser(samh)
  }

  private def makeSams(header: SAMLineParser, raws: Seq[String]): Seq[SAMRecord] =
    raws.map(s => header.parseLine(s))
Wai Yi Leung's avatar
Wai Yi Leung committed
58
59
60
61
62
63

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

64
  val sBamRecs1 = Seq(
Wai Yi Leung's avatar
Wai Yi Leung committed
65
66
67
68
69
70
71
72
73
    "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"
  )

74
75
76
77
78
79
  val sBamRecs2 = Seq(
    "r02\t0\tchrQ\t50\t60\t10M\t*\t0\t0\tTACGTACGTA\tEEFFGGHHII\t",
    "r01\t16\tchrQ\t190\t60\t10M\t*\t0\t0\tTACGTACGTA\tEEFFGGHHII\t"
  )

  val pBamRecs1 = Seq(
Wai Yi Leung's avatar
Wai Yi Leung committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    "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"
  )

96
97
98
99
100
101
102
  val pBamRecs2 = Seq(
    "r01\t163\tchrQ\t150\t60\t10M\t=\t190\t50\tAAAAAGGGGG\tGGGGGGGGGG\t",
    "r01\t83\tchrQ\t190\t60\t10M\t=\t150\t-50\tGGGGGAAAAA\tGGGGGGGGGG\t",
    "r01\t163\tchrQ\t250\t60\t10M\t=\t290\t50\tAAAAAGGGGG\tGGGGGGGGGG\t",
    "r01\t83\tchrQ\t290\t60\t10M\t=\t250\t-50\tGGGGGAAAAA\tGGGGGGGGGG\t"
  )

Wai Yi Leung's avatar
Wai Yi Leung committed
103
104
  @BeforeClass
  def start: Unit = {
105
106
107
108
109
110
111
112
113
    createTestFileFrom(makeSams(samHeaderTemplate, sBamRecs1), samHeaderTemplate, BamUtilsTest.singleEndBam01)
    createTestFileFrom(makeSams(samHeaderTemplate, pBamRecs1), samHeaderTemplate, BamUtilsTest.pairedEndBam01)
    createTestFileFrom(makeSams(samHeaderTemplate, pBamRecs1), samHeaderTemplate, BamUtilsTest.pairedEndBam02)

    createTestFileFrom(makeSams(samHeaderTemplateDoubleSample, sBamRecs1), samHeaderTemplateDoubleSample, BamUtilsTest.singleEndBam01WithDoubleSamples)
    createTestFileFrom(makeSams(samHeaderTemplateDoubleSample, pBamRecs1), samHeaderTemplateDoubleSample, BamUtilsTest.pairedEndBam01WithDoubleSamples)

    createTestFileFrom(makeSams(samHeaderTemplateErrornousNoReadgroup, sBamRecs2), samHeaderTemplateErrornousNoReadgroup, BamUtilsTest.singleEndBam01NoRG)
    createTestFileFrom(makeSams(samHeaderTemplateErrornousNoReadgroup, pBamRecs2), samHeaderTemplateErrornousNoReadgroup, BamUtilsTest.pairedEndBam01NoRG)
Wai Yi Leung's avatar
Wai Yi Leung committed
114
115
  }

116
  private def createTestFileFrom(records: Seq[SAMRecord], header: SAMLineParser, output: File): File = {
Wai Yi Leung's avatar
Wai Yi Leung committed
117
118
119
120
    output.getParentFile.createNewFile()
    val outBam = new SAMFileWriterFactory()
      .setCreateIndex(true)
      .setUseAsyncIo(true)
121
      .makeBAMWriter(header.getFileHeader, true, output)
Wai Yi Leung's avatar
Wai Yi Leung committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    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
    )
  }

176
177
178
179
  @Test def testSampleBamNamesMultipleSamplesSE = {
    val thrown = intercept[IllegalArgumentException] {
      sampleBamMap(List(BamUtilsTest.singleEndBam01WithDoubleSamples))
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
180
    thrown.getMessage should ===("Bam contains multiple sample IDs: " + BamUtilsTest.singleEndBam01WithDoubleSamples)
181
182
183
184
185
186
  }

  @Test def testSampleBamNamesMultipleSamplesPE = {
    val thrown = intercept[IllegalArgumentException] {
      sampleBamMap(List(BamUtilsTest.pairedEndBam01WithDoubleSamples))
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
187
    thrown.getMessage should ===("Bam contains multiple sample IDs: " + BamUtilsTest.pairedEndBam01WithDoubleSamples)
188
189
190
191
192
193
  }

  @Test def testSampleBamNamesNoRGSE = {
    val thrown = intercept[IllegalArgumentException] {
      sampleBamMap(List(BamUtilsTest.singleEndBam01NoRG))
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
194
    thrown.getMessage should ===("Bam does not contain sample ID or have no readgroups defined: " + BamUtilsTest.singleEndBam01NoRG)
195
196
197
198
199
200
  }

  @Test def testSampleBamNamesNoRGPE = {
    val thrown = intercept[IllegalArgumentException] {
      sampleBamMap(List(BamUtilsTest.pairedEndBam01NoRG))
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
201
    thrown.getMessage should ===("Bam does not contain sample ID or have no readgroups defined: " + BamUtilsTest.pairedEndBam01NoRG)
202
203
204
205
206
207
208
209
  }

  @Test def testSampleFoundTwice = {
    val thrown = intercept[IllegalArgumentException] {
      sampleBamMap(List(BamUtilsTest.pairedEndBam01, BamUtilsTest.pairedEndBam02))
    }
    thrown.getMessage should ===("Samples has been found twice")
  }
Wai Yi Leung's avatar
Wai Yi Leung committed
210
211
212
213
214
215
216
}

object BamUtilsTest {
  var singleEndBam01: File = File.createTempFile("bamutils", "single01.bam")
  singleEndBam01.deleteOnExit()
  var pairedEndBam01: File = File.createTempFile("bamutils", "paired01.bam")
  singleEndBam01.deleteOnExit()
217
218
219
220
  var pairedEndBam02: File = File.createTempFile("bamutils", "paired02.bam")
  singleEndBam01.deleteOnExit()

  var singleEndBam01WithDoubleSamples: File = File.createTempFile("bamutils", "single01ds.bam")
221
  singleEndBam01WithDoubleSamples.deleteOnExit()
222
223
224
225
226
227
228
  var pairedEndBam01WithDoubleSamples: File = File.createTempFile("bamutils", "paired01ds.bam")
  pairedEndBam01WithDoubleSamples.deleteOnExit()

  var singleEndBam01NoRG: File = File.createTempFile("bamutils", "single01norg.bam")
  singleEndBam01NoRG.deleteOnExit()
  var pairedEndBam01NoRG: File = File.createTempFile("bamutils", "paired01norg.bam")
  singleEndBam01NoRG.deleteOnExit()
Peter van 't Hof's avatar
Peter van 't Hof committed
229
}