{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "14fbaf1b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:26:40.728849Z", "iopub.status.busy": "2025-03-25T07:26:40.728745Z", "iopub.status.idle": "2025-03-25T07:26:40.896158Z", "shell.execute_reply": "2025-03-25T07:26:40.895826Z" } }, "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 = \"Large_B-cell_Lymphoma\"\n", "cohort = \"GSE159472\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Large_B-cell_Lymphoma\"\n", "in_cohort_dir = \"../../input/GEO/Large_B-cell_Lymphoma/GSE159472\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Large_B-cell_Lymphoma/GSE159472.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Large_B-cell_Lymphoma/gene_data/GSE159472.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Large_B-cell_Lymphoma/clinical_data/GSE159472.csv\"\n", "json_path = \"../../output/preprocess/Large_B-cell_Lymphoma/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "93fc5f2b", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "f5d8ce42", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:26:40.897633Z", "iopub.status.busy": "2025-03-25T07:26:40.897486Z", "iopub.status.idle": "2025-03-25T07:26:41.418210Z", "shell.execute_reply": "2025-03-25T07:26:41.417825Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Expression data from DLBCL expression study\"\n", "!Series_summary\t\"We used microarray data sets to determine concordance of microarray vs qRT-PCR based LCOO cell-of-origin for Diffuse large B-cell Lymphome subtypes.\"\n", "!Series_overall_design\t\"Three independent sets of samples were processed and analyzed for a total of 180 samples. Training sets were developed to build and develop a DLBCL classification model. A subsequent validation data set was used to validate the classification model.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['dataset group: Training 1', 'dataset group: Training 2', 'dataset group: Validation'], 1: ['tissue: large B-cell lymphoma tissue'], 2: ['subtype (rmsg100): GCB', 'subtype (rmsg100): ABC']}\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": "c7c7164b", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "dffe6b5a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:26:41.419490Z", "iopub.status.busy": "2025-03-25T07:26:41.419372Z", "iopub.status.idle": "2025-03-25T07:26:41.443016Z", "shell.execute_reply": "2025-03-25T07:26:41.442705Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of selected clinical features:\n", "{'GSM4830213': [0.0], 'GSM4830214': [1.0], 'GSM4830215': [1.0], 'GSM4830216': [1.0], 'GSM4830217': [0.0], 'GSM4830218': [0.0], 'GSM4830219': [0.0], 'GSM4830220': [0.0], 'GSM4830221': [1.0], 'GSM4830222': [1.0], 'GSM4830223': [1.0], 'GSM4830224': [0.0], 'GSM4830225': [0.0], 'GSM4830226': [0.0], 'GSM4830227': [1.0], 'GSM4830228': [1.0], 'GSM4830229': [1.0], 'GSM4830230': [0.0], 'GSM4830231': [0.0], 'GSM4830232': [0.0], 'GSM4830233': [0.0], 'GSM4830234': [0.0], 'GSM4830235': [1.0], 'GSM4830236': [0.0], 'GSM4830237': [0.0], 'GSM4830238': [1.0], 'GSM4830239': [0.0], 'GSM4830240': [1.0], 'GSM4830241': [0.0], 'GSM4830242': [1.0], 'GSM4830243': [0.0], 'GSM4830244': [1.0], 'GSM4830245': [1.0], 'GSM4830246': [1.0], 'GSM4830247': [0.0], 'GSM4830248': [0.0], 'GSM4830249': [1.0], 'GSM4830250': [1.0], 'GSM4830251': [1.0], 'GSM4830252': [0.0], 'GSM4830253': [1.0], 'GSM4830254': [0.0], 'GSM4830255': [1.0], 'GSM4830256': [0.0], 'GSM4830257': [1.0], 'GSM4830258': [0.0], 'GSM4830259': [0.0], 'GSM4830260': [0.0], 'GSM4830261': [0.0], 'GSM4830262': [1.0], 'GSM4830263': [0.0], 'GSM4830264': [0.0], 'GSM4830265': [1.0], 'GSM4830266': [0.0], 'GSM4830267': [0.0], 'GSM4830268': [1.0], 'GSM4830269': [0.0], 'GSM4830270': [1.0], 'GSM4830271': [0.0], 'GSM4830272': [0.0], 'GSM4830273': [0.0], 'GSM4830274': [0.0], 'GSM4830275': [1.0], 'GSM4830276': [0.0], 'GSM4830277': [0.0], 'GSM4830278': [1.0], 'GSM4830279': [0.0], 'GSM4830280': [0.0], 'GSM4830281': [0.0], 'GSM4830282': [0.0], 'GSM4830283': [0.0], 'GSM4830284': [0.0], 'GSM4830285': [0.0], 'GSM4830286': [0.0], 'GSM4830287': [0.0], 'GSM4830288': [0.0], 'GSM4830289': [0.0], 'GSM4830290': [1.0], 'GSM4830291': [1.0], 'GSM4830292': [1.0], 'GSM4830293': [0.0], 'GSM4830294': [1.0], 'GSM4830295': [0.0], 'GSM4830296': [0.0], 'GSM4830297': [0.0], 'GSM4830298': [0.0], 'GSM4830299': [0.0], 'GSM4830300': [0.0], 'GSM4830301': [1.0], 'GSM4830302': [1.0], 'GSM4830303': [1.0], 'GSM4830304': [1.0], 'GSM4830305': [1.0], 'GSM4830306': [1.0], 'GSM4830307': [1.0], 'GSM4830308': [1.0], 'GSM4830309': [1.0], 'GSM4830310': [1.0], 'GSM4830311': [1.0], 'GSM4830312': [1.0], 'GSM4830313': [1.0], 'GSM4830314': [1.0], 'GSM4830315': [1.0], 'GSM4830316': [1.0], 'GSM4830317': [1.0], 'GSM4830318': [0.0], 'GSM4830319': [0.0], 'GSM4830320': [1.0], 'GSM4830321': [0.0], 'GSM4830322': [0.0], 'GSM4830323': [0.0], 'GSM4830324': [0.0], 'GSM4830325': [0.0], 'GSM4830326': [0.0], 'GSM4830327': [0.0], 'GSM4830328': [0.0], 'GSM4830329': [0.0], 'GSM4830330': [1.0], 'GSM4830331': [0.0], 'GSM4830332': [0.0], 'GSM4830333': [1.0], 'GSM4830334': [1.0], 'GSM4830335': [1.0], 'GSM4830336': [1.0], 'GSM4830337': [1.0], 'GSM4830338': [1.0], 'GSM4830339': [0.0], 'GSM4830340': [0.0], 'GSM4830341': [1.0], 'GSM4830342': [1.0], 'GSM4830343': [1.0], 'GSM4830344': [0.0], 'GSM4830345': [1.0], 'GSM4830346': [0.0], 'GSM4830347': [0.0], 'GSM4830348': [1.0], 'GSM4830349': [1.0], 'GSM4830350': [0.0], 'GSM4830351': [1.0], 'GSM4830352': [0.0], 'GSM4830353': [0.0], 'GSM4830354': [1.0], 'GSM4830355': [1.0], 'GSM4830356': [1.0], 'GSM4830357': [0.0], 'GSM4830358': [0.0], 'GSM4830359': [1.0], 'GSM4830360': [0.0], 'GSM4830361': [0.0], 'GSM4830362': [0.0], 'GSM4830363': [0.0], 'GSM4830364': [0.0], 'GSM4830365': [0.0], 'GSM4830366': [0.0], 'GSM4830367': [0.0], 'GSM4830368': [0.0], 'GSM4830369': [0.0], 'GSM4830370': [0.0], 'GSM4830371': [0.0], 'GSM4830372': [0.0], 'GSM4830373': [0.0], 'GSM4830374': [1.0], 'GSM4830375': [0.0], 'GSM4830376': [0.0], 'GSM4830377': [0.0], 'GSM4830378': [0.0], 'GSM4830379': [0.0], 'GSM4830380': [0.0], 'GSM4830381': [0.0], 'GSM4830382': [0.0], 'GSM4830383': [0.0], 'GSM4830384': [0.0], 'GSM4830385': [0.0], 'GSM4830386': [1.0], 'GSM4830387': [0.0], 'GSM4830388': [0.0], 'GSM4830389': [1.0], 'GSM4830390': [0.0], 'GSM4830391': [1.0], 'GSM4830392': [0.0]}\n", "Clinical data saved to ../../output/preprocess/Large_B-cell_Lymphoma/clinical_data/GSE159472.csv\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "import os\n", "import json\n", "from typing import Callable, Optional, Dict, Any\n", "\n", "# 1. Gene Expression Data Availability\n", "# Based on the background information, this dataset appears to contain microarray data for gene expression\n", "# The series title and summary mention \"expression data\" and \"microarray data sets\"\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability for trait, age, and gender\n", "\n", "# For trait (DLBCL subtype)\n", "# Row 2 contains \"subtype (rmsg100): GCB\" and \"subtype (rmsg100): ABC\", which are DLBCL subtypes\n", "trait_row = 2\n", "\n", "# For age - Not available in the sample characteristics dictionary\n", "age_row = None\n", "\n", "# For gender - Not available in the sample characteristics dictionary\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion Functions\n", "\n", "def convert_trait(value):\n", " \"\"\"Convert DLBCL subtype to binary (0 for GCB, 1 for ABC)\"\"\"\n", " if pd.isna(value) or value is None:\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\n", " if 'GCB' in value:\n", " return 0\n", " elif 'ABC' in value:\n", " return 1\n", " else:\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age to continuous, but not used in this dataset as age is not available\"\"\"\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender to binary (0 for female, 1 for male), but not used in this dataset as gender is not available\"\"\"\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Determine trait data availability\n", "is_trait_available = trait_row is not None\n", "\n", "# Validate and save cohort info\n", "validate_and_save_cohort_info(\n", " is_final=False,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=is_gene_available,\n", " is_trait_available=is_trait_available\n", ")\n", "\n", "# 4. Clinical Feature Extraction\n", "# Since trait_row is not None, we need to extract clinical features\n", "if trait_row is not None and 'clinical_data' in locals():\n", " # Use geo_select_clinical_features to extract clinical features\n", " selected_clinical_df = geo_select_clinical_features(\n", " clinical_df=clinical_data,\n", " trait=trait,\n", " trait_row=trait_row,\n", " convert_trait=convert_trait,\n", " age_row=age_row,\n", " convert_age=convert_age,\n", " gender_row=gender_row,\n", " convert_gender=convert_gender\n", " )\n", " \n", " # Preview the dataframe\n", " preview = preview_df(selected_clinical_df)\n", " print(\"Preview of selected clinical features:\")\n", " print(preview)\n", " \n", " # Ensure output directory exists\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " \n", " # Save the clinical data\n", " selected_clinical_df.to_csv(out_clinical_data_file, index=False)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "aa39cfd0", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "8dfaec2e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:26:41.444238Z", "iopub.status.busy": "2025-03-25T07:26:41.444119Z", "iopub.status.idle": "2025-03-25T07:26:42.432839Z", "shell.execute_reply": "2025-03-25T07:26:42.432442Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Examining matrix file structure...\n", "Line 0: !Series_title\t\"Expression data from DLBCL expression study\"\n", "Line 1: !Series_geo_accession\t\"GSE159472\"\n", "Line 2: !Series_status\t\"Public on Dec 07 2020\"\n", "Line 3: !Series_submission_date\t\"Oct 13 2020\"\n", "Line 4: !Series_last_update_date\t\"Jan 04 2021\"\n", "Line 5: !Series_pubmed_id\t\"33385586\"\n", "Line 6: !Series_summary\t\"We used microarray data sets to determine concordance of microarray vs qRT-PCR based LCOO cell-of-origin for Diffuse large B-cell Lymphome subtypes.\"\n", "Line 7: !Series_overall_design\t\"Three independent sets of samples were processed and analyzed for a total of 180 samples. Training sets were developed to build and develop a DLBCL classification model. A subsequent validation data set was used to validate the classification model.\"\n", "Line 8: !Series_type\t\"Expression profiling by array\"\n", "Line 9: !Series_contributor\t\"Rajiv,,Dua\"\n", "Found table marker at line 61\n", "First few lines after marker:\n", "\"ID_REF\"\t\"GSM4830213\"\t\"GSM4830214\"\t\"GSM4830215\"\t\"GSM4830216\"\t\"GSM4830217\"\t\"GSM4830218\"\t\"GSM4830219\"\t\"GSM4830220\"\t\"GSM4830221\"\t\"GSM4830222\"\t\"GSM4830223\"\t\"GSM4830224\"\t\"GSM4830225\"\t\"GSM4830226\"\t\"GSM4830227\"\t\"GSM4830228\"\t\"GSM4830229\"\t\"GSM4830230\"\t\"GSM4830231\"\t\"GSM4830232\"\t\"GSM4830233\"\t\"GSM4830234\"\t\"GSM4830235\"\t\"GSM4830236\"\t\"GSM4830237\"\t\"GSM4830238\"\t\"GSM4830239\"\t\"GSM4830240\"\t\"GSM4830241\"\t\"GSM4830242\"\t\"GSM4830243\"\t\"GSM4830244\"\t\"GSM4830245\"\t\"GSM4830246\"\t\"GSM4830247\"\t\"GSM4830248\"\t\"GSM4830249\"\t\"GSM4830250\"\t\"GSM4830251\"\t\"GSM4830252\"\t\"GSM4830253\"\t\"GSM4830254\"\t\"GSM4830255\"\t\"GSM4830256\"\t\"GSM4830257\"\t\"GSM4830258\"\t\"GSM4830259\"\t\"GSM4830260\"\t\"GSM4830261\"\t\"GSM4830262\"\t\"GSM4830263\"\t\"GSM4830264\"\t\"GSM4830265\"\t\"GSM4830266\"\t\"GSM4830267\"\t\"GSM4830268\"\t\"GSM4830269\"\t\"GSM4830270\"\t\"GSM4830271\"\t\"GSM4830272\"\t\"GSM4830273\"\t\"GSM4830274\"\t\"GSM4830275\"\t\"GSM4830276\"\t\"GSM4830277\"\t\"GSM4830278\"\t\"GSM4830279\"\t\"GSM4830280\"\t\"GSM4830281\"\t\"GSM4830282\"\t\"GSM4830283\"\t\"GSM4830284\"\t\"GSM4830285\"\t\"GSM4830286\"\t\"GSM4830287\"\t\"GSM4830288\"\t\"GSM4830289\"\t\"GSM4830290\"\t\"GSM4830291\"\t\"GSM4830292\"\t\"GSM4830293\"\t\"GSM4830294\"\t\"GSM4830295\"\t\"GSM4830296\"\t\"GSM4830297\"\t\"GSM4830298\"\t\"GSM4830299\"\t\"GSM4830300\"\t\"GSM4830301\"\t\"GSM4830302\"\t\"GSM4830303\"\t\"GSM4830304\"\t\"GSM4830305\"\t\"GSM4830306\"\t\"GSM4830307\"\t\"GSM4830308\"\t\"GSM4830309\"\t\"GSM4830310\"\t\"GSM4830311\"\t\"GSM4830312\"\t\"GSM4830313\"\t\"GSM4830314\"\t\"GSM4830315\"\t\"GSM4830316\"\t\"GSM4830317\"\t\"GSM4830318\"\t\"GSM4830319\"\t\"GSM4830320\"\t\"GSM4830321\"\t\"GSM4830322\"\t\"GSM4830323\"\t\"GSM4830324\"\t\"GSM4830325\"\t\"GSM4830326\"\t\"GSM4830327\"\t\"GSM4830328\"\t\"GSM4830329\"\t\"GSM4830330\"\t\"GSM4830331\"\t\"GSM4830332\"\t\"GSM4830333\"\t\"GSM4830334\"\t\"GSM4830335\"\t\"GSM4830336\"\t\"GSM4830337\"\t\"GSM4830338\"\t\"GSM4830339\"\t\"GSM4830340\"\t\"GSM4830341\"\t\"GSM4830342\"\t\"GSM4830343\"\t\"GSM4830344\"\t\"GSM4830345\"\t\"GSM4830346\"\t\"GSM4830347\"\t\"GSM4830348\"\t\"GSM4830349\"\t\"GSM4830350\"\t\"GSM4830351\"\t\"GSM4830352\"\t\"GSM4830353\"\t\"GSM4830354\"\t\"GSM4830355\"\t\"GSM4830356\"\t\"GSM4830357\"\t\"GSM4830358\"\t\"GSM4830359\"\t\"GSM4830360\"\t\"GSM4830361\"\t\"GSM4830362\"\t\"GSM4830363\"\t\"GSM4830364\"\t\"GSM4830365\"\t\"GSM4830366\"\t\"GSM4830367\"\t\"GSM4830368\"\t\"GSM4830369\"\t\"GSM4830370\"\t\"GSM4830371\"\t\"GSM4830372\"\t\"GSM4830373\"\t\"GSM4830374\"\t\"GSM4830375\"\t\"GSM4830376\"\t\"GSM4830377\"\t\"GSM4830378\"\t\"GSM4830379\"\t\"GSM4830380\"\t\"GSM4830381\"\t\"GSM4830382\"\t\"GSM4830383\"\t\"GSM4830384\"\t\"GSM4830385\"\t\"GSM4830386\"\t\"GSM4830387\"\t\"GSM4830388\"\t\"GSM4830389\"\t\"GSM4830390\"\t\"GSM4830391\"\t\"GSM4830392\"\n", "\"1007_s_at\"\t0.624357104\t0.248309419\t0.177771971\t0.173536733\t0.288181633\t0.518944442\t0.191582158\t0.302528769\t0.467873812\t0.30716899\t0.320480019\t0.362571716\t0.313339144\t0.422173828\t0.121908069\t0.452062279\t0.240142271\t0.316772252\t0.358646005\t0.276853889\t0.557558358\t0.354477733\t0.354375362\t0.545686245\t0.298092425\t0.41925624\t0.399516851\t0.550086021\t0.294498384\t0.306997538\t0.243316203\t0.49341172\t0.242423251\t0.457231253\t0.466345578\t0.25472194\t0.602641225\t0.246927917\t0.328280866\t0.306483597\t0.252276272\t0.246943086\t0.171521962\t0.224146172\t0.273160785\t0.264725268\t0.347890973\t0.319397271\t0.192376211\t0.235459581\t0.220313638\t0.65050149\t0.469893754\t0.156662256\t0.354503304\t0.385417193\t0.564168036\t0.374002665\t0.297107428\t0.28866452\t0.482910752\t0.455413133\t0.249618292\t0.364759773\t0.519585788\t0.16683735\t0.383188725\t0.37997368\t0.283781439\t0.419699937\t0.292689621\t0.353021532\t0.227027133\t0.346483707\t0.462505966\t0.133901939\t0.279604405\t0.315955162\t0.383161128\t0.155039355\t0.263847649\t0.219783261\t0.316351831\t0.332126796\t0.18649739\t0.287589639\t0.265709162\t0.428888261\t0.229050294\t0.266694933\t0.175330952\t0.231355697\t0.639430106\t0.281612664\t0.170283258\t0.121420115\t0.215049773\t0.255296618\t0.270065337\t0.243989393\t0.259192288\t0.20943211\t0.325911254\t0.278098315\t0.147683069\t0.253676802\t0.260238171\t0.220516264\t0.384488165\t0.080815792\t0.217695236\t0.267377734\t0.147543162\t0.415621698\t0.24930346\t0.152135789\t0.246831894\t0.284967542\t0.425023735\t0.372766137\t0.178931907\t0.339877516\t0.476064593\t0.239065066\t0.232290283\t0.449232906\t0.411591828\t0.323151439\t0.42368868\t0.293931693\t0.215939909\t0.266548008\t0.357690066\t0.396154076\t0.455447346\t0.247125059\t0.2499028\t0.3246243\t0.383506238\t0.468462318\t0.350974053\t0.095233761\t0.292922258\t0.278087556\t0.303083539\t0.631188631\t0.252868742\t0.529576421\t0.273756653\t0.474739879\t0.368191332\t0.323341221\t0.536290288\t0.374420255\t0.249801174\t0.378990024\t0.282392293\t0.414067298\t0.291108549\t0.462086231\t0.413120002\t0.262606949\t0.249070004\t0.274822474\t0.221491754\t0.252562195\t0.300485402\t0.380576074\t0.458593279\t0.409178644\t0.274267942\t0.147071078\t0.293876171\t0.473847896\t0.301714838\t0.286637455\t0.289565802\t0.26961112\t0.478722841\t0.610374153\n", "\"1053_at\"\t0.095983624\t0.511885762\t0.046437629\t0.516690731\t0.519029856\t0.029606869\t0.494698286\t0.429518372\t0.472632587\t0.034841277\t0.116915569\t0.048555072\t0.147202194\t0.080221929\t0.08514823\t0.01097777\t0.015669651\t0.084605835\t0.020819971\t0.039268438\t0.058816127\t0.032509428\t0.075602382\t0.090383962\t0.057478566\t0.13737902\t0.034531564\t0.064479232\t0.069693521\t0.014345434\t0.022282779\t0.037631594\t0.068080336\t0.046402402\t0.017923534\t0.219330415\t0.079560064\t0.05947974\t0.01520635\t0.071270756\t0.03025718\t0.53487885\t0.534901559\t0.285316318\t0.47927776\t0.078634903\t0.040316414\t0.393531978\t0.449685901\t0.49792096\t0.537342608\t0.484207243\t0.471963584\t0.497664154\t0.452011555\t0.42556861\t0.028739791\t0.53093648\t0.137638912\t0.098670259\t0.076137826\t0.515191734\t0.514204085\t0.484489858\t0.49493283\t0.495735705\t0.475323826\t0.49491328\t0.465566665\t0.520679653\t0.493178427\t0.124032862\t0.564731479\t0.589218557\t0.472523987\t0.130236596\t0.456217974\t0.014616041\t0.167114288\t0.444308937\t0.128687337\t0.023798138\t0.066037402\t0.473793358\t0.531182528\t0.457179666\t0.427788466\t0.552730143\t0.494288296\t0.023288278\t0.435613871\t0.131718695\t0.367515773\t0.012904458\t0.502866149\t0.496128201\t0.449837059\t0.149477512\t0.477671385\t0.543585837\t0.530869424\t0.472126156\t0.063853599\t0.003440866\t0.53069073\t0.098856755\t0.360421956\t0.492537856\t0.130878344\t0.293609768\t0.59296149\t0.509216368\t0.538929284\t0.38459897\t0.432942033\t0.510682344\t0.477082729\t0.457627177\t0.543703377\t0.396855593\t0.022641288\t0.076571181\t0.106086046\t0.180511549\t0.086427763\t0.069719113\t0.063296206\t0.063356243\t0.008373176\t0.043628249\t0.055497918\t0.055116706\t0.047475569\t0.062103152\t0.11071106\t0.137942195\t0.098204032\t0.104922555\t0.068029091\t0.182641163\t0.160085127\t0.024009291\t0.118922755\t0.019608324\t0.067225978\t0.061802506\t0.019657806\t0.132313758\t0.054232132\t0.127432033\t0.040432658\t0.030905573\t0.105746292\t0.120401897\t0.089782536\t0.024602866\t0.056588396\t0.095644698\t0.02916912\t0.083843\t0.011802529\t0.05182353\t0.093069203\t0.0833259\t0.064316429\t0.096500523\t0.093890928\t0.009484288\t0.074412003\t0.038496025\t0.038217146\t0.088427171\t0.047308598\t0.0440358\t0.155973941\t0.039385039\t0.033115003\t0.081155099\t0.036040425\t0.009293315\n", "\"117_at\"\t0.225438908\t0.390900701\t0.281363875\t0.367860019\t0.49994424\t0.140831798\t0.461283326\t0.697216809\t0.393134624\t0.123081483\t0.361215323\t0.131597996\t0.188911095\t0.172798589\t0.292844683\t0.322203904\t0.15121378\t0.256190658\t0.206434309\t0.132572562\t0.212844312\t0.219879657\t0.307958484\t0.176613778\t0.294932187\t0.147823021\t0.108184956\t0.152522415\t0.1893709\t0.247863725\t0.176207021\t0.056873716\t0.141440183\t0.134515315\t0.178886563\t0.118939854\t0.304263234\t0.134230182\t0.113282591\t0.257529467\t0.246730849\t0.35464412\t0.303809136\t0.527406275\t0.333509833\t0.203375235\t0.13395375\t0.290909916\t0.340223193\t0.367198288\t0.453758717\t0.337429017\t0.372712463\t0.419531554\t0.349353731\t0.430623978\t0.224243268\t0.301285833\t0.190650746\t0.296738565\t0.199965954\t0.356504887\t0.294342756\t0.491357565\t0.469929636\t0.4456985\t0.349467456\t0.317836732\t0.362793893\t0.413270175\t0.340606242\t0.253830343\t0.333959699\t0.316456914\t0.451082587\t0.199825838\t0.285829067\t0.166739106\t0.131899774\t0.39153555\t0.296638042\t0.214285091\t0.285949141\t0.406664282\t0.534198701\t0.453333735\t0.403714687\t0.476678729\t0.521864355\t0.339150041\t0.338399291\t0.153085068\t0.420865476\t0.2309037\t0.483023256\t0.500721753\t0.573749959\t0.136227518\t0.403860867\t0.430829734\t0.42083475\t0.556565166\t0.196555436\t0.345118821\t0.390731603\t0.228072047\t0.410591722\t0.233522341\t0.243667766\t0.174284577\t0.45532766\t0.406413853\t0.471422225\t0.19601655\t0.441613346\t0.31517455\t0.319655925\t0.384792954\t0.473757029\t0.381742597\t0.222439781\t0.201078594\t0.29924801\t0.163645878\t0.408466846\t0.181511894\t0.219503805\t0.179122373\t0.183042288\t0.409891903\t0.236341432\t0.231021568\t0.206707969\t0.245439038\t0.192505568\t0.240531772\t0.203121752\t0.224816486\t0.191388398\t0.216869712\t0.167373434\t0.261795342\t0.178007439\t0.305150062\t0.240941554\t0.103751019\t0.189021423\t0.231749013\t0.107292503\t0.227876589\t0.188690498\t0.165703788\t0.210903078\t0.121035002\t0.246135026\t0.331920952\t0.280488729\t0.22607173\t0.078948982\t0.261868149\t0.101926617\t0.232231215\t0.241751999\t0.305662483\t0.268788248\t0.162827656\t0.225448638\t0.213807642\t0.170911342\t0.18862617\t0.125585526\t0.251602888\t0.338780642\t0.243326247\t0.172133058\t0.108082943\t0.160909638\t0.199200347\t0.234282613\t0.280380815\n", "\"121_at\"\t0.432273507\t0.325696498\t0.422096729\t0.416606337\t0.251368433\t0.516119719\t0.325076669\t0.375337869\t0.298215687\t0.463031471\t0.102647781\t0.465831995\t0.35865894\t0.406796932\t0.416030347\t0.416849136\t0.419271529\t0.434955865\t0.463926941\t0.41430828\t0.457282871\t0.498019814\t0.354157925\t0.37731564\t0.444325417\t0.428699464\t0.418843687\t0.468748063\t0.427427888\t0.475104719\t0.428841054\t0.474084288\t0.414097399\t0.437466651\t0.336179644\t0.361306489\t0.478039891\t0.488399506\t0.510910153\t0.5393911\t0.544740021\t0.302212059\t0.325791925\t0.43263945\t0.354042858\t0.394940376\t0.427506238\t0.385694981\t0.331207335\t0.219667614\t0.300552964\t0.255060494\t0.268314123\t0.314813823\t0.285960048\t0.420773298\t0.471386194\t0.293376774\t0.370705307\t0.483323485\t0.367370248\t0.348949701\t0.314023614\t0.273735344\t0.307878315\t0.339754105\t0.310254842\t0.338190317\t0.281666756\t0.284521043\t0.374676406\t0.347614169\t0.283977091\t0.282218933\t0.279636741\t0.430244505\t0.302845687\t0.492945313\t0.516521394\t0.329783708\t0.469141394\t0.418096244\t0.462576002\t0.294865429\t0.262690276\t0.290700316\t0.371239513\t0.336142957\t0.285534441\t0.442958295\t0.38236168\t0.370398492\t0.287096888\t0.464701623\t0.326353073\t0.319914758\t0.236470357\t0.443599939\t0.335922897\t0.385153502\t0.420650452\t0.34529388\t0.348911822\t0.408096701\t0.312690467\t0.435196459\t0.254701406\t0.344132453\t0.451926976\t0.323329359\t0.372873515\t0.362950802\t0.307260454\t0.300192624\t0.427662998\t0.376149386\t0.351430953\t0.355849385\t0.353506327\t0.34639591\t0.392312974\t0.442695409\t0.490153372\t0.412812322\t0.406428576\t0.324505329\t0.364221454\t0.470091134\t0.476899028\t0.367132187\t0.463347226\t0.452807575\t0.424324483\t0.370465159\t0.384169787\t0.325911254\t0.401980102\t0.363251716\t0.478095204\t0.348142803\t0.355425835\t0.545923352\t0.346998304\t0.369107217\t0.445151865\t0.390957087\t0.374662936\t0.36343497\t0.403992444\t0.213263795\t0.515339136\t0.416166693\t0.42542842\t0.434459239\t0.381880075\t0.542788088\t0.513931513\t0.402198255\t0.445433348\t0.790848672\t0.435822785\t0.545852184\t0.490451515\t0.481582701\t0.398983002\t0.431019753\t0.343160957\t0.417061746\t0.326938868\t0.436096191\t0.392900705\t0.453928858\t0.368469834\t0.457851171\t0.327106386\t0.457420558\t0.417806894\t0.397515446\t0.426754832\t0.48979789\n", "Total lines examined: 62\n", "\n", "Attempting to extract gene data from matrix file...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Successfully extracted gene data with 54630 rows\n", "First 20 gene IDs:\n", "Index(['1007_s_at', '1053_at', '117_at', '121_at', '1255_g_at', '1294_at',\n", " '1316_at', '1320_at', '1405_i_at', '1431_at', '1438_at', '1487_at',\n", " '1494_f_at', '1552256_a_at', '1552257_a_at', '1552258_at', '1552261_at',\n", " '1552263_at', '1552264_a_at', '1552266_at'],\n", " dtype='object', name='ID')\n", "\n", "Gene expression data available: True\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", "# Add diagnostic code to check file content and structure\n", "print(\"Examining matrix file structure...\")\n", "with gzip.open(matrix_file, 'rt') as file:\n", " table_marker_found = False\n", " lines_read = 0\n", " for i, line in enumerate(file):\n", " lines_read += 1\n", " if '!series_matrix_table_begin' in line:\n", " table_marker_found = True\n", " print(f\"Found table marker at line {i}\")\n", " # Read a few lines after the marker to check data structure\n", " next_lines = [next(file, \"\").strip() for _ in range(5)]\n", " print(\"First few lines after marker:\")\n", " for next_line in next_lines:\n", " print(next_line)\n", " break\n", " if i < 10: # Print first few lines to see file structure\n", " print(f\"Line {i}: {line.strip()}\")\n", " if i > 100: # Don't read the entire file\n", " break\n", " \n", " if not table_marker_found:\n", " print(\"Table marker '!series_matrix_table_begin' not found in first 100 lines\")\n", " print(f\"Total lines examined: {lines_read}\")\n", "\n", "# 2. Try extracting gene expression data from the matrix file again with better diagnostics\n", "try:\n", " print(\"\\nAttempting to extract gene data from matrix file...\")\n", " gene_data = get_genetic_data(matrix_file)\n", " if gene_data.empty:\n", " print(\"Extracted gene expression data is empty\")\n", " is_gene_available = False\n", " else:\n", " print(f\"Successfully extracted gene data with {len(gene_data.index)} rows\")\n", " print(\"First 20 gene IDs:\")\n", " print(gene_data.index[:20])\n", " is_gene_available = True\n", "except Exception as e:\n", " print(f\"Error extracting gene data: {str(e)}\")\n", " print(\"This dataset appears to have an empty or malformed gene expression matrix\")\n", " is_gene_available = False\n", "\n", "print(f\"\\nGene expression data available: {is_gene_available}\")\n", "\n", "# If data extraction failed, try an alternative approach using pandas directly\n", "if not is_gene_available:\n", " print(\"\\nTrying alternative approach to read gene expression data...\")\n", " try:\n", " with gzip.open(matrix_file, 'rt') as file:\n", " # Skip lines until we find the marker\n", " for line in file:\n", " if '!series_matrix_table_begin' in line:\n", " break\n", " \n", " # Try to read the data directly with pandas\n", " gene_data = pd.read_csv(file, sep='\\t', index_col=0)\n", " \n", " if not gene_data.empty:\n", " print(f\"Successfully extracted gene data with alternative method: {gene_data.shape}\")\n", " print(\"First 20 gene IDs:\")\n", " print(gene_data.index[:20])\n", " is_gene_available = True\n", " else:\n", " print(\"Alternative extraction method also produced empty data\")\n", " except Exception as e:\n", " print(f\"Alternative extraction failed: {str(e)}\")\n" ] }, { "cell_type": "markdown", "id": "218bf164", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "ef873fa9", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:26:42.434207Z", "iopub.status.busy": "2025-03-25T07:26:42.434075Z", "iopub.status.idle": "2025-03-25T07:26:42.436027Z", "shell.execute_reply": "2025-03-25T07:26:42.435718Z" } }, "outputs": [], "source": [ "# Based on my examination of the gene identifiers, they are Affymetrix probe IDs \n", "# and need to be mapped to standard human gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "031af76d", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "7b95afa0", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:26:42.437205Z", "iopub.status.busy": "2025-03-25T07:26:42.437087Z", "iopub.status.idle": "2025-03-25T07:26:54.635643Z", "shell.execute_reply": "2025-03-25T07:26:54.635262Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Extracting gene annotation data from SOFT file...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Successfully extracted gene annotation data with 9888255 rows\n", "\n", "Gene annotation preview (first few rows):\n", "{'ID': ['1007_s_at', '1053_at', '117_at', '121_at', '1255_g_at'], 'GB_ACC': ['U48705', 'M87338', 'X51757', 'X69699', 'L36861'], 'SPOT_ID': [nan, nan, nan, nan, nan], 'Species Scientific Name': ['Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens'], 'Annotation Date': ['Oct 6, 2014', 'Oct 6, 2014', 'Oct 6, 2014', 'Oct 6, 2014', 'Oct 6, 2014'], 'Sequence Type': ['Exemplar sequence', 'Exemplar sequence', 'Exemplar sequence', 'Exemplar sequence', 'Exemplar sequence'], 'Sequence Source': ['Affymetrix Proprietary Database', 'GenBank', 'Affymetrix Proprietary Database', 'GenBank', 'Affymetrix Proprietary Database'], 'Target Description': ['U48705 /FEATURE=mRNA /DEFINITION=HSU48705 Human receptor tyrosine kinase DDR gene, complete cds', 'M87338 /FEATURE= /DEFINITION=HUMA1SBU Human replication factor C, 40-kDa subunit (A1) mRNA, complete cds', \"X51757 /FEATURE=cds /DEFINITION=HSP70B Human heat-shock protein HSP70B' gene\", 'X69699 /FEATURE= /DEFINITION=HSPAX8A H.sapiens Pax8 mRNA', 'L36861 /FEATURE=expanded_cds /DEFINITION=HUMGCAPB Homo sapiens guanylate cyclase activating protein (GCAP) gene exons 1-4, complete cds'], 'Representative Public ID': ['U48705', 'M87338', 'X51757', 'X69699', 'L36861'], 'Gene Title': ['discoidin domain receptor tyrosine kinase 1 /// microRNA 4640', 'replication factor C (activator 1) 2, 40kDa', \"heat shock 70kDa protein 6 (HSP70B')\", 'paired box 8', 'guanylate cyclase activator 1A (retina)'], 'Gene Symbol': ['DDR1 /// MIR4640', 'RFC2', 'HSPA6', 'PAX8', 'GUCA1A'], 'ENTREZ_GENE_ID': ['780 /// 100616237', '5982', '3310', '7849', '2978'], 'RefSeq Transcript ID': ['NM_001202521 /// NM_001202522 /// NM_001202523 /// NM_001954 /// NM_013993 /// NM_013994 /// NR_039783 /// XM_005249385 /// XM_005249386 /// XM_005249387 /// XM_005249389 /// XM_005272873 /// XM_005272874 /// XM_005272875 /// XM_005272877 /// XM_005275027 /// XM_005275028 /// XM_005275030 /// XM_005275031 /// XM_005275162 /// XM_005275163 /// XM_005275164 /// XM_005275166 /// XM_005275457 /// XM_005275458 /// XM_005275459 /// XM_005275461 /// XM_006715185 /// XM_006715186 /// XM_006715187 /// XM_006715188 /// XM_006715189 /// XM_006715190 /// XM_006725501 /// XM_006725502 /// XM_006725503 /// XM_006725504 /// XM_006725505 /// XM_006725506 /// XM_006725714 /// XM_006725715 /// XM_006725716 /// XM_006725717 /// XM_006725718 /// XM_006725719 /// XM_006725720 /// XM_006725721 /// XM_006725722 /// XM_006725827 /// XM_006725828 /// XM_006725829 /// XM_006725830 /// XM_006725831 /// XM_006725832 /// XM_006726017 /// XM_006726018 /// XM_006726019 /// XM_006726020 /// XM_006726021 /// XM_006726022 /// XR_427836 /// XR_430858 /// XR_430938 /// XR_430974 /// XR_431015', 'NM_001278791 /// NM_001278792 /// NM_001278793 /// NM_002914 /// NM_181471 /// XM_006716080', 'NM_002155', 'NM_003466 /// NM_013951 /// NM_013952 /// NM_013953 /// NM_013992', 'NM_000409 /// XM_006715073'], 'Gene Ontology Biological Process': ['0001558 // regulation of cell growth // inferred from electronic annotation /// 0001952 // regulation of cell-matrix adhesion // inferred from electronic annotation /// 0006468 // protein phosphorylation // inferred from electronic annotation /// 0007155 // cell adhesion // traceable author statement /// 0007169 // transmembrane receptor protein tyrosine kinase signaling pathway // inferred from electronic annotation /// 0007565 // female pregnancy // inferred from electronic annotation /// 0007566 // embryo implantation // inferred from electronic annotation /// 0007595 // lactation // inferred from electronic annotation /// 0008285 // negative regulation of cell proliferation // inferred from electronic annotation /// 0010715 // regulation of extracellular matrix disassembly // inferred from mutant phenotype /// 0014909 // smooth muscle cell migration // inferred from mutant phenotype /// 0016310 // phosphorylation // inferred from electronic annotation /// 0018108 // peptidyl-tyrosine phosphorylation // inferred from electronic annotation /// 0030198 // extracellular matrix organization // traceable author statement /// 0038063 // collagen-activated tyrosine kinase receptor signaling pathway // inferred from direct assay /// 0038063 // collagen-activated tyrosine kinase receptor signaling pathway // inferred from mutant phenotype /// 0038083 // peptidyl-tyrosine autophosphorylation // inferred from direct assay /// 0043583 // ear development // inferred from electronic annotation /// 0044319 // wound healing, spreading of cells // inferred from mutant phenotype /// 0046777 // protein autophosphorylation // inferred from direct assay /// 0060444 // branching involved in mammary gland duct morphogenesis // inferred from electronic annotation /// 0060749 // mammary gland alveolus development // inferred from electronic annotation /// 0061302 // smooth muscle cell-matrix adhesion // inferred from mutant phenotype', '0000278 // mitotic cell cycle // traceable author statement /// 0000722 // telomere maintenance via recombination // traceable author statement /// 0000723 // telomere maintenance // traceable author statement /// 0006260 // DNA replication // traceable author statement /// 0006271 // DNA strand elongation involved in DNA replication // traceable author statement /// 0006281 // DNA repair // traceable author statement /// 0006283 // transcription-coupled nucleotide-excision repair // traceable author statement /// 0006289 // nucleotide-excision repair // traceable author statement /// 0006297 // nucleotide-excision repair, DNA gap filling // traceable author statement /// 0015979 // photosynthesis // inferred from electronic annotation /// 0015995 // chlorophyll biosynthetic process // inferred from electronic annotation /// 0032201 // telomere maintenance via semi-conservative replication // traceable author statement', '0000902 // cell morphogenesis // inferred from electronic annotation /// 0006200 // ATP catabolic process // inferred from direct assay /// 0006950 // response to stress // inferred from electronic annotation /// 0006986 // response to unfolded protein // traceable author statement /// 0034605 // cellular response to heat // inferred from direct assay /// 0042026 // protein refolding // inferred from direct assay /// 0070370 // cellular heat acclimation // inferred from mutant phenotype', '0001655 // urogenital system development // inferred from sequence or structural similarity /// 0001656 // metanephros development // inferred from electronic annotation /// 0001658 // branching involved in ureteric bud morphogenesis // inferred from expression pattern /// 0001822 // kidney development // inferred from expression pattern /// 0001823 // mesonephros development // inferred from sequence or structural similarity /// 0003337 // mesenchymal to epithelial transition involved in metanephros morphogenesis // inferred from expression pattern /// 0006351 // transcription, DNA-templated // inferred from direct assay /// 0006355 // regulation of transcription, DNA-templated // inferred from electronic annotation /// 0007275 // multicellular organismal development // inferred from electronic annotation /// 0007417 // central nervous system development // inferred from expression pattern /// 0009653 // anatomical structure morphogenesis // traceable author statement /// 0030154 // cell differentiation // inferred from electronic annotation /// 0030878 // thyroid gland development // inferred from expression pattern /// 0030878 // thyroid gland development // inferred from mutant phenotype /// 0038194 // thyroid-stimulating hormone signaling pathway // traceable author statement /// 0039003 // pronephric field specification // inferred from sequence or structural similarity /// 0042472 // inner ear morphogenesis // inferred from sequence or structural similarity /// 0042981 // regulation of apoptotic process // inferred from sequence or structural similarity /// 0045893 // positive regulation of transcription, DNA-templated // inferred from direct assay /// 0045893 // positive regulation of transcription, DNA-templated // inferred from sequence or structural similarity /// 0045944 // positive regulation of transcription from RNA polymerase II promoter // inferred from direct assay /// 0048793 // pronephros development // inferred from sequence or structural similarity /// 0071371 // cellular response to gonadotropin stimulus // inferred from direct assay /// 0071599 // otic vesicle development // inferred from expression pattern /// 0072050 // S-shaped body morphogenesis // inferred from electronic annotation /// 0072073 // kidney epithelium development // inferred from electronic annotation /// 0072108 // positive regulation of mesenchymal to epithelial transition involved in metanephros morphogenesis // inferred from sequence or structural similarity /// 0072164 // mesonephric tubule development // inferred from electronic annotation /// 0072207 // metanephric epithelium development // inferred from expression pattern /// 0072221 // metanephric distal convoluted tubule development // inferred from sequence or structural similarity /// 0072278 // metanephric comma-shaped body morphogenesis // inferred from expression pattern /// 0072284 // metanephric S-shaped body morphogenesis // inferred from expression pattern /// 0072289 // metanephric nephron tubule formation // inferred from sequence or structural similarity /// 0072305 // negative regulation of mesenchymal cell apoptotic process involved in metanephric nephron morphogenesis // inferred from sequence or structural similarity /// 0072307 // regulation of metanephric nephron tubule epithelial cell differentiation // inferred from sequence or structural similarity /// 0090190 // positive regulation of branching involved in ureteric bud morphogenesis // inferred from sequence or structural similarity /// 1900212 // negative regulation of mesenchymal cell apoptotic process involved in metanephros development // inferred from sequence or structural similarity /// 1900215 // negative regulation of apoptotic process involved in metanephric collecting duct development // inferred from sequence or structural similarity /// 1900218 // negative regulation of apoptotic process involved in metanephric nephron tubule development // inferred from sequence or structural similarity /// 2000594 // positive regulation of metanephric DCT cell differentiation // inferred from sequence or structural similarity /// 2000611 // positive regulation of thyroid hormone generation // inferred from mutant phenotype /// 2000612 // regulation of thyroid-stimulating hormone secretion // inferred from mutant phenotype', '0007165 // signal transduction // non-traceable author statement /// 0007601 // visual perception // inferred from electronic annotation /// 0007602 // phototransduction // inferred from electronic annotation /// 0007603 // phototransduction, visible light // traceable author statement /// 0016056 // rhodopsin mediated signaling pathway // traceable author statement /// 0022400 // regulation of rhodopsin mediated signaling pathway // traceable author statement /// 0030828 // positive regulation of cGMP biosynthetic process // inferred from electronic annotation /// 0031282 // regulation of guanylate cyclase activity // inferred from electronic annotation /// 0031284 // positive regulation of guanylate cyclase activity // inferred from electronic annotation /// 0050896 // response to stimulus // inferred from electronic annotation'], 'Gene Ontology Cellular Component': ['0005576 // extracellular region // inferred from electronic annotation /// 0005615 // extracellular space // inferred from direct assay /// 0005886 // plasma membrane // traceable author statement /// 0005887 // integral component of plasma membrane // traceable author statement /// 0016020 // membrane // inferred from electronic annotation /// 0016021 // integral component of membrane // inferred from electronic annotation /// 0043235 // receptor complex // inferred from direct assay /// 0070062 // extracellular vesicular exosome // inferred from direct assay', '0005634 // nucleus // inferred from electronic annotation /// 0005654 // nucleoplasm // traceable author statement /// 0005663 // DNA replication factor C complex // inferred from direct assay', '0005737 // cytoplasm // inferred from direct assay /// 0005814 // centriole // inferred from direct assay /// 0005829 // cytosol // inferred from direct assay /// 0008180 // COP9 signalosome // inferred from direct assay /// 0070062 // extracellular vesicular exosome // inferred from direct assay /// 0072562 // blood microparticle // inferred from direct assay', '0005634 // nucleus // inferred from direct assay /// 0005654 // nucleoplasm // inferred from sequence or structural similarity /// 0005730 // nucleolus // inferred from direct assay', '0001750 // photoreceptor outer segment // inferred from electronic annotation /// 0001917 // photoreceptor inner segment // inferred from electronic annotation /// 0005578 // proteinaceous extracellular matrix // inferred from electronic annotation /// 0005886 // plasma membrane // inferred from direct assay /// 0016020 // membrane // inferred from electronic annotation /// 0097381 // photoreceptor disc membrane // traceable author statement'], 'Gene Ontology Molecular Function': ['0000166 // nucleotide binding // inferred from electronic annotation /// 0004672 // protein kinase activity // inferred from electronic annotation /// 0004713 // protein tyrosine kinase activity // inferred from electronic annotation /// 0004714 // transmembrane receptor protein tyrosine kinase activity // traceable author statement /// 0005515 // protein binding // inferred from physical interaction /// 0005518 // collagen binding // inferred from direct assay /// 0005518 // collagen binding // inferred from mutant phenotype /// 0005524 // ATP binding // inferred from electronic annotation /// 0016301 // kinase activity // inferred from electronic annotation /// 0016740 // transferase activity // inferred from electronic annotation /// 0016772 // transferase activity, transferring phosphorus-containing groups // inferred from electronic annotation /// 0038062 // protein tyrosine kinase collagen receptor activity // inferred from direct assay /// 0046872 // metal ion binding // inferred from electronic annotation', '0000166 // nucleotide binding // inferred from electronic annotation /// 0003677 // DNA binding // inferred from electronic annotation /// 0005515 // protein binding // inferred from physical interaction /// 0005524 // ATP binding // inferred from electronic annotation /// 0016851 // magnesium chelatase activity // inferred from electronic annotation /// 0017111 // nucleoside-triphosphatase activity // inferred from electronic annotation', '0000166 // nucleotide binding // inferred from electronic annotation /// 0005524 // ATP binding // inferred from electronic annotation /// 0019899 // enzyme binding // inferred from physical interaction /// 0031072 // heat shock protein binding // inferred from physical interaction /// 0042623 // ATPase activity, coupled // inferred from direct assay /// 0051082 // unfolded protein binding // inferred from direct assay', '0000979 // RNA polymerase II core promoter sequence-specific DNA binding // inferred from direct assay /// 0003677 // DNA binding // inferred from direct assay /// 0003677 // DNA binding // inferred from mutant phenotype /// 0003700 // sequence-specific DNA binding transcription factor activity // inferred from direct assay /// 0004996 // thyroid-stimulating hormone receptor activity // traceable author statement /// 0005515 // protein binding // inferred from physical interaction /// 0044212 // transcription regulatory region DNA binding // inferred from direct assay', '0005509 // calcium ion binding // inferred from electronic annotation /// 0008048 // calcium sensitive guanylate cyclase activator activity // inferred from electronic annotation /// 0030249 // guanylate cyclase regulator activity // inferred from electronic annotation /// 0046872 // metal ion binding // inferred from electronic annotation']}\n", "\n", "Column names in gene annotation data:\n", "['ID', 'GB_ACC', 'SPOT_ID', 'Species Scientific Name', 'Annotation Date', 'Sequence Type', 'Sequence Source', 'Target Description', 'Representative Public ID', 'Gene Title', 'Gene Symbol', 'ENTREZ_GENE_ID', 'RefSeq Transcript ID', 'Gene Ontology Biological Process', 'Gene Ontology Cellular Component', 'Gene Ontology Molecular Function']\n", "\n", "The dataset contains GenBank accessions (GB_ACC) that could be used for gene mapping.\n", "Number of rows with GenBank accessions: 9888193 out of 9888255\n", "\n", "The dataset contains genomic regions (SPOT_ID) that could be used for location-based gene mapping.\n", "Example SPOT_ID format: nan\n" ] } ], "source": [ "# 1. Extract gene annotation data from the SOFT file\n", "print(\"Extracting gene annotation data from SOFT file...\")\n", "try:\n", " # Use the library function to extract gene annotation\n", " gene_annotation = get_gene_annotation(soft_file)\n", " print(f\"Successfully extracted gene annotation data with {len(gene_annotation.index)} rows\")\n", " \n", " # Preview the annotation DataFrame\n", " print(\"\\nGene annotation preview (first few rows):\")\n", " print(preview_df(gene_annotation))\n", " \n", " # Show column names to help identify which columns we need for mapping\n", " print(\"\\nColumn names in gene annotation data:\")\n", " print(gene_annotation.columns.tolist())\n", " \n", " # Check for relevant mapping columns\n", " if 'GB_ACC' in gene_annotation.columns:\n", " print(\"\\nThe dataset contains GenBank accessions (GB_ACC) that could be used for gene mapping.\")\n", " # Count non-null values in GB_ACC column\n", " non_null_count = gene_annotation['GB_ACC'].count()\n", " print(f\"Number of rows with GenBank accessions: {non_null_count} out of {len(gene_annotation)}\")\n", " \n", " if 'SPOT_ID' in gene_annotation.columns:\n", " print(\"\\nThe dataset contains genomic regions (SPOT_ID) that could be used for location-based gene mapping.\")\n", " print(\"Example SPOT_ID format:\", gene_annotation['SPOT_ID'].iloc[0])\n", " \n", "except Exception as e:\n", " print(f\"Error processing gene annotation data: {e}\")\n", " is_gene_available = False\n" ] }, { "cell_type": "markdown", "id": "5ed80e87", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "70b8bd69", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:26:54.637033Z", "iopub.status.busy": "2025-03-25T07:26:54.636902Z", "iopub.status.idle": "2025-03-25T07:26:57.743907Z", "shell.execute_reply": "2025-03-25T07:26:57.743489Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Creating gene mapping from 'ID' to 'Gene Symbol'...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Created gene mapping with 45782 rows\n", "Gene mapping sample:\n", "{'ID': ['1007_s_at', '1053_at', '117_at', '121_at', '1255_g_at'], 'Gene': ['DDR1 /// MIR4640', 'RFC2', 'HSPA6', 'PAX8', 'GUCA1A']}\n", "\n", "Converting probe-level measurements to gene expression data...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Generated gene expression data with 21278 unique genes\n", "Gene expression data preview:\n", "{'GSM4830213': [0.221462756, 0.333740771, 0.212150149, 0.959113508, 0.401674867], 'GSM4830214': [0.294765294, 0.325517625, 0.132626891, 0.858857632, 0.533249497], 'GSM4830215': [0.058988549, 0.249811336, 0.19511625999999999, 0.9491526189999999, 0.459077477], 'GSM4830216': [0.267430305, 0.289917976, 0.141253118, 0.9247989059999999, 0.307557851], 'GSM4830217': [0.136383295, 0.203056037, 0.245214999, 1.010768578, 0.401036233], 'GSM4830218': [0.161734939, 0.263107061, 0.394205831, 0.9552827479999999, 0.618241847], 'GSM4830219': [0.056903966, 0.362911552, 0.133408297, 0.9444870059999999, 0.315838575], 'GSM4830220': [0.293876171, 0.291660756, 0.300348483, 0.944939867, 0.393645585], 'GSM4830221': [0.319997162, 0.340124398, 0.424134016, 0.933243335, 0.289862931], 'GSM4830222': [0.209602758, 0.305389136, 0.345813357, 0.9003249409999999, 0.553316236], 'GSM4830223': [0.211825371, 0.264213145, 0.362419479, 0.33431822099999997, 0.606410325], 'GSM4830224': [0.094467014, 0.202511892, 0.32887178699999997, 1.008458286, 0.490971684], 'GSM4830225': [0.2101814, 0.260165602, 0.335468695, 0.856983692, 0.428778112], 'GSM4830226': [0.039447811, 0.206972316, 0.25817510499999996, 1.027703911, 0.564603329], 'GSM4830227': [0.194105983, 0.263764143, 0.396269738, 0.903255761, 0.490721107], 'GSM4830228': [0.124067158, 0.220077321, 0.332958192, 1.024965823, 0.697267652], 'GSM4830229': [0.280289084, 0.280218959, 0.395074711, 1.161428124, 0.575704098], 'GSM4830230': [0.171324357, 0.204559505, 0.204197198, 0.8948514160000001, 0.523423433], 'GSM4830231': [0.121574193, 0.236837462, 0.22066026900000002, 0.903807968, 0.449015021], 'GSM4830232': [0.076741099, 0.279970884, 0.31694473300000003, 0.978895455, 0.53069073], 'GSM4830233': [0.222488195, 0.303480208, 0.238573737, 1.069523901, 0.604716659], 'GSM4830234': [0.048055112, 0.386794269, 0.28576687, 0.857771114, 0.452468604], 'GSM4830235': [0.072863385, 0.265091389, 0.207252532, 0.941209495, 0.568219066], 'GSM4830236': [0.150196195, 0.164197728, 0.27567866399999996, 1.0177003740000001, 0.534062862], 'GSM4830237': [0.084376991, 0.217051998, 0.334375426, 0.991428047, 0.545947075], 'GSM4830238': [0.123359978, 0.203093588, 0.285007327, 1.052132517, 0.541689456], 'GSM4830239': [0.117555909, 0.212206081, 0.338628769, 1.026856631, 0.604747295], 'GSM4830240': [0.134282023, 0.351685017, 0.317445383, 1.038540393, 0.489203215], 'GSM4830241': [0.108486757, 0.103649162, 0.167653948, 1.0072968599999998, 0.616286993], 'GSM4830242': [0.151090905, 0.302528769, 0.37847925800000004, 1.05283019, 0.590191364], 'GSM4830243': [0.03111821, 0.302404314, 0.243605375, 1.0003246959999998, 0.53803122], 'GSM4830244': [0.270763308, 0.094428882, 0.515821442, 1.235173136, 0.612750888], 'GSM4830245': [0.233690098, 0.404050946, 0.2901216, 0.815351278, 0.418141961], 'GSM4830246': [0.179830268, 0.337993711, 0.33065307200000005, 1.1139757929999998, 0.64225775], 'GSM4830247': [0.164420351, 0.298855007, 0.179975085, 1.0347308819999999, 0.48148942], 'GSM4830248': [0.163147733, 0.089282759, 0.227553159, 1.026110947, 0.518134296], 'GSM4830249': [0.249542102, 0.270287335, 0.262578033, 1.2278911469999998, 0.561269522], 'GSM4830250': [0.105372608, 0.279884636, 0.353654072, 0.924510151, 0.307889789], 'GSM4830251': [0.031048883, 0.324874252, 0.482140243, 0.959156126, 0.526877582], 'GSM4830252': [0.145725965, 0.308531463, 0.505238727, 1.0329554079999999, 0.499466836], 'GSM4830253': [0.204573616, 0.312239259, 0.294018463, 1.069379181, 0.471169949], 'GSM4830254': [0.318164796, 0.317544043, 0.06919972, 0.89979136, 0.356466293], 'GSM4830255': [0.361462772, 0.304729193, 0.173449334, 0.915542841, 0.288961083], 'GSM4830256': [0.28068307, 0.295996219, 0.332118571, 1.048332944, 0.625643551], 'GSM4830257': [0.2645998, 0.236579478, 0.200959533, 0.970357597, 0.409282565], 'GSM4830258': [0.124667451, 0.210608616, 0.26323984500000003, 0.9721418609999999, 0.526569664], 'GSM4830259': [0.050488852, 0.334897459, 0.31220115800000003, 0.913767606, 0.469463617], 'GSM4830260': [0.266589969, 0.290556967, 0.13953327199999999, 1.004850388, 0.384432793], 'GSM4830261': [0.232447833, 0.254383564, 0.165382214, 0.998026937, 0.344257176], 'GSM4830262': [0.262632996, 0.259865135, 0.141465977, 0.942347899, 0.402766168], 'GSM4830263': [0.431701571, 0.39383027, 0.246919758, 1.05172348, 0.343384951], 'GSM4830264': [0.3468225, 0.367052883, 0.22929406200000002, 0.897163123, 0.217796117], 'GSM4830265': [0.333169699, 0.20215562, 0.499894306, 0.9856649340000001, 0.327525467], 'GSM4830266': [0.308692038, 0.307123244, 0.311830498, 0.953827799, 0.361254394], 'GSM4830267': [0.206556961, 0.301229417, 0.26414609, 1.021580905, 0.524424374], 'GSM4830268': [0.282078117, 0.347790301, 0.228409097, 0.97902143, 0.611068487], 'GSM4830269': [0.227681205, 0.301466405, 0.459006071, 1.024870754, 0.541736126], 'GSM4830270': [0.248745352, 0.281547755, 0.466507003, 1.009949773, 0.353225589], 'GSM4830271': [0.166167736, 0.219060794, 0.224692136, 1.006958544, 0.512447834], 'GSM4830272': [0.124332972, 0.096941188, 0.21463948500000002, 0.8401282729999999, 0.472578287], 'GSM4830273': [0.160998344, 0.315454006, 0.322320223, 0.994921803, 0.57492578], 'GSM4830274': [0.388890743, 0.37339735, 0.31140332000000004, 1.008393407, 0.241822079], 'GSM4830275': [0.2547732, 0.23998256, 0.349549413, 0.894199341, 0.372994334], 'GSM4830276': [0.217095181, 0.341200083, 0.26273219999999997, 0.939626664, 0.320621461], 'GSM4830277': [0.08145196, 0.317485511, 0.318209693, 1.0573643449999999, 0.398954153], 'GSM4830278': [0.283683628, 0.298383862, 0.303095892, 1.0303143559999999, 0.363042355], 'GSM4830279': [0.153903261, 0.396583408, 0.28576836, 0.939837456, 0.373021185], 'GSM4830280': [0.243225843, 0.359474629, 0.251579792, 0.9093771279999999, 0.395853817], 'GSM4830281': [0.297733992, 0.260435164, 0.21047054199999998, 0.98510778, 0.425989538], 'GSM4830282': [0.227446824, 0.427208602, 0.229255766, 0.976190821, 0.416106075], 'GSM4830283': [0.186918974, 0.284216344, 0.18429484200000001, 1.0611720679999999, 0.371386558], 'GSM4830284': [0.130701721, 0.233986273, 0.25625951599999996, 1.045161844, 0.573936641], 'GSM4830285': [0.2437381, 0.333024025, 0.194880351, 0.920406341, 0.293332428], 'GSM4830286': [0.193161935, 0.243687868, 0.196207628, 0.9706963900000001, 0.339199334], 'GSM4830287': [0.176405862, 0.272066712, 0.245207503, 1.022407428, 0.462208599], 'GSM4830288': [0.152698204, 0.375581115, 0.203862145, 0.948229075, 0.407387137], 'GSM4830289': [0.295221627, 0.32615003, 0.301929727, 0.960350394, 0.344681531], 'GSM4830290': [0.032927066, 0.326102257, 0.306745306, 1.385020077, 0.748077691], 'GSM4830291': [0.078465112, 0.413660973, 0.565998971, 0.757055491, 0.479240745], 'GSM4830292': [0.204950035, 0.289444774, 0.20534586900000001, 1.005913973, 0.256911129], 'GSM4830293': [0.228815362, 0.289169908, 0.196365916, 1.020308257, 0.496128201], 'GSM4830294': [0.09723781, 0.395368278, 0.34924502700000004, 0.956931859, 0.478353471], 'GSM4830295': [0.067217432, 0.433659852, 0.384646989, 1.2794808149999999, 0.672909498], 'GSM4830296': [0.260559589, 0.240431875, 0.192324829, 1.022795811, 0.483793229], 'GSM4830297': [0.205783635, 0.365588456, 0.401243299, 0.855092793, 0.389437795], 'GSM4830298': [0.248268887, 0.368125051, 0.114881161, 1.146542997, 0.612750888], 'GSM4830299': [0.232487231, 0.276918203, 0.144530341, 1.018910528, 0.422235519], 'GSM4830300': [0.368390232, 0.317942172, 0.270381891, 0.996736199, 0.47122398], 'GSM4830301': [0.2690413, 0.1379942, 0.176406577, 0.9274280069999999, 0.335458696], 'GSM4830302': [0.168151349, 0.350416332, 0.38892003799999997, 1.060560315, 0.561168432], 'GSM4830303': [0.2945095, 0.271303326, 0.186732821, 0.952288076, 0.308737904], 'GSM4830304': [0.075032793, 0.29278931, 0.163867025, 0.875103891, 0.382499367], 'GSM4830305': [0.252562195, 0.195570886, 0.416911512, 1.037205547, 0.50853765], 'GSM4830306': [0.242894635, 0.323827863, 0.334683776, 1.114858151, 0.446811229], 'GSM4830307': [0.027610512, 0.323851615, 0.444310681, 1.074128807, 0.374204665], 'GSM4830308': [0.288488865, 0.174988016, 0.239385836, 1.041401595, 0.402227372], 'GSM4830309': [0.272225887, 0.33045873, 0.172804952, 1.059872508, 0.309036285], 'GSM4830310': [0.174667776, 0.317239851, 0.286726214, 0.966178953, 0.457782239], 'GSM4830311': [0.164954886, 0.354349762, 0.27268263, 0.961693361, 0.460360825], 'GSM4830312': [0.355759501, 0.334239572, 0.367454857, 0.946576521, 0.212225124], 'GSM4830313': [0.213197038, 0.305457443, 0.198742747, 1.040326297, 0.434635401], 'GSM4830314': [0.327585369, 0.323364943, 0.16492241600000002, 1.0423156919999998, 0.312748343], 'GSM4830315': [0.176939338, 0.321565509, 0.400816024, 0.929227888, 0.454201311], 'GSM4830316': [0.123030074, 0.223302066, 0.384448498, 1.023338109, 0.522339761], 'GSM4830317': [0.296392381, 0.353123546, 0.40285865899999995, 0.899989962, 0.306243956], 'GSM4830318': [0.055463273, 0.240771607, 0.41677986, 0.9414681789999999, 0.486381114], 'GSM4830319': [0.138752803, 0.273953646, 0.38539104199999996, 1.0031739769999999, 0.644790471], 'GSM4830320': [0.194884405, 0.319597125, 0.230587445, 0.49180999400000003, 0.224262699], 'GSM4830321': [0.18755202, 0.43604821, 0.728992701, 0.62324813, 0.389157176], 'GSM4830322': [0.291428745, 0.313153714, 0.148840201, 0.5873972780000001, 0.260362566], 'GSM4830323': [0.379167438, 0.356672138, 0.15731509700000001, 0.978160382, 0.444606185], 'GSM4830324': [0.247327328, 0.294976711, 0.307472825, 0.982325077, 0.314709157], 'GSM4830325': [0.137543604, 0.255491734, 0.199532285, 1.011100203, 0.335470885], 'GSM4830326': [0.317462116, 0.259409577, 0.191315729, 0.935151905, 0.312366486], 'GSM4830327': [0.344057649, 0.270297885, 0.247785807, 1.02232942, 0.319350243], 'GSM4830328': [0.268862039, 0.311015248, 0.18722447, 0.9510056379999999, 0.440322936], 'GSM4830329': [0.25048247, 0.304649591, 0.278870434, 0.970759064, 0.493567348], 'GSM4830330': [0.21202521, 0.208617434, 0.26322524999999997, 1.091099888, 0.332696438], 'GSM4830331': [0.187533453, 0.289125919, 0.25986192399999997, 1.1126969450000002, 0.450358242], 'GSM4830332': [0.175285816, 0.244794264, 0.23944406899999998, 0.955120296, 0.410278857], 'GSM4830333': [0.196183741, 0.284923971, 0.204323121, 1.039749861, 0.564091265], 'GSM4830334': [0.032876618, 0.194726795, 0.381866499, 1.041818589, 0.575784743], 'GSM4830335': [0.162409946, 0.24498561, 0.310806178, 0.860472709, 0.415651977], 'GSM4830336': [0.135924667, 0.336204112, 0.251797273, 0.862392575, 0.451166928], 'GSM4830337': [0.15428181, 0.405296445, 0.247117847, 0.970747709, 0.407490522], 'GSM4830338': [0.202680722, 0.225331888, 0.654211715, 0.9578442269999999, 0.506858528], 'GSM4830339': [0.082367837, 0.268219352, 0.292597815, 1.110461503, 0.623515189], 'GSM4830340': [0.113444477, 0.238616839, 0.215050898, 0.900360807, 0.527582765], 'GSM4830341': [0.067328535, 0.216265664, 0.27077578, 0.8318622560000001, 0.531383991], 'GSM4830342': [0.204314962, 0.319103509, 0.302353546, 1.185780107, 0.587285817], 'GSM4830343': [0.097551391, 0.284858614, 0.217249565, 0.860989496, 0.472216547], 'GSM4830344': [0.092239045, 0.233739451, 0.297389731, 1.122971952, 0.656874537], 'GSM4830345': [0.117521755, 0.22942245, 0.187641527, 1.045803368, 0.550907731], 'GSM4830346': [0.233621016, 0.238069504, 0.331377297, 0.922450185, 0.47402972], 'GSM4830347': [0.035722967, 0.287009358, 0.258614987, 0.98740378, 0.507573962], 'GSM4830348': [0.16138874, 0.262284249, 0.253337995, 1.000920028, 0.541829467], 'GSM4830349': [0.108108446, 0.215202823, 0.291522964, 1.051503122, 0.602701962], 'GSM4830350': [0.024679299, 0.211359292, 0.23475356400000003, 1.227296948, 0.661046147], 'GSM4830351': [0.103352115, 0.271939397, 0.219489232, 0.863041878, 0.480688304], 'GSM4830352': [0.092425406, 0.101816334, 0.205298133, 0.9570073189999999, 0.456767142], 'GSM4830353': [0.081324734, 0.22107625, 0.246153608, 1.062539965, 0.515634239], 'GSM4830354': [0.142692819, 0.399451852, 0.275503363, 0.920962289, 0.447527438], 'GSM4830355': [0.114509873, 0.275698155, 0.286552027, 0.91377753, 0.446412176], 'GSM4830356': [0.073552772, 0.233344764, 0.217617415, 0.964110106, 0.60352385], 'GSM4830357': [0.092239045, 0.31424427, 0.169982794, 0.9823534190000001, 0.549243033], 'GSM4830358': [0.163227767, 0.216572404, 1.138170481, 0.9963471589999999, 0.498772115], 'GSM4830359': [0.106663719, 0.22166577, 0.284623615, 0.9265404939999999, 0.477395296], 'GSM4830360': [0.104820661, 0.265761524, 0.221102342, 1.026982695, 0.492790014], 'GSM4830361': [0.109545484, 0.32209745, 0.381748199, 1.005369604, 0.531787395], 'GSM4830362': [0.143241242, 0.164108694, 0.32812465700000004, 0.877098426, 0.388400495], 'GSM4830363': [0.096119203, 0.277711779, 0.256582126, 1.2192947269999999, 0.611987352], 'GSM4830364': [0.140354022, 0.267966717, 0.141321674, 1.107405663, 0.554026306], 'GSM4830365': [0.13872245, 0.265761524, 0.183076732, 1.041585416, 0.596769631], 'GSM4830366': [0.03968085, 0.240292042, 0.26360047200000003, 1.168083012, 0.626835823], 'GSM4830367': [0.172231942, 0.239713132, 0.253486, 0.8640638300000001, 0.460621625], 'GSM4830368': [0.026380563, 0.199135035, 0.152023926, 0.952796787, 0.514183104], 'GSM4830369': [0.072539873, 0.371841401, 0.289325707, 1.047129124, 0.465372264], 'GSM4830370': [0.138843864, 0.258262098, 0.248815432, 0.9383412, 0.470055223], 'GSM4830371': [0.133107588, 0.282143086, 0.31253901100000003, 0.975765974, 0.564065695], 'GSM4830372': [0.161184654, 0.273575664, 0.199859112, 1.053886324, 0.578734994], 'GSM4830373': [0.199219003, 0.229128614, 0.5806389750000001, 1.1228440100000001, 0.591283679], 'GSM4830374': [0.109596521, 0.28176415, 0.45517008000000003, 0.998097032, 0.60980773], 'GSM4830375': [0.151266441, 0.25807628, 0.292277843, 0.880899519, 0.519350469], 'GSM4830376': [0.037883967, 0.298361421, 0.247576937, 0.8487127269999999, 0.512239575], 'GSM4830377': [0.186268359, 0.26032111, 0.31215552999999996, 0.9882819350000001, 0.570994377], 'GSM4830378': [0.078329273, 0.300237656, 0.266860008, 1.122605413, 0.540201247], 'GSM4830379': [0.150073454, 0.214848951, 0.257890359, 1.174374163, 0.614511371], 'GSM4830380': [0.133262962, 0.166596234, 0.33614975199999997, 1.0474704209999999, 0.573536813], 'GSM4830381': [0.117274135, 0.284303397, 0.217990868, 0.964205712, 0.49232465], 'GSM4830382': [0.123312838, 0.258582324, 0.377291456, 0.998492062, 0.519307673], 'GSM4830383': [0.19049409, 0.239045143, 0.24763817300000002, 1.005118609, 0.471620619], 'GSM4830384': [0.023133952, 0.267325163, 0.42960028299999997, 0.9623155299999999, 0.355785191], 'GSM4830385': [0.412609845, 0.139087215, 0.44237050499999997, 1.2162560820000001, 0.553462982], 'GSM4830386': [0.108848117, 0.164589584, 0.1952601, 1.063836902, 0.637291491], 'GSM4830387': [0.122045062, 0.238159031, 0.282766685, 0.93673417, 0.42830649], 'GSM4830388': [0.033897348, 0.333109021, 0.293056145, 0.932183206, 0.563427329], 'GSM4830389': [0.162116781, 0.227310151, 0.28153827, 0.8592199679999999, 0.458386004], 'GSM4830390': [0.138791829, 0.299922526, 0.301133007, 1.116986514, 0.561927438], 'GSM4830391': [0.147167221, 0.227329671, 0.220292724, 0.9417183100000001, 0.553071856], 'GSM4830392': [0.022195442, 0.265290231, 0.393707216, 1.081731856, 0.502343535]}\n", "\n", "Normalizing gene symbols...\n", "After normalization: 19845 unique genes\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Large_B-cell_Lymphoma/gene_data/GSE159472.csv\n" ] } ], "source": [ "# 1. Determine which columns to use for the gene mapping\n", "# From previous output, we can see that the gene expression data uses identifiers like '1007_s_at'\n", "# These correspond to the 'ID' column in the gene annotation dataframe\n", "# 'Gene Symbol' column contains the human gene symbols we need\n", "print(\"Creating gene mapping from 'ID' to 'Gene Symbol'...\")\n", "\n", "# 2. Extract the gene mapping dataframe with the two identified columns\n", "gene_mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Gene Symbol')\n", "print(f\"Created gene mapping with {len(gene_mapping_df)} rows\")\n", "print(\"Gene mapping sample:\")\n", "print(preview_df(gene_mapping_df))\n", "\n", "# 3. Apply gene mapping to convert probe-level measurements to gene-level expression\n", "print(\"\\nConverting probe-level measurements to gene expression data...\")\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping_df)\n", "print(f\"Generated gene expression data with {len(gene_data.index)} unique genes\")\n", "print(\"Gene expression data preview:\")\n", "print(preview_df(gene_data))\n", "\n", "# 4. Normalize gene symbols\n", "print(\"\\nNormalizing gene symbols...\")\n", "gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"After normalization: {len(gene_data.index)} unique genes\")\n", "\n", "# Save the gene data to 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": "2495e0b0", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "01fdda62", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:26:57.745329Z", "iopub.status.busy": "2025-03-25T07:26:57.745196Z", "iopub.status.idle": "2025-03-25T07:26:57.988166Z", "shell.execute_reply": "2025-03-25T07:26:57.987750Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Normalizing gene symbols...\n", "\n", "Linking clinical and gene expression data...\n", "Loaded clinical data with shape: (1, 180)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Loaded gene expression data with shape: (19845, 180)\n", "Created linked data with shape: (180, 19846)\n", "Columns in linked_data: [0, 'A1BG', 'A1BG-AS1', 'A1CF', 'A2M', 'A2M-AS1', 'A2ML1', 'A2MP1', 'A4GALT', 'A4GNT'] ... and more\n", "\n", "Handling missing values...\n", "Using trait column: GSM4830213\n", "Error in data linking process: ['GSM4830213']\n", "Abnormality detected in the cohort: GSE159472. Preprocessing failed.\n", "Dataset usability for Large_B-cell_Lymphoma association studies: False\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Traceback (most recent call last):\n", " File \"/tmp/ipykernel_63513/691907968.py\", line 41, in \n", " linked_data = handle_missing_values(linked_data, trait_col=trait_col)\n", " File \"/media/techt/DATA/GenoAgent/tools/preprocess.py\", line 430, in handle_missing_values\n", " df = df.dropna(subset=[trait_col])\n", " File \"/home/techt/anaconda3/envs/agent/lib/python3.10/site-packages/pandas/core/frame.py\", line 6670, in dropna\n", " raise KeyError(np.array(subset)[check].tolist())\n", "KeyError: ['GSM4830213']\n" ] } ], "source": [ "# 1. Normalize gene symbols in the obtained gene expression data\n", "print(\"\\nNormalizing gene symbols...\")\n", "# The normalization step was already completed in Step 6, so we can proceed directly to linking\n", "\n", "# 2. Link the clinical and genetic data\n", "print(\"\\nLinking clinical and gene expression data...\")\n", "try:\n", " # Load the clinical data that was processed in Step 2\n", " if os.path.exists(out_clinical_data_file):\n", " clinical_df = pd.read_csv(out_clinical_data_file)\n", " print(f\"Loaded clinical data with shape: {clinical_df.shape}\")\n", " is_trait_available = True\n", " else:\n", " print(f\"Clinical data file not found at {out_clinical_data_file}\")\n", " is_trait_available = False\n", " \n", " # Load gene expression data\n", " if os.path.exists(out_gene_data_file):\n", " gene_data = pd.read_csv(out_gene_data_file, index_col=0)\n", " print(f\"Loaded gene expression data with shape: {gene_data.shape}\")\n", " is_gene_available = True\n", " else:\n", " print(f\"Gene data file not found at {out_gene_data_file}\")\n", " is_gene_available = False\n", " \n", " # If both data types are available, proceed with linking\n", " if is_gene_available and is_trait_available:\n", " # Link the clinical and gene expression data\n", " linked_data = geo_link_clinical_genetic_data(clinical_df, gene_data)\n", " print(f\"Created linked data with shape: {linked_data.shape}\")\n", " \n", " # Print columns to debug\n", " print(\"Columns in linked_data:\", linked_data.columns.tolist()[:10], \"... and more\")\n", " \n", " # 3. Handle missing values\n", " print(\"\\nHandling missing values...\")\n", " # In our dataset, the trait is the first row in the dataframe after linking\n", " trait_col = linked_data.index[0] if not linked_data.empty else trait\n", " print(f\"Using trait column: {trait_col}\")\n", " \n", " linked_data = handle_missing_values(linked_data, trait_col=trait_col)\n", " print(f\"After handling missing values, linked data shape: {linked_data.shape}\")\n", " \n", " # 4. Determine if trait and demographic features are biased\n", " print(\"\\nChecking for biased features...\")\n", " is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait_col)\n", " \n", " # 5. Conduct final quality validation\n", " note = f\"Dataset contains gene expression data for Large_B-cell_Lymphoma with trait representing DLBCL subtypes (GCB=0, ABC=1).\"\n", " \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=is_trait_available,\n", " is_biased=is_biased,\n", " df=linked_data,\n", " note=note\n", " )\n", " \n", " # 6. Save linked data if usable\n", " if is_usable:\n", " print(f\"Dataset is usable. Saving linked data...\")\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " linked_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", " else:\n", " print(f\"Dataset is not usable for {trait} association studies due to bias.\")\n", " else:\n", " print(\"Cannot link data: either gene expression or clinical data is missing.\")\n", " # Create a small empty DataFrame as a placeholder for validation\n", " linked_data = pd.DataFrame()\n", " is_biased = True\n", " \n", " note = \"Dataset is missing either gene expression or clinical data for proper linkage.\"\n", " \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=is_trait_available,\n", " is_biased=is_biased,\n", " df=linked_data,\n", " note=note\n", " )\n", " \n", " print(f\"Dataset usability for {trait} association studies: {is_usable}\")\n", " \n", "except Exception as e:\n", " print(f\"Error in data linking process: {str(e)}\")\n", " import traceback\n", " traceback.print_exc() # Print detailed error information\n", " # Handle the error case for validation\n", " linked_data = pd.DataFrame()\n", " is_biased = True\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 if 'is_gene_available' in locals() else False,\n", " is_trait_available=is_trait_available if 'is_trait_available' in locals() else False,\n", " is_biased=is_biased,\n", " df=linked_data,\n", " note=f\"Error during data processing: {str(e)}\"\n", " )\n", " print(f\"Dataset usability for {trait} association studies: {is_usable}\")" ] } ], "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 }