{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "6377e2d8", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:01:57.433551Z", "iopub.status.busy": "2025-03-25T06:01:57.433433Z", "iopub.status.idle": "2025-03-25T06:01:57.595590Z", "shell.execute_reply": "2025-03-25T06:01:57.595193Z" } }, "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 = \"Osteoporosis\"\n", "cohort = \"GSE80614\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Osteoporosis\"\n", "in_cohort_dir = \"../../input/GEO/Osteoporosis/GSE80614\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Osteoporosis/GSE80614.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Osteoporosis/gene_data/GSE80614.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Osteoporosis/clinical_data/GSE80614.csv\"\n", "json_path = \"../../output/preprocess/Osteoporosis/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "bfc55d46", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "7aed8691", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:01:57.596855Z", "iopub.status.busy": "2025-03-25T06:01:57.596702Z", "iopub.status.idle": "2025-03-25T06:01:57.813357Z", "shell.execute_reply": "2025-03-25T06:01:57.813013Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Gene expression analyses of the differentiating hMSC into Osteoblasts and Adipocytes.\"\n", "!Series_summary\t\"Age-related skeletal degeneration in patients with osteoporosis is characterized by decreased bone mass and occurs concomitant with an increase in bone marrow adipocytes. Using microarray expression profiling with high temporal resolution, we identified gene regulatory events in early stages of osteogenic and adipogenic lineage commitment of human mesenchymal stromal cells (hMSCs). Data analysis reveal three distinct phases when cells adopt a committed expression phenotype: initiation of differentiation (0-3h, Phase I), lineage-acquisition (6-24h, Phase II) and early lineage-progression (48-96h, Phase III). Upstream regulator analysis identifies 34 transcription factors (TFs) in Phase I with a role in hMSC differentiation. Interestingly, expression levels of identified TFs did not always change and indicate additional post-transcriptional regulatory mechanisms. Functional analysis reveals that forced expression of IRF2 enhances osteogenic differentiation. Thus, IRF2 and other ‘early-responder‘ TFs may control osteogenic cell fate of MSCs and should be considered in mechanistic models that clarify bone-anabolic changes during clinical progression of osteoporosis. \"\n", "!Series_overall_design\t\"Total RNA obtained from hMSC cultured in Osteogenic or Adipogenic differentiation medium . Each samples consist of 3 pooled wells and for each timepoint we have generated 3 biologcial replicates. (for the non-differentiated cells 6 replicates)\"\n", "Sample Characteristics Dictionary:\n", "{0: ['gender: Male'], 1: ['age: 19 years', 'age: 19'], 2: ['tissue: Bone Marrow']}\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": "4d7358c2", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "10610aa2", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:01:57.814888Z", "iopub.status.busy": "2025-03-25T06:01:57.814762Z", "iopub.status.idle": "2025-03-25T06:01:57.820888Z", "shell.execute_reply": "2025-03-25T06:01:57.820560Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import os\n", "import json\n", "import pandas as pd\n", "from typing import Optional, Callable, Dict, Any\n", "\n", "# 1. Gene Expression Data Availability\n", "# From the background information, we can see this dataset is about gene expression analyses\n", "# of MSCs differentiating into osteoblasts and adipocytes, which indicates gene expression data.\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "\n", "# 2.1 Data Availability\n", "# Looking at the sample characteristics dictionary:\n", "# For trait: There's no direct mention of osteoporosis status in the sample data\n", "# For age: Found in key 1 - \"age: 19 years\", \"age: 19\" \n", "# For gender: Found in key 0 - \"gender: Male\"\n", "\n", "# Examining if these are constant features:\n", "# For trait: Not directly available in sample characteristics\n", "# For age: Value appears to be constant at 19 years\n", "# For gender: Value appears to be constant as Male\n", "\n", "trait_row = None # Osteoporosis status not directly available in sample characteristics\n", "age_row = None # Age is constant (19 years), not useful for association study\n", "gender_row = None # Gender is constant (Male), not useful for association study\n", "\n", "# 2.2 Data Type Conversion\n", "# Since all variables are not available or constant, we won't need conversion functions\n", "# but we still need to define trait conversion for the validate_and_save_cohort_info function\n", "\n", "def convert_trait(value):\n", " \"\"\"Convert trait value to binary (0 for control, 1 for case).\"\"\"\n", " if value is None:\n", " return None\n", " value = value.lower().strip()\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " if value in [\"osteoporosis\", \"case\", \"yes\", \"positive\", \"1\", \"true\"]:\n", " return 1\n", " elif value in [\"control\", \"no\", \"negative\", \"0\", \"false\", \"normal\"]:\n", " return 0\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Initial filtering and saving information\n", "is_trait_available = trait_row is not None\n", "\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", "# Since trait_row is None, we skip this step as instructed\n" ] }, { "cell_type": "markdown", "id": "b8deef5f", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "861755b7", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:01:57.822398Z", "iopub.status.busy": "2025-03-25T06:01:57.822170Z", "iopub.status.idle": "2025-03-25T06:01:58.175381Z", "shell.execute_reply": "2025-03-25T06:01:58.175014Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['ILMN_1343291', 'ILMN_1343295', 'ILMN_1651199', 'ILMN_1651209',\n", " 'ILMN_1651210', 'ILMN_1651221', 'ILMN_1651228', 'ILMN_1651229',\n", " 'ILMN_1651230', 'ILMN_1651232', 'ILMN_1651235', 'ILMN_1651236',\n", " 'ILMN_1651237', 'ILMN_1651238', 'ILMN_1651249', 'ILMN_1651253',\n", " 'ILMN_1651254', 'ILMN_1651259', 'ILMN_1651260', 'ILMN_1651262'],\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": "05ba85c8", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "f8d87e54", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:01:58.176865Z", "iopub.status.busy": "2025-03-25T06:01:58.176737Z", "iopub.status.idle": "2025-03-25T06:01:58.178742Z", "shell.execute_reply": "2025-03-25T06:01:58.178415Z" } }, "outputs": [], "source": [ "# These identifiers start with \"ILMN_\" which indicates they are Illumina probe IDs, not human gene symbols.\n", "# Illumina IDs need to be mapped to gene symbols for meaningful biological interpretation.\n", "# The format \"ILMN_\" followed by numbers is characteristic of Illumina BeadArray microarray platforms.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "08661f05", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "bbcffd85", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:01:58.180126Z", "iopub.status.busy": "2025-03-25T06:01:58.180011Z", "iopub.status.idle": "2025-03-25T06:02:05.018806Z", "shell.execute_reply": "2025-03-25T06:02:05.018421Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['ILMN_1725881', 'ILMN_1910180', 'ILMN_1804174', 'ILMN_1796063', 'ILMN_1811966'], 'nuID': ['rp13_p1x6D80lNLk3c', 'NEX0oqCV8.er4HVfU4', 'KyqQynMZxJcruyylEU', 'xXl7eXuF7sbPEp.KFI', '9ckqJrioiaej9_ajeQ'], 'Species': ['Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens'], 'Source': ['RefSeq', 'Unigene', 'RefSeq', 'RefSeq', 'RefSeq'], 'Search_Key': ['ILMN_44919', 'ILMN_127219', 'ILMN_139282', 'ILMN_5006', 'ILMN_38756'], 'Transcript': ['ILMN_44919', 'ILMN_127219', 'ILMN_139282', 'ILMN_5006', 'ILMN_38756'], 'ILMN_Gene': ['LOC23117', 'HS.575038', 'FCGR2B', 'TRIM44', 'LOC653895'], 'Source_Reference_ID': ['XM_933824.1', 'Hs.575038', 'XM_938851.1', 'NM_017583.3', 'XM_936379.1'], 'RefSeq_ID': ['XM_933824.1', nan, 'XM_938851.1', 'NM_017583.3', 'XM_936379.1'], 'Unigene_ID': [nan, 'Hs.575038', nan, nan, nan], 'Entrez_Gene_ID': [23117.0, nan, 2213.0, 54765.0, 653895.0], 'GI': [89040007.0, 10437021.0, 88952550.0, 29029528.0, 89033487.0], 'Accession': ['XM_933824.1', 'AK024680', 'XM_938851.1', 'NM_017583.3', 'XM_936379.1'], 'Symbol': ['LOC23117', nan, 'FCGR2B', 'TRIM44', 'LOC653895'], 'Protein_Product': ['XP_938917.1', nan, 'XP_943944.1', 'NP_060053.2', 'XP_941472.1'], 'Array_Address_Id': [1710221.0, 5900364.0, 2480717.0, 1300239.0, 4480719.0], 'Probe_Type': ['I', 'S', 'I', 'S', 'S'], 'Probe_Start': [122.0, 1409.0, 1643.0, 2901.0, 25.0], 'SEQUENCE': ['GGCTCCTCTTTGGGCTCCTACTGGAATTTATCAGCCATCAGTGCATCTCT', 'ACACCTTCAGGAGGGAAGCCCTTATTTCTGGGTTGAACTCCCCTTCCATG', 'TAGGGGCAATAGGCTATACGCTACAGCCTAGGTGTGTAGTAGGCCACACC', 'CCTGCCTGTCTGCCTGTGACCTGTGTACGTATTACAGGCTTTAGGACCAG', 'CTAGCAGGGAGCGGTGAGGGAGAGCGGCTGGATTTCTTGCGGGATCTGCA'], 'Chromosome': ['16', nan, nan, '11', nan], 'Probe_Chr_Orientation': ['-', nan, nan, '+', nan], 'Probe_Coordinates': ['21766363-21766363:21769901-21769949', nan, nan, '35786070-35786119', nan], 'Cytoband': ['16p12.2a', nan, '1q23.3b', '11p13a', '10q11.23b'], 'Definition': ['PREDICTED: Homo sapiens KIAA0220-like protein, transcript variant 11 (LOC23117), mRNA.', 'Homo sapiens cDNA: FLJ21027 fis, clone CAE07110', 'PREDICTED: Homo sapiens Fc fragment of IgG, low affinity IIb, receptor (CD32) (FCGR2B), mRNA.', 'Homo sapiens tripartite motif-containing 44 (TRIM44), mRNA.', 'PREDICTED: Homo sapiens similar to protein geranylgeranyltransferase type I, beta subunit (LOC653895), mRNA.'], 'Ontology_Component': [nan, nan, nan, 'intracellular [goid 5622] [evidence IEA]', nan], 'Ontology_Process': [nan, nan, nan, nan, nan], 'Ontology_Function': [nan, nan, nan, 'zinc ion binding [goid 8270] [evidence IEA]; metal ion binding [goid 46872] [evidence IEA]', nan], 'Synonyms': [nan, nan, nan, 'MGC3490; MC7; HSA249128; DIPB', nan], 'Obsolete_Probe_Id': [nan, nan, nan, 'MGC3490; MC7; HSA249128; DIPB', nan], 'GB_ACC': ['XM_933824.1', 'AK024680', 'XM_938851.1', 'NM_017583.3', 'XM_936379.1']}\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": "113b39c4", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "ba1aa337", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:02:05.020677Z", "iopub.status.busy": "2025-03-25T06:02:05.020539Z", "iopub.status.idle": "2025-03-25T06:02:05.301492Z", "shell.execute_reply": "2025-03-25T06:02:05.301117Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Shape of gene expression data after mapping: (19120, 69)\n", "First few gene symbols after mapping:\n", "Index(['A1BG', 'A1CF', 'A26A1', 'A26B1', 'A26C1B', 'A26C3', 'A2BP1', 'A2M',\n", " 'A2ML1', 'A3GALT2'],\n", " dtype='object', name='Gene')\n" ] } ], "source": [ "# 1. Determine which columns to use for mapping\n", "# From the preview, we can see:\n", "# - 'ID' column contains the probe identifiers (ILMN_XXXXXX) matching our gene expression data\n", "# - 'Symbol' column contains the gene symbols we want to map to\n", "\n", "# 2. Get a gene mapping dataframe by extracting these two columns\n", "gene_mapping = get_gene_mapping(gene_annotation, 'ID', 'Symbol')\n", "\n", "# 3. Convert probe-level measurements to gene expression data\n", "# This applies the mapping and handles many-to-many relations between probes and genes\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "\n", "# Print the shape of the mapped gene data to verify transformation\n", "print(f\"Shape of gene expression data after mapping: {gene_data.shape}\")\n", "\n", "# Display the first few gene symbols to confirm mapping worked\n", "print(\"First few gene symbols after mapping:\")\n", "print(gene_data.index[:10])\n" ] }, { "cell_type": "markdown", "id": "b719c317", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "7f734e3a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:02:05.303329Z", "iopub.status.busy": "2025-03-25T06:02:05.303173Z", "iopub.status.idle": "2025-03-25T06:02:06.199665Z", "shell.execute_reply": "2025-03-25T06:02:06.199292Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Osteoporosis/gene_data/GSE80614.csv\n", "Data does not contain Osteoporosis trait information and cannot be used for association studies\n" ] } ], "source": [ "# 1. Normalize the obtained gene data with the 'normalize_gene_symbols_in_index' function from the library.\n", "normalized_gene_data = normalize_gene_symbols_in_index(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 determined in Step 2 that trait data is not available, we should use non-final validation\n", "# Update the cohort information with is_final=False\n", "validate_and_save_cohort_info(\n", " is_final=False,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=True,\n", " is_trait_available=False\n", ")\n", "\n", "print(\"Data does not contain Osteoporosis trait information and cannot be used for association studies\")" ] } ], "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 }