{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "1bbdfce9", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:10:00.649154Z", "iopub.status.busy": "2025-03-25T07:10:00.648960Z", "iopub.status.idle": "2025-03-25T07:10:00.813351Z", "shell.execute_reply": "2025-03-25T07:10:00.813022Z" } }, "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 = \"Intellectual_Disability\"\n", "cohort = \"GSE273850\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Intellectual_Disability\"\n", "in_cohort_dir = \"../../input/GEO/Intellectual_Disability/GSE273850\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Intellectual_Disability/GSE273850.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Intellectual_Disability/gene_data/GSE273850.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Intellectual_Disability/clinical_data/GSE273850.csv\"\n", "json_path = \"../../output/preprocess/Intellectual_Disability/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "2c0e6160", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "82fc6dca", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:10:00.814739Z", "iopub.status.busy": "2025-03-25T07:10:00.814607Z", "iopub.status.idle": "2025-03-25T07:10:00.885792Z", "shell.execute_reply": "2025-03-25T07:10:00.885507Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Expression data from human trisomy 21 and euploid fibroblasts, iPSCs, and neural progenitor cells\"\n", "!Series_summary\t\"Trisomy of human chromosome 21 (T21) gives rise to Down syndrome, the most frequent life-born autosomal aneuploidy. To enable in vitro analyses of the cellular and moelcular mechanisms leading to the neurological alterations associated with T21, we created and characterized a panel of genomically diverse T21 and euploid induced pluripotent stem cells (iPSCs) from fibroblasts obtained from the Coriell Institute for Biomedical Research, and we then differentiated these iPSCs into neural progenitor cells (NPCs).\"\n", "!Series_summary\t\"Microarray transcriptomic analyses were performed on this panel of fibroblasts, iPSCs, and NPCs, identifying genes and pathways that were altered in T21 lines relative to euploid as well as genes and pathways in NPCs that showed inter-individual variability.\"\n", "!Series_overall_design\t\"This study used cell lines obtained from racially diverse individuals with trisomy for human chromosome 21 along with age-and sex-matched euploid control cell lines. One RNA sample was collected per line. Fibroblast, iPSC, and NPC cDNA samples were hybridized to Affymetrix Clariom S HT arrays (902970, Thermofisher Scientific).\"\n", "Sample Characteristics Dictionary:\n", "{0: ['genotype: T21', 'genotype: Euploid'], 1: ['Sex: male', 'Sex: female']}\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": "26f28163", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "a6147b0e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:10:00.886835Z", "iopub.status.busy": "2025-03-25T07:10:00.886732Z", "iopub.status.idle": "2025-03-25T07:10:00.895514Z", "shell.execute_reply": "2025-03-25T07:10:00.895234Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of clinical data:\n", "{'GSM8438101': [1.0, 1.0], 'GSM8438102': [1.0, 0.0], 'GSM8438103': [1.0, 1.0], 'GSM8438104': [1.0, 0.0], 'GSM8438105': [1.0, 1.0], 'GSM8438106': [1.0, 0.0], 'GSM8438107': [1.0, 1.0], 'GSM8438108': [0.0, 0.0], 'GSM8438109': [0.0, 1.0], 'GSM8438110': [0.0, 0.0], 'GSM8438111': [1.0, 1.0], 'GSM8438112': [0.0, 1.0], 'GSM8438113': [0.0, 1.0], 'GSM8438114': [0.0, 1.0], 'GSM8438115': [0.0, 1.0], 'GSM8438116': [0.0, 0.0], 'GSM8438117': [0.0, 1.0], 'GSM8438118': [1.0, 1.0], 'GSM8438119': [1.0, 0.0], 'GSM8438120': [0.0, 0.0], 'GSM8438121': [1.0, 1.0], 'GSM8438122': [1.0, 0.0], 'GSM8438123': [1.0, 1.0], 'GSM8438124': [1.0, 0.0], 'GSM8438125': [1.0, 1.0], 'GSM8438126': [0.0, 0.0], 'GSM8438127': [0.0, 1.0], 'GSM8438128': [0.0, 0.0], 'GSM8438129': [1.0, 1.0], 'GSM8438130': [0.0, 1.0], 'GSM8438131': [0.0, 1.0], 'GSM8438132': [0.0, 1.0], 'GSM8438133': [0.0, 1.0], 'GSM8438134': [0.0, 0.0], 'GSM8438135': [0.0, 1.0], 'GSM8438136': [1.0, 1.0], 'GSM8438137': [1.0, 0.0], 'GSM8438138': [0.0, 0.0], 'GSM8438139': [1.0, 1.0], 'GSM8438140': [1.0, 0.0], 'GSM8438141': [1.0, 1.0], 'GSM8438142': [1.0, 0.0], 'GSM8438143': [1.0, 1.0], 'GSM8438144': [0.0, 0.0], 'GSM8438145': [0.0, 1.0], 'GSM8438146': [0.0, 0.0], 'GSM8438147': [0.0, 1.0], 'GSM8438148': [0.0, 1.0], 'GSM8438149': [0.0, 1.0], 'GSM8438150': [0.0, 0.0], 'GSM8438151': [0.0, 1.0]}\n", "Clinical data saved to ../../output/preprocess/Intellectual_Disability/clinical_data/GSE273850.csv\n" ] } ], "source": [ "# STEP 1: Gene Expression Data Availability\n", "# Based on the background information, this dataset contains gene expression data \n", "# from microarray transcriptomic analyses performed on fibroblasts, iPSCs, and NPCs\n", "is_gene_available = True\n", "\n", "# STEP 2: Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# For trait (Intellectual Disability), from the data we see trisomy 21 (Down syndrome) which is linked to intellectual disability\n", "trait_row = 0 # 'genotype: T21' vs 'genotype: Euploid'\n", "\n", "# Age is not available in the sample characteristics\n", "age_row = None\n", "\n", "# Gender information is available \n", "gender_row = 1 # 'Sex: male' vs 'Sex: female'\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value):\n", " \"\"\"Convert trisomy 21 status to binary value for intellectual disability\"\"\"\n", " if value is None:\n", " return None\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " if value.lower() == \"t21\":\n", " return 1 # T21 (Down syndrome) is associated with intellectual disability\n", " elif value.lower() == \"euploid\":\n", " return 0 # Euploid (normal) is the control\n", " else:\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender values to binary (0 for female, 1 for male)\"\"\"\n", " if value is None:\n", " return None\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " if value.lower() == \"male\":\n", " return 1\n", " elif value.lower() == \"female\":\n", " return 0\n", " else:\n", " return None\n", "\n", "# STEP 3: Save Metadata\n", "is_trait_available = trait_row is not None\n", "validate_and_save_cohort_info(\n", " is_final=False,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=is_gene_available,\n", " is_trait_available=is_trait_available\n", ")\n", "\n", "# STEP 4: Clinical Feature Extraction\n", "if trait_row is not None:\n", " # Use the library function to 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", " gender_row=gender_row,\n", " convert_gender=convert_gender\n", " )\n", " \n", " # Preview the extracted clinical data\n", " print(\"Preview of clinical data:\")\n", " print(preview_df(clinical_df))\n", " \n", " # Save clinical data to CSV\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": "f544b7ef", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "05a1f77a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:10:00.896486Z", "iopub.status.busy": "2025-03-25T07:10:00.896386Z", "iopub.status.idle": "2025-03-25T07:10:00.993922Z", "shell.execute_reply": "2025-03-25T07:10:00.993589Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Extracting gene data from matrix file:\n", "Successfully extracted gene data with 21448 rows\n", "First 20 gene IDs:\n", "Index(['TC0100006437.hg.1', 'TC0100006476.hg.1', 'TC0100006479.hg.1',\n", " 'TC0100006480.hg.1', 'TC0100006483.hg.1', 'TC0100006486.hg.1',\n", " 'TC0100006490.hg.1', 'TC0100006492.hg.1', 'TC0100006494.hg.1',\n", " 'TC0100006497.hg.1', 'TC0100006499.hg.1', 'TC0100006501.hg.1',\n", " 'TC0100006502.hg.1', 'TC0100006514.hg.1', 'TC0100006516.hg.1',\n", " 'TC0100006517.hg.1', 'TC0100006524.hg.1', 'TC0100006540.hg.1',\n", " 'TC0100006548.hg.1', 'TC0100006550.hg.1'],\n", " dtype='object', name='ID')\n", "\n", "Gene expression data available: True\n" ] } ], "source": [ "# 1. Get the file paths for the SOFT file and matrix file\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. Extract gene expression data from the matrix file\n", "try:\n", " print(\"Extracting gene data from matrix file:\")\n", " gene_data = get_genetic_data(matrix_file)\n", " if gene_data.empty:\n", " print(\"Extracted gene expression data is empty\")\n", " is_gene_available = False\n", " else:\n", " print(f\"Successfully extracted gene data with {len(gene_data.index)} rows\")\n", " print(\"First 20 gene IDs:\")\n", " print(gene_data.index[:20])\n", " is_gene_available = True\n", "except Exception as e:\n", " print(f\"Error extracting gene data: {e}\")\n", " print(\"This dataset appears to have an empty or malformed gene expression matrix\")\n", " is_gene_available = False\n", "\n", "print(f\"\\nGene expression data available: {is_gene_available}\")\n" ] }, { "cell_type": "markdown", "id": "2e72260c", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "6dcc97b1", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:10:00.995211Z", "iopub.status.busy": "2025-03-25T07:10:00.995102Z", "iopub.status.idle": "2025-03-25T07:10:00.996886Z", "shell.execute_reply": "2025-03-25T07:10:00.996619Z" } }, "outputs": [], "source": [ "# The identifiers \"TC0100006437.hg.1\", \"TC0100006476.hg.1\", etc. appear to be probe IDs\n", "# from a microarray platform rather than standard human gene symbols.\n", "# These are likely Affymetrix transcript cluster IDs that need to be mapped to gene symbols.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "1caf07f9", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "9315bd16", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:10:00.998039Z", "iopub.status.busy": "2025-03-25T07:10:00.997933Z", "iopub.status.idle": "2025-03-25T07:10:03.519396Z", "shell.execute_reply": "2025-03-25T07:10:03.518973Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Extracting gene annotation data from SOFT file...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Successfully extracted gene annotation data with 1121088 rows\n", "\n", "Gene annotation preview (first few rows):\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", "\n", "Column names in gene annotation data:\n", "['ID', 'probeset_id', 'seqname', 'strand', 'start', 'stop', 'total_probes', 'category', 'SPOT_ID', 'SPOT_ID.1']\n", "\n", "The dataset contains genomic regions (SPOT_ID) that could be used for location-based gene mapping.\n", "Example SPOT_ID format: Coding\n" ] } ], "source": [ "# 1. Extract gene annotation data from the SOFT file\n", "print(\"Extracting gene annotation data from SOFT file...\")\n", "try:\n", " # Use the library function to extract gene annotation\n", " gene_annotation = get_gene_annotation(soft_file)\n", " print(f\"Successfully extracted gene annotation data with {len(gene_annotation.index)} rows\")\n", " \n", " # Preview the annotation DataFrame\n", " print(\"\\nGene annotation preview (first few rows):\")\n", " print(preview_df(gene_annotation))\n", " \n", " # Show column names to help identify which columns we need for mapping\n", " print(\"\\nColumn names in gene annotation data:\")\n", " print(gene_annotation.columns.tolist())\n", " \n", " # Check for relevant mapping columns\n", " if 'GB_ACC' in gene_annotation.columns:\n", " print(\"\\nThe dataset contains GenBank accessions (GB_ACC) that could be used for gene mapping.\")\n", " # Count non-null values in GB_ACC column\n", " non_null_count = gene_annotation['GB_ACC'].count()\n", " print(f\"Number of rows with GenBank accessions: {non_null_count} out of {len(gene_annotation)}\")\n", " \n", " if 'SPOT_ID' in gene_annotation.columns:\n", " print(\"\\nThe dataset contains genomic regions (SPOT_ID) that could be used for location-based gene mapping.\")\n", " print(\"Example SPOT_ID format:\", gene_annotation['SPOT_ID'].iloc[0])\n", " \n", "except Exception as e:\n", " print(f\"Error processing gene annotation data: {e}\")\n", " is_gene_available = False\n" ] }, { "cell_type": "markdown", "id": "1ee98b7f", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "200043fd", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:10:03.520930Z", "iopub.status.busy": "2025-03-25T07:10:03.520805Z", "iopub.status.idle": "2025-03-25T07:10:06.744864Z", "shell.execute_reply": "2025-03-25T07:10:06.744542Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Retrieved gene expression data with 21448 rows and 51 columns\n", "Creating gene mapping dataframe...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Created mapping dataframe with 23477 rows\n", "Sample of mapping data:\n", "{'ID': ['TC0100006437.hg.1', 'TC0100006476.hg.1', 'TC0100006479.hg.1', 'TC0100006480.hg.1', 'TC0100006483.hg.1'], 'Gene': [['OR4F5', 'CCDS30547'], ['SAMD11', 'BC024295', 'BC033213', 'CCDS2'], ['KLHL17', 'BC166618', 'CCDS30550'], ['PLEKHN1', 'BC101386', 'BC101387', 'CCDS4', 'CCDS53256'], ['ISG15', 'BC009507', 'CCDS6']]}\n", "\n", "Converting probe-level measurements to gene expression data...\n", "WARNING: No gene expression data was generated after mapping. This indicates a problem with the gene mapping process.\n" ] } ], "source": [ "# 1. Examine gene identifiers and annotations\n", "# Based on the previous outputs, we can see the gene expression data identifiers (e.g., TC0100006437.hg.1)\n", "# match with the 'ID' column in the gene annotation dataframe.\n", "# The SPOT_ID.1 column contains gene name information embedded with RefSeq, ENSEMBL, etc. annotations\n", "\n", "# First, ensure we have the gene expression data retrieved from the matrix file\n", "gene_data = get_genetic_data(matrix_file)\n", "print(f\"Retrieved gene expression data with {gene_data.shape[0]} rows and {gene_data.shape[1]} columns\")\n", "\n", "# 2. Create gene mapping dataframe\n", "print(\"Creating gene mapping dataframe...\")\n", "\n", "# Use the library function to extract gene symbols from SPOT_ID.1 field\n", "gene_annotation['Gene'] = gene_annotation['SPOT_ID.1'].apply(extract_human_gene_symbols)\n", "\n", "# Filter out common database/platform terms that are not actual gene symbols\n", "def filter_gene_symbols(symbols):\n", " if not symbols:\n", " return []\n", " filtered = []\n", " common_terms = {'ENSEMBL', 'UCSC', 'CCDS', 'HGNC', 'NONCODE', 'MGC', 'IMAGE', \n", " 'ANNOTATED', 'CDS', 'INTERNAL', 'OVCODE', 'OVERLAPTX', 'OVEXON', 'UTR3', 'ID'}\n", " for symbol in symbols:\n", " # Keep only if it's not in our list of common terms and looks like a gene symbol\n", " if symbol not in common_terms and re.match(r'^[A-Z0-9-]{2,15}$', symbol):\n", " filtered.append(symbol)\n", " return filtered\n", "\n", "# Apply filtering to remove non-gene terms\n", "gene_annotation['Gene'] = gene_annotation['Gene'].apply(filter_gene_symbols)\n", "\n", "# Create mapping dataframe with ID and Gene columns\n", "mapping_df = gene_annotation[['ID', 'Gene']].copy()\n", "mapping_df = mapping_df.dropna(subset=['Gene'])\n", "\n", "# Filter to include only rows where Gene is not an empty list\n", "mapping_df = mapping_df[mapping_df['Gene'].apply(lambda x: len(x) > 0)]\n", "\n", "print(f\"Created mapping dataframe with {len(mapping_df)} rows\")\n", "print(\"Sample of mapping data:\")\n", "print(preview_df(mapping_df))\n", "\n", "# 3. Convert probe-level measurements to gene expression data\n", "print(\"\\nConverting probe-level measurements to gene expression data...\")\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "\n", "# Check the result\n", "if gene_data.empty:\n", " print(\"WARNING: No gene expression data was generated after mapping. This indicates a problem with the gene mapping process.\")\n", " is_gene_available = False\n", "else:\n", " print(f\"Successfully generated gene expression data with {len(gene_data.index)} genes\")\n", " print(f\"First 10 gene symbols: {list(gene_data.index[:10])}\")\n", " is_gene_available = True\n", "\n", "# Normalize gene symbols to standard NCBI Gene symbols\n", "if is_gene_available:\n", " print(\"\\nNormalizing gene symbols using NCBI Gene database information...\")\n", " gene_data = normalize_gene_symbols_in_index(gene_data)\n", " print(f\"After normalization: {len(gene_data.index)} genes\")\n", " print(f\"First 10 normalized gene symbols: {list(gene_data.index[:10])}\")\n", " \n", " # Save processed gene data\n", " gene_data.to_csv(out_gene_data_file)\n", " print(f\"Gene expression data saved to {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "1725922c", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "18d6b2cb", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:10:06.746158Z", "iopub.status.busy": "2025-03-25T07:10:06.746039Z", "iopub.status.idle": "2025-03-25T07:10:36.749136Z", "shell.execute_reply": "2025-03-25T07:10:36.748749Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Attempting to fix gene mapping issues...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Created new mapping dataframe with 19524 rows\n", "Sample of new mapping data:\n", "{'ID': ['TC0100006437.hg.1', 'TC0100006476.hg.1', 'TC0100006479.hg.1'], 'Gene': [['001005484'], ['152486'], ['198317']]}\n", "Still no gene expression data after improved mapping. Trying alternative method...\n", "Creating a direct gene mapping from annotation...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Direct mapping created with 4261 probe-gene pairs\n", "WARNING: Gene mapping still unsuccessful. Cannot proceed with gene expression analysis.\n", "\n", "Loading clinical data from file...\n", "Available clinical features: ['Intellectual_Disability', 'Gender']\n", "Gene expression data not available. Cannot link with clinical data.\n", "\n", "Performing final validation...\n", "Abnormality detected in the cohort: GSE273850. Preprocessing failed.\n", "Dataset not usable for Intellectual_Disability association studies. Data not saved.\n" ] } ], "source": [ "# 1-2. Check if we need to fix the gene mapping issue from the previous step\n", "print(\"Attempting to fix gene mapping issues...\")\n", "try:\n", " # Re-extract the gene expression data from the matrix file\n", " gene_data = get_genetic_data(matrix_file)\n", " \n", " # Create a simpler mapping for genes based on the SPOT_ID.1 field\n", " # This approach extracts potential gene symbols more aggressively\n", " def extract_likely_gene(text):\n", " if not isinstance(text, str):\n", " return []\n", " # Use a pattern that's more likely to capture gene names from the RefSeq annotations\n", " matches = re.findall(r'([A-Z0-9]+)(?:\\s+//\\s+RefSeq|\\[Source:HGNC Symbol;Acc:HGNC)', text)\n", " return list(dict.fromkeys(matches)) # Remove duplicates while preserving order\n", " \n", " # Apply the new extraction method\n", " gene_annotation['Gene'] = gene_annotation['SPOT_ID.1'].apply(extract_likely_gene)\n", " \n", " # Create mapping dataframe with ID and Gene columns\n", " mapping_df = gene_annotation[['ID', 'Gene']].copy()\n", " mapping_df = mapping_df[mapping_df['Gene'].apply(lambda x: len(x) > 0)]\n", " \n", " print(f\"Created new mapping dataframe with {len(mapping_df)} rows\")\n", " print(\"Sample of new mapping data:\")\n", " print(preview_df(mapping_df.head(3)))\n", " \n", " # Apply the mapping with the new approach\n", " gene_data = apply_gene_mapping(gene_data, mapping_df)\n", " \n", " # Check if we now have gene expression data\n", " if gene_data.empty:\n", " print(\"Still no gene expression data after improved mapping. Trying alternative method...\")\n", " \n", " # Last resort: extract genes from column annotations directly\n", " print(\"Creating a direct gene mapping from annotation...\")\n", " simple_mapping = []\n", " for idx, row in gene_annotation.iterrows():\n", " spot_id = row['SPOT_ID.1']\n", " if isinstance(spot_id, str):\n", " # Look for Homo sapiens gene name patterns\n", " genes = re.findall(r'Homo sapiens ([A-Z0-9-]{2,15})', spot_id)\n", " if genes:\n", " simple_mapping.append({'ID': row['ID'], 'Gene': genes})\n", " \n", " if simple_mapping:\n", " mapping_df = pd.DataFrame(simple_mapping)\n", " gene_data = apply_gene_mapping(gene_data, mapping_df)\n", " print(f\"Direct mapping created with {len(mapping_df)} probe-gene pairs\")\n", " \n", " # Verify if we now have gene data\n", " if gene_data.empty:\n", " print(\"WARNING: Gene mapping still unsuccessful. Cannot proceed with gene expression analysis.\")\n", " is_gene_available = False\n", " else:\n", " print(f\"Mapping successful! Generated gene expression data with {len(gene_data.index)} genes\")\n", " print(f\"First 10 gene symbols: {list(gene_data.index[:10])}\")\n", " is_gene_available = True\n", " \n", " # Normalize gene symbols to standard NCBI Gene symbols\n", " print(\"\\nNormalizing gene symbols using NCBI Gene database information...\")\n", " normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", " print(f\"After normalization: {len(normalized_gene_data.index)} genes\")\n", " \n", " # Create directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", " \n", " # Save normalized gene data\n", " normalized_gene_data.to_csv(out_gene_data_file)\n", " print(f\"Normalized gene data saved to {out_gene_data_file}\")\n", " print(f\"Normalized gene data shape: {normalized_gene_data.shape}\")\n", "except Exception as e:\n", " print(f\"Error fixing gene mapping: {e}\")\n", " is_gene_available = False\n", " normalized_gene_data = pd.DataFrame()\n", "\n", "# 3. Load clinical data from file and link with genetic data\n", "print(\"\\nLoading clinical data from file...\")\n", "try:\n", " # Load the previously saved clinical data\n", " clinical_df = pd.read_csv(out_clinical_data_file, index_col=0)\n", " \n", " # Determine available clinical features\n", " clinical_features = clinical_df.index.tolist()\n", " print(f\"Available clinical features: {clinical_features}\")\n", " \n", " # Set is_trait_available based on whether the clinical data contains the trait\n", " is_trait_available = trait in clinical_features\n", " \n", " # Transpose clinical data for linking (samples as rows)\n", " clinical_df_t = clinical_df.T\n", " \n", " # Only attempt linking if we have gene data\n", " if is_gene_available and not normalized_gene_data.empty:\n", " print(\"\\nLinking clinical and genetic data...\")\n", " \n", " # Link clinical and genetic data - transpose gene data so samples are rows\n", " linked_data = pd.merge(clinical_df_t, normalized_gene_data.T, \n", " left_index=True, right_index=True, how='inner')\n", " print(f\"Linked data shape: {linked_data.shape}\")\n", " \n", " # 4. Handle missing values systematically\n", " print(\"\\nHandling missing values...\")\n", " linked_data = handle_missing_values(linked_data, trait)\n", " print(f\"After handling missing values, data shape: {linked_data.shape}\")\n", " \n", " # 5. Determine whether features are biased\n", " print(\"\\nChecking for bias in features...\")\n", " is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", " else:\n", " print(\"Gene expression data not available. Cannot link with clinical data.\")\n", " linked_data = clinical_df_t.copy()\n", " is_biased = True # Set as biased since we don't have gene data\n", " \n", " # 6. Final validation and save metadata\n", " print(\"\\nPerforming final validation...\")\n", " is_usable = validate_and_save_cohort_info(\n", " is_final=True,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=is_gene_available,\n", " is_trait_available=is_trait_available,\n", " is_biased=is_biased,\n", " df=linked_data,\n", " note=\"Trisomy 21 (Down syndrome) patients as intellectual disability cases\"\n", " )\n", " \n", " # 7. Save the linked data if usable\n", " if is_usable:\n", " # Create directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " \n", " # Save linked data\n", " linked_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", " else:\n", " print(f\"Dataset not usable for {trait} association studies. Data not saved.\")\n", "\n", "except Exception as e:\n", " print(f\"Error in data linking or processing: {e}\")\n", " # Create a minimal dataframe for validation purposes\n", " linked_data = pd.DataFrame({trait: [0, 1]})\n", " \n", " # Perform final validation with appropriate flags\n", " is_usable = validate_and_save_cohort_info(\n", " is_final=True,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=is_gene_available, \n", " is_trait_available=is_trait_available,\n", " is_biased=True, # Not relevant since data isn't usable\n", " df=linked_data,\n", " note=\"Failed to link gene and clinical data: \" + str(e)\n", " )\n", " print(f\"Dataset usability: {is_usable}\")" ] } ], "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 }