def test_bws_model_err(self): ''' Test ModelError raised because of pedigree file with only one twin. ''' target = Female("FAM1", "F0", "001", "002", "003", target="1", age="20", mztwin="1", yob=str(date.today().year-20), gtests=CanRiskGeneticTests.default_factory()) pedigree = CanRiskPedigree(people=[target]) with self.assertRaisesRegex(ModelError, r"ERRORS IN THE PEDIGREE FILE"): Predictions(pedigree)
def test_calcs_validation_err(self): """ Test invalid calculation type raise ValidationError. """ target = Female("FAM1", "F0", "001", "002", "003", target="1", age="20", yob=str(date.today().year-20), gtests=CanRiskGeneticTests.default_factory()) pedigree = CanRiskPedigree(people=[target]) with self.assertRaisesRegex(ValidationError, r"Unknown calculation requested: dribble"): Predictions(pedigree, model_settings=settings.OC_MODEL, calcs=['dribble'])
def setUp(self): ''' Build pedigree data. ''' self.year = date.today().year target = Female("FAM1", "F0", "001", "002", "003", target="1", age="20", yob=str(self.year-20), cancers=Cancers()) self.pedigree = BwaPedigree(people=[target]) # parents (_father, _mother) = self.pedigree.add_parents(target) self.cwd = tempfile.mkdtemp(prefix="TEST_", dir="/tmp")
def test_mztwin_parents(self): """ Check if an error is raised if MZ twin have different parents. """ pedigree_file = deepcopy(self.pedigree_file) pedigree = pedigree_file.pedigrees[0] m2 = pedigree.get_person_by_name('M2') self._add_twin(pedigree, twin=m2) m2.mothid = "313" pedigree.people.append(Female(m2.famid, "F3A", "313", "0", "0")) with self.assertRaisesRegex(PedigreeError, r"MZ twins must have the same parents"): PedigreeFile.validate(pedigree_file.pedigrees)
def test_father_sex(self): ''' Test an error is raised if the sex of the father is not 'M'. ''' # change sex of father to 'F' pedigree_file = deepcopy(self.pedigree_file) m2 = pedigree_file.pedigrees[0].get_person_by_name('M2') pedigree_file.pedigrees[0].people.remove(m2) pedigree_file.pedigrees[0].people.append( Female(m2.famid, m2.name, m2.pid, m2.fathid, m2.mothid)) with self.assertRaisesRegex( PersonError, r"All fathers in the pedigree must have sex specified as 'M'"): PedigreeFile.validate(pedigree_file.pedigrees)
def test_niceness(self): """ Test niceness level for pedigree with sibling and large pedigree. """ pedigree = deepcopy(self.pedigree) self.assertEqual(Predictions._get_niceness(pedigree, factor=1), len(pedigree.people)) self.assertEqual(Predictions._get_niceness(pedigree, factor=0.01), 19) # sister target = pedigree.get_target() sister = Female("FAM1", "F01", "0011", target.fathid, target.mothid, age="22", yob=str(self.year-23), cancers=Cancers(bc1=Cancer("20"))) pedigree.people.append(sister) self.assertEqual(Predictions._get_niceness(pedigree), 1)
def setUp(self): ''' Build pedigree data. ''' self.year = date.today().year target = Female("FAM1", "F0", "001", "002", "003", target="1", age="20", yob=str(self.year - 20), cancers=Cancers()) self.pedigree = BwaPedigree(people=[target]) # parents (_father, _mother) = self.pedigree.add_parents(target) # canrisk pedigree target = Female("FAM1", "F0", "001", "002", "003", target="1", age="20", yob=str(self.year - 20), cancers=Cancers(), gtests=CanRiskGeneticTests.default_factory()) self.canrisk_pedigree = CanRiskPedigree(people=[target]) (_father, _mother) = self.canrisk_pedigree.add_parents( target, gtests=CanRiskGeneticTests.default_factory()) _mother.yob = str(self.year - 55) _mother.age = "55" _mother.cancers = Cancers(bc1=Cancer("54"), bc2=Cancer("55"), oc=Cancer(), prc=Cancer(), pac=Cancer()) self.cwd = tempfile.mkdtemp(prefix="TEST_", dir="/tmp")
def _get_pedi(self): t = self.predictions.pedi.get_target() if t.cancers.is_cancer_diagnosed(): cancers = Cancers(bc1=Cancer(t.cancers.diagnoses.bc1.age), bc2=Cancer(), oc=Cancer(), prc=Cancer(), pac=Cancer()) else: cancers = Cancers() if self.predictions.model_settings['NAME'] == 'BC': gtests = BWSGeneticTests.default_factory() else: gtests = CanRiskGeneticTests.default_factory() if t.sex() is "M": new_t = Male(t.famid, t.name, t.pid, "", "", target=t.target, dead="0", age=t.age, yob=t.yob, cancers=cancers, gtests=gtests) else: new_t = Female(t.famid, t.name, t.pid, "", "", target=t.target, dead="0", age=t.age, yob=t.yob, cancers=cancers, gtests=gtests) if self.predictions.model_settings['NAME'] == 'BC': return BwaPedigree(people=[new_t]) else: return CanRiskPedigree(people=[new_t])
def _add_twin(self, pedigree, twin=None, twin_key="1"): """ Utility for creating a twin given a person in a pedigree. """ if twin is None: twin = pedigree.get_person_by_name('M2') twin.mztwin = twin_key twin.fathid = "311" twin.mothid = "312" pedigree.people.append(Male(twin.famid, "M3", "311", "0", "0")) pedigree.people.append(Female(twin.famid, "F3", "312", "0", "0")) pedigree.people.append( Male(twin.famid, "M2A", "111", "311", "312", mztwin=twin_key, age=twin.age, yob=twin.yob, gtests=deepcopy(twin.gtests), cancers=deepcopy(twin.cancers)))
def _get_pedi(self): t = super()._get_pedi().get_target() cancers = Cancers() if self.predictions.model_settings['NAME'] == 'BC': gtests = BWSGeneticTests.default_factory() else: gtests = CanRiskGeneticTests.default_factory() if t.sex() is "M": new_t = Male(t.famid, t.name, t.pid, "", "", target=t.target, dead="0", age=t.age, yob=t.yob, cancers=cancers, gtests=gtests) else: new_t = Female(t.famid, t.name, t.pid, "", "", target=t.target, dead="0", age=t.age, yob=t.yob, cancers=cancers, gtests=gtests) if self.predictions.model_settings['NAME'] == 'BC': return BwaPedigree(people=[new_t]) else: return CanRiskPedigree(people=[new_t])
def test_mztwin_sex(self): """ Check if an error is raised if MZ twins are not the same sex. """ pedigree_file = deepcopy(self.pedigree_file) pedigree = pedigree_file.pedigrees[0] m2 = pedigree.get_person_by_name('M2') self._add_twin(pedigree, twin=m2) pedigree_file.pedigrees[0].people.remove(m2) pedigree_file.pedigrees[0].people.append( Female(m2.famid, m2.name, m2.pid, m2.fathid, m2.mothid, mztwin=m2.mztwin, age=m2.age, yob=m2.yob, cancers=m2.cancers)) with self.assertRaisesRegex(PedigreeError, r"MZ twins must have the same sex"): PedigreeFile.validate(pedigree_file.pedigrees)
def test_ovarian_calculations(self): """ Test prediction of cancer risk and mutation probability. """ target = Female("FAM1", "F0", "001", "002", "003", target="1", age="20", yob=str(self.year - 20), cancers=Cancers(), gtests=CanRiskGeneticTests.default_factory()) pedigree = CanRiskPedigree(people=[target]) # parents (_father, _mother) = pedigree.add_parents( target, gtests=CanRiskGeneticTests.default_factory()) PedigreeFile.validate(pedigree) params = ModelParams( mutation_frequency=settings.OC_MODEL['MUTATION_FREQUENCIES']["UK"], mutation_sensitivity=settings.OC_MODEL['GENETIC_TEST_SENSITIVITY']) calcs = Predictions(pedigree, cwd=self.cwd, model_params=params, model_settings=settings.OC_MODEL, calcs=[]) # each gene should have a mutation probability plus a result for no mutations for mp in calcs.mutation_probabilties: key = list(mp.keys())[0] self.assertTrue(key in settings.OC_MODEL['GENES'] or key == "no mutation") self.assertEqual(len(calcs.mutation_probabilties), len(settings.OC_MODEL['GENES']) + 1) # risks calculated at 16 different ages: self.assertEqual(len(calcs.cancer_risks), 16) self.assertTrue( [c.get('age') for c in calcs.cancer_risks] == [21, 22, 23, 24, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80])