{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "f0818ef1", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:48:52.284240Z", "iopub.status.busy": "2025-03-25T05:48:52.284129Z", "iopub.status.idle": "2025-03-25T05:48:52.446164Z", "shell.execute_reply": "2025-03-25T05:48:52.445767Z" } }, "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 = \"Hypertension\"\n", "cohort = \"GSE151158\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Hypertension\"\n", "in_cohort_dir = \"../../input/GEO/Hypertension/GSE151158\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Hypertension/GSE151158.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Hypertension/gene_data/GSE151158.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Hypertension/clinical_data/GSE151158.csv\"\n", "json_path = \"../../output/preprocess/Hypertension/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "08e07ff1", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "14329eb7", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:48:52.447658Z", "iopub.status.busy": "2025-03-25T05:48:52.447506Z", "iopub.status.idle": "2025-03-25T05:48:52.470081Z", "shell.execute_reply": "2025-03-25T05:48:52.469747Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Transcriptional analysis of non-fibrotic NAFLD progression\"\n", "!Series_summary\t\"Background & Aims: Non-alcoholic steatohepatitis (NASH), a subtype of non-alcoholic fatty liver disease (NAFLD) that can lead to fibrosis, cirrhosis, and hepatocellular carcinoma, is characterized by hepatic inflammation. Despite evolving therapies aimed to ameliorate inflammation in NASH, the transcriptional changes that lead to inflammation progression in NAFLD remain poorly understood. The aim of this study is to define transcriptional changes in early, non-fibrotic NAFLD using a biopsy-proven non-fibrotic NAFLD cohort. Methods: We extracted RNA from liver tissue of 40 patients with biopsy-proven NAFLD based on NAFLD Activity Score (NAS) (23 with NAS ≤3, 17 with NAS ≥5) and 21 healthy controls and compared changes in expression of 594 genes involved in innate immune function. Results: Compared to healthy controls, NAFLD patients with NAS ≥5 had differential expression of 211 genes, while those with NAS ≤3 had differential expression of only 14 genes. Notably, osteopontin (SPP1) (3.74-fold in NAS ≤3, 8.28-fold in NAS ≥5) and CXCL10 (2.27-fold in NAS ≤3, 8.28-fold in NAS ≥5) gene expression were significantly upregulated with histologic progression of NAFLD.\"\n", "!Series_overall_design\t\"We extracted RNA from liver tissue of 40 patients with biopsy-proven NAFLD based on NAFLD Activity Score (NAS) (23 with NAS ≤3, 17 with NAS ≥5) and 21 healthy controls (protocol biopsy obtained during living liver donation) and compared changes in expression of 594 genes involved in innate immune function\"\n", "Sample Characteristics Dictionary:\n", "{0: ['tissue: liver', 'sample type: blank'], 1: ['age: 53', 'age: 40', 'age: 51', 'age: 36', 'age: 44', 'age: 60', 'age: 31', 'age: 41', 'age: 55', 'age: 15', 'age: 57', 'age: 56', 'age: 34', 'age: 43', 'age: 49', 'age: 52', 'age: 35', 'age: 42', 'age: 33', 'age: 48', 'age: 47', 'age: 65', 'age: 59', 'age: 61', 'age: 28', 'age: 46', 'age: 25', 'age: 27', 'age: 54', 'age: 37'], 2: ['Sex: F', 'Sex: M', nan], 3: ['ethnicity: White', 'ethnicity: Hispanic', 'ethnicity: AA', nan], 4: ['bmi: 28.4', 'bmi: 37.8', 'bmi: 33.1', 'bmi: 39.6', 'bmi: 31.5', 'bmi: 29.9', 'bmi: 39.9', 'bmi: 33.3', 'bmi: 41.1', 'bmi: 62.9', 'bmi: 47.6', 'bmi: 31.7', 'bmi: 53.4', 'bmi: 31.4', 'bmi: 23.9', 'bmi: 22.4', 'bmi: 23.7', 'bmi: 28', 'bmi: 27.8', 'bmi: 37.7', 'bmi: 36.1', 'bmi: 36.7', 'bmi: 39.4', 'bmi: 36.8', 'bmi: 29.2', 'bmi: 35.2', 'bmi: 38.4', 'bmi: 30.8', 'bmi: 29', 'bmi: 47.8'], 5: ['dm2: N', 'dm2: Y', nan], 6: ['insulin: N', 'insulin: Y', nan], 7: ['hypertension: N', 'hypertension: Y', nan], 8: ['hyperlipidemia: N', 'hyperlipidemia: Y', nan], 9: ['statin/fibrate: N', 'statin/fibrate: Y', nan], 10: ['osa: N', 'osa: Y', nan], 11: ['pcos: N', nan], 12: ['hypothyroidism: N', 'hypothyroidism: Y', nan], 13: ['cardiovascular disease: N', 'cardiovascular disease: Y', nan], 14: ['ast (units/l): 77', 'ast (units/l): 66', 'ast (units/l): 64', 'ast (units/l): 68', 'ast (units/l): 21', 'ast (units/l): 51', 'ast (units/l): 174', 'ast (units/l): 58', 'ast (units/l): 45', 'ast (units/l): 19', 'ast (units/l): 41', 'ast (units/l): 24', 'ast (units/l): 49', 'ast (units/l): 26', 'ast (units/l): 16', 'ast (units/l): 15', 'ast (units/l): 17', 'ast (units/l): 20', 'ast (units/l): 27', 'ast (units/l): 305', 'ast (units/l): 43', 'ast (units/l): 75', 'ast (units/l): 67', 'ast (units/l): 118', 'ast (units/l): 69', 'ast (units/l): 59', 'ast (units/l): 31', 'ast (units/l): 18', 'ast (units/l): 33', 'ast (units/l): 37'], 15: ['alt (units/l): 129', 'alt (units/l): 123', 'alt (units/l): 84', 'alt (units/l): 120', 'alt (units/l): 28', 'alt (units/l): 88', 'alt (units/l): 429', 'alt (units/l): 66', 'alt (units/l): 26', 'alt (units/l): 40', 'alt (units/l): 46', 'alt (units/l): 94', 'alt (units/l): 72', 'alt (units/l): 17', 'alt (units/l): 12', 'alt (units/l): 27', 'alt (units/l): 3', 'alt (units/l): 16', 'alt (units/l): 70', 'alt (units/l): 301', 'alt (units/l): 6', 'alt (units/l): 102', 'alt (units/l): 97', 'alt (units/l): 110', 'alt (units/l): 89', 'alt (units/l): 44', 'alt (units/l): 42', 'alt (units/l): 33', 'alt (units/l): 52', 'alt (units/l): 31'], 16: ['alkaline phosphatase (units/l): 114', 'alkaline phosphatase (units/l): 60', 'alkaline phosphatase (units/l): 91', 'alkaline phosphatase (units/l): 130', 'alkaline phosphatase (units/l): 120', 'alkaline phosphatase (units/l): 58', 'alkaline phosphatase (units/l): 78', 'alkaline phosphatase (units/l): 83', 'alkaline phosphatase (units/l): 89', 'alkaline phosphatase (units/l): 95', 'alkaline phosphatase (units/l): 150', 'alkaline phosphatase (units/l): 131', 'alkaline phosphatase (units/l): 52', 'alkaline phosphatase (units/l): 72', 'alkaline phosphatase (units/l): 65', 'alkaline phosphatase (units/l): 94', 'alkaline phosphatase (units/l): 62', 'alkaline phosphatase (units/l): 105', 'alkaline phosphatase (units/l): 71', 'alkaline phosphatase (units/l): 76', 'alkaline phosphatase (units/l): 74', 'alkaline phosphatase (units/l): 90', 'nas: Steatosis: 2', 'alkaline phosphatase (units/l): 117', 'alkaline phosphatase (units/l): 48', 'alkaline phosphatase (units/l): 41', 'alkaline phosphatase (units/l): 93', 'alkaline phosphatase (units/l): 46', 'alkaline phosphatase (units/l): 67', 'alkaline phosphatase (units/l): 66'], 17: ['total bilirubin (mg/dl): 0.4', 'total bilirubin (mg/dl): 0.7', 'total bilirubin (mg/dl): 1.1', 'total bilirubin (mg/dl): 0.6', 'total bilirubin (mg/dl): 0.5', 'total bilirubin (mg/dl): 1', 'total bilirubin (mg/dl): 0.3', 'total bilirubin (mg/dl): 0.8', 'total bilirubin (mg/dl): 1.4', 'total bilirubin (mg/dl): 1.5', 'nas: Ballooning: 2', 'total bilirubin (mg/dl): 0.2', 'total bilirubin (mg/dl): 0.9', nan], 18: ['albumin (g/dl): 4.3', 'albumin (g/dl): 4.4', 'albumin (g/dl): 4.2', 'albumin (g/dl): 4.7', 'albumin (g/dl): 4', 'albumin (g/dl): 5.2', 'albumin (g/dl): 4.1', 'albumin (g/dl): 4.5', 'albumin (g/dl): 3.5', 'albumin (g/dl): 3.6', 'albumin (g/dl): 3.8', 'nas: Lobular inflammation: 1', 'albumin (g/dl): 3.9', 'albumin (g/dl): 3.7', 'albumin (g/dl): 3.2', 'albumin (g/dl): 4.9', 'albumin (g/dl): 4.6', nan], 19: ['total protein (g/dl): 8.2', 'total protein (g/dl): 7.7', 'total protein (g/dl): 7.2', 'total protein (g/dl): 8', 'total protein (g/dl): 7.6', 'total protein (g/dl): 8.7', 'total protein (g/dl): 7.1', 'total protein (g/dl): 7.9', 'total protein (g/dl): 6.8', 'total protein (g/dl): 6.5', 'total protein (g/dl): 7', 'total protein (g/dl): 7.3', 'total protein (g/dl): 6.6', 'total protein (g/dl): 7.5', 'nas: Total score: 5', 'total protein (g/dl): 7.8', 'total protein (g/dl): 7.4', 'total protein (g/dl): 6.9', 'total protein (g/dl): 8.1', 'total protein (g/dl): 8.4', 'total protein (g/dl): 6.3', 'total protein (g/dl): 6.7', nan], 20: ['nas: Steatosis: 1', 'nas: Steatosis: 2', 'nas: Steatosis: 3', 'nas: Steatosis: 0', nan], 21: ['nas: Ballooning: 2', 'nas: Ballooning: 1', 'nas: Ballooning: 0', nan], 22: ['nas: Lobular inflammation: 2', 'nas: Lobular inflammation: 1', 'nas: Lobular inflammation: 0', 'nas: Lobular inflammation: 3', nan], 23: ['nas: Total score: 5', 'nas: Total score: 6', 'nas: Total score: 3', 'nas: Total score: 0', nan, 'nas: Total score: 2']}\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": "a32e219d", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "3ba927ae", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:48:52.471181Z", "iopub.status.busy": "2025-03-25T05:48:52.471065Z", "iopub.status.idle": "2025-03-25T05:48:52.476471Z", "shell.execute_reply": "2025-03-25T05:48:52.476137Z" } }, "outputs": [], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the background information, this dataset contains gene expression data for liver tissue\n", "# from patients with NAFLD and healthy controls.\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "\n", "# For hypertension (trait), row 7 contains hypertension data\n", "trait_row = 7\n", "\n", "# For age, row 1 contains age data\n", "age_row = 1\n", "\n", "# For gender, row 2 contains sex data\n", "gender_row = 2\n", "\n", "# 2.2 Data Type Conversion\n", "\n", "def convert_trait(value):\n", " \"\"\"Convert hypertension data to binary (0=N, 1=Y).\"\"\"\n", " if pd.isna(value):\n", " return None\n", " # Extract value after colon\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " if value.upper() == 'Y':\n", " return 1\n", " elif value.upper() == 'N':\n", " return 0\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age to continuous numeric value.\"\"\"\n", " if pd.isna(value):\n", " return None\n", " # Extract value after colon\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " try:\n", " return float(value)\n", " except:\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender to binary (0=F, 1=M).\"\"\"\n", " if pd.isna(value):\n", " return None\n", " # Extract value after colon\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " if value.upper() == 'F':\n", " return 0\n", " elif value.upper() == 'M':\n", " return 1\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Initial filtering on the usability of the dataset based on gene and trait availability\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", "# Since trait_row is not None, we need to extract clinical features\n", "if trait_row is not None:\n", " # Find the clinical data file(s) in the cohort directory\n", " import glob\n", " import os\n", " \n", " clinical_files = glob.glob(os.path.join(in_cohort_dir, \"*_clinical.csv\"))\n", " if clinical_files:\n", " clinical_data = pd.read_csv(clinical_files[0])\n", " \n", " # Extract clinical features using the function from tools.preprocess\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 extracted clinical features\n", " preview = preview_df(selected_clinical_df)\n", " print(\"Clinical features preview:\", preview)\n", " \n", " # Save the extracted clinical features 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, index=False)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "395aac8a", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "18b2e86d", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:48:52.477504Z", "iopub.status.busy": "2025-03-25T05:48:52.477388Z", "iopub.status.idle": "2025-03-25T05:48:52.490573Z", "shell.execute_reply": "2025-03-25T05:48:52.490259Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['ABCB1', 'ABCF1', 'ABL1', 'ADA', 'AHR', 'AICDA', 'AIRE', 'ALAS1', 'APP',\n", " 'AREG', 'ARG1', 'ARG2', 'ARHGDIB', 'ATG10', 'ATG12', 'ATG16L1', 'ATG5',\n", " 'ATG7', 'ATM', 'B2M'],\n", " dtype='object', name='ID')\n" ] } ], "source": [ "# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.\n", "gene_data = get_genetic_data(matrix_file)\n", "\n", "# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.\n", "print(gene_data.index[:20])\n" ] }, { "cell_type": "markdown", "id": "ecea1e13", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "46cc4b45", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:48:52.491638Z", "iopub.status.busy": "2025-03-25T05:48:52.491524Z", "iopub.status.idle": "2025-03-25T05:48:52.493419Z", "shell.execute_reply": "2025-03-25T05:48:52.493099Z" } }, "outputs": [], "source": [ "# Based on the gene identifiers shown in the output, these appear to be standard human gene symbols.\n", "# For example:\n", "# - ABCB1 is the gene encoding ATP Binding Cassette Subfamily B Member 1\n", "# - ABL1 is the ABL Proto-Oncogene 1, Non-Receptor Tyrosine Kinase\n", "# - B2M is Beta-2-Microglobulin\n", "# - ATM is Ataxia Telangiectasia Mutated\n", "\n", "# These are already in the preferred format of human gene symbols, so no mapping is needed.\n", "\n", "requires_gene_mapping = False\n" ] }, { "cell_type": "markdown", "id": "cdd92a60", "metadata": {}, "source": [ "### Step 5: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 6, "id": "9596ddfc", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:48:52.494479Z", "iopub.status.busy": "2025-03-25T05:48:52.494370Z", "iopub.status.idle": "2025-03-25T05:48:52.721723Z", "shell.execute_reply": "2025-03-25T05:48:52.721358Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Hypertension/gene_data/GSE151158.csv\n", "Clinical data saved to ../../output/preprocess/Hypertension/clinical_data/GSE151158.csv\n", "Shape of linked data before missing value handling: (66, 586)\n", "Shape of linked data after missing value handling: (61, 586)\n", "For the feature 'Hypertension', the least common label is '1.0' with 20 occurrences. This represents 32.79% of the dataset.\n", "The distribution of the feature 'Hypertension' in this dataset is fine.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Quartiles for 'Age':\n", " 25%: 37.0\n", " 50% (Median): 45.0\n", " 75%: 52.0\n", "Min: 15.0\n", "Max: 65.0\n", "The distribution of the feature 'Age' in this dataset is fine.\n", "\n", "For the feature 'Gender', the least common label is '1.0' with 25 occurrences. This represents 40.98% of the dataset.\n", "The distribution of the feature 'Gender' in this dataset is fine.\n", "\n", "Linked data saved to ../../output/preprocess/Hypertension/GSE151158.csv\n" ] } ], "source": [ "# 1. Normalize the obtained gene data with the 'normalize_gene_symbols_in_index' function from the library.\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\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. Extract clinical features using the function from tools.preprocess\n", "# Use the parameters defined in STEP 2 for the trait, age, and gender rows\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", "# Save the extracted clinical features 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", "# 3. Link the clinical and genetic data\n", "linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)\n", "print(f\"Shape of linked data before missing value handling: {linked_data.shape}\")\n", "\n", "# 4. Handle missing values systematically\n", "linked_data = handle_missing_values(linked_data, trait)\n", "print(f\"Shape of linked data after missing value handling: {linked_data.shape}\")\n", "\n", "# 5. Check if the trait and demographic features are severely biased\n", "is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "\n", "# 6. Validate and save cohort information\n", "note = \"Dataset from NAFLD study with hypertension trait information available.\"\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=note\n", ")\n", "\n", "# 7. 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(\"Dataset is not usable for trait-gene association studies due to quality 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 }