{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "91861535", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:49:50.233041Z", "iopub.status.busy": "2025-03-25T05:49:50.232868Z", "iopub.status.idle": "2025-03-25T05:49:50.400352Z", "shell.execute_reply": "2025-03-25T05:49:50.400033Z" } }, "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 = \"GSE77627\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Hypertension\"\n", "in_cohort_dir = \"../../input/GEO/Hypertension/GSE77627\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Hypertension/GSE77627.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Hypertension/gene_data/GSE77627.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Hypertension/clinical_data/GSE77627.csv\"\n", "json_path = \"../../output/preprocess/Hypertension/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "321b57a4", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "3f694568", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:49:50.401832Z", "iopub.status.busy": "2025-03-25T05:49:50.401685Z", "iopub.status.idle": "2025-03-25T05:49:50.508422Z", "shell.execute_reply": "2025-03-25T05:49:50.508079Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Whole genome profiling of liver biopsies discloses potential biomarkers for diagnosis of idiopathic non-cirrhotic portal hypertension\"\n", "!Series_summary\t\"Background. Idiopathic non-cirrhotic portal hypertension (INCPH) is a frequently misdiagnosed cause of portal hypertension. It also lacks a specific test for its diagnosis. This study evaluates whether using new immunohistochemistry makers derived from whole genome analysis improves the diagnosis of INCPH. Methods. We analyzed formalin-fixed, paraffin embedded (FFPE) liver tissue from 18 INCPH and 22 patients with cirrhosis (LC) as well as from 14 histologically normal livers (HNL) as controls. Microarray experiments were performed using Illumina Whole-Genome DASL HT BeadChip arrays. Selected genes showing differential expression at Illumina were confirmed using quantitative real-time PCR (qRT-PCR) gene expression performed with Fluidigm Biomark HD system in a subgroup of samples. Immunohistochemistry was used to confirm the qRT-PCR results. Results. At Illumina, a total of 292 genes were differentially expressed (FC>+2/-2 and p-value <0.05) in INCPH compared to the control group (LC and HNL) (202 up-regulated and 90 down-regulated).\"\n", "!Series_overall_design\t\"Transcriptomic profile of liver tissue from 18 INCPH and 22 patients with cirrhosis (LC) as well as from 14 histologically normal livers (HNL) as controls.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['liver group: HNL', 'liver group: INCPH', 'liver group: LC'], 1: ['tissue: liver']}\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": "a8149916", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "9266774f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:49:50.509777Z", "iopub.status.busy": "2025-03-25T05:49:50.509664Z", "iopub.status.idle": "2025-03-25T05:49:50.518013Z", "shell.execute_reply": "2025-03-25T05:49:50.517716Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of selected clinical features:\n", "{'GSM2055372': [0.0], 'GSM2055373': [0.0], 'GSM2055374': [0.0], 'GSM2055375': [0.0], 'GSM2055376': [0.0], 'GSM2055377': [0.0], 'GSM2055378': [0.0], 'GSM2055379': [0.0], 'GSM2055380': [0.0], 'GSM2055381': [0.0], 'GSM2055382': [0.0], 'GSM2055383': [0.0], 'GSM2055384': [0.0], 'GSM2055385': [0.0], 'GSM2055386': [1.0], 'GSM2055387': [1.0], 'GSM2055388': [1.0], 'GSM2055389': [1.0], 'GSM2055390': [1.0], 'GSM2055391': [1.0], 'GSM2055392': [1.0], 'GSM2055393': [1.0], 'GSM2055394': [1.0], 'GSM2055395': [1.0], 'GSM2055396': [1.0], 'GSM2055397': [1.0], 'GSM2055398': [1.0], 'GSM2055399': [1.0], 'GSM2055400': [1.0], 'GSM2055401': [1.0], 'GSM2055402': [1.0], 'GSM2055403': [1.0], 'GSM2055404': [0.0], 'GSM2055405': [0.0], 'GSM2055406': [0.0], 'GSM2055407': [0.0], 'GSM2055408': [0.0], 'GSM2055409': [0.0], 'GSM2055410': [0.0], 'GSM2055411': [0.0], 'GSM2055412': [0.0], 'GSM2055413': [0.0], 'GSM2055414': [0.0], 'GSM2055415': [0.0], 'GSM2055416': [0.0], 'GSM2055417': [0.0], 'GSM2055418': [0.0], 'GSM2055419': [0.0], 'GSM2055420': [0.0], 'GSM2055421': [0.0], 'GSM2055422': [0.0], 'GSM2055423': [0.0], 'GSM2055424': [0.0], 'GSM2055425': [0.0]}\n", "Clinical data saved to ../../output/preprocess/Hypertension/clinical_data/GSE77627.csv\n" ] } ], "source": [ "import pandas as pd\n", "import os\n", "import json\n", "from typing import Optional, Callable, Dict, Any\n", "\n", "# 1. Determine gene expression data availability\n", "# Based on the background information, this dataset contains transcriptomic data from liver tissue\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Identify rows for trait, age, and gender\n", "\n", "# For trait (Hypertension):\n", "# In this study, the dataset contains information about liver groups: HNL (normal), INCPH (idiopathic non-cirrhotic portal hypertension), and LC (cirrhosis)\n", "# Row 0 contains the liver group classification which can be used to identify hypertension status\n", "trait_row = 0 # 'liver group' is in row 0\n", "\n", "# Age and gender are not provided in the sample characteristics dictionary\n", "age_row = None\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion Functions\n", "\n", "def convert_trait(value):\n", " \"\"\"\n", " Convert liver group values to binary hypertension status.\n", " INCPH (idiopathic non-cirrhotic portal hypertension) -> 1 (has hypertension)\n", " LC (liver cirrhosis) and HNL (histologically normal livers) -> 0 (no hypertension)\n", " \"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if \":\" in value:\n", " parts = value.split(\":\", 1)\n", " value = parts[1].strip()\n", " \n", " # Convert to binary\n", " if \"INCPH\" in value:\n", " return 1 # INCPH is a type of portal hypertension\n", " elif \"HNL\" in value or \"LC\" in value:\n", " return 0 # Normal livers or cirrhosis without explicit portal hypertension\n", " else:\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"\n", " Placeholder function for age conversion (not used as age data is unavailable)\n", " \"\"\"\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"\n", " Placeholder function for gender conversion (not used as gender data is unavailable)\n", " \"\"\"\n", " return None\n", "\n", "# 3. Save Metadata - Conduct initial filtering\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", "if trait_row is not None:\n", " # Load clinical_data that was prepared in a previous step\n", " # Do not create a new DataFrame, we should be using the one from the previous step\n", " try:\n", " # Extract clinical features\n", " selected_clinical_df = geo_select_clinical_features(\n", " clinical_df=clinical_data, # clinical_data should exist from a previous step\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 DataFrame\n", " preview = preview_df(selected_clinical_df)\n", " print(\"Preview of selected clinical features:\")\n", " print(preview)\n", " \n", " # Save the clinical data\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", " except NameError:\n", " print(\"Error: clinical_data is not available from previous steps.\")\n" ] }, { "cell_type": "markdown", "id": "e576475a", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "09bf5707", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:49:50.519089Z", "iopub.status.busy": "2025-03-25T05:49:50.518973Z", "iopub.status.idle": "2025-03-25T05:49:50.705324Z", "shell.execute_reply": "2025-03-25T05:49:50.704886Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['ILMN_1343291', 'ILMN_1651209', 'ILMN_1651228', 'ILMN_1651229',\n", " 'ILMN_1651235', 'ILMN_1651236', 'ILMN_1651237', 'ILMN_1651238',\n", " 'ILMN_1651254', 'ILMN_1651260', 'ILMN_1651262', 'ILMN_1651268',\n", " 'ILMN_1651278', 'ILMN_1651282', 'ILMN_1651285', 'ILMN_1651286',\n", " 'ILMN_1651292', 'ILMN_1651303', 'ILMN_1651309', 'ILMN_1651315'],\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": "3fad1e07", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "7657e5bd", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:49:50.706646Z", "iopub.status.busy": "2025-03-25T05:49:50.706532Z", "iopub.status.idle": "2025-03-25T05:49:50.708372Z", "shell.execute_reply": "2025-03-25T05:49:50.708094Z" } }, "outputs": [], "source": [ "# These are Illumina probe IDs, not human gene symbols\n", "# They start with \"ILMN_\" which is a standard prefix for Illumina microarray probes\n", "# These need to be mapped to gene symbols for biological interpretation\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "f7b28608", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "30c63fa6", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:49:50.709440Z", "iopub.status.busy": "2025-03-25T05:49:50.709341Z", "iopub.status.idle": "2025-03-25T05:49:54.113860Z", "shell.execute_reply": "2025-03-25T05:49:54.113478Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['ILMN_3166687', 'ILMN_3165566', 'ILMN_3164811', 'ILMN_3165363', 'ILMN_3166511'], 'Transcript': ['ILMN_333737', 'ILMN_333646', 'ILMN_333584', 'ILMN_333628', 'ILMN_333719'], 'Species': ['ILMN Controls', 'ILMN Controls', 'ILMN Controls', 'ILMN Controls', 'ILMN Controls'], 'Source': ['ILMN_Controls', 'ILMN_Controls', 'ILMN_Controls', 'ILMN_Controls', 'ILMN_Controls'], 'Search_Key': ['ERCC-00162', 'ERCC-00071', 'ERCC-00009', 'ERCC-00053', 'ERCC-00144'], 'ILMN_Gene': ['ERCC-00162', 'ERCC-00071', 'ERCC-00009', 'ERCC-00053', 'ERCC-00144'], 'Source_Reference_ID': ['ERCC-00162', 'ERCC-00071', 'ERCC-00009', 'ERCC-00053', 'ERCC-00144'], 'RefSeq_ID': [nan, nan, nan, nan, nan], 'Entrez_Gene_ID': [nan, nan, nan, nan, nan], 'GI': [nan, nan, nan, nan, nan], 'Accession': ['DQ516750', 'DQ883654', 'DQ668364', 'DQ516785', 'DQ854995'], 'Symbol': ['ERCC-00162', 'ERCC-00071', 'ERCC-00009', 'ERCC-00053', 'ERCC-00144'], 'Protein_Product': [nan, nan, nan, nan, nan], 'Array_Address_Id': [5270161.0, 4260594.0, 7610424.0, 5260356.0, 2030196.0], 'Probe_Type': ['S', 'S', 'S', 'S', 'S'], 'Probe_Start': [12.0, 224.0, 868.0, 873.0, 130.0], 'SEQUENCE': ['CCCATGTGTCCAATTCTGAATATCTTTCCAGCTAAGTGCTTCTGCCCACC', 'GGATTAACTGCTGTGGTGTGTCATACTCGGCTACCTCCTGGTTTGGCGTC', 'GACCACGCCTTGTAATCGTATGACACGCGCTTGACACGACTGAATCCAGC', 'CTGCAATGCCATTAACAACCTTAGCACGGTATTTCCAGTAGCTGGTGAGC', 'CGTGCAGACAGGGATCGTAAGGCGATCCAGCCGGTATACCTTAGTCACAT'], 'Chromosome': [nan, nan, nan, nan, nan], 'Probe_Chr_Orientation': [nan, nan, nan, nan, nan], 'Probe_Coordinates': [nan, nan, nan, nan, nan], 'Cytoband': [nan, nan, nan, nan, nan], 'Definition': ['Methanocaldococcus jannaschii spike-in control MJ-500-33 genomic sequence', 'Synthetic construct clone NISTag13 external RNA control sequence', 'Synthetic construct clone TagJ microarray control', 'Methanocaldococcus jannaschii spike-in control MJ-1000-68 genomic sequence', 'Synthetic construct clone AG006.1100 external RNA control sequence'], 'Ontology_Component': [nan, nan, nan, nan, nan], 'Ontology_Process': [nan, nan, nan, nan, nan], 'Ontology_Function': [nan, nan, nan, nan, nan], 'Synonyms': [nan, nan, nan, nan, nan], 'Obsolete_Probe_Id': [nan, nan, nan, nan, nan], 'GB_ACC': ['DQ516750', 'DQ883654', 'DQ668364', 'DQ516785', 'DQ854995']}\n" ] } ], "source": [ "# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n", "gene_annotation = get_gene_annotation(soft_file)\n", "\n", "# 2. Use the 'preview_df' function from the library to preview the data and print out the results.\n", "print(\"Gene annotation preview:\")\n", "print(preview_df(gene_annotation))\n" ] }, { "cell_type": "markdown", "id": "59725621", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "0b20223a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:49:54.115178Z", "iopub.status.busy": "2025-03-25T05:49:54.115056Z", "iopub.status.idle": "2025-03-25T05:49:54.283874Z", "shell.execute_reply": "2025-03-25T05:49:54.283492Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of mapped gene expression data:\n", "Index(['A1BG', 'A1CF', 'A26C3', 'A2BP1', 'A2LD1', 'A2M', 'A2ML1', 'A3GALT2',\n", " 'A4GALT', 'A4GNT', 'AAA1', 'AAAS', 'AACS', 'AADAC', 'AADACL1',\n", " 'AADACL2', 'AADACL4', 'AADAT', 'AAGAB', 'AAK1'],\n", " dtype='object', name='Gene')\n" ] } ], "source": [ "# 1. Identify the columns in the gene annotation data that store gene identifiers and gene symbols\n", "probe_id_column = \"ID\" # Column containing the same identifiers as in gene_data\n", "gene_symbol_column = \"Symbol\" # Column containing the gene symbols\n", "\n", "# 2. Get the gene mapping dataframe using the two columns\n", "gene_mapping_df = get_gene_mapping(gene_annotation, prob_col=probe_id_column, gene_col=gene_symbol_column)\n", "\n", "# 3. Apply the gene mapping to convert probe-level measurements to gene expression data\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping_df)\n", "\n", "# Print the first few rows of the mapped gene expression data\n", "print(\"Preview of mapped gene expression data:\")\n", "print(gene_data.index[:20])\n" ] }, { "cell_type": "markdown", "id": "94e07c6f", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "29ddc8b3", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:49:54.285208Z", "iopub.status.busy": "2025-03-25T05:49:54.285091Z", "iopub.status.idle": "2025-03-25T05:50:04.572371Z", "shell.execute_reply": "2025-03-25T05:50:04.571815Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Hypertension/gene_data/GSE77627.csv\n", "Clinical data loaded from ../../output/preprocess/Hypertension/clinical_data/GSE77627.csv\n", "Clinical data shape: (1, 54)\n", "Clinical data preview:\n", "{'GSM2055372': [0.0], 'GSM2055373': [0.0], 'GSM2055374': [0.0], 'GSM2055375': [0.0], 'GSM2055376': [0.0], 'GSM2055377': [0.0], 'GSM2055378': [0.0], 'GSM2055379': [0.0], 'GSM2055380': [0.0], 'GSM2055381': [0.0], 'GSM2055382': [0.0], 'GSM2055383': [0.0], 'GSM2055384': [0.0], 'GSM2055385': [0.0], 'GSM2055386': [1.0], 'GSM2055387': [1.0], 'GSM2055388': [1.0], 'GSM2055389': [1.0], 'GSM2055390': [1.0], 'GSM2055391': [1.0], 'GSM2055392': [1.0], 'GSM2055393': [1.0], 'GSM2055394': [1.0], 'GSM2055395': [1.0], 'GSM2055396': [1.0], 'GSM2055397': [1.0], 'GSM2055398': [1.0], 'GSM2055399': [1.0], 'GSM2055400': [1.0], 'GSM2055401': [1.0], 'GSM2055402': [1.0], 'GSM2055403': [1.0], 'GSM2055404': [0.0], 'GSM2055405': [0.0], 'GSM2055406': [0.0], 'GSM2055407': [0.0], 'GSM2055408': [0.0], 'GSM2055409': [0.0], 'GSM2055410': [0.0], 'GSM2055411': [0.0], 'GSM2055412': [0.0], 'GSM2055413': [0.0], 'GSM2055414': [0.0], 'GSM2055415': [0.0], 'GSM2055416': [0.0], 'GSM2055417': [0.0], 'GSM2055418': [0.0], 'GSM2055419': [0.0], 'GSM2055420': [0.0], 'GSM2055421': [0.0], 'GSM2055422': [0.0], 'GSM2055423': [0.0], 'GSM2055424': [0.0], 'GSM2055425': [0.0]}\n", "Transposed clinical data preview:\n", "{'Hypertension': [0.0, 0.0, 0.0, 0.0, 0.0]}\n", "Linked data shape: (54, 19451)\n", "Linked data columns preview:\n", "Index(['Hypertension', 'A1BG', 'A1BG-AS1', 'A1CF', 'A2M', 'A2ML1', 'A3GALT2',\n", " 'A4GALT', 'A4GNT', 'AAA1'],\n", " dtype='object')\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (54, 19451)\n", "Unique values in trait column: [0. 1.]\n", "For the feature 'Hypertension', the least common label is '1.0' with 18 occurrences. This represents 33.33% of the dataset.\n", "The distribution of the feature 'Hypertension' in this dataset is fine.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Processed dataset saved to ../../output/preprocess/Hypertension/GSE77627.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. Load the previously saved clinical data instead of re-extracting it\n", "clinical_df = pd.read_csv(out_clinical_data_file)\n", "print(f\"Clinical data loaded from {out_clinical_data_file}\")\n", "print(f\"Clinical data shape: {clinical_df.shape}\")\n", "print(\"Clinical data preview:\")\n", "print(preview_df(clinical_df))\n", "\n", "# If the clinical data has unnamed index column (typical when saving without index=False),\n", "# let's set the first column as the index\n", "if clinical_df.columns[0] == 'Unnamed: 0':\n", " clinical_df = clinical_df.set_index(clinical_df.columns[0])\n", "\n", "# Ensure the trait column exists by transposing the clinical data\n", "# In our case, it was stored with samples as columns, traits as rows\n", "clinical_df = clinical_df.T\n", "clinical_df.columns = [trait] # Rename the column to match our trait variable\n", "print(\"Transposed clinical data preview:\")\n", "print(preview_df(clinical_df))\n", "\n", "# 3. Link the clinical and genetic data\n", "linked_data = pd.merge(clinical_df, normalized_gene_data.T, left_index=True, right_index=True, how='inner')\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "print(\"Linked data columns preview:\")\n", "print(linked_data.columns[:10]) # Show first 10 columns to verify structure\n", "\n", "# 4. Handle missing values in the linked data\n", "linked_data = handle_missing_values(linked_data, trait)\n", "print(f\"Data shape after handling missing values: {linked_data.shape}\")\n", "\n", "# Verify that the trait column has at least two unique values\n", "unique_trait_values = linked_data[trait].unique()\n", "print(f\"Unique values in trait column: {unique_trait_values}\")\n", "\n", "# 5. Determine whether the trait and some demographic features are severely biased\n", "is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "\n", "# 6. Conduct quality check and save the cohort information\n", "note = \"Dataset contains liver tissue samples with information about portal hypertension status.\"\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_trait_biased, \n", " df=unbiased_linked_data,\n", " note=note\n", ")\n", "\n", "# 7. If the linked data is usable, save it\n", "if is_usable:\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " unbiased_linked_data.to_csv(out_data_file)\n", " print(f\"Processed dataset saved to {out_data_file}\")\n", "else:\n", " print(\"Dataset not usable due to bias in trait distribution. 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 }