File size: 37,702 Bytes
92d2f89 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "889b6fb9",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:38:14.303508Z",
"iopub.status.busy": "2025-03-25T06:38:14.303270Z",
"iopub.status.idle": "2025-03-25T06:38:14.468510Z",
"shell.execute_reply": "2025-03-25T06:38:14.468079Z"
}
},
"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 = \"Arrhythmia\"\n",
"cohort = \"GSE93101\"\n",
"\n",
"# Input paths\n",
"in_trait_dir = \"../../input/GEO/Arrhythmia\"\n",
"in_cohort_dir = \"../../input/GEO/Arrhythmia/GSE93101\"\n",
"\n",
"# Output paths\n",
"out_data_file = \"../../output/preprocess/Arrhythmia/GSE93101.csv\"\n",
"out_gene_data_file = \"../../output/preprocess/Arrhythmia/gene_data/GSE93101.csv\"\n",
"out_clinical_data_file = \"../../output/preprocess/Arrhythmia/clinical_data/GSE93101.csv\"\n",
"json_path = \"../../output/preprocess/Arrhythmia/cohort_info.json\"\n"
]
},
{
"cell_type": "markdown",
"id": "23c4de08",
"metadata": {},
"source": [
"### Step 1: Initial Data Loading"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "762dab80",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:38:14.469794Z",
"iopub.status.busy": "2025-03-25T06:38:14.469653Z",
"iopub.status.idle": "2025-03-25T06:38:14.557086Z",
"shell.execute_reply": "2025-03-25T06:38:14.556597Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Background Information:\n",
"!Series_title\t\"Molecular Prognosis of Cardiogenic Shock Patients under Extracorporeal Membrane Oxygenation\"\n",
"!Series_summary\t\"Prognosis for cardiogenic shock patients under ECMO was our study goal. Success defined as survived more than 7 days after ECMO installation and failure died or had multiple organ failure in 7 days. Total 34 cases were enrolled, 17 success and 17 failure.\"\n",
"!Series_summary\t\"Peripheral blood mononuclear cells collected at ECMO installation were used analyzed.\"\n",
"!Series_overall_design\t\"Analysis of the cardiogenic shock patients at extracorporeal membrane oxygenation treatment by genome-wide expression and methylation. Transcriptomic profiling and DNA methylation between successful and failure groups were analyzed.\"\n",
"!Series_overall_design\t\"This submission represents the transcriptome data.\"\n",
"Sample Characteristics Dictionary:\n",
"{0: ['course: Acute myocarditis', 'course: Acute myocardial infarction', 'course: Dilated cardiomyopathy, DCMP', 'course: Congestive heart failure', 'course: Dilated cardiomyopathy', 'course: Arrhythmia', 'course: Aortic dissection'], 1: ['age: 33.4', 'age: 51.2', 'age: 51.9', 'age: 47.8', 'age: 41.5', 'age: 67.3', 'age: 52.8', 'age: 16.1', 'age: 78.9', 'age: 53.2', 'age: 70.9', 'age: 59.9', 'age: 21.9', 'age: 45.2', 'age: 52.4', 'age: 32.3', 'age: 55.8', 'age: 47', 'age: 57.3', 'age: 31.7', 'age: 49.3', 'age: 66.1', 'age: 55.9', 'age: 49.1', 'age: 63', 'age: 21', 'age: 53.6', 'age: 50.1', 'age: 37.4', 'age: 71.5'], 2: ['gender: F', 'gender: M'], 3: ['outcome: Success', 'outcome: Failure', 'outcome: failure']}\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": "a304d5e6",
"metadata": {},
"source": [
"### Step 2: Dataset Analysis and Clinical Feature Extraction"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6bdd38ca",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:38:14.558621Z",
"iopub.status.busy": "2025-03-25T06:38:14.558509Z",
"iopub.status.idle": "2025-03-25T06:38:14.563763Z",
"shell.execute_reply": "2025-03-25T06:38:14.563314Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cannot extract clinical features: clinical data matrix not available\n",
"The sample characteristics dictionary only provides possible values, not sample-specific data\n",
"Clinical data extraction skipped due to missing proper clinical data matrix format.\n"
]
}
],
"source": [
"import pandas as pd\n",
"import os\n",
"from typing import Optional, Dict, Any, Callable\n",
"import json\n",
"\n",
"# Set variables based on analysis\n",
"is_gene_available = True # The dataset appears to contain gene expression data based on the Series_overall_design\n",
"\n",
"# 2.1 Data Availability\n",
"# Based on the sample characteristics dictionary:\n",
"trait_row = 0 # Course of disease (contains Arrhythmia)\n",
"age_row = 1 # Age information\n",
"gender_row = 2 # Gender information\n",
"\n",
"# 2.2 Data Type Conversion Functions\n",
"def convert_trait(value: str) -> int:\n",
" \"\"\"Convert trait value to binary (0 or 1).\"\"\"\n",
" if value is None:\n",
" return None\n",
" # Extract the value after the colon and strip whitespace\n",
" if \":\" in value:\n",
" value = value.split(\":\", 1)[1].strip()\n",
" \n",
" # Check if \"Arrhythmia\" is in the value\n",
" return 1 if \"Arrhythmia\" in value else 0\n",
"\n",
"def convert_age(value: str) -> float:\n",
" \"\"\"Convert age value to continuous float.\"\"\"\n",
" if value is None:\n",
" return None\n",
" # Extract the value after the colon and strip whitespace\n",
" if \":\" in value:\n",
" value = value.split(\":\", 1)[1].strip()\n",
" \n",
" try:\n",
" return float(value)\n",
" except (ValueError, TypeError):\n",
" return None\n",
"\n",
"def convert_gender(value: str) -> int:\n",
" \"\"\"Convert gender value to binary (0 for female, 1 for male).\"\"\"\n",
" if value is None:\n",
" return None\n",
" # Extract the value after the colon and strip whitespace\n",
" if \":\" in value:\n",
" value = value.split(\":\", 1)[1].strip()\n",
" \n",
" if value.upper() == 'F':\n",
" return 0\n",
" elif value.upper() == 'M':\n",
" return 1\n",
" else:\n",
" return None\n",
"\n",
"# 3. Save Metadata\n",
"is_trait_available = trait_row is not None\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\n",
"if trait_row is not None:\n",
" try:\n",
" # Since we don't have the actual clinical data matrix and cannot create one from the\n",
" # sample characteristics dictionary directly, we'll skip this step for now\n",
" print(\"Cannot extract clinical features: clinical data matrix not available\")\n",
" print(\"The sample characteristics dictionary only provides possible values, not sample-specific data\")\n",
" \n",
" # Create a note about this dataset\n",
" note = \"Clinical data extraction skipped due to missing proper clinical data matrix format.\"\n",
" print(note)\n",
" except Exception as e:\n",
" print(f\"Error processing clinical data: {e}\")\n",
"else:\n",
" print(\"Clinical data not available. Skipping clinical feature extraction.\")\n"
]
},
{
"cell_type": "markdown",
"id": "ebb9c0ce",
"metadata": {},
"source": [
"### Step 3: Gene Data Extraction"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a98817f4",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:38:14.565213Z",
"iopub.status.busy": "2025-03-25T06:38:14.565107Z",
"iopub.status.idle": "2025-03-25T06:38:14.688631Z",
"shell.execute_reply": "2025-03-25T06:38:14.688125Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Matrix file found: ../../input/GEO/Arrhythmia/GSE93101/GSE93101_series_matrix.txt.gz\n",
"Gene data shape: (29363, 33)\n",
"First 20 gene/probe identifiers:\n",
"Index(['ILMN_1343291', 'ILMN_1651209', 'ILMN_1651228', 'ILMN_1651229',\n",
" 'ILMN_1651235', 'ILMN_1651236', 'ILMN_1651237', 'ILMN_1651238',\n",
" 'ILMN_1651254', 'ILMN_1651260', 'ILMN_1651262', 'ILMN_1651268',\n",
" 'ILMN_1651278', 'ILMN_1651282', 'ILMN_1651285', 'ILMN_1651286',\n",
" 'ILMN_1651292', 'ILMN_1651303', 'ILMN_1651309', 'ILMN_1651315'],\n",
" dtype='object', name='ID')\n"
]
}
],
"source": [
"# 1. Get the SOFT and matrix file paths again \n",
"soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
"print(f\"Matrix file found: {matrix_file}\")\n",
"\n",
"# 2. Use the get_genetic_data function from the library to get the gene_data\n",
"try:\n",
" gene_data = get_genetic_data(matrix_file)\n",
" print(f\"Gene data shape: {gene_data.shape}\")\n",
" \n",
" # 3. Print the first 20 row IDs (gene or probe identifiers)\n",
" print(\"First 20 gene/probe identifiers:\")\n",
" print(gene_data.index[:20])\n",
"except Exception as e:\n",
" print(f\"Error extracting gene data: {e}\")\n"
]
},
{
"cell_type": "markdown",
"id": "74948b52",
"metadata": {},
"source": [
"### Step 4: Gene Identifier Review"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "0db0c762",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:38:14.689770Z",
"iopub.status.busy": "2025-03-25T06:38:14.689655Z",
"iopub.status.idle": "2025-03-25T06:38:14.691676Z",
"shell.execute_reply": "2025-03-25T06:38:14.691338Z"
}
},
"outputs": [],
"source": [
"# Analyzing the gene identifiers from the previous step\n",
"\n",
"# The identifiers start with \"ILMN_\" which indicates they are Illumina probe IDs\n",
"# Illumina probe IDs are not human gene symbols, they need to be mapped to gene symbols\n",
"# These are likely from an Illumina BeadArray microarray platform\n",
"\n",
"# Therefore, gene mapping is required\n",
"requires_gene_mapping = True\n"
]
},
{
"cell_type": "markdown",
"id": "fa8273b4",
"metadata": {},
"source": [
"### Step 5: Gene Annotation"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "bedb2310",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:38:14.692685Z",
"iopub.status.busy": "2025-03-25T06:38:14.692581Z",
"iopub.status.idle": "2025-03-25T06:38:25.677024Z",
"shell.execute_reply": "2025-03-25T06:38:25.676520Z"
}
},
"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', 'ILMN_3165363', 'ILMN_3166511'], 'Transcript': ['ILMN_333737', 'ILMN_333646', 'ILMN_333584', 'ILMN_333628', 'ILMN_333719'], 'Species': ['ILMN Controls', 'ILMN Controls', 'ILMN Controls', 'ILMN Controls', 'ILMN Controls'], 'Source': ['ILMN_Controls', 'ILMN_Controls', 'ILMN_Controls', 'ILMN_Controls', 'ILMN_Controls'], 'Search_Key': ['ERCC-00162', 'ERCC-00071', 'ERCC-00009', 'ERCC-00053', 'ERCC-00144'], 'ILMN_Gene': ['ERCC-00162', 'ERCC-00071', 'ERCC-00009', 'ERCC-00053', 'ERCC-00144'], 'Source_Reference_ID': ['ERCC-00162', 'ERCC-00071', 'ERCC-00009', 'ERCC-00053', 'ERCC-00144'], 'RefSeq_ID': [nan, nan, nan, nan, nan], 'Entrez_Gene_ID': [nan, nan, nan, nan, nan], 'GI': [nan, nan, nan, nan, nan], 'Accession': ['DQ516750', 'DQ883654', 'DQ668364', 'DQ516785', 'DQ854995'], 'Symbol': ['ERCC-00162', 'ERCC-00071', 'ERCC-00009', 'ERCC-00053', 'ERCC-00144'], 'Protein_Product': [nan, nan, nan, nan, nan], 'Array_Address_Id': [5270161.0, 4260594.0, 7610424.0, 5260356.0, 2030196.0], 'Probe_Type': ['S', 'S', 'S', 'S', 'S'], 'Probe_Start': [12.0, 224.0, 868.0, 873.0, 130.0], 'SEQUENCE': ['CCCATGTGTCCAATTCTGAATATCTTTCCAGCTAAGTGCTTCTGCCCACC', 'GGATTAACTGCTGTGGTGTGTCATACTCGGCTACCTCCTGGTTTGGCGTC', 'GACCACGCCTTGTAATCGTATGACACGCGCTTGACACGACTGAATCCAGC', 'CTGCAATGCCATTAACAACCTTAGCACGGTATTTCCAGTAGCTGGTGAGC', 'CGTGCAGACAGGGATCGTAAGGCGATCCAGCCGGTATACCTTAGTCACAT'], 'Chromosome': [nan, nan, nan, nan, nan], 'Probe_Chr_Orientation': [nan, nan, nan, nan, nan], 'Probe_Coordinates': [nan, nan, nan, nan, nan], 'Cytoband': [nan, nan, 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', 'Methanocaldococcus jannaschii spike-in control MJ-1000-68 genomic sequence', 'Synthetic construct clone AG006.1100 external RNA control sequence'], 'Ontology_Component': [nan, nan, nan, nan, nan], 'Ontology_Process': [nan, nan, nan, nan, nan], 'Ontology_Function': [nan, nan, nan, nan, nan], 'Synonyms': [nan, nan, nan, nan, nan], 'Obsolete_Probe_Id': [nan, nan, nan, nan, nan], 'GB_ACC': ['DQ516750', 'DQ883654', 'DQ668364', 'DQ516785', 'DQ854995']}\n",
"\n",
"Analyzing SPOT_ID.1 column for gene symbols:\n",
"\n",
"Gene data ID prefix: ILMN\n",
"Column 'ID' contains values matching gene data ID pattern\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Column 'Transcript' contains values matching gene data ID pattern\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Column 'Species' contains values matching gene data ID pattern\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Column 'Source' contains values matching gene data ID pattern\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Checking for columns containing transcript or gene related terms:\n",
"Column 'Transcript' may contain gene-related information\n",
"Sample values: ['ILMN_333737', 'ILMN_333646', 'ILMN_333584']\n",
"Column 'ILMN_Gene' may contain gene-related information\n",
"Sample values: ['ERCC-00162', 'ERCC-00071', 'ERCC-00009']\n",
"Column 'Entrez_Gene_ID' may contain gene-related information\n",
"Sample values: [nan, nan, nan]\n",
"Column 'Symbol' may contain gene-related information\n",
"Sample values: ['ERCC-00162', 'ERCC-00071', 'ERCC-00009']\n"
]
}
],
"source": [
"# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\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=5))\n",
"\n",
"# Check for gene information in the SPOT_ID.1 column which appears to contain gene names\n",
"print(\"\\nAnalyzing SPOT_ID.1 column for gene symbols:\")\n",
"if 'SPOT_ID.1' in gene_annotation.columns:\n",
" # Extract a few sample values\n",
" sample_values = gene_annotation['SPOT_ID.1'].head(3).tolist()\n",
" for i, value in enumerate(sample_values):\n",
" print(f\"Sample {i+1} excerpt: {value[:200]}...\") # Print first 200 chars\n",
" # Test the extract_human_gene_symbols function on these values\n",
" symbols = extract_human_gene_symbols(value)\n",
" print(f\" Extracted gene symbols: {symbols}\")\n",
"\n",
"# Try to find the probe IDs in the gene annotation\n",
"gene_data_id_prefix = gene_data.index[0].split('_')[0] # Get prefix of first gene ID\n",
"print(f\"\\nGene data ID prefix: {gene_data_id_prefix}\")\n",
"\n",
"# Look for columns that might match the gene data IDs\n",
"for col in gene_annotation.columns:\n",
" if gene_annotation[col].astype(str).str.contains(gene_data_id_prefix).any():\n",
" print(f\"Column '{col}' contains values matching gene data ID pattern\")\n",
"\n",
"# Check if there's any column that might contain transcript or gene IDs\n",
"print(\"\\nChecking for columns containing transcript or gene related terms:\")\n",
"for col in gene_annotation.columns:\n",
" if any(term in col.upper() for term in ['GENE', 'TRANSCRIPT', 'SYMBOL', 'NAME', 'DESCRIPTION']):\n",
" print(f\"Column '{col}' may contain gene-related information\")\n",
" # Show sample values\n",
" print(f\"Sample values: {gene_annotation[col].head(3).tolist()}\")\n"
]
},
{
"cell_type": "markdown",
"id": "debd03f9",
"metadata": {},
"source": [
"### Step 6: Gene Identifier Mapping"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "8658340f",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:38:25.678943Z",
"iopub.status.busy": "2025-03-25T06:38:25.678819Z",
"iopub.status.idle": "2025-03-25T06:38:26.269667Z",
"shell.execute_reply": "2025-03-25T06:38:26.269133Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gene mapping dataframe shape: (29377, 2)\n",
"First few rows of 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",
"Gene expression dataframe shape: (20206, 33)\n",
"First few rows of gene expression dataframe:\n",
" GSM2443799 GSM2443800 GSM2443801 GSM2443802 GSM2443803 \\\n",
"Gene \n",
"A1BG 129.442547 142.061233 103.958331 137.556161 111.260768 \n",
"A1CF 460.835089 324.958428 484.608278 683.954295 657.945539 \n",
"A26C3 117.769485 96.247228 143.474170 113.274705 111.123349 \n",
"A2BP1 445.728633 419.931068 1118.462328 882.773847 455.880246 \n",
"A2LD1 726.498733 129.188312 273.126915 724.925706 1183.148561 \n",
"\n",
" GSM2443804 GSM2443805 GSM2443806 GSM2443807 GSM2443808 ... \\\n",
"Gene ... \n",
"A1BG 241.767585 157.977946 147.578249 113.936195 161.539471 ... \n",
"A1CF 483.623025 388.058988 347.761757 846.802093 348.534342 ... \n",
"A26C3 189.907418 121.229217 180.446535 114.821849 146.988180 ... \n",
"A2BP1 629.064099 482.388074 472.663155 673.371186 451.317487 ... \n",
"A2LD1 831.739064 430.191854 980.267191 1435.172976 438.148076 ... \n",
"\n",
" GSM2443822 GSM2443823 GSM2443824 GSM2443825 GSM2443826 \\\n",
"Gene \n",
"A1BG 117.848741 124.533076 132.452962 144.929004 187.460276 \n",
"A1CF 369.897346 1241.655372 318.911691 281.418179 331.841325 \n",
"A26C3 179.599911 149.774005 97.226031 120.221383 168.306395 \n",
"A2BP1 401.373193 480.150197 447.940559 404.073618 485.758301 \n",
"A2LD1 387.785812 675.875024 345.430061 840.092985 251.316867 \n",
"\n",
" GSM2443827 GSM2443828 GSM2443829 GSM2443830 GSM2443831 \n",
"Gene \n",
"A1BG 146.166922 170.230229 158.397937 160.564160 164.299385 \n",
"A1CF 328.965408 349.474755 815.252732 290.193532 293.717875 \n",
"A26C3 150.220434 107.689969 112.205759 132.270634 131.737339 \n",
"A2BP1 461.588680 373.266036 396.053696 410.223933 427.722595 \n",
"A2LD1 1059.292255 755.505991 415.081233 702.809967 649.972564 \n",
"\n",
"[5 rows x 33 columns]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Gene expression data saved to ../../output/preprocess/Arrhythmia/gene_data/GSE93101.csv\n"
]
}
],
"source": [
"# 1. Identify the columns for gene identifiers and gene symbols\n",
"# From the previous output, we can see:\n",
"# - 'ID' column contains the probe IDs (ILMN_*)\n",
"# - 'Symbol' column contains gene symbols\n",
"\n",
"# 2. Create the gene mapping dataframe\n",
"mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol')\n",
"print(f\"Gene mapping dataframe shape: {mapping_df.shape}\")\n",
"print(\"First few rows of mapping dataframe:\")\n",
"print(mapping_df.head())\n",
"\n",
"# 3. Apply the gene mapping to convert probe-level data to gene-level data\n",
"gene_data = apply_gene_mapping(gene_data, mapping_df)\n",
"\n",
"# Show the first few rows of the gene expression dataframe\n",
"print(f\"\\nGene expression dataframe shape: {gene_data.shape}\")\n",
"print(\"First few rows of gene expression dataframe:\")\n",
"print(gene_data.head())\n",
"\n",
"# Save the gene expression data to CSV\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\"\\nGene expression data saved to {out_gene_data_file}\")\n"
]
},
{
"cell_type": "markdown",
"id": "89be3833",
"metadata": {},
"source": [
"### Step 7: Data Normalization and Linking"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "7a51c83c",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:38:26.271463Z",
"iopub.status.busy": "2025-03-25T06:38:26.271338Z",
"iopub.status.idle": "2025-03-25T06:38:32.503432Z",
"shell.execute_reply": "2025-03-25T06:38:32.503085Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gene data shape before normalization: (20206, 33)\n",
"Gene data shape after normalization: (19445, 33)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Normalized gene expression data saved to ../../output/preprocess/Arrhythmia/gene_data/GSE93101.csv\n",
"Original clinical data preview:\n",
" !Sample_geo_accession GSM2443799 \\\n",
"0 !Sample_characteristics_ch1 course: Acute myocarditis \n",
"1 !Sample_characteristics_ch1 age: 33.4 \n",
"2 !Sample_characteristics_ch1 gender: F \n",
"3 !Sample_characteristics_ch1 outcome: Success \n",
"\n",
" GSM2443800 GSM2443801 \\\n",
"0 course: Acute myocarditis course: Acute myocarditis \n",
"1 age: 51.2 age: 51.9 \n",
"2 gender: M gender: F \n",
"3 outcome: Success outcome: Failure \n",
"\n",
" GSM2443802 GSM2443803 \\\n",
"0 course: Acute myocardial infarction course: Acute myocarditis \n",
"1 age: 47.8 age: 41.5 \n",
"2 gender: M gender: F \n",
"3 outcome: Success outcome: Failure \n",
"\n",
" GSM2443804 GSM2443805 \\\n",
"0 course: Acute myocardial infarction course: Acute myocardial infarction \n",
"1 age: 67.3 age: 52.8 \n",
"2 gender: M gender: M \n",
"3 outcome: Failure outcome: Success \n",
"\n",
" GSM2443806 GSM2443807 \\\n",
"0 course: Dilated cardiomyopathy, DCMP course: Acute myocardial infarction \n",
"1 age: 16.1 age: 78.9 \n",
"2 gender: M gender: M \n",
"3 outcome: Failure outcome: Failure \n",
"\n",
" ... GSM2443822 GSM2443823 \\\n",
"0 ... course: Congestive heart failure course: Aortic dissection \n",
"1 ... age: 66.1 age: 55.9 \n",
"2 ... gender: M gender: M \n",
"3 ... outcome: Success outcome: Failure \n",
"\n",
" GSM2443824 GSM2443825 \\\n",
"0 course: Dilated cardiomyopathy, DCMP course: Acute myocardial infarction \n",
"1 age: 49.1 age: 63 \n",
"2 gender: F gender: M \n",
"3 outcome: Failure outcome: Failure \n",
"\n",
" GSM2443826 GSM2443827 \\\n",
"0 course: Dilated cardiomyopathy, DCMP course: Acute myocardial infarction \n",
"1 age: 21 age: 53.6 \n",
"2 gender: M gender: M \n",
"3 outcome: Failure outcome: Success \n",
"\n",
" GSM2443828 GSM2443829 \\\n",
"0 course: Acute myocardial infarction course: Acute myocardial infarction \n",
"1 age: 50.1 age: 37.4 \n",
"2 gender: F gender: M \n",
"3 outcome: Success outcome: Failure \n",
"\n",
" GSM2443830 GSM2443831 \n",
"0 course: Acute myocarditis course: Congestive heart failure \n",
"1 age: 71.5 age: 56.5 \n",
"2 gender: F gender: M \n",
"3 outcome: Success outcome: Success \n",
"\n",
"[4 rows x 34 columns]\n",
"Selected clinical data shape: (3, 33)\n",
"Clinical data preview:\n",
" GSM2443799 GSM2443800 GSM2443801 GSM2443802 GSM2443803 \\\n",
"Arrhythmia 0.0 0.0 0.0 0.0 0.0 \n",
"Age 33.4 51.2 51.9 47.8 41.5 \n",
"Gender 0.0 1.0 0.0 1.0 0.0 \n",
"\n",
" GSM2443804 GSM2443805 GSM2443806 GSM2443807 GSM2443808 ... \\\n",
"Arrhythmia 0.0 0.0 0.0 0.0 0.0 ... \n",
"Age 67.3 52.8 16.1 78.9 53.2 ... \n",
"Gender 1.0 1.0 1.0 1.0 1.0 ... \n",
"\n",
" GSM2443822 GSM2443823 GSM2443824 GSM2443825 GSM2443826 \\\n",
"Arrhythmia 0.0 0.0 0.0 0.0 0.0 \n",
"Age 66.1 55.9 49.1 63.0 21.0 \n",
"Gender 1.0 1.0 0.0 1.0 1.0 \n",
"\n",
" GSM2443827 GSM2443828 GSM2443829 GSM2443830 GSM2443831 \n",
"Arrhythmia 0.0 0.0 0.0 0.0 0.0 \n",
"Age 53.6 50.1 37.4 71.5 56.5 \n",
"Gender 1.0 0.0 1.0 0.0 1.0 \n",
"\n",
"[3 rows x 33 columns]\n",
"Linked data shape before processing: (33, 19448)\n",
"Linked data preview (first 5 rows, 5 columns):\n",
" Arrhythmia Age Gender A1BG A1BG-AS1\n",
"GSM2443799 0.0 33.4 0.0 129.442547 1330.542639\n",
"GSM2443800 0.0 51.2 1.0 142.061233 2177.610030\n",
"GSM2443801 0.0 51.9 0.0 103.958331 1130.866630\n",
"GSM2443802 0.0 47.8 1.0 137.556161 1116.450458\n",
"GSM2443803 0.0 41.5 0.0 111.260768 1112.964973\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Data shape after handling missing values: (33, 19448)\n",
"For the feature 'Arrhythmia', the least common label is '1.0' with 2 occurrences. This represents 6.06% of the dataset.\n",
"The distribution of the feature 'Arrhythmia' in this dataset is severely biased.\n",
"\n",
"Quartiles for 'Age':\n",
" 25%: 45.2\n",
" 50% (Median): 52.4\n",
" 75%: 56.5\n",
"Min: 16.1\n",
"Max: 78.9\n",
"The distribution of the feature 'Age' in this dataset is fine.\n",
"\n",
"For the feature 'Gender', the least common label is '0.0' with 10 occurrences. This represents 30.30% of the dataset.\n",
"The distribution of the feature 'Gender' in this dataset is fine.\n",
"\n",
"Data shape after removing biased features: (33, 19448)\n",
"Dataset is not usable for analysis. No linked data file saved.\n"
]
}
],
"source": [
"# 1. Normalize gene symbols in the gene expression data\n",
"# Use normalize_gene_symbols_in_index to standardize gene symbols\n",
"normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n",
"print(f\"Gene data shape before normalization: {gene_data.shape}\")\n",
"print(f\"Gene data shape after normalization: {normalized_gene_data.shape}\")\n",
"\n",
"# Save the normalized gene data to file\n",
"os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n",
"normalized_gene_data.to_csv(out_gene_data_file)\n",
"print(f\"Normalized gene expression data saved to {out_gene_data_file}\")\n",
"\n",
"# Load the actual clinical data from the matrix file that was previously obtained in Step 1\n",
"soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
"background_info, clinical_data = get_background_and_clinical_data(matrix_file)\n",
"\n",
"# Get preview of clinical data to understand its structure\n",
"print(\"Original clinical data preview:\")\n",
"print(clinical_data.head())\n",
"\n",
"# 2. If we have trait data available, proceed with linking\n",
"if trait_row is not None:\n",
" # Extract clinical features using the original clinical data\n",
" selected_clinical_df = geo_select_clinical_features(\n",
" clinical_df=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",
" print(f\"Selected clinical data shape: {selected_clinical_df.shape}\")\n",
" print(\"Clinical data preview:\")\n",
" print(selected_clinical_df.head())\n",
"\n",
" # Link the clinical and genetic data\n",
" linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)\n",
" print(f\"Linked data shape before processing: {linked_data.shape}\")\n",
" print(\"Linked data preview (first 5 rows, 5 columns):\")\n",
" print(linked_data.iloc[:5, :5] if not linked_data.empty else \"Empty dataframe\")\n",
"\n",
" # 3. Handle missing values\n",
" try:\n",
" linked_data = handle_missing_values(linked_data, trait)\n",
" print(f\"Data shape after handling missing values: {linked_data.shape}\")\n",
" except Exception as e:\n",
" print(f\"Error handling missing values: {e}\")\n",
" linked_data = pd.DataFrame() # Create empty dataframe if error occurs\n",
"\n",
" # 4. Check for bias in features\n",
" if not linked_data.empty and linked_data.shape[0] > 0:\n",
" is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n",
" print(f\"Data shape after removing biased features: {linked_data.shape}\")\n",
" else:\n",
" is_biased = True\n",
" print(\"Cannot check for bias as dataframe is empty or has no rows after missing value handling\")\n",
"\n",
" # 5. Validate and save cohort information\n",
" note = \"\"\n",
" if linked_data.empty or linked_data.shape[0] == 0:\n",
" note = \"Dataset contains gene expression data related to liver fibrosis progression, but linking clinical and genetic data failed, possibly due to mismatched sample IDs.\"\n",
" else:\n",
" note = \"Dataset contains gene expression data for liver fibrosis progression, which is relevant to liver cirrhosis research.\"\n",
" \n",
" is_usable = validate_and_save_cohort_info(\n",
" is_final=True,\n",
" cohort=cohort,\n",
" info_path=json_path,\n",
" is_gene_available=True,\n",
" is_trait_available=True,\n",
" is_biased=is_biased,\n",
" df=linked_data,\n",
" note=note\n",
" )\n",
"\n",
" # 6. Save the linked data if usable\n",
" if is_usable:\n",
" os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n",
" linked_data.to_csv(out_data_file)\n",
" print(f\"Linked data saved to {out_data_file}\")\n",
" else:\n",
" print(\"Dataset is not usable for analysis. No linked data file saved.\")\n",
"else:\n",
" # If no trait data available, validate with trait_available=False\n",
" is_usable = validate_and_save_cohort_info(\n",
" is_final=True,\n",
" cohort=cohort,\n",
" info_path=json_path,\n",
" is_gene_available=True,\n",
" is_trait_available=False,\n",
" is_biased=True, # Set to True since we can't use data without trait\n",
" df=pd.DataFrame(), # Empty DataFrame\n",
" note=\"Dataset contains gene expression data but lacks proper clinical trait information for liver cirrhosis analysis.\"\n",
" )\n",
" \n",
" print(\"Dataset is not usable for liver cirrhosis analysis due to lack of clinical trait data. No linked data file saved.\")"
]
}
],
"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
}
|