File size: 25,123 Bytes
4c40650 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 47,
"id": "945a82aa-1398-422a-98df-b3db93973271",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Entry</th>\n",
" <th>Protein families</th>\n",
" <th>Modified residue</th>\n",
" <th>Sequence</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>A0A009GHC8</td>\n",
" <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
" <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
" <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>A0A009HTZ2</td>\n",
" <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
" <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
" <td>MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>A0A009IVE2</td>\n",
" <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
" <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
" <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>A0A009MYL5</td>\n",
" <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
" <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
" <td>MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>A0A009PHM9</td>\n",
" <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
" <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
" <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Entry Protein families \\\n",
"0 A0A009GHC8 Precorrin methyltransferase family; Precorrin ... \n",
"1 A0A009HTZ2 Precorrin methyltransferase family; Precorrin ... \n",
"2 A0A009IVE2 Precorrin methyltransferase family; Precorrin ... \n",
"3 A0A009MYL5 Precorrin methyltransferase family; Precorrin ... \n",
"4 A0A009PHM9 Precorrin methyltransferase family; Precorrin ... \n",
"\n",
" Modified residue \\\n",
"0 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
"1 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
"2 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
"3 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
"4 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
"\n",
" Sequence \n",
"0 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n",
"1 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n",
"2 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n",
"3 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n",
"4 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... "
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"# Load the TSV file\n",
"file_path = 'PTM/uniprotkb_family_AND_ft_mod_res_AND_pro_2023_10_02.tsv'\n",
"data = pd.read_csv(file_path, sep='\\t')\n",
"\n",
"# Display the first few rows of the data\n",
"data.head()\n"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "a03f8ff8-0612-4f8c-bccd-49fde3dce0f5",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Entry</th>\n",
" <th>Protein families</th>\n",
" <th>Modified residue</th>\n",
" <th>Sequence</th>\n",
" <th>PTM sites</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>A0A009GHC8</td>\n",
" <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
" <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
" <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
" <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>A0A009HTZ2</td>\n",
" <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
" <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
" <td>MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...</td>\n",
" <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>A0A009IVE2</td>\n",
" <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
" <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
" <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
" <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>A0A009MYL5</td>\n",
" <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
" <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
" <td>MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...</td>\n",
" <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>A0A009PHM9</td>\n",
" <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
" <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
" <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
" <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Entry Protein families \\\n",
"0 A0A009GHC8 Precorrin methyltransferase family; Precorrin ... \n",
"1 A0A009HTZ2 Precorrin methyltransferase family; Precorrin ... \n",
"2 A0A009IVE2 Precorrin methyltransferase family; Precorrin ... \n",
"3 A0A009MYL5 Precorrin methyltransferase family; Precorrin ... \n",
"4 A0A009PHM9 Precorrin methyltransferase family; Precorrin ... \n",
"\n",
" Modified residue \\\n",
"0 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
"1 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
"2 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
"3 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
"4 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
"\n",
" Sequence \\\n",
"0 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n",
"1 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n",
"2 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n",
"3 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n",
"4 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n",
"\n",
" PTM sites \n",
"0 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n",
"1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n",
"2 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n",
"3 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n",
"4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... "
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import re\n",
"\n",
"def get_ptm_sites(row):\n",
" # Extract the positions of modified residues from the 'Modified residue' column\n",
" modified_positions = [int(i) for i in re.findall(r'MOD_RES (\\d+)', row['Modified residue'])]\n",
" \n",
" # Create a list of zeros of length equal to the protein sequence\n",
" ptm_sites = [0] * len(row['Sequence'])\n",
" \n",
" # Replace the zeros with ones at the positions of modified residues\n",
" for position in modified_positions:\n",
" # Subtracting 1 because positions are 1-indexed, but lists are 0-indexed\n",
" ptm_sites[position - 1] = 1\n",
" \n",
" return ptm_sites\n",
"\n",
"# Apply the function to each row in the DataFrame\n",
"data['PTM sites'] = data.apply(get_ptm_sites, axis=1)\n",
"\n",
"# Display the first few rows of the updated DataFrame\n",
"data.head()\n"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "5d2e5043-e2f9-44ec-899b-7dad4f83f823",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Entry</th>\n",
" <th>Protein families</th>\n",
" <th>Modified residue</th>\n",
" <th>Sequence</th>\n",
" <th>PTM sites</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>A0A009GHC8</td>\n",
" <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
" <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
" <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
" <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>A0A009HTZ2</td>\n",
" <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
" <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
" <td>MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...</td>\n",
" <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>A0A009IVE2</td>\n",
" <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
" <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
" <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
" <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>A0A009MYL5</td>\n",
" <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
" <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
" <td>MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...</td>\n",
" <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>A0A009PHM9</td>\n",
" <td>Precorrin methyltransferase family; Precorrin ...</td>\n",
" <td>MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...</td>\n",
" <td>MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...</td>\n",
" <td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Entry Protein families \\\n",
"0 A0A009GHC8 Precorrin methyltransferase family; Precorrin ... \n",
"1 A0A009HTZ2 Precorrin methyltransferase family; Precorrin ... \n",
"2 A0A009IVE2 Precorrin methyltransferase family; Precorrin ... \n",
"3 A0A009MYL5 Precorrin methyltransferase family; Precorrin ... \n",
"4 A0A009PHM9 Precorrin methyltransferase family; Precorrin ... \n",
"\n",
" Modified residue \\\n",
"0 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
"1 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
"2 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
"3 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
"4 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n",
"\n",
" Sequence \\\n",
"0 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n",
"1 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n",
"2 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n",
"3 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n",
"4 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n",
"\n",
" PTM sites \n",
"0 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n",
"1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n",
"2 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n",
"3 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n",
"4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... "
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Function to split sequences and PTM sites into chunks\n",
"def split_into_chunks(row):\n",
" sequence = row['Sequence']\n",
" ptm_sites = row['PTM sites']\n",
" chunk_size = 1000\n",
" \n",
" # Calculate the number of chunks\n",
" num_chunks = (len(sequence) + chunk_size - 1) // chunk_size\n",
" \n",
" # Split sequences and PTM sites into chunks\n",
" sequence_chunks = [sequence[i * chunk_size: (i + 1) * chunk_size] for i in range(num_chunks)]\n",
" ptm_sites_chunks = [ptm_sites[i * chunk_size: (i + 1) * chunk_size] for i in range(num_chunks)]\n",
" \n",
" # Create new rows for each chunk\n",
" rows = []\n",
" for i in range(num_chunks):\n",
" new_row = row.copy()\n",
" new_row['Sequence'] = sequence_chunks[i]\n",
" new_row['PTM sites'] = ptm_sites_chunks[i]\n",
" rows.append(new_row)\n",
" \n",
" return rows\n",
"\n",
"# Create a new DataFrame to store the chunks\n",
"chunks_data = []\n",
"\n",
"# Iterate through each row of the original DataFrame and split into chunks\n",
"for _, row in data.iterrows():\n",
" chunks_data.extend(split_into_chunks(row))\n",
"\n",
"# Convert the list of chunks into a DataFrame\n",
"chunks_df = pd.DataFrame(chunks_data)\n",
"\n",
"# Reset the index of the DataFrame\n",
"chunks_df.reset_index(drop=True, inplace=True)\n",
"\n",
"# Display the first few rows of the new DataFrame\n",
"chunks_df.head()\n"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "0e36e5bb-8e57-45af-a9da-9171875a0b88",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"% Test Data: 21.17% | % Test Families: 15.15%: 15%|█▌ | 661/4364 [00:05<00:30, 120.20it/s]\n"
]
}
],
"source": [
"from tqdm import tqdm\n",
"import numpy as np\n",
"\n",
"# Function to split data into train and test based on families\n",
"def split_data(df):\n",
" # Get a unique list of protein families\n",
" unique_families = df['Protein families'].unique().tolist()\n",
" np.random.shuffle(unique_families) # Shuffle the list to randomize the order of families\n",
" \n",
" test_data = []\n",
" test_families = []\n",
" total_entries = len(df)\n",
" total_families = len(unique_families)\n",
" \n",
" # Set up tqdm progress bar\n",
" with tqdm(total=total_families) as pbar:\n",
" for family in unique_families:\n",
" # Separate out all proteins in the current family into the test data\n",
" family_data = df[df['Protein families'] == family]\n",
" test_data.append(family_data)\n",
" \n",
" # Update the list of test families\n",
" test_families.append(family)\n",
" \n",
" # Remove the current family data from the original DataFrame\n",
" df = df[df['Protein families'] != family]\n",
" \n",
" # Calculate the percentage of test data and the percentage of families in the test data\n",
" percent_test_data = sum(len(data) for data in test_data) / total_entries * 100\n",
" percent_test_families = len(test_families) / total_families * 100\n",
" \n",
" # Update tqdm progress bar with readout of percentages\n",
" pbar.set_description(f'% Test Data: {percent_test_data:.2f}% | % Test Families: {percent_test_families:.2f}%')\n",
" pbar.update(1)\n",
" \n",
" # Check if the 20% threshold for test data is crossed\n",
" if percent_test_data >= 20:\n",
" break\n",
" \n",
" # Concatenate the list of test data DataFrames into a single DataFrame\n",
" test_df = pd.concat(test_data, ignore_index=True)\n",
" \n",
" return df, test_df # Return the remaining data and the test data\n",
"\n",
"# Split the data into train and test based on families\n",
"train_df, test_df = split_data(chunks_df)\n"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "0d5e7371-a6d0-4c5c-8587-dd0037f052f8",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"# Assuming train_df and test_df are your dataframes\n",
"fraction = 0.105 # 10.5%\n",
"\n",
"# Randomly select 10.5% of the data\n",
"reduced_train_df = train_df.sample(frac=fraction, random_state=42)\n",
"reduced_test_df = test_df.sample(frac=fraction, random_state=42)\n",
"\n",
"# Split the reduced dataframes into sequences and PTM sites\n",
"#train_sequences = reduced_train_df['Sequence']\n",
"#train_ptm_sites = reduced_train_df['PTM sites']\n",
"#test_sequences = reduced_test_df['Sequence']\n",
"#test_ptm_sites = reduced_test_df['PTM sites']\n",
"\n",
"# Save the reduced data as pickle files\n",
"#train_sequences.to_pickle('train_sequences.pkl')\n",
"#train_ptm_sites.to_pickle('train_ptm_sites.pkl')\n",
"#test_sequences.to_pickle('test_sequences.pkl')\n",
"#test_ptm_sites.to_pickle('test_ptm_sites.pkl')\n"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "a5ac2515-2aaa-4417-b5bb-09b25ce31d44",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"['50K_ptm_data/train_sequences_chunked_by_family.pkl',\n",
" '50K_ptm_data/test_sequences_chunked_by_family.pkl',\n",
" '50K_ptm_data/train_labels_chunked_by_family.pkl',\n",
" '50K_ptm_data/test_labels_chunked_by_family.pkl']"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pickle \n",
"\n",
"# Extract sequences and PTM site labels from the reduced train and test DataFrames\n",
"train_sequences_reduced = reduced_train_df['Sequence'].tolist()\n",
"train_labels_reduced = reduced_train_df['PTM sites'].tolist()\n",
"test_sequences_reduced = reduced_test_df['Sequence'].tolist()\n",
"test_labels_reduced = reduced_test_df['PTM sites'].tolist()\n",
"\n",
"# Save the lists to the specified pickle files\n",
"pickle_file_path = \"50K_ptm_data/\"\n",
"\n",
"with open(pickle_file_path + \"train_sequences_chunked_by_family.pkl\", \"wb\") as f:\n",
" pickle.dump(train_sequences_reduced, f)\n",
"\n",
"with open(pickle_file_path + \"test_sequences_chunked_by_family.pkl\", \"wb\") as f:\n",
" pickle.dump(test_sequences_reduced, f)\n",
"\n",
"with open(pickle_file_path + \"train_labels_chunked_by_family.pkl\", \"wb\") as f:\n",
" pickle.dump(train_labels_reduced, f)\n",
"\n",
"with open(pickle_file_path + \"test_labels_chunked_by_family.pkl\", \"wb\") as f:\n",
" pickle.dump(test_labels_reduced, f)\n",
"\n",
"# Return the paths to the saved pickle files\n",
"saved_files = [\n",
" pickle_file_path + \"train_sequences_chunked_by_family.pkl\",\n",
" pickle_file_path + \"test_sequences_chunked_by_family.pkl\",\n",
" pickle_file_path + \"train_labels_chunked_by_family.pkl\",\n",
" pickle_file_path + \"test_labels_chunked_by_family.pkl\"\n",
"]\n",
"saved_files\n"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "5ec5c5fc-7e9a-4c2c-a954-b2d2ad168b11",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'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"
]
}
],
"source": [
"import pickle\n",
"\n",
"def get_number_of_rows(pickle_file):\n",
" with open(pickle_file, \"rb\") as f:\n",
" data = pickle.load(f)\n",
" return len(data)\n",
"\n",
"# Paths to the pickle files\n",
"files = [\n",
" \"50K_ptm_data/train_sequences_chunked_by_family.pkl\",\n",
" \"50K_ptm_data/test_sequences_chunked_by_family.pkl\",\n",
" \"50K_ptm_data/train_labels_chunked_by_family.pkl\",\n",
" \"50K_ptm_data/test_labels_chunked_by_family.pkl\"\n",
"]\n",
"\n",
"# Get the number of rows for each file\n",
"number_of_rows = {file: get_number_of_rows(file) for file in files}\n",
"print(number_of_rows)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "71cc9d3d-bb35-4e2a-a382-7218bff5cb53",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "esm2_binding_py38b",
"language": "python",
"name": "esm2_binding_py38b"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.17"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|