{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "bb2564ae", "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 = \"Head_and_Neck_Cancer\"\n", "cohort = \"GSE104006\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Head_and_Neck_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Head_and_Neck_Cancer/GSE104006\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Head_and_Neck_Cancer/GSE104006.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Head_and_Neck_Cancer/gene_data/GSE104006.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Head_and_Neck_Cancer/clinical_data/GSE104006.csv\"\n", "json_path = \"../../output/preprocess/Head_and_Neck_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "1ed4d88b", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": null, "id": "9d3d0929", "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": "04323746", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": null, "id": "b0a0605b", "metadata": {}, "outputs": [], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the series title which mentions \"miRNA and gene expression profiling\", \n", "# we can infer that gene expression data is available\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# For trait: Looking at key 0, we see disease status which matches our Head_and_Neck_Cancer trait\n", "trait_row = 0\n", "\n", "# For age: The age information is available in key 2\n", "age_row = 2\n", "\n", "# For gender: Gender information is available in key 3 as \"Sex\"\n", "gender_row = 3\n", "\n", "# 2.2 Data Type Conversion Functions\n", "def convert_trait(value):\n", " \"\"\"Convert trait value to binary format (0 for control, 1 for case)\"\"\"\n", " if pd.isna(value) or value is None:\n", " return None\n", " \n", " # Extract value after colon and strip spaces\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " # For Head and Neck Cancer, map thyroid carcinoma to case (1) and non-neoplastic to control (0)\n", " if \"Thyroid_carcinoma\" in value or \"carcinoma\" in value.lower():\n", " return 1\n", " elif \"Non-neoplastic\" in value:\n", " return 0\n", " else:\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age value to continuous format\"\"\"\n", " if pd.isna(value) or value is None:\n", " return None\n", " \n", " # Extract value after colon and strip spaces\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " try:\n", " return float(value)\n", " except (ValueError, TypeError):\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender value to binary format (0 for female, 1 for male)\"\"\"\n", " if pd.isna(value) or value is None:\n", " return None\n", " \n", " # Extract value after colon and strip spaces\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \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", "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_features_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 dataframe\n", " preview = preview_df(clinical_features_df)\n", " print(\"Preview of clinical features dataframe:\")\n", " print(preview)\n", " \n", " # Save the clinical data\n", " clinical_features_df.to_csv(out_clinical_data_file)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "fd69a911", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": null, "id": "30e48cc6", "metadata": {}, "outputs": [], "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": "57c9db6e", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": null, "id": "16c094a3", "metadata": {}, "outputs": [], "source": [ "# Analyzing the gene identifiers in the dataset\n", "\n", "# Looking at the first 20 identifiers, they include:\n", "# - 'Blank' (likely control spots)\n", "# - 'dmr_X' prefixed identifiers (likely custom probes)\n", "# - 'hsa-let-X' prefixed identifiers (these are human microRNA identifiers)\n", "\n", "# These identifiers appear to be microRNA (miRNA) names/IDs rather than standard human gene symbols\n", "# The 'hsa-' prefix indicates human (Homo sapiens) miRNAs\n", "# This is a miRNA profiling dataset, not a standard gene expression dataset\n", "\n", "# Since these are already standard miRNA identifiers, no mapping to gene symbols is needed\n", "# We would use these miRNA identifiers directly in analysis\n", "\n", "requires_gene_mapping = False\n" ] }, { "cell_type": "markdown", "id": "de0e7ce3", "metadata": {}, "source": [ "### Step 5: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": null, "id": "b8a798fc", "metadata": {}, "outputs": [], "source": [ "# 1. Save the gene data (miRNA data in this case) since we determined no mapping is needed\n", "print(f\"Gene data shape: {gene_data.shape}\")\n", "\n", "# Save the gene expression data\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "gene_data.to_csv(out_gene_data_file)\n", "print(f\"Gene expression data saved to {out_gene_data_file}\")\n", "\n", "# 2. Use the clinical data we extracted in step 2\n", "if 'clinical_features_df' not in locals():\n", " # Get clinical data from matrix file again if needed\n", " background_info, clinical_data = get_background_and_clinical_data(matrix_file)\n", " \n", " # Define proper conversion functions for the trait\n", " def convert_trait(value):\n", " \"\"\"Convert histology information to binary trait values (0: control, 1: cancer)\"\"\"\n", " if not isinstance(value, str):\n", " return None\n", " value = value.lower().split(': ')[-1] # Extract value after colon\n", " \n", " # Map to binary: 1 for any tumor tissue (PDTC/PTC variants), 0 for non-neoplastic\n", " if 'non-neoplastic_thyroid' in value:\n", " return 0 # Control\n", " elif 'ptc' in value or 'pdtc' in value:\n", " return 1 # Cancer\n", " else:\n", " return None # Unclear\n", " \n", " # Use the trait_row, age_row, and gender_row from step 2\n", " clinical_features_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 clinical data shape and preview\n", "print(\"Clinical data shape:\", clinical_features_df.shape)\n", "print(\"Clinical data preview:\", preview_df(clinical_features_df))\n", "\n", "# 3. Link clinical and genetic data\n", "linked_data = geo_link_clinical_genetic_data(clinical_features_df, gene_data)\n", "print(f\"Linked data shape before handling missing values: {linked_data.shape}\")\n", "\n", "# 4. Handle missing values\n", "linked_data = handle_missing_values(linked_data, trait)\n", "print(f\"Linked data shape after handling missing values: {linked_data.shape}\")\n", "\n", "# 5. Evaluate bias in trait and demographic features\n", "is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "\n", "# 6. Conduct final quality validation\n", "note = \"Dataset contains thyroid carcinoma expression data (PDTC/PTC), which is relevant for head and neck cancer. This is a miRNA dataset.\"\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", "# 7. Save 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 deemed not usable due to quality issues - linked data not saved\")" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 5 }