{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "47279c18", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:56:23.597238Z", "iopub.status.busy": "2025-03-25T03:56:23.597131Z", "iopub.status.idle": "2025-03-25T03:56:23.802401Z", "shell.execute_reply": "2025-03-25T03:56:23.802026Z" } }, "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 = \"Schizophrenia\"\n", "cohort = \"GSE165813\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Schizophrenia\"\n", "in_cohort_dir = \"../../input/GEO/Schizophrenia/GSE165813\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Schizophrenia/GSE165813.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Schizophrenia/gene_data/GSE165813.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Schizophrenia/clinical_data/GSE165813.csv\"\n", "json_path = \"../../output/preprocess/Schizophrenia/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "1577ac3c", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "be860916", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:56:23.803940Z", "iopub.status.busy": "2025-03-25T03:56:23.803774Z", "iopub.status.idle": "2025-03-25T03:56:23.910613Z", "shell.execute_reply": "2025-03-25T03:56:23.910225Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Astroblastomas exhibit radial glia stem cell lineages and differential expression of imprinted and X-inactivation escape genes [Affymetrix]\"\n", "!Series_summary\t\"Astroblastoma (AB) is an unusual brain tumor of unknown origin. We performed an integrated clinicogenomic analysis of 36 AB-like tumors. Lesions with MN1-BEND2 fusions demonstrated decreased promoter methylation and increased expression of IGF2-H19 and DLK1-DIO3 imprinted region genes. They also relatively overexpressed genes highly expressed during fetal brain development prior to 25 post-conception weeks (pcw), including genes enriched in ventricular zone radial glia (vRG), and generally presented in young children. Other tumors highly expressed MAP kinase pathway, PI3K pathway and X-inactivation escape genes. These and a third group of tumors tended to occur in young adults and showed enriched expression of outer radial glia (oRG) and truncated radial glia (tRG) genes, and genes highly expressed after 25 pcw. Many of the latter are involved in axonal migration or synaptic plasticity and are implicated in autism, schizophrenia and other cognitive disorders. Findings suggest that AB-like tumors arise in the context of epigenetic and genetic changes in neural progenitors during fetal and later brain development: early ependymal tumors with MN1-BEND2 fusions (EET-MN1) from vRG-derived progenitor cells, and MAPK/PI3K and classic astroblastomas from oRG- and tRG-derived progenitors, respectively. Lastly, we found that like EET-MN1, immature ependymal cells express IGF2 and may represent an important source of this growth factor in the fetal lateral ventricular zone neural stem cell niche.\"\n", "!Series_overall_design\t\"Sixteen AB-like tumors, four PTPR, six PXA and twenty-two control normal brain samples were analyzed. No replicates were possible due to limited available material from these rare tumors, however, the Affymetrix data was validated by comparison to RNAseq data for the same AB-like tumor and PTPR samples. Tumor sample types were compared to each other and to controls.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['tissue: tumor', 'tissue: brain'], 1: ['histological type: astroblastoma', 'histological type: PTPR', 'histological type: PXA', 'histological type: fetal hippocampus', 'histological type: fetal germinal matrix', 'histological type: fetal cerebellum', 'histological type: fetal cerebrum', 'histological type: adult frontal lobe', 'histological type: adult thalamus', 'histological type: adult amygdala', 'histological type: adult choroid plexus', 'histological type: pediatric hippocampus', 'histological type: pediatric thalamus', 'histological type: pediatric cerebellum']}\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": "dc19b983", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "68f0fcda", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:56:23.911874Z", "iopub.status.busy": "2025-03-25T03:56:23.911753Z", "iopub.status.idle": "2025-03-25T03:56:23.917782Z", "shell.execute_reply": "2025-03-25T03:56:23.917495Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Step 1: Examining gene expression data availability\n", "# Based on the background information, this dataset contains Affymetrix data (microarray gene expression)\n", "# The series title and summary mention gene expression analysis\n", "is_gene_available = True\n", "\n", "# Step 2: Variable availability and data type conversion\n", "\n", "# 2.1 Data Availability\n", "# Looking at the Sample Characteristics Dictionary:\n", "# There's no explicit trait (Schizophrenia) data in the sample characteristics\n", "# From the background information, this dataset is about astroblastomas and brain tumors, not directly related to schizophrenia\n", "trait_row = None # No schizophrenia trait data available\n", "\n", "# No age information is available in the sample characteristics\n", "age_row = None\n", "\n", "# No gender information is available in the sample characteristics\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion\n", "# Since none of the variables are available, we'll define placeholder conversion functions\n", "def convert_trait(value):\n", " if not value or pd.isna(value):\n", " return None\n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " # Not applicable for this dataset as it doesn't have schizophrenia trait data\n", " return None\n", "\n", "def convert_age(value):\n", " if not value or pd.isna(value):\n", " return None\n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " # Not applicable as age data is not available\n", " return None\n", "\n", "def convert_gender(value):\n", " if not value or pd.isna(value):\n", " return None\n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " # Not applicable as gender data is not available\n", " return None\n", "\n", "# Step 3: Save Metadata\n", "# Determine if trait data is available\n", "is_trait_available = trait_row is not None\n", "\n", "# Conduct initial filtering and save cohort information\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", "# Skip this step since trait_row is None (no clinical data available for schizophrenia)\n" ] }, { "cell_type": "markdown", "id": "c71d4d46", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "354770bb", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:56:23.918878Z", "iopub.status.busy": "2025-03-25T03:56:23.918769Z", "iopub.status.idle": "2025-03-25T03:56:24.062847Z", "shell.execute_reply": "2025-03-25T03:56:24.062502Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix file found: ../../input/GEO/Schizophrenia/GSE165813/GSE165813_series_matrix.txt.gz\n", "Gene data shape: (23937, 48)\n", "First 20 gene/probe identifiers:\n", "Index(['A1BG', 'A1BG-AS1', 'A1CF', 'A2M', 'A2M-AS1', 'A2ML1', 'A2MP1',\n", " 'A4GALT', 'A4GNT', 'AA06', 'AAAS', 'AACS', 'AACSP1', 'AADAC', 'AADACL2',\n", " 'AADACL3', 'AADACL4', 'AADACP1', 'AADAT', 'AAGAB'],\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": "cd34c34d", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "af1675bc", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:56:24.063958Z", "iopub.status.busy": "2025-03-25T03:56:24.063846Z", "iopub.status.idle": "2025-03-25T03:56:24.065775Z", "shell.execute_reply": "2025-03-25T03:56:24.065494Z" } }, "outputs": [], "source": [ "# Looking at the identifiers, these appear to be standard human gene symbols\n", "# For example: A1BG, A2M, AADAT, etc. are recognized human gene symbols\n", "# No mapping should be required as these are already in the correct format\n", "\n", "requires_gene_mapping = False\n" ] }, { "cell_type": "markdown", "id": "cadeb121", "metadata": {}, "source": [ "### Step 5: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 6, "id": "dfd4bea9", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:56:24.066878Z", "iopub.status.busy": "2025-03-25T03:56:24.066775Z", "iopub.status.idle": "2025-03-25T03:56:24.889635Z", "shell.execute_reply": "2025-03-25T03:56:24.889294Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape after normalization: (23721, 48)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene expression data saved to ../../output/preprocess/Schizophrenia/gene_data/GSE165813.csv\n", "No Schizophrenia trait data available in this dataset. Skipping clinical data processing.\n", "Abnormality detected in the cohort: GSE165813. Preprocessing failed.\n", "Dataset is not usable for Schizophrenia analysis. No linked data file saved.\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Gene data shape after normalization: {gene_data.shape}\")\n", "\n", "# Save the normalized gene data to file\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\"Normalized gene expression data saved to {out_gene_data_file}\")\n", "\n", "# 2-6. Process based on trait availability\n", "# Since trait_row is None (determined in Step 2), we cannot create proper clinical data\n", "# Skip clinical data processing and mark the dataset as not usable for Schizophrenia analysis\n", "\n", "if trait_row is None:\n", " print(\"No Schizophrenia trait data available in this dataset. Skipping clinical data processing.\")\n", " \n", " # Create an empty DataFrame as placeholder for linked_data\n", " linked_data = pd.DataFrame()\n", " \n", " # Validate and save cohort information (with is_trait_available=False)\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=False, # We confirmed trait data is not available\n", " is_biased=False, # Providing a default value as required by the function\n", " df=linked_data,\n", " note=\"Dataset contains gene expression data but no Schizophrenia-related trait information.\"\n", " )\n", " \n", " print(\"Dataset is not usable for Schizophrenia analysis. No linked data file saved.\")\n", "else:\n", " # This branch won't execute since trait_row is None, but including it for completeness\n", " # Process clinical data\n", " selected_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", " # Link clinical and genetic data\n", " linked_data = geo_link_clinical_genetic_data(selected_clinical_df, gene_data)\n", " \n", " # Handle missing values\n", " linked_data = handle_missing_values(linked_data, trait)\n", " \n", " # Check for bias in features\n", " is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", " \n", " # Validate and save 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_biased,\n", " df=linked_data,\n", " note=\"\"\n", " )\n", " \n", " # Save the linked data if usable\n", " if is_usable and not linked_data.empty:\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 is not usable for analysis. No linked data file 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 }