|
1 | 1 | import json |
| 2 | +import random |
| 3 | +from pronto import Ontology, Definition |
| 4 | +import io |
| 5 | +from flask_wtf.file import FileField |
| 6 | + |
| 7 | + |
| 8 | +class ImpatientVocab: |
| 9 | + def __init__(self) -> None: |
| 10 | + self.used_colors: list[str] = [] |
| 11 | + self.impatient_json: list[dict] = [] |
| 12 | + self.impatient_onto: Ontology = None |
| 13 | + self.list_of_terms: list[str] = [] |
| 14 | + |
| 15 | + def load_json(self, path: str) -> list[dict]: |
| 16 | + self.impatient_json = json.load(open(path, "r")) |
| 17 | + return self.impatient_json |
| 18 | + |
| 19 | + def load_ontology(self, path: str) -> Ontology: |
| 20 | + self.impatient_onto = Ontology(path) |
| 21 | + return self.impatient_onto |
| 22 | + |
| 23 | + def load_json_f(self, file: FileField) -> list[dict]: |
| 24 | + # Read the JSON data from the file object |
| 25 | + json_data = json.loads(file.read()) |
| 26 | + self.impatient_json = json_data |
| 27 | + return json_data |
| 28 | + |
| 29 | + def load_ontology_f(self, file: FileField) -> Ontology: |
| 30 | + # Read the ontology data from the file object |
| 31 | + ontology_data = io.BytesIO(file.read()) |
| 32 | + ontology = Ontology(ontology_data) |
| 33 | + self.impatient_onto = ontology |
| 34 | + return ontology |
| 35 | + |
| 36 | + def json_to_onto(self) -> Ontology: |
| 37 | + self.impatient_onto = Ontology() |
| 38 | + term_mapping = ( |
| 39 | + {} |
| 40 | + ) # A dictionary to store term IDs and their corresponding created terms |
| 41 | + |
| 42 | + # First pass: Create terms without adding superclasses |
| 43 | + for term in self.impatient_json: |
| 44 | + term_id = term["id"].replace("_", ":") |
| 45 | + added_term = self.impatient_onto.create_term(term_id) |
| 46 | + added_term.name = term["text"] |
| 47 | + for syn in term["data"]["synonymes"].split(","): |
| 48 | + if syn.strip() != "": |
| 49 | + added_term.add_synonym(syn.strip(), scope="EXACT") |
| 50 | + if term["data"]["description"] != "": |
| 51 | + added_term.definition = Definition(term["data"]["description"]) |
| 52 | + |
| 53 | + term_mapping[term_id] = added_term # Store the term in the mapping |
| 54 | + |
| 55 | + # Second pass: Add superclasses |
| 56 | + for term in self.impatient_json: |
| 57 | + term_id = term["id"].replace("_", ":") |
| 58 | + added_term = term_mapping[term_id] |
| 59 | + |
| 60 | + if term["parent"] != "#": |
| 61 | + parent_id = term["parent"].replace("_", ":") |
| 62 | + parent_term = term_mapping.get(parent_id) |
| 63 | + if parent_term: |
| 64 | + added_term.superclasses().add(parent_term) |
| 65 | + |
| 66 | + self.list_of_terms.append(added_term) |
| 67 | + |
| 68 | + return self.impatient_onto |
| 69 | + |
| 70 | + def onto_to_json(self) -> list[dict]: |
| 71 | + self.impatient_json = [] |
| 72 | + index = 0 |
| 73 | + for term in self.impatient_onto.terms(): |
| 74 | + relationships = [] |
| 75 | + for rel in term.superclasses(): |
| 76 | + relationships.append(rel.id) |
| 77 | + relationships.pop(0) |
| 78 | + self.impatient_json.append( |
| 79 | + { |
| 80 | + "id": term.id.replace("_", ":"), |
| 81 | + "text": term.name if term.name is not None else "", |
| 82 | + "icon": True, |
| 83 | + "data": { |
| 84 | + "description": term.definition |
| 85 | + if term.definition is not None |
| 86 | + else "", |
| 87 | + "synonymes": ",".join( |
| 88 | + [syn.description for syn in term.synonyms] |
| 89 | + ), |
| 90 | + "phenotype_datamined": "", |
| 91 | + "gene_datamined": "", |
| 92 | + "alternative_language": term.name |
| 93 | + if term.name is not None |
| 94 | + else "", |
| 95 | + "correlates_with": "", |
| 96 | + "image_annotation": True if index == 0 else False, |
| 97 | + "hex_color": self._generate_hex_color(), |
| 98 | + "hpo_datamined": "", |
| 99 | + }, |
| 100 | + "parent": relationships[0].replace("_", ":") |
| 101 | + if relationships != [] |
| 102 | + else "#", |
| 103 | + } |
| 104 | + ) |
| 105 | + index += 1 |
| 106 | + return self.impatient_json |
| 107 | + |
| 108 | + def _generate_hex_color(self): |
| 109 | + while True: |
| 110 | + # Generate a random hex color |
| 111 | + color = "#{:06x}".format(random.randint(0, 0xFFFFFF)) |
| 112 | + # Check if the color has already been used |
| 113 | + if color not in self.used_colors: |
| 114 | + # Add the color to the list of used colors and return it |
| 115 | + self.used_colors.append(color) |
| 116 | + return color |
| 117 | + |
| 118 | + def dump_onto(self, path: str) -> None: |
| 119 | + with open(path, "wb") as f: |
| 120 | + self.impatient_onto.dump(f, format="obo") |
| 121 | + |
| 122 | + def dump_json(self, path: str) -> None: |
| 123 | + with open(path, "w") as f: |
| 124 | + json.dump(self.impatient_json, f, indent=2) |
2 | 125 |
|
3 | 126 |
|
4 | 127 | class StandardVocabulary: |
|
0 commit comments