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

Catalogue download #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "smpc-global"]
path = smpc-global
url = https://github.com/Athena-MHMD/smpc-global.git
url = https://github.com/athenarc/smpc-global.git
2 changes: 2 additions & 0 deletions scripts/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ verify_ssl = true
[packages]
numpy = "*"
pandas = "*"
requests = "*"
tqdm = "*"

[requires]
python_version = "3.7"
94 changes: 69 additions & 25 deletions scripts/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 79 additions & 16 deletions scripts/catalogue.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
#!/usr/bin/python3

import json
import argparse
import requests
import re
from tqdm import tqdm
import xml.etree.ElementTree as ET


def read_file(file):
with open(file) as f:
return json.load(f)


def write_file(file, out):
with open(file, 'w') as f:
json.dump(out, f, indent=4)
from utils import read_json, write_json


def process(args, callback):
catalogue = None
out = {}

catalogue = read_file(args.catalogue)
catalogue = read_json(args.catalogue)

for entry in catalogue:
callback(entry, out)

write_file(args.output, out)
write_json(args.output, out)


def normal(args):
Expand All @@ -38,12 +33,51 @@ def code(args):
process(args, lambda entry, out: out.update({entry['code']: entry}))


def download(args):
mesh_codes = read_json('../smpc-global/meshTermsByCode.json')
xml = ET.parse(args.catalogue)
root = xml.getroot()

string = '{(.*?)}'
prefix = re.search(string, root.tag)[0]
attributes = set()
values = set()
mesh_terms = []

for tname in root.findall('.//' + prefix + 'ClinicalVariables'):
attribute = tname.find(prefix + 'TypeName').text
value = tname.find(prefix + 'Value').text
attributes.add(attribute)
values.add(value)

for attr in tqdm(attributes):
data = {
'term': attr,
'terminology': 'text',
'result_format': 'json'
}

response = requests.post(args.url[0].strip('\t\n\r'), data=data)
terms = response.json()

tmp = {'attribute': attr, 'terms': []}
for t in terms:
if t['mesh_code'] in mesh_codes:
tmp['terms'].append(mesh_codes[t['mesh_code']])
else:
tmp['terms'].append(t)

mesh_terms.append(tmp)

write_json(args.output, mesh_terms)


def group(args):
catalogue = None
normalized_catalogue = {}
out = []

catalogue = read_file(args.catalogue)
catalogue = read_json(args.catalogue)

for entry in catalogue:
normalized_catalogue[entry['id']] = entry
Expand All @@ -55,14 +89,28 @@ def group(args):
normalized_catalogue[parent_id]['children'].append({key: normalized_catalogue[id][key] for key in normalized_catalogue[id] if key != 'children'})
out.append(normalized_catalogue[id])

write_file(args.output, out)
write_json(args.output, out)


def keywords(args):
mesh_terms = read_json('../smpc-global/meshTermsInversed.json')
headers = {'accept': 'application/json'}
response = requests.get(args.keywords[0].strip('\t\n\r'), headers=headers)
keywords = response.json()
out = []

for k in keywords['data']:
if k['label'] in mesh_terms:
out.append(mesh_terms[k['label']])

write_json(args.keywords[1], out)


def main():
parser = argparse.ArgumentParser(
description='SMPC catalogue processor')
parser.add_argument('catalogue', help='Catalogue file')
parser.add_argument('output', help='Output file')
parser.add_argument('catalogue', nargs='?', help='Catalogue file')
parser.add_argument('output', nargs='?', help='Output file')
parser.add_argument(
'-i',
'--inverse',
Expand All @@ -83,6 +131,17 @@ def main():
'--group',
action='store_true',
help='Create a file where each mesh term has its childer.')
parser.add_argument(
'-d',
'--download',
nargs=1,
dest='url',
help='Create a file where each attribute and value of the dataset is mapped to a mesh term by quering the catalogue.')
parser.add_argument(
'-k',
'--keywords',
nargs=2,
help='Create a file where each keyword from the given url is mapped to a mesh term.')
parser.add_argument('--version', action='version', version='%(prog)s 0.1')
args = parser.parse_args()

Expand All @@ -95,6 +154,10 @@ def main():
code(args)
elif(args.group):
group(args)
elif(args.url):
download(args)
elif(args.keywords):
keywords(args)
else:
normal(args)

Expand Down
6 changes: 3 additions & 3 deletions scripts/generate_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ def generate(dataset, output):

string = '{(.*?)}'
prefix = re.search(string, root.tag)[0]
attrributes = set()
attributes = set()

for tname in root.findall('.//' + prefix + 'TypeName'):
attrributes.add(tname.text)
attributes.add(tname.text)

with open(output, 'w') as f:
json.dump(list(attrributes), f, indent=4)
json.dump(list(attributes), f, indent=4)


def main():
Expand Down
2 changes: 2 additions & 0 deletions scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
numpy==1.16.0
pandas==0.23.4
requests==2.22.0
tqdm==4.32.1
2 changes: 1 addition & 1 deletion smpc-global
Submodule smpc-global updated 7 files
+188,956 −0 catalogue.json
+133,816 −14 mapping.json
+232,368 −0 mesh.json
+226,748 −0 meshTerms.json
+103,328 −0 meshTermsByCode.json
+501,141 −0 meshTermsGrouped.json
+103,328 −0 meshTermsInversed.json