Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
jsstevenson committed Mar 29, 2024
1 parent d4d23b2 commit d9e5ad5
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 225 deletions.
59 changes: 33 additions & 26 deletions analysis/civic/examples/harvester/civic_harvester_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,35 @@
def create_evidence_examples(data):
"""Create five CIViC evidence examples."""
evidence_items = list()
for i in range(len(data['evidence'])):
if data['evidence'][i]['assertions']:
evidence_items.append(data['evidence'][i])
for i in range(len(data["evidence"])):
if data["evidence"][i]["assertions"]:
evidence_items.append(data["evidence"][i])
if len(evidence_items) == 6:
break

for evidence_item in evidence_items:
variant_id = evidence_item['variant_id']
gene_id = evidence_item['gene_id']
assertions = evidence_item['assertions']
variant_id = evidence_item["variant_id"]
gene_id = evidence_item["gene_id"]
assertions = evidence_item["assertions"]

for v in data['variants']:
if v['id'] == variant_id:
for v in data["variants"]:
if v["id"] == variant_id:
variant = v

for g in data['genes']:
if g['id'] == gene_id:
for g in data["genes"]:
if g["id"] == gene_id:
gene = g

with open(f"{PROJECT_ROOT}/analysis/civic/examples/harvester/"
f"{evidence_item['name']}.json", 'w+') as f:
with open(
f"{PROJECT_ROOT}/analysis/civic/examples/harvester/"
f"{evidence_item['name']}.json",
"w+",
) as f:
example = {
'EVIDENCE': evidence_item,
'GENE': gene,
'VARIANT': variant,
'ASSERTIONS': assertions
"EVIDENCE": evidence_item,
"GENE": gene,
"VARIANT": variant,
"ASSERTIONS": assertions,
}

json.dump(example, f, indent=4)
Expand All @@ -45,26 +48,30 @@ def create_variant_examples(data):
"""
variants_ids = [12, 1, 221, 190]
variants = list()
for i in range(len(data['variants'])):
if data['variants'][i]['id'] in variants_ids:
variants.append(data['variants'][i])
for i in range(len(data["variants"])):
if data["variants"][i]["id"] in variants_ids:
variants.append(data["variants"][i])

for variant in variants:
with open(f"{PROJECT_ROOT}/analysis/civic/examples/harvester/"
f"{variant['name'].lower()}.json", 'w+') as f:
variant['evidence_items'] = variant['evidence_items'][0]
with open(
f"{PROJECT_ROOT}/analysis/civic/examples/harvester/"
f"{variant['name'].lower()}.json",
"w+",
) as f:
variant["evidence_items"] = variant["evidence_items"][0]
f.write(json.dumps(variant, indent=4))


if __name__ == '__main__':
if __name__ == "__main__":
c = CivicHarvester()
c.harvest()
latest = sorted((APP_ROOT / "data" / "civic" / "harvester").glob("civic_harvester_*.json"))[-1] # noqa: E501
latest = sorted(
(APP_ROOT / "data" / "civic" / "harvester").glob("civic_harvester_*.json")
)[-1] # noqa: E501
with open(latest, "r") as f:
civic_data = json.load(f)

civic_ex_dir =\
PROJECT_ROOT / 'analysis' / 'civic' / 'examples' / 'harvester'
civic_ex_dir = PROJECT_ROOT / "analysis" / "civic" / "examples" / "harvester"
civic_ex_dir.mkdir(exist_ok=True, parents=True)

create_evidence_examples(civic_data)
Expand Down
109 changes: 60 additions & 49 deletions analysis/civic/examples/transform/civic_transform_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@
def create_civic_example(civic_data):
"""Create CIViC transform examples from list of evidence items."""
ex = {
'statements': [],
'propositions': [],
'variation_descriptors': [],
'gene_descriptors': [],
'therapy_descriptors': [],
'disease_descriptors': [],
'methods': [],
'documents': []
"statements": [],
"propositions": [],
"variation_descriptors": [],
"gene_descriptors": [],
"therapy_descriptors": [],
"disease_descriptors": [],
"methods": [],
"documents": [],
}
supported_by_statement_ids = set()
for s in civic_data['statements']:
if s['id'] == 'civic.aid:6':
supported_by_statement_ids = \
{s for s in s['supported_by'] if s.startswith('civic.eid')}
supported_by_statement_ids.add(s['id'])
for s in civic_data["statements"]:
if s["id"] == "civic.aid:6":
supported_by_statement_ids = {
s for s in s["supported_by"] if s.startswith("civic.eid")
}
supported_by_statement_ids.add(s["id"])
break

proposition_ids = set()
Expand All @@ -32,56 +33,66 @@ def create_civic_example(civic_data):
gids = set()
methods = set()
documents = set()
for s in civic_data['statements']:
if s['id'] in supported_by_statement_ids:
ex['statements'].append(s)
proposition_ids.add(s['proposition'])
vids.add(s['variation_descriptor'])
tids.add(s['therapy_descriptor'])
dids.add(s['disease_descriptor'])
methods.add(s['method'])
documents.update({d for d in s['supported_by'] if
not d.startswith('civic.eid')})
for s in civic_data["statements"]:
if s["id"] in supported_by_statement_ids:
ex["statements"].append(s)
proposition_ids.add(s["proposition"])
vids.add(s["variation_descriptor"])
tids.add(s["therapy_descriptor"])
dids.add(s["disease_descriptor"])
methods.add(s["method"])
documents.update(
{d for d in s["supported_by"] if not d.startswith("civic.eid")}
)

for p in civic_data['propositions']:
if p['id'] in proposition_ids:
ex['propositions'].append(p)
for p in civic_data["propositions"]:
if p["id"] in proposition_ids:
ex["propositions"].append(p)

for v in civic_data['variation_descriptors']:
if v['id'] in vids:
ex['variation_descriptors'].append(v)
gids.add(v['gene_context'])
for v in civic_data["variation_descriptors"]:
if v["id"] in vids:
ex["variation_descriptors"].append(v)
gids.add(v["gene_context"])

for t in civic_data['therapy_descriptors']:
if t['id'] in tids:
ex['therapy_descriptors'].append(t)
for t in civic_data["therapy_descriptors"]:
if t["id"] in tids:
ex["therapy_descriptors"].append(t)

for d in civic_data['disease_descriptors']:
if d['id'] in dids:
ex['disease_descriptors'].append(d)
for d in civic_data["disease_descriptors"]:
if d["id"] in dids:
ex["disease_descriptors"].append(d)

for g in civic_data['gene_descriptors']:
if g['id'] in gids:
ex['gene_descriptors'].append(g)
for g in civic_data["gene_descriptors"]:
if g["id"] in gids:
ex["gene_descriptors"].append(g)

for m in civic_data['methods']:
if m['id'] in methods:
ex['methods'].append(m)
for m in civic_data["methods"]:
if m["id"] in methods:
ex["methods"].append(m)

for d in civic_data['documents']:
if d['id'] in documents:
ex['documents'].append(d)
for d in civic_data["documents"]:
if d["id"] in documents:
ex["documents"].append(d)

with open(PROJECT_ROOT / "analysis" / "civic" / "examples" / # noqa: W504
"transform" / "civic_cdm_example.json", 'w+') as f2:
with open(
PROJECT_ROOT
/ "analysis"
/ "civic"
/ "examples" # noqa: W504
/ "transform"
/ "civic_cdm_example.json",
"w+",
) as f2:
json.dump(ex, f2, indent=4)


if __name__ == '__main__':
if __name__ == "__main__":
civic = CivicTransform()
civic.transform()
civic.create_json()
latest = sorted((APP_ROOT / "data" / "civic" / "transform").glob("civic_cdm_*.json"))[-1] # noqa: E501
latest = sorted(
(APP_ROOT / "data" / "civic" / "transform").glob("civic_cdm_*.json")
)[-1] # noqa: E501
with open(latest, "r") as f:
civic_data = json.load(f)
create_civic_example(civic_data)
6 changes: 3 additions & 3 deletions analysis/graph/db_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
g = Graph(uri="bolt://localhost:7687", credentials=("neo4j", "admin"))
g.clear()

fpath = APP_ROOT / 'data' / 'civic' / 'transform' / 'civic_cdm.json'
with open(fpath, 'r') as f:
fpath = APP_ROOT / "data" / "civic" / "transform" / "civic_cdm.json"
with open(fpath, "r") as f:
items = json.load(f)

count = 0
for item in items:
if 'assertion' in item.keys():
if "assertion" in item.keys():
continue
else:
g.add_transformed_data(item)
Expand Down
50 changes: 27 additions & 23 deletions analysis/moa/examples/harvester/moa_harvester_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,28 @@ def create_assertion_examples(data):
"""Create five MOAlmanac assertion examples."""
assertions = []
for i in [0, 69, 599, 699, 759]:
if data['assertions'][i]['source_ids']:
assertions.append(data['assertions'][i])
if data["assertions"][i]["source_ids"]:
assertions.append(data["assertions"][i])

for assertion in assertions:
source_id = assertion['source_ids']
for s in data['sources']:
if s['id'] == source_id:
source_id = assertion["source_ids"]
for s in data["sources"]:
if s["id"] == source_id:
source = s
break

feature_id = assertion['variant']['id']
for v in data['variants']:
if v['id'] == feature_id:
feature_id = assertion["variant"]["id"]
for v in data["variants"]:
if v["id"] == feature_id:
variant = v
break

with open(f"{PROJECT_ROOT}/analysis/moa/examples/harvester/"
f"assertion {assertion['id']}.json", 'w+') as f:
example = {
'ASSERTIONS': assertion,
'SOURCES': source,
'VARIANTS': variant
}
with open(
f"{PROJECT_ROOT}/analysis/moa/examples/harvester/"
f"assertion {assertion['id']}.json",
"w+",
) as f:
example = {"ASSERTIONS": assertion, "SOURCES": source, "VARIANTS": variant}

json.dump(example, f, indent=4)
print(f"Created JSON for evidence: assertion {assertion['id']}")
Expand All @@ -44,24 +43,29 @@ def create_variant_examples(data):
"""
variants_ids = [1, 147, 551, 701]
variants = []
for i in range(len(data['variants'])):
if data['variants'][i]['id'] in variants_ids:
variants.append(data['variants'][i])
for i in range(len(data["variants"])):
if data["variants"][i]["id"] in variants_ids:
variants.append(data["variants"][i])
for variant in variants:
with open(f"{PROJECT_ROOT}/analysis/moa/examples/harvester/"
f"{variant['feature'].lower()}.json", 'w+') as f:
with open(
f"{PROJECT_ROOT}/analysis/moa/examples/harvester/"
f"{variant['feature'].lower()}.json",
"w+",
) as f:
f.write(json.dumps(variant, indent=4))
print(f"Created JSON for variant: {variant['feature']}")
f.close()


if __name__ == '__main__':
if __name__ == "__main__":
moa = MoaHarvester()
moa.harvest()
latest = sorted((APP_ROOT / "data" / "moa" / "harvester").glob("moa_harvester_*.json"))[-1] # noqa: E501
latest = sorted(
(APP_ROOT / "data" / "moa" / "harvester").glob("moa_harvester_*.json")
)[-1] # noqa: E501
with open(latest, "r") as f:
moa_data = json.load(f)
moa_ex_dir = PROJECT_ROOT / 'analysis' / 'moa' / 'examples'
moa_ex_dir = PROJECT_ROOT / "analysis" / "moa" / "examples"
moa_ex_dir.mkdir(exist_ok=True, parents=True)
create_assertion_examples(moa_data)
create_variant_examples(moa_data)
Loading

0 comments on commit d9e5ad5

Please sign in to comment.