-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
344 lines (272 loc) · 12.5 KB
/
search.py
File metadata and controls
344 lines (272 loc) · 12.5 KB
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
import asyncio
import traceback
from nicegui import ui
import config
from uniprot import fetch_taxonomy, fetch_uniprot_data, fetch_rank
from ncbi import fetch_ncbi_proteins, fetch_ncbi_genes
current_search_task = None
async def search_genes(gene_name, taxonomy_name, selected_rank):
global current_search_task
if not gene_name:
ui.notify('Please enter a gene name.')
return {'success': False, 'error': 'No gene name provided'}
config.current_search_type = 'gene'
start_search()
try:
current_search_task = asyncio.current_task()
config.search_params['uniprot'] = False
config.search_params['ncbi'] = True
config.search_params['term'] = gene_name
gene_rank_dict = {}
loop = asyncio.get_event_loop()
ui.notify('Searching in NCBI...', color='info')
taxo = await loop.run_in_executor(None, fetch_taxonomy, taxonomy_name) if taxonomy_name else None
taxid = taxo['taxid'] if taxo else None
config.search_params['taxid'] = taxid
# Fetch genes from NCBI
ncbi_genes = await loop.run_in_executor(None, fetch_ncbi_genes, gene_name, taxid)
ncbi_genes_correct_rank = await update_taxonomic_rank(
ncbi_genes, gene_rank_dict, selected_rank, 'taxid', 'scientific_name'
)
print("NCBI search completed.")
config.ncbi_genes = ncbi_genes_correct_rank
config.selected_data = config.ncbi_genes
# Count species
ncbi_species_count = count_species(config.ncbi_genes, 'taxid')
# Display results
with config.table_container:
ui.label('Search Results').classes(f'text-2xl font-bold text-[{config.VIOLET_COLOR}]')
search_results_text = f'Search results for "{gene_name}"'
if taxonomy_name:
search_results_text += f' in taxonomy "{taxonomy_name}"'
ui.markdown(search_results_text)
ui.markdown(
f'Found **{len(config.ncbi_genes)}** entries '
f'in **{ncbi_species_count}** species '
)
# Finish search successfully
finish_search(success=True)
return {
'success': True,
'gene_name': gene_name,
'taxonomy_name': taxonomy_name,
'ncbi_genes': ncbi_genes_correct_rank,
'ncbi_species_count': ncbi_species_count,
'total_species': ncbi_species_count
}
except asyncio.CancelledError:
# Search was cancelled, reset state
reset_search_state()
print("Gene search was cancelled")
return {'success': False, 'error': 'Search was cancelled'}
except Exception as e:
# Print full traceback to console for debugging
print("Full traceback:")
print(traceback.format_exc())
# Display error in UI
config.table_container.clear()
error_message = f"**Error:** {str(e)}\n\n**Traceback:**\n```\n{traceback.format_exc()}\n```"
with config.table_container:
ui.markdown(error_message)
# Finish search with error
finish_search(success=True) # Show table with error message
return {'success': False, 'error': str(e)}
async def search_protein(protein_name, taxonomy_name, selected_rank):
global current_search_task
if not protein_name:
ui.notify('Please enter a protein name.')
return {'success': False, 'error': 'No protein name provided'}
# Set search type for unified selection system
config.current_search_type = 'protein'
# Start new search
start_search()
try:
current_search_task = asyncio.current_task()
config.search_params['uniprot'] = True
config.search_params['ncbi'] = True
config.search_params['term'] = protein_name
protein_rank_dict = {}
loop = asyncio.get_event_loop()
# Get taxonomy if specified
taxo = await loop.run_in_executor(None, fetch_taxonomy, taxonomy_name) if taxonomy_name else None
taxid = taxo['taxid'] if taxo else None
config.search_params['taxid'] = taxid
# Search in UniProt
ui.notify('Searching in UniProtKB...', color='info')
uniprot_proteins = await loop.run_in_executor(None, fetch_uniprot_data, protein_name, taxid)
# Update taxonomic ranks for UniProt proteins
uniprot_proteins_correct_rank = await update_taxonomic_rank(
uniprot_proteins, protein_rank_dict, selected_rank, 'organism.taxonId', 'organism.scientificName'
)
# Add mRNA information for UniProt proteins
for prot in uniprot_proteins_correct_rank:
original_crossrefs = prot.get('uniProtKBCrossReferences', [])
nucleotide_ref = extract_nucleotide_reference(original_crossrefs)
prot['mRNA'] = nucleotide_ref
print("UniProt search completed.")
# Search in NCBI
ui.notify('Searching in NCBI...', color='info')
ncbi_proteins = await loop.run_in_executor(None, fetch_ncbi_proteins, protein_name, taxid)
# Update taxonomic ranks for NCBI proteins
ncbi_proteins_correct_rank = await update_taxonomic_rank(
ncbi_proteins, protein_rank_dict, selected_rank, 'taxid', 'scientific_name'
)
print("NCBI search completed.")
# Store results in config
config.uniprot_proteins = uniprot_proteins_correct_rank
config.ncbi_proteins = ncbi_proteins_correct_rank
config.all_proteins = uniprot_proteins_correct_rank + ncbi_proteins_correct_rank
config.selected_data = config.all_proteins
# Count species
uniprot_species_count = count_species(config.uniprot_proteins, 'organism.taxonId')
ncbi_species_count = count_species(config.ncbi_proteins, 'taxid')
# Count total unique species
all_taxids = set()
for protein in config.all_proteins:
taxid = protein.get('organism', {}).get('taxonId') or protein.get('taxid', 'Unknown')
all_taxids.add(taxid)
total_species = len(all_taxids)
# Display results
with config.table_container:
ui.label('Search Results').classes(f'text-2xl font-bold text-[{config.VIOLET_COLOR}]')
search_results_text = f'Search results for "{protein_name}"'
if taxonomy_name:
search_results_text += f' in taxonomy "{taxonomy_name}"'
ui.markdown(search_results_text)
ui.markdown(
f'Found **{len(config.uniprot_proteins)}** UniProtKB entries '
f'in **{uniprot_species_count}** species and '
f'**{len(config.ncbi_proteins)}** NCBI entries '
f'in **{ncbi_species_count}** species '
f'(Total: **{len(config.all_proteins)}** '
f'in **{total_species}** unique species)'
)
# Finish search successfully
finish_search(success=True)
return {
'success': True,
'protein_name': protein_name,
'taxonomy_name': taxonomy_name,
'uniprot_proteins': uniprot_proteins_correct_rank,
'ncbi_proteins': ncbi_proteins_correct_rank,
'uniprot_species_count': uniprot_species_count,
'ncbi_species_count': ncbi_species_count,
'total_species': total_species
}
except asyncio.CancelledError:
# Search was cancelled, reset state
reset_search_state()
print("Protein search was cancelled")
return {'success': False, 'error': 'Search was cancelled'}
except Exception as e:
# Print full traceback to console for debugging
print("Full traceback:")
print(traceback.format_exc())
# Display error in UI
config.table_container.clear()
error_message = f"**Error:** {str(e)}\n\n**Traceback:**\n```\n{traceback.format_exc()}\n```"
with config.table_container:
ui.markdown(error_message)
# Finish search with error
finish_search(success=True) # Show table with error message
return {'success': False, 'error': str(e)}
def reset_search_state():
config.table_container.clear()
config.table_container.set_visibility(False)
config.sequence_selection_container.clear()
config.sequence_selection_container.set_visibility(False)
config.length_distribution_container.clear()
config.length_distribution_container.set_visibility(False)
config.uniprot_proteins = []
config.ncbi_proteins = []
config.ncbi_genes = []
config.all_proteins = []
config.loading_spinner.set_visibility(False)
def start_search():
global current_search_task
# Cancel any ongoing search
if current_search_task and not current_search_task.done():
current_search_task.cancel()
reset_search_state()
config.loading_spinner.set_visibility(True)
def finish_search(success=True):
config.loading_spinner.set_visibility(False)
if success:
config.table_container.set_visibility(True)
def extract_nucleotide_reference(cross_references):
if not cross_references:
return None
refseq_ref = None
mrna_ref = None
for ref in cross_references:
database = ref.get('database', '')
ref_id = ref.get('id', '')
if database == 'RefSeq':
properties = ref.get('properties', [])
for prop in properties:
if prop.get('key') == 'NucleotideSequenceId':
refseq_ref = prop.get('value')
elif database == 'EMBL':
properties = ref.get('properties', [])
for prop in properties:
if prop.get('key') == 'MoleculeType' and prop.get('value') == 'mRNA':
mrna_ref = ref_id
break
return refseq_ref or mrna_ref
async def update_taxonomic_rank(items, rank_dict, selected_rank, taxid_key, name_key):
loop = asyncio.get_event_loop()
processed_items = []
for item in items:
if '.' in taxid_key:
keys = taxid_key.split('.')
taxid = item
for key in keys:
taxid = taxid.get(key) if isinstance(taxid, dict) else None
else:
taxid = item.get(taxid_key)
if '.' in name_key:
keys = name_key.split('.')
scientific_name = item
for key in keys:
scientific_name = scientific_name.get(key) if isinstance(scientific_name, dict) else None
else:
scientific_name = item.get(name_key)
# Only process rank if the scientific name has more than 2 words (not species level)
if scientific_name and scientific_name.count(' ') > 1:
if taxid in rank_dict:
updated_taxid, updated_scientific_name = rank_dict[taxid]
else:
updated_taxid, updated_scientific_name = await loop.run_in_executor(None, fetch_rank, taxid, selected_rank)
rank_dict[taxid] = (updated_taxid, updated_scientific_name)
if not updated_taxid:
continue
if '.' in taxid_key:
keys = taxid_key.split('.')
target = item
for key in keys[:-1]:
target = target[key]
target[keys[-1]] = updated_taxid
else:
item[taxid_key] = updated_taxid
if '.' in name_key:
keys = name_key.split('.')
target = item
for key in keys[:-1]:
target = target[key]
target[keys[-1]] = updated_scientific_name
else:
item[name_key] = updated_scientific_name
processed_items.append(item)
return processed_items
def count_species(items, taxid_key):
taxids = set()
for item in items:
if '.' in taxid_key:
keys = taxid_key.split('.')
taxid = item
for key in keys:
taxid = taxid.get(key) if isinstance(taxid, dict) else None
else:
taxid = item.get(taxid_key)
taxids.add(taxid if taxid else 'Unknown')
return len(taxids)