From 7a3e8399e2e43d48333f59c5e3537bb3c9d5cf96 Mon Sep 17 00:00:00 2001
From: Peter van 't Hof <p.j.van_t_hof@lumc.nl>
Date: Tue, 19 Jul 2016 16:47:17 +0200
Subject: [PATCH] Added unit testing to count / histogram

---
 .../biopet/tools/bamstats/Histogram.scala     | 12 +++-
 .../sasc/biopet/tools/bamstats/Stats.scala    | 14 ++---
 .../biopet/tools/bamstats/CountsTest.scala    | 59 +++++++++++++++++++
 .../biopet/tools/bamstats/HistogramTest.scala | 59 +++++++++++++++++++
 4 files changed, 134 insertions(+), 10 deletions(-)
 create mode 100644 biopet-tools/src/test/scala/nl/lumc/sasc/biopet/tools/bamstats/CountsTest.scala
 create mode 100644 biopet-tools/src/test/scala/nl/lumc/sasc/biopet/tools/bamstats/HistogramTest.scala

diff --git a/biopet-tools/src/main/scala/nl/lumc/sasc/biopet/tools/bamstats/Histogram.scala b/biopet-tools/src/main/scala/nl/lumc/sasc/biopet/tools/bamstats/Histogram.scala
index fa68fa455..67f9bd6cf 100644
--- a/biopet-tools/src/main/scala/nl/lumc/sasc/biopet/tools/bamstats/Histogram.scala
+++ b/biopet-tools/src/main/scala/nl/lumc/sasc/biopet/tools/bamstats/Histogram.scala
@@ -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) {
 
 }
diff --git a/biopet-tools/src/main/scala/nl/lumc/sasc/biopet/tools/bamstats/Stats.scala b/biopet-tools/src/main/scala/nl/lumc/sasc/biopet/tools/bamstats/Stats.scala
index 14bb97525..bf446e2dd 100644
--- a/biopet-tools/src/main/scala/nl/lumc/sasc/biopet/tools/bamstats/Stats.scala
+++ b/biopet-tools/src/main/scala/nl/lumc/sasc/biopet/tools/bamstats/Stats.scala
@@ -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 = {
diff --git a/biopet-tools/src/test/scala/nl/lumc/sasc/biopet/tools/bamstats/CountsTest.scala b/biopet-tools/src/test/scala/nl/lumc/sasc/biopet/tools/bamstats/CountsTest.scala
new file mode 100644
index 000000000..286abc245
--- /dev/null
+++ b/biopet-tools/src/test/scala/nl/lumc/sasc/biopet/tools/bamstats/CountsTest.scala
@@ -0,0 +1,59 @@
+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()
+  }
+}
diff --git a/biopet-tools/src/test/scala/nl/lumc/sasc/biopet/tools/bamstats/HistogramTest.scala b/biopet-tools/src/test/scala/nl/lumc/sasc/biopet/tools/bamstats/HistogramTest.scala
new file mode 100644
index 000000000..9944821b5
--- /dev/null
+++ b/biopet-tools/src/test/scala/nl/lumc/sasc/biopet/tools/bamstats/HistogramTest.scala
@@ -0,0 +1,59 @@
+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()
+  }
+}
-- 
GitLab