|
8 | 8 | "source": [ |
9 | 9 | "import json\n", |
10 | 10 | "import random\n", |
| 11 | + "\n", |
11 | 12 | "from pronto import Ontology, Definition\n", |
12 | 13 | "\n", |
13 | | - "class ImpatientVocab():\n", |
| 14 | + "\n", |
| 15 | + "class ImpatientVocab:\n", |
14 | 16 | " def __init__(self) -> None:\n", |
15 | 17 | " self.used_colors: list[str] = []\n", |
16 | 18 | " self.impatient_json: list[dict] = []\n", |
|
20 | 22 | " def load_json(self, path: str) -> list[dict]:\n", |
21 | 23 | " self.impatient_json = json.load(open(path, \"r\"))\n", |
22 | 24 | " return self.impatient_json\n", |
23 | | - " \n", |
| 25 | + "\n", |
24 | 26 | " def load_ontology(self, path: str) -> Ontology:\n", |
25 | 27 | " self.impatient_onto = Ontology(path)\n", |
26 | 28 | " return self.impatient_onto\n", |
27 | | - " \n", |
| 29 | + "\n", |
28 | 30 | " def json_to_onto(self) -> Ontology:\n", |
29 | 31 | " self.impatient_onto = Ontology()\n", |
30 | 32 | " for term in self.impatient_json:\n", |
|
36 | 38 | " if term[\"data\"][\"description\"] != \"\":\n", |
37 | 39 | " added_term.definition = Definition(term[\"data\"][\"description\"])\n", |
38 | 40 | " if term[\"parent\"] != \"#\":\n", |
39 | | - " added_term.superclasses().add(self.impatient_onto[term[\"parent\"].replace(\"_\", \":\")])\n", |
40 | | - " \n", |
| 41 | + " added_term.superclasses().add(\n", |
| 42 | + " self.impatient_onto[term[\"parent\"].replace(\"_\", \":\")]\n", |
| 43 | + " )\n", |
| 44 | + "\n", |
41 | 45 | " self.list_of_terms.append(added_term)\n", |
42 | 46 | " return self.impatient_onto\n", |
43 | | - " \n", |
| 47 | + "\n", |
44 | 48 | " def onto_to_json(self) -> list[dict]:\n", |
45 | 49 | " self.impatient_json = []\n", |
46 | 50 | " index = 0\n", |
|
55 | 59 | " \"text\": term.name,\n", |
56 | 60 | " \"icon\": True,\n", |
57 | 61 | " \"data\": {\n", |
58 | | - " \"description\": term.definition if term.definition is not None else \"\",\n", |
59 | | - " \"synonymes\": \",\".join([syn.description for syn in term.synonyms]),\n", |
| 62 | + " \"description\": term.definition\n", |
| 63 | + " if term.definition is not None\n", |
| 64 | + " else \"\",\n", |
| 65 | + " \"synonymes\": \",\".join(\n", |
| 66 | + " [syn.description for syn in term.synonyms]\n", |
| 67 | + " ),\n", |
60 | 68 | " \"phenotype_datamined\": \"\",\n", |
61 | 69 | " \"gene_datamined\": \"\",\n", |
62 | 70 | " \"alternative_language\": term.name,\n", |
|
65 | 73 | " \"hex_color\": self._generate_hex_color(),\n", |
66 | 74 | " \"hpo_datamined\": \"\",\n", |
67 | 75 | " },\n", |
68 | | - " \"parent\": relationships[0].replace(\"_\", \":\") if relationships != [] else \"#\"\n", |
| 76 | + " \"parent\": relationships[0].replace(\"_\", \":\")\n", |
| 77 | + " if relationships != []\n", |
| 78 | + " else \"#\",\n", |
69 | 79 | " }\n", |
70 | 80 | " )\n", |
71 | 81 | " index += 1\n", |
72 | 82 | " return self.impatient_json\n", |
73 | | - " \n", |
| 83 | + "\n", |
74 | 84 | " def _generate_hex_color(self):\n", |
75 | 85 | " while True:\n", |
76 | 86 | " # Generate a random hex color\n", |
|
80 | 90 | " # Add the color to the list of used colors and return it\n", |
81 | 91 | " self.used_colors.append(color)\n", |
82 | 92 | " return color\n", |
83 | | - " \n", |
| 93 | + "\n", |
84 | 94 | " def dump_onto(self, path: str) -> None:\n", |
85 | 95 | " with open(path, \"wb\") as f:\n", |
86 | 96 | " self.impatient_onto.dump(f, format=\"obo\")\n", |
|
0 commit comments