{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "91fe87dd", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:42:53.381822Z", "iopub.status.busy": "2025-03-25T06:42:53.381706Z", "iopub.status.idle": "2025-03-25T06:42:53.549215Z", "shell.execute_reply": "2025-03-25T06:42:53.548814Z" } }, "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 = \"Atherosclerosis\"\n", "cohort = \"GSE123088\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Atherosclerosis\"\n", "in_cohort_dir = \"../../input/GEO/Atherosclerosis/GSE123088\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Atherosclerosis/GSE123088.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Atherosclerosis/gene_data/GSE123088.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Atherosclerosis/clinical_data/GSE123088.csv\"\n", "json_path = \"../../output/preprocess/Atherosclerosis/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "4f56bf40", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "bd783335", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:42:53.550631Z", "iopub.status.busy": "2025-03-25T06:42:53.550474Z", "iopub.status.idle": "2025-03-25T06:42:53.836236Z", "shell.execute_reply": "2025-03-25T06:42:53.835876Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"A validated single-cell-based strategy to identify diagnostic and therapeutic targets in complex diseases\"\n", "!Series_summary\t\"This SuperSeries is composed of the SubSeries listed below.\"\n", "!Series_overall_design\t\"Refer to individual Series\"\n", "Sample Characteristics Dictionary:\n", "{0: ['cell type: CD4+ T cells'], 1: ['primary diagnosis: ASTHMA', 'primary diagnosis: ATHEROSCLEROSIS', 'primary diagnosis: BREAST_CANCER', 'primary diagnosis: CHRONIC_LYMPHOCYTIC_LEUKEMIA', 'primary diagnosis: CROHN_DISEASE', 'primary diagnosis: ATOPIC_ECZEMA', 'primary diagnosis: HEALTHY_CONTROL', 'primary diagnosis: INFLUENZA', 'primary diagnosis: OBESITY', 'primary diagnosis: PSORIASIS', 'primary diagnosis: SEASONAL_ALLERGIC_RHINITIS', 'primary diagnosis: TYPE_1_DIABETES', 'primary diagnosis: ACUTE_TONSILLITIS', 'primary diagnosis: ULCERATIVE_COLITIS', 'primary diagnosis: Breast cancer', 'primary diagnosis: Control'], 2: ['Sex: Male', 'diagnosis2: ATOPIC_ECZEMA', 'Sex: Female', 'diagnosis2: ATHEROSCLEROSIS', 'diagnosis2: ASTHMA_OBESITY', 'diagnosis2: ASTHMA', 'diagnosis2: ASTMHA_SEASONAL_ALLERGIC_RHINITIS', 'diagnosis2: OBESITY'], 3: ['age: 56', 'Sex: Male', 'age: 20', 'age: 51', 'age: 37', 'age: 61', 'age: 31', 'age: 41', 'age: 80', 'age: 53', 'age: 73', 'age: 60', 'age: 76', 'age: 77', 'age: 74', 'age: 69', 'age: 81', 'age: 70', 'age: 82', 'age: 67', 'age: 78', 'age: 72', 'age: 66', 'age: 36', 'age: 45', 'age: 65', 'age: 48', 'age: 50', 'age: 24', 'age: 42'], 4: [nan, 'age: 63', 'age: 74', 'age: 49', 'age: 60', 'age: 68', 'age: 38', 'age: 16', 'age: 12', 'age: 27']}\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": "e2fdf62c", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "dbd8240e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:42:53.837563Z", "iopub.status.busy": "2025-03-25T06:42:53.837443Z", "iopub.status.idle": "2025-03-25T06:42:53.879376Z", "shell.execute_reply": "2025-03-25T06:42:53.879096Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of selected clinical features:\n", "{'GSM3494884': [0.0, 56.0, 1.0], 'GSM3494885': [0.0, nan, nan], 'GSM3494886': [0.0, 20.0, 0.0], 'GSM3494887': [0.0, 51.0, 0.0], 'GSM3494888': [0.0, 37.0, 1.0], 'GSM3494889': [0.0, 61.0, 1.0], 'GSM3494890': [0.0, nan, nan], 'GSM3494891': [0.0, 31.0, 1.0], 'GSM3494892': [0.0, 56.0, 0.0], 'GSM3494893': [0.0, 41.0, 0.0], 'GSM3494894': [0.0, 61.0, 0.0], 'GSM3494895': [1.0, nan, nan], 'GSM3494896': [1.0, 80.0, 1.0], 'GSM3494897': [1.0, 53.0, 1.0], 'GSM3494898': [1.0, 61.0, 1.0], 'GSM3494899': [1.0, 73.0, 1.0], 'GSM3494900': [1.0, 60.0, 1.0], 'GSM3494901': [1.0, 76.0, 1.0], 'GSM3494902': [1.0, 77.0, 0.0], 'GSM3494903': [1.0, 74.0, 0.0], 'GSM3494904': [1.0, 69.0, 1.0], 'GSM3494905': [0.0, 77.0, 0.0], 'GSM3494906': [0.0, 81.0, 0.0], 'GSM3494907': [0.0, 70.0, 0.0], 'GSM3494908': [0.0, 82.0, 0.0], 'GSM3494909': [0.0, 69.0, 0.0], 'GSM3494910': [0.0, 82.0, 0.0], 'GSM3494911': [0.0, 67.0, 0.0], 'GSM3494912': [0.0, 67.0, 0.0], 'GSM3494913': [0.0, 78.0, 0.0], 'GSM3494914': [0.0, 67.0, 0.0], 'GSM3494915': [0.0, 74.0, 1.0], 'GSM3494916': [0.0, nan, nan], 'GSM3494917': [0.0, 51.0, 1.0], 'GSM3494918': [0.0, 72.0, 1.0], 'GSM3494919': [0.0, 66.0, 1.0], 'GSM3494920': [0.0, 80.0, 0.0], 'GSM3494921': [0.0, 36.0, 1.0], 'GSM3494922': [0.0, 67.0, 0.0], 'GSM3494923': [0.0, 31.0, 0.0], 'GSM3494924': [0.0, 31.0, 0.0], 'GSM3494925': [0.0, 45.0, 0.0], 'GSM3494926': [0.0, 56.0, 0.0], 'GSM3494927': [0.0, 65.0, 0.0], 'GSM3494928': [0.0, 53.0, 0.0], 'GSM3494929': [0.0, 48.0, 0.0], 'GSM3494930': [0.0, 50.0, 0.0], 'GSM3494931': [0.0, 76.0, 1.0], 'GSM3494932': [0.0, nan, nan], 'GSM3494933': [0.0, 24.0, 0.0], 'GSM3494934': [0.0, 42.0, 0.0], 'GSM3494935': [0.0, 76.0, 1.0], 'GSM3494936': [0.0, 22.0, 1.0], 'GSM3494937': [0.0, nan, nan], 'GSM3494938': [0.0, 23.0, 0.0], 'GSM3494939': [0.0, 34.0, 1.0], 'GSM3494940': [0.0, 43.0, 1.0], 'GSM3494941': [0.0, 47.0, 1.0], 'GSM3494942': [0.0, 24.0, 0.0], 'GSM3494943': [0.0, 55.0, 1.0], 'GSM3494944': [0.0, 48.0, 1.0], 'GSM3494945': [0.0, 58.0, 1.0], 'GSM3494946': [0.0, 30.0, 0.0], 'GSM3494947': [0.0, 28.0, 1.0], 'GSM3494948': [0.0, 41.0, 0.0], 'GSM3494949': [0.0, 63.0, 1.0], 'GSM3494950': [0.0, 55.0, 0.0], 'GSM3494951': [0.0, 55.0, 0.0], 'GSM3494952': [0.0, 67.0, 1.0], 'GSM3494953': [0.0, 47.0, 0.0], 'GSM3494954': [0.0, 46.0, 0.0], 'GSM3494955': [0.0, 49.0, 1.0], 'GSM3494956': [0.0, 23.0, 1.0], 'GSM3494957': [0.0, 68.0, 1.0], 'GSM3494958': [0.0, 39.0, 1.0], 'GSM3494959': [0.0, 24.0, 1.0], 'GSM3494960': [0.0, 36.0, 0.0], 'GSM3494961': [0.0, 58.0, 0.0], 'GSM3494962': [0.0, 38.0, 0.0], 'GSM3494963': [0.0, 27.0, 0.0], 'GSM3494964': [0.0, 67.0, 0.0], 'GSM3494965': [0.0, 61.0, 1.0], 'GSM3494966': [0.0, 69.0, 1.0], 'GSM3494967': [0.0, 63.0, 1.0], 'GSM3494968': [0.0, 60.0, 0.0], 'GSM3494969': [0.0, 17.0, 1.0], 'GSM3494970': [0.0, 10.0, 0.0], 'GSM3494971': [0.0, 9.0, 1.0], 'GSM3494972': [0.0, 13.0, 0.0], 'GSM3494973': [0.0, 10.0, 1.0], 'GSM3494974': [0.0, 13.0, 0.0], 'GSM3494975': [0.0, 15.0, 1.0], 'GSM3494976': [0.0, 12.0, 1.0], 'GSM3494977': [0.0, 13.0, 1.0], 'GSM3494978': [0.0, 81.0, 0.0], 'GSM3494979': [0.0, 94.0, 0.0], 'GSM3494980': [0.0, 51.0, 1.0], 'GSM3494981': [0.0, 40.0, 1.0], 'GSM3494982': [0.0, nan, nan], 'GSM3494983': [0.0, 97.0, 1.0], 'GSM3494984': [0.0, 23.0, 1.0], 'GSM3494985': [0.0, 93.0, 0.0], 'GSM3494986': [0.0, 58.0, 1.0], 'GSM3494987': [0.0, 28.0, 0.0], 'GSM3494988': [0.0, 54.0, 1.0], 'GSM3494989': [0.0, 15.0, 1.0], 'GSM3494990': [0.0, 8.0, 1.0], 'GSM3494991': [0.0, 11.0, 1.0], 'GSM3494992': [0.0, 12.0, 1.0], 'GSM3494993': [0.0, 8.0, 0.0], 'GSM3494994': [0.0, 14.0, 1.0], 'GSM3494995': [0.0, 8.0, 0.0], 'GSM3494996': [0.0, 10.0, 1.0], 'GSM3494997': [0.0, 14.0, 1.0], 'GSM3494998': [0.0, 13.0, 1.0], 'GSM3494999': [0.0, 40.0, 0.0], 'GSM3495000': [0.0, 52.0, 0.0], 'GSM3495001': [0.0, 42.0, 0.0], 'GSM3495002': [0.0, 29.0, 0.0], 'GSM3495003': [0.0, 43.0, 0.0], 'GSM3495004': [0.0, 41.0, 0.0], 'GSM3495005': [0.0, 54.0, 1.0], 'GSM3495006': [0.0, 42.0, 1.0], 'GSM3495007': [0.0, 49.0, 1.0], 'GSM3495008': [0.0, 45.0, 0.0], 'GSM3495009': [0.0, 56.0, 1.0], 'GSM3495010': [0.0, 64.0, 0.0], 'GSM3495011': [0.0, 71.0, 0.0], 'GSM3495012': [0.0, 48.0, 0.0], 'GSM3495013': [0.0, 20.0, 1.0], 'GSM3495014': [0.0, 53.0, 0.0], 'GSM3495015': [0.0, 32.0, 0.0], 'GSM3495016': [0.0, 26.0, 0.0], 'GSM3495017': [0.0, 28.0, 0.0], 'GSM3495018': [0.0, 47.0, 0.0], 'GSM3495019': [0.0, 24.0, 0.0], 'GSM3495020': [0.0, 48.0, 0.0], 'GSM3495021': [0.0, nan, nan], 'GSM3495022': [0.0, 19.0, 0.0], 'GSM3495023': [0.0, 41.0, 0.0], 'GSM3495024': [0.0, 38.0, 0.0], 'GSM3495025': [0.0, nan, nan], 'GSM3495026': [0.0, 15.0, 0.0], 'GSM3495027': [0.0, 12.0, 1.0], 'GSM3495028': [0.0, 13.0, 0.0], 'GSM3495029': [0.0, nan, nan], 'GSM3495030': [0.0, 11.0, 1.0], 'GSM3495031': [0.0, nan, nan], 'GSM3495032': [0.0, 16.0, 1.0], 'GSM3495033': [0.0, 11.0, 1.0], 'GSM3495034': [0.0, nan, nan], 'GSM3495035': [0.0, 35.0, 0.0], 'GSM3495036': [0.0, 26.0, 0.0], 'GSM3495037': [0.0, 39.0, 0.0], 'GSM3495038': [0.0, 46.0, 0.0], 'GSM3495039': [0.0, 42.0, 0.0], 'GSM3495040': [0.0, 20.0, 1.0], 'GSM3495041': [0.0, 69.0, 1.0], 'GSM3495042': [0.0, 69.0, 0.0], 'GSM3495043': [0.0, 47.0, 1.0], 'GSM3495044': [0.0, 47.0, 1.0], 'GSM3495045': [0.0, 56.0, 0.0], 'GSM3495046': [0.0, 54.0, 0.0], 'GSM3495047': [0.0, 53.0, 0.0], 'GSM3495048': [0.0, 50.0, 0.0], 'GSM3495049': [0.0, 22.0, 1.0], 'GSM3495050': [0.0, 62.0, 0.0], 'GSM3495051': [0.0, 74.0, 0.0], 'GSM3495052': [0.0, 57.0, 0.0], 'GSM3495053': [0.0, 47.0, 0.0], 'GSM3495054': [0.0, 70.0, 0.0], 'GSM3495055': [0.0, 50.0, 0.0], 'GSM3495056': [0.0, 52.0, 0.0], 'GSM3495057': [0.0, 43.0, 0.0], 'GSM3495058': [0.0, 57.0, 0.0], 'GSM3495059': [0.0, 53.0, 0.0], 'GSM3495060': [0.0, 70.0, 0.0], 'GSM3495061': [0.0, 41.0, 0.0], 'GSM3495062': [0.0, 61.0, 0.0], 'GSM3495063': [0.0, 39.0, 0.0], 'GSM3495064': [0.0, 58.0, 0.0], 'GSM3495065': [0.0, 55.0, 0.0], 'GSM3495066': [0.0, 63.0, 0.0], 'GSM3495067': [0.0, 60.0, 0.0], 'GSM3495068': [0.0, 43.0, 0.0], 'GSM3495069': [0.0, 68.0, 0.0], 'GSM3495070': [0.0, 67.0, 0.0], 'GSM3495071': [0.0, 50.0, 0.0], 'GSM3495072': [0.0, 67.0, 0.0], 'GSM3495073': [0.0, 51.0, 0.0], 'GSM3495074': [0.0, 59.0, 0.0], 'GSM3495075': [0.0, 44.0, 0.0], 'GSM3495076': [0.0, 35.0, 0.0], 'GSM3495077': [0.0, 83.0, 0.0], 'GSM3495078': [0.0, 78.0, 0.0], 'GSM3495079': [0.0, 88.0, 0.0], 'GSM3495080': [0.0, 41.0, 0.0], 'GSM3495081': [0.0, 60.0, 0.0], 'GSM3495082': [0.0, 72.0, 0.0], 'GSM3495083': [0.0, 53.0, 0.0]}\n", "Clinical data saved to: ../../output/preprocess/Atherosclerosis/clinical_data/GSE123088.csv\n" ] } ], "source": [ "import pandas as pd\n", "import os\n", "import json\n", "from typing import Callable, Optional, Dict, Any\n", "\n", "# 1. Determine gene expression data availability\n", "# Based on the background information, this dataset appears to be a SuperSeries containing gene expression data\n", "is_gene_available = True\n", "\n", "# 2. Identify variable availability and create conversion functions\n", "\n", "# 2.1 For trait (Atherosclerosis)\n", "trait_row = 1 # \"primary diagnosis\" in row 1 contains the trait information\n", "\n", "def convert_trait(value: str) -> int:\n", " \"\"\"Convert trait value to binary (0 or 1).\"\"\"\n", " if pd.isna(value):\n", " return None\n", " \n", " # Extract the value after the colon if present\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " # Convert to binary based on Atherosclerosis diagnosis\n", " if \"ATHEROSCLEROSIS\" in value.upper():\n", " return 1\n", " elif \"HEALTHY_CONTROL\" in value.upper() or \"CONTROL\" in value.upper():\n", " return 0\n", " else:\n", " # Other diagnoses are not related to Atherosclerosis\n", " return 0\n", "\n", "# 2.2 For age\n", "age_row = 3 # \"age\" appears in row 3 and 4, but primarily in row 3\n", "\n", "def convert_age(value: str) -> float:\n", " \"\"\"Convert age value to continuous numeric value.\"\"\"\n", " if pd.isna(value):\n", " return None\n", " \n", " # Extract the value after the colon if present\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " try:\n", " return float(value)\n", " except (ValueError, TypeError):\n", " return None\n", "\n", "# 2.3 For gender\n", "gender_row = 2 # \"Sex\" appears in row 2 and 3, but primarily in row 2\n", "\n", "def convert_gender(value: str) -> int:\n", " \"\"\"Convert gender value to binary (0 for female, 1 for male).\"\"\"\n", " if pd.isna(value):\n", " return None\n", " \n", " # Extract the value after the colon if present\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip().upper()\n", " \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", "# Trait data is available since trait_row is not None\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", "# 4. Extract clinical features if trait data is available\n", "if trait_row is not None:\n", " try:\n", " # Use the clinical_data variable that should be available from a previous step\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 selected clinical features\n", " preview = preview_df(selected_clinical_df)\n", " print(\"Preview of selected clinical features:\")\n", " print(preview)\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 selected clinical features to a CSV file\n", " selected_clinical_df.to_csv(out_clinical_data_file, index=False)\n", " print(f\"Clinical data saved to: {out_clinical_data_file}\")\n", " except Exception as e:\n", " print(f\"Error processing clinical data: {e}\")\n", " # If clinical data is not available or there's an error, we'll proceed without it\n", " print(\"Proceeding without clinical data.\")\n" ] }, { "cell_type": "markdown", "id": "53062ab1", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "766d17b8", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:42:53.880511Z", "iopub.status.busy": "2025-03-25T06:42:53.880401Z", "iopub.status.idle": "2025-03-25T06:42:54.402583Z", "shell.execute_reply": "2025-03-25T06:42:54.402179Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix file found: ../../input/GEO/Atherosclerosis/GSE123088/GSE123088_series_matrix.txt.gz\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape: (24166, 204)\n", "First 20 gene/probe identifiers:\n", "Index(['1', '2', '3', '9', '10', '12', '13', '14', '15', '16', '18', '19',\n", " '20', '21', '22', '23', '24', '25', '26', '27'],\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": "36411bf2", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "e3ccf7f9", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:42:54.404015Z", "iopub.status.busy": "2025-03-25T06:42:54.403879Z", "iopub.status.idle": "2025-03-25T06:42:54.405816Z", "shell.execute_reply": "2025-03-25T06:42:54.405516Z" } }, "outputs": [], "source": [ "# Examining the gene identifiers from the previous step\n", "# These appear to be simple numeric identifiers (1, 2, 3, etc.), not human gene symbols\n", "# These are likely probe IDs or some other type of numeric identifiers that need mapping to gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "6553e21c", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "2cfb2a40", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:42:54.407135Z", "iopub.status.busy": "2025-03-25T06:42:54.407011Z", "iopub.status.idle": "2025-03-25T06:42:59.433645Z", "shell.execute_reply": "2025-03-25T06:42:59.433239Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene annotation preview:\n", "Columns in gene annotation: ['ID', 'ENTREZ_GENE_ID', 'SPOT_ID']\n", "{'ID': ['1', '2', '3', '9', '10'], 'ENTREZ_GENE_ID': ['1', '2', '3', '9', '10'], 'SPOT_ID': [1.0, 2.0, 3.0, 9.0, 10.0]}\n", "\n", "Checking the SOFT file structure:\n", "^DATABASE = GeoMiame\n", "!Database_name = Gene Expression Omnibus (GEO)\n", "!Database_institute = NCBI NLM NIH\n", "!Database_web_link = http://www.ncbi.nlm.nih.gov/geo\n", "!Database_email = geo@ncbi.nlm.nih.gov\n", "^SERIES = GSE123088\n", "!Series_title = A validated single-cell-based strategy to identify diagnostic and therapeutic targets in complex diseases\n", "!Series_geo_accession = GSE123088\n", "!Series_status = Public on Nov 23 2021\n", "!Series_submission_date = Nov 28 2018\n", "!Series_last_update_date = Apr 21 2023\n", "!Series_pubmed_id = 31358043\n", "!Series_summary = This SuperSeries is composed of the SubSeries listed below.\n", "!Series_overall_design = Refer to individual Series\n", "!Series_type = Expression profiling by array\n", "!Series_sample_id = GSM3494884\n", "!Series_sample_id = GSM3494885\n", "!Series_sample_id = GSM3494886\n", "!Series_sample_id = GSM3494887\n", "!Series_sample_id = GSM3494888\n", "\n", "Let's create a mapping using ENTREZ_GENE_ID as this corresponds to gene identifiers in NCBI\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Mapping data shape: (4740924, 2)\n", "Sample mapping data:\n", "{'ID': ['1', '2', '3', '9', '10'], 'Gene': ['1', '2', '3', '9', '10']}\n", "\n", "Found gene identifiers for 4740924 out of 4740924 probes (100.00%)\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", "# Based on the preview, we can see that we have ID and ENTREZ_GENE_ID\n", "# We need to check if there's any other information in the SOFT file that could help us map to gene symbols\n", "\n", "# Check the first few rows of the SOFT file to better understand its structure\n", "print(\"\\nChecking the SOFT file structure:\")\n", "with gzip.open(soft_file, 'rt') as f:\n", " for i, line in enumerate(f):\n", " if i < 20: # Just check the first 20 lines\n", " print(line.strip())\n", " else:\n", " break\n", "\n", "print(\"\\nLet's create a mapping using ENTREZ_GENE_ID as this corresponds to gene identifiers in NCBI\")\n", "# Create a mapping dataframe with probe IDs and ENTREZ_GENE_ID (which are numeric gene identifiers)\n", "mapping_data = gene_annotation[['ID', 'ENTREZ_GENE_ID']].copy()\n", "mapping_data = mapping_data.dropna(subset=['ENTREZ_GENE_ID'])\n", "\n", "# Since ENTREZ_GENE_ID is already in the format of gene identifiers, we'll use those directly\n", "mapping_data = mapping_data.rename(columns={'ENTREZ_GENE_ID': 'Gene'})\n", "\n", "# Filter out rows with empty gene values\n", "mapping_data = mapping_data[mapping_data['Gene'] != '']\n", "print(f\"Mapping data shape: {mapping_data.shape}\")\n", "print(\"Sample mapping data:\")\n", "print(preview_df(mapping_data, n=5))\n", "\n", "# Count how many probes map to at least one gene identifier\n", "genes_mapped = mapping_data.shape[0]\n", "total_probes = gene_annotation.shape[0]\n", "mapping_percentage = (genes_mapped / total_probes) * 100 if total_probes > 0 else 0\n", "print(f\"\\nFound gene identifiers for {genes_mapped} out of {total_probes} probes ({mapping_percentage:.2f}%)\")\n", "\n", "# Save the mapping for later use\n", "gene_mapping = mapping_data\n" ] }, { "cell_type": "markdown", "id": "3f023ddc", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "306f04a3", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:42:59.435187Z", "iopub.status.busy": "2025-03-25T06:42:59.434948Z", "iopub.status.idle": "2025-03-25T06:43:07.493153Z", "shell.execute_reply": "2025-03-25T06:43:07.492757Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mapped gene expression data shape: (0, 204)\n", "First 5 gene symbols in the mapped data:\n", "[]\n", "Gene expression data saved to: ../../output/preprocess/Atherosclerosis/gene_data/GSE123088.csv\n" ] } ], "source": [ "# 1. Based on the gene identifiers in the gene expression data and the gene annotation data:\n", "# - Gene expression data identifiers: numeric IDs like '1', '2', '3', etc. (probe IDs)\n", "# - Gene annotation contains 'ID', 'ENTREZ_GENE_ID', 'SPOT_ID' columns\n", "# - 'ID' in gene_annotation matches the index of gene_data (probe IDs)\n", "# - 'ENTREZ_GENE_ID' contains Entrez Gene IDs which can be mapped to human gene symbols\n", "\n", "# 2. Create a gene mapping dataframe \n", "# We need 'ID' as the identifier and 'ENTREZ_GENE_ID' as the gene reference\n", "mapping_df = gene_annotation[['ID', 'ENTREZ_GENE_ID']].copy()\n", "mapping_df = mapping_df.rename(columns={'ENTREZ_GENE_ID': 'Gene'})\n", "mapping_df = mapping_df.dropna(subset=['Gene'])\n", "\n", "# 3. Apply gene mapping to convert probe-level measurements to gene expression data\n", "# The function apply_gene_mapping handles the many-to-many relation between probes and genes\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "\n", "print(f\"Mapped gene expression data shape: {gene_data.shape}\")\n", "print(\"First 5 gene symbols in the mapped data:\")\n", "print(gene_data.index[:5].tolist())\n", "\n", "# Save the gene expression data\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "gene_data.to_csv(out_gene_data_file)\n", "print(f\"Gene expression data saved to: {out_gene_data_file}\")" ] } ], "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 }