{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "29ae5890", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:26.170369Z", "iopub.status.busy": "2025-03-25T07:33:26.170193Z", "iopub.status.idle": "2025-03-25T07:33:26.334390Z", "shell.execute_reply": "2025-03-25T07:33:26.334022Z" } }, "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 = \"GSE163211\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Liver_cirrhosis\"\n", "in_cohort_dir = \"../../input/GEO/Liver_cirrhosis/GSE163211\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Liver_cirrhosis/GSE163211.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Liver_cirrhosis/gene_data/GSE163211.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Liver_cirrhosis/clinical_data/GSE163211.csv\"\n", "json_path = \"../../output/preprocess/Liver_cirrhosis/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "cfec598c", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "282c5e6a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:26.335836Z", "iopub.status.busy": "2025-03-25T07:33:26.335692Z", "iopub.status.idle": "2025-03-25T07:33:26.362180Z", "shell.execute_reply": "2025-03-25T07:33:26.361856Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Distinct hepatic gene expression patterns characterize progressive disease in NAFLD\"\n", "!Series_summary\t\"The pathogenesis of non-alcoholic fatty liver disease is not fully understood. Transcriptomic analysis of a large cohort of 318 patients provides evidence of gene perturbations related to inflammation, complement and coagulation pathways, and tissue remodeling in distinct states of NAFLD.\"\n", "!Series_overall_design\t\"Using the Nanostring nCounter assay, we quantified expression of 795 genes (and 5 housekeeping genes) hypothesized to be involved in hepatic fibrosis, inflammation, and steatosis in liver tissue from 318 adults. Liver specimens were categorized into 4 distinct NAFLD phenotypes: normal liver histology (NLH), steatosis only (steatosis), non-alcoholic steatohepatitis without fibrosis (NASH F0), and NASH with fibrosis stage 1-4 (NASH F1-F4).\"\n", "Sample Characteristics Dictionary:\n", "{0: ['tissue: Human liver'], 1: ['race: Black or African American', 'race: White', 'race: Refused', 'race: Other', 'race: Unknown'], 2: ['bmi: 45.77', 'bmi: 42.8', 'bmi: 40.44', 'bmi: 58.48', 'bmi: 41.91', 'bmi: 41.81', 'bmi: 36.11', 'bmi: 54.25', 'bmi: 50.34', 'bmi: 36.44', 'bmi: 42.4', 'bmi: 51.3', 'bmi: 53.2', 'bmi: 35.86', 'bmi: 40.65', 'bmi: 48.43', 'bmi: 43.42', 'bmi: 59.53', 'bmi: 37.51', 'bmi: 44.2', 'bmi: 38.79', 'bmi: 51.56', 'bmi: 51.91', 'bmi: 45.38', 'bmi: 51.87', 'bmi: 48.53', 'bmi: 43.98', 'bmi: 39.22', 'bmi: 54', 'bmi: 42.47'], 3: ['age: 35', 'age: 33', 'age: 55', 'age: 49', 'age: 27', 'age: 44', 'age: 58', 'age: 53', 'age: 46', 'age: 61', 'age: 29', 'age: 19', 'age: 64', 'age: 16', 'age: 25', 'age: 72', 'age: 24', 'age: 60', 'age: 70', 'age: 32', 'age: 17', 'age: 56', 'age: 34', 'age: 45', 'age: 30', 'age: 39', 'age: 21', 'age: 31', 'age: 37', 'age: 48'], 4: ['Sex: Female', 'Sex: Male'], 5: ['diabetes: No', 'diabetes: Yes'], 6: ['alt: 37', 'alt: 16', 'alt: 19', 'alt: 11', 'alt: 13', 'alt: 23', 'alt: 8', 'alt: 35', 'alt: 22', 'alt: 85', 'alt: 32', 'alt: 17', 'alt: 26', 'alt: 25', 'alt: 57', 'alt: 34', 'alt: 43', 'alt: 33', 'alt: 38', 'alt: 42', 'alt: 39', 'alt: 41', 'alt: 27', 'alt: 59', 'alt: 10', 'alt: 54', 'alt: 36', 'alt: 48', 'alt: 29', 'alt: 44'], 7: ['ast: 32', 'ast: 18', 'ast: 22', 'ast: 17', 'ast: 56', 'ast: 16', 'ast: 33', 'ast: 35', 'ast: 23', 'ast: 21', 'ast: 55', 'ast: 28', 'ast: 25', 'ast: 38', 'ast: 29', 'ast: 26', 'ast: 60', 'ast: 27', 'ast: 31', 'ast: 62', 'ast: 52', 'ast: 39', 'ast: 20', 'ast: 34', 'ast: 65', 'ast: 30', 'ast: 42', 'ast: 12', 'ast: 19', 'ast: 9'], 8: ['nafld stage: Normal', 'nafld stage: Steatosis', 'nafld stage: NASH_F1_F4', 'nafld stage: NASH_F0']}\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": "bb32d834", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "466a90cb", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:26.363276Z", "iopub.status.busy": "2025-03-25T07:33:26.363174Z", "iopub.status.idle": "2025-03-25T07:33:26.392388Z", "shell.execute_reply": "2025-03-25T07:33:26.392076Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of extracted clinical features:\n", "{'GSM4974854': [0.0, 35.0, 0.0], 'GSM4974855': [0.0, 33.0, 0.0], 'GSM4974856': [0.0, 55.0, 0.0], 'GSM4974857': [0.0, 49.0, 0.0], 'GSM4974858': [0.0, 27.0, 0.0], 'GSM4974859': [0.0, 44.0, 0.0], 'GSM4974860': [0.0, 58.0, 0.0], 'GSM4974861': [0.0, 53.0, 0.0], 'GSM4974862': [0.0, 46.0, 1.0], 'GSM4974863': [0.0, 61.0, 0.0], 'GSM4974864': [0.0, 29.0, 0.0], 'GSM4974865': [0.0, 19.0, 1.0], 'GSM4974866': [1.0, 33.0, 1.0], 'GSM4974867': [0.0, 64.0, 1.0], 'GSM4974868': [0.0, 16.0, 0.0], 'GSM4974869': [0.0, 25.0, 0.0], 'GSM4974870': [0.0, 72.0, 0.0], 'GSM4974871': [0.0, 24.0, 0.0], 'GSM4974872': [1.0, 60.0, 0.0], 'GSM4974873': [1.0, 58.0, 0.0], 'GSM4974874': [1.0, 70.0, 1.0], 'GSM4974875': [1.0, 33.0, 0.0], 'GSM4974876': [1.0, 44.0, 1.0], 'GSM4974877': [0.0, 29.0, 1.0], 'GSM4974878': [0.0, 46.0, 1.0], 'GSM4974879': [0.0, 58.0, 0.0], 'GSM4974880': [1.0, 16.0, 0.0], 'GSM4974881': [1.0, 32.0, 1.0], 'GSM4974882': [1.0, 17.0, 0.0], 'GSM4974883': [0.0, 44.0, 0.0], 'GSM4974884': [1.0, 56.0, 1.0], 'GSM4974885': [0.0, 34.0, 1.0], 'GSM4974886': [1.0, 61.0, 0.0], 'GSM4974887': [0.0, 34.0, 1.0], 'GSM4974888': [1.0, 45.0, 0.0], 'GSM4974889': [0.0, 30.0, 0.0], 'GSM4974890': [0.0, 58.0, 0.0], 'GSM4974891': [0.0, 39.0, 0.0], 'GSM4974892': [0.0, 25.0, 0.0], 'GSM4974893': [0.0, 21.0, 0.0], 'GSM4974894': [1.0, 31.0, 0.0], 'GSM4974895': [0.0, 49.0, 1.0], 'GSM4974896': [1.0, 37.0, 1.0], 'GSM4974897': [1.0, 48.0, 0.0], 'GSM4974898': [1.0, 35.0, 1.0], 'GSM4974899': [1.0, 56.0, 0.0], 'GSM4974900': [1.0, 49.0, 0.0], 'GSM4974901': [1.0, 46.0, 0.0], 'GSM4974902': [1.0, 29.0, 0.0], 'GSM4974903': [1.0, 35.0, 0.0], 'GSM4974904': [1.0, 36.0, 0.0], 'GSM4974905': [0.0, 18.0, 0.0], 'GSM4974906': [0.0, 60.0, 0.0], 'GSM4974907': [0.0, 63.0, 0.0], 'GSM4974908': [0.0, 39.0, 0.0], 'GSM4974909': [0.0, 53.0, 0.0], 'GSM4974910': [1.0, 35.0, 1.0], 'GSM4974911': [0.0, 62.0, 0.0], 'GSM4974912': [1.0, 28.0, 0.0], 'GSM4974913': [1.0, 60.0, 1.0], 'GSM4974914': [1.0, 44.0, 0.0], 'GSM4974915': [0.0, 41.0, 0.0], 'GSM4974916': [1.0, 50.0, 0.0], 'GSM4974917': [0.0, 40.0, 1.0], 'GSM4974918': [0.0, 51.0, 0.0], 'GSM4974919': [0.0, 56.0, 1.0], 'GSM4974920': [0.0, 50.0, 0.0], 'GSM4974921': [0.0, 34.0, 0.0], 'GSM4974922': [0.0, 26.0, 0.0], 'GSM4974923': [0.0, 51.0, 0.0], 'GSM4974924': [0.0, 33.0, 1.0], 'GSM4974925': [0.0, 41.0, 0.0], 'GSM4974926': [0.0, 47.0, 0.0], 'GSM4974927': [0.0, 32.0, 0.0], 'GSM4974928': [1.0, 49.0, 1.0], 'GSM4974929': [1.0, 38.0, 0.0], 'GSM4974930': [0.0, 56.0, 0.0], 'GSM4974931': [1.0, 43.0, 0.0], 'GSM4974932': [0.0, 61.0, 1.0], 'GSM4974933': [0.0, 51.0, 0.0], 'GSM4974934': [0.0, 27.0, 0.0], 'GSM4974935': [0.0, 37.0, 0.0], 'GSM4974936': [0.0, 57.0, 0.0], 'GSM4974937': [0.0, 27.0, 0.0], 'GSM4974938': [0.0, 56.0, 1.0], 'GSM4974939': [0.0, 44.0, 0.0], 'GSM4974940': [0.0, 54.0, 1.0], 'GSM4974941': [1.0, 36.0, 1.0], 'GSM4974942': [0.0, 53.0, 0.0], 'GSM4974943': [0.0, 59.0, 0.0], 'GSM4974944': [0.0, 29.0, 0.0], 'GSM4974945': [0.0, 43.0, 0.0], 'GSM4974946': [0.0, 46.0, 0.0], 'GSM4974947': [0.0, 26.0, 1.0], 'GSM4974948': [0.0, 60.0, 1.0], 'GSM4974949': [0.0, 58.0, 0.0], 'GSM4974950': [0.0, 59.0, 0.0], 'GSM4974951': [1.0, 60.0, 0.0], 'GSM4974952': [1.0, 59.0, 1.0], 'GSM4974953': [1.0, 51.0, 0.0], 'GSM4974954': [0.0, 35.0, 0.0], 'GSM4974955': [0.0, 32.0, 0.0], 'GSM4974956': [0.0, 43.0, 0.0], 'GSM4974957': [0.0, 52.0, 1.0], 'GSM4974958': [1.0, 38.0, 0.0], 'GSM4974959': [1.0, 47.0, 0.0], 'GSM4974960': [0.0, 40.0, 0.0], 'GSM4974961': [0.0, 47.0, 0.0], 'GSM4974962': [0.0, 43.0, 1.0], 'GSM4974963': [1.0, 41.0, 0.0], 'GSM4974964': [0.0, 26.0, 0.0], 'GSM4974965': [0.0, 26.0, 0.0], 'GSM4974966': [1.0, 51.0, 0.0], 'GSM4974967': [1.0, 44.0, 0.0], 'GSM4974968': [0.0, 38.0, 0.0], 'GSM4974969': [0.0, 57.0, 0.0], 'GSM4974970': [0.0, 51.0, 1.0], 'GSM4974971': [0.0, 32.0, 0.0], 'GSM4974972': [0.0, 41.0, 0.0], 'GSM4974973': [1.0, 53.0, 0.0], 'GSM4974974': [0.0, 27.0, 0.0], 'GSM4974975': [0.0, 58.0, 0.0], 'GSM4974976': [0.0, 50.0, 0.0], 'GSM4974977': [0.0, 58.0, 0.0], 'GSM4974978': [0.0, 36.0, 0.0], 'GSM4974979': [0.0, 50.0, 0.0], 'GSM4974980': [1.0, 43.0, 1.0], 'GSM4974981': [0.0, 38.0, 0.0], 'GSM4974982': [0.0, 47.0, 0.0], 'GSM4974983': [1.0, 50.0, 0.0], 'GSM4974984': [1.0, 60.0, 0.0], 'GSM4974985': [0.0, 41.0, 0.0], 'GSM4974986': [0.0, 33.0, 1.0], 'GSM4974987': [0.0, 37.0, 0.0], 'GSM4974988': [0.0, 34.0, 0.0], 'GSM4974989': [0.0, 42.0, 0.0], 'GSM4974990': [1.0, 56.0, 0.0], 'GSM4974991': [0.0, 43.0, 0.0], 'GSM4974992': [0.0, 38.0, 0.0], 'GSM4974993': [0.0, 60.0, 0.0], 'GSM4974994': [0.0, 54.0, 0.0], 'GSM4974995': [0.0, 49.0, 0.0], 'GSM4974996': [0.0, 53.0, 0.0], 'GSM4974997': [1.0, 46.0, 0.0], 'GSM4974998': [1.0, 48.0, 0.0], 'GSM4974999': [1.0, 74.0, 0.0], 'GSM4975000': [1.0, 41.0, 0.0], 'GSM4975001': [1.0, 57.0, 0.0], 'GSM4975002': [0.0, 31.0, 0.0], 'GSM4975003': [1.0, 54.0, 1.0], 'GSM4975004': [0.0, 23.0, 0.0], 'GSM4975005': [1.0, 49.0, 0.0], 'GSM4975006': [0.0, 62.0, 0.0], 'GSM4975007': [0.0, 35.0, 0.0], 'GSM4975008': [0.0, 62.0, 0.0], 'GSM4975009': [0.0, 46.0, 1.0], 'GSM4975010': [1.0, 40.0, 0.0], 'GSM4975011': [1.0, 62.0, 1.0], 'GSM4975012': [0.0, 56.0, 1.0], 'GSM4975013': [0.0, 35.0, 0.0], 'GSM4975014': [1.0, 48.0, 0.0], 'GSM4975015': [0.0, 60.0, 0.0], 'GSM4975016': [0.0, 41.0, 0.0], 'GSM4975017': [0.0, 43.0, 0.0], 'GSM4975018': [0.0, 47.0, 0.0], 'GSM4975019': [1.0, 57.0, 0.0], 'GSM4975020': [0.0, 36.0, 0.0], 'GSM4975021': [0.0, 17.0, 0.0], 'GSM4975022': [0.0, 29.0, 0.0], 'GSM4975023': [0.0, 36.0, 0.0], 'GSM4975024': [0.0, 39.0, 1.0], 'GSM4975025': [1.0, 41.0, 0.0], 'GSM4975026': [1.0, 30.0, 0.0], 'GSM4975027': [0.0, 43.0, 0.0], 'GSM4975028': [1.0, 31.0, 0.0], 'GSM4975029': [0.0, 53.0, 0.0], 'GSM4975030': [0.0, 41.0, 0.0], 'GSM4975031': [1.0, 35.0, 1.0], 'GSM4975032': [0.0, 65.0, 1.0], 'GSM4975033': [0.0, 49.0, 1.0], 'GSM4975034': [0.0, 51.0, 0.0], 'GSM4975035': [0.0, 48.0, 0.0], 'GSM4975036': [0.0, 32.0, 0.0], 'GSM4975037': [0.0, 38.0, 0.0], 'GSM4975038': [1.0, 18.0, 1.0], 'GSM4975039': [0.0, 34.0, 0.0], 'GSM4975040': [0.0, 35.0, 0.0], 'GSM4975041': [0.0, 40.0, 0.0], 'GSM4975042': [0.0, 49.0, 0.0], 'GSM4975043': [1.0, 56.0, 1.0], 'GSM4975044': [0.0, 30.0, 0.0], 'GSM4975045': [0.0, 33.0, 0.0], 'GSM4975046': [0.0, 55.0, 0.0], 'GSM4975047': [0.0, 52.0, 0.0], 'GSM4975048': [0.0, 50.0, 1.0], 'GSM4975049': [0.0, 24.0, 0.0], 'GSM4975050': [0.0, 63.0, 1.0], 'GSM4975051': [0.0, 33.0, 0.0], 'GSM4975052': [1.0, 37.0, 1.0], 'GSM4975053': [0.0, 37.0, 0.0]}\n", "Clinical features saved to ../../output/preprocess/Liver_cirrhosis/clinical_data/GSE163211.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the background information, this dataset contains gene expression data from \n", "# Nanostring nCounter assay measuring 795 genes in liver tissue.\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# For trait (Liver_cirrhosis), we need to analyze NAFLD stage data\n", "trait_row = 8 # 'nafld stage' corresponds to liver disease status\n", "\n", "# For age, it's clearly available\n", "age_row = 3 # 'age' is directly available\n", "\n", "# For gender, it's available as 'Sex'\n", "gender_row = 4 # 'Sex' field contains gender information\n", "\n", "# 2.2 Data Type Conversion Functions\n", "def convert_trait(value):\n", " \"\"\"Convert NAFLD stage value to binary indicator for liver cirrhosis.\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract the value after colon and strip whitespace\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # In NAFLD, cirrhosis is the advanced stage of NASH with fibrosis\n", " # NASH_F1_F4 includes cirrhosis (F4) and advanced fibrosis stages\n", " if value == \"NASH_F1_F4\":\n", " return 1 # Represents presence of cirrhosis or advanced fibrosis\n", " elif value in [\"Normal\", \"Steatosis\", \"NASH_F0\"]:\n", " return 0 # Represents absence of cirrhosis or significant fibrosis\n", " else:\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age value to continuous numeric value.\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract the value after colon and strip whitespace\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " try:\n", " return float(value) # Convert to numeric value\n", " except (ValueError, TypeError):\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender value to binary (0 for female, 1 for male).\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract the value after colon and strip whitespace\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " if value.lower() == \"female\":\n", " return 0\n", " elif value.lower() == \"male\":\n", " return 1\n", " else:\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 information\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", " # Extract clinical features\n", " clinical_features = 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(clinical_features)\n", " print(\"Preview of extracted clinical features:\")\n", " print(preview)\n", " \n", " # Save the clinical features to CSV\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " clinical_features.to_csv(out_clinical_data_file, index=False)\n", " print(f\"Clinical features saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "ff52baa3", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "b9f6d86c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:26.393424Z", "iopub.status.busy": "2025-03-25T07:33:26.393325Z", "iopub.status.idle": "2025-03-25T07:33:26.435571Z", "shell.execute_reply": "2025-03-25T07:33:26.435260Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix file found: ../../input/GEO/Liver_cirrhosis/GSE163211/GSE163211_series_matrix.txt.gz\n", "Gene data shape: (800, 318)\n", "First 20 gene/probe identifiers:\n", "Index(['A1BG', 'A2M', 'A2ML1', 'AARS', 'ABCC4', 'ABCG5', 'ABCG8', 'ABLIM1',\n", " 'ABO', 'ACAN', 'ACOT11', 'ACOT2', 'ACSM3', 'ACTA2', 'ACTR2', 'ADAM10',\n", " 'ADAM12', 'ADAM15', 'ADAM17', 'ADAM19'],\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": "67545bcb", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "223efe83", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:26.436653Z", "iopub.status.busy": "2025-03-25T07:33:26.436551Z", "iopub.status.idle": "2025-03-25T07:33:26.438426Z", "shell.execute_reply": "2025-03-25T07:33:26.438124Z" } }, "outputs": [], "source": [ "# The gene identifiers in the gene expression data appear to be human gene symbols already.\n", "# They include well-known gene symbols like A1BG, A2M, ACTA2, etc., which are standard human gene symbols.\n", "# There's no need to map these identifiers to gene symbols as they already are in the correct format.\n", "\n", "requires_gene_mapping = False\n" ] }, { "cell_type": "markdown", "id": "6d28d450", "metadata": {}, "source": [ "### Step 5: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 6, "id": "d50431a3", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:33:26.439467Z", "iopub.status.busy": "2025-03-25T07:33:26.439370Z", "iopub.status.idle": "2025-03-25T07:33:26.956437Z", "shell.execute_reply": "2025-03-25T07:33:26.956069Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape before normalization: (800, 318)\n", "Gene data shape after normalization: (798, 318)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene expression data saved to ../../output/preprocess/Liver_cirrhosis/gene_data/GSE163211.csv\n", "Original clinical data preview:\n", " !Sample_geo_accession GSM4974854 \\\n", "0 !Sample_characteristics_ch1 tissue: Human liver \n", "1 !Sample_characteristics_ch1 race: Black or African American \n", "2 !Sample_characteristics_ch1 bmi: 45.77 \n", "3 !Sample_characteristics_ch1 age: 35 \n", "4 !Sample_characteristics_ch1 Sex: Female \n", "\n", " GSM4974855 GSM4974856 \\\n", "0 tissue: Human liver tissue: Human liver \n", "1 race: White race: Black or African American \n", "2 bmi: 42.8 bmi: 40.44 \n", "3 age: 33 age: 55 \n", "4 Sex: Female Sex: Female \n", "\n", " GSM4974857 GSM4974858 \\\n", "0 tissue: Human liver tissue: Human liver \n", "1 race: Black or African American race: Black or African American \n", "2 bmi: 58.48 bmi: 41.91 \n", "3 age: 49 age: 27 \n", "4 Sex: Female Sex: Female \n", "\n", " GSM4974859 GSM4974860 GSM4974861 \\\n", "0 tissue: Human liver tissue: Human liver tissue: Human liver \n", "1 race: White race: White race: White \n", "2 bmi: 41.81 bmi: 36.11 bmi: 54.25 \n", "3 age: 44 age: 58 age: 53 \n", "4 Sex: Female Sex: Female Sex: Female \n", "\n", " GSM4974862 ... GSM4975162 \\\n", "0 tissue: Human liver ... tissue: Human liver \n", "1 race: Black or African American ... race: White \n", "2 bmi: 50.34 ... bmi: 37.98 \n", "3 age: 46 ... age: 60 \n", "4 Sex: Male ... Sex: Female \n", "\n", " GSM4975163 GSM4975164 GSM4975165 \\\n", "0 tissue: Human liver tissue: Human liver tissue: Human liver \n", "1 race: White race: White race: White \n", "2 bmi: 44.79 bmi: 45.39 bmi: 42.27 \n", "3 age: 45 age: 53 age: 33 \n", "4 Sex: Female Sex: Female Sex: Female \n", "\n", " GSM4975166 GSM4975167 GSM4975168 \\\n", "0 tissue: Human liver tissue: Human liver tissue: Human liver \n", "1 race: White race: White race: Black or African American \n", "2 bmi: 42.69 bmi: 44.07 bmi: 58.18 \n", "3 age: 57 age: 52 age: 37 \n", "4 Sex: Female Sex: Male Sex: Female \n", "\n", " GSM4975169 GSM4975170 GSM4975171 \n", "0 tissue: Human liver tissue: Human liver tissue: Human liver \n", "1 race: White race: White race: Black or African American \n", "2 bmi: 48.5 bmi: 56.87 bmi: 38.3 \n", "3 age: 56 age: 41 age: 31 \n", "4 Sex: Male Sex: Female Sex: Female \n", "\n", "[5 rows x 319 columns]\n", "Selected clinical data shape: (3, 318)\n", "Clinical data preview:\n", " GSM4974854 GSM4974855 GSM4974856 GSM4974857 GSM4974858 \\\n", "Liver_cirrhosis 0.0 0.0 0.0 0.0 0.0 \n", "Age 35.0 33.0 55.0 49.0 27.0 \n", "Gender 0.0 0.0 0.0 0.0 0.0 \n", "\n", " GSM4974859 GSM4974860 GSM4974861 GSM4974862 GSM4974863 \\\n", "Liver_cirrhosis 0.0 0.0 0.0 0.0 0.0 \n", "Age 44.0 58.0 53.0 46.0 61.0 \n", "Gender 0.0 0.0 0.0 1.0 0.0 \n", "\n", " ... GSM4975162 GSM4975163 GSM4975164 GSM4975165 \\\n", "Liver_cirrhosis ... 0.0 0.0 0.0 0.0 \n", "Age ... 60.0 45.0 53.0 33.0 \n", "Gender ... 0.0 0.0 0.0 0.0 \n", "\n", " GSM4975166 GSM4975167 GSM4975168 GSM4975169 GSM4975170 \\\n", "Liver_cirrhosis 0.0 0.0 0.0 0.0 1.0 \n", "Age 57.0 52.0 37.0 56.0 41.0 \n", "Gender 0.0 1.0 0.0 1.0 0.0 \n", "\n", " GSM4975171 \n", "Liver_cirrhosis 0.0 \n", "Age 31.0 \n", "Gender 0.0 \n", "\n", "[3 rows x 318 columns]\n", "Linked data shape before processing: (318, 801)\n", "Linked data preview (first 5 rows, 5 columns):\n", " Liver_cirrhosis Age Gender A1BG A2M\n", "GSM4974854 0.0 35.0 0.0 41.15 14769.96\n", "GSM4974855 0.0 33.0 0.0 41.98 25242.80\n", "GSM4974856 0.0 55.0 0.0 43.52 19693.55\n", "GSM4974857 0.0 49.0 0.0 126.34 16936.10\n", "GSM4974858 0.0 27.0 0.0 48.34 14468.75\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (318, 801)\n", "For the feature 'Liver_cirrhosis', the least common label is '1.0' with 82 occurrences. This represents 25.79% of the dataset.\n", "The distribution of the feature 'Liver_cirrhosis' in this dataset is fine.\n", "\n", "Quartiles for 'Age':\n", " 25%: 35.0\n", " 50% (Median): 44.0\n", " 75%: 53.0\n", "Min: 16.0\n", "Max: 74.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 75 occurrences. This represents 23.58% of the dataset.\n", "The distribution of the feature 'Gender' in this dataset is fine.\n", "\n", "Data shape after removing biased features: (318, 801)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Liver_cirrhosis/GSE163211.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", "# Load the actual clinical data from the matrix file that was previously obtained in Step 1\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "background_info, clinical_data = get_background_and_clinical_data(matrix_file)\n", "\n", "# Get preview of clinical data to understand its structure\n", "print(\"Original clinical data preview:\")\n", "print(clinical_data.head())\n", "\n", "# 2. If we have trait data available, proceed with linking\n", "if trait_row is not None:\n", " # Extract clinical features using the original clinical 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 and linked_data.shape[0] > 0:\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 or has no rows after missing value handling\")\n", "\n", " # 5. Validate and save cohort information\n", " note = \"\"\n", " if linked_data.empty or linked_data.shape[0] == 0:\n", " note = \"Dataset contains gene expression data related to liver fibrosis progression, but linking clinical and genetic data failed, possibly due to mismatched sample IDs.\"\n", " else:\n", " note = \"Dataset contains gene expression data for liver fibrosis progression, which is relevant to liver cirrhosis research.\"\n", " \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", " # 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.\")\n", "else:\n", " # If no trait data available, validate 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 data without trait\n", " df=pd.DataFrame(), # Empty DataFrame\n", " note=\"Dataset contains gene expression data but lacks proper clinical trait information for liver cirrhosis analysis.\"\n", " )\n", " \n", " print(\"Dataset is not usable for liver cirrhosis analysis due to lack of clinical trait data. 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 }