{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "bb8a37fb", "metadata": {}, "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 = \"GSE256539\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Hypertension\"\n", "in_cohort_dir = \"../../input/GEO/Hypertension/GSE256539\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Hypertension/GSE256539.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Hypertension/gene_data/GSE256539.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Hypertension/clinical_data/GSE256539.csv\"\n", "json_path = \"../../output/preprocess/Hypertension/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "f6745206", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": null, "id": "5d1e7f20", "metadata": {}, "outputs": [], "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": "28327849", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": null, "id": "0f38482a", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import os\n", "import json\n", "from typing import Optional, Callable, Dict, Any\n", "\n", "# Step 1: Gene Expression Data Availability\n", "# Based on the background information, this is a digital spatial transcriptomics dataset\n", "# It mentions \"whole genome sequencing\" and \"genome-wide differential transcriptomic signature\"\n", "# So it's likely to contain gene expression data\n", "is_gene_available = True\n", "\n", "# Step 2: Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# From the sample characteristics dictionary, we don't see explicit trait (hypertension), age, or gender information\n", "# The dataset seems to be comparing IPAH vs control samples, but individual characteristics are not provided\n", "# Based on the background information, the dataset contains IPAH (Idiopathic Pulmonary Arterial Hypertension) patients vs. controls\n", "\n", "# Let's look for trait (Hypertension) data\n", "# Since the dataset is about IPAH, we could potentially infer IPAH vs control from the \"individuial\" field\n", "# However, there's no clear indication which individuals are cases vs controls\n", "trait_row = None # No explicit trait information available for individuals\n", "\n", "# Age data\n", "age_row = None # No age information available\n", "\n", "# Gender data\n", "gender_row = None # No gender information available\n", "\n", "# 2.2 Data Type Conversion\n", "# Even though we don't have this information, we'll define placeholder conversion functions\n", "\n", "def convert_trait(value: str) -> Optional[int]:\n", " \"\"\"Convert trait value to binary format (0: control, 1: case)\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Since we don't have explicit trait information, return None\n", " return None\n", "\n", "def convert_age(value: str) -> Optional[float]:\n", " \"\"\"Convert age value to continuous format\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Since we don't have age information, return None\n", " return None\n", "\n", "def convert_gender(value: str) -> Optional[int]:\n", " \"\"\"Convert gender value to binary format (0: female, 1: male)\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Since we don't have gender information, return None\n", " return None\n", "\n", "# Step 3: Save Metadata\n", "# Initial filtering based on trait and gene availability\n", "is_trait_available = trait_row is not None\n", "validate_and_save_cohort_info(\n", " is_final=False,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=is_gene_available,\n", " is_trait_available=is_trait_available\n", ")\n", "\n", "# Step 4: Clinical Feature Extraction\n", "# Since trait_row is None, we skip this step\n", "# If trait_row were not None, we would execute:\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", "# preview = preview_df(clinical_df)\n", "# print(\"Clinical data preview:\", preview)\n", "# os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", "# clinical_df.to_csv(out_clinical_data_file, index=False)\n" ] }, { "cell_type": "markdown", "id": "e9207fb1", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": null, "id": "f29a9ae7", "metadata": {}, "outputs": [], "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": "2d0b1a14", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": null, "id": "7db5e7dd", "metadata": {}, "outputs": [], "source": [ "# These identifiers appear to be human gene symbols.\n", "# A2M is Alpha-2-Macroglobulin\n", "# A4GALT is Alpha 1,4-Galactosyltransferase\n", "# AAAS is Aladin WD Repeat Nucleoporin\n", "# AACS is Acetoacetyl-CoA Synthetase\n", "# etc.\n", "\n", "# These are standard HGNC gene symbols for human genes, not probe IDs or other types of identifiers\n", "# that would require mapping to gene symbols.\n", "\n", "requires_gene_mapping = False\n" ] }, { "cell_type": "markdown", "id": "46942177", "metadata": {}, "source": [ "### Step 5: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": null, "id": "8c3848fc", "metadata": {}, "outputs": [], "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. Since trait_row is None, as determined in Step 2, we cannot extract clinical features.\n", "# Therefore, we cannot link clinical and genetic data.\n", "# We'll proceed with the gene data only and properly report the absence of trait data.\n", "\n", "# Check if trait data is available (from Step 2)\n", "is_trait_available = trait_row is not None\n", "print(f\"Trait data availability: {is_trait_available}\")\n", "\n", "# 3. Since there is no trait data, we'll create a minimal dataframe with only gene data\n", "# to satisfy the requirements of validate_and_save_cohort_info\n", "minimal_df = pd.DataFrame(index=normalized_gene_data.columns[:5], \n", " data={gene: normalized_gene_data.loc[gene, :5] \n", " for gene in normalized_gene_data.index[:5]})\n", "\n", "# We'll set is_biased to False since there's no trait to evaluate bias\n", "is_biased = False\n", "\n", "# 4. Conduct quality check and save the cohort information\n", "note = \"Dataset contains gene expression data from IPAH and control samples, but lacks individual trait, age, and gender annotations needed for associational studies.\"\n", "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=is_trait_available, \n", " is_biased=is_biased,\n", " df=minimal_df,\n", " note=note\n", ")\n", "\n", "print(\"Dataset is not usable for trait-gene association studies due to missing trait information.\")\n" ] }, { "cell_type": "markdown", "id": "7c1e025b", "metadata": {}, "source": [ "### Step 6: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": null, "id": "56e6b2e2", "metadata": {}, "outputs": [], "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. Since trait_row is None, as determined in Step 2, we cannot extract clinical features.\n", "# Therefore, we cannot link clinical and genetic data.\n", "# We'll proceed with the gene data only and properly report the absence of trait data.\n", "\n", "# Check if trait data is available (from Step 2)\n", "is_trait_available = trait_row is not None\n", "print(f\"Trait data availability: {is_trait_available}\")\n", "\n", "# 3. Create a minimal dataframe with only gene data to satisfy the requirements for validation\n", "# We'll use a minimal representation of the gene data\n", "minimal_df = pd.DataFrame(index=normalized_gene_data.columns[:5], \n", " columns=normalized_gene_data.index[:5])\n", "\n", "# 4. Conduct quality check and save the cohort information\n", "note = \"Dataset contains gene expression data from IPAH and control samples, but lacks individual trait, age, and gender annotations needed for associational studies.\"\n", "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=is_trait_available, \n", " is_biased=False, # Set to False since there's no trait to evaluate bias\n", " df=minimal_df,\n", " note=note\n", ")\n", "\n", "print(\"Dataset is not usable for trait-gene association studies due to missing trait information.\")" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 5 }