{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "bf6d68b1", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:28.877007Z", "iopub.status.busy": "2025-03-25T07:33:28.876899Z", "iopub.status.idle": "2025-03-25T07:33:29.039989Z", "shell.execute_reply": "2025-03-25T07:33:29.039655Z" } }, "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 = \"GSE182065\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Liver_cirrhosis\"\n", "in_cohort_dir = \"../../input/GEO/Liver_cirrhosis/GSE182065\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Liver_cirrhosis/GSE182065.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Liver_cirrhosis/gene_data/GSE182065.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Liver_cirrhosis/clinical_data/GSE182065.csv\"\n", "json_path = \"../../output/preprocess/Liver_cirrhosis/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "b1fac390", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "856c970f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:29.041339Z", "iopub.status.busy": "2025-03-25T07:33:29.041205Z", "iopub.status.idle": "2025-03-25T07:33:29.061390Z", "shell.execute_reply": "2025-03-25T07:33:29.061110Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Expression profiling of prognostic liver signature in clinical fibrotic liver tissues cultured with various anti-fibrotic and chemopreventive agents\"\n", "!Series_summary\t\"Background/Aims: There is a major unmet need to assess prognostic impact of anti-fibrotics in clinical trials due to the slow rate of liver fibrosis progression. We aimed to develop a surrogate biomarker to predict future fibrosis progression.\"\n", "!Series_summary\t\"Methods: A Fibrosis Progression Signature (FPS) was defined to predict fibrosis progression within 5 years in HCV and NAFLD patients with no to minimal fibrosis at baseline (n=421), and validated in an independent NAFLD cohort (n=78). The FPS was used to assess response to 13 candidate anti-fibrotics in organotypic ex vivo cultures of clinical fibrotic liver tissues (n=78), and cenicriviroc in NASH patients enrolled in a clinical trial (n=19, NCT02217475). A serum-protein-based surrogate FPS (FPSec) was developed and technically evaluated in a liver disease patient cohort (n=79).\"\n", "!Series_summary\t\"Results: A 20-gene FPS was defined and validated in an independent NAFLD cohort (aOR=10.93, AUROC=0.86). Among computationally inferred fibrosis-driving FPS genes, BCL2 was confirmed as a potential pharmacological target using clinical liver tissues. Systematic ex vivo evaluation of 13 candidate anti-fibrotics identified rational combination therapies based on epigallocatechin gallate, some of which were validated for enhanced anti-fibrotic effect in ex vivo culture of clinical liver tissues. In NASH patients treated with cenicriviroc, FPS modulation was associated with 1-year fibrosis improvement accompanied by suppression of the E2F pathway. Induction of PPAR-alfa pathway was absent in patients without fibrosis improvement, suggesting benefit of combining PPAR-alfa agonism to improve anti-fibrotic efficacy of cenicriviroc. A 7-protein FPSec panel showed concordant prognostic prediction with FPS.\"\n", "!Series_summary\t\"Conclusion: FPS predicts long-term fibrosis progression in an etiology-agnostic manner, which can inform anti-fibrotic drug development.\"\n", "!Series_overall_design\t\"Gene expression profiling of snap-frozen surgical liver tissues treated with various anti-fibrotic and chemopreventive agents in ex vivo precision-cut liver slice (PCLS) culture. The samples in the FPS validation set 2.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['tissue: Liver'], 1: ['sample group: Compound treatment', 'sample group: Baseline (before culture)', 'sample group: Vehicle control'], 2: ['compound: Galunisertib', 'compound: Erlotinib', 'compound: AM095', 'compound: MG132', 'compound: Bortezomib', 'compound: Cenicriviroc', 'compound: Pioglitazone', 'compound: Metformin', 'compound: EGCG', 'compound: I-BET 151', 'compound: JQ1', 'compound: Captopril', 'compound: Nizatidine', 'compound: none', 'compound: DMSO'], 3: ['concentration: 10microM', 'concentration: 5microM', 'concentration: 3microM', 'concentration: 20microM', 'concentration: 100microM', 'concentration: 30microM', 'concentration: na', 'concentration: 0.1%']}\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": "88280a36", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "b2879e07", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:29.062380Z", "iopub.status.busy": "2025-03-25T07:33:29.062281Z", "iopub.status.idle": "2025-03-25T07:33:29.067009Z", "shell.execute_reply": "2025-03-25T07:33:29.066743Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the background information and series title, this dataset appears to contain gene expression data\n", "# The summary mentions a 20-gene Fibrosis Progression Signature (FPS) and gene expression profiling\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "\n", "# For trait (Liver cirrhosis):\n", "# The background information indicates this is a study on liver fibrosis, but there's no explicit cirrhosis indicator\n", "# in the sample characteristics. However, it's a study about fibrotic liver tissues, so all samples have some level\n", "# of fibrosis but we can't distinguish cirrhosis specifically.\n", "trait_row = None # Cannot determine cirrhosis status from the available data\n", "\n", "# For age:\n", "# No age information is provided in the sample characteristics\n", "age_row = None\n", "\n", "# For gender:\n", "# No gender information is provided in the sample characteristics\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion Functions\n", "# Since trait_row is None, we don't need to define convert_trait, but we'll create a placeholder function\n", "def convert_trait(value):\n", " # This function won't be used since trait_row is None\n", " return None\n", "\n", "def convert_age(value):\n", " # This function won't be used since age_row is None\n", " return None\n", "\n", "def convert_gender(value):\n", " # This function won't be used since gender_row is None\n", " return None\n", "\n", "# 3. Save Metadata\n", "# The trait_row is None, meaning trait data is not available\n", "is_trait_available = False if trait_row is None else True\n", "\n", "# Initial filtering on usability\n", "validate_and_save_cohort_info(\n", " is_final=False,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=is_gene_available,\n", " is_trait_available=is_trait_available\n", ")\n", "\n", "# 4. Clinical Feature Extraction\n", "# Since trait_row is None, we should skip this substep\n" ] }, { "cell_type": "markdown", "id": "5ef18fbd", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "320263bc", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:29.067972Z", "iopub.status.busy": "2025-03-25T07:33:29.067874Z", "iopub.status.idle": "2025-03-25T07:33:29.094363Z", "shell.execute_reply": "2025-03-25T07:33:29.094067Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix file found: ../../input/GEO/Liver_cirrhosis/GSE182065/GSE182065_series_matrix.txt.gz\n", "Gene data shape: (192, 293)\n", "First 20 gene/probe identifiers:\n", "Index(['AARS', 'ABLIM1', 'ACOT2', 'ACSM3', 'ACTR2', 'ADD3', 'ADH5', 'ADH6',\n", " 'ADRA2B', 'AEBP1', 'AKAP13', 'AKR1A1', 'AKR1D1', 'ALAS1', 'ALDH9A1',\n", " 'ANKRD46', 'ANXA1', 'ANXA3', 'AOX1', 'AP1B1'],\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": "2bb980f4", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "266bc878", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:29.095309Z", "iopub.status.busy": "2025-03-25T07:33:29.095208Z", "iopub.status.idle": "2025-03-25T07:33:29.096945Z", "shell.execute_reply": "2025-03-25T07:33:29.096642Z" } }, "outputs": [], "source": [ "# Examining the gene identifiers in the data\n", "# Looking at the first 20 gene identifiers: 'AARS', 'ABLIM1', 'ACOT2', etc.\n", "# These appear to be standard human gene symbols\n", "# No mapping to gene symbols is required as they're already in that format\n", "\n", "requires_gene_mapping = False\n" ] }, { "cell_type": "markdown", "id": "6fd322e5", "metadata": {}, "source": [ "### Step 5: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 6, "id": "60fdea07", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:29.097921Z", "iopub.status.busy": "2025-03-25T07:33:29.097817Z", "iopub.status.idle": "2025-03-25T07:33:29.218096Z", "shell.execute_reply": "2025-03-25T07:33:29.217721Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape before normalization: (192, 293)\n", "Gene data shape after normalization: (191, 293)\n", "Normalized gene expression data saved to ../../output/preprocess/Liver_cirrhosis/gene_data/GSE182065.csv\n", "No clinical data available for this dataset, skipping clinical data processing.\n", "Abnormality detected in the cohort: GSE182065. Preprocessing failed.\n", "Dataset is not usable for liver cirrhosis analysis due to lack of clinical data. No linked data file saved.\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 }