{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "c795b421", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:13:17.706055Z", "iopub.status.busy": "2025-03-25T08:13:17.705930Z", "iopub.status.idle": "2025-03-25T08:13:17.869156Z", "shell.execute_reply": "2025-03-25T08:13:17.868801Z" } }, "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 = \"Cervical_Cancer\"\n", "cohort = \"GSE114243\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Cervical_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Cervical_Cancer/GSE114243\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Cervical_Cancer/GSE114243.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Cervical_Cancer/gene_data/GSE114243.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Cervical_Cancer/clinical_data/GSE114243.csv\"\n", "json_path = \"../../output/preprocess/Cervical_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "9ed61419", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "02958252", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:13:17.870641Z", "iopub.status.busy": "2025-03-25T08:13:17.870495Z", "iopub.status.idle": "2025-03-25T08:13:18.010535Z", "shell.execute_reply": "2025-03-25T08:13:18.010230Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"The role of DOCK10 in the regulation of the transcriptome and aging\"\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: ['genotype/variation: KO'], 2: ['treatment: Pre'], 3: ['sample: 6 to 10'], 4: ['cell type: spleen B cell'], 5: ['age: 12 weeks'], 6: ['reference composition: Spleen B cell pool of KO samples 6 to 10, before culture (Pre)']}\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": "54c59863", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "c8c2cb72", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:13:18.011812Z", "iopub.status.busy": "2025-03-25T08:13:18.011700Z", "iopub.status.idle": "2025-03-25T08:13:18.031730Z", "shell.execute_reply": "2025-03-25T08:13:18.031445Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A new JSON file was created at: ../../output/preprocess/Cervical_Cancer/cohort_info.json\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the background information, this seems to be about human embryonic kidney 293T cells,\n", "# which suggests gene expression data may be available, but we need to examine further.\n", "# Since it's labeled as a SuperSeries composed of SubSeries, it's unclear if it contains gene expression data.\n", "# To be conservative, we'll set is_gene_available to False.\n", "is_gene_available = False\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# From the sample characteristics dictionary, we only see tissue information.\n", "# There's no information about cervical cancer, age, or gender.\n", "\n", "# 2.1 Data Availability\n", "trait_row = None # No information about cervical cancer\n", "age_row = None # No age information\n", "gender_row = None # No gender information\n", "\n", "# 2.2 Data Type Conversion\n", "# Define conversion functions even though we don't have the data\n", "def convert_trait(value):\n", " if value is None:\n", " return None\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " # Convert to binary: 1 for cervical cancer, 0 for normal\n", " if 'cancer' in value.lower() or 'tumor' in value.lower() or 'carcinoma' in value.lower():\n", " return 1\n", " elif 'normal' in value.lower() or 'healthy' in value.lower() or 'control' in value.lower():\n", " return 0\n", " return None\n", "\n", "def convert_age(value):\n", " if value is None:\n", " return None\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " # Try to extract age as a number\n", " try:\n", " # Extract digits if there are any\n", " import re\n", " digits = re.findall(r'\\d+', value)\n", " if digits:\n", " return float(digits[0])\n", " except:\n", " pass\n", " return None\n", "\n", "def convert_gender(value):\n", " if value is None:\n", " return None\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip().lower()\n", " # Convert to binary: 0 for female, 1 for male\n", " if 'female' in value or 'f' == value:\n", " return 0\n", " elif 'male' in value or 'm' == value:\n", " return 1\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Determine trait data availability\n", "is_trait_available = trait_row is not None\n", "\n", "# Save initial filtering 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 since trait_row is None\n" ] }, { "cell_type": "markdown", "id": "be0687fb", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "a4385160", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:13:18.032912Z", "iopub.status.busy": "2025-03-25T08:13:18.032806Z", "iopub.status.idle": "2025-03-25T08:13:18.160283Z", "shell.execute_reply": "2025-03-25T08:13:18.159888Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['(+)E1A_r60_1', '(+)E1A_r60_3', '(+)E1A_r60_a104', '(+)E1A_r60_a107',\n", " '(+)E1A_r60_a135', '(+)E1A_r60_a20', '(+)E1A_r60_a22', '(+)E1A_r60_a97',\n", " '(+)E1A_r60_n11', '(+)E1A_r60_n9', '(-)3xSLv1', 'A_51_P100034',\n", " 'A_51_P100174', 'A_51_P100208', 'A_51_P100289', 'A_51_P100298',\n", " 'A_51_P100309', 'A_51_P100327', 'A_51_P100347', 'A_51_P100519'],\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": "8c841f57", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "86d75844", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:13:18.161655Z", "iopub.status.busy": "2025-03-25T08:13:18.161534Z", "iopub.status.idle": "2025-03-25T08:13:18.163470Z", "shell.execute_reply": "2025-03-25T08:13:18.163188Z" } }, "outputs": [], "source": [ "# Looking at the gene identifiers, these are Agilent microarray probe IDs (starting with A_23_P),\n", "# not standard human gene symbols. These probe IDs need to be mapped to official gene symbols.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "3ddc3b21", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "9665718f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:13:18.164701Z", "iopub.status.busy": "2025-03-25T08:13:18.164599Z", "iopub.status.idle": "2025-03-25T08:13:21.648395Z", "shell.execute_reply": "2025-03-25T08:13:21.648004Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['A_23_P100001', 'A_23_P100011', 'A_23_P100022', 'A_23_P100056', 'A_23_P100074'], 'SPOT_ID': ['A_23_P100001', 'A_23_P100011', 'A_23_P100022', 'A_23_P100056', 'A_23_P100074'], 'CONTROL_TYPE': ['FALSE', 'FALSE', 'FALSE', 'FALSE', 'FALSE'], 'REFSEQ': ['NM_207446', 'NM_005829', 'NM_014848', 'NM_194272', 'NM_020371'], 'GB_ACC': ['NM_207446', 'NM_005829', 'NM_014848', 'NM_194272', 'NM_020371'], 'GENE': ['400451', '10239', '9899', '348093', '57099'], 'GENE_SYMBOL': ['FAM174B', 'AP3S2', 'SV2B', 'RBPMS2', 'AVEN'], 'GENE_NAME': ['family with sequence similarity 174, member B', 'adaptor-related protein complex 3, sigma 2 subunit', 'synaptic vesicle glycoprotein 2B', 'RNA binding protein with multiple splicing 2', 'apoptosis, caspase activation inhibitor'], 'UNIGENE_ID': ['Hs.27373', 'Hs.632161', 'Hs.21754', 'Hs.436518', 'Hs.555966'], 'ENSEMBL_ID': ['ENST00000557398', nan, 'ENST00000557410', 'ENST00000300069', 'ENST00000306730'], 'TIGR_ID': [nan, nan, nan, nan, nan], 'ACCESSION_STRING': ['ref|NM_207446|ens|ENST00000557398|ens|ENST00000553393|ens|ENST00000327355', 'ref|NM_005829|ref|NM_001199058|ref|NR_023361|ref|NR_037582', 'ref|NM_014848|ref|NM_001167580|ens|ENST00000557410|ens|ENST00000330276', 'ref|NM_194272|ens|ENST00000300069|gb|AK127873|gb|AK124123', 'ref|NM_020371|ens|ENST00000306730|gb|AF283508|gb|BC010488'], 'CHROMOSOMAL_LOCATION': ['chr15:93160848-93160789', 'chr15:90378743-90378684', 'chr15:91838329-91838388', 'chr15:65032375-65032316', 'chr15:34158739-34158680'], 'CYTOBAND': ['hs|15q26.1', 'hs|15q26.1', 'hs|15q26.1', 'hs|15q22.31', 'hs|15q14'], 'DESCRIPTION': ['Homo sapiens family with sequence similarity 174, member B (FAM174B), mRNA [NM_207446]', 'Homo sapiens adaptor-related protein complex 3, sigma 2 subunit (AP3S2), transcript variant 1, mRNA [NM_005829]', 'Homo sapiens synaptic vesicle glycoprotein 2B (SV2B), transcript variant 1, mRNA [NM_014848]', 'Homo sapiens RNA binding protein with multiple splicing 2 (RBPMS2), mRNA [NM_194272]', 'Homo sapiens apoptosis, caspase activation inhibitor (AVEN), mRNA [NM_020371]'], 'GO_ID': ['GO:0016020(membrane)|GO:0016021(integral to membrane)', 'GO:0005794(Golgi apparatus)|GO:0006886(intracellular protein transport)|GO:0008565(protein transporter activity)|GO:0016020(membrane)|GO:0016192(vesicle-mediated transport)|GO:0030117(membrane coat)|GO:0030659(cytoplasmic vesicle membrane)|GO:0031410(cytoplasmic vesicle)', 'GO:0001669(acrosomal vesicle)|GO:0006836(neurotransmitter transport)|GO:0016020(membrane)|GO:0016021(integral to membrane)|GO:0022857(transmembrane transporter activity)|GO:0030054(cell junction)|GO:0030672(synaptic vesicle membrane)|GO:0031410(cytoplasmic vesicle)|GO:0045202(synapse)', 'GO:0000166(nucleotide binding)|GO:0003676(nucleic acid binding)', 'GO:0005515(protein binding)|GO:0005622(intracellular)|GO:0005624(membrane fraction)|GO:0006915(apoptosis)|GO:0006916(anti-apoptosis)|GO:0012505(endomembrane system)|GO:0016020(membrane)'], 'SEQUENCE': ['ATCTCATGGAAAAGCTGGATTCCTCTGCCTTACGCAGAAACACCCGGGCTCCATCTGCCA', 'TCAAGTATTGGCCTGACATAGAGTCCTTAAGACAAGCAAAGACAAGCAAGGCAAGCACGT', 'ATGTCGGCTGTGGAGGGTTAAAGGGATGAGGCTTTCCTTTGTTTAGCAAATCTGTTCACA', 'CCCTGTCAGATAAGTTTAATGTTTAGTTTGAGGCATGAAGAAGAAAAGGGTTTCCATTCT', 'GACCAGCCAGTTTACAAGCATGTCTCAAGCTAGTGTGTTCCATTATGCTCACAGCAGTAA']}\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": "ea2f681c", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "d92a250c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:13:21.649704Z", "iopub.status.busy": "2025-03-25T08:13:21.649579Z", "iopub.status.idle": "2025-03-25T08:13:21.808877Z", "shell.execute_reply": "2025-03-25T08:13:21.808490Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data after mapping (first 5 genes):\n", "Index(['A130033P14', 'A230055C15', 'A330041K01', 'A330044H09', 'A430017K17'], dtype='object', name='Gene')\n", "(555, 40)\n" ] } ], "source": [ "# 1. Determine which columns to use for mapping\n", "# Based on the preview, the 'ID' column in gene_annotation contains probe IDs that match the \n", "# gene_data index, and 'GENE_SYMBOL' contains the gene symbols we want to map to\n", "prob_col = 'ID' # Column containing probe IDs\n", "gene_col = 'GENE_SYMBOL' # Column containing gene symbols\n", "\n", "# 2. Get the gene mapping dataframe by extracting the relevant columns\n", "gene_mapping = get_gene_mapping(gene_annotation, prob_col, gene_col)\n", "\n", "# 3. Apply the gene mapping to convert from probe-level to gene-level expression data\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "\n", "# Preview the first few gene symbols and their expression values\n", "print(\"Gene expression data after mapping (first 5 genes):\")\n", "print(gene_data.index[:5])\n", "print(gene_data.shape)\n" ] }, { "cell_type": "markdown", "id": "b137b330", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "3d5de08d", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:13:21.810212Z", "iopub.status.busy": "2025-03-25T08:13:21.810090Z", "iopub.status.idle": "2025-03-25T08:13:21.887565Z", "shell.execute_reply": "2025-03-25T08:13:21.887179Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Cervical_Cancer/gene_data/GSE114243.csv\n", "Clinical data saved to ../../output/preprocess/Cervical_Cancer/clinical_data/GSE114243.csv\n", "Abnormality detected in the cohort: GSE114243. Preprocessing failed.\n", "Data was determined to be unusable due to missing trait information and was not saved.\n" ] } ], "source": [ "# 1. Normalize the obtained gene data with the 'normalize_gene_symbols_in_index' function from the library.\n", "normalized_gene_data = normalize_gene_symbols_in_index(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", "# Create empty clinical features DataFrame since no trait data is available\n", "clinical_features_df = pd.DataFrame()\n", "os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", "clinical_features_df.to_csv(out_clinical_data_file)\n", "print(f\"Clinical data saved to {out_clinical_data_file}\")\n", "\n", "# Create a minimal dataset for validation\n", "minimal_df = pd.DataFrame({trait: [0, 1]}) # Dummy data with both classes represented\n", "\n", "# 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, # No trait data available\n", " is_biased=True, # Consider biased since no trait data exists\n", " df=minimal_df,\n", " note=\"This dataset contains gene expression data but lacks information about cervical cancer status.\"\n", ")\n", "\n", "print(\"Data was determined to be unusable due to missing trait information and was not 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 }