libokj commited on
Commit
9321db6
·
verified ·
1 Parent(s): 65b3da3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -29
app.py CHANGED
@@ -43,7 +43,7 @@ import panel as pn
43
  from apscheduler.schedulers.background import BackgroundScheduler
44
  from tinydb import TinyDB, Query
45
 
46
- # import swifter
47
  from tqdm.auto import tqdm
48
 
49
  from deepscreen.data.dti import validate_seq_str, rdkit_canonicalize, FASTA_PAT, SMILES_PAT
@@ -786,7 +786,7 @@ def submit_predict(predict_filepath, task, preset, target_family, opts, job_info
786
  orig_df['Target Family'] = None
787
  if orig_df['Target Family'].isna().any():
788
  orig_df.loc[orig_df['Target Family'].isna(), 'Target Family'] = (
789
- orig_df.loc[orig_df['Target Family'].isna(), 'X2'].parallel_apply(detect_family)
790
  )
791
  detect_family.cache_clear()
792
 
@@ -885,7 +885,7 @@ def submit_predict(predict_filepath, task, preset, target_family, opts, job_info
885
  if "Include Compound Max. Tanimoto Similarity to Training Compounds" in opts:
886
  for family in prediction_df['Target Family'].unique():
887
  family_smiles_df = get_seen_smiles(family=family, task=task_value)
888
- family_smiles_df['FP'] = family_smiles_df['X1'].parallel_apply(smiles_to_ecfp)
889
 
890
  @cache
891
  def max_sim(smi):
@@ -893,7 +893,7 @@ def submit_predict(predict_filepath, task, preset, target_family, opts, job_info
893
 
894
  prediction_df.loc[
895
  prediction_df['Target Family'] == family, 'Max. Tanimoto Similarity to Training Compounds'] = (
896
- prediction_df.loc[prediction_df['Target Family'] == family, 'X1'].parallel_apply(max_sim)
897
  )
898
  max_sim.cache_clear()
899
 
@@ -907,13 +907,13 @@ def submit_predict(predict_filepath, task, preset, target_family, opts, job_info
907
  return max_tanimoto_similarity(smiles, seen_smiles_with_fp=pos_compounds_df)
908
 
909
  prediction_df[['Max. Tanimoto Similarity', 'Max. Tanimoto Similarity Compound']] = (
910
- prediction_df['X1'].parallel_apply(max_sim).apply(pd.Series)
911
  )
912
  max_sim.cache_clear()
913
 
914
  if "Include Target Max. Sequence Identity to Known Interacting Targets of Compound" in opts:
915
  x2 = prediction_df['X2'].iloc[0]
916
- prediction_df['X1^'] = prediction_df['X1'].parallel_apply(rdkit_canonicalize)
917
 
918
  @cache
919
  def calculate_max_sequence_identity(compound):
@@ -921,7 +921,7 @@ def submit_predict(predict_filepath, task, preset, target_family, opts, job_info
921
  return max_sequence_identity(x2, seen_fastas=compound_targets)
922
 
923
  prediction_df[['Max. Sequence Identity', 'Max. Sequence Identity Target']] = (
924
- prediction_df['X1^'].parallel_apply(calculate_max_sequence_identity).apply(pd.Series)
925
  )
926
  prediction_df.drop(['X1^'], axis=1, inplace=True)
927
 
@@ -937,7 +937,7 @@ def submit_predict(predict_filepath, task, preset, target_family, opts, job_info
937
 
938
  prediction_df.loc[
939
  prediction_df['Target Family'] == family, 'Max. Sequence Identity to Training Targets'] = (
940
- prediction_df.loc[prediction_df['Target Family'] == family, 'X2'].parallel_apply(max_id)
941
  )
942
  max_id.cache_clear()
943
 
@@ -991,10 +991,10 @@ def update_df(file, progress=gr.Progress(track_tqdm=True)):
991
 
992
  if 'X1' in df.columns:
993
  if 'Compound' not in df.columns or df['Compound'].dtype != 'object':
994
- df['Compound'] = df['X1'].parallel_apply(
995
  lambda smiles: PandasTools._MolPlusFingerprint(Chem.MolFromSmiles(smiles)))
996
- df['Scaffold'] = df['Compound'].parallel_apply(MurckoScaffold.GetScaffoldForMol)
997
- df['Scaffold SMILES'] = df['Scaffold'].parallel_apply(lambda x: Chem.MolToSmiles(x))
998
 
999
  if task == 'Compound-Protein Binding Affinity':
1000
  # Convert Y^ from pIC50 to IC50
@@ -1038,6 +1038,19 @@ def create_html_report(df, file=None, task=None, opts=(), progress=gr.Progress(t
1038
  unique_df = None
1039
  category = None
1040
  columns_unique = None
 
 
 
 
 
 
 
 
 
 
 
 
 
1041
  if 'X1' in df_html.columns and 'X2' in df_html.columns:
1042
  n_compound = df_html['X1'].nunique()
1043
  n_protein = df_html['X2'].nunique()
@@ -1059,22 +1072,11 @@ def create_html_report(df, file=None, task=None, opts=(), progress=gr.Progress(t
1059
 
1060
  elif 'Y^' in df_html.columns:
1061
  job = 'Interaction Pair Inference'
1062
- if 'Compound' in df_html.columns and 'Exclude Molecular Graph' not in opts:
1063
- df_html['Compound'] = df_html['Compound'].parallel_apply(
1064
- lambda x: PandasTools.PrintAsImageString(x) if not pd.isna(x) else x)
1065
- else:
1066
- df_html.drop(['Compound'], axis=1, inplace=True)
1067
-
1068
- if 'Scaffold' in df_html.columns and 'Exclude Scaffold Graph' not in opts:
1069
- df_html['Scaffold'] = df_html['Scaffold'].parallel_apply(
1070
- lambda x: PandasTools.PrintAsImageString(x) if not pd.isna(x) else x)
1071
- else:
1072
- df_html.drop(['Scaffold'], axis=1, inplace=True)
1073
 
1074
  df_html.rename(columns=column_aliases, inplace=True)
1075
  df_html.index.name = 'Index'
1076
  if 'Target FASTA' in df_html.columns:
1077
- df_html['Target FASTA'] = df_html['Target FASTA'].parallel_apply(
1078
  lambda x: wrap_text(x) if not pd.isna(x) else x)
1079
 
1080
  num_cols = df_html.select_dtypes('number').columns
@@ -1092,7 +1094,7 @@ def create_html_report(df, file=None, task=None, opts=(), progress=gr.Progress(t
1092
  if 'Target ID' in df_html.columns:
1093
  df_html.drop(['Target FASTA'], axis=1, inplace=True)
1094
  if 'Target FASTA' in df_html.columns:
1095
- df_html['Target FASTA'] = df_html['Target FASTA'].parallel_apply(
1096
  lambda x: wrap_text(x) if not pd.isna(x) else x)
1097
  if 'Scaffold SMILES' in df_html.columns:
1098
  df_html.drop(['Scaffold SMILES'], axis=1, inplace=True)
@@ -1351,11 +1353,11 @@ def submit_report(df, score_list, filter_list, task, progress=gr.Progress(track_
1351
  df_report = df.copy()
1352
  try:
1353
  for filter_name in filter_list:
1354
- df_report[filter_name] = df_report['Compound'].parallel_apply(
1355
  lambda x: FILTER_MAP[filter_name](x) if not pd.isna(x) else x)
1356
 
1357
  for score_name in score_list:
1358
- df_report[score_name] = df_report['Compound'].parallel_apply(
1359
  lambda x: SCORE_MAP[score_name](x) if not pd.isna(x) else x)
1360
 
1361
  return (create_html_report(df_report, file=None, task=task), df_report,
@@ -1988,7 +1990,7 @@ with gr.Blocks(theme=theme, title='DeepSEQreen', css=CSS, delete_cache=(3600, 48
1988
  alignment = aligner.align(processed_fasta, query)
1989
  return alignment.score / max(len(processed_fasta), len(query))
1990
 
1991
- alignment_df['score'] = alignment_df['X2'].parallel_apply(align_score)
1992
  row = alignment_df.loc[alignment_df['score'].idxmax()]
1993
  family = str(row['Target Family']).title()
1994
  return gr.Dropdown(value=family,
@@ -2314,13 +2316,13 @@ QALAHAYFAQYHDPDDEPVADPYDQSFESRDLLIDEWKSLTYDEVISFVPPPLDQEEMES
2314
  infer_df = pd.read_csv(drug_target_pair_upload)
2315
  validate_columns(infer_df, ['X1', 'X2'])
2316
 
2317
- infer_df['X1_ERR'] = infer_df['X1'].parallel_apply(
2318
  validate_seq_str, regex=SMILES_PAT)
2319
  if not infer_df['X1_ERR'].isna().all():
2320
  raise ValueError(
2321
  f"Encountered invalid SMILES:\n{infer_df[~infer_df['X1_ERR'].isna()][['X1', 'X1_ERR']]}")
2322
 
2323
- infer_df['X2_ERR'] = infer_df['X2'].parallel_apply(
2324
  validate_seq_str, regex=FASTA_PAT)
2325
  if not infer_df['X2_ERR'].isna().all():
2326
  raise ValueError(
 
43
  from apscheduler.schedulers.background import BackgroundScheduler
44
  from tinydb import TinyDB, Query
45
 
46
+ import swifter
47
  from tqdm.auto import tqdm
48
 
49
  from deepscreen.data.dti import validate_seq_str, rdkit_canonicalize, FASTA_PAT, SMILES_PAT
 
786
  orig_df['Target Family'] = None
787
  if orig_df['Target Family'].isna().any():
788
  orig_df.loc[orig_df['Target Family'].isna(), 'Target Family'] = (
789
+ orig_df.loc[orig_df['Target Family'].isna(), 'X2'].swifter.apply(detect_family)
790
  )
791
  detect_family.cache_clear()
792
 
 
885
  if "Include Compound Max. Tanimoto Similarity to Training Compounds" in opts:
886
  for family in prediction_df['Target Family'].unique():
887
  family_smiles_df = get_seen_smiles(family=family, task=task_value)
888
+ family_smiles_df['FP'] = family_smiles_df['X1'].swifter.apply(smiles_to_ecfp)
889
 
890
  @cache
891
  def max_sim(smi):
 
893
 
894
  prediction_df.loc[
895
  prediction_df['Target Family'] == family, 'Max. Tanimoto Similarity to Training Compounds'] = (
896
+ prediction_df.loc[prediction_df['Target Family'] == family, 'X1'].swifter.apply(max_sim)
897
  )
898
  max_sim.cache_clear()
899
 
 
907
  return max_tanimoto_similarity(smiles, seen_smiles_with_fp=pos_compounds_df)
908
 
909
  prediction_df[['Max. Tanimoto Similarity', 'Max. Tanimoto Similarity Compound']] = (
910
+ prediction_df['X1'].swifter.apply(max_sim).apply(pd.Series)
911
  )
912
  max_sim.cache_clear()
913
 
914
  if "Include Target Max. Sequence Identity to Known Interacting Targets of Compound" in opts:
915
  x2 = prediction_df['X2'].iloc[0]
916
+ prediction_df['X1^'] = prediction_df['X1'].swifter.apply(rdkit_canonicalize)
917
 
918
  @cache
919
  def calculate_max_sequence_identity(compound):
 
921
  return max_sequence_identity(x2, seen_fastas=compound_targets)
922
 
923
  prediction_df[['Max. Sequence Identity', 'Max. Sequence Identity Target']] = (
924
+ prediction_df['X1^'].swifter.apply(calculate_max_sequence_identity).apply(pd.Series)
925
  )
926
  prediction_df.drop(['X1^'], axis=1, inplace=True)
927
 
 
937
 
938
  prediction_df.loc[
939
  prediction_df['Target Family'] == family, 'Max. Sequence Identity to Training Targets'] = (
940
+ prediction_df.loc[prediction_df['Target Family'] == family, 'X2'].swifter.apply(max_id)
941
  )
942
  max_id.cache_clear()
943
 
 
991
 
992
  if 'X1' in df.columns:
993
  if 'Compound' not in df.columns or df['Compound'].dtype != 'object':
994
+ df['Compound'] = df['X1'].swifter.apply(
995
  lambda smiles: PandasTools._MolPlusFingerprint(Chem.MolFromSmiles(smiles)))
996
+ df['Scaffold'] = df['Compound'].swifter.apply(MurckoScaffold.GetScaffoldForMol)
997
+ df['Scaffold SMILES'] = df['Scaffold'].swifter.apply(lambda x: Chem.MolToSmiles(x))
998
 
999
  if task == 'Compound-Protein Binding Affinity':
1000
  # Convert Y^ from pIC50 to IC50
 
1038
  unique_df = None
1039
  category = None
1040
  columns_unique = None
1041
+
1042
+ if 'Compound' in df_html.columns and 'Exclude Molecular Graph' not in opts:
1043
+ df_html['Compound'] = df_html['Compound'].swifter.apply(
1044
+ lambda x: PandasTools.PrintAsImageString(x) if not pd.isna(x) else x)
1045
+ else:
1046
+ df_html.drop(['Compound'], axis=1, inplace=True)
1047
+
1048
+ if 'Scaffold' in df_html.columns and 'Exclude Scaffold Graph' not in opts:
1049
+ df_html['Scaffold'] = df_html['Scaffold'].swifter.apply(
1050
+ lambda x: PandasTools.PrintAsImageString(x) if not pd.isna(x) else x)
1051
+ else:
1052
+ df_html.drop(['Scaffold'], axis=1, inplace=True)
1053
+
1054
  if 'X1' in df_html.columns and 'X2' in df_html.columns:
1055
  n_compound = df_html['X1'].nunique()
1056
  n_protein = df_html['X2'].nunique()
 
1072
 
1073
  elif 'Y^' in df_html.columns:
1074
  job = 'Interaction Pair Inference'
 
 
 
 
 
 
 
 
 
 
 
1075
 
1076
  df_html.rename(columns=column_aliases, inplace=True)
1077
  df_html.index.name = 'Index'
1078
  if 'Target FASTA' in df_html.columns:
1079
+ df_html['Target FASTA'] = df_html['Target FASTA'].swifter.apply(
1080
  lambda x: wrap_text(x) if not pd.isna(x) else x)
1081
 
1082
  num_cols = df_html.select_dtypes('number').columns
 
1094
  if 'Target ID' in df_html.columns:
1095
  df_html.drop(['Target FASTA'], axis=1, inplace=True)
1096
  if 'Target FASTA' in df_html.columns:
1097
+ df_html['Target FASTA'] = df_html['Target FASTA'].swifter.apply(
1098
  lambda x: wrap_text(x) if not pd.isna(x) else x)
1099
  if 'Scaffold SMILES' in df_html.columns:
1100
  df_html.drop(['Scaffold SMILES'], axis=1, inplace=True)
 
1353
  df_report = df.copy()
1354
  try:
1355
  for filter_name in filter_list:
1356
+ df_report[filter_name] = df_report['Compound'].swifter.apply(
1357
  lambda x: FILTER_MAP[filter_name](x) if not pd.isna(x) else x)
1358
 
1359
  for score_name in score_list:
1360
+ df_report[score_name] = df_report['Compound'].swifter.apply(
1361
  lambda x: SCORE_MAP[score_name](x) if not pd.isna(x) else x)
1362
 
1363
  return (create_html_report(df_report, file=None, task=task), df_report,
 
1990
  alignment = aligner.align(processed_fasta, query)
1991
  return alignment.score / max(len(processed_fasta), len(query))
1992
 
1993
+ alignment_df['score'] = alignment_df['X2'].swifter.apply(align_score)
1994
  row = alignment_df.loc[alignment_df['score'].idxmax()]
1995
  family = str(row['Target Family']).title()
1996
  return gr.Dropdown(value=family,
 
2316
  infer_df = pd.read_csv(drug_target_pair_upload)
2317
  validate_columns(infer_df, ['X1', 'X2'])
2318
 
2319
+ infer_df['X1_ERR'] = infer_df['X1'].swifter.apply(
2320
  validate_seq_str, regex=SMILES_PAT)
2321
  if not infer_df['X1_ERR'].isna().all():
2322
  raise ValueError(
2323
  f"Encountered invalid SMILES:\n{infer_df[~infer_df['X1_ERR'].isna()][['X1', 'X1_ERR']]}")
2324
 
2325
+ infer_df['X2_ERR'] = infer_df['X2'].swifter.apply(
2326
  validate_seq_str, regex=FASTA_PAT)
2327
  if not infer_df['X2_ERR'].isna().all():
2328
  raise ValueError(