{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "155f354d", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:25:34.704821Z", "iopub.status.busy": "2025-03-25T07:25:34.704526Z", "iopub.status.idle": "2025-03-25T07:25:34.865229Z", "shell.execute_reply": "2025-03-25T07:25:34.864764Z" } }, "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 = \"GSE142494\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Large_B-cell_Lymphoma\"\n", "in_cohort_dir = \"../../input/GEO/Large_B-cell_Lymphoma/GSE142494\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Large_B-cell_Lymphoma/GSE142494.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Large_B-cell_Lymphoma/gene_data/GSE142494.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Large_B-cell_Lymphoma/clinical_data/GSE142494.csv\"\n", "json_path = \"../../output/preprocess/Large_B-cell_Lymphoma/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "94f716d2", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "0c8cce82", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:25:34.866674Z", "iopub.status.busy": "2025-03-25T07:25:34.866525Z", "iopub.status.idle": "2025-03-25T07:25:35.077230Z", "shell.execute_reply": "2025-03-25T07:25:35.076620Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"A dichotomy of gene regulatory associations during the activated B-cell to plasmablast transition\"\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: memory B cells', 'cell type: total B cells'], 1: ['differentiation: Day3', 'differentiation: Day4', 'differentiation: Day5', 'differentiation: Day6', 'differentiation: Day0'], 2: ['culture-conditions: At day-3 cells were detached from the CD40L L-cell layer and reseeded at 1 X 10^5/ml in media supplemented with IL-2 (20 U/ml) and IL-21 (50 ng/ml).', 'culture-conditions: B cells were cultured at 2.5 X 10^5/ml with IL-2 (20 U/ml), IL-21 (50 ng/ml), F(ab’)2 goat anti-human IgM & IgG (10 µg/ml) on γ-irradiated CD40L expressing L cells (6.25 X 10^4/well).', 'culture-conditions: At day-3 cells were detached from the CD40L L-cell layer and reseeded at 1 X 10^5/ml in media supplemented with IL-2 (20 U/ml) and IL-21 (50 ng/ml) + 2 µM UNC0638 G9A inhibitor']}\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": "29fb969f", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "32ef1195", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:25:35.079047Z", "iopub.status.busy": "2025-03-25T07:25:35.078918Z", "iopub.status.idle": "2025-03-25T07:25:35.099131Z", "shell.execute_reply": "2025-03-25T07:25:35.098682Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "import os\n", "\n", "# 1. Gene Expression Data Availability\n", "# Based on the series summary and overall design, this appears to be a gene expression dataset\n", "# studying B-cell differentiation and gene regulation\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "\n", "# For trait (Large_B-cell_Lymphoma):\n", "# Looking at the sample characteristics, no explicit \"Lymphoma\" values are present\n", "# The keys in the dictionary are:\n", "# 0: 'cell type' - memory B cells, total B cells\n", "# 1: 'differentiation' - Day0 through Day6\n", "# 2: 'culture-conditions' - various treatment conditions\n", "# \n", "# This dataset appears to be about normal B-cell differentiation, not lymphoma\n", "trait_row = None\n", "\n", "# Age data is not available in the sample characteristics\n", "age_row = None\n", "\n", "# Gender data is not available in the sample characteristics\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion\n", "# Since trait data is not available, we'll define a placeholder function\n", "def convert_trait(value):\n", " return None\n", "\n", "# Similarly for age and gender\n", "def convert_age(value):\n", " return None\n", "\n", "def convert_gender(value):\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Conduct initial filtering on the usability of the dataset\n", "is_trait_available = trait_row is not None\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", "# Skip this step as trait_row is None, indicating clinical data for the trait is not available\n" ] }, { "cell_type": "markdown", "id": "e22cb672", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "3e8e0776", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:25:35.100946Z", "iopub.status.busy": "2025-03-25T07:25:35.100729Z", "iopub.status.idle": "2025-03-25T07:25:35.421988Z", "shell.execute_reply": "2025-03-25T07:25:35.421355Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Examining matrix file structure...\n", "Line 0: !Series_title\t\"A dichotomy of gene regulatory associations during the activated B-cell to plasmablast transition\"\n", "Line 1: !Series_geo_accession\t\"GSE142494\"\n", "Line 2: !Series_status\t\"Public on Aug 24 2020\"\n", "Line 3: !Series_submission_date\t\"Dec 21 2019\"\n", "Line 4: !Series_last_update_date\t\"Sep 21 2020\"\n", "Line 5: !Series_pubmed_id\t\"32843533\"\n", "Line 6: !Series_summary\t\"This SuperSeries is composed of the SubSeries listed below.\"\n", "Line 7: !Series_overall_design\t\"Refer to individual Series\"\n", "Line 8: !Series_type\t\"Expression profiling by array\"\n", "Line 9: !Series_type\t\"Genome binding/occupancy profiling by high throughput sequencing\"\n", "Found table marker at line 68\n", "First few lines after marker:\n", "\"ID_REF\"\t\"GSM4230397\"\t\"GSM4230398\"\t\"GSM4230399\"\t\"GSM4230400\"\t\"GSM4230401\"\t\"GSM4230402\"\t\"GSM4230403\"\t\"GSM4230404\"\t\"GSM4230405\"\t\"GSM4230406\"\t\"GSM4230407\"\t\"GSM4230408\"\t\"GSM4230409\"\t\"GSM4230410\"\t\"GSM4230411\"\t\"GSM4230412\"\t\"GSM4230413\"\t\"GSM4230414\"\t\"GSM4230415\"\t\"GSM4230416\"\t\"GSM4230417\"\t\"GSM4230418\"\t\"GSM4230419\"\t\"GSM4230420\"\t\"GSM4230421\"\t\"GSM4230422\"\t\"GSM4230423\"\t\"GSM4230424\"\t\"GSM4230425\"\t\"GSM4230426\"\t\"GSM4230427\"\t\"GSM4230428\"\t\"GSM4230429\"\t\"GSM4230430\"\t\"GSM4230431\"\t\"GSM4230432\"\t\"GSM4230433\"\t\"GSM4230434\"\t\"GSM4230435\"\t\"GSM4230436\"\t\"GSM4230437\"\t\"GSM4230438\"\t\"GSM4230439\"\t\"GSM4230440\"\t\"GSM4230441\"\t\"GSM4230442\"\t\"GSM4230443\"\t\"GSM4230444\"\t\"GSM4230445\"\t\"GSM4230446\"\t\"GSM4230447\"\t\"GSM4230448\"\t\"GSM4230449\"\t\"GSM4230450\"\t\"GSM4230451\"\t\"GSM4230452\"\t\"GSM4230453\"\t\"GSM4230454\"\t\"GSM4230455\"\t\"GSM4230456\"\t\"GSM4230457\"\t\"GSM4230458\"\t\"GSM4230459\"\n", "\"ILMN_1343291\"\t14.55147924\t14.81168139\t14.82270555\t14.83643405\t14.89342205\t14.90520268\t14.72048208\t14.89751067\t14.8551658\t14.96969175\t14.75307162\t14.73907085\t14.81168139\t15.05648912\t14.92970056\t14.91483571\t14.88977916\t14.71706525\t14.86882597\t15.05648912\t14.8869085\t14.61711825\t14.35108044\t14.69932632\t14.63144466\t14.57757232\t14.67450133\t14.65322374\t14.6103382\t14.77190989\t14.56477815\t14.50304598\t14.70587722\t14.55893962\t14.6103382\t14.54580677\t14.62179709\t14.56766691\t14.64031437\t14.71223372\t14.66778672\t14.63144466\t14.64844893\t14.69932632\t14.62179709\t14.58430743\t14.67450133\t14.60692886\t14.4989825\t14.69932632\t14.54580677\t14.48110456\t14.75832068\t14.56766691\t14.37100571\t14.77190989\t14.55132374\t14.48500262\t14.73353238\t14.56163747\t14.50304598\t14.59968264\t14.73353238\n", "\"ILMN_1343295\"\t14.39826177\t14.94617571\t14.5957837\t14.68162204\t14.63183528\t14.62005448\t14.78666389\t14.77933586\t14.73702456\t14.80593766\t14.86882597\t14.4637816\t14.68162204\t14.67944003\t14.45018721\t14.78666389\t14.71250224\t14.61637154\t14.6223871\t14.67785075\t14.4031209\t11.89074328\t11.89201877\t11.2586541\t14.21156868\t14.20073922\t13.90641356\t14.77190989\t14.24558946\t14.02911154\t14.29538239\t14.20842379\t13.78736398\t13.95900716\t14.17341285\t13.92951722\t14.09975902\t14.15978734\t14.03308736\t14.08436344\t14.13668383\t14.1554926\t14.08231664\t13.99003266\t13.9245709\t14.62179709\t14.19598736\t13.90641356\t14.6103382\t14.19927329\t13.9222473\t14.29538239\t14.07410972\t13.77911928\t14.14444266\t14.1250663\t13.77911928\t14.46661138\t14.14251135\t13.97290301\t14.49199503\t14.00004317\t13.99815104\n", "\"ILMN_1651199\"\t8.154667822\t8.254253964\t8.117098706\t8.172390763\t8.041502851\t8.067939938\t8.056763466\t8.05015287\t8.062052826\t7.978582148\t8.154573671\t8.090742212\t8.115748726\t8.172851571\t8.133514808\t8.05356189\t8.124912998\t8.072060232\t8.109236137\t8.07974707\t8.094306904\t7.852889714\t7.880278932\t8.02203813\t8.014542103\t7.963347041\t7.964918905\t8.055722007\t7.973551429\t7.896460642\t7.988059454\t7.866417343\t7.988308813\t7.91015544\t8.030403508\t7.918004393\t7.961127346\t7.926003328\t8.020852763\t7.991060303\t7.930759864\t7.978884563\t7.923977442\t8.02539816\t7.962577327\t7.927598096\t7.952171961\t8.040447228\t7.931614955\t8.018637646\t7.891251282\t7.923592661\t7.95688915\t7.901738868\t8.055746617\t7.935137738\t7.910721895\t8.034324785\t7.925861574\t7.902920539\t7.97537656\t7.894097339\t7.98849228\n", "\"ILMN_1651209\"\t8.302169633\t8.372392696\t8.324325112\t8.223849722\t8.370165614\t8.09432187\t8.203535469\t8.25537799\t8.234631998\t8.238641039\t8.368576106\t8.206575023\t8.255353222\t8.136348788\t8.215482396\t8.308260323\t8.350009601\t8.336332713\t8.426433886\t8.380289833\t8.29428226\t8.228445245\t8.052417455\t8.197050334\t8.183114299\t8.112830823\t7.984234267\t8.293775678\t8.126529995\t8.064277592\t8.049384156\t8.277571068\t8.041587954\t8.073861203\t8.241332815\t8.04730762\t8.105163325\t8.181055492\t8.186281386\t8.025200676\t8.16695889\t8.064954598\t8.147590315\t8.107260935\t8.08988242\t8.117625932\t8.14041193\t7.999278431\t8.138256079\t8.041739929\t7.96609892\t8.102477045\t8.27871065\t8.105309282\t8.196737783\t8.04549087\t8.055548225\t8.277090915\t8.104499063\t7.986251759\t8.05614039\t8.314316439\t8.09474635\n", "Total lines examined: 69\n", "\n", "Attempting to extract gene data from matrix file...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Successfully extracted gene data with 47323 rows\n", "First 20 gene IDs:\n", "Index(['ILMN_1343291', 'ILMN_1343295', 'ILMN_1651199', 'ILMN_1651209',\n", " 'ILMN_1651210', 'ILMN_1651221', 'ILMN_1651228', 'ILMN_1651229',\n", " 'ILMN_1651230', 'ILMN_1651232', 'ILMN_1651235', 'ILMN_1651236',\n", " 'ILMN_1651237', 'ILMN_1651238', 'ILMN_1651249', 'ILMN_1651253',\n", " 'ILMN_1651254', 'ILMN_1651259', 'ILMN_1651260', 'ILMN_1651262'],\n", " dtype='object', name='ID')\n", "\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": "3f3230cd", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "2fa4f613", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:25:35.423850Z", "iopub.status.busy": "2025-03-25T07:25:35.423708Z", "iopub.status.idle": "2025-03-25T07:25:35.426205Z", "shell.execute_reply": "2025-03-25T07:25:35.425722Z" } }, "outputs": [], "source": [ "# Examining the gene identifiers\n", "# The IDs start with \"ILMN_\" which indicates these are Illumina probe IDs\n", "# These are not standard human gene symbols but probe identifiers from an Illumina microarray platform\n", "# They need to be mapped to gene symbols for biological interpretation\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "90a298fd", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "74a5180f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:25:35.427978Z", "iopub.status.busy": "2025-03-25T07:25:35.427838Z", "iopub.status.idle": "2025-03-25T07:25:41.840640Z", "shell.execute_reply": "2025-03-25T07:25:41.839958Z" } }, "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 3029519 rows\n", "\n", "Gene annotation preview (first few rows):\n", "{'ID': ['ILMN_1343048', 'ILMN_1343049', 'ILMN_1343050', 'ILMN_1343052', 'ILMN_1343059'], 'Species': [nan, nan, nan, nan, nan], 'Source': [nan, nan, nan, nan, nan], 'Search_Key': [nan, nan, nan, nan, nan], 'Transcript': [nan, nan, nan, nan, nan], 'ILMN_Gene': [nan, nan, nan, nan, nan], 'Source_Reference_ID': [nan, nan, nan, nan, nan], 'RefSeq_ID': [nan, nan, nan, nan, nan], 'Unigene_ID': [nan, nan, nan, nan, nan], 'Entrez_Gene_ID': [nan, nan, nan, nan, nan], 'GI': [nan, nan, nan, nan, nan], 'Accession': [nan, nan, nan, nan, nan], 'Symbol': ['phage_lambda_genome', 'phage_lambda_genome', 'phage_lambda_genome:low', 'phage_lambda_genome:low', 'thrB'], 'Protein_Product': [nan, nan, nan, nan, 'thrB'], 'Probe_Id': [nan, nan, nan, nan, nan], 'Array_Address_Id': [5090180.0, 6510136.0, 7560739.0, 1450438.0, 1240647.0], 'Probe_Type': [nan, nan, nan, nan, nan], 'Probe_Start': [nan, nan, nan, nan, nan], 'SEQUENCE': ['GAATAAAGAACAATCTGCTGATGATCCCTCCGTGGATCTGATTCGTGTAA', 'CCATGTGATACGAGGGCGCGTAGTTTGCATTATCGTTTTTATCGTTTCAA', 'CCGACAGATGTATGTAAGGCCAACGTGCTCAAATCTTCATACAGAAAGAT', 'TCTGTCACTGTCAGGAAAGTGGTAAAACTGCAACTCAATTACTGCAATGC', 'CTTGTGCCTGAGCTGTCAAAAGTAGAGCACGTCGCCGAGATGAAGGGCGC'], 'Chromosome': [nan, nan, nan, nan, nan], 'Probe_Chr_Orientation': [nan, nan, nan, nan, nan], 'Probe_Coordinates': [nan, nan, nan, nan, nan], 'Cytoband': [nan, nan, nan, nan, nan], 'Definition': [nan, nan, nan, nan, nan], 'Ontology_Component': [nan, nan, nan, nan, nan], 'Ontology_Process': [nan, nan, nan, nan, nan], 'Ontology_Function': [nan, nan, nan, nan, nan], 'Synonyms': [nan, nan, nan, nan, nan], 'Obsolete_Probe_Id': [nan, nan, nan, nan, nan], 'GB_ACC': [nan, nan, nan, nan, nan]}\n", "\n", "Column names in gene annotation data:\n", "['ID', 'Species', 'Source', 'Search_Key', 'Transcript', 'ILMN_Gene', 'Source_Reference_ID', 'RefSeq_ID', 'Unigene_ID', 'Entrez_Gene_ID', 'GI', 'Accession', 'Symbol', 'Protein_Product', 'Probe_Id', 'Array_Address_Id', 'Probe_Type', 'Probe_Start', 'SEQUENCE', 'Chromosome', 'Probe_Chr_Orientation', 'Probe_Coordinates', 'Cytoband', 'Definition', 'Ontology_Component', 'Ontology_Process', 'Ontology_Function', 'Synonyms', 'Obsolete_Probe_Id', 'GB_ACC']\n", "\n", "The dataset contains GenBank accessions (GB_ACC) that could be used for gene mapping.\n", "Number of rows with GenBank accessions: 47323 out of 3029519\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": "8cb1fe7f", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "a192ba3a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:25:41.843048Z", "iopub.status.busy": "2025-03-25T07:25:41.842890Z", "iopub.status.idle": "2025-03-25T07:25:43.006795Z", "shell.execute_reply": "2025-03-25T07:25:43.006166Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Creating gene mapping from probe IDs to gene symbols...\n", "Created mapping with 44837 rows\n", "First few rows of mapping data:\n", "{'ID': ['ILMN_1343048', 'ILMN_1343049', 'ILMN_1343050', 'ILMN_1343052', 'ILMN_1343059'], 'Gene': ['phage_lambda_genome', 'phage_lambda_genome', 'phage_lambda_genome:low', 'phage_lambda_genome:low', 'thrB']}\n", "\n", "Probes in expression data: 47323\n", "Probes in mapping data: 44837\n", "Overlap (probes that can be mapped): 44053\n", "Mapping coverage: 93.09%\n", "\n", "Converting probe-level measurements to gene expression data...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Converted to gene expression data with 21464 genes\n", "\n", "Normalizing gene symbols...\n", "After normalization: 20259 unique genes\n", "\n", "Preview of gene expression data:\n", "{'GSM4230397': [16.650590606, 8.216769318, 24.619701452, 8.020100477, 7.968417558], 'GSM4230398': [16.303410088, 8.29990581, 24.672474215, 8.001034902, 7.972244982], 'GSM4230399': [16.474239626, 8.293178378, 24.604444522, 7.998611331, 8.06327577], 'GSM4230400': [16.404515596, 8.244805096, 24.583375097, 7.983330304, 8.091351085], 'GSM4230401': [16.516765953, 8.200109698, 24.563564699, 7.981676863, 7.967531026], 'GSM4230402': [16.549311276, 8.206625807, 24.44487138, 7.986913154, 7.920091144], 'GSM4230403': [16.624553708, 8.172420111, 24.692044333, 8.00338877, 8.152064676], 'GSM4230404': [16.347913586, 8.247254959, 24.724101554999997, 8.054338462, 7.987656143], 'GSM4230405': [16.470867104, 8.112620089, 24.460172373, 8.039134877, 8.019603193], 'GSM4230406': [16.665684482, 8.273386579, 24.496097482, 8.029102956, 7.995906883], 'GSM4230407': [16.429601418, 8.226960365, 24.626972898, 8.015709188, 7.99476494], 'GSM4230408': [16.563260893, 8.335633619, 24.610417655, 7.980078733, 8.089626578], 'GSM4230409': [16.447659209, 8.225401909, 24.550662627999998, 8.020439435, 8.013701833], 'GSM4230410': [16.520880883, 8.306464817, 24.531399995, 8.069645205, 7.97868844], 'GSM4230411': [16.360069531999997, 8.301948634, 24.689043308000002, 7.994319176, 7.958702837], 'GSM4230412': [16.581139845000003, 8.358792568, 24.653843856, 8.081836159, 7.968675229], 'GSM4230413': [16.387676317999997, 8.241874452, 24.657202593, 8.073467694, 8.005912266], 'GSM4230414': [16.427326221999998, 8.177448252, 24.661769802000002, 8.225068927, 7.968579479], 'GSM4230415': [16.540581017, 8.201955194, 24.581263289, 8.057018991, 8.017384228], 'GSM4230416': [16.553386213, 8.372181694, 24.6043163, 7.998952584, 8.100128454], 'GSM4230417': [16.459027015, 8.129040896, 24.53395929, 8.233357303, 8.098967601], 'GSM4230418': [16.077728909, 8.010547025, 23.870185044, 7.904594291, 7.989107773], 'GSM4230419': [16.375544929999997, 8.168511527, 24.163198034, 7.878622332, 7.875756065], 'GSM4230420': [16.040185385, 7.942461041, 23.80336674, 7.980207988, 8.050030245], 'GSM4230421': [16.103696609, 8.056687973, 24.117085878, 7.956983164, 7.878969015], 'GSM4230422': [15.977022504, 8.047286422, 23.977923408000002, 7.824197908, 7.826257052], 'GSM4230423': [16.009684414, 8.19113902, 23.994667539, 8.352262098, 7.899285057], 'GSM4230424': [16.859170288, 8.006027021, 24.381993792, 7.864219271, 7.924367822], 'GSM4230425': [16.162901664, 7.996409356, 24.091300018000002, 7.934324406, 7.93171678], 'GSM4230426': [15.858749382, 7.91341747, 23.999307128, 8.487340166, 7.882712343], 'GSM4230427': [16.161524474, 8.015955625, 24.082412232, 7.961478929, 7.892302916], 'GSM4230428': [16.181170998, 8.127447091, 24.011332872, 7.900198484, 7.888587826], 'GSM4230429': [16.194600029, 7.991674478, 23.897426818, 8.4590109, 7.96201842], 'GSM4230430': [16.076638422000002, 8.077791396, 23.98127888, 7.883680876, 7.856462075], 'GSM4230431': [16.089039184, 8.132880464, 23.991048489, 7.861266703, 7.875566812], 'GSM4230432': [16.166617367, 8.046146171, 23.985090032, 8.426118778, 7.916720539], 'GSM4230433': [16.215540685, 8.208945039, 23.957595084, 7.921199366, 7.90334983], 'GSM4230434': [16.286927006, 8.064301398, 24.160786463999997, 7.892412567, 7.903162649], 'GSM4230435': [16.101256583, 8.056482475, 24.019937405, 7.995624713, 7.935775684], 'GSM4230436': [16.228950668, 8.112264283, 24.121690957, 7.878325322, 7.952292592], 'GSM4230437': [16.32668052, 8.140669471, 24.100850728, 7.942557646, 7.93464238], 'GSM4230438': [16.321436129, 8.046286026, 24.048563737000002, 8.046604945, 7.941979188], 'GSM4230439': [16.233290095999998, 8.014408089, 23.942160681, 7.90803857, 7.95547245], 'GSM4230440': [16.168128882, 8.04499368, 24.179942263, 7.912517658, 7.860994953], 'GSM4230441': [16.0807408, 8.061328124, 24.038129685999998, 7.993277484, 7.973673242], 'GSM4230442': [16.186826089, 8.020099611, 24.004626256999998, 7.969868417, 7.858787669], 'GSM4230443': [16.054356997, 8.045015017, 23.970867683999998, 7.907534652, 7.873661399], 'GSM4230444': [16.102144903000003, 7.970873949, 23.943696325, 8.562098774, 7.881390732], 'GSM4230445': [16.205739628, 8.063045575, 23.976398834999998, 7.931156307, 7.964812312], 'GSM4230446': [16.1574648, 8.005739253, 23.968787285999998, 7.922122245, 7.915285973], 'GSM4230447': [16.008111549, 7.984894537, 23.91286092, 8.460293234, 7.868452001], 'GSM4230448': [16.182552838, 8.097536707, 24.145585353, 8.04499368, 7.949139599], 'GSM4230449': [16.17219345, 8.072972144, 24.122812904, 7.921508285, 7.946762724], 'GSM4230450': [16.133763626, 8.08662619, 23.940094473, 8.246794029, 7.879707438], 'GSM4230451': [16.108662966, 8.109120013, 24.058960142, 7.84966012, 7.836300535], 'GSM4230452': [16.059842656, 8.032395864, 23.877550612, 7.903793684, 7.942116729], 'GSM4230453': [16.067638171, 8.180967739, 24.071922489000002, 8.073133622, 7.869245143], 'GSM4230454': [16.196038754, 8.023884202, 24.088539531000002, 7.916073963, 7.868386023], 'GSM4230455': [16.248037752, 8.108994395, 23.870983742, 7.915301844, 7.912018773], 'GSM4230456': [16.059546874, 8.046876359, 23.974519076999997, 7.927480384, 7.898387673], 'GSM4230457': [16.085696232, 8.106723293, 24.009558402, 7.849308743, 7.866521071], 'GSM4230458': [16.114674522999998, 8.090957625, 24.136028191999998, 7.993724248, 7.946883914], 'GSM4230459': [16.146703806, 8.174756269, 23.922202478, 7.866417343, 7.918852436]}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene expression data saved to ../../output/preprocess/Large_B-cell_Lymphoma/gene_data/GSE142494.csv\n" ] } ], "source": [ "# 1. Identify columns for gene ID mapping\n", "# From the preview, we can see we need to map from 'ID' (probe identifiers) to 'Symbol' (gene symbols)\n", "print(\"\\nCreating gene mapping from probe IDs to gene symbols...\")\n", "\n", "# 2. Extract mapping between probe IDs and gene symbols\n", "mapping_df = get_gene_mapping(gene_annotation, 'ID', 'Symbol')\n", "print(f\"Created mapping with {len(mapping_df)} rows\")\n", "print(\"First few rows of mapping data:\")\n", "print(preview_df(mapping_df))\n", "\n", "# Check for mapping coverage\n", "probe_ids_in_expr = set(gene_data.index)\n", "probe_ids_in_map = set(mapping_df['ID'])\n", "overlap = probe_ids_in_expr.intersection(probe_ids_in_map)\n", "print(f\"\\nProbes in expression data: {len(probe_ids_in_expr)}\")\n", "print(f\"Probes in mapping data: {len(probe_ids_in_map)}\")\n", "print(f\"Overlap (probes that can be mapped): {len(overlap)}\")\n", "print(f\"Mapping coverage: {len(overlap)/len(probe_ids_in_expr):.2%}\")\n", "\n", "# 3. Apply the mapping to convert probe-level data to gene expression data\n", "print(\"\\nConverting probe-level measurements to gene expression data...\")\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "print(f\"Converted to gene expression data with {len(gene_data)} genes\")\n", "\n", "# Normalize gene symbols to handle synonyms\n", "print(\"\\nNormalizing gene symbols...\")\n", "gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"After normalization: {len(gene_data)} unique genes\")\n", "\n", "# Preview the resulting gene expression data\n", "print(\"\\nPreview of gene expression data:\")\n", "print(preview_df(gene_data))\n", "\n", "# Save the gene expression data to a CSV 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\"\\nGene expression data saved to {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "b9c5a253", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "6d8a9177", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:25:43.008678Z", "iopub.status.busy": "2025-03-25T07:25:43.008549Z", "iopub.status.idle": "2025-03-25T07:25:43.097055Z", "shell.execute_reply": "2025-03-25T07:25:43.096377Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Checking normalized gene data availability...\n", "Loading normalized gene data from ../../output/preprocess/Large_B-cell_Lymphoma/gene_data/GSE142494.csv...\n", "Loaded gene data with shape: (20259, 63)\n", "Sample of gene symbols: ['A1BG', 'A1BG-AS1', 'A1CF', 'A2M', 'A2ML1']\n", "\n", "Checking clinical data availability...\n", "No clinical data available for this cohort. Cannot proceed with linking.\n", "Created diagnostic data frame with shape: (63, 20259)\n", "\n", "Performing final validation...\n", "\n", "Dataset usability for Large_B-cell_Lymphoma association studies: False\n", "Reason: Dataset contains gene expression data but lacks clinical information for Large_B-cell_Lymphoma. The samples appear to be B-cells at different stages of differentiation, not lymphoma cases.\n" ] } ], "source": [ "# 1. Gene data was already normalized and saved in the previous step\n", "print(\"\\nChecking normalized gene data availability...\")\n", "try:\n", " # Load the already normalized gene data from the correct path\n", " if os.path.exists(out_gene_data_file):\n", " print(f\"Loading normalized gene data from {out_gene_data_file}...\")\n", " gene_data = pd.read_csv(out_gene_data_file, index_col=0)\n", " print(f\"Loaded gene data with shape: {gene_data.shape}\")\n", " print(f\"Sample of gene symbols: {gene_data.index[:5].tolist()}\")\n", " is_gene_available = True\n", " else:\n", " print(f\"ERROR: Normalized gene data file not found at {out_gene_data_file}\")\n", " is_gene_available = False\n", "except Exception as e:\n", " print(f\"Error loading normalized gene data: {str(e)}\")\n", " is_gene_available = False\n", "\n", "# 2. Check clinical data availability\n", "print(\"\\nChecking clinical data availability...\")\n", "# From previous steps, we determined that trait_row was None\n", "trait_row = None\n", "is_trait_available = trait_row is not None\n", "\n", "if is_trait_available:\n", " print(\"Clinical data is available for this cohort.\")\n", " # This block won't execute since trait_row is None\n", "else:\n", " print(\"No clinical data available for this cohort. Cannot proceed with linking.\")\n", "\n", "# 3. Since clinical data is not available, we can't create proper linked data\n", "# But we'll use the gene data for diagnostic purposes\n", "linked_data = gene_data.T if is_gene_available else pd.DataFrame()\n", "print(f\"Created diagnostic data frame with shape: {linked_data.shape}\")\n", "\n", "# 4. Since we can't evaluate trait bias without trait data, mark as biased\n", "is_biased = True\n", "\n", "# 5. Validate and save cohort information\n", "print(\"\\nPerforming final validation...\")\n", "note = \"Dataset contains gene expression data but lacks clinical information for Large_B-cell_Lymphoma. \" \\\n", " \"The samples appear to be B-cells at different stages of differentiation, not lymphoma cases.\"\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. Report final status - we don't save the linked data because it's not usable\n", "print(f\"\\nDataset usability for {trait} association studies: {is_usable}\")\n", "if not is_usable:\n", " print(f\"Reason: {note}\")\n", "else:\n", " # This block won't execute since is_usable should be False\n", " print(f\"Saving linked data to {out_data_file}\")\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " linked_data.to_csv(out_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 }