{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "4b976206", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:25:29.553144Z", "iopub.status.busy": "2025-03-25T07:25:29.552968Z", "iopub.status.idle": "2025-03-25T07:25:29.716702Z", "shell.execute_reply": "2025-03-25T07:25:29.716369Z" } }, "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 = \"GSE114022\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Large_B-cell_Lymphoma\"\n", "in_cohort_dir = \"../../input/GEO/Large_B-cell_Lymphoma/GSE114022\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Large_B-cell_Lymphoma/GSE114022.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Large_B-cell_Lymphoma/gene_data/GSE114022.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Large_B-cell_Lymphoma/clinical_data/GSE114022.csv\"\n", "json_path = \"../../output/preprocess/Large_B-cell_Lymphoma/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "7c4e207c", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "36dbb6c2", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:25:29.718152Z", "iopub.status.busy": "2025-03-25T07:25:29.718010Z", "iopub.status.idle": "2025-03-25T07:25:29.829313Z", "shell.execute_reply": "2025-03-25T07:25:29.829011Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"The ETS inhibitors YK-4-279 and TK-216 have anti-tumor activity in lymphomas and interfere with SPIB in activated B cell-like type diffuse large B cell lymphoma\"\n", "!Series_summary\t\"TMD8 and U2932 were treated with YK-4-279 for 4 and 8 hours.\"\n", "!Series_overall_design\t\"Identifing genes modulated by the YK-4-279 in human lymphoma cell lines\"\n", "Sample Characteristics Dictionary:\n", "{0: ['cell line: TMD8', 'cell line: U2932'], 1: ['treatment: YK-S', 'treatment: YK-R', 'treatment: DMSO'], 2: ['time point: 4hr', 'time point: 8hr']}\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": "fe869e0f", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "733a013b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:25:29.830468Z", "iopub.status.busy": "2025-03-25T07:25:29.830359Z", "iopub.status.idle": "2025-03-25T07:25:29.837661Z", "shell.execute_reply": "2025-03-25T07:25:29.837357Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of selected clinical features: {'GSM3130825': [1.0], 'GSM3130826': [1.0], 'GSM3130827': [1.0], 'GSM3130828': [1.0], 'GSM3130829': [1.0], 'GSM3130830': [1.0], 'GSM3130831': [1.0], 'GSM3130832': [1.0], 'GSM3130833': [1.0], 'GSM3130834': [1.0], 'GSM3130835': [1.0], 'GSM3130836': [1.0], 'GSM3130837': [1.0], 'GSM3130838': [1.0], 'GSM3130839': [1.0], 'GSM3130840': [1.0], 'GSM3130841': [1.0], 'GSM3130842': [1.0], 'GSM3130843': [1.0], 'GSM3130844': [1.0], 'GSM3130845': [1.0], 'GSM3130846': [1.0], 'GSM3130847': [1.0], 'GSM3130848': [1.0], 'GSM3130849': [1.0], 'GSM3130850': [1.0], 'GSM3130851': [1.0], 'GSM3130852': [1.0], 'GSM3130853': [1.0], 'GSM3130854': [1.0]}\n", "Clinical data saved to ../../output/preprocess/Large_B-cell_Lymphoma/clinical_data/GSE114022.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the background information, this dataset appears to contain gene expression data\n", "# as it mentions \"Identifing genes modulated by the YK-4-279 in human lymphoma cell lines\"\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", "# From the characteristics dictionary, we can see this is a cell line experiment\n", "# Cell lines (TMD8 and U2932) are mentioned as lymphoma cell lines in the Series_title\n", "# Cell lines can be used as a proxy for the trait, as they represent the disease\n", "trait_row = 0 # The row containing cell line information\n", "\n", "# No age information is available in this cell line experiment\n", "age_row = None\n", "\n", "# No gender information is available in this cell line experiment\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion\n", "\n", "def convert_trait(value):\n", " \"\"\"\n", " Convert the cell line information to a binary trait variable.\n", " Both cell lines represent lymphoma, so we'll use 1 for all samples.\n", " \"\"\"\n", " if not isinstance(value, str):\n", " return None\n", " # Since both cell lines represent lymphoma (the trait of interest), we'll map both to 1\n", " # Extract the value after the colon\n", " if ':' in value:\n", " cell_line = value.split(':', 1)[1].strip()\n", " if cell_line in ['TMD8', 'U2932']:\n", " return 1\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Placeholder function for age conversion\"\"\"\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Placeholder function for gender conversion\"\"\"\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Check if trait data is available (trait_row is not None)\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 proceed with clinical feature extraction\n", "if trait_row is not None:\n", " # Initial dataframe set in the previous step would be called clinical_data\n", " # Let's assume it's available\n", " if 'clinical_data' in locals() or 'clinical_data' in globals():\n", " selected_clinical_df = geo_select_clinical_features(\n", " clinical_df=clinical_data,\n", " trait=trait,\n", " trait_row=trait_row,\n", " convert_trait=convert_trait,\n", " age_row=age_row,\n", " convert_age=convert_age,\n", " gender_row=gender_row,\n", " convert_gender=convert_gender\n", " )\n", " \n", " # Preview the selected clinical features\n", " preview = preview_df(selected_clinical_df)\n", " print(\"Preview of selected clinical features:\", preview)\n", " \n", " # Save the clinical data to a CSV file\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " selected_clinical_df.to_csv(out_clinical_data_file, index=True)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "acf3151e", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "ed45c450", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:25:29.838782Z", "iopub.status.busy": "2025-03-25T07:25:29.838676Z", "iopub.status.idle": "2025-03-25T07:25:29.979054Z", "shell.execute_reply": "2025-03-25T07:25:29.978679Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Examining matrix file structure...\n", "Line 0: !Series_title\t\"The ETS inhibitors YK-4-279 and TK-216 have anti-tumor activity in lymphomas and interfere with SPIB in activated B cell-like type diffuse large B cell lymphoma\"\n", "Line 1: !Series_geo_accession\t\"GSE114022\"\n", "Line 2: !Series_status\t\"Public on May 01 2021\"\n", "Line 3: !Series_submission_date\t\"May 03 2018\"\n", "Line 4: !Series_last_update_date\t\"May 02 2021\"\n", "Line 5: !Series_summary\t\"TMD8 and U2932 were treated with YK-4-279 for 4 and 8 hours.\"\n", "Line 6: !Series_overall_design\t\"Identifing genes modulated by the YK-4-279 in human lymphoma cell lines\"\n", "Line 7: !Series_type\t\"Expression profiling by array\"\n", "Line 8: !Series_contributor\t\"Luciano,,Cascione\"\n", "Line 9: !Series_contributor\t\"Francesco,,Bertoni\"\n", "Found table marker at line 61\n", "First few lines after marker:\n", "\"ID_REF\"\t\"GSM3130825\"\t\"GSM3130826\"\t\"GSM3130827\"\t\"GSM3130828\"\t\"GSM3130829\"\t\"GSM3130830\"\t\"GSM3130831\"\t\"GSM3130832\"\t\"GSM3130833\"\t\"GSM3130834\"\t\"GSM3130835\"\t\"GSM3130836\"\t\"GSM3130837\"\t\"GSM3130838\"\t\"GSM3130839\"\t\"GSM3130840\"\t\"GSM3130841\"\t\"GSM3130842\"\t\"GSM3130843\"\t\"GSM3130844\"\t\"GSM3130845\"\t\"GSM3130846\"\t\"GSM3130847\"\t\"GSM3130848\"\t\"GSM3130849\"\t\"GSM3130850\"\t\"GSM3130851\"\t\"GSM3130852\"\t\"GSM3130853\"\t\"GSM3130854\"\n", "\"ILMN_1343291\"\t13.99\t14.04632306\t14.00039501\t13.87189296\t13.98905804\t13.98607689\t13.93968839\t13.8440911\t13.8992632\t13.96166545\t13.98607689\t13.98905804\t13.7399\t13.4834\t13.7751\t13.933\t12.833\t13.8329\t13.7004\t13.8687\t13.8687\t13.7206\t13.7004\t13.8329\t13.7399\t13.6843\t13.7399\t13.933\t13.7989\t13.6585\n", "\"ILMN_1343295\"\t12.78\t12.70030112\t12.65593103\t12.47068546\t12.51942712\t12.48302617\t12.09646991\t12.67075676\t12.72634686\t12.56476551\t12.71074969\t12.85904844\t12.3856\t12.0978\t11.9087\t11.5434\t11.8563\t11.0026\t11.5724\t11.0583\t11.0062\t12.1317\t12.0491\t11.7767\t12.1395\t12.1554\t11.9731\t12.2653\t11.6834\t11.9895\n", "\"ILMN_1651199\"\t4.49\t4.424828882\t4.619709369\t4.689107786\t4.493859325\t4.525728475\t5.021444564\t4.607027355\t4.474364117\t4.591811365\t4.450849413\t4.711694474\t6.38303\t6.27562\t6.37703\t6.30519\t6.28616\t6.37609\t6.24422\t6.42613\t6.23024\t6.35131\t6.29749\t6.28269\t6.38868\t6.25454\t6.37573\t6.37091\t6.45302\t6.35539\n", "\"ILMN_1651209\"\t5.42\t5.112142189\t4.955173233\t5.088374667\t4.932564723\t4.993378266\t5.102866746\t5.056534485\t5.832945163\t5.109397176\t4.929110934\t5.259027408\t6.45466\t6.40234\t6.19695\t6.56696\t7.46938\t6.3021\t6.47801\t6.22899\t6.37919\t6.48807\t6.31026\t6.32084\t6.33419\t6.34899\t6.48797\t6.4414\t6.55587\t6.49817\n", "Total lines examined: 62\n", "\n", "Attempting to extract gene data from matrix file...\n", "Successfully extracted gene data with 47231 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": "47c9481a", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "bccec266", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:25:29.980297Z", "iopub.status.busy": "2025-03-25T07:25:29.980191Z", "iopub.status.idle": "2025-03-25T07:25:29.982039Z", "shell.execute_reply": "2025-03-25T07:25:29.981775Z" } }, "outputs": [], "source": [ "# The identifiers in the gene expression data start with \"ILMN_\" which indicates they are Illumina probe IDs\n", "# Illumina probe IDs are not human gene symbols but rather platform-specific identifiers\n", "# They need to be mapped to standard gene symbols for meaningful analysis\n", "\n", "# These \"ILMN_\" identifiers are from Illumina microarray platforms and require mapping to \n", "# standard gene symbols for interpretation and cross-platform compatibility\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "fee644a7", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "7de7f6f9", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:25:29.983138Z", "iopub.status.busy": "2025-03-25T07:25:29.983037Z", "iopub.status.idle": "2025-03-25T07:25:33.339635Z", "shell.execute_reply": "2025-03-25T07:25:33.339253Z" } }, "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 1465067 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 1465067\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": "32ac0c5d", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "0c33d9a1", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:25:33.341050Z", "iopub.status.busy": "2025-03-25T07:25:33.340922Z", "iopub.status.idle": "2025-03-25T07:25:33.852300Z", "shell.execute_reply": "2025-03-25T07:25:33.851908Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Creating gene mapping...\n", "Successfully created mapping with 44837 entries\n", "\n", "Mapping preview (first 5 rows):\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", "Gene expression data contains 47231 unique probe IDs\n", "Mapping data contains 44837 unique probe IDs\n", "Number of probes that can be mapped: 43961 (93.08%)\n", "\n", "Applying gene mapping to convert probe measurements to gene expression...\n", "Successfully created gene expression data with 21372 genes\n", "\n", "Gene expression data preview (first few genes):\n", "{'GSM3130825': [10.42, 14.17, 13.600000000000001, 18.33, 9.54], 'GSM3130826': [10.59, 13.469999999999999, 13.6, 18.43, 9.26], 'GSM3130827': [10.14, 14.49, 13.57, 18.17, 9.41], 'GSM3130828': [10.42, 14.09, 13.61, 18.24, 9.47], 'GSM3130829': [9.879999999999999, 14.0, 13.5, 18.560000000000002, 9.27], 'GSM3130830': [9.53, 13.64, 14.47, 18.25, 9.18], 'GSM3130831': [10.34, 13.86, 13.66, 18.11, 9.77], 'GSM3130832': [10.12, 13.98, 13.73, 18.41, 9.66], 'GSM3130833': [10.4, 14.129999999999999, 13.99, 17.75, 9.51], 'GSM3130834': [10.8, 14.61, 13.940000000000001, 18.42, 9.24], 'GSM3130835': [10.38, 14.21, 13.83, 18.35, 9.29], 'GSM3130836': [10.629999999999999, 14.65, 13.760000000000002, 18.06, 9.26], 'GSM3130837': [12.899999999999999, 19.16, 18.86, 25.22, 7.16], 'GSM3130838': [12.98, 19.18, 19.05, 25.3, 7.12], 'GSM3130839': [12.74, 19.03, 18.9, 25.04, 7.41], 'GSM3130840': [12.94, 19.07, 19.009999999999998, 25.25, 7.53], 'GSM3130841': [14.100000000000001, 19.560000000000002, 19.259999999999998, 25.95, 6.7], 'GSM3130842': [12.719999999999999, 19.69, 19.11, 25.369999999999997, 7.59], 'GSM3130843': [12.92, 19.17, 18.92, 25.4, 7.49], 'GSM3130844': [12.8, 19.06, 19.02, 25.13, 7.49], 'GSM3130845': [12.9, 18.95, 19.009999999999998, 25.509999999999998, 7.57], 'GSM3130846': [12.91, 19.119999999999997, 18.9, 25.33, 7.33], 'GSM3130847': [12.98, 19.02, 19.32, 25.2, 7.43], 'GSM3130848': [12.95, 19.009999999999998, 19.08, 25.240000000000002, 7.25], 'GSM3130849': [12.68, 18.94, 19.23, 25.44, 7.39], 'GSM3130850': [12.940000000000001, 19.15, 19.16, 25.240000000000002, 7.36], 'GSM3130851': [12.899999999999999, 19.15, 18.96, 25.1, 7.46], 'GSM3130852': [12.969999999999999, 19.04, 18.93, 25.35, 7.35], 'GSM3130853': [12.780000000000001, 18.93, 18.990000000000002, 25.14, 6.83], 'GSM3130854': [12.92, 19.23, 18.98, 25.13, 7.38]}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene expression data saved to ../../output/preprocess/Large_B-cell_Lymphoma/gene_data/GSE114022.csv\n" ] } ], "source": [ "# 1. Identify the appropriate columns for mapping\n", "# From previous output, we can see:\n", "# - Gene expression data uses Illumina probe IDs (ILMN_*) in the 'ID' column\n", "# - Gene annotation has 'ID' column for probe IDs and 'Symbol' column for gene symbols\n", "\n", "print(\"Creating gene mapping...\")\n", "# Extract the mapping between probe IDs and gene symbols\n", "mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol')\n", "print(f\"Successfully created mapping with {len(mapping_df)} entries\")\n", "\n", "# Preview the mapping to verify structure\n", "print(\"\\nMapping preview (first 5 rows):\")\n", "print(preview_df(mapping_df))\n", "\n", "# 2. Check the mapping coverage of the gene expression data\n", "probes_in_gene_data = set(gene_data.index)\n", "probes_in_mapping = set(mapping_df['ID'])\n", "common_probes = probes_in_gene_data.intersection(probes_in_mapping)\n", "\n", "print(f\"\\nGene expression data contains {len(probes_in_gene_data)} unique probe IDs\")\n", "print(f\"Mapping data contains {len(probes_in_mapping)} unique probe IDs\")\n", "print(f\"Number of probes that can be mapped: {len(common_probes)} ({len(common_probes)/len(probes_in_gene_data)*100:.2f}%)\")\n", "\n", "# 3. Apply gene mapping to convert probe-level measurements to gene expression data\n", "print(\"\\nApplying gene mapping to convert probe measurements to gene expression...\")\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "print(f\"Successfully created gene expression data with {len(gene_data)} genes\")\n", "\n", "# Preview the gene expression data\n", "print(\"\\nGene expression data preview (first few genes):\")\n", "print(preview_df(gene_data))\n", "\n", "# Save the gene expression data to a 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": "41c1b93f", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "bf095db2", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:25:33.853682Z", "iopub.status.busy": "2025-03-25T07:25:33.853566Z", "iopub.status.idle": "2025-03-25T07:25:33.896055Z", "shell.execute_reply": "2025-03-25T07:25:33.895685Z" } }, "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/GSE114022.csv...\n", "Loaded gene data with shape: (21372, 30)\n", "Sample of gene symbols: ['A1BG', 'A1CF', 'A26C3', 'A2BP1', 'A2LD1']\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: (30, 21372)\n", "\n", "Performing final validation...\n", "A new JSON file was created at: ../../output/preprocess/Large_B-cell_Lymphoma/cohort_info.json\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 }