{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "5d2dfeea", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:21:27.291310Z", "iopub.status.busy": "2025-03-25T08:21:27.291087Z", "iopub.status.idle": "2025-03-25T08:21:27.452798Z", "shell.execute_reply": "2025-03-25T08:21:27.452483Z" } }, "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 = \"Chronic_obstructive_pulmonary_disease_(COPD)\"\n", "cohort = \"GSE32030\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Chronic_obstructive_pulmonary_disease_(COPD)\"\n", "in_cohort_dir = \"../../input/GEO/Chronic_obstructive_pulmonary_disease_(COPD)/GSE32030\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Chronic_obstructive_pulmonary_disease_(COPD)/GSE32030.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Chronic_obstructive_pulmonary_disease_(COPD)/gene_data/GSE32030.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Chronic_obstructive_pulmonary_disease_(COPD)/clinical_data/GSE32030.csv\"\n", "json_path = \"../../output/preprocess/Chronic_obstructive_pulmonary_disease_(COPD)/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "937ad1fa", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "fddbb499", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:21:27.454220Z", "iopub.status.busy": "2025-03-25T08:21:27.454080Z", "iopub.status.idle": "2025-03-25T08:21:27.844263Z", "shell.execute_reply": "2025-03-25T08:21:27.843932Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Empirical Bayes Conditional Independence Graphs for Dense Regulatory Network Recovery\"\n", "!Series_summary\t\"Motivation: Computational inference methods that make use of graphical models to extract regulatory networks from gene expression data can have difficulty reconstructing dense regions of a network, a consequence of both computational complexity and unreliable parameter estimation when sample size is small. As a result, identification of hub genes is of special difficulty for these methods.Methods: We present a new algorithm, Empirical Light Mutual Min (ELMM), for large network reconstruction that has properties well suited for dense graph recovery. ELMM reconstructs the undirected graph of a regulatory network using empirical Bayes conditional independence testing with a heuristic relaxation of independence constraints in dense areas of the graph. This relaxation allows only one gene of a pair with a putative relation to be aware of the network connection, an approach that is aimed at easing multiple testing problems associated with recovering densely connected structures.Results: Using in silico data, we show that ELMM has better performance than commonly used network inference algorithms including PC Algorithm, GeneNet, and ARACNE. We also apply ELMM to reconstruct a network among 5,400 genes expressed in human lung airway epithelium of healthy nonsmokers, healthy smokers, and smokers with pulmonary diseases assayed using microarrays. The analysis identifies dense subnetworks that are consistent with known regulatory relationships in the lung airway and also suggests novel hub regulatory relationships among a number of genes that play roles in oxidative stress, wound response, and secretion.\"\n", "!Series_overall_design\t\"We present a new approach to extracting regulatory networks from gene expression data. The algorithm is applied to reconstruct the gene regulatory network in human lung airway epithelium using microarray data extracted from healthy nonsmokers and smokers and smokers with pulmonary diseases.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['department of genetic medicine id: DGM-00028', 'ethnicity: Afr', 'department of genetic medicine id: DGM-00060', 'department of genetic medicine id: DGM-00061', 'department of genetic medicine id: DGM-00104', 'department of genetic medicine id: DGM-10131', 'department of genetic medicine id: DGM-00167', 'department of genetic medicine id: DGM-00171', 'department of genetic medicine id: DGM-00262', 'department of genetic medicine id: DGM-00313', 'department of genetic medicine id: DGM-10287', 'department of genetic medicine id: DGM-00382', 'department of genetic medicine id: DGM-10139', 'department of genetic medicine id: DGM-00471', 'department of genetic medicine id: DGM-00512', 'department of genetic medicine id: DGM-02088', 'department of genetic medicine id: DGM-10152', 'department of genetic medicine id: DGM-00632', 'department of genetic medicine id: DGM-00683', 'department of genetic medicine id: DGM-10404', 'department of genetic medicine id: DGM-00791', 'department of genetic medicine id: DGM-10132', 'department of genetic medicine id: DGM-10135', 'department of genetic medicine id: DGM-01275', 'department of genetic medicine id: DGM-10144', 'department of genetic medicine id: DGM-10130', 'department of genetic medicine id: DGM-10406', 'department of genetic medicine id: DGM-01607', 'department of genetic medicine id: DGM-01737', 'department of genetic medicine id: DGM-01749'], 1: ['smoking status: S', 'department of genetic medicine id: DGM-00038', 'department of genetic medicine id: DGM-00029', 'department of genetic medicine id: DGM-00030', 'department of genetic medicine id: DGM-00035', 'department of genetic medicine id: DGM-00036', 'department of genetic medicine id: DGM-00041', 'department of genetic medicine id: DGM-00044', 'department of genetic medicine id: DGM-00052', 'department of genetic medicine id: DGM-00073', 'department of genetic medicine id: DGM-00069', 'department of genetic medicine id: DGM-00072', 'department of genetic medicine id: DGM-00078', 'smoking status: NS', 'department of genetic medicine id: DGM-00123', 'department of genetic medicine id: DGM-00244', 'department of genetic medicine id: DGM-00249', 'department of genetic medicine id: DGM-00416', 'department of genetic medicine id: DGM-00465', 'department of genetic medicine id: DGM-00530', 'department of genetic medicine id: DGM-00548', 'department of genetic medicine id: DGM-00551', 'department of genetic medicine id: DGM-00553', 'department of genetic medicine id: DGM-00625', 'department of genetic medicine id: DGM-00661', 'department of genetic medicine id: DGM-00685', 'department of genetic medicine id: DGM-00696', 'department of genetic medicine id: DGM-00757', 'department of genetic medicine id: DGM-00751', 'department of genetic medicine id: DGM-00778'], 2: ['copd status: yes', 'smoking status: S', 'smoking status: NS', nan, 'serum 25-oh-d: low vitamin D', 'serum 25-oh-d: mid vitamin D', 'serum 25-oh-d: high vitamin D', 'cell type: mixture of epithelium, basal, ciliated, secretory, undifferentiated and inflammatory cells'], 3: [nan, 'copd status: yes', 'serum 25-oh-d: mid vitamin D', 'serum 25-oh-d: high vitamin D', 'serum 25-oh-d: low vitamin D', 'time: n/a']}\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": "75abe0a5", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "fa407627", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:21:27.845595Z", "iopub.status.busy": "2025-03-25T08:21:27.845488Z", "iopub.status.idle": "2025-03-25T08:21:27.854118Z", "shell.execute_reply": "2025-03-25T08:21:27.853859Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of selected clinical features:\n", "{0: [1.0], 1: [nan], 2: [nan], 3: [nan], 4: [nan], 5: [nan], 6: [nan], 7: [nan], 8: [nan], 9: [nan], 10: [nan], 11: [nan], 12: [nan], 13: [nan], 14: [nan], 15: [nan], 16: [nan], 17: [nan], 18: [nan], 19: [nan], 20: [nan], 21: [nan], 22: [nan], 23: [nan], 24: [nan], 25: [nan], 26: [nan], 27: [nan], 28: [nan], 29: [nan]}\n", "Clinical data saved to ../../output/preprocess/Chronic_obstructive_pulmonary_disease_(COPD)/clinical_data/GSE32030.csv\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "import os\n", "import re\n", "from typing import Dict, Any, Optional, Callable\n", "\n", "# 1. Determine gene expression data availability\n", "# Based on the background information, this dataset contains microarray data for gene expression\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# For trait (COPD status), it appears to be in row 2\n", "trait_row = 2\n", "\n", "# Age data doesn't appear to be available in the sample characteristics\n", "age_row = None\n", "\n", "# Gender data doesn't appear to be available in the sample characteristics\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value):\n", " if pd.isna(value):\n", " return None\n", " \n", " # Extract the value after colon\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip().lower()\n", " \n", " # Check for COPD status\n", " if \"yes\" in value:\n", " return 1\n", " elif \"no\" in value:\n", " return 0\n", " return None\n", "\n", "def convert_age(value):\n", " # This function isn't needed since age data isn't available\n", " return None\n", "\n", "def convert_gender(value):\n", " # This function isn't needed since gender data isn't available\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Check if trait data is available (trait_row is not None)\n", "is_trait_available = trait_row is not None\n", "\n", "# Validate and save initial 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", " # Create clinical data DataFrame\n", " # The sample characteristics dictionary has rows as keys and values as lists\n", " sample_char_dict = {\n", " 0: ['department of genetic medicine id: DGM-00028', 'ethnicity: Afr', 'department of genetic medicine id: DGM-00060', 'department of genetic medicine id: DGM-00061', 'department of genetic medicine id: DGM-00104', 'department of genetic medicine id: DGM-10131', 'department of genetic medicine id: DGM-00167', 'department of genetic medicine id: DGM-00171', 'department of genetic medicine id: DGM-00262', 'department of genetic medicine id: DGM-00313', 'department of genetic medicine id: DGM-10287', 'department of genetic medicine id: DGM-00382', 'department of genetic medicine id: DGM-10139', 'department of genetic medicine id: DGM-00471', 'department of genetic medicine id: DGM-00512', 'department of genetic medicine id: DGM-02088', 'department of genetic medicine id: DGM-10152', 'department of genetic medicine id: DGM-00632', 'department of genetic medicine id: DGM-00683', 'department of genetic medicine id: DGM-10404', 'department of genetic medicine id: DGM-00791', 'department of genetic medicine id: DGM-10132', 'department of genetic medicine id: DGM-10135', 'department of genetic medicine id: DGM-01275', 'department of genetic medicine id: DGM-10144', 'department of genetic medicine id: DGM-10130', 'department of genetic medicine id: DGM-10406', 'department of genetic medicine id: DGM-01607', 'department of genetic medicine id: DGM-01737', 'department of genetic medicine id: DGM-01749'],\n", " 1: ['smoking status: S', 'department of genetic medicine id: DGM-00038', 'department of genetic medicine id: DGM-00029', 'department of genetic medicine id: DGM-00030', 'department of genetic medicine id: DGM-00035', 'department of genetic medicine id: DGM-00036', 'department of genetic medicine id: DGM-00041', 'department of genetic medicine id: DGM-00044', 'department of genetic medicine id: DGM-00052', 'department of genetic medicine id: DGM-00073', 'department of genetic medicine id: DGM-00069', 'department of genetic medicine id: DGM-00072', 'department of genetic medicine id: DGM-00078', 'smoking status: NS', 'department of genetic medicine id: DGM-00123', 'department of genetic medicine id: DGM-00244', 'department of genetic medicine id: DGM-00249', 'department of genetic medicine id: DGM-00416', 'department of genetic medicine id: DGM-00465', 'department of genetic medicine id: DGM-00530', 'department of genetic medicine id: DGM-00548', 'department of genetic medicine id: DGM-00551', 'department of genetic medicine id: DGM-00553', 'department of genetic medicine id: DGM-00625', 'department of genetic medicine id: DGM-00661', 'department of genetic medicine id: DGM-00685', 'department of genetic medicine id: DGM-00696', 'department of genetic medicine id: DGM-00757', 'department of genetic medicine id: DGM-00751', 'department of genetic medicine id: DGM-00778'],\n", " 2: ['copd status: yes', 'smoking status: S', 'smoking status: NS', np.nan, 'serum 25-oh-d: low vitamin D', 'serum 25-oh-d: mid vitamin D', 'serum 25-oh-d: high vitamin D', 'cell type: mixture of epithelium, basal, ciliated, secretory, undifferentiated and inflammatory cells'],\n", " 3: [np.nan, 'copd status: yes', 'serum 25-oh-d: mid vitamin D', 'serum 25-oh-d: high vitamin D', 'serum 25-oh-d: low vitamin D', 'time: n/a']\n", " }\n", " \n", " # Using the correct format for the geo_select_clinical_features function\n", " clinical_data = pd.DataFrame.from_dict(sample_char_dict, orient='index')\n", " \n", " # Extract clinical features using the library function\n", " selected_clinical_df = geo_select_clinical_features(\n", " clinical_df=clinical_data,\n", " trait=trait,\n", " trait_row=trait_row,\n", " convert_trait=convert_trait,\n", " age_row=age_row,\n", " convert_age=convert_age,\n", " gender_row=gender_row,\n", " convert_gender=convert_gender\n", " )\n", " \n", " # Preview the DataFrame to check extraction results\n", " preview_results = preview_df(selected_clinical_df)\n", " print(\"Preview of selected clinical features:\")\n", " print(preview_results)\n", " \n", " # Create directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " \n", " # Save the clinical data to CSV\n", " selected_clinical_df.to_csv(out_clinical_data_file, index=False)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "fb319b26", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "c74e9add", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:21:27.855172Z", "iopub.status.busy": "2025-03-25T08:21:27.855072Z", "iopub.status.idle": "2025-03-25T08:21:28.713863Z", "shell.execute_reply": "2025-03-25T08:21:28.713541Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix file found: ../../input/GEO/Chronic_obstructive_pulmonary_disease_(COPD)/GSE32030/GSE32030_series_matrix.txt.gz\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape: (54675, 270)\n", "First 20 gene/probe identifiers:\n", "Index(['1007_s_at', '1053_at', '117_at', '121_at', '1255_g_at', '1294_at',\n", " '1316_at', '1320_at', '1405_i_at', '1431_at', '1438_at', '1487_at',\n", " '1494_f_at', '1552256_a_at', '1552257_a_at', '1552258_at', '1552261_at',\n", " '1552263_at', '1552264_a_at', '1552266_at'],\n", " dtype='object', name='ID')\n" ] } ], "source": [ "# 1. Get the SOFT and matrix file paths again \n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "print(f\"Matrix file found: {matrix_file}\")\n", "\n", "# 2. Use the get_genetic_data function from the library to get the gene_data\n", "try:\n", " gene_data = get_genetic_data(matrix_file)\n", " print(f\"Gene data shape: {gene_data.shape}\")\n", " \n", " # 3. Print the first 20 row IDs (gene or probe identifiers)\n", " print(\"First 20 gene/probe identifiers:\")\n", " print(gene_data.index[:20])\n", "except Exception as e:\n", " print(f\"Error extracting gene data: {e}\")\n" ] }, { "cell_type": "markdown", "id": "015e7e88", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "5db9073f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:21:28.715722Z", "iopub.status.busy": "2025-03-25T08:21:28.715568Z", "iopub.status.idle": "2025-03-25T08:21:28.717562Z", "shell.execute_reply": "2025-03-25T08:21:28.717295Z" } }, "outputs": [], "source": [ "# Review gene identifiers\n", "# The gene identifiers (e.g., '1007_s_at', '1053_at') appear to be Affymetrix probe IDs\n", "# rather than standard human gene symbols (which would look like BRCA1, TP53, etc.)\n", "# These need to be mapped to gene symbols for biological interpretation\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "5d2e8fcb", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "4082114b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:21:28.719197Z", "iopub.status.busy": "2025-03-25T08:21:28.719096Z", "iopub.status.idle": "2025-03-25T08:21:42.433785Z", "shell.execute_reply": "2025-03-25T08:21:42.433399Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene annotation preview:\n", "Columns in gene annotation: ['ID', 'GB_ACC', 'SPOT_ID', 'Species Scientific Name', 'Annotation Date', 'Sequence Type', 'Sequence Source', 'Target Description', 'Representative Public ID', 'Gene Title', 'Gene Symbol', 'ENTREZ_GENE_ID', 'RefSeq Transcript ID', 'Gene Ontology Biological Process', 'Gene Ontology Cellular Component', 'Gene Ontology Molecular Function']\n", "{'ID': ['1007_s_at', '1053_at', '117_at', '121_at', '1255_g_at'], 'GB_ACC': ['U48705', 'M87338', 'X51757', 'X69699', 'L36861'], 'SPOT_ID': [nan, nan, nan, nan, nan], 'Species Scientific Name': ['Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens'], 'Annotation Date': ['Oct 6, 2014', 'Oct 6, 2014', 'Oct 6, 2014', 'Oct 6, 2014', 'Oct 6, 2014'], 'Sequence Type': ['Exemplar sequence', 'Exemplar sequence', 'Exemplar sequence', 'Exemplar sequence', 'Exemplar sequence'], 'Sequence Source': ['Affymetrix Proprietary Database', 'GenBank', 'Affymetrix Proprietary Database', 'GenBank', 'Affymetrix Proprietary Database'], 'Target Description': ['U48705 /FEATURE=mRNA /DEFINITION=HSU48705 Human receptor tyrosine kinase DDR gene, complete cds', 'M87338 /FEATURE= /DEFINITION=HUMA1SBU Human replication factor C, 40-kDa subunit (A1) mRNA, complete cds', \"X51757 /FEATURE=cds /DEFINITION=HSP70B Human heat-shock protein HSP70B' gene\", 'X69699 /FEATURE= /DEFINITION=HSPAX8A H.sapiens Pax8 mRNA', 'L36861 /FEATURE=expanded_cds /DEFINITION=HUMGCAPB Homo sapiens guanylate cyclase activating protein (GCAP) gene exons 1-4, complete cds'], 'Representative Public ID': ['U48705', 'M87338', 'X51757', 'X69699', 'L36861'], 'Gene Title': ['discoidin domain receptor tyrosine kinase 1 /// microRNA 4640', 'replication factor C (activator 1) 2, 40kDa', \"heat shock 70kDa protein 6 (HSP70B')\", 'paired box 8', 'guanylate cyclase activator 1A (retina)'], 'Gene Symbol': ['DDR1 /// MIR4640', 'RFC2', 'HSPA6', 'PAX8', 'GUCA1A'], 'ENTREZ_GENE_ID': ['780 /// 100616237', '5982', '3310', '7849', '2978'], 'RefSeq Transcript ID': ['NM_001202521 /// NM_001202522 /// NM_001202523 /// NM_001954 /// NM_013993 /// NM_013994 /// NR_039783 /// XM_005249385 /// XM_005249386 /// XM_005249387 /// XM_005249389 /// XM_005272873 /// XM_005272874 /// XM_005272875 /// XM_005272877 /// XM_005275027 /// XM_005275028 /// XM_005275030 /// XM_005275031 /// XM_005275162 /// XM_005275163 /// XM_005275164 /// XM_005275166 /// XM_005275457 /// XM_005275458 /// XM_005275459 /// XM_005275461 /// XM_006715185 /// XM_006715186 /// XM_006715187 /// XM_006715188 /// XM_006715189 /// XM_006715190 /// XM_006725501 /// XM_006725502 /// XM_006725503 /// XM_006725504 /// XM_006725505 /// XM_006725506 /// XM_006725714 /// XM_006725715 /// XM_006725716 /// XM_006725717 /// XM_006725718 /// XM_006725719 /// XM_006725720 /// XM_006725721 /// XM_006725722 /// XM_006725827 /// XM_006725828 /// XM_006725829 /// XM_006725830 /// XM_006725831 /// XM_006725832 /// XM_006726017 /// XM_006726018 /// XM_006726019 /// XM_006726020 /// XM_006726021 /// XM_006726022 /// XR_427836 /// XR_430858 /// XR_430938 /// XR_430974 /// XR_431015', 'NM_001278791 /// NM_001278792 /// NM_001278793 /// NM_002914 /// NM_181471 /// XM_006716080', 'NM_002155', 'NM_003466 /// NM_013951 /// NM_013952 /// NM_013953 /// NM_013992', 'NM_000409 /// XM_006715073'], 'Gene Ontology Biological Process': ['0001558 // regulation of cell growth // inferred from electronic annotation /// 0001952 // regulation of cell-matrix adhesion // inferred from electronic annotation /// 0006468 // protein phosphorylation // inferred from electronic annotation /// 0007155 // cell adhesion // traceable author statement /// 0007169 // transmembrane receptor protein tyrosine kinase signaling pathway // inferred from electronic annotation /// 0007565 // female pregnancy // inferred from electronic annotation /// 0007566 // embryo implantation // inferred from electronic annotation /// 0007595 // lactation // inferred from electronic annotation /// 0008285 // negative regulation of cell proliferation // inferred from electronic annotation /// 0010715 // regulation of extracellular matrix disassembly // inferred from mutant phenotype /// 0014909 // smooth muscle cell migration // inferred from mutant phenotype /// 0016310 // phosphorylation // inferred from electronic annotation /// 0018108 // peptidyl-tyrosine phosphorylation // inferred from electronic annotation /// 0030198 // extracellular matrix organization // traceable author statement /// 0038063 // collagen-activated tyrosine kinase receptor signaling pathway // inferred from direct assay /// 0038063 // collagen-activated tyrosine kinase receptor signaling pathway // inferred from mutant phenotype /// 0038083 // peptidyl-tyrosine autophosphorylation // inferred from direct assay /// 0043583 // ear development // inferred from electronic annotation /// 0044319 // wound healing, spreading of cells // inferred from mutant phenotype /// 0046777 // protein autophosphorylation // inferred from direct assay /// 0060444 // branching involved in mammary gland duct morphogenesis // inferred from electronic annotation /// 0060749 // mammary gland alveolus development // inferred from electronic annotation /// 0061302 // smooth muscle cell-matrix adhesion // inferred from mutant phenotype', '0000278 // mitotic cell cycle // traceable author statement /// 0000722 // telomere maintenance via recombination // traceable author statement /// 0000723 // telomere maintenance // traceable author statement /// 0006260 // DNA replication // traceable author statement /// 0006271 // DNA strand elongation involved in DNA replication // traceable author statement /// 0006281 // DNA repair // traceable author statement /// 0006283 // transcription-coupled nucleotide-excision repair // traceable author statement /// 0006289 // nucleotide-excision repair // traceable author statement /// 0006297 // nucleotide-excision repair, DNA gap filling // traceable author statement /// 0015979 // photosynthesis // inferred from electronic annotation /// 0015995 // chlorophyll biosynthetic process // inferred from electronic annotation /// 0032201 // telomere maintenance via semi-conservative replication // traceable author statement', '0000902 // cell morphogenesis // inferred from electronic annotation /// 0006200 // ATP catabolic process // inferred from direct assay /// 0006950 // response to stress // inferred from electronic annotation /// 0006986 // response to unfolded protein // traceable author statement /// 0034605 // cellular response to heat // inferred from direct assay /// 0042026 // protein refolding // inferred from direct assay /// 0070370 // cellular heat acclimation // inferred from mutant phenotype', '0001655 // urogenital system development // inferred from sequence or structural similarity /// 0001656 // metanephros development // inferred from electronic annotation /// 0001658 // branching involved in ureteric bud morphogenesis // inferred from expression pattern /// 0001822 // kidney development // inferred from expression pattern /// 0001823 // mesonephros development // inferred from sequence or structural similarity /// 0003337 // mesenchymal to epithelial transition involved in metanephros morphogenesis // inferred from expression pattern /// 0006351 // transcription, DNA-templated // inferred from direct assay /// 0006355 // regulation of transcription, DNA-templated // inferred from electronic annotation /// 0007275 // multicellular organismal development // inferred from electronic annotation /// 0007417 // central nervous system development // inferred from expression pattern /// 0009653 // anatomical structure morphogenesis // traceable author statement /// 0030154 // cell differentiation // inferred from electronic annotation /// 0030878 // thyroid gland development // inferred from expression pattern /// 0030878 // thyroid gland development // inferred from mutant phenotype /// 0038194 // thyroid-stimulating hormone signaling pathway // traceable author statement /// 0039003 // pronephric field specification // inferred from sequence or structural similarity /// 0042472 // inner ear morphogenesis // inferred from sequence or structural similarity /// 0042981 // regulation of apoptotic process // inferred from sequence or structural similarity /// 0045893 // positive regulation of transcription, DNA-templated // inferred from direct assay /// 0045893 // positive regulation of transcription, DNA-templated // inferred from sequence or structural similarity /// 0045944 // positive regulation of transcription from RNA polymerase II promoter // inferred from direct assay /// 0048793 // pronephros development // inferred from sequence or structural similarity /// 0071371 // cellular response to gonadotropin stimulus // inferred from direct assay /// 0071599 // otic vesicle development // inferred from expression pattern /// 0072050 // S-shaped body morphogenesis // inferred from electronic annotation /// 0072073 // kidney epithelium development // inferred from electronic annotation /// 0072108 // positive regulation of mesenchymal to epithelial transition involved in metanephros morphogenesis // inferred from sequence or structural similarity /// 0072164 // mesonephric tubule development // inferred from electronic annotation /// 0072207 // metanephric epithelium development // inferred from expression pattern /// 0072221 // metanephric distal convoluted tubule development // inferred from sequence or structural similarity /// 0072278 // metanephric comma-shaped body morphogenesis // inferred from expression pattern /// 0072284 // metanephric S-shaped body morphogenesis // inferred from expression pattern /// 0072289 // metanephric nephron tubule formation // inferred from sequence or structural similarity /// 0072305 // negative regulation of mesenchymal cell apoptotic process involved in metanephric nephron morphogenesis // inferred from sequence or structural similarity /// 0072307 // regulation of metanephric nephron tubule epithelial cell differentiation // inferred from sequence or structural similarity /// 0090190 // positive regulation of branching involved in ureteric bud morphogenesis // inferred from sequence or structural similarity /// 1900212 // negative regulation of mesenchymal cell apoptotic process involved in metanephros development // inferred from sequence or structural similarity /// 1900215 // negative regulation of apoptotic process involved in metanephric collecting duct development // inferred from sequence or structural similarity /// 1900218 // negative regulation of apoptotic process involved in metanephric nephron tubule development // inferred from sequence or structural similarity /// 2000594 // positive regulation of metanephric DCT cell differentiation // inferred from sequence or structural similarity /// 2000611 // positive regulation of thyroid hormone generation // inferred from mutant phenotype /// 2000612 // regulation of thyroid-stimulating hormone secretion // inferred from mutant phenotype', '0007165 // signal transduction // non-traceable author statement /// 0007601 // visual perception // inferred from electronic annotation /// 0007602 // phototransduction // inferred from electronic annotation /// 0007603 // phototransduction, visible light // traceable author statement /// 0016056 // rhodopsin mediated signaling pathway // traceable author statement /// 0022400 // regulation of rhodopsin mediated signaling pathway // traceable author statement /// 0030828 // positive regulation of cGMP biosynthetic process // inferred from electronic annotation /// 0031282 // regulation of guanylate cyclase activity // inferred from electronic annotation /// 0031284 // positive regulation of guanylate cyclase activity // inferred from electronic annotation /// 0050896 // response to stimulus // inferred from electronic annotation'], 'Gene Ontology Cellular Component': ['0005576 // extracellular region // inferred from electronic annotation /// 0005615 // extracellular space // inferred from direct assay /// 0005886 // plasma membrane // traceable author statement /// 0005887 // integral component of plasma membrane // traceable author statement /// 0016020 // membrane // inferred from electronic annotation /// 0016021 // integral component of membrane // inferred from electronic annotation /// 0043235 // receptor complex // inferred from direct assay /// 0070062 // extracellular vesicular exosome // inferred from direct assay', '0005634 // nucleus // inferred from electronic annotation /// 0005654 // nucleoplasm // traceable author statement /// 0005663 // DNA replication factor C complex // inferred from direct assay', '0005737 // cytoplasm // inferred from direct assay /// 0005814 // centriole // inferred from direct assay /// 0005829 // cytosol // inferred from direct assay /// 0008180 // COP9 signalosome // inferred from direct assay /// 0070062 // extracellular vesicular exosome // inferred from direct assay /// 0072562 // blood microparticle // inferred from direct assay', '0005634 // nucleus // inferred from direct assay /// 0005654 // nucleoplasm // inferred from sequence or structural similarity /// 0005730 // nucleolus // inferred from direct assay', '0001750 // photoreceptor outer segment // inferred from electronic annotation /// 0001917 // photoreceptor inner segment // inferred from electronic annotation /// 0005578 // proteinaceous extracellular matrix // inferred from electronic annotation /// 0005886 // plasma membrane // inferred from direct assay /// 0016020 // membrane // inferred from electronic annotation /// 0097381 // photoreceptor disc membrane // traceable author statement'], 'Gene Ontology Molecular Function': ['0000166 // nucleotide binding // inferred from electronic annotation /// 0004672 // protein kinase activity // inferred from electronic annotation /// 0004713 // protein tyrosine kinase activity // inferred from electronic annotation /// 0004714 // transmembrane receptor protein tyrosine kinase activity // traceable author statement /// 0005515 // protein binding // inferred from physical interaction /// 0005518 // collagen binding // inferred from direct assay /// 0005518 // collagen binding // inferred from mutant phenotype /// 0005524 // ATP binding // inferred from electronic annotation /// 0016301 // kinase activity // inferred from electronic annotation /// 0016740 // transferase activity // inferred from electronic annotation /// 0016772 // transferase activity, transferring phosphorus-containing groups // inferred from electronic annotation /// 0038062 // protein tyrosine kinase collagen receptor activity // inferred from direct assay /// 0046872 // metal ion binding // inferred from electronic annotation', '0000166 // nucleotide binding // inferred from electronic annotation /// 0003677 // DNA binding // inferred from electronic annotation /// 0005515 // protein binding // inferred from physical interaction /// 0005524 // ATP binding // inferred from electronic annotation /// 0016851 // magnesium chelatase activity // inferred from electronic annotation /// 0017111 // nucleoside-triphosphatase activity // inferred from electronic annotation', '0000166 // nucleotide binding // inferred from electronic annotation /// 0005524 // ATP binding // inferred from electronic annotation /// 0019899 // enzyme binding // inferred from physical interaction /// 0031072 // heat shock protein binding // inferred from physical interaction /// 0042623 // ATPase activity, coupled // inferred from direct assay /// 0051082 // unfolded protein binding // inferred from direct assay', '0000979 // RNA polymerase II core promoter sequence-specific DNA binding // inferred from direct assay /// 0003677 // DNA binding // inferred from direct assay /// 0003677 // DNA binding // inferred from mutant phenotype /// 0003700 // sequence-specific DNA binding transcription factor activity // inferred from direct assay /// 0004996 // thyroid-stimulating hormone receptor activity // traceable author statement /// 0005515 // protein binding // inferred from physical interaction /// 0044212 // transcription regulatory region DNA binding // inferred from direct assay', '0005509 // calcium ion binding // inferred from electronic annotation /// 0008048 // calcium sensitive guanylate cyclase activator activity // inferred from electronic annotation /// 0030249 // guanylate cyclase regulator activity // inferred from electronic annotation /// 0046872 // metal ion binding // inferred from electronic annotation']}\n", "\n", "Searching for platform information in SOFT file:\n", "Platform ID not found in first 100 lines\n", "\n", "Searching for gene symbol information in SOFT file:\n", "Found references to gene symbols:\n", "!Platform_relation = Alternative to: GPL19918 (Gene symbol version, 10K)\n", "!Platform_relation = Alternative to: GPL20182 (Gene Symbol Version)\n", "#Gene Symbol = A gene symbol, when one is available (from UniGene).\n", "ID\tGB_ACC\tSPOT_ID\tSpecies Scientific Name\tAnnotation Date\tSequence Type\tSequence Source\tTarget Description\tRepresentative Public ID\tGene Title\tGene Symbol\tENTREZ_GENE_ID\tRefSeq Transcript ID\tGene Ontology Biological Process\tGene Ontology Cellular Component\tGene Ontology Molecular Function\n", "\n", "Checking for additional annotation files in the directory:\n", "[]\n" ] } ], "source": [ "# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n", "gene_annotation = get_gene_annotation(soft_file)\n", "\n", "# 2. Analyze the gene annotation dataframe to identify which columns contain the gene identifiers and gene symbols\n", "print(\"\\nGene annotation preview:\")\n", "print(f\"Columns in gene annotation: {gene_annotation.columns.tolist()}\")\n", "print(preview_df(gene_annotation, n=5))\n", "\n", "# Let's look for platform information in the SOFT file to understand the annotation better\n", "print(\"\\nSearching for platform information in SOFT file:\")\n", "with gzip.open(soft_file, 'rt') as f:\n", " for i, line in enumerate(f):\n", " if '!Series_platform_id' in line:\n", " print(line.strip())\n", " break\n", " if i > 100: # Limit search to first 100 lines\n", " print(\"Platform ID not found in first 100 lines\")\n", " break\n", "\n", "# Check if the SOFT file includes any reference to gene symbols\n", "print(\"\\nSearching for gene symbol information in SOFT file:\")\n", "with gzip.open(soft_file, 'rt') as f:\n", " gene_symbol_lines = []\n", " for i, line in enumerate(f):\n", " if 'GENE_SYMBOL' in line or 'gene_symbol' in line.lower() or 'symbol' in line.lower():\n", " gene_symbol_lines.append(line.strip())\n", " if i > 1000 and len(gene_symbol_lines) > 0: # Limit search but ensure we found something\n", " break\n", " \n", " if gene_symbol_lines:\n", " print(\"Found references to gene symbols:\")\n", " for line in gene_symbol_lines[:5]: # Show just first 5 matches\n", " print(line)\n", " else:\n", " print(\"No explicit gene symbol references found in first 1000 lines\")\n", "\n", "# Look for alternative annotation files or references in the directory\n", "print(\"\\nChecking for additional annotation files in the directory:\")\n", "all_files = os.listdir(in_cohort_dir)\n", "print([f for f in all_files if 'annotation' in f.lower() or 'platform' in f.lower() or 'gpl' in f.lower()])\n" ] }, { "cell_type": "markdown", "id": "a126c357", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "4bd07934", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:21:42.435667Z", "iopub.status.busy": "2025-03-25T08:21:42.435552Z", "iopub.status.idle": "2025-03-25T08:21:45.807327Z", "shell.execute_reply": "2025-03-25T08:21:45.806684Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene mapping shape: (45782, 2)\n", "Preview of gene mapping dataframe:\n", "{'ID': ['1007_s_at', '1053_at', '117_at', '121_at', '1255_g_at'], 'Gene': ['DDR1 /// MIR4640', 'RFC2', 'HSPA6', 'PAX8', 'GUCA1A']}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data shape after mapping: (21278, 270)\n", "Preview of gene expression data after mapping:\n", "{'GSM549645': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549646': [83.487, 24.5968, 191.55792000000002, 2417.498, 155.804], 'GSM549647': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549648': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549649': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549650': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549651': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549652': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549653': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549654': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549655': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549656': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549657': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549658': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549659': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549660': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549661': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549662': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549663': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549664': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549665': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549666': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549667': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549668': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549669': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549670': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549671': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549672': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549673': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549674': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549675': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549676': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549677': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549678': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549679': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549680': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549681': [79.8985, 33.8459, 63.103899999999996, 1106.6274999999998, 97.6581], 'GSM549682': [106.748, 13.6255, 109.96461, 548.9238, 169.609], 'GSM549683': [56.3852, 6.20186, 19.28494, 689.6041, 152.512], 'GSM549684': [103.892, 170.254, 128.8056, 3142.0717999999997, 116.834], 'GSM549685': [96.0217, 26.3959, 59.059509999999996, 298.7124, 246.903], 'GSM549686': [92.5172, 47.0249, 39.6686, 1025.4899, 239.629], 'GSM549687': [112.926, 17.8522, 178.0674, 474.0219, 162.484], 'GSM549688': [101.499, 13.0719, 21.300600000000003, 515.4746, 167.698], 'GSM549689': [89.8639, 13.2181, 50.0547, 598.1255, 162.291], 'GSM549690': [94.3876, 14.6992, 115.45563999999999, 259.3296, 155.956], 'GSM549691': [81.1039, 111.923, 118.52878999999999, 556.3079, 196.811], 'GSM549692': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549693': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549694': [145.432, 18.2403, 149.34135, 633.056, 122.885], 'GSM549695': [73.3468, 64.1878, 44.3457, 942.999, 208.25], 'GSM549696': [108.285, 33.2299, 212.95942000000002, 375.5501, 167.874], 'GSM549697': [124.976, 8.75021, 111.8255, 769.7198, 204.835], 'GSM549698': [69.7605, 17.0834, 64.7095, 373.198, 158.053], 'GSM549699': [107.592, 43.5908, 138.7194, 570.734, 132.418], 'GSM549700': [7.17064, 24.6093, 45.22879, 337.1489, 235.543], 'GSM549701': [34.6541, 17.6079, 99.8776, 975.0500000000001, 259.372], 'GSM549702': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549703': [183.701, 19.3953, 277.4496, 1082.1532, 211.555], 'GSM549704': [142.264, 28.553, 28.873109999999997, 532.496, 136.837], 'GSM549705': [107.299, 10.8269, 48.199600000000004, 286.3279, 146.149], 'GSM549706': [188.175, 16.5678, 78.14500000000001, 517.939, 139.76], 'GSM549707': [177.098, 34.0955, 63.03193, 1204.2428, 182.461], 'GSM549708': [89.995, 18.7426, 118.566, 1321.1582, 204.395], 'GSM549709': [70.258, 34.8666, 60.5271, 353.6587, 114.924], 'GSM549710': [60.799, 19.9833, 198.94548, 796.954, 157.771], 'GSM549711': [80.6466, 14.4982, 25.522289999999998, 1695.833, 198.77], 'GSM549712': [161.328, 23.284, 75.31163, 386.2253, 131.845], 'GSM549713': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549714': [42.4446, 15.7597, 31.61233, 577.6783, 157.883], 'GSM549715': [57.4838, 20.786, 138.3775, 374.71709999999996, 227.028], 'GSM549716': [80.9654, 14.209, 206.14034, 364.59229999999997, 196.922], 'GSM549717': [115.75, 33.7845, 181.32569999999998, 607.8303999999999, 209.76], 'GSM549718': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549719': [198.899, 123.785, 201.5245, 933.3109999999999, 208.961], 'GSM549720': [147.466, 94.2022, 118.3036, 3761.7142000000003, 213.531], 'GSM549721': [97.2731, 17.9249, 143.2686, 552.2657, 331.664], 'GSM549722': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549723': [117.342, 14.86, 174.5779, 2989.244, 338.921], 'GSM549724': [96.0609, 15.716, 141.5864, 493.2584, 271.366], 'GSM549725': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549726': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549727': [135.918, 101.086, 188.51556, 584.6, 286.441], 'GSM549728': [135.262, 27.5867, 101.0747, 343.3082, 192.168], 'GSM549729': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549730': [85.1284, 21.6714, 194.88240000000002, 405.323, 237.595], 'GSM549731': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549732': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549733': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549734': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549735': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549736': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549737': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549738': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549739': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549740': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549742': [102.525, 37.3286, 284.0047, 1892.2227, 180.101], 'GSM549743': [117.759, 111.526, 122.05963, 380.6166, 270.506], 'GSM549744': [84.8877, 51.5581, 176.8804, 717.3501, 204.978], 'GSM549745': [57.158, 13.8994, 66.26289, 299.7611, 203.035], 'GSM549746': [119.755, 177.131, 33.6361, 447.231, 160.842], 'GSM549747': [93.1971, 14.5636, 38.62879, 444.996, 135.855], 'GSM549748': [105.483, 45.7346, 32.6573, 612.0436, 175.287], 'GSM549749': [52.9281, 116.751, 108.8237, 378.7838, 199.777], 'GSM549751': [112.23, 49.049, 52.1387, 908.6529999999999, 138.632], 'GSM549752': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549753': [73.6893, 32.6686, 137.9174, 513.083, 160.705], 'GSM549754': [77.9724, 18.6286, 20.003619999999998, 627.147, 129.998], 'GSM549755': [173.35, 98.2514, 82.5012, 1427.838, 160.175], 'GSM549756': [26.4713, 32.8067, 99.715, 387.28700000000003, 234.95], 'GSM549757': [75.5629, 23.5903, 244.0832, 585.525, 126.536], 'GSM549758': [92.5288, 24.9548, 220.4152, 345.773, 147.479], 'GSM549759': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549760': [118.227, 53.3219, 158.85789999999997, 350.57, 157.223], 'GSM549761': [37.1261, 47.8838, 191.3957, 585.7436, 203.579], 'GSM549762': [41.7881, 15.2259, 340.365, 479.73900000000003, 141.619], 'GSM549763': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549764': [52.7428, 84.3251, 181.4475, 589.184, 104.067], 'GSM549765': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549766': [233.169, 34.525, 162.6141, 380.06969999999995, 180.595], 'GSM549767': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549768': [226.56, 137.929, 227.7192, 351.4525, 204.234], 'GSM549769': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549770': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549771': [81.2921, 21.5796, 66.58802, 358.2874, 126.463], 'GSM549772': [132.18, 23.2595, 136.70881000000003, 548.9593, 202.11], 'GSM549773': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549774': [53.3759, 98.2225, 149.86019000000002, 342.4681, 191.429], 'GSM549775': [150.976, 30.2578, 218.45680000000002, 602.6143999999999, 126.973], 'GSM549776': [59.0237, 13.9924, 156.4393, 389.9093, 178.275], 'GSM549777': [109.544, 223.866, 143.57254, 1060.1621, 172.028], 'GSM549778': [199.898, 38.5934, 402.121, 868.739, 202.949], 'GSM549779': [158.479, 72.9566, 142.1064, 1010.8523, 179.791], 'GSM549780': [177.685, 148.571, 196.14395, 420.722, 178.046], 'GSM549781': [19.1452, 107.463, 145.3769, 443.711, 324.683], 'GSM549783': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549784': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549785': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549786': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549787': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549788': [121.093, 19.1427, 151.1011, 737.7529999999999, 210.965], 'GSM549789': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549790': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549791': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549792': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549793': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549794': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549795': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549796': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549797': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549798': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549799': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549800': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549801': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549802': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549803': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549804': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549805': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549806': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549807': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549808': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549809': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549810': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549811': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549812': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549813': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549814': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549815': [19.387, 38.8757, 60.567400000000006, 337.2875, 147.551], 'GSM549816': [50.9951, 20.8406, 30.584553000000003, 506.486, 145.483], 'GSM549817': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549818': [93.6746, 168.798, 105.08609999999999, 215.679, 148.589], 'GSM549819': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549820': [0.0, 0.0, 0.0, 0.0, 0.0], 'GSM549821': [130.232, 37.8335, 149.86756, 466.117, 213.617], 'GSM569911': [116.002, 23.0029, 190.6557, 351.28290000000004, 195.064], 'GSM569912': [66.6933, 33.971, 193.4934, 355.5367, 196.432], 'GSM569913': [137.351, 132.715, 87.11158, 354.317, 175.905], 'GSM569914': [57.4315, 33.4559, 103.12708, 610.013, 221.058], 'GSM569915': [65.1075, 37.7201, 137.94915, 1824.444, 263.326], 'GSM569916': [48.5214, 37.2721, 36.291900000000005, 412.703, 234.132], 'GSM569917': [44.6359, 20.4832, 151.24349999999998, 542.8928999999999, 209.84], 'GSM569918': [69.322, 67.7748, 123.69810000000001, 777.2716, 99.847], 'GSM569919': [24.9046, 79.2262, 20.598750000000003, 695.9859, 147.574], 'GSM569920': [139.054, 235.286, 131.22938, 576.1043999999999, 254.284], 'GSM569921': [97.4634, 70.4461, 162.9117, 608.5450000000001, 127.923], 'GSM569922': [29.465, 111.283, 30.5985, 566.4000000000001, 178.086], 'GSM569923': [125.246, 175.492, 19.844079999999998, 884.897, 127.627], 'GSM569924': [86.1242, 80.2365, 133.56458999999998, 559.3254, 165.852], 'GSM569925': [228.702, 35.0545, 136.4715, 610.391, 130.059], 'GSM569926': [153.626, 23.8631, 147.5693, 1229.7387, 130.665], 'GSM599910': [70.3282, 11.2307, 140.5716, 241.9946, 196.603], 'GSM599911': [60.8249, 145.274, 283.4954, 547.0609999999999, 148.359], 'GSM599912': [39.379, 23.6161, 125.4984, 627.439, 326.518], 'GSM599913': [53.5048, 15.5618, 78.1506, 724.7255, 119.698], 'GSM599914': [107.087, 22.3735, 95.1035, 714.3497, 223.895], 'GSM599915': [34.9291, 7.50517, 55.8276, 406.01599999999996, 204.857], 'GSM599916': [53.0812, 51.4187, 76.1837, 722.2659, 160.397], 'GSM599917': [49.0427, 27.6813, 74.6809, 465.952, 115.685], 'GSM599918': [187.701, 66.5259, 78.81049999999999, 275.52930000000003, 251.993], 'GSM599919': [15.0983, 18.2123, 169.8992, 823.3629999999999, 101.393]}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Chronic_obstructive_pulmonary_disease_(COPD)/gene_data/GSE32030.csv\n" ] } ], "source": [ "# 1. Identify which columns in gene_annotation contain probe IDs and gene symbols\n", "# Based on the preview, 'ID' contains probe IDs and 'Gene Symbol' contains gene symbols\n", "id_column = 'ID'\n", "gene_symbol_column = 'Gene Symbol'\n", "\n", "# 2. Get a gene mapping dataframe\n", "gene_mapping = get_gene_mapping(gene_annotation, id_column, gene_symbol_column)\n", "print(f\"Gene mapping shape: {gene_mapping.shape}\")\n", "print(\"Preview of gene mapping dataframe:\")\n", "print(preview_df(gene_mapping))\n", "\n", "# 3. Apply gene mapping to convert from probe-level to gene-level expression\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "print(f\"Gene expression data shape after mapping: {gene_data.shape}\")\n", "print(\"Preview of gene expression data after mapping:\")\n", "print(preview_df(gene_data))\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 the gene data to CSV\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": "7bc532f7", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "a4b3adcb", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:21:45.809274Z", "iopub.status.busy": "2025-03-25T08:21:45.809149Z", "iopub.status.idle": "2025-03-25T08:21:53.839654Z", "shell.execute_reply": "2025-03-25T08:21:53.839026Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data shape: (21278, 270)\n", "Gene data column names (sample IDs):\n", "Index(['GSM549645', 'GSM549646', 'GSM549647', 'GSM549648', 'GSM549649'], dtype='object')\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Raw clinical data structure:\n", "Clinical data shape: (4, 271)\n", "Clinical data columns: Index(['!Sample_geo_accession', 'GSM549645', 'GSM549646', 'GSM549647',\n", " 'GSM549648'],\n", " dtype='object')\n", "\n", "Sample characteristics dictionary:\n", "{0: ['department of genetic medicine id: DGM-00028', 'ethnicity: Afr', 'department of genetic medicine id: DGM-00060', 'department of genetic medicine id: DGM-00061', 'department of genetic medicine id: DGM-00104', 'department of genetic medicine id: DGM-10131', 'department of genetic medicine id: DGM-00167', 'department of genetic medicine id: DGM-00171', 'department of genetic medicine id: DGM-00262', 'department of genetic medicine id: DGM-00313', 'department of genetic medicine id: DGM-10287', 'department of genetic medicine id: DGM-00382', 'department of genetic medicine id: DGM-10139', 'department of genetic medicine id: DGM-00471', 'department of genetic medicine id: DGM-00512', 'department of genetic medicine id: DGM-02088', 'department of genetic medicine id: DGM-10152', 'department of genetic medicine id: DGM-00632', 'department of genetic medicine id: DGM-00683', 'department of genetic medicine id: DGM-10404', 'department of genetic medicine id: DGM-00791', 'department of genetic medicine id: DGM-10132', 'department of genetic medicine id: DGM-10135', 'department of genetic medicine id: DGM-01275', 'department of genetic medicine id: DGM-10144', 'department of genetic medicine id: DGM-10130', 'department of genetic medicine id: DGM-10406', 'department of genetic medicine id: DGM-01607', 'department of genetic medicine id: DGM-01737', 'department of genetic medicine id: DGM-01749'], 1: ['smoking status: S', 'department of genetic medicine id: DGM-00038', 'department of genetic medicine id: DGM-00029', 'department of genetic medicine id: DGM-00030', 'department of genetic medicine id: DGM-00035', 'department of genetic medicine id: DGM-00036', 'department of genetic medicine id: DGM-00041', 'department of genetic medicine id: DGM-00044', 'department of genetic medicine id: DGM-00052', 'department of genetic medicine id: DGM-00073', 'department of genetic medicine id: DGM-00069', 'department of genetic medicine id: DGM-00072', 'department of genetic medicine id: DGM-00078', 'smoking status: NS', 'department of genetic medicine id: DGM-00123', 'department of genetic medicine id: DGM-00244', 'department of genetic medicine id: DGM-00249', 'department of genetic medicine id: DGM-00416', 'department of genetic medicine id: DGM-00465', 'department of genetic medicine id: DGM-00530', 'department of genetic medicine id: DGM-00548', 'department of genetic medicine id: DGM-00551', 'department of genetic medicine id: DGM-00553', 'department of genetic medicine id: DGM-00625', 'department of genetic medicine id: DGM-00661', 'department of genetic medicine id: DGM-00685', 'department of genetic medicine id: DGM-00696', 'department of genetic medicine id: DGM-00757', 'department of genetic medicine id: DGM-00751', 'department of genetic medicine id: DGM-00778'], 2: ['copd status: yes', 'smoking status: S', 'smoking status: NS', nan, 'serum 25-oh-d: low vitamin D', 'serum 25-oh-d: mid vitamin D', 'serum 25-oh-d: high vitamin D', 'cell type: mixture of epithelium, basal, ciliated, secretory, undifferentiated and inflammatory cells'], 3: [nan, 'copd status: yes', 'serum 25-oh-d: mid vitamin D', 'serum 25-oh-d: high vitamin D', 'serum 25-oh-d: low vitamin D', 'time: n/a']}\n", "\n", "Values in trait row:\n", "['!Sample_characteristics_ch1' 'copd status: yes' 'smoking status: S'\n", " 'copd status: yes' 'copd status: yes']\n", "\n", "Created clinical features dataframe:\n", "Shape: (1, 270)\n", " GSM549645 GSM549646 GSM549647 \\\n", "Chronic_obstructive_pulmonary_disease_(COPD) 1 None 1 \n", "\n", " GSM549648 GSM549649 \n", "Chronic_obstructive_pulmonary_disease_(COPD) 1 1 \n", "\n", "Linked data shape before handling missing values: (270, 21279)\n", "Actual trait column in linked data: Chronic_obstructive_pulmonary_disease_(COPD)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/media/techt/DATA/GenoAgent/tools/preprocess.py:400: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.\n", " linked_data = pd.concat([clinical_df, genetic_df], axis=0).T\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/media/techt/DATA/GenoAgent/tools/preprocess.py:455: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n", " df[gene_cols] = df[gene_cols].fillna(df[gene_cols].mean())\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data shape after handling missing values: (35, 21279)\n", "Quartiles for 'Chronic_obstructive_pulmonary_disease_(COPD)':\n", " 25%: 1.0\n", " 50% (Median): 1.0\n", " 75%: 1.0\n", "Min: 1\n", "Max: 1\n", "The distribution of the feature 'Chronic_obstructive_pulmonary_disease_(COPD)' in this dataset is severely biased.\n", "\n", "Dataset deemed not usable due to quality issues - linked data not saved\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data \n", "# (This was already done in the previous step, so no need to repeat)\n", "print(f\"Normalized gene data shape: {gene_data.shape}\")\n", "\n", "# 2. Examine the sample IDs in the gene expression data to understand the structure\n", "print(\"Gene data column names (sample IDs):\")\n", "print(gene_data.columns[:5]) # Print first 5 for brevity\n", "\n", "# Inspect the clinical data format from the matrix file directly\n", "background_info, clinical_data = get_background_and_clinical_data(matrix_file)\n", "print(\"\\nRaw clinical data structure:\")\n", "print(f\"Clinical data shape: {clinical_data.shape}\")\n", "print(f\"Clinical data columns: {clinical_data.columns[:5]}\")\n", "\n", "# Get the sample characteristics to re-extract the disease information\n", "sample_characteristics_dict = get_unique_values_by_row(clinical_data)\n", "print(\"\\nSample characteristics dictionary:\")\n", "print(sample_characteristics_dict)\n", "\n", "# 3. Directly create clinical features from the raw data again\n", "# Verify trait row contains the disease information (OA vs RA)\n", "print(\"\\nValues in trait row:\")\n", "trait_values = clinical_data.iloc[trait_row].values\n", "print(trait_values[:5])\n", "\n", "# Create clinical dataframe with proper structure\n", "# First get the sample IDs from gene data as these are our actual sample identifiers\n", "sample_ids = gene_data.columns.tolist()\n", "\n", "# Create the clinical features dataframe with those sample IDs\n", "clinical_features = pd.DataFrame(index=[trait], columns=sample_ids)\n", "\n", "# Fill the clinical features with our trait values by mapping GSM IDs to actual values\n", "for col in clinical_data.columns:\n", " if col in sample_ids:\n", " # Extract the disease value and convert it\n", " disease_val = clinical_data.iloc[trait_row][col]\n", " clinical_features.loc[trait, col] = convert_trait(disease_val)\n", "\n", "print(\"\\nCreated clinical features dataframe:\")\n", "print(f\"Shape: {clinical_features.shape}\")\n", "print(clinical_features.iloc[:, :5]) # Show first 5 columns\n", "\n", "# 4. Link clinical and genetic data\n", "linked_data = geo_link_clinical_genetic_data(clinical_features, gene_data)\n", "print(f\"\\nLinked data shape before handling missing values: {linked_data.shape}\")\n", "\n", "# 5. Handle missing values - we need to use the actual column name, not the trait variable\n", "# First identify the actual trait column name in the linked data\n", "trait_column = clinical_features.index[0] # This should be 'Osteoarthritis'\n", "print(f\"Actual trait column in linked data: {trait_column}\")\n", "\n", "# Now handle missing values with the correct column name\n", "linked_data_clean = handle_missing_values(linked_data, trait_column)\n", "print(f\"Linked data shape after handling missing values: {linked_data_clean.shape}\")\n", "\n", "# 6. Evaluate bias in trait and demographic features\n", "is_biased, linked_data_clean = judge_and_remove_biased_features(linked_data_clean, trait_column)\n", "\n", "# 7. Conduct final quality validation\n", "note = \"Dataset contains gene expression data from synovial fibroblasts of RA and OA patients. Data includes high serum and low serum responses.\"\n", "is_usable = validate_and_save_cohort_info(\n", " is_final=True,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=True,\n", " is_trait_available=(linked_data_clean.shape[0] > 0),\n", " is_biased=is_biased,\n", " df=linked_data_clean,\n", " note=note\n", ")\n", "\n", "# 8. Save linked data if usable\n", "if is_usable:\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " linked_data_clean.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", "else:\n", " print(\"Dataset deemed not usable due to quality issues - linked data not saved\")" ] } ], "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.16" } }, "nbformat": 4, "nbformat_minor": 5 }