{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "114d95d5", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:08:17.615240Z", "iopub.status.busy": "2025-03-25T05:08:17.615124Z", "iopub.status.idle": "2025-03-25T05:08:17.774219Z", "shell.execute_reply": "2025-03-25T05:08:17.773787Z" } }, "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 = \"X-Linked_Lymphoproliferative_Syndrome\"\n", "cohort = \"GSE248835\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/X-Linked_Lymphoproliferative_Syndrome\"\n", "in_cohort_dir = \"../../input/GEO/X-Linked_Lymphoproliferative_Syndrome/GSE248835\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/X-Linked_Lymphoproliferative_Syndrome/GSE248835.csv\"\n", "out_gene_data_file = \"../../output/preprocess/X-Linked_Lymphoproliferative_Syndrome/gene_data/GSE248835.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/X-Linked_Lymphoproliferative_Syndrome/clinical_data/GSE248835.csv\"\n", "json_path = \"../../output/preprocess/X-Linked_Lymphoproliferative_Syndrome/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "2213db78", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "f552657d", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:08:17.775680Z", "iopub.status.busy": "2025-03-25T05:08:17.775539Z", "iopub.status.idle": "2025-03-25T05:08:17.809691Z", "shell.execute_reply": "2025-03-25T05:08:17.809105Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Files in the cohort directory:\n", "['GSE248835_family.soft.gz', 'GSE248835_series_matrix.txt.gz']\n", "Identified SOFT files: ['GSE248835_family.soft.gz']\n", "Identified matrix files: ['GSE248835_series_matrix.txt.gz']\n", "\n", "Background Information:\n", "!Series_title\t\"Impact of Tumor Microenvironment on Efficacy of CD19 CAR T-Cell Therapy or Chemotherapy and Transplant in Large B-Cell Lymphoma\"\n", "!Series_summary\t\"The phase 3 ZUMA-7 trial in second-line large B-cell lymphoma demonstrated superiority of anti-CD19 CAR T-cell therapy (axicabtagene ciloleucel; axi-cel) over standard of care (SOC; salvage chemotherapy followed by hematopoietic transplantation). Here, we present a prespecified exploratory analysis examining the association between pretreatment tumor characteristics and the efficacy of axi-cel versus SOC. B-cell gene expression signature (GES) and CD19 expression significantly associated with improved event-free survival (EFS) for axi-cel (P=.0002 for B-cell GES; P=.0165 for CD19 expression) but not SOC (P=.9374 for B-cell GES; P=.5526 for CD19 expression). Axi-cel showed superior EFS over SOC irrespective of B-cell GES and CD19 expression (P=8.56e–9 for B-cell GES high; P=.0019 for B-cell GES low; P=3.85e–9 for CD19 gene high; P=.0017 for CD19 gene low). Low CD19 expression in malignant cells correlated with a tumor GES consisting of immune suppressive stromal and myeloid genes, highlighting the inter-relation between malignant cell features and immune contexture substantially impacting axi-cel outcomes. Tumor burden, lactate dehydrogenase, and cell-of-origin impacted SOC more than axi-cel outcomes. T-cell activation and B-cell GES, which are associated with improved axi-cel outcome, decreased with increasing lines of therapy. These data highlight differences in resistance mechanisms to axi-cel and SOC and support earlier intervention with axi-cel.\"\n", "!Series_overall_design\t\"256 pretreatment tumor biopsies were analyzed, 134 from the Axicabtagene Ciloleucel arm and 122 from the Standard of Care Chemotherapy arm\"\n", "\n", "Sample Characteristics Dictionary:\n", "{0: ['visit: Screening'], 1: ['treatment arm: Axicabtagene Ciloleucel', 'treatment arm: Standard of Care Chemotherapy'], 2: ['baseline tumor burden (spd): 1033.5', 'baseline tumor burden (spd): 2851.5', 'baseline tumor burden (spd): 1494.9', 'baseline tumor burden (spd): null', 'baseline tumor burden (spd): 12712.9', 'baseline tumor burden (spd): 2654.8', 'baseline tumor burden (spd): 6714', 'baseline tumor burden (spd): 1487.1', 'baseline tumor burden (spd): 5443.9', 'baseline tumor burden (spd): 1026.8', 'baseline tumor burden (spd): 8888.1', 'baseline tumor burden (spd): 1491', 'baseline tumor burden (spd): 938.1', 'baseline tumor burden (spd): 2071.7', 'baseline tumor burden (spd): 1244.9', 'baseline tumor burden (spd): 181', 'baseline tumor burden (spd): 714.3', 'baseline tumor burden (spd): 1358.3', 'baseline tumor burden (spd): 7219.2', 'baseline tumor burden (spd): 508.4', 'baseline tumor burden (spd): 13791.8', 'baseline tumor burden (spd): 1330.3', 'baseline tumor burden (spd): 1825.1', 'baseline tumor burden (spd): 1105.8', 'baseline tumor burden (spd): 12322.7', 'baseline tumor burden (spd): 4883.7', 'baseline tumor burden (spd): 1549.9', 'baseline tumor burden (spd): 9403.8', 'baseline tumor burden (spd): 692.7', 'baseline tumor burden (spd): 323.6'], 3: ['cell of origin: GCB', 'cell of origin: Unclassified', 'cell of origin: ABC', 'cell of origin: null'], 4: ['ongoing_2grps: Missing', 'ongoing_2grps: Others', 'ongoing_2grps: Ongoing'], 5: ['ongoing.response: Missing', 'ongoing.response: Relapsed', 'ongoing.response: Nonresponders', 'ongoing.response: Ongoing Response'], 6: ['duration.of.response.months: 1.675564682', 'duration.of.response.months: 1.18275154', 'duration.of.response.months: 1.412731006', 'duration.of.response.months: NA', 'duration.of.response.months: 0.032854209', 'duration.of.response.months: 16.22997947', 'duration.of.response.months: 1.905544148', 'duration.of.response.months: 26.87474333', 'duration.of.response.months: 28.28747433', 'duration.of.response.months: 27.86036961', 'duration.of.response.months: 4.862422998', 'duration.of.response.months: 13.99589322', 'duration.of.response.months: 23.81930185', 'duration.of.response.months: 22.275154', 'duration.of.response.months: 6.209445585', 'duration.of.response.months: 2.168377823', 'duration.of.response.months: 5.749486653', 'duration.of.response.months: 1.642710472', 'duration.of.response.months: 31.93429158', 'duration.of.response.months: 0.657084189', 'duration.of.response.months: 20.23819302', 'duration.of.response.months: 1.445585216', 'duration.of.response.months: 3.449691992', 'duration.of.response.months: 0.919917864', 'duration.of.response.months: 22.40657084', 'duration.of.response.months: 21.88090349', 'duration.of.response.months: 21.94661191', 'duration.of.response.months: 28.09034908', 'duration.of.response.months: 21.65092402', 'duration.of.response.months: 1.872689938'], 7: ['duration.of.response.event: 0', 'duration.of.response.event: 1', 'duration.of.response.event: NA'], 8: ['event.free.survival.months: 3.449691992', 'event.free.survival.months: 3.252566735', 'event.free.survival.months: 1.577002053', 'event.free.survival.months: 1.511293634', 'event.free.survival.months: 2.694045175', 'event.free.survival.months: 17.83983573', 'event.free.survival.months: 3.646817248', 'event.free.survival.months: 28.64887064', 'event.free.survival.months: 29.99589322', 'event.free.survival.months: 29.70020534', 'event.free.survival.months: 6.472279261', 'event.free.survival.months: 3.416837782', 'event.free.survival.months: 15.83572895', 'event.free.survival.months: 25.75770021', 'event.free.survival.months: 24.11498973', 'event.free.survival.months: 8.476386037', 'event.free.survival.months: 1.642710472', 'event.free.survival.months: 3.679671458', 'event.free.survival.months: 1.445585216', 'event.free.survival.months: 7.260780287', 'event.free.survival.months: 3.811088296', 'event.free.survival.months: 33.34702259', 'event.free.survival.months: 2.825462012', 'event.free.survival.months: 23.95071869', 'event.free.survival.months: 1.708418891', 'event.free.survival.months: 7.983572895', 'event.free.survival.months: 3.154004107', 'event.free.survival.months: 4.960985626', 'event.free.survival.months: 1.478439425', 'event.free.survival.months: 2.004106776'], 9: ['event.free.survival.event: 1', 'event.free.survival.event: 0'], 10: ['histologically.proven.dlbcl.group: DLBCL+Others', 'histologically.proven.dlbcl.group: HGBL'], 11: ['grade3_ne: N', 'grade3_ne: Y'], 12: ['grade3_crs: N', 'grade3_crs: Y']}\n" ] } ], "source": [ "# 1. Let's first list the directory contents to understand what files are available\n", "import os\n", "\n", "print(\"Files in the cohort directory:\")\n", "files = os.listdir(in_cohort_dir)\n", "print(files)\n", "\n", "# Adapt file identification to handle different naming patterns\n", "soft_files = [f for f in files if 'soft' in f.lower() or '.soft' in f.lower() or '_soft' in f.lower()]\n", "matrix_files = [f for f in files if 'matrix' in f.lower() or '.matrix' in f.lower() or '_matrix' in f.lower()]\n", "\n", "# If no files with these patterns are found, look for alternative file types\n", "if not soft_files:\n", " soft_files = [f for f in files if f.endswith('.txt') or f.endswith('.gz')]\n", "if not matrix_files:\n", " matrix_files = [f for f in files if f.endswith('.txt') or f.endswith('.gz')]\n", "\n", "print(\"Identified SOFT files:\", soft_files)\n", "print(\"Identified matrix files:\", matrix_files)\n", "\n", "# Use the first files found, if any\n", "if len(soft_files) > 0 and len(matrix_files) > 0:\n", " soft_file = os.path.join(in_cohort_dir, soft_files[0])\n", " matrix_file = os.path.join(in_cohort_dir, matrix_files[0])\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(\"\\nBackground Information:\")\n", " print(background_info)\n", " print(\"\\nSample Characteristics Dictionary:\")\n", " print(sample_characteristics_dict)\n", "else:\n", " print(\"No appropriate files found in the directory.\")\n" ] }, { "cell_type": "markdown", "id": "1188aac8", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "f71f7e53", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:08:17.810872Z", "iopub.status.busy": "2025-03-25T05:08:17.810766Z", "iopub.status.idle": "2025-03-25T05:08:17.815260Z", "shell.execute_reply": "2025-03-25T05:08:17.814819Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Trait information identified at row 1 in sample characteristics\n", "Trait conversion function defined: 1 for Axicabtagene Ciloleucel, 0 for Standard of Care Chemotherapy\n", "Clinical data extraction will be performed after loading the matrix file in subsequent steps\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the series title and summary, this appears to be a genomic dataset comparing \n", "# CD19 CAR T-cell therapy vs standard chemotherapy in lymphoma patients\n", "# The dataset mentions \"B-cell gene expression signature\" and \"CD19 expression\"\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "\n", "# 2.1 Data Availability\n", "# Examining the sample characteristics dictionary:\n", "\n", "# For trait: Looking at treatment arm (index 1)\n", "trait_row = 1 # 'treatment arm' contains info about CAR T-cell therapy vs standard chemotherapy\n", "\n", "# For age: Not explicitly provided in sample characteristics\n", "age_row = None # Age data is not available\n", "\n", "# For gender: Not explicitly provided in sample characteristics\n", "gender_row = None # Gender data is not available\n", "\n", "# 2.2 Data Type Conversion Functions\n", "def convert_trait(value_str):\n", " \"\"\"Convert treatment arm values to binary trait values.\n", " 0: Standard of Care Chemotherapy (control)\n", " 1: Axicabtagene Ciloleucel (CAR T-cell therapy)\n", " \"\"\"\n", " if value_str is None or pd.isna(value_str):\n", " return None\n", " \n", " # Extract value after colon if present\n", " if \":\" in value_str:\n", " value = value_str.split(\":\", 1)[1].strip()\n", " else:\n", " value = value_str.strip()\n", " \n", " if \"Axicabtagene Ciloleucel\" in value:\n", " return 1 # CAR T-cell therapy\n", " elif \"Standard of Care Chemotherapy\" in value:\n", " return 0 # Standard chemotherapy\n", " else:\n", " return None\n", "\n", "def convert_age(value_str):\n", " \"\"\"This function is a placeholder since age data is not available.\"\"\"\n", " return None\n", "\n", "def convert_gender(value_str):\n", " \"\"\"This function is a placeholder since gender data 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 (initial filtering)\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", "# Note: We need to first load the clinical data from the matrix file\n", "# since there's no prepared clinical_data.csv file\n", "if trait_row is not None:\n", " # The clinical data will be extracted in a later step after parsing the matrix file\n", " # For now, we've identified that trait information is available and defined how to convert it\n", " print(f\"Trait information identified at row {trait_row} in sample characteristics\")\n", " print(f\"Trait conversion function defined: 1 for Axicabtagene Ciloleucel, 0 for Standard of Care Chemotherapy\")\n", " print(f\"Clinical data extraction will be performed after loading the matrix file in subsequent steps\")\n" ] }, { "cell_type": "markdown", "id": "78b78cb7", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "ef31947e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:08:17.816266Z", "iopub.status.busy": "2025-03-25T05:08:17.816159Z", "iopub.status.idle": "2025-03-25T05:08:17.861173Z", "shell.execute_reply": "2025-03-25T05:08:17.860703Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First 20 gene/probe identifiers:\n", "Index(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13',\n", " '14', '15', '16', '17', '18', '19', '20'],\n", " dtype='object', name='ID')\n", "\n", "Gene expression data shape: (817, 256)\n" ] } ], "source": [ "# Use the helper function to get the proper file paths\n", "soft_file_path, matrix_file_path = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# Extract gene expression data\n", "try:\n", " gene_data = get_genetic_data(matrix_file_path)\n", " \n", " # Print the first 20 row IDs (gene or probe identifiers)\n", " print(\"First 20 gene/probe identifiers:\")\n", " print(gene_data.index[:20])\n", " \n", " # Print shape to understand the dataset dimensions\n", " print(f\"\\nGene expression data shape: {gene_data.shape}\")\n", " \n", "except Exception as e:\n", " print(f\"Error extracting gene data: {e}\")\n" ] }, { "cell_type": "markdown", "id": "e61f3b8d", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "e3a74680", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:08:17.862218Z", "iopub.status.busy": "2025-03-25T05:08:17.862111Z", "iopub.status.idle": "2025-03-25T05:08:17.864618Z", "shell.execute_reply": "2025-03-25T05:08:17.864153Z" } }, "outputs": [], "source": [ "# First, I need to examine whether these identifiers are numeric indices or actual gene symbols\n", "# Based on the preview, these appear to be simple numeric indices (1, 2, 3, etc.)\n", "# These are not human gene symbols, which would typically be alphanumeric like BRCA1, TP53, etc.\n", "# Therefore, these identifiers likely need to be mapped to proper gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "f0cfda33", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "26b5d61a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:08:17.865756Z", "iopub.status.busy": "2025-03-25T05:08:17.865653Z", "iopub.status.idle": "2025-03-25T05:08:18.151582Z", "shell.execute_reply": "2025-03-25T05:08:18.150934Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sample of gene expression data (first 5 rows, first 5 columns):\n", " GSM7920866 GSM7920867 GSM7920868 GSM7920869 GSM7920870\n", "ID \n", "1 428.455036 1478.583496 320.904453 205.785854 131.143273\n", "2 34.272986 136.996953 77.011394 28.266082 58.851203\n", "3 1.830198 1.296121 1.562547 1.741825 2.426707\n", "4 18.455770 19.616558 28.109775 19.053710 21.362373\n", "5 2.899894 1.016105 1.650611 2.173470 0.697614\n", "\n", "Platform information:\n", "!Series_title = Impact of Tumor Microenvironment on Efficacy of CD19 CAR T-Cell Therapy or Chemotherapy and Transplant in Large B-Cell Lymphoma\n", "!Platform_title = NanoString Human PanCancer IO360™ Panel and Lymphoma Subtyping Test\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene annotation columns:\n", "['ID', 'Gene_Signature_Name', 'ORF', 'GB_ACC', 'NS Probe ID', 'Analyte Type', 'SPOT_ID']\n", "\n", "Gene annotation preview:\n", "{'ID': ['1', '2', '3', '4', '5'], 'Gene_Signature_Name': ['TIS.IO360', 'APM.IO360', 'APM Loss.IO360', 'Apoptosis.IO360', 'ARG1.IO360'], 'ORF': [nan, nan, nan, nan, nan], 'GB_ACC': [nan, nan, nan, nan, nan], 'NS Probe ID': [nan, nan, nan, nan, nan], 'Analyte Type': ['IO360 Signature', 'IO360 Signature', 'IO360 Signature', 'IO360 Signature', 'IO360 Signature'], 'SPOT_ID': ['TIS.IO360', 'APM.IO360', 'APM Loss.IO360', 'Apoptosis.IO360', 'ARG1.IO360']}\n", "\n", "Matching rows in annotation for sample IDs: 2570\n", "\n", "Potential gene symbol columns: ['Gene_Signature_Name']\n", "\n", "Is this dataset likely to contain gene expression data? False\n", "\n", "NOTE: Based on our analysis, this dataset doesn't appear to contain gene expression data.\n", "It appears to be a different type of data (possibly SNP array or other genomic data).\n" ] } ], "source": [ "# 1. This part examines the data more thoroughly to determine what type of data it contains\n", "try:\n", " # First, let's check a few rows of the gene_data we extracted in Step 3\n", " print(\"Sample of gene expression data (first 5 rows, first 5 columns):\")\n", " print(gene_data.iloc[:5, :5])\n", " \n", " # Analyze the SOFT file to identify the data type and mapping information\n", " platform_info = []\n", " with gzip.open(soft_file_path, 'rt', encoding='latin-1') as f:\n", " for line in f:\n", " if line.startswith(\"!Platform_title\") or line.startswith(\"!Series_title\") or \"description\" in line.lower():\n", " platform_info.append(line.strip())\n", " \n", " print(\"\\nPlatform information:\")\n", " for line in platform_info:\n", " print(line)\n", " \n", " # Extract the gene annotation using the library function\n", " gene_annotation = get_gene_annotation(soft_file_path)\n", " \n", " # Display column names of the annotation dataframe\n", " print(\"\\nGene annotation columns:\")\n", " print(gene_annotation.columns.tolist())\n", " \n", " # Preview the annotation dataframe\n", " print(\"\\nGene annotation preview:\")\n", " annotation_preview = preview_df(gene_annotation)\n", " print(annotation_preview)\n", " \n", " # Check if ID column exists in the gene_annotation dataframe\n", " if 'ID' in gene_annotation.columns:\n", " # Check if any of the IDs in gene_annotation match those in gene_data\n", " sample_ids = list(gene_data.index[:10])\n", " matching_rows = gene_annotation[gene_annotation['ID'].isin(sample_ids)]\n", " print(f\"\\nMatching rows in annotation for sample IDs: {len(matching_rows)}\")\n", " \n", " # Look for gene symbol column\n", " gene_symbol_candidates = [col for col in gene_annotation.columns if 'gene' in col.lower() or 'symbol' in col.lower() or 'name' in col.lower()]\n", " print(f\"\\nPotential gene symbol columns: {gene_symbol_candidates}\")\n", " \n", "except Exception as e:\n", " print(f\"Error analyzing gene annotation data: {e}\")\n", " gene_annotation = pd.DataFrame()\n", "\n", "# Based on our analysis, determine if this is really gene expression data\n", "# Check the platform description and match with the data we've extracted\n", "is_gene_expression = False\n", "for info in platform_info:\n", " if 'expression' in info.lower() or 'transcript' in info.lower() or 'mrna' in info.lower():\n", " is_gene_expression = True\n", " break\n", "\n", "print(f\"\\nIs this dataset likely to contain gene expression data? {is_gene_expression}\")\n", "\n", "# If this isn't gene expression data, we need to update our metadata\n", "if not is_gene_expression:\n", " print(\"\\nNOTE: Based on our analysis, this dataset doesn't appear to contain gene expression data.\")\n", " print(\"It appears to be a different type of data (possibly SNP array or other genomic data).\")\n", " # Update is_gene_available for metadata\n", " is_gene_available = False\n", " \n", " # Save the updated metadata\n", " validate_and_save_cohort_info(\n", " is_final=False,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=is_gene_available,\n", " is_trait_available=is_trait_available\n", " )" ] } ], "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 }