Spaces:
Sleeping
Sleeping
File size: 39,448 Bytes
7c2a63c 300aa0f 7c2a63c e8c9457 7c2a63c 24fcb1d 7c2a63c 300aa0f afd5a83 7c2a63c e8c9457 7c2a63c e8c9457 7c2a63c e8c9457 7c2a63c e8c9457 7c2a63c e8c9457 7c2a63c e8c9457 7c2a63c e8c9457 7c2a63c e8c9457 7c2a63c 6e3f560 7c2a63c e8c9457 7c2a63c 0db0b2f 49678ef 7c2a63c e8c9457 7c2a63c e8c9457 7c2a63c 11b17b3 7c2a63c e8c9457 49678ef e8c9457 49678ef e8c9457 7c2a63c e8c9457 7c2a63c e8c9457 7c2a63c e8c9457 7c2a63c 5ed6962 300aa0f e8c9457 300aa0f e8c9457 300aa0f e8c9457 300aa0f 7c2a63c 300aa0f afd5a83 7c2a63c 9c28e37 7c2a63c ceb5757 7c2a63c 2c69a6a ba775f6 7c2a63c 2c69a6a ba775f6 2c69a6a ba775f6 7c2a63c ba775f6 7c2a63c 5f2b6ab afd5a83 e8c9457 7c2a63c 0ece4c9 2c69a6a 7c2a63c e8c9457 7c2a63c 9c28e37 7c2a63c e8c9457 7c2a63c 2c69a6a b66cb3e 7c2a63c b66cb3e 7c2a63c 2c69a6a b66cb3e 2c69a6a b66cb3e 7c2a63c b66cb3e 7c2a63c b66cb3e 7c2a63c afd5a83 7c2a63c a1dbbf2 7c2a63c afd5a83 7c2a63c afd5a83 7c2a63c a46ed6d 7c2a63c 71076f5 7c2a63c a46ed6d 7c2a63c bb9741f 7c2a63c 300aa0f e8c9457 7c2a63c 71076f5 7c2a63c 300aa0f e8c9457 7c2a63c 300aa0f e8c9457 7c2a63c 07fcaca e8c9457 7c2a63c 300aa0f e8c9457 7c2a63c 71076f5 7c2a63c 71076f5 7c2a63c 8bb604c 9c28e37 8bb604c 7c2a63c e8c9457 7c2a63c 300aa0f e8c9457 7c2a63c |
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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 |
import torch
from typing import Annotated, TypedDict, Literal
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_core.tools import tool
from langgraph.prebuilt import ToolNode, tools_condition
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langchain_core.messages import SystemMessage, trim_messages, AIMessage, HumanMessage, ToolCall
from langchain_huggingface.llms import HuggingFacePipeline
from langchain_huggingface import ChatHuggingFace
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate
from langchain_core.runnables import chain
from uuid import uuid4
import matplotlib.pyplot as plt
from rdkit import Chem
from rdkit.Chem import AllChem, QED
from rdkit.Chem import Draw
from rdkit.Chem.Draw import MolsToGridImage
from rdkit import rdBase
from rdkit.Chem import rdMolAlign
import os, re
from rdkit import RDConfig
import gradio as gr
from PIL import Image
import numpy as np
import pandas as pd
from chembl_webresource_client.new_client import new_client
from tqdm.auto import tqdm
import requests
import spaces
from rcsbapi.search import TextQuery
import requests
import itertools
import lightgbm as lgb
from lightgbm import LGBMRegressor
import deepchem as dc
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import StandardScaler
import tensorflow as tf
import random
from finetune_gpt import *
device = "cuda" if torch.cuda.is_available() else "cpu"
hf = HuggingFacePipeline.from_model_id(
model_id= "microsoft/Phi-4-mini-instruct",
task="text-generation",
pipeline_kwargs = {"max_new_tokens": 800, "temperature": 0.4})
chat_model = ChatHuggingFace(llm=hf)
class State(TypedDict):
'''
The state of the agent.
'''
messages: Annotated[list, add_messages]
query_smiles: str
query_task: str
query_protein: str
query_up_id: str
query_pdb: str
query_chembl: str
tool_choice: tuple
which_tool: int
props_string: str
loop_again: str
which_pdbs: int
recursion_count: int
#(Literal["lipinski_tool", "substitution_tool", "pharm_feature_tool"],
# Literal["lipinski_tool", "substitution_tool", "pharm_feature_tool"])
def uniprot_node(state: State) -> State:
'''
This tool takes in the user requested protein and searches UNIPROT for matches.
It returns a string scontaining the protein ID, gene name, organism, and protein name.
Args:
query_protein: the name of the protein to search for.
Returns:
protein_string: a string containing the protein ID, gene name, organism, and protein name.
'''
print("UNIPROT tool")
print('===================================================')
protein_name = state["query_protein"]
current_props_string = state["props_string"]
try:
url = f'https://rest.uniprot.org/uniprotkb/search?query={protein_name}&format=tsv'
response = requests.get(url).text
f = open(f"{protein_name}_uniprot_ids.tsv", "w")
f.write(response)
f.close()
prot_df = pd.read_csv(f'{protein_name}_uniprot_ids.tsv', sep='\t')
prot_human_df = prot_df[prot_df['Organism'] == "Homo sapiens (Human)"]
print(f"Found {len(prot_human_df)} Human proteins out of {len(prot_df)} total proteins")
prot_ids = prot_df['Entry'].tolist()
prot_ids_human = prot_human_df['Entry'].tolist()
genes = prot_df['Gene Names'].tolist()
genes_human = prot_human_df['Gene Names'].tolist()
organisms = prot_df['Organism'].tolist()
names = prot_df['Protein names'].tolist()
names_human = prot_human_df['Protein names'].tolist()
protein_string = ''
for id, gene, organism, name in zip(prot_ids, genes, organisms, names):
protein_string += f'Protein ID: {id}, Gene: {gene}, Organism: {organism}, Name: {name}\n'
except:
protein_string = 'No proteins found'
current_props_string += protein_string
state["props_string"] = current_props_string
state["which_tool"] += 1
return state
def get_qed(smiles):
'''
Helper function to compute QED for a given molecule.
Args:
smiles: the input smiles string
Returns:
qed: the QED score of the molecule.
'''
mol = Chem.MolFromSmiles(smiles)
qed = Chem.QED.default(mol)
return qed
def listbioactives_node(state: State) -> State:
'''
Accepts a UNIPROT ID and searches for bioactive molecules
Args:
up_id: the UNIPROT ID of the protein to search for.
Returns:
props_string: the number of bioactive molecules for the given protein
'''
print("List bioactives tool")
print('===================================================')
up_id = state["query_up_id"].strip()
current_props_string = state["props_string"]
targets = new_client.target
bioact = new_client.activity
try:
target_info = targets.get(target_components__accession=up_id).only("target_chembl_id","organism", "pref_name", "target_type")
target_info = pd.DataFrame.from_records(target_info)
print(target_info)
if len(target_info) > 0:
print(f"Found info for Uniprot ID: {up_id}")
chembl_ids = target_info['target_chembl_id'].tolist()
chembl_ids = list(set(chembl_ids))
print(f"Found {len(chembl_ids)} unique ChEMBL IDs")
len_all_bioacts = []
bioact_string = f'Chembl IDs for the UNIPROT ID: {up_id}: \n'
for chembl_id in chembl_ids:
bioact_chosen = bioact.filter(target_chembl_id=chembl_id, type="IC50", relation="=").only(
"molecule_chembl_id",
"type",
"standard_units",
"relation",
"standard_value",
)
len_this_bioacts = len(bioact_chosen)
len_all_bioacts.append(len_this_bioacts)
this_bioact_string = f"Lenth of Bioactivities for ChEMBL ID {chembl_id}: {len_this_bioacts}"
bioact_string += this_bioact_string + '\n'
except:
bioact_string = 'No bioactives found\n'
current_props_string += bioact_string
state["props_string"] = current_props_string
state["which_tool"] += 1
return state
def getbioactives_node(state: State) -> State:
'''
Accepts a Chembl ID and get all bioactives molecule SMILES and IC50s for that ID
Args:
chembl_id: the chembl ID to query
Returns:
props_string: the bioactive molecule SMILES and IC50s for the chembl ID
'''
print("Get bioactives tool")
print('===================================================')
chembl_id = state["query_chembl"].strip()
current_props_string = state["props_string"]
if (chembl_id == None) or (chembl_id.lower() == 'none') or (chembl_id == ''):
return state
#check if f'{chembl_id}_bioactives.csv' exists
if os.path.exists(f'{chembl_id}_bioactives.csv'):
print(f'Found {chembl_id}_bioactives.csv')
total_bioact_df = pd.read_csv(f'{chembl_id}_bioactives.csv')
print(f"number of records: {len(total_bioact_df)}")
else:
compounds = new_client.molecule
bioact = new_client.activity
bioact_chosen = bioact.filter(target_chembl_id=chembl_id, type="IC50", relation="=").only(
"molecule_chembl_id",
"type",
"standard_units",
"relation",
"standard_value",
)
chembl_ids = []
ic50s = []
for record in bioact_chosen:
if record["standard_units"] == 'nM':
chembl_ids.append(record["molecule_chembl_id"])
ic50s.append(float(record["standard_value"]))
bioact_dict = {'chembl_ids' : chembl_ids, 'IC50s': ic50s}
bioact_df = pd.DataFrame.from_dict(bioact_dict)
bioact_df.drop_duplicates(subset=["chembl_ids"], keep= "last")
print(f"Number of records: {len(bioact_df)}")
print(bioact_df.shape)
compounds_provider = compounds.filter(molecule_chembl_id__in=bioact_df["chembl_ids"].to_list()).only(
"molecule_chembl_id",
"molecule_structures"
)
cids_list = []
smiles_list = []
for record in compounds_provider:
cid = record['molecule_chembl_id']
cids_list.append(cid)
if record['molecule_structures']:
if record['molecule_structures']['canonical_smiles']:
smile = record['molecule_structures']['canonical_smiles']
else:
print("No canonical smiles")
smile = None
else:
print('no structures')
smile = None
smiles_list.append(smile)
new_dict = {'SMILES': smiles_list, 'chembl_ids_2': cids_list}
new_df = pd.DataFrame.from_dict(new_dict)
total_bioact_df = pd.merge(bioact_df, new_df, left_on='chembl_ids', right_on='chembl_ids_2')
print(f"number of records: {len(total_bioact_df)}")
total_bioact_df.drop_duplicates(subset=["chembl_ids"], keep= "last")
print(f"number of records after removing duplicates: {len(total_bioact_df)}")
total_bioact_df.dropna(axis=0, how='any', inplace=True)
total_bioact_df.drop(["chembl_ids_2"],axis=1,inplace=True)
print(f"number of records after dropping Null values: {len(total_bioact_df)}")
total_bioact_df.sort_values(by=["IC50s"],inplace=True)
if len(total_bioact_df) > 0:
total_bioact_df.to_csv(f'{chembl_id}_bioactives.csv')
limit = 50
if len(total_bioact_df) > limit:
total_bioact_df = total_bioact_df.iloc[:limit]
bioact_string = f'Results for top bioactivity (IC50 value) for molecules in ChEMBL ID: {chembl_id}. \n'
for smile, ic50 in zip(total_bioact_df['SMILES'], total_bioact_df['IC50s']):
smile = smile.replace('#','~')
bioact_string += f'Molecule SMILES: {smile}, IC50 (nM): {ic50}\n'
if len(total_bioact_df) > 0:
mols = [Chem.MolFromSmiles(smile) for smile in total_bioact_df['SMILES'].to_list()]
legends = [f'IC50: {ic50}' for ic50 in total_bioact_df['IC50s'].to_list()]
img = MolsToGridImage(mols, molsPerRow=5, legends=legends, subImgSize=(200,200))
filename = "Substitution_image.png"
# pic = img.data
# with open(filename,'wb+') as outf:
# outf.write(pic)
img.save(filename)
current_props_string += bioact_string
state["props_string"] = current_props_string
state["which_tool"] += 1
return state
def predict_node(state: State) -> State:
'''
uses the current_bioactives.csv file from the get_bioactives node to fit the
Light GBM model and predict the IC50 for the current smiles.
'''
print("Predict Tool")
print('===================================================')
current_props_string = state["props_string"]
smiles = state["query_smiles"]
chembl_id = state["query_chembl"].strip()
print(f"in predict node, smiles: {smiles}")
try:
df = pd.read_csv(f'{chembl_id}_bioactives.csv')
#if length of the dataframe is over 2000, take a random sample of 2000 points
if len(df) > 2000:
df = df.sample(n=2000, random_state=42)
y_raw = df["IC50s"].to_list()
smiles_list = df["SMILES"].to_list()
ions_to_clean = ["[Na+].",".[Na+]","[Cl-].",".[Cl-]","[K+].",".[K+]"]
Xa = []
y = []
for smile, value in zip(smiles_list, y_raw):
for ion in ions_to_clean:
smile = smile.replace(ion,"")
y.append(np.log10(value))
Xa.append(smile)
mols = [Chem.MolFromSmiles(smile) for smile in Xa]
print(f"Number of molecules: {len(mols)}")
featurizer=dc.feat.RDKitDescriptors()
featname="RDKitDescriptors"
f = featurizer.featurize(mols)
nan_indicies = np.isnan(f)
bad_rows = []
for i, row in enumerate(nan_indicies):
for item in row:
if item == True:
if i not in bad_rows:
print(f"Row {i} has a NaN.")
bad_rows.append(i)
print(f"Old dimensions are: {f.shape}.")
for j,i in enumerate(bad_rows):
k=i-j
f = np.delete(f,k,axis=0)
y = np.delete(y,k,axis=0)
Xa = np.delete(Xa,k,axis=0)
print(f"Deleting row {k} from arrays.")
print(f"New dimensions are: {f.shape}")
if f.shape[0] != len(y) or f.shape[0] != len(Xa):
raise ValueError("Number of rows in X and y do not match.")
X_train, X_test, y_train, y_test = train_test_split(f, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
model = LGBMRegressor(metric='rmse', max_depth = 50, verbose = -1, num_leaves = 31,
feature_fraction = 0.8, min_data_in_leaf = 20)
modelname = "LightGBM Regressor"
model.fit(X_train, y_train)
train_score = model.score(X_train,y_train)
print(f"score for training set: {train_score:.3f}")
valid_score = model.score(X_test, y_test)
print(f"score for validation set: {valid_score:.3f}")
for ion in ions_to_clean:
smiles = smiles.replace(ion,"")
test_mol = Chem.MolFromSmiles(smiles)
test_feat = featurizer.featurize([test_mol])
test_feat = scaler.transform(test_feat)
prediction = model.predict(test_feat)
test_ic50 = 10**(prediction[0])
print(f"Predicted IC50: {test_ic50}")
prop_string = f"The predicted IC50 value for the test molecule is : {test_ic50:.3f} nM. \
The Bioactive data was fitted with the LightGMB model, using RDKit descriptors. The trainin score \
was {train_score:.3f} and the testing score was {valid_score:.3f}. "
print(prop_string)
except:
prop_string = ''
current_props_string += prop_string
state["props_string"] = current_props_string
state["which_tool"] += 1
return state
def gpt_node(state: State) -> State:
'''
Uses a Chembl dataset, previously stored in a CSV file by the get_bioactives node, to
to finetune a GPT model to generate novel molecules for the target protein.
Args:
chembl_id
returns:
prop_string: a string of the novel, generated molecules
'''
print("GPT node")
print('===================================================')
current_props_string = state["props_string"]
chembl_id = state["query_chembl"].strip()
print(f"in gpt node, chembl id: {chembl_id}")
try:
df = pd.read_csv(f'{chembl_id}_bioactives.csv')
prop_string, img = finetune_gpt(df, chembl_id)
prop_string = prop_string.replace("#","~")
filename = "Substitution_image.png"
# pic = img.data
# with open(filename,'wb+') as outf:
# outf.write(pic)
img.save(filename)
except:
prop_string = ''
current_props_string += prop_string
state["props_string"] = current_props_string
state["which_tool"] += 1
return state
def get_protein_from_pdb(pdb_id):
url = f"https://files.rcsb.org/download/{pdb_id}.pdb"
r = requests.get(url)
return r.text
def one_to_three(one_seq):
rev_aa_hash = {
'A': 'ALA',
'R': 'ARG',
'N': 'ASN',
'D': 'ASP',
'C': 'CYS',
'Q': 'GLN',
'E': 'GLU',
'G': 'GLY',
'H': 'HIS',
'I': 'ILE',
'L': 'LEU',
'K': 'LYS',
'M': 'MET',
'F': 'PHE',
'P': 'PRO',
'S': 'SER',
'T': 'THR',
'W': 'TRP',
'Y': 'TYR',
'V': 'VAL'
}
try:
three_seq = rev_aa_hash[one_seq]
except:
three_seq = 'X'
return three_seq
def three_to_one(three_seq):
aa_hash = {
'ALA': 'A',
'ARG': 'R',
'ASN': 'N',
'ASP': 'D',
'CYS': 'C',
'GLN': 'Q',
'GLU': 'E',
'GLY': 'G',
'HIS': 'H',
'ILE': 'I',
'LEU': 'L',
'LYS': 'K',
'MET': 'M',
'PHE': 'F',
'PRO': 'P',
'SER': 'S',
'THR': 'T',
'TRP': 'W',
'TYR': 'Y',
'VAL': 'V'
}
one_seq = []
for residue in three_seq:
try:
one_seq.append(aa_hash[residue])
except:
one_seq.append('X')
return one_seq
def pdb_node(state: State) -> State:
'''
Accepts a PDB ID and queires the protein databank for the sequence of the protein, as well as other
information such as ligands.
Args:
pdb: the PDB ID to query
Returns:
props_string: a string of the
'''
test_pdb = state["query_pdb"].strip()
current_props_string = state["props_string"]
print(f"pdb tool using {test_pdb}")
print('===================================================')
pdb_str = get_protein_from_pdb(test_pdb)
chains = {}
other_molecules = {}
#print(pdb_str.split('\n')[0])
for line in pdb_str.split('\n'):
parts = line.split()
try:
if parts[0] == 'SEQRES':
if parts[2] not in chains:
chains[parts[2]] = []
chains[parts[2]].extend(parts[4:])
if parts[0] == 'HETNAM':
j = 1
if parts[1].strip() in ['2','3','4','5','6','7','8','9']:
j = 2
print(parts[j])
if parts[j] not in other_molecules:
other_molecules[parts[j]] = []
other_molecules[parts[j]].extend(parts[2:])
except:
print('Blank line')
chains_ol = {}
for chain in chains:
chains_ol[chain] = three_to_one(chains[chain])
props_string = f"Chains in PDB ID {test_pdb}: {', '.join(chains.keys())} \n"
for chain in chains_ol:
props_string += f"Chain {chain}: {''.join(chains_ol[chain])} \n"
print(f"Chain {chain}: {''.join(chains_ol[chain])}")
props_string += f"Ligands in PDB ID {test_pdb}.\n"
for mol in other_molecules:
props_string += f"Molecule {mol}: {''.join(other_molecules[mol])} \n"
current_props_string += props_string
state["props_string"] = current_props_string
state["which_tool"] += 1
return state
def find_node(state: State) -> State:
'''
Accepts a protein name and searches the protein databack for PDB IDs that match along with the entry titles.
Args:
protein_name: the protein to query
Returns:
props_string: a string of the
'''
test_protein = state["query_protein"].strip()
which_pdbs = state["which_pdbs"]
current_props_string = state["props_string"]
print(f"find tool using {test_protein}")
print('===================================================')
try:
query = TextQuery(value=test_protein)
results = query()
def pdb_gen():
for rid in results:
yield(rid)
take10 = itertools.islice(pdb_gen(), which_pdbs, which_pdbs+10, 1)
pdb_string = f'10 PDBs that match the protein {test_protein} are: \n'
for pdb in take10:
data = requests.get(f"https://data.rcsb.org/rest/v1/core/entry/{pdb}").json()
title = data['struct']['title']
pdb_string += f'PDB ID: {pdb}, with title: {title} \n'
state["which_pdbs"] = which_pdbs+10
except:
pdb_string = ''
current_props_string += pdb_string
state["props_string"] = current_props_string
state["which_tool"] += 1
return state
def first_node(state: State) -> State:
'''
The first node of the agent. This node receives the input and asks the LLM
to determine which is the best tool to use to answer the QUERY TASK.
Input: the initial prompt from the user. should contain only one of more of the following:
query_protein: the name of the protein to search for.
query_up_id: the Uniprot ID of the protein to search for.
query_chembl: the chembl ID to query
query_pdb: the PDB ID to query
query_smiles: the smiles string
query_task: the query task
the value should be separated from the name by a ':' and each field should
be separated from the previous one by a ','.
All of these values are saved to the state
Output: the tool choice
'''
query_smiles = None
state["query_smiles"] = query_smiles
query_task = None
state["query_task"] = query_task
query_protein = None
state["query_protein"] = query_protein
query_up_id = None
state["query_up_id"] = query_up_id
query_pdb = None
state["query_pdb"] = query_pdb
query_chembl = None
state["query_chembl"] = query_chembl
props_string = ""
state["props_string"] = props_string
state["loop_again"] = None
state['which_pdbs'] = 0
state['recursion_count'] = 0
raw_input = state["messages"][-1].content
parts = raw_input.split(',')
for part in parts:
if 'smiles' in part:
query_smiles = part.split(':')[1]
if query_smiles.lower() == 'none':
query_smiles = None
state["query_smiles"] = query_smiles
if 'task' in part:
query_task = part.split(':')[1]
state["query_task"] = query_task
if 'protein' in part:
query_protein = part.split(':')[1]
if query_protein.lower() == 'none':
query_protein = None
state["query_protein"] = query_protein
if 'up_id' in part:
query_up_id = part.split(':')[1]
if query_up_id.lower() == 'none':
query_up_id = None
state["query_up_id"] = query_up_id
if 'pdb' in part:
query_pdb = part.split(':')[1]
if query_pdb.lower() == 'none':
query_pdb = None
state["query_pdb"] = query_pdb
if 'chembl' in part:
query_chembl = part.split(':')[1]
if query_chembl.lower() == 'none':
query_chembl = None
state["query_chembl"] = query_chembl
prompt = f'''
# For the QUERY_TASK given below, determine if one or two of the tools descibed below
can complete the task. If so, reply with only the tool names followed by "#". If two tools
are required, reply with both tool names separated by a comma and followed by "#".
If the tools cannot complete the task, reply with "None #".
## QUERY_TASK: {query_task}.
## Tools:
- uniprot_tool: this tool takes in the user requested protein and searches UNIPROT for matches.
- It returns a string containing the uniprot protein ID, gene name, organism, and protein name.
- list_bioactives_tool: Accepts a given UNIPROT ID and searches for Chembl IDs and bioactive molecules.
Returns Chembl IDs and numbers of bioactive molecules.
- get_bioactives_tool: Accepts a Chembl ID and get all bioactives molecule SMILES and IC50s for that ID. Requires a
chembl ID, so the list_bioactives_tool should be called before this tool.
- pdb_tool: Accepts a PDB ID and queires the protein databank for the number of chains in and sequence of the
protein, as well as other information such as ligands in the structure.
- find_tool: Accepts a protein name and seaches for PDB IDs that match, returning the PDB ID and the title.
- predict_tool: Predicts the IC50 value for the molecule indicated by the SMILES string provided
Uses the LightGBM model for prediction. Requires a Chembl dataset, so the get_bioactives_tool should be called before this tool.
- gpt_tool: Uses a machine-learning GPT model to generate novel molecules for a chembl dataset. It returns a list
of novel molecules generated by the GPT and an image of the molecules. Requires a Chembl dataset, so the get_bioactives_tool
should be called before this tool.
'''
res = chat_model.invoke(prompt)
none_list = [None, '', 'None', 'none']
tool_choices = str(res).split('<|assistant|>')[1].split('#')[0].strip()
tool_choices = tool_choices.split(',')
if len(tool_choices) == 1:
tool1 = tool_choices[0].strip()
if (tool1 == 'get_bioactives_tool') and (state['query_chembl'].strip() == ''):
tool1 = 'list_bioactives_tool'
if tool1.lower() == 'none':
tool_choice = (None, None)
else:
tool_choice = (tool1, None)
elif len(tool_choices) == 2:
tool1 = tool_choices[0].strip()
tool2 = tool_choices[1].strip()
if (tool1 == 'get_bioactives_tool') and (state['query_chembl'].strip() == ''):
tool1 = 'list_bioactives_tool'
if (tool2 == 'get_bioactives_tool') and (state['query_chembl'].strip() == ''):
tool2 = 'list_bioactives_tool'
if tool1.lower() == 'none' and tool2.lower() == 'none':
tool_choice = (None, None)
elif tool1.lower() == 'none' and tool2.lower() != 'none':
tool_choice = (tool2, None)
elif tool2.lower() == 'none' and tool1.lower() != 'none':
tool_choice = (tool1, None)
else:
tool_choice = (tool1, tool2)
else:
tool_choice = (None, None)
if (len(tool_choice) == 2) and (tool_choice[1] == tool_choice[0]):
tool_choice = (tool_choice[0], None)
state["tool_choice"] = tool_choice
state["which_tool"] = 0
print(f"The chosen tools are: {tool_choice}")
print('task, chembl')
print(f"{state['query_task']}, chembl =({state['query_chembl']}), uniprot =({state['query_up_id']})")
return state
def retry_node(state: State) -> State:
'''
If the previous loop of the agent does not get enough information from the
tools to answer the query, this node is called to retry the previous loop.
Input: the previous loop of the agent.
Output: the tool choice
'''
query_task = state["query_task"]
query_protein = state["query_protein"]
query_up_id = state["query_up_id"]
query_chembl = state["query_chembl"]
query_pdb = state["query_pdb"]
query_smiles = state["query_smiles"]
prompt = f'''
# You were previously given the QUERY_TASK below, and asked to determine if one
or two of the tools described below could complete the task. The tool choices did not succeed.
Please re-examine the tool choices and determine if one or two of the tools described below
can complete the task. If so, reply with only the tool names followed by "#". If two tools
are required, reply with both tool names separated by a comma and followed by "#".
If the tools cannot complete the task, reply with "None #".
## The information provided by the user is:
- QUERY_PROTEIN: {query_protein}.
- QUERY_UP_ID: {query_up_id}.
- QUERY_CHEMBL: {query_chembl}.
- QUERY_PDB: {query_pdb}.
- QUERY_SMILES: {query_smiles}.
## The task is:
- QUERY_TASK: {query_task}.
## Tools:
- uniprot_tool: this tool takes in the user requested protein and searches UNIPROT for matches.
- It returns a string containing the uniprot protein ID, gene name, organism, and protein name.
- list_bioactives_tool: Accepts a given UNIPROT ID and searches for Chembl IDs and bioactive molecules.
Returns Chembl IDs and numbers of bioactive molecules.
- get_bioactives_tool: Accepts a Chembl ID and get all bioactives molecule SMILES and IC50s for that ID. Requires a
chembl ID, so the list_bioactives_tool should be called before this tool.
- pdb_tool: Accepts a PDB ID and queires the protein databank for the number of chains in and sequence of the
protein, as well as other information such as ligands in the structure.
- find_tool: Accepts a protein name and seaches for PDB IDs that match, returning the PDB ID and the title.
- predict_tool: Predicts the IC50 value for the molecule indicated by the SMILES string provided
Uses the LightGBM model for prediction. Requires a Chembl dataset, so the get_bioactives_tool should be called before this tool.
- gpt_tool: Uses a machine-learning GPT model to generate novel molecules for a chembl dataset. It returns a list
of novel molecules generated by the GPT and an image of the molecules. Requires a Chembl dataset, so the get_bioactives_tool
should be called before this tool.
'''
res = chat_model.invoke(prompt)
tool_choices = str(res).split('<|assistant|>')[1].split('#')[0].strip()
tool_choices = tool_choices.split(',')
if len(tool_choices) == 1:
tool1 = tool_choices[0].strip()
if (tool1 == 'get_bioactives_tool') and (state['query_chembl'].strip() == ''):
tool1 = 'list_bioactives_tool'
if tool1.lower() == 'none':
tool_choice = (None, None)
else:
tool_choice = (tool1, None)
elif len(tool_choices) == 2:
tool1 = tool_choices[0].strip()
tool2 = tool_choices[1].strip()
if (tool1 == 'get_bioactives_tool') and (state['query_chembl'].strip() == ''):
tool1 = 'list_bioactives_tool'
if (tool2 == 'get_bioactives_tool') and (state['query_chembl'].strip() == ''):
tool2 = 'list_bioactives_tool'
if tool1.lower() == 'none' and tool2.lower() == 'none':
tool_choice = (None, None)
elif tool1.lower() == 'none' and tool2.lower() != 'none':
tool_choice = (tool2, None)
elif tool2.lower() == 'none' and tool1.lower() != 'none':
tool_choice = (tool1, None)
else:
tool_choice = (tool1, tool2)
else:
tool_choice = (None, None)
if (len(tool_choice) == 2) and (tool_choice[1] == tool_choice[0]):
tool_choice = (tool1, None)
state["tool_choice"] = tool_choice
state["which_tool"] = 0
print(f"The chosen tools are (Retry): {tool_choice}")
return state
def loop_node(state: State) -> State:
'''
This node accepts the tool returns and decides if it needs to call another
tool or go on to the parser node.
Input: the tool returns.
Output: the next node to call.
'''
return state
def parser_node(state: State) -> State:
'''
This is the third node in the agent. It receives the output from the tool,
puts it into a prompt as CONTEXT, and asks the LLM to answer the original
query.
Input: the output from the tool.
Output: the answer to the original query.
'''
props_string = state["props_string"]
query_task = state["query_task"]
tool_choice = state["tool_choice"]
if type(tool_choice) != tuple and tool_choice == None:
state["loop_again"] = "finish_gracefully"
return state
elif type(tool_choice) == tuple and (tool_choice[0] == None) and (tool_choice[1] == None):
state["loop_again"] = "finish_gracefully"
return state
elif state['recursion_count'] > 20:
state["loop_again"] = "finish_gracefully"
return state
prompt = f'Using the CONTEXT below, answer the original query, which \
was to answer the QUERY_TASK. Remember that novel molecules generated in the CONTEXT \
were made using a fine-tuned GPT. End your answer with a "#" \
QUERY_TASK: {query_task}.\n \
CONTEXT: {props_string}.\n '
res = chat_model.invoke(prompt)
trial_answer = str(res).split('<|assistant|>')[1]
print('parser 1 ', trial_answer)
state["messages"] = res
check_prompt = f'Determine if the TRIAL ANSWER below answers the original \
QUERY TASK. If it does, respond with "PROCEED #" . If the TRIAL ANSWER did not \
answer the QUERY TASK, respond with "LOOP #" \n \
Only loop again if the TRIAL ANSWER did not answer the QUERY TASK. \
TRIAL ANSWER: {trial_answer}.\n \
QUERY_TASK: {query_task}.\n'
res = chat_model.invoke(check_prompt)
print('parser, loop again? ', res)
if str(res).split('<|assistant|>')[1].split('#')[0].strip().lower() == "loop":
state["loop_again"] = "loop_again"
state['recursion_count'] += 1
return state
elif str(res).split('<|assistant|>')[1].split('#')[0].strip().lower() == "proceed":
state["loop_again"] = None
print('trying to break loop')
elif "proceed" in str(res).split('<|assistant|>')[1].lower():
state["loop_again"] = None
print('trying to break loop')
state['recursion_count'] += 1
return state
def reflect_node(state: State) -> State:
'''
This is the fourth node of the agent. It recieves the LLMs previous answer and
tries to improve it.
Input: the LLMs last answer.
Output: the improved answer.
'''
previous_answer = state["messages"][-1].content
props_string = state["props_string"]
prompt = f'Look at the PREVIOUS ANSWER below which you provided and the \
TOOL RESULTS. Write an improved answer based on the PREVIOUS ANSWER and the \
TOOL RESULTS by adding additional clarifying and enriching information. End \
your new answer with a "#" \
PREVIOUS ANSWER: {previous_answer}.\n \
TOOL RESULTS: {props_string}. '
res = chat_model.invoke(prompt)
print(res)
return {"messages": res}
def gracefulexit_node(state: State) -> State:
'''
Called when the Agent cannot assign any tools for the task
'''
props_string = state["props_string"]
prompt = f'Summarize the information in the CONTEXT, including any useful chemical information. Start your answer with: \
Here is what I found: \n \
CONTEXT: {props_string}'
res = chat_model.invoke(prompt)
print(res)
return {"messages": res}
def get_chemtool(state):
'''
'''
which_tool = state["which_tool"]
tool_choice = state["tool_choice"]
if tool_choice is None or tool_choice == (None, None):
return None
if which_tool == 0 or which_tool == 1:
current_tool = tool_choice[which_tool]
if current_tool is None:
return None
elif which_tool > 1:
current_tool = None
return current_tool
def loop_or_not(state):
'''
'''
print(f"(line 690) Loop? {state['loop_again']}")
if state["loop_again"] == "loop_again":
return True
elif state["loop_again"] == "finish_gracefully":
return 'lets_get_outta_here'
else:
return False
def pretty_print(answer):
final = str(answer['messages'][-1]).split('<|assistant|>')[-1].split('#')[0].strip("n").strip('\\').strip('n').strip('\\')
for i in range(0,len(final),100):
print(final[i:i+100])
def print_short(answer):
for i in range(0,len(answer),100):
print(answer[i:i+100])
builder = StateGraph(State)
builder.add_node("first_node", first_node)
builder.add_node("retry_node", retry_node)
builder.add_node("uniprot_node", uniprot_node)
builder.add_node("listbioactives_node", listbioactives_node)
builder.add_node("getbioactives_node", getbioactives_node)
builder.add_node("pdb_node", pdb_node)
builder.add_node("find_node", find_node)
builder.add_node("predict_node", predict_node)
builder.add_node("gpt_node", gpt_node)
builder.add_node("loop_node", loop_node)
builder.add_node("parser_node", parser_node)
builder.add_node("reflect_node", reflect_node)
builder.add_node("gracefulexit_node", gracefulexit_node)
builder.add_edge(START, "first_node")
builder.add_conditional_edges("first_node", get_chemtool, {
"uniprot_tool": "uniprot_node",
"list_bioactives_tool": "listbioactives_node",
"get_bioactives_tool": "getbioactives_node",
"pdb_tool": "pdb_node",
"find_tool": "find_node",
"predict_tool": "predict_node",
"gpt_tool": "gpt_node",
None: "parser_node"})
builder.add_conditional_edges("retry_node", get_chemtool, {
"uniprot_tool": "uniprot_node",
"list_bioactives_tool": "listbioactives_node",
"get_bioactives_tool": "getbioactives_node",
"pdb_tool": "pdb_node",
"find_tool": "find_node",
"predict_tool": "predict_node",
"gpt_tool": "gpt_node",
None: "parser_node"})
builder.add_edge("uniprot_node", "loop_node")
builder.add_edge("listbioactives_node", "loop_node")
builder.add_edge("getbioactives_node", "loop_node")
builder.add_edge("pdb_node", "loop_node")
builder.add_edge("find_node", "loop_node")
builder.add_edge("predict_node", "loop_node")
builder.add_edge("gpt_node", "loop_node")
builder.add_conditional_edges("loop_node", get_chemtool, {
"uniprot_tool": "uniprot_node",
"list_bioactives_tool": "listbioactives_node",
"get_bioactives_tool": "getbioactives_node",
"pdb_tool": "pdb_node",
"find_tool": "find_node",
"predict_tool": "predict_node",
"gpt_tool": "gpt_node",
None: "parser_node"})
builder.add_conditional_edges("parser_node", loop_or_not, {
True: "retry_node",
'lets_get_outta_here': "gracefulexit_node",
False: "reflect_node"})
builder.add_edge("reflect_node", END)
builder.add_edge("gracefulexit_node", END)
graph = builder.compile()
@spaces.GPU
def ProteinAgent(task, protein, up_id, chembl_id, pdb_id, smiles):
'''
This Agent can perform several protein-related tasks.
1. It can find UNIPROT IDs for a protein, or,
2. given a UNIPROT ID it can find Chembl IDs that match.
3. It can find numbers of and lists of bioactive molecules based on a Chembl ID.
4. It can query the protein databank to find PDB IDs matching a protein name and return the IDs and titles.
5. It can find a particular PDB ID and report information such as how many chains it contains,
the sequence, and any small molecules or ligands bound in the structure.
6. It can predict the IC50 value of a molecule based on a Chembl dataset using the LightGBM model.
7. It can generate novel molecules using a finetuned GPT based on a Chembl dataset.
If Task 6 or 7 are to be called, a chembl dataset is needed. If a Chembl ID is not provided, then task 2 should be called
first to find chembl IDs, then task 4 should be called to collect the dataset based on the ID. If a chembl ID is provided,
then task 4 should be called to collect the chembl dataset.
Args:
task: the task to carry out
protein: a protein name
up_id: a UNIPROT ID
chembl_id: a chembl ID
pdb_id: a PDB ID
smiles: a SMILES string for a molecule.
Returns:
replies[-1]: a text string containing the information requested
img: an image if appropriate, otherwise a blank image.
'''
input = {
"messages": [
HumanMessage(f'query_task: {task}, query_protein: {protein}, query_up_id: {up_id}, query_chembl: {chembl_id}, query_pdb: {pdb_id}, query_smiles: {smiles}')
]
}
#if Substitution_image.png exists, remove it
if os.path.exists('Substitution_image.png'):
os.remove('Substitution_image.png')
#print(input)
replies = []
for c in graph.stream(input): #, stream_mode='updates'):
m = re.findall(r'[a-z]+\_node', str(c))
if len(m) != 0:
reply = c[str(m[0])]['messages']
if 'assistant' in str(reply):
reply = str(reply).split("<|assistant|>")[-1].split('#')[0].strip()
reply = reply.replace("~","#")
replies.append(reply)
#check if image exists
if os.path.exists('Substitution_image.png'):
img_loc = 'Substitution_image.png'
img = Image.open(img_loc)
#else create a dummy blank image
else:
img = Image.new('RGB', (250, 250), color = (255, 255, 255))
return replies[-1], img
with gr.Blocks(fill_height=True) as forest:
gr.Markdown('''
# Protein Agent
- calls Uniprot to find uniprot ids
- calls Chembl to find hits for a given uniprot id and reports number of bioactive molecules in the hit
- calls Chembl to find a list bioactive molecules for a given chembl id and their IC50 values
- calls PDB to find the number of chains in a protein, proteins sequences and small molecules in the structure
- calls PDB to find PDB IDs that match a protein name.
- Uses Bioactive molecules to predict IC50 values for novel molecules with a LightGBM model.
- Uses Bioactive molecules to generate novel molecules using a fine-tuned GPT.
''')
with gr.Row():
with gr.Column():
protein = gr.Textbox(label="Protein name of interest (optional): ", placeholder='none')
up_id = gr.Textbox(label="Uniprot ID of interest (optional): ", placeholder='none')
chembl_id = gr.Textbox(label="Chembl ID of interest (optional): ", placeholder='none')
pdb_id = gr.Textbox(label="PDB ID of interest (optional): ", placeholder='none')
smiles = gr.Textbox(label="Molecule SMILES of interest (optional): ", placeholder='none')
task = gr.Textbox(label="Task for Agent: ")
calc_btn = gr.Button(value = "Submit to Agent")
with gr.Column():
props = gr.Textbox(label="Agent results: ", lines=20 )
pic = gr.Image(label="Molecule")
calc_btn.click(ProteinAgent, inputs = [task, protein, up_id, chembl_id, pdb_id, smiles], outputs = [props, pic])
task.submit(ProteinAgent, inputs = [task, protein, up_id, chembl_id, pdb_id, smiles], outputs = [props, pic])
forest.launch(debug=False, mcp_server=True) |