From d714260914aefe7ede3bccb050f203280fbeb404 Mon Sep 17 00:00:00 2001 From: Susanna Kiwala Date: Tue, 22 Oct 2024 08:17:32 -0500 Subject: [PATCH] For variants, implement a subtype property to easier identify the type of variant --- civicpy/civic.py | 17 +++++++++++------ civicpy/tests/test_civic.py | 6 ++++++ civicpy/utils.py | 10 +++------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/civicpy/civic.py b/civicpy/civic.py index c870955..791ce1e 100644 --- a/civicpy/civic.py +++ b/civicpy/civic.py @@ -505,6 +505,7 @@ def sanitized_name(self): class Variant(CivicRecord): _SIMPLE_FIELDS = CivicRecord._SIMPLE_FIELDS.union({ + 'subtype', 'feature_id', 'name', 'single_variant_molecular_profile_id', @@ -524,6 +525,9 @@ def __init__(self, **kwargs): self._molecular_profiles = [] super().__init__(**kwargs) + def __repr__(self): + return ''.format(self.type, self.subtype, self.id) + @property def aliases(self): return self.variant_aliases @@ -1341,7 +1345,10 @@ def _get_elements_by_ids(element, id_list=[], allow_cached=True, get_all=False): ids = [] for e in response_elements: e = _postprocess_response_element(e, element) - cls = get_class(e['type']) + if element == 'variant': + cls = get_class(e['subtype']) + else: + cls = get_class(e['type']) partial_element = cls(**e, partial=True) ids.append(e['id']) elements.append(partial_element) @@ -1375,9 +1382,9 @@ def _postprocess_response_element(e, element): e['variant_ids'] = [v['id'] for v in e['variants']] del e['variants'] elif element == 'variant': - e['type'] = e['__typename'] e['feature_id'] = e['feature']['id'] if e['__typename'] == 'GeneVariant': + e['subtype'] = 'gene_variant' e['entrez_id'] = e['feature']['featureInstance']['entrezId'] e['entrez_name'] = e['feature']['name'] build = e['coordinates']['reference_build'] @@ -1387,11 +1394,9 @@ def _postprocess_response_element(e, element): build = 'GRCh38' e['coordinates']['reference_build'] = build elif e['__typename'] == 'FactorVariant': - #nothing special to handle - pass + e['subtype'] = 'factor_variant' elif e['__typename'] == 'FusionVariant': - #nothing special to handle - pass + e['subtype'] = 'fusion_variant' else: raise Exception("Variant type {} not supported yet".format(e['__typename'])) elif element == 'variant_group': diff --git a/civicpy/tests/test_civic.py b/civicpy/tests/test_civic.py index 10539dd..9588363 100644 --- a/civicpy/tests/test_civic.py +++ b/civicpy/tests/test_civic.py @@ -160,6 +160,8 @@ class TestGeneVariants(object): def test_get_all(self): variants = civic.get_all_gene_variants() assert len(variants) >= 3557 + for variant in variants: + assert variant.subtype == 'gene_variant' def test_properties(self): variant = civic.get_variant_by_id(11) @@ -171,6 +173,8 @@ class TestFusionVariants(object): def test_get_all(self): variants = civic.get_all_fusion_variants() assert len(variants) >= 263 + for variant in variants: + assert variant.subtype == 'fusion_variant' def test_properties(self): variant = civic.get_variant_by_id(1) @@ -182,6 +186,8 @@ class TestFactorVariants(object): def test_get_all(self): variants = civic.get_all_factor_variants() assert len(variants) >= 8 + for variant in variants: + assert variant.subtype == 'factor_variant' def test_properties(self): variant = civic.get_variant_by_id(4985) diff --git a/civicpy/utils.py b/civicpy/utils.py index 4716c96..dc30839 100644 --- a/civicpy/utils.py +++ b/civicpy/utils.py @@ -24,10 +24,6 @@ def search_url(element, use_search_meta): def snake_to_camel(snake_string): - if '_' not in snake_string and any(ele.isupper() for ele in snake_string): - #this is already camel case - return snake_string - else: - words = snake_string.split('_') - cap_words = [x.capitalize() for x in words] - return ''.join(cap_words) + words = snake_string.split('_') + cap_words = [x.capitalize() for x in words] + return ''.join(cap_words)