FastaUtilsTest.scala 2.19 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1 2 3 4 5
package nl.lumc.sasc.biopet.utils

import java.io.{File, PrintWriter}
import java.nio.file.Paths

6
import htsjdk.samtools.reference.IndexedFastaSequenceFile
Peter van 't Hof's avatar
Peter van 't Hof committed
7 8 9 10 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
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test

class FastaUtilsTest extends TestNGSuite with Matchers {

  private def resourcePath(p: String): String = {
    Paths.get(getClass.getResource(p).toURI).toString
  }

  @Test
  def testRebuildContigMap(): Unit = {
    val inputFile = File.createTempFile("contigMap.", ".tsv")
    inputFile.deleteOnExit()

    val writer = new PrintWriter(inputFile)
    writer.println("chrT\tchrQ")
    writer.close()

    val referenceFile = new File(resourcePath("/fake_chrQ.fa"))

    val map = FastaUtils.rebuildContigMap(inputFile, referenceFile)

    map shouldBe Map("chrQ" -> Set("chrT"))
  }

  @Test
  def testReadContigMapReverse(): Unit = {
    val inputFile = File.createTempFile("contigMap.", ".tsv")
    inputFile.deleteOnExit()

    val writer = new PrintWriter(inputFile)
    writer.println("chrT\tchrQ")
    writer.close()

    val map = FastaUtils.readContigMapReverse(inputFile)

    map shouldBe Map("chrQ" -> "chrT")
  }

  @Test
  def testGetSequenceGc(): Unit = {
    val referenceFile = new File(resourcePath("/fake_chrQ.fa"))

51 52 53 54 55 56 57 58 59 60 61 62 63
    val reader = new IndexedFastaSequenceFile(referenceFile)
    reader.getSubsequenceAt("chrQ", 11, 20).getBaseString shouldBe "GAAACTCCAA"

    FastaUtils.getSequenceGc(referenceFile, "chrQ", 11, 20) shouldBe 0.4
  }

  @Test
  def testGetSequenceGcCompressed(): Unit = {
    val referenceFile = new File(resourcePath("/fake_chrQ.fa.gz"))

    val reader = new IndexedFastaSequenceFile(referenceFile)
    reader.getSubsequenceAt("chrQ", 11, 20).getBaseString shouldBe "GAAACTCCAA"

Peter van 't Hof's avatar
Peter van 't Hof committed
64 65
    FastaUtils.getSequenceGc(referenceFile, "chrQ", 11, 20) shouldBe 0.4
  }
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

  @Test
  def testReadContigMap(): Unit = {
    val inputFile = File.createTempFile("contigMap.", ".tsv")
    inputFile.deleteOnExit()

    val writer = new PrintWriter(inputFile)
    writer.println("chrT\tchrQ")
    writer.println("chrX\t")
    writer.close()

    val map = FastaUtils.readContigMap(inputFile)

    map shouldBe Map("chrT" -> Set("chrQ"), "chrX" -> Set())
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
81
}