{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "028f1fa0", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:25:41.452199Z", "iopub.status.busy": "2025-03-25T06:25:41.451988Z", "iopub.status.idle": "2025-03-25T06:25:41.622792Z", "shell.execute_reply": "2025-03-25T06:25:41.622359Z" } }, "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 = \"GSE122063\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Alzheimers_Disease\"\n", "in_cohort_dir = \"../../input/GEO/Alzheimers_Disease/GSE122063\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Alzheimers_Disease/GSE122063.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Alzheimers_Disease/gene_data/GSE122063.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Alzheimers_Disease/clinical_data/GSE122063.csv\"\n", "json_path = \"../../output/preprocess/Alzheimers_Disease/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "7a0ea38b", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "97c07b6e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:25:41.624145Z", "iopub.status.busy": "2025-03-25T06:25:41.624001Z", "iopub.status.idle": "2025-03-25T06:25:42.120255Z", "shell.execute_reply": "2025-03-25T06:25:42.119661Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Dementia Comparison: VaD vs. AD vs. Controls\"\n", "!Series_summary\t\"Gene expression profiling was performed on frontal and temporal cortex from vascular dementia (VaD), Alzheimer's disease (AD), and non-demented controls (Control) obtained from the University of Michigan Brain Bank. Controls and AD cases had no infarcts in the autopsied hemisphere. Vascular dementia cases had low Braak staging.\"\n", "!Series_overall_design\t\"Each sample (VaD=8), (AD=12), (Controls=11) was run, at a minimum, in duplicate with a dye swap (Cy3/Cy5) on Agilent Human 8x60k v2 microarrays.\"\n", "!Series_overall_design\t\"\"\n", "!Series_overall_design\t\"These are dual channel arrays, but have been processed as a single channel analysis. Normalized log2 signal is provided for each sample. Raw files are included in a tar archive on the series record. Please see 'Description' field for the name of the raw file for each sample.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['patient diagnosis: Vascular dementia', \"patient diagnosis: Alzheimer's disease\", 'patient diagnosis: Control'], 1: ['tissue: Brain'], 2: ['brain region: frontal cortex', 'brain region: temporal cortex'], 3: ['subject id: 381', 'subject id: 444', 'subject id: 488', 'subject id: 745', 'subject id: 981', 'subject id: 1063', 'subject id: 1370', 'subject id: 1396', 'subject id: 279', 'subject id: 326', 'subject id: 413', 'subject id: 418', 'subject id: 544', 'subject id: 754', 'subject id: 765', 'subject id: 850', 'subject id: 895', 'subject id: 958', 'subject id: 1181', 'subject id: 1337', 'subject id: 57', 'subject id: 90', 'subject id: 100', 'subject id: 110', 'subject id: 382', 'subject id: 566', 'subject id: 729', 'subject id: 732', 'subject id: 915', 'subject id: 978'], 4: ['pmi: 17', 'pmi: 15', 'pmi: 12', 'pmi: 4', 'pmi: 7', 'pmi: 6', 'pmi: 9', 'pmi: 5', 'pmi: 14', 'pmi: 8', 'pmi: 10'], 5: ['Sex: Male', 'Sex: Female'], 6: ['age: 75', 'age: 90', 'age: 78', 'age: 82', 'age: 96', 'age: 77', 'age: 93', 'age: 62', 'age: 89', 'age: 79', 'age: 81', 'age: 91', 'age: 83', 'age: 63', 'age: 88', 'age: 74', 'age: 73', 'age: 87', 'age: 60']}\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": "a7b31f08", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "b3131b3d", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:25:42.122126Z", "iopub.status.busy": "2025-03-25T06:25:42.121969Z", "iopub.status.idle": "2025-03-25T06:25:42.157171Z", "shell.execute_reply": "2025-03-25T06:25:42.156697Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical Data Preview:\n", "{'GSM3454053': [nan, 75.0, 1.0], 'GSM3454054': [nan, 75.0, 1.0], 'GSM3454055': [nan, 75.0, 1.0], 'GSM3454056': [nan, 75.0, 1.0], 'GSM3454057': [nan, 75.0, 1.0], 'GSM3454058': [nan, 75.0, 1.0], 'GSM3454059': [nan, 75.0, 1.0], 'GSM3454060': [nan, 75.0, 1.0], 'GSM3454061': [nan, 90.0, 0.0], 'GSM3454062': [nan, 90.0, 0.0], 'GSM3454063': [nan, 90.0, 0.0], 'GSM3454064': [nan, 90.0, 0.0], 'GSM3454065': [nan, 78.0, 1.0], 'GSM3454066': [nan, 78.0, 1.0], 'GSM3454067': [nan, 78.0, 1.0], 'GSM3454068': [nan, 78.0, 1.0], 'GSM3454069': [nan, 82.0, 0.0], 'GSM3454070': [nan, 82.0, 0.0], 'GSM3454071': [nan, 82.0, 0.0], 'GSM3454072': [nan, 82.0, 0.0], 'GSM3454073': [nan, 96.0, 0.0], 'GSM3454074': [nan, 96.0, 0.0], 'GSM3454075': [nan, 96.0, 0.0], 'GSM3454076': [nan, 96.0, 0.0], 'GSM3454077': [nan, 77.0, 1.0], 'GSM3454078': [nan, 77.0, 1.0], 'GSM3454079': [nan, 77.0, 1.0], 'GSM3454080': [nan, 77.0, 1.0], 'GSM3454081': [nan, 93.0, 0.0], 'GSM3454082': [nan, 93.0, 0.0], 'GSM3454083': [nan, 93.0, 0.0], 'GSM3454084': [nan, 93.0, 0.0], 'GSM3454085': [nan, 62.0, 1.0], 'GSM3454086': [nan, 62.0, 1.0], 'GSM3454087': [nan, 62.0, 1.0], 'GSM3454088': [nan, 62.0, 1.0], 'GSM3454089': [1.0, 82.0, 0.0], 'GSM3454090': [1.0, 82.0, 0.0], 'GSM3454091': [1.0, 82.0, 0.0], 'GSM3454092': [1.0, 82.0, 0.0], 'GSM3454093': [1.0, 82.0, 0.0], 'GSM3454094': [1.0, 82.0, 0.0], 'GSM3454095': [1.0, 82.0, 0.0], 'GSM3454096': [1.0, 82.0, 0.0], 'GSM3454097': [1.0, 89.0, 0.0], 'GSM3454098': [1.0, 89.0, 0.0], 'GSM3454099': [1.0, 89.0, 0.0], 'GSM3454100': [1.0, 89.0, 0.0], 'GSM3454101': [1.0, 82.0, 0.0], 'GSM3454102': [1.0, 82.0, 0.0], 'GSM3454103': [1.0, 82.0, 0.0], 'GSM3454104': [1.0, 82.0, 0.0], 'GSM3454105': [1.0, 77.0, 0.0], 'GSM3454106': [1.0, 77.0, 0.0], 'GSM3454107': [1.0, 77.0, 0.0], 'GSM3454108': [1.0, 77.0, 0.0], 'GSM3454109': [1.0, 79.0, 1.0], 'GSM3454110': [1.0, 79.0, 1.0], 'GSM3454111': [1.0, 79.0, 1.0], 'GSM3454112': [1.0, 79.0, 1.0], 'GSM3454113': [1.0, 81.0, 0.0], 'GSM3454114': [1.0, 81.0, 0.0], 'GSM3454115': [1.0, 81.0, 0.0], 'GSM3454116': [1.0, 81.0, 0.0], 'GSM3454117': [1.0, 81.0, 0.0], 'GSM3454118': [1.0, 81.0, 0.0], 'GSM3454119': [1.0, 81.0, 0.0], 'GSM3454120': [1.0, 81.0, 0.0], 'GSM3454121': [1.0, 75.0, 0.0], 'GSM3454122': [1.0, 75.0, 0.0], 'GSM3454123': [1.0, 75.0, 0.0], 'GSM3454124': [1.0, 75.0, 0.0], 'GSM3454125': [1.0, 81.0, 1.0], 'GSM3454126': [1.0, 81.0, 1.0], 'GSM3454127': [1.0, 81.0, 1.0], 'GSM3454128': [1.0, 81.0, 1.0], 'GSM3454129': [1.0, 91.0, 1.0], 'GSM3454130': [1.0, 91.0, 1.0], 'GSM3454131': [1.0, 91.0, 1.0], 'GSM3454132': [1.0, 91.0, 1.0], 'GSM3454133': [1.0, 83.0, 0.0], 'GSM3454134': [1.0, 83.0, 0.0], 'GSM3454135': [1.0, 83.0, 0.0], 'GSM3454136': [1.0, 83.0, 0.0], 'GSM3454137': [1.0, 63.0, 0.0], 'GSM3454138': [1.0, 63.0, 0.0], 'GSM3454139': [1.0, 63.0, 0.0], 'GSM3454140': [1.0, 63.0, 0.0], 'GSM3454141': [1.0, 88.0, 0.0], 'GSM3454142': [1.0, 88.0, 0.0], 'GSM3454143': [1.0, 88.0, 0.0], 'GSM3454144': [1.0, 88.0, 0.0], 'GSM3454145': [0.0, 74.0, 0.0], 'GSM3454146': [0.0, 74.0, 0.0], 'GSM3454147': [0.0, 74.0, 0.0], 'GSM3454148': [0.0, 74.0, 0.0], 'GSM3454149': [0.0, 73.0, 1.0], 'GSM3454150': [0.0, 73.0, 1.0], 'GSM3454151': [0.0, 73.0, 1.0], 'GSM3454152': [0.0, 73.0, 1.0], 'GSM3454153': [0.0, 87.0, 0.0], 'GSM3454154': [0.0, 87.0, 0.0], 'GSM3454155': [0.0, 87.0, 0.0], 'GSM3454156': [0.0, 87.0, 0.0], 'GSM3454157': [0.0, 73.0, 0.0], 'GSM3454158': [0.0, 73.0, 0.0], 'GSM3454159': [0.0, 73.0, 0.0], 'GSM3454160': [0.0, 73.0, 0.0], 'GSM3454161': [0.0, 81.0, 1.0], 'GSM3454162': [0.0, 81.0, 1.0], 'GSM3454163': [0.0, 81.0, 1.0], 'GSM3454164': [0.0, 81.0, 1.0], 'GSM3454165': [0.0, 81.0, 0.0], 'GSM3454166': [0.0, 81.0, 0.0], 'GSM3454167': [0.0, 81.0, 0.0], 'GSM3454168': [0.0, 81.0, 0.0], 'GSM3454169': [0.0, 60.0, 1.0], 'GSM3454170': [0.0, 60.0, 1.0], 'GSM3454171': [0.0, 60.0, 1.0], 'GSM3454172': [0.0, 60.0, 1.0], 'GSM3454173': [0.0, 91.0, 1.0], 'GSM3454174': [0.0, 91.0, 1.0], 'GSM3454175': [0.0, 91.0, 1.0], 'GSM3454176': [0.0, 91.0, 1.0], 'GSM3454177': [0.0, 81.0, 0.0], 'GSM3454178': [0.0, 81.0, 0.0], 'GSM3454179': [0.0, 81.0, 0.0], 'GSM3454180': [0.0, 81.0, 0.0], 'GSM3454181': [0.0, 77.0, 0.0], 'GSM3454182': [0.0, 77.0, 0.0], 'GSM3454183': [0.0, 77.0, 0.0], 'GSM3454184': [0.0, 77.0, 0.0], 'GSM3454185': [0.0, 89.0, 1.0], 'GSM3454186': [0.0, 89.0, 1.0], 'GSM3454187': [0.0, 89.0, 1.0], 'GSM3454188': [0.0, 89.0, 1.0]}\n", "Clinical data saved to ../../output/preprocess/Alzheimers_Disease/clinical_data/GSE122063.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the Series_title, Series_summary, and Series_overall_design, this dataset contains gene expression data\n", "# from frontal and temporal cortex samples using Agilent Human 8x60k v2 microarrays.\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "\n", "# Trait (Alzheimer's Disease) - available in key 0 \"patient diagnosis\"\n", "trait_row = 0\n", "\n", "# Age - available in key 6 \"age\"\n", "age_row = 6\n", "\n", "# Gender - available in key 5 \"Sex\"\n", "gender_row = 5\n", "\n", "# 2.2 Data Type Conversion\n", "\n", "def convert_trait(val):\n", " \"\"\"Convert trait values to binary (0: Control, 1: Alzheimer's disease)\"\"\"\n", " if not isinstance(val, str):\n", " return None\n", " \n", " if \":\" in val:\n", " val = val.split(\":\", 1)[1].strip()\n", " \n", " if \"Alzheimer's disease\" in val or \"AD\" in val:\n", " return 1\n", " elif \"Control\" in val:\n", " return 0\n", " else: # \"Vascular dementia\" or other values\n", " return None # We only want AD vs Control\n", "\n", "def convert_age(val):\n", " \"\"\"Convert age values to continuous numeric values\"\"\"\n", " if not isinstance(val, str):\n", " return None\n", " \n", " if \":\" in val:\n", " val = val.split(\":\", 1)[1].strip()\n", " \n", " try:\n", " return float(val)\n", " except:\n", " return None\n", "\n", "def convert_gender(val):\n", " \"\"\"Convert gender values to binary (0: Female, 1: Male)\"\"\"\n", " if not isinstance(val, str):\n", " return None\n", " \n", " if \":\" in val:\n", " val = val.split(\":\", 1)[1].strip().lower()\n", " else:\n", " val = val.lower()\n", " \n", " if \"female\" in val:\n", " return 0\n", " elif \"male\" in val:\n", " return 1\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Determine trait data availability\n", "is_trait_available = trait_row is not None\n", "\n", "# Save initial filtering information\n", "validate_and_save_cohort_info(\n", " is_final=False,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=is_gene_available,\n", " is_trait_available=is_trait_available\n", ")\n", "\n", "# 4. Clinical Feature Extraction\n", "if trait_row is not None:\n", " # Extract clinical features\n", " 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 extracted clinical data\n", " preview = preview_df(clinical_df)\n", " print(\"Clinical Data Preview:\")\n", " print(preview)\n", " \n", " # Save the clinical data\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " clinical_df.to_csv(out_clinical_data_file)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "01fb46c8", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "1ac31201", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:25:42.158879Z", "iopub.status.busy": "2025-03-25T06:25:42.158740Z", "iopub.status.idle": "2025-03-25T06:25:42.944411Z", "shell.execute_reply": "2025-03-25T06:25:42.943751Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First 20 gene/probe identifiers:\n", "Index(['4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16',\n", " '17', '18', '19', '20', '21', '22', '23'],\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": "8340e0f3", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "029c3d4e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:25:42.946406Z", "iopub.status.busy": "2025-03-25T06:25:42.946245Z", "iopub.status.idle": "2025-03-25T06:25:42.948832Z", "shell.execute_reply": "2025-03-25T06:25:42.948464Z" } }, "outputs": [], "source": [ "# This is not a code execution step but an assessment of gene identifiers\n", "# Looking at the provided indices which are numeric values like '4', '5', '6', etc.\n", "# These are not standard human gene symbols (which would be alphanumeric like 'APOE', 'PSEN1', etc.)\n", "# These appear to be probe IDs or some other numerical identifiers that would need mapping to gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "11d40dc1", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "9051a6c7", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:25:42.950061Z", "iopub.status.busy": "2025-03-25T06:25:42.949949Z", "iopub.status.idle": "2025-03-25T06:25:53.411831Z", "shell.execute_reply": "2025-03-25T06:25:53.411185Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['1', '2', '3', '4', '5'], 'COL': ['192', '192', '192', '192', '192'], 'ROW': [328.0, 326.0, 324.0, 322.0, 320.0], 'NAME': ['GE_BrightCorner', 'DarkCorner', 'DarkCorner', 'A_23_P117082', 'A_33_P3246448'], 'SPOT_ID': ['CONTROL', 'CONTROL', 'CONTROL', 'A_23_P117082', 'A_33_P3246448'], 'CONTROL_TYPE': ['pos', 'pos', 'pos', 'FALSE', 'FALSE'], 'REFSEQ': [nan, nan, nan, 'NM_015987', 'NM_080671'], 'GB_ACC': [nan, nan, nan, 'NM_015987', 'NM_080671'], 'LOCUSLINK_ID': [nan, nan, nan, 50865.0, 23704.0], 'GENE_SYMBOL': [nan, nan, nan, 'HEBP1', 'KCNE4'], 'GENE_NAME': [nan, nan, nan, 'heme binding protein 1', 'potassium voltage-gated channel, Isk-related family, member 4'], 'UNIGENE_ID': [nan, nan, nan, 'Hs.642618', 'Hs.348522'], 'ENSEMBL_ID': [nan, nan, nan, 'ENST00000014930', 'ENST00000281830'], 'ACCESSION_STRING': [nan, nan, nan, 'ref|NM_015987|ens|ENST00000014930|gb|AF117615|gb|BC016277', 'ref|NM_080671|ens|ENST00000281830|tc|THC2655788'], 'CHROMOSOMAL_LOCATION': [nan, nan, nan, 'chr12:13127906-13127847', 'chr2:223920197-223920256'], 'CYTOBAND': [nan, nan, nan, 'hs|12p13.1', 'hs|2q36.1'], 'DESCRIPTION': [nan, nan, nan, 'Homo sapiens heme binding protein 1 (HEBP1), mRNA [NM_015987]', 'Homo sapiens potassium voltage-gated channel, Isk-related family, member 4 (KCNE4), mRNA [NM_080671]'], 'GO_ID': [nan, nan, nan, 'GO:0005488(binding)|GO:0005576(extracellular region)|GO:0005737(cytoplasm)|GO:0005739(mitochondrion)|GO:0005829(cytosol)|GO:0007623(circadian rhythm)|GO:0020037(heme binding)', 'GO:0005244(voltage-gated ion channel activity)|GO:0005249(voltage-gated potassium channel activity)|GO:0006811(ion transport)|GO:0006813(potassium ion transport)|GO:0016020(membrane)|GO:0016021(integral to membrane)|GO:0016324(apical plasma membrane)'], 'SEQUENCE': [nan, nan, nan, 'AAGGGGGAAAATGTGATTTGTGCCTGATCTTTCATCTGTGATTCTTATAAGAGCTTTGTC', 'GCAAGTCTCTCTGCACCTATTAAAAAGTGATGTATATACTTCCTTCTTATTCTGTTGAGT']}\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": "399268e0", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "0995315e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:25:53.413594Z", "iopub.status.busy": "2025-03-25T06:25:53.413473Z", "iopub.status.idle": "2025-03-25T06:25:53.990324Z", "shell.execute_reply": "2025-03-25T06:25:53.989786Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mapping data shape: (54295, 2)\n", "First 5 rows of mapping data:\n", " ID Gene\n", "3 4 HEBP1\n", "4 5 KCNE4\n", "5 6 BPIFA3\n", "6 7 LOC100129869\n", "7 8 IRG1\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data shape after mapping: (20353, 136)\n", "First 5 gene symbols after mapping:\n", "Index(['A1BG', 'A1BG-AS1', 'A1CF', 'A2LD1', 'A2M'], dtype='object', name='Gene')\n" ] } ], "source": [ "# 1. Observe the gene identifiers and decide on the mapping columns\n", "# From the preview, we can see that 'ID' in gene_annotation corresponds to row identifiers in gene_data\n", "# And 'GENE_SYMBOL' contains the human gene symbols we need\n", "\n", "# 2. Get a gene mapping dataframe with the ID and GENE_SYMBOL columns\n", "mapping_data = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='GENE_SYMBOL')\n", "print(f\"Mapping data shape: {mapping_data.shape}\")\n", "print(\"First 5 rows of mapping data:\")\n", "print(mapping_data.head())\n", "\n", "# 3. Apply gene mapping to convert probe-level measurements to gene expression data\n", "gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_data)\n", "print(f\"Gene expression data shape after mapping: {gene_data.shape}\")\n", "print(\"First 5 gene symbols after mapping:\")\n", "print(gene_data.index[:5])\n" ] }, { "cell_type": "markdown", "id": "35e410ec", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "2c4b4692", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:25:53.991850Z", "iopub.status.busy": "2025-03-25T06:25:53.991724Z", "iopub.status.idle": "2025-03-25T06:26:08.028453Z", "shell.execute_reply": "2025-03-25T06:26:08.027765Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalizing gene symbols...\n", "Gene data shape after normalization: (19847, 136)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Alzheimers_Disease/gene_data/GSE122063.csv\n", "Loading the original clinical data...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Extracting clinical features...\n", "Clinical data preview:\n", "{'GSM3454053': [nan, 75.0, 1.0], 'GSM3454054': [nan, 75.0, 1.0], 'GSM3454055': [nan, 75.0, 1.0], 'GSM3454056': [nan, 75.0, 1.0], 'GSM3454057': [nan, 75.0, 1.0], 'GSM3454058': [nan, 75.0, 1.0], 'GSM3454059': [nan, 75.0, 1.0], 'GSM3454060': [nan, 75.0, 1.0], 'GSM3454061': [nan, 90.0, 0.0], 'GSM3454062': [nan, 90.0, 0.0], 'GSM3454063': [nan, 90.0, 0.0], 'GSM3454064': [nan, 90.0, 0.0], 'GSM3454065': [nan, 78.0, 1.0], 'GSM3454066': [nan, 78.0, 1.0], 'GSM3454067': [nan, 78.0, 1.0], 'GSM3454068': [nan, 78.0, 1.0], 'GSM3454069': [nan, 82.0, 0.0], 'GSM3454070': [nan, 82.0, 0.0], 'GSM3454071': [nan, 82.0, 0.0], 'GSM3454072': [nan, 82.0, 0.0], 'GSM3454073': [nan, 96.0, 0.0], 'GSM3454074': [nan, 96.0, 0.0], 'GSM3454075': [nan, 96.0, 0.0], 'GSM3454076': [nan, 96.0, 0.0], 'GSM3454077': [nan, 77.0, 1.0], 'GSM3454078': [nan, 77.0, 1.0], 'GSM3454079': [nan, 77.0, 1.0], 'GSM3454080': [nan, 77.0, 1.0], 'GSM3454081': [nan, 93.0, 0.0], 'GSM3454082': [nan, 93.0, 0.0], 'GSM3454083': [nan, 93.0, 0.0], 'GSM3454084': [nan, 93.0, 0.0], 'GSM3454085': [nan, 62.0, 1.0], 'GSM3454086': [nan, 62.0, 1.0], 'GSM3454087': [nan, 62.0, 1.0], 'GSM3454088': [nan, 62.0, 1.0], 'GSM3454089': [1.0, 82.0, 0.0], 'GSM3454090': [1.0, 82.0, 0.0], 'GSM3454091': [1.0, 82.0, 0.0], 'GSM3454092': [1.0, 82.0, 0.0], 'GSM3454093': [1.0, 82.0, 0.0], 'GSM3454094': [1.0, 82.0, 0.0], 'GSM3454095': [1.0, 82.0, 0.0], 'GSM3454096': [1.0, 82.0, 0.0], 'GSM3454097': [1.0, 89.0, 0.0], 'GSM3454098': [1.0, 89.0, 0.0], 'GSM3454099': [1.0, 89.0, 0.0], 'GSM3454100': [1.0, 89.0, 0.0], 'GSM3454101': [1.0, 82.0, 0.0], 'GSM3454102': [1.0, 82.0, 0.0], 'GSM3454103': [1.0, 82.0, 0.0], 'GSM3454104': [1.0, 82.0, 0.0], 'GSM3454105': [1.0, 77.0, 0.0], 'GSM3454106': [1.0, 77.0, 0.0], 'GSM3454107': [1.0, 77.0, 0.0], 'GSM3454108': [1.0, 77.0, 0.0], 'GSM3454109': [1.0, 79.0, 1.0], 'GSM3454110': [1.0, 79.0, 1.0], 'GSM3454111': [1.0, 79.0, 1.0], 'GSM3454112': [1.0, 79.0, 1.0], 'GSM3454113': [1.0, 81.0, 0.0], 'GSM3454114': [1.0, 81.0, 0.0], 'GSM3454115': [1.0, 81.0, 0.0], 'GSM3454116': [1.0, 81.0, 0.0], 'GSM3454117': [1.0, 81.0, 0.0], 'GSM3454118': [1.0, 81.0, 0.0], 'GSM3454119': [1.0, 81.0, 0.0], 'GSM3454120': [1.0, 81.0, 0.0], 'GSM3454121': [1.0, 75.0, 0.0], 'GSM3454122': [1.0, 75.0, 0.0], 'GSM3454123': [1.0, 75.0, 0.0], 'GSM3454124': [1.0, 75.0, 0.0], 'GSM3454125': [1.0, 81.0, 1.0], 'GSM3454126': [1.0, 81.0, 1.0], 'GSM3454127': [1.0, 81.0, 1.0], 'GSM3454128': [1.0, 81.0, 1.0], 'GSM3454129': [1.0, 91.0, 1.0], 'GSM3454130': [1.0, 91.0, 1.0], 'GSM3454131': [1.0, 91.0, 1.0], 'GSM3454132': [1.0, 91.0, 1.0], 'GSM3454133': [1.0, 83.0, 0.0], 'GSM3454134': [1.0, 83.0, 0.0], 'GSM3454135': [1.0, 83.0, 0.0], 'GSM3454136': [1.0, 83.0, 0.0], 'GSM3454137': [1.0, 63.0, 0.0], 'GSM3454138': [1.0, 63.0, 0.0], 'GSM3454139': [1.0, 63.0, 0.0], 'GSM3454140': [1.0, 63.0, 0.0], 'GSM3454141': [1.0, 88.0, 0.0], 'GSM3454142': [1.0, 88.0, 0.0], 'GSM3454143': [1.0, 88.0, 0.0], 'GSM3454144': [1.0, 88.0, 0.0], 'GSM3454145': [0.0, 74.0, 0.0], 'GSM3454146': [0.0, 74.0, 0.0], 'GSM3454147': [0.0, 74.0, 0.0], 'GSM3454148': [0.0, 74.0, 0.0], 'GSM3454149': [0.0, 73.0, 1.0], 'GSM3454150': [0.0, 73.0, 1.0], 'GSM3454151': [0.0, 73.0, 1.0], 'GSM3454152': [0.0, 73.0, 1.0], 'GSM3454153': [0.0, 87.0, 0.0], 'GSM3454154': [0.0, 87.0, 0.0], 'GSM3454155': [0.0, 87.0, 0.0], 'GSM3454156': [0.0, 87.0, 0.0], 'GSM3454157': [0.0, 73.0, 0.0], 'GSM3454158': [0.0, 73.0, 0.0], 'GSM3454159': [0.0, 73.0, 0.0], 'GSM3454160': [0.0, 73.0, 0.0], 'GSM3454161': [0.0, 81.0, 1.0], 'GSM3454162': [0.0, 81.0, 1.0], 'GSM3454163': [0.0, 81.0, 1.0], 'GSM3454164': [0.0, 81.0, 1.0], 'GSM3454165': [0.0, 81.0, 0.0], 'GSM3454166': [0.0, 81.0, 0.0], 'GSM3454167': [0.0, 81.0, 0.0], 'GSM3454168': [0.0, 81.0, 0.0], 'GSM3454169': [0.0, 60.0, 1.0], 'GSM3454170': [0.0, 60.0, 1.0], 'GSM3454171': [0.0, 60.0, 1.0], 'GSM3454172': [0.0, 60.0, 1.0], 'GSM3454173': [0.0, 91.0, 1.0], 'GSM3454174': [0.0, 91.0, 1.0], 'GSM3454175': [0.0, 91.0, 1.0], 'GSM3454176': [0.0, 91.0, 1.0], 'GSM3454177': [0.0, 81.0, 0.0], 'GSM3454178': [0.0, 81.0, 0.0], 'GSM3454179': [0.0, 81.0, 0.0], 'GSM3454180': [0.0, 81.0, 0.0], 'GSM3454181': [0.0, 77.0, 0.0], 'GSM3454182': [0.0, 77.0, 0.0], 'GSM3454183': [0.0, 77.0, 0.0], 'GSM3454184': [0.0, 77.0, 0.0], 'GSM3454185': [0.0, 89.0, 1.0], 'GSM3454186': [0.0, 89.0, 1.0], 'GSM3454187': [0.0, 89.0, 1.0], 'GSM3454188': [0.0, 89.0, 1.0]}\n", "Clinical data saved to ../../output/preprocess/Alzheimers_Disease/clinical_data/GSE122063.csv\n", "Linking clinical and genetic data...\n", "Linked data shape: (136, 19850)\n", "Handling missing values...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data shape after handling missing values: (100, 19850)\n", "Checking for bias in trait distribution...\n", "For the feature 'Alzheimers_Disease', the least common label is '0.0' with 44 occurrences. This represents 44.00% of the dataset.\n", "The distribution of the feature 'Alzheimers_Disease' in this dataset is fine.\n", "\n", "Quartiles for 'Age':\n", " 25%: 77.0\n", " 50% (Median): 81.0\n", " 75%: 83.0\n", "Min: 60.0\n", "Max: 91.0\n", "The distribution of the feature 'Age' in this dataset is fine.\n", "\n", "For the feature 'Gender', the least common label is '1.0' with 32 occurrences. This represents 32.00% of the dataset.\n", "The distribution of the feature 'Gender' 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/GSE122063.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 }