Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error messages #96

Merged
merged 9 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/genophenocorr/analysis/_commie.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,15 @@ def __init__(self, cohort: Cohort,
missing_implies_excluded: bool = False,
include_sv: bool = False,
p_val_correction: typing.Optional[str] = None,
min_perc_patients_w_hpo: typing.Union[float, int] = .1,
recessive: bool = False):
min_perc_patients_w_hpo: typing.Union[float, int] = .1):
if not isinstance(cohort, Cohort):
raise ValueError(f"cohort must be type Cohort but was type {type(cohort)}")

self._logger = logging.getLogger(__name__)
handler = logging.FileHandler(f"{__name__}.log", mode='w')
formatter = logging.Formatter("%(name)s %(asctime)s %(levelname)s %(message)s")
handler.setFormatter(formatter)
self._logger.addHandler(handler)
self._hpo = hpotk.util.validate_instance(hpo, hpotk.MinimalOntology, 'hpo')
self._phenotype_predicate_factory = PropagatingPhenotypeBooleanPredicateFactory(self._hpo,
missing_implies_excluded)
self._correction = p_val_correction
#TODO: For recessive tests, we need new predicates that return 3 categories
self._recessive = recessive
self._patient_list = list(cohort.all_patients) \
if include_sv \
else [pat for pat in cohort.all_patients if not all(var.variant_coordinates.is_structural() for var in pat.variants)]
Expand Down
45 changes: 11 additions & 34 deletions src/genophenocorr/analysis/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ class CohortAnalysisConfiguration:
def __init__(self, missing_implies_excluded: bool,
pval_correction: typing.Optional[str],
min_perc_patients_w_hpo: float,
include_sv: bool,
recessive: bool):
include_sv: bool):
self._missing_implies_excluded = missing_implies_excluded
self._pval_correction = pval_correction
self._min_perc_patients_w_hpo = min_perc_patients_w_hpo
self._include_sv = include_sv
self._recessive = recessive

@staticmethod
def builder():
Expand Down Expand Up @@ -89,15 +87,6 @@ def include_sv(self) -> bool:
(i.e. the variants that use symbolic VCF notation).
"""
return self._include_sv

@property
def recessive(self) -> bool:
"""
`True` if we want to test the correlation between heterozygous variants,
homozygous variants, and no variants (i.e. comparing frameshift variants on
both alleles, frameshift variant on one allele, and no frameshift variants)
"""
return self._recessive


class CohortAnalysisConfigurationBuilder:
Expand All @@ -111,15 +100,10 @@ class CohortAnalysisConfigurationBuilder:

def __init__(self):
self._logger = logging.getLogger(__name__)
handler = logging.FileHandler(f"{__name__}.log", mode='w')
formatter = logging.Formatter("%(name)s %(asctime)s %(levelname)s %(message)s")
handler.setFormatter(formatter)
self._logger.addHandler(handler)
self._missing_implies_excluded = False
self._pval_correction = 'bonferroni'
self._min_perc_patients_w_hpo = .1
self._include_sv = False
self._recessive = False

def missing_implies_excluded(self, missing_implies_excluded: bool):
"""
Expand All @@ -140,15 +124,21 @@ def pval_correction(self, pval_correction: typing.Optional[str]):
if pval_correction in P_VAL_OPTIONS:
lnrekerle marked this conversation as resolved.
Show resolved Hide resolved
self._pval_correction = pval_correction
else:
self._logger.warning('Ignoring invalid `pval_correction` value %s. Not doing p-value correction.', pval_correction)
self._logger.warning('Ignoring invalid `pval_correction` value %s. Using default "bonferroni" correction.', pval_correction)
ielis marked this conversation as resolved.
Show resolved Hide resolved
return self

def min_perc_patients_w_hpo(self, min_perc_patients_w_hpo: float):
"""
Set `min_perc_patients_w_hpo` option.
"""
if not isinstance(min_perc_patients_w_hpo, float):
try:
min_perc_patients_w_hpo = float(min_perc_patients_w_hpo)
except ValueError:
self._logger.warning("min_perc_patients_w_hpo must be a number, but was %s. Using default of 0.1", min_perc_patients_w_hpo)
min_perc_patients_w_hpo = 0.1
if min_perc_patients_w_hpo > 1 or min_perc_patients_w_hpo <= 0:
lnrekerle marked this conversation as resolved.
Show resolved Hide resolved
self._logger.warning("min_perc_patients_w_hpo must be greater than 0 and at most 1. Using default of 0.1")
self._logger.warning("min_perc_patients_w_hpo must be greater than 0 and at most 1, but was %f. Using default of 0.1", min_perc_patients_w_hpo)
else:
self._min_perc_patients_w_hpo = min_perc_patients_w_hpo
return self
Expand All @@ -162,17 +152,6 @@ def include_sv(self, include_sv: bool):
else:
self._logger.warning('Ignoring invalid `include_sv` value %s. Defaulting to not include large structural variants.', include_sv)
return self

def recessive(self, recessive: bool):
"""
Set `recessive` option.
"""
if isinstance(recessive, bool):
self._recessive = recessive
else:
self._logger.warning('Ignoring invalid `recessive` value %s. Defaulting to a dominant test.', recessive)
return self


def build(self) -> CohortAnalysisConfiguration:
"""
Expand All @@ -181,8 +160,7 @@ def build(self) -> CohortAnalysisConfiguration:
return CohortAnalysisConfiguration(self._missing_implies_excluded,
self._pval_correction,
self._min_perc_patients_w_hpo,
self._include_sv,
self._recessive)
self._include_sv)


def configure_cohort_analysis(cohort: Cohort,
Expand All @@ -203,5 +181,4 @@ def configure_cohort_analysis(cohort: Cohort,
missing_implies_excluded=config.missing_implies_excluded,
include_sv=config.include_sv,
p_val_correction=config.pval_correction,
min_perc_patients_w_hpo=config.min_perc_patients_w_hpo,
recessive=config.recessive)
min_perc_patients_w_hpo=config.min_perc_patients_w_hpo)
10 changes: 1 addition & 9 deletions src/genophenocorr/preprocessing/_phenopacket.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ class PhenopacketVariantCoordinateFinder(VariantCoordinateFinder[GenomicInterpre
def __init__(self, build: GenomeBuild,
hgvs_coordinate_finder: VariantCoordinateFinder[str]):
self._logger = logging.getLogger(__name__)
handler = logging.FileHandler(f"{__name__}.log", mode='w')
formatter = logging.Formatter("%(name)s %(asctime)s %(levelname)s %(message)s")
handler.setFormatter(formatter)
self._logger.addHandler(handler)
self._build = hpotk.util.validate_instance(build, GenomeBuild, 'build')
self._hgvs_finder = hpotk.util.validate_instance(hgvs_coordinate_finder, VariantCoordinateFinder,
'hgvs_coordinate_finder')
Expand Down Expand Up @@ -144,10 +140,6 @@ def __init__(self, build: GenomeBuild,
var_func_ann: FunctionalAnnotator,
hgvs_coordinate_finder: VariantCoordinateFinder[str]):
self._logger = logging.getLogger(__name__)
handler = logging.FileHandler(f"{__name__}.log", mode='w')
formatter = logging.Formatter("%(name)s %(asctime)s %(levelname)s %(message)s")
handler.setFormatter(formatter)
self._logger.addHandler(handler)
# Violates DI, but it is specific to this class, so I'll leave it "as is".
self._coord_finder = PhenopacketVariantCoordinateFinder(build, hgvs_coordinate_finder)
self._phenotype_creator = hpotk.util.validate_instance(phenotype_creator, PhenotypeCreator, 'phenotype_creator')
Expand Down Expand Up @@ -193,7 +185,7 @@ def _add_variants(self, sample_id: str, pp: Phenopacket) -> typing.Sequence[Vari
continue
tx_annotations = self._func_ann.annotate(vc)
if tx_annotations is None:
self._logger.error("Patient %s has an error with variant %s, this variant will not be included.", pp.id, vc.variant_key)
self._logger.warning("Patient %s has an error with variant %s, this variant will not be included.", pp.id, vc.variant_key)
continue
genotype = Genotypes.single(sample_id, gt)
variant = Variant(vc, tx_annotations, genotype)
Expand Down
4 changes: 0 additions & 4 deletions src/genophenocorr/preprocessing/_phenotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ def __init__(self, hpo: hpotk.MinimalOntology,
validator (hpotk.validate.ValidationRunner): A ValidationRunner object
"""
self._logger = logging.getLogger(__name__)
handler = logging.FileHandler(f"{__name__}.log", mode='w')
formatter = logging.Formatter("%(name)s %(asctime)s %(levelname)s %(message)s")
handler.setFormatter(formatter)
self._logger.addHandler(handler)
self._hpo = hpotk.util.validate_instance(hpo, hpotk.MinimalOntology, 'hpo')
self._validator = hpotk.util.validate_instance(validator, hpotk.validate.ValidationRunner, 'validator')

Expand Down
4 changes: 0 additions & 4 deletions src/genophenocorr/preprocessing/_uniprot.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ def __init__(self):
"""Constructs all necessary attributes for a UniprotProteinMetadataService object
"""
self._logger = logging.getLogger(__name__)
handler = logging.FileHandler(f"{__name__}.log", mode='w')
formatter = logging.Formatter("%(name)s %(asctime)s %(levelname)s %(message)s")
handler.setFormatter(formatter)
self._logger.addHandler(handler)
self._url = 'https://rest.uniprot.org/uniprotkb/search?query=(%s)AND(reviewed:true)&fields=accession,id,' \
'gene_names,gene_primary,protein_name,ft_domain,ft_motif,ft_region,ft_repeat,xref_refseq'

Expand Down
9 changes: 1 addition & 8 deletions src/genophenocorr/preprocessing/_vep.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ class VepFunctionalAnnotator(FunctionalAnnotator):
def __init__(self, protein_annotator: ProteinMetadataService,
include_computational_txs: bool = False):
self._logger = logging.getLogger(__name__)
handler = logging.FileHandler(f"{__name__}.log", mode='w')
formatter = logging.Formatter("%(name)s %(asctime)s %(levelname)s %(message)s")
handler.setFormatter(formatter)
self._logger.addHandler(handler)
self._protein_annotator = protein_annotator
self._url = 'https://rest.ensembl.org/vep/human/region/%s?LoF=1&canonical=1' \
'&domains=1&hgvs=1' \
Expand All @@ -81,9 +77,6 @@ def annotate(self, variant_coordinates: VariantCoordinates) -> typing.Sequence[T
"""
response = self._query_vep(variant_coordinates)
annotations = []
if response is None:
self._logger.error('VEP did not finish successfully.')
return None
if 'transcript_consequences' not in response:
self._logger.error('The VEP response lacked the required `transcript_consequences` field. %s', response)
return None
Expand Down Expand Up @@ -164,7 +157,7 @@ def _query_vep(self, variant_coordinates: VariantCoordinates) -> dict:
r = requests.get(api_url, headers={'Content-Type': 'application/json'})
if not r.ok:
self._logger.error("Expected a result but got an Error for variant: %s", variant_coordinates.variant_key)
self._logger.error(r.raise_for_status())
self._logger.error(r.text)
return None
results = r.json()
if not isinstance(results, list):
Expand Down