{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0b44c77f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:52:28.142305Z", "iopub.status.busy": "2025-03-25T07:52:28.142133Z", "iopub.status.idle": "2025-03-25T07:52:28.307846Z", "shell.execute_reply": "2025-03-25T07:52:28.307502Z" } }, "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 = \"Lung_Cancer\"\n", "cohort = \"GSE249568\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Lung_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Lung_Cancer/GSE249568\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Lung_Cancer/GSE249568.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Lung_Cancer/gene_data/GSE249568.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Lung_Cancer/clinical_data/GSE249568.csv\"\n", "json_path = \"../../output/preprocess/Lung_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "e6bf3a38", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "f9afa40d", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:52:28.309328Z", "iopub.status.busy": "2025-03-25T07:52:28.309068Z", "iopub.status.idle": "2025-03-25T07:52:28.342520Z", "shell.execute_reply": "2025-03-25T07:52:28.342225Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Gene expression from paired biopsies from a patient with METex14 skiping non-small cell lung cancer before and after treatment with neoadjuvant tepotinib (42 days)\"\n", "!Series_summary\t\"Background: MET inhibitors have demonstrated efficacy in treating patients with non-small cell lung cancer (NSCLC) harboring METex14 skipping alterations. Advancements in spatial profiling technologies have unveiled the complex dynamics of the tumor microenvironment, a crucial factor in cancer progression and therapeutic response. Using spatial profiling, this study investigates the effects of the MET inhibitor tepotinib on the TME in a case of locally advanced NSCLC with a METex14 skipping alteration. Methods: A patient with resectable stage IIIB NSCLC, unresponsive to neoadjuvant platinum-based doublet chemotherapy, received tepotinib following detection of a METex14 skipping alteration. Paired pre- and post-treatment biopsies were subjected to GeoMx Digital Spatial Profiling using the Cancer Transcriptome Atlas and immune-related protein panels to evaluate shifts in the tumor immune microenvironment (TIME). Results: Tepotinib administration resulted in pathological downstaging to stage IA1, which allowed for a successful lobectomy and evidenced a significant pathological response. The TIME was transformed from an immunosuppressive to a more permissive state, with upregulation of antigen-presenting and pro-inflammatory immune cells. Moreover, a marked decrease in immune checkpoint molecules, including PD-L1, was noted. Spatial profiling identified discrete immune-enriched clusters, indicating the role of tepotinib in modulating immune cell trafficking and function. Conclusions: Tepotinib appears to remodel the TIME in a patient with METex14 skipping NSCLC, possibly increasing responsiveness to immunotherapy. This case supports the integration of genetic profiling into the management of early and locally advanced NSCLC to guide personalized, targeted interventions. These findings highlight the need to further evaluate combinations of MET inhibitors and immunotherapies.\"\n", "!Series_overall_design\t\"On the GeoMx Digital Spatial Profiler, regions of interest of 250 micrometers in diameter were selected in the pre-treatment biopsy (15) and tepotinib-treated surgical resection (79) from the same patient, following hybridization of tissue with Cancer Transcriptome Atlas probes and custom probes for METex14, CLDN18.1, CLDN18.2, CEACAM5, spliced XBP1 and morphology marker staining using SYTO13, TTF1, CD3 and CD33.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['tissue: NSCLC']}\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": "4f267eca", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "2f4d5875", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:52:28.343741Z", "iopub.status.busy": "2025-03-25T07:52:28.343632Z", "iopub.status.idle": "2025-03-25T07:52:28.350263Z", "shell.execute_reply": "2025-03-25T07:52:28.349978Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sample characteristics dictionary: {0: ['tissue: NSCLC']}\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Let's first determine gene expression availability\n", "# Based on the background information, this dataset appears to have gene expression data from biopsy samples\n", "is_gene_available = True\n", "\n", "# Now let's analyze the sample characteristics dictionary to find trait, age, and gender information\n", "print(\"Sample characteristics dictionary:\", {0: ['tissue: NSCLC']})\n", "\n", "# 1. Trait (Lung Cancer) data:\n", "# From the sample characteristics, all samples are marked as \"tissue: NSCLC\" (Non-Small Cell Lung Cancer)\n", "# Since all samples have the same value (NSCLC), this is a constant feature\n", "# According to our rules, constant features are not useful for associative studies\n", "trait_row = None\n", "\n", "# 2. Age data: \n", "# No age information is provided in the sample characteristics\n", "age_row = None\n", "\n", "# 3. Gender data:\n", "# No gender information is provided in the sample characteristics\n", "gender_row = None\n", "\n", "# Define conversion functions (even though we won't use them in this case)\n", "def convert_trait(value):\n", " if value is None:\n", " return None\n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # In this case, all are NSCLC, but we would convert to binary if we had control samples\n", " if 'NSCLC' in value:\n", " return 1 # Has lung cancer\n", " else:\n", " return 0 # Control/normal\n", "\n", "def convert_age(value):\n", " # Not needed for this dataset, but including for completeness\n", " if value is None:\n", " return None\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " try:\n", " return float(value) # Age as a continuous variable\n", " except:\n", " return None\n", "\n", "def convert_gender(value):\n", " # Not needed for this dataset, but including for completeness\n", " if value is None:\n", " return None\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " value = value.lower()\n", " if value in ['female', 'f']:\n", " return 0\n", " elif value in ['male', 'm']:\n", " return 1\n", " else:\n", " return None\n", "\n", "# Save metadata about dataset usability\n", "# Since trait_row is None, is_trait_available is False\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", "# Since trait_row is None, we'll skip the clinical feature extraction step\n" ] }, { "cell_type": "markdown", "id": "5fc0b17c", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "931e7b2a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:52:28.351461Z", "iopub.status.busy": "2025-03-25T07:52:28.351354Z", "iopub.status.idle": "2025-03-25T07:52:28.386944Z", "shell.execute_reply": "2025-03-25T07:52:28.386652Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Examining matrix file structure...\n", "Line 0: !Series_title\t\"Gene expression from paired biopsies from a patient with METex14 skiping non-small cell lung cancer before and after treatment with neoadjuvant tepotinib (42 days)\"\n", "Line 1: !Series_geo_accession\t\"GSE249568\"\n", "Line 2: !Series_status\t\"Public on Sep 27 2024\"\n", "Line 3: !Series_submission_date\t\"Dec 06 2023\"\n", "Line 4: !Series_last_update_date\t\"Sep 28 2024\"\n", "Line 5: !Series_summary\t\"Background: MET inhibitors have demonstrated efficacy in treating patients with non-small cell lung cancer (NSCLC) harboring METex14 skipping alterations. Advancements in spatial profiling technologies have unveiled the complex dynamics of the tumor microenvironment, a crucial factor in cancer progression and therapeutic response. Using spatial profiling, this study investigates the effects of the MET inhibitor tepotinib on the TME in a case of locally advanced NSCLC with a METex14 skipping alteration. Methods: A patient with resectable stage IIIB NSCLC, unresponsive to neoadjuvant platinum-based doublet chemotherapy, received tepotinib following detection of a METex14 skipping alteration. Paired pre- and post-treatment biopsies were subjected to GeoMx Digital Spatial Profiling using the Cancer Transcriptome Atlas and immune-related protein panels to evaluate shifts in the tumor immune microenvironment (TIME). Results: Tepotinib administration resulted in pathological downstaging to stage IA1, which allowed for a successful lobectomy and evidenced a significant pathological response. The TIME was transformed from an immunosuppressive to a more permissive state, with upregulation of antigen-presenting and pro-inflammatory immune cells. Moreover, a marked decrease in immune checkpoint molecules, including PD-L1, was noted. Spatial profiling identified discrete immune-enriched clusters, indicating the role of tepotinib in modulating immune cell trafficking and function. Conclusions: Tepotinib appears to remodel the TIME in a patient with METex14 skipping NSCLC, possibly increasing responsiveness to immunotherapy. This case supports the integration of genetic profiling into the management of early and locally advanced NSCLC to guide personalized, targeted interventions. These findings highlight the need to further evaluate combinations of MET inhibitors and immunotherapies.\"\n", "Line 6: !Series_overall_design\t\"On the GeoMx Digital Spatial Profiler, regions of interest of 250 micrometers in diameter were selected in the pre-treatment biopsy (15) and tepotinib-treated surgical resection (79) from the same patient, following hybridization of tissue with Cancer Transcriptome Atlas probes and custom probes for METex14, CLDN18.1, CLDN18.2, CEACAM5, spliced XBP1 and morphology marker staining using SYTO13, TTF1, CD3 and CD33.\"\n", "Line 7: !Series_type\t\"Expression profiling by array\"\n", "Line 8: !Series_contributor\t\"Manon,A,Simard\"\n", "Line 9: !Series_contributor\t\"Niki,,Karachaliou\"\n", "Found table marker at line 63\n", "First few lines after marker:\n", "\"ID_REF\"\t\"GSM7950142\"\t\"GSM7950143\"\t\"GSM7950144\"\t\"GSM7950145\"\t\"GSM7950146\"\t\"GSM7950147\"\t\"GSM7950148\"\t\"GSM7950149\"\t\"GSM7950150\"\t\"GSM7950151\"\t\"GSM7950152\"\t\"GSM7950153\"\t\"GSM7950154\"\t\"GSM7950155\"\t\"GSM7950156\"\t\"GSM7950157\"\t\"GSM7950158\"\t\"GSM7950159\"\t\"GSM7950160\"\t\"GSM7950161\"\t\"GSM7950162\"\t\"GSM7950163\"\t\"GSM7950164\"\t\"GSM7950165\"\t\"GSM7950166\"\t\"GSM7950167\"\t\"GSM7950168\"\t\"GSM7950169\"\t\"GSM7950170\"\t\"GSM7950171\"\t\"GSM7950172\"\t\"GSM7950173\"\t\"GSM7950174\"\t\"GSM7950175\"\t\"GSM7950176\"\t\"GSM7950177\"\t\"GSM7950178\"\t\"GSM7950179\"\t\"GSM7950180\"\t\"GSM7950181\"\t\"GSM7950182\"\t\"GSM7950183\"\t\"GSM7950184\"\t\"GSM7950185\"\t\"GSM7950186\"\t\"GSM7950187\"\t\"GSM7950188\"\t\"GSM7950189\"\t\"GSM7950190\"\t\"GSM7950191\"\t\"GSM7950192\"\t\"GSM7950193\"\t\"GSM7950194\"\t\"GSM7950195\"\t\"GSM7950196\"\t\"GSM7950197\"\t\"GSM7950198\"\t\"GSM7950199\"\t\"GSM7950200\"\t\"GSM7950201\"\t\"GSM7950202\"\t\"GSM7950203\"\t\"GSM7950204\"\t\"GSM7950205\"\t\"GSM7950206\"\t\"GSM7950207\"\t\"GSM7950208\"\t\"GSM7950209\"\t\"GSM7950210\"\t\"GSM7950211\"\t\"GSM7950212\"\t\"GSM7950213\"\t\"GSM7950214\"\t\"GSM7950215\"\t\"GSM7950216\"\t\"GSM7950217\"\t\"GSM7950218\"\t\"GSM7950219\"\t\"GSM7950220\"\t\"GSM7950221\"\t\"GSM7950222\"\t\"GSM7950223\"\t\"GSM7950224\"\t\"GSM7950225\"\t\"GSM7950226\"\t\"GSM7950227\"\t\"GSM7950228\"\t\"GSM7950229\"\t\"GSM7950230\"\t\"GSM7950231\"\t\"GSM7950232\"\t\"GSM7950233\"\t\"GSM7950234\"\t\"GSM7950235\"\n", "\"A2M\"\t58.38108452\t68.13960402\t69.51810371\t64.05357403\t92.72623168\t65.13896457\t59.99873606\t67.21986693\t72.19068815\t58.98111977\t60.24057916\t77.67845496\t69.33769846\t68.51269377\t93.06441387\t1809.606215\t1303.270895\t1200.64948\t1789.068667\t1389.223552\t391.547777\t1671.12751\t1746.679634\t2070.237553\t730.1633549\t2071.697395\t1676.521592\t1754.629548\t727.7353278\t2915.511077\t1384.998115\t1396.953619\t1338.90953\t2217.995881\t2162.980366\t1794.583453\t2229.03945\t2159.762693\t458.5339578\t1573.205784\t1659.378113\t1251.190204\t837.141452\t1240.56749\t1329.142174\t463.6279067\t740.5694486\t584.2400635\t556.7172425\t911.1776066\t1624.308597\t266.5580714\t823.842681\t374.6109172\t166.4601379\t863.1233384\t1668.159071\t630.2580223\t862.0246748\t916.3820423\t165.4497335\t859.1332595\t708.3500055\t980.0902347\t951.3677922\t337.494863\t1467.554625\t1553.893975\t776.2753172\t1006.964898\t1192.855087\t1025.208697\t1163.643232\t969.6378986\t1284.039234\t435.249916\t1604.369705\t1256.061856\t1861.159631\t525.7061426\t465.1944438\t3454.8626\t1512.113924\t1542.877802\t1013.895341\t1067.066614\t866.0237904\t1239.206674\t1067.756202\t688.1864527\t1102.818138\t962.0345705\t758.8350909\t1499.984181\n", "\"ABCB1\"\t79.63900774\t78.68159546\t63.57800604\t70.67074018\t69.92732849\t82.93008303\t76.09967387\t76.32789034\t74.68812539\t67.2784196\t59.01050193\t77.18348114\t67.63141247\t79.71034773\t79.8889006\t52.63843303\t51.40860113\t57.77030815\t56.37114039\t62.11111796\t68.74336029\t51.20348791\t55.99996051\t47.06613358\t53.07804703\t51.16777361\t54.74523194\t49.5039907\t46.81778539\t63.81808174\t46.38181136\t50.79246369\t50.38765686\t53.44229673\t50.46672661\t48.58969657\t51.07348405\t56.12134915\t44.71902269\t51.24653712\t45.47738851\t46.87189687\t43.62267175\t51.38862369\t53.12748903\t63.65044587\t48.69177115\t63.16844576\t58.32202466\t64.46999851\t52.79238991\t54.98914892\t58.17006891\t71.56033603\t43.15784532\t56.7176598\t47.21668288\t64.41638524\t55.24320251\t49.54928028\t67.46076053\t74.86913323\t69.69291631\t48.80346674\t57.36725691\t57.45185031\t55.18516516\t53.74671647\t55.43825447\t62.69678429\t55.88744261\t58.90251854\t60.10599974\t68.01008144\t54.18619084\t71.15391186\t62.59971581\t57.71894646\t57.27916117\t51.33131464\t69.11697479\t49.27106682\t51.87733056\t61.12490046\t60.1540216\t67.56530262\t63.77901884\t63.70107975\t51.11258059\t67.09108124\t61.47318475\t64.99891168\t67.70380836\t53.28415182\n", "\"ABCF1\"\t81.73907231\t77.85070699\t70.48740433\t84.90917174\t66.86485198\t71.88279235\t71.4085517\t80.04685153\t74.48198243\t84.19158753\t75.77381411\t69.5040936\t76.21696394\t67.87275542\t84.65427857\t76.35794329\t72.99017306\t76.41734088\t77.72682385\t82.25802271\t79.70113855\t80.36835608\t80.83005642\t84.76837513\t72.81789455\t71.01171882\t74.9055032\t81.44491072\t73.11666805\t71.05412698\t72.40244083\t68.38929656\t76.9205716\t83.03660154\t75.80465473\t68.55317113\t77.96086174\t71.92863733\t81.20948488\t72.17442492\t80.9690852\t71.93778492\t71.69148616\t81.26283511\t69.88707658\t90.20345654\t78.18471641\t77.45611836\t85.0172158\t73.86199017\t70.22167922\t84.26748969\t79.7254292\t73.36469568\t83.12097312\t70.30082939\t75.96822796\t87.00242437\t80.32969359\t78.96192238\t69.45250798\t76.04416975\t75.29195826\t85.19369304\t88.11499328\t72.26745394\t85.2215155\t69.28536518\t70.33061129\t75.21227003\t74.224512\t76.29749534\t82.44359094\t80.00723756\t73.45886392\t71.58827041\t73.95983661\t71.14202033\t78.12661529\t75.16267536\t71.57939688\t74.31197905\t75.8445654\t72.34094296\t84.57815144\t66.08298817\t73.21574781\t77.20447332\t74.12033611\t73.7106903\t86.03337179\t76.1940752\t75.14121066\t77.06179412\n", "\"ABL1\"\t65.19137969\t57.84595918\t59.30828075\t65.43651937\t58.24021851\t54.57978489\t58.77367997\t57.82177118\t66.61850493\t60.05035304\t64.72403376\t57.54221451\t64.89875417\t71.21567129\t57.64885855\t90.41442302\t87.38884527\t81.48271707\t91.98092368\t103.0512446\t89.39422785\t86.46269461\t93.91645354\t108.00424\t69.49258959\t89.03598776\t86.41803566\t93.37306707\t70.93254854\t108.907712\t82.81737735\t80.94283779\t81.6360875\t88.34118231\t88.9692145\t93.20578171\t98.573584\t94.74695273\t83.18513058\t90.95769085\t84.05889658\t84.99908736\t86.58433037\t95.47265834\t90.47294072\t73.42505088\t79.25127986\t83.60751147\t83.31464143\t85.5273735\t85.26414877\t70.04221315\t83.68888758\t78.82905443\t71.36324867\t79.23512829\t96.05443756\t80.89235946\t80.2703338\t77.44942445\t93.46719738\t82.05527697\t74.56728073\t75.04953333\t73.62210384\t76.59426016\t79.94435654\t77.3079961\t82.06465652\t72.12178615\t81.71100002\t85.58005536\t84.27464976\t80.69911917\t85.39639761\t79.96451867\t80.12326532\t84.38923719\t78.60896489\t76.69895055\t71.95423095\t93.99365329\t82.7768513\t83.14882171\t90.69451745\t82.36764063\t81.80042093\t83.05700795\t72.56537664\t80.4286472\t87.42798894\t83.84935827\t76.50167444\t79.78797396\n", "Total lines examined: 64\n", "\n", "Attempting to extract gene data from matrix file...\n", "Successfully extracted gene data with 1820 rows\n", "First 20 gene IDs:\n", "Index(['A2M', 'ABCB1', 'ABCF1', 'ABL1', 'ACOT12', 'ACSF3', 'ACTA2', 'ACTB',\n", " 'ACTR3B', 'ACVR1B', 'ACVR1C', 'ACVR2A', 'ACY1', 'ADA', 'ADAM12',\n", " 'ADGRE1', 'ADGRE5', 'ADH1A/B/C', 'ADH4', 'ADH6'],\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": "7b8f827e", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "c803921d", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:52:28.388123Z", "iopub.status.busy": "2025-03-25T07:52:28.388016Z", "iopub.status.idle": "2025-03-25T07:52:28.389828Z", "shell.execute_reply": "2025-03-25T07:52:28.389556Z" } }, "outputs": [], "source": [ "# Examine the gene IDs to determine if they are human gene symbols or other identifiers\n", "# Looking at the first 20 gene IDs: A2M, ABCB1, ABCF1, ABL1, etc.\n", "# These appear to be standard human gene symbols\n", "\n", "# Based on my biomedical knowledge, these are proper human gene symbols\n", "# A2M is Alpha-2-Macroglobulin\n", "# ABCB1 is ATP Binding Cassette Subfamily B Member 1\n", "# ABCF1 is ATP Binding Cassette Subfamily F Member 1\n", "# ABL1 is ABL Proto-Oncogene 1, Non-Receptor Tyrosine Kinase\n", "# ACTA2 is Actin Alpha 2, Smooth Muscle\n", "# ACTB is Actin Beta\n", "\n", "# These are standard HGNC (HUGO Gene Nomenclature Committee) approved symbols\n", "# No mapping to gene symbols is required as the data already uses them\n", "\n", "requires_gene_mapping = False\n" ] }, { "cell_type": "markdown", "id": "b31389fb", "metadata": {}, "source": [ "### Step 5: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 6, "id": "6515b721", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:52:28.391004Z", "iopub.status.busy": "2025-03-25T07:52:28.390896Z", "iopub.status.idle": "2025-03-25T07:52:28.614922Z", "shell.execute_reply": "2025-03-25T07:52:28.614546Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Normalizing gene symbols using NCBI Gene database...\n", "After normalization: 1798 unique genes remain\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene expression data saved to ../../output/preprocess/Lung_Cancer/gene_data/GSE249568.csv\n", "\n", "Extracting clinical data directly from the matrix file...\n", "\n", "Processing clinical data...\n", "No trait data available in clinical information.\n", "\n", "Cannot create linked data: missing clinical or gene data\n", "Abnormality detected in the cohort: GSE249568. Preprocessing failed.\n", "\n", "Dataset not usable for Lung_Cancer association studies due to bias or quality issues.\n" ] } ], "source": [ "# 1. Normalize gene symbols using NCBI Gene database\n", "print(\"\\nNormalizing gene symbols using NCBI Gene database...\")\n", "try:\n", " gene_data_normalized = normalize_gene_symbols_in_index(gene_data)\n", " print(f\"After normalization: {len(gene_data_normalized)} unique genes remain\")\n", " gene_data_cleaned = gene_data_normalized\n", " \n", " # Save the normalized gene expression data\n", " os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", " gene_data_cleaned.to_csv(out_gene_data_file)\n", " print(f\"Normalized gene expression data saved to {out_gene_data_file}\")\n", "except Exception as e:\n", " print(f\"Error during gene symbol normalization: {str(e)}\")\n", " print(\"Falling back to original gene data\")\n", " gene_data_cleaned = gene_data.copy()\n", "\n", "# 2. We need to recreate the clinical data from the original matrix file\n", "print(\"\\nExtracting clinical data directly from the matrix file...\")\n", "# Extract clinical data from the matrix file again\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", "# Process clinical data\n", "print(\"\\nProcessing clinical data...\")\n", "# Create clinical features dataframe \n", "if trait_row is not None:\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 if age_row is not None else None,\n", " gender_row=gender_row,\n", " convert_gender=convert_gender if gender_row is not None else None\n", " )\n", " \n", " print(f\"Extracted clinical data with shape: {selected_clinical_df.shape}\")\n", " # Save clinical data\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " selected_clinical_df.to_csv(out_clinical_data_file)\n", " print(f\"Clinical data saved to: {out_clinical_data_file}\")\n", " \n", " is_trait_available = True\n", "else:\n", " selected_clinical_df = pd.DataFrame()\n", " is_trait_available = False\n", " print(\"No trait data available in clinical information.\")\n", "\n", "# 3. Link clinical and genetic data\n", "if is_trait_available and is_gene_available:\n", " print(\"\\nLinking clinical and genetic data...\")\n", " try:\n", " # Ensure the sample IDs match between clinical and genetic data\n", " common_samples = list(set(selected_clinical_df.columns).intersection(set(gene_data_cleaned.columns)))\n", " \n", " if len(common_samples) == 0:\n", " print(\"Warning: No common samples between clinical and genetic data\")\n", " linked_data = pd.DataFrame()\n", " is_biased = True\n", " else:\n", " print(f\"Found {len(common_samples)} common samples between clinical and genetic data\")\n", " \n", " # Filter data to include only common samples\n", " clinical_subset = selected_clinical_df[common_samples]\n", " genetic_subset = gene_data_cleaned[common_samples]\n", " \n", " # Link the data\n", " linked_data = pd.concat([clinical_subset, genetic_subset], axis=0).T\n", " print(f\"Linked data shape: {linked_data.shape}\")\n", " \n", " # 4. Handle missing values\n", " print(\"\\nHandling missing values in linked data...\")\n", " linked_data = handle_missing_values(linked_data, trait)\n", " print(f\"Data shape after handling missing values: {linked_data.shape}\")\n", " \n", " # 5. Determine if trait and demographic features are severely biased\n", " print(\"\\nEvaluating trait and demographic feature bias...\")\n", " is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", " except Exception as e:\n", " print(f\"Error during data linking: {str(e)}\")\n", " linked_data = pd.DataFrame()\n", " is_biased = True\n", "else:\n", " print(\"\\nCannot create linked data: missing clinical or gene data\")\n", " linked_data = pd.DataFrame()\n", " is_biased = True\n", "\n", "# 6. Final validation and saving\n", "note = \"This dataset contains gene expression data from astrocytoma cell lines with modified GFAP isoform expression. The trait represents different experimental conditions related to the GFAPδ/GFAPα ratio.\"\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 if len(linked_data) > 0 else True,\n", " df=linked_data,\n", " note=note\n", ")\n", "\n", "# Save the linked data if it's usable\n", "if is_usable and len(linked_data) > 0:\n", " print(f\"\\nSaving 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)\n", " print(f\"Linked data saved successfully!\")\n", "else:\n", " print(f\"\\nDataset not usable for {trait} association studies due to bias or quality issues.\")" ] } ], "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 }