{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "f86fa2e5", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:14:47.577765Z", "iopub.status.busy": "2025-03-25T06:14:47.577585Z", "iopub.status.idle": "2025-03-25T06:14:47.743238Z", "shell.execute_reply": "2025-03-25T06:14:47.742906Z" } }, "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 = \"Post-Traumatic_Stress_Disorder\"\n", "cohort = \"GSE52875\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Post-Traumatic_Stress_Disorder\"\n", "in_cohort_dir = \"../../input/GEO/Post-Traumatic_Stress_Disorder/GSE52875\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Post-Traumatic_Stress_Disorder/GSE52875.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Post-Traumatic_Stress_Disorder/gene_data/GSE52875.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Post-Traumatic_Stress_Disorder/clinical_data/GSE52875.csv\"\n", "json_path = \"../../output/preprocess/Post-Traumatic_Stress_Disorder/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "4d0ac56c", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "d2063d8f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:14:47.744650Z", "iopub.status.busy": "2025-03-25T06:14:47.744512Z", "iopub.status.idle": "2025-03-25T06:14:47.771018Z", "shell.execute_reply": "2025-03-25T06:14:47.770753Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Expression signatures in heart tissues of mice simulating posttraumatic stress disorder (PTSD)\"\n", "!Series_summary\t\"This SuperSeries is composed of the SubSeries listed below.\"\n", "!Series_overall_design\t\"Refer to individual Series\"\n", "Sample Characteristics Dictionary:\n", "{0: ['strain: C57BL/6'], 1: ['tissue: Control_heart_tissue, C10R42', 'tissue: Control_heart_tissue, C10R1', 'tissue: Stressed_heart_tissue, T10R1', 'tissue: Control_heart_tissue, C5R1', 'tissue: Control_heart_tissue, C5R10', 'tissue: Stressed_heart_tissue, T10R42', 'tissue: Stressed_heart_tissue, T5R10', 'tissue: Stressed_heart_tissue, T5R1']}\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": "28f2eb94", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "3126a204", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:14:47.772226Z", "iopub.status.busy": "2025-03-25T06:14:47.772124Z", "iopub.status.idle": "2025-03-25T06:14:47.778673Z", "shell.execute_reply": "2025-03-25T06:14:47.778435Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 1. Determine Gene Expression Data Availability\n", "# Based on the background info and sample characteristics, this dataset appears to be related to gene expression \n", "# in heart tissues of mice simulating PTSD. The title suggests it's an expression study.\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "\n", "# 2.1 Data Availability\n", "# Looking at the sample characteristics dictionary, we don't see explicit trait (PTSD),\n", "# age, or gender information. The dictionary only contains strain and tissue information.\n", "trait_row = None # No explicit PTSD status in the sample characteristics\n", "age_row = None # No age information available\n", "gender_row = None # No gender information available\n", "\n", "# 2.2 Data Type Conversion Functions\n", "# Since we don't have these variables in the data, we'll define placeholder functions\n", "# that would be appropriate if the data were available\n", "def convert_trait(value):\n", " \"\"\"\n", " Convert PTSD status to binary format.\n", " Expected format: \"status: value\"\n", " \"\"\"\n", " if value is None:\n", " return None\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip().lower()\n", " else:\n", " value = value.lower()\n", " \n", " if value in ['ptsd', 'yes', 'true', 'case', 'positive', '1']:\n", " return 1\n", " elif value in ['control', 'no', 'false', 'normal', 'negative', '0']:\n", " return 0\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"\n", " Convert age to continuous format.\n", " Expected format: \"age: value\"\n", " \"\"\"\n", " if value is None:\n", " return None\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " try:\n", " return float(value)\n", " except:\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"\n", " Convert gender to binary format (0=female, 1=male).\n", " Expected format: \"gender: value\" or \"sex: value\"\n", " \"\"\"\n", " if value is None:\n", " return None\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip().lower()\n", " else:\n", " value = value.lower()\n", " \n", " if value in ['male', 'm', 'man', 'boy', '1']:\n", " return 1\n", " elif value in ['female', 'f', 'woman', 'girl', '0']:\n", " return 0\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", "\n", "# Save the initial filtering metadata\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", "# Since trait_row is None, we should skip the clinical feature extraction\n", "# The dataset appears to be a mouse model study without explicit trait classification in the sample characteristics\n" ] }, { "cell_type": "markdown", "id": "f057cdbe", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "4702f582", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:14:47.779804Z", "iopub.status.busy": "2025-03-25T06:14:47.779705Z", "iopub.status.idle": "2025-03-25T06:14:47.793475Z", "shell.execute_reply": "2025-03-25T06:14:47.793222Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "First 20 gene/probe identifiers:\n", "Index(['-1', '10138', '10306', '105441', '10899', '10901', '10902', '10903',\n", " '10904', '10905', '10906', '10907', '10916', '10919', '10923', '10925',\n", " '10928', '10936', '10937', '10942'],\n", " dtype='object', name='ID')\n", "\n", "Gene data dimensions: 2025 genes × 32 samples\n" ] } ], "source": [ "# 1. Re-identify the SOFT and matrix files to ensure we have the correct paths\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. Extract the gene expression data from the matrix file\n", "gene_data = get_genetic_data(matrix_file)\n", "\n", "# 3. Print the first 20 row IDs (gene or probe identifiers)\n", "print(\"\\nFirst 20 gene/probe identifiers:\")\n", "print(gene_data.index[:20])\n", "\n", "# 4. Print the dimensions of the gene expression data\n", "print(f\"\\nGene data dimensions: {gene_data.shape[0]} genes × {gene_data.shape[1]} samples\")\n", "\n", "# Note: we keep is_gene_available as True since we successfully extracted gene expression data\n", "is_gene_available = True\n" ] }, { "cell_type": "markdown", "id": "c0919029", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "8d6fd0c9", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:14:47.794604Z", "iopub.status.busy": "2025-03-25T06:14:47.794506Z", "iopub.status.idle": "2025-03-25T06:14:47.796132Z", "shell.execute_reply": "2025-03-25T06:14:47.795856Z" } }, "outputs": [], "source": [ "# Looking at the gene identifiers, these appear to be simple numeric values (1, 2, 3, etc.)\n", "# These are not standard human gene symbols like BRCA1, TP53, etc.\n", "# These are likely probe IDs or some other identifier that needs to be mapped to gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "584e7103", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "adda72ce", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:14:47.797230Z", "iopub.status.busy": "2025-03-25T06:14:47.797134Z", "iopub.status.idle": "2025-03-25T06:14:50.939949Z", "shell.execute_reply": "2025-03-25T06:14:50.939581Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['10916', '10998', '42918', '17883', '10997'], 'name': ['hsa-miR-1/mmu-miR-1', 'hsa-miR-19b/mmu-miR-19b/rno-miR-19b', 'hsa-miR-19b-2*', 'hsa-miR-19b-1*', 'hsa-miR-19a/mmu-miR-19a/rno-miR-19a'], 'accession': ['MIMAT0000416/MIMAT0000123', 'MIMAT0000074/MIMAT0000513/MIMAT0000788', 'MIMAT0004492', 'MIMAT0004491', 'MIMAT0000073/MIMAT0000651/MIMAT0000789'], 'miRNA_LIST': ['hsa-miR-1/mmu-miR-1', 'hsa-miR-19b/mmu-miR-19b/rno-miR-19b', 'hsa-miR-19b-2*', 'hsa-miR-19b-1*', 'hsa-miR-19a/mmu-miR-19a/rno-miR-19a'], 'SEQUENCE': ['UGGAAUGUAAAGAAGUAUGUAU', 'UGUGCAAAUCCAUGCAAAACUGA', 'AGUUUUGCAGGUUUGCAUUUCA', 'AGUUUUGCAGGUUUGCAUCCAGC', 'UGUGCAAAUCUAUGCAAAACUGA'], 'database': ['miRBase 14.0', 'miRBase 14.0', 'miRBase 14.0', 'miRBase 14.0', 'miRBase 14.0'], 'SPOT_ID': [nan, nan, nan, nan, nan]}\n" ] } ], "source": [ "# 1. First get the file paths using geo_get_relevant_filepaths function\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. 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", "# 3. 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": "d9a4be76", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "cf3459ae", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:14:50.941435Z", "iopub.status.busy": "2025-03-25T06:14:50.941319Z", "iopub.status.idle": "2025-03-25T06:14:50.959509Z", "shell.execute_reply": "2025-03-25T06:14:50.959221Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Column names in gene_annotation: ['ID', 'name', 'accession', 'miRNA_LIST', 'SEQUENCE', 'database', 'SPOT_ID']\n", "\n", "This appears to be a miRNA dataset, not gene expression data\n", "\n", "Dataset rejected: This dataset contains miRNA data from mice rather than human gene expression data.\n" ] } ], "source": [ "# 1. Based on the gene annotation preview, this dataset contains miRNA data, not gene expression data\n", "# The \"ID\" column does match the numeric identifiers in the gene expression data\n", "# However, miRNA_LIST contains miRNA identifiers, not human gene symbols\n", "print(\"Column names in gene_annotation:\", gene_annotation.columns.tolist())\n", "print(\"\\nThis appears to be a miRNA dataset, not gene expression data\")\n", "\n", "# This is a critical issue as our pipeline requires gene expression data that can be mapped to human gene symbols\n", "# According to the background info, this is a mouse study with miRNA data\n", "\n", "# Update our assessment of gene data availability\n", "is_gene_available = False # miRNA data is not suitable for our gene expression analysis pipeline\n", "\n", "# Save the updated metadata reflecting that this dataset doesn't contain usable gene expression data\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", "print(\"\\nDataset rejected: This dataset contains miRNA data from mice rather than human gene expression data.\")" ] } ], "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 }