{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "f81855e9", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:51:31.978521Z", "iopub.status.busy": "2025-03-25T03:51:31.978403Z", "iopub.status.idle": "2025-03-25T03:51:32.149113Z", "shell.execute_reply": "2025-03-25T03:51:32.148717Z" } }, "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 = \"Rheumatoid_Arthritis\"\n", "cohort = \"GSE224330\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Rheumatoid_Arthritis\"\n", "in_cohort_dir = \"../../input/GEO/Rheumatoid_Arthritis/GSE224330\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Rheumatoid_Arthritis/GSE224330.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Rheumatoid_Arthritis/gene_data/GSE224330.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Rheumatoid_Arthritis/clinical_data/GSE224330.csv\"\n", "json_path = \"../../output/preprocess/Rheumatoid_Arthritis/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "d9cbbd27", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "267edc92", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:51:32.150629Z", "iopub.status.busy": "2025-03-25T03:51:32.150483Z", "iopub.status.idle": "2025-03-25T03:51:32.294812Z", "shell.execute_reply": "2025-03-25T03:51:32.294478Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Gene expression of monocytes from rheumatoid arthritis patients treated with bDMARDs and methotrexate.\"\n", "!Series_summary\t\"It is well documented that patients affected by rheumatoid arthritis (RA) have distinct susceptibility to the different biologic Disease-Modifying AntiRheumatic Drugs (bDMARDs) available on the market, probably because of the many facets of the disease. Monocytes are deeply involved in the pathogenesis of RA and we therefore evaluated and compared the transcriptomic profile of monocytes isolated from patients on treatment with methotrexate alone or in combination with tocilizumab, anti-TNFalpha or abatacept, and from healthy donors. Differential expression analysis of whole-genome transcriptomics yielded a list of regulated genes suitable for functional annotation enrichment analysis. Specifically, abatacept, tocilizumab and anti-TNFalpha cohorts were separately compared with methotrexate using a rank-product-based statistical approach, leading to the identification of 78, 6, and 436 differentially expressed genes, respectively.\"\n", "!Series_overall_design\t\"Gene expression profiling was performed on primary monocyte cultures from a total of 31 samples, according to the following experimental design: 10 samples from healthy patients, 6 samples from MTX-, 5 samples from abatacept-, 5 samples from anti-TNFalpha-, and 5 samples from tocilizumab-treated patients.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['tissue: Isolated monocytes'], 1: ['age: 63y', 'age: 64y', 'age: 48y', 'age: 70y', 'age: 62y', 'age: 58y', 'age: 57y', 'age: 60y', 'age: 52y', 'age: 51y', 'age: 53y', 'age: 56y', 'age: 54y', 'age: 61y', 'age: 55y', 'age: 65y', 'age: 84y', 'age: 76y', 'age: 73y', 'age: 71y', 'age: 59y', 'age: 47y'], 2: ['gender: female', 'gender: male'], 3: ['comorbidity: hypothyroidism', 'comorbidity: none', 'comorbidity: osteoporosis', nan, 'comorbidity: schizoaffective disorder\\xa0', 'comorbidity: arthrosis']}\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": "3a479f64", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "498f1673", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:51:32.296593Z", "iopub.status.busy": "2025-03-25T03:51:32.296477Z", "iopub.status.idle": "2025-03-25T03:51:32.303149Z", "shell.execute_reply": "2025-03-25T03:51:32.302878Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Step 1: Determine if gene expression data is available\n", "# From the background information, this dataset contains gene expression profiling\n", "# of monocytes from various patients including RA patients and healthy controls\n", "is_gene_available = True\n", "\n", "# Step 2: Determine the availability of trait, age, and gender data\n", "# Looking at the sample characteristics, there is no direct indication of RA status\n", "# The series description mentions RA patients and healthy controls, but this information\n", "# is not directly encoded in the sample characteristics dictionary\n", "trait_row = None # No direct trait information in sample characteristics\n", "\n", "# Age is available in row 1\n", "age_row = 1\n", "\n", "# Gender is available in row 2\n", "gender_row = 2\n", "\n", "# Define conversion functions\n", "def convert_trait(value):\n", " # This won't be used since trait_row is None, but we'll define it anyway\n", " if pd.isna(value):\n", " return None\n", " value = value.lower().strip()\n", " if \"rheumatoid arthritis\" in value or \"ra\" in value:\n", " return 1\n", " elif \"healthy\" in value or \"control\" in value:\n", " return 0\n", " else:\n", " return None\n", "\n", "def convert_age(value):\n", " if pd.isna(value):\n", " return None\n", " # Extract the age value which is in format \"age: XXy\"\n", " try:\n", " if \":\" in value:\n", " age_str = value.split(\":\")[1].strip()\n", " # Remove the 'y' and convert to integer\n", " age = int(age_str.replace('y', '').strip())\n", " return age\n", " else:\n", " return None\n", " except:\n", " return None\n", "\n", "def convert_gender(value):\n", " if pd.isna(value):\n", " return None\n", " # Extract the gender value which is in format \"gender: XXX\"\n", " if \":\" in value:\n", " gender = value.split(\":\")[1].strip().lower()\n", " if \"female\" in gender:\n", " return 0\n", " elif \"male\" in gender:\n", " return 1\n", " else:\n", " return None\n", " else:\n", " return None\n", "\n", "# Step 3: Save metadata\n", "# Determine trait availability - since we don't have a direct trait indicator\n", "is_trait_available = trait_row is not None\n", "\n", "# Validate and save cohort information\n", "validate_and_save_cohort_info(\n", " is_final=False,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=is_gene_available,\n", " is_trait_available=is_trait_available\n", ")\n", "\n", "# Step 4: Since trait_row is None, we skip clinical feature extraction\n" ] }, { "cell_type": "markdown", "id": "72cd6b72", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "ca757f47", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:51:32.304648Z", "iopub.status.busy": "2025-03-25T03:51:32.304543Z", "iopub.status.idle": "2025-03-25T03:51:32.495020Z", "shell.execute_reply": "2025-03-25T03:51:32.494631Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['A_19_P00315452', 'A_19_P00315492', 'A_19_P00315493', 'A_19_P00315502',\n", " 'A_19_P00315506', 'A_19_P00315518', 'A_19_P00315519', 'A_19_P00315529',\n", " 'A_19_P00315541', 'A_19_P00315543', 'A_19_P00315551', 'A_19_P00315581',\n", " 'A_19_P00315584', 'A_19_P00315593', 'A_19_P00315603', 'A_19_P00315625',\n", " 'A_19_P00315627', 'A_19_P00315631', 'A_19_P00315641', 'A_19_P00315647'],\n", " dtype='object', name='ID')\n" ] } ], "source": [ "# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.\n", "gene_data = get_genetic_data(matrix_file)\n", "\n", "# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.\n", "print(gene_data.index[:20])\n" ] }, { "cell_type": "markdown", "id": "62ddaa60", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "77af76da", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:51:32.496698Z", "iopub.status.busy": "2025-03-25T03:51:32.496385Z", "iopub.status.idle": "2025-03-25T03:51:32.498525Z", "shell.execute_reply": "2025-03-25T03:51:32.498255Z" } }, "outputs": [], "source": [ "# Reviewing the gene identifiers in the gene expression data\n", "\n", "# The identifiers starting with \"A_19_P...\" appear to be Agilent microarray probe IDs\n", "# rather than standard human gene symbols.\n", "# These are microarray-specific identifiers that would need to be mapped to human gene symbols.\n", "# The format is typical of Agilent platform-specific probe identifiers.\n", "\n", "# Human gene symbols would typically look like BRCA1, TP53, IL6, etc.\n", "# The identifiers shown are clearly platform-specific probe IDs that require mapping.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "9436fcb7", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "5a56b511", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:51:32.499570Z", "iopub.status.busy": "2025-03-25T03:51:32.499465Z", "iopub.status.idle": "2025-03-25T03:51:35.511851Z", "shell.execute_reply": "2025-03-25T03:51:35.511487Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['GE_BrightCorner', 'DarkCorner', 'A_21_P0014386', 'A_33_P3396872', 'A_33_P3267760'], 'CONTROL_TYPE': ['pos', 'pos', 'FALSE', 'FALSE', 'FALSE'], 'REFSEQ': [nan, nan, nan, 'NM_001105533', nan], 'GB_ACC': [nan, nan, nan, 'NM_001105533', nan], 'LOCUSLINK_ID': [nan, nan, nan, 79974.0, 54880.0], 'GENE_SYMBOL': [nan, nan, nan, 'CPED1', 'BCOR'], 'GENE_NAME': [nan, nan, nan, 'cadherin-like and PC-esterase domain containing 1', 'BCL6 corepressor'], 'UNIGENE_ID': [nan, nan, nan, 'Hs.189652', nan], 'ENSEMBL_ID': [nan, nan, nan, nan, 'ENST00000378463'], 'ACCESSION_STRING': [nan, nan, nan, 'ref|NM_001105533|gb|AK025639|gb|BC030538|tc|THC2601673', 'ens|ENST00000378463'], 'CHROMOSOMAL_LOCATION': [nan, nan, 'unmapped', 'chr7:120901888-120901947', 'chrX:39909128-39909069'], 'CYTOBAND': [nan, nan, nan, 'hs|7q31.31', 'hs|Xp11.4'], 'DESCRIPTION': [nan, nan, nan, 'Homo sapiens cadherin-like and PC-esterase domain containing 1 (CPED1), transcript variant 2, mRNA [NM_001105533]', 'BCL6 corepressor [Source:HGNC Symbol;Acc:HGNC:20893] [ENST00000378463]'], 'GO_ID': [nan, nan, nan, 'GO:0005783(endoplasmic reticulum)', 'GO:0000122(negative regulation of transcription from RNA polymerase II promoter)|GO:0000415(negative regulation of histone H3-K36 methylation)|GO:0003714(transcription corepressor activity)|GO:0004842(ubiquitin-protein ligase activity)|GO:0005515(protein binding)|GO:0005634(nucleus)|GO:0006351(transcription, DNA-dependent)|GO:0007507(heart development)|GO:0008134(transcription factor binding)|GO:0030502(negative regulation of bone mineralization)|GO:0031072(heat shock protein binding)|GO:0031519(PcG protein complex)|GO:0035518(histone H2A monoubiquitination)|GO:0042476(odontogenesis)|GO:0042826(histone deacetylase binding)|GO:0044212(transcription regulatory region DNA binding)|GO:0045892(negative regulation of transcription, DNA-dependent)|GO:0051572(negative regulation of histone H3-K4 methylation)|GO:0060021(palate development)|GO:0065001(specification of axis polarity)|GO:0070171(negative regulation of tooth mineralization)'], 'SEQUENCE': [nan, nan, 'AATACATGTTTTGGTAAACACTCGGTCAGAGCACCCTCTTTCTGTGGAATCAGACTGGCA', 'GCTTATCTCACCTAATACAGGGACTATGCAACCAAGAAACTGGAAATAAAAACAAAGATA', 'CATCAAAGCTACGAGAGATCCTACACACCCAGATTTAAAAAATAATAAAAACTTAAGGGC'], 'SPOT_ID': ['GE_BrightCorner', 'DarkCorner', 'A_21_P0014386', 'A_33_P3396872', 'A_33_P3267760']}\n" ] } ], "source": [ "# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n", "gene_annotation = get_gene_annotation(soft_file)\n", "\n", "# 2. Use the 'preview_df' function from the library to preview the data and print out the results.\n", "print(\"Gene annotation preview:\")\n", "print(preview_df(gene_annotation))\n" ] }, { "cell_type": "markdown", "id": "2d40f0f4", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "bc46de6a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:51:35.513201Z", "iopub.status.busy": "2025-03-25T03:51:35.513072Z", "iopub.status.idle": "2025-03-25T03:51:35.743752Z", "shell.execute_reply": "2025-03-25T03:51:35.743406Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First few rows of the mapped gene expression data:\n", " GSM7019507 GSM7019508 GSM7019509 GSM7019510 GSM7019511 \\\n", "Gene \n", "A1BG 9.809589 10.329705 9.745500 10.834169 9.798319 \n", "A1BG-AS1 7.160811 6.989542 7.031746 7.916862 7.645764 \n", "A1CF 6.110426 6.359480 6.127004 5.932559 6.170733 \n", "A1CF-2 6.161767 5.948398 5.868721 5.825201 5.901578 \n", "A1CF-3 6.081881 6.855441 6.619097 6.117701 6.343309 \n", "\n", " GSM7019512 GSM7019513 GSM7019514 GSM7019515 GSM7019516 ... \\\n", "Gene ... \n", "A1BG 9.578622 9.730334 9.686282 10.763011 10.080284 ... \n", "A1BG-AS1 6.965617 7.302826 7.289203 7.590010 6.989714 ... \n", "A1CF 6.177787 6.029664 6.195742 6.116004 6.805011 ... \n", "A1CF-2 6.015555 6.008692 5.965431 6.086006 6.250949 ... \n", "A1CF-3 6.456985 6.404266 6.687078 6.661359 6.041631 ... \n", "\n", " GSM7019528 GSM7019529 GSM7019530 GSM7019531 GSM7019532 \\\n", "Gene \n", "A1BG 9.724614 10.186862 10.039998 9.922852 10.108389 \n", "A1BG-AS1 7.028299 7.280407 7.302357 7.378149 7.238104 \n", "A1CF 6.035599 6.382450 6.025403 5.985312 5.995381 \n", "A1CF-2 6.006214 5.965098 6.120725 5.859149 6.117033 \n", "A1CF-3 6.267565 6.467267 6.082947 6.360659 6.511120 \n", "\n", " GSM7019533 GSM7019534 GSM7019535 GSM7019536 GSM7019537 \n", "Gene \n", "A1BG 8.225830 10.018493 10.165201 11.279688 9.522288 \n", "A1BG-AS1 6.458788 7.538693 7.275054 7.428836 6.925335 \n", "A1CF 6.039518 5.873892 6.135850 6.249139 6.109745 \n", "A1CF-2 5.893409 5.989623 5.905877 5.943874 6.023270 \n", "A1CF-3 6.267860 6.304474 6.490375 6.215660 6.248810 \n", "\n", "[5 rows x 31 columns]\n", "Shape of gene expression data: (29222, 31)\n" ] } ], "source": [ "# 1. Observe the gene expression data and gene annotation data to identify mapping columns\n", "# The gene expression data uses row IDs similar to A_19_P00315452, A_19_P00315492, etc.\n", "# In the gene annotation data, the 'ID' column appears to contain these same identifiers.\n", "# The 'GENE_SYMBOL' column contains the human gene symbols we want to map to.\n", "\n", "# 2. Extract the mapping data from the gene annotation dataframe\n", "# Create a mapping dataframe with the probe IDs and corresponding gene symbols\n", "mapping_data = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='GENE_SYMBOL')\n", "\n", "# 3. Apply the gene mapping to convert probe-level measurements to gene expression data\n", "# This function handles the many-to-many relationships between probes and genes\n", "gene_data = apply_gene_mapping(gene_data, mapping_data)\n", "\n", "# Preview the first few rows of the mapped gene data\n", "print(\"First few rows of the mapped gene expression data:\")\n", "print(gene_data.head())\n", "print(f\"Shape of gene expression data: {gene_data.shape}\")\n" ] }, { "cell_type": "markdown", "id": "d00d0c8a", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "086055d3", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:51:35.745147Z", "iopub.status.busy": "2025-03-25T03:51:35.745030Z", "iopub.status.idle": "2025-03-25T03:51:36.255414Z", "shell.execute_reply": "2025-03-25T03:51:36.255017Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "No trait information available in the sample characteristics, skipping clinical feature extraction\n", "Normalized gene data shape: (20778, 31)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Rheumatoid_Arthritis/gene_data/GSE224330.csv\n", "Dataset contains gene expression data but lacks the trait information needed for association analysis\n" ] } ], "source": [ "# Since trait_row is None (meaning we don't have direct trait information), \n", "# we can't extract clinical features or properly link the data as originally planned\n", "\n", "# We skip clinical feature extraction and go directly to gene data normalization and validation\n", "print(\"No trait information available in the sample characteristics, skipping clinical feature extraction\")\n", "\n", "# 1. Normalize gene symbols in the gene expression data\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Normalized gene data shape: {normalized_gene_data.shape}\")\n", "\n", "# Save the normalized gene data\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "normalized_gene_data.to_csv(out_gene_data_file)\n", "print(f\"Normalized gene data saved to {out_gene_data_file}\")\n", "\n", "# Since we don't have trait information, we can't create a linked dataset for association analysis\n", "# We need to provide a valid DataFrame and is_biased value for final validation\n", "# Since there's no trait information, we can't assess bias, so we'll set it to False\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=False,\n", " is_biased=False, # Cannot be None for final validation\n", " df=normalized_gene_data, # Provide actual data instead of empty DataFrame\n", " note=\"Dataset contains gene expression data but lacks rheumatoid arthritis trait information in the sample characteristics\"\n", ")\n", "\n", "print(\"Dataset contains gene expression data but lacks the trait information needed for association analysis\")" ] } ], "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 }