{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "bc839191", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:52.534121Z", "iopub.status.busy": "2025-03-25T07:33:52.533948Z", "iopub.status.idle": "2025-03-25T07:33:52.701889Z", "shell.execute_reply": "2025-03-25T07:33:52.701432Z" } }, "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 = \"Liver_cirrhosis\"\n", "cohort = \"GSE285291\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Liver_cirrhosis\"\n", "in_cohort_dir = \"../../input/GEO/Liver_cirrhosis/GSE285291\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Liver_cirrhosis/GSE285291.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Liver_cirrhosis/gene_data/GSE285291.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Liver_cirrhosis/clinical_data/GSE285291.csv\"\n", "json_path = \"../../output/preprocess/Liver_cirrhosis/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "2b02d184", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "a52cacd9", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:52.703378Z", "iopub.status.busy": "2025-03-25T07:33:52.703230Z", "iopub.status.idle": "2025-03-25T07:33:52.716684Z", "shell.execute_reply": "2025-03-25T07:33:52.716240Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Intestinal Mucosal Mitochondrial Oxidative Phosphorylation worsens with Cirrhosis Progression and is ameliorated with FMT\"\n", "!Series_summary\t\"Intestinal barrier dysfunction, driven by increased oxidative phosphorylation (OXPHOS) activity that leads to tissue hypoxia, contributes to the progression of cirrhosis, particularly impacting the upper intestine. This study explores the interplay between intestinal OXPHOS, gut microbiota changes, and the effects of fecal microbiota transplant (FMT) in cirrhotic patients. We investigated 32 age-matched men across three groups: healthy controls, compensated cirrhosis, and decompensated cirrhosis. Each underwent endoscopy with duodenal and ascending colon biopsies. Subsequently, in a follow-up study, nine patients with hepatic encephalopathy, previously enrolled in a randomized controlled trial for FMT capsules, underwent repeat pre and post-FMT upper endoscopy. Our bioinformatics analysis highlighted a significant upregulation of nuclear-encoded OXPHOS genes in both intestinal regions of cirrhosis patients compared to controls, with further dysregulation in the decompensated group. We also observed a strong correlation between shifts in gut microbiota composition, Model for End-Stage Liver Disease (MELD) scores, and OXPHOS activity. Following FMT, patients displayed a significant reduction in OXPHOS gene expression in the duodenum, suggesting that FMT may restore intestinal barrier function and offer a therapeutic avenue to mitigate liver disease progression. The findings indicate that managing intestinal OXPHOS and microbiota through FMT could be relevant in modulating microbially-based therapies.\"\n", "!Series_overall_design\t\"32 age-matched men across three groups: healthy controls, compensated cirrhosis, and decompensated cirrhosis. Each underwent endoscopy with duodenal and ascending colon biopsies. Subsequently, in a follow-up study, nine patients with hepatic encephalopathy, previously enrolled in a randomized controlled trial for FMT capsules, underwent repeat pre and post-FMT upper endoscopy.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['group: Compensated', 'group: Control', 'group: Rifaximin only', 'group: Lactulose Only Rifaximin only', 'group: Rifaximin', 'group: Lactulose Rifaximin', 'group: Baseline', 'group: After FMT'], 1: ['status: Compensated', 'status: Control', 'status: Decompensated'], 2: ['patientid: N/A', 'patientid: FMT_101', 'patientid: FMT_102', 'patientid: FMT_103', 'patientid: FMT_104', 'patientid: FMT_105', 'patientid: FMT_106', 'patientid: FMT_107', 'patientid: FMT_108', 'patientid: FMT_109', 'patientid: FMT_110', 'patientid: FMT_201', 'patientid: FMT_202', 'patientid: FMT_203', 'patientid: FMT_204', 'patientid: FMT_205', 'patientid: FMT_206', 'patientid: FMT_207', 'patientid: FMT_208', 'patientid: FMT_209', 'patientid: FMT_210'], 3: ['tissue: Duodenum', 'tissue: Ascending Colon']}\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": "237a74b6", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "eb07c5b5", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:52.718050Z", "iopub.status.busy": "2025-03-25T07:33:52.717932Z", "iopub.status.idle": "2025-03-25T07:33:52.722128Z", "shell.execute_reply": "2025-03-25T07:33:52.721691Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical data file not found. Skipping clinical feature extraction.\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "is_gene_available = True # Based on the background, this is a transcriptomic study of OXPHOS genes\n", "\n", "# 2.1 Data Availability\n", "# For cirrhosis trait - row 1 has \"status\" which indicates cirrhosis status\n", "trait_row = 1\n", "\n", "# Age is not mentioned in sample characteristics, not available\n", "age_row = None\n", "\n", "# Gender is not explicitly available, but the background mentions \"32 age-matched men\"\n", "# Since all subjects are male, gender is a constant feature and not useful for our analysis\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value):\n", " \"\"\"Convert cirrhosis status to binary: Control=0, Compensated/Decompensated=1.\"\"\"\n", " if isinstance(value, str) and \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " if value == \"Control\":\n", " return 0\n", " elif value in [\"Compensated\", \"Decompensated\"]:\n", " return 1\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Not used as age data is not available.\"\"\"\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Not used as gender data is not available.\"\"\"\n", " return None\n", "\n", "# 3. Save Metadata\n", "is_trait_available = trait_row is not None\n", "validate_and_save_cohort_info(\n", " is_final=False,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=is_gene_available,\n", " is_trait_available=is_trait_available\n", ")\n", "\n", "# 4. Clinical Feature Extraction\n", "# For this step, we need to first check if clinical_data is available\n", "# Since we don't have a clinical_data.csv file, we'll skip this substep\n", "print(\"Clinical data file not found. Skipping clinical feature extraction.\")\n" ] }, { "cell_type": "markdown", "id": "040e711d", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "e382dcc1", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:52.723454Z", "iopub.status.busy": "2025-03-25T07:33:52.723345Z", "iopub.status.idle": "2025-03-25T07:33:52.737302Z", "shell.execute_reply": "2025-03-25T07:33:52.736832Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix file found: ../../input/GEO/Liver_cirrhosis/GSE285291/GSE285291_series_matrix.txt.gz\n", "Gene data shape: (803, 53)\n", "First 20 gene/probe identifiers:\n", "Index(['A2M', 'AADAT', 'AANAT', 'ABCB7', 'ABL1', 'ACAA2', 'ACACA', 'ACACB',\n", " 'ACAD11', 'ACADL', 'ACADM', 'ACADVL', 'ACAP2', 'ACAT1', 'ACAT2',\n", " 'ACMSD', 'ACO2', 'ACOT12', 'ACOX1', 'ACSF3'],\n", " dtype='object', name='ID')\n" ] } ], "source": [ "# 1. Get the SOFT and matrix file paths again \n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "print(f\"Matrix file found: {matrix_file}\")\n", "\n", "# 2. Use the get_genetic_data function from the library to get the gene_data\n", "try:\n", " gene_data = get_genetic_data(matrix_file)\n", " print(f\"Gene data shape: {gene_data.shape}\")\n", " \n", " # 3. Print the first 20 row IDs (gene or probe identifiers)\n", " print(\"First 20 gene/probe identifiers:\")\n", " print(gene_data.index[:20])\n", "except Exception as e:\n", " print(f\"Error extracting gene data: {e}\")\n" ] }, { "cell_type": "markdown", "id": "b7dac5a9", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "8853a6ba", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:52.738741Z", "iopub.status.busy": "2025-03-25T07:33:52.738630Z", "iopub.status.idle": "2025-03-25T07:33:52.741197Z", "shell.execute_reply": "2025-03-25T07:33:52.740689Z" } }, "outputs": [], "source": [ "# These identifiers appear to be standard human gene symbols already.\n", "# They follow the standard HGNC naming conventions for human genes.\n", "# For example:\n", "# A2M = Alpha-2-Macroglobulin\n", "# AADAT = Aminoadipate Aminotransferase\n", "# ACACA = Acetyl-CoA Carboxylase Alpha\n", "# ACO2 = Aconitase 2\n", "\n", "# The identifiers are already in the correct format and don't need mapping\n", "requires_gene_mapping = False\n" ] }, { "cell_type": "markdown", "id": "3a2907a0", "metadata": {}, "source": [ "### Step 5: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 6, "id": "1f8e4d7d", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:52.742943Z", "iopub.status.busy": "2025-03-25T07:33:52.742734Z", "iopub.status.idle": "2025-03-25T07:33:53.024620Z", "shell.execute_reply": "2025-03-25T07:33:53.024023Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape before normalization: (803, 53)\n", "Gene data shape after normalization: (800, 53)\n", "Normalized gene expression data saved to ../../output/preprocess/Liver_cirrhosis/gene_data/GSE285291.csv\n", "Selected clinical data shape: (1, 53)\n", "Clinical data preview:\n", " GSM8700031 GSM8700032 GSM8700033 GSM8700034 GSM8700035 \\\n", "Liver_cirrhosis 1.0 1.0 0.0 1.0 1.0 \n", "\n", " GSM8700036 GSM8700037 GSM8700038 GSM8700039 GSM8700040 \\\n", "Liver_cirrhosis 1.0 1.0 1.0 1.0 0.0 \n", "\n", " ... GSM8700074 GSM8700075 GSM8700076 GSM8700077 \\\n", "Liver_cirrhosis ... 1.0 1.0 1.0 1.0 \n", "\n", " GSM8700078 GSM8700079 GSM8700080 GSM8700081 GSM8700082 \\\n", "Liver_cirrhosis 1.0 1.0 1.0 1.0 1.0 \n", "\n", " GSM8700083 \n", "Liver_cirrhosis 1.0 \n", "\n", "[1 rows x 53 columns]\n", "Linked data shape before processing: (53, 801)\n", "Linked data preview (first 5 rows, 5 columns):\n", " Liver_cirrhosis A2M AADAT AANAT ABCB7\n", "GSM8700031 1.0 7.738540 4.181906 6.933227 6.029903\n", "GSM8700032 1.0 7.626456 5.551493 7.675822 5.512499\n", "GSM8700033 0.0 7.600063 3.324785 6.968642 6.150756\n", "GSM8700034 1.0 6.983159 5.983159 5.983159 6.070622\n", "GSM8700035 1.0 7.620274 7.059559 6.782025 6.544986\n", "Data shape after handling missing values: (53, 782)\n", "For the feature 'Liver_cirrhosis', the least common label is '0.0' with 8 occurrences. This represents 15.09% of the dataset.\n", "The distribution of the feature 'Liver_cirrhosis' in this dataset is fine.\n", "\n", "Data shape after removing biased features: (53, 782)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Liver_cirrhosis/GSE285291.csv\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "# Use normalize_gene_symbols_in_index to standardize gene symbols\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Gene data shape before normalization: {gene_data.shape}\")\n", "print(f\"Gene data shape after normalization: {normalized_gene_data.shape}\")\n", "\n", "# Save the normalized gene data to 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 expression data saved to {out_gene_data_file}\")\n", "\n", "# Check if trait_row is None (indicating no clinical data is available)\n", "if trait_row is None:\n", " print(\"No clinical data available for this dataset, skipping clinical data processing.\")\n", " \n", " # Validate and save cohort information with trait_available=False\n", " is_usable = validate_and_save_cohort_info(\n", " is_final=True,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=True,\n", " is_trait_available=False,\n", " is_biased=True, # Set to True since we can't use this data without clinical features\n", " df=pd.DataFrame(), # Empty DataFrame since we have no linked data\n", " note=\"Dataset contains gene expression data from cell lines with HCV infection, which is not appropriate for liver cirrhosis trait analysis.\"\n", " )\n", " \n", " print(\"Dataset is not usable for liver cirrhosis analysis due to lack of clinical data. No linked data file saved.\")\n", "else:\n", " # If clinical data is available, proceed with the linking and processing\n", " # 2. Link the clinical and genetic data\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(f\"Selected clinical data shape: {selected_clinical_df.shape}\")\n", " print(\"Clinical data preview:\")\n", " print(selected_clinical_df.head())\n", "\n", " # Link the clinical and genetic data\n", " linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)\n", " print(f\"Linked data shape before processing: {linked_data.shape}\")\n", " print(\"Linked data preview (first 5 rows, 5 columns):\")\n", " print(linked_data.iloc[:5, :5] if not linked_data.empty else \"Empty dataframe\")\n", "\n", " # 3. Handle missing values\n", " try:\n", " linked_data = handle_missing_values(linked_data, trait)\n", " print(f\"Data shape after handling missing values: {linked_data.shape}\")\n", " except Exception as e:\n", " print(f\"Error handling missing values: {e}\")\n", " linked_data = pd.DataFrame() # Create empty dataframe if error occurs\n", "\n", " # 4. Check for bias in features\n", " if not linked_data.empty:\n", " is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", " print(f\"Data shape after removing biased features: {linked_data.shape}\")\n", " else:\n", " is_biased = True\n", " print(\"Cannot check for bias as dataframe is empty after missing value handling\")\n", "\n", " # 5. Validate and save cohort information\n", " is_usable = validate_and_save_cohort_info(\n", " is_final=True,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=True,\n", " is_trait_available=True,\n", " is_biased=is_biased,\n", " df=linked_data,\n", " note=\"Dataset contains gene expression data for liver fibrosis progression, which is relevant to liver cirrhosis research.\"\n", " )\n", "\n", " # 6. Save the 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 analysis. No linked data file saved.\")" ] } ], "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 }