{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "27d02673", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:31:27.864178Z", "iopub.status.busy": "2025-03-25T07:31:27.863934Z", "iopub.status.idle": "2025-03-25T07:31:28.027546Z", "shell.execute_reply": "2025-03-25T07:31:28.027151Z" } }, "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_Cancer\"\n", "cohort = \"GSE45032\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Liver_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Liver_Cancer/GSE45032\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Liver_Cancer/GSE45032.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Liver_Cancer/gene_data/GSE45032.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Liver_Cancer/clinical_data/GSE45032.csv\"\n", "json_path = \"../../output/preprocess/Liver_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "874f639a", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "57cd993c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:31:28.028785Z", "iopub.status.busy": "2025-03-25T07:31:28.028643Z", "iopub.status.idle": "2025-03-25T07:31:28.200867Z", "shell.execute_reply": "2025-03-25T07:31:28.200301Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Gene expression in liver of HCC and CHC patients\"\n", "!Series_summary\t\"In order to compare age depenpdence of mRNA between HCC and CHC patients, we measured gene expression by microarray.\"\n", "!Series_overall_design\t\"24 liver samples are taken from HCC and CHC patients with various ages and gender.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['cell type: hepatocallular carcinoma', 'cell type: chronic hepatitis type C'], 1: ['tissue: liver'], 2: ['gender: male', 'gender: female'], 3: ['age(yrs): 67', 'age(yrs): 56', 'age(yrs): 76', 'age(yrs): 79', 'age(yrs): 66', 'age(yrs): 70', 'age(yrs): 68', 'age(yrs): 72', 'age(yrs): 62', 'age(yrs): 55', 'age(yrs): 71', 'age(yrs): 73', 'age(yrs): 74', 'age(yrs): 61', 'age(yrs): 54', 'age(yrs): 64', 'age(yrs): 59', 'age(yrs): 69', 'age(yrs): 25', 'age(yrs): 41', 'age(yrs): 50', 'age(yrs): 58', 'age(yrs): 49', 'age(yrs): 63', 'age(yrs): 60', 'age(yrs): 52', 'age(yrs): 51']}\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": "88920245", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "37a5e31d", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:31:28.202606Z", "iopub.status.busy": "2025-03-25T07:31:28.202495Z", "iopub.status.idle": "2025-03-25T07:31:28.225352Z", "shell.execute_reply": "2025-03-25T07:31:28.225017Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of clinical data:\n", "{'GSM1096016': [1.0, 67.0, 1.0], 'GSM1096017': [1.0, 56.0, 1.0], 'GSM1096018': [1.0, 76.0, 0.0], 'GSM1096019': [1.0, 79.0, 1.0], 'GSM1096020': [1.0, 66.0, 1.0], 'GSM1096021': [1.0, 70.0, 1.0], 'GSM1096022': [1.0, 68.0, 1.0], 'GSM1096023': [1.0, 72.0, 0.0], 'GSM1096024': [1.0, 62.0, 1.0], 'GSM1096025': [1.0, 66.0, 1.0], 'GSM1096026': [1.0, 55.0, 1.0], 'GSM1096027': [1.0, 62.0, 1.0], 'GSM1096028': [1.0, 71.0, 1.0], 'GSM1096029': [1.0, 73.0, 0.0], 'GSM1096030': [1.0, 74.0, 0.0], 'GSM1096031': [1.0, 61.0, 0.0], 'GSM1096032': [1.0, 54.0, 1.0], 'GSM1096033': [1.0, 64.0, 0.0], 'GSM1096034': [1.0, 68.0, 1.0], 'GSM1096035': [1.0, 59.0, 1.0], 'GSM1096036': [1.0, 79.0, 1.0], 'GSM1096037': [1.0, 69.0, 0.0], 'GSM1096038': [1.0, 59.0, 1.0], 'GSM1096039': [1.0, 71.0, 1.0], 'GSM1096040': [0.0, 64.0, 1.0], 'GSM1096041': [0.0, 55.0, 0.0], 'GSM1096042': [0.0, 66.0, 0.0], 'GSM1096043': [0.0, 56.0, 1.0], 'GSM1096044': [0.0, 66.0, 1.0], 'GSM1096045': [0.0, 68.0, 1.0], 'GSM1096046': [0.0, 25.0, 0.0], 'GSM1096047': [0.0, 41.0, 1.0], 'GSM1096048': [0.0, 50.0, 0.0], 'GSM1096049': [0.0, 56.0, 0.0], 'GSM1096050': [0.0, 66.0, 0.0], 'GSM1096051': [0.0, 58.0, 1.0], 'GSM1096052': [0.0, 67.0, 0.0], 'GSM1096053': [0.0, 49.0, 1.0], 'GSM1096054': [0.0, 63.0, 1.0], 'GSM1096055': [0.0, 70.0, 1.0], 'GSM1096056': [0.0, 60.0, 0.0], 'GSM1096057': [0.0, 50.0, 0.0], 'GSM1096058': [0.0, 58.0, 1.0], 'GSM1096059': [0.0, 61.0, 1.0], 'GSM1096060': [0.0, 60.0, 0.0], 'GSM1096061': [0.0, 59.0, 1.0], 'GSM1096062': [0.0, 52.0, 1.0], 'GSM1096063': [0.0, 51.0, 1.0]}\n", "Clinical data saved to ../../output/preprocess/Liver_Cancer/clinical_data/GSE45032.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the Series_title and Series_summary, this appears to be a microarray gene expression dataset\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# For trait (Liver_Cancer): From the sample characteristics, key 0 has \"cell type: hepatocallular carcinoma\" and \"cell type: chronic hepatitis type C\"\n", "trait_row = 0\n", "\n", "# For age: From the sample characteristics, key 3 has age information\n", "age_row = 3\n", "\n", "# For gender: From the sample characteristics, key 2 has gender information\n", "gender_row = 2\n", "\n", "# 2.2 Data Type Conversion Functions\n", "def convert_trait(value):\n", " \"\"\"Convert cell type values to binary trait values (HCC=1, CHC=0)\"\"\"\n", " if isinstance(value, str):\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " if \"hepatocallular carcinoma\" in value.lower() or \"hcc\" in value.lower():\n", " return 1 # HCC = 1 (Liver Cancer)\n", " elif \"chronic hepatitis\" in value.lower() or \"chc\" in value.lower():\n", " return 0 # CHC = 0 (not Liver Cancer but Hepatitis)\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age values to continuous values\"\"\"\n", " if isinstance(value, str):\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " # Extract digits only to handle potential format like \"age(yrs): 67\"\n", " age_str = ''.join(c for c in value if c.isdigit())\n", " if age_str:\n", " return float(age_str)\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender values to binary (female=0, male=1)\"\"\"\n", " if isinstance(value, str):\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip().lower()\n", " else:\n", " value = value.lower()\n", " \n", " if \"female\" in value or \"f\" == value:\n", " return 0\n", " elif \"male\" in value or \"m\" == value:\n", " return 1\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", "# Save initial metadata\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 using the library function\n", " 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 data\n", " preview = preview_df(clinical_df)\n", " print(\"Preview of clinical data:\")\n", " print(preview)\n", " \n", " # Save clinical data to CSV file\n", " clinical_df.to_csv(out_clinical_data_file)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "f5fd01b5", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "e731691f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:31:28.226537Z", "iopub.status.busy": "2025-03-25T07:31:28.226428Z", "iopub.status.idle": "2025-03-25T07:31:28.504581Z", "shell.execute_reply": "2025-03-25T07:31:28.503936Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix file found: ../../input/GEO/Liver_Cancer/GSE45032/GSE45032_series_matrix.txt.gz\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape: (62976, 48)\n", "First 20 gene/probe identifiers:\n", "Index(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13',\n", " '14', '15', '16', '17', '18', '19', '20'],\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": "399611bb", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "8083ceac", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:31:28.506366Z", "iopub.status.busy": "2025-03-25T07:31:28.506221Z", "iopub.status.idle": "2025-03-25T07:31:28.508785Z", "shell.execute_reply": "2025-03-25T07:31:28.508362Z" } }, "outputs": [], "source": [ "# Analyzing the gene identifiers in the gene expression data.\n", "# The identifiers appear to be simple numeric indices (e.g. '1', '2', '3') rather than \n", "# actual human gene symbols like 'BRCA1', 'TP53', etc. or other typical gene identifiers \n", "# such as Entrez IDs or Ensembl IDs.\n", "#\n", "# In order to perform meaningful biological analysis, these numeric identifiers will need\n", "# to be mapped to standard human gene symbols or other recognized gene identifiers.\n", "# This likely means we need to find a mapping file or reference that connects these\n", "# numeric indices to gene symbols, possibly in the platform annotation file.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "96c4826c", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "ef89fbed", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:31:28.510378Z", "iopub.status.busy": "2025-03-25T07:31:28.510246Z", "iopub.status.idle": "2025-03-25T07:31:32.499624Z", "shell.execute_reply": "2025-03-25T07:31:32.499029Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene annotation preview:\n", "Columns in gene annotation: ['ID', 'ProbeName', 'GB_ACC', 'ControlType', 'accessions', 'GeneName', 'Description', 'chr_coord', 'SEQUENCE', 'SPOT_ID']\n", "{'ID': ['1', '2', '3', '4', '5'], 'ProbeName': ['GE_BrightCorner', 'DarkCorner', 'DarkCorner', 'A_23_P326296', 'A_24_P287941'], 'GB_ACC': [nan, nan, nan, 'NM_144987', 'NM_013290'], 'ControlType': [1.0, 1.0, 1.0, 0.0, 0.0], 'accessions': [nan, nan, nan, 'ref|NM_144987|ref|NM_001040425|ens|ENST00000292879|ens|ENST00000392196', 'ref|NM_013290|ref|NM_016556|ens|ENST00000393795|ens|ENST00000253789'], 'GeneName': ['GE_BrightCorner', 'DarkCorner', 'DarkCorner', 'U2AF1L4', 'PSMC3IP'], 'Description': [nan, nan, nan, 'ref|Homo sapiens U2 small nuclear RNA auxiliary factor 1-like 4 (U2AF1L4), transcript variant 2, mRNA [NM_144987]', 'ref|Homo sapiens PSMC3 interacting protein (PSMC3IP), transcript variant 1, mRNA [NM_013290]'], 'chr_coord': [nan, nan, nan, 'hs|chr19:036235296-036235237', 'hs|chr17:040724775-040724716'], 'SEQUENCE': [nan, nan, nan, 'GTATGGGGAGATTGAAGAGATGAATGTGTGCGACAACCTTGGGGACCACGTCGTGGGCAA', 'AAATTGCAGTAGCTTGAGGTTAACATTTAGACTTGGAACAATGCTAAAGGAAAGCATTTG'], 'SPOT_ID': ['--GE_BrightCorner', '--DarkCorner', '--DarkCorner', nan, nan]}\n", "\n", "Examining potential gene mapping columns:\n" ] } ], "source": [ "# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "gene_annotation = get_gene_annotation(soft_file)\n", "\n", "# 2. Analyze the gene annotation dataframe to identify which columns contain the gene identifiers and gene symbols\n", "print(\"\\nGene annotation preview:\")\n", "print(f\"Columns in gene annotation: {gene_annotation.columns.tolist()}\")\n", "print(preview_df(gene_annotation, n=5))\n", "\n", "# Look more closely at columns that might contain gene information\n", "print(\"\\nExamining potential gene mapping columns:\")\n", "potential_gene_columns = ['gene_assignment', 'mrna_assignment', 'swissprot', 'unigene']\n", "for col in potential_gene_columns:\n", " if col in gene_annotation.columns:\n", " print(f\"\\nSample values from '{col}' column:\")\n", " print(gene_annotation[col].head(3).tolist())\n" ] }, { "cell_type": "markdown", "id": "5b84e3aa", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "615a5425", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:31:32.501466Z", "iopub.status.busy": "2025-03-25T07:31:32.501341Z", "iopub.status.idle": "2025-03-25T07:31:33.418541Z", "shell.execute_reply": "2025-03-25T07:31:33.417971Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mapping dataframe shape: (62976, 2)\n", "First few rows of mapping dataframe:\n", " ID Gene\n", "0 1 GE_BrightCorner\n", "1 2 DarkCorner\n", "2 3 DarkCorner\n", "3 4 U2AF1L4\n", "4 5 PSMC3IP\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data after mapping, shape: (20147, 48)\n", "First few gene symbols after mapping:\n", "['A1BG', 'A1CF', 'A2BP1', 'A2LD1', 'A2M', 'A2ML1', 'A4GALT', 'A4GNT', 'AA081107', 'AA213559']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data after normalization, shape: (19274, 48)\n", "First few normalized gene symbols:\n", "['A1BG', 'A1BG-AS1', 'A1CF', 'A2M', 'A2ML1', 'A4GALT', 'A4GNT', 'AAA1', 'AAAS', 'AACS']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Liver_Cancer/gene_data/GSE45032.csv\n" ] } ], "source": [ "# Analyzing the gene annotation dataframe to identify mapping columns\n", "# The 'ID' column in gene_annotation appears to match with the row IDs in gene_data\n", "# The 'GeneName' column appears to contain the gene symbols we want to map to\n", "\n", "# 1. Create a mapping dataframe with columns 'ID' and 'Gene'\n", "mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='GeneName')\n", "print(f\"Mapping dataframe shape: {mapping_df.shape}\")\n", "print(\"First few rows of mapping dataframe:\")\n", "print(mapping_df.head())\n", "\n", "# 2. Apply the gene mapping to convert probe-level expression to gene-level expression\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "print(f\"Gene expression data after mapping, shape: {gene_data.shape}\")\n", "print(\"First few gene symbols after mapping:\")\n", "print(gene_data.index[:10].tolist())\n", "\n", "# 3. Normalize gene symbols to ensure consistency\n", "gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Gene expression data after normalization, shape: {gene_data.shape}\")\n", "print(\"First few normalized gene symbols:\")\n", "print(gene_data.index[:10].tolist())\n", "\n", "# 4. Save the processed gene expression data\n", "gene_data.to_csv(out_gene_data_file)\n", "print(f\"Gene expression data saved to {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "52d7da88", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "fab36e05", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:31:33.420328Z", "iopub.status.busy": "2025-03-25T07:31:33.420200Z", "iopub.status.idle": "2025-03-25T07:31:41.710671Z", "shell.execute_reply": "2025-03-25T07:31:41.710316Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical data loaded from file:\n", "{'GSM1096016': [1.0, 67.0, 1.0], 'GSM1096017': [1.0, 56.0, 1.0], 'GSM1096018': [1.0, 76.0, 0.0], 'GSM1096019': [1.0, 79.0, 1.0], 'GSM1096020': [1.0, 66.0, 1.0], 'GSM1096021': [1.0, 70.0, 1.0], 'GSM1096022': [1.0, 68.0, 1.0], 'GSM1096023': [1.0, 72.0, 0.0], 'GSM1096024': [1.0, 62.0, 1.0], 'GSM1096025': [1.0, 66.0, 1.0], 'GSM1096026': [1.0, 55.0, 1.0], 'GSM1096027': [1.0, 62.0, 1.0], 'GSM1096028': [1.0, 71.0, 1.0], 'GSM1096029': [1.0, 73.0, 0.0], 'GSM1096030': [1.0, 74.0, 0.0], 'GSM1096031': [1.0, 61.0, 0.0], 'GSM1096032': [1.0, 54.0, 1.0], 'GSM1096033': [1.0, 64.0, 0.0], 'GSM1096034': [1.0, 68.0, 1.0], 'GSM1096035': [1.0, 59.0, 1.0], 'GSM1096036': [1.0, 79.0, 1.0], 'GSM1096037': [1.0, 69.0, 0.0], 'GSM1096038': [1.0, 59.0, 1.0], 'GSM1096039': [1.0, 71.0, 1.0], 'GSM1096040': [0.0, 64.0, 1.0], 'GSM1096041': [0.0, 55.0, 0.0], 'GSM1096042': [0.0, 66.0, 0.0], 'GSM1096043': [0.0, 56.0, 1.0], 'GSM1096044': [0.0, 66.0, 1.0], 'GSM1096045': [0.0, 68.0, 1.0], 'GSM1096046': [0.0, 25.0, 0.0], 'GSM1096047': [0.0, 41.0, 1.0], 'GSM1096048': [0.0, 50.0, 0.0], 'GSM1096049': [0.0, 56.0, 0.0], 'GSM1096050': [0.0, 66.0, 0.0], 'GSM1096051': [0.0, 58.0, 1.0], 'GSM1096052': [0.0, 67.0, 0.0], 'GSM1096053': [0.0, 49.0, 1.0], 'GSM1096054': [0.0, 63.0, 1.0], 'GSM1096055': [0.0, 70.0, 1.0], 'GSM1096056': [0.0, 60.0, 0.0], 'GSM1096057': [0.0, 50.0, 0.0], 'GSM1096058': [0.0, 58.0, 1.0], 'GSM1096059': [0.0, 61.0, 1.0], 'GSM1096060': [0.0, 60.0, 0.0], 'GSM1096061': [0.0, 59.0, 1.0], 'GSM1096062': [0.0, 52.0, 1.0], 'GSM1096063': [0.0, 51.0, 1.0]}\n", "\n", "Verifying gene data normalization...\n", "Gene data shape: (19274, 48)\n", "First 10 gene identifiers:\n", "['A1BG', 'A1BG-AS1', 'A1CF', 'A2M', 'A2ML1', 'A4GALT', 'A4GNT', 'AAA1', 'AAAS', 'AACS']\n", "\n", "Linking clinical and genetic data...\n", "Linked data shape: (48, 19277)\n", "Linked data preview (first 5 rows, 5 columns):\n", " Liver_Cancer Age Gender A1BG A1BG-AS1\n", "GSM1096016 1.0 67.0 1.0 98630.75 13.64972\n", "GSM1096017 1.0 56.0 1.0 101580.80 43.58710\n", "GSM1096018 1.0 76.0 0.0 18535.65 20.95010\n", "GSM1096019 1.0 79.0 1.0 97672.96 19.52341\n", "GSM1096020 1.0 66.0 1.0 28156.31 35.88990\n", "\n", "Handling missing values...\n", "Samples with missing trait values: 0 out of 48\n", "Genes with ≤20% missing values: 19274 out of 19274\n", "Samples with ≤5% missing gene values: 48 out of 48\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data shape after handling missing values: (48, 19277)\n", "\n", "Checking for bias in dataset features...\n", "For the feature 'Liver_Cancer', the least common label is '1.0' with 24 occurrences. This represents 50.00% of the dataset.\n", "The distribution of the feature 'Liver_Cancer' in this dataset is fine.\n", "\n", "Quartiles for 'Age':\n", " 25%: 56.0\n", " 50% (Median): 62.5\n", " 75%: 68.0\n", "Min: 25.0\n", "Max: 79.0\n", "The distribution of the feature 'Age' in this dataset is fine.\n", "\n", "For the feature 'Gender', the least common label is '0.0' with 17 occurrences. This represents 35.42% of the dataset.\n", "The distribution of the feature 'Gender' in this dataset is fine.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Liver_Cancer/GSE45032.csv\n" ] } ], "source": [ "# 1. Load the previously saved clinical data\n", "clinical_df = pd.read_csv(out_clinical_data_file, index_col=0)\n", "print(\"Clinical data loaded from file:\")\n", "print(preview_df(clinical_df))\n", "\n", "# 2. Normalize gene symbols \n", "# Note: This was already done in a previous step, but we'll ensure gene_data has been normalized\n", "print(\"\\nVerifying gene data normalization...\")\n", "print(f\"Gene data shape: {gene_data.shape}\")\n", "print(\"First 10 gene identifiers:\")\n", "print(gene_data.index[:10].tolist())\n", "\n", "# 3. Link clinical and genetic data\n", "print(\"\\nLinking clinical and genetic data...\")\n", "linked_data = geo_link_clinical_genetic_data(clinical_df, gene_data)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "print(\"Linked data preview (first 5 rows, 5 columns):\")\n", "if linked_data.shape[0] > 0 and linked_data.shape[1] > 5:\n", " preview_cols = list(linked_data.columns[:5])\n", " print(linked_data.iloc[:5, :5])\n", "else:\n", " print(linked_data)\n", "\n", "# 4. Handle missing values\n", "print(\"\\nHandling missing values...\")\n", "# First check how many samples have missing trait values\n", "if trait in linked_data.columns:\n", " missing_trait = linked_data[trait].isna().sum()\n", " print(f\"Samples with missing trait values: {missing_trait} out of {len(linked_data)}\")\n", "\n", "# Check gene missing value percentages\n", "gene_cols = [col for col in linked_data.columns if col not in [trait, 'Age', 'Gender']]\n", "gene_missing_pct = linked_data[gene_cols].isna().mean()\n", "genes_to_keep = gene_missing_pct[gene_missing_pct <= 0.2].index\n", "print(f\"Genes with ≤20% missing values: {len(genes_to_keep)} out of {len(gene_cols)}\")\n", "\n", "# Check sample missing value percentages\n", "if len(gene_cols) > 0:\n", " sample_missing_pct = linked_data[gene_cols].isna().mean(axis=1)\n", " samples_to_keep = sample_missing_pct[sample_missing_pct <= 0.05].index\n", " print(f\"Samples with ≤5% missing gene values: {len(samples_to_keep)} out of {len(linked_data)}\")\n", "\n", "# Apply missing value handling\n", "linked_data_clean = handle_missing_values(linked_data, trait)\n", "print(f\"Linked data shape after handling missing values: {linked_data_clean.shape}\")\n", "\n", "# 5. Check for bias in the dataset\n", "print(\"\\nChecking for bias in dataset features...\")\n", "trait_biased, linked_data_clean = judge_and_remove_biased_features(linked_data_clean, trait)\n", "\n", "# 6. Conduct final quality validation\n", "note = \"This dataset contains liver tissue gene expression from hepatocellular carcinoma (HCC) and chronic hepatitis C (CHC) patients with various ages and gender.\"\n", "is_gene_available = len(gene_data) > 0\n", "is_trait_available = trait in linked_data.columns\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=trait_biased,\n", " df=linked_data_clean,\n", " note=note\n", ")\n", "\n", "# 7. Save the linked data if it's usable\n", "if is_usable and linked_data_clean.shape[0] > 0:\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " linked_data_clean.to_csv(out_data_file, index=True)\n", " print(f\"Linked data saved to {out_data_file}\")\n", "else:\n", " print(\"Dataset deemed not usable for associative studies. Linked data not 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 }