Created
February 24, 2022 21:26
-
-
Save wagenrace/67cf60d5e00aaaee2795fb3fadb1d7c9 to your computer and use it in GitHub Desktop.
Adding synonimes and components to neo4j with python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#%% | |
import gzip | |
import os | |
import urllib.request | |
from py2neo import Graph | |
temp_dir = "temp" | |
os.makedirs(temp_dir, exist_ok=True) | |
user = "neo4j" | |
port = "7687" | |
pswd = input("password") | |
graph = Graph("bolt://localhost:" + port, name="pubchem", auth=(user, pswd)) | |
number = 1 | |
while True: | |
# Get | |
download_url = fr"https://ftp.ncbi.nlm.nih.gov/pubchem/RDF/synonym/pc_synonym2compound_{str(number).zfill(6)}.ttl.gz" | |
print("download: ", download_url) | |
gz_file = os.path.join(temp_dir, "file.gz") | |
ttl_file = os.path.abspath(os.path.join(temp_dir, "file.tll")) | |
try: | |
urllib.request.urlretrieve(download_url, gz_file) | |
except: | |
break | |
with gzip.open(gz_file, "r") as f: | |
file_content = f.read() | |
file_content = file_content.decode("utf-8") | |
with open(ttl_file, "w+") as f_out: | |
f_out.write(file_content) | |
cypher = """CALL n10s.rdf.import.fetch("file:""" + ttl_file.replace("\\", "\\\\") + """","Turtle");""" | |
print(graph.run(cypher).data()) | |
number += 1 | |
number = 1 | |
while True: | |
# Get | |
download_url = fr"https://ftp.ncbi.nlm.nih.gov/pubchem/RDF/synonym/pc_synonym_value_{str(number).zfill(6)}.ttl.gz" | |
print("download: ", download_url) | |
gz_file = os.path.join(temp_dir, "file.gz") | |
ttl_file = os.path.abspath(os.path.join(temp_dir, "file.tll")) | |
try: | |
urllib.request.urlretrieve(download_url, gz_file) | |
except: | |
break | |
with gzip.open(gz_file, "r") as f: | |
file_content = f.read() | |
file_content = file_content.decode("utf-8") | |
with open(ttl_file, "w+") as f_out: | |
f_out.write(file_content) | |
cypher = """CALL n10s.rdf.import.fetch("file:""" + ttl_file.replace("\\", "\\\\") + """","Turtle");""" | |
print(graph.run(cypher).data()) | |
number += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment