{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0dd3dc6b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:19:27.856377Z", "iopub.status.busy": "2025-03-25T07:19:27.856137Z", "iopub.status.idle": "2025-03-25T07:19:28.020939Z", "shell.execute_reply": "2025-03-25T07:19:28.020495Z" } }, "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 = \"Kidney_Papillary_Cell_Carcinoma\"\n", "cohort = \"GSE40914\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Kidney_Papillary_Cell_Carcinoma\"\n", "in_cohort_dir = \"../../input/GEO/Kidney_Papillary_Cell_Carcinoma/GSE40914\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Kidney_Papillary_Cell_Carcinoma/GSE40914.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Kidney_Papillary_Cell_Carcinoma/gene_data/GSE40914.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Kidney_Papillary_Cell_Carcinoma/clinical_data/GSE40914.csv\"\n", "json_path = \"../../output/preprocess/Kidney_Papillary_Cell_Carcinoma/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "1a24d559", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "9c0b2efc", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:19:28.022436Z", "iopub.status.busy": "2025-03-25T07:19:28.022297Z", "iopub.status.idle": "2025-03-25T07:19:28.060321Z", "shell.execute_reply": "2025-03-25T07:19:28.059888Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Expression analysis and in silico characterization of intronic long noncoding RNAs in renal cell carcinoma: emerging functional associations\"\n", "!Series_summary\t\"Intronic and intergenic long noncoding RNAs (lncRNAs) are emerging gene expression regulators. The molecular pathogenesis of renal cell carcinoma (RCC) is still poorly understood, and in particular, limited studies are available for intronic lncRNAs expressed in RCC. Microarray experiments were performed with two different custom-designed arrays enriched with probes for lncRNAs mapping to intronic genomic regions. Samples from 18 primary clear cell RCC tumors and 11 nontumor adjacent matched tissues were analyzed with 4k-probes microarrays. Oligoarrays with 44k-probes were used to interrogate 17 RCC samples (14 clear cell, 2 papillary, 1 chromophobe subtypes) split into four pools. Meta-analyses were performed by taking the genomic coordinates of the RCC-expressed lncRNAs, and cross-referencing them with microarray expression data from three additional human tissues (normal liver, prostate tumor and kidney nontumor samples), and with large-scale public data for epigenetic regulatory marks and for evolutionarily conserved sequences. A signature of 29 intronic lncRNAs differentially expressed between RCC and nontumor samples was obtained (false discovery rate (FDR) <5%). An additional signature of 26 intronic lncRNAs significantly correlated with the RCC five-year patient survival outcome was identified (FDR <5%, p-value ≤0.01). We identified 4303 intronic antisense lncRNAs expressed in RCC, of which 25% were cis correlated (r >|0.6|) with the expression of the mRNA in the same locus across three human tissues. Gene Ontology (GO) analysis of those loci pointed to ‘regulation of biological processes’ as the main enriched category. A module map analysis of all expressed protein-coding genes in RCC that had a significant (r ≥|0.8|) trans correlation with the 20% most abundant lncRNAs identified 35 relevant (p <0.05) GO sets. In addition, we determined that 60% of these lncRNAs are evolutionarily conserved. At the genomic loci containing the intronic RCC-expressed lncRNAs, a strong association (p <0.001) was found between their transcription start sites and genomic marks such as CpG islands and histones methylation and acetylation. Intronic antisense lncRNAs are widely expressed in RCC tumors. Some of them are significantly altered in RCC in comparison with nontumor samples. The majority of these lncRNAs is evolutionarily conserved and possibly modulated by epigenetic modifications. Our data suggest that these RCC lncRNAs may contribute to the complex network of regulatory RNAs playing a role in renal cell malignant transformation.\"\n", "!Series_overall_design\t\"This SuperSeries is composed of the SubSeries listed below.\"\n", "!Series_overall_design\t\"Refer to individual Series.\"\n", "!Series_overall_design\t\"\"\n", "!Series_overall_design\t\"Data from the Fachel et al. Mol Cancer 2013 paper comes from two different microarray data sets. The ID_REF column of data table of each sample refers to the ID column of the 4K array (GPL3985) or of the 44K array (GPL4051). The gene name / gene ID are identified at the microarray platform description:\"\n", "!Series_overall_design\t\"44K array: http://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GPL4051 (used for expression set analysis)\"\n", "!Series_overall_design\t\"4K array: http://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GPL3985 (used for malignancy and survival set analysis)\"\n", "!Series_overall_design\t\"\"\n", "Sample Characteristics Dictionary:\n", "{0: ['patient identifier: 3', 'patient identifier: 5', 'patient identifier: 8', 'patient identifier: 9', 'patient identifier: 10', 'patient identifier: 11', 'patient identifier: 24', 'patient identifier: 26', 'patient identifier: 28', 'patient identifier: 30', 'patient identifier: 31', 'patient identifier: 1', 'patient identifier: 7', 'patient identifier: 13', 'patient identifier: 15', 'patient identifier: 29', 'patient identifier: 32', 'patient identifier: 33'], 1: ['disease: clear cell renal cell carcinoma (RCC)'], 2: ['tissue: adjacent nontumor kidney tissue', 'tissue: primary kidney tumor', 'tissue: kidney tumor'], 3: ['gender: female', 'gender: male'], 4: ['age at surgery (yrs): 78', 'age at surgery (yrs): 53', 'age at surgery (yrs): 71', 'age at surgery (yrs): 39', 'age at surgery (yrs): 34', 'age at surgery (yrs): 51', 'age at surgery (yrs): 75', 'age at surgery (yrs): 40', 'age at surgery (yrs): 50', 'age at surgery (yrs): 41', 'age at surgery (yrs): 66', 'age at surgery (yrs): 63', 'age at surgery (yrs): 35'], 5: ['patient status: cancer-specific death', 'patient status: dead from other causes', 'patient status: alive without cancer', 'patient status: alive with cancer', 'fuhrman grade: IV', 'fuhrman grade: III', 'fuhrman grade: II'], 6: [nan, 'tumor size (cm): 6', 'tumor size (cm): 8', 'tumor size (cm): 5', 'tumor size (cm): 6.5', 'tumor size (cm): 7', 'tumor size (cm): 15', 'tumor size (cm): 8.5', 'tumor size (cm): 18', 'tumor size (cm): 11', 'tumor size (cm): 10', 'tumor size (cm): 20', 'tumor size (cm): 13', 'tumor size (cm): 4'], 7: [nan, 'necrosis: yes', 'necrosis: no'], 8: [nan, 'capsule infiltration: yes', 'capsule infiltration: no'], 9: [nan, 'tnm classification (t): 3c', 'tnm classification (t): 2', 'tnm classification (t): 3a', 'tnm classification (t): 1b', 'tnm classification (t): 3b', 'tnm classification (t): 1', 'tnm classification (t): 3', 'tnm classification (t): 1a'], 10: [nan, 'tnm classification (n): no data available', 'tnm classification (n): 1', 'tnm classification (n): 0', 'tnm classification (n): 2'], 11: [nan, 'tnm classification (m): 1', 'tnm classification (m): no data available'], 12: [nan, 'organ metastasis at surgery: endocava, bones', 'organ metastasis at surgery: liver', 'organ metastasis at surgery: no data available', 'organ metastasis at surgery: lung, adjacent tissues', 'organ metastasis at surgery: lung', 'organ metastasis at surgery: peritoneum'], 13: [nan, 'organ metastasis after surgery: no data available', 'organ metastasis after surgery: liver, spleen', 'organ metastasis after surgery: brain, lung, bones', 'organ metastasis after surgery: bones'], 14: [nan, 'patient status: cancer-specific death', 'patient status: dead from other causes', 'patient status: alive without cancer', 'patient status: alive with cancer'], 15: [nan, 'follow-up (months): 0', 'follow-up (months): 21', 'follow-up (months): 6', 'follow-up (months): 66', 'follow-up (months): 60', 'follow-up (months): 8', 'follow-up (months): 16', 'follow-up (months): 62', 'follow-up (months): 54', 'follow-up (months): 56', 'follow-up (months): 17']}\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": "62171bba", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "53a8da51", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:19:28.061703Z", "iopub.status.busy": "2025-03-25T07:19:28.061596Z", "iopub.status.idle": "2025-03-25T07:19:28.070834Z", "shell.execute_reply": "2025-03-25T07:19:28.070382Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical Features Preview:\n", "{'GSM1004655': [nan], 'GSM1004656': [nan], 'GSM1004657': [nan], 'GSM1004658': [nan], 'GSM1004659': [nan], 'GSM1004660': [nan], 'GSM1004661': [nan], 'GSM1004662': [nan], 'GSM1004663': [nan], 'GSM1004664': [nan], 'GSM1004665': [nan], 'GSM1004666': [nan], 'GSM1004667': [nan], 'GSM1004668': [nan], 'GSM1004669': [nan], 'GSM1004670': [nan], 'GSM1004671': [nan], 'GSM1004672': [nan], 'GSM1004673': [nan], 'GSM1004674': [nan], 'GSM1004675': [nan], 'GSM1004676': [nan], 'GSM1004677': [nan], 'GSM1004678': [nan], 'GSM1004679': [nan], 'GSM1004680': [nan], 'GSM1004681': [nan], 'GSM1004682': [nan], 'GSM1004683': [nan], 'GSM1004684': [nan], 'GSM1004685': [nan], 'GSM1004686': [nan], 'GSM1004687': [nan], 'GSM1004688': [nan], 'GSM1004689': [nan], 'GSM1004690': [nan], 'GSM1004691': [nan], 'GSM1004692': [nan], 'GSM1004693': [nan], 'GSM1004694': [nan], 'GSM1004695': [nan], 'GSM1004696': [nan], 'GSM1004697': [nan], 'GSM1004698': [nan], 'GSM1004699': [nan], 'GSM1004700': [nan], 'GSM1004701': [nan], 'GSM1004702': [nan], 'GSM1004703': [nan], 'GSM1004704': [nan], 'GSM1004705': [nan], 'GSM1004706': [nan], 'GSM1004707': [nan], 'GSM1004708': [nan], 'GSM1004709': [nan], 'GSM1004710': [nan], 'GSM1004711': [nan], 'GSM1004712': [nan], 'GSM1004713': [nan], 'GSM1004714': [nan], 'GSM1004715': [nan], 'GSM1004716': [nan], 'GSM1004717': [nan], 'GSM1004718': [nan], 'GSM1004719': [nan], 'GSM1004720': [nan], 'GSM1004721': [nan], 'GSM1004722': [nan], 'GSM1004723': [nan], 'GSM1004724': [nan], 'GSM1004725': [nan], 'GSM1004726': [nan], 'GSM1004727': [nan], 'GSM1004728': [nan], 'GSM1004729': [nan], 'GSM1004730': [nan]}\n", "Clinical data saved to ../../output/preprocess/Kidney_Papillary_Cell_Carcinoma/clinical_data/GSE40914.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the background information, this dataset contains expression data for lncRNAs and possibly mRNAs\n", "# The Series_summary mentions \"microarray experiments\" and expression analysis, indicating gene expression data\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# From the sample characteristics dictionary, we can see:\n", "# - Key 0 contains disease information (renal cell carcinoma)\n", "# - Key 1 contains tissue information (kidney tumor)\n", "# - Key 2 contains sample type (pool)\n", "\n", "# For trait (Kidney_Papillary_Cell_Carcinoma):\n", "# The disease field indicates renal cell carcinoma, but we need to confirm if it's papillary type\n", "# From the background information, we see \"17 RCC samples (14 clear cell, 2 papillary, 1 chromophobe subtypes)\"\n", "# This confirms the dataset includes papillary RCC samples, but all samples in this cohort are labeled generically as RCC\n", "trait_row = 0 # Using the disease field to extract trait\n", "\n", "# For age:\n", "# There is no age information in the sample characteristics\n", "age_row = None\n", "\n", "# For gender:\n", "# There is no gender information in the sample characteristics\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value):\n", " \"\"\"\n", " Convert RCC value to binary indicator for Kidney Papillary Cell Carcinoma.\n", " From the background info, we know there are 2 papillary RCC samples out of 17 total RCC samples.\n", " Since the data is pooled and we can't distinguish which samples are papillary type,\n", " we'll consider all RCC samples as potentially containing papillary RCC.\n", " \"\"\"\n", " if not value:\n", " return None\n", " \n", " # Extract the value part after the colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Check if the value indicates RCC\n", " if 'renal cell carcinoma' in value.lower() or 'rcc' in value.lower():\n", " return 1 # Treat all RCC samples as potentially containing papillary type\n", " elif 'normal' in value.lower() or 'nontumor' in value.lower() or 'non-tumor' in value.lower():\n", " return 0 # Non-tumor samples\n", " else:\n", " return None # Unknown\n", "\n", "# Age and gender conversion functions are not needed as the data is not available\n", "\n", "# 3. Save Metadata\n", "# We have trait data but it's generically labeled as RCC, not specifically papillary\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", " # Extract clinical features using the function from the library\n", " clinical_features = 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=None,\n", " gender_row=gender_row,\n", " convert_gender=None\n", " )\n", " \n", " # Preview the extracted clinical features\n", " preview = preview_df(clinical_features)\n", " print(\"Clinical Features Preview:\")\n", " print(preview)\n", " \n", " # Save the clinical features to a CSV file\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " clinical_features.to_csv(out_clinical_data_file, index=False)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "98befa92", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "bc29b677", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:19:28.072273Z", "iopub.status.busy": "2025-03-25T07:19:28.072168Z", "iopub.status.idle": "2025-03-25T07:19:28.111427Z", "shell.execute_reply": "2025-03-25T07:19:28.110987Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Examining matrix file structure...\n", "Line 0: !Series_title\t\"Expression analysis and in silico characterization of intronic long noncoding RNAs in renal cell carcinoma: emerging functional associations\"\n", "Line 1: !Series_geo_accession\t\"GSE40914\"\n", "Line 2: !Series_status\t\"Public on Jan 21 2014\"\n", "Line 3: !Series_submission_date\t\"Sep 15 2012\"\n", "Line 4: !Series_last_update_date\t\"Apr 21 2015\"\n", "Line 5: !Series_pubmed_id\t\"24238219\"\n", "Line 6: !Series_summary\t\"Intronic and intergenic long noncoding RNAs (lncRNAs) are emerging gene expression regulators. The molecular pathogenesis of renal cell carcinoma (RCC) is still poorly understood, and in particular, limited studies are available for intronic lncRNAs expressed in RCC. Microarray experiments were performed with two different custom-designed arrays enriched with probes for lncRNAs mapping to intronic genomic regions. Samples from 18 primary clear cell RCC tumors and 11 nontumor adjacent matched tissues were analyzed with 4k-probes microarrays. Oligoarrays with 44k-probes were used to interrogate 17 RCC samples (14 clear cell, 2 papillary, 1 chromophobe subtypes) split into four pools. Meta-analyses were performed by taking the genomic coordinates of the RCC-expressed lncRNAs, and cross-referencing them with microarray expression data from three additional human tissues (normal liver, prostate tumor and kidney nontumor samples), and with large-scale public data for epigenetic regulatory marks and for evolutionarily conserved sequences. A signature of 29 intronic lncRNAs differentially expressed between RCC and nontumor samples was obtained (false discovery rate (FDR) <5%). An additional signature of 26 intronic lncRNAs significantly correlated with the RCC five-year patient survival outcome was identified (FDR <5%, p-value ≤0.01). We identified 4303 intronic antisense lncRNAs expressed in RCC, of which 25% were cis correlated (r >|0.6|) with the expression of the mRNA in the same locus across three human tissues. Gene Ontology (GO) analysis of those loci pointed to ‘regulation of biological processes’ as the main enriched category. A module map analysis of all expressed protein-coding genes in RCC that had a significant (r ≥|0.8|) trans correlation with the 20% most abundant lncRNAs identified 35 relevant (p <0.05) GO sets. In addition, we determined that 60% of these lncRNAs are evolutionarily conserved. At the genomic loci containing the intronic RCC-expressed lncRNAs, a strong association (p <0.001) was found between their transcription start sites and genomic marks such as CpG islands and histones methylation and acetylation. Intronic antisense lncRNAs are widely expressed in RCC tumors. Some of them are significantly altered in RCC in comparison with nontumor samples. The majority of these lncRNAs is evolutionarily conserved and possibly modulated by epigenetic modifications. Our data suggest that these RCC lncRNAs may contribute to the complex network of regulatory RNAs playing a role in renal cell malignant transformation.\"\n", "Line 7: !Series_overall_design\t\"This SuperSeries is composed of the SubSeries listed below.\"\n", "Line 8: !Series_overall_design\t\"Refer to individual Series.\"\n", "Line 9: !Series_overall_design\t\"\"\n", "Found table marker at line 82\n", "First few lines after marker:\n", "\"ID_REF\"\t\"GSM1004655\"\t\"GSM1004656\"\t\"GSM1004657\"\t\"GSM1004658\"\t\"GSM1004659\"\t\"GSM1004660\"\t\"GSM1004661\"\t\"GSM1004662\"\t\"GSM1004663\"\t\"GSM1004664\"\t\"GSM1004665\"\t\"GSM1004666\"\t\"GSM1004667\"\t\"GSM1004668\"\t\"GSM1004669\"\t\"GSM1004670\"\t\"GSM1004671\"\t\"GSM1004672\"\t\"GSM1004673\"\t\"GSM1004674\"\t\"GSM1004675\"\t\"GSM1004676\"\t\"GSM1004677\"\t\"GSM1004678\"\t\"GSM1004679\"\t\"GSM1004680\"\t\"GSM1004681\"\t\"GSM1004682\"\t\"GSM1004683\"\t\"GSM1004684\"\t\"GSM1004685\"\t\"GSM1004686\"\t\"GSM1004687\"\t\"GSM1004688\"\t\"GSM1004689\"\t\"GSM1004690\"\t\"GSM1004691\"\t\"GSM1004692\"\t\"GSM1004693\"\t\"GSM1004694\"\t\"GSM1004695\"\t\"GSM1004696\"\t\"GSM1004697\"\t\"GSM1004698\"\t\"GSM1004699\"\t\"GSM1004700\"\t\"GSM1004701\"\t\"GSM1004702\"\t\"GSM1004703\"\t\"GSM1004704\"\t\"GSM1004705\"\t\"GSM1004706\"\t\"GSM1004707\"\t\"GSM1004708\"\t\"GSM1004709\"\t\"GSM1004710\"\t\"GSM1004711\"\t\"GSM1004712\"\t\"GSM1004713\"\t\"GSM1004714\"\t\"GSM1004715\"\t\"GSM1004716\"\t\"GSM1004717\"\t\"GSM1004718\"\t\"GSM1004719\"\t\"GSM1004720\"\t\"GSM1004721\"\t\"GSM1004722\"\t\"GSM1004723\"\t\"GSM1004724\"\t\"GSM1004725\"\t\"GSM1004726\"\t\"GSM1004727\"\t\"GSM1004728\"\t\"GSM1004729\"\t\"GSM1004730\"\n", "1\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t2.654861111\t2.70125\t0.393083333\t0.117694444\t4.997333333\t4.200388889\t0.699236111\t0.621777778\t0.322138889\t0.469111111\t0.522083333\t0.447472222\t1.720416667\t1.431111111\t0.288055556\t0.688\t2.053555556\t3.216069444\t1.284222222\t0.862575333\t2.297305556\t4.343916667\t0.693666667\t0.594680556\t0.33025\t0.548416667\t0.163111111\t0.017166667\t1.837888889\t1.087125\t2.211583333\t3.268763889\n", "2\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t2.651722222\t2.543597222\t0.3985\t0.0492095\t6.803111111\t3.667\t0.604263889\t0.557055556\t0.565472222\t0.339083333\t0.511055556\t0.301305556\t1.949916667\t1.318361111\t0.076194444\t0.303555556\t2.284972222\t2.786777778\t1.451388889\t1.406611111\t2.549388889\t3.911222222\t0.227\t0.350166667\t0.109194444\t0.506263889\t0.06775\t0.001916667\t2.20325\t1.402069444\t2.627361111\t3.346138889\n", "3\t1.437272727\t1.674227273\t5.305340909\t4.193954545\t1.227295455\t3.416227273\t3.718272727\t4.329772727\t2.180568182\t1.933909091\t3.399431818\t2.745636364\t0.493170455\t2.895636364\t5.641727273\t5.072\t2.914045455\t2.665613636\t4.520590909\t3.763863636\t1.114784091\t1.269386364\t1.644761364\t2.243136364\t10.09538636\t6.444636364\t3.452818182\t2.988477273\t2.970159091\t2.901965909\t1.916840909\t2.302204545\t3.091909091\t5.328590909\t2.263227273\t1.810613636\t5.394136364\t5.185056818\t2.817340909\t2.631954545\t1.718863636\t0.911318182\t1.290840909\t1.1845\t3.462694444\t3.297166667\t1.192097222\t1.673138889\t8.329944444\t5.107444444\t0.924388889\t0.537930556\t2.286861111\t2.234305556\t1.988444444\t1.787194444\t2.302027778\t4.1805\t1.868041667\t2.432777778\t2.464819444\t2.307763889\t1.718583333\t1.373027778\t4.239388889\t4.062194444\t2.089861111\t1.921888889\t1.245277778\t1.913277778\t1.256444444\t0.678847222\t2.479541667\t1.932333333\t3.4675\t5.056472222\n", "4\t2.049590909\t2.043772727\t4.909659091\t4.76975\t1.303795455\t2.549125\t5.019545455\t5.817340909\t2.5055\t2.462613636\t3.957761364\t3.516977273\t3.828909091\t3.663988636\t5.990022727\t6.076318182\t3.246022727\t2.836340909\t3.916181818\t3.690272727\t1.114784091\t1.830977273\t1.885590909\t2.281795455\t9.922113636\t7.537636364\t3.981659091\t4.833022727\t4.379863636\t4.322\t1.916840909\t2.846193182\t4.258579545\t4.729909091\t2.949727273\t2.252272727\t8.628886364\t9.6825\t3.649704545\t3.624045455\t0.911318182\t1.885590909\t3.894159091\t2.964522727\t4.405833333\t4.165083333\t1.38225\t1.704069444\t8.170083333\t6.059083333\t1.999958333\t2.792333333\t3.415763889\t3.366\t2.380972222\t2.196486111\t3.242708333\t3.643305556\t3.593333333\t4.1205\t3.38775\t4.309666667\t2.247291667\t1.713416667\t7.067611111\t7.994361111\t2.728\t2.673805556\t2.591138889\t3.594555556\t0.634666667\t1.402583333\t5.10325\t4.640333333\t4.449944444\t5.205\n", "Total lines examined: 83\n", "\n", "Attempting to extract gene data from matrix file...\n", "Successfully extracted gene data with 3275 rows\n", "First 20 gene IDs:\n", "Index(['1', '2', '3', '4', '5', '6', '7', '9', '10', '11', '13', '14', '15',\n", " '16', '17', '18', '19', '20', '21', '22'],\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", "# Add diagnostic code to check file content and structure\n", "print(\"Examining matrix file structure...\")\n", "with gzip.open(matrix_file, 'rt') as file:\n", " table_marker_found = False\n", " lines_read = 0\n", " for i, line in enumerate(file):\n", " lines_read += 1\n", " if '!series_matrix_table_begin' in line:\n", " table_marker_found = True\n", " print(f\"Found table marker at line {i}\")\n", " # Read a few lines after the marker to check data structure\n", " next_lines = [next(file, \"\").strip() for _ in range(5)]\n", " print(\"First few lines after marker:\")\n", " for next_line in next_lines:\n", " print(next_line)\n", " break\n", " if i < 10: # Print first few lines to see file structure\n", " print(f\"Line {i}: {line.strip()}\")\n", " if i > 100: # Don't read the entire file\n", " break\n", " \n", " if not table_marker_found:\n", " print(\"Table marker '!series_matrix_table_begin' not found in first 100 lines\")\n", " print(f\"Total lines examined: {lines_read}\")\n", "\n", "# 2. Try extracting gene expression data from the matrix file again with better diagnostics\n", "try:\n", " print(\"\\nAttempting to extract 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: {str(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", "\n", "# If data extraction failed, try an alternative approach using pandas directly\n", "if not is_gene_available:\n", " print(\"\\nTrying alternative approach to read gene expression data...\")\n", " try:\n", " with gzip.open(matrix_file, 'rt') as file:\n", " # Skip lines until we find the marker\n", " for line in file:\n", " if '!series_matrix_table_begin' in line:\n", " break\n", " \n", " # Try to read the data directly with pandas\n", " gene_data = pd.read_csv(file, sep='\\t', index_col=0)\n", " \n", " if not gene_data.empty:\n", " print(f\"Successfully extracted gene data with alternative method: {gene_data.shape}\")\n", " print(\"First 20 gene IDs:\")\n", " print(gene_data.index[:20])\n", " is_gene_available = True\n", " else:\n", " print(\"Alternative extraction method also produced empty data\")\n", " except Exception as e:\n", " print(f\"Alternative extraction failed: {str(e)}\")\n" ] }, { "cell_type": "markdown", "id": "ccee389f", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "c04103d8", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:19:28.112735Z", "iopub.status.busy": "2025-03-25T07:19:28.112628Z", "iopub.status.idle": "2025-03-25T07:19:28.114891Z", "shell.execute_reply": "2025-03-25T07:19:28.114455Z" } }, "outputs": [], "source": [ "# Examine the first 20 gene identifiers shown above\n", "# These appear to be just numeric values (1, 2, 3, etc.) rather than standard gene symbols\n", "# This indicates they are likely probe IDs that need to be mapped to gene symbols\n", "\n", "# Based on biomedical knowledge, human gene symbols follow specific naming conventions\n", "# They are typically a combination of letters and sometimes numbers (e.g., TP53, BRCA1, ESR1)\n", "# The identifiers shown are purely numeric, suggesting they are probe IDs or some other identifier\n", "\n", "# Since we need proper gene symbols for biological interpretation\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "092a2582", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "bf57231e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:19:28.116210Z", "iopub.status.busy": "2025-03-25T07:19:28.116104Z", "iopub.status.idle": "2025-03-25T07:19:28.643914Z", "shell.execute_reply": "2025-03-25T07:19:28.643274Z" } }, "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 463119 rows\n", "\n", "Gene annotation preview (first few rows):\n", "{'ID': ['910', '4260', '1981', '2381', '4288'], 'GB_ACC': ['BE833259', 'BE702227', 'BF364095', 'BE081005', 'AW880607'], 'SPOT_TYPE': ['Exonic', 'Exonic', 'Exonic', 'Exonic', 'Exonic'], 'GENE_ID': ['85439', '2776', '84131', '2776', '54768'], 'GENE_SYMBOL': ['STON2', 'GNAQ', 'CEP78', 'GNAQ', 'HYDIN'], 'GENE_ANNOTATION': ['stonin 2', 'Guanine nucleotide binding protein (G protein), q polypeptide', 'centrosomal protein 78kDa', 'Guanine nucleotide binding protein (G protein), q polypeptide', 'hydrocephalus inducing homolog 2 (mouse); hydrocephalus inducing homolog (mouse)'], 'CPC_CODING_POTENTIAL': ['noncoding', 'noncoding', 'noncoding', 'noncoding', '-'], 'SEQUENCE': ['CTGATCCGCTTAAGCTTAGTATGTTTGAGTGTGTAATTTTAGTTTCTTTTCTGGTTGTATTTGTGGTAGTCAGATGTGTTGGATTGATTCCAACTGGACAGAGTAAGGAATTCCAGCATCCTCTTCCTGCTTGCTCGTGTTACCCCACAGATCAAACCCTCAATTCTAGTTGGGGATGCTGTCTAGCCCCACACCATGACTGAAGCCTTAAGCACTGTTGCGCCTCCATGTGCTTTGGATCAGCAACCCCAGTGGTATTCTACCAGAGCATTGTGGGAAAGCAGATGTATAGTCAGGTCCCAACAGCAAATTGTTGGGTGTGAGAGTTCTAAAGTATAGGGGTGAGGGAAGAGAAGGATATGAACTCCT', 'CTCTTCCGAAAGATATATCTTGGTTAGAAACACAAAAAAATAAAACTAGTAATATTGTATGTTTATCTATCTCTACATATTTCCAGCATATGTAGCGTTAATAGATCTGTCCTGGTAACTGTGTCTTTGGGATTTCATTTTGGTTCCATCAAATTAGGAAAAGAAATGGCTTAGTTGTATATGATTAGCTAGAGATTTTTGGAGCCAGACACCTGCTGTTTAGTAGATAACTTAGTACAGACCCTAAACTTGTCATTTGTTTTTCTCACAGAATAGCCATTTCCTGCTGTCTTCCCAATGATCACTGCCCTTTCAATAACACTCTTGCCTCTAGAATCATATG', 'CCTTTGAAATGACTGGAGAATATTAAAATAAGAAATAATCATGCAGAGTTGGAAACCAGAAATCTGAACAGTGAAATTGTCTGGCAGGATAAGACGCAGATGCATTTAAGTACCAGTTCAATTAAAGGATGGAACAGCTAAGCCATTCCACTCATCTTCGTGAGCATCTGATTCTGGAGTTTGCGCACCGAGGCTAAGAAAGCAGCTATCTGAAGTGGGAGCGCTGACCCAAGAAATGCTGGGATCGGAGAATAAGGGAATTATCCAAAATGGCTCCGAAGAGGAACTGAAGTTAAGCTGCCCACATGATCTCTCTAACTATGATGACCTGCCACTTCCGTTTATAATCACCACATAAGTGCCTGTAATCATTTGTGTTCATTAAAAGTGAACCAGAATTCCCATTTGGATGAAAAAATAACACTTCCAACTTTAATCTTAGGCCCTCATTTATAAATATGGACAACCAAGAATCATCAAATTTGAAGAAAACCAGTAACATAAAAGGAGGCATGAAATTAAAATTAACCTGTTCAAGAAGATAGTTACTAGGAGAAACATGAAATTTTTAAATTAATGAATCAAAATCTTCAGCAATTCATAAAGATACTGTGTTCATAAAGAATAGGATGCCATGACAAAAATATTTCGAGTTTCCTGGAATTAAACATTTGA', 'CCGTAGCACTTCCTGTACTATACAAGAACAAGAACATAAAACACAGAAACCTTTCTTCAGCATACCAAGGCAAGCAGCCATTTCATGACTCACTTAACACATTGCAGTGTACCAGTTTACAGATGATTTTTCCCTTTTTGCGTGACATGGCAGACCCTGCCGCCAGAGAATTCCTTATTTGTAAATTGGAAGTTTCTACTATGCCTTACAGAGCTTAAATTCAGAAGTTTGTGCCTCATATCTGAAACAAAGGGAAATAACACACCCATTCAAAAGTAAATAAATCTCCTATAAGTTTTTGTTTTTAACATTTCCATATAAAGAGCTCTGTTGAATGTCATGAATAGACTGGAACATAACATTTTAAGAACCTGCATATGTTGTTTACTAGCAGATGACAACTACAAAAGGAATCTGAAGAACACGTAAAACTTGTATTTTTTGGCTTGCACGGGGATATCAACTACCAGGCCTTTTTCAAAAAGGTATTTCAGCTAAGGGGCCAATACACTTTTTGGTACTCCAGACTATCTTGATTGTCAAGGTGTCCGACCTGTATTTTTAAATTTTATACTGCCACATGATTGTA', 'GAGGGATTGGCCCCTGTGGGTCAAATCTCACTTCAAATATTTCCGTTTCACAATGAGGCAGATTCTTTACACGATCTAGCTCAGTACTGAATCCTGTCTCATGAAGGACACGCTTGTCTGCATGGAATGACACTGGAAAGTGACTGGTGTTGATGATCTTGATGATGTGGGTTCGGACTTCGCCAAGGATGATGTAGCCAAAGTCCAGGATGTACTCTGGTAGCTGGATTTTGGCCAGTTTGCGGCGACTCCGATGGCTGAAGCAGGGGTCATCCATAGGATCAGGGGTGGTTGTATTCTGATGTTCTAGGACATAGCTTTGGACTATAAGTCTTTCTACCTCCATCTGGAGATGAGCACTTACCTCAGCAGGCTCGTCTTCTGGCACTTCCTCAGTTATTACGTCAAAGTGATCGAGCATTTCACATTTGTTATACTCTTTGTCTGTGTTTTTCCTGGCTTGATTCAAGAACATTTCATACTTTTCATTTGCTGTGAGGTTCCTGGGGAGATCGAGGCAGATTTGG'], 'COORDINATES': ['chr14:81727450-81727801', 'chr9:80331795-80332137', 'chr9:80885760-80886427', 'chr9:80332512-80333071', 'chr1_gl000192_random:211847-219091'], 'CLONE ID': ['QV3-OT0065-150600-231-c01', 'QV0-NN1022-070700-294-f10', 'QV0-NN1022-120500-220-f07', 'QV1-BT0631-210300-120-f05', 'QV0-OT0031-100300-157-h12'], 'SPOT_ID': ['Exonic', 'Exonic', 'Exonic', 'Exonic', 'Exonic']}\n", "\n", "Column names in gene annotation data:\n", "['ID', 'GB_ACC', 'SPOT_TYPE', 'GENE_ID', 'GENE_SYMBOL', 'GENE_ANNOTATION', 'CPC_CODING_POTENTIAL', 'SEQUENCE', 'COORDINATES', 'CLONE ID', 'SPOT_ID']\n", "\n", "The dataset contains GenBank accessions (GB_ACC) that could be used for gene mapping.\n", "Number of rows with GenBank accessions: 461866 out of 463119\n", "\n", "The dataset contains genomic regions (SPOT_ID) that could be used for location-based gene mapping.\n", "Example SPOT_ID format: Exonic\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": "e2545037", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "5ddf3a47", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:19:28.645889Z", "iopub.status.busy": "2025-03-25T07:19:28.645734Z", "iopub.status.idle": "2025-03-25T07:19:28.815877Z", "shell.execute_reply": "2025-03-25T07:19:28.815267Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Creating gene mapping from probe IDs to gene symbols...\n", "Created gene mapping with 45204 entries\n", "Gene mapping preview:\n", "{'ID': ['910', '4260', '1981', '2381', '4288'], 'Gene': ['STON2', 'GNAQ', 'CEP78', 'GNAQ', 'HYDIN']}\n", "\n", "Ensuring gene expression data has numeric values...\n", "Gene data shape before mapping: (3275, 76)\n", "Data types in gene_data after conversion:\n", "GSM1004655 float64\n", "GSM1004656 float64\n", "GSM1004657 float64\n", "GSM1004658 float64\n", "GSM1004659 float64\n", "dtype: object\n", "\n", "Converting probe-level measurements to gene expression data...\n", "Created gene expression data with 2645 genes\n", "Gene expression data preview (first few rows):\n", "{'GSM1004655': [11.611, 10.926954546000001, 5.866136364, 0.0, 2.304568182], 'GSM1004656': [12.39861364, 15.123068182, 6.7795, 0.0, 2.531636364], 'GSM1004657': [8.044136364, 9.524568182, 7.152886364, 0.0, 4.169409091], 'GSM1004658': [8.556590909, 9.290693181, 7.174977273, 0.0, 3.884727273], 'GSM1004659': [7.704806818, 16.374318182, 5.923522727, 0.0, 0.992409091], 'GSM1004660': [6.522909091, 9.687295455000001, 7.814090909, 0.0, 2.001590909], 'GSM1004661': [9.999795455, 10.968193182, 5.647227273, 0.0, 2.02575], 'GSM1004662': [9.7725, 12.251704546, 5.936204545, 0.0, 4.709954545], 'GSM1004663': [7.132090909, 9.254681818, 5.132613636, 0.0, 2.187363636], 'GSM1004664': [5.202318182, 9.108636364, 8.291772727, 0.0, 2.650977273], 'GSM1004665': [7.729409091, 11.017318182, 6.432477273, 0.0, 2.658306818], 'GSM1004666': [6.749909091, 9.934795455, 5.235159091, 0.0, 4.222090909], 'GSM1004667': [9.229159091, 8.232295454, 8.291772727, 0.0, 2.534909091], 'GSM1004668': [9.699704545, 10.358159091000001, 6.468090909, 0.0, 3.8385], 'GSM1004669': [7.898068182, 10.755727273, 2.946704545, 0.0, 3.163329545], 'GSM1004670': [7.305909091, 15.909022732, 4.333090909, 0.0, 3.442909091], 'GSM1004671': [11.17552273, 12.279795454, 7.206340909, 0.0, 2.149045455], 'GSM1004672': [11.36931818, 10.411022727, 5.377204545, 0.0, 2.704318182], 'GSM1004673': [6.710772727, 12.407284091000001, 7.865613636, 0.0, 5.0345], 'GSM1004674': [7.999045455, 14.665750000000001, 5.283454545, 0.0, 3.0705], 'GSM1004675': [6.756704545, 10.103159091, 5.647227273, 0.0, 1.114784091], 'GSM1004676': [6.301863636, 10.231477273, 4.661909091, 0.0, 2.160954545], 'GSM1004677': [11.611, 11.460386364, 6.440431818, 0.0, 0.912181818], 'GSM1004678': [10.85413636, 12.134636363999999, 5.702545455, 0.0, 2.234193182], 'GSM1004679': [6.810522727, 9.054931818, 3.482795455, 0.0, 6.671613636], 'GSM1004680': [7.465022727, 10.094306818, 3.427090909, 0.0, 5.208977273], 'GSM1004681': [11.93831818, 12.231340909, 6.920977273, 0.0, 1.309045455], 'GSM1004682': [10.73384091, 11.851431818, 6.515409091, 0.0, 3.503613636], 'GSM1004683': [12.11627273, 15.047090909, 7.524295455, 0.0, 5.547136364], 'GSM1004684': [12.37495455, 16.76318182, 6.880375, 0.0, 6.660886364], 'GSM1004685': [9.169454545, 7.645613637, 6.037113636, 0.0, 1.916840909], 'GSM1004686': [5.752090909, 8.816909091, 6.260897727, 0.0, 3.121272727], 'GSM1004687': [7.957818182, 8.856874999999999, 7.904306818, 0.0, 2.993954545], 'GSM1004688': [6.022886364, 8.296022727, 8.421681818, 0.0, 3.789227273], 'GSM1004689': [8.696045455, 11.166045454999999, 6.651886364, 0.0, 4.423670455], 'GSM1004690': [7.371818182, 9.934681818000001, 6.834840909, 0.0, 4.664215909], 'GSM1004691': [8.591147727, 10.042613636, 5.958522727, 0.0, 5.718386364], 'GSM1004692': [8.100954545, 10.340318181999999, 6.061704545, 0.0, 6.238136364], 'GSM1004693': [11.28059091, 9.906738637, 6.756704545, 0.0, 4.003159091], 'GSM1004694': [11.14823864, 11.463659091, 6.643340909, 0.0, 5.027863636], 'GSM1004695': [11.10872727, 11.734454545, 8.088909091, 0.0, 2.009568182], 'GSM1004696': [9.756568182, 11.018136364, 6.793795455, 0.0, 0.258431818], 'GSM1004697': [8.489977273, 12.513181818, 6.476136364, 0.0, 2.962681818], 'GSM1004698': [5.909795455, 9.900090909, 7.770227273, 0.0, 2.970159091], 'GSM1004699': [8.048916667, 7.8384166660000005, 4.13925, 0.445597222, 3.026111111], 'GSM1004700': [7.270555556, 8.616319445, 5.026222222, 0.457694444, 2.754555556], 'GSM1004701': [9.747916667, 9.063583333, 5.118805556, 0.681722222, 0.695805556], 'GSM1004702': [9.073722222, 9.665055555, 4.499833333, 1.123555556, 1.666055556], 'GSM1004703': [5.40375, 6.883083333, 2.538805556, 0.479208333, 5.265708333], 'GSM1004704': [5.9935, 7.882166666, 2.531333333, 0.635638889, 4.059638889], 'GSM1004705': [6.827361111, 9.319944443999999, 4.739527778, 1.199680556, 1.098861111], 'GSM1004706': [6.717972222, 4.869722223, 4.233333333, 0.609527778, 2.029305556], 'GSM1004707': [10.40791667, 12.480888889000001, 6.207291667, 0.76825, 4.454], 'GSM1004708': [10.66872222, 14.05275, 5.627277778, 0.885666667, 5.417805556], 'GSM1004709': [7.718138889, 5.917166667, 4.890527778, 0.635638889, 1.468861111], 'GSM1004710': [4.645166667, 6.945222223, 5.095152778, 1.225222222, 2.427458333], 'GSM1004711': [6.437388889, 6.888305556000001, 6.389361111, 0.358666667, 2.225], 'GSM1004712': [4.766805556, 6.4084722229999995, 6.869777778, 0.634666667, 2.841694444], 'GSM1004713': [8.008805556, 8.894222222, 4.088, 0.470083333, 2.183055556], 'GSM1004714': [8.802222222, 9.823861112, 6.584083333, 0.743861111, 2.898361111], 'GSM1004715': [6.289861111, 6.582819445, 3.911222222, 0.997027778, 4.355291667], 'GSM1004716': [7.420722222, 7.140583333, 4.698833333, 0.49725, 3.785791667], 'GSM1004717': [7.122472222, 8.810777777, 5.310694444, 0.780847222, 3.402361111], 'GSM1004718': [5.952194444, 7.742805556, 5.481638889, 0.560861111, 3.608708333], 'GSM1004719': [7.026777778, 7.814833333999999, 4.723666667, 0.904527778, 4.519083333], 'GSM1004720': [6.584083333, 8.076444444, 4.806833333, 0.793208333, 4.96675], 'GSM1004721': [9.444111111, 7.757902778, 5.382277778, 0.179333333, 3.031694444], 'GSM1004722': [9.304041667, 9.007722222, 5.289861111, 1.005708333, 3.892055556], 'GSM1004723': [10.74886111, 10.246166667, 4.925694444, 0.4325, 1.651166667], 'GSM1004724': [10.87780556, 9.368916667, 2.898361111, 0.613194444, 1.93475], 'GSM1004725': [9.235111111, 9.236138889, 6.563, 0.471888889, 1.451388889], 'GSM1004726': [8.082805556, 8.676194445, 5.4305, 0.504805556, 0.2785], 'GSM1004727': [8.753833333, 7.394222222, 7.752208333, 0.348805556, 2.163319444], 'GSM1004728': [8.605055556, 8.176652778000001, 7.091666667, 0.512569444, 2.979527778], 'GSM1004729': [4.573166667, 8.550736110999999, 4.043805556, 0.363, 4.622], 'GSM1004730': [4.276361111, 8.188333333, 4.091722222, 0.145527778, 3.409666667]}\n", "Gene expression data saved to ../../output/preprocess/Kidney_Papillary_Cell_Carcinoma/gene_data/GSE40914.csv\n" ] } ], "source": [ "# 1. Identify which columns in gene_annotation correspond to probe IDs and gene symbols\n", "# From the gene annotation preview, we can see:\n", "# - 'ID' column contains numeric identifiers (matching gene expression data's index)\n", "# - 'GENE_SYMBOL' column contains gene symbols (e.g., STON2, GNAQ)\n", "\n", "print(\"Creating gene mapping from probe IDs to gene symbols...\")\n", "\n", "# 2. Create the gene mapping dataframe\n", "gene_mapping = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='GENE_SYMBOL')\n", "print(f\"Created gene mapping with {len(gene_mapping)} entries\")\n", "print(\"Gene mapping preview:\")\n", "print(preview_df(gene_mapping))\n", "\n", "# 3. Ensure gene expression data has numeric values before applying mapping\n", "print(\"\\nEnsuring gene expression data has numeric values...\")\n", "# Convert all columns (except ID if it exists) to numeric types\n", "for col in gene_data.columns:\n", " gene_data[col] = pd.to_numeric(gene_data[col], errors='coerce')\n", "\n", "print(f\"Gene data shape before mapping: {gene_data.shape}\")\n", "print(\"Data types in gene_data after conversion:\")\n", "print(gene_data.dtypes.head())\n", "\n", "# 4. Apply the gene mapping to convert probe-level data to gene expression data\n", "print(\"\\nConverting probe-level measurements to gene expression data...\")\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "print(f\"Created gene expression data with {len(gene_data.index)} genes\")\n", "print(\"Gene expression data preview (first few rows):\")\n", "print(preview_df(gene_data))\n", "\n", "# Save the gene expression data to the output file\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\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": "3617d140", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "cd4f9fa4", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:19:28.817791Z", "iopub.status.busy": "2025-03-25T07:19:28.817674Z", "iopub.status.idle": "2025-03-25T07:19:29.019893Z", "shell.execute_reply": "2025-03-25T07:19:29.019252Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Normalizing gene symbols...\n", "Loaded gene data with 2645 genes and 76 samples\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "After normalization: 2629 genes\n", "Normalized gene data saved to ../../output/preprocess/Kidney_Papillary_Cell_Carcinoma/gene_data/GSE40914.csv\n", "\n", "Linking clinical and genetic data...\n", "Loaded clinical data with shape: (1, 76)\n", "Error linking clinical and genetic data: \"None of ['Kidney_Papillary_Cell_Carcinoma'] are in the columns\"\n", "\n", "Skipping missing value handling and bias evaluation as linked data is not available\n", "\n", "Performing final validation...\n", "Abnormality detected in the cohort: GSE40914. Preprocessing failed.\n", "\n", "Dataset usability for Kidney_Papillary_Cell_Carcinoma association studies: False\n", "Reason: Dataset does not contain required trait information\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "print(\"\\nNormalizing gene symbols...\")\n", "try:\n", " # Load gene data that was saved in step 6\n", " gene_data = pd.read_csv(out_gene_data_file, index_col=0)\n", " print(f\"Loaded gene data with {gene_data.shape[0]} genes and {gene_data.shape[1]} samples\")\n", " \n", " # Normalize gene symbols using NCBI Gene database\n", " gene_data = normalize_gene_symbols_in_index(gene_data)\n", " print(f\"After normalization: {gene_data.shape[0]} genes\")\n", " \n", " # Save the normalized gene data\n", " os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", " gene_data.to_csv(out_gene_data_file)\n", " print(f\"Normalized gene data saved to {out_gene_data_file}\")\n", " \n", " is_gene_available = True\n", "except Exception as e:\n", " print(f\"Error normalizing gene data: {e}\")\n", " is_gene_available = False\n", "\n", "# 2. Link clinical and genetic data\n", "print(\"\\nLinking clinical and genetic data...\")\n", "try:\n", " # Load clinical data from step 2\n", " clinical_data = pd.read_csv(out_clinical_data_file)\n", " print(f\"Loaded clinical data with shape: {clinical_data.shape}\")\n", " \n", " # Convert clinical data to proper format for linking (need to transpose for geo_link_clinical_genetic_data)\n", " clinical_transpose = clinical_data.set_index(trait).T\n", " \n", " # Link clinical and genetic data\n", " linked_data = geo_link_clinical_genetic_data(clinical_transpose, gene_data)\n", " print(f\"Created linked data with {linked_data.shape[0]} samples and {linked_data.shape[1]} features\")\n", " is_trait_available = True\n", "except Exception as e:\n", " print(f\"Error linking clinical and genetic data: {e}\")\n", " is_trait_available = False\n", " linked_data = pd.DataFrame()\n", "\n", "# 3. Handle missing values in the linked data\n", "if is_trait_available and not linked_data.empty:\n", " print(\"\\nHandling missing values...\")\n", " try:\n", " linked_data = handle_missing_values(linked_data, trait)\n", " print(f\"After handling missing values: {linked_data.shape[0]} samples and {linked_data.shape[1]} features\")\n", " except Exception as e:\n", " print(f\"Error handling missing values: {e}\")\n", " \n", " # 4. Determine whether the trait and demographic features are biased\n", " print(\"\\nEvaluating feature bias...\")\n", " try:\n", " is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", " print(f\"Trait bias determination: {is_biased}\")\n", " print(f\"Final linked data shape: {linked_data.shape[0]} samples and {linked_data.shape[1]} features\")\n", " except Exception as e:\n", " print(f\"Error evaluating feature bias: {e}\")\n", " is_biased = True\n", "else:\n", " print(\"\\nSkipping missing value handling and bias evaluation as linked data is not available\")\n", " is_biased = True\n", "\n", "# 5. Validate and save cohort information\n", "print(\"\\nPerforming final validation...\")\n", "note = \"\"\n", "if not is_trait_available:\n", " note = \"Dataset does not contain required trait information\"\n", "elif is_biased:\n", " note = \"Dataset has severe bias in the trait distribution\"\n", "\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=note\n", ")\n", "\n", "# 6. Save the linked data if usable\n", "print(f\"\\nDataset usability for {trait} association studies: {is_usable}\")\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\"Final linked data saved to {out_data_file}\")\n", "else:\n", " if note:\n", " print(f\"Reason: {note}\")\n", " else:\n", " print(\"Dataset does not meet quality criteria for the specified trait\")" ] } ], "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 }