f-galkin commited on
Commit
b1cd545
1 Parent(s): 7bd2a58

Add demo TCM P3GPT notebook

Browse files
.gitattributes CHANGED
@@ -59,3 +59,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
59
  data/batman_formulas.csv filter=lfs diff=lfs merge=lfs -text
60
  data/batman_herbs.csv filter=lfs diff=lfs merge=lfs -text
61
  data/batman_ingredients.csv filter=lfs diff=lfs merge=lfs -text
 
 
59
  data/batman_formulas.csv filter=lfs diff=lfs merge=lfs -text
60
  data/batman_herbs.csv filter=lfs diff=lfs merge=lfs -text
61
  data/batman_ingredients.csv filter=lfs diff=lfs merge=lfs -text
62
+ demo/P3GPT_compound_targets.pckl filter=lfs diff=lfs merge=lfs -text
demo/1Aug2024_aging_signatures.pckl ADDED
Binary file (99 kB). View file
 
demo/HF_TCM+P3GPT_sceening.ipynb ADDED
@@ -0,0 +1,919 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "377f3d60-eddc-46aa-b21b-d30b4cf7607d",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Finding natural geroprotectors with Precious3GPT and BATMAN\n",
9
+ "\n",
10
+ "This notebook will teach you how to use P3GPT to emulate the workflow of a chemical screening projects.\n",
11
+ "\n",
12
+ "In particular, we will identify the compounds encountered in traditional Chinese medicine (TCM) with a geroprotective potential.\n",
13
+ "\n",
14
+ "In this notebook, we generate omics signatures in 8 human tissues. We will then use these signatures and an external catalog of TCM ingredients [BATMAN](http://bionet.ncpsb.org.cn/batman-tcm/#/browse) to select the compounds that target the genes in P3GPT-generated signatures.\n",
15
+ "\n",
16
+ "<center><img src=\"./experiment_scheme.png\" width=\"800\" ></center>\n",
17
+ "<center><i>This notebook only focuses on TCM compounds with known targets, but you may modify the code to look for geroprotectors based on the information about predicted targets as well. In this case, the identified potential geroprptectors will be more numerous and might contain novel compounds.</i></center>\n"
18
+ ]
19
+ },
20
+ {
21
+ "cell_type": "markdown",
22
+ "id": "05eb4e79-9040-4963-ab1f-62cfb4a9f9f3",
23
+ "metadata": {},
24
+ "source": [
25
+ "> ⚠️ **Dependency:** To replicate this project, you need to download some files from [Precious3GPT's repo](https://doi.org/10.57967/hf/2699) and then move the ```mpt_7b``` directory and ```precious3_gpt_multi_modal.py``` from `p3-mm` to `~/P3LIB/`. You can remove all the other downloaded files.\n",
26
+ "\n",
27
+ "``` python\n",
28
+ "from huggingface_hub import snapshot_download\n",
29
+ "snapshot_download(repo_id=\"insilicomedicine/precious3-gpt-multi-modal\", local_dir='./p3-mm')\n",
30
+ "```\n",
31
+ "> In the end, your directory structure should look as follows:\n",
32
+ "```\n",
33
+ "└─ P3GPT+TCM_Demo\n",
34
+ " ├── HF_TCM+P3GPT_sceening.ipynb\n",
35
+ " │ └── P3LIB\n",
36
+ " │ ├── mpt_7b\n",
37
+ " │ │ ├── ...\n",
38
+ " │ │ └── ...\n",
39
+ " │ ├─ endpoints.py\n",
40
+ " │ ├─ formula_picker.py\n",
41
+ " │ └─ precious3_gpt_multi_modal.py\n",
42
+ " │\n",
43
+ " ├── cpds_affecting_tissue_aging.svg\n",
44
+ " ├── experiment_scheme.png\n",
45
+ " ├── 1Aug2024_aging_signatures.pckl [optional]\n",
46
+ " └── P3GPT_compound_targets.pckl\n",
47
+ "```"
48
+ ]
49
+ },
50
+ {
51
+ "cell_type": "code",
52
+ "execution_count": 1,
53
+ "id": "27aceeaa-2db6-4df7-b9bb-3a775032b725",
54
+ "metadata": {},
55
+ "outputs": [],
56
+ "source": [
57
+ "from typing import Dict, List, Any\n",
58
+ "import os\n",
59
+ "import torch\n",
60
+ "from transformers import AutoTokenizer, AutoModel\n",
61
+ "import pandas as pd\n",
62
+ "import time\n",
63
+ "import numpy as np\n",
64
+ "from transformers import GenerationConfig\n",
65
+ "import pickle\n",
66
+ "import numpy as np\n",
67
+ "import pandas as pd\n",
68
+ "from scipy.stats import fisher_exact\n",
69
+ "\n",
70
+ "from P3LIB.endpoints import EndpointHandler"
71
+ ]
72
+ },
73
+ {
74
+ "cell_type": "markdown",
75
+ "id": "fe4c382e-2ef8-4cc1-a050-7cd8388519c2",
76
+ "metadata": {},
77
+ "source": [
78
+ "## 1. Generate aging signatures\n",
79
+ "\n",
80
+ "Define the tissues and the omics domains to be screened to be inserted in the P3GPT prompt template.\n",
81
+ "\n",
82
+ "Since we are only intereseted in aging-related omics differences, we only need one instruction: `age_group2diff2age_group`.\n",
83
+ "Make sure to specify the case and control age groups exactly as specified in the `p3_entities_with_type.csv` file.\n",
84
+ "\n",
85
+ "We can leave most other prompt fields empty but you are free to modify them and thus reproduce a different setting. E.g. you can leave `tissue` vacant and fill in the `cell` field to enable an *in vitro* setting, or you can specify the `drug` to explore how a compound affects the aging signature."
86
+ ]
87
+ },
88
+ {
89
+ "cell_type": "code",
90
+ "execution_count": 2,
91
+ "id": "36fac0e0-79a8-4128-9604-81b382639b84",
92
+ "metadata": {},
93
+ "outputs": [],
94
+ "source": [
95
+ "tissue_options = ['skin', 'muscle', 'whole blood', 'epithelium', 'artery', 'fat tissue', 'brain', 'liver']\n",
96
+ "domain_options = ['methylation', 'proteomics', 'expression']\n",
97
+ "\n",
98
+ "prompt_config_age = {\n",
99
+ " \"instruction\": 'age_group2diff2age_group',\n",
100
+ " \"tissue\": '', #insert tissue here\n",
101
+ " \"age\": \"\",\n",
102
+ " \"cell\": \"\", #insert cell line here\n",
103
+ " \"efo\": \"\",\n",
104
+ " \"datatype\": \"\",\n",
105
+ " \"drug\": \"\", #insert cpd here if any\n",
106
+ " \"dose\": \"\",\n",
107
+ " \"time\": \"\",\n",
108
+ " \"case\": \"70.0-80.0\",\n",
109
+ " \"control\": \"19.95-25.0\",\n",
110
+ " \"dataset_type\": \"\", #insert domain here\n",
111
+ " \"gender\": \"\",\n",
112
+ " \"species\": \"human\",\n",
113
+ " \"up\": [],\n",
114
+ " \"down\": []\n",
115
+ " }"
116
+ ]
117
+ },
118
+ {
119
+ "cell_type": "markdown",
120
+ "id": "872a6b5d-4d52-46f9-839f-7378153d2f67",
121
+ "metadata": {},
122
+ "source": [
123
+ "The `endpoints.py` module contains several options to invoke P3GPT.\n",
124
+ "Unless your input prompt contains lists of up-/down-regulated genes, it is advised to use `EndpointHandler` for local inference.\n",
125
+ "\n",
126
+ "The only parameter you need to specify at handler initialization is the GPU device to deploy P3GPT on."
127
+ ]
128
+ },
129
+ {
130
+ "cell_type": "code",
131
+ "execution_count": 3,
132
+ "id": "f85d0b5b-7b50-400b-82ad-bd655a137da6",
133
+ "metadata": {},
134
+ "outputs": [
135
+ {
136
+ "name": "stderr",
137
+ "output_type": "stream",
138
+ "text": [
139
+ "/home/f.galkin/.cache/huggingface/modules/transformers_modules/mosaicml/mpt-7b/039e37745f00858f0e01e988383a8c4393b1a4f5/configuration_mpt.py:114: UserWarning: alibi or rope is turned on, setting `learned_pos_emb` to `False.`\n",
140
+ " warnings.warn(f'alibi or rope is turned on, setting `learned_pos_emb` to `False.`')\n",
141
+ "/home/f.galkin/.cache/huggingface/modules/transformers_modules/mosaicml/mpt-7b/039e37745f00858f0e01e988383a8c4393b1a4f5/configuration_mpt.py:141: UserWarning: If not using a Prefix Language Model, we recommend setting \"attn_impl\" to \"flash\" instead of \"triton\".\n",
142
+ " warnings.warn(UserWarning('If not using a Prefix Language Model, we recommend setting \"attn_impl\" to \"flash\" instead of \"triton\".'))\n"
143
+ ]
144
+ }
145
+ ],
146
+ "source": [
147
+ "handler = EndpointHandler(device='cuda:2')"
148
+ ]
149
+ },
150
+ {
151
+ "cell_type": "markdown",
152
+ "id": "c4df757f-7abd-4d50-9b3c-888b2d1208d0",
153
+ "metadata": {},
154
+ "source": [
155
+ "With 8 tissues and 3 omics domains, you will generate a total of 24 aging signatures.\n",
156
+ "\n",
157
+ "The default mode of execution for P3GPT is prompt completion, which is inherently stocahstic. To avoid any sampling effects, we enabled a special mode of P3GPT execution in `EndpointHandler`. Instead of generating gene tokens one at a time, we access the complete output probability distribution for up- and down-regulated genes and take top-N tokens from them.\n",
158
+ "\n",
159
+ "We can treat a gene's output probability as a proxy for its importance in the aging process to be used later."
160
+ ]
161
+ },
162
+ {
163
+ "cell_type": "code",
164
+ "execution_count": 7,
165
+ "id": "2a654df4-93cd-4e53-8468-99a870e427f0",
166
+ "metadata": {
167
+ "scrolled": true
168
+ },
169
+ "outputs": [
170
+ {
171
+ "name": "stdout",
172
+ "output_type": "stream",
173
+ "text": [
174
+ "Running generations for skin-methylation\n",
175
+ "[BOS]<age_group2diff2age_group><tissue>skin </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>methylation </dataset_type><gender></gender><species>human </species><up>\n"
176
+ ]
177
+ },
178
+ {
179
+ "name": "stderr",
180
+ "output_type": "stream",
181
+ "text": [
182
+ "/home/f.galkin/.cache/huggingface/modules/transformers_modules/mosaicml/mpt-7b/039e37745f00858f0e01e988383a8c4393b1a4f5/attention.py:87: UserWarning: Propagating key_padding_mask to the attention module and applying it within the attention module can cause unnecessary computation/memory usage. Consider integrating into attn_bias once and passing that to each attention module instead.\n",
183
+ " warnings.warn('Propagating key_padding_mask to the attention module ' + 'and applying it within the attention module can cause ' + 'unnecessary computation/memory usage. Consider integrating ' + 'into attn_bias once and passing that to each attention ' + 'module instead.')\n"
184
+ ]
185
+ },
186
+ {
187
+ "name": "stdout",
188
+ "output_type": "stream",
189
+ "text": [
190
+ "This is token index where gene should be predicted: 1\n",
191
+ "[BOS]<age_group2diff2age_group><tissue>skin </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>methylation </dataset_type><gender></gender><species>human </species>\n",
192
+ "This is token index where gene should be predicted: 260\n",
193
+ "Running generations for skin-proteomics\n",
194
+ "[BOS]<age_group2diff2age_group><tissue>skin </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>proteomics </dataset_type><gender></gender><species>human </species><up>\n",
195
+ "This is token index where gene should be predicted: 1\n",
196
+ "[BOS]<age_group2diff2age_group><tissue>skin </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>proteomics </dataset_type><gender></gender><species>human </species>\n",
197
+ "This is token index where gene should be predicted: 259\n",
198
+ "Running generations for skin-expression\n",
199
+ "[BOS]<age_group2diff2age_group><tissue>skin </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>expression </dataset_type><gender></gender><species>human </species><up>\n",
200
+ "This is token index where gene should be predicted: 1\n",
201
+ "[BOS]<age_group2diff2age_group><tissue>skin </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>expression </dataset_type><gender></gender><species>human </species>\n",
202
+ "This is token index where gene should be predicted: 251\n",
203
+ "Running generations for muscle-methylation\n",
204
+ "[BOS]<age_group2diff2age_group><tissue>muscle </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>methylation </dataset_type><gender></gender><species>human </species><up>\n",
205
+ "This is token index where gene should be predicted: 1\n",
206
+ "[BOS]<age_group2diff2age_group><tissue>muscle </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>methylation </dataset_type><gender></gender><species>human </species>\n",
207
+ "This is token index where gene should be predicted: 260\n",
208
+ "Running generations for muscle-proteomics\n",
209
+ "[BOS]<age_group2diff2age_group><tissue>muscle </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>proteomics </dataset_type><gender></gender><species>human </species><up>\n",
210
+ "This is token index where gene should be predicted: 1\n",
211
+ "[BOS]<age_group2diff2age_group><tissue>muscle </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>proteomics </dataset_type><gender></gender><species>human </species>\n",
212
+ "This is token index where gene should be predicted: 259\n",
213
+ "Running generations for muscle-expression\n",
214
+ "[BOS]<age_group2diff2age_group><tissue>muscle </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>expression </dataset_type><gender></gender><species>human </species><up>\n",
215
+ "This is token index where gene should be predicted: 1\n",
216
+ "[BOS]<age_group2diff2age_group><tissue>muscle </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>expression </dataset_type><gender></gender><species>human </species>\n",
217
+ "This is token index where gene should be predicted: 259\n",
218
+ "Running generations for whole blood-methylation\n",
219
+ "[BOS]<age_group2diff2age_group><tissue>whole blood </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>methylation </dataset_type><gender></gender><species>human </species><up>\n",
220
+ "This is token index where gene should be predicted: 1\n",
221
+ "[BOS]<age_group2diff2age_group><tissue>whole blood </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>methylation </dataset_type><gender></gender><species>human </species>\n",
222
+ "This is token index where gene should be predicted: 259\n",
223
+ "Running generations for whole blood-proteomics\n",
224
+ "[BOS]<age_group2diff2age_group><tissue>whole blood </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>proteomics </dataset_type><gender></gender><species>human </species><up>\n",
225
+ "This is token index where gene should be predicted: 1\n",
226
+ "[BOS]<age_group2diff2age_group><tissue>whole blood </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>proteomics </dataset_type><gender></gender><species>human </species>\n",
227
+ "This is token index where gene should be predicted: 120\n",
228
+ "Running generations for whole blood-expression\n",
229
+ "[BOS]<age_group2diff2age_group><tissue>whole blood </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>expression </dataset_type><gender></gender><species>human </species><up>\n",
230
+ "This is token index where gene should be predicted: 1\n",
231
+ "[BOS]<age_group2diff2age_group><tissue>whole blood </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>expression </dataset_type><gender></gender><species>human </species>\n",
232
+ "This is token index where gene should be predicted: 13\n",
233
+ "Running generations for epithelium-methylation\n",
234
+ "[BOS]<age_group2diff2age_group><tissue>epithelium </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>methylation </dataset_type><gender></gender><species>human </species><up>\n",
235
+ "This is token index where gene should be predicted: 1\n",
236
+ "[BOS]<age_group2diff2age_group><tissue>epithelium </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>methylation </dataset_type><gender></gender><species>human </species>\n",
237
+ "This is token index where gene should be predicted: 120\n",
238
+ "Running generations for epithelium-proteomics\n",
239
+ "[BOS]<age_group2diff2age_group><tissue>epithelium </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>proteomics </dataset_type><gender></gender><species>human </species><up>\n",
240
+ "This is token index where gene should be predicted: 1\n",
241
+ "[BOS]<age_group2diff2age_group><tissue>epithelium </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>proteomics </dataset_type><gender></gender><species>human </species>\n",
242
+ "This is token index where gene should be predicted: 11\n",
243
+ "Running generations for epithelium-expression\n",
244
+ "[BOS]<age_group2diff2age_group><tissue>epithelium </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>expression </dataset_type><gender></gender><species>human </species><up>\n",
245
+ "This is token index where gene should be predicted: 1\n",
246
+ "[BOS]<age_group2diff2age_group><tissue>epithelium </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>expression </dataset_type><gender></gender><species>human </species>\n",
247
+ "This is token index where gene should be predicted: 11\n",
248
+ "Running generations for artery-methylation\n",
249
+ "[BOS]<age_group2diff2age_group><tissue>artery </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>methylation </dataset_type><gender></gender><species>human </species><up>\n",
250
+ "This is token index where gene should be predicted: 1\n",
251
+ "[BOS]<age_group2diff2age_group><tissue>artery </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>methylation </dataset_type><gender></gender><species>human </species>\n",
252
+ "This is token index where gene should be predicted: 13\n",
253
+ "Running generations for artery-proteomics\n",
254
+ "[BOS]<age_group2diff2age_group><tissue>artery </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>proteomics </dataset_type><gender></gender><species>human </species><up>\n",
255
+ "This is token index where gene should be predicted: 1\n",
256
+ "[BOS]<age_group2diff2age_group><tissue>artery </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>proteomics </dataset_type><gender></gender><species>human </species>\n",
257
+ "This is token index where gene should be predicted: 261\n",
258
+ "Running generations for artery-expression\n",
259
+ "[BOS]<age_group2diff2age_group><tissue>artery </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>expression </dataset_type><gender></gender><species>human </species><up>\n",
260
+ "This is token index where gene should be predicted: 1\n",
261
+ "[BOS]<age_group2diff2age_group><tissue>artery </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>expression </dataset_type><gender></gender><species>human </species>\n",
262
+ "This is token index where gene should be predicted: 261\n",
263
+ "Running generations for fat tissue-methylation\n",
264
+ "[BOS]<age_group2diff2age_group><tissue>fat tissue </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>methylation </dataset_type><gender></gender><species>human </species><up>\n",
265
+ "This is token index where gene should be predicted: 1\n",
266
+ "[BOS]<age_group2diff2age_group><tissue>fat tissue </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>methylation </dataset_type><gender></gender><species>human </species>\n",
267
+ "This is token index where gene should be predicted: 261\n",
268
+ "Running generations for fat tissue-proteomics\n",
269
+ "[BOS]<age_group2diff2age_group><tissue>fat tissue </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>proteomics </dataset_type><gender></gender><species>human </species><up>\n",
270
+ "This is token index where gene should be predicted: 1\n",
271
+ "[BOS]<age_group2diff2age_group><tissue>fat tissue </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>proteomics </dataset_type><gender></gender><species>human </species>\n",
272
+ "This is token index where gene should be predicted: 259\n",
273
+ "Running generations for fat tissue-expression\n",
274
+ "[BOS]<age_group2diff2age_group><tissue>fat tissue </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>expression </dataset_type><gender></gender><species>human </species><up>\n",
275
+ "This is token index where gene should be predicted: 1\n",
276
+ "[BOS]<age_group2diff2age_group><tissue>fat tissue </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>expression </dataset_type><gender></gender><species>human </species>\n",
277
+ "This is token index where gene should be predicted: 260\n",
278
+ "Running generations for brain-methylation\n",
279
+ "[BOS]<age_group2diff2age_group><tissue>brain </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>methylation </dataset_type><gender></gender><species>human </species><up>\n",
280
+ "This is token index where gene should be predicted: 1\n",
281
+ "[BOS]<age_group2diff2age_group><tissue>brain </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>methylation </dataset_type><gender></gender><species>human </species>\n",
282
+ "This is token index where gene should be predicted: 261\n",
283
+ "Running generations for brain-proteomics\n",
284
+ "[BOS]<age_group2diff2age_group><tissue>brain </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>proteomics </dataset_type><gender></gender><species>human </species><up>\n",
285
+ "This is token index where gene should be predicted: 1\n",
286
+ "[BOS]<age_group2diff2age_group><tissue>brain </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>proteomics </dataset_type><gender></gender><species>human </species>\n",
287
+ "This is token index where gene should be predicted: 260\n",
288
+ "Running generations for brain-expression\n",
289
+ "[BOS]<age_group2diff2age_group><tissue>brain </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>expression </dataset_type><gender></gender><species>human </species><up>\n",
290
+ "This is token index where gene should be predicted: 1\n",
291
+ "[BOS]<age_group2diff2age_group><tissue>brain </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>expression </dataset_type><gender></gender><species>human </species>\n",
292
+ "This is token index where gene should be predicted: 260\n",
293
+ "Running generations for liver-methylation\n",
294
+ "[BOS]<age_group2diff2age_group><tissue>liver </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>methylation </dataset_type><gender></gender><species>human </species><up>\n",
295
+ "This is token index where gene should be predicted: 1\n",
296
+ "[BOS]<age_group2diff2age_group><tissue>liver </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>methylation </dataset_type><gender></gender><species>human </species>\n",
297
+ "This is token index where gene should be predicted: 125\n",
298
+ "Running generations for liver-proteomics\n",
299
+ "[BOS]<age_group2diff2age_group><tissue>liver </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>proteomics </dataset_type><gender></gender><species>human </species><up>\n",
300
+ "This is token index where gene should be predicted: 1\n",
301
+ "[BOS]<age_group2diff2age_group><tissue>liver </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>proteomics </dataset_type><gender></gender><species>human </species>\n",
302
+ "This is token index where gene should be predicted: 261\n",
303
+ "Running generations for liver-expression\n",
304
+ "[BOS]<age_group2diff2age_group><tissue>liver </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>expression </dataset_type><gender></gender><species>human </species><up>\n",
305
+ "This is token index where gene should be predicted: 1\n",
306
+ "[BOS]<age_group2diff2age_group><tissue>liver </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>expression </dataset_type><gender></gender><species>human </species>\n",
307
+ "[BOS]<age_group2diff2age_group><tissue>liver </tissue><cell></cell><efo></efo><datatype></datatype><drug></drug><dose></dose><time></time><case>70.0-80.0 </case><control>19.95-25.0 </control><dataset_type>expression </dataset_type><gender></gender><species>human </species>\n",
308
+ "This is token index where gene should be predicted: 261\n"
309
+ ]
310
+ }
311
+ ],
312
+ "source": [
313
+ "generations = {z:{x:{y:[] for y in ('up', 'down')}for x in domain_options} for z in tissue_options}\n",
314
+ "for tis in generations:\n",
315
+ " prompt_config_age['tissue'] = tis\n",
316
+ " for dom in generations[tis]:\n",
317
+ " print(f\"Running generations for {tis}-{dom}\")\n",
318
+ " prompt_config_age['dataset_type'] = dom\n",
319
+ " for direct in generations[tis][dom]:\n",
320
+ " # uses the get_gene_probabilities method to get a list of top-k genes by their output probability\n",
321
+ " try:\n",
322
+ " generations[tis][dom][direct] = handler.get_gene_probabilities(prompt_config=prompt_config_age, top_k=100, list_type=direct,\n",
323
+ " random_seed = 137)\n",
324
+ " except ValueError:\n",
325
+ " # invoke P3GPT with a non-default random seed in case you encounter any errors in your generation\n",
326
+ " generations[tis][dom][direct] = handler.get_gene_probabilities(prompt_config=prompt_config_age, top_k=100, list_type=direct,\n",
327
+ " random_seed = 138)"
328
+ ]
329
+ },
330
+ {
331
+ "cell_type": "markdown",
332
+ "id": "d585e881-3057-4e7a-a609-e7982e891237",
333
+ "metadata": {},
334
+ "source": [
335
+ "### ↓ This file stores the generated up-down lists for aging tissues"
336
+ ]
337
+ },
338
+ {
339
+ "cell_type": "code",
340
+ "execution_count": 6,
341
+ "id": "4f38b543-3109-40ff-b73d-a30d152de159",
342
+ "metadata": {},
343
+ "outputs": [],
344
+ "source": [
345
+ "with open('./1Aug2024_aging_signatures.pckl', 'wb') as f:\n",
346
+ " pickle.dump(generations,f)"
347
+ ]
348
+ },
349
+ {
350
+ "cell_type": "markdown",
351
+ "id": "e631b582-b393-4084-b528-df5a8540272f",
352
+ "metadata": {},
353
+ "source": [
354
+ "> 💡 **Note**: You may load the file above directly to skip step-1 and use P3GPT-generated aging signature in your TCM research\n",
355
+ "```python\n",
356
+ "with open('./1Aug2024_aging_signatures.pckl', 'rb') as f:\n",
357
+ " generations = pickle.load(f)\n",
358
+ "```"
359
+ ]
360
+ },
361
+ {
362
+ "cell_type": "markdown",
363
+ "id": "ed5e17cb-0242-40be-bd24-056d3ad61bc1",
364
+ "metadata": {},
365
+ "source": [
366
+ "## 2. Screen TCM compounds"
367
+ ]
368
+ },
369
+ {
370
+ "cell_type": "markdown",
371
+ "id": "9e8f2ea2-1298-4ecc-9686-e7426afd9a1b",
372
+ "metadata": {},
373
+ "source": [
374
+ "The file below contains 659 TCM ingredients that are represented in the P3GPT training set.\n",
375
+ "\n",
376
+ "The BATMAN catalog refers to compounds by Pubchem CIDs while P3GPT uses conventional names, so we created a mapping CID→Names.\n",
377
+ "BATMAN also specifies protein targets the compounds are known or predicted to affect. We will measure the overlap between these lists and the P3GPT-generated aging signatures to find the most promising compounds. "
378
+ ]
379
+ },
380
+ {
381
+ "cell_type": "code",
382
+ "execution_count": 9,
383
+ "id": "4dd3cd2e-0b8c-413f-87fb-0ce9bd0464e8",
384
+ "metadata": {},
385
+ "outputs": [],
386
+ "source": [
387
+ "with open('./P3GPT_compound_targets.pckl', 'rb') as f:\n",
388
+ " TCM_cpds = pickle.load(f)"
389
+ ]
390
+ },
391
+ {
392
+ "cell_type": "code",
393
+ "execution_count": 10,
394
+ "id": "fe272676-96c6-401b-af36-31896daad5a0",
395
+ "metadata": {
396
+ "scrolled": true
397
+ },
398
+ "outputs": [
399
+ {
400
+ "data": {
401
+ "text/plain": [
402
+ "(5281792,\n",
403
+ " {'title': 'rosmarinic-acid',\n",
404
+ " 'known_targets': {'symbols': ['C5',\n",
405
+ " 'CCR3',\n",
406
+ " 'CCL2',\n",
407
+ " 'PREP',\n",
408
+ " 'NFKB1',\n",
409
+ " 'IKBKB',\n",
410
+ " 'CASP3',\n",
411
+ " 'C3',\n",
412
+ " 'OLIG2',\n",
413
+ " 'CCL11',\n",
414
+ " 'CD86',\n",
415
+ " 'AURKA',\n",
416
+ " 'IL2',\n",
417
+ " 'F2',\n",
418
+ " 'IDO1',\n",
419
+ " 'IL4',\n",
420
+ " 'RPS6KB1',\n",
421
+ " 'ABAT',\n",
422
+ " 'CCL3',\n",
423
+ " 'STAT1',\n",
424
+ " 'CD80',\n",
425
+ " 'IL5',\n",
426
+ " 'IL6'],\n",
427
+ " 'entrez_ids': [18,\n",
428
+ " 3565,\n",
429
+ " 4790,\n",
430
+ " 836,\n",
431
+ " 3551,\n",
432
+ " 3620,\n",
433
+ " 6772,\n",
434
+ " 718,\n",
435
+ " 2147,\n",
436
+ " 3558,\n",
437
+ " 3569,\n",
438
+ " 6347,\n",
439
+ " 6356,\n",
440
+ " 727,\n",
441
+ " 6790,\n",
442
+ " 3567,\n",
443
+ " 942,\n",
444
+ " 6348,\n",
445
+ " 10215,\n",
446
+ " 6198,\n",
447
+ " 5550,\n",
448
+ " 1232,\n",
449
+ " 941]},\n",
450
+ " 'predicted_targets': {'symbols': ['IFNG', 'PTGS1', 'PTGS2', 'TPMT'],\n",
451
+ " 'entrez_ids': [3458, 5742, 5743, 7172]}})"
452
+ ]
453
+ },
454
+ "execution_count": 10,
455
+ "metadata": {},
456
+ "output_type": "execute_result"
457
+ }
458
+ ],
459
+ "source": [
460
+ "# See what an entry in the mapping looks like\n",
461
+ "list(TCM_cpds.items())[0]"
462
+ ]
463
+ },
464
+ {
465
+ "cell_type": "markdown",
466
+ "id": "324be182-fd49-4b5f-bc65-7c59ff56e46b",
467
+ "metadata": {},
468
+ "source": [
469
+ "Next, let's count how many times each compound's targets pop up in the generated signatures. To see if the overlap between targets and important aging genes is significant, we'll apply Fisher's exact test. As the backround set, we shall use the total number of genes used by P3GPT — 25,332. "
470
+ ]
471
+ },
472
+ {
473
+ "cell_type": "code",
474
+ "execution_count": 20,
475
+ "id": "853128d9-bafb-407a-9234-7376914ab9c0",
476
+ "metadata": {},
477
+ "outputs": [],
478
+ "source": [
479
+ "list_len = 100\n",
480
+ "enrich_stats = pd.DataFrame(None, index = None,\n",
481
+ " columns = ['compound', 'cid', 'tissue', 'domain',\n",
482
+ " 'p_value', 'target_type', 'N_targets',\n",
483
+ " 'hit_targets'])\n",
484
+ "for cid in TCM_cpds:\n",
485
+ " tg_sets = {'known':set(TCM_cpds[cid]['known_targets']['symbols']),\n",
486
+ " \"predicted\":set(TCM_cpds[cid]['predicted_targets']['symbols'])}\n",
487
+ " tg_sets[\"both\"] = tg_sets['known'] | tg_sets['predicted']\n",
488
+ " for tis in generations:\n",
489
+ " for dom in generations[tis]:\n",
490
+ " p3_set = set(generations[tis][dom]['up'][:list_len] + generations[tis][dom]['down'][:list_len])\n",
491
+ " for tg_type, tg_set in tg_sets.items():\n",
492
+ " p3_hits = tg_set & p3_set\n",
493
+ " # cols: in targets, not in targets\n",
494
+ " # rows: in P3 output, not in P3 output\n",
495
+ " cont_table = (a := np.array([[len(p3_hits), len(tg_set - p3_hits)],\n",
496
+ " [len(p3_hits - tg_set), 0]])) + \\\n",
497
+ " np.array([[0, 0],\n",
498
+ " [0, 25332 - a.sum().sum()]])\n",
499
+ "\n",
500
+ " F_exact = fisher_exact(table=cont_table, alternative='greater')\n",
501
+ " #['compound', 'tissue', 'domain', 'p_value', 'target_type']\n",
502
+ " enrich_stats.loc[enrich_stats.shape[0],:] = [TCM_cpds[cid]['title'],\n",
503
+ " cid,\n",
504
+ " tis,\n",
505
+ " dom,\n",
506
+ " F_exact[1],\n",
507
+ " tg_type,\n",
508
+ " len(tg_set),\n",
509
+ " len(p3_hits)]"
510
+ ]
511
+ },
512
+ {
513
+ "cell_type": "code",
514
+ "execution_count": 21,
515
+ "id": "9bb28a7d-b1f2-4159-b93c-3acad16d8d22",
516
+ "metadata": {},
517
+ "outputs": [
518
+ {
519
+ "name": "stdout",
520
+ "output_type": "stream",
521
+ "text": [
522
+ "Total number of tests: 47448\n"
523
+ ]
524
+ }
525
+ ],
526
+ "source": [
527
+ "print(f\"Total number of tests: {enrich_stats.shape[0]}\")"
528
+ ]
529
+ },
530
+ {
531
+ "cell_type": "markdown",
532
+ "id": "170d3779-0f09-4b93-b26c-2cf2c75bd712",
533
+ "metadata": {},
534
+ "source": [
535
+ "Since we have run thousands of statistical tests, it makes sense to include a multiple comparison correction"
536
+ ]
537
+ },
538
+ {
539
+ "cell_type": "code",
540
+ "execution_count": 22,
541
+ "id": "d087b76b-978d-4f87-81fc-97e3b8c0dad7",
542
+ "metadata": {},
543
+ "outputs": [
544
+ {
545
+ "name": "stdout",
546
+ "output_type": "stream",
547
+ "text": [
548
+ "Remaining entries: 8866\n",
549
+ "Compounds that passed 1+ enrichment tests: 424\n"
550
+ ]
551
+ }
552
+ ],
553
+ "source": [
554
+ "enrich_stats[\"corr_Pv\"] = [min(1, x) for x in enrich_stats[\"p_value\"] * enrich_stats.shape[0]]\n",
555
+ "enrich_stats = enrich_stats[enrich_stats[\"corr_Pv\"]<.001]\n",
556
+ "print(f\"Remaining entries: {enrich_stats.shape[0]}\")\n",
557
+ "print(f'Compounds that passed 1+ enrichment tests: {len(enrich_stats.compound.unique())}')"
558
+ ]
559
+ },
560
+ {
561
+ "cell_type": "markdown",
562
+ "id": "c9817c0b-51f2-4b7a-913c-9741345f1895",
563
+ "metadata": {},
564
+ "source": [
565
+ "We can now zoom in on the compoundы that passed the test.\n",
566
+ "\n",
567
+ "Out of all 659 TCM compounds, the targets of 424 of them significantly overlapped with an aging aging signature.\n",
568
+ "Let's trim the table of significant enrichments even further by considering only the documented targets. \n",
569
+ "\n",
570
+ "> 💡 **Note:** You may choose a different target confirmation level. Looking for enrichment in known targets creates a bias toward well-studied compounds. Such popular anti-aging compounds as curcumin, for example, have thousands of publications covering their cellular interactions. Meanwhile, an obscure but an equally potent geroprotector may have zero known targets. Repeating this workflow with predicted targets is likely to provide more novel compounds."
571
+ ]
572
+ },
573
+ {
574
+ "cell_type": "code",
575
+ "execution_count": 23,
576
+ "id": "3f71d393-2f67-4257-97a4-9a46b62ff334",
577
+ "metadata": {},
578
+ "outputs": [
579
+ {
580
+ "name": "stdout",
581
+ "output_type": "stream",
582
+ "text": [
583
+ "Remaining entries: 375\n",
584
+ "Compounds that passed 1+ enrichment tests: 80\n"
585
+ ]
586
+ }
587
+ ],
588
+ "source": [
589
+ "confirmation_level = 'known'\n",
590
+ "enrich_stats = enrich_stats[enrich_stats[\"target_type\"]==confirmation_level]\n",
591
+ "print(f\"Remaining entries: {enrich_stats.shape[0]}\")\n",
592
+ "print(f'Compounds that passed 1+ enrichment tests: {len(enrich_stats.compound.unique())}')"
593
+ ]
594
+ },
595
+ {
596
+ "cell_type": "markdown",
597
+ "id": "acf8a7b2-21c4-4394-adb1-77fba7e950f4",
598
+ "metadata": {},
599
+ "source": [
600
+ "80 compounds is a much more manageable compound selection.\n",
601
+ "\n",
602
+ "These TCM compounds are targetting the genes involved in aging and may be considered for futher investigation *in vitro* or *in vivo*.\n",
603
+ "But what if we we wanted to compose a cocktail of TCM ingredients that will slow down the aging processes in as many tissues as possible?\n",
604
+ "Are there any TCM compounds that have been detected in 8/8 tissues?\n"
605
+ ]
606
+ },
607
+ {
608
+ "cell_type": "code",
609
+ "execution_count": 32,
610
+ "id": "812aaa05-8ead-4cfb-b9f4-59ea7b25ead9",
611
+ "metadata": {},
612
+ "outputs": [
613
+ {
614
+ "name": "stdout",
615
+ "output_type": "stream",
616
+ "text": [
617
+ "--- N potential geroprotectors identified in each tissue ---\n",
618
+ "N skin-compounds = 38\n",
619
+ "N muscle-compounds = 25\n",
620
+ "N whole blood-compounds = 48\n",
621
+ "N epithelium-compounds = 19\n",
622
+ "N artery-compounds = 32\n",
623
+ "N fat tissue-compounds = 17\n",
624
+ "N brain-compounds = 47\n",
625
+ "N liver-compounds = 23\n",
626
+ "\n",
627
+ "--- TCM ingredients targeting aging in 6 tissues (N = 4) ---\n",
628
+ "alpha-tocopherol\n",
629
+ "capsaicin\n",
630
+ "baicalin\n",
631
+ "testosterone\n",
632
+ "\n",
633
+ "--- TCM ingredients targeting aging in 7 tissues (N = 2) ---\n",
634
+ "kaempferol\n",
635
+ "triptolide\n",
636
+ "\n",
637
+ "--- TCM ingredients targeting aging in 8 tissues (N = 8) ---\n",
638
+ "colchicine\n",
639
+ "quercetin\n",
640
+ "apigenin\n",
641
+ "resveratrol\n",
642
+ "curcumin\n",
643
+ "brd-k55591206;epigallocatechin 3-gallate\n",
644
+ "estradiol\n",
645
+ "progesterone\n"
646
+ ]
647
+ }
648
+ ],
649
+ "source": [
650
+ "cpd_counts = {}\n",
651
+ "print(\"--- N potential geroprotectors identified in each tissue ---\")\n",
652
+ "for t in tissue_options:\n",
653
+ " tis_df = enrich_stats[(enrich_stats.tissue == t)& \\\n",
654
+ " (enrich_stats.target_type == confirmation_level)]\n",
655
+ " print(f'N {t}-compounds = {len(tis_df.compound.unique())}')\n",
656
+ " for cpd in tis_df.compound.unique():\n",
657
+ " if cpd not in cpd_counts:\n",
658
+ " cpd_counts[cpd] = 0\n",
659
+ " cpd_counts[cpd]+=1\n",
660
+ "\n",
661
+ "cpds_by_tis_count = dict()\n",
662
+ "for i in range(1,9):\n",
663
+ " cpds_by_tis_count[i] = [x for x,y in cpd_counts.items() if y == i]\n",
664
+ "\n",
665
+ "for i in (6,7,8):\n",
666
+ " print(f\"\\n--- TCM ingredients targeting aging in {i} tissues (N = {len(cpds_by_tis_count[i])}) ---\")\n",
667
+ " print(*cpds_by_tis_count[i], sep ='\\n')\n",
668
+ " \n"
669
+ ]
670
+ },
671
+ {
672
+ "cell_type": "markdown",
673
+ "id": "931bf007-843e-41b3-b0ef-be7d754b025c",
674
+ "metadata": {},
675
+ "source": [
676
+ "A total of 14 compounds were detected as potential geroprotectors in 6+ tissues, while almost half of all detected ingredients affect agig only in one tissue.\n",
677
+ "Among them, 8 were targetting the aging signatures in 8/8 tissues'. \n",
678
+ "\n",
679
+ "<center><img src=\"./cpds_affecting_tissue_aging.svg\" width=\"600\" ></center>\n",
680
+ "\n",
681
+ "> 💡 **Note:** The compounds we call \"potential geroprotectors\" may in fact accelerate aging. When we difined important aging genes, we did binned the up- and down-regulated genes together, although the directionality of change is extremely important in this setting. Additionally, the mechanism of action (agonist/antagonist) of a TCM ingredient needs to be taken into account. We can lean on the fact that most compounds in BATMAN are generally considered safe or even beneficial to the organism, but ultimately, the final list of TCM geroprotectors needs to be manually reviewed."
682
+ ]
683
+ },
684
+ {
685
+ "cell_type": "markdown",
686
+ "id": "16a8fec8-6587-41d0-bde7-1d1f42a69812",
687
+ "metadata": {},
688
+ "source": [
689
+ "## 3. Selecting a TCM formula\n",
690
+ "\n",
691
+ "Now that we have a list of 14 compounds with a systemic effect on aging, we can leverage BATMAN to do either of the following:\n",
692
+ "\n",
693
+ "- Screen existing TCM formulas to find those that contain as many compounds from our list as possible;\n",
694
+ "- Make a new TCM formula ourselves by selecting herbs one by one.\n",
695
+ "\n",
696
+ "We will demonstrate only the former option in this notebook."
697
+ ]
698
+ },
699
+ {
700
+ "cell_type": "code",
701
+ "execution_count": 38,
702
+ "id": "189c2367-4a63-44b5-a0be-1a9d5b65c35c",
703
+ "metadata": {},
704
+ "outputs": [],
705
+ "source": [
706
+ "# Here are the CIDs of the potential geroprotectors \n",
707
+ "# we identified in section 2\n",
708
+ "cids = [969516, #curcumin\n",
709
+ " 445154, #resveratrol\n",
710
+ " 5280343, #quercetin\n",
711
+ " 6167, #colchicine\n",
712
+ " 5280443, #apigening\n",
713
+ " 65064, #EGCG3\n",
714
+ " 5757, # estradiol\n",
715
+ " 5994, # progesterone\n",
716
+ " 5280863, # kaempferol\n",
717
+ " 107985, # triptolide\n",
718
+ " 14985, #alpha-tocopherol\n",
719
+ " 1548943, # Capsaicin\n",
720
+ " 64982, # Baicalin\n",
721
+ " 6013, # Testosterone\n",
722
+ " ]"
723
+ ]
724
+ },
725
+ {
726
+ "cell_type": "markdown",
727
+ "id": "d1969f91-d51b-4974-b1d6-fe9f834f7462",
728
+ "metadata": {},
729
+ "source": [
730
+ "For the sake of brevity, we have externalized the scripts used to parse TCMDB files into linked lists of ingredients-herbs-formulas.\n",
731
+ "And to save you some time parsing the files from BATMAN's [download page](http://bionet.ncpsb.org.cn/batman-tcm/#/download), we have uploaded the dataset to Hugging Face. Go to the [batman2](https://huggingface.co/datasets/f-galkin/batman2) repository to preview the data. \n",
732
+ "\n",
733
+ "Feel free reuse the codes and files we prepared for your own TCM-related projects. If you don't need our `TCMDB` wrapper, you can access BATMAN data by executing the following commands:\n",
734
+ "```python\n",
735
+ "from datasets import load_dataset\n",
736
+ "batman_ingredients = load_dataset(\"f-galkin/batman2\", \"batman_ingredients\")\n",
737
+ "batman_herbs = load_dataset(\"f-galkin/batman2\", \"batman_herbs\")\n",
738
+ "batman_formulas = load_dataset(\"f-galkin/batman2\", \"batman_formulas\")\n",
739
+ "```"
740
+ ]
741
+ },
742
+ {
743
+ "cell_type": "code",
744
+ "execution_count": 34,
745
+ "id": "73c59513-f48e-4570-8c66-8bfc5cfb547d",
746
+ "metadata": {},
747
+ "outputs": [],
748
+ "source": [
749
+ "from P3LIB.formula_picker import TCMDB\n",
750
+ "from datasets import load_dataset\n",
751
+ "db = TCMDB.download_from_hf()"
752
+ ]
753
+ },
754
+ {
755
+ "cell_type": "markdown",
756
+ "id": "2acc6a91-51ab-4626-993e-5689eb03858c",
757
+ "metadata": {},
758
+ "source": [
759
+ "We formatted the BATMAN files (accessed on 8/1/2024) to support basic search operations, such as looking up a TCM formula that has the maximum amount of the compounds provided in a query."
760
+ ]
761
+ },
762
+ {
763
+ "cell_type": "code",
764
+ "execution_count": 41,
765
+ "id": "ee611008-0335-4243-824e-2c78e032c844",
766
+ "metadata": {},
767
+ "outputs": [
768
+ {
769
+ "name": "stdout",
770
+ "output_type": "stream",
771
+ "text": [
772
+ "13 compounds of 14 are encountered in the following TCM formulas:\n",
773
+ "['ZHANG ZHOU SHEN QU']\n"
774
+ ]
775
+ }
776
+ ],
777
+ "source": [
778
+ "p3_formula = db.select_formula_by_cpd(cids)\n",
779
+ "print(\"%s compounds of 14 are encountered in the following TCM formulas:\\n%s\"%p3_formula)"
780
+ ]
781
+ },
782
+ {
783
+ "cell_type": "markdown",
784
+ "id": "60706086-fc5f-4b10-9355-d5e1f11e3507",
785
+ "metadata": {},
786
+ "source": [
787
+ "Not a single TCM formula out of the 54,832 described in BATMAN2 has all the 14 potential geroprotectors we selected.\n",
788
+ "And onlu 1 formula has 13/14 compounds — ZHANG ZHOU SHEN QU. Let's take a closer look at it."
789
+ ]
790
+ },
791
+ {
792
+ "cell_type": "code",
793
+ "execution_count": 45,
794
+ "id": "d2ddf3f9-5515-49e2-8409-0caaf5aac4cd",
795
+ "metadata": {},
796
+ "outputs": [
797
+ {
798
+ "name": "stdout",
799
+ "output_type": "stream",
800
+ "text": [
801
+ "The contents of the ZHANG ZHOU SHEN QU (漳州神曲) TCM formula\n",
802
+ "1. Weeping forsythia (连翘)\n",
803
+ "2. Tsaoko (草果)\n",
804
+ "3. Yerbadetajo (墨旱莲)\n",
805
+ "4. Haichow elsholtzia (香薷)\n",
806
+ "5. Common anemarrhena (知母)\n",
807
+ "6. Medicine terminalia equivalent plant: terminalia chebula var tomentella (诃子)\n",
808
+ "7. Slenderstyle acanthopanax root-bark equivalent plant: acanthopanax sessiliflorus , acanthopanax senticosus (五加皮)\n",
809
+ "8. Dried tangerine peel (陈皮)\n",
810
+ "9. Bark of officinal magnolia (厚朴)\n",
811
+ "10. Perilia fruit (紫苏子)\n",
812
+ "11. Oriental waterplantain (泽泻)\n",
813
+ "12. Chinese hawthorn equivalent plant: crataegus pinnatifida var major (山楂)\n",
814
+ "13. Common hogfennel equivalent plant: peucedanum praeruptorum , peucedanum longshengens, peucedanum rubricaule , ligusticum brachylobum (前胡)\n",
815
+ "14. Glabrous greenbrier equivalent plant: smilax menispermoidea (土茯苓)\n",
816
+ "15. Chinese star anise (八角茴香)\n",
817
+ "16. White mulberry branch (桑枝)\n",
818
+ "17. (槟榔)\n",
819
+ "18. Wild mint (薄荷)\n",
820
+ "19. Pummelo peel (化橘红)\n",
821
+ "20. Mongolian snakegourd root (天花粉)\n",
822
+ "21. Red peony root (赤芍)\n",
823
+ "22. Grassleaved sweetflag equivalent plant: acorus gramineus (石菖蒲)\n",
824
+ "23. Lesser galangal (高粱姜)\n",
825
+ "24. Common yam equivalent plant: dioscorea japonica (山药)\n",
826
+ "25. Fineleaf schizonepeta (荆芥)\n",
827
+ "26. Doubleteeth pubescent angelica equivalent plant: heracleum lanatum (独活)\n",
828
+ "27. Amur corktree equivalent plant: phellodendron chinense , phellodendron chinense var glabriusculum, phellodendron amurense var wilsonii (黄柏)\n",
829
+ "28. Barley germinating fruit (麦芽)\n",
830
+ "29. Incised notopterygium equivalent plant: notopterygium forbesii (羌活)\n",
831
+ "30. Nutgrass galingale (香附)\n",
832
+ "31. Seed of gordon euryale (芡实)\n",
833
+ "32. Largehead atractylodes (白术)\n",
834
+ "33. Seville orange young fruit equivalent plant: citrus wilsonii , poncirus trifoliata (枳实)\n",
835
+ "34. Rangooncreeper (使君子)\n",
836
+ "35. Baikal skullcap equivalent plant: scutellaria amoena , scutellaria viscidula, scutellaria likiangensis , scutellaria rehderiana, scutellaria hypericifolia (黄芩)\n",
837
+ "36. Common nutmeg (肉豆蔻)\n",
838
+ "37. Common burreed (三棱)\n",
839
+ "38. Ural licorice equivalent plant: glycyrrhiza inflata, glycyrrhiza glabra, glycyrrhiza kansuensis, glycyrrhiza aspera, glycyrrhiza yunnanensis , glycyrrhiza squamulosa (甘草)\n",
840
+ "39. Common aucklandia (costustoot) (木香)\n",
841
+ "40. Zedoray rhizome (莪术)\n",
842
+ "41. Medicinal rhubarb equivalent plant: rheum palmatum, rheum tanguticum (大黄)\n",
843
+ "42. Celery wormwood (青蒿)\n",
844
+ "43. Common perilla leaf equivalent plant: perilla frutescens var acuta (紫苏叶)\n",
845
+ "44. Fruit of seville orange (枳壳)\n",
846
+ "45. Divaricate saposhnikovia (防风)\n",
847
+ "46. Fennel fruit (小茴香)\n",
848
+ "47. Root - bark of chinese wolfberry (地骨皮)\n",
849
+ "48. Cassiabarktree twig (桂枝)\n",
850
+ "49. Swordlike atractylodes equivalent plant: atractylodes chinensis, atractylodes japonica (苍术)\n",
851
+ "50. Lobed kudzuvine root equivalent plant: pueraria thomsonii, pueraria edulis, pueraria omeiensis , pueraria phaseoloides (葛根)\n",
852
+ "51. Indian bread (茯苓)\n",
853
+ "52. Rice bean (赤小豆)\n",
854
+ "53. Dahurian angelica (白芷)\n",
855
+ "54. Cassiabarktree (肉桂)\n",
856
+ "55. Balloonflower (桔梗)\n",
857
+ "56. Chinese nardostachys equivalent plant: nardostachys jatamansi (甘松)\n",
858
+ "57. Galanga resurrectionlily (山柰)\n",
859
+ "58. Bitter apricot seed (苦杏仁)\n",
860
+ "59. White hyacinth bean (白扁豆)\n",
861
+ "60. Bunge pricklyash equivalent plant: zanthoxylum schinifolium (花椒)\n",
862
+ "61. Bugbane equivalent plant: cimicifuga dahurica, cimicifuga heracleifolia (升麻)\n",
863
+ "62. Chinese ephedra equivalent plant: ephedra equisetina , ephedra intermedia , ephedra minuta , ephedra monosperma, ephedra likiangensis, ephedra saxatilis, ephedra gerardiana (麻黄)\n",
864
+ "63. Jobstears seed (薏苡仁)\n",
865
+ "64. Garden radish seed (莱菔子)\n",
866
+ "65. Common selfheal (夏枯草)\n",
867
+ "66. Common ginger dried rhizome (干姜)\n",
868
+ "67. Shiny bugleweed (泽兰)\n",
869
+ "68. Green tangerine peel (青皮)\n",
870
+ "69. Chinese thorowax equivalent plant: bupleurum scorzonerifolium, bupleurum smithii var parvifolium , bupleurum aureum, bupleurum longiradiatum, bupleurum marginatum , bupleurum marginatum var stenophyllum, bupleurum angustissimum, bupleurum chaishoui, bupleurum wenchuanense , bupleurum sibiricum, bupleurum yinchowense (柴胡)\n",
871
+ "70. Cape jasmine fruit (栀子)\n",
872
+ "71. Virgate wormwood herb (茵陈)\n",
873
+ "72. Clove tree (丁香)\n",
874
+ "73. Combined spicebush (乌药)\n",
875
+ "74. Cablin potchouli (广霍香)\n",
876
+ "75. Villous amomum equivalent plant: amomum xanthioides , amomum longiligulare (砂仁)\n"
877
+ ]
878
+ }
879
+ ],
880
+ "source": [
881
+ "pinyin_name = 'ZHANG ZHOU SHEN QU'\n",
882
+ "print(f'The contents of the {pinyin_name} ({db.formulas[pinyin_name].synonyms[0]}) TCM formula')\n",
883
+ "for i, herb in enumerate(db.formulas[pinyin_name].herbs):\n",
884
+ " print(f\"{i+1}. {herb.synonyms[2]} ({herb.synonyms[1]})\")\n"
885
+ ]
886
+ },
887
+ {
888
+ "cell_type": "markdown",
889
+ "id": "690e5223-3091-4dd0-9944-e6c4c7323abf",
890
+ "metadata": {},
891
+ "source": [
892
+ "The formula we ended up with is quite complex and contains 75 herbal components, such as nutmeg, turmeric, scutellaria, citrus peels, patchouli and others.\n",
893
+ "\n",
894
+ "You can learn more about this composition on other [TCM databasess](https://bidd.group/TCMID/tcmf.php?formula=TCMFx6833). For now, that is where we shall conclude our search"
895
+ ]
896
+ }
897
+ ],
898
+ "metadata": {
899
+ "kernelspec": {
900
+ "display_name": "ism311",
901
+ "language": "python",
902
+ "name": "myenv"
903
+ },
904
+ "language_info": {
905
+ "codemirror_mode": {
906
+ "name": "ipython",
907
+ "version": 3
908
+ },
909
+ "file_extension": ".py",
910
+ "mimetype": "text/x-python",
911
+ "name": "python",
912
+ "nbconvert_exporter": "python",
913
+ "pygments_lexer": "ipython3",
914
+ "version": "3.11.0"
915
+ }
916
+ },
917
+ "nbformat": 4,
918
+ "nbformat_minor": 5
919
+ }
demo/P3GPT_compound_targets.pckl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9e14e75bf8d027e06b3048b9a30b165dd1b696c729c2b94736d330f28ca55824
3
+ size 1137080
demo/cpds_affecting_tissue_aging.svg ADDED
demo/experiment_scheme.png ADDED

Git LFS Details

  • SHA256: bc3a21c401ffa4bb63dcbc399cc4160f12b0877374d1e02e3ce984eebd386dbd
  • Pointer size: 132 Bytes
  • Size of remote file: 1.67 MB