-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
notes: - disregard python code - important parts are EDAM files: - tests/EDAM_dev.owl -> raw EDAM file - tests/EDAM_dev.robot.owl -> raw EDAM file loaded/saved with robot - tests/edamontology.org.owl -> raw EDAM file loaded/saved with WebProtégé - tests/edamontology.org.robot.owl -> -> raw EDAM file loaded/saved with WebProtégé then robot
- Loading branch information
Showing
16 changed files
with
245,705 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import shutil | ||
import tempfile | ||
|
||
from edamfu.tree import reorder_root, add_comments | ||
|
||
from edamfu.utils import ( | ||
escape_irrelevant_xml_entities_in_text, | ||
unescape_irrelevant_xml_entities_in_text, | ||
prettify_xml, | ||
) | ||
|
||
|
||
def reformat(input_file_path, output_file_path): | ||
temp_edam_file = tempfile.NamedTemporaryFile(delete=False) | ||
temp_edam_file.close() | ||
shutil.copy2(input_file_path, temp_edam_file.name) | ||
escape_irrelevant_xml_entities_in_text(temp_edam_file.name) | ||
reorder_root(temp_edam_file.name) | ||
add_comments(temp_edam_file.name) | ||
unescape_irrelevant_xml_entities_in_text(temp_edam_file.name) | ||
prettify_xml(temp_edam_file.name) | ||
shutil.copy2(temp_edam_file.name, output_file_path) | ||
return output_file_path |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import shutil | ||
from edamfu.core import reformat | ||
|
||
# Input file: | ||
xml_file_path = ( | ||
"/home/hmenager/edamfu/tests/EDAM_dev.owl" # Replace with the path to your XML file | ||
) | ||
# Processed file | ||
sorted_path = "/home/hmenager/edamfu/tests/EDAM_dev.processed.owl" | ||
|
||
reformat(xml_file_path, sorted_path) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
from lxml import etree | ||
import re | ||
from copy import copy | ||
|
||
def get_element_sort_key(elem, order_mapping): | ||
first_key = order_mapping.get(elem.tag, {}).get("element_order", 0) | ||
if elem.tag == "{http://www.w3.org/2002/07/owl#}Class": | ||
secondary_key = elem.get("{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about") | ||
elif elem.tag == "{http://www.w3.org/2002/07/owl#}Axiom": | ||
annotated_source = elem.find("{http://www.w3.org/2002/07/owl#}annotatedSource") | ||
secondary_key = annotated_source.get("{http://www.w3.org/1999/02/22-rdf-syntax-ns#}resource") if annotated_source is not None else "zzz" | ||
else: | ||
secondary_key = "zzz" | ||
return (first_key, secondary_key) | ||
|
||
|
||
## A | ||
## C | ||
## C1 | ||
## C2 | ||
## B | ||
## B1 | ||
## B2 | ||
def reorder_elements(element, order_mapping): | ||
## element=A | ||
sorted_elements = sorted(element, key=lambda elem: get_element_sort_key(elem, order_mapping)) | ||
## element=A, sorted_elements=[B,C] | ||
new_element = etree.Element(element.tag, attrib=element.attrib) | ||
if element.text: | ||
new_element.text = etree.CDATA(element.text) | ||
for child in sorted_elements: | ||
child = reorder_elements(child, order_mapping) | ||
new_element.extend(sorted_elements) | ||
print(new_element.tag, len(new_element.getchildren())) | ||
ontology = new_element.iter("{http://www.w3.org/2002/07/owl#}Ontology") | ||
return new_element | ||
|
||
def add_comments(xml_file_path): | ||
tree = etree.parse(xml_file_path) | ||
root = tree.getroot() | ||
for class_element in root.findall("{http://www.w3.org/2002/07/owl#}*"): | ||
class_uri = class_element.get("{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about") | ||
root.insert(list(root).index(class_element), etree.Comment(f" {class_uri} ")) | ||
with open(xml_file_path, "wb") as f: | ||
tree.write(f, encoding="utf-8", xml_declaration=True) | ||
print(f"Comments added to '{xml_file_path}'.") | ||
|
||
|
||
def reorder_root(xml_file_path, order_mapping, namespaces): | ||
# etree.register_namespace(None, "http://edamontology.org/") | ||
# etree.register_namespace("dc", "http://purl.org/dc/elements/1.1/") | ||
# etree.register_namespace("dcterms", "http://purl.org/dc/terms/") | ||
# etree.register_namespace("owl", "http://www.w3.org/2002/07/owl#") | ||
# etree.register_namespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#") | ||
# etree.register_namespace("skos", "http://www.w3.org/2004/02/skos/core#") | ||
# etree.register_namespace("xml", "http://www.w3.org/XML/1998/namespace") | ||
# etree.register_namespace("xsd", "http://www.w3.org/2001/XMLSchema#") | ||
# etree.register_namespace("doap", "http://usefulinc.com/ns/doap#") | ||
# etree.register_namespace("foaf", "http://xmlns.com/foaf/0.1/") | ||
# etree.register_namespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#") | ||
# etree.register_namespace("oboInOwl", "http://www.geneontology.org/formats/oboInOwl#") | ||
# etree.register_namespace("oboLegacy", "http://purl.obolibrary.org/obo/") | ||
|
||
tree = etree.parse(xml_file_path, parser=etree.XMLParser(resolve_entities=False, remove_comments=True)) | ||
root = tree.getroot() | ||
etree.strip_elements(root, etree.Comment, with_tail=True) | ||
new_root = reorder_elements(root, order_mapping) | ||
ontology = new_root.iter("{http://www.w3.org/2002/07/owl#}Ontology") | ||
for el in ontology: | ||
print("##", el) | ||
|
||
#escape_special_characters(new_root) | ||
|
||
sorted_path = "/home/hmenager/edamfu/tests/EDAM_dev.sorted-lxml.owl" | ||
|
||
with open(sorted_path, "wb") as f: | ||
f.write(etree.tostring(new_root, xml_declaration=True)) | ||
|
||
print(f"XML elements reordered and saved to '{sorted_path}'.") | ||
return sorted_path | ||
|
||
def prettify_xml(file_path): | ||
with open(file_path, 'r') as file: | ||
file_content = file.read() | ||
modified_content = file_content.replace('" />', '"/>') | ||
modified_content = modified_content.replace('--><', '-->\n\n <') | ||
modified_content = modified_content.replace('>\n \n\n\n \n\n <!--', '>\n \n\n\n <!--') | ||
with open(file_path, 'w') as file: | ||
file.write(modified_content) | ||
print(f"XML prettified in '{file_path}'.") | ||
|
||
# Example usage: | ||
xml_file_path = "/home/hmenager/edamfu/tests/EDAM_dev.owl" # Replace with the path to your XML file | ||
|
||
order_mapping = { | ||
"{http://www.w3.org/2002/07/owl#}Ontology": { | ||
"element_order": 1, | ||
"attribute_order": {etree.QName("http://example.com/ns1", "attribute1"): 1}, | ||
}, | ||
"{http://www.w3.org/2002/07/owl#}AnnotationProperty": { | ||
"element_order": 2, | ||
"attribute_order": {etree.QName("http://example.com/ns1", "attribute1"): 1}, | ||
}, | ||
"{http://www.w3.org/2002/07/owl#}ObjectProperty": { | ||
"element_order": 3, | ||
"attribute_order": {etree.QName("http://example.com/ns1", "attribute1"): 1}, | ||
}, | ||
"{http://www.w3.org/2002/07/owl#}Axiom": { | ||
"element_order": 4, | ||
"attribute_order": {etree.QName("http://example.com/ns1", "attribute1"): 1}, | ||
}, | ||
"{http://www.w3.org/2002/07/owl#}Class": { | ||
"element_order": 4, | ||
"attribute_order": {etree.QName("http://example.com/ns1", "attribute1"): 1}, | ||
}, | ||
} | ||
|
||
namespaces = {"prefix1": "http://example.com/ns1", "prefix2": "http://example.com/ns2"} | ||
|
||
sorted_path = reorder_root(xml_file_path, order_mapping, namespaces) | ||
#add_comments(sorted_path) | ||
#prettify_xml(sorted_path) |
Oops, something went wrong.