-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcellosaurus_db.py
356 lines (298 loc) · 11.7 KB
/
cellosaurus_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import gzip
import re
from typing import Union, List, Dict, Tuple, Optional
import click
import numpy as np
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
def read_obo_file(file_path: str) -> Dict[str, dict]:
"""
Reads an OBO file and returns a dictionary with the parsed content.
Parameters:
file_path (str): Path to the OBO file.
Returns:
Dict[str, dict]: A dictionary where keys are OBO term IDs and values are dictionaries of term attributes.
"""
with open(file_path, "r") as file:
content = file.read()
entries = content.split("\n\n")
def parse_obo_term(entry: str) -> dict:
obo_dict = {}
lines = entry.strip().split("\n")
for line in lines:
if line.startswith("id:"):
obo_dict["id"] = line.split("id: ")[1].strip()
elif line.startswith("name:"):
obo_dict["name"] = line.split("name: ")[1].strip()
elif line.startswith("def:"):
obo_dict["def"] = line.split("def: ")[1].strip()
elif line.startswith("is_a:"):
obo_dict.setdefault("is_a", []).append(line.split("is_a: ")[1].strip())
elif line.startswith("synonym:"):
obo_dict.setdefault("synonyms", []).append(
line.split("synonym: ")[1].strip()
)
obo_dict["synonyms"] = [
synonym.replace("RELATED []", "")
.replace("RELATED MS []", "")
.strip()
.strip('"')
for synonym in obo_dict["synonyms"]
]
obo_dict.setdefault("synonyms", [])
return obo_dict
return {
parse_obo_term(entry)["id"]: parse_obo_term(entry)
for entry in entries
if entry.strip() and "id:" in entry
}
def string_if_not_empty(param: List[Union[str, float]]) -> str:
"""
Returns a string if the list is not empty, otherwise returns 'not available'.
Parameters:
param (List[Union[str, float]]): List of strings or floats.
Returns:
str: Concatenated string of list elements or 'not available'.
"""
if param == "None":
param = []
if param:
l = [
x
for x in param
if (isinstance(x, float) and not np.isnan(x))
or (not isinstance(x, float) and x is not None)
]
return "; ".join(l)
return "not available"
def write_database_cellosaurus(current_cl_database: List[dict], database: str) -> None:
"""
Writes the database objects to the specified database file.
Parameters:
current_cl_database (List[dict]): Current cell line database list.
database (str): Path to the database file.
"""
headers = [
"cellosaurus name",
"cellosaurus accession",
"bto cell line",
"organism",
"age",
"developmental stage",
"sex",
"ancestry category",
"disease",
"cell type",
"sampling site",
"synonyms",
]
with open(database, "w") as file:
file.write("\t".join(headers) + "\n")
for entry in current_cl_database:
row = [entry.get(header, "not available") for header in headers[:-1]] + [
string_if_not_empty(entry.get("synonyms", []))
]
file.write("\t".join(row) + "\n")
def is_age_in_text(age_text: str) -> bool:
"""
Checks if the age field contains a number.
Parameters:
age_text (str): Age text to check.
Returns:
bool: True if the age is a number, False otherwise.
"""
return any(char.isdigit() for char in age_text)
def get_sampling_site(cellosaurus_comment: str) -> Optional[str]:
"""
Extracts the sampling site from a Cellosaurus comment string.
Parameters:
cellosaurus_comment (str): The comment string from which to extract the sampling site information.
Returns:
Optional[str]: The tissue sampling site if found, otherwise None.
"""
pattern = r"Derived from site:\s*(.+?);\s*(.+?);\s*UBERON=(UBERON_\d{7})"
match = re.search(pattern, cellosaurus_comment)
return match.group(2) if match else None
def get_cell_type(cellosaurus_comment: str) -> Optional[str]:
"""
Extracts the cell type from the Cellosaurus comment field.
Parameters:
cellosaurus_comment (str): Cellosaurus comment field.
Returns:
Optional[str]: Cell type if found, otherwise None.
"""
pattern = r"Cell type:\s*(.+?);\s*CL=(CL_\d{7})"
match = re.search(pattern, cellosaurus_comment)
return match.group(2).replace("_", ":").strip() if match else None
def parse_cellosaurus_taxonomy(
organism_text: str,
) -> Tuple[Optional[str], Optional[str]]:
"""
Parses the organism text from the Cellosaurus database.
Parameters:
organism_text (str): Organism text from the Cellosaurus database.
Returns:
Tuple[Optional[str], Optional[str]]: Species name and taxonomy ID if found, otherwise (None, None).
"""
pattern = r"NCBI_TaxID=(\d+); ! ([\w\s]+) \(([\w\s]+)\)"
match = re.search(pattern, organism_text)
return (match.group(2), match.group(1)) if match else (None, None)
def parse_cellosaurus_file(
file_path: str, bto: Dict[str, dict], cl_type: Dict[str, dict]
) -> List[dict]:
"""
Parses the CelloSaurus file and returns a list of dictionaries with the parsed content.
Parameters:
file_path (str): CelloSaurus file path.
bto (Dict[str, dict]): BTO ontology dictionary.
cl_type (Dict[str, dict]): Cell type ontology dictionary.
Returns:
List[dict]: List of CelloSaurus dictionaries.
"""
def parse_entry(entry: str, bto: Dict[str, dict], cl_type: Dict[str, dict]) -> dict:
data = {
"cellosaurus name": "not available",
"cellosaurus accession": "not available",
"bto cell line": "not available",
"efo": "not available",
"organism": "not available",
"age": "not available",
"developmental stage": "not available",
"sex": "not available",
"ancestry category": "not available",
"disease": "not available",
"cell type": "not available",
"sampling site": "not available",
"synonyms": [],
}
lines = entry.strip().split("\n")
for line in lines:
if line.startswith("ID"):
data["cellosaurus name"] = line.split("ID ")[1].strip()
elif line.startswith("AC"):
data["cellosaurus accession"] = line.split("AC ")[1].strip()
elif line.startswith("SY"):
data["synonyms"] = line.split("SY ")[1].strip().split("; ")
elif line.startswith("DR BTO"):
bto_accession = line.split("; ")[1]
if bto_accession in bto:
data["bto cell line"] = bto[bto_accession]["name"]
data["synonyms"].extend(bto[bto_accession].get("synonyms", []))
elif line.startswith("DR EFO"):
data["efo"] = line.split("; ")[1]
elif line.startswith("OX"):
data["organism"] = line.split("OX ")[1].strip()
scientific_name, tax = parse_cellosaurus_taxonomy(data["organism"])
data["organism"] = scientific_name
elif line.startswith("SX"):
data["sex"] = line.split()[1]
elif line.startswith("AG"):
data["age"] = line.split("AG ")[1].strip()
elif line.startswith("CC") and "Population" in line:
data["ancestry category"] = line.split(": ")[1].strip().replace(".", "")
elif line.startswith("DI"):
if "NCIt" in line:
pattern = r"NCIt;\s*C\d+;\s*([^;]+)"
match = re.search(pattern, line)
if match:
data["disease"] = match.group(1).strip()
elif line.startswith("CC") and "Derived from site" in line:
data["sampling site"] = get_sampling_site(line)
elif line.startswith("CC") and "Cell type" in line:
code_cl = get_cell_type(line)
if code_cl and code_cl in cl_type:
data["cell type"] = cl_type[code_cl]["name"]
return data
with gzip.open(file_path, "r") as file:
content = file.read().decode("utf-8")
entries = content.split("//\n")
parsed_data = [
parse_entry(entry, bto, cl_type) for entry in entries if entry.strip()
]
return [entry for entry in parsed_data if entry]
def create_new_entry_from_cellosaurus(cellosaurus: dict) -> dict:
"""
Creates a new entry for a cell line not found in the database.
Parameters:
cellosaurus (dict): Dictionary containing cellosaurus data.
Returns:
dict: New entry dictionary.
"""
entry = {
"cellosaurus name": cellosaurus.get("cellosaurus name", "not available"),
"bto cell line": cellosaurus.get("bto cell line"),
"cellosaurus accession": cellosaurus.get("cellosaurus accession"),
"organism": cellosaurus.get("organism"),
"organism part": None,
"age": None,
"developmental stage": None,
"sex": cellosaurus.get("sex"),
"ancestry category": cellosaurus.get("ancestry category"),
"disease": cellosaurus.get("disease"),
"synonyms": cellosaurus.get("synonyms", []),
"cell type": cellosaurus.get("cell type"),
"sampling site": cellosaurus.get("sampling site"),
}
if "age" in cellosaurus:
if is_age_in_text(cellosaurus["age"]):
entry["age"] = cellosaurus["age"]
else:
entry["developmental stage"] = cellosaurus["age"]
return entry
@click.command(
"cellosaurus-database",
short_help="Create the cellosaurus database from the cellosaurus file",
)
@click.option(
"--cellosaurus",
help="CelloSaurus database file, the file is gzipped",
required=True,
type=click.Path(exists=True),
)
@click.option(
"--output", help="Output file with the cellosaurus database", required=True
)
@click.option(
"--bto", help="BTO ontology file", required=True, type=click.Path(exists=True)
)
@click.option(
"--cl", help="Cell type ontology file", required=True, type=click.Path(exists=True)
)
@click.option(
"--filter-species", help="Include only the following species", required=False
)
def cellosaurus_db(
cellosaurus: str, output: str, bto: str, cl: str, filter_species: Optional[str]
) -> None:
"""
Creates a CelloSaurus database from the cellosaurus file, mapping to BTO and parsing organisms, diseases, etc.
Parameters:
cellosaurus (str): CelloSaurus database file.
output (str): Output file with the cellosaurus database.
bto (str): BTO ontology file.
cl (str): Cell type ontology file.
filter_species (Optional[str]): Comma-separated list of species to include.
"""
bto_data = read_obo_file(bto)
cl_type_data = read_obo_file(cl)
cellosaurus_list = parse_cellosaurus_file(cellosaurus, bto_data, cl_type_data)
if filter_species:
filter_species_list = filter_species.split(",")
cellosaurus_list = [
entry
for entry in cellosaurus_list
if entry["organism"] in filter_species_list
]
current_cl_database = [
create_new_entry_from_cellosaurus(cellosaurus_cl)
for cellosaurus_cl in cellosaurus_list
]
write_database_cellosaurus(current_cl_database, output)
@click.group(context_settings=CONTEXT_SETTINGS)
def cli():
"""
Main function to run the CLI.
"""
pass
cli.add_command(cellosaurus_db)
if __name__ == "__main__":
cli()