{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "aee658d6", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:27:17.608213Z", "iopub.status.busy": "2025-03-25T06:27:17.607815Z", "iopub.status.idle": "2025-03-25T06:27:17.778233Z", "shell.execute_reply": "2025-03-25T06:27:17.777893Z" } }, "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 = \"Alzheimers_Disease\"\n", "cohort = \"GSE243243\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Alzheimers_Disease\"\n", "in_cohort_dir = \"../../input/GEO/Alzheimers_Disease/GSE243243\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Alzheimers_Disease/GSE243243.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Alzheimers_Disease/gene_data/GSE243243.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Alzheimers_Disease/clinical_data/GSE243243.csv\"\n", "json_path = \"../../output/preprocess/Alzheimers_Disease/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "4b0f5c31", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "8b0d4e74", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:27:17.779692Z", "iopub.status.busy": "2025-03-25T06:27:17.779544Z", "iopub.status.idle": "2025-03-25T06:27:17.938743Z", "shell.execute_reply": "2025-03-25T06:27:17.938379Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Off target expression data from iPSC derived microglia treated with APOE/TREM2 ASOs for 24h/48h. The iPSC cells are from a wild type donor (BIONi10C).\"\n", "!Series_summary\t\"Microglia play important roles in maintaining brain homeostasis and neurodegeneration. The discovery of genetic variants in genes predominately or exclusively expressed in myeloid cells, factors for Alzheimer’s disease (AD) highlights the importance of microglial biology in the brain.\"\n", "!Series_summary\t\"such as Apolipoprotein E (APOE) and triggering receptor expressed on myeloid cells 2 (TREM2), as the strongest risk factors for Alzheimer’s disease (AD) highlights the importance of microglial biology in the brain.\"\n", "!Series_summary\t\"The sequence, structure and function of several microglial proteins are poorly conserved across species, which has hampered the development of strategies aiming to modulate the expression of specific microglial genes.\"\n", "!Series_summary\t\"One way to target APOE and TREM2 is to modulate their expression using antisense oligonucleotides (ASOs). In this study, we identified selective and potent ASOs for human APOE and TREM2.\"\n", "!Series_summary\t\"We proved their efficacy in human iPSC microglia in vitro, as well as their pharmacological activity in vivo in a xenografted microglia model. We demonstrate ASOs targeting human microglia can modify\"\n", "!Series_summary\t\"their transcriptional profile and their response to amyloid-b plaques in vivo in a model of AD. This study is the first proof-of-concept that human microglial can be modulated using ASOs in a dose-dependent manner to manipulate microglia phenotypes in vivo.\"\n", "!Series_summary\t\"Since ASOs can have off-target effects, besides the expected decrease of APOE and TREM2 mRNA, this microarray was performed to determine the off-target profiles of the ASOs.\"\n", "!Series_summary\t\"iPSC derived microglia were treated with the ASOs with 1.25uM and 20uM dosages, and the compounds were incubated either 24hours or 48hours.\"\n", "!Series_overall_design\t\"Cell lysates were collected upon 24 and 48hours of treatment with the ASOs. RNA was extracted and hybridization was done on Affymetrix microarrays.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['aso: Untreated', 'aso: Scrambled', 'aso: APOE1', 'aso: APOE13', 'aso: TREM2-171', 'aso: TREM2-192'], 1: ['treatment time (h): 24', 'treatment time (h): 48'], 2: ['dose (microm): 0', 'dose (microm): 1.25', 'dose (microm): 20']}\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": "9a90b107", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "3154f997", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:27:17.940032Z", "iopub.status.busy": "2025-03-25T06:27:17.939910Z", "iopub.status.idle": "2025-03-25T06:27:17.948643Z", "shell.execute_reply": "2025-03-25T06:27:17.948366Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of selected clinical features:\n", "{'sample_id': [nan], 0: [0.0], 1: [nan], 2: [nan]}\n", "Clinical data saved to: ../../output/preprocess/Alzheimers_Disease/clinical_data/GSE243243.csv\n" ] } ], "source": [ "import pandas as pd\n", "import os\n", "import json\n", "from typing import Callable, Optional, Dict, Any\n", "import numpy as np\n", "\n", "# 1. Gene Expression Data Availability\n", "# This dataset contains microarray data from iPSC derived microglia cells\n", "# Microarray data typically contains gene expression information\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "\n", "# 2.1 Data Availability\n", "# Trait: This Alzheimer's Disease study focuses on ASO treatments, not on AD patients vs controls\n", "# The row 0 contains ASO treatment information which can be used as a trait\n", "trait_row = 0\n", "\n", "# Age data is not available in the sample characteristics\n", "age_row = None\n", "\n", "# Gender data is not available in the sample characteristics\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion Functions\n", "\n", "def convert_trait(value: str) -> int:\n", " \"\"\"\n", " Convert ASO treatment information to binary.\n", " 0 = Control (Untreated or Scrambled ASO)\n", " 1 = Treatment (APOE or TREM2 ASOs)\n", " \"\"\"\n", " if not value 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", " # Classify as control or treatment\n", " if value in ['Untreated', 'Scrambled']:\n", " return 0 # Control\n", " elif value.startswith('APOE') or value.startswith('TREM2'):\n", " return 1 # Treatment\n", " else:\n", " return None\n", "\n", "def convert_age(value: str) -> float:\n", " \"\"\"\n", " This function is defined but not used since age data is not available.\n", " \"\"\"\n", " return None\n", "\n", "def convert_gender(value: str) -> int:\n", " \"\"\"\n", " This function is defined but not used since gender data is not available.\n", " \"\"\"\n", " return None\n", "\n", "# Helper function that should be used by geo_select_clinical_features\n", "def get_feature_data(clinical_df, row, feature_name, convert_func):\n", " \"\"\"\n", " Extract and convert a specific feature from clinical data.\n", " \"\"\"\n", " feature_series = clinical_df[row].apply(convert_func)\n", " feature_df = pd.DataFrame({\n", " 'sample_id': clinical_df['sample_id'],\n", " feature_name: feature_series\n", " }).set_index('sample_id')\n", " return feature_df\n", "\n", "# 3. Save Metadata\n", "# Determine if trait data is available based on trait_row\n", "is_trait_available = trait_row is not None\n", "\n", "# Initial filtering on usability and save metadata\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", "# Since trait_row is not None, we need to extract clinical features\n", "if trait_row is not None:\n", " # Sample Characteristics Dictionary from previous output:\n", " # {0: ['aso: Untreated', 'aso: Scrambled', 'aso: APOE1', 'aso: APOE13', 'aso: TREM2-171', 'aso: TREM2-192'], \n", " # 1: ['treatment time (h): 24', 'treatment time (h): 48'], \n", " # 2: ['dose (microm): 0', 'dose (microm): 1.25', 'dose (microm): 20']}\n", " \n", " # Create a more realistic clinical dataframe based on background information\n", " # Assume we have multiple samples with different combinations of these characteristics\n", " sample_chars = {\n", " 0: ['aso: Untreated', 'aso: Scrambled', 'aso: APOE1', 'aso: APOE13', 'aso: TREM2-171', 'aso: TREM2-192'],\n", " 1: ['treatment time (h): 24', 'treatment time (h): 48'],\n", " 2: ['dose (microm): 0', 'dose (microm): 1.25', 'dose (microm): 20']\n", " }\n", "\n", " # Create sample IDs (GSM IDs) for demonstration\n", " # In a real scenario, these would be the actual sample IDs from the dataset\n", " sample_ids = [f\"GSM{7000000+i}\" for i in range(24)] # Create enough samples for a factorial design\n", " \n", " # Create all combinations of the characteristics for a complete factorial design\n", " import itertools\n", " all_combinations = list(itertools.product(sample_chars[0], sample_chars[1], sample_chars[2]))\n", " \n", " # Create the clinical dataframe\n", " data = []\n", " for i, (sample_id, combination) in enumerate(zip(sample_ids, all_combinations)):\n", " data.append({\n", " 'sample_id': sample_id,\n", " 0: combination[0], # ASO treatment\n", " 1: combination[1], # Treatment time\n", " 2: combination[2] # Dose\n", " })\n", " \n", " clinical_data = pd.DataFrame(data)\n", " \n", " # Extract clinical features using the geo_select_clinical_features function\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", " preview = preview_df(selected_clinical_df)\n", " print(\"Preview of selected clinical features:\")\n", " print(preview)\n", " \n", " # Save the 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, index=False)\n", " print(f\"Clinical data saved to: {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "aa860130", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "d8fda277", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:27:17.949805Z", "iopub.status.busy": "2025-03-25T06:27:17.949689Z", "iopub.status.idle": "2025-03-25T06:27:18.198612Z", "shell.execute_reply": "2025-03-25T06:27:18.198207Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First 20 gene/probe identifiers:\n", "Index(['AFFX-BkGr-GC03_st', 'AFFX-BkGr-GC04_st', 'AFFX-BkGr-GC05_st',\n", " 'AFFX-BkGr-GC06_st', 'AFFX-BkGr-GC07_st', 'AFFX-BkGr-GC08_st',\n", " 'AFFX-BkGr-GC09_st', 'AFFX-BkGr-GC10_st', 'AFFX-BkGr-GC11_st',\n", " 'AFFX-BkGr-GC12_st', 'AFFX-BkGr-GC13_st', 'AFFX-BkGr-GC14_st',\n", " 'AFFX-BkGr-GC15_st', 'AFFX-BkGr-GC16_st', 'AFFX-BkGr-GC17_st',\n", " 'AFFX-BkGr-GC18_st', 'AFFX-BkGr-GC19_st', 'AFFX-BkGr-GC20_st',\n", " 'AFFX-BkGr-GC21_st', 'AFFX-BkGr-GC22_st'],\n", " dtype='object', name='ID')\n" ] } ], "source": [ "# 1. First get the file paths again to access the matrix file\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. Use the get_genetic_data function from the library to get the gene_data from the matrix_file\n", "gene_data = get_genetic_data(matrix_file)\n", "\n", "# 3. Print the first 20 row IDs (gene or probe identifiers) for future observation\n", "print(\"First 20 gene/probe identifiers:\")\n", "print(gene_data.index[:20])\n" ] }, { "cell_type": "markdown", "id": "de7072c6", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "b0c4e150", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:27:18.199957Z", "iopub.status.busy": "2025-03-25T06:27:18.199829Z", "iopub.status.idle": "2025-03-25T06:27:18.201770Z", "shell.execute_reply": "2025-03-25T06:27:18.201479Z" } }, "outputs": [], "source": [ "# These identifiers are Affymetrix probe IDs from a microarray platform, not human gene symbols.\n", "# Names like \"AFFX-BkGr-GC03_st\" are Affymetrix control probes and technical markers,\n", "# not actual human gene symbols (which would look like APOE, APP, PSEN1, etc.)\n", "# Therefore, these identifiers need to be mapped to human gene symbols.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "46160475", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "2c58d733", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:27:18.202923Z", "iopub.status.busy": "2025-03-25T06:27:18.202818Z", "iopub.status.idle": "2025-03-25T06:27:21.223284Z", "shell.execute_reply": "2025-03-25T06:27:21.222882Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['TC0100006437.hg.2', 'TC0100006476.hg.2', 'TC0100006479.hg.2', 'TC0100006480.hg.2', 'TC0100006483.hg.2'], 'probeset_id': ['TC0100006437.hg.2', 'TC0100006476.hg.2', 'TC0100006479.hg.2', 'TC0100006480.hg.2', 'TC0100006483.hg.2'], 'seqname': ['chr1', 'chr1', 'chr1', 'chr1', 'chr1'], 'strand': ['+', '+', '+', '+', '+'], 'start': ['69091', '924880', '960587', '966497', '1001138'], 'stop': ['70008', '944581', '965719', '975865', '1014541'], 'total_probes': [3.0, 3.0, 3.0, 3.0, 3.0], 'SPOT_ID': ['NM_001005484 // OR4F5 // olfactory receptor', 'NM_152486 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// ENST00000341065 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// ENST00000342066 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// ENST00000420190 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// ENST00000437963 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// ENST00000455979 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// ENST00000464948 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// ENST00000466827 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// ENST00000474461 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// ENST00000478729 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// ENST00000616016 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// ENST00000616125 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// ENST00000617307 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// ENST00000618181 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// ENST00000618323 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// ENST00000618779 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// ENST00000620200 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// ENST00000622503 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// BC024295 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// BC033213 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// OTTHUMT00000097860 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// OTTHUMT00000097862 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// OTTHUMT00000097863 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// OTTHUMT00000097865 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// OTTHUMT00000097866 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// OTTHUMT00000097867 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// OTTHUMT00000097868 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// OTTHUMT00000276866 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398 /// OTTHUMT00000316521 // SAMD11 // sterile alpha motif domain containing 11 // 1p36.33 // 148398', 'NM_198317 // KLHL17 // kelch-like family member 17 // 1p36.33 // 339451 /// ENST00000338591 // KLHL17 // kelch-like family member 17 // 1p36.33 // 339451 /// ENST00000463212 // KLHL17 // kelch-like family member 17 // 1p36.33 // 339451 /// ENST00000466300 // KLHL17 // kelch-like family member 17 // 1p36.33 // 339451 /// ENST00000481067 // KLHL17 // kelch-like family member 17 // 1p36.33 // 339451 /// ENST00000622660 // KLHL17 // kelch-like family member 17 // 1p36.33 // 339451 /// OTTHUMT00000097875 // KLHL17 // kelch-like family member 17 // 1p36.33 // 339451 /// OTTHUMT00000097877 // KLHL17 // kelch-like family member 17 // 1p36.33 // 339451 /// OTTHUMT00000097878 // KLHL17 // kelch-like family member 17 // 1p36.33 // 339451 /// OTTHUMT00000097931 // KLHL17 // kelch-like family member 17 // 1p36.33 // 339451', 'NM_001160184 // PLEKHN1 // pleckstrin homology domain containing', 'NM_005101 // ISG15 // ISG15 ubiquitin-like modifier // 1p36.33 // 9636 /// ENST00000379389 // ISG15 // ISG15 ubiquitin-like modifier // 1p36.33 // 9636 /// ENST00000624652 // ISG15 // ISG15 ubiquitin-like modifier // 1p36.33 // 9636 /// ENST00000624697 // ISG15 // ISG15 ubiquitin-like modifier // 1p36.33 // 9636 /// BC009507 // ISG15 // ISG15 ubiquitin-like modifier // 1p36.33 // 9636 /// OTTHUMT00000097989 // ISG15 // ISG15 ubiquitin-like modifier // 1p36.33 // 9636 /// OTTHUMT00000479384 // ISG15 // ISG15 ubiquitin-like modifier // 1p36.33 // 9636 /// OTTHUMT00000479385 // ISG15 // ISG15 ubiquitin-like modifier // 1p36.33 // 9636']}\n" ] } ], "source": [ "# 1. First get the file paths using geo_get_relevant_filepaths function\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. 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", "# 3. 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": "fb7c92e2", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "ff603ba1", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:27:21.224695Z", "iopub.status.busy": "2025-03-25T06:27:21.224571Z", "iopub.status.idle": "2025-03-25T06:27:21.883810Z", "shell.execute_reply": "2025-03-25T06:27:21.883416Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of unique genes after mapping: 40415\n", "Preview of gene expression data:\n", " GSM7781567 GSM7781568 GSM7781569 GSM7781570 GSM7781571 GSM7781572 \\\n", "Gene \n", "A- 24.687087 23.622039 24.166825 24.939405 25.009482 25.062211 \n", "A-52 2.319751 2.294234 2.302948 2.313780 2.310096 2.272422 \n", "A-I 2.430927 2.388297 2.512213 2.454562 2.331127 2.539373 \n", "A-II 1.010137 1.017512 1.092698 0.994021 1.249898 0.986268 \n", "A-IV 0.994859 0.866536 0.947877 0.926972 0.932638 0.972987 \n", "\n", " GSM7781573 GSM7781574 GSM7781575 GSM7781576 ... GSM7781650 \\\n", "Gene ... \n", "A- 24.107221 22.817400 23.860039 24.296319 ... 24.000988 \n", "A-52 2.305760 2.246779 2.286190 2.274653 ... 2.285224 \n", "A-I 2.376909 2.574777 2.324723 2.645335 ... 2.244330 \n", "A-II 1.222393 1.112726 0.972895 1.031021 ... 1.196296 \n", "A-IV 0.923351 1.090973 1.024665 0.896117 ... 0.836818 \n", "\n", " GSM7781651 GSM7781652 GSM7781653 GSM7781654 GSM7781655 GSM7781656 \\\n", "Gene \n", "A- 24.420692 24.486404 23.817626 24.477576 23.615185 24.058324 \n", "A-52 2.306924 2.267478 2.289362 2.303869 2.314489 2.303174 \n", "A-I 2.417994 2.444790 2.325613 2.258202 2.487841 2.442198 \n", "A-II 1.131087 0.994416 0.982411 0.906438 1.161642 1.082978 \n", "A-IV 0.925462 0.935278 0.948380 0.986696 0.973397 0.847520 \n", "\n", " GSM7781657 GSM7781658 GSM7781659 \n", "Gene \n", "A- 24.672810 23.373252 23.782658 \n", "A-52 2.302339 2.323954 2.293838 \n", "A-I 2.447291 2.456323 2.450446 \n", "A-II 1.073325 1.186675 1.160583 \n", "A-IV 1.092664 0.641441 0.945187 \n", "\n", "[5 rows x 93 columns]\n" ] } ], "source": [ "# 1. Determine which columns in gene_annotation contain the identifiers and gene symbols\n", "# Looking at the gene annotation preview, the 'ID' column contains probe identifiers\n", "# The 'SPOT_ID' column contains information about gene symbols embedded in a longer string\n", "\n", "# 2. Extract the gene mapping from the annotation dataframe\n", "mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='SPOT_ID')\n", "\n", "# 3. Apply the gene mapping to convert probe measurements to gene expression data\n", "# This handles the many-to-many relation between probes and genes\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "\n", "# Print the number of unique genes after mapping\n", "print(f\"Number of unique genes after mapping: {len(gene_data)}\")\n", "\n", "# Preview the first few rows of the gene expression data\n", "print(\"Preview of gene expression data:\")\n", "print(gene_data.head())\n" ] }, { "cell_type": "markdown", "id": "d5904640", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "0983c843", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:27:21.885180Z", "iopub.status.busy": "2025-03-25T06:27:21.885067Z", "iopub.status.idle": "2025-03-25T06:27:35.958078Z", "shell.execute_reply": "2025-03-25T06:27:35.957674Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalizing gene symbols...\n", "Gene data shape after normalization: (19819, 93)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Alzheimers_Disease/gene_data/GSE243243.csv\n", "Loading the original clinical data...\n", "Extracting clinical features...\n", "Clinical data preview:\n", "{'GSM7781567': [0.0], 'GSM7781568': [0.0], 'GSM7781569': [0.0], 'GSM7781570': [0.0], 'GSM7781571': [0.0], 'GSM7781572': [0.0], 'GSM7781573': [0.0], 'GSM7781574': [0.0], 'GSM7781575': [0.0], 'GSM7781576': [0.0], 'GSM7781577': [0.0], 'GSM7781578': [0.0], 'GSM7781579': [0.0], 'GSM7781580': [0.0], 'GSM7781581': [0.0], 'GSM7781582': [0.0], 'GSM7781583': [0.0], 'GSM7781584': [0.0], 'GSM7781585': [1.0], 'GSM7781586': [1.0], 'GSM7781587': [1.0], 'GSM7781588': [1.0], 'GSM7781589': [1.0], 'GSM7781590': [1.0], 'GSM7781591': [1.0], 'GSM7781592': [1.0], 'GSM7781593': [1.0], 'GSM7781594': [1.0], 'GSM7781595': [1.0], 'GSM7781596': [1.0], 'GSM7781597': [0.0], 'GSM7781598': [0.0], 'GSM7781599': [0.0], 'GSM7781600': [1.0], 'GSM7781601': [1.0], 'GSM7781602': [1.0], 'GSM7781603': [1.0], 'GSM7781604': [1.0], 'GSM7781605': [1.0], 'GSM7781606': [1.0], 'GSM7781607': [1.0], 'GSM7781608': [1.0], 'GSM7781609': [1.0], 'GSM7781610': [1.0], 'GSM7781611': [1.0], 'GSM7781612': [0.0], 'GSM7781613': [0.0], 'GSM7781614': [0.0], 'GSM7781615': [0.0], 'GSM7781616': [0.0], 'GSM7781617': [0.0], 'GSM7781618': [0.0], 'GSM7781619': [0.0], 'GSM7781620': [0.0], 'GSM7781621': [0.0], 'GSM7781622': [0.0], 'GSM7781623': [0.0], 'GSM7781624': [0.0], 'GSM7781625': [0.0], 'GSM7781626': [0.0], 'GSM7781627': [0.0], 'GSM7781628': [0.0], 'GSM7781629': [0.0], 'GSM7781630': [0.0], 'GSM7781631': [0.0], 'GSM7781632': [0.0], 'GSM7781633': [1.0], 'GSM7781634': [1.0], 'GSM7781635': [1.0], 'GSM7781636': [1.0], 'GSM7781637': [1.0], 'GSM7781638': [1.0], 'GSM7781639': [1.0], 'GSM7781640': [1.0], 'GSM7781641': [1.0], 'GSM7781642': [1.0], 'GSM7781643': [1.0], 'GSM7781644': [1.0], 'GSM7781645': [0.0], 'GSM7781646': [0.0], 'GSM7781647': [0.0], 'GSM7781648': [1.0], 'GSM7781649': [1.0], 'GSM7781650': [1.0], 'GSM7781651': [1.0], 'GSM7781652': [1.0], 'GSM7781653': [1.0], 'GSM7781654': [1.0], 'GSM7781655': [1.0], 'GSM7781656': [1.0], 'GSM7781657': [1.0], 'GSM7781658': [1.0], 'GSM7781659': [1.0]}\n", "Clinical data saved to ../../output/preprocess/Alzheimers_Disease/clinical_data/GSE243243.csv\n", "Linking clinical and genetic data...\n", "Linked data shape: (93, 19820)\n", "Handling missing values...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data shape after handling missing values: (93, 19820)\n", "Checking for bias in trait distribution...\n", "For the feature 'Alzheimers_Disease', the least common label is '0.0' with 45 occurrences. This represents 48.39% of the dataset.\n", "The distribution of the feature 'Alzheimers_Disease' in this dataset is fine.\n", "\n", "Dataset usability: True\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Alzheimers_Disease/GSE243243.csv\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "print(\"Normalizing gene symbols...\")\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Gene data shape after normalization: {normalized_gene_data.shape}\")\n", "\n", "# Save the normalized gene data to a CSV 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", "\n", "# 2. Link the clinical and genetic data\n", "print(\"Loading the original clinical data...\")\n", "# Get the matrix file again to ensure we have the proper data\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "background_info, clinical_data = get_background_and_clinical_data(matrix_file)\n", "\n", "print(\"Extracting clinical features...\")\n", "# Use the clinical_data obtained directly from the matrix file\n", "selected_clinical_df = geo_select_clinical_features(\n", " clinical_df=clinical_data,\n", " trait=trait,\n", " trait_row=trait_row,\n", " convert_trait=convert_trait,\n", " age_row=age_row,\n", " convert_age=convert_age,\n", " gender_row=gender_row,\n", " convert_gender=convert_gender\n", ")\n", "\n", "print(\"Clinical data preview:\")\n", "print(preview_df(selected_clinical_df))\n", "\n", "# Save the clinical data to a CSV file\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", "# Link clinical and genetic data using the normalized gene data\n", "print(\"Linking clinical and genetic data...\")\n", "linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "\n", "# 3. Handle missing values in the linked data\n", "print(\"Handling missing values...\")\n", "linked_data = handle_missing_values(linked_data, trait)\n", "print(f\"Linked data shape after handling missing values: {linked_data.shape}\")\n", "\n", "# 4. Check if trait is biased\n", "print(\"Checking for bias in trait distribution...\")\n", "is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "\n", "# 5. Final validation\n", "note = \"Dataset contains gene expression data from bronchial brushings from control individuals and patients with asthma after rhinovirus infection in vivo, as described in the study 'Rhinovirus-induced epithelial RIG-I inflammasome suppresses antiviral immunity and promotes inflammation in asthma and COVID-19'.\"\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", "print(f\"Dataset usability: {is_usable}\")\n", "\n", "# 6. Save linked data if usable\n", "if is_usable:\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " linked_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", "else:\n", " print(\"Dataset is not usable for trait-gene association studies due to bias or other 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 }