File size: 38,002 Bytes
f88156f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "49167018",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T07:04:52.265220Z",
"iopub.status.busy": "2025-03-25T07:04:52.265123Z",
"iopub.status.idle": "2025-03-25T07:04:52.426435Z",
"shell.execute_reply": "2025-03-25T07:04:52.426101Z"
}
},
"outputs": [],
"source": [
"import sys\n",
"import os\n",
"sys.path.append(os.path.abspath(os.path.join(os.getcwd(), '../..')))\n",
"\n",
"# Path Configuration\n",
"from tools.preprocess import *\n",
"\n",
"# Processing context\n",
"trait = \"Brugada_Syndrome\"\n",
"cohort = \"GSE136992\"\n",
"\n",
"# Input paths\n",
"in_trait_dir = \"../../input/GEO/Brugada_Syndrome\"\n",
"in_cohort_dir = \"../../input/GEO/Brugada_Syndrome/GSE136992\"\n",
"\n",
"# Output paths\n",
"out_data_file = \"../../output/preprocess/Brugada_Syndrome/GSE136992.csv\"\n",
"out_gene_data_file = \"../../output/preprocess/Brugada_Syndrome/gene_data/GSE136992.csv\"\n",
"out_clinical_data_file = \"../../output/preprocess/Brugada_Syndrome/clinical_data/GSE136992.csv\"\n",
"json_path = \"../../output/preprocess/Brugada_Syndrome/cohort_info.json\"\n"
]
},
{
"cell_type": "markdown",
"id": "4b0d0767",
"metadata": {},
"source": [
"### Step 1: Initial Data Loading"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8950fb41",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T07:04:52.427798Z",
"iopub.status.busy": "2025-03-25T07:04:52.427667Z",
"iopub.status.idle": "2025-03-25T07:04:52.560504Z",
"shell.execute_reply": "2025-03-25T07:04:52.560161Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Background Information:\n",
"!Series_title\t\"mRNA expression in SIDS\"\n",
"!Series_summary\t\"Genetic predispositions in cases suffering sudden unexpected infant death have been a research focus worldwide the last decade. Despite large efforts there is still uncertainty concerning the molecular pathogenesis of these deaths. With genetic technology in constant development the possibility of an alternative approach into this research field have become available, like mRNA expression studies. Methods: In this study we investigated mRNA gene expression in 14 cases that died suddenly and unexpectedly from infection without a history of severe illness prior to death. The control group included eight accidents, two cases of natural death, one undetermined, one case of medical malpractice and two homicides. The study included tissue from liver, heart and brain. The mRNA expression was determined using Illumina whole genome gene expression DASL HT assay. Results: From the array, 19 genes showed altered expression in the infectious deaths compared to controls. The heart was the organ were most genes showed altered expression: 15 genes showed different mRNA expression compared to the control group. Conclusion: Down-regulation of KCNE5 in heart tissue from cases of infectious death was of particular interest. Variants of KCNE5 are associated with Brugada syndrome KCNE5 gene is known to give increased risk of cardiac arrhythmia and sudden death, and could be responsible for the fatal outcome in the group of infectious death.\"\n",
"!Series_overall_design\t\"The purpose of this study was to investigate gene expression in infection cases and controls, in order to uncover genes that are differentially expressed in the two groups. Tissue from brain, heart and liver from 10 infection cases and 10 controls were included in this study, and mRNA expression was determined using the Illumina whole genome gene expression DASL HT assay. The cases diagnosed as infectious death died suddenly and unexpectedly, without a history of severe illness prior to death.\"\n",
"Sample Characteristics Dictionary:\n",
"{0: ['condition: Infection', 'condition: Control'], 1: ['tissue: Heart', 'tissue: Liver', 'tissue: Brain'], 2: ['age: 24 weeks', 'age: 112 weeks', 'age: 8 weeks', 'age: 0.6 weeks', 'age: 72 weeks', 'age: 36 weeks', 'age: 52 weeks', 'age: 20 weeks', 'age: 0 weeks', 'age: 80 weeks', 'age: 0.5 weeks', 'age: 144 weeks', 'age: 12 weeks', 'age: 2 weeks', 'age: 60 weeks'], 3: ['gender: male', 'gender: female']}\n"
]
}
],
"source": [
"from tools.preprocess import *\n",
"# 1. Identify the paths to the SOFT file and the matrix file\n",
"soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
"\n",
"# 2. Read the matrix file to obtain background information and sample characteristics data\n",
"background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']\n",
"clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']\n",
"background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)\n",
"\n",
"# 3. Obtain the sample characteristics dictionary from the clinical dataframe\n",
"sample_characteristics_dict = get_unique_values_by_row(clinical_data)\n",
"\n",
"# 4. Explicitly print out all the background information and the sample characteristics dictionary\n",
"print(\"Background Information:\")\n",
"print(background_info)\n",
"print(\"Sample Characteristics Dictionary:\")\n",
"print(sample_characteristics_dict)\n"
]
},
{
"cell_type": "markdown",
"id": "c78241be",
"metadata": {},
"source": [
"### Step 2: Dataset Analysis and Clinical Feature Extraction"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e360af68",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T07:04:52.561687Z",
"iopub.status.busy": "2025-03-25T07:04:52.561582Z",
"iopub.status.idle": "2025-03-25T07:04:52.573163Z",
"shell.execute_reply": "2025-03-25T07:04:52.572880Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Clinical Features Preview:\n",
"{'GSM4064970': [1.0, 24.0, 1.0], 'GSM4064971': [1.0, 112.0, 1.0], 'GSM4064972': [1.0, 8.0, 0.0], 'GSM4064973': [1.0, 24.0, 1.0], 'GSM4064974': [1.0, 0.6, 0.0], 'GSM4064975': [1.0, 72.0, 1.0], 'GSM4064976': [1.0, 24.0, 0.0], 'GSM4064977': [1.0, 36.0, 1.0], 'GSM4064978': [1.0, 52.0, 1.0], 'GSM4064979': [1.0, 20.0, 1.0], 'GSM4064980': [0.0, 24.0, 0.0], 'GSM4064981': [0.0, 0.0, 0.0], 'GSM4064982': [0.0, 0.0, 0.0], 'GSM4064983': [0.0, 80.0, 0.0], 'GSM4064984': [0.0, 52.0, 0.0], 'GSM4064985': [0.0, 0.5, 0.0], 'GSM4064986': [0.0, 144.0, 1.0], 'GSM4064987': [0.0, 0.0, 1.0], 'GSM4064988': [0.0, 24.0, 1.0], 'GSM4064989': [0.0, 0.0, 1.0], 'GSM4064990': [1.0, 112.0, 1.0], 'GSM4064991': [1.0, 24.0, 1.0], 'GSM4064992': [1.0, 8.0, 0.0], 'GSM4064993': [1.0, 0.6, 0.0], 'GSM4064994': [1.0, 20.0, 1.0], 'GSM4064995': [1.0, 36.0, 1.0], 'GSM4064996': [1.0, 12.0, 0.0], 'GSM4064997': [1.0, 72.0, 1.0], 'GSM4064998': [1.0, 0.0, 0.0], 'GSM4064999': [1.0, 52.0, 1.0], 'GSM4065000': [1.0, 24.0, 1.0], 'GSM4065001': [0.0, 0.0, 0.0], 'GSM4065002': [0.0, 0.0, 0.0], 'GSM4065003': [0.0, 0.0, 0.0], 'GSM4065004': [0.0, 144.0, 1.0], 'GSM4065005': [0.0, 52.0, 0.0], 'GSM4065006': [0.0, 0.0, 0.0], 'GSM4065007': [0.0, 24.0, 1.0], 'GSM4065008': [0.0, 2.0, 1.0], 'GSM4065009': [0.0, 80.0, 0.0], 'GSM4065010': [0.0, 24.0, 0.0], 'GSM4065011': [1.0, 8.0, 0.0], 'GSM4065012': [1.0, 20.0, 1.0], 'GSM4065013': [1.0, 24.0, 1.0], 'GSM4065014': [1.0, 0.6, 0.0], 'GSM4065015': [1.0, 72.0, 1.0], 'GSM4065016': [1.0, 0.0, 0.0], 'GSM4065017': [1.0, 36.0, 1.0], 'GSM4065018': [1.0, 0.0, 1.0], 'GSM4065019': [1.0, 24.0, 1.0], 'GSM4065020': [1.0, 60.0, 0.0], 'GSM4065021': [0.0, 52.0, 0.0], 'GSM4065022': [0.0, 0.0, 0.0], 'GSM4065023': [0.0, 0.0, 0.0], 'GSM4065024': [0.0, 52.0, 0.0], 'GSM4065025': [0.0, 0.0, 1.0], 'GSM4065026': [0.0, 0.0, 0.0], 'GSM4065027': [0.0, 2.0, 1.0], 'GSM4065028': [0.0, 2.0, 0.0], 'GSM4065029': [0.0, 144.0, 1.0]}\n",
"Clinical features saved to ../../output/preprocess/Brugada_Syndrome/clinical_data/GSE136992.csv\n"
]
}
],
"source": [
"# 1. Determine gene expression data availability\n",
"is_gene_available = True # Based on the background information which mentions \"mRNA expression studies\" and \"Illumina whole genome gene expression DASL HT assay\"\n",
"\n",
"# 2.1 and 2.2 Data Availability and Type Conversion\n",
"\n",
"# For trait - Brugada Syndrome\n",
"# The condition field (row 0) has 'Infection' and 'Control' values\n",
"# Since KCNE5 downregulation in infection cases is associated with Brugada syndrome, \n",
"# we can use the 'condition' field to determine Brugada syndrome risk\n",
"trait_row = 0\n",
"\n",
"def convert_trait(value):\n",
" # Extract the value after the colon and strip whitespace\n",
" if ':' in value:\n",
" value = value.split(':', 1)[1].strip()\n",
" \n",
" # Map 'Infection' to 1 (higher risk of Brugada syndrome due to KCNE5 downregulation)\n",
" # and 'Control' to 0\n",
" if value.lower() == 'infection':\n",
" return 1\n",
" elif value.lower() == 'control':\n",
" return 0\n",
" else:\n",
" return None\n",
"\n",
"# For age\n",
"age_row = 2\n",
"\n",
"def convert_age(value):\n",
" if ':' in value:\n",
" value = value.split(':', 1)[1].strip()\n",
" \n",
" try:\n",
" # Extract the numeric part (age in weeks)\n",
" age_value = float(value.split()[0])\n",
" return age_value # Return age as a continuous variable\n",
" except (ValueError, IndexError):\n",
" return None\n",
"\n",
"# For gender\n",
"gender_row = 3\n",
"\n",
"def convert_gender(value):\n",
" if ':' in value:\n",
" value = value.split(':', 1)[1].strip()\n",
" \n",
" if value.lower() == 'male':\n",
" return 1\n",
" elif value.lower() == 'female':\n",
" return 0\n",
" else:\n",
" return None\n",
"\n",
"# 3. Save metadata\n",
"# Determine if trait data is available\n",
"is_trait_available = trait_row is not None\n",
"\n",
"# Initial filtering of dataset usability\n",
"validate_and_save_cohort_info(\n",
" is_final=False,\n",
" cohort=cohort,\n",
" info_path=json_path,\n",
" is_gene_available=is_gene_available,\n",
" is_trait_available=is_trait_available\n",
")\n",
"\n",
"# 4. Clinical Feature Extraction (only if trait_row is not None)\n",
"if trait_row is not None:\n",
" try:\n",
" # Process clinical features using the actual clinical_data from previous steps\n",
" selected_clinical_df = geo_select_clinical_features(\n",
" clinical_df=clinical_data, # Use the variable from previous steps\n",
" trait=trait,\n",
" trait_row=trait_row,\n",
" convert_trait=convert_trait,\n",
" age_row=age_row,\n",
" convert_age=convert_age,\n",
" gender_row=gender_row,\n",
" convert_gender=convert_gender\n",
" )\n",
" \n",
" # Preview processed clinical data\n",
" preview = preview_df(selected_clinical_df)\n",
" print(\"Clinical Features Preview:\")\n",
" print(preview)\n",
" \n",
" # Save the processed clinical data\n",
" os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n",
" selected_clinical_df.to_csv(out_clinical_data_file)\n",
" print(f\"Clinical features saved to {out_clinical_data_file}\")\n",
" except NameError:\n",
" print(\"Error: clinical_data variable not found. Make sure clinical data is loaded in a previous step.\")\n",
" except Exception as e:\n",
" print(f\"Error processing clinical features: {str(e)}\")\n"
]
},
{
"cell_type": "markdown",
"id": "09dda259",
"metadata": {},
"source": [
"### Step 3: Gene Data Extraction"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "28a98de5",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T07:04:52.574302Z",
"iopub.status.busy": "2025-03-25T07:04:52.574200Z",
"iopub.status.idle": "2025-03-25T07:04:52.807910Z",
"shell.execute_reply": "2025-03-25T07:04:52.807468Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SOFT file: ../../input/GEO/Brugada_Syndrome/GSE136992/GSE136992_family.soft.gz\n",
"Matrix file: ../../input/GEO/Brugada_Syndrome/GSE136992/GSE136992_series_matrix.txt.gz\n",
"Found the matrix table marker at line 60\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gene data shape: (29377, 60)\n",
"First 20 gene/probe identifiers:\n",
"['ILMN_1343291', 'ILMN_1651209', 'ILMN_1651228', 'ILMN_1651229', 'ILMN_1651235', 'ILMN_1651236', 'ILMN_1651237', 'ILMN_1651238', 'ILMN_1651254', 'ILMN_1651260', 'ILMN_1651262', 'ILMN_1651268', 'ILMN_1651278', 'ILMN_1651282', 'ILMN_1651285', 'ILMN_1651286', 'ILMN_1651292', 'ILMN_1651303', 'ILMN_1651309', 'ILMN_1651315']\n"
]
}
],
"source": [
"# 1. Identify the paths to the SOFT file and the matrix file\n",
"soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
"print(f\"SOFT file: {soft_file}\")\n",
"print(f\"Matrix file: {matrix_file}\")\n",
"\n",
"# Set gene availability flag\n",
"is_gene_available = True # Initially assume gene data is available\n",
"\n",
"# First check if the matrix file contains the expected marker\n",
"found_marker = False\n",
"marker_row = None\n",
"try:\n",
" with gzip.open(matrix_file, 'rt') as file:\n",
" for i, line in enumerate(file):\n",
" if \"!series_matrix_table_begin\" in line:\n",
" found_marker = True\n",
" marker_row = i\n",
" print(f\"Found the matrix table marker at line {i}\")\n",
" break\n",
" \n",
" if not found_marker:\n",
" print(\"Warning: Could not find '!series_matrix_table_begin' marker in the file.\")\n",
" is_gene_available = False\n",
" \n",
" # If marker was found, try to extract gene data\n",
" if is_gene_available:\n",
" try:\n",
" # Try using the library function\n",
" gene_data = get_genetic_data(matrix_file)\n",
" \n",
" if gene_data.shape[0] == 0:\n",
" print(\"Warning: Extracted gene data has 0 rows.\")\n",
" is_gene_available = False\n",
" else:\n",
" print(f\"Gene data shape: {gene_data.shape}\")\n",
" # Print the first 20 gene/probe identifiers\n",
" print(\"First 20 gene/probe identifiers:\")\n",
" print(gene_data.index[:20].tolist())\n",
" except Exception as e:\n",
" print(f\"Error extracting gene data with get_genetic_data(): {e}\")\n",
" is_gene_available = False\n",
" \n",
" # If gene data extraction failed, examine file content to diagnose\n",
" if not is_gene_available:\n",
" print(\"Examining file content to diagnose the issue:\")\n",
" try:\n",
" with gzip.open(matrix_file, 'rt') as file:\n",
" # Print lines around the marker if found\n",
" if marker_row is not None:\n",
" for i, line in enumerate(file):\n",
" if i >= marker_row - 2 and i <= marker_row + 10:\n",
" print(f\"Line {i}: {line.strip()[:100]}...\")\n",
" if i > marker_row + 10:\n",
" break\n",
" else:\n",
" # If marker not found, print first 10 lines\n",
" for i, line in enumerate(file):\n",
" if i < 10:\n",
" print(f\"Line {i}: {line.strip()[:100]}...\")\n",
" else:\n",
" break\n",
" except Exception as e2:\n",
" print(f\"Error examining file: {e2}\")\n",
" \n",
"except Exception as e:\n",
" print(f\"Error processing file: {e}\")\n",
" is_gene_available = False\n",
"\n",
"# Update validation information if gene data extraction failed\n",
"if not is_gene_available:\n",
" print(\"Gene expression data could not be successfully extracted from this dataset.\")\n",
" # Update the validation record since gene data isn't available\n",
" is_trait_available = False # We already determined trait data isn't available in step 2\n",
" validate_and_save_cohort_info(is_final=False, cohort=cohort, info_path=json_path,\n",
" is_gene_available=is_gene_available, is_trait_available=is_trait_available)\n"
]
},
{
"cell_type": "markdown",
"id": "c1ea82ee",
"metadata": {},
"source": [
"### Step 4: Gene Identifier Review"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "64dbcd94",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T07:04:52.809347Z",
"iopub.status.busy": "2025-03-25T07:04:52.809232Z",
"iopub.status.idle": "2025-03-25T07:04:52.811059Z",
"shell.execute_reply": "2025-03-25T07:04:52.810788Z"
}
},
"outputs": [],
"source": [
"# Analyzing gene identifiers\n",
"# The ILMN_ prefix indicates these are Illumina probe IDs, not standard human gene symbols\n",
"# Illumina BeadArray technologies use these proprietary identifiers which need to be mapped to standard gene symbols\n",
"# Therefore, gene mapping is required\n",
"\n",
"requires_gene_mapping = True\n"
]
},
{
"cell_type": "markdown",
"id": "6a8347ab",
"metadata": {},
"source": [
"### Step 5: Gene Annotation"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "56f21746",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T07:04:52.812181Z",
"iopub.status.busy": "2025-03-25T07:04:52.812081Z",
"iopub.status.idle": "2025-03-25T07:04:56.663713Z",
"shell.execute_reply": "2025-03-25T07:04:56.663029Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Gene annotation preview:\n",
"Columns in gene annotation: ['ID', 'Transcript', 'Species', 'Source', 'Search_Key', 'ILMN_Gene', 'Source_Reference_ID', 'RefSeq_ID', 'Entrez_Gene_ID', 'GI', 'Accession', 'Symbol', 'Protein_Product', 'Array_Address_Id', 'Probe_Type', 'Probe_Start', 'SEQUENCE', 'Chromosome', 'Probe_Chr_Orientation', 'Probe_Coordinates', 'Cytoband', 'Definition', 'Ontology_Component', 'Ontology_Process', 'Ontology_Function', 'Synonyms', 'Obsolete_Probe_Id', 'GB_ACC']\n",
"{'ID': ['ILMN_3166687', 'ILMN_3165566', 'ILMN_3164811'], 'Transcript': ['ILMN_333737', 'ILMN_333646', 'ILMN_333584'], 'Species': ['ILMN Controls', 'ILMN Controls', 'ILMN Controls'], 'Source': ['ILMN_Controls', 'ILMN_Controls', 'ILMN_Controls'], 'Search_Key': ['ERCC-00162', 'ERCC-00071', 'ERCC-00009'], 'ILMN_Gene': ['ERCC-00162', 'ERCC-00071', 'ERCC-00009'], 'Source_Reference_ID': ['ERCC-00162', 'ERCC-00071', 'ERCC-00009'], 'RefSeq_ID': [nan, nan, nan], 'Entrez_Gene_ID': [nan, nan, nan], 'GI': [nan, nan, nan], 'Accession': ['DQ516750', 'DQ883654', 'DQ668364'], 'Symbol': ['ERCC-00162', 'ERCC-00071', 'ERCC-00009'], 'Protein_Product': [nan, nan, nan], 'Array_Address_Id': [5270161.0, 4260594.0, 7610424.0], 'Probe_Type': ['S', 'S', 'S'], 'Probe_Start': [12.0, 224.0, 868.0], 'SEQUENCE': ['CCCATGTGTCCAATTCTGAATATCTTTCCAGCTAAGTGCTTCTGCCCACC', 'GGATTAACTGCTGTGGTGTGTCATACTCGGCTACCTCCTGGTTTGGCGTC', 'GACCACGCCTTGTAATCGTATGACACGCGCTTGACACGACTGAATCCAGC'], 'Chromosome': [nan, nan, nan], 'Probe_Chr_Orientation': [nan, nan, nan], 'Probe_Coordinates': [nan, nan, nan], 'Cytoband': [nan, nan, nan], 'Definition': ['Methanocaldococcus jannaschii spike-in control MJ-500-33 genomic sequence', 'Synthetic construct clone NISTag13 external RNA control sequence', 'Synthetic construct clone TagJ microarray control'], 'Ontology_Component': [nan, nan, nan], 'Ontology_Process': [nan, nan, nan], 'Ontology_Function': [nan, nan, nan], 'Synonyms': [nan, nan, nan], 'Obsolete_Probe_Id': [nan, nan, nan], 'GB_ACC': ['DQ516750', 'DQ883654', 'DQ668364']}\n",
"\n",
"Examining gene mapping columns:\n",
"Column 'ID' examples (probe identifiers):\n",
"Example 1: ILMN_3166687\n",
"Example 2: ILMN_3165566\n",
"Example 3: ILMN_3164811\n",
"Example 4: ILMN_3165363\n",
"Example 5: ILMN_3166511\n",
"\n",
"Column 'Symbol' examples (contains gene symbols):\n",
"Example 1: ERCC-00162\n",
"Example 2: ERCC-00071\n",
"Example 3: ERCC-00009\n",
"Example 4: ERCC-00053\n",
"Example 5: ERCC-00144\n",
"Example 6: ERCC-00003\n",
"Example 7: ERCC-00138\n",
"Example 8: ERCC-00084\n",
"Example 9: ERCC-00017\n",
"Example 10: ERCC-00057\n",
"\n",
"Checking if symbols are proper human gene symbols:\n",
"Example 1: 'ERCC-00162' - Likely human gene\n",
"Example 2: 'ERCC-00071' - Likely human gene\n",
"Example 3: 'ERCC-00009' - Likely human gene\n",
"Example 4: 'ERCC-00053' - Likely human gene\n",
"Example 5: 'ERCC-00144' - Likely human gene\n",
"Example 6: 'ERCC-00003' - Likely human gene\n",
"Example 7: 'ERCC-00138' - Likely human gene\n",
"Example 8: 'ERCC-00084' - Likely human gene\n",
"Example 9: 'ERCC-00017' - Likely human gene\n",
"Example 10: 'ERCC-00057' - Likely human gene\n",
"\n",
"Out of 50 examined symbols, 10 appear to be standard human gene symbols.\n",
"\n",
"Columns identified for gene mapping:\n",
"- 'ID': Contains probe IDs (e.g., ILMN_3166687)\n",
"- 'Symbol': Contains gene symbols (e.g., ERCC-00162)\n",
"\n",
"Checking other columns that might contain gene information:\n",
"\n",
"Examples from 'ILMN_Gene' column:\n",
"Example 1: ERCC-00162\n",
"Example 2: ERCC-00071\n",
"Example 3: ERCC-00009\n",
"Example 4: ERCC-00053\n",
"Example 5: ERCC-00144\n",
"\n",
"Examples from 'Entrez_Gene_ID' column:\n",
"Example 1: 54765.0\n",
"Example 2: 158833.0\n",
"Example 3: 56905.0\n",
"Example 4: 56107.0\n",
"Example 5: 56107.0\n",
"\n",
"Examples from 'Synonyms' column:\n",
"Example 1: MGC3490; MC7; HSA249128; DIPB\n",
"Example 2: AWAT1; DGA2\n",
"Example 3: DKFZP434H132; FLJ46337; MGC117209\n",
"Example 4: PCDH-GAMMA-A9\n",
"Example 5: PCDH-GAMMA-A9\n"
]
}
],
"source": [
"# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n",
"soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
"gene_annotation = get_gene_annotation(soft_file)\n",
"\n",
"# 2. Analyze the gene annotation dataframe to identify which columns contain the gene identifiers and gene symbols\n",
"print(\"\\nGene annotation preview:\")\n",
"print(f\"Columns in gene annotation: {gene_annotation.columns.tolist()}\")\n",
"print(preview_df(gene_annotation, n=3))\n",
"\n",
"# Examine the columns to find gene information\n",
"print(\"\\nExamining gene mapping columns:\")\n",
"print(\"Column 'ID' examples (probe identifiers):\")\n",
"id_samples = gene_annotation['ID'].head(5).tolist()\n",
"for i, sample in enumerate(id_samples):\n",
" print(f\"Example {i+1}: {sample}\")\n",
"\n",
"# Look at Symbol column which contains gene symbols\n",
"print(\"\\nColumn 'Symbol' examples (contains gene symbols):\")\n",
"if 'Symbol' in gene_annotation.columns:\n",
" # Display a few examples of the Symbol column\n",
" symbol_samples = gene_annotation['Symbol'].head(10).tolist()\n",
" for i, sample in enumerate(symbol_samples):\n",
" print(f\"Example {i+1}: {sample}\")\n",
" \n",
" # Extract some gene symbols to verify\n",
" print(\"\\nChecking if symbols are proper human gene symbols:\")\n",
" human_gene_count = 0\n",
" total_symbols = 50 # Check a few more rows\n",
" symbol_samples_extended = gene_annotation['Symbol'].dropna().head(total_symbols).tolist()\n",
" for i, sample in enumerate(symbol_samples_extended[:10]): # Show first 10 examples\n",
" symbols = extract_human_gene_symbols(str(sample))\n",
" is_human = bool(symbols) and symbols[0] == sample\n",
" human_gene_count += int(is_human)\n",
" print(f\"Example {i+1}: '{sample}' - {'Likely human gene' if is_human else 'Not standard human gene'}\")\n",
" \n",
" print(f\"\\nOut of {total_symbols} examined symbols, {human_gene_count} appear to be standard human gene symbols.\")\n",
" \n",
" # Identify the columns needed for gene mapping\n",
" print(\"\\nColumns identified for gene mapping:\")\n",
" print(\"- 'ID': Contains probe IDs (e.g., ILMN_3166687)\")\n",
" print(\"- 'Symbol': Contains gene symbols (e.g., ERCC-00162)\")\n",
"else:\n",
" print(\"Error: 'Symbol' column not found in annotation data.\")\n",
"\n",
"# Check if there are other columns that might contain human gene information\n",
"print(\"\\nChecking other columns that might contain gene information:\")\n",
"potential_gene_columns = ['ILMN_Gene', 'Entrez_Gene_ID', 'Synonyms']\n",
"for col in potential_gene_columns:\n",
" if col in gene_annotation.columns:\n",
" print(f\"\\nExamples from '{col}' column:\")\n",
" examples = gene_annotation[col].dropna().head(5).tolist()\n",
" for i, ex in enumerate(examples):\n",
" print(f\"Example {i+1}: {ex}\")\n"
]
},
{
"cell_type": "markdown",
"id": "2d4b3039",
"metadata": {},
"source": [
"### Step 6: Gene Identifier Mapping"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "79d2a13e",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T07:04:56.665533Z",
"iopub.status.busy": "2025-03-25T07:04:56.665412Z",
"iopub.status.idle": "2025-03-25T07:04:57.673066Z",
"shell.execute_reply": "2025-03-25T07:04:57.672407Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gene mapping dataframe shape: (29377, 2)\n",
"First few rows of the mapping dataframe:\n",
" ID Gene\n",
"0 ILMN_3166687 ERCC-00162\n",
"1 ILMN_3165566 ERCC-00071\n",
"2 ILMN_3164811 ERCC-00009\n",
"3 ILMN_3165363 ERCC-00053\n",
"4 ILMN_3166511 ERCC-00144\n",
"\n",
"Overlap between expression data and mapping data: 29377 probes\n",
"Percentage of expression probes with mapping: 100.00%\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Gene-level expression data shape: (20211, 60)\n",
"First few rows of gene expression data:\n",
" GSM4064970 GSM4064971 GSM4064972 GSM4064973 GSM4064974 \\\n",
"Gene \n",
"A1BG 62.355348 10.278570 54.362789 5.764988 23.992323 \n",
"A1CF 43.321260 22.707244 16.152246 24.781712 55.479592 \n",
"A26C3 5.361441 5.217641 5.870840 9.111313 5.317085 \n",
"A2BP1 23878.363768 12539.905276 20798.931157 6185.899282 18210.533055 \n",
"A2LD1 22.563890 31.984639 11.114187 5.945264 17.456945 \n",
"\n",
" GSM4064975 GSM4064976 GSM4064977 GSM4064978 GSM4064979 \\\n",
"Gene \n",
"A1BG 31.366236 267.552914 1879.845723 112.182100 78.592842 \n",
"A1CF 16.285685 26.136836 862.133248 22.251610 92.120753 \n",
"A26C3 5.510478 59.801096 5.260670 4.438418 8.331549 \n",
"A2BP1 14989.807316 945.588591 29775.725031 12359.110313 21510.281217 \n",
"A2LD1 7.291426 20.684290 8.635058 8.021981 5.260201 \n",
"\n",
" ... GSM4065020 GSM4065021 GSM4065022 GSM4065023 \\\n",
"Gene ... \n",
"A1BG ... 68.604577 33.742407 234.644583 123.994745 \n",
"A1CF ... 33.850215 40.503179 98.087848 115.634796 \n",
"A26C3 ... 8.256577 8.036368 16.416072 8.146367 \n",
"A2BP1 ... 24898.485537 29705.993455 34889.485138 33383.894840 \n",
"A2LD1 ... 211.336860 87.354851 115.817957 95.939720 \n",
"\n",
" GSM4065024 GSM4065025 GSM4065026 GSM4065027 GSM4065028 \\\n",
"Gene \n",
"A1BG 153.160805 133.941268 499.330427 436.495579 59.558639 \n",
"A1CF 24.050889 65.535743 46.055937 84.467518 2220.419381 \n",
"A26C3 10.478260 9.509930 14.867590 6.962255 3454.632917 \n",
"A2BP1 39630.297277 32499.364331 17721.571171 23042.866664 1348.045338 \n",
"A2LD1 48.082713 92.580961 102.398870 420.111491 7022.980141 \n",
"\n",
" GSM4065029 \n",
"Gene \n",
"A1BG 392.801435 \n",
"A1CF 66.581181 \n",
"A26C3 32.552785 \n",
"A2BP1 27774.726832 \n",
"A2LD1 334.353840 \n",
"\n",
"[5 rows x 60 columns]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gene expression data saved to ../../output/preprocess/Brugada_Syndrome/gene_data/GSE136992.csv\n"
]
}
],
"source": [
"# 1. Identify the columns for gene mapping\n",
"# From the previous output, we can see:\n",
"# - 'ID' column contains the probe identifiers (ILMN_xxxx) which match the gene identifiers in the gene expression data\n",
"# - 'Symbol' column contains gene symbols we want to map to\n",
"\n",
"# 2. Create a gene mapping dataframe\n",
"mapping_df = get_gene_mapping(gene_annotation, 'ID', 'Symbol')\n",
"print(f\"Gene mapping dataframe shape: {mapping_df.shape}\")\n",
"print(\"First few rows of the mapping dataframe:\")\n",
"print(mapping_df.head())\n",
"\n",
"# Check overlap between probes in expression data and mapping data\n",
"genes_in_expr = set(gene_data.index)\n",
"genes_in_mapping = set(mapping_df['ID'])\n",
"overlap = genes_in_expr.intersection(genes_in_mapping)\n",
"print(f\"\\nOverlap between expression data and mapping data: {len(overlap)} probes\")\n",
"print(f\"Percentage of expression probes with mapping: {len(overlap)/len(genes_in_expr)*100:.2f}%\")\n",
"\n",
"# 3. Apply gene mapping to convert probe-level to gene-level expression\n",
"gene_data = apply_gene_mapping(gene_data, mapping_df)\n",
"print(f\"\\nGene-level expression data shape: {gene_data.shape}\")\n",
"print(\"First few rows of gene expression data:\")\n",
"print(gene_data.head())\n",
"\n",
"# Save the processed gene expression data\n",
"os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n",
"gene_data.to_csv(out_gene_data_file)\n",
"print(f\"Gene expression data saved to {out_gene_data_file}\")\n"
]
},
{
"cell_type": "markdown",
"id": "b1c721e1",
"metadata": {},
"source": [
"### Step 7: Data Normalization and Linking"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "a51715e5",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T07:04:57.675091Z",
"iopub.status.busy": "2025-03-25T07:04:57.674946Z",
"iopub.status.idle": "2025-03-25T07:05:08.367897Z",
"shell.execute_reply": "2025-03-25T07:05:08.367046Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gene data shape after normalization: (19450, 60)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Normalized gene data saved to ../../output/preprocess/Brugada_Syndrome/gene_data/GSE136992.csv\n",
"Clinical features saved to ../../output/preprocess/Brugada_Syndrome/clinical_data/GSE136992.csv\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"For the feature 'Brugada_Syndrome', the least common label is '0.0' with 29 occurrences. This represents 48.33% of the dataset.\n",
"The distribution of the feature 'Brugada_Syndrome' in this dataset is fine.\n",
"\n",
"Quartiles for 'Age':\n",
" 25%: 0.375\n",
" 50% (Median): 24.0\n",
" 75%: 52.0\n",
"Min: 0.0\n",
"Max: 144.0\n",
"The distribution of the feature 'Age' in this dataset is fine.\n",
"\n",
"For the feature 'Gender', the least common label is '1.0' with 30 occurrences. This represents 50.00% of the dataset.\n",
"The distribution of the feature 'Gender' in this dataset is fine.\n",
"\n",
"A new JSON file was created at: ../../output/preprocess/Brugada_Syndrome/cohort_info.json\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Linked data saved to ../../output/preprocess/Brugada_Syndrome/GSE136992.csv\n"
]
}
],
"source": [
"# 1. Normalize gene symbols in the gene expression data\n",
"normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n",
"print(f\"Gene data shape after normalization: {normalized_gene_data.shape}\")\n",
"\n",
"# Create output directory if it doesn't exist\n",
"os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n",
"\n",
"# Save the normalized gene data\n",
"normalized_gene_data.to_csv(out_gene_data_file)\n",
"print(f\"Normalized gene data saved to {out_gene_data_file}\")\n",
"\n",
"# 2. Check if clinical data is available before trying to extract features\n",
"if trait_row is not None:\n",
" # Extract clinical features only if trait_row exists\n",
" clinical_features = geo_select_clinical_features(\n",
" clinical_data,\n",
" trait=trait,\n",
" trait_row=trait_row,\n",
" convert_trait=convert_trait,\n",
" age_row=age_row,\n",
" convert_age=convert_age,\n",
" gender_row=gender_row,\n",
" convert_gender=convert_gender\n",
" )\n",
" \n",
" # Save clinical features\n",
" os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n",
" clinical_features.to_csv(out_clinical_data_file)\n",
" print(f\"Clinical features saved to {out_clinical_data_file}\")\n",
" \n",
" # Link clinical and genetic data\n",
" linked_data = geo_link_clinical_genetic_data(clinical_features, normalized_gene_data)\n",
" \n",
" # Handle missing values and check for bias\n",
" linked_data = handle_missing_values(linked_data, trait)\n",
" is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n",
"else:\n",
" # No clinical data available\n",
" print(\"No clinical data available for the trait of interest.\")\n",
" linked_data = pd.DataFrame() # Empty dataframe\n",
" is_biased = True # Dataset is biased since we have no trait data\n",
"\n",
"# 6. Validate and save cohort info\n",
"is_trait_available = trait_row is not None\n",
"is_usable = validate_and_save_cohort_info(\n",
" is_final=True,\n",
" cohort=cohort,\n",
" info_path=json_path,\n",
" is_gene_available=is_gene_available,\n",
" is_trait_available=is_trait_available,\n",
" is_biased=is_biased,\n",
" df=linked_data,\n",
" note=\"Dataset contains gene expression data but lacks COVID-19 trait information.\"\n",
")\n",
"\n",
"# 7. Save the linked data if it's usable\n",
"if is_usable:\n",
" # Create output directory if it doesn't exist\n",
" os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n",
" \n",
" # Save the linked data\n",
" linked_data.to_csv(out_data_file)\n",
" print(f\"Linked data saved to {out_data_file}\")\n",
"else:\n",
" print(\"Linked data not saved due to quality issues.\")"
]
}
],
"metadata": {
"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.10.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|