{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "a7820204", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:25:18.902570Z", "iopub.status.busy": "2025-03-25T05:25:18.902408Z", "iopub.status.idle": "2025-03-25T05:25:19.071909Z", "shell.execute_reply": "2025-03-25T05:25:19.071475Z" } }, "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 = \"Glucocorticoid_Sensitivity\"\n", "cohort = \"GSE50012\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Glucocorticoid_Sensitivity\"\n", "in_cohort_dir = \"../../input/GEO/Glucocorticoid_Sensitivity/GSE50012\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Glucocorticoid_Sensitivity/GSE50012.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Glucocorticoid_Sensitivity/gene_data/GSE50012.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Glucocorticoid_Sensitivity/clinical_data/GSE50012.csv\"\n", "json_path = \"../../output/preprocess/Glucocorticoid_Sensitivity/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "a5aba0f6", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "5bc4aa1f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:25:19.073509Z", "iopub.status.busy": "2025-03-25T05:25:19.073355Z", "iopub.status.idle": "2025-03-25T05:25:19.327189Z", "shell.execute_reply": "2025-03-25T05:25:19.326704Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Comparison of cellular and transcriptional responses to 1,25-dihydroxyvitamin D3 and glucocorticoids in peripheral blood mononuclear cells\"\n", "!Series_summary\t\"Glucocorticoids (GC) and 1,25-dihydroxyvitamin D3 (1,25(OH)2 D3) are steroid hormones with anti-inflammatory properties with enhanced effects when combined. We previously showed that transcriptional response to GCs was correlated with inter-individual and inter-ethnic cellular response. Here, we profiled cellular and transcriptional responses to 1,25(OH)2 D3 from the same donors. We studied cellular response to combined treatment with GCs and 1,25(OH)2 D3 in a subset of individuals least responsive to GCs. We found that combination treatment had significantly greater inhibition of proliferation than with either steroid hormone alone. Overlapping differentially expressed (DE) genes between the two hormones were enriched for adaptive and innate immune processes. Non-overlapping differentially expressed genes with 1,25(OH)2 D3 treatment were enriched for pathways involving the electron transport chain, while with GC treatment, non-overlapping genes were enriched for RNA-related processes. These results suggest that 1,25(OH)2 D3 enhances GC anti-inflammatory properties through a number of shared and non-shared transcriptionally-mediated pathways.\"\n", "!Series_overall_design\t\"Total RNA was obtained from aliquots of peripheral blood mononuclear cells treated with 1,25-dihydroxyvitamin D3 (1,25(OH)2 D3) for 8 and 24 hours. These data were analyzed together with previously published data from expression analysis of PBMC aliquots collected in parallel to these and treated with dexamethasone or vehicle (EtOH).\"\n", "Sample Characteristics Dictionary:\n", "{0: ['cell type: peripheral blood mononuclear cells', 'population: African-American', 'population: European-American'], 1: ['population: African-American', 'population: European-American', 'treatment: 1,25-dihydroxyvitamin D'], 2: ['treatment: dexamethasone', 'treatment: vehicle (EtOH)', 'in vitro lymphocyte vitd sensitivity (lgs - %inhibition by vitd): 18.89', 'in vitro lymphocyte vitd sensitivity (lgs - %inhibition by vitd): 29.99', 'in vitro lymphocyte vitd sensitivity (lgs - %inhibition by vitd): 22.84', 'in vitro lymphocyte vitd sensitivity (lgs - %inhibition by vitd): 59.62', 'in vitro lymphocyte vitd sensitivity (lgs - %inhibition by vitd): 47.72', 'in vitro lymphocyte vitd sensitivity (lgs - %inhibition by vitd): 3.43', 'in vitro lymphocyte vitd sensitivity (lgs - %inhibition by vitd): 11.97', 'in vitro lymphocyte vitd sensitivity (lgs - %inhibition by vitd): 31.77', 'in vitro lymphocyte vitd sensitivity (lgs - %inhibition by vitd): 84.49', 'in vitro lymphocyte vitd sensitivity (lgs - %inhibition by vitd): 27.58', 'in vitro lymphocyte vitd sensitivity (lgs - %inhibition by vitd): -48.98', 'in vitro lymphocyte vitd sensitivity (lgs - %inhibition by vitd): 39.98'], 3: ['in vitro lymphocyte gc sensitivity (lgs - %inhibition by dex): 89.43486', 'in vitro lymphocyte gc sensitivity (lgs - %inhibition by dex): 95.88507', 'in vitro lymphocyte gc sensitivity (lgs - %inhibition by dex): 95.22036', 'in vitro lymphocyte gc sensitivity (lgs - %inhibition by dex): 92.86704', 'in vitro lymphocyte gc sensitivity (lgs - %inhibition by dex): 93.71633', 'in vitro lymphocyte gc sensitivity (lgs - %inhibition by dex): 96.76962', 'in vitro lymphocyte gc sensitivity (lgs - %inhibition by dex): 88.55031', 'in vitro lymphocyte gc sensitivity (lgs - %inhibition by dex): 90.09957', 'in vitro lymphocyte gc sensitivity (lgs - %inhibition by dex): 94.17097', 'in vitro lymphocyte gc sensitivity (lgs - %inhibition by dex): 86.97089', 'in vitro lymphocyte gc sensitivity (lgs - %inhibition by dex): 98.34904', 'in vitro lymphocyte gc sensitivity (lgs - %inhibition by dex): 91.14896', 'duration of treatment (hours): 8', 'duration of treatment (hours): 24'], 4: ['duration of treatment (hours): 8', 'duration of treatment (hours): 24', 'gender: female', 'gender: male'], 5: ['gender: female', 'gender: male', 'age (years): 44.15', 'age (years): 24.72', 'age (years): 32.38', 'age (years): 20.38', 'age (years): 21.24', 'age (years): 22.54', 'age (years): 26.14', 'age (years): 21.56', 'age (years): 21.99', 'age (years): 26.77', 'age (years): 23.59', 'age (years): 23.48'], 6: ['age (years): 44.15342', 'age (years): 24.72329', 'age (years): 32.37808', 'age (years): 20.38082', 'age (years): 21.2411', 'age (years): 22.54247', 'age (years): 26.13973', 'age (years): 21.5616', 'age (years): 21.9863', 'age (years): 26.76712', 'age (years): 23.59452', 'age (years): 23.47945', nan]}\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": "c387f908", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "994012b8", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:25:19.328636Z", "iopub.status.busy": "2025-03-25T05:25:19.328515Z", "iopub.status.idle": "2025-03-25T05:25:19.344200Z", "shell.execute_reply": "2025-03-25T05:25:19.343885Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of selected clinical features:\n", "{'GSM832137': [89.43486, nan, nan], 'GSM832138': [89.43486, nan, nan], 'GSM832139': [89.43486, nan, nan], 'GSM832140': [89.43486, nan, nan], 'GSM832141': [95.88507, nan, nan], 'GSM832142': [95.88507, nan, nan], 'GSM832143': [95.88507, nan, nan], 'GSM832144': [95.88507, nan, nan], 'GSM832145': [95.22036, nan, nan], 'GSM832146': [95.22036, nan, nan], 'GSM832147': [95.22036, nan, nan], 'GSM832148': [95.22036, nan, nan], 'GSM832149': [92.86704, nan, nan], 'GSM832150': [92.86704, nan, nan], 'GSM832151': [92.86704, nan, nan], 'GSM832152': [92.86704, nan, nan], 'GSM832153': [93.71633, nan, nan], 'GSM832154': [93.71633, nan, nan], 'GSM832155': [93.71633, nan, nan], 'GSM832156': [93.71633, nan, nan], 'GSM832157': [96.76962, nan, nan], 'GSM832158': [96.76962, nan, nan], 'GSM832159': [96.76962, nan, nan], 'GSM832160': [96.76962, nan, nan], 'GSM832161': [88.55031, nan, nan], 'GSM832162': [88.55031, nan, nan], 'GSM832163': [88.55031, nan, nan], 'GSM832164': [88.55031, nan, nan], 'GSM832165': [90.09957, nan, nan], 'GSM832166': [90.09957, nan, nan], 'GSM832167': [90.09957, nan, nan], 'GSM832168': [90.09957, nan, nan], 'GSM832169': [94.17097, nan, nan], 'GSM832170': [94.17097, nan, nan], 'GSM832171': [94.17097, nan, nan], 'GSM832172': [94.17097, nan, nan], 'GSM832173': [86.97089, nan, nan], 'GSM832174': [86.97089, nan, nan], 'GSM832175': [86.97089, nan, nan], 'GSM832176': [86.97089, nan, nan], 'GSM832177': [98.34904, nan, nan], 'GSM832178': [98.34904, nan, nan], 'GSM832179': [98.34904, nan, nan], 'GSM832180': [98.34904, nan, nan], 'GSM832181': [91.14896, nan, nan], 'GSM832182': [91.14896, nan, nan], 'GSM832183': [91.14896, nan, nan], 'GSM832184': [91.14896, nan, nan], 'GSM1212354': [8.0, 44.15, 0.0], 'GSM1212355': [24.0, 44.15, 0.0], 'GSM1212356': [8.0, 24.72, 0.0], 'GSM1212357': [24.0, 24.72, 0.0], 'GSM1212358': [8.0, 32.38, 1.0], 'GSM1212359': [24.0, 32.38, 1.0], 'GSM1212360': [8.0, 20.38, 0.0], 'GSM1212361': [24.0, 20.38, 0.0], 'GSM1212362': [8.0, 21.24, 0.0], 'GSM1212363': [24.0, 21.24, 0.0], 'GSM1212364': [8.0, 22.54, 0.0], 'GSM1212365': [24.0, 22.54, 0.0], 'GSM1212366': [24.0, 26.14, 1.0], 'GSM1212367': [8.0, 26.14, 1.0], 'GSM1212368': [24.0, 21.56, 1.0], 'GSM1212369': [8.0, 21.56, 1.0], 'GSM1212370': [24.0, 21.99, 0.0], 'GSM1212371': [8.0, 21.99, 0.0], 'GSM1212372': [24.0, 26.77, 1.0], 'GSM1212373': [8.0, 26.77, 1.0], 'GSM1212374': [24.0, 23.59, 1.0], 'GSM1212375': [8.0, 23.59, 1.0], 'GSM1212376': [24.0, 23.48, 1.0], 'GSM1212377': [8.0, 23.48, 1.0]}\n", "Clinical data saved to ../../output/preprocess/Glucocorticoid_Sensitivity/clinical_data/GSE50012.csv\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "import os\n", "\n", "# 1. Gene Expression Data Availability\n", "# Based on the series summary, it mentions transcriptional responses and RNA analysis,\n", "# which suggests gene expression data is available\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Trait row identification\n", "# For Glucocorticoid_Sensitivity, looking at row 3 which has \"in vitro lymphocyte gc sensitivity\" \n", "# This is the appropriate measurement for our trait\n", "trait_row = 3\n", "\n", "# Age data is available in row 5\n", "age_row = 5\n", "\n", "# Gender data is available in row 4\n", "gender_row = 4\n", "\n", "# 2.2 Data Type Conversion functions\n", "def convert_trait(value):\n", " \"\"\"Convert glucocorticoid sensitivity values to continuous values.\"\"\"\n", " if pd.isna(value):\n", " return None\n", " \n", " # Extract the value after the colon\n", " if \":\" in value:\n", " value = value.split(\":\")[1].strip()\n", " \n", " # Extract the numeric part from the gc sensitivity value\n", " if \"in vitro lymphocyte gc sensitivity (lgs - %inhibition by dex)\" in value:\n", " try:\n", " return float(value.replace(\"in vitro lymphocyte gc sensitivity (lgs - %inhibition by dex)\", \"\").strip())\n", " except:\n", " return None\n", " \n", " try:\n", " return float(value)\n", " except:\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age values to continuous values.\"\"\"\n", " if pd.isna(value):\n", " return None\n", " \n", " # Extract the value after the colon\n", " if \":\" in value:\n", " value = value.split(\":\")[1].strip()\n", " \n", " # Extract the numeric part from the age value\n", " if \"age (years)\" in value:\n", " try:\n", " return float(value.replace(\"age (years)\", \"\").strip())\n", " except:\n", " return None\n", " \n", " try:\n", " return float(value)\n", " except:\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender values to binary (0 for female, 1 for male).\"\"\"\n", " if pd.isna(value):\n", " return None\n", " \n", " # Extract the value after the colon\n", " if \":\" in value:\n", " value = value.split(\":\")[1].strip()\n", " \n", " # Convert gender to binary\n", " value = value.lower()\n", " if \"female\" in value:\n", " return 0\n", " elif \"male\" in value:\n", " return 1\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Determine trait data availability\n", "is_trait_available = trait_row is not None\n", "\n", "# Initial filtering and saving metadata\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", "# Check if clinical data is available (trait_row is not None)\n", "if trait_row is not None:\n", " try:\n", " # Extract clinical features from the clinical_data DataFrame which should be available in the environment\n", " selected_clinical_df = geo_select_clinical_features(\n", " clinical_df=clinical_data, # Use the clinical_data variable that should be available\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\n", " preview = preview_df(selected_clinical_df)\n", " print(\"Preview of selected clinical features:\")\n", " print(preview)\n", " \n", " # Create the output directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " \n", " # Save the dataframe to CSV\n", " selected_clinical_df.to_csv(out_clinical_data_file)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n", " except Exception as e:\n", " print(f\"Error extracting clinical features: {e}\")\n", "else:\n", " print(\"No clinical data available (trait_row is None)\")\n" ] }, { "cell_type": "markdown", "id": "6ec09f9a", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "063a2c61", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:25:19.345377Z", "iopub.status.busy": "2025-03-25T05:25:19.345266Z", "iopub.status.idle": "2025-03-25T05:25:19.774908Z", "shell.execute_reply": "2025-03-25T05:25:19.774558Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found data marker at line 70\n", "Header line: \"ID_REF\"\t\"GSM832137\"\t\"GSM832138\"\t\"GSM832139\"\t\"GSM832140\"\t\"GSM832141\"\t\"GSM832142\"\t\"GSM832143\"\t\"GSM832144\"\t\"GSM832145\"\t\"GSM832146\"\t\"GSM832147\"\t\"GSM832148\"\t\"GSM832149\"\t\"GSM832150\"\t\"GSM832151\"\t\"GSM832152\"\t\"GSM832153\"\t\"GSM832154\"\t\"GSM832155\"\t\"GSM832156\"\t\"GSM832157\"\t\"GSM832158\"\t\"GSM832159\"\t\"GSM832160\"\t\"GSM832161\"\t\"GSM832162\"\t\"GSM832163\"\t\"GSM832164\"\t\"GSM832165\"\t\"GSM832166\"\t\"GSM832167\"\t\"GSM832168\"\t\"GSM832169\"\t\"GSM832170\"\t\"GSM832171\"\t\"GSM832172\"\t\"GSM832173\"\t\"GSM832174\"\t\"GSM832175\"\t\"GSM832176\"\t\"GSM832177\"\t\"GSM832178\"\t\"GSM832179\"\t\"GSM832180\"\t\"GSM832181\"\t\"GSM832182\"\t\"GSM832183\"\t\"GSM832184\"\t\"GSM1212354\"\t\"GSM1212355\"\t\"GSM1212356\"\t\"GSM1212357\"\t\"GSM1212358\"\t\"GSM1212359\"\t\"GSM1212360\"\t\"GSM1212361\"\t\"GSM1212362\"\t\"GSM1212363\"\t\"GSM1212364\"\t\"GSM1212365\"\t\"GSM1212366\"\t\"GSM1212367\"\t\"GSM1212368\"\t\"GSM1212369\"\t\"GSM1212370\"\t\"GSM1212371\"\t\"GSM1212372\"\t\"GSM1212373\"\t\"GSM1212374\"\t\"GSM1212375\"\t\"GSM1212376\"\t\"GSM1212377\"\n", "First data line: \"ILMN_1343291\"\t14.12073024\t14.1847953\t14.3271103\t14.21074679\t14.35649097\t14.21573196\t14.25949372\t14.26541254\t14.36153392\t14.25490712\t14.28494604\t14.21327393\t14.37099787\t14.37099787\t14.32494472\t14.32079848\t14.26699913\t14.08661628\t14.33650015\t14.33877929\t14.24410318\t14.21573196\t14.34573164\t14.38961689\t14.32959504\t14.31869455\t14.37099787\t14.4243792\t14.31077135\t14.24773914\t14.20496391\t14.29628828\t14.27520624\t14.16802087\t14.22209016\t14.32288942\t14.32079848\t14.29628828\t14.27674846\t14.31077135\t14.20610208\t14.11111632\t14.10822775\t14.40216307\t14.25657841\t14.24534098\t14.21675287\t14.21074679\t14.2520022435986\t14.2024895096374\t14.2849460399787\t14.3937657950042\t14.2767484645838\t14.2317917064486\t14.1943088752250\t14.3979999390661\t14.2654125383898\t14.30720559192\t14.3822498905462\t14.2931971608562\t14.2733919812744\t14.3822498905462\t14.2931971608562\t14.3589008750322\t14.2453409774463\t14.2882495288221\t14.2752062410866\t14.3128310879575\t14.1943088752250\t14.2372610115893\t14.3615339156902\t14.1838257701293\n" ] }, { "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. Get the file paths for the SOFT file and matrix file\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. First, let's examine the structure of the matrix file to understand its format\n", "import gzip\n", "\n", "# Peek at the first few lines of the file to understand its structure\n", "with gzip.open(matrix_file, 'rt') as file:\n", " # Read first 100 lines to find the header structure\n", " for i, line in enumerate(file):\n", " if '!series_matrix_table_begin' in line:\n", " print(f\"Found data marker at line {i}\")\n", " # Read the next line which should be the header\n", " header_line = next(file)\n", " print(f\"Header line: {header_line.strip()}\")\n", " # And the first data line\n", " first_data_line = next(file)\n", " print(f\"First data line: {first_data_line.strip()}\")\n", " break\n", " if i > 100: # Limit search to first 100 lines\n", " print(\"Matrix table marker not found in first 100 lines\")\n", " break\n", "\n", "# 3. Now try to get the genetic data with better error handling\n", "try:\n", " gene_data = get_genetic_data(matrix_file)\n", " print(gene_data.index[:20])\n", "except KeyError as e:\n", " print(f\"KeyError: {e}\")\n", " \n", " # Alternative approach: manually extract the data\n", " print(\"\\nTrying alternative approach to read the gene data:\")\n", " with gzip.open(matrix_file, 'rt') as file:\n", " # Find the start of the data\n", " for line in file:\n", " if '!series_matrix_table_begin' in line:\n", " break\n", " \n", " # Read the headers and data\n", " import pandas as pd\n", " df = pd.read_csv(file, sep='\\t', index_col=0)\n", " print(f\"Column names: {df.columns[:5]}\")\n", " print(f\"First 20 row IDs: {df.index[:20]}\")\n", " gene_data = df\n" ] }, { "cell_type": "markdown", "id": "701d3857", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "58c30aa5", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:25:19.776232Z", "iopub.status.busy": "2025-03-25T05:25:19.776106Z", "iopub.status.idle": "2025-03-25T05:25:19.778298Z", "shell.execute_reply": "2025-03-25T05:25:19.777990Z" } }, "outputs": [], "source": [ "# These are Illumina probe IDs (starting with \"ILMN_\"), not human gene symbols\n", "# Illumina probe IDs need to be mapped to official gene symbols for analysis\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "ebb1b182", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "126f9261", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:25:19.779507Z", "iopub.status.busy": "2025-03-25T05:25:19.779400Z", "iopub.status.idle": "2025-03-25T05:25:20.708672Z", "shell.execute_reply": "2025-03-25T05:25:20.708326Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Examining SOFT file structure:\n", "Line 0: ^DATABASE = GeoMiame\n", "Line 1: !Database_name = Gene Expression Omnibus (GEO)\n", "Line 2: !Database_institute = NCBI NLM NIH\n", "Line 3: !Database_web_link = http://www.ncbi.nlm.nih.gov/geo\n", "Line 4: !Database_email = geo@ncbi.nlm.nih.gov\n", "Line 5: ^SERIES = GSE50012\n", "Line 6: !Series_title = Comparison of cellular and transcriptional responses to 1,25-dihydroxyvitamin D3 and glucocorticoids in peripheral blood mononuclear cells\n", "Line 7: !Series_geo_accession = GSE50012\n", "Line 8: !Series_status = Public on Aug 21 2013\n", "Line 9: !Series_submission_date = Aug 20 2013\n", "Line 10: !Series_last_update_date = Aug 13 2018\n", "Line 11: !Series_pubmed_id = 24116131\n", "Line 12: !Series_pubmed_id = 24550213\n", "Line 13: !Series_summary = Glucocorticoids (GC) and 1,25-dihydroxyvitamin D3 (1,25(OH)2 D3) are steroid hormones with anti-inflammatory properties with enhanced effects when combined. We previously showed that transcriptional response to GCs was correlated with inter-individual and inter-ethnic cellular response. Here, we profiled cellular and transcriptional responses to 1,25(OH)2 D3 from the same donors. We studied cellular response to combined treatment with GCs and 1,25(OH)2 D3 in a subset of individuals least responsive to GCs. We found that combination treatment had significantly greater inhibition of proliferation than with either steroid hormone alone. Overlapping differentially expressed (DE) genes between the two hormones were enriched for adaptive and innate immune processes. Non-overlapping differentially expressed genes with 1,25(OH)2 D3 treatment were enriched for pathways involving the electron transport chain, while with GC treatment, non-overlapping genes were enriched for RNA-related processes. These results suggest that 1,25(OH)2 D3 enhances GC anti-inflammatory properties through a number of shared and non-shared transcriptionally-mediated pathways.\n", "Line 14: !Series_overall_design = Total RNA was obtained from aliquots of peripheral blood mononuclear cells treated with 1,25-dihydroxyvitamin D3 (1,25(OH)2 D3) for 8 and 24 hours. These data were analyzed together with previously published data from expression analysis of PBMC aliquots collected in parallel to these and treated with dexamethasone or vehicle (EtOH).\n", "Line 15: !Series_type = Expression profiling by array\n", "Line 16: !Series_contributor = Sonia,S,Kupfer\n", "Line 17: !Series_contributor = Joseph,C,Maranville\n", "Line 18: !Series_contributor = Shaneen,S,Baxter\n", "Line 19: !Series_contributor = Yong,,Huang\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene annotation preview:\n", "{'ID': ['ILMN_1343048', 'ILMN_1343049', 'ILMN_1343050', 'ILMN_1343052', 'ILMN_1343059'], 'Species': [nan, nan, nan, nan, nan], 'Source': [nan, nan, nan, nan, nan], 'Search_Key': [nan, nan, nan, nan, nan], 'Transcript': [nan, nan, nan, nan, nan], 'ILMN_Gene': [nan, nan, nan, nan, nan], 'Source_Reference_ID': [nan, nan, nan, nan, nan], 'RefSeq_ID': [nan, nan, nan, nan, nan], 'Unigene_ID': [nan, nan, nan, nan, nan], 'Entrez_Gene_ID': [nan, nan, nan, nan, nan], 'GI': [nan, nan, nan, nan, nan], 'Accession': [nan, nan, nan, nan, nan], 'Symbol': ['phage_lambda_genome', 'phage_lambda_genome', 'phage_lambda_genome:low', 'phage_lambda_genome:low', 'thrB'], 'Protein_Product': [nan, nan, nan, nan, 'thrB'], 'Probe_Id': [nan, nan, nan, nan, nan], 'Array_Address_Id': [5090180, 6510136, 7560739, 1450438, 1240647], 'Probe_Type': [nan, nan, nan, nan, nan], 'Probe_Start': [nan, nan, nan, nan, nan], 'SEQUENCE': ['GAATAAAGAACAATCTGCTGATGATCCCTCCGTGGATCTGATTCGTGTAA', 'CCATGTGATACGAGGGCGCGTAGTTTGCATTATCGTTTTTATCGTTTCAA', 'CCGACAGATGTATGTAAGGCCAACGTGCTCAAATCTTCATACAGAAAGAT', 'TCTGTCACTGTCAGGAAAGTGGTAAAACTGCAACTCAATTACTGCAATGC', 'CTTGTGCCTGAGCTGTCAAAAGTAGAGCACGTCGCCGAGATGAAGGGCGC'], 'Chromosome': [nan, nan, nan, nan, nan], 'Probe_Chr_Orientation': [nan, nan, nan, nan, nan], 'Probe_Coordinates': [nan, nan, nan, nan, nan], 'Cytoband': [nan, nan, nan, nan, nan], 'Definition': [nan, nan, nan, nan, nan], 'Ontology_Component': [nan, nan, nan, nan, nan], 'Ontology_Process': [nan, nan, nan, nan, nan], 'Ontology_Function': [nan, nan, nan, nan, nan], 'Synonyms': [nan, nan, nan, nan, nan], 'Obsolete_Probe_Id': [nan, nan, nan, nan, nan], 'GB_ACC': [nan, nan, nan, nan, nan]}\n" ] } ], "source": [ "# 1. Let's first examine the structure of the SOFT file before trying to parse it\n", "import gzip\n", "\n", "# Look at the first few lines of the SOFT file to understand its structure\n", "print(\"Examining SOFT file structure:\")\n", "try:\n", " with gzip.open(soft_file, 'rt') as file:\n", " # Read first 20 lines to understand the file structure\n", " for i, line in enumerate(file):\n", " if i < 20:\n", " print(f\"Line {i}: {line.strip()}\")\n", " else:\n", " break\n", "except Exception as e:\n", " print(f\"Error reading SOFT file: {e}\")\n", "\n", "# 2. Now let's try a more robust approach to extract the gene annotation\n", "# Instead of using the library function which failed, we'll implement a custom approach\n", "try:\n", " # First, look for the platform section which contains gene annotation\n", " platform_data = []\n", " with gzip.open(soft_file, 'rt') as file:\n", " in_platform_section = False\n", " for line in file:\n", " if line.startswith('^PLATFORM'):\n", " in_platform_section = True\n", " continue\n", " if in_platform_section and line.startswith('!platform_table_begin'):\n", " # Next line should be the header\n", " header = next(file).strip()\n", " platform_data.append(header)\n", " # Read until the end of the platform table\n", " for table_line in file:\n", " if table_line.startswith('!platform_table_end'):\n", " break\n", " platform_data.append(table_line.strip())\n", " break\n", " \n", " # If we found platform data, convert it to a DataFrame\n", " if platform_data:\n", " import pandas as pd\n", " import io\n", " platform_text = '\\n'.join(platform_data)\n", " gene_annotation = pd.read_csv(io.StringIO(platform_text), delimiter='\\t', \n", " low_memory=False, on_bad_lines='skip')\n", " print(\"\\nGene annotation preview:\")\n", " print(preview_df(gene_annotation))\n", " else:\n", " print(\"Could not find platform table in SOFT file\")\n", " \n", " # Try an alternative approach - extract mapping from other sections\n", " with gzip.open(soft_file, 'rt') as file:\n", " for line in file:\n", " if 'ANNOTATION information' in line or 'annotation information' in line:\n", " print(f\"Found annotation information: {line.strip()}\")\n", " if line.startswith('!Platform_title') or line.startswith('!platform_title'):\n", " print(f\"Platform title: {line.strip()}\")\n", " \n", "except Exception as e:\n", " print(f\"Error processing gene annotation: {e}\")\n" ] }, { "cell_type": "markdown", "id": "c32dd89b", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "8bb71788", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:25:20.710039Z", "iopub.status.busy": "2025-03-25T05:25:20.709908Z", "iopub.status.idle": "2025-03-25T05:25:21.950076Z", "shell.execute_reply": "2025-03-25T05:25:21.949703Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene mapping dataframe - first 5 rows:\n", " ID Gene\n", "0 ILMN_1343048 phage_lambda_genome\n", "1 ILMN_1343049 phage_lambda_genome\n", "2 ILMN_1343050 phage_lambda_genome:low\n", "3 ILMN_1343052 phage_lambda_genome:low\n", "4 ILMN_1343059 thrB\n", "Total number of mappings: 44837\n", "Gene expression data shape after mapping: (21372, 72)\n", "First 5 gene symbols after mapping:\n", "Index(['A1BG', 'A1CF', 'A26C3', 'A2BP1', 'A2LD1'], dtype='object', name='Gene')\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Glucocorticoid_Sensitivity/gene_data/GSE50012.csv\n" ] } ], "source": [ "# 1. Identify the keys for gene identifiers and gene symbols in the gene annotation\n", "# From the preview, we can see:\n", "# - 'ID' column contains the Illumina probe IDs (starting with ILMN_)\n", "# - 'Symbol' column contains the gene symbols\n", "\n", "# 2. Get the gene mapping dataframe\n", "prob_col = 'ID' # Column containing probe IDs\n", "gene_col = 'Symbol' # Column containing gene symbols\n", "\n", "# Extract the mapping between probe IDs and gene symbols\n", "mapping_df = get_gene_mapping(gene_annotation, prob_col, gene_col)\n", "print(f\"Gene mapping dataframe - first 5 rows:\")\n", "print(mapping_df.head())\n", "print(f\"Total number of mappings: {len(mapping_df)}\")\n", "\n", "# 3. Convert probe-level measurements to gene expression data\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "print(f\"Gene expression data shape after mapping: {gene_data.shape}\")\n", "print(f\"First 5 gene symbols after mapping:\")\n", "print(gene_data.index[:5])\n", "\n", "# Save the gene expression data to the output file\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "gene_data.to_csv(out_gene_data_file)\n", "print(f\"Gene expression data saved to {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "d63d10ce", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "e2caf44e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:25:21.951509Z", "iopub.status.busy": "2025-03-25T05:25:21.951385Z", "iopub.status.idle": "2025-03-25T05:25:35.178531Z", "shell.execute_reply": "2025-03-25T05:25:35.178100Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape after normalization: (20259, 72)\n", "Sample gene symbols after normalization: ['A1BG', 'A1BG-AS1', 'A1CF', 'A2M', 'A2ML1', 'A3GALT2', 'A4GALT', 'A4GNT', 'AAA1', 'AAAS']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data saved to ../../output/preprocess/Glucocorticoid_Sensitivity/gene_data/GSE50012.csv\n", "Clinical data shape: (3, 72)\n", "Clinical data preview:\n", " GSM832137 GSM832138 GSM832139 GSM832140 \\\n", "Glucocorticoid_Sensitivity 89.43486 89.43486 89.43486 89.43486 \n", "Age NaN NaN NaN NaN \n", "Gender NaN NaN NaN NaN \n", "\n", " GSM832141 GSM832142 GSM832143 GSM832144 \\\n", "Glucocorticoid_Sensitivity 95.88507 95.88507 95.88507 95.88507 \n", "Age NaN NaN NaN NaN \n", "Gender NaN NaN NaN NaN \n", "\n", " GSM832145 GSM832146 ... GSM1212368 GSM1212369 \\\n", "Glucocorticoid_Sensitivity 95.22036 95.22036 ... 24.00 8.00 \n", "Age NaN NaN ... 21.56 21.56 \n", "Gender NaN NaN ... 1.00 1.00 \n", "\n", " GSM1212370 GSM1212371 GSM1212372 GSM1212373 \\\n", "Glucocorticoid_Sensitivity 24.00 8.00 24.00 8.00 \n", "Age 21.99 21.99 26.77 26.77 \n", "Gender 0.00 0.00 1.00 1.00 \n", "\n", " GSM1212374 GSM1212375 GSM1212376 GSM1212377 \n", "Glucocorticoid_Sensitivity 24.00 8.00 24.00 8.00 \n", "Age 23.59 23.59 23.48 23.48 \n", "Gender 1.00 1.00 1.00 1.00 \n", "\n", "[3 rows x 72 columns]\n", "Gene samples: 72\n", "Clinical samples: 72\n", "Common samples: 72\n", "Linked data shape: (72, 20262)\n", "Linked data preview (first 5 rows, first 5 columns):\n", " Glucocorticoid_Sensitivity Age Gender A1BG A1BG-AS1\n", "GSM832137 89.43486 NaN NaN 15.956246 7.895359\n", "GSM832138 89.43486 NaN NaN 15.847209 7.873267\n", "GSM832139 89.43486 NaN NaN 15.781695 7.835743\n", "GSM832140 89.43486 NaN NaN 15.764754 7.877882\n", "GSM832141 95.88507 NaN NaN 15.795053 7.896896\n", "\n", "Missing values before handling:\n", " Trait (Glucocorticoid_Sensitivity) missing: 0 out of 72\n", " Genes with >20% missing: 0\n", " Samples with >5% missing genes: 0\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (72, 20262)\n", "Quartiles for 'Glucocorticoid_Sensitivity':\n", " 25%: 24.0\n", " 50% (Median): 89.767215\n", " 75%: 94.17097\n", "Min: 8.0\n", "Max: 98.34904\n", "The distribution of the feature 'Glucocorticoid_Sensitivity' in this dataset is fine.\n", "\n", "Quartiles for 'Age':\n", " 25%: 25.745\n", " 50% (Median): 25.745\n", " 75%: 25.745\n", "Min: 20.38\n", "Max: 44.15\n", "The distribution of the feature 'Age' in this dataset is fine.\n", "\n", "For the feature 'Gender', the least common label is '1.0' with 12 occurrences. This represents 16.67% of the dataset.\n", "The distribution of the feature 'Gender' in this dataset is fine.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Glucocorticoid_Sensitivity/GSE50012.csv\n" ] } ], "source": [ "# 1. Normalize gene symbols in the obtained gene expression data\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Gene data shape after normalization: {normalized_gene_data.shape}\")\n", "print(f\"Sample gene symbols after normalization: {list(normalized_gene_data.index[:10])}\")\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\"Gene data saved to {out_gene_data_file}\")\n", "\n", "# 2. Load the previously saved clinical data\n", "clinical_data = pd.read_csv(out_clinical_data_file, index_col=0)\n", "print(f\"Clinical data shape: {clinical_data.shape}\")\n", "print(\"Clinical data preview:\")\n", "print(clinical_data.head())\n", "\n", "# 3. Link clinical and genetic data using proper sample IDs\n", "# First, transpose gene expression data to have samples as rows\n", "gene_data_t = normalized_gene_data.T\n", "\n", "# The clinical data should have samples as columns - verify sample IDs match\n", "gene_samples = set(gene_data_t.index)\n", "clinical_samples = set(clinical_data.columns)\n", "common_samples = gene_samples.intersection(clinical_samples)\n", "\n", "print(f\"Gene samples: {len(gene_samples)}\")\n", "print(f\"Clinical samples: {len(clinical_samples)}\")\n", "print(f\"Common samples: {len(common_samples)}\")\n", "\n", "# Use the geo_link_clinical_genetic_data function to properly link the data\n", "linked_data = geo_link_clinical_genetic_data(clinical_data, normalized_gene_data)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "print(\"Linked data preview (first 5 rows, first 5 columns):\")\n", "if linked_data.shape[1] >= 5:\n", " print(linked_data.iloc[:5, :5])\n", "else:\n", " print(linked_data.head())\n", "\n", "# 4. Handle missing values\n", "print(\"\\nMissing values before handling:\")\n", "print(f\" Trait ({trait}) missing: {linked_data[trait].isna().sum()} out of {len(linked_data)}\")\n", "gene_cols = [col for col in linked_data.columns if col not in [trait, 'Age', 'Gender']]\n", "if gene_cols:\n", " missing_genes_pct = linked_data[gene_cols].isna().mean()\n", " genes_with_high_missing = sum(missing_genes_pct > 0.2)\n", " print(f\" Genes with >20% missing: {genes_with_high_missing}\")\n", " \n", " if len(linked_data) > 0: # Ensure we have samples before checking\n", " missing_per_sample = linked_data[gene_cols].isna().mean(axis=1)\n", " samples_with_high_missing = sum(missing_per_sample > 0.05)\n", " print(f\" Samples with >5% missing genes: {samples_with_high_missing}\")\n", "\n", "# Handle missing values\n", "cleaned_data = handle_missing_values(linked_data, trait)\n", "print(f\"Data shape after handling missing values: {cleaned_data.shape}\")\n", "\n", "# 5. Evaluate bias in trait and demographic features\n", "trait_biased, cleaned_data = judge_and_remove_biased_features(cleaned_data, trait)\n", "\n", "# 6. Final validation and save\n", "note = \"Dataset contains gene expression data from glucocorticoid sensitivity studies. \"\n", "if 'Age' in cleaned_data.columns:\n", " note += \"Age data is available. \"\n", "if 'Gender' in cleaned_data.columns:\n", " note += \"Gender data is available. \"\n", "\n", "is_gene_available = len(normalized_gene_data) > 0\n", "is_usable = validate_and_save_cohort_info(\n", " is_final=True, \n", " cohort=cohort, \n", " info_path=json_path, \n", " is_gene_available=is_gene_available, \n", " is_trait_available=True, \n", " is_biased=trait_biased, \n", " df=cleaned_data,\n", " note=note\n", ")\n", "\n", "# 7. Save if usable\n", "if is_usable and len(cleaned_data) > 0:\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " cleaned_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", "else:\n", " print(\"Data was determined to be unusable or empty and was 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 }