Skip to content
Snippets Groups Projects
Commit 7a3e8399 authored by Peter van 't Hof's avatar Peter van 't Hof
Browse files

Added unit testing to count / histogram

parent fb27385e
No related branches found
No related tags found
No related merge requests found
......@@ -7,8 +7,14 @@ import scala.collection.mutable
/**
* Created by pjvanthof on 05/07/16.
*/
class Counts[T](implicit ord: Ordering[T]) {
protected[Counts] val counts: mutable.Map[T, Long] = mutable.Map()
class Counts[T](_counts: Map[T, Long] = Map[T, Long]())(implicit ord: Ordering[T]) {
protected[Counts] val counts: mutable.Map[T, Long] = mutable.Map() ++ _counts
/** Returns histogram as map */
def countsMap = counts.toMap
/** Returns value if it does exist */
def get(key: T) = counts.get(key)
/** This will add an other histogram to `this` */
def +=(other: Counts[T]): Counts[T] = {
......@@ -30,6 +36,6 @@ class Counts[T](implicit ord: Ordering[T]) {
}
}
class Histogram[T](implicit ord: Numeric[T]) extends Counts[T] {
class Histogram[T](_counts: Map[T, Long] = Map[T, Long]())(implicit ord: Numeric[T]) extends Counts[T](_counts) {
}
......@@ -8,13 +8,13 @@ case class Stats() {
var totalReads = 0L
var unmapped = 0L
var secondary = 0L
val mappingQualityHistogram = new Histogram[Int]
val insertSizeHistogram = new Histogram[Int]
val clippingHistogram = new Histogram[Int]
val leftClippingHistogram = new Histogram[Int]
val rightClippingHistogram = new Histogram[Int]
val _5_ClippingHistogram = new Histogram[Int]
val _3_ClippingHistogram = new Histogram[Int]
val mappingQualityHistogram = new Histogram[Int]()
val insertSizeHistogram = new Histogram[Int]()
val clippingHistogram = new Histogram[Int]()
val leftClippingHistogram = new Histogram[Int]()
val rightClippingHistogram = new Histogram[Int]()
val _5_ClippingHistogram = new Histogram[Int]()
val _3_ClippingHistogram = new Histogram[Int]()
/** This will add an other [[Stats]] inside `this` */
def +=(other: Stats): Stats = {
......
package nl.lumc.sasc.biopet.tools.bamstats
import java.io.File
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test
import scala.io.Source
/**
* Created by pjvan_thof on 19-7-16.
*/
class CountsTest extends TestNGSuite with Matchers {
@Test
def testValues: Unit = {
val data: Map[String, Long] = Map("1" -> 1, "2" -> 2, "3" -> 3)
val c1 = new Counts[String](data)
c1.countsMap shouldBe data
c1.get("1") shouldBe Some(1)
c1.get("2") shouldBe Some(2)
c1.get("3") shouldBe Some(3)
c1.get("4") shouldBe None
c1.add("1")
c1.get("1") shouldBe Some(2)
c1.add("4")
c1.get("4") shouldBe Some(1)
val c2 = new Counts[String](data)
c1 += c2
c1.get("1") shouldBe Some(3)
c1.get("2") shouldBe Some(4)
c1.get("3") shouldBe Some(6)
c1.get("4") shouldBe Some(1)
}
@Test
def testEmpty: Unit = {
val c1 = new Counts[Int]()
c1.countsMap.isEmpty shouldBe true
}
@Test
def testTsv: Unit = {
val data: Map[Int, Long] = Map(1 -> 1, 2 -> 2, 3 -> 3)
val c1 = new Counts[Int](data)
val tsvFile = File.createTempFile("counts.", ".tsv")
tsvFile.deleteOnExit()
c1.writeToTsv(tsvFile)
val reader = Source.fromFile(tsvFile)
reader.getLines().toList shouldBe List("value\tcount", "1\t1", "2\t2", "3\t3")
reader.close()
}
}
package nl.lumc.sasc.biopet.tools.bamstats
import java.io.File
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test
import scala.io.Source
/**
* Created by pjvan_thof on 19-7-16.
*/
class HistogramTest extends TestNGSuite with Matchers {
@Test
def testValues: Unit = {
val data: Map[Int, Long] = Map(1 -> 1, 2 -> 2, 3 -> 3)
val c1 = new Histogram[Int](data)
c1.countsMap shouldBe data
c1.get(1) shouldBe Some(1)
c1.get(2) shouldBe Some(2)
c1.get(3) shouldBe Some(3)
c1.get(4) shouldBe None
c1.add(1)
c1.get(1) shouldBe Some(2)
c1.add(4)
c1.get(4) shouldBe Some(1)
val c2 = new Counts[Int](data)
c1 += c2
c1.get(1) shouldBe Some(3)
c1.get(2) shouldBe Some(4)
c1.get(3) shouldBe Some(6)
c1.get(4) shouldBe Some(1)
}
@Test
def testEmpty: Unit = {
val c1 = new Histogram[Int]()
c1.countsMap.isEmpty shouldBe true
}
@Test
def testTsv: Unit = {
val data: Map[Int, Long] = Map(1 -> 1, 2 -> 2, 3 -> 3)
val c1 = new Histogram[Int](data)
val tsvFile = File.createTempFile("counts.", ".tsv")
tsvFile.deleteOnExit()
c1.writeToTsv(tsvFile)
val reader = Source.fromFile(tsvFile)
reader.getLines().toList shouldBe List("value\tcount", "1\t1", "2\t2", "3\t3")
reader.close()
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment