{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "b793ca62", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:06:49.902349Z", "iopub.status.busy": "2025-03-25T06:06:49.901908Z", "iopub.status.idle": "2025-03-25T06:06:50.071411Z", "shell.execute_reply": "2025-03-25T06:06:50.070906Z" } }, "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 = \"Pancreatic_Cancer\"\n", "cohort = \"GSE223409\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Pancreatic_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Pancreatic_Cancer/GSE223409\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Pancreatic_Cancer/GSE223409.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Pancreatic_Cancer/gene_data/GSE223409.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Pancreatic_Cancer/clinical_data/GSE223409.csv\"\n", "json_path = \"../../output/preprocess/Pancreatic_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "120d1607", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "2323836c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:06:50.073094Z", "iopub.status.busy": "2025-03-25T06:06:50.072933Z", "iopub.status.idle": "2025-03-25T06:06:50.182186Z", "shell.execute_reply": "2025-03-25T06:06:50.181759Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Dual targeted extracellular vesicles regulating oncogenic genes in pancreatic cancer\"\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: ['cell type: Mouse Embryonic Fibroblast (MEF)', 'cell type: Bone Marrow Stem Cells (BMSCs)'], 1: ['tissue: extracellular vesicles (EVs)'], 2: ['treatment: Plasmids cd64_TP53 treated', 'treatment: PBS treated', 'treatment: Control']}\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": "d006c2cc", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "128d8540", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:06:50.183546Z", "iopub.status.busy": "2025-03-25T06:06:50.183430Z", "iopub.status.idle": "2025-03-25T06:06:50.188917Z", "shell.execute_reply": "2025-03-25T06:06:50.188537Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Analysis of dataset GSE223409 for Pancreatic Cancer study\n", "\n", "# 1. Gene Expression Data Availability\n", "# Based on the background information, this seems to be about EVs and gene expression\n", "# The series contains information about extracellular vesicles and TP53 gene regulation\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# Looking at the sample characteristics dictionary:\n", "\n", "# 2.1 Trait (Pancreatic Cancer)\n", "# There is no direct mention of pancreatic cancer status in the sample characteristics\n", "# This appears to be an experimental dataset with different treatments rather than patient samples\n", "trait_row = None # No trait information available\n", "\n", "# Since we don't have trait information, we'll define a placeholder function\n", "def convert_trait(value):\n", " return None\n", "\n", "# 2.2 Age\n", "# No age information is provided in the sample characteristics\n", "age_row = None # No age information available\n", "\n", "def convert_age(value):\n", " return None\n", "\n", "# 2.3 Gender\n", "# No gender information is provided in the sample characteristics\n", "gender_row = None # No gender information available\n", "\n", "def convert_gender(value):\n", " return None\n", "\n", "# 3. Save Metadata\n", "# The dataset doesn't contain the clinical trait information we need (is_trait_available = False)\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", "# Since trait_row is None, we skip this substep\n", "# No need to extract clinical features when trait data is not available\n" ] }, { "cell_type": "markdown", "id": "feb34fdc", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "edf961fc", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:06:50.190209Z", "iopub.status.busy": "2025-03-25T06:06:50.189992Z", "iopub.status.idle": "2025-03-25T06:06:50.304985Z", "shell.execute_reply": "2025-03-25T06:06:50.304380Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13',\n", " '14', '15', '16', '17', '18', '19', '20'],\n", " dtype='object', name='ID')\n" ] } ], "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": "985f1ce2", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "2014f44b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:06:50.306690Z", "iopub.status.busy": "2025-03-25T06:06:50.306552Z", "iopub.status.idle": "2025-03-25T06:06:50.308934Z", "shell.execute_reply": "2025-03-25T06:06:50.308491Z" } }, "outputs": [], "source": [ "# The identifiers in the gene expression data are numeric (1, 2, 3, etc.) and not standard human gene symbols\n", "# These appear to be arbitrary numeric identifiers and would need to be mapped to actual gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "8496975f", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "8d856d07", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:06:50.310528Z", "iopub.status.busy": "2025-03-25T06:06:50.310420Z", "iopub.status.idle": "2025-03-25T06:06:53.759412Z", "shell.execute_reply": "2025-03-25T06:06:53.758769Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['1', '2', '3', '4', '5'], 'COL': ['192', '192', '192', '192', '192'], 'ROW': [328.0, 326.0, 324.0, 322.0, 320.0], 'NAME': [nan, nan, nan, 'A_55_P2051983', 'A_52_P169082'], 'SPOT_ID': ['GE_BrightCorner', 'DarkCorner', 'DarkCorner', nan, nan], 'CONTROL_TYPE': ['pos', 'pos', 'pos', 'FALSE', 'FALSE'], 'GB_ACC': [nan, nan, nan, 'NM_001001803', 'NM_021294'], 'GENE_ID': [nan, nan, nan, 408198.0, 13168.0], 'GENE_SYMBOL': [nan, nan, nan, 'Spink7', 'Dbil5'], 'GENE_NAME': [nan, nan, nan, 'serine peptidase inhibitor, Kazal type 7 (putative)', 'diazepam binding inhibitor-like 5'], 'UNIGENE_ID': [nan, nan, nan, 'Mm.478742', 'Mm.347413'], 'ENSEMBL_ID': [nan, nan, nan, 'ENSMUST00000076194', nan], 'ACCESSION_STRING': [nan, nan, nan, 'ref|NM_001001803|ens|ENSMUST00000076194|gb|BC148575|gb|BC156750', 'ref|NM_021294'], 'CHROMOSOMAL_LOCATION': [nan, nan, nan, 'chr18:62753954-62753895', 'chr11:76031955-76032014'], 'CYTOBAND': [nan, nan, nan, 'mm|18qE1', 'mm|11qB5'], 'DESCRIPTION': [nan, nan, nan, 'Mus musculus serine peptidase inhibitor, Kazal type 7 (putative) (Spink7), mRNA [NM_001001803]', 'Mus musculus diazepam binding inhibitor-like 5 (Dbil5), mRNA [NM_021294]'], 'GO_ID': [nan, nan, nan, 'GO:0003674(molecular_function)|GO:0004867(serine-type endopeptidase inhibitor activity)|GO:0005575(cellular_component)|GO:0005576(extracellular region)|GO:0008150(biological_process)|GO:0030414(peptidase inhibitor activity)', 'GO:0000062(fatty-acyl-CoA binding)|GO:0005488(binding)|GO:0005737(cytoplasm)|GO:0006810(transport)|GO:0008289(lipid binding)'], 'SEQUENCE': [nan, nan, nan, 'CAGTTTGTGGATCTGACTATATCACTTACGGGAATAAATGCAAGCTGTGTACAGAGATCT', 'TAAACAAAGGGATGTCCAAGATGGATGCCATGAGGATCTACATTGCTAAAGTGGAAGAGC']}\n" ] } ], "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": "7a0f191b", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "b8396c1b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:06:53.761239Z", "iopub.status.busy": "2025-03-25T06:06:53.761077Z", "iopub.status.idle": "2025-03-25T06:06:53.907599Z", "shell.execute_reply": "2025-03-25T06:06:53.906960Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data after mapping (first 5 rows):\n", "{'GSM6947333': [5.765499247, 8.90549615, 6.351152373, 5.722009762, 5.854746855], 'GSM6947334': [5.733167493, 8.309656336, 5.996497708, 6.470031597, 5.814009788], 'GSM6947335': [5.700167959, 5.951790839, 5.742400314, 6.012391263, 6.364721615], 'GSM6947336': [5.891782887, 5.840510505, 5.650289558, 6.008373763, 6.932568617], 'GSM6947337': [5.747773928, 8.780743481, 6.886196389, 6.059094686, 5.963228122], 'GSM6947338': [5.929038451, 6.019699325, 5.8293881, 5.818277684, 6.383367386], 'GSM6947339': [5.935432529, 5.968244709, 5.765170731, 5.890302204, 6.405747256], 'GSM6947340': [5.663317403, 5.782679504, 5.686461768, 5.826241194, 7.061529204], 'GSM6947341': [5.699334692, 5.967858065, 5.799722935, 6.18135465, 5.997149675], 'GSM6947342': [5.88262595, 7.252686231, 8.345568571, 5.797517166, 5.602564453], 'GSM6947343': [5.875824971, 6.02315016, 5.696309368, 5.997787261, 6.10026366], 'GSM6947344': [5.648246035, 7.499101979, 8.462235322, 5.85624817, 5.602345704], 'GSM6947345': [5.699528511, 5.88848535, 5.83531835, 5.839494836, 6.019906276], 'GSM6947346': [5.926228542, 5.852511639, 5.890617422, 5.894847543, 6.163559169]}\n" ] } ], "source": [ "# 1. Identify which columns in gene_annotation store identifiers and gene symbols\n", "# From the preview, we can see that gene identifiers are in 'ID' column which match the gene expression data indices\n", "# Gene symbols are in the 'GENE_SYMBOL' column\n", "\n", "# 2. Create a gene mapping dataframe with the identifier and gene symbol columns\n", "gene_mapping = get_gene_mapping(gene_annotation, 'ID', 'GENE_SYMBOL')\n", "\n", "# 3. Convert probe-level measurements to gene expression data\n", "# This applies the gene mapping to the gene expression data\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "\n", "# Let's preview the resulting gene expression data\n", "print(\"Gene expression data after mapping (first 5 rows):\")\n", "print(preview_df(gene_data))\n" ] }, { "cell_type": "markdown", "id": "81d0400e", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "28393fc3", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:06:53.909384Z", "iopub.status.busy": "2025-03-25T06:06:53.909252Z", "iopub.status.idle": "2025-03-25T06:06:53.970968Z", "shell.execute_reply": "2025-03-25T06:06:53.970410Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Pancreatic_Cancer/gene_data/GSE223409.csv\n", "Data quality check failed due to missing trait information. Only gene data saved.\n" ] } ], "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", "# Based on our analysis in Step 2, we determined that trait data is not available\n", "# So we don't need to extract clinical features or create linked_data\n", "\n", "# Create a minimal DataFrame to satisfy the requirements of validate_and_save_cohort_info\n", "# We'll use the first few rows of our normalized gene data to provide structure\n", "df_for_validation = pd.DataFrame(index=normalized_gene_data.index[:5], columns=normalized_gene_data.columns[:5])\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=False, # We determined this in Step 2\n", " is_biased=True, # Set to True since absence of trait data means we can't use it for analysis\n", " df=df_for_validation,\n", " note=\"Dataset contains gene expression data but lacks pancreatic cancer trait information required for analysis.\"\n", ")\n", "\n", "# 6. Since trait data is not available, this dataset is not usable for our trait analysis\n", "# We've saved the gene expression data, but we won't create or save linked data\n", "print(\"Data quality check failed due to missing trait information. Only gene data 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 }