Skip to content

Commit

Permalink
added unit tests for check ped parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy McRae committed Jun 29, 2015
1 parent b5a320b commit 37f5bd1
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 additions & 0 deletions src/test/python/clinicalfilter/test_load_ped.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
""" unit testing of loading ped files
"""

import unittest
import tempfile

from clinicalfilter.ped import load_ped_file, load_families, Person, Family

class TestLoadPed(unittest.TestCase):
"""
"""

def setUp(self):
""" define and write a temporary ped file
"""

self.tempfile = tempfile.NamedTemporaryFile(mode="w")
self.path = self.tempfile.name

self.tempfile.write("fam_ID proband dad mom F 2 /path/to/proband_vcf.gz\n")
self.tempfile.write("fam_ID dad 0 0 M 1 /path/to/dad_vcf.gz\n")
self.tempfile.write("fam_ID mom 0 0 F 1 /path/to/mom_vcf.gz\n")
self.tempfile.flush()

def test_load_ped_file_single_family(self):
""" check that we correctly parse a ped file with a single trio
"""

# load all the components from the file
mothers, fathers, children, affected, sex, vcfs = load_ped_file(self.path)

# check that they all match
self.assertEqual(mothers, {'mom': '0', 'proband': 'mom', 'dad': '0'})
self.assertEqual(fathers, {'mom': '0', 'proband': 'dad', 'dad': '0'})
self.assertEqual(children, {'proband': 'fam_ID'})
self.assertEqual(affected, {'mom': '1', 'proband': '2', 'dad': '1'})
self.assertEqual(sex, {'mom': 'F', 'proband': 'F', 'dad': 'M'})
self.assertEqual(vcfs, {'mom': '/path/to/mom_vcf.gz', \
'proband': '/path/to/proband_vcf.gz', \
'dad': '/path/to/dad_vcf.gz'})

def test_load_ped_file_multiple_sibs(self):
""" check that we correctly parse a ped file with multiple siblings
"""

# add an extra sibling
self.tempfile.write("fam_ID sib dad mom F 2 /path/to/sib_vcf.gz\n")
self.tempfile.flush()

# load all the components from the file
mothers, fathers, children, affected, sex, vcfs = load_ped_file(self.path)

# check that they all match
self.assertEqual(mothers, {'mom': '0', 'proband': 'mom', 'dad': '0', 'sib': 'mom'})
self.assertEqual(fathers, {'mom': '0', 'proband': 'dad', 'dad': '0', 'sib': 'dad'})
self.assertEqual(children, {'proband': 'fam_ID', "sib": "fam_ID"})
self.assertEqual(affected, {'mom': '1', 'proband': '2', 'dad': '1', 'sib': '2'})
self.assertEqual(sex, {'mom': 'F', 'proband': 'F', 'dad': 'M', 'sib': 'F'})
self.assertEqual(vcfs, {'mom': '/path/to/mom_vcf.gz',
'proband': '/path/to/proband_vcf.gz',
'dad': '/path/to/dad_vcf.gz',
'sib': '/path/to/sib_vcf.gz'})

def test_load_ped_file_multiple_families(self):
""" check that we correctly parse a ped file with multiple families
"""

# add an extra family, with multiple sibs
self.tempfile.write("fam_ID2 proband2 dad2 mom2 F 2 /path/to/proband2_vcf.gz\n")
self.tempfile.write("fam_ID2 dad2 0 0 M 1 /path/to/dad2_vcf.gz\n")
self.tempfile.write("fam_ID2 mom2 0 0 F 1 /path/to/mom2_vcf.gz\n")
self.tempfile.write("fam_ID2 sib dad2 mom2 F 2 /path/to/sib_vcf.gz\n")
self.tempfile.flush()

# load all the components from the file
mothers, fathers, children, affected, sex, vcfs = load_ped_file(self.path)

# check that they all match
self.assertEqual(mothers, {'mom': '0', 'proband': 'mom', 'dad': '0',
'mom2': '0', 'proband2': 'mom2', 'dad2': '0', 'sib': 'mom2'})
self.assertEqual(fathers, {'mom': '0', 'proband': 'dad', 'dad': '0',
'mom2': '0', 'proband2': 'dad2', 'dad2': '0', 'sib': 'dad2'})
self.assertEqual(children, {'proband': 'fam_ID',
"proband2": "fam_ID2", "sib": "fam_ID2"})
self.assertEqual(affected, {'mom': '1', 'proband': '2', 'dad': '1',
'mom2': '1', 'proband2': '2', 'dad2': '1', 'sib': '2'})
self.assertEqual(sex, {'mom': 'F', 'proband': 'F', 'dad': 'M',
'mom2': 'F', 'proband2': 'F', 'dad2': 'M', 'sib': 'F'})
self.assertEqual(vcfs, {'mom': '/path/to/mom_vcf.gz',
'proband': '/path/to/proband_vcf.gz',
'dad': '/path/to/dad_vcf.gz',
'proband2': '/path/to/proband2_vcf.gz',
'dad2': '/path/to/dad2_vcf.gz',
'mom2': '/path/to/mom2_vcf.gz',
'sib': '/path/to/sib_vcf.gz'})

def test_load_families(self):
""" check that load_families works correctly
"""

# construct a temporary family that will have the same sample IDs etc
# as for the one loaded from the ped file.
family = Family("fam_ID")
family.add_child("proband", "/path/to/proband_vcf.gz", "2", "F")
family.add_mother("mom", "/path/to/mom_vcf.gz", "1", "F")
family.add_father("dad", "/path/to/dad_vcf.gz", "1", "M")

# load the ped file, and check that the load_families function returns
# the expected Family object
families = load_families(self.path)
self.assertEqual(families, {"fam_ID": family})

# add an extra family, with multiple sibs
self.tempfile.write("fam_ID2 proband2 dad2 mom2 F 2 /path/to/proband2_vcf.gz\n")
self.tempfile.write("fam_ID2 dad2 0 0 M 1 /path/to/dad2_vcf.gz\n")
self.tempfile.write("fam_ID2 mom2 0 0 F 1 /path/to/mom2_vcf.gz\n")
self.tempfile.write("fam_ID2 sib dad2 mom2 F 2 /path/to/sib_vcf.gz\n")
self.tempfile.flush()

# construct a temporary family that will have the same sample IDs etc
# as for the one loaded from the ped file.
fam2 = Family("fam_ID2")
fam2.add_child("proband2", "/path/to/proband2_vcf.gz", "2", "F")
fam2.add_child("sib", "/path/to/sib_vcf.gz", "2", "F")
fam2.add_mother("mom2", "/path/to/mom2_vcf.gz", "1", "F")
fam2.add_father("dad2", "/path/to/dad2_vcf.gz", "1", "M")

# load the ped file, and check that the load_families function returns
# the expected Families objects
families = load_families(self.path)
self.assertEqual(set(families.values()), set([family, fam2]))

0 comments on commit 37f5bd1

Please sign in to comment.