Commit 5def7c50 authored by Wai Yi Leung's avatar Wai Yi Leung
Browse files

Adding tests for BamUtils

parent 31982c15
......@@ -48,7 +48,7 @@ object BamUtils {
val insertsizes: List[Int] = (for {
read <- samIterator.toStream.takeWhile(rec => {
val paired = rec.getReadPairedFlag && rec.getProperPairFlag
val bothMapped = (rec.getReadUnmappedFlag == false) && (rec.getMateUnmappedFlag == false)
val bothMapped = if (paired) ((rec.getReadUnmappedFlag == false) && (rec.getMateUnmappedFlag == false)) else false
paired && bothMapped
}).take(samplingSize)
} yield {
......@@ -74,9 +74,12 @@ object BamUtils {
contig => BamUtils.contigInsertSize(bamFile, contig.getSequenceName, 1, contig.getSequenceLength, samplingSize)
}).toList
val counts = baminsertsizes.flatMap(x => x)
val sum = counts.reduceLeft(_ + _)
val n = counts.size
sum / n
if (counts.size != 0) {
counts.reduceLeft(_ + _) / counts.size
} else {
0
}
}
/**
......
package nl.lumc.sasc.biopet.utils
import java.io.File
import htsjdk.samtools._
import org.mockito.Mockito.{inOrder => inOrd}
import org.scalatest.Matchers
import org.scalatest.mock.MockitoSugar
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.{BeforeClass, Test}
/**
* 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()
}
\ No newline at end of file
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