{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "59f5434a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:28:45.214697Z", "iopub.status.busy": "2025-03-25T08:28:45.214587Z", "iopub.status.idle": "2025-03-25T08:28:45.375790Z", "shell.execute_reply": "2025-03-25T08:28:45.375460Z" } }, "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 = \"Coronary_artery_disease\"\n", "cohort = \"GSE59867\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Coronary_artery_disease\"\n", "in_cohort_dir = \"../../input/GEO/Coronary_artery_disease/GSE59867\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Coronary_artery_disease/GSE59867.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Coronary_artery_disease/gene_data/GSE59867.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Coronary_artery_disease/clinical_data/GSE59867.csv\"\n", "json_path = \"../../output/preprocess/Coronary_artery_disease/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "60c8bfe3", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "d86a0dc5", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:28:45.377246Z", "iopub.status.busy": "2025-03-25T08:28:45.377105Z", "iopub.status.idle": "2025-03-25T08:28:45.908877Z", "shell.execute_reply": "2025-03-25T08:28:45.908517Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Gene expression profiling reveals potential prognostic biomarkers associated with the progression of heart failure\"\n", "!Series_summary\t\"Heart failure (HF) is the most common cause of morbidity and mortality in the developed countries, especially considering the present demographic tendencies in those populations.\"\n", "!Series_summary\t\"We identified biologically relevant transcripts that are significantly altered in the early phase of myocardial infarction (MI) and are associated with the development of post-myocardial infarction HF.\"\n", "!Series_overall_design\t\"We collected peripheral blood samples from patients (n=111) with ST-segment elevation myocardial infarction (STEMI) at four time points (admission, discharge, 1 month after MI, and 6 months after MI). Control group comprised patients (n=46) with a stable coronary artery disease and without a history of myocardial infarction. Affymetrix HuGene 1.0 ST arrays were used to analyze mRNA levels in periperal blood mononuclear cells (PBMCs) isolated from the study and control groups. Samples from the first three time points were compared with the samples from the same patients collected 6 months after MI (stable phase) and with the control group. Additionaly, based on plasma NT-proBNP level and left ventricular ejection fraction parameters the STEMI patients were divided into HF and non-HF groups.We attempted to identify transcripts whose differential expression on the 1st day of myocardial infarction predicted which patients would develop symptoms of HF during the 6 months of follow-up. For this purpose, we compared the microarray results for samples collected on admission for the HF group versus the non-HF group.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['cell type: PBMC'], 1: ['samples collection: on the 1st day of MI (admission)', 'samples collection: after 4-6 days of MI (discharge)', 'samples collection: 1 month after MI', 'samples collection: 6 months after MI', 'samples collection: N/A'], 2: ['hf progression: N/A', 'hf progression: HF', 'hf progression: non-HF', 'hf progression: stable CAD']}\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": "3b46ba6d", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "2b386545", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:28:45.910299Z", "iopub.status.busy": "2025-03-25T08:28:45.910185Z", "iopub.status.idle": "2025-03-25T08:28:45.935897Z", "shell.execute_reply": "2025-03-25T08:28:45.935588Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Unique values in trait_row:\n", " !Sample_characteristics_ch1\n", " hf progression: N/A\n", " hf progression: HF\n", " hf progression: non-HF\n", " hf progression: stable CAD\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'non-HF'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'HF'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'N/A'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Processing trait value: 'stable CAD'\n", "Preview of clinical data:\n", "{'GSM1448335': [nan], 'GSM1448336': [nan], 'GSM1448337': [nan], 'GSM1448338': [nan], 'GSM1448339': [nan], 'GSM1448340': [nan], 'GSM1448341': [nan], 'GSM1448342': [nan], 'GSM1448343': [nan], 'GSM1448344': [nan], 'GSM1448345': [nan], 'GSM1448346': [1.0], 'GSM1448347': [1.0], 'GSM1448348': [1.0], 'GSM1448349': [1.0], 'GSM1448350': [nan], 'GSM1448351': [nan], 'GSM1448352': [nan], 'GSM1448353': [nan], 'GSM1448354': [nan], 'GSM1448355': [nan], 'GSM1448356': [nan], 'GSM1448357': [nan], 'GSM1448358': [nan], 'GSM1448359': [nan], 'GSM1448360': [nan], 'GSM1448361': [nan], 'GSM1448362': [nan], 'GSM1448363': [nan], 'GSM1448364': [nan], 'GSM1448365': [nan], 'GSM1448366': [nan], 'GSM1448367': [nan], 'GSM1448368': [nan], 'GSM1448369': [nan], 'GSM1448370': [nan], 'GSM1448371': [nan], 'GSM1448372': [nan], 'GSM1448373': [nan], 'GSM1448374': [nan], 'GSM1448375': [nan], 'GSM1448376': [nan], 'GSM1448377': [nan], 'GSM1448378': [nan], 'GSM1448379': [nan], 'GSM1448380': [nan], 'GSM1448381': [nan], 'GSM1448382': [nan], 'GSM1448383': [nan], 'GSM1448384': [nan], 'GSM1448385': [nan], 'GSM1448386': [nan], 'GSM1448387': [nan], 'GSM1448388': [nan], 'GSM1448389': [nan], 'GSM1448390': [nan], 'GSM1448391': [nan], 'GSM1448392': [nan], 'GSM1448393': [nan], 'GSM1448394': [nan], 'GSM1448395': [nan], 'GSM1448396': [nan], 'GSM1448397': [nan], 'GSM1448398': [nan], 'GSM1448399': [nan], 'GSM1448400': [nan], 'GSM1448401': [nan], 'GSM1448402': [nan], 'GSM1448403': [nan], 'GSM1448404': [nan], 'GSM1448405': [nan], 'GSM1448406': [nan], 'GSM1448407': [nan], 'GSM1448408': [nan], 'GSM1448409': [nan], 'GSM1448410': [nan], 'GSM1448411': [nan], 'GSM1448412': [nan], 'GSM1448413': [nan], 'GSM1448414': [nan], 'GSM1448415': [nan], 'GSM1448416': [nan], 'GSM1448417': [nan], 'GSM1448418': [nan], 'GSM1448419': [nan], 'GSM1448420': [nan], 'GSM1448421': [nan], 'GSM1448422': [nan], 'GSM1448423': [nan], 'GSM1448424': [nan], 'GSM1448425': [1.0], 'GSM1448426': [1.0], 'GSM1448427': [1.0], 'GSM1448428': [1.0], 'GSM1448429': [nan], 'GSM1448430': [nan], 'GSM1448431': [nan], 'GSM1448432': [nan], 'GSM1448433': [nan], 'GSM1448434': [nan], 'GSM1448435': [nan], 'GSM1448436': [nan], 'GSM1448437': [nan], 'GSM1448438': [nan], 'GSM1448439': [nan], 'GSM1448440': [nan], 'GSM1448441': [nan], 'GSM1448442': [1.0], 'GSM1448443': [1.0], 'GSM1448444': [1.0], 'GSM1448445': [nan], 'GSM1448446': [1.0], 'GSM1448447': [1.0], 'GSM1448448': [1.0], 'GSM1448449': [1.0], 'GSM1448450': [nan], 'GSM1448451': [nan], 'GSM1448452': [nan], 'GSM1448453': [nan], 'GSM1448454': [nan], 'GSM1448455': [1.0], 'GSM1448456': [1.0], 'GSM1448457': [1.0], 'GSM1448458': [1.0], 'GSM1448459': [nan], 'GSM1448460': [nan], 'GSM1448461': [nan], 'GSM1448462': [nan], 'GSM1448463': [1.0], 'GSM1448464': [1.0], 'GSM1448465': [1.0], 'GSM1448466': [1.0], 'GSM1448467': [nan], 'GSM1448468': [nan], 'GSM1448469': [nan], 'GSM1448470': [nan], 'GSM1448471': [nan], 'GSM1448472': [nan], 'GSM1448473': [nan], 'GSM1448474': [nan], 'GSM1448475': [nan], 'GSM1448476': [nan], 'GSM1448477': [nan], 'GSM1448478': [nan], 'GSM1448479': [nan], 'GSM1448480': [nan], 'GSM1448481': [nan], 'GSM1448482': [nan], 'GSM1448483': [nan], 'GSM1448484': [nan], 'GSM1448485': [nan], 'GSM1448486': [nan], 'GSM1448487': [nan], 'GSM1448488': [nan], 'GSM1448489': [nan], 'GSM1448490': [nan], 'GSM1448491': [nan], 'GSM1448492': [nan], 'GSM1448493': [nan], 'GSM1448494': [nan], 'GSM1448495': [nan], 'GSM1448496': [nan], 'GSM1448497': [nan], 'GSM1448498': [nan], 'GSM1448499': [nan], 'GSM1448500': [nan], 'GSM1448501': [nan], 'GSM1448502': [nan], 'GSM1448503': [nan], 'GSM1448504': [nan], 'GSM1448505': [nan], 'GSM1448506': [nan], 'GSM1448507': [nan], 'GSM1448508': [nan], 'GSM1448509': [nan], 'GSM1448510': [nan], 'GSM1448511': [nan], 'GSM1448512': [1.0], 'GSM1448513': [1.0], 'GSM1448514': [1.0], 'GSM1448515': [1.0], 'GSM1448516': [nan], 'GSM1448517': [nan], 'GSM1448518': [nan], 'GSM1448519': [nan], 'GSM1448520': [nan], 'GSM1448521': [nan], 'GSM1448522': [nan], 'GSM1448523': [nan], 'GSM1448524': [nan], 'GSM1448525': [nan], 'GSM1448526': [nan], 'GSM1448527': [nan], 'GSM1448528': [nan], 'GSM1448529': [nan], 'GSM1448530': [nan], 'GSM1448531': [nan], 'GSM1448532': [nan], 'GSM1448533': [nan], 'GSM1448534': [nan]}\n", "Clinical data saved to ../../output/preprocess/Coronary_artery_disease/clinical_data/GSE59867.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the background information, this dataset contains gene expression data from PBMCs\n", "# using Affymetrix HuGene 1.0 ST arrays, so it's suitable for our analysis.\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# From the Sample Characteristics Dictionary, we can identify the following:\n", "\n", "# For trait (Coronary_artery_disease):\n", "# Row 2 contains 'hf progression', which has information about heart failure (HF) and stable CAD\n", "trait_row = 2\n", "\n", "# Age is not explicitly mentioned in the characteristics\n", "age_row = None\n", "\n", "# Gender is not explicitly mentioned in the characteristics\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion Functions\n", "def convert_trait(value):\n", " \"\"\"\n", " Convert trait value to binary:\n", " 1 for MI patients (HF, non-HF) - these are the cases with CAD that had myocardial infarction\n", " 0 for controls (stable CAD without MI history)\n", " None for unclear cases\n", " \"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract the value after the colon if present\n", " if isinstance(value, str) and ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Print for debugging\n", " print(f\"Processing trait value: '{value}'\")\n", " \n", " if value == 'stable CAD':\n", " return 0 # Control group (CAD without MI)\n", " elif value in ['HF', 'non-HF']:\n", " return 1 # CAD/MI patients (cases)\n", " elif value == 'N/A':\n", " return None\n", " else:\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Placeholder function for age conversion since age data is not available\"\"\"\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Placeholder function for gender conversion since gender data is not available\"\"\"\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Determine trait data availability\n", "is_trait_available = trait_row is not None\n", "\n", "# Validate and save cohort info\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", " # Print the unique values in the trait row for debugging\n", " print(\"Unique values in trait_row:\")\n", " unique_vals = clinical_data.iloc[trait_row].unique()\n", " for val in unique_vals:\n", " print(f\" {val}\")\n", " \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", " print(\"Preview of clinical data:\")\n", " print(preview_df(clinical_df))\n", " \n", " # Save the clinical data to CSV\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": "7d417fdf", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "6630c059", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:28:45.937218Z", "iopub.status.busy": "2025-03-25T08:28:45.937096Z", "iopub.status.idle": "2025-03-25T08:28:47.035904Z", "shell.execute_reply": "2025-03-25T08:28:47.035546Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SOFT file: ../../input/GEO/Coronary_artery_disease/GSE59867/GSE59867_family.soft.gz\n", "Matrix file: ../../input/GEO/Coronary_artery_disease/GSE59867/GSE59867_series_matrix.txt.gz\n", "Found the matrix table marker at line 66\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape: (33297, 436)\n", "First 20 gene/probe identifiers:\n", "['7892501', '7892502', '7892503', '7892504', '7892505', '7892506', '7892507', '7892508', '7892509', '7892510', '7892511', '7892512', '7892513', '7892514', '7892515', '7892516', '7892517', '7892518', '7892519', '7892520']\n" ] } ], "source": [ "# 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", "print(f\"SOFT file: {soft_file}\")\n", "print(f\"Matrix file: {matrix_file}\")\n", "\n", "# Set gene availability flag\n", "is_gene_available = True # Initially assume gene data is available\n", "\n", "# First check if the matrix file contains the expected marker\n", "found_marker = False\n", "marker_row = None\n", "try:\n", " with gzip.open(matrix_file, 'rt') as file:\n", " for i, line in enumerate(file):\n", " if \"!series_matrix_table_begin\" in line:\n", " found_marker = True\n", " marker_row = i\n", " print(f\"Found the matrix table marker at line {i}\")\n", " break\n", " \n", " if not found_marker:\n", " print(\"Warning: Could not find '!series_matrix_table_begin' marker in the file.\")\n", " is_gene_available = False\n", " \n", " # If marker was found, try to extract gene data\n", " if is_gene_available:\n", " try:\n", " # Try using the library function\n", " gene_data = get_genetic_data(matrix_file)\n", " \n", " if gene_data.shape[0] == 0:\n", " print(\"Warning: Extracted gene data has 0 rows.\")\n", " is_gene_available = False\n", " else:\n", " print(f\"Gene data shape: {gene_data.shape}\")\n", " # Print the first 20 gene/probe identifiers\n", " print(\"First 20 gene/probe identifiers:\")\n", " print(gene_data.index[:20].tolist())\n", " except Exception as e:\n", " print(f\"Error extracting gene data with get_genetic_data(): {e}\")\n", " is_gene_available = False\n", " \n", " # If gene data extraction failed, examine file content to diagnose\n", " if not is_gene_available:\n", " print(\"Examining file content to diagnose the issue:\")\n", " try:\n", " with gzip.open(matrix_file, 'rt') as file:\n", " # Print lines around the marker if found\n", " if marker_row is not None:\n", " for i, line in enumerate(file):\n", " if i >= marker_row - 2 and i <= marker_row + 10:\n", " print(f\"Line {i}: {line.strip()[:100]}...\")\n", " if i > marker_row + 10:\n", " break\n", " else:\n", " # If marker not found, print first 10 lines\n", " for i, line in enumerate(file):\n", " if i < 10:\n", " print(f\"Line {i}: {line.strip()[:100]}...\")\n", " else:\n", " break\n", " except Exception as e2:\n", " print(f\"Error examining file: {e2}\")\n", " \n", "except Exception as e:\n", " print(f\"Error processing file: {e}\")\n", " is_gene_available = False\n", "\n", "# Update validation information if gene data extraction failed\n", "if not is_gene_available:\n", " print(\"Gene expression data could not be successfully extracted from this dataset.\")\n", " # Update the validation record since gene data isn't available\n", " is_trait_available = False # We already determined trait data isn't available in step 2\n", " validate_and_save_cohort_info(is_final=False, cohort=cohort, info_path=json_path,\n", " is_gene_available=is_gene_available, is_trait_available=is_trait_available)\n" ] }, { "cell_type": "markdown", "id": "545e46bb", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "94e50ed3", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:28:47.037317Z", "iopub.status.busy": "2025-03-25T08:28:47.037188Z", "iopub.status.idle": "2025-03-25T08:28:47.039157Z", "shell.execute_reply": "2025-03-25T08:28:47.038881Z" } }, "outputs": [], "source": [ "# The gene identifiers in the expression data appear to be numerical probe IDs (7892501, 7892502, etc.)\n", "# These are not standard human gene symbols (which would be like BRCA1, TP53, etc.)\n", "# Most likely these are Illumina or Affymetrix probe IDs that need to be mapped to gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "17257032", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "8eb08ea2", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:28:47.040409Z", "iopub.status.busy": "2025-03-25T08:28:47.040304Z", "iopub.status.idle": "2025-03-25T08:29:06.806860Z", "shell.execute_reply": "2025-03-25T08:29:06.806177Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene annotation preview:\n", "Columns in gene annotation: ['ID', 'GB_LIST', 'SPOT_ID', 'seqname', 'RANGE_GB', 'RANGE_STRAND', 'RANGE_START', 'RANGE_STOP', 'total_probes', 'gene_assignment', 'mrna_assignment', 'category']\n", "{'ID': ['7896736', '7896738', '7896740'], 'GB_LIST': [nan, nan, 'NM_001004195,NM_001005240,NM_001005484,BC136848,BC136867,BC136907,BC136908'], 'SPOT_ID': ['chr1:53049-54936', 'chr1:63015-63887', 'chr1:69091-70008'], 'seqname': ['chr1', 'chr1', 'chr1'], 'RANGE_GB': ['NC_000001.10', 'NC_000001.10', 'NC_000001.10'], 'RANGE_STRAND': ['+', '+', '+'], 'RANGE_START': ['53049', '63015', '69091'], 'RANGE_STOP': ['54936', '63887', '70008'], 'total_probes': [7.0, 31.0, 24.0], 'gene_assignment': ['---', 'ENST00000328113 // OR4G2P // olfactory receptor, family 4, subfamily G, member 2 pseudogene // --- // --- /// ENST00000492842 // OR4G11P // olfactory receptor, family 4, subfamily G, member 11 pseudogene // --- // --- /// ENST00000588632 // OR4G1P // olfactory receptor, family 4, subfamily G, member 1 pseudogene // --- // ---', 'NM_001004195 // OR4F4 // olfactory receptor, family 4, subfamily F, member 4 // 15q26.3 // 26682 /// NM_001005240 // OR4F17 // olfactory receptor, family 4, subfamily F, member 17 // 19p13.3 // 81099 /// 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 /// ENST00000326183 // OR4F4 // olfactory receptor, family 4, subfamily F, member 4 // 15q26.3 // 26682 /// ENST00000335137 // OR4F5 // olfactory receptor, family 4, subfamily F, member 5 // 1p36.33 // 79501 /// ENST00000585993 // OR4F17 // olfactory receptor, family 4, subfamily F, member 17 // 19p13.3 // 81099 /// BC136848 // OR4F17 // olfactory receptor, family 4, subfamily F, member 17 // 19p13.3 // 81099 /// BC136867 // 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 /// BC136908 // OR4F4 // olfactory receptor, family 4, subfamily F, member 4 // 15q26.3 // 26682'], 'mrna_assignment': ['NONHSAT060105 // NONCODE // Non-coding transcript identified by NONCODE // chr1 // 100 // 100 // 7 // 7 // 0', 'ENST00000328113 // ENSEMBL // havana:known chromosome:GRCh38:15:101926805:101927707:-1 gene:ENSG00000183909 gene_biotype:unprocessed_pseudogene transcript_biotype:unprocessed_pseudogene // chr1 // 100 // 100 // 31 // 31 // 0 /// ENST00000492842 // ENSEMBL // havana:known chromosome:GRCh38:1:62948:63887:1 gene:ENSG00000240361 gene_biotype:unprocessed_pseudogene transcript_biotype:unprocessed_pseudogene // chr1 // 100 // 100 // 31 // 31 // 0 /// ENST00000588632 // ENSEMBL // havana:known chromosome:GRCh38:19:104535:105471:1 gene:ENSG00000267310 gene_biotype:unprocessed_pseudogene transcript_biotype:unprocessed_pseudogene // chr1 // 100 // 100 // 31 // 31 // 0 /// NONHSAT000016 // NONCODE // Non-coding transcript identified by NONCODE: Linc // chr1 // 100 // 100 // 31 // 31 // 0 /// NONHSAT051704 // NONCODE // Non-coding transcript identified by NONCODE: Linc // chr1 // 100 // 100 // 31 // 31 // 0 /// NONHSAT060106 // NONCODE // Non-coding transcript identified by NONCODE: Linc // chr1 // 100 // 100 // 31 // 31 // 0', 'NM_001004195 // RefSeq // Homo sapiens olfactory receptor, family 4, subfamily F, member 4 (OR4F4), mRNA. // chr1 // 100 // 100 // 24 // 24 // 0 /// NM_001005240 // RefSeq // Homo sapiens olfactory receptor, family 4, subfamily F, member 17 (OR4F17), mRNA. // chr1 // 100 // 100 // 24 // 24 // 0 /// NM_001005484 // RefSeq // Homo sapiens olfactory receptor, family 4, subfamily F, member 5 (OR4F5), mRNA. // chr1 // 100 // 100 // 24 // 24 // 0 /// ENST00000318050 // ENSEMBL // ensembl:known chromosome:GRCh38:19:110643:111696:1 gene:ENSG00000176695 gene_biotype:protein_coding transcript_biotype:protein_coding // chr1 // 100 // 100 // 24 // 24 // 0 /// ENST00000326183 // ENSEMBL // ensembl_havana_transcript:known chromosome:GRCh38:15:101922042:101923095:-1 gene:ENSG00000177693 gene_biotype:protein_coding transcript_biotype:protein_coding // chr1 // 100 // 100 // 24 // 24 // 0 /// ENST00000335137 // ENSEMBL // ensembl_havana_transcript:known chromosome:GRCh38:1:69091:70008:1 gene:ENSG00000186092 gene_biotype:protein_coding transcript_biotype:protein_coding // chr1 // 100 // 100 // 24 // 24 // 0 /// ENST00000585993 // ENSEMBL // havana:known chromosome:GRCh38:19:107461:111696:1 gene:ENSG00000176695 gene_biotype:protein_coding transcript_biotype:protein_coding // chr1 // 100 // 100 // 24 // 24 // 0 /// BC136848 // GenBank // Homo sapiens olfactory receptor, family 4, subfamily F, member 17, mRNA (cDNA clone MGC:168462 IMAGE:9020839), complete cds. // chr1 // 100 // 100 // 24 // 24 // 0 /// BC136867 // GenBank // Homo sapiens olfactory receptor, family 4, subfamily F, member 17, mRNA (cDNA clone MGC:168481 IMAGE:9020858), complete cds. // chr1 // 100 // 100 // 24 // 24 // 0 /// BC136907 // GenBank // Homo sapiens olfactory receptor, family 4, subfamily F, member 4, mRNA (cDNA clone MGC:168521 IMAGE:9020898), complete cds. // chr1 // 100 // 100 // 24 // 24 // 0 /// BC136908 // GenBank // Homo sapiens olfactory receptor, family 4, subfamily F, member 4, mRNA (cDNA clone MGC:168522 IMAGE:9020899), complete cds. // chr1 // 100 // 100 // 24 // 24 // 0 /// ENST00000618231 // ENSEMBL // havana:known chromosome:GRCh38:19:110613:111417:1 gene:ENSG00000176695 gene_biotype:protein_coding transcript_biotype:retained_intron // chr1 // 100 // 88 // 21 // 21 // 0'], 'category': ['main', 'main', 'main']}\n", "\n", "Examining gene_assignment column format (first 3 rows):\n", "Row 0: ID=7896736\n", "Gene assignment: ---...\n", "Row 1: ID=7896738\n", "Gene assignment: ENST00000328113 // OR4G2P // olfactory receptor, family 4, subfamily G, member 2 pseudogene // --- // --- /// ENST00000492842 // OR4G11P // olfactory receptor, family 4, subfamily G, member 11 pseudog...\n", "Row 2: ID=7896740\n", "Gene assignment: NM_001004195 // OR4F4 // olfactory receptor, family 4, subfamily F, member 4 // 15q26.3 // 26682 /// NM_001005240 // OR4F17 // olfactory receptor, family 4, subfamily F, member 17 // 19p13.3 // 81099 ...\n", "\n", "Gene assignment column completeness: 33297/14551225 rows (0.23%)\n", "\n", "Attempting to extract gene symbols from the first few gene_assignment entries:\n", "Row 0 extracted symbols: []\n", "Row 1 extracted symbols: ['OR4G2P', 'OR4G11P', 'OR4G1P']\n", "Row 2 extracted symbols: ['OR4F4', 'OR4F17', 'OR4F5', 'BC136848', 'BC136867', 'BC136907', 'BC136908']\n" ] } ], "source": [ "# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "gene_annotation = get_gene_annotation(soft_file)\n", "\n", "# 2. Analyze the gene annotation dataframe to identify which columns contain the gene identifiers and gene symbols\n", "print(\"\\nGene annotation preview:\")\n", "print(f\"Columns in gene annotation: {gene_annotation.columns.tolist()}\")\n", "print(preview_df(gene_annotation, n=3))\n", "\n", "# Examine the gene_assignment column that contains gene symbol information\n", "print(\"\\nExamining gene_assignment column format (first 3 rows):\")\n", "if 'ID' in gene_annotation.columns and 'gene_assignment' in gene_annotation.columns:\n", " for i in range(min(3, len(gene_annotation))):\n", " print(f\"Row {i}: ID={gene_annotation['ID'].iloc[i]}\")\n", " print(f\"Gene assignment: {gene_annotation['gene_assignment'].iloc[i][:200]}...\") # Show first 200 chars\n", "\n", " # Check the quality and completeness of the mapping\n", " non_null_assignments = gene_annotation['gene_assignment'].notna().sum()\n", " total_rows = len(gene_annotation)\n", " print(f\"\\nGene assignment column completeness: {non_null_assignments}/{total_rows} rows ({non_null_assignments/total_rows:.2%})\")\n", " \n", " # Check if some extracted gene symbols can be found in the gene_assignment column\n", " print(\"\\nAttempting to extract gene symbols from the first few gene_assignment entries:\")\n", " for i in range(min(3, len(gene_annotation))):\n", " if pd.notna(gene_annotation['gene_assignment'].iloc[i]):\n", " symbols = extract_human_gene_symbols(str(gene_annotation['gene_assignment'].iloc[i]))\n", " print(f\"Row {i} extracted symbols: {symbols}\")\n" ] }, { "cell_type": "markdown", "id": "dce65401", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "59113efd", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:29:06.808763Z", "iopub.status.busy": "2025-03-25T08:29:06.808638Z", "iopub.status.idle": "2025-03-25T08:29:16.657122Z", "shell.execute_reply": "2025-03-25T08:29:16.656458Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene mapping dataframe shape: (33297, 2)\n", "Sample of mapping data:\n", " ID Gene\n", "0 7896736 ---\n", "1 7896738 ENST00000328113 // OR4G2P // olfactory recepto...\n", "2 7896740 NM_001004195 // OR4F4 // olfactory receptor, f...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data after mapping - shape: (24229, 436)\n", "First 10 gene symbols after mapping:\n", "['A1BG', 'A1CF', 'A2M', 'A2ML1', 'A3GALT2', 'A4GALT', 'A4GNT', 'AAA1', 'AAAS', 'AACS']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Coronary_artery_disease/gene_data/GSE59867.csv\n" ] } ], "source": [ "# 1. Identify the columns in the gene annotation that contain probe IDs and gene symbols\n", "# Based on examining the data, 'ID' appears to contain the probe IDs matching those in gene_data\n", "# 'gene_assignment' contains the gene symbol information that needs extraction\n", "\n", "# 2. Extract gene mapping dataframe from annotation\n", "mapping_data = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='gene_assignment')\n", "\n", "# Print mapping info to verify\n", "print(f\"Gene mapping dataframe shape: {mapping_data.shape}\")\n", "print(\"Sample of mapping data:\")\n", "print(mapping_data.head(3))\n", "\n", "# 3. Apply gene mapping to convert probe-level measurements to gene expression\n", "# The apply_gene_mapping function will handle many-to-many mappings as specified\n", "gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_data)\n", "\n", "# Normalize gene symbols to standard format\n", "gene_data = normalize_gene_symbols_in_index(gene_data)\n", "\n", "print(f\"Gene expression data after mapping - shape: {gene_data.shape}\")\n", "print(\"First 10 gene symbols after mapping:\")\n", "print(gene_data.index[:10].tolist())\n", "\n", "# Save gene data to CSV 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" ] }, { "cell_type": "markdown", "id": "67264aea", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "8f802e35", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:29:16.658928Z", "iopub.status.busy": "2025-03-25T08:29:16.658814Z", "iopub.status.idle": "2025-03-25T08:29:41.899002Z", "shell.execute_reply": "2025-03-25T08:29:41.898239Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape before normalization: (24229, 436)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data saved to ../../output/preprocess/Coronary_artery_disease/gene_data/GSE59867.csv\n", "Loaded clinical data shape: (1, 436)\n", "Initial linked data shape: (436, 24230)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data shape after handling missing values: (110, 24230)\n", "For the feature 'Coronary_artery_disease', the least common label is '0.0' with 46 occurrences. This represents 41.82% of the dataset.\n", "The distribution of the feature 'Coronary_artery_disease' in this dataset is fine.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Coronary_artery_disease/GSE59867.csv\n" ] } ], "source": [ "# 1. Attempt to load gene data and handle possible issues with normalization\n", "try:\n", " # Create output directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", " \n", " # Check if gene_data (from previous step) has any content\n", " if gene_data.shape[0] == 0:\n", " print(\"WARNING: Gene data is empty after normalization in previous step.\")\n", " print(\"This appears to be miRNA data rather than gene expression data.\")\n", " \n", " # Since gene_data is empty, set gene_available to False\n", " is_gene_available = False\n", " \n", " # Create an empty dataframe for metadata purposes\n", " empty_df = pd.DataFrame()\n", " \n", " # Log information about this dataset for future reference\n", " 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=True, # Consider it biased as we can't use it\n", " df=empty_df,\n", " note=\"Dataset appears to contain miRNA data rather than gene expression data. Gene symbols could not be normalized.\"\n", " )\n", " \n", " print(\"Dataset marked as unusable due to lack of valid gene expression data.\")\n", " else:\n", " # If gene_data is not empty, proceed with normalization and linking\n", " print(f\"Gene data shape before normalization: {gene_data.shape}\")\n", " \n", " # Save the gene data we have, even if it's already normalized\n", " gene_data.to_csv(out_gene_data_file)\n", " print(f\"Gene data saved to {out_gene_data_file}\")\n", " \n", " # Attempt to link clinical and gene data\n", " if is_trait_available:\n", " # Load clinical data\n", " clinical_features = pd.read_csv(out_clinical_data_file, index_col=0)\n", " print(f\"Loaded clinical data shape: {clinical_features.shape}\")\n", " \n", " # Link the clinical and genetic data\n", " linked_data = geo_link_clinical_genetic_data(clinical_features, gene_data)\n", " print(f\"Initial linked data shape: {linked_data.shape}\")\n", " \n", " # Handle 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", " if linked_data.shape[0] > 0:\n", " # Check for bias in trait and demographic features\n", " is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", " \n", " # Validate data quality and save cohort info\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=\"Successfully processed gene expression data for coronary artery disease.\"\n", " )\n", " \n", " # Save the linked data if it's 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(\"Data not usable for trait study - not saving final linked data.\")\n", " else:\n", " print(\"After handling missing values, no samples remain.\")\n", " 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=True,\n", " df=pd.DataFrame(),\n", " note=\"No valid samples after handling missing values.\"\n", " )\n", " else:\n", " # Cannot proceed with linking if trait data is missing\n", " 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=True,\n", " df=pd.DataFrame(),\n", " note=\"Cannot link data because trait information is not available.\"\n", " )\n", "except Exception as e:\n", " print(f\"Error in data processing: {e}\")\n", " \n", " # Log the error and mark the dataset as unusable\n", " validate_and_save_cohort_info(\n", " is_final=True,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=False, # Consider gene data unavailable if we had an error\n", " is_trait_available=is_trait_available,\n", " is_biased=True, # Consider it biased as we can't use it\n", " df=pd.DataFrame(), # Empty dataframe for metadata\n", " note=f\"Error during normalization or linking: {str(e)}\"\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 }