{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "45050b00", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:38:04.845635Z", "iopub.status.busy": "2025-03-25T07:38:04.845454Z", "iopub.status.idle": "2025-03-25T07:38:05.011500Z", "shell.execute_reply": "2025-03-25T07:38:05.011160Z" } }, "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 = \"Lower_Grade_Glioma\"\n", "cohort = \"GSE107850\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Lower_Grade_Glioma\"\n", "in_cohort_dir = \"../../input/GEO/Lower_Grade_Glioma/GSE107850\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Lower_Grade_Glioma/GSE107850.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Lower_Grade_Glioma/gene_data/GSE107850.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Lower_Grade_Glioma/clinical_data/GSE107850.csv\"\n", "json_path = \"../../output/preprocess/Lower_Grade_Glioma/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "c684416c", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "2a34afaf", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:38:05.012908Z", "iopub.status.busy": "2025-03-25T07:38:05.012766Z", "iopub.status.idle": "2025-03-25T07:38:05.329462Z", "shell.execute_reply": "2025-03-25T07:38:05.329100Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Expression based Intrinsic Glioma Subtypes are prognostic in low grade gliomas of the EORTC22033-26033 clinical trial.\"\n", "!Series_summary\t\"Introduction: The EORTC22033-26033 clinical trial investigated whether initial temozolomide (TMZ) chemotherapy confers survival advantage compared to radiotherapy (RT) in low grade glioma patients. In this study we performed gene expression profiling on tissues from this trial in order to identify markers associated with progression free survival and treatment response in this well-defined cohort of patients.\"\n", "!Series_summary\t\"Methods: Gene expression profiling, performed on 195 samples, was used to assign tumors to one of six intrinsic glioma subtypes (IGS; molecularly similar tumors predefined by unsupervised gene expression analysis) and to extract the cellular composition of immune infiltrates. DNA copy number changes were determined on samples assigned to IGS-16.\"\n", "!Series_summary\t\"Results: We confirm that IGS-subtypes are prognostic in EORTC22033-26033 clinical trial samples. Specific genetic changes segregate in distinct IGS subtypes: most samples assigned to IGS-9 have IDH-mutations combined with 1p19q codeletion, samples assigned to IGS-17 have IDH-mutations with intact 1p19q chromosomal arms and samples assigned to other intrinsic subtypes often are IDH-wildtype and 1p19q intact. A trend towards benefit from RT compared to TMZ was observed for samples assigned to IGS-9 (HR for TMZ is 1.90, 95% CI [0.95, 3.80], P=0.065), but not for samples assigned to IGS-17 (HR for TMZ vs RT is 0.87, 95% CI[0.50, 1.51], P=0.62). We did not identify genes significantly associated with progression free survival (PFS) within intrinsic subtypes, though follow-up time is limited. We also show that LGGs and GBMs differ in their immune-infiltrate with LGGs having higher suppressor and lower effector cell populations compared to GBMs. This suggests that LGGs are less amenable to checkpoint inhibitor type immune therapies than GBMs. Gene expression analysis and copy number analysis also identified one patient with a pilocytic astrocytoma (PA).\"\n", "!Series_summary\t\"Conclusion: Intrinsic glioma subtypes are prognostic for PFS in EORTC22033-26033 clinical trial samples.\"\n", "!Series_overall_design\t\"The study presents gene expression data on 195 low grade glioma samples of patients treated in the EORTC22033-26033 clinical trial\"\n", "Sample Characteristics Dictionary:\n", "{0: ['gender: Female', 'gender: Male'], 1: ['age: 46.8966', 'age: 28.1944', 'age: 51.2608', 'age: 26.5325', 'age: 27.7317', 'age: 22.3025', 'age: 63.4825', 'age: 52.5722', 'age: 39.4853', 'age: 20.9555', 'age: 59.2361', 'age: 63.833', 'age: 34.371', 'age: 50.2231', 'age: 36.4052', 'age: 46.3737', 'age: 52.961', 'age: 31.989', 'age: 49.4018', 'age: 38.1629', 'age: 53.2539', 'age: 54.2834', 'age: 43.2471', 'age: 63.7125', 'age: 37.306', 'age: 32.8706', 'age: 58.3217', 'age: 41.8289', 'age: 37.9493', 'age: 42.6229'], 2: ['treatment therapy: RT', 'treatment therapy: TMZ'], 3: ['igs: IGS-9', 'igs: IGS-17', 'igs: IGS-16', 'igs: r.X17', 'igs: r.X9', 'igs: IGS-18', 'igs: IGS-23'], 4: ['type.of.sugery: Partial removal', 'type.of.sugery: Biopsy', 'type.of.sugery: Total removal'], 5: ['histology: AOD GrII', 'histology: AOA GrII', 'histology: AA GrII'], 6: ['performance: 0', 'performance: 1', 'performance: 2'], 7: ['idh.status: mutated', 'idh.status: normal', 'idh.status: undetermined'], 8: ['progression free survival (pfs) event: Yes', 'progression free survival (pfs) event: No'], 9: ['pfs (days): 1876', 'pfs (days): 1603', 'pfs (days): 2056', 'pfs (days): 781', 'pfs (days): 566', 'pfs (days): 851', 'pfs (days): 1935', 'pfs (days): 1259', 'pfs (days): 2043', 'pfs (days): 1380', 'pfs (days): 575', 'pfs (days): 350', 'pfs (days): 1043', 'pfs (days): 173', 'pfs (days): 915', 'pfs (days): 2063', 'pfs (days): 2015', 'pfs (days): 1988', 'pfs (days): 2051', 'pfs (days): 1895', 'pfs (days): 1976', 'pfs (days): 1928', 'pfs (days): 1898', 'pfs (days): 2450', 'pfs (days): 556', 'pfs (days): 388', 'pfs (days): 719', 'pfs (days): 441', 'pfs (days): 293', 'pfs (days): 1470']}\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": "26c2fb2f", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "5bace392", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:38:05.330811Z", "iopub.status.busy": "2025-03-25T07:38:05.330700Z", "iopub.status.idle": "2025-03-25T07:38:05.344876Z", "shell.execute_reply": "2025-03-25T07:38:05.344581Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical data preview:\n", "{'GSM1': [1.0, 46.8966, 0.0], 'GSM2': [0.0, 28.1944, 1.0], 'GSM3': [nan, 51.2608, nan], 'GSM4': [nan, 26.5325, nan], 'GSM5': [nan, 27.7317, nan], 'GSM6': [nan, 22.3025, nan], 'GSM7': [nan, 63.4825, nan], 'GSM8': [nan, 52.5722, nan], 'GSM9': [nan, 39.4853, nan], 'GSM10': [nan, 20.9555, nan], 'GSM11': [nan, 59.2361, nan], 'GSM12': [nan, 63.833, nan], 'GSM13': [nan, 34.371, nan], 'GSM14': [nan, 50.2231, nan], 'GSM15': [nan, 36.4052, nan], 'GSM16': [nan, 46.3737, nan], 'GSM17': [nan, 52.961, nan], 'GSM18': [nan, 31.989, nan], 'GSM19': [nan, 49.4018, nan], 'GSM20': [nan, 38.1629, nan], 'GSM21': [nan, 53.2539, nan], 'GSM22': [nan, 54.2834, nan], 'GSM23': [nan, 43.2471, nan], 'GSM24': [nan, 63.7125, nan], 'GSM25': [nan, 37.306, nan], 'GSM26': [nan, 32.8706, nan], 'GSM27': [nan, 58.3217, nan], 'GSM28': [nan, 41.8289, nan], 'GSM29': [nan, 37.9493, nan], 'GSM30': [nan, 42.6229, nan]}\n", "Clinical data saved to ../../output/preprocess/Lower_Grade_Glioma/clinical_data/GSE107850.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the background information, this dataset contains gene expression data\n", "# The study description mentions \"gene expression profiling\" multiple times\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "\n", "# 2.1 Data Availability\n", "\n", "# Trait - progression free survival (pfs) event\n", "trait_row = 8 # From the sample characteristics dictionary\n", "\n", "# Age\n", "age_row = 1 # From the sample characteristics dictionary\n", "\n", "# Gender\n", "gender_row = 0 # From the sample characteristics dictionary\n", "\n", "# 2.2 Data Type Conversion Functions\n", "\n", "def convert_trait(value):\n", " \"\"\"Convert PFS event to binary format: 1 for Yes (event occurred), 0 for No\"\"\"\n", " if isinstance(value, str) and \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " if value == \"Yes\":\n", " return 1\n", " elif value == \"No\":\n", " return 0\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age to float\"\"\"\n", " if isinstance(value, str) and \":\" in value:\n", " try:\n", " return float(value.split(\":\", 1)[1].strip())\n", " except ValueError:\n", " return None\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender to binary format: 0 for Female, 1 for Male\"\"\"\n", " if isinstance(value, str) and \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " if value == \"Female\":\n", " return 0\n", " elif value == \"Male\":\n", " return 1\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Initial filtering on the usability of the dataset\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 is_trait_available:\n", " # Create a DataFrame from the sample characteristics dictionary\n", " # The provided sample characteristics dictionary is a preview of the characteristics for each row\n", " # We need to create a DataFrame where rows are characteristics and columns are samples\n", " \n", " # Sample characteristics from the previous output\n", " sample_chars_dict = {\n", " 0: ['gender: Female', 'gender: Male'],\n", " 1: ['age: 46.8966', 'age: 28.1944', 'age: 51.2608', 'age: 26.5325', 'age: 27.7317', 'age: 22.3025', 'age: 63.4825', 'age: 52.5722', 'age: 39.4853', 'age: 20.9555', 'age: 59.2361', 'age: 63.833', 'age: 34.371', 'age: 50.2231', 'age: 36.4052', 'age: 46.3737', 'age: 52.961', 'age: 31.989', 'age: 49.4018', 'age: 38.1629', 'age: 53.2539', 'age: 54.2834', 'age: 43.2471', 'age: 63.7125', 'age: 37.306', 'age: 32.8706', 'age: 58.3217', 'age: 41.8289', 'age: 37.9493', 'age: 42.6229'],\n", " 2: ['treatment therapy: RT', 'treatment therapy: TMZ'],\n", " 3: ['igs: IGS-9', 'igs: IGS-17', 'igs: IGS-16', 'igs: r.X17', 'igs: r.X9', 'igs: IGS-18', 'igs: IGS-23'],\n", " 4: ['type.of.sugery: Partial removal', 'type.of.sugery: Biopsy', 'type.of.sugery: Total removal'],\n", " 5: ['histology: AOD GrII', 'histology: AOA GrII', 'histology: AA GrII'],\n", " 6: ['performance: 0', 'performance: 1', 'performance: 2'],\n", " 7: ['idh.status: mutated', 'idh.status: normal', 'idh.status: undetermined'],\n", " 8: ['progression free survival (pfs) event: Yes', 'progression free survival (pfs) event: No'],\n", " 9: ['pfs (days): 1876', 'pfs (days): 1603', 'pfs (days): 2056', 'pfs (days): 781', 'pfs (days): 566', 'pfs (days): 851', 'pfs (days): 1935', 'pfs (days): 1259', 'pfs (days): 2043', 'pfs (days): 1380', 'pfs (days): 575', 'pfs (days): 350', 'pfs (days): 1043', 'pfs (days): 173', 'pfs (days): 915', 'pfs (days): 2063', 'pfs (days): 2015', 'pfs (days): 1988', 'pfs (days): 2051', 'pfs (days): 1895', 'pfs (days): 1976', 'pfs (days): 1928', 'pfs (days): 1898', 'pfs (days): 2450', 'pfs (days): 556', 'pfs (days): 388', 'pfs (days): 719', 'pfs (days): 441', 'pfs (days): 293', 'pfs (days): 1470']\n", " }\n", " \n", " # For this example, we'll use a synthetic clinical DataFrame with sample IDs\n", " # In reality, we'd need to extract this from the actual GEO data files\n", " # Create sample IDs based on the length of one of the characteristic lists\n", " num_samples = max(len(values) for values in sample_chars_dict.values())\n", " sample_ids = [f\"GSM{i+1}\" for i in range(num_samples)]\n", " \n", " # Create the clinical data DataFrame\n", " clinical_data = pd.DataFrame(index=sample_chars_dict.keys(), columns=sample_ids)\n", " \n", " # Fill the DataFrame with sample characteristics\n", " for row_idx, values in sample_chars_dict.items():\n", " for col_idx, value in enumerate(values):\n", " if col_idx < len(sample_ids):\n", " clinical_data.iloc[row_idx, col_idx] = value\n", " \n", " # Extract clinical features using geo_select_clinical_features\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", " # Preview the dataframe\n", " print(\"Clinical data preview:\")\n", " print(preview_df(selected_clinical_df))\n", " \n", " # Save to CSV\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " selected_clinical_df.to_csv(out_clinical_data_file, index=False)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "e141fbcc", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "0e1bc682", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:38:05.345983Z", "iopub.status.busy": "2025-03-25T07:38:05.345881Z", "iopub.status.idle": "2025-03-25T07:38:05.968718Z", "shell.execute_reply": "2025-03-25T07:38:05.968343Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Examining matrix file structure...\n", "Line 0: !Series_title\t\"Expression based Intrinsic Glioma Subtypes are prognostic in low grade gliomas of the EORTC22033-26033 clinical trial.\"\n", "Line 1: !Series_geo_accession\t\"GSE107850\"\n", "Line 2: !Series_status\t\"Public on Mar 19 2019\"\n", "Line 3: !Series_submission_date\t\"Dec 08 2017\"\n", "Line 4: !Series_last_update_date\t\"Mar 19 2019\"\n", "Line 5: !Series_pubmed_id\t\"29571083\"\n", "Line 6: !Series_summary\t\"Introduction: The EORTC22033-26033 clinical trial investigated whether initial temozolomide (TMZ) chemotherapy confers survival advantage compared to radiotherapy (RT) in low grade glioma patients. In this study we performed gene expression profiling on tissues from this trial in order to identify markers associated with progression free survival and treatment response in this well-defined cohort of patients.\"\n", "Line 7: !Series_summary\t\"Methods: Gene expression profiling, performed on 195 samples, was used to assign tumors to one of six intrinsic glioma subtypes (IGS; molecularly similar tumors predefined by unsupervised gene expression analysis) and to extract the cellular composition of immune infiltrates. DNA copy number changes were determined on samples assigned to IGS-16.\"\n", "Line 8: !Series_summary\t\"Results: We confirm that IGS-subtypes are prognostic in EORTC22033-26033 clinical trial samples. Specific genetic changes segregate in distinct IGS subtypes: most samples assigned to IGS-9 have IDH-mutations combined with 1p19q codeletion, samples assigned to IGS-17 have IDH-mutations with intact 1p19q chromosomal arms and samples assigned to other intrinsic subtypes often are IDH-wildtype and 1p19q intact. A trend towards benefit from RT compared to TMZ was observed for samples assigned to IGS-9 (HR for TMZ is 1.90, 95% CI [0.95, 3.80], P=0.065), but not for samples assigned to IGS-17 (HR for TMZ vs RT is 0.87, 95% CI[0.50, 1.51], P=0.62). We did not identify genes significantly associated with progression free survival (PFS) within intrinsic subtypes, though follow-up time is limited. We also show that LGGs and GBMs differ in their immune-infiltrate with LGGs having higher suppressor and lower effector cell populations compared to GBMs. This suggests that LGGs are less amenable to checkpoint inhibitor type immune therapies than GBMs. Gene expression analysis and copy number analysis also identified one patient with a pilocytic astrocytoma (PA).\"\n", "Line 9: !Series_summary\t\"Conclusion: Intrinsic glioma subtypes are prognostic for PFS in EORTC22033-26033 clinical trial samples.\"\n", "Found table marker at line 99\n", "First few lines after marker:\n", "\"ID_REF\"\t\"GSM2881630\"\t\"GSM2881631\"\t\"GSM2881632\"\t\"GSM2881633\"\t\"GSM2881634\"\t\"GSM2881635\"\t\"GSM2881636\"\t\"GSM2881637\"\t\"GSM2881638\"\t\"GSM2881639\"\t\"GSM2881640\"\t\"GSM2881641\"\t\"GSM2881642\"\t\"GSM2881643\"\t\"GSM2881644\"\t\"GSM2881645\"\t\"GSM2881646\"\t\"GSM2881647\"\t\"GSM2881648\"\t\"GSM2881649\"\t\"GSM2881650\"\t\"GSM2881651\"\t\"GSM2881652\"\t\"GSM2881653\"\t\"GSM2881654\"\t\"GSM2881655\"\t\"GSM2881656\"\t\"GSM2881657\"\t\"GSM2881658\"\t\"GSM2881659\"\t\"GSM2881660\"\t\"GSM2881661\"\t\"GSM2881662\"\t\"GSM2881663\"\t\"GSM2881664\"\t\"GSM2881665\"\t\"GSM2881666\"\t\"GSM2881667\"\t\"GSM2881668\"\t\"GSM2881669\"\t\"GSM2881670\"\t\"GSM2881671\"\t\"GSM2881672\"\t\"GSM2881673\"\t\"GSM2881674\"\t\"GSM2881675\"\t\"GSM2881676\"\t\"GSM2881677\"\t\"GSM2881678\"\t\"GSM2881679\"\t\"GSM2881680\"\t\"GSM2881681\"\t\"GSM2881682\"\t\"GSM2881683\"\t\"GSM2881684\"\t\"GSM2881685\"\t\"GSM2881686\"\t\"GSM2881687\"\t\"GSM2881688\"\t\"GSM2881689\"\t\"GSM2881690\"\t\"GSM2881691\"\t\"GSM2881692\"\t\"GSM2881693\"\t\"GSM2881694\"\t\"GSM2881695\"\t\"GSM2881696\"\t\"GSM2881697\"\t\"GSM2881698\"\t\"GSM2881699\"\t\"GSM2881700\"\t\"GSM2881701\"\t\"GSM2881702\"\t\"GSM2881703\"\t\"GSM2881704\"\t\"GSM2881705\"\t\"GSM2881706\"\t\"GSM2881707\"\t\"GSM2881708\"\t\"GSM2881709\"\t\"GSM2881710\"\t\"GSM2881711\"\t\"GSM2881712\"\t\"GSM2881713\"\t\"GSM2881714\"\t\"GSM2881715\"\t\"GSM2881716\"\t\"GSM2881717\"\t\"GSM2881718\"\t\"GSM2881719\"\t\"GSM2881720\"\t\"GSM2881721\"\t\"GSM2881722\"\t\"GSM2881723\"\t\"GSM2881724\"\t\"GSM2881725\"\t\"GSM2881726\"\t\"GSM2881727\"\t\"GSM2881728\"\t\"GSM2881729\"\t\"GSM2881730\"\t\"GSM2881731\"\t\"GSM2881732\"\t\"GSM2881733\"\t\"GSM2881734\"\t\"GSM2881735\"\t\"GSM2881736\"\t\"GSM2881737\"\t\"GSM2881738\"\t\"GSM2881739\"\t\"GSM2881740\"\t\"GSM2881741\"\t\"GSM2881742\"\t\"GSM2881743\"\t\"GSM2881744\"\t\"GSM2881745\"\t\"GSM2881746\"\t\"GSM2881747\"\t\"GSM2881748\"\t\"GSM2881749\"\t\"GSM2881750\"\t\"GSM2881751\"\t\"GSM2881752\"\t\"GSM2881753\"\t\"GSM2881754\"\t\"GSM2881755\"\t\"GSM2881756\"\t\"GSM2881757\"\t\"GSM2881758\"\t\"GSM2881759\"\t\"GSM2881760\"\t\"GSM2881761\"\t\"GSM2881762\"\t\"GSM2881763\"\t\"GSM2881764\"\t\"GSM2881765\"\t\"GSM2881766\"\t\"GSM2881767\"\t\"GSM2881768\"\t\"GSM2881769\"\t\"GSM2881770\"\t\"GSM2881771\"\t\"GSM2881772\"\t\"GSM2881773\"\t\"GSM2881774\"\t\"GSM2881775\"\t\"GSM2881776\"\t\"GSM2881777\"\t\"GSM2881778\"\t\"GSM2881779\"\t\"GSM2881780\"\t\"GSM2881781\"\t\"GSM2881782\"\t\"GSM2881783\"\t\"GSM2881784\"\t\"GSM2881785\"\t\"GSM2881786\"\t\"GSM2881787\"\t\"GSM2881788\"\t\"GSM2881789\"\t\"GSM2881790\"\t\"GSM2881791\"\t\"GSM2881792\"\t\"GSM2881793\"\t\"GSM2881794\"\t\"GSM2881795\"\t\"GSM2881796\"\t\"GSM2881797\"\t\"GSM2881798\"\t\"GSM2881799\"\t\"GSM2881800\"\t\"GSM2881801\"\t\"GSM2881802\"\t\"GSM2881803\"\t\"GSM2881804\"\t\"GSM2881805\"\t\"GSM2881806\"\t\"GSM2881807\"\t\"GSM2881808\"\t\"GSM2881809\"\t\"GSM2881810\"\t\"GSM2881811\"\t\"GSM2881812\"\t\"GSM2881813\"\t\"GSM2881814\"\t\"GSM2881815\"\t\"GSM2881816\"\t\"GSM2881817\"\t\"GSM2881818\"\t\"GSM2881819\"\t\"GSM2881820\"\t\"GSM2881821\"\t\"GSM2881822\"\t\"GSM2881823\"\t\"GSM2881824\"\n", "\"ILMN_1343291\"\t13.72272915\t13.05422185\t13.25687487\t14.14748155\t13.49284888\t13.91961236\t13.81999815\t13.42019634\t13.98040188\t13.79856169\t13.8729623\t13.61323199\t13.81724464\t13.26898033\t13.3560765\t13.40066261\t14.05037528\t13.35873703\t13.70821907\t13.46049348\t14.05962727\t13.8141861\t13.14279257\t13.98707325\t13.35979788\t13.49610323\t13.63279858\t12.70942817\t14.09455473\t14.60432831\t13.41371121\t13.43998783\t13.02365853\t14.34714989\t13.57191319\t13.68859022\t12.79785413\t13.67676926\t13.69594348\t13.52622908\t13.50612552\t14.01538714\t13.3604225\t13.55709919\t13.63570625\t13.90566478\t13.78049142\t13.64550993\t13.30718376\t13.89896632\t13.62467859\t14.15316229\t13.48559641\t13.85446624\t13.51060999\t13.70650384\t14.30549966\t13.36691785\t13.46982694\t13.29750319\t14.354571\t14.22239806\t13.4559637\t13.69849386\t13.58414126\t13.39104107\t13.40763\t13.04902249\t13.62243011\t13.50144738\t13.7619102\t13.27004414\t13.62340566\t13.57478046\t13.61234356\t13.38581807\t13.74845844\t13.30754702\t13.7133743\t13.54203716\t13.69775729\t12.87945179\t13.6579175\t13.98547512\t13.36501767\t13.63026083\t13.11528816\t13.06198112\t13.78199331\t13.26764184\t13.21301619\t14.27420723\t13.78352769\t13.70638513\t13.67578649\t13.656\t13.74524907\t13.09959976\t13.44964269\t13.91654858\t13.63560376\t12.91608699\t13.91558695\t13.51406794\t13.40177345\t14.19837372\t13.24527658\t12.92901526\t13.03167303\t13.60051756\t14.03070463\t13.25412266\t12.91446391\t13.49588151\t13.25687487\t13.77459179\t13.69895609\t13.76124809\t13.78626078\t13.52060635\t12.7949476\t13.27664732\t13.41244524\t13.39658492\t13.67638866\t13.1255292\t13.81123607\t13.54736129\t13.85898621\t13.8761265\t13.80053517\t13.55542812\t14.00584184\t13.62189967\t13.3956598\t13.89376802\t12.86932447\t14.20305161\t13.76880152\t13.81026221\t13.99856188\t14.24804777\t13.16024803\t13.66954979\t13.72787236\t12.89123415\t14.13827495\t12.9763\t13.2469781\t14.00009171\t13.69372417\t13.57093355\t13.20442751\t13.55029788\t13.32908264\t13.41052\t14.29412557\t13.32687224\t13.71761245\t13.65850123\t13.78252384\t13.67840661\t13.7301864\t13.06888037\t13.69993796\t13.96304855\t13.216823\t13.35171108\t13.58492286\t13.60178318\t13.32370605\t13.53888012\t13.35664366\t13.17865262\t12.89013612\t13.39381925\t14.17282603\t13.07435687\t13.38006424\t13.5121173\t13.34446646\t13.65626409\t13.76595015\t14.09051749\t13.61774158\t13.8024386\t13.5174563\t13.51276294\t13.80714295\t13.53667231\t13.51277188\t13.52347776\t13.59351021\t13.62823431\t13.79728004\n", "\"ILMN_1651209\"\t8.875330781\t9.711294752\t10.50299064\t9.510053245\t9.985847332\t9.55039093\t9.610393513\t9.832570394\t8.750646719\t9.074436816\t10.20628625\t9.723429262\t10.15885842\t9.243415958\t9.584923619\t9.652198742\t9.471786546\t8.906773139\t9.573630551\t9.648225097\t8.453319112\t10.13086751\t9.032212609\t9.161812583\t9.369968467\t9.78576668\t9.534110866\t10.88312221\t9.686601864\t10.21901611\t10.24910213\t9.355128006\t9.904104727\t9.428320065\t10.40945166\t9.335340596\t9.952272268\t9.485842683\t9.882579141\t8.935672609\t9.653745287\t10.16767664\t10.3297583\t10.41893879\t9.66693604\t9.162854997\t9.91496686\t9.741148374\t9.824054095\t9.943573329\t10.01603449\t9.184170772\t8.816436773\t9.124197744\t9.577835468\t9.223688887\t10.13728842\t9.419949778\t9.787398065\t9.549625879\t10.27677667\t10.60084888\t9.093378643\t8.702690876\t9.59968615\t9.209908464\t9.368696259\t10.34780308\t9.215795205\t9.214029364\t9.786719977\t10.64627488\t9.858138077\t9.833342728\t9.493231726\t9.832103937\t9.420479296\t9.965844352\t9.561399898\t9.74481942\t9.582787151\t9.519075785\t9.182764184\t9.695627594\t9.167386145\t9.65863608\t9.685677338\t9.611436929\t9.475424662\t10.76752211\t9.964152523\t9.846714523\t9.881522762\t9.31214428\t10.21144675\t9.523595286\t9.349860873\t9.520410284\t9.574493968\t10.16355426\t9.337745761\t9.727606427\t9.442296372\t9.908100065\t9.018258754\t9.536899527\t9.542068097\t9.39071629\t10.03603508\t10.02451133\t9.772483205\t9.150311847\t9.159762088\t9.385604365\t10.11594357\t8.912720082\t9.402730064\t10.32228943\t9.021350353\t10.23351392\t9.522755599\t9.215467362\t9.416053741\t9.808857872\t10.00024981\t10.01441237\t9.696663336\t9.962837561\t10.54563107\t10.02268768\t9.742956238\t9.890171839\t9.374884868\t9.229072471\t9.563393016\t9.645295777\t9.735880388\t9.910339757\t9.855645105\t9.943573329\t9.993119331\t9.211998372\t9.077508966\t9.823938345\t9.893111927\t9.658468772\t9.598450918\t9.529006545\t9.020285028\t10.12851212\t9.653348593\t8.069143838\t8.790278523\t9.506355516\t10.00108454\t10.15330754\t9.301063008\t9.968784914\t9.663773564\t9.325938855\t10.18066902\t9.727071824\t9.739613348\t10.13866949\t9.614501688\t9.965844352\t9.023342943\t10.55706893\t9.461205124\t9.689584718\t10.26136658\t10.08279792\t9.87826443\t9.646596792\t10.78666039\t10.07735853\t10.39647443\t9.950807605\t10.30769125\t9.48829227\t9.984738692\t9.581308742\t9.526984233\t9.329514928\t9.724672309\t9.927923639\t8.948317693\t9.102789518\t9.474646862\t8.970448368\t9.789687808\t9.457037843\t10.1359445\t9.848072883\t10.82684677\n", "\"ILMN_1651228\"\t14.50322333\t14.57107678\t14.57222643\t14.5210792\t14.39362314\t14.45614127\t14.67155152\t14.67532163\t14.62309345\t13.9987234\t14.48647416\t14.79437284\t14.03646889\t13.63793621\t14.79475917\t14.67943325\t14.83639828\t14.75152893\t14.71594551\t14.34038522\t14.02982171\t14.31271033\t14.55143145\t15.22555516\t14.34691175\t14.5244203\t14.73421993\t14.99504825\t14.58887663\t14.38559451\t14.65420146\t14.28089847\t14.26492924\t14.55627313\t14.70847051\t14.67147217\t14.52178959\t14.4251965\t14.54539827\t14.79849756\t14.79897942\t14.61450702\t14.73272372\t14.29015235\t14.23116671\t13.88058702\t14.79437284\t14.59084959\t14.39569448\t14.59676005\t14.38183491\t14.81766424\t14.15862681\t14.37946701\t14.59781668\t14.52787996\t14.40119546\t14.74182526\t14.46718468\t14.50232562\t14.83107484\t14.84835425\t13.64165988\t13.92553704\t14.53000512\t14.40977274\t14.03762157\t14.81507001\t14.21249918\t14.29686213\t14.59017794\t14.17078016\t14.80674031\t14.26071764\t14.92298935\t14.27002374\t14.54580715\t14.57530717\t14.36134442\t14.77179637\t14.59632517\t14.51514803\t14.75968691\t14.53302336\t14.19779991\t14.66424353\t14.21184592\t13.98702124\t14.30374953\t14.7740516\t14.6223071\t14.64877355\t14.49336212\t14.68796164\t14.82107941\t14.69119615\t14.18268634\t14.21249918\t14.0091062\t14.50008286\t14.55443239\t14.74387614\t14.34038522\t14.37422867\t13.99105427\t14.89570189\t15.20119903\t14.31341222\t14.48210909\t14.20771427\t14.75593462\t14.54686722\t14.63932251\t14.16755082\t14.73762855\t14.87230347\t14.46001831\t14.73000509\t14.27204459\t14.74257425\t14.16461491\t14.57745783\t14.71261402\t14.54765533\t14.27002374\t14.5322907\t14.48741124\t14.65150026\t14.89909479\t14.66327138\t14.61290871\t14.67943325\t14.64032085\t14.66776901\t14.12410919\t14.77033065\t14.21906575\t13.94061382\t14.6173967\t14.86413174\t14.71594551\t14.79259681\t14.44744033\t14.72936003\t14.53456979\t14.3466321\t14.49242348\t14.6872153\t14.40234578\t14.70059893\t14.70498161\t14.26042947\t14.90603851\t14.54539827\t14.58766472\t14.87087202\t14.48668096\t14.57745783\t14.2010665\t14.66990935\t14.30935039\t14.62088116\t14.54013203\t14.50239638\t14.29814055\t14.18817466\t13.49679905\t14.33626175\t14.84013392\t14.36134442\t14.48773875\t14.69304694\t13.8382362\t14.69304694\t14.59271644\t14.55443239\t14.56734705\t14.84595618\t14.93676875\t14.31344133\t14.51768699\t14.35866334\t14.51798316\t14.59463749\t14.95015342\t14.68363939\t13.93764328\t14.98582627\t14.08757322\t14.49434576\t14.59887302\t14.29015235\t14.42121039\t14.5942432\t14.9600748\n", "\"ILMN_1651229\"\t12.37697478\t12.55691804\t12.67240832\t12.80001831\t12.13686746\t12.15046979\t12.36643107\t12.52998555\t12.02888094\t13.10593676\t12.19778664\t12.27987735\t12.34730818\t12.62664785\t12.2192643\t12.33009668\t12.2567083\t12.33028168\t11.76736316\t12.876393\t11.95899933\t12.22726179\t12.24326184\t12.06280298\t12.06232888\t12.16269041\t12.54607994\t12.0390809\t12.87696912\t12.53238853\t12.10392531\t12.98829561\t13.0897392\t12.55092475\t12.54132783\t12.21330351\t12.26068115\t12.85742721\t12.24500294\t13.38477455\t11.5716146\t12.04842792\t12.00266604\t12.48843257\t12.12868918\t12.89491062\t11.67690662\t12.18834834\t12.34026406\t11.93756586\t12.41154228\t12.16399921\t12.09163644\t12.20976235\t11.73453631\t12.55526753\t12.74400347\t11.90857527\t12.02222593\t12.05223289\t9.860814134\t12.26390246\t13.11798986\t12.04284301\t12.5883544\t12.73448906\t12.62025028\t11.96945207\t12.45954013\t12.45255997\t12.23989494\t12.19418433\t12.50397322\t12.40322011\t11.95306139\t12.09357721\t12.64161548\t12.96470037\t12.25517322\t12.71087989\t12.57766494\t11.69491854\t12.0075054\t12.58574237\t11.69523163\t12.32655337\t11.78843304\t12.21807868\t12.066593\t11.97223152\t12.25033946\t12.09653409\t12.38171721\t12.44072842\t12.29441668\t12.02742613\t12.4474191\t12.49317718\t12.091686\t13.06295663\t12.35933422\t12.06849681\t12.0579592\t12.00375185\t11.56181549\t11.52737441\t12.53745266\t13.2567171\t12.55977374\t12.40049365\t12.35535015\t12.27977468\t12.19271378\t12.88390603\t13.46035091\t11.38006536\t12.59416758\t12.49236306\t12.17211153\t12.23729359\t12.58479249\t11.96739574\t11.92906629\t12.32839553\t12.54873879\t12.73301577\t12.33009318\t12.4941907\t12.68106951\t12.35156665\t12.66482246\t12.35668116\t12.07707871\t12.32673059\t12.39119516\t12.18019777\t12.55019607\t12.66468593\t12.83987683\t12.5620022\t11.907031\t12.80927578\t12.82901924\t12.04972715\t12.50531965\t12.653142\t12.5242462\t13.00912005\t12.51936974\t12.75089056\t13.57148304\t12.96548031\t12.64921832\t12.64193416\t12.24261812\t12.63507028\t12.98601693\t12.86924426\t12.30055934\t12.48896343\t13.04403664\t11.79988005\t12.75344171\t11.58522203\t12.25636838\t12.57666863\t12.91817697\t13.01319687\t12.03180587\t12.2766719\t13.12011758\t12.82981203\t12.80991174\t12.21316083\t12.44962987\t12.03527579\t12.5367817\t12.36673667\t12.33595169\t12.38543491\t12.54234297\t12.6271158\t12.24261812\t13.29211017\t12.3521287\t11.20032178\t12.99495019\t12.55400192\t13.10902626\t12.02691669\t12.76681074\t12.52998145\t12.1657365\t12.46839795\t12.68019587\n", "Total lines examined: 100\n", "\n", "Attempting to extract gene data from matrix file...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Successfully extracted gene data with 29377 rows\n", "First 20 gene IDs:\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", "\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", "# Add diagnostic code to check file content and structure\n", "print(\"Examining matrix file structure...\")\n", "with gzip.open(matrix_file, 'rt') as file:\n", " table_marker_found = False\n", " lines_read = 0\n", " for i, line in enumerate(file):\n", " lines_read += 1\n", " if '!series_matrix_table_begin' in line:\n", " table_marker_found = True\n", " print(f\"Found table marker at line {i}\")\n", " # Read a few lines after the marker to check data structure\n", " next_lines = [next(file, \"\").strip() for _ in range(5)]\n", " print(\"First few lines after marker:\")\n", " for next_line in next_lines:\n", " print(next_line)\n", " break\n", " if i < 10: # Print first few lines to see file structure\n", " print(f\"Line {i}: {line.strip()}\")\n", " if i > 100: # Don't read the entire file\n", " break\n", " \n", " if not table_marker_found:\n", " print(\"Table marker '!series_matrix_table_begin' not found in first 100 lines\")\n", " print(f\"Total lines examined: {lines_read}\")\n", "\n", "# 2. Try extracting gene expression data from the matrix file again with better diagnostics\n", "try:\n", " print(\"\\nAttempting to extract 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: {str(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", "\n", "# If data extraction failed, try an alternative approach using pandas directly\n", "if not is_gene_available:\n", " print(\"\\nTrying alternative approach to read gene expression data...\")\n", " try:\n", " with gzip.open(matrix_file, 'rt') as file:\n", " # Skip lines until we find the marker\n", " for line in file:\n", " if '!series_matrix_table_begin' in line:\n", " break\n", " \n", " # Try to read the data directly with pandas\n", " gene_data = pd.read_csv(file, sep='\\t', index_col=0)\n", " \n", " if not gene_data.empty:\n", " print(f\"Successfully extracted gene data with alternative method: {gene_data.shape}\")\n", " print(\"First 20 gene IDs:\")\n", " print(gene_data.index[:20])\n", " is_gene_available = True\n", " else:\n", " print(\"Alternative extraction method also produced empty data\")\n", " except Exception as e:\n", " print(f\"Alternative extraction failed: {str(e)}\")\n" ] }, { "cell_type": "markdown", "id": "e24c1898", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "c3b020cd", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:38:05.969976Z", "iopub.status.busy": "2025-03-25T07:38:05.969856Z", "iopub.status.idle": "2025-03-25T07:38:05.971747Z", "shell.execute_reply": "2025-03-25T07:38:05.971473Z" } }, "outputs": [], "source": [ "# Examining the gene identifiers from the data\n", "# The identifiers start with \"ILMN_\" followed by numbers (e.g., ILMN_1343291)\n", "# These are Illumina BeadArray probe IDs, not standard human gene symbols\n", "# They need to be mapped to official gene symbols for biological interpretation\n", "\n", "# Illumina IDs require mapping to standard gene symbols\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "d6be8b99", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "ba2d1441", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:38:05.972806Z", "iopub.status.busy": "2025-03-25T07:38:05.972707Z", "iopub.status.idle": "2025-03-25T07:38:17.577669Z", "shell.execute_reply": "2025-03-25T07:38:17.577340Z" } }, "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 5758087 rows\n", "\n", "Gene annotation preview (first few rows):\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", "Column names in gene annotation data:\n", "['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", "\n", "The dataset contains GenBank accessions (GB_ACC) that could be used for gene mapping.\n", "Number of rows with GenBank accessions: 29377 out of 5758087\n" ] } ], "source": [ "# 1. Extract gene annotation data from the SOFT file\n", "print(\"Extracting gene annotation data from SOFT file...\")\n", "try:\n", " # 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", " # Check for relevant mapping columns\n", " if 'GB_ACC' in gene_annotation.columns:\n", " print(\"\\nThe dataset contains GenBank accessions (GB_ACC) that could be used for gene mapping.\")\n", " # Count non-null values in GB_ACC column\n", " non_null_count = gene_annotation['GB_ACC'].count()\n", " print(f\"Number of rows with GenBank accessions: {non_null_count} out of {len(gene_annotation)}\")\n", " \n", " if 'SPOT_ID' in gene_annotation.columns:\n", " print(\"\\nThe dataset contains genomic regions (SPOT_ID) that could be used for location-based gene mapping.\")\n", " print(\"Example SPOT_ID format:\", gene_annotation['SPOT_ID'].iloc[0])\n", " \n", "except Exception as e:\n", " print(f\"Error processing gene annotation data: {e}\")\n", " is_gene_available = False\n" ] }, { "cell_type": "markdown", "id": "0cdabac0", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "12df2765", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:38:17.579065Z", "iopub.status.busy": "2025-03-25T07:38:17.578936Z", "iopub.status.idle": "2025-03-25T07:38:20.594704Z", "shell.execute_reply": "2025-03-25T07:38:20.594137Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created gene mapping with 29377 entries\n", "First few rows of gene mapping:\n", "{'ID': ['ILMN_3166687', 'ILMN_3165566', 'ILMN_3164811', 'ILMN_3165363', 'ILMN_3166511'], 'Gene': ['ERCC-00162', 'ERCC-00071', 'ERCC-00009', 'ERCC-00053', 'ERCC-00144']}\n", "\n", "Converting probe-level data to gene expression data...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Successfully created gene expression data with 20211 genes\n", "First few gene symbols:\n", "['A1BG', 'A1CF', 'A26C3', 'A2BP1', 'A2LD1', 'A2M', 'A2ML1', 'A3GALT2', 'A4GALT', 'A4GNT']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Lower_Grade_Glioma/gene_data/GSE107850.csv\n" ] } ], "source": [ "# 1. Identify the relevant columns for gene mapping\n", "# Looking at the gene annotation and expression data, we need:\n", "# - 'ID' column in the annotation data matches the row indices in gene expression data (ILMN_XXXXXX format)\n", "# - 'Symbol' column in the annotation data contains the gene symbols we want to map to\n", "\n", "# 2. Create a gene mapping dataframe\n", "gene_mapping = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol')\n", "print(f\"Created gene mapping with {len(gene_mapping)} entries\")\n", "print(\"First few rows of gene mapping:\")\n", "print(preview_df(gene_mapping))\n", "\n", "# 3. Apply the gene mapping to convert probe-level data to gene-level data\n", "print(\"\\nConverting probe-level data to gene expression data...\")\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "print(f\"Successfully created gene expression data with {len(gene_data.index)} genes\")\n", "print(\"First few gene symbols:\")\n", "print(gene_data.index[:10].tolist())\n", "\n", "# Save the gene data for later use\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": "634718b6", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "9bd83277", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:38:20.596625Z", "iopub.status.busy": "2025-03-25T07:38:20.596496Z", "iopub.status.idle": "2025-03-25T07:38:41.356650Z", "shell.execute_reply": "2025-03-25T07:38:41.356276Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Normalizing gene symbols using NCBI Gene database...\n", "After normalization: 19450 unique genes remain\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene expression data saved to ../../output/preprocess/Lower_Grade_Glioma/gene_data/GSE107850.csv\n", "\n", "Extracting clinical data directly from the matrix file...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Processing clinical data...\n", "Extracted clinical data with shape: (3, 195)\n", "Clinical data saved to: ../../output/preprocess/Lower_Grade_Glioma/clinical_data/GSE107850.csv\n", "\n", "Linking clinical and genetic data...\n", "Found 195 common samples between clinical and genetic data\n", "Linked data shape: (195, 19453)\n", "\n", "Handling missing values in linked data...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (195, 19453)\n", "\n", "Evaluating trait and demographic feature bias...\n", "For the feature 'Lower_Grade_Glioma', the least common label is '0.0' with 94 occurrences. This represents 48.21% of the dataset.\n", "The distribution of the feature 'Lower_Grade_Glioma' in this dataset is fine.\n", "\n", "Quartiles for 'Age':\n", " 25%: 36.16975\n", " 50% (Median): 43.2471\n", " 75%: 51.3785\n", "Min: 19.4771\n", "Max: 71.2279\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 85 occurrences. This represents 43.59% 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/Lower_Grade_Glioma/cohort_info.json\n", "\n", "Saving linked data to ../../output/preprocess/Lower_Grade_Glioma/GSE107850.csv\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved successfully!\n" ] } ], "source": [ "# 1. Normalize gene symbols using NCBI Gene database\n", "print(\"\\nNormalizing gene symbols using NCBI Gene database...\")\n", "try:\n", " gene_data_normalized = normalize_gene_symbols_in_index(gene_data)\n", " print(f\"After normalization: {len(gene_data_normalized)} unique genes remain\")\n", " gene_data_cleaned = gene_data_normalized\n", " \n", " # Save the normalized gene expression data\n", " os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", " gene_data_cleaned.to_csv(out_gene_data_file)\n", " print(f\"Normalized gene expression data saved to {out_gene_data_file}\")\n", "except Exception as e:\n", " print(f\"Error during gene symbol normalization: {str(e)}\")\n", " print(\"Falling back to original gene data\")\n", " gene_data_cleaned = gene_data.copy()\n", "\n", "# 2. We need to recreate the clinical data from the original matrix file\n", "print(\"\\nExtracting clinical data directly from the matrix file...\")\n", "# Extract clinical data from the matrix file again\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", "# Process clinical data\n", "print(\"\\nProcessing clinical data...\")\n", "# Create clinical features dataframe \n", "if trait_row is not None:\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 if age_row is not None else None,\n", " gender_row=gender_row,\n", " convert_gender=convert_gender if gender_row is not None else None\n", " )\n", " \n", " print(f\"Extracted clinical data with shape: {selected_clinical_df.shape}\")\n", " # Save 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 data saved to: {out_clinical_data_file}\")\n", " \n", " is_trait_available = True\n", "else:\n", " selected_clinical_df = pd.DataFrame()\n", " is_trait_available = False\n", " print(\"No trait data available in clinical information.\")\n", "\n", "# 3. Link clinical and genetic data\n", "if is_trait_available and is_gene_available:\n", " print(\"\\nLinking clinical and genetic data...\")\n", " try:\n", " # Ensure the sample IDs match between clinical and genetic data\n", " common_samples = list(set(selected_clinical_df.columns).intersection(set(gene_data_cleaned.columns)))\n", " \n", " if len(common_samples) == 0:\n", " print(\"Warning: No common samples between clinical and genetic data\")\n", " linked_data = pd.DataFrame()\n", " is_biased = True\n", " else:\n", " print(f\"Found {len(common_samples)} common samples between clinical and genetic data\")\n", " \n", " # Filter data to include only common samples\n", " clinical_subset = selected_clinical_df[common_samples]\n", " genetic_subset = gene_data_cleaned[common_samples]\n", " \n", " # Link the data\n", " linked_data = pd.concat([clinical_subset, genetic_subset], axis=0).T\n", " print(f\"Linked data shape: {linked_data.shape}\")\n", " \n", " # 4. Handle missing values\n", " print(\"\\nHandling missing values in linked data...\")\n", " linked_data = handle_missing_values(linked_data, trait)\n", " print(f\"Data shape after handling missing values: {linked_data.shape}\")\n", " \n", " # 5. Determine if trait and demographic features are severely biased\n", " print(\"\\nEvaluating trait and demographic feature bias...\")\n", " is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", " except Exception as e:\n", " print(f\"Error during data linking: {str(e)}\")\n", " linked_data = pd.DataFrame()\n", " is_biased = True\n", "else:\n", " print(\"\\nCannot create linked data: missing clinical or gene data\")\n", " linked_data = pd.DataFrame()\n", " is_biased = True\n", "\n", "# 6. Final validation and saving\n", "note = \"This dataset contains gene expression data from astrocytoma cell lines with modified GFAP isoform expression. The trait represents different experimental conditions related to the GFAPδ/GFAPα ratio.\"\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=is_gene_available,\n", " is_trait_available=is_trait_available,\n", " is_biased=is_biased if len(linked_data) > 0 else True,\n", " df=linked_data,\n", " note=note\n", ")\n", "\n", "# Save the linked data if it's usable\n", "if is_usable and len(linked_data) > 0:\n", " print(f\"\\nSaving linked data to {out_data_file}\")\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 successfully!\")\n", "else:\n", " print(f\"\\nDataset not usable for {trait} association studies due to bias or 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 }