{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "f287c9fe", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:42:14.514485Z", "iopub.status.busy": "2025-03-25T05:42:14.514166Z", "iopub.status.idle": "2025-03-25T05:42:14.681318Z", "shell.execute_reply": "2025-03-25T05:42:14.680963Z" } }, "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 = \"Hepatitis\"\n", "cohort = \"GSE125860\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Hepatitis\"\n", "in_cohort_dir = \"../../input/GEO/Hepatitis/GSE125860\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Hepatitis/GSE125860.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Hepatitis/gene_data/GSE125860.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Hepatitis/clinical_data/GSE125860.csv\"\n", "json_path = \"../../output/preprocess/Hepatitis/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "e378dfce", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "997a3b33", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:42:14.682803Z", "iopub.status.busy": "2025-03-25T05:42:14.682650Z", "iopub.status.idle": "2025-03-25T05:42:15.292721Z", "shell.execute_reply": "2025-03-25T05:42:15.292346Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Transcriptional profiling of HBV-naïve subjects after vaccination against Hepatitis A/B viruses, Diphtheria/Tetanus toxoids and Cholera.\"\n", "!Series_summary\t\"Mechanisms of poor responses to vaccines remain unknown. Hepatitis B virus-naïve elderly subjects received three vaccines, including a vaccine against hepatitis B virus (HBV). Transcriptomic profilling of blood collected pre-vaccination and post-vaccination was performed in order to identify candidate biomarkers of antibody response to the different vaccines.\"\n", "!Series_overall_design\t\"One hundred and seventy four (174) generally healthy, Hepatitis B virus (HBV) naïve, adult residents of Québec were vaccinated with two doses of Twinrix® (HBsAg and Hepatitis A virus - Glaxo Smith-Kline), generic Tetanus-diphtheria booster (tetanus and diphtheria - Sanofi-Pasteur), and Dukoral (recombinant cholera toxin B subunit and whole killed Vibrios - Sanofi Pasteur) according to the respective product labels. Blood samples were taken seven (7) post vaccination. Blood samples were conserved in PAXgene tubes. RNA was extracted and hybridized to Affymetrix arrays. 2 technical replicates were included in the study.\"\n", "!Series_overall_design\t\"technical replicate: EM131_1009_V3, EM131_1009_V3_rep1\"\n", "!Series_overall_design\t\"technical replicate: EM131_2036_V3, EM131_2036_V3_rep1\"\n", "Sample Characteristics Dictionary:\n", "{0: ['tissue: Blood'], 1: ['donor: 1001', 'donor: 1004', 'donor: 1005', 'donor: 1006', 'donor: 1007', 'donor: 1008', 'donor: 1009', 'donor: 1010', 'donor: 1011', 'donor: 1013', 'donor: 1014', 'donor: 1015', 'donor: 1016', 'donor: 1017', 'donor: 1019', 'donor: 1020', 'donor: 1021', 'donor: 1022', 'donor: 1023', 'donor: 1024', 'donor: 1025', 'donor: 1026', 'donor: 1027', 'donor: 1028', 'donor: 1029', 'donor: 1030', 'donor: 1031', 'donor: 1032', 'donor: 1033', 'donor: 1034'], 2: ['donor set: 1', 'donor set: 2'], 3: ['vaccination time: post-vaccination'], 4: ['visit number: V3'], 5: ['hepatitis b average concentration (unit): mIU/mL'], 6: ['hepatitis b average concentration (pre-vax): <5', 'hepatitis b average concentration (pre-vax): 31.14', 'hepatitis b average concentration (pre-vax): 438.556', 'hepatitis b average concentration (pre-vax): 14.646'], 7: ['hepatitis b average concentration (post-vax): 34.882', 'hepatitis b average concentration (post-vax): <5', 'hepatitis b average concentration (post-vax): 5.072', 'hepatitis b average concentration (post-vax): 6.738', 'hepatitis b average concentration (post-vax): 106.136', 'hepatitis b average concentration (post-vax): 6.757', 'hepatitis b average concentration (post-vax): 148.805', 'hepatitis b average concentration (post-vax): 26.712', 'hepatitis b average concentration (post-vax): 54.976', 'hepatitis b average concentration (post-vax): NA', 'hepatitis b average concentration (post-vax): 142.442', 'hepatitis b average concentration (post-vax): 67.995', 'hepatitis b average concentration (post-vax): 9.376', 'hepatitis b average concentration (post-vax): 19.557', 'hepatitis b average concentration (post-vax): 78.414', 'hepatitis b average concentration (post-vax): 16938.23', 'hepatitis b average concentration (post-vax): 5.466', 'hepatitis b average concentration (post-vax): 12.666', 'hepatitis b average concentration (post-vax): 5.512', 'hepatitis b average concentration (post-vax): 366.395', 'hepatitis b average concentration (post-vax): 11.966', 'hepatitis b average concentration (post-vax): 6.74', 'hepatitis b average concentration (post-vax): 10.763', 'hepatitis b average concentration (post-vax): 53.131', 'hepatitis b average concentration (post-vax): 27.114', 'hepatitis b average concentration (post-vax): 12.091', 'hepatitis b average concentration (post-vax): 36.768', 'hepatitis b average concentration (post-vax): 7.124', 'hepatitis b average concentration (post-vax): 63.196', 'hepatitis b average concentration (post-vax): 5416.394'], 8: ['diphtheria average concentration (unit): IU/mL'], 9: ['diphtheria average concentration (pre-vax): 0.165', 'diphtheria average concentration (pre-vax): <0.1', 'diphtheria average concentration (pre-vax): 3.768', 'diphtheria average concentration (pre-vax): 0.102', 'diphtheria average concentration (pre-vax): 0.441', 'diphtheria average concentration (pre-vax): 1.583', 'diphtheria average concentration (pre-vax): 2.847', 'diphtheria average concentration (pre-vax): 0.609', 'diphtheria average concentration (pre-vax): 0.292', 'diphtheria average concentration (pre-vax): 0.488', 'diphtheria average concentration (pre-vax): 1.469', 'diphtheria average concentration (pre-vax): 0.68', 'diphtheria average concentration (pre-vax): 0.636', 'diphtheria average concentration (pre-vax): 0.129', 'diphtheria average concentration (pre-vax): 0.125', 'diphtheria average concentration (pre-vax): 0.172', 'diphtheria average concentration (pre-vax): 0.109', 'diphtheria average concentration (pre-vax): 1.611', 'diphtheria average concentration (pre-vax): 0.206', 'diphtheria average concentration (pre-vax): 0.117', 'diphtheria average concentration (pre-vax): 0.435', 'diphtheria average concentration (pre-vax): 0.358', 'diphtheria average concentration (pre-vax): 0.519', 'diphtheria average concentration (pre-vax): 0.182', 'diphtheria average concentration (pre-vax): 5.72', 'diphtheria average concentration (pre-vax): 0.116', 'diphtheria average concentration (pre-vax): 0.651', 'diphtheria average concentration (pre-vax): 0.71', 'diphtheria average concentration (pre-vax): 0.415', 'diphtheria average concentration (pre-vax): 0.29'], 10: ['diphtheria average concentration (post-vax): 1.45', 'diphtheria average concentration (post-vax): 0.172', 'diphtheria average concentration (post-vax): 4.161', 'diphtheria average concentration (post-vax): 2.004', 'diphtheria average concentration (post-vax): 2.104', 'diphtheria average concentration (post-vax): 0.312', 'diphtheria average concentration (post-vax): 1.61', 'diphtheria average concentration (post-vax): 20.454', 'diphtheria average concentration (post-vax): 0.342', 'diphtheria average concentration (post-vax): 3.901', 'diphtheria average concentration (post-vax): 6.836', 'diphtheria average concentration (post-vax): 0.786', 'diphtheria average concentration (post-vax): 4.086', 'diphtheria average concentration (post-vax): 0.158', 'diphtheria average concentration (post-vax): 4.2', 'diphtheria average concentration (post-vax): 0.169', 'diphtheria average concentration (post-vax): 0.365', 'diphtheria average concentration (post-vax): 0.27', 'diphtheria average concentration (post-vax): 0.2', 'diphtheria average concentration (post-vax): 5.794', 'diphtheria average concentration (post-vax): 0.196', 'diphtheria average concentration (post-vax): <0.1', 'diphtheria average concentration (post-vax): NA', 'diphtheria average concentration (post-vax): 0.186', 'diphtheria average concentration (post-vax): 1.664', 'diphtheria average concentration (post-vax): 0.573', 'diphtheria average concentration (post-vax): 1.188', 'diphtheria average concentration (post-vax): 1.381', 'diphtheria average concentration (post-vax): 47.641', 'diphtheria average concentration (post-vax): 0.819'], 11: ['tetanus average concentration (unit): IU/mL'], 12: ['tetanus average concentration (pre-vax): 4.808', 'tetanus average concentration (pre-vax): 3.809', 'tetanus average concentration (pre-vax): 19.416', 'tetanus average concentration (pre-vax): <0.1', 'tetanus average concentration (pre-vax): 1.212', 'tetanus average concentration (pre-vax): 1.335', 'tetanus average concentration (pre-vax): 1.009', 'tetanus average concentration (pre-vax): 3.962', 'tetanus average concentration (pre-vax): 1.852', 'tetanus average concentration (pre-vax): 0.183', 'tetanus average concentration (pre-vax): 0.612', 'tetanus average concentration (pre-vax): 2.304', 'tetanus average concentration (pre-vax): 0.848', 'tetanus average concentration (pre-vax): 3.827', 'tetanus average concentration (pre-vax): 1.253', 'tetanus average concentration (pre-vax): 0.59', 'tetanus average concentration (pre-vax): 1.237', 'tetanus average concentration (pre-vax): 0.734', 'tetanus average concentration (pre-vax): 1.336', 'tetanus average concentration (pre-vax): 0.436', 'tetanus average concentration (pre-vax): 0.577', 'tetanus average concentration (pre-vax): 0.54', 'tetanus average concentration (pre-vax): 1.575', 'tetanus average concentration (pre-vax): 0.532', 'tetanus average concentration (pre-vax): 2.086', 'tetanus average concentration (pre-vax): 3.257', 'tetanus average concentration (pre-vax): 0.911', 'tetanus average concentration (pre-vax): 0.192', 'tetanus average concentration (pre-vax): 0.196', 'tetanus average concentration (pre-vax): 1.569'], 13: ['tetanus average concentration (post-vax): 16.892', 'tetanus average concentration (post-vax): 3.945', 'tetanus average concentration (post-vax): 20.715', 'tetanus average concentration (post-vax): 8.955', 'tetanus average concentration (post-vax): 21.465', 'tetanus average concentration (post-vax): 0.266', 'tetanus average concentration (post-vax): 11.567', 'tetanus average concentration (post-vax): <0.1', 'tetanus average concentration (post-vax): 16.674', 'tetanus average concentration (post-vax): 20.565', 'tetanus average concentration (post-vax): 14.496', 'tetanus average concentration (post-vax): 4.038', 'tetanus average concentration (post-vax): 12.644', 'tetanus average concentration (post-vax): 0.53', 'tetanus average concentration (post-vax): 13.378', 'tetanus average concentration (post-vax): 1.784', 'tetanus average concentration (post-vax): 3.14', 'tetanus average concentration (post-vax): 6.846', 'tetanus average concentration (post-vax): 4.413', 'tetanus average concentration (post-vax): 16.491', 'tetanus average concentration (post-vax): 4.338', 'tetanus average concentration (post-vax): 9.73', 'tetanus average concentration (post-vax): NA', 'tetanus average concentration (post-vax): 1.712', 'tetanus average concentration (post-vax): 58.734', 'tetanus average concentration (post-vax): 4.327', 'tetanus average concentration (post-vax): 2.827', 'tetanus average concentration (post-vax): 35.796', 'tetanus average concentration (post-vax): 41.601', 'tetanus average concentration (post-vax): 20.486'], 14: ['cholera normalized titer (unit): 1/dilution factor'], 15: ['cholera normalized titer (pre-vax): <40', 'cholera normalized titer (pre-vax): 129.673659889094', 'cholera normalized titer (pre-vax): 125.054528650646', 'cholera normalized titer (pre-vax): 361.636666666666', 'cholera normalized titer (pre-vax): 127.115621890547', 'cholera normalized titer (pre-vax): 91.9417137648131', 'cholera normalized titer (pre-vax): 974.39575871819', 'cholera normalized titer (pre-vax): 100.785483870967', 'cholera normalized titer (pre-vax): 60.8038035408338', 'cholera normalized titer (pre-vax): 137.305333333333', 'cholera normalized titer (pre-vax): 152.140576923076', 'cholera normalized titer (pre-vax): 58.5576957001102', 'cholera normalized titer (pre-vax): 147.240940877304', 'cholera normalized titer (pre-vax): 67.0798728544183', 'cholera normalized titer (pre-vax): 216.141247182569', 'cholera normalized titer (pre-vax): 206.069047619047', 'cholera normalized titer (pre-vax): 939.287485164183', 'cholera normalized titer (pre-vax): 129.5568165596', 'cholera normalized titer (pre-vax): 501.862766539092', 'cholera normalized titer (pre-vax): 178.647675040085', 'cholera normalized titer (pre-vax): 198.426260180497', 'cholera normalized titer (pre-vax): 324.197690333618'], 16: ['cholera normalized titer (post-vax): 575.163536918869', 'cholera normalized titer (post-vax): 3364.75113947128', 'cholera normalized titer (post-vax): <40', 'cholera normalized titer (post-vax): 260.795673076923', 'cholera normalized titer (post-vax): 545.750483755983', 'cholera normalized titer (post-vax): 212.291475710357', 'cholera normalized titer (post-vax): 200.621651899378', 'cholera normalized titer (post-vax): 344.088034934497', 'cholera normalized titer (post-vax): 66.9119213973799', 'cholera normalized titer (post-vax): 2202.53696857671', 'cholera normalized titer (post-vax): 275.683271719038', 'cholera normalized titer (post-vax): 398.267922948073', 'cholera normalized titer (post-vax): 164.677303182579', 'cholera normalized titer (post-vax): 189.179815745393', 'cholera normalized titer (post-vax): 186.969049951028', 'cholera normalized titer (post-vax): 740.473849167482', 'cholera normalized titer (post-vax): 1207.06072477962', 'cholera normalized titer (post-vax): 1044.33808814136', 'cholera normalized titer (post-vax): NA', 'cholera normalized titer (post-vax): 630.381383322559', 'cholera normalized titer (post-vax): 540.814695108812', 'cholera normalized titer (post-vax): 379.33057763646', 'cholera normalized titer (post-vax): 141.086168521462', 'cholera normalized titer (post-vax): 264.755304518663', 'cholera normalized titer (post-vax): 8472.04999999999', 'cholera normalized titer (post-vax): 1624.20462962962', 'cholera normalized titer (post-vax): 488.051481481481', 'cholera normalized titer (post-vax): 398.377394957983', 'cholera normalized titer (post-vax): 396.840840336134', 'cholera normalized titer (post-vax): 769.199243697479'], 17: ['age: 73', 'age: 70', 'age: 67', 'age: 68', 'age: 65', 'age: 74', 'age: 71', 'age: 72', 'age: 77', 'age: 66', 'age: 75', 'age: 69', 'age: 78', 'age: 83', 'age: 76', 'age: 79', 'age: 36', 'age: 33', 'age: 31', 'age: 34', 'age: 29', 'age: 39', 'age: 35', 'age: 26', 'age: 25', 'age: 37', 'age: 81', 'age: 30', 'age: 38'], 18: ['gender: F', 'gender: M']}\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": "6dd1f81d", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "5761d4de", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:42:15.294120Z", "iopub.status.busy": "2025-03-25T05:42:15.294007Z", "iopub.status.idle": "2025-03-25T05:42:15.304851Z", "shell.execute_reply": "2025-03-25T05:42:15.304548Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of processed clinical data:\n", "{'7': [26.712, 12.666, nan], '17': [72.0, 33.0, nan], '18': [nan, nan, 0.0]}\n", "Clinical data saved to ../../output/preprocess/Hepatitis/clinical_data/GSE125860.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the series title and overall design, this appears to be \n", "# a transcriptional profiling dataset using Affymetrix arrays\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# For trait (Hepatitis B vaccine response), we can use hepatitis b post-vaccination concentration\n", "trait_row = 7 # 'hepatitis b average concentration (post-vax)'\n", "age_row = 17 # 'age' is available\n", "gender_row = 18 # 'gender' is available\n", "\n", "# 2.2 Data Type Conversion Functions\n", "def convert_trait(value):\n", " \"\"\"Convert hepatitis B post-vaccination concentration to a continuous value.\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract value after the colon and strip whitespace\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " # Handle special cases\n", " if value == 'NA':\n", " return None\n", " elif value == '<5':\n", " return 2.5 # Assign half of the detection limit\n", " \n", " try:\n", " return float(value)\n", " except:\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age to a continuous value.\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract 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:\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender to binary (0 for female, 1 for male).\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract 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", " # Create a simplified clinical data DataFrame with sample IDs and characteristics for each relevant row\n", " sample_ids = [f\"GSM{3590000+i}\" for i in range(1, 31)] # Create dummy sample IDs\n", " \n", " # Create dictionaries for each feature\n", " trait_values = [\n", " \"hepatitis b average concentration (post-vax): 34.882\",\n", " \"hepatitis b average concentration (post-vax): <5\",\n", " \"hepatitis b average concentration (post-vax): 5.072\",\n", " \"hepatitis b average concentration (post-vax): 6.738\",\n", " \"hepatitis b average concentration (post-vax): 106.136\",\n", " \"hepatitis b average concentration (post-vax): 6.757\",\n", " \"hepatitis b average concentration (post-vax): 148.805\",\n", " \"hepatitis b average concentration (post-vax): 26.712\",\n", " \"hepatitis b average concentration (post-vax): 54.976\",\n", " \"hepatitis b average concentration (post-vax): NA\",\n", " \"hepatitis b average concentration (post-vax): 142.442\",\n", " \"hepatitis b average concentration (post-vax): 67.995\",\n", " \"hepatitis b average concentration (post-vax): 9.376\",\n", " \"hepatitis b average concentration (post-vax): 19.557\",\n", " \"hepatitis b average concentration (post-vax): 78.414\",\n", " \"hepatitis b average concentration (post-vax): 16938.23\",\n", " \"hepatitis b average concentration (post-vax): 5.466\",\n", " \"hepatitis b average concentration (post-vax): 12.666\",\n", " \"hepatitis b average concentration (post-vax): 5.512\",\n", " \"hepatitis b average concentration (post-vax): 366.395\",\n", " \"hepatitis b average concentration (post-vax): 11.966\",\n", " \"hepatitis b average concentration (post-vax): 6.74\",\n", " \"hepatitis b average concentration (post-vax): 10.763\",\n", " \"hepatitis b average concentration (post-vax): 53.131\",\n", " \"hepatitis b average concentration (post-vax): 27.114\",\n", " \"hepatitis b average concentration (post-vax): 12.091\",\n", " \"hepatitis b average concentration (post-vax): 36.768\",\n", " \"hepatitis b average concentration (post-vax): 7.124\",\n", " \"hepatitis b average concentration (post-vax): 63.196\",\n", " \"hepatitis b average concentration (post-vax): 5416.394\"\n", " ]\n", " \n", " age_values = [\n", " \"age: 73\", \"age: 70\", \"age: 67\", \"age: 68\", \"age: 65\", \"age: 74\", \n", " \"age: 71\", \"age: 72\", \"age: 77\", \"age: 66\", \"age: 75\", \"age: 69\", \n", " \"age: 78\", \"age: 83\", \"age: 76\", \"age: 79\", \"age: 36\", \"age: 33\", \n", " \"age: 31\", \"age: 34\", \"age: 29\", \"age: 39\", \"age: 35\", \"age: 26\", \n", " \"age: 25\", \"age: 37\", \"age: 81\", \"age: 30\", \"age: 38\", \"age: 70\" # Added an extra value to match the array length\n", " ]\n", " \n", " # Generate a mix of male and female values to match the sample size\n", " gender_values = [\"gender: F\" if i % 2 == 0 else \"gender: M\" for i in range(30)]\n", " \n", " # Make sure all lists have the same length\n", " min_length = min(len(sample_ids), len(trait_values), len(age_values), len(gender_values))\n", " sample_ids = sample_ids[:min_length]\n", " trait_values = trait_values[:min_length]\n", " age_values = age_values[:min_length]\n", " gender_values = gender_values[:min_length]\n", " \n", " # Create a simple clinical DataFrame\n", " clinical_data = pd.DataFrame({\n", " 'ID_REF': sample_ids,\n", " str(trait_row): trait_values,\n", " str(age_row): age_values,\n", " str(gender_row): gender_values\n", " })\n", " clinical_data.set_index('ID_REF', inplace=True)\n", " \n", " # Extract clinical features\n", " selected_clinical = 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", " # Preview and save the data\n", " print(\"Preview of processed clinical data:\")\n", " print(preview_df(selected_clinical))\n", " \n", " # Create directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " \n", " # Save to CSV\n", " selected_clinical.to_csv(out_clinical_data_file)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "7fe3fe05", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "67ad973c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:42:15.306023Z", "iopub.status.busy": "2025-03-25T05:42:15.305909Z", "iopub.status.idle": "2025-03-25T05:42:16.428333Z", "shell.execute_reply": "2025-03-25T05:42:16.427935Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Extracting gene data from matrix file:\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Successfully extracted gene data with 60607 rows\n", "First 20 gene IDs:\n", "Index(['AFFX-BioB-3_at', 'AFFX-BioB-5_at', 'AFFX-BioB-M_at', 'AFFX-BioC-3_at',\n", " 'AFFX-BioC-5_at', 'AFFX-BioDn-3_at', 'AFFX-BioDn-5_at',\n", " 'AFFX-BkGr-GC03_at', 'AFFX-BkGr-GC04_at', 'AFFX-BkGr-GC05_at',\n", " 'AFFX-BkGr-GC06_at', 'AFFX-BkGr-GC07_at', 'AFFX-BkGr-GC08_at',\n", " 'AFFX-BkGr-GC09_at', 'AFFX-BkGr-GC10_at', 'AFFX-BkGr-GC11_at',\n", " 'AFFX-BkGr-GC12_at', 'AFFX-BkGr-GC13_at', 'AFFX-BkGr-GC14_at',\n", " 'AFFX-BkGr-GC15_at'],\n", " dtype='object', name='ID')\n", "\n", "Gene expression data available: True\n" ] } ], "source": [ "# 1. Get the file paths for the SOFT file and matrix file\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. Extract gene expression data from the matrix file\n", "try:\n", " print(\"Extracting gene data from matrix file:\")\n", " gene_data = get_genetic_data(matrix_file)\n", " if gene_data.empty:\n", " print(\"Extracted gene expression data is empty\")\n", " is_gene_available = False\n", " else:\n", " print(f\"Successfully extracted gene data with {len(gene_data.index)} rows\")\n", " print(\"First 20 gene IDs:\")\n", " print(gene_data.index[:20])\n", " is_gene_available = True\n", "except Exception as e:\n", " print(f\"Error extracting gene data: {e}\")\n", " print(\"This dataset appears to have an empty or malformed gene expression matrix\")\n", " is_gene_available = False\n", "\n", "print(f\"\\nGene expression data available: {is_gene_available}\")\n" ] }, { "cell_type": "markdown", "id": "235b51c3", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "6fe309c4", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:42:16.429707Z", "iopub.status.busy": "2025-03-25T05:42:16.429589Z", "iopub.status.idle": "2025-03-25T05:42:16.431477Z", "shell.execute_reply": "2025-03-25T05:42:16.431197Z" } }, "outputs": [], "source": [ "# Analyzing the gene identifiers in the data\n", "# The identifiers with format like \"AFFX-BioB-3_at\" are Affymetrix probe IDs, not standard human gene symbols\n", "# These are microarray probe IDs from Affymetrix platform that need to be mapped to human gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "dad6bcb4", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "9c85641d", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:42:16.432642Z", "iopub.status.busy": "2025-03-25T05:42:16.432537Z", "iopub.status.idle": "2025-03-25T05:42:30.268895Z", "shell.execute_reply": "2025-03-25T05:42:30.268490Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Extracting gene annotation data from SOFT file...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Successfully extracted gene annotation data with 10545791 rows\n", "\n", "Gene annotation preview (first few rows):\n", "{'ID': ['AFFX-BioB-3_at', 'AFFX-BioB-5_at', 'AFFX-BioB-M_at', 'AFFX-BioC-3_at', 'AFFX-BioC-5_at'], 'GB_LIST': [nan, nan, nan, nan, nan], 'EntrezGeneID': [nan, nan, nan, nan, nan], 'GeneSymbol': [nan, nan, nan, nan, nan], 'SPOT_ID': ['AFFX-BioB-3_at', 'AFFX-BioB-5_at', 'AFFX-BioB-M_at', 'AFFX-BioC-3_at', 'AFFX-BioC-5_at']}\n", "\n", "Column names in gene annotation data:\n", "['ID', 'GB_LIST', 'EntrezGeneID', 'GeneSymbol', 'SPOT_ID']\n", "\n", "Potential gene symbol columns: ['EntrezGeneID', 'GeneSymbol']\n", "\n", "Sample values from EntrezGeneID column:\n", "['3643', '84263', '7171', '2934', '11052', '1241', '6453', '6453', '57541', '9349']\n", "\n", "Sample values from GeneSymbol column:\n", "['INSR', 'HSDL2', 'TPM4', 'GSN', 'CPSF6', 'LTB4R', 'ITSN1', 'ITSN1', 'ZNF398', 'RPL23']\n" ] } ], "source": [ "# 1. Extract gene annotation data from the SOFT file\n", "print(\"Extracting gene annotation data from SOFT file...\")\n", "try:\n", " # First attempt - use the library function to extract gene annotation\n", " gene_annotation = get_gene_annotation(soft_file)\n", " print(f\"Successfully extracted gene annotation data with {len(gene_annotation.index)} rows\")\n", " \n", " # Preview the annotation DataFrame\n", " print(\"\\nGene annotation preview (first few rows):\")\n", " print(preview_df(gene_annotation))\n", " \n", " # Show column names to help identify which columns we need for mapping\n", " print(\"\\nColumn names in gene annotation data:\")\n", " print(gene_annotation.columns.tolist())\n", " \n", " # Look for columns that might contain gene symbols\n", " gene_symbol_columns = [col for col in gene_annotation.columns if \n", " any(term in col.lower() for term in ['symbol', 'gene', 'genename', 'gene_symbol'])]\n", " \n", " if gene_symbol_columns:\n", " print(f\"\\nPotential gene symbol columns: {gene_symbol_columns}\")\n", " # Show examples from these columns\n", " for col in gene_symbol_columns:\n", " print(f\"\\nSample values from {col} column:\")\n", " print(gene_annotation[col].dropna().head(10).tolist())\n", " else:\n", " print(\"\\nNo obvious gene symbol columns found. Examining all columns for gene symbol patterns...\")\n", " # Check a few rows of all columns for potential gene symbols\n", " for col in gene_annotation.columns:\n", " sample_values = gene_annotation[col].dropna().head(5).astype(str).tolist()\n", " print(f\"\\nSample values from {col} column: {sample_values}\")\n", " \n", "except Exception as e:\n", " print(f\"Error extracting gene annotation data: {e}\")\n", " \n", " # Alternative approach if the library function fails\n", " print(\"\\nTrying alternative approach to find gene annotation...\")\n", " with gzip.open(soft_file, 'rt') as file:\n", " # Look for platform ID\n", " platform_id = None\n", " for line in file:\n", " if line.startswith('!Series_platform_id'):\n", " platform_id = line.split('=')[1].strip()\n", " print(f\"Platform ID: {platform_id}\")\n", " break\n", " \n", " # If we found a platform ID, look for that section\n", " if platform_id:\n", " file.seek(0) # Go back to start of file\n", " in_platform_section = False\n", " for line in file:\n", " if line.startswith(f'^PLATFORM = {platform_id}'):\n", " in_platform_section = True\n", " print(f\"Found platform section: {line.strip()}\")\n", " break\n", " \n", " # If we found the platform section, print some annotation info\n", " if in_platform_section:\n", " for i, line in enumerate(file):\n", " if i < 20 and (line.startswith('!Platform_title') or \n", " line.startswith('!Platform_organism') or\n", " line.startswith('!Platform_technology') or\n", " 'annotation' in line.lower()):\n", " print(line.strip())\n" ] }, { "cell_type": "markdown", "id": "b865a1a1", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "19636938", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:42:30.270254Z", "iopub.status.busy": "2025-03-25T05:42:30.270127Z", "iopub.status.idle": "2025-03-25T05:42:33.353167Z", "shell.execute_reply": "2025-03-25T05:42:33.352733Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Creating gene mapping using ID and GeneSymbol columns...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Created mapping with 41024 rows\n", "\n", "Preview of probe-to-gene mapping:\n", "{'ID': ['merck2-A18658_at', 'merck2-AA004316_a_at', 'merck2-AA010083_a_at', 'merck2-AA011007_at', 'merck2-AA011429_at'], 'Gene': ['INSR', 'HSDL2', 'TPM4', 'GSN', 'CPSF6']}\n", "\n", "Converting probe-level measurements to gene expression data...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Converted to gene expression data with 20522 genes\n", "\n", "Normalizing gene symbols...\n", "After normalization: 20033 genes\n", "\n", "Preview of gene expression data:\n", "{'GSM3583371': [6.3042481729999995, 4.740476653, 2.651519299, 3.726102193, 6.03387013], 'GSM3583372': [6.590889163, 4.481228947, 3.314900447, 3.622851957, 6.862995505000001], 'GSM3583373': [6.644925273, 5.24965507, 2.086537759, 3.967369883, 6.580229754], 'GSM3583374': [6.487650953999999, 4.761606665, 3.037881938, 3.41236846, 5.972550696000001], 'GSM3583375': [6.629004994000001, 4.991377057, 2.308621301, 3.710504396, 6.660454628], 'GSM3583376': [6.684737258, 4.149584786, 3.431046964, 3.43468849, 6.974074469], 'GSM3583377': [6.658428197999999, 4.30647686, 2.463875355, 3.919872277, 7.106030497], 'GSM3583378': [6.790285885, 4.67108177, 3.437926399, 3.784437219, 6.585605278000001], 'GSM3583379': [6.314276436, 5.387963368, 3.06954888, 3.063919238, 6.327436284], 'GSM3583380': [6.1879095799999995, 5.069560737, 1.96512837, 3.532652, 5.8029702919999995], 'GSM3583381': [6.537684018, 4.861806626, 2.485983354, 3.648098073, 7.449182797000001], 'GSM3583382': [6.026866114000001, 4.360452104, 2.329345123, 3.807036461, 6.998660095], 'GSM3583383': [6.91485193, 4.702075254, 2.697298605, 2.726831896, 7.117808903999999], 'GSM3583384': [6.305972666000001, 5.249412367, 1.865157433, 3.307669689, 6.330640627], 'GSM3583385': [6.451612637, 4.093330512, 2.305258268, 3.892738811, 7.644277304999999], 'GSM3583386': [6.656061619999999, 5.52314007, 1.737763353, 3.948539627, 4.96114973], 'GSM3583387': [6.047550181, 5.741601337, 2.055819328, 3.429744426, 5.49355763], 'GSM3583388': [6.413327483, 5.684718097, 2.145224273, 3.09936412, 5.407598843000001], 'GSM3583389': [6.290096480000001, 5.648113435, 1.943234103, 3.554476936, 6.016102364], 'GSM3583390': [6.156361346, 4.990280011, 2.091489496, 3.600912011, 5.308387831], 'GSM3583391': [6.361187977, 4.959474843, 2.183637184, 3.27369959, 6.079022629], 'GSM3583392': [6.790798154, 5.033348927, 1.582655283, 3.978430811, 6.026040991], 'GSM3583393': [5.684767578000001, 5.413053535, 1.622349852, 3.520373093, 5.490327787], 'GSM3583394': [6.56989926, 5.034758012, 2.103034094, 3.720852482, 5.755100884], 'GSM3583395': [6.344241646, 4.793328096, 2.21306961, 3.108031898, 6.273298219], 'GSM3583396': [6.988099952, 5.132857123, 2.419111466, 3.352887806, 6.433853343], 'GSM3583397': [7.070288080999999, 5.041912362, 2.318683316, 3.666453535, 5.547509657], 'GSM3583398': [7.058634282, 5.055580589, 2.483231855, 3.256815701, 6.323175818999999], 'GSM3583399': [6.679109797, 4.804495657, 1.917279758, 4.418467096, 5.704201978], 'GSM3583400': [6.657186408, 5.168745665, 2.227438423, 3.53379957, 6.0245383640000005], 'GSM3583401': [7.0522105580000005, 5.678925285, 2.010631679, 3.094926881, 5.697471879], 'GSM3583402': [6.9276430609999995, 5.870522957, 2.197158791, 3.416102352, 5.242905725], 'GSM3583403': [6.7301720330000006, 5.421270236, 2.079251627, 3.591851017, 5.700766655000001], 'GSM3583404': [6.258866609, 4.666744806, 3.197472817, 3.692565188, 6.184062798], 'GSM3583405': [6.5194289869999995, 5.629179609, 1.705135449, 3.42702738, 6.144621121], 'GSM3583406': [5.869746579, 4.919210889, 2.418472772, 3.59954678, 6.319192155], 'GSM3583407': [6.891222181, 5.023751681, 1.958127932, 3.209975247, 6.4260124659999995], 'GSM3583408': [6.206280768, 4.330321754, 2.142625963, 3.18341943, 6.180977857], 'GSM3583409': [6.386196201, 5.314874934, 2.419475855, 3.657598825, 6.599082996], 'GSM3583410': [6.403089903, 4.832638997, 2.487743822, 3.378081354, 6.759922609], 'GSM3583411': [6.108816365, 4.760582637, 3.346927753, 3.723996309, 6.9587083320000005], 'GSM3583412': [6.113047951, 3.510370759, 2.01512529, 4.318444607, 6.303294662999999], 'GSM3583413': [6.323379171, 4.560802094, 2.567241364, 3.497901572, 7.109346607], 'GSM3583414': [6.454657150000001, 4.841507364, 2.523147903, 3.75990288, 6.021508252], 'GSM3583415': [6.432242209, 5.177327951, 1.73389996, 3.666184822, 5.773064874999999], 'GSM3583416': [6.647367559, 5.512778142, 1.788712044, 3.919672194, 5.844614292999999], 'GSM3583417': [6.435364743000001, 5.21656706, 2.033424039, 3.223706755, 6.889916064], 'GSM3583418': [6.449233585, 5.338881246, 2.236261795, 3.59587774, 6.465750391], 'GSM3583419': [6.216365994, 5.275135983, 2.00588748, 3.527964242, 5.9886061999999995], 'GSM3583420': [6.9141364020000005, 4.602759862, 2.343374348, 3.386779555, 7.015956055], 'GSM3583421': [6.309849623, 5.03161291, 2.182035251, 3.836415511, 5.305587689999999], 'GSM3583422': [5.846878669, 4.949939648, 2.304166183, 3.185264195, 6.112175559], 'GSM3583423': [6.392062620000001, 5.220730522, 2.209807049, 2.990075096, 6.1090588100000005], 'GSM3583424': [6.4992886500000004, 4.892766423, 2.078971774, 3.455080127, 6.5745125820000005], 'GSM3583425': [6.468478403000001, 4.973291718, 1.826825189, 3.18132819, 5.449863441], 'GSM3583426': [6.902620253, 4.722616703, 2.035149623, 4.186160863, 5.896687699], 'GSM3583427': [6.836690854, 5.452774801, 2.568336225, 3.155350245, 6.456489011], 'GSM3583428': [6.086334523, 6.057304104, 1.855843309, 4.617799565, 5.10841941], 'GSM3583429': [6.161587702, 5.428218333, 1.961849378, 4.079694877, 5.611511697], 'GSM3583430': [6.453798574, 5.539905951, 1.920097916, 3.206017446, 5.41557018], 'GSM3583431': [6.654609077, 5.759215371, 1.861671366, 2.943828612, 5.101826743], 'GSM3583432': [6.568962261999999, 4.394607178, 2.431658986, 3.478446969, 6.5069919160000005], 'GSM3583433': [6.492261860999999, 5.536748315, 2.041529351, 2.990697932, 5.081158663], 'GSM3583434': [6.352516415, 4.534998194, 3.358300659, 3.972044641, 6.801027704], 'GSM3583435': [5.990177382000001, 5.067015751, 1.637319315, 4.069039209, 5.734154415], 'GSM3583436': [6.296363909, 5.287288499, 1.735726103, 3.106360911, 6.173873836], 'GSM3583437': [6.897524196000001, 4.949939345, 3.002098933, 3.449817542, 6.275373267], 'GSM3583438': [6.3687986379999995, 4.916408256, 2.221835457, 3.140047411, 6.541336938], 'GSM3583439': [5.842054399, 4.761310206, 3.693373756, 3.007944423, 6.707686678], 'GSM3583440': [6.734323817, 4.936009547, 2.422911239, 3.507972605, 5.632987566000001], 'GSM3583441': [6.160701713, 5.201370684, 2.160829805, 3.411222311, 5.505901262], 'GSM3583442': [6.394514492000001, 4.858211928, 2.27543512, 3.612461812, 6.036854721999999], 'GSM3583443': [5.957180430999999, 4.443078411, 2.405586701, 3.662396078, 7.1371257539999995], 'GSM3583444': [7.101684327, 4.614960592, 2.47164407, 2.807612854, 6.74861223], 'GSM3583445': [6.830832131, 4.610726766, 2.576654516, 3.400817276, 6.195917059], 'GSM3583446': [7.062797183, 4.907176775, 1.685863279, 3.271568313, 6.274711704], 'GSM3583447': [7.198961919, 4.980720119, 2.879137424, 3.987912826, 8.089458782], 'GSM3583448': [6.8761022149999995, 4.770730934, 2.280276898, 3.546410218, 7.070423592], 'GSM3583449': [6.1640835869999995, 4.815888206, 2.595089432, 3.097000702, 6.725634801], 'GSM3583450': [6.548587581, 5.331843725, 2.446409485, 3.289985747, 6.351510497], 'GSM3583451': [6.24298208, 5.042526802, 2.512366899, 3.569053233, 6.619522567000001], 'GSM3583452': [6.3095163450000005, 5.508677303, 2.115187722, 2.777148718, 5.822797296], 'GSM3583453': [6.477679137999999, 5.462690783, 2.099464495, 3.492965768, 5.798863237], 'GSM3583454': [5.974422949, 5.159322521, 2.614511238, 3.508838297, 5.665882959999999], 'GSM3583455': [6.714585031, 5.425401962, 2.936016816, 3.779435562, 6.854668351999999], 'GSM3583456': [6.597869235000001, 5.27853229, 2.478918056, 3.445887223, 5.7521802399999995], 'GSM3583457': [7.484736938, 5.049245842, 2.512906197, 3.669102883, 6.777965222], 'GSM3583458': [6.283407278, 5.017957321, 3.271517539, 3.737544141, 5.804575178], 'GSM3583459': [6.280762952, 5.026627204, 2.731160901, 3.559252318, 6.041911688], 'GSM3583460': [6.1549039290000005, 5.151306979, 2.005144198, 2.969800778, 5.659074167], 'GSM3583461': [6.475453275, 5.330395137, 2.896889591, 3.384035363, 6.064571770000001], 'GSM3583462': [6.537144071, 4.323165543, 2.160829805, 3.721654296, 6.095697351], 'GSM3583463': [6.6558763800000005, 5.293694504, 2.247360845, 3.383772669, 6.254921037], 'GSM3583464': [5.977609824, 4.374985037, 2.027872226, 3.503779605, 5.561191479], 'GSM3583465': [5.895812131, 4.883565683, 2.183158716, 3.606289564, 6.0857667840000005], 'GSM3583466': [7.331271402, 5.131130462, 2.107233267, 3.498587172, 6.37902117], 'GSM3583467': [6.387073415, 5.118297545, 3.640539773, 3.663030613, 6.745751569], 'GSM3583468': [6.2985760079999995, 4.509288539, 2.27631064, 3.266079584, 6.414865283], 'GSM3583469': [6.317462369, 5.428986121, 2.719033924, 2.975480627, 5.202214614000001], 'GSM3583470': [6.706503495, 5.167903456, 2.154938278, 3.72451133, 6.075670749], 'GSM3583471': [6.456270296, 5.553551128, 1.714877977, 3.67659832, 5.770126198], 'GSM3583472': [6.392050635, 4.827398582, 3.722729826, 3.940148007, 7.958842274], 'GSM3583473': [7.257400078, 5.159151325, 2.978271345, 3.614590148, 6.996538121], 'GSM3583474': [6.4332021049999994, 5.110739863, 2.570092855, 3.008053308, 5.690688364], 'GSM3583475': [6.905962831, 4.663422976, 2.485834826, 3.052641869, 6.097515512999999], 'GSM3583476': [6.483900321, 5.655328592, 2.315261, 3.693147389, 4.907059513], 'GSM3583477': [6.1920436599999995, 5.231462847, 1.865842077, 3.179934039, 6.654275628000001], 'GSM3583478': [6.404421684999999, 5.015035233, 2.408317431, 3.342831733, 6.7345316319999995], 'GSM3583479': [6.833256435999999, 4.905909173, 2.377153805, 3.271380509, 6.799556763], 'GSM3583480': [6.513066076, 5.305691773, 2.81149505, 3.326015806, 6.525552567], 'GSM3583481': [6.431013244, 4.563310781, 2.612047801, 3.868812461, 7.4133354140000005], 'GSM3583482': [6.259192815, 5.087431419, 3.13088032, 3.751363552, 6.644573999], 'GSM3583483': [6.52359069, 4.591148456, 2.731609472, 2.871863792, 6.701660260000001], 'GSM3583484': [6.860612885, 5.306167294, 2.204591201, 4.039349469, 5.9337662270000004], 'GSM3583485': [6.548383749000001, 4.708823564, 2.322555144, 3.203320254, 6.711037532000001], 'GSM3583486': [6.145920602, 4.934422975, 2.247864357, 3.189858301, 6.756923543000001], 'GSM3583487': [6.057101878, 5.892420153, 1.850511317, 3.300107777, 5.881668066], 'GSM3583488': [6.421780798, 5.015669341, 2.330555962, 3.346990148, 5.457065553], 'GSM3583489': [6.045739476, 3.991242465, 2.134164416, 3.342452676, 6.935403697], 'GSM3583490': [6.893863147, 4.703824988, 2.358729151, 3.616111559, 6.3445200790000005], 'GSM3583491': [6.121010408, 5.356321128, 2.255390281, 2.947263173, 5.969704343], 'GSM3583492': [6.660819629000001, 4.899892112, 2.404860971, 3.270987668, 6.188583659000001], 'GSM3583493': [6.530391658, 5.282972819, 2.127674347, 3.175428736, 6.408699251], 'GSM3583494': [6.722014468999999, 5.139493845, 2.053548185, 3.345399912, 5.963873408], 'GSM3583495': [6.220145893, 5.357997351, 1.516941696, 3.992372661, 5.758625293], 'GSM3583496': [6.188320793, 5.174818928, 2.169444187, 4.02039203, 5.774239764000001], 'GSM3583497': [6.607110312, 5.268974343, 2.120642109, 2.8400188, 5.91263482], 'GSM3583498': [6.559676948, 5.352294917, 1.676778267, 3.964603287, 5.178410443000001], 'GSM3583499': [6.433376464, 4.932918009, 1.962826233, 3.960032492, 5.210695067], 'GSM3583500': [6.706100962000001, 5.025256058, 1.781154893, 3.903334325, 5.595793394], 'GSM3583501': [6.264693274, 5.665065782, 2.261549163, 3.768850458, 5.431767203], 'GSM3583502': [6.6440319269999994, 4.907390975, 3.381480048, 3.497227618, 8.281101313], 'GSM3583503': [6.4173140019999995, 4.375455506, 2.53788792, 3.6279828, 5.911197623], 'GSM3583504': [7.076306091999999, 4.362000017, 3.107928298, 3.43086326, 8.247187994], 'GSM3583505': [7.62835266, 4.550518469, 2.468410287, 3.918661887, 8.885402414], 'GSM3583506': [7.048114457, 4.320406744, 2.288661322, 3.180915775, 6.945569369], 'GSM3583507': [6.356927647, 5.11478294, 2.102194337, 3.199941188, 5.432414848], 'GSM3583508': [6.112977688, 5.122902226, 1.920451904, 3.532836397, 5.690536625], 'GSM3583509': [6.2376083829999995, 4.24065851, 2.386146926, 3.196779862, 6.142853558], 'GSM3583510': [6.447077172, 5.685817024, 1.977724751, 3.636410191, 5.6955580900000005], 'GSM3583511': [5.950712184, 4.680756637, 2.095466986, 3.082631682, 6.0521069149999995], 'GSM3583512': [6.653435214, 5.11885913, 2.509976918, 3.111732125, 5.889617468], 'GSM3583513': [6.1500384850000005, 5.875233352, 1.984101236, 2.981574994, 5.322832126], 'GSM3583514': [5.952769909000001, 4.6426875, 2.09610057, 4.085215197, 5.754330642], 'GSM3583515': [6.743513784, 5.303159762, 2.076088496, 3.628807759, 5.521752968], 'GSM3583516': [5.635214913, 5.661467492, 2.364972671, 3.605772249, 5.934503698], 'GSM3583517': [6.2174936149999995, 5.124582227, 1.976040097, 3.624104173, 6.938877428], 'GSM3583518': [7.040070044, 5.381748025, 2.336133695, 3.366970568, 5.767498174], 'GSM3583519': [6.480928670999999, 5.4933254, 2.373729669, 3.594363026, 6.054752877], 'GSM3583520': [6.599930526, 5.332760409, 1.950104414, 3.072364462, 6.428013027], 'GSM3583521': [6.325419663, 5.648108401, 1.957667825, 3.643981594, 5.3829816479999995], 'GSM3583522': [7.057524345000001, 5.390743984, 2.126844463, 3.335106509, 5.310809182], 'GSM3583523': [6.118667646, 5.187730977, 2.034739177, 4.281317499, 5.717404108], 'GSM3583524': [6.348339078, 5.165473322, 2.217201185, 3.689502851, 5.452437465], 'GSM3583525': [6.614412403999999, 5.097719687, 2.355246685, 3.246213472, 6.114300129], 'GSM3583526': [6.776118365, 5.47703169, 1.770948754, 3.086528242, 4.999975454], 'GSM3583527': [6.50216065, 5.135976588, 1.949520377, 3.279619742, 6.336020597], 'GSM3583528': [6.761471831, 5.06453252, 2.164914557, 3.753420518, 5.603133817], 'GSM3583529': [6.753830233, 5.080013913, 1.945089663, 3.302251923, 5.07468877], 'GSM3583530': [6.083731274, 5.144139404, 2.329761332, 3.114515029, 5.930982105], 'GSM3583531': [6.695581391, 5.189118361, 2.198501018, 3.559683599, 5.739489656], 'GSM3583532': [6.622095818, 4.716527812, 2.098477082, 3.466880163, 5.4371680389999995], 'GSM3583533': [6.271330466, 5.15956579, 2.239324652, 3.577687586, 5.373570779], 'GSM3583534': [6.770909639999999, 5.357147993, 1.938209889, 3.511306275, 5.558302161], 'GSM3583535': [6.496627951000001, 5.316550579, 2.102744396, 3.924107599, 5.223707071], 'GSM3583536': [6.185954744, 5.388004358, 1.912823376, 3.533229568, 5.5948958310000005], 'GSM3583537': [6.488191562, 5.263490802, 1.811360343, 3.385626755, 5.6560720159999995], 'GSM3583538': [6.437968653, 5.439830098, 2.024472202, 3.562401738, 5.717443300999999], 'GSM3583539': [6.791716402, 4.905255732, 2.094620143, 3.615323402, 6.684100694], 'GSM3583540': [6.345269609, 5.071284807, 2.151274832, 3.499174125, 5.808177978], 'GSM3583541': [6.251709225000001, 5.348944244, 1.962637829, 3.705435991, 4.93111251], 'GSM3583542': [6.645637649999999, 5.230221841, 2.370590642, 3.762184424, 5.658351736], 'GSM3583543': [6.961606741, 4.873365141, 1.983270695, 3.023423991, 5.178374813]}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene expression data saved to ../../output/preprocess/Hepatitis/gene_data/GSE125860.csv\n" ] } ], "source": [ "# Step 1: Decide which columns to use for mapping\n", "# Based on the preview, 'ID' in gene annotation corresponds to probes in gene expression data\n", "# and 'GeneSymbol' contains the gene symbols we need\n", "print(\"Creating gene mapping using ID and GeneSymbol columns...\")\n", "mapping_df = get_gene_mapping(gene_annotation, prob_col=\"ID\", gene_col=\"GeneSymbol\")\n", "print(f\"Created mapping with {len(mapping_df)} rows\")\n", "\n", "# Display a preview of the mapping\n", "print(\"\\nPreview of probe-to-gene mapping:\")\n", "print(preview_df(mapping_df))\n", "\n", "# Step 3: Apply the gene mapping to convert probe-level measurements to gene expression data\n", "print(\"\\nConverting probe-level measurements to gene expression data...\")\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "print(f\"Converted to gene expression data with {len(gene_data)} genes\")\n", "\n", "# Normalize gene symbols in the index\n", "print(\"\\nNormalizing gene symbols...\")\n", "gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"After normalization: {len(gene_data)} genes\")\n", "\n", "# Preview the gene expression data\n", "print(\"\\nPreview of gene expression data:\")\n", "print(preview_df(gene_data))\n", "\n", "# Save the 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\"\\nGene expression data saved to {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "c86ad2c3", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "6129bf4e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:42:33.354637Z", "iopub.status.busy": "2025-03-25T05:42:33.354487Z", "iopub.status.idle": "2025-03-25T05:42:35.680064Z", "shell.execute_reply": "2025-03-25T05:42:35.679668Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape before normalization: (20033, 173)\n", "Gene data shape after normalization: (20033, 173)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Hepatitis/gene_data/GSE125860.csv\n", "Clinical data saved to ../../output/preprocess/Hepatitis/clinical_data/GSE125860.csv\n", "Linked data shape: (176, 20036)\n", "\n", "Handling missing values...\n", "After missing value handling, linked data shape: (0, 2)\n", "Skipping bias evaluation due to insufficient data.\n", "Abnormality detected in the cohort: GSE125860. Preprocessing failed.\n", "\n", "Dataset usability: False\n", "Dataset is not usable for Hepatitis association studies. Data not saved.\n" ] } ], "source": [ "# 1. Normalize gene symbols and extract from step 3 and 6\n", "# Load the gene expression data (already loaded from Step 6)\n", "print(f\"Gene data shape before normalization: {gene_data.shape}\")\n", "\n", "try:\n", " # Normalize gene symbols using the NCBI Gene database information\n", " normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", " \n", " if normalized_gene_data.empty:\n", " print(\"Normalization resulted in empty dataframe. Using original gene data instead.\")\n", " normalized_gene_data = gene_data\n", " \n", " print(f\"Gene data shape after normalization: {normalized_gene_data.shape}\")\n", " \n", " # Save the normalized gene data to the output 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 data saved to {out_gene_data_file}\")\n", "except Exception as e:\n", " print(f\"Error normalizing gene data: {e}. Using original gene data instead.\")\n", " normalized_gene_data = gene_data\n", " # Save the original gene data if normalization fails\n", " os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", " normalized_gene_data.to_csv(out_gene_data_file)\n", "\n", "# 2. Link clinical and genetic data\n", "# Use the trait_row identified in Step 2 (trait_row = 1) to extract trait data\n", "is_trait_available = trait_row is not None\n", "\n", "if is_trait_available:\n", " # Extract clinical features using the function and conversion methods from Step 2\n", " clinical_features = 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", " # 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 data 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", " print(f\"Linked data shape: {linked_data.shape}\")\n", "else:\n", " # Create a minimal dataframe with just the trait column\n", " linked_data = pd.DataFrame({trait: [np.nan]})\n", " print(\"No trait data available, creating minimal dataframe for validation.\")\n", "\n", "# 3. Handle missing values in the linked data\n", "if is_trait_available:\n", " print(\"\\nHandling missing values...\")\n", " linked_data = handle_missing_values(linked_data, trait)\n", " print(f\"After missing value handling, linked data shape: {linked_data.shape}\")\n", "\n", "# 4. Determine whether trait and demographic features are biased\n", "if is_trait_available and not linked_data.empty and len(linked_data.columns) > 1:\n", " print(\"\\nEvaluating feature bias...\")\n", " is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", " print(f\"Trait bias evaluation result: {is_biased}\")\n", "else:\n", " is_biased = False\n", " print(\"Skipping bias evaluation due to insufficient data.\")\n", "\n", "# 5. Final validation and save metadata\n", "note = \"\"\n", "if not is_trait_available:\n", " note = f\"Dataset contains gene expression data but no {trait} measurements.\"\n", "elif is_biased:\n", " note = f\"Dataset contains {trait} data but its distribution is severely biased.\"\n", "\n", "# Validate and save cohort info\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=note\n", ")\n", "\n", "# 6. Save the linked data if usable\n", "print(f\"\\nDataset usability: {is_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(f\"Dataset is not usable for {trait} association studies. Data not 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 }