libokj commited on
Commit
f5d6ae5
·
1 Parent(s): 3641f11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -16
app.py CHANGED
@@ -1,4 +1,3 @@
1
- import concurrent.futures
2
  import glob
3
  import smtplib
4
  from datetime import datetime, timedelta
@@ -7,6 +6,7 @@ import textwrap
7
  from email.mime.multipart import MIMEMultipart
8
  from email.mime.text import MIMEText
9
  from email.utils import formatdate, make_msgid
 
10
  from math import pi
11
  from time import sleep, time
12
  from uuid import uuid4
@@ -428,12 +428,45 @@ def rule_of_three(mol):
428
  return True
429
 
430
 
431
- # def smarts_filter():
432
- # alerts = Chem.MolFromSmarts("enter one smart here")
433
- # detected_alerts = []
434
- # for smiles in data['X1']:
435
- # mol = Chem.MolFromSmiles(smiles)
436
- # detected_alerts.append(mol.HasSubstructMatch(alerts))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
437
 
438
 
439
  SCORE_MAP = {
@@ -455,7 +488,11 @@ FILTER_MAP = {
455
  'Ghose': ghose,
456
  'Rule of Three': rule_of_three,
457
  'Veber': veber,
458
- # 'PAINS': pains,
 
 
 
 
459
  }
460
 
461
 
@@ -876,7 +913,7 @@ def create_html_report(df, file=None, task=None, progress=gr.Progress(track_tqdm
876
  if any(df_html.columns.isin(bool_cols)):
877
  styled_df.applymap(lambda val: f'background-color: {bool_col_colors[val]}', subset=bool_cols)
878
 
879
- table_html = styled_df.to_html()
880
  unique_html = ''
881
  if unique_df is not None:
882
  if 'Target FASTA' in unique_df.columns:
@@ -885,7 +922,7 @@ def create_html_report(df, file=None, task=None, progress=gr.Progress(track_tqdm
885
  unique_df = unique_df.style.applymap(
886
  lambda val: f"background-color: {bool_col_colors[val]}", subset=bool_cols)
887
  unique_html = (f'<div style="font-family: Courier !important;">'
888
- f'{unique_df.to_html(escape=False, index=False)}</div>')
889
 
890
  return (f'<div style="font-size: 16px; font-weight: bold;">{job} Report Preview (Top 30 Records)</div>'
891
  f'<div style="overflow-x:auto; font-family: Courier !important;">{unique_html}</div>'
@@ -1701,13 +1738,19 @@ with gr.Blocks(theme=theme, title='DeepSEQreen', css=CSS, delete_cache=(3600, 48
1701
  fasta = fasta_upload.decode()
1702
  except Exception as e:
1703
  gr.Warning(f"Please upload a valid FASTA file. Error: {str(e)}")
1704
- return fasta
1705
 
1706
 
1707
- target_upload_btn.upload(fn=process_fasta_upload, inputs=target_upload_btn, outputs=target_fasta)
1708
- target_query_btn.click(uniprot_query,
1709
- inputs=[target_input_type, target_id, target_gene, target_organism],
1710
- outputs=target_fasta)
 
 
 
 
 
 
1711
 
1712
 
1713
  def target_family_detect(fasta, progress=gr.Progress(track_tqdm=True)):
@@ -2080,6 +2123,7 @@ QALAHAYFAQYHDPDDEPVADPYDQSFESRDLLIDEWKSLTYDEVISFVPPPLDQEEMES
2080
  else:
2081
  raise gr.Error('System failed to create temporary files. Please try again later.')
2082
 
 
2083
  def fill_job_id(job_info):
2084
  try:
2085
  return job_info['id']
@@ -2261,7 +2305,7 @@ QALAHAYFAQYHDPDDEPVADPYDQSFESRDLLIDEWKSLTYDEVISFVPPPLDQEEMES
2261
  try:
2262
  now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
2263
  filename = f"/data/{Path(file_report.name).stem}_DeepSEQreen_report_{now}.csv"
2264
- df.drop(labels=['Compound', 'Scaffold'], axis=1).to_csv(filename, index=False)
2265
 
2266
  return gr.File(filename)
2267
  except Exception as e:
 
 
1
  import glob
2
  import smtplib
3
  from datetime import datetime, timedelta
 
6
  from email.mime.multipart import MIMEMultipart
7
  from email.mime.text import MIMEText
8
  from email.utils import formatdate, make_msgid
9
+ from functools import cache
10
  from math import pi
11
  from time import sleep, time
12
  from uuid import uuid4
 
428
  return True
429
 
430
 
431
+ @cache
432
+ def load_smarts_patterns(smarts_path):
433
+ # Load the CSV file containing SMARTS patterns
434
+ smarts_df = pd.read_csv(Path(smarts_path))
435
+ # Convert all SMARTS patterns to molecules
436
+ smarts_mols = [Chem.MolFromSmarts(smarts) for smarts in smarts_df['smarts']]
437
+ return smarts_mols
438
+
439
+
440
+ def smarts_filter(mol, smarts_mols):
441
+ for smarts_mol in smarts_mols:
442
+ if smarts_mol is not None and mol.HasSubstructMatch(smarts_mol):
443
+ return False
444
+ return True
445
+
446
+
447
+ def pains(mol):
448
+ smarts_mols = load_smarts_patterns("data/filters/pains.csv")
449
+ return smarts_filter(mol, smarts_mols)
450
+
451
+
452
+ def mlsmr(mol):
453
+ smarts_mols = load_smarts_patterns("data/filters/mlsmr.csv")
454
+ return smarts_filter(mol, smarts_mols)
455
+
456
+
457
+ def dundee(mol):
458
+ smarts_mols = load_smarts_patterns("data/filters/dundee.csv")
459
+ return smarts_filter(mol, smarts_mols)
460
+
461
+
462
+ def glaxo(mol):
463
+ smarts_mols = load_smarts_patterns("data/filters/glaxo.csv")
464
+ return smarts_filter(mol, smarts_mols)
465
+
466
+
467
+ def bms(mol):
468
+ smarts_mols = load_smarts_patterns("data/filters/bms.csv")
469
+ return smarts_filter(mol, smarts_mols)
470
 
471
 
472
  SCORE_MAP = {
 
488
  'Ghose': ghose,
489
  'Rule of Three': rule_of_three,
490
  'Veber': veber,
491
+ 'PAINS': pains,
492
+ 'MLSMR': mlsmr,
493
+ 'Dundee': dundee,
494
+ 'Glaxo': glaxo,
495
+ 'BMS': bms,
496
  }
497
 
498
 
 
913
  if any(df_html.columns.isin(bool_cols)):
914
  styled_df.applymap(lambda val: f'background-color: {bool_col_colors[val]}', subset=bool_cols)
915
 
916
+ table_html = styled_df.to_html(na_rep='')
917
  unique_html = ''
918
  if unique_df is not None:
919
  if 'Target FASTA' in unique_df.columns:
 
922
  unique_df = unique_df.style.applymap(
923
  lambda val: f"background-color: {bool_col_colors[val]}", subset=bool_cols)
924
  unique_html = (f'<div style="font-family: Courier !important;">'
925
+ f'{unique_df.to_html(escape=False, index=False, na_rep="")}</div>')
926
 
927
  return (f'<div style="font-size: 16px; font-weight: bold;">{job} Report Preview (Top 30 Records)</div>'
928
  f'<div style="overflow-x:auto; font-family: Courier !important;">{unique_html}</div>'
 
1738
  fasta = fasta_upload.decode()
1739
  except Exception as e:
1740
  gr.Warning(f"Please upload a valid FASTA file. Error: {str(e)}")
1741
+ return fasta
1742
 
1743
 
1744
+ target_upload_btn.upload(
1745
+ fn=process_fasta_upload, inputs=target_upload_btn, outputs=target_fasta
1746
+ ).then(
1747
+ fn=wrap_text, inputs=target_fasta, outputs=target_fasta, show_progress='hidden'
1748
+ )
1749
+ target_query_btn.click(
1750
+ fn=uniprot_query, inputs=[target_input_type, target_id, target_gene, target_organism], outputs=target_fasta
1751
+ ).then(
1752
+ fn=wrap_text, inputs=target_fasta, outputs=target_fasta, show_progress='hidden'
1753
+ )
1754
 
1755
 
1756
  def target_family_detect(fasta, progress=gr.Progress(track_tqdm=True)):
 
2123
  else:
2124
  raise gr.Error('System failed to create temporary files. Please try again later.')
2125
 
2126
+
2127
  def fill_job_id(job_info):
2128
  try:
2129
  return job_info['id']
 
2305
  try:
2306
  now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
2307
  filename = f"/data/{Path(file_report.name).stem}_DeepSEQreen_report_{now}.csv"
2308
+ df.drop(labels=['Compound', 'Scaffold'], axis=1).to_csv(filename, index=True, na_rep='')
2309
 
2310
  return gr.File(filename)
2311
  except Exception as e: