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_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___str__(self): """ check that we can convert a Person back to ped line """ i = Person('A', 'B', '0', '0', '1', '0') self.assertEqual(str(i), 'A\tB\t0\t0\t1\t0\n') i = Person('A', 'B', '0', '0', '1', '0', 'EXTRA', 'EXTRA2') self.assertEqual(str(i), 'A\tB\t0\t0\t1\t0\tEXTRA\tEXTRA2\n')
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_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_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_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_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_initialize_person_data(self): """ check that Person() initializes correctly with additional columns """ i = Person('A', 'B', '0', '0', '1', '0') self.assertEqual(i.data, ()) i = Person('A', 'B', '0', '0', '1', '0', 'PATH_TO_VCF') self.assertEqual(i.data, ('PATH_TO_VCF', )) i = Person('A', 'B', '0', '0', '1', '0', 'PATH_TO_VCF', 'SOMETHING_ELSE') self.assertEqual(i.data, ('PATH_TO_VCF', 'SOMETHING_ELSE'))
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_is_male(self): """ check that is_male works correctly """ a = Person('A', 'B', '0', '0', '1', '1') self.assertTrue(a.is_male()) a = Person('A', 'B', '0', '0', '2', '1') self.assertFalse(a.is_male()) a = Person('A', 'B', '0', '0', '0', '1') self.assertFalse(a.is_male()) self.assertTrue(a.unknown_sex())
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_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_equality(self): """ check that we can compare two individuals """ # two Persons with the same family ID and individual ID are equivalent a = Person('A', 'B', '0', '0', '1', '1') b = Person('A', 'B', '1', '1', '1', '1') self.assertEqual(a, b) # different individual IDs don't match c = Person('A', 'C', '1', '1', '1', '1') self.assertNotEqual(a, c) # different family IDs don't match d = Person('D', 'B', '1', '1', '1', '1') self.assertNotEqual(a, d)
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_initialize_person(self): """ check Person() initializes correctly """ i = Person('A', 'B', '0', '0', '1', '0') self.assertEqual(i.id, 'B') self.assertEqual(i.family, 'A') self.assertEqual(i.mom, '0') self.assertEqual(i.dad, '0')
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_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]))
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_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_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_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_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_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_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_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)