{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "3a0c8375", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:28:05.311896Z", "iopub.status.busy": "2025-03-25T05:28:05.311730Z", "iopub.status.idle": "2025-03-25T05:28:05.482615Z", "shell.execute_reply": "2025-03-25T05:28:05.482252Z" } }, "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 = \"GSE212250\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Head_and_Neck_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Head_and_Neck_Cancer/GSE212250\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Head_and_Neck_Cancer/GSE212250.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Head_and_Neck_Cancer/gene_data/GSE212250.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Head_and_Neck_Cancer/clinical_data/GSE212250.csv\"\n", "json_path = \"../../output/preprocess/Head_and_Neck_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "5bd0e236", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "81675035", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:28:05.483859Z", "iopub.status.busy": "2025-03-25T05:28:05.483711Z", "iopub.status.idle": "2025-03-25T05:28:05.581040Z", "shell.execute_reply": "2025-03-25T05:28:05.580615Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Deciphering the function of intrinsic and genomics-driven epigenetic heterogeneity in head and neck cancer progression with single-nucleus CUT&RUN\"\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 line: HN120Pri', 'cell line: HN120Met', 'cell line: HN120PCR', 'cell line: HN137Pri', 'cell line: HN137Met', 'cell line: HN137PCR'], 1: ['cell type: Primary patient derived oral cancer cell line'], 2: ['disease state: Primary tumor', 'disease state: Metastatic tumor', 'disease state: Primary Cisplatin Resistant tumor'], 3: ['antibody: H3K4me3 (Abcam, ab213224)', 'antibody: H3K27ac (Merck, MABE647)']}\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": "c35c5335", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "b91898fa", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:28:05.582468Z", "iopub.status.busy": "2025-03-25T05:28:05.582339Z", "iopub.status.idle": "2025-03-25T05:28:05.742617Z", "shell.execute_reply": "2025-03-25T05:28:05.742286Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical Data Preview:\n", "{'GSM6515847': [0.0], 'GSM6515848': [0.0], 'GSM6515849': [0.0], 'GSM6515850': [0.0], 'GSM6515851': [0.0], 'GSM6515852': [0.0], 'GSM6515853': [0.0], 'GSM6515854': [0.0], 'GSM6515855': [0.0], 'GSM6515856': [0.0], 'GSM6515857': [0.0], 'GSM6515858': [0.0], 'GSM6515859': [0.0], 'GSM6515860': [0.0], 'GSM6515861': [0.0], 'GSM6515862': [0.0], 'GSM6515863': [0.0], 'GSM6515864': [0.0], 'GSM6515865': [0.0], 'GSM6515866': [0.0], 'GSM6515867': [0.0], 'GSM6515868': [0.0], 'GSM6515869': [0.0], 'GSM6515870': [0.0], 'GSM6515871': [0.0], 'GSM6515872': [0.0], 'GSM6515873': [0.0], 'GSM6515874': [0.0], 'GSM6515875': [0.0], 'GSM6515876': [0.0], 'GSM6515877': [0.0], 'GSM6515878': [0.0], 'GSM6515879': [0.0], 'GSM6515880': [0.0], 'GSM6515881': [0.0], 'GSM6515882': [0.0], 'GSM6515883': [0.0], 'GSM6515884': [0.0], 'GSM6515885': [0.0], 'GSM6515886': [0.0], 'GSM6515887': [0.0], 'GSM6515888': [0.0], 'GSM6515889': [0.0], 'GSM6515890': [0.0], 'GSM6515891': [0.0], 'GSM6515892': [0.0], 'GSM6515893': [0.0], 'GSM6515894': [0.0], 'GSM6515895': [0.0], 'GSM6515896': [0.0], 'GSM6515897': [0.0], 'GSM6515898': [0.0], 'GSM6515899': [0.0], 'GSM6515900': [0.0], 'GSM6515901': [0.0], 'GSM6515902': [0.0], 'GSM6515903': [0.0], 'GSM6515904': [0.0], 'GSM6515905': [0.0], 'GSM6515906': [0.0], 'GSM6515907': [0.0], 'GSM6515908': [0.0], 'GSM6515909': [0.0], 'GSM6515910': [0.0], 'GSM6515911': [0.0], 'GSM6515912': [0.0], 'GSM6515913': [0.0], 'GSM6515914': [0.0], 'GSM6515915': [0.0], 'GSM6515916': [0.0], 'GSM6515917': [0.0], 'GSM6515918': [0.0], 'GSM6515919': [0.0], 'GSM6515920': [0.0], 'GSM6515921': [0.0], 'GSM6515922': [0.0], 'GSM6515923': [0.0], 'GSM6515924': [0.0], 'GSM6515925': [0.0], 'GSM6515926': [0.0], 'GSM6515927': [0.0], 'GSM6515928': [0.0], 'GSM6515929': [0.0], 'GSM6515930': [0.0], 'GSM6515931': [0.0], 'GSM6515932': [0.0], 'GSM6515933': [0.0], 'GSM6515934': [0.0], 'GSM6515935': [0.0], 'GSM6515936': [0.0], 'GSM6515937': [0.0], 'GSM6515938': [0.0], 'GSM6515939': [0.0], 'GSM6515940': [0.0], 'GSM6515941': [0.0], 'GSM6515942': [0.0], 'GSM6515943': [0.0], 'GSM6515944': [0.0], 'GSM6515945': [0.0], 'GSM6515946': [0.0], 'GSM6515947': [0.0], 'GSM6515948': [0.0], 'GSM6515949': [0.0], 'GSM6515950': [0.0], 'GSM6515951': [0.0], 'GSM6515952': [0.0], 'GSM6515953': [0.0], 'GSM6515954': [0.0], 'GSM6515955': [0.0], 'GSM6515956': [0.0], 'GSM6515957': [0.0], 'GSM6515958': [0.0], 'GSM6515959': [0.0], 'GSM6515960': [0.0], 'GSM6515961': [0.0], 'GSM6515962': [0.0], 'GSM6515963': [0.0], 'GSM6515964': [0.0], 'GSM6515965': [0.0], 'GSM6515966': [0.0], 'GSM6515967': [0.0], 'GSM6515968': [0.0], 'GSM6515969': [0.0], 'GSM6515970': [0.0], 'GSM6515971': [0.0], 'GSM6515972': [0.0], 'GSM6515973': [0.0], 'GSM6515974': [0.0], 'GSM6515975': [0.0], 'GSM6515976': [0.0], 'GSM6515977': [0.0], 'GSM6515978': [0.0], 'GSM6515979': [0.0], 'GSM6515980': [0.0], 'GSM6515981': [0.0], 'GSM6515982': [0.0], 'GSM6515983': [0.0], 'GSM6515984': [0.0], 'GSM6515985': [0.0], 'GSM6515986': [0.0], 'GSM6515987': [0.0], 'GSM6515988': [0.0], 'GSM6515989': [0.0], 'GSM6515990': [0.0], 'GSM6515991': [0.0], 'GSM6515992': [0.0], 'GSM6515993': [0.0], 'GSM6515994': [0.0], 'GSM6515995': [0.0], 'GSM6515996': [0.0], 'GSM6515997': [0.0], 'GSM6515998': [0.0], 'GSM6515999': [0.0], 'GSM6516000': [0.0], 'GSM6516001': [0.0], 'GSM6516002': [0.0], 'GSM6516003': [0.0], 'GSM6516004': [0.0], 'GSM6516005': [0.0], 'GSM6516006': [0.0], 'GSM6516007': [0.0], 'GSM6516008': [0.0], 'GSM6516009': [0.0], 'GSM6516010': [0.0], 'GSM6516011': [0.0], 'GSM6516012': [0.0], 'GSM6516013': [0.0], 'GSM6516014': [0.0], 'GSM6516015': [0.0], 'GSM6516016': [0.0], 'GSM6516017': [0.0], 'GSM6516018': [0.0], 'GSM6516019': [0.0], 'GSM6516020': [0.0], 'GSM6516021': [0.0], 'GSM6516022': [0.0], 'GSM6516023': [0.0], 'GSM6516024': [0.0], 'GSM6516025': [0.0], 'GSM6516026': [0.0], 'GSM6516027': [0.0], 'GSM6516028': [0.0], 'GSM6516029': [0.0], 'GSM6516030': [0.0], 'GSM6516031': [0.0], 'GSM6516032': [0.0], 'GSM6516033': [0.0], 'GSM6516034': [0.0], 'GSM6516035': [0.0], 'GSM6516036': [0.0], 'GSM6516037': [0.0], 'GSM6516038': [0.0], 'GSM6516039': [0.0], 'GSM6516040': [0.0], 'GSM6516041': [0.0], 'GSM6516042': [0.0], 'GSM6516043': [0.0], 'GSM6516044': [0.0], 'GSM6516045': [0.0], 'GSM6516046': [0.0]}\n", "Clinical data saved to ../../output/preprocess/Head_and_Neck_Cancer/clinical_data/GSE212250.csv\n" ] } ], "source": [ "# Analyze the dataset to determine gene expression, trait, age, and gender availability\n", "\n", "# 1. Gene Expression Data Availability\n", "# Based on the background information, this dataset contains CUT&RUN data for histone modifications (H3K4me3, H3K27ac)\n", "# from head and neck cancer cell lines, which is epigenetic data rather than gene expression data\n", "is_gene_available = False # Epigenetic data, not gene expression data\n", "\n", "# 2. Clinical Feature Availability and Conversion\n", "# 2.1 Trait Availability\n", "# Looking at the sample characteristics, I can identify:\n", "# Key 2 contains disease state information relevant to Head and Neck Cancer\n", "trait_row = 2 # Maps to \"disease state: Primary tumor\", \"disease state: Metastatic tumor\", etc.\n", "\n", "# Age information is not available in the sample characteristics\n", "age_row = None\n", "\n", "# Gender information is not available in the sample characteristics \n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion Functions\n", "def convert_trait(value):\n", " \"\"\"\n", " Convert disease state values to binary format:\n", " 1 = Metastatic tumor (more severe)\n", " 0 = Primary tumor (less severe)\n", " None = Other or unknown\n", " \"\"\"\n", " if not isinstance(value, str):\n", " return None\n", " \n", " # Extract the value after the colon\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " if \"Metastatic\" in value:\n", " return 1 # Metastatic tumor\n", " elif \"Primary\" in value:\n", " return 0 # Primary tumor\n", " else:\n", " return None # Other or unknown disease state\n", "\n", "def convert_age(value):\n", " \"\"\"Placeholder function since age data is not available\"\"\"\n", " return None\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", "# Determine trait data availability\n", "is_trait_available = trait_row is not None\n", "\n", "# 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", "# 4. Clinical Feature Extraction\n", "# Skip this step if trait_row is None\n", "if trait_row is not None:\n", " # We need clinical_data from a previous step\n", " try:\n", " # Extract and process clinical features\n", " clinical_df = geo_select_clinical_features(\n", " clinical_data, # Assumed to be defined in previous step\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 data\n", " preview = preview_df(clinical_df)\n", " print(\"Clinical Data Preview:\")\n", " print(preview)\n", " \n", " # Ensure the output directory exists\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " \n", " # Save the clinical data to CSV\n", " clinical_df.to_csv(out_clinical_data_file, index=False)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n", " except NameError:\n", " print(\"Warning: clinical_data not defined in previous steps. Skipping clinical feature extraction.\")\n" ] }, { "cell_type": "markdown", "id": "0705951f", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "72476b3a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:28:05.744062Z", "iopub.status.busy": "2025-03-25T05:28:05.743938Z", "iopub.status.idle": "2025-03-25T05:28:06.280008Z", "shell.execute_reply": "2025-03-25T05:28:06.279617Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix file found: ../../input/GEO/Head_and_Neck_Cancer/GSE212250/GSE212250-GPL15520_series_matrix.txt.gz\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape: (0, 3955)\n", "First 20 gene/probe identifiers:\n", "Index([], 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": "d7686294", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "14aa43e8", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:28:06.281742Z", "iopub.status.busy": "2025-03-25T05:28:06.281620Z", "iopub.status.idle": "2025-03-25T05:28:06.286102Z", "shell.execute_reply": "2025-03-25T05:28:06.285804Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Based on the analysis of the file, we've found that GSE212250 is a SuperSeries\n", "# which doesn't contain direct gene expression data itself but refers to multiple SubSeries.\n", "# The file appears to be a microRNA dataset (GPL15520 - Agilent-045997 Arraystar miRNA platform)\n", "# and doesn't contain gene identifiers in the expected format.\n", "\n", "# We cannot determine the need for gene mapping when no gene data is available\n", "# The gene data shape (0, 3955) indicates no gene rows were found in the dataset\n", "\n", "# Since we don't have actual gene identifiers to evaluate, we should indicate that \n", "# gene data is not available rather than determining if mapping is needed\n", "\n", "requires_gene_mapping = False # This is not relevant as gene data is not available\n", "\n", "# Record that gene data is unavailable for this dataset\n", "is_gene_available = False\n", "is_trait_available = False # We haven't confirmed trait data either\n", "\n", "# Save this information about the dataset\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", ")" ] } ], "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 }