|
|
|
|
|
import json |
|
import gzip |
|
import sys |
|
|
|
from datetime import datetime |
|
|
|
|
|
|
|
def read_gz_by_row(FILENAME): |
|
try: |
|
|
|
with gzip.open(FILENAME, 'rb') as f: |
|
|
|
for line in f: |
|
|
|
decoded_line = line.decode('utf-8') |
|
yield decoded_line.strip() |
|
|
|
|
|
print(decoded_line.rstrip()) |
|
except FileNotFoundError: |
|
print(f"Error: File '{FILENAME}' not found.") |
|
|
|
def limit_string_size(text, max_size=1024, suffix="..."): |
|
"""Limits the size of a string to a specified maximum, adding an optional suffix. |
|
|
|
Args: |
|
text: The string to limit. |
|
max_size: The maximum allowed size (default: 1024 characters). |
|
suffix: The suffix to add if the string is truncated (default: "..."). |
|
|
|
Returns: |
|
The truncated string if it exceeds the limit, otherwise the original string. |
|
""" |
|
|
|
if len(text) > max_size: |
|
|
|
truncated_text = text[:max_size - len(suffix)] |
|
return truncated_text + suffix |
|
|
|
else: |
|
return text |
|
|
|
|
|
|
|
def fix_string(string): |
|
|
|
string = string.replace('\\','') |
|
string = string.replace("'","") |
|
string = string.replace('"','') |
|
string = string.replace("\n",'') |
|
string = limit_string_size(string) |
|
|
|
return string |
|
|
|
|
|
def object2nt(json_doc): |
|
|
|
ntriples_str = '' |
|
|
|
for key, value in json_doc.items(): |
|
|
|
|
|
|
|
if not value is None: |
|
if isinstance(value, bool): |
|
if value: |
|
ntriple = f"<http://bio2rdf.org/{NAMESPACE}:{doc_id}> <http://bio2rdf.org/intermine_voc:{key}> true .\n" |
|
else: |
|
ntriple = f"<http://bio2rdf.org/{NAMESPACE}:{doc_id}> <http://bio2rdf.org/intermine_voc:{key}> false .\n" |
|
elif isinstance(value, str): |
|
value = fix_string(value) |
|
ntriple = f"<http://bio2rdf.org/{NAMESPACE}:{doc_id}> <http://bio2rdf.org/intermine_voc:{key}> '{value}' .\n" |
|
if key == 'class_name': |
|
ntriple1 = f"<http://bio2rdf.org/{NAMESPACE}:{doc_id}> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://bio2rdf.org/intermine_voc:{value}> .\n" |
|
ntriple = ntriple1 + ntriple |
|
else: |
|
ntriple = f"<http://bio2rdf.org/{NAMESPACE}:{doc_id}> <http://bio2rdf.org/intermine_voc:{key}> {value} .\n" |
|
|
|
|
|
ntriples_str = ntriples_str + ntriple |
|
return ntriples_str |
|
|
|
|
|
def relation2nt(json_doc): |
|
|
|
|
|
|
|
ntriple1 = f"<http://bio2rdf.org/{json_doc['namespace']}:{json_doc['class_name_from']}:{str(json_doc['id_from'])}> " |
|
ntriple2 = f"<http://bio2rdf.org/intermine_voc:{json_doc['predicate'].replace('.id','_id')}> " |
|
ntriple3 = f"<http://bio2rdf.org/{json_doc['namespace']}:{json_doc['class_name_to']}:{str(json_doc['id_to'])}> .\n" |
|
|
|
ntriple = ntriple1 + ntriple2 + ntriple3 |
|
|
|
return ntriple |
|
|
|
|
|
|
|
def ndjson2nt(TYPE, NAMESPACE, FOLDER): |
|
file_nt_gz = gzip.open(f"{FOLDER}/{NAMESPACE}-{TYPE}.nt.gz2", "wb") |
|
|
|
ctr = 0 |
|
str_len = 0 |
|
|
|
|
|
for json_str in read_gz_by_row(f"{FOLDER}/{NAMESPACE}-{TYPE}.ndjson.gz"): |
|
ctr += 1 |
|
if ctr % 100000 == 0: |
|
print(NAMESPACE, TYPE, datetime.utcnow().isoformat(), ctr, str_len) |
|
|
|
json_dict = json.loads(json_str) |
|
json_doc = json_dict['_source'] |
|
doc_id = json_doc['doc_id'].replace(NAMESPACE+'-','').replace('-',':') |
|
|
|
|
|
if TYPE == 'object': |
|
ntriple_str = object2nt(json_doc) |
|
else: |
|
ntriple_str = relation2nt(json_doc) |
|
|
|
str_len = str_len + len(ntriple_str) |
|
|
|
file_nt_gz.write(ntriple_str.encode("utf-8")) |
|
|
|
file_nt_gz.close() |
|
print(NAMESPACE, TYPE, datetime.utcnow().isoformat(), ctr, str_len) |
|
|
|
|
|
|
|
|
|
NAMESPACE = 'flymine' |
|
FOLDER = './' |
|
|
|
TYPE = 'relation' |
|
TYPE = 'object' |
|
TYPE = sys.argv[1] |
|
NAMESPACE = sys.argv[2] |
|
FOLDER = sys.argv[3] |
|
|
|
try: |
|
ndjson2nt(TYPE, NAMESPACE, FOLDER) |
|
except KeyboardInterrupt: |
|
print("Stopping execution due to Ctrl+C...") |