{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "2c0bf6d8", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:39:26.856915Z", "iopub.status.busy": "2025-03-25T08:39:26.856809Z", "iopub.status.idle": "2025-03-25T08:39:27.026914Z", "shell.execute_reply": "2025-03-25T08:39:27.026527Z" } }, "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 = \"Duchenne_Muscular_Dystrophy\"\n", "cohort = \"GSE48828\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Duchenne_Muscular_Dystrophy\"\n", "in_cohort_dir = \"../../input/GEO/Duchenne_Muscular_Dystrophy/GSE48828\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Duchenne_Muscular_Dystrophy/GSE48828.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Duchenne_Muscular_Dystrophy/gene_data/GSE48828.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Duchenne_Muscular_Dystrophy/clinical_data/GSE48828.csv\"\n", "json_path = \"../../output/preprocess/Duchenne_Muscular_Dystrophy/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "e2dfd01b", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "b3a3b527", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:39:27.028184Z", "iopub.status.busy": "2025-03-25T08:39:27.028034Z", "iopub.status.idle": "2025-03-25T08:39:27.083218Z", "shell.execute_reply": "2025-03-25T08:39:27.082785Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Expression and Splicing Analysis of Myotonic Dystrophy and Other Dystrophic Muscle\"\n", "!Series_summary\t\"The prevailing patho-mechanistic paradigm for myotonic dystrophy (DM) is that the aberrant presence of embryonic isoforms is responsible for many, if not most, aspects of the pleiotropic disease phenotype. In order to identify such aberrantly expressed isoforms in skeletal muscle of DM type 1 (DM1) and type 2 (DM2) patients, we utilized the Affymetrix exon array to characterize the largest collection of DM samples analyzed to date, and included non-DM dystrophic muscle samples (NMD) as disease controls.\"\n", "!Series_overall_design\t\"For the exon array profiling on the Human Exon 1.0 ST array (Affymetrix Santa Clara, CA) we used a panel of 28 skeletal muscle biopsies from DM1 (n=8), DM2 (n=10), Becker muscular dystrophy, BMD, (n=3), Duchenne muscular dystrophy, DMD (n=1), Tibial muscular dystrophy, TMD, (n=2) and normal skeletal muscle (n=4). Normal control RNAs were purchased commercially.\"\n", "!Series_overall_design\t\".CEL files were generated with a pre-commercial version of the Affymetrix processing software, and the headers might be non-standard. In our lab, users of the Partek software could use them, whereas users of GeneSpring had to modify the header information.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['diagnosis: Myotonic Dystrophy Type 1', 'diagnosis: Myotonic Dystrophy Type 2', 'diagnosis: Becker Muscular Dystrophy', 'diagnosis: Duchenne Muscular Dystrophy', 'diagnosis: Tibial muscular Dystophy', 'diagnosis: Normal'], 1: ['gender: F', 'gender: M', 'gender: Not available'], 2: ['age (yrs): Not available', 'age (yrs): 54', 'age (yrs): 29', 'age (yrs): 25', 'age (yrs): 21', 'age (yrs): 55', 'age (yrs): na', 'age (yrs): 39', 'age (yrs): 58', 'age (yrs): 50', 'age (yrs): 51', 'age (yrs): 43', 'age (yrs): 37', 'age (yrs): 65', 'age (yrs): 45', 'age (yrs): 26', 'age (yrs): 20', 'age (yrs): 88', 'age (yrs): 61', 'age (yrs): 85']}\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": "4ac7f92c", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "db768e2d", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:39:27.084503Z", "iopub.status.busy": "2025-03-25T08:39:27.084388Z", "iopub.status.idle": "2025-03-25T08:39:27.089737Z", "shell.execute_reply": "2025-03-25T08:39:27.089344Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Initial validation - Gene data available: True, Trait data available: True\n" ] } ], "source": [ "import pandas as pd\n", "import os\n", "import json\n", "from typing import Callable, Optional, Dict, Any\n", "\n", "# Analyzing gene expression data availability\n", "is_gene_available = True # The dataset contains gene expression data on the Affymetrix Human Exon 1.0 ST array\n", "\n", "# Analyzing trait data availability\n", "# From the sample characteristics dictionary, key 0 contains diagnosis information including DMD\n", "trait_row = 0 # The key for trait data in sample characteristics dictionary\n", "\n", "# Age data availability\n", "age_row = 2 # The key for age data in sample characteristics dictionary\n", "\n", "# Gender data availability\n", "gender_row = 1 # The key for gender data in sample characteristics dictionary\n", "\n", "# Define conversion functions for each variable\n", "def convert_trait(value: str) -> int:\n", " \"\"\"Convert trait value to binary (0 for non-DMD, 1 for DMD).\"\"\"\n", " if value is None or pd.isna(value):\n", " return None\n", " # Extract the value after the colon\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Check if the value indicates Duchenne Muscular Dystrophy\n", " if 'Duchenne Muscular Dystrophy' in value:\n", " return 1\n", " else:\n", " return 0\n", "\n", "def convert_age(value: str) -> Optional[float]:\n", " \"\"\"Convert age value to continuous numeric value.\"\"\"\n", " if value is None or pd.isna(value):\n", " return None\n", " \n", " # Extract the value after the colon\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Handle various age formats and missing values\n", " if value in ['Not available', 'na', '']:\n", " return None\n", " \n", " try:\n", " return float(value)\n", " except ValueError:\n", " return None\n", "\n", "def convert_gender(value: str) -> Optional[int]:\n", " \"\"\"Convert gender value to binary (0 for female, 1 for male).\"\"\"\n", " if value is None or pd.isna(value):\n", " return None\n", " \n", " # Extract the value after the colon\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Convert gender values\n", " if value.upper() in ['F', 'FEMALE']:\n", " return 0\n", " elif value.upper() in ['M', 'MALE']:\n", " return 1\n", " else: # 'Not available' or other values\n", " return None\n", "\n", "# Validate and save cohort info (initial filtering)\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", "# Print a message about the results of the initial validation\n", "print(f\"Initial validation - Gene data available: {is_gene_available}, Trait data available: {is_trait_available}\")\n" ] }, { "cell_type": "markdown", "id": "5805b1c2", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "8cb6248e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:39:27.090947Z", "iopub.status.busy": "2025-03-25T08:39:27.090837Z", "iopub.status.idle": "2025-03-25T08:39:27.146350Z", "shell.execute_reply": "2025-03-25T08:39:27.145922Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix table marker not found in first 100 lines\n", "Index(['2315554', '2315633', '2315674', '2315739', '2315894', '2315918',\n", " '2315951', '2316218', '2316245', '2316379', '2316558', '2316605',\n", " '2316746', '2316905', '2316953', '2317246', '2317317', '2317434',\n", " '2317472', '2317512'],\n", " dtype='object', name='ID')\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. First, let's examine the structure of the matrix file to understand its format\n", "import gzip\n", "\n", "# Peek at the first few lines of the file to understand its structure\n", "with gzip.open(matrix_file, 'rt') as file:\n", " # Read first 100 lines to find the header structure\n", " for i, line in enumerate(file):\n", " if '!series_matrix_table_begin' in line:\n", " print(f\"Found data marker at line {i}\")\n", " # Read the next line which should be the header\n", " header_line = next(file)\n", " print(f\"Header line: {header_line.strip()}\")\n", " # And the first data line\n", " first_data_line = next(file)\n", " print(f\"First data line: {first_data_line.strip()}\")\n", " break\n", " if i > 100: # Limit search to first 100 lines\n", " print(\"Matrix table marker not found in first 100 lines\")\n", " break\n", "\n", "# 3. Now try to get the genetic data with better error handling\n", "try:\n", " gene_data = get_genetic_data(matrix_file)\n", " print(gene_data.index[:20])\n", "except KeyError as e:\n", " print(f\"KeyError: {e}\")\n", " \n", " # Alternative approach: manually extract the data\n", " print(\"\\nTrying alternative approach to read the gene data:\")\n", " with gzip.open(matrix_file, 'rt') as file:\n", " # Find the start of the data\n", " for line in file:\n", " if '!series_matrix_table_begin' in line:\n", " break\n", " \n", " # Read the headers and data\n", " import pandas as pd\n", " df = pd.read_csv(file, sep='\\t', index_col=0)\n", " print(f\"Column names: {df.columns[:5]}\")\n", " print(f\"First 20 row IDs: {df.index[:20]}\")\n", " gene_data = df\n" ] }, { "cell_type": "markdown", "id": "111d0575", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "b9dd858a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:39:27.147724Z", "iopub.status.busy": "2025-03-25T08:39:27.147610Z", "iopub.status.idle": "2025-03-25T08:39:27.149687Z", "shell.execute_reply": "2025-03-25T08:39:27.149303Z" } }, "outputs": [], "source": [ "# These identifiers look like probe IDs from a microarray platform rather than human gene symbols\n", "# They are numeric IDs that need to be mapped to gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "6f41c6ec", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "fa8cc5b1", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:39:27.150865Z", "iopub.status.busy": "2025-03-25T08:39:27.150759Z", "iopub.status.idle": "2025-03-25T08:39:40.332509Z", "shell.execute_reply": "2025-03-25T08:39:40.331827Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['2315100', '2315106', '2315109', '2315111', '2315113'], 'GB_LIST': ['NR_024005,NR_034090,NR_024004,AK093685', 'DQ786314', nan, nan, 'DQ786265'], 'SPOT_ID': ['chr1:11884-14409', 'chr1:14760-15198', 'chr1:19408-19712', 'chr1:25142-25532', 'chr1:27563-27813'], 'seqname': ['chr1', 'chr1', 'chr1', 'chr1', 'chr1'], 'RANGE_GB': ['NC_000001.10', 'NC_000001.10', 'NC_000001.10', 'NC_000001.10', 'NC_000001.10'], 'RANGE_STRAND': ['+', '+', '+', '+', '+'], 'RANGE_START': ['11884', '14760', '19408', '25142', '27563'], 'RANGE_STOP': ['14409', '15198', '19712', '25532', '27813'], 'total_probes': ['20', '8', '4', '4', '4'], 'gene_assignment': ['NR_024005 // DDX11L2 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 // 2q13 // 84771 /// NR_034090 // DDX11L9 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 9 // 15q26.3 // 100288486 /// NR_024004 // DDX11L2 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 // 2q13 // 84771 /// AK093685 // DDX11L2 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 // 2q13 // 84771', '---', '---', '---', '---'], 'mrna_assignment': ['NR_024005 // RefSeq // Homo sapiens DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 (DDX11L2), transcript variant 2, non-coding RNA. // chr1 // 100 // 80 // 16 // 16 // 0 /// NR_034090 // RefSeq // Homo sapiens DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 9 (DDX11L9), non-coding RNA. // chr1 // 100 // 80 // 16 // 16 // 0 /// NR_024004 // RefSeq // Homo sapiens DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 (DDX11L2), transcript variant 1, non-coding RNA. // chr1 // 100 // 75 // 15 // 15 // 0 /// AK093685 // GenBank // Homo sapiens cDNA FLJ36366 fis, clone THYMU2007824. // chr1 // 94 // 80 // 15 // 16 // 0 /// ENST00000513886 // ENSEMBL // cdna:known chromosome:GRCh37:16:61555:64090:1 gene:ENSG00000233614 // chr1 // 100 // 80 // 16 // 16 // 0 /// ENST00000456328 // ENSEMBL // cdna:known chromosome:GRCh37:1:11869:14409:1 gene:ENSG00000223972 // chr1 // 100 // 80 // 16 // 16 // 0 /// ENST00000518655 // ENSEMBL // cdna:known chromosome:GRCh37:1:11869:14409:1 gene:ENSG00000253101 // chr1 // 100 // 80 // 16 // 16 // 0', 'DQ786314 // GenBank // Homo sapiens clone HLS_IMAGE_811138 mRNA sequence. // chr1 // 100 // 38 // 3 // 3 // 0', '---', '---', 'DQ786265 // GenBank // Homo sapiens clone HLS_IMAGE_298685 mRNA sequence. // chr1 // 100 // 100 // 4 // 4 // 0'], 'category': ['main', 'main', '---', '---', 'main']}\n" ] } ], "source": [ "# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n", "gene_annotation = get_gene_annotation(soft_file)\n", "\n", "# 2. Use the 'preview_df' function from the library to preview the data and print out the results.\n", "print(\"Gene annotation preview:\")\n", "print(preview_df(gene_annotation))\n" ] }, { "cell_type": "markdown", "id": "3cdee796", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "ce2c07e1", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:39:40.334529Z", "iopub.status.busy": "2025-03-25T08:39:40.334364Z", "iopub.status.idle": "2025-03-25T08:39:46.035850Z", "shell.execute_reply": "2025-03-25T08:39:46.035209Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sample gene_assignment values:\n", "Example 1: NR_024005 // DDX11L2 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 // 2q13 // 84771 /// NR_034090 // DDX11L9 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 9 // 15q26.3 // 100288486 /// NR_024004 // DDX11L2 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 // 2q13 // 84771 /// AK093685 // DDX11L2 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 // 2q13 // 84771\n", "Example 2: NM_001005240 // OR4F17 // olfactory receptor, family 4, subfamily F, member 17 // 19p13.3 // 81099 /// NM_001004195 // OR4F4 // olfactory receptor, family 4, subfamily F, member 4 // 15q26.3 // 26682 /// NM_001005484 // OR4F5 // olfactory receptor, family 4, subfamily F, member 5 // 1p36.33 // 79501 /// ENST00000318050 // OR4F17 // olfactory receptor, family 4, subfamily F, member 17 // 19p13.3 // 81099 /// ENST00000335137 // OR4F4 // olfactory receptor, family 4, subfamily F, member 4 // 15q26.3 // 26682 /// ENST00000326183 // OR4F5 // olfactory receptor, family 4, subfamily F, member 5 // 1p36.33 // 79501 /// BC136848 // OR4F17 // olfactory receptor, family 4, subfamily F, member 17 // 19p13.3 // 81099 /// BC136907 // OR4F4 // olfactory receptor, family 4, subfamily F, member 4 // 15q26.3 // 26682 /// ENST00000442916 // OR4F17 // olfactory receptor, family 4, subfamily F, member 17 // 19p13.3 // 81099\n", "Example 3: XM_002343043 // LOC100288692 // protein capicua homolog // 11p15.5 // 100288692 /// XM_002344123 // LOC100289383 // protein capicua homolog // 16q24.3 // 100289383 /// XM_003119218 // LOC100506283 // protein capicua homolog // --- // 100506283\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Improved gene mapping preview (first 10 rows):\n", "{'ID': ['2315100', '2315100', '2315100', '2315100', '2315125'], 'Gene': ['DDX11L2', 'DDX11L9', 'DDX11L2', 'DDX11L2', 'OR4F17']}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene expression data after mapping (first 5 genes):\n", "{'GSM1185313': [26.80524, 6.70131, 52.586949999999995, 32.1961, 36.43848], 'GSM1185314': [25.04744, 6.26186, 48.33504, 30.528550000000003, 39.52976], 'GSM1185315': [26.10308, 6.52577, 50.88642, 30.96505, 38.271], 'GSM1185316': [23.97268, 5.99317, 50.92048, 30.78035, 36.27584], 'GSM1185317': [24.65856, 6.16464, 48.13991, 30.1939, 39.51208], 'GSM1185318': [28.12448, 7.03112, 59.186659999999996, 34.2899, 35.71308], 'GSM1185319': [25.6806, 6.42015, 47.675290000000004, 31.274, 37.94328], 'GSM1185320': [27.04864, 6.76216, 55.54588, 32.52645, 37.22852], 'GSM1185321': [25.77264, 6.44316, 52.70629, 31.888299999999997, 37.9546], 'GSM1185322': [24.53292, 6.13323, 53.968070000000004, 30.308500000000002, 37.40376], 'GSM1185323': [27.44768, 6.86192, 59.14688, 32.926950000000005, 36.83836], 'GSM1185324': [26.79176, 6.69794, 51.47896, 31.3275, 36.5818], 'GSM1185325': [24.84228, 6.21057, 49.93586, 30.87975, 38.5736], 'GSM1185326': [24.4734, 6.11835, 54.22378, 30.18575, 37.54584], 'GSM1185327': [26.43268, 6.60817, 51.92304, 31.44365, 37.89124], 'GSM1185328': [25.79248, 6.44812, 51.58036, 29.616600000000002, 37.10952], 'GSM1185329': [25.88972, 6.47243, 51.245090000000005, 30.41995, 36.47536], 'GSM1185330': [26.8522, 6.71305, 53.04598000000001, 32.71275, 37.38896], 'GSM1185331': [25.21068, 6.30267, 53.36552, 29.0584, 39.19372], 'GSM1185332': [25.97012, 6.49253, 50.53191, 30.44225, 38.10404], 'GSM1185333': [25.59276, 6.39819, 45.609590000000004, 30.179299999999998, 39.63564], 'GSM1185334': [25.8804, 6.4701, 48.59712, 31.408450000000002, 39.74452], 'GSM1185335': [25.38072, 6.34518, 51.70178, 31.74505, 40.9816], 'GSM1185336': [25.46992, 6.36748, 52.0143, 31.0618, 37.29164], 'GSM1185337': [25.73028, 6.43257, 51.143299999999996, 30.95955, 37.27044], 'GSM1185338': [27.33548, 6.83387, 53.07744, 30.9497, 36.13148], 'GSM1185339': [25.54328, 6.38582, 48.92095, 30.3493, 38.3254], 'GSM1185340': [26.67252, 6.66813, 53.52555, 31.7746, 35.7304]}\n", "\n", "Dimensions of gene expression data: (18609, 28)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Duchenne_Muscular_Dystrophy/gene_data/GSE48828.csv\n" ] } ], "source": [ "# 1. Identify the columns for mapping\n", "# The 'ID' column in gene_annotation contains the same identifiers as in gene_data (probe IDs)\n", "# The 'gene_assignment' column contains gene symbol information, but in a complex format\n", "\n", "# Let's examine the structure of the gene_assignment field more closely\n", "print(\"Sample gene_assignment values:\")\n", "non_empty_assignments = gene_annotation['gene_assignment'].dropna().replace('---', None).dropna().head(3)\n", "for idx, assignment in enumerate(non_empty_assignments):\n", " print(f\"Example {idx+1}: {assignment}\")\n", "\n", "# 2. Define a more specific extraction function for this dataset\n", "def extract_gene_symbols_from_assignment(assignment_text):\n", " \"\"\"Extract gene symbols from complex gene_assignment text format.\"\"\"\n", " if assignment_text is None or pd.isna(assignment_text) or assignment_text == '---':\n", " return []\n", " \n", " # Split by /// to get separate gene entries\n", " gene_entries = assignment_text.split('///')\n", " symbols = []\n", " \n", " for entry in gene_entries:\n", " # Split each entry by // and extract the second element (gene symbol)\n", " parts = entry.strip().split('//')\n", " if len(parts) >= 2:\n", " symbol = parts[1].strip()\n", " if symbol and symbol != '---':\n", " symbols.append(symbol)\n", " \n", " return symbols\n", "\n", "# 3. Create a modified version of the mapping dataframe with extracted gene symbols\n", "mapping_df = gene_annotation[['ID', 'gene_assignment']].copy()\n", "mapping_df['Gene'] = mapping_df['gene_assignment'].apply(extract_gene_symbols_from_assignment)\n", "mapping_df = mapping_df.explode('Gene').dropna(subset=['Gene'])\n", "mapping_df = mapping_df[['ID', 'Gene']]\n", "\n", "# Check the resulting mapping dataframe\n", "print(\"\\nImproved gene mapping preview (first 10 rows):\")\n", "print(preview_df(mapping_df.head(10)))\n", "\n", "# 4. Apply the gene mapping to convert probe-level measurements to gene expression data\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "\n", "# Check the results - print the first few gene symbols and their expression values\n", "print(\"\\nGene expression data after mapping (first 5 genes):\")\n", "print(preview_df(gene_data.head(5)))\n", "\n", "# Check dimensions of the resulting dataframe\n", "print(f\"\\nDimensions of gene expression data: {gene_data.shape}\")\n", "\n", "# Save the gene expression data for future 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" ] } ], "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 }