{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "5548d6be", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:04:52.918554Z", "iopub.status.busy": "2025-03-25T08:04:52.918372Z", "iopub.status.idle": "2025-03-25T08:04:53.084371Z", "shell.execute_reply": "2025-03-25T08:04:53.084023Z" } }, "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 = \"Hypothyroidism\"\n", "cohort = \"GSE151158\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Hypothyroidism\"\n", "in_cohort_dir = \"../../input/GEO/Hypothyroidism/GSE151158\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Hypothyroidism/GSE151158.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Hypothyroidism/gene_data/GSE151158.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Hypothyroidism/clinical_data/GSE151158.csv\"\n", "json_path = \"../../output/preprocess/Hypothyroidism/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "4a0b41a3", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "fca94983", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:04:53.085836Z", "iopub.status.busy": "2025-03-25T08:04:53.085695Z", "iopub.status.idle": "2025-03-25T08:04:53.105558Z", "shell.execute_reply": "2025-03-25T08:04:53.105262Z" } }, "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": "9fbbaa76", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "6f614e64", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:04:53.106611Z", "iopub.status.busy": "2025-03-25T08:04:53.106505Z", "iopub.status.idle": "2025-03-25T08:04:53.119362Z", "shell.execute_reply": "2025-03-25T08:04:53.119072Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of clinical data:\n", "{'GSM4567420': [0.0, 53.0, 0.0], 'GSM4567421': [0.0, 40.0, 1.0], 'GSM4567422': [0.0, 51.0, 0.0], 'GSM4567423': [0.0, 36.0, 0.0], 'GSM4567424': [0.0, 44.0, 0.0], 'GSM4567425': [1.0, 60.0, 1.0], 'GSM4567426': [0.0, 31.0, 1.0], 'GSM4567427': [0.0, 41.0, 0.0], 'GSM4567428': [0.0, 55.0, 1.0], 'GSM4567429': [0.0, 15.0, 0.0], 'GSM4567430': [1.0, 57.0, 0.0], 'GSM4567431': [1.0, 56.0, 0.0], 'GSM4567432': [0.0, 34.0, 1.0], 'GSM4567433': [0.0, 43.0, 1.0], 'GSM4567434': [0.0, 49.0, 0.0], 'GSM4567435': [0.0, 55.0, 0.0], 'GSM4567436': [0.0, 52.0, 1.0], 'GSM4567437': [0.0, 35.0, 0.0], 'GSM4567438': [0.0, 35.0, 0.0], 'GSM4567439': [0.0, 40.0, 1.0], 'GSM4567440': [0.0, 34.0, 1.0], 'GSM4567441': [0.0, 42.0, 0.0], 'GSM4567442': [1.0, 53.0, 0.0], 'GSM4567443': [0.0, 33.0, 0.0], 'GSM4567444': [0.0, 31.0, 0.0], 'GSM4567445': [1.0, 57.0, 0.0], 'GSM4567446': [0.0, 42.0, 0.0], 'GSM4567447': [0.0, 48.0, 0.0], 'GSM4567448': [0.0, 47.0, 0.0], 'GSM4567449': [0.0, 51.0, 0.0], 'GSM4567450': [0.0, 65.0, 1.0], 'GSM4567451': [0.0, 40.0, 0.0], 'GSM4567452': [0.0, 59.0, 1.0], 'GSM4567453': [1.0, 49.0, 0.0], 'GSM4567454': [0.0, 61.0, 1.0], 'GSM4567455': [1.0, 59.0, 1.0], 'GSM4567456': [0.0, 28.0, 0.0], 'GSM4567457': [0.0, 46.0, 1.0], 'GSM4567458': [0.0, 42.0, 0.0], 'GSM4567459': [1.0, 60.0, 0.0], 'GSM4567460': [0.0, 25.0, 0.0], 'GSM4567461': [0.0, 43.0, 1.0], 'GSM4567462': [0.0, 51.0, 1.0], 'GSM4567463': [0.0, 52.0, 1.0], 'GSM4567464': [0.0, 51.0, 0.0], 'GSM4567465': [0.0, 56.0, 0.0], 'GSM4567466': [0.0, 27.0, 0.0], 'GSM4567467': [0.0, 35.0, 0.0], 'GSM4567468': [0.0, 54.0, 0.0], 'GSM4567469': [0.0, 37.0, 1.0], 'GSM4567470': [0.0, 45.0, 1.0], 'GSM4567471': [0.0, 45.0, 0.0], 'GSM4567472': [0.0, 47.0, 1.0], 'GSM4567473': [0.0, 40.0, 1.0], 'GSM4567474': [0.0, 33.0, 0.0], 'GSM4567475': [0.0, 39.0, 1.0], 'GSM4567476': [0.0, 39.0, 1.0], 'GSM4567477': [0.0, 44.0, 0.0], 'GSM4567478': [0.0, 47.0, 1.0], 'GSM4567479': [0.0, 37.0, 1.0], 'GSM4567480': [1.0, 49.0, 0.0], 'GSM4567481': [nan, nan, nan], 'GSM4567482': [nan, nan, nan], 'GSM4567483': [nan, nan, nan], 'GSM4567484': [nan, nan, nan], 'GSM4567485': [nan, nan, nan]}\n", "Clinical data saved to ../../output/preprocess/Hypothyroidism/clinical_data/GSE151158.csv\n" ] } ], "source": [ "# 1. Determine if gene expression data is available\n", "# Based on the background information, the study compares \"expression of 594 genes involved in innate immune function\"\n", "# This confirms gene expression data is available\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "\n", "# 2.1 Trait (Hypothyroidism)\n", "# From the sample characteristics dictionary, index 12 contains hypothyroidism data\n", "trait_row = 12 # Hypothyroidism data is at key 12\n", "\n", "# Define conversion function for trait data (binary)\n", "def convert_trait(value):\n", " if pd.isna(value):\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Convert to binary (0 for N/no, 1 for Y/yes)\n", " if value.upper() == 'Y':\n", " return 1\n", " elif value.upper() == 'N':\n", " return 0\n", " else:\n", " return None\n", "\n", "# 2.2 Age\n", "# Age data is at key 1\n", "age_row = 1\n", "\n", "# Define conversion function for age (continuous)\n", "def convert_age(value):\n", " if pd.isna(value):\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Convert to numeric value\n", " try:\n", " return float(value)\n", " except:\n", " return None\n", "\n", "# 2.3 Gender\n", "# Gender data is at key 2, labeled as \"Sex\"\n", "gender_row = 2\n", "\n", "# Define conversion function for gender (binary)\n", "def convert_gender(value):\n", " if pd.isna(value):\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Convert to binary (0 for female, 1 for male)\n", " if value.upper() == 'F':\n", " return 0\n", " elif value.upper() == 'M':\n", " return 1\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Determine if trait data is available\n", "is_trait_available = trait_row is not None\n", "\n", "# Perform initial filtering 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", "# Since trait_row is not None, we should extract clinical features\n", "if trait_row is not None:\n", " # Assume clinical_data DataFrame is available from previous step\n", " try:\n", " # Extract relevant clinical features\n", " clinical_df = geo_select_clinical_features(\n", " 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 processed clinical data\n", " print(\"Preview of clinical data:\")\n", " print(preview_df(clinical_df))\n", " \n", " # Create directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " \n", " # Save the clinical data to CSV\n", " clinical_df.to_csv(out_clinical_data_file)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n", " except NameError:\n", " print(\"Error: clinical_data DataFrame not found. Please ensure it's available from previous steps.\")\n" ] }, { "cell_type": "markdown", "id": "f1432473", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "8c91f73f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:04:53.120379Z", "iopub.status.busy": "2025-03-25T08:04:53.120273Z", "iopub.status.idle": "2025-03-25T08:04:53.133832Z", "shell.execute_reply": "2025-03-25T08:04:53.133547Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Extracting gene data from matrix file:\n", "Successfully extracted gene data with 618 rows\n", "First 20 gene IDs:\n", "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", "\n", "Gene expression data available: True\n" ] } ], "source": [ "# 1. Get the file paths for the SOFT file and matrix file\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. Extract gene expression data from the matrix file\n", "try:\n", " print(\"Extracting gene data from matrix file:\")\n", " gene_data = get_genetic_data(matrix_file)\n", " if gene_data.empty:\n", " print(\"Extracted gene expression data is empty\")\n", " is_gene_available = False\n", " else:\n", " print(f\"Successfully extracted gene data with {len(gene_data.index)} rows\")\n", " print(\"First 20 gene IDs:\")\n", " print(gene_data.index[:20])\n", " is_gene_available = True\n", "except Exception as e:\n", " print(f\"Error extracting gene data: {e}\")\n", " print(\"This dataset appears to have an empty or malformed gene expression matrix\")\n", " is_gene_available = False\n", "\n", "print(f\"\\nGene expression data available: {is_gene_available}\")\n" ] }, { "cell_type": "markdown", "id": "9a3aa8e5", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "6894a380", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:04:53.134821Z", "iopub.status.busy": "2025-03-25T08:04:53.134716Z", "iopub.status.idle": "2025-03-25T08:04:53.136411Z", "shell.execute_reply": "2025-03-25T08:04:53.136123Z" } }, "outputs": [], "source": [ "# Looking at the gene identifiers in the gene expression data \n", "# The first 20 gene IDs shown are human gene symbols (e.g., ABCB1, ABL1, ADA, etc.)\n", "# These are standardized human gene symbols that don't need to be mapped to other identifiers\n", "\n", "requires_gene_mapping = False\n" ] }, { "cell_type": "markdown", "id": "45f9f1a9", "metadata": {}, "source": [ "### Step 5: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 6, "id": "25490a4a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:04:53.137416Z", "iopub.status.busy": "2025-03-25T08:04:53.137312Z", "iopub.status.idle": "2025-03-25T08:04:53.248169Z", "shell.execute_reply": "2025-03-25T08:04:53.247811Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalizing gene symbols in the expression data...\n", "Normalized gene data saved to ../../output/preprocess/Hypothyroidism/gene_data/GSE151158.csv\n", "Normalized gene data shape: (583, 66)\n", "\n", "Linking clinical and genetic data...\n", "Linked data shape: (649, 69)\n", "\n", "Handling missing values...\n", "After handling missing values, data shape: (0, 2)\n", "\n", "Checking for bias in features...\n", "Quartiles for 'Hypothyroidism':\n", " 25%: nan\n", " 50% (Median): nan\n", " 75%: nan\n", "Min: nan\n", "Max: nan\n", "The distribution of the feature 'Hypothyroidism' in this dataset is fine.\n", "\n", "Quartiles for 'Age':\n", " 25%: nan\n", " 50% (Median): nan\n", " 75%: nan\n", "Min: nan\n", "Max: nan\n", "The distribution of the feature 'Age' in this dataset is fine.\n", "\n", "\n", "Performing final validation...\n", "Abnormality detected in the cohort: GSE151158. Preprocessing failed.\n", "A new JSON file was created at: ../../output/preprocess/Hypothyroidism/cohort_info.json\n", "Dataset not usable for Hypothyroidism association studies. Data not saved.\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "print(\"Normalizing gene symbols in the expression data...\")\n", "try:\n", " # If previous steps have already loaded gene_data\n", " normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", " \n", " # Create directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", " \n", " # Save normalized gene data\n", " normalized_gene_data.to_csv(out_gene_data_file)\n", " print(f\"Normalized gene data saved to {out_gene_data_file}\")\n", " print(f\"Normalized gene data shape: {normalized_gene_data.shape}\")\n", "except Exception as e:\n", " print(f\"Error normalizing gene data: {e}\")\n", " is_gene_available = False\n", "\n", "# 2. Link clinical and genetic data\n", "if is_gene_available and 'clinical_df' in locals():\n", " print(\"\\nLinking clinical and genetic data...\")\n", " try:\n", " # Transpose normalized gene data to match clinical data format\n", " gene_data_t = normalized_gene_data.T\n", " \n", " # Link clinical and genetic data\n", " linked_data = geo_link_clinical_genetic_data(clinical_df, gene_data_t)\n", " print(f\"Linked data shape: {linked_data.shape}\")\n", " \n", " # 3. Handle missing values systematically\n", " print(\"\\nHandling missing values...\")\n", " linked_data = handle_missing_values(linked_data, trait)\n", " print(f\"After handling missing values, data shape: {linked_data.shape}\")\n", " \n", " # 4. Determine whether features are biased\n", " print(\"\\nChecking for bias in features...\")\n", " is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", " \n", " # 5. Final validation and save metadata\n", " print(\"\\nPerforming final validation...\")\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=\"\"\n", " )\n", " \n", " # 6. Save the linked data if usable\n", " if is_usable:\n", " # Create directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " \n", " # Save linked data\n", " linked_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", " else:\n", " print(f\"Dataset not usable for {trait} association studies. Data not saved.\")\n", " \n", " except Exception as e:\n", " print(f\"Error in data linking or processing: {e}\")\n", " is_usable = False\n", "else:\n", " print(\"Cannot link data: either gene data or clinical data is unavailable.\")\n", " # Create a minimal dataframe for validation purposes\n", " import pandas as pd\n", " linked_data = pd.DataFrame({trait: [None]})\n", " \n", " # Perform final validation with appropriate flags\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=True, # Not relevant since data isn't usable\n", " df=linked_data,\n", " note=\"Failed to link gene and clinical data.\"\n", " )\n", " print(f\"Dataset usability: {is_usable}\")" ] } ], "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 }