From 37f5bd11a397c5654e01cbebebd3fbc3d2e7326f Mon Sep 17 00:00:00 2001 From: Jeremy McRae Date: Mon, 29 Jun 2015 14:06:11 +0100 Subject: [PATCH] added unit tests for check ped parsing --- .../python/clinicalfilter/test_load_ped.py | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/test/python/clinicalfilter/test_load_ped.py diff --git a/src/test/python/clinicalfilter/test_load_ped.py b/src/test/python/clinicalfilter/test_load_ped.py new file mode 100644 index 0000000..726021b --- /dev/null +++ b/src/test/python/clinicalfilter/test_load_ped.py @@ -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])) +