tests/testthat/test-get_compound_hetz_ko.R

context('get_compound_hetz_ko')


test_that("can find compound hetz LoF (trans) variants", {

  ref = data.frame(chr = c(1,1,1), rsid = c('rs1','rs2','rs3'), gene = c('A','B','A'), info = c("CSQ|1", "CSQ|2", "CSQ|3"))
  vcf = data.frame(chr = c(1,1,1,1),
                   rsid = c('rs1','rs2','rs3','rs4'),
                   'X111' = c('1|0','0|0','0|0','0|0'),
                   'X112' = c('1|0','0|0','0|0','0|0'),
                   'X113' = c('0|0','0|1','0|1','0|0'),
                   'X114' = c('0|1','0|0','1|0','0|0'))

  result = get_compound_hetz_ko(ref = ref, vcf = vcf,
                       vcf.col.chr = 1, vcf.col.rsid = 2,
                       ref.col.chr = 1, ref.col.rsid = 2, ref.col.gene = 3, ref.col.info = 4,
                       verbose = F)

  expect_equal(result$sample_id, 'X114')
  expect_equal(result$knockout, 'COMPOUND_HETEROZYGOUS')
  expect_equal(result$knockout_how, 'TRANS')
  expect_equal(result$genotype, 'rs1=0|1;rs3=1|0')
  expect_equal(result$info, 'rs1=CSQ|1;rs3=CSQ|3')

})

test_that("can find compound hetz LoF (cis) variants", {

  ref = data.frame(chr = c(2,2,2,2), rsid = c('rs1','rs2','rs3','rs4'), gene = c('A','B','A','A'),info = c("CSQ|1", "CSQ|2", "CSQ|3","CSQ|4"))
  vcf = data.frame(chr = c(2,2,2,2),
                   rsid = c('rs1','rs2','rs3','rs4'),
                   'X111' = c('1|0','0|0','0|0','0|0'),
                   'X112' = c('1|0','0|0','1|0','0|0'),
                   'X113' = c('0|0','0|1','0|1','0|0'),
                   'X114' = c('0|1','1|0','1|0','1|0'))

  result = get_compound_hetz_ko(ref = ref, vcf = vcf,
                                vcf.col.chr = 1, vcf.col.rsid = 2,
                                ref.col.chr = 1, ref.col.rsid = 2, ref.col.gene = 3,  ref.col.info = 4,
                                verbose = F)

  expect_equal(result$sample_id, c('X112','X114'))
  expect_equal(result$knockout, c('COMPOUND_HETEROZYGOUS','COMPOUND_HETEROZYGOUS'))
  expect_equal(result$knockout_how, c('CIS','TRANS'))
  expect_equal(result$genotype, c('rs1=1|0;rs3=1|0', 'rs1=0|1;rs3=1|0;rs4=1|0'))




})

test_that("Marks homozygous + compound hetz as homozygous", {

  ref = data.frame(chr = c(1,1,1), rsid = c('rs1','rs2','rs3'), gene = c('A','B','A'), info = c("CSQ|1", "CSQ|2", "CSQ|3"))
  vcf = data.frame(chr = c(1,1,1,1),
                   rsid = c('rs1','rs2','rs3','rs4'),
                   'X111' = c('1|0','0|0','0|0','0|0'),
                   'X112' = c('1|0','0|0','0|0','0|0'),
                   'X113' = c('0|0','0|1','0|1','0|0'),
                   'X114' = c('0|1','0|0','1|1','0|0'))

  result = get_compound_hetz_ko(ref = ref, vcf = vcf,
                                vcf.col.chr = 1, vcf.col.rsid = 2,
                                ref.col.chr = 1, ref.col.rsid = 2, ref.col.gene = 3, ref.col.info = 4,
                                verbose = F)

  expect_equal(result$sample_id, 'X114')
  expect_equal(result$knockout, 'HOMOZYGOUS')
  expect_equal(result$knockout_how, 'TRANS')
  expect_equal(result$genotype, 'rs1=0|1;rs3=1|1')

})


test_that("error: No recurring genes were found", {

  ref = data.frame(chr = c(1,1,1), rsid = c('rs1','rs2','rs3'), gene = c('A','B','C'), info = c("CSQ|1", "CSQ|2", "CSQ|3"))
  vcf = data.frame(chr = c(1,1,1,1),
                   rsid = c('rs1','rs2','rs3','rs4'),
                   'X111' = c('1|0','0|0','0|0','0|0'),
                   'X112' = c('1|0','0|0','0|0','0|0'),
                   'X113' = c('0|0','0|1','0|1','0|0'),
                   'X114' = c('0|1','0|0','1|1','0|0'))

  expect_error(get_compound_hetz_ko(ref = ref, vcf = vcf,
                                    vcf.col.chr = 1, vcf.col.rsid = 2,
                                    ref.col.chr = 1, ref.col.rsid = 2, ref.col.gene = 3, ref.col.info = 4,
                                    verbose = F))

})


test_that("error: No chr and variant match between vcf and ref file.", {

  ref = data.frame(chr = c(1,1,1), rsid = c('rs1','rs2','rs3'), gene = c('A','B','C'), info = c("CSQ|1", "CSQ|3","CSQ|4"))
  vcf = data.frame(chr = c(1,1,1,1),
                   rsid = c('rs1','rs2','rs3','rs4'),
                   'X111' = c('1|0','0|0','0|0','0|0'),
                   'X112' = c('1|0','0|0','0|0','0|0'),
                   'X113' = c('0|0','0|1','0|1','0|0'),
                   'X114' = c('0|1','0|0','1|1','0|0'))

  expect_error(get_compound_hetz_ko(ref = ref, vcf = vcf,
                                vcf.col.chr = 1, vcf.col.rsid = 2,
                                ref.col.chr = 1, ref.col.rsid = 2, ref.col.gene = 3, ref.col.info = 4,
                                verbose = F))

})
frhl/our documentation built on Feb. 5, 2021, 7:30 p.m.