{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "3b3b5a7d", "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 = \"Pheochromocytoma_and_Paraganglioma\"\n", "cohort = \"GSE67066\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Pheochromocytoma_and_Paraganglioma\"\n", "in_cohort_dir = \"../../input/GEO/Pheochromocytoma_and_Paraganglioma/GSE67066\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Pheochromocytoma_and_Paraganglioma/GSE67066.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Pheochromocytoma_and_Paraganglioma/gene_data/GSE67066.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Pheochromocytoma_and_Paraganglioma/clinical_data/GSE67066.csv\"\n", "json_path = \"../../output/preprocess/Pheochromocytoma_and_Paraganglioma/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "8179c186", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": null, "id": "359267ea", "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": "0d9e7cbe", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": null, "id": "be582542", "metadata": {}, "outputs": [], "source": [ "# 1. Assess gene expression data availability\n", "is_gene_available = True # From the background information, we can see that mRNA expression arrays were performed\n", "\n", "# 2. Assess variable availability and create conversion functions\n", "\n", "# 2.1 Trait (Malignancy)\n", "trait_row = 1 # \"tissue type: benign\" or \"tissue type: malignant\"\n", "\n", "def convert_trait(value):\n", " if not isinstance(value, str):\n", " return None\n", " value = value.lower()\n", " if 'malignant' in value:\n", " return 1 # Malignant\n", " elif 'benign' in value:\n", " return 0 # Benign\n", " return None\n", "\n", "# 2.2 Age\n", "age_row = None # Age information is not available in the sample characteristics\n", "\n", "def convert_age(value):\n", " # Placeholder function since age data is not available\n", " return None\n", "\n", "# 2.3 Gender\n", "gender_row = None # Gender information is not available in the sample characteristics\n", "\n", "def convert_gender(value):\n", " # Placeholder function since gender data is not available\n", " return None\n", "\n", "# 3. Save metadata (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. Extract clinical features if trait data is available\n", "if trait_row is not None:\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 clinical dataframe\n", " preview = preview_df(clinical_df)\n", " print(\"Clinical data preview:\")\n", " print(preview)\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\n", " clinical_df.to_csv(out_clinical_data_file, index=False)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "7592e1c4", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": null, "id": "8ff7f96e", "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": "cbefd162", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": null, "id": "7040577b", "metadata": {}, "outputs": [], "source": [ "# Reviewing the gene identifiers\n", "# These look like Affymetrix probe IDs (for example \"1007_s_at\", \"1053_at\") rather than standard human gene symbols\n", "# Human gene symbols would typically be like BRCA1, TP53, etc.\n", "# Affymetrix IDs need to be mapped to gene symbols for biological interpretation\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "45c9ad20", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": null, "id": "b1ec1164", "metadata": {}, "outputs": [], "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": "d55361c2", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": null, "id": "6eaf93d1", "metadata": {}, "outputs": [], "source": [ "# Looking at the gene annotation data, we can see that:\n", "# - \"ID\" contains Affymetrix probe IDs like '1007_s_at', '1053_at', etc. (same format as in gene_data)\n", "# - \"Gene Symbol\" contains human gene symbols like 'DDR1 /// MIR4640', 'RFC2', etc.\n", "\n", "# 1. Identify the columns in the annotation data for mapping\n", "probe_col = 'ID' # Column with probe identifiers\n", "gene_col = 'Gene Symbol' # Column with gene symbols\n", "\n", "# 2. Create the gene mapping dataframe\n", "gene_mapping = get_gene_mapping(gene_annotation, probe_col, gene_col)\n", "\n", "# Preview the mapping dataframe\n", "print(\"Gene mapping preview:\")\n", "print(preview_df(gene_mapping))\n", "\n", "# 3. Apply the gene mapping to convert probe-level data to gene-level data\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "\n", "# Preview the gene expression data after mapping\n", "print(\"\\nGene expression data after mapping (first 5 genes, 5 samples):\")\n", "preview_gene_data = gene_data.iloc[:5, :5]\n", "print(preview_gene_data)\n" ] }, { "cell_type": "markdown", "id": "b99eeff0", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": null, "id": "bf4bc5b5", "metadata": {}, "outputs": [], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "\n", "# Save the normalized 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 clinical data from step 2\n", "clinical_data = pd.read_csv(out_clinical_data_file)\n", "# Convert to transposed format for linking (samples as columns)\n", "clinical_df = pd.DataFrame(clinical_data.values.T, columns=[trait], index=clinical_data.columns)\n", "print(f\"Loaded clinical data with shape: {clinical_df.shape}\")\n", "\n", "# Link the clinical and genetic data on sample IDs\n", "linked_data = geo_link_clinical_genetic_data(clinical_df, normalized_gene_data)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "\n", "# 3. Handle missing values in the linked data\n", "linked_data = handle_missing_values(linked_data, trait)\n", "print(f\"Shape after handling missing values: {linked_data.shape}\")\n", "\n", "# 4. Determine whether the trait and demographic features are severely biased\n", "is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "\n", "# 5. Conduct quality check and save the cohort information\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=f\"Dataset contains gene expression data for pheochromocytoma/paraganglioma samples with benign/malignant classification.\"\n", ")\n", "\n", "# 6. Save the data if it's 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", " # Save the data\n", " unbiased_linked_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", "else:\n", " print(f\"Data quality check failed. The dataset is not suitable for association studies.\")\n" ] }, { "cell_type": "markdown", "id": "d460b567", "metadata": {}, "source": [ "### Step 8: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": null, "id": "cb384ee6", "metadata": {}, "outputs": [], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "\n", "# Save the normalized 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 actual clinical data that was created in step 2\n", "clinical_data = pd.read_csv(out_clinical_data_file)\n", "# Since the clinical data's columns are sample IDs and the only row is the trait data\n", "# We need to transpose it to have samples as rows and traits as columns\n", "clinical_df = pd.DataFrame(clinical_data.T.values, \n", " index=clinical_data.columns,\n", " columns=[trait])\n", "print(f\"Loaded clinical data with shape: {clinical_df.shape}\")\n", "\n", "# Link the clinical and genetic data on sample IDs\n", "linked_data = pd.concat([clinical_df, normalized_gene_data.T], axis=1)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "\n", "# 3. Handle missing values in the linked data\n", "linked_data = handle_missing_values(linked_data, trait)\n", "print(f\"Shape after handling missing values: {linked_data.shape}\")\n", "\n", "# 4. Determine whether the trait and demographic features are severely biased\n", "is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "\n", "# 5. Conduct quality check and save the cohort information\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=f\"Dataset contains gene expression data for pheochromocytoma/paraganglioma samples with benign/malignant classification.\"\n", ")\n", "\n", "# 6. Save the data if it's 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", " # Save the data\n", " unbiased_linked_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", "else:\n", " print(f\"Data quality check failed. The dataset is not suitable for association studies.\")" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 5 }