{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "103e92b9", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:58:14.252180Z", "iopub.status.busy": "2025-03-25T06:58:14.251986Z", "iopub.status.idle": "2025-03-25T06:58:14.420053Z", "shell.execute_reply": "2025-03-25T06:58:14.419608Z" } }, "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 = \"Bladder_Cancer\"\n", "cohort = \"GSE245953\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Bladder_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Bladder_Cancer/GSE245953\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Bladder_Cancer/GSE245953.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Bladder_Cancer/gene_data/GSE245953.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Bladder_Cancer/clinical_data/GSE245953.csv\"\n", "json_path = \"../../output/preprocess/Bladder_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "e14b51c5", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "75abf1de", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:58:14.421349Z", "iopub.status.busy": "2025-03-25T06:58:14.421206Z", "iopub.status.idle": "2025-03-25T06:58:14.718437Z", "shell.execute_reply": "2025-03-25T06:58:14.718022Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Gene expression data from muscle-invasive bladder cancer samples II\"\n", "!Series_summary\t\"Gene signatures based on the median expression of a preselected set of genes can provide prognostic and treatment outcome prediction and so be valuable clinically.\"\n", "!Series_summary\t\"Different health care services use different gene expression platforms to derive gene expression data. Here we have derived gene expression data using a microarray platform.\"\n", "!Series_overall_design\t\"RNA extracted from FFPE blocks from patients with muscle-invasive bladder cancer and full transcriptome analysis on Clariom S microarray platform.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['condition: Muscle-invasive bladder cancer']}\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": "63936b11", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "75d8d64a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:58:14.719549Z", "iopub.status.busy": "2025-03-25T06:58:14.719435Z", "iopub.status.idle": "2025-03-25T06:58:14.744524Z", "shell.execute_reply": "2025-03-25T06:58:14.744139Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Selected Clinical Features Preview: {0: [1.0]}\n", "Clinical data saved to ../../output/preprocess/Bladder_Cancer/clinical_data/GSE245953.csv\n" ] } ], "source": [ "import pandas as pd\n", "import os\n", "import json\n", "from typing import Optional, Callable, Dict, Any\n", "\n", "# 1. Gene Expression Data Availability\n", "# From the background info, it appears there is gene expression data\n", "# The series mentions \"Gene expression data using a microarray platform\"\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 dict, we only have one key (0)\n", "# Trait (Bladder Cancer): row 0 contains cancer status - all samples are MIBC\n", "trait_row = 0\n", "\n", "# Age: Not available in the sample characteristics\n", "age_row = None\n", "\n", "# Gender: 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 trait values to binary.\n", " For Bladder Cancer: 1 for having cancer, 0 for control.\n", " \"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract the part after the colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # All samples are cancer cases (muscle-invasive bladder cancer)\n", " if \"muscle-invasive bladder cancer\" in value.lower():\n", " return 1\n", " return None # Unknown/unclear values\n", "\n", "def convert_age(value):\n", " \"\"\"\n", " Convert age values to continuous numeric format.\n", " \"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract the part after the colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " try:\n", " # Try to convert to float (handles both integers and decimals)\n", " return float(value)\n", " except (ValueError, TypeError):\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"\n", " Convert gender values to binary format where:\n", " 0 = Female\n", " 1 = Male\n", " \"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract the part after the colon if present\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']:\n", " return 1\n", " elif value in ['female', 'f']:\n", " return 0\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Determine if trait data is available (if trait_row is not None)\n", "is_trait_available = trait_row is not None\n", "\n", "# Validate 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", "# 4. Clinical Feature Extraction\n", "# If trait_row is not None, extract clinical features\n", "if trait_row is not None:\n", " # Load clinical data from previous step\n", " clinical_data = pd.DataFrame({0: ['condition: Muscle-invasive bladder cancer']})\n", " \n", " # Extract clinical features using the provided function\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", " # Preview the selected clinical dataframe\n", " preview = preview_df(selected_clinical_df)\n", " print(\"Selected Clinical Features Preview:\", preview)\n", " \n", " # Create directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " \n", " # Save clinical features to CSV\n", " selected_clinical_df.to_csv(out_clinical_data_file, index=True)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "f87e30e4", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "46dd3f6b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:58:14.745751Z", "iopub.status.busy": "2025-03-25T06:58:14.745641Z", "iopub.status.idle": "2025-03-25T06:58:15.332377Z", "shell.execute_reply": "2025-03-25T06:58:15.331720Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['AFFX-BkGr-GC03_st', 'AFFX-BkGr-GC04_st', 'AFFX-BkGr-GC05_st',\n", " 'AFFX-BkGr-GC06_st', 'AFFX-BkGr-GC07_st', 'AFFX-BkGr-GC08_st',\n", " 'AFFX-BkGr-GC09_st', 'AFFX-BkGr-GC10_st', 'AFFX-BkGr-GC11_st',\n", " 'AFFX-BkGr-GC12_st', 'AFFX-BkGr-GC13_st', 'AFFX-BkGr-GC14_st',\n", " 'AFFX-BkGr-GC15_st', 'AFFX-BkGr-GC16_st', 'AFFX-BkGr-GC17_st',\n", " 'AFFX-BkGr-GC18_st', 'AFFX-BkGr-GC19_st', 'AFFX-BkGr-GC20_st',\n", " 'AFFX-BkGr-GC21_st', 'AFFX-BkGr-GC22_st'],\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": "f9f660b1", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "a3b63571", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:58:15.334192Z", "iopub.status.busy": "2025-03-25T06:58:15.334062Z", "iopub.status.idle": "2025-03-25T06:58:15.336348Z", "shell.execute_reply": "2025-03-25T06:58:15.335909Z" } }, "outputs": [], "source": [ "# These identifiers are Affymetrix microarray probe IDs (AFFX prefix), not human gene symbols.\n", "# They need to be mapped to standard gene symbols for analysis.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "0b1220cf", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "080e3eaf", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:58:15.338091Z", "iopub.status.busy": "2025-03-25T06:58:15.337974Z", "iopub.status.idle": "2025-03-25T06:58:25.389015Z", "shell.execute_reply": "2025-03-25T06:58:25.388325Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['TC0100006437.hg.1', 'TC0100006476.hg.1', 'TC0100006479.hg.1', 'TC0100006480.hg.1', 'TC0100006483.hg.1'], 'probeset_id': ['TC0100006437.hg.1', 'TC0100006476.hg.1', 'TC0100006479.hg.1', 'TC0100006480.hg.1', 'TC0100006483.hg.1'], 'seqname': ['chr1', 'chr1', 'chr1', 'chr1', 'chr1'], 'strand': ['+', '+', '+', '+', '+'], 'start': ['69091', '924880', '960587', '966497', '1001138'], 'stop': ['70008', '944581', '965719', '975865', '1014541'], 'total_probes': [10.0, 10.0, 10.0, 10.0, 10.0], 'category': ['main', 'main', 'main', 'main', 'main'], 'SPOT_ID': ['Coding', 'Multiple_Complex', 'Multiple_Complex', 'Multiple_Complex', 'Multiple_Complex'], 'SPOT_ID.1': ['NM_001005484 // RefSeq // Homo sapiens olfactory receptor, family 4, subfamily F, member 5 (OR4F5), mRNA. // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000335137 // ENSEMBL // olfactory receptor, family 4, subfamily F, member 5 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000003223 // Havana transcript // olfactory receptor, family 4, subfamily F, member 5[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// uc001aal.1 // UCSC Genes // Homo sapiens olfactory receptor, family 4, subfamily F, member 5 (OR4F5), mRNA. // chr1 // 100 // 100 // 0 // --- // 0 /// CCDS30547.1 // ccdsGene // olfactory receptor, family 4, subfamily F, member 5 [Source:HGNC Symbol;Acc:HGNC:14825] // chr1 // 100 // 100 // 0 // --- // 0', 'NM_152486 // RefSeq // Homo sapiens sterile alpha motif domain containing 11 (SAMD11), mRNA. // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000341065 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000342066 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000420190 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000437963 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000455979 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000464948 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000466827 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000474461 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000478729 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:processed_transcript] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000616016 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000616125 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000617307 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000618181 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000618323 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000618779 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000620200 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000622503 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// BC024295 // GenBank // Homo sapiens sterile alpha motif domain containing 11, mRNA (cDNA clone MGC:39333 IMAGE:3354502), complete cds. // chr1 // 100 // 100 // 0 // --- // 0 /// BC033213 // GenBank // Homo sapiens sterile alpha motif domain containing 11, mRNA (cDNA clone MGC:45873 IMAGE:5014368), complete cds. // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097860 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097862 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097863 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097865 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:processed_transcript] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097866 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097867 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097868 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000276866 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000316521 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// CCDS2.2 // ccdsGene // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009185 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVERLAPTX, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009186 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVERLAPTX, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009187 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009188 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009189 // circbase // Salzman2013 ALT_DONOR, CDS, coding, INTERNAL, OVCODE, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009190 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009191 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009192 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVERLAPTX, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009193 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVERLAPTX, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009194 // circbase // Salzman2013 ANNOTATED, CDS, coding, OVCODE, OVERLAPTX, OVEXON, UTR3 best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009195 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVERLAPTX, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// uc001abw.2 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc031pjt.2 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc031pju.2 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc031pkg.2 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc031pkh.2 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc031pkk.2 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc031pkm.2 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc031pko.2 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057axs.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057axt.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057axu.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057axv.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057axw.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057axx.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057axy.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057axz.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057aya.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// NONHSAT000212 // lncRNAWiki // Non-coding transcript identified by NONCODE // chr1 // 100 // 100 // 0 // --- // 0 /// NONHSAT000212 // NONCODE // Non-coding transcript identified by NONCODE: Exonic // chr1 // 100 // 100 // 0 // --- // 0 /// NONHSAT000213 // lncRNAWiki // Non-coding transcript identified by NONCODE // chr1 // 100 // 100 // 0 // --- // 0 /// NONHSAT000213 // NONCODE // Non-coding transcript identified by NONCODE: Exonic // chr1 // 100 // 100 // 0 // --- // 0', 'NM_198317 // RefSeq // Homo sapiens kelch-like family member 17 (KLHL17), mRNA. // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000338591 // ENSEMBL // kelch-like family member 17 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000463212 // ENSEMBL // kelch-like family member 17 [gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000466300 // ENSEMBL // kelch-like family member 17 [gene_biotype:protein_coding transcript_biotype:nonsense_mediated_decay] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000481067 // ENSEMBL // kelch-like family member 17 [gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000622660 // ENSEMBL // kelch-like family member 17 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097875 // Havana transcript // kelch-like 17 (Drosophila)[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097877 // Havana transcript // kelch-like 17 (Drosophila)[gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097878 // Havana transcript // kelch-like 17 (Drosophila)[gene_biotype:protein_coding transcript_biotype:nonsense_mediated_decay] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097931 // Havana transcript // kelch-like 17 (Drosophila)[gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// BC166618 // GenBank // Synthetic construct Homo sapiens clone IMAGE:100066344, MGC:195481 kelch-like 17 (Drosophila) (KLHL17) mRNA, encodes complete protein. // chr1 // 100 // 100 // 0 // --- // 0 /// CCDS30550.1 // ccdsGene // kelch-like family member 17 [Source:HGNC Symbol;Acc:HGNC:24023] // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009209 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVEXON best transcript NM_198317 // chr1 // 100 // 100 // 0 // --- // 0 /// uc001aca.3 // UCSC Genes // kelch-like family member 17 [Source:HGNC Symbol;Acc:HGNC:24023] // chr1 // 100 // 100 // 0 // --- // 0 /// uc001acb.2 // UCSC Genes // kelch-like family member 17 [Source:HGNC Symbol;Acc:HGNC:24023] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057ayg.1 // UCSC Genes // kelch-like family member 17 [Source:HGNC Symbol;Acc:HGNC:24023] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057ayh.1 // UCSC Genes // kelch-like family member 17 [Source:HGNC Symbol;Acc:HGNC:24023] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057ayi.1 // UCSC Genes // kelch-like family member 17 [Source:HGNC Symbol;Acc:HGNC:24023] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057ayj.1 // UCSC Genes // N/A // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000617073 // ENSEMBL // ncrna:novel chromosome:GRCh38:1:965110:965166:1 gene:ENSG00000277294 gene_biotype:miRNA transcript_biotype:miRNA // chr1 // 100 // 100 // 0 // --- // 0 /// NONHSAT000216 // lncRNAWiki // Non-coding transcript identified by NONCODE // chr1 // 100 // 100 // 0 // --- // 0 /// NONHSAT000216 // NONCODE // Non-coding transcript identified by NONCODE: Exonic // chr1 // 100 // 100 // 0 // --- // 0', 'NM_001160184 // RefSeq // Homo sapiens pleckstrin homology domain containing, family N member 1 (PLEKHN1), transcript variant 2, mRNA. // chr1 // 100 // 100 // 0 // --- // 0 /// NM_032129 // RefSeq // Homo sapiens pleckstrin homology domain containing, family N member 1 (PLEKHN1), transcript variant 1, mRNA. // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000379407 // ENSEMBL // pleckstrin homology domain containing, family N member 1 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000379409 // ENSEMBL // pleckstrin homology domain containing, family N member 1 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000379410 // ENSEMBL // pleckstrin homology domain containing, family N member 1 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000480267 // ENSEMBL // pleckstrin homology domain containing, family N member 1 [gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000491024 // ENSEMBL // pleckstrin homology domain containing, family N member 1 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// BC101386 // GenBank // Homo sapiens pleckstrin homology domain containing, family N member 1, mRNA (cDNA clone MGC:120613 IMAGE:40026400), complete cds. // chr1 // 100 // 100 // 0 // --- // 0 /// BC101387 // GenBank // Homo sapiens pleckstrin homology domain containing, family N member 1, mRNA (cDNA clone MGC:120616 IMAGE:40026404), complete cds. // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097940 // Havana transcript // pleckstrin homology domain containing, family N member 1[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097941 // Havana transcript // pleckstrin homology domain containing, family N member 1[gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097942 // Havana transcript // pleckstrin homology domain containing, family N member 1[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000473255 // Havana transcript // pleckstrin homology domain containing, family N member 1[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000473256 // Havana transcript // pleckstrin homology domain containing, family N member 1[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// CCDS4.1 // ccdsGene // pleckstrin homology domain containing, family N member 1 [Source:HGNC Symbol;Acc:HGNC:25284] // chr1 // 100 // 100 // 0 // --- // 0 /// CCDS53256.1 // ccdsGene // pleckstrin homology domain containing, family N member 1 [Source:HGNC Symbol;Acc:HGNC:25284] // chr1 // 100 // 100 // 0 // --- // 0 /// PLEKHN1.aAug10 // Ace View // Transcript Identified by AceView, Entrez Gene ID(s) 84069 // chr1 // 100 // 100 // 0 // --- // 0 /// PLEKHN1.bAug10 // Ace View // Transcript Identified by AceView, Entrez Gene ID(s) 84069, RefSeq ID(s) NM_032129 // chr1 // 100 // 100 // 0 // --- // 0 /// uc001acd.4 // UCSC Genes // pleckstrin homology domain containing, family N member 1 [Source:HGNC Symbol;Acc:HGNC:25284] // chr1 // 100 // 100 // 0 // --- // 0 /// uc001ace.4 // UCSC Genes // pleckstrin homology domain containing, family N member 1 [Source:HGNC Symbol;Acc:HGNC:25284] // chr1 // 100 // 100 // 0 // --- // 0 /// uc001acf.4 // UCSC Genes // pleckstrin homology domain containing, family N member 1 [Source:HGNC Symbol;Acc:HGNC:25284] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057ayk.1 // UCSC Genes // pleckstrin homology domain containing, family N member 1 [Source:HGNC Symbol;Acc:HGNC:25284] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057ayl.1 // UCSC Genes // pleckstrin homology domain containing, family N member 1 [Source:HGNC Symbol;Acc:HGNC:25284] // chr1 // 100 // 100 // 0 // --- // 0 /// NONHSAT000217 // lncRNAWiki // Non-coding transcript identified by NONCODE // chr1 // 100 // 100 // 0 // --- // 0 /// NONHSAT000217 // NONCODE // Non-coding transcript identified by NONCODE: Exonic // chr1 // 100 // 100 // 0 // --- // 0', 'NM_005101 // RefSeq // Homo sapiens ISG15 ubiquitin-like modifier (ISG15), mRNA. // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000379389 // ENSEMBL // ISG15 ubiquitin-like modifier [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000624652 // ENSEMBL // ISG15 ubiquitin-like modifier [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000624697 // ENSEMBL // ISG15 ubiquitin-like modifier [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// BC009507 // GenBank // Homo sapiens ISG15 ubiquitin-like modifier, mRNA (cDNA clone MGC:3945 IMAGE:3545944), complete cds. // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097989 // Havana transcript // ISG15 ubiquitin-like modifier[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000479384 // Havana transcript // ISG15 ubiquitin-like modifier[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000479385 // Havana transcript // ISG15 ubiquitin-like modifier[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// CCDS6.1 // ccdsGene // ISG15 ubiquitin-like modifier [Source:HGNC Symbol;Acc:HGNC:4053] // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009211 // circbase // Salzman2013 ANNOTATED, CDS, coding, OVCODE, OVEXON, UTR3 best transcript NM_005101 // chr1 // 100 // 100 // 0 // --- // 0 /// ISG15.bAug10 // Ace View // Transcript Identified by AceView, Entrez Gene ID(s) 9636 // chr1 // 100 // 100 // 0 // --- // 0 /// ISG15.cAug10 // Ace View // Transcript Identified by AceView, Entrez Gene ID(s) 9636 // chr1 // 100 // 100 // 0 // --- // 0 /// uc001acj.5 // UCSC Genes // ISG15 ubiquitin-like modifier [Source:HGNC Symbol;Acc:HGNC:4053] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057ayq.1 // UCSC Genes // ISG15 ubiquitin-like modifier [Source:HGNC Symbol;Acc:HGNC:4053] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057ayr.1 // UCSC Genes // ISG15 ubiquitin-like modifier [Source:HGNC Symbol;Acc:HGNC:4053] // chr1 // 100 // 100 // 0 // --- // 0']}\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": "df56eb0c", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "a6a15acb", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:58:25.390973Z", "iopub.status.busy": "2025-03-25T06:58:25.390843Z", "iopub.status.idle": "2025-03-25T06:58:29.558752Z", "shell.execute_reply": "2025-03-25T06:58:29.558116Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First few probe IDs from gene expression data:\n", "Index(['AFFX-BkGr-GC03_st', 'AFFX-BkGr-GC04_st', 'AFFX-BkGr-GC05_st',\n", " 'AFFX-BkGr-GC06_st', 'AFFX-BkGr-GC07_st'],\n", " dtype='object', name='ID')\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene mapping preview:\n", " ID Gene\n", "0 TC0100006437.hg.1 NM_001005484 // RefSeq // Homo sapiens olfacto...\n", "1 TC0100006476.hg.1 NM_152486 // RefSeq // Homo sapiens sterile al...\n", "2 TC0100006479.hg.1 NM_198317 // RefSeq // Homo sapiens kelch-like...\n", "3 TC0100006480.hg.1 NM_001160184 // RefSeq // Homo sapiens pleckst...\n", "4 TC0100006483.hg.1 NM_005101 // RefSeq // Homo sapiens ISG15 ubiq...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data after mapping:\n", "(84779, 310)\n", "Index(['A-', 'A-1', 'A-2', 'A-52', 'A-E', 'A-I', 'A-II', 'A-IV', 'A-V', 'A0',\n", " 'A0A075B6T3', 'A0A075B739', 'A0A075B767', 'A0A087WSY0', 'A0A087WTG2',\n", " 'A0A087WTH5', 'A0A087WV48', 'A0A087WWA1', 'A0A087WWL8', 'A0A087WWU0'],\n", " dtype='object', name='Gene')\n" ] } ], "source": [ "# 1. Looking at the gene annotation columns and gene expression data indexes\n", "# The gene expression data has index values like 'AFFX-BkGr-GC03_st', etc.\n", "# In the gene annotation dataframe:\n", "# - 'ID' contains values like 'TC0100006437.hg.1' which don't match our gene expression indexes\n", "# - 'SPOT_ID.1' seems to contain gene information in a complex format\n", "\n", "# We need to extract the relationship between probe IDs and gene symbols\n", "# First, let's check if our gene expression IDs match any of the columns in gene_annotation\n", "# Since we couldn't find a direct match in the preview, we need to check if the gene_data's index values\n", "# are present in the gene_annotation dataframe\n", "\n", "# Check a few probe IDs from gene_data\n", "print(\"First few probe IDs from gene expression data:\")\n", "print(gene_data.index[:5])\n", "\n", "# Let's check if any column in gene_annotation might contain information about gene symbols\n", "# We need to extract gene symbols from the 'SPOT_ID.1' column which contains RefSeq annotations\n", "# Create a mapping function that will extract gene symbols from the complex annotation text\n", "\n", "# Create a mapping dataframe with probe IDs and gene symbols\n", "# We'll use the ID column as probe ID and extract gene symbols from SPOT_ID.1\n", "gene_mapping = get_gene_mapping(gene_annotation, 'ID', 'SPOT_ID.1')\n", "print(\"Gene mapping preview:\")\n", "print(gene_mapping.head())\n", "\n", "# 3. Apply the gene mapping to convert probe-level measurements to gene expression data\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "print(\"Gene expression data after mapping:\")\n", "print(gene_data.shape)\n", "print(gene_data.index[:20]) # Print first 20 gene symbols\n" ] }, { "cell_type": "markdown", "id": "50d2ea10", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "0383a60f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:58:29.560454Z", "iopub.status.busy": "2025-03-25T06:58:29.560336Z", "iopub.status.idle": "2025-03-25T06:58:40.907895Z", "shell.execute_reply": "2025-03-25T06:58:40.907226Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original gene count: 84779\n", "Normalized gene count: 19858\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Bladder_Cancer/gene_data/GSE245953.csv\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Clinical data structure:\n", "(1, 311)\n", "First few rows of clinical data:\n", " !Sample_geo_accession GSM7851882 \\\n", "0 !Sample_characteristics_ch1 condition: Muscle-invasive bladder cancer \n", "\n", " GSM7851883 \\\n", "0 condition: Muscle-invasive bladder cancer \n", "\n", " GSM7851884 \\\n", "0 condition: Muscle-invasive bladder cancer \n", "\n", " GSM7851885 \\\n", "0 condition: Muscle-invasive bladder cancer \n", "\n", " GSM7851886 \\\n", "0 condition: Muscle-invasive bladder cancer \n", "\n", " GSM7851887 \\\n", "0 condition: Muscle-invasive bladder cancer \n", "\n", " GSM7851888 \\\n", "0 condition: Muscle-invasive bladder cancer \n", "\n", " GSM7851889 \\\n", "0 condition: Muscle-invasive bladder cancer \n", "\n", " GSM7851890 ... \\\n", "0 condition: Muscle-invasive bladder cancer ... \n", "\n", " GSM7852182 \\\n", "0 condition: Muscle-invasive bladder cancer \n", "\n", " GSM7852183 \\\n", "0 condition: Muscle-invasive bladder cancer \n", "\n", " GSM7852184 \\\n", "0 condition: Muscle-invasive bladder cancer \n", "\n", " GSM7852185 \\\n", "0 condition: Muscle-invasive bladder cancer \n", "\n", " GSM7852186 \\\n", "0 condition: Muscle-invasive bladder cancer \n", "\n", " GSM7852187 \\\n", "0 condition: Muscle-invasive bladder cancer \n", "\n", " GSM7852188 \\\n", "0 condition: Muscle-invasive bladder cancer \n", "\n", " GSM7852189 \\\n", "0 condition: Muscle-invasive bladder cancer \n", "\n", " GSM7852190 \\\n", "0 condition: Muscle-invasive bladder cancer \n", "\n", " GSM7852191 \n", "0 condition: Muscle-invasive bladder cancer \n", "\n", "[1 rows x 311 columns]\n", "Clinical data shape after extraction: (1, 310)\n", "First few sample IDs in clinical data:\n", "['GSM7851882', 'GSM7851883', 'GSM7851884', 'GSM7851885', 'GSM7851886']\n", "First few sample IDs in gene data:\n", "['GSM7851882', 'GSM7851883', 'GSM7851884', 'GSM7851885', 'GSM7851886']\n", "Number of common samples between clinical and gene data: 310\n", "Clinical data saved to ../../output/preprocess/Bladder_Cancer/clinical_data/GSE245953.csv\n", "Linked data shape: (310, 19859)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data shape after handling missing values: (310, 19859)\n", "Quartiles for 'Bladder_Cancer':\n", " 25%: 1.0\n", " 50% (Median): 1.0\n", " 75%: 1.0\n", "Min: 1.0\n", "Max: 1.0\n", "The distribution of the feature 'Bladder_Cancer' in this dataset is severely biased.\n", "\n", "The dataset was determined to be not usable for analysis due to bias in the trait distribution.\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "# First, normalize gene symbols using the function from the library\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Original gene count: {len(gene_data)}\")\n", "print(f\"Normalized gene count: {len(normalized_gene_data)}\")\n", "\n", "# Create directory for the gene data file if it doesn't exist\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "\n", "# Save the normalized gene data to a CSV file\n", "normalized_gene_data.to_csv(out_gene_data_file)\n", "print(f\"Normalized gene data saved to {out_gene_data_file}\")\n", "\n", "# 2. Load clinical data from the matrix file again to ensure we have the correct sample IDs\n", "background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']\n", "clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']\n", "_, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)\n", "\n", "print(\"Clinical data structure:\")\n", "print(clinical_data.shape)\n", "print(\"First few rows of clinical data:\")\n", "print(clinical_data.head())\n", "\n", "# Extract clinical features with the correct sample IDs\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", "print(f\"Clinical data shape after extraction: {selected_clinical_df.shape}\")\n", "print(\"First few sample IDs in clinical data:\")\n", "print(list(selected_clinical_df.columns)[:5])\n", "print(\"First few sample IDs in gene data:\")\n", "print(list(normalized_gene_data.columns)[:5])\n", "\n", "# Check for column overlap\n", "common_samples = set(selected_clinical_df.columns).intersection(set(normalized_gene_data.columns))\n", "print(f\"Number of common samples between clinical and gene data: {len(common_samples)}\")\n", "\n", "# Save the clinical data for inspection\n", "os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", "selected_clinical_df.to_csv(out_clinical_data_file)\n", "print(f\"Clinical data saved to {out_clinical_data_file}\")\n", "\n", "# Link the clinical and genetic data\n", "linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "\n", "# Check if linking was successful\n", "if len(linked_data) == 0 or trait not in linked_data.columns:\n", " print(\"Linking clinical and genetic data failed - no valid rows or trait column missing\")\n", " \n", " # Check what columns are in the linked data\n", " if len(linked_data.columns) > 0:\n", " print(\"Columns in linked data:\")\n", " print(list(linked_data.columns)[:10]) # Print first 10 columns\n", " \n", " # Set is_usable to False and save cohort info\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=True, # Consider it biased if linking fails\n", " df=pd.DataFrame({trait: [], 'Gender': []}), \n", " note=\"Data linking failed - unable to match sample IDs between clinical and gene expression data.\"\n", " )\n", " print(\"The dataset was determined to be not usable for analysis.\")\n", "else:\n", " # 3. Handle missing values in the linked data\n", " linked_data = handle_missing_values(linked_data, trait)\n", " \n", " print(f\"Linked data shape after handling missing values: {linked_data.shape}\")\n", " \n", " # 4. Determine whether the trait and demographic features are severely biased\n", " is_trait_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", " \n", " # 5. Conduct quality check and save the cohort information.\n", " note = \"Dataset contains gene expression data from bladder cancer samples with molecular subtyping 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_trait_biased, \n", " df=linked_data, \n", " note=note\n", " )\n", " \n", " # 6. If the linked data is usable, save it as a CSV file.\n", " if is_usable:\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(\"The dataset was determined to be not usable for analysis due to bias in the trait distribution.\")" ] } ], "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 }