{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "83577720", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:27:05.173166Z", "iopub.status.busy": "2025-03-25T06:27:05.173061Z", "iopub.status.idle": "2025-03-25T06:27:05.340366Z", "shell.execute_reply": "2025-03-25T06:27:05.340024Z" } }, "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 = \"GSE139384\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Alzheimers_Disease\"\n", "in_cohort_dir = \"../../input/GEO/Alzheimers_Disease/GSE139384\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Alzheimers_Disease/GSE139384.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Alzheimers_Disease/gene_data/GSE139384.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Alzheimers_Disease/clinical_data/GSE139384.csv\"\n", "json_path = \"../../output/preprocess/Alzheimers_Disease/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "3e62d77b", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "a4dd7688", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:27:05.341834Z", "iopub.status.busy": "2025-03-25T06:27:05.341684Z", "iopub.status.idle": "2025-03-25T06:27:05.374796Z", "shell.execute_reply": "2025-03-25T06:27:05.374483Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Synaptopathy in Kii ALS/PDC, a disease concept based on transcriptome analyses of human brains\"\n", "!Series_summary\t\"Amyotrophic lateral sclerosis (ALS) and parkinsonism-dementia complex (PDC) (ALS/PDC) is a unique endemic neurodegenerative disease, with high-incidence foci in the Kii Peninsula, Japan. Although ALS/PDC presents with multiple proteinopathies, the genetic and environmental factors that influence disease onset remain unknown. We performed transcriptome analyses of patients’ brains, which may provide new insights into the pathomechanisms underlying Kii ALS/PDC.\"\n", "!Series_summary\t\"We prepared frozen brains from 3 healthy controls (frontal lobe and temporal lobe), 3 patients with Alzheimer’s disease (AD) (frontal lobe and temporal lobe) as tauopathy-disease controls, and 21 patients with Kii ALS/PDC (frontal lobe and/or temporal lobe). We acquired microarray data from the cerebral gray and white matter tissues of Kii ALS/PDC patients.\"\n", "!Series_summary\t\"Microarray data revealed that the expression levels of genes associated with neurons, heat shock proteins (Hsps), DNA binding/damage, and senescence were significantly changed in Kii ALS/PDC brains compared with those in control brains. The RNA expression pattern observed for Kii ALS type brains was similar to that for Kii PDC type brains and unlike those of control and AD brains.\"\n", "!Series_summary\t\"Additionally, pathway and network analyses indicated that the molecular pathogenic mechanism underlying Kii ALS/PDC may be associated with the oxidative phosphorylation of mitochondria, ribosomes, and the synaptic vesicle cycle; in particular, upstream regulators of these mechanisms may be found in synapses and during synaptic trafficking. Therefore, we propose the novel disease concept of “synaptopathy” for Kii ALS/PDC. Furthermore, phenotypic differences between Kii ALS type and Kii PDC type were observed, based on the human leukocyte antigen (HLA) haplotype.\"\n", "!Series_summary\t\"We performed exhaustive transcriptome analyses of Kii ALS/PDC brains, for the first time, and revealed new insights indicating that Kii ALS/PDC may be a synaptopathy. Determining the relationship between synaptic dysfunction and the pathogenesis of ALS/PDC may provide a new step toward understanding this mysterious disease.\"\n", "!Series_overall_design\t\"Total RNA was extracted with an RNeasy Kit (Qiagen, Hilden, Germany), and RNA quality was assessed using an Agilent 2100 bioanalyzer (Agilent Technologies, Palo Alto, CA, USA). Total RNA (100 ng) was reverse transcribed, labeled with biotin, using a TargetAmp-Nano Labeling kit (Epicentre, Madison, WI, USA), and hybridized to a HumanHT-12 v4 Expression BeadChip (Illumina, San Diego, CA, USA). The arrays were washed and stained, using Cy3-Streptavidin, and then scanned with the BeadChip Scanner iScan System (Illumina, San Diego, CA, USA), according to the manufacturer’s instructions. The raw probe intensity data were normalized [RMA normalization (85th percentile), Low signal cutoff (cut off value: 100), Log transformation (Base 2), Ratio to control samples (mean)] by using the transcriptome data analysis software Subio Platform (Subio, Kagoshima, Japan).\"\n", "Sample Characteristics Dictionary:\n", "{0: ['subject id: CT1', 'subject id: CT2', 'subject id: CT3', 'subject id: AD1', 'subject id: AD2', 'subject id: AD3', 'clinical phenotypes: ALS', 'clinical phenotypes: ALS+D', 'clinical phenotypes: PDC+A', 'clinical phenotypes: PDC'], 1: ['clinical phenotypes: Healthy Control', 'clinical phenotypes: Alzheimer`s Disease', 'gender: Female', 'gender: Male'], 2: ['gender: Male', 'age: 66', 'age: 77', 'age: 70', 'age: 74', 'age: 76', 'age: 60', 'age: 79', 'age: 71', 'age: 63', 'age: 65', 'age: 81', 'age: 73', 'age: 72', 'age: 75', 'age: 85'], 3: ['age: 75', 'age: 76', 'age: 83', 'age: 84', 'age: 87', 'age: 88', 'age: 67', 'age: 68', 'age: 86', 'age: 74', 'tissue: Human Postmortem Brain'], 4: ['tissue: Human Postmortem Brain', 'tissue subtype: Frontal lobe', 'tissue subtype: Temporal lobe'], 5: ['tissue subtype: Frontal lobe', 'tissue subtype: Temporal lobe', nan]}\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": "9d98045c", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "950bb5b5", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:27:05.375921Z", "iopub.status.busy": "2025-03-25T06:27:05.375810Z", "iopub.status.idle": "2025-03-25T06:27:05.397916Z", "shell.execute_reply": "2025-03-25T06:27:05.397615Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of selected clinical data: {0: [0.0, nan, nan], 1: [1.0, 66.0, nan], 2: [nan, 77.0, 0.0], 3: [nan, 70.0, 1.0], 4: [nan, 74.0, nan], 5: [nan, 76.0, nan], 6: [nan, 60.0, nan], 7: [nan, 79.0, nan], 8: [nan, 71.0, nan], 9: [nan, 63.0, nan], 10: [nan, 65.0, nan], 11: [nan, 81.0, nan], 12: [nan, 73.0, nan], 13: [nan, 72.0, nan], 14: [nan, 75.0, nan], 15: [nan, 85.0, nan]}\n", "Clinical data saved to ../../output/preprocess/Alzheimers_Disease/clinical_data/GSE139384.csv\n" ] } ], "source": [ "import pandas as pd\n", "from typing import Callable, Optional, Dict, Any\n", "import os\n", "import json\n", "\n", "# 1. Gene Expression Data Availability\n", "# Based on the background information, it mentions \"transcriptome analyses\" and use of \n", "# \"HumanHT-12 v4 Expression BeadChip\" which indicates gene expression data\n", "is_gene_available = True\n", "\n", "# 2.1 Data Availability for trait, age, and gender\n", "# From the Sample Characteristics Dictionary\n", "# For trait: Looking at rows 0 and 1, row 1 has 'clinical phenotypes' including healthy control and AD\n", "trait_row = 1\n", "\n", "# For age: Looking at rows 2 and 3, both contain age information\n", "age_row = 2\n", "\n", "# For gender: Row 1 and 2 contain gender information\n", "gender_row = 1\n", "\n", "# 2.2 Data Type Conversion Functions\n", "def convert_trait(value: str) -> int:\n", " \"\"\"Convert trait value to binary (0: Control, 1: Alzheimer's Disease)\"\"\"\n", " if value is None or pd.isna(value):\n", " return None\n", " \n", " # Extract value after the colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " if \"Healthy Control\" in value:\n", " return 0\n", " elif \"Alzheimer\" in value:\n", " return 1\n", " else:\n", " # Other clinical phenotypes like ALS, PDC, etc. are not relevant for Alzheimer's Disease study\n", " return None\n", "\n", "def convert_age(value: str) -> float:\n", " \"\"\"Convert age value to continuous numeric value\"\"\"\n", " if value is None or pd.isna(value):\n", " return None\n", " \n", " # Extract value after the colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " try:\n", " return float(value)\n", " except (ValueError, TypeError):\n", " return None\n", "\n", "def convert_gender(value: str) -> int:\n", " \"\"\"Convert gender value to binary (0: Female, 1: Male)\"\"\"\n", " if value is None or pd.isna(value):\n", " return None\n", " \n", " # Extract value after the colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " if \"Female\" in value:\n", " return 0\n", " elif \"Male\" in value:\n", " return 1\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Trait data is available if trait_row is not None\n", "is_trait_available = trait_row is not None\n", "\n", "# Save initial validation results\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", " # Use the sample characteristics from previous step instead of trying to read from a file\n", " # Create a DataFrame from the sample characteristics dictionary provided in the previous output\n", " sample_chars = {\n", " 0: ['subject id: CT1', 'subject id: CT2', 'subject id: CT3', 'subject id: AD1', 'subject id: AD2', 'subject id: AD3', 'clinical phenotypes: ALS', 'clinical phenotypes: ALS+D', 'clinical phenotypes: PDC+A', 'clinical phenotypes: PDC'],\n", " 1: ['clinical phenotypes: Healthy Control', 'clinical phenotypes: Alzheimer`s Disease', 'gender: Female', 'gender: Male'],\n", " 2: ['gender: Male', 'age: 66', 'age: 77', 'age: 70', 'age: 74', 'age: 76', 'age: 60', 'age: 79', 'age: 71', 'age: 63', 'age: 65', 'age: 81', 'age: 73', 'age: 72', 'age: 75', 'age: 85'],\n", " 3: ['age: 75', 'age: 76', 'age: 83', 'age: 84', 'age: 87', 'age: 88', 'age: 67', 'age: 68', 'age: 86', 'age: 74', 'tissue: Human Postmortem Brain'],\n", " 4: ['tissue: Human Postmortem Brain', 'tissue subtype: Frontal lobe', 'tissue subtype: Temporal lobe'],\n", " 5: ['tissue subtype: Frontal lobe', 'tissue subtype: Temporal lobe', None]\n", " }\n", " \n", " # Convert the dictionary to DataFrame format needed for geo_select_clinical_features\n", " clinical_data = pd.DataFrame.from_dict(sample_chars, orient='index')\n", " \n", " try:\n", " # Extract 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 selected clinical data\n", " preview = preview_df(selected_clinical_df)\n", " print(\"Preview of selected clinical data:\", preview)\n", " \n", " # Create directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " \n", " # Save the clinical data to CSV\n", " selected_clinical_df.to_csv(out_clinical_data_file, index=False)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n", " except Exception as e:\n", " print(f\"Error during clinical feature extraction: {e}\")\n" ] }, { "cell_type": "markdown", "id": "0e2a945c", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "9c464e93", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:27:05.398923Z", "iopub.status.busy": "2025-03-25T06:27:05.398817Z", "iopub.status.idle": "2025-03-25T06:27:05.425312Z", "shell.execute_reply": "2025-03-25T06:27:05.425015Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First 20 gene/probe identifiers:\n", "Index(['ILMN_1343291', 'ILMN_1343295', 'ILMN_1651228', 'ILMN_1651229',\n", " 'ILMN_1651254', 'ILMN_1651262', 'ILMN_1651315', 'ILMN_1651354',\n", " 'ILMN_1651385', 'ILMN_1651405', 'ILMN_1651429', 'ILMN_1651438',\n", " 'ILMN_1651498', 'ILMN_1651680', 'ILMN_1651705', 'ILMN_1651719',\n", " 'ILMN_1651735', 'ILMN_1651745', 'ILMN_1651799', 'ILMN_1651819'],\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": "c56f5bfb", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "d4905b51", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:27:05.426358Z", "iopub.status.busy": "2025-03-25T06:27:05.426251Z", "iopub.status.idle": "2025-03-25T06:27:05.428019Z", "shell.execute_reply": "2025-03-25T06:27:05.427732Z" } }, "outputs": [], "source": [ "# Examining the gene identifiers in the gene expression data\n", "# The identifiers shown start with \"ILMN_\" which indicates Illumina probe IDs\n", "# These are not standard human gene symbols but probe identifiers from Illumina microarray platforms\n", "# They need to be mapped to proper gene symbols for biological interpretation\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "27dca22d", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "6b17c64a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:27:05.428988Z", "iopub.status.busy": "2025-03-25T06:27:05.428886Z", "iopub.status.idle": "2025-03-25T06:27:06.833660Z", "shell.execute_reply": "2025-03-25T06:27:06.833265Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['ILMN_1343048', 'ILMN_1343049', 'ILMN_1343050', 'ILMN_1343052', 'ILMN_1343059'], 'Species': [nan, nan, nan, nan, nan], 'Source': [nan, nan, nan, nan, nan], 'Search_Key': [nan, nan, nan, nan, nan], 'Transcript': [nan, nan, nan, nan, nan], 'ILMN_Gene': [nan, nan, nan, nan, nan], 'Source_Reference_ID': [nan, nan, nan, nan, nan], 'RefSeq_ID': [nan, nan, nan, nan, nan], 'Unigene_ID': [nan, nan, nan, nan, nan], 'Entrez_Gene_ID': [nan, nan, nan, nan, nan], 'GI': [nan, nan, nan, nan, nan], 'Accession': [nan, nan, nan, nan, nan], 'Symbol': ['phage_lambda_genome', 'phage_lambda_genome', 'phage_lambda_genome:low', 'phage_lambda_genome:low', 'thrB'], 'Protein_Product': [nan, nan, nan, nan, 'thrB'], 'Probe_Id': [nan, nan, nan, nan, nan], 'Array_Address_Id': [5090180.0, 6510136.0, 7560739.0, 1450438.0, 1240647.0], 'Probe_Type': [nan, nan, nan, nan, nan], 'Probe_Start': [nan, nan, nan, nan, nan], 'SEQUENCE': ['GAATAAAGAACAATCTGCTGATGATCCCTCCGTGGATCTGATTCGTGTAA', 'CCATGTGATACGAGGGCGCGTAGTTTGCATTATCGTTTTTATCGTTTCAA', 'CCGACAGATGTATGTAAGGCCAACGTGCTCAAATCTTCATACAGAAAGAT', 'TCTGTCACTGTCAGGAAAGTGGTAAAACTGCAACTCAATTACTGCAATGC', 'CTTGTGCCTGAGCTGTCAAAAGTAGAGCACGTCGCCGAGATGAAGGGCGC'], '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': [nan, nan, nan, nan, nan], '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': [nan, nan, nan, nan, nan]}\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": "1178fff6", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "6f716f2f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:27:06.835104Z", "iopub.status.busy": "2025-03-25T06:27:06.834958Z", "iopub.status.idle": "2025-03-25T06:27:06.879866Z", "shell.execute_reply": "2025-03-25T06:27:06.879447Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene mapping preview:\n", "{'ID': ['ILMN_1343048', 'ILMN_1343049', 'ILMN_1343050', 'ILMN_1343052', 'ILMN_1343059'], 'Gene': ['phage_lambda_genome', 'phage_lambda_genome', 'phage_lambda_genome:low', 'phage_lambda_genome:low', 'thrB']}\n", "Gene-level expression data preview:\n", "(5550, 33)\n", "First 10 gene symbols:\n", "Index(['A2BP1', 'A2M', 'AADACL1', 'AADAT', 'AAGAB', 'AARS', 'AARSD1',\n", " 'AASDHPPT', 'AATK', 'ABAT'],\n", " dtype='object', name='Gene')\n" ] } ], "source": [ "# 1. First identify which columns in gene_annotation contain probe IDs and gene symbols\n", "# From previewing gene_annotation, we can see:\n", "# - 'ID' column contains Illumina probe IDs (ILMN_*) which match our gene expression data\n", "# - 'Symbol' column contains gene symbols\n", "\n", "# 2. Get a gene mapping dataframe with the ID and Symbol columns\n", "gene_mapping = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol')\n", "\n", "# Preview the mapping dataframe\n", "print(\"Gene mapping preview:\")\n", "print(preview_df(gene_mapping))\n", "\n", "# 3. Apply the gene mapping to convert probe-level measurements to gene-level expression\n", "# This handles many-to-many relationships between probes and genes\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "\n", "# Preview the gene-level expression data\n", "print(\"Gene-level expression data preview:\")\n", "print(gene_data.shape)\n", "print(\"First 10 gene symbols:\")\n", "print(gene_data.index[:10])\n" ] }, { "cell_type": "markdown", "id": "912c0f0b", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "ad4214a6", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:27:06.881496Z", "iopub.status.busy": "2025-03-25T06:27:06.881376Z", "iopub.status.idle": "2025-03-25T06:27:08.184910Z", "shell.execute_reply": "2025-03-25T06:27:08.184507Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalizing gene symbols...\n", "Gene data shape after normalization: (5434, 33)\n", "Normalized gene data saved to ../../output/preprocess/Alzheimers_Disease/gene_data/GSE139384.csv\n", "Loading the original clinical data...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Extracting clinical features...\n", "Clinical data preview:\n", "{'GSM4140293': [0.0, nan, nan], 'GSM4140294': [0.0, nan, nan], 'GSM4140295': [0.0, nan, nan], 'GSM4140296': [0.0, nan, nan], 'GSM4140297': [0.0, nan, nan], 'GSM4140298': [0.0, nan, nan], 'GSM4140299': [1.0, nan, nan], 'GSM4140300': [1.0, nan, nan], 'GSM4140301': [1.0, nan, nan], 'GSM4140302': [1.0, nan, nan], 'GSM4140303': [1.0, nan, nan], 'GSM4140304': [1.0, nan, nan], 'GSM4140305': [nan, 66.0, 0.0], 'GSM4140306': [nan, 77.0, 1.0], 'GSM4140307': [nan, 70.0, 0.0], 'GSM4140308': [nan, 74.0, 0.0], 'GSM4140309': [nan, 76.0, 0.0], 'GSM4140310': [nan, 60.0, 0.0], 'GSM4140311': [nan, 79.0, 1.0], 'GSM4140312': [nan, 71.0, 0.0], 'GSM4140313': [nan, 63.0, 0.0], 'GSM4140314': [nan, 65.0, 1.0], 'GSM4140315': [nan, 70.0, 0.0], 'GSM4140316': [nan, 81.0, 0.0], 'GSM4140317': [nan, 70.0, 0.0], 'GSM4140318': [nan, 74.0, 1.0], 'GSM4140319': [nan, 73.0, 0.0], 'GSM4140320': [nan, 72.0, 1.0], 'GSM4140321': [nan, 72.0, 0.0], 'GSM4140322': [nan, 75.0, 1.0], 'GSM4140323': [nan, 85.0, 1.0], 'GSM4140324': [nan, 76.0, 0.0], 'GSM4140325': [nan, 74.0, 0.0]}\n", "Clinical data saved to ../../output/preprocess/Alzheimers_Disease/clinical_data/GSE139384.csv\n", "Linking clinical and genetic data...\n", "Linked data shape: (33, 5437)\n", "Handling missing values...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data shape after handling missing values: (12, 5436)\n", "Checking for bias in trait distribution...\n", "For the feature 'Alzheimers_Disease', the least common label is '0.0' with 6 occurrences. This represents 50.00% of the dataset.\n", "The distribution of the feature 'Alzheimers_Disease' in this dataset is fine.\n", "\n", "Quartiles for 'Age':\n", " 25%: nan\n", " 50% (Median): nan\n", " 75%: nan\n", "Min: nan\n", "Max: nan\n", "The distribution of the feature 'Age' in this dataset is fine.\n", "\n", "Dataset usability: True\n", "Linked data saved to ../../output/preprocess/Alzheimers_Disease/GSE139384.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 }