def get_trios(family): """ get complete trios (child and parents) in a family Args: family: Family object (a graph with Persons as nodes) Returns: list of peds.Family objects, each for a unique trio """ trios = [] for x in family: mom = family.get_mother(x) dad = family.get_father(x) if mom is None or dad is None: continue # ignore people whose parents are based on their own ped line if mom._is_inferred() or dad._is_inferred(): continue trio = Family(x.family) trio.add_person(x) trio.add_person(mom) trio.add_person(dad) trio.set_mom(mom, x) trio.set_dad(dad, x) trios.append(trio) return trios
def test_link_members(self): """ test that link_members works correctly """ child1 = Person('A', 'B', 'C', 'D', '1', '1') child2 = Person('A', 'E', 'C', 'D', '1', '1') fam = Family('A') fam.add_person(child1) fam.add_person(child2) fam = link_members(fam) dad = Person('A', 'C', 'NA', 'NA', 'NA', 'NA') mom = Person('A', 'D', 'NA', 'NA', 'NA', 'NA') expected = Family('A') expected.add_person(child1) expected.add_person(child2) expected.add_person(mom) expected.add_person(dad) expected.set_mom(mom, child1) expected.set_mom(mom, child2) expected.set_dad(dad, child1) expected.set_dad(dad, child2) self.assertEqual(expected.nodes, fam.nodes) self.assertEqual(expected.edges, fam.edges)
def test_open_ped_partial_parents(self): """ test that open_ped identifies sibs, even when parents aren't present """ # define a ped file where the parents are referred to only within the # child lines. We have to spot siblings from these. self.temp.write('A B C D 1 1\n') self.temp.write('A E C D 1 1\n') self.temp.flush() families = open_ped(self.temp.name) child1 = Person('A', 'B', 'C', 'D', '1', '1') child2 = Person('A', 'E', 'C', 'D', '1', '1') dad = Person('A', 'C', 'NA', 'NA', 'NA', 'NA') mom = Person('A', 'D', 'NA', 'NA', 'NA', 'NA') fam = Family('A') fam.add_person(child1) fam.add_person(child2) fam.add_person(mom) fam.add_person(dad) fam.set_mom(mom, child1) fam.set_mom(mom, child2) fam.set_dad(dad, child1) fam.set_dad(dad, child2) self.assertEqual(families[0].nodes, fam.nodes) self.assertEqual(families[0].edges, fam.edges)
def test_set_dad_male(self): """ check we raise an error if we try to add a female father """ fam = Family('A') child = Person('A', 'B', 'C', '0', '1', '1') dad = Person('A', 'C', '0', '0', '2', '1') fam.add_person(child) fam.add_person(dad) with self.assertRaises(ValueError): fam.set_dad(dad, child)
def test_set_dad_mismatching_id(self): """ if we set a father, the fathers ID must be expected in child """ fam = Family('A') child = Person('A', 'B', 'D', '0', '1', '1') dad = Person('A', 'C', '0', '0', '1', '1') fam.add_person(child) fam.add_person(dad) with self.assertRaises(ValueError): fam.set_dad(dad, child)
def test_get_probands(self): """ check get_probands works """ fam = Family('A') child = Person('A', 'B', 'C', 'D', '1', '2') dad = Person('A', 'C', '0', '0', '1', '1') fam.add_person(child) fam.add_person(dad) fam.set_dad(dad, child) self.assertEqual(get_probands(fam), [child])
def test_get_probands_missing(self): """ check get_probands works for family wihtout affected proband """ fam = Family('A') child = Person('A', 'B', 'C', 'D', '1', '1') dad = Person('A', 'C', '0', '0', '1', '1') fam.add_person(child) fam.add_person(dad) fam.set_dad(dad, child) self.assertEqual(get_probands(fam), [])
def test_set_father_placeholder(self): """ check the father when set with a placeholder father """ fam = Family('A') child = Person('A', 'B', 'C', '0', '1', '1') father = Person('A', 'C', '0', '0', '1', '1') placeholder = Person('A', 'C', 'NA', 'NA', 'NA', 'NA') fam.add_person(child) fam.add_person(father) fam.set_dad(placeholder, child) # and check we can still pick up the parent. self.assertIsNotNone(fam.get_father(child))
def test_get_trios(self): """ check get_trios works """ fam = Family('A') child = Person('A', 'B', 'C', 'D', '1', '2') dad = Person('A', 'C', '0', '0', '1', '1') mom = Person('A', 'D', '0', '0', '2', '1') fam.add_person(child) fam.add_person(dad) fam.add_person(mom) fam.set_dad(dad, child) fam.set_mom(mom, child) self.assertEqual(set(get_trios(fam)[0]), set([child, dad, mom]))
def test_get_father(self): """ test getting a father """ fam = Family('A') child = Person('A', 'B', 'C', '0', '1', '1') fam.add_person(child) # check when no parent available self.assertIsNone(fam.get_father(child)) father = Person('A', 'C', '0', '0', '1', '1') fam.add_person(father) fam.set_dad(father, child) self.assertEqual(fam.get_father(child), father) # despite a father being present, there is still not mother self.assertIsNone(fam.get_mother(child))
def test_set_dad_missing_members(self): """ test that if we link parent to child, both must be present """ child = Person('A', 'B', 'C', '0', '1', '1') dad = Person('A', 'C', '0', '0', '1', '1') fam = Family('A') fam.add_person(child) # the father must be in the family to set the parent with self.assertRaises(ValueError): fam.set_dad(dad, child) # the child must be in the family to set the parent fam = Family('A') fam.add_person(dad) with self.assertRaises(ValueError): fam.set_dad(dad, child)
def test_open_ped_with_parent(self): """ check open_ped correctly identifies parental relationships """ self.temp.write('A B C 0 1 1\n') self.temp.write('A C 0 0 1 1\n') self.temp.flush() families = open_ped(self.temp.name) fam = Family('A') child = Person('A', 'B', 'C', '0', '1', '1') dad = Person('A', 'C', '0', '0', '1', '1') fam.add_person(child) fam.add_person(dad) fam.set_dad(dad, child) self.assertEqual(families[0].nodes, fam.nodes) self.assertEqual(families[0].edges, fam.edges)
def test_get_parents(self): """ test that get_parents works correctly """ fam = Family('A') child = Person('A', 'B', 'C', 'D', '1', '1') fam.add_person(child) # check when no parent available self.assertEqual(list(fam.get_parents(child)), []) mother = Person('A', 'D', '0', '0', '2', '1') father = Person('A', 'C', '0', '0', '1', '1') fam.add_person(mother) fam.add_person(father) fam.set_mom(mother, child) fam.set_dad(father, child) self.assertEqual(set(fam.get_parents(child)), set([mother, father]))
def test_get_children(self): """ test getting children """ fam = Family('A') child1 = Person('A', 'B', 'C', '0', '1', '1') fam.add_person(child1) self.assertEqual(list(fam.get_children(child1)), []) father = Person('A', 'C', '0', '0', '1', '1') fam.add_person(father) fam.set_dad(father, child1) self.assertEqual(list(fam.get_children(father)), [child1]) child2 = Person('A', 'D', 'C', '0', '1', '1') fam.add_person(child2) fam.set_dad(father, child2) self.assertEqual(set(fam.get_children(father)), set([child1, child2]))
def test_set_dad(self): """ """ fam = Family('A') child = Person('A', 'B', 'C', '0', '1', '1') dad = Person('A', 'C', '0', '0', '1', '1') fam.add_person(child) fam.add_person(dad) fam.set_dad(dad, child) self.assertEqual(fam.get_father(child), dad) # make sure we can't add a second, different, father dad2 = Person('A', 'D', '0', '0', '2', '1') fam.add_person(dad) with self.assertRaises(ValueError): fam.set_dad(dad2, child) # but we can set the original father again fam.set_dad(dad, child)
def test_open_ped_multigenerational(self): """ test that open_ped works with multigenerational families """ self.temp.write('A B C D 1 1\n') self.temp.write('A C E F 1 1\n') self.temp.write('A D G H 2 1\n') self.temp.write('A E 0 0 1 1\n') self.temp.write('A F 0 0 2 1\n') self.temp.write('A G 0 0 1 1\n') self.temp.write('A H 0 0 2 1\n') self.temp.flush() families = open_ped(self.temp.name) fam = Family('A') child = Person('A', 'B', 'C', 'D', '1', '1') dad = Person('A', 'C', 'E', 'F', '1', '1') mom = Person('A', 'D', 'G', 'H', '2', '1') pat_gnddad = Person('A', 'E', '0', '0', '1', '1') pat_gndmom = Person('A', 'F', '0', '0', 'F', '1') mat_gnddad = Person('A', 'G', '0', '0', '1', '1') mat_gndmom = Person('A', 'H', '0', '0', 'F', '1') for x in [child, dad, mom, pat_gnddad, pat_gndmom, mat_gnddad, mat_gndmom]: fam.add_person(x) # and define relationships fam.set_dad(dad, child) fam.set_mom(mom, child) fam.set_dad(pat_gnddad, dad) fam.set_mom(pat_gndmom, dad) fam.set_dad(mat_gnddad, mom) fam.set_mom(mat_gndmom, mom) self.assertEqual(families[0].nodes, fam.nodes) self.assertEqual(families[0].edges, fam.edges)