{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "1a130bc7", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:56:51.903284Z", "iopub.status.busy": "2025-03-25T06:56:51.903026Z", "iopub.status.idle": "2025-03-25T06:56:52.067247Z", "shell.execute_reply": "2025-03-25T06:56:52.066932Z" } }, "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 = \"GSE201395\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Bladder_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Bladder_Cancer/GSE201395\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Bladder_Cancer/GSE201395.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Bladder_Cancer/gene_data/GSE201395.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Bladder_Cancer/clinical_data/GSE201395.csv\"\n", "json_path = \"../../output/preprocess/Bladder_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "2dd0da00", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "e7db3cf0", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:56:52.068649Z", "iopub.status.busy": "2025-03-25T06:56:52.068517Z", "iopub.status.idle": "2025-03-25T06:56:52.178839Z", "shell.execute_reply": "2025-03-25T06:56:52.178557Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"An in vitro study of the development of resistance to FGFR inhibition in two urothelial carcinoma cell lines\"\n", "!Series_summary\t\"Expression array data was used to compare parental FGFR3-TACC3 fusion-driven urothelial cell lines with their FGFR inhibitor-resistant derivatives.\"\n", "!Series_summary\t\"In this dataset, we include RT112 and RT4 parental cells, RT112 cells acutely treated with PD173074 (24 h), RT112 and RT4 resistant derivatives cultured with drug and their resistant derivatives cultured for four to six passages out of drug.\"\n", "!Series_overall_design\t\"11 cell lines were analysed on the Affymetrix HTA 2.0 platform: RT112 and RT4 parental cells (RT112 parental no PD; RT4 parental no PD), RT112 cells acutely treated with PD173074 (24 h) (RT112 parental PD), RT112 and RT4 resistant derivatives cultured with drug (RT112 R1 PD; RT112 R2 PD; RT112 R3 PD; RT4 R1 PD) and their resistant derivatives cultured for four to six passages out of drug (RT112 R1 no PD; RT112 R2 no PD; RT112 R3 no PD; RT4 R1 no PD). Each cell line was analysed in triplicate (33 samples in total).\"\n", "Sample Characteristics Dictionary:\n", "{0: ['cell line: urothelial carcinoma cell line, RT112', 'cell line: urothelial carcinoma cell line, RT4']}\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": "374fbef2", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "dc88d5b2", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:56:52.180039Z", "iopub.status.busy": "2025-03-25T06:56:52.179923Z", "iopub.status.idle": "2025-03-25T06:56:52.186906Z", "shell.execute_reply": "2025-03-25T06:56:52.186653Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical Data Preview:\n", "{'GSM6062606': [1.0], 'GSM6062607': [1.0], 'GSM6062608': [1.0], 'GSM6062609': [1.0], 'GSM6062610': [1.0], 'GSM6062611': [1.0], 'GSM6062612': [1.0], 'GSM6062613': [1.0], 'GSM6062614': [1.0], 'GSM6062615': [1.0], 'GSM6062616': [1.0], 'GSM6062617': [1.0], 'GSM6062618': [1.0], 'GSM6062619': [1.0], 'GSM6062620': [1.0], 'GSM6062621': [1.0], 'GSM6062622': [1.0], 'GSM6062623': [1.0], 'GSM6062624': [1.0], 'GSM6062625': [1.0], 'GSM6062626': [1.0], 'GSM6062627': [1.0], 'GSM6062628': [1.0], 'GSM6062629': [1.0], 'GSM6062630': [1.0], 'GSM6062631': [1.0], 'GSM6062632': [1.0], 'GSM6062633': [1.0], 'GSM6062634': [1.0], 'GSM6062635': [1.0], 'GSM6062636': [1.0], 'GSM6062637': [1.0], 'GSM6062638': [1.0]}\n", "Clinical data saved to ../../output/preprocess/Bladder_Cancer/clinical_data/GSE201395.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the Series overall design, this dataset uses Affymetrix HTA 2.0 platform\n", "# which is a gene expression microarray, so gene expression data should be available\n", "is_gene_available = True\n", "\n", "# 2.1 Data Availability\n", "# From the sample characteristics dictionary, we can see that this dataset contains information\n", "# about cell lines, not human patients. The dataset is comparing parental cell lines with\n", "# resistant derivatives, which is not directly related to our trait (Bladder_Cancer)\n", "# There is no information about age or gender since these are cell lines\n", "trait_row = 0 # The cell line information can be used to infer bladder cancer status\n", "age_row = None # No age information available for cell lines\n", "gender_row = None # No gender information available for cell lines\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value):\n", " \"\"\"Convert cell line information to binary trait (Bladder_Cancer).\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " # All cell lines in this dataset are urothelial carcinoma (bladder cancer) cell lines\n", " # So they all would be classified as having the trait\n", " if \"urothelial carcinoma\" in value.lower():\n", " return 1\n", " else:\n", " return None\n", "\n", "# No age conversion function needed as age data is not available\n", "def convert_age(value):\n", " return None\n", "\n", "# No gender conversion function needed as gender data is not available\n", "def convert_gender(value):\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Determine trait data availability\n", "is_trait_available = trait_row is not None\n", "\n", "# Validate and save cohort info\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:\n", " # We need to check if clinical_data is already defined from a previous step\n", " try:\n", " clinical_data\n", " except NameError:\n", " # If clinical_data is not defined, we need to notify about this issue\n", " print(\"Warning: clinical_data DataFrame is not available from previous steps.\")\n", " else:\n", " # Extract clinical features\n", " 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 clinical dataframe\n", " preview = preview_df(clinical_df)\n", " print(\"Clinical Data Preview:\")\n", " print(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 the clinical data\n", " clinical_df.to_csv(out_clinical_data_file)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "e70baaef", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "40bec053", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:56:52.187932Z", "iopub.status.busy": "2025-03-25T06:56:52.187831Z", "iopub.status.idle": "2025-03-25T06:56:52.320898Z", "shell.execute_reply": "2025-03-25T06:56:52.320570Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['2824546_st', '2824549_st', '2824551_st', '2824554_st', '2827992_st',\n", " '2827995_st', '2827996_st', '2828010_st', '2828012_st', '2835442_st',\n", " '2835447_st', '2835453_st', '2835456_st', '2835459_st', '2835461_st',\n", " '2839509_st', '2839511_st', '2839513_st', '2839515_st', '2839517_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": "c6dcfba6", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "95de2f14", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:56:52.322226Z", "iopub.status.busy": "2025-03-25T06:56:52.322106Z", "iopub.status.idle": "2025-03-25T06:56:52.323956Z", "shell.execute_reply": "2025-03-25T06:56:52.323693Z" } }, "outputs": [], "source": [ "# These identifiers appear to be probe IDs from an Affymetrix microarray, not human gene symbols\n", "# They follow the format of numerical IDs with \"_st\" suffix, which is typical for Affymetrix arrays\n", "# They need to be mapped to standard gene symbols for proper biological interpretation\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "82c92f3f", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "d0f0661c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:56:52.325083Z", "iopub.status.busy": "2025-03-25T06:56:52.324966Z", "iopub.status.idle": "2025-03-25T06:56:57.055025Z", "shell.execute_reply": "2025-03-25T06:56:57.054682Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['TC01000001.hg.1', 'TC01000002.hg.1', 'TC01000003.hg.1', 'TC01000004.hg.1', 'TC01000005.hg.1'], 'probeset_id': ['TC01000001.hg.1', 'TC01000002.hg.1', 'TC01000003.hg.1', 'TC01000004.hg.1', 'TC01000005.hg.1'], 'seqname': ['chr1', 'chr1', 'chr1', 'chr1', 'chr1'], 'strand': ['+', '+', '+', '+', '+'], 'start': ['11869', '29554', '69091', '160446', '317811'], 'stop': ['14409', '31109', '70008', '161525', '328581'], 'total_probes': [49.0, 60.0, 30.0, 30.0, 191.0], 'gene_assignment': ['NR_046018 // DDX11L1 // DEAD/H (Asp-Glu-Ala-Asp/His) box helicase 11 like 1 // 1p36.33 // 100287102 /// ENST00000456328 // DDX11L5 // DEAD/H (Asp-Glu-Ala-Asp/His) box helicase 11 like 5 // 9p24.3 // 100287596 /// ENST00000456328 // DDX11L1 // DEAD/H (Asp-Glu-Ala-Asp/His) box helicase 11 like 1 // 1p36.33 // 100287102', 'ENST00000408384 // MIR1302-11 // microRNA 1302-11 // --- // 100422919 /// ENST00000408384 // MIR1302-10 // microRNA 1302-10 // --- // 100422834 /// ENST00000408384 // MIR1302-9 // microRNA 1302-9 // --- // 100422831 /// ENST00000408384 // MIR1302-2 // microRNA 1302-2 // --- // 100302278 /// ENST00000469289 // MIR1302-11 // microRNA 1302-11 // --- // 100422919 /// ENST00000469289 // MIR1302-10 // microRNA 1302-10 // --- // 100422834 /// ENST00000469289 // MIR1302-9 // microRNA 1302-9 // --- // 100422831 /// ENST00000469289 // MIR1302-2 // microRNA 1302-2 // --- // 100302278 /// ENST00000473358 // MIR1302-11 // microRNA 1302-11 // --- // 100422919 /// ENST00000473358 // MIR1302-10 // microRNA 1302-10 // --- // 100422834 /// ENST00000473358 // MIR1302-9 // microRNA 1302-9 // --- // 100422831 /// ENST00000473358 // MIR1302-2 // microRNA 1302-2 // --- // 100302278 /// OTTHUMT00000002841 // OTTHUMG00000000959 // NULL // --- // --- /// OTTHUMT00000002841 // RP11-34P13.3 // NULL // --- // --- /// OTTHUMT00000002840 // OTTHUMG00000000959 // NULL // --- // --- /// OTTHUMT00000002840 // RP11-34P13.3 // NULL // --- // ---', 'NM_001005484 // OR4F5 // olfactory receptor, family 4, subfamily F, member 5 // 1p36.33 // 79501 /// ENST00000335137 // OR4F5 // olfactory receptor, family 4, subfamily F, member 5 // 1p36.33 // 79501 /// OTTHUMT00000003223 // OR4F5 // NULL // --- // ---', 'OTTHUMT00000007169 // OTTHUMG00000002525 // NULL // --- // --- /// OTTHUMT00000007169 // RP11-34P13.9 // NULL // --- // ---', 'NR_028322 // LOC100132287 // uncharacterized LOC100132287 // 1p36.33 // 100132287 /// NR_028327 // LOC100133331 // uncharacterized LOC100133331 // 1p36.33 // 100133331 /// ENST00000425496 // LOC101060495 // uncharacterized LOC101060495 // --- // 101060495 /// ENST00000425496 // LOC101060494 // uncharacterized LOC101060494 // --- // 101060494 /// ENST00000425496 // LOC101059936 // uncharacterized LOC101059936 // --- // 101059936 /// ENST00000425496 // LOC100996502 // uncharacterized LOC100996502 // --- // 100996502 /// ENST00000425496 // LOC100996328 // uncharacterized LOC100996328 // --- // 100996328 /// ENST00000425496 // LOC100287894 // uncharacterized LOC100287894 // 7q11.21 // 100287894 /// NR_028325 // LOC100132062 // uncharacterized LOC100132062 // 5q35.3 // 100132062 /// OTTHUMT00000346878 // OTTHUMG00000156968 // NULL // --- // --- /// OTTHUMT00000346878 // RP4-669L17.10 // NULL // --- // --- /// OTTHUMT00000346879 // OTTHUMG00000156968 // NULL // --- // --- /// OTTHUMT00000346879 // RP4-669L17.10 // NULL // --- // --- /// OTTHUMT00000346880 // OTTHUMG00000156968 // NULL // --- // --- /// OTTHUMT00000346880 // RP4-669L17.10 // NULL // --- // --- /// OTTHUMT00000346881 // OTTHUMG00000156968 // NULL // --- // --- /// OTTHUMT00000346881 // RP4-669L17.10 // NULL // --- // ---'], 'mrna_assignment': ['NR_046018 // RefSeq // Homo sapiens DEAD/H (Asp-Glu-Ala-Asp/His) box helicase 11 like 1 (DDX11L1), non-coding RNA. // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000456328 // ENSEMBL // cdna:known chromosome:GRCh37:1:11869:14409:1 gene:ENSG00000223972 gene_biotype:pseudogene transcript_biotype:processed_transcript // chr1 // 100 // 100 // 0 // --- // 0 /// uc001aaa.3 // UCSC Genes // --- // chr1 // 100 // 100 // 0 // --- // 0 /// uc010nxq.1 // UCSC Genes // --- // chr1 // 100 // 100 // 0 // --- // 0 /// uc010nxr.1 // UCSC Genes // --- // chr1 // 100 // 100 // 0 // --- // 0', 'ENST00000408384 // ENSEMBL // ncrna:miRNA chromosome:GRCh37:1:30366:30503:1 gene:ENSG00000221311 gene_biotype:miRNA transcript_biotype:miRNA // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000469289 // ENSEMBL // havana:lincRNA chromosome:GRCh37:1:30267:31109:1 gene:ENSG00000243485 gene_biotype:lincRNA transcript_biotype:lincRNA // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000473358 // ENSEMBL // havana:lincRNA chromosome:GRCh37:1:29554:31097:1 gene:ENSG00000243485 gene_biotype:lincRNA transcript_biotype:lincRNA // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000002841 // Havana transcript // cdna:all chromosome:VEGA52:1:30267:31109:1 Gene:OTTHUMG00000000959 // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000002840 // Havana transcript // cdna:all chromosome:VEGA52:1:29554:31097:1 Gene:OTTHUMG00000000959 // chr1 // 100 // 100 // 0 // --- // 0', 'NM_001005484 // RefSeq // Homo sapiens olfactory receptor, family 4, subfamily F, member 5 (OR4F5), mRNA. // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000335137 // ENSEMBL // cdna:known chromosome:GRCh37:1:69091:70008:1 gene:ENSG00000186092 gene_biotype:protein_coding transcript_biotype:protein_coding // chr1 // 100 // 100 // 0 // --- // 0 /// uc001aal.1 // UCSC Genes // --- // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000003223 // Havana transcript // cdna:all chromosome:VEGA52:1:69091:70008:1 Gene:OTTHUMG00000001094 // chr1 // 100 // 100 // 0 // --- // 0', 'ENST00000496488 // ENSEMBL // havana:lincRNA chromosome:GRCh37:1:160446:161525:1 gene:ENSG00000241599 gene_biotype:lincRNA transcript_biotype:lincRNA // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000007169 // Havana transcript // cdna:all chromosome:VEGA52:1:160446:161525:1 Gene:OTTHUMG00000002525 // chr1 // 100 // 100 // 0 // --- // 0', 'NR_028322 // RefSeq // Homo sapiens uncharacterized LOC100132287 (LOC100132287), non-coding RNA. // chr1 // 100 // 100 // 0 // --- // 0 /// NR_028327 // RefSeq // Homo sapiens uncharacterized LOC100133331 (LOC100133331), non-coding RNA. // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000425496 // ENSEMBL // ensembl:lincRNA chromosome:GRCh37:1:324756:328453:1 gene:ENSG00000237094 gene_biotype:lincRNA transcript_biotype:lincRNA // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000426316 // ENSEMBL // [retired] cdna:known chromosome:GRCh37:1:317811:328455:1 gene:ENSG00000240876 gene_biotype:processed_transcript transcript_biotype:processed_transcript // chr1 // 100 // 100 // 0 // --- // 0 /// NR_028325 // RefSeq // Homo sapiens uncharacterized LOC100132062 (LOC100132062), non-coding RNA. // chr1 // 100 // 100 // 0 // --- // 0 /// uc009vjk.2 // UCSC Genes // --- // chr1 // 100 // 100 // 0 // --- // 0 /// uc021oeh.1 // UCSC Genes // --- // chr1 // 100 // 100 // 0 // --- // 0 /// uc021oei.1 // UCSC Genes // --- // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000346906 // Havana transcript // [retired] cdna:all chromosome:VEGA50:1:317811:328455:1 Gene:OTTHUMG00000156972 // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000346878 // Havana transcript // cdna:all chromosome:VEGA52:1:320162:321056:1 Gene:OTTHUMG00000156968 // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000346879 // Havana transcript // cdna:all chromosome:VEGA52:1:320162:324461:1 Gene:OTTHUMG00000156968 // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000346880 // Havana transcript // cdna:all chromosome:VEGA52:1:317720:324873:1 Gene:OTTHUMG00000156968 // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000346881 // Havana transcript // cdna:all chromosome:VEGA52:1:322672:324955:1 Gene:OTTHUMG00000156968 // chr1 // 100 // 100 // 0 // --- // 0'], 'swissprot': ['NR_046018 // B7ZGX0 /// NR_046018 // B7ZGX2 /// NR_046018 // B7ZGX7 /// NR_046018 // B7ZGX8 /// ENST00000456328 // B7ZGX0 /// ENST00000456328 // B7ZGX2 /// ENST00000456328 // B7ZGX3 /// ENST00000456328 // B7ZGX7 /// ENST00000456328 // B7ZGX8 /// ENST00000456328 // Q6ZU42', '---', 'NM_001005484 // Q8NH21 /// ENST00000335137 // Q8NH21', '---', 'NR_028325 // B4DYM5 /// NR_028325 // B4E0H4 /// NR_028325 // B4E3X0 /// NR_028325 // B4E3X2 /// NR_028325 // Q6ZQS4'], 'unigene': ['NR_046018 // Hs.714157 // testis| normal| adult /// ENST00000456328 // Hs.719844 // brain| testis| normal /// ENST00000456328 // Hs.714157 // testis| normal| adult /// ENST00000456328 // Hs.618434 // testis| normal', 'ENST00000469289 // Hs.622486 // eye| normal| adult /// ENST00000469289 // Hs.729632 // testis| normal /// ENST00000469289 // Hs.742718 // testis /// ENST00000473358 // Hs.622486 // eye| normal| adult /// ENST00000473358 // Hs.729632 // testis| normal /// ENST00000473358 // Hs.742718 // testis', 'NM_001005484 // Hs.554500 // --- /// ENST00000335137 // Hs.554500 // ---', '---', 'NR_028322 // Hs.446409 // adrenal gland| blood| bone| brain| connective tissue| embryonic tissue| eye| intestine| kidney| larynx| lung| lymph node| mouth| pharynx| placenta| prostate| skin| testis| thymus| thyroid| uterus| bladder carcinoma| chondrosarcoma| colorectal tumor| germ cell tumor| head and neck tumor| kidney tumor| leukemia| lung tumor| normal| primitive neuroectodermal tumor of the CNS| uterine tumor|embryoid body| blastocyst| fetus| neonate| adult /// NR_028327 // Hs.733048 // ascites| bladder| blood| brain| embryonic tissue| eye| intestine| kidney| larynx| liver| lung| mammary gland| mouth| pancreas| placenta| prostate| skin| stomach| testis| thymus| thyroid| trachea| uterus| bladder carcinoma| breast (mammary gland) tumor| colorectal tumor| gastrointestinal tumor| head and neck tumor| kidney tumor| leukemia| liver tumor| lung tumor| normal| pancreatic tumor| prostate cancer| retinoblastoma| skin tumor| soft tissue/muscle tissue tumor| uterine tumor|embryoid body| blastocyst| fetus| adult /// ENST00000425496 // Hs.744556 // mammary gland| normal| adult /// ENST00000425496 // Hs.660700 // eye| placenta| testis| normal| adult /// ENST00000425496 // Hs.518952 // blood| brain| intestine| lung| mammary gland| mouth| muscle| pharynx| placenta| prostate| spleen| testis| thymus| thyroid| trachea| breast (mammary gland) tumor| colorectal tumor| head and neck tumor| leukemia| lung tumor| normal| prostate cancer| fetus| adult /// ENST00000425496 // Hs.742131 // testis| normal| adult /// ENST00000425496 // Hs.636102 // uterus| uterine tumor /// ENST00000425496 // Hs.646112 // brain| intestine| larynx| lung| mouth| prostate| testis| thyroid| colorectal tumor| head and neck tumor| lung tumor| normal| prostate cancer| adult /// ENST00000425496 // Hs.647795 // brain| lung| lung tumor| adult /// ENST00000425496 // Hs.684307 // --- /// ENST00000425496 // Hs.720881 // testis| normal /// ENST00000425496 // Hs.729353 // brain| lung| placenta| testis| trachea| lung tumor| normal| fetus| adult /// ENST00000425496 // Hs.735014 // ovary| ovarian tumor /// NR_028325 // Hs.732199 // ascites| blood| brain| connective tissue| embryonic tissue| eye| intestine| kidney| lung| ovary| placenta| prostate| stomach| testis| thymus| uterus| chondrosarcoma| colorectal tumor| gastrointestinal tumor| kidney tumor| leukemia| lung tumor| normal| ovarian tumor| fetus| adult'], 'category': ['main', 'main', 'main', 'main', 'main'], 'locus type': ['Coding', 'Coding', 'Coding', 'Coding', 'Coding'], 'notes': ['---', '---', '---', '---', '2 retired transcript(s) from ENSEMBL, Havana transcript'], 'SPOT_ID': ['chr1(+):11869-14409', 'chr1(+):29554-31109', 'chr1(+):69091-70008', 'chr1(+):160446-161525', 'chr1(+):317811-328581']}\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": "deecda8b", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "4aa5a525", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:56:57.056365Z", "iopub.status.busy": "2025-03-25T06:56:57.056243Z", "iopub.status.idle": "2025-03-25T06:56:59.652354Z", "shell.execute_reply": "2025-03-25T06:56:59.652018Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene data index preview:\n", "Index(['2824546_st', '2824549_st', '2824551_st', '2824554_st', '2827992_st'], dtype='object', name='ID')\n", "\n", "Checking ID in gene_annotation:\n", "0 TC01000001.hg.1\n", "1 TC01000002.hg.1\n", "2 TC01000003.hg.1\n", "3 TC01000004.hg.1\n", "4 TC01000005.hg.1\n", "Name: ID, dtype: object\n", "\n", "Checking probeset_id in gene_annotation:\n", "0 TC01000001.hg.1\n", "1 TC01000002.hg.1\n", "2 TC01000003.hg.1\n", "3 TC01000004.hg.1\n", "4 TC01000005.hg.1\n", "Name: probeset_id, dtype: object\n", "\n", "Mapping dataframe sample:\n", " ID Gene\n", "0 TC01000001.hg.1 NR_046018 // DDX11L1 // DEAD/H (Asp-Glu-Ala-As...\n", "1 TC01000002.hg.1 ENST00000408384 // MIR1302-11 // microRNA 1302...\n", "\n", "First few probe IDs in gene expression data:\n", "Index(['2824546_st', '2824549_st', '2824551_st', '2824554_st', '2827992_st'], dtype='object', name='ID')\n", "\n", "First few probe IDs in mapping dataframe:\n", "0 TC01000001.hg.1\n", "1 TC01000002.hg.1\n", "2 TC01000003.hg.1\n", "3 TC01000004.hg.1\n", "4 TC01000005.hg.1\n", "Name: ID, dtype: object\n", "\n", "Number of common IDs between gene_data and mapping_df: 70523\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene expression data after mapping:\n", "Shape: (71528, 33)\n", "First few gene symbols: Index(['A-', 'A-2', 'A-52', 'A-575C2', 'A-E', 'A-I', 'A-II', 'A-IV', 'A-V',\n", " 'A0'],\n", " dtype='object', name='Gene')\n" ] } ], "source": [ "# 1. Observe gene identifiers in gene data and gene annotation data\n", "# In gene_data, identifiers have format like '2824546_st'\n", "# In gene_annotation, the most similar identifiers would be 'probeset_id' but they are different format\n", "# The 'gene_assignment' column contains gene symbols\n", "\n", "# Let's get a better look at the 'ID' column in gene_data and check if it matches any column in gene_annotation\n", "print(\"Gene data index preview:\")\n", "print(gene_data.index[:5])\n", "\n", "# Let's try to find a matching ID column in the gene annotation data\n", "# First check if any annotation columns contain matching formats\n", "for col in gene_annotation.columns:\n", " if col in ['ID', 'probeset_id']:\n", " print(f\"\\nChecking {col} in gene_annotation:\")\n", " print(gene_annotation[col][:5])\n", "\n", "# It appears there's a mismatch between the probe IDs in gene_data and gene_annotation\n", "# We need to determine how they relate to map them properly\n", "\n", "# Looking at the preview, we need to see if our gene expression probes map to the annotation data\n", "# Let's try to find a match by dropping the \"_st\" suffix\n", "\n", "# 2. Create a mapping from probe IDs to gene symbols\n", "# The 'gene_assignment' column contains gene information\n", "# We need to extract gene symbols from this column\n", "\n", "# Create a mapping dataframe by selecting the ID and gene_assignment columns\n", "mapping_df = gene_annotation[['ID', 'gene_assignment']].copy()\n", "mapping_df = mapping_df.rename(columns={'gene_assignment': 'Gene'})\n", "\n", "# Print a sample of mapping_df to confirm it has the right structure\n", "print(\"\\nMapping dataframe sample:\")\n", "print(mapping_df.head(2))\n", "\n", "# 3. Apply gene mapping to convert probe-level measurements to gene-level expression\n", "# Use the apply_gene_mapping function from the library\n", "\n", "# First, check if we can find any matches between gene_data and our mapping dataframe\n", "print(\"\\nFirst few probe IDs in gene expression data:\")\n", "print(gene_data.index[:5])\n", "print(\"\\nFirst few probe IDs in mapping dataframe:\")\n", "print(mapping_df['ID'][:5])\n", "\n", "# It seems there might be format differences between the two datasets\n", "# Let's try to find a connection or modify the mapping\n", "\n", "# Check if ID formats are compatible by looking at a subset\n", "common_ids = set(gene_data.index) & set(mapping_df['ID'])\n", "print(f\"\\nNumber of common IDs between gene_data and mapping_df: {len(common_ids)}\")\n", "\n", "# If no common IDs, we need to reformulate our approach\n", "# Let's try to create a mapping using the 'ID' column from gene_data\n", "# and extracting gene symbols from the 'gene_assignment' column\n", "\n", "# Modified approach: create a new mapping using extracted gene symbols\n", "gene_data_mapped = apply_gene_mapping(gene_data, mapping_df)\n", "\n", "# Preview the result\n", "print(\"\\nGene expression data after mapping:\")\n", "print(f\"Shape: {gene_data_mapped.shape}\")\n", "print(f\"First few gene symbols: {gene_data_mapped.index[:10]}\")\n" ] }, { "cell_type": "markdown", "id": "e66ffe43", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "cf24cc4b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:56:59.653805Z", "iopub.status.busy": "2025-03-25T06:56:59.653696Z", "iopub.status.idle": "2025-03-25T06:57:10.100136Z", "shell.execute_reply": "2025-03-25T06:57:10.099808Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene data after mapping, before normalization: (71528, 33)\n", "Sample of gene symbols before normalization:\n", "Index(['A-', 'A-2', 'A-52', 'A-575C2', 'A-E', 'A-I', 'A-II', 'A-IV', 'A-V',\n", " 'A0', 'A1', 'A1-', 'A10', 'A11', 'A12', 'A13', 'A14', 'A16', 'A18',\n", " 'A19'],\n", " dtype='object', name='Gene')\n", "Original gene count after mapping: 71528\n", "Normalized gene count: 24018\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data saved to ../../output/preprocess/Bladder_Cancer/gene_data/GSE201395.csv\n", "Clinical data shape: (1, 33)\n", "First few sample IDs in clinical data:\n", "['GSM6062606', 'GSM6062607', 'GSM6062608', 'GSM6062609', 'GSM6062610']\n", "First few sample IDs in gene data:\n", "['GSM6062606', 'GSM6062607', 'GSM6062608', 'GSM6062609', 'GSM6062610']\n", "Linked data shape: (33, 24019)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data shape after handling missing values: (33, 24019)\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. Bias in trait: True\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "# First, let's examine a few gene symbols before normalization to understand the issue\n", "gene_data_mapped = apply_gene_mapping(gene_data, mapping_df)\n", "print(f\"Gene data after mapping, before normalization: {gene_data_mapped.shape}\")\n", "print(f\"Sample of gene symbols before normalization:\")\n", "print(gene_data_mapped.index[:20]) # Display first 20 gene symbols\n", "\n", "# Try normalizing the mapped gene data\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data_mapped)\n", "print(f\"Original gene count after mapping: {len(gene_data_mapped)}\")\n", "print(f\"Normalized gene count: {len(normalized_gene_data)}\")\n", "\n", "# If normalization resulted in too few genes, use the mapped data without normalization\n", "if len(normalized_gene_data) < 1000: # Arbitrary threshold - if less than 1000 genes remain\n", " print(\"Warning: Gene symbol normalization removed too many genes. Using mapped gene data without normalization.\")\n", " normalized_gene_data = gene_data_mapped # Use the mapped data without normalization\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 gene data to a CSV file\n", "normalized_gene_data.to_csv(out_gene_data_file)\n", "print(f\"Gene data saved to {out_gene_data_file}\")\n", "\n", "# 2. Load clinical data from the CSV file we saved in a previous step\n", "try:\n", " selected_clinical_df = pd.read_csv(out_clinical_data_file, index_col=0)\n", "except FileNotFoundError:\n", " # If the file doesn't exist, extract from matrix file again\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", " # 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: {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", "# 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 we have enough valid data after linking\n", "if linked_data.shape[0] <= 1 or linked_data.shape[1] <= 1:\n", " print(\"Warning: Linked data has insufficient dimensions\")\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=linked_data, \n", " note=\"Data linking failed - insufficient dimensions in linked 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_cleaned = handle_missing_values(linked_data, trait)\n", " \n", " print(f\"Linked data shape after handling missing values: {linked_data_cleaned.shape}\")\n", " \n", " # Check if we still have enough data after cleaning\n", " if linked_data_cleaned.shape[0] < 10 or linked_data_cleaned.shape[1] < 10:\n", " print(\"Warning: After handling missing values, insufficient data remains for analysis\")\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,\n", " df=linked_data_cleaned, \n", " note=\"After cleaning, insufficient data remains for analysis.\"\n", " )\n", " print(\"The dataset was determined to be not usable for analysis.\")\n", " else:\n", " # 4. Determine whether the trait and demographic features are severely biased\n", " is_trait_biased, linked_data_cleaned = judge_and_remove_biased_features(linked_data_cleaned, trait)\n", " \n", " # 5. Conduct quality check and save the cohort information.\n", " note = \"Dataset contains gene expression data from bladder cancer cell lines.\"\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_cleaned, \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_cleaned.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", " else:\n", " print(f\"The dataset was determined to be not usable for analysis. Bias in trait: {is_trait_biased}\")" ] } ], "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 }