tests/testthat/testPeakMapping.R

context("Mapping of peaks to closest features")
library(PeakMapper)

test_that("Dataframe has correct column names",  {
  testMapping <- mapPeaks(PeakMapper::testPeaks, PeakMapper::testGenes)
  H3K27me3MappingSmall <- mapPeaks(PeakMapper::H3K27me3PeaksSmall,
                                   PeakMapper::WS263Genes)

  correctColNames <- c("Peak_Chr", "Peak_Start", "Peak_End", "Peak_Name",
                       "Peak_Score", "Peak_Strand", "Feature_Chr",
                       "Feature_Start", "Feature_End", "Feature_Name",
                       "Feature_Score", "Feature_Strand", "Peak_Position",
                       "Peak_Distance", "Percent_Overlap")

  expect_equal(colnames(testMapping), correctColNames)
  expect_equal(colnames(H3K27me3MappingSmall), correctColNames)

})

test_that("No mapping peaks to features on different chromosomes",  {
  mismatchMapping1 <- mapPeaks(PeakMapper::testPeaks, PeakMapper::WS263Genes)
  mismatchMapping2 <- mapPeaks(PeakMapper::H3K27me3PeaksSmall,
                               PeakMapper::testGenes)


  expect_equal(mismatchMapping1, NULL)
  expect_equal(mismatchMapping2, NULL)

})

test_that("Peaks map to closest feature",  {
  testMapping <- mapPeaks(PeakMapper::testPeaks, PeakMapper::testGenes)

  expect_equal(testMapping$Feature_Name[1], "Gene_3")
  expect_equal(testMapping$Feature_Name[2], "Gene_2")
  expect_equal(testMapping$Feature_Name[3], "Gene_1")
  expect_equal(testMapping$Feature_Name[4], "Gene_5")
  expect_equal(testMapping$Feature_Name[5], "Gene_1")

})

test_that("Relative peak orientation is correct",  {
  testMapping <- mapPeaks(PeakMapper::testPeaks, PeakMapper::testGenes)

  expect_equal(testMapping$Peak_Position[1], "Upstream (no overlap)")
  expect_equal(testMapping$Peak_Position[2], "Downstream (no overlap)")
  expect_equal(testMapping$Peak_Position[3], "Downstream (no overlap)")
  expect_equal(testMapping$Peak_Position[4], "Overlap (upstream and downstream)")
  expect_equal(testMapping$Peak_Position[5], "Overlap (upstream)")

})

test_that("Relative peak distance is correct",  {
  testMapping <- mapPeaks(PeakMapper::testPeaks, PeakMapper::testGenes)

  expect_equal(testMapping$Peak_Distance[1], 25)
  expect_equal(testMapping$Peak_Distance[2], 50)
  expect_equal(testMapping$Peak_Distance[3], 600)
  expect_equal(testMapping$Peak_Distance[4], 0)
  expect_equal(testMapping$Peak_Distance[5], 0)

})

test_that("Relative peak overlap is correct",  {
  testMapping <- mapPeaks(PeakMapper::testPeaks, PeakMapper::testGenes)

  expect_equal(testMapping$Percent_Overlap[1], 0)
  expect_equal(testMapping$Percent_Overlap[2], 0)
  expect_equal(testMapping$Percent_Overlap[3], 0)
  expect_equal(testMapping$Percent_Overlap[4], 100)
  expect_equal(floor(testMapping$Percent_Overlap[5]), 75)

})

test_that("Peaks can map to multiple features",  {
  H3K27me3MappingSmall <- mapPeaks(PeakMapper::H3K27me3PeaksSmall,
                                   PeakMapper::WS263Genes)

  peak2Features <- H3K27me3MappingSmall[which(
    H3K27me3MappingSmall$Peak_Name=="42-ARa4-2_peak_2"), ]

  peak5Features <- H3K27me3MappingSmall[which(
    H3K27me3MappingSmall$Peak_Name=="42-ARa4-2_peak_5"), ]

  peak8Features <- H3K27me3MappingSmall[which(
    H3K27me3MappingSmall$Peak_Name=="42-ARa4-2_peak_8"), ]

  expect_equal(dim(peak2Features)[1], 1)
  expect_equal(dim(peak5Features)[1], 2)
  expect_equal(dim(peak8Features)[1], 4)


})
fuscada2/PeakMapper documentation built on Dec. 8, 2019, 12:35 p.m.