Spaces:
Sleeping
Sleeping
Eachan Johnson commited on
Commit ·
a11f59c
1
Parent(s): 7dbc2af
Update examples and add second plot
Browse filesFiles changed (4)
22 unmodified lines2324252627282949 unmodified lines798081828384858687887 unmodified lines96979894959699100101102991031041051061071081091041051061071081091101101111122 unmodified lines1151161171191181191201219 unmodified lines13113213313513413513613813713813914013 unmodified lines15415515615815915715815916016116216316416516616716816917017117217317417517617717816117918018118218318418518644 unmodified lines23123223323423523623723823924024124224324424521821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427524624724824925025125225325425525625725825926026126226326426526626726814 unmodified lines2832842852862872882892902912922932972942952962993002972982993003013043053063073023033083043053063123133143153073083093103113123193203133143153163173 unmodified lines32132232332432532632732832933033133233333433533633733833934034134234334433434534634734834935035135238 unmodified lines39139239338239439539639713 unmodified lines41141241341441541641715 unmodified lines4334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654264664674684691 unmodified line47147247343443547447547647747847937 unmodified lines51751851947952052152252341 unmodified lines5655665675275685695705715725735745345755765775785795805815825835845855865875885895905915925935945955965975985996006 unmodified lines6076086095476106116126136145526156166176186196206216226236246256266276286296306316326336345556356366376386396406416425605615625635645655666436446456466476486496506516526536546556566576586596606616625695705716636646655745755766666675786686696706719 unmodified lines6816826835945955965975985996006016846856046866876886896906916926936946956966973 unmodified lines70170270361570470570670770870971071171271371471571622 unmodified linesimport torch
CACHE = "./cache"MAX_ROWS = 4000HEADER_FILE = os.path.join("sources", "header.md")MODEL_REPOS = { "Klebsiella pneumoniae": "hf://scbirlab/spark-dv-fp-2503-kpn",49 unmodified lines input_representation: str = 'smiles', output_representation: Union[Iterable[str], str] = 'smiles'): output_representation = cast(output_representation, to=list) for rep in output_representation: message = f"Converting from {input_representation} to {rep}..." gr.Info(message, duration=10)
df = pd.DataFrame({ input_representation: _clean_split_input(strings),7 unmodified lines )
def predict_one( strings: str, input_representation: str = 'smiles', def _prediction_loop( df: pd.DataFrame, predict: Union[Iterable[str], str] = 'smiles', extra_metrics: Optional[Union[Iterable[str], str]] = None):) -> pd.DataFrame: species_to_predict = cast(predict, to=list) prediction_cols = [] if extra_metrics is None: extra_metrics = [] else: extra_metrics = cast(extra_metrics, to=list) prediction_df = convert_one( strings=strings, input_representation=input_representation, output_representation=['id', 'pubchem_name', 'pubchem_id', 'smiles', 'inchikey', "mwt", "clogp"], ) species_to_predict = cast(predict, to=list) prediction_cols = [] for species in species_to_predict: message = f"Predicting for species: {species}" print_err(message)2 unmodified lines this_features = this_modelbox._input_cols this_labels = this_modelbox._label_cols this_prediction_input = ( prediction_df df .rename(columns={ "smiles": this_features[0], })9 unmodified lines ).with_format("numpy")["__prediction__"].flatten() print(prediction) this_col = f"{species}: predicted MIC (µM)" prediction_df[this_col] = np.power(10., -prediction) * 1e6 df[this_col] = np.power(10., -prediction) * 1e6 prediction_cols.append(this_col) this_col = f"{species}: predicted MIC (µg / mL)" prediction_df[this_col] = np.power(10., -prediction) * 1e3 * prediction_df["mwt"] df[this_col] = np.power(10., -prediction) * 1e3 * df["mwt"] prediction_cols.append(this_col)
for extra_metric in extra_metrics:13 unmodified lines ) .with_format("numpy") ) prediction_df[this_col] = this_extra[this_extra.column_names[-1]] df[this_col] = this_extra[this_extra.column_names[-1]]
return prediction_cols, df
def predict_one( strings: str, input_representation: str = 'smiles', predict: Union[Iterable[str], str] = 'smiles', extra_metrics: Optional[Union[Iterable[str], str]] = None): prediction_df = convert_one( strings=strings, input_representation=input_representation, output_representation=['id', 'pubchem_name', 'pubchem_id', 'smiles', 'inchikey', "mwt", "clogp"], ) prediction_cols, prediction_df = _prediction_loop( prediction_df, predict=predict, extra_metrics=extra_metrics, ) return gr.DataFrame( prediction_df[['id', 'pubchem_name', 'pubchem_id'] + prediction_cols + ['smiles', 'inchikey', "mwt", "clogp"]], prediction_df[ ['id', 'pubchem_name', 'pubchem_id'] + prediction_cols + ['smiles', 'inchikey', "mwt", "clogp"] ], visible=True )
44 unmodified lines else: extra_metrics = cast(extra_metrics, to=list)
if df.shape[0] > MAX_ROWS: message = f"Truncating input to {MAX_ROWS} rows" print_err(message) gr.Info(message, duration=15) df = df.iloc[:MAX_ROWS]
prediction_df = convert_file( df, column=column, input_representation=input_representation, output_representation=["id", "smiles", "inchikey", "mwt", "clogp"], ) if prediction_df.shape[0] > 1000: message = f"Truncating input to 1000 rows" print_err(message) gr.Info(message, duration=15) prediction_df = prediction_df.iloc[:1000] species_to_predict = cast(predict, to=list) prediction_cols = [] for species in species_to_predict: message = f"Predicting for species: {species}" print_err(message) gr.Info(message, duration=3) this_modelbox = MODELBOXES[species] this_features = this_modelbox._input_cols this_labels = this_modelbox._label_cols this_prediction_input = ( prediction_df .rename(columns={ "smiles": this_features[0], }) .assign(**{label: np.nan for label in this_labels}) ) print(this_prediction_input) prediction = this_modelbox.predict( data=this_prediction_input, features=this_features, labels=this_labels, aggregator="mean", cache=CACHE, ).with_format("numpy")["__prediction__"].flatten() print(prediction) this_col = f"{species}: predicted MIC (µM)" prediction_df[this_col] = np.power(10., -prediction) * 1e6 prediction_cols.append(this_col) this_col = f"{species}: predicted MIC (µg / mL)" prediction_df[this_col] = np.power(10., -prediction) * 1e3 * prediction_df["mwt"] prediction_cols.append(this_col)
for extra_metric in extra_metrics: message = f"Calculating {extra_metric} for species: {species}" print_err(message) gr.Info(message, duration=10) # this_modelbox._input_training_data = this_modelbox._input_training_data.remove_columns([this_modelbox._in_key]) this_col = f"{species}: {extra_metric}" prediction_cols.append(this_col) print(">>>", this_modelbox._input_training_data) print(">>>", this_modelbox._input_training_data.format) print(">>>", this_modelbox._in_key, this_modelbox._out_key) this_extra = ( EXTRA_METRICS[extra_metric]( this_modelbox, this_prediction_input, ) .with_format("numpy") ) prediction_df[this_col] = this_extra[this_extra.column_names[-1]] other_cols = [col for col in prediction_df if col not in ['id', 'inchikey', 'smiles', "mwt", "clogp"] + [column] + prediction_cols]
return prediction_df[['id', 'inchikey'] + [column] + prediction_cols + other_cols + ['smiles', "mwt", "clogp"]] prediction_cols, prediction_df = _prediction_loop( prediction_df, predict=predict, extra_metrics=extra_metrics, ) main_cols = set( ['id', 'inchikey', 'smiles', "mwt", "clogp"] + [column] + prediction_cols ) other_cols = [ col for col in prediction_df if col not in main_cols ] return prediction_df[ ['id', 'inchikey'] + [column] + prediction_cols + other_cols + ['smiles', "mwt", "clogp"] ]
def draw_one( strings: Union[Iterable[str], str],14 unmodified lines legends=["\n".join(items) for items in zip(*_ids.values())], )
def log10_if_all_positive(df, col): if np.all(df[col] > 0.): df[col] = np.log10(df[col]) title = f"log10[ {col} ]" else: title = col return title, df
def plot_pred_vs_observed(
def plot_x_vs_y( df, species: str, observed: str, x: str, y: str, color: Optional[str] = None,): print_err(df.head()) xcol = f"{species}: predicted MIC (µM)" ycol = observed y_title = f"Observed ({ycol})" cols = ["id", "inchikey", "smiles", "mwt", "clogp", xcol, ycol] y_title = y cols = ["id", "inchikey", "smiles", "mwt", "clogp", x, y] color_title = color if color is not None and color not in cols: cols.append(color) cols = list(set(cols)) print_err(df[cols].columns) if np.all(df[xcol] > 0): df[xcol] = np.log10(df[xcol]) x_title = f"Predicted log10[MIC(µM)]" x_title, df = log10_if_all_positive(df, x) y_title, df = log10_if_all_positive(df, y) color_title, df = log10_if_all_positive(df, color)
return gr.ScatterPlot( value=df[cols], x=xcol, y=ycol, x=x, y=y, color=color, x_title=x_title, y_title=y_title,3 unmodified lines )
def plot_pred_vs_observed( df, species: str, observed: str, color: Optional[str] = None,): print_err(df.head()) xcol = f"{species}: predicted MIC (µM)" ycol = observed return plot_x_vs_y( df, x=xcol, y=ycol, color=color, )
def download_table( df: pd.DataFrame) -> str: df_hash = nm.hash(pd.util.hash_pandas_object(df).values) filename = f"converted-{df_hash}.csv" filename = f"predicted-{df_hash}.csv" df.to_csv(filename, index=False) return gr.DownloadButton(value=filename, visible=True)
with gr.Blocks() as demo:
with open(HEADER_FILE, 'r') as f:38 unmodified lines ]), list(MODEL_REPOS)[0], list(EXTRA_METRICS)[:2], ], # cipro, ceftriaxone, cefiderocol, linezolid, gepotidacin ], # cipro, ceftriaxone, cefiderocol, linezolid, gepotidacin [ '\n'.join([ "C[C@H]1[C@H]([C@H](C[C@@H](O1)O[C@H]2C[C@@](CC3=C2C(=C4C(=C3O)C(=O)C5=C(C4=O)C(=CC=C5)OC)O)(C(=O)CO)O)N)O",13 unmodified lines "COC1=CC(=CC(=C1OC)OC)CC2=CN=C(N=C2N)N", "CC1=CC(=NO1)NS(=O)(=O)C2=CC=C(C=C2)N", "C1[C@@H]([C@H]([C@@H]([C@H]([C@@H]1NC(=O)[C@H](CCN)O)O[C@@H]2[C@@H]([C@H]([C@@H]([C@H](O2)CO)O)N)O)O)O[C@@H]3[C@@H]([C@H]([C@@H]([C@H](O3)CN)O)O)O)N\nC1=CN=CC=C1C(=O)NN", "C1=CN=CC=C1C(=O)NN ", ]), list(MODEL_REPOS)[0], list(EXTRA_METRICS)[:2],15 unmodified lines "CC1=C(OC2=CC=CC=C12)CN(C)C(=O)/C=C/C3=CC4=C(NC(=O)CC4)N=C3", "CC1=C(OC2=CC=CC=C12)CN(C)C(=O)/C=C/C3=CC4=C(NC(=O)[C@@H](C4)N)N=C3", "CC1=C(OC2=CC=CC=C12)CN(C)C(=O)/C=C/C3=CC4=C(NC(=O)[C@H](CC4)[NH3+])N=C3.[Cl-]", "C1=C(C(=O)NC(=O)N1)F", "CCCCCCNC(=O)N1C=C(C(=O)NC1=O)F", "C[C@@H]1OC[C@@H]2[C@@H](O1)[C@@H]([C@H]([C@@H](O2)O[C@H]3[C@H]4COC(=O)[C@@H]4[C@@H](C5=CC6=C(C=C35)OCO6)C7=CC(=C(C(=C7)OC)O)OC)O)O", ]), list(MODEL_REPOS)[0], list(EXTRA_METRICS)[:2], ], # Debio1452, Debio-1452-NH3, Fabimycin, 5-FU, Carmofur, Etoposide [ '\n'.join([ "COC1=CC(=CC(=C1OC)OC)CC2=CN=C(N=C2N)N", "CC(C)C1=CC=C(C=C1)CN2C=CC3=C2C=CC4=C3C(=NC(=N4)NC5CC5)N", "C1=CC(=CC=C1CCC2=CNC3=C2C(=O)NC(=N3)N)C(=O)N[C@@H](CCC(=O)O)C(=O)O", "CC1=C(C2=C(C=C1)N=C(NC2=O)N)SC3=CC=NC=C3", "CN(CC1=CN=C2C(=N1)C(=NC(=N2)N)N)C3=CC=C(C=C3)C(=O)N[C@@H](CCC(=O)O)C(=O)O", "CC1=NC2=C(C=C(C=C2)CN(C)C3=CC=C(S3)C(=O)N[C@@H](CCC(=O)O)C(=O)O)C(=O)N1", ]), list(MODEL_REPOS)[0], list(EXTRA_METRICS)[:2], ], # Trimethoprim, SCH79797, Pemetrexed, Nolatrexed, Methotrexate, Raltitrexed [ '\n'.join([ "C[C@H]([C@@H](C(=O)NO)NC(=O)C1=CC=C(C=C1)C#CC2=CC=C(C=C2)CN3CCOCC3)O", "CC(C)C1=CC=C(C=C1)CN2C=CC3=C2C=CC4=C3C(=NC(=N4)NC5CC5)N", "C1=CC=C(C=C1)CNC2=NC(=NC3=CC=CC=C32)NCC4=CC=CC=C4", "CC(C)(C)C1=CC=C(C=C1)C(=O)NC(=S)NC2=CC=C(C=C2)NC(=O)CCCCN(C)C", "CCC1=C(C(=NC(=N1)N)N)C2=CC=C(C=C2)Cl", "C1=CC(=CC=C1C(=O)N[C@@H](CCC(=O)O)C(=O)O)NCC2=CN=C3C(=N2)C(=NC(=N3)N)N", ]), list(MODEL_REPOS)[0], list(EXTRA_METRICS)[:2], ], # Debio1452, Debio-1452-NH3, Fabimycin, ], # CHIR-090, SCH79797, DBeQ, Tenovin-6, Pyrimethamine, Aminopterin
], example_labels=[1 unmodified line "Doxorubicin, Ampicillin, Amoxicillin, Meropenem, Tetracycline, Anhydrotetracycline", "Halicin, Abaucin, Trimethoprim, Sulfamethoxazole, Amikacin, Isoniazid", "Murepavadin, Vancomycin, Zosurabalpin, Plazomicin, Gentamicin, Rifampicin", "Debio-1452, Debio-1452-NH3, Fabimycin",
"Debio-1452, Debio-1452-NH3, Fabimycin, 5-FU, Carmofur, Etoposide", "Trimethoprim, Pemetrexed, Nolatrexed, Methotrexate, Raltitrexed", "CHIR-090, SCH79797, DBeQ, Tenovin-6, Pyrimethamine, Aminopterin" ], inputs=[input_line, output_species_single, extra_metric], cache_mode="eager",37 unmodified lines outputs=download_single )
with gr.Tab("Predict on structures from a file (max. 1000 rows, single species)"): with gr.Tab(f"Predict on structures from a file (max. {MAX_ROWS} rows, single species)"): input_file = gr.File( label="Upload a table of chemical compounds here", file_types=[".xlsx", ".csv", ".tsv", ".txt"],41 unmodified lines ) with gr.Row(): observed_col = gr.Dropdown( label="Observed column (y-axis) for comparison plot", label="Observed column (y-axis) for left plot", choices=[], value=None, interactive=True, visible=False, ) color_col = gr.Dropdown( label="Color for comparison plot", label="Color for left plot", choices=[], value=None, interactive=True, visible=False, )
any_x_col = gr.Dropdown( label="x-axis for right plot", choices=[], value=None, interactive=True, visible=False, ) any_y_col = gr.Dropdown( label="y-axis for right plot", choices=[], value=None, interactive=True, visible=False, ) any_color_col = gr.Dropdown( label="Color for right plot", choices=[], value=None, interactive=True,6 unmodified lines file_examples = gr.Examples( examples=[ [ "example-data/stokes2020-eco-1000.csv", "example-data/stokes2020-eco.csv", "SMILES", "Klebsiella pneumoniae", "Mean_Inhibition", "Klebsiella pneumoniae: Doubtscore", list(EXTRA_METRICS)[:3]], list(EXTRA_METRICS)[:3], ], [ "example-data/liu23-abau.csv", "SMILES", "Klebsiella pneumoniae", "Mean", "Klebsiella pneumoniae: Doubtscore", list(EXTRA_METRICS)[:3], ], [ "example-data/wong24-sau-tox-5000.csv", "SMILES", "Klebsiella pneumoniae", "Mean", "Klebsiella pneumoniae: Doubtscore", list(EXTRA_METRICS)[:3], ], ], example_labels=[ "Stokes J. et al., Cell, 2020", "E. coli training data from Stokes J. et al., Cell, 2020", "A. baumannii training data from Liu, 2023", "S. aureus and toxicity training data from Wong, 2024", ], inputs=[input_file, input_column, output_species, observed_col, color_col, extra_metric_file], cache_mode="eager", ) with gr.Row(): pred_vs_observed = gr.ScatterPlot( label="Prediction vs observed", x_title="Predicted MIC (µM)", y_title="Observed", visible=False, height=600, ) pred_vs_observed = gr.ScatterPlot( label="Prediction vs observed", x_title="Predicted MIC (µM)", y_title="Observed", visible=False, height=600, ) plot_any_vs_any = gr.ScatterPlot( label="Any vs any", visible=False, height=600, )
load_data_action = { "fn": load_input_data, "inputs": [input_file], "outputs": [input_data, input_column] } file_examples.load_input_event.then( load_input_data, inputs=[input_file], outputs=[input_data, input_column], **load_data_action, ) input_file.upload( load_input_data, inputs=[input_file], outputs=[input_data, input_column] **load_data_action, ) go_button2.click( go2_click_event = go_button2.click( predict_file, inputs=[ input_data, 9 unmodified lines download_table, inputs=input_data, outputs=download ).then( partial(get_dropdown_options, _type="number"), inputs=[input_data], outputs=[observed_col], ).then( partial(get_dropdown_options, _type="number"), inputs=[input_data], outputs=[color_col], ).then( lambda: gr.Button(visible=True), outputs=[plot_button], outputs=[plot_button] ) for dropdown in [observed_col, color_col, any_color_col, any_x_col, any_y_col]: go2_click_event.then( partial(get_dropdown_options, _type="number"), inputs=[input_data], outputs=[dropdown], )
plot_button.click( plot_pred_vs_observed,3 unmodified lines observed_col, color_col, ], outputs=pred_vs_observed, outputs=[pred_vs_observed], ).then( plot_x_vs_y, inputs=[ input_data, any_x_col, any_y_col, any_color_col, ], outputs=[plot_any_vs_any], )
if __name__ == "__main__":app.py+222-124
example-data/liu23-abau.csvAdded
example-data/stokes2020-eco-1000.csv → example-data/stokes2020-eco.csvRenamed
example-data/wong24-sau-tox-5000.csvAdded