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

EVA-3732 - New script to help find children that match the definition of a new term #452

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
56 changes: 56 additions & 0 deletions bin/trait_mapping/get_children_with_keywords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import argparse

from cmat.clinvar_xml_io.ontology_uri import OntologyUri
from cmat.trait_mapping.ols import build_ols_query
from cmat.trait_mapping.utils import json_request

def append_embedded(results, json_response):
if json_response and '_embedded' in json_response:
for key in json_response['_embedded']:
if key not in results:
results[key] = []
results[key].extend(json_response['_embedded'][key])

def query_and_depaginate(url):
json_response = json_request(url)
results = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be a defaultdict(list)

append_embedded(results, json_response)
while 'next' in json_response['_links']:
json_response = json_request(json_response['_links']['next']['href'])
append_embedded(results, json_response)
return results

def search_in(keywords, text):
return set((keyword for keyword in keywords if keyword in text))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

& same elsewhere

def main():
parser = argparse.ArgumentParser('Search OLS for children of a term that match certain keywords in their label, description or synonyms')
parser.add_argument('--ontology', type=str, default='MONDO', help='Name of the Ontology to find the parent and children')
parser.add_argument('--parent_curie', type=str, help='Curie of the parent term', required=True)
parser.add_argument('--keywords', type=str, nargs='+', help="Words that must be present in the child's ontology label, description or synonyms to be reported")

args = parser.parse_args()


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

keywords = set(args.keywords)
db = args.ontology
parent_curie = args.parent_curie
url = build_ols_query(OntologyUri(parent_curie, db).uri)
results = query_and_depaginate(url)
for term in results['terms']:
if term['ontology_prefix'] == db:
results2 = query_and_depaginate(term['_links']['children']['href'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could maybe call this children_results or something similar

for term in results2['terms']:
if term['ontology_prefix'] == db:
keyword_found = set()
keyword_found.update(search_in(keywords, term['label']))
keyword_found.update(search_in(keywords, term['description']))
for synonym in term['synonyms']:
keyword_found.update(search_in(keywords, synonym))
if keyword_found == keywords:
print(term['iri'], term['label'])


if __name__ == '__main__':
main()

Loading