Skip to content

Commit

Permalink
For variants, implement a subtype property to easier identify the typ…
Browse files Browse the repository at this point in the history
…e of variant
  • Loading branch information
susannasiebert committed Oct 22, 2024
1 parent c899572 commit d714260
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
17 changes: 11 additions & 6 deletions civicpy/civic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -524,6 +525,9 @@ def __init__(self, **kwargs):
self._molecular_profiles = []
super().__init__(**kwargs)

def __repr__(self):
return '<CIViC {} ({}) {}>'.format(self.type, self.subtype, self.id)

@property
def aliases(self):
return self.variant_aliases
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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']
Expand All @@ -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':
Expand Down
6 changes: 6 additions & 0 deletions civicpy/tests/test_civic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 3 additions & 7 deletions civicpy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit d714260

Please sign in to comment.