AmelieSchreiber commited on
Commit
4c40650
1 Parent(s): 889629e

Upload ptm_data_preprocessing.ipynb

Browse files
Files changed (1) hide show
  1. ptm_data_preprocessing.ipynb +633 -0
ptm_data_preprocessing.ipynb ADDED
@@ -0,0 +1,633 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 47,
6
+ "id": "945a82aa-1398-422a-98df-b3db93973271",
7
+ "metadata": {
8
+ "tags": []
9
+ },
10
+ "outputs": [
11
+ {
12
+ "data": {
13
+ "text/html": [
14
+ "<div>\n",
15
+ "<style scoped>\n",
16
+ " .dataframe tbody tr th:only-of-type {\n",
17
+ " vertical-align: middle;\n",
18
+ " }\n",
19
+ "\n",
20
+ " .dataframe tbody tr th {\n",
21
+ " vertical-align: top;\n",
22
+ " }\n",
23
+ "\n",
24
+ " .dataframe thead th {\n",
25
+ " text-align: right;\n",
26
+ " }\n",
27
+ "</style>\n",
28
+ "<table border=\"1\" class=\"dataframe\">\n",
29
+ " <thead>\n",
30
+ " <tr style=\"text-align: right;\">\n",
31
+ " <th></th>\n",
32
+ " <th>Entry</th>\n",
33
+ " <th>Protein families</th>\n",
34
+ " <th>Modified residue</th>\n",
35
+ " <th>Sequence</th>\n",
36
+ " </tr>\n",
37
+ " </thead>\n",
38
+ " <tbody>\n",
39
+ " <tr>\n",
40
+ " <th>0</th>\n",
41
+ " <td>A0A009GHC8</td>\n",
42
+ " <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
43
+ " <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
44
+ " <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
45
+ " </tr>\n",
46
+ " <tr>\n",
47
+ " <th>1</th>\n",
48
+ " <td>A0A009HTZ2</td>\n",
49
+ " <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
50
+ " <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
51
+ " <td>MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...</td>\n",
52
+ " </tr>\n",
53
+ " <tr>\n",
54
+ " <th>2</th>\n",
55
+ " <td>A0A009IVE2</td>\n",
56
+ " <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
57
+ " <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
58
+ " <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
59
+ " </tr>\n",
60
+ " <tr>\n",
61
+ " <th>3</th>\n",
62
+ " <td>A0A009MYL5</td>\n",
63
+ " <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
64
+ " <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
65
+ " <td>MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...</td>\n",
66
+ " </tr>\n",
67
+ " <tr>\n",
68
+ " <th>4</th>\n",
69
+ " <td>A0A009PHM9</td>\n",
70
+ " <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
71
+ " <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
72
+ " <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
73
+ " </tr>\n",
74
+ " </tbody>\n",
75
+ "</table>\n",
76
+ "</div>"
77
+ ],
78
+ "text/plain": [
79
+ " Entry Protein families \\\n",
80
+ "0 A0A009GHC8 Precorrin methyltransferase family; Precorrin ... \n",
81
+ "1 A0A009HTZ2 Precorrin methyltransferase family; Precorrin ... \n",
82
+ "2 A0A009IVE2 Precorrin methyltransferase family; Precorrin ... \n",
83
+ "3 A0A009MYL5 Precorrin methyltransferase family; Precorrin ... \n",
84
+ "4 A0A009PHM9 Precorrin methyltransferase family; Precorrin ... \n",
85
+ "\n",
86
+ " Modified residue \\\n",
87
+ "0 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
88
+ "1 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
89
+ "2 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
90
+ "3 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
91
+ "4 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
92
+ "\n",
93
+ " Sequence \n",
94
+ "0 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n",
95
+ "1 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n",
96
+ "2 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n",
97
+ "3 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n",
98
+ "4 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... "
99
+ ]
100
+ },
101
+ "execution_count": 47,
102
+ "metadata": {},
103
+ "output_type": "execute_result"
104
+ }
105
+ ],
106
+ "source": [
107
+ "import pandas as pd\n",
108
+ "\n",
109
+ "# Load the TSV file\n",
110
+ "file_path = 'PTM/uniprotkb_family_AND_ft_mod_res_AND_pro_2023_10_02.tsv'\n",
111
+ "data = pd.read_csv(file_path, sep='\\t')\n",
112
+ "\n",
113
+ "# Display the first few rows of the data\n",
114
+ "data.head()\n"
115
+ ]
116
+ },
117
+ {
118
+ "cell_type": "code",
119
+ "execution_count": 48,
120
+ "id": "a03f8ff8-0612-4f8c-bccd-49fde3dce0f5",
121
+ "metadata": {
122
+ "tags": []
123
+ },
124
+ "outputs": [
125
+ {
126
+ "data": {
127
+ "text/html": [
128
+ "<div>\n",
129
+ "<style scoped>\n",
130
+ " .dataframe tbody tr th:only-of-type {\n",
131
+ " vertical-align: middle;\n",
132
+ " }\n",
133
+ "\n",
134
+ " .dataframe tbody tr th {\n",
135
+ " vertical-align: top;\n",
136
+ " }\n",
137
+ "\n",
138
+ " .dataframe thead th {\n",
139
+ " text-align: right;\n",
140
+ " }\n",
141
+ "</style>\n",
142
+ "<table border=\"1\" class=\"dataframe\">\n",
143
+ " <thead>\n",
144
+ " <tr style=\"text-align: right;\">\n",
145
+ " <th></th>\n",
146
+ " <th>Entry</th>\n",
147
+ " <th>Protein families</th>\n",
148
+ " <th>Modified residue</th>\n",
149
+ " <th>Sequence</th>\n",
150
+ " <th>PTM sites</th>\n",
151
+ " </tr>\n",
152
+ " </thead>\n",
153
+ " <tbody>\n",
154
+ " <tr>\n",
155
+ " <th>0</th>\n",
156
+ " <td>A0A009GHC8</td>\n",
157
+ " <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
158
+ " <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
159
+ " <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
160
+ " <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
161
+ " </tr>\n",
162
+ " <tr>\n",
163
+ " <th>1</th>\n",
164
+ " <td>A0A009HTZ2</td>\n",
165
+ " <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
166
+ " <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
167
+ " <td>MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...</td>\n",
168
+ " <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
169
+ " </tr>\n",
170
+ " <tr>\n",
171
+ " <th>2</th>\n",
172
+ " <td>A0A009IVE2</td>\n",
173
+ " <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
174
+ " <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
175
+ " <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
176
+ " <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
177
+ " </tr>\n",
178
+ " <tr>\n",
179
+ " <th>3</th>\n",
180
+ " <td>A0A009MYL5</td>\n",
181
+ " <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
182
+ " <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
183
+ " <td>MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...</td>\n",
184
+ " <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
185
+ " </tr>\n",
186
+ " <tr>\n",
187
+ " <th>4</th>\n",
188
+ " <td>A0A009PHM9</td>\n",
189
+ " <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
190
+ " <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
191
+ " <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
192
+ " <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
193
+ " </tr>\n",
194
+ " </tbody>\n",
195
+ "</table>\n",
196
+ "</div>"
197
+ ],
198
+ "text/plain": [
199
+ " Entry Protein families \\\n",
200
+ "0 A0A009GHC8 Precorrin methyltransferase family; Precorrin ... \n",
201
+ "1 A0A009HTZ2 Precorrin methyltransferase family; Precorrin ... \n",
202
+ "2 A0A009IVE2 Precorrin methyltransferase family; Precorrin ... \n",
203
+ "3 A0A009MYL5 Precorrin methyltransferase family; Precorrin ... \n",
204
+ "4 A0A009PHM9 Precorrin methyltransferase family; Precorrin ... \n",
205
+ "\n",
206
+ " Modified residue \\\n",
207
+ "0 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
208
+ "1 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
209
+ "2 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
210
+ "3 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
211
+ "4 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
212
+ "\n",
213
+ " Sequence \\\n",
214
+ "0 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n",
215
+ "1 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n",
216
+ "2 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n",
217
+ "3 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n",
218
+ "4 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n",
219
+ "\n",
220
+ " PTM sites \n",
221
+ "0 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n",
222
+ "1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n",
223
+ "2 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n",
224
+ "3 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n",
225
+ "4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... "
226
+ ]
227
+ },
228
+ "execution_count": 48,
229
+ "metadata": {},
230
+ "output_type": "execute_result"
231
+ }
232
+ ],
233
+ "source": [
234
+ "import re\n",
235
+ "\n",
236
+ "def get_ptm_sites(row):\n",
237
+ " # Extract the positions of modified residues from the 'Modified residue' column\n",
238
+ " modified_positions = [int(i) for i in re.findall(r'MOD_RES (\\d+)', row['Modified residue'])]\n",
239
+ " \n",
240
+ " # Create a list of zeros of length equal to the protein sequence\n",
241
+ " ptm_sites = [0] * len(row['Sequence'])\n",
242
+ " \n",
243
+ " # Replace the zeros with ones at the positions of modified residues\n",
244
+ " for position in modified_positions:\n",
245
+ " # Subtracting 1 because positions are 1-indexed, but lists are 0-indexed\n",
246
+ " ptm_sites[position - 1] = 1\n",
247
+ " \n",
248
+ " return ptm_sites\n",
249
+ "\n",
250
+ "# Apply the function to each row in the DataFrame\n",
251
+ "data['PTM sites'] = data.apply(get_ptm_sites, axis=1)\n",
252
+ "\n",
253
+ "# Display the first few rows of the updated DataFrame\n",
254
+ "data.head()\n"
255
+ ]
256
+ },
257
+ {
258
+ "cell_type": "code",
259
+ "execution_count": 50,
260
+ "id": "5d2e5043-e2f9-44ec-899b-7dad4f83f823",
261
+ "metadata": {
262
+ "tags": []
263
+ },
264
+ "outputs": [
265
+ {
266
+ "data": {
267
+ "text/html": [
268
+ "<div>\n",
269
+ "<style scoped>\n",
270
+ " .dataframe tbody tr th:only-of-type {\n",
271
+ " vertical-align: middle;\n",
272
+ " }\n",
273
+ "\n",
274
+ " .dataframe tbody tr th {\n",
275
+ " vertical-align: top;\n",
276
+ " }\n",
277
+ "\n",
278
+ " .dataframe thead th {\n",
279
+ " text-align: right;\n",
280
+ " }\n",
281
+ "</style>\n",
282
+ "<table border=\"1\" class=\"dataframe\">\n",
283
+ " <thead>\n",
284
+ " <tr style=\"text-align: right;\">\n",
285
+ " <th></th>\n",
286
+ " <th>Entry</th>\n",
287
+ " <th>Protein families</th>\n",
288
+ " <th>Modified residue</th>\n",
289
+ " <th>Sequence</th>\n",
290
+ " <th>PTM sites</th>\n",
291
+ " </tr>\n",
292
+ " </thead>\n",
293
+ " <tbody>\n",
294
+ " <tr>\n",
295
+ " <th>0</th>\n",
296
+ " <td>A0A009GHC8</td>\n",
297
+ " <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
298
+ " <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
299
+ " <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
300
+ " <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
301
+ " </tr>\n",
302
+ " <tr>\n",
303
+ " <th>1</th>\n",
304
+ " <td>A0A009HTZ2</td>\n",
305
+ " <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
306
+ " <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
307
+ " <td>MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...</td>\n",
308
+ " <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
309
+ " </tr>\n",
310
+ " <tr>\n",
311
+ " <th>2</th>\n",
312
+ " <td>A0A009IVE2</td>\n",
313
+ " <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
314
+ " <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
315
+ " <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
316
+ " <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
317
+ " </tr>\n",
318
+ " <tr>\n",
319
+ " <th>3</th>\n",
320
+ " <td>A0A009MYL5</td>\n",
321
+ " <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
322
+ " <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
323
+ " <td>MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...</td>\n",
324
+ " <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
325
+ " </tr>\n",
326
+ " <tr>\n",
327
+ " <th>4</th>\n",
328
+ " <td>A0A009PHM9</td>\n",
329
+ " <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
330
+ " <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
331
+ " <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
332
+ " <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
333
+ " </tr>\n",
334
+ " </tbody>\n",
335
+ "</table>\n",
336
+ "</div>"
337
+ ],
338
+ "text/plain": [
339
+ " Entry Protein families \\\n",
340
+ "0 A0A009GHC8 Precorrin methyltransferase family; Precorrin ... \n",
341
+ "1 A0A009HTZ2 Precorrin methyltransferase family; Precorrin ... \n",
342
+ "2 A0A009IVE2 Precorrin methyltransferase family; Precorrin ... \n",
343
+ "3 A0A009MYL5 Precorrin methyltransferase family; Precorrin ... \n",
344
+ "4 A0A009PHM9 Precorrin methyltransferase family; Precorrin ... \n",
345
+ "\n",
346
+ " Modified residue \\\n",
347
+ "0 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
348
+ "1 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
349
+ "2 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
350
+ "3 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
351
+ "4 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
352
+ "\n",
353
+ " Sequence \\\n",
354
+ "0 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n",
355
+ "1 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n",
356
+ "2 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n",
357
+ "3 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n",
358
+ "4 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n",
359
+ "\n",
360
+ " PTM sites \n",
361
+ "0 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n",
362
+ "1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n",
363
+ "2 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n",
364
+ "3 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n",
365
+ "4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... "
366
+ ]
367
+ },
368
+ "execution_count": 50,
369
+ "metadata": {},
370
+ "output_type": "execute_result"
371
+ }
372
+ ],
373
+ "source": [
374
+ "# Function to split sequences and PTM sites into chunks\n",
375
+ "def split_into_chunks(row):\n",
376
+ " sequence = row['Sequence']\n",
377
+ " ptm_sites = row['PTM sites']\n",
378
+ " chunk_size = 1000\n",
379
+ " \n",
380
+ " # Calculate the number of chunks\n",
381
+ " num_chunks = (len(sequence) + chunk_size - 1) // chunk_size\n",
382
+ " \n",
383
+ " # Split sequences and PTM sites into chunks\n",
384
+ " sequence_chunks = [sequence[i * chunk_size: (i + 1) * chunk_size] for i in range(num_chunks)]\n",
385
+ " ptm_sites_chunks = [ptm_sites[i * chunk_size: (i + 1) * chunk_size] for i in range(num_chunks)]\n",
386
+ " \n",
387
+ " # Create new rows for each chunk\n",
388
+ " rows = []\n",
389
+ " for i in range(num_chunks):\n",
390
+ " new_row = row.copy()\n",
391
+ " new_row['Sequence'] = sequence_chunks[i]\n",
392
+ " new_row['PTM sites'] = ptm_sites_chunks[i]\n",
393
+ " rows.append(new_row)\n",
394
+ " \n",
395
+ " return rows\n",
396
+ "\n",
397
+ "# Create a new DataFrame to store the chunks\n",
398
+ "chunks_data = []\n",
399
+ "\n",
400
+ "# Iterate through each row of the original DataFrame and split into chunks\n",
401
+ "for _, row in data.iterrows():\n",
402
+ " chunks_data.extend(split_into_chunks(row))\n",
403
+ "\n",
404
+ "# Convert the list of chunks into a DataFrame\n",
405
+ "chunks_df = pd.DataFrame(chunks_data)\n",
406
+ "\n",
407
+ "# Reset the index of the DataFrame\n",
408
+ "chunks_df.reset_index(drop=True, inplace=True)\n",
409
+ "\n",
410
+ "# Display the first few rows of the new DataFrame\n",
411
+ "chunks_df.head()\n"
412
+ ]
413
+ },
414
+ {
415
+ "cell_type": "code",
416
+ "execution_count": 52,
417
+ "id": "0e36e5bb-8e57-45af-a9da-9171875a0b88",
418
+ "metadata": {
419
+ "tags": []
420
+ },
421
+ "outputs": [
422
+ {
423
+ "name": "stderr",
424
+ "output_type": "stream",
425
+ "text": [
426
+ "% Test Data: 21.17% | % Test Families: 15.15%: 15%|█▌ | 661/4364 [00:05<00:30, 120.20it/s]\n"
427
+ ]
428
+ }
429
+ ],
430
+ "source": [
431
+ "from tqdm import tqdm\n",
432
+ "import numpy as np\n",
433
+ "\n",
434
+ "# Function to split data into train and test based on families\n",
435
+ "def split_data(df):\n",
436
+ " # Get a unique list of protein families\n",
437
+ " unique_families = df['Protein families'].unique().tolist()\n",
438
+ " np.random.shuffle(unique_families) # Shuffle the list to randomize the order of families\n",
439
+ " \n",
440
+ " test_data = []\n",
441
+ " test_families = []\n",
442
+ " total_entries = len(df)\n",
443
+ " total_families = len(unique_families)\n",
444
+ " \n",
445
+ " # Set up tqdm progress bar\n",
446
+ " with tqdm(total=total_families) as pbar:\n",
447
+ " for family in unique_families:\n",
448
+ " # Separate out all proteins in the current family into the test data\n",
449
+ " family_data = df[df['Protein families'] == family]\n",
450
+ " test_data.append(family_data)\n",
451
+ " \n",
452
+ " # Update the list of test families\n",
453
+ " test_families.append(family)\n",
454
+ " \n",
455
+ " # Remove the current family data from the original DataFrame\n",
456
+ " df = df[df['Protein families'] != family]\n",
457
+ " \n",
458
+ " # Calculate the percentage of test data and the percentage of families in the test data\n",
459
+ " percent_test_data = sum(len(data) for data in test_data) / total_entries * 100\n",
460
+ " percent_test_families = len(test_families) / total_families * 100\n",
461
+ " \n",
462
+ " # Update tqdm progress bar with readout of percentages\n",
463
+ " pbar.set_description(f'% Test Data: {percent_test_data:.2f}% | % Test Families: {percent_test_families:.2f}%')\n",
464
+ " pbar.update(1)\n",
465
+ " \n",
466
+ " # Check if the 20% threshold for test data is crossed\n",
467
+ " if percent_test_data >= 20:\n",
468
+ " break\n",
469
+ " \n",
470
+ " # Concatenate the list of test data DataFrames into a single DataFrame\n",
471
+ " test_df = pd.concat(test_data, ignore_index=True)\n",
472
+ " \n",
473
+ " return df, test_df # Return the remaining data and the test data\n",
474
+ "\n",
475
+ "# Split the data into train and test based on families\n",
476
+ "train_df, test_df = split_data(chunks_df)\n"
477
+ ]
478
+ },
479
+ {
480
+ "cell_type": "code",
481
+ "execution_count": 53,
482
+ "id": "0d5e7371-a6d0-4c5c-8587-dd0037f052f8",
483
+ "metadata": {
484
+ "tags": []
485
+ },
486
+ "outputs": [],
487
+ "source": [
488
+ "import pandas as pd\n",
489
+ "\n",
490
+ "# Assuming train_df and test_df are your dataframes\n",
491
+ "fraction = 0.105 # 10.5%\n",
492
+ "\n",
493
+ "# Randomly select 10.5% of the data\n",
494
+ "reduced_train_df = train_df.sample(frac=fraction, random_state=42)\n",
495
+ "reduced_test_df = test_df.sample(frac=fraction, random_state=42)\n",
496
+ "\n",
497
+ "# Split the reduced dataframes into sequences and PTM sites\n",
498
+ "#train_sequences = reduced_train_df['Sequence']\n",
499
+ "#train_ptm_sites = reduced_train_df['PTM sites']\n",
500
+ "#test_sequences = reduced_test_df['Sequence']\n",
501
+ "#test_ptm_sites = reduced_test_df['PTM sites']\n",
502
+ "\n",
503
+ "# Save the reduced data as pickle files\n",
504
+ "#train_sequences.to_pickle('train_sequences.pkl')\n",
505
+ "#train_ptm_sites.to_pickle('train_ptm_sites.pkl')\n",
506
+ "#test_sequences.to_pickle('test_sequences.pkl')\n",
507
+ "#test_ptm_sites.to_pickle('test_ptm_sites.pkl')\n"
508
+ ]
509
+ },
510
+ {
511
+ "cell_type": "code",
512
+ "execution_count": 55,
513
+ "id": "a5ac2515-2aaa-4417-b5bb-09b25ce31d44",
514
+ "metadata": {
515
+ "tags": []
516
+ },
517
+ "outputs": [
518
+ {
519
+ "data": {
520
+ "text/plain": [
521
+ "['50K_ptm_data/train_sequences_chunked_by_family.pkl',\n",
522
+ " '50K_ptm_data/test_sequences_chunked_by_family.pkl',\n",
523
+ " '50K_ptm_data/train_labels_chunked_by_family.pkl',\n",
524
+ " '50K_ptm_data/test_labels_chunked_by_family.pkl']"
525
+ ]
526
+ },
527
+ "execution_count": 55,
528
+ "metadata": {},
529
+ "output_type": "execute_result"
530
+ }
531
+ ],
532
+ "source": [
533
+ "import pickle \n",
534
+ "\n",
535
+ "# Extract sequences and PTM site labels from the reduced train and test DataFrames\n",
536
+ "train_sequences_reduced = reduced_train_df['Sequence'].tolist()\n",
537
+ "train_labels_reduced = reduced_train_df['PTM sites'].tolist()\n",
538
+ "test_sequences_reduced = reduced_test_df['Sequence'].tolist()\n",
539
+ "test_labels_reduced = reduced_test_df['PTM sites'].tolist()\n",
540
+ "\n",
541
+ "# Save the lists to the specified pickle files\n",
542
+ "pickle_file_path = \"50K_ptm_data/\"\n",
543
+ "\n",
544
+ "with open(pickle_file_path + \"train_sequences_chunked_by_family.pkl\", \"wb\") as f:\n",
545
+ " pickle.dump(train_sequences_reduced, f)\n",
546
+ "\n",
547
+ "with open(pickle_file_path + \"test_sequences_chunked_by_family.pkl\", \"wb\") as f:\n",
548
+ " pickle.dump(test_sequences_reduced, f)\n",
549
+ "\n",
550
+ "with open(pickle_file_path + \"train_labels_chunked_by_family.pkl\", \"wb\") as f:\n",
551
+ " pickle.dump(train_labels_reduced, f)\n",
552
+ "\n",
553
+ "with open(pickle_file_path + \"test_labels_chunked_by_family.pkl\", \"wb\") as f:\n",
554
+ " pickle.dump(test_labels_reduced, f)\n",
555
+ "\n",
556
+ "# Return the paths to the saved pickle files\n",
557
+ "saved_files = [\n",
558
+ " pickle_file_path + \"train_sequences_chunked_by_family.pkl\",\n",
559
+ " pickle_file_path + \"test_sequences_chunked_by_family.pkl\",\n",
560
+ " pickle_file_path + \"train_labels_chunked_by_family.pkl\",\n",
561
+ " pickle_file_path + \"test_labels_chunked_by_family.pkl\"\n",
562
+ "]\n",
563
+ "saved_files\n"
564
+ ]
565
+ },
566
+ {
567
+ "cell_type": "code",
568
+ "execution_count": 57,
569
+ "id": "5ec5c5fc-7e9a-4c2c-a954-b2d2ad168b11",
570
+ "metadata": {
571
+ "tags": []
572
+ },
573
+ "outputs": [
574
+ {
575
+ "name": "stdout",
576
+ "output_type": "stream",
577
+ "text": [
578
+ "{'50K_ptm_data/train_sequences_chunked_by_family.pkl': 5132, '50K_ptm_data/test_sequences_chunked_by_family.pkl': 1378, '50K_ptm_data/train_labels_chunked_by_family.pkl': 5132, '50K_ptm_data/test_labels_chunked_by_family.pkl': 1378}\n"
579
+ ]
580
+ }
581
+ ],
582
+ "source": [
583
+ "import pickle\n",
584
+ "\n",
585
+ "def get_number_of_rows(pickle_file):\n",
586
+ " with open(pickle_file, \"rb\") as f:\n",
587
+ " data = pickle.load(f)\n",
588
+ " return len(data)\n",
589
+ "\n",
590
+ "# Paths to the pickle files\n",
591
+ "files = [\n",
592
+ " \"50K_ptm_data/train_sequences_chunked_by_family.pkl\",\n",
593
+ " \"50K_ptm_data/test_sequences_chunked_by_family.pkl\",\n",
594
+ " \"50K_ptm_data/train_labels_chunked_by_family.pkl\",\n",
595
+ " \"50K_ptm_data/test_labels_chunked_by_family.pkl\"\n",
596
+ "]\n",
597
+ "\n",
598
+ "# Get the number of rows for each file\n",
599
+ "number_of_rows = {file: get_number_of_rows(file) for file in files}\n",
600
+ "print(number_of_rows)\n"
601
+ ]
602
+ },
603
+ {
604
+ "cell_type": "code",
605
+ "execution_count": null,
606
+ "id": "71cc9d3d-bb35-4e2a-a382-7218bff5cb53",
607
+ "metadata": {},
608
+ "outputs": [],
609
+ "source": []
610
+ }
611
+ ],
612
+ "metadata": {
613
+ "kernelspec": {
614
+ "display_name": "esm2_binding_py38b",
615
+ "language": "python",
616
+ "name": "esm2_binding_py38b"
617
+ },
618
+ "language_info": {
619
+ "codemirror_mode": {
620
+ "name": "ipython",
621
+ "version": 3
622
+ },
623
+ "file_extension": ".py",
624
+ "mimetype": "text/x-python",
625
+ "name": "python",
626
+ "nbconvert_exporter": "python",
627
+ "pygments_lexer": "ipython3",
628
+ "version": "3.8.17"
629
+ }
630
+ },
631
+ "nbformat": 4,
632
+ "nbformat_minor": 5
633
+ }