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 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_open_ped_tabs(self): """ check open_ped works when the file has tabs """ self.temp.write('A\tB\t0\t0\t1\t1\n') self.temp.flush() families = open_ped(self.temp.name) fam = Family('A') fam.add_person(Person('A', 'B', '0', '0', '1', '1')) self.assertEqual(families[0].nodes, fam.nodes)
def test_open_ped(self): """ check open_ped works with a ped file with spaces """ self.temp.write('A B 0 0 1 1\n') self.temp.flush() families = open_ped(self.temp.name) fam = Family('A') fam.add_person(Person('A', 'B', '0', '0', '1', '1')) self.assertEqual(families[0].nodes, fam.nodes)
def test_open_ped_mismatch(self): """ check open_ped doesn't give match for different families """ self.temp.write('A B 0 0 1 1\n') self.temp.flush() families = open_ped(self.temp.name) fam = Family('A') fam.add_person(Person('A', 'B', '0', '0', '1', '1')) fam.add_person(Person('A', 'C', '0', '0', '1', '1')) self.assertNotEqual(families[0].nodes, fam.nodes)
def test_open_ped_comment_line(self): """ check open_ped works when we have a comment line """ self.temp.write('A B 0 0 1 1\n') self.temp.write('#anything can go here\n') self.temp.flush() families = open_ped(self.temp.name) fam = Family('A') fam.add_person(Person('A', 'B', '0', '0', '1', '1')) self.assertEqual(families[0].nodes, fam.nodes)
def test_open_ped_header(self): """ check open_ped works when we have a header """ self.temp.write('family_id person_id dad mom sex phenotype\n') self.temp.write('A B 0 0 1 1\n') self.temp.flush() families = open_ped(self.temp.name) fam = Family('A') fam.add_person(Person('A', 'B', '0', '0', '1', '1')) self.assertEqual(families[0].nodes, fam.nodes)
def test_link_members_inferred(self): """ test that link_members works with parents from child lines """ 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) # getting a list or iterator of family members doesn't include parents # only described in child lines self.assertEqual(set(fam), set([child1, child2])) # still identify parents, even though we don't iterate through them self.assertEqual(fam.get_mother(child1), Person('A', 'D', 'NA', 'NA', 'female', 'NA')) self.assertEqual(fam.get_father(child1), Person('A', 'C', 'NA', 'NA', 'male', 'NA')) self.assertEqual(fam.get_mother(child2), Person('A', 'D', 'NA', 'NA', 'female', 'NA')) self.assertEqual(fam.get_father(child2), Person('A', 'C', 'NA', 'NA', 'male', 'NA'))
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_family_initialize(self): """ check Family() initializes correctly """ fam = Family('A') self.assertEqual(fam.id, 'A') self.assertEqual(len(fam), 0)
def test_set_mom_male(self): """ check we raise an error if we try to add a male mother """ fam = Family('A') child = Person('A', 'B', '0', 'C', '1', '1') mom = Person('A', 'C', '0', '0', '1', '1') fam.add_person(child) fam.add_person(mom) with self.assertRaises(ValueError): fam.set_mom(mom, child)
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_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_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_mom_mismatching_id(self): """ if we set a mother, the mothers ID must be expected in child """ fam = Family('A') child = Person('A', 'B', '0', 'D', '1', '1') mom = Person('A', 'C', '0', '0', '2', '1') fam.add_person(child) fam.add_person(mom) with self.assertRaises(ValueError): fam.set_mom(mom, child)
def test_add_person(self): """ test adding a person """ fam = Family('A') person = Person('A', 'B', '0', '0', '1', '1') fam.add_person(person) self.assertTrue(person in fam) # check for an error if the family ID doesn't match person = Person('NOT_A', 'B', '0', '0', '1', '1') with self.assertRaises(ValueError): fam.add_person(person)
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_open_ped_multifamily(self): """ test that open_ped works with multiple families """ self.temp.write('A B 0 0 1 1\n') self.temp.write('C D 0 0 1 1\n') self.temp.flush() families = open_ped(self.temp.name) fam1 = Family('A') fam1.add_person(Person('A', 'B', '0', '0', '1', '1')) fam2 = Family('C') fam2.add_person(Person('C', 'D', '0', '0', '1', '1')) expected = [fam1, fam2] # the self.assertEqual(len(families), len(expected)) for a, b in zip(sorted(families), sorted(expected)): self.assertEqual(a.nodes, b.nodes) self.assertEqual(a.edges, b.edges)
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_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_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_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)
def test_set_mom(self): """ """ fam = Family('A') child = Person('A', 'B', '0', 'C', '1', '1') mom = Person('A', 'C', '0', '0', '2', '1') fam.add_person(child) fam.add_person(mom) fam.set_mom(mom, child) self.assertEqual(fam.get_mother(child), mom) # make sure we can't add a second, different, mother mom2 = Person('A', 'D', '0', '0', '2', '1') fam.add_person(mom) with self.assertRaises(ValueError): fam.set_mom(mom2, child) # but we can set the original mother again fam.set_mom(mom, 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_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_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]))