{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "419a49ac", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:52:29.392589Z", "iopub.status.busy": "2025-03-25T07:52:29.392355Z", "iopub.status.idle": "2025-03-25T07:52:29.558142Z", "shell.execute_reply": "2025-03-25T07:52:29.557821Z" } }, "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 = \"GSE280643\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Lung_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Lung_Cancer/GSE280643\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Lung_Cancer/GSE280643.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Lung_Cancer/gene_data/GSE280643.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Lung_Cancer/clinical_data/GSE280643.csv\"\n", "json_path = \"../../output/preprocess/Lung_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "3d0c1782", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "fce00c97", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:52:29.559553Z", "iopub.status.busy": "2025-03-25T07:52:29.559417Z", "iopub.status.idle": "2025-03-25T07:52:29.624169Z", "shell.execute_reply": "2025-03-25T07:52:29.623882Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Differential KEAP1/NRF2-mediated signaling widens the therapeutic window of redox-targeting drugs in SCLC therapy\"\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: ['tissue: normal lung', 'tissue: small cell lung cancer', 'tissue: normal skin']}\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": "c4be6fe2", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "7bb24ebc", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:52:29.625460Z", "iopub.status.busy": "2025-03-25T07:52:29.625359Z", "iopub.status.idle": "2025-03-25T07:52:29.631995Z", "shell.execute_reply": "2025-03-25T07:52:29.631720Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical Features Preview:\n", "{'GSM8602788': [0.0], 'GSM8602789': [0.0], 'GSM8602790': [0.0], 'GSM8602791': [0.0], 'GSM8602792': [0.0], 'GSM8602793': [0.0], 'GSM8602794': [1.0], 'GSM8602795': [1.0], 'GSM8602796': [1.0], 'GSM8602797': [1.0], 'GSM8602798': [1.0], 'GSM8602799': [1.0], 'GSM8602800': [nan], 'GSM8602801': [nan], 'GSM8602802': [nan], 'GSM8602803': [nan], 'GSM8602804': [nan], 'GSM8602805': [nan], 'GSM8602806': [1.0], 'GSM8602807': [1.0], 'GSM8602808': [1.0], 'GSM8602809': [1.0], 'GSM8602810': [1.0], 'GSM8602811': [1.0]}\n", "Clinical data saved to ../../output/preprocess/Lung_Cancer/clinical_data/GSE280643.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Since this is a SuperSeries composed of SubSeries, and the sample characteristics\n", "# indicates tissue types including normal lung and small cell lung cancer,\n", "# it's likely that gene expression data is included in one of the SubSeries\n", "is_gene_available = True\n", "\n", "# 2.1 Data Availability\n", "# From the sample characteristics dictionary, we see only one key (0)\n", "# The values are tissue types: normal lung, small cell lung cancer, normal skin\n", "# We can use this to infer our trait (lung cancer) - samples with \"small cell lung cancer\" are cases (1)\n", "# and samples with \"normal lung\" are controls (0)\n", "trait_row = 0 # Using the available row for tissue type\n", "\n", "# Age and gender are not available in the sample characteristics\n", "age_row = None\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion Functions\n", "def convert_trait(value):\n", " \"\"\"Convert tissue type to binary trait value (1 for cancer, 0 for normal lung)\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " # Convert to binary based on tissue type\n", " if \"small cell lung cancer\" in value.lower():\n", " return 1 # Case: has lung cancer\n", " elif \"normal lung\" in value.lower():\n", " return 0 # Control: normal lung tissue\n", " else:\n", " return None # Other tissue types like normal skin are not relevant\n", "\n", "def convert_age(value):\n", " \"\"\"Placeholder function as age data is not available\"\"\"\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Placeholder function as gender data is not available\"\"\"\n", " return None\n", "\n", "# 3. Save Metadata\n", "# The trait data is available (trait_row is not None)\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", "# Since trait_row is not None, we should extract clinical features\n", "if is_trait_available:\n", " # Assuming clinical_data is defined from a previous step\n", " clinical_features = geo_select_clinical_features(\n", " clinical_df=clinical_data, # This should be defined in a previous step\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 extracted clinical features\n", " preview = preview_df(clinical_features)\n", " print(\"Clinical Features Preview:\")\n", " print(preview)\n", " \n", " # Save the clinical features to CSV\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " clinical_features.to_csv(out_clinical_data_file)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "cbea5f55", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "3523dfcd", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:52:29.633173Z", "iopub.status.busy": "2025-03-25T07:52:29.633071Z", "iopub.status.idle": "2025-03-25T07:52:29.706288Z", "shell.execute_reply": "2025-03-25T07:52:29.705947Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Examining matrix file structure...\n", "Line 0: !Series_title\t\"Differential KEAP1/NRF2-mediated signaling widens the therapeutic window of redox-targeting drugs in SCLC therapy\"\n", "Line 1: !Series_geo_accession\t\"GSE280643\"\n", "Line 2: !Series_status\t\"Public on Nov 11 2024\"\n", "Line 3: !Series_submission_date\t\"Oct 30 2024\"\n", "Line 4: !Series_last_update_date\t\"Nov 12 2024\"\n", "Line 5: !Series_web_link\t\"https://doi.org/10.1101/2024.11.06.621846\"\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 high throughput sequencing\"\n", "Line 9: !Series_type\t\"Expression profiling by array\"\n", "Found table marker at line 63\n", "First few lines after marker:\n", "\"ID_REF\"\t\"GSM8602788\"\t\"GSM8602789\"\t\"GSM8602790\"\t\"GSM8602791\"\t\"GSM8602792\"\t\"GSM8602793\"\t\"GSM8602794\"\t\"GSM8602795\"\t\"GSM8602796\"\t\"GSM8602797\"\t\"GSM8602798\"\t\"GSM8602799\"\t\"GSM8602800\"\t\"GSM8602801\"\t\"GSM8602802\"\t\"GSM8602803\"\t\"GSM8602804\"\t\"GSM8602805\"\t\"GSM8602806\"\t\"GSM8602807\"\t\"GSM8602808\"\t\"GSM8602809\"\t\"GSM8602810\"\t\"GSM8602811\"\n", "\"23064070\"\t2000.555556\t1472.875\t1182.25\t1432.125\t1851\t1426\t1467.125\t1137.75\t1604.5\t1847\t1540.625\t1695.777778\t1489.625\t1412.25\t1653.444444\t1037\t2112\t1252.75\t2384\t3073.9\t2125.875\t3102\t1982.875\t3265\n", "\"23064071\"\t886.2857143\t818.4444444\t675\t718.2222222\t760.8888889\t702.3333333\t440.375\t413\t460.75\t565.5\t494.4285714\t530.625\t901.1111111\t692.375\t1003.9\t716.2222222\t747.75\t672\t973.3333333\t951.1\t976.4\t939.2222222\t842.4\t1023.3\n", "\"23064072\"\t353.7777778\t332.25\t312\t295.125\t382.4\t351\t221\t194.8888889\t202.9\t237.3333333\t201.75\t249.3\t689.5\t848.7777778\t734.25\t683.2222222\t438.8333333\t545.625\t482.1\t262.375\t352.1111111\t427\t338.4444444\t408\n", "\"23064073\"\t1062\t1055.5\t909.2\t923\t931.4\t730.1111111\t733.6666667\t1014.6\t862.7777778\t1040.333333\t1229.3\t861\t968.7777778\t832.875\t995.3333333\t791.8888889\t1131.7\t998.6\t1082.142857\t1126.666667\t974.125\t983\t1210\t1269.111111\n", "Total lines examined: 64\n", "\n", "Attempting to extract gene data from matrix file...\n", "Successfully extracted gene data with 27189 rows\n", "First 20 gene IDs:\n", "Index(['23064070', '23064071', '23064072', '23064073', '23064074', '23064075',\n", " '23064076', '23064077', '23064078', '23064079', '23064080', '23064081',\n", " '23064083', '23064084', '23064085', '23064086', '23064087', '23064088',\n", " '23064089', '23064090'],\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": "e8c891b2", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "a5e265c5", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:52:29.707692Z", "iopub.status.busy": "2025-03-25T07:52:29.707574Z", "iopub.status.idle": "2025-03-25T07:52:29.709403Z", "shell.execute_reply": "2025-03-25T07:52:29.709138Z" } }, "outputs": [], "source": [ "# Examining the gene identifiers in the gene expression data\n", "# IDs like '23064070', '23064071', etc. are numeric identifiers, likely probe IDs\n", "# These are not standard human gene symbols (which would typically be like 'BRCA1', 'TP53', etc.)\n", "# These appear to be Illumina or Affymetrix probe IDs that need to be mapped to gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "e56a43cd", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "72fc0710", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:52:29.710733Z", "iopub.status.busy": "2025-03-25T07:52:29.710631Z", "iopub.status.idle": "2025-03-25T07:52:31.613169Z", "shell.execute_reply": "2025-03-25T07:52:31.612831Z" } }, "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 679749 rows\n", "\n", "Gene annotation preview (first few rows):\n", "{'ID': ['TC0100006437.hg.1', 'TC0100006476.hg.1', 'TC0100006479.hg.1', 'TC0100006480.hg.1', 'TC0100006483.hg.1'], 'probeset_id': ['TC0100006437.hg.1', 'TC0100006476.hg.1', 'TC0100006479.hg.1', 'TC0100006480.hg.1', 'TC0100006483.hg.1'], 'seqname': ['chr1', 'chr1', 'chr1', 'chr1', 'chr1'], 'strand': ['+', '+', '+', '+', '+'], 'start': ['69091', '924880', '960587', '966497', '1001138'], 'stop': ['70008', '944581', '965719', '975865', '1014541'], 'total_probes': [10.0, 10.0, 10.0, 10.0, 10.0], 'category': ['main', 'main', 'main', 'main', 'main'], 'SPOT_ID': ['Coding', 'Multiple_Complex', 'Multiple_Complex', 'Multiple_Complex', 'Multiple_Complex'], 'SPOT_ID.1': ['NM_001005484 // RefSeq // Homo sapiens olfactory receptor, family 4, subfamily F, member 5 (OR4F5), mRNA. // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000335137 // ENSEMBL // olfactory receptor, family 4, subfamily F, member 5 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000003223 // Havana transcript // olfactory receptor, family 4, subfamily F, member 5[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// uc001aal.1 // UCSC Genes // Homo sapiens olfactory receptor, family 4, subfamily F, member 5 (OR4F5), mRNA. // chr1 // 100 // 100 // 0 // --- // 0 /// CCDS30547.1 // ccdsGene // olfactory receptor, family 4, subfamily F, member 5 [Source:HGNC Symbol;Acc:HGNC:14825] // chr1 // 100 // 100 // 0 // --- // 0', 'NM_152486 // RefSeq // Homo sapiens sterile alpha motif domain containing 11 (SAMD11), mRNA. // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000341065 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000342066 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000420190 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000437963 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000455979 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000464948 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000466827 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000474461 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000478729 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:processed_transcript] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000616016 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000616125 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000617307 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000618181 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000618323 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000618779 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000620200 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000622503 // ENSEMBL // sterile alpha motif domain containing 11 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// BC024295 // GenBank // Homo sapiens sterile alpha motif domain containing 11, mRNA (cDNA clone MGC:39333 IMAGE:3354502), complete cds. // chr1 // 100 // 100 // 0 // --- // 0 /// BC033213 // GenBank // Homo sapiens sterile alpha motif domain containing 11, mRNA (cDNA clone MGC:45873 IMAGE:5014368), complete cds. // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097860 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097862 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097863 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097865 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:processed_transcript] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097866 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097867 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097868 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000276866 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000316521 // Havana transcript // sterile alpha motif domain containing 11[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// CCDS2.2 // ccdsGene // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009185 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVERLAPTX, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009186 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVERLAPTX, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009187 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009188 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009189 // circbase // Salzman2013 ALT_DONOR, CDS, coding, INTERNAL, OVCODE, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009190 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009191 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009192 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVERLAPTX, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009193 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVERLAPTX, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009194 // circbase // Salzman2013 ANNOTATED, CDS, coding, OVCODE, OVERLAPTX, OVEXON, UTR3 best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009195 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVERLAPTX, OVEXON best transcript NM_152486 // chr1 // 100 // 100 // 0 // --- // 0 /// uc001abw.2 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc031pjt.2 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc031pju.2 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc031pkg.2 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc031pkh.2 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc031pkk.2 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc031pkm.2 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc031pko.2 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057axs.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057axt.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057axu.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057axv.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057axw.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057axx.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057axy.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057axz.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057aya.1 // UCSC Genes // sterile alpha motif domain containing 11 [Source:HGNC Symbol;Acc:HGNC:28706] // chr1 // 100 // 100 // 0 // --- // 0 /// NONHSAT000212 // lncRNAWiki // Non-coding transcript identified by NONCODE // chr1 // 100 // 100 // 0 // --- // 0 /// NONHSAT000212 // NONCODE // Non-coding transcript identified by NONCODE: Exonic // chr1 // 100 // 100 // 0 // --- // 0 /// NONHSAT000213 // lncRNAWiki // Non-coding transcript identified by NONCODE // chr1 // 100 // 100 // 0 // --- // 0 /// NONHSAT000213 // NONCODE // Non-coding transcript identified by NONCODE: Exonic // chr1 // 100 // 100 // 0 // --- // 0', 'NM_198317 // RefSeq // Homo sapiens kelch-like family member 17 (KLHL17), mRNA. // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000338591 // ENSEMBL // kelch-like family member 17 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000463212 // ENSEMBL // kelch-like family member 17 [gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000466300 // ENSEMBL // kelch-like family member 17 [gene_biotype:protein_coding transcript_biotype:nonsense_mediated_decay] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000481067 // ENSEMBL // kelch-like family member 17 [gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000622660 // ENSEMBL // kelch-like family member 17 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097875 // Havana transcript // kelch-like 17 (Drosophila)[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097877 // Havana transcript // kelch-like 17 (Drosophila)[gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097878 // Havana transcript // kelch-like 17 (Drosophila)[gene_biotype:protein_coding transcript_biotype:nonsense_mediated_decay] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097931 // Havana transcript // kelch-like 17 (Drosophila)[gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// BC166618 // GenBank // Synthetic construct Homo sapiens clone IMAGE:100066344, MGC:195481 kelch-like 17 (Drosophila) (KLHL17) mRNA, encodes complete protein. // chr1 // 100 // 100 // 0 // --- // 0 /// CCDS30550.1 // ccdsGene // kelch-like family member 17 [Source:HGNC Symbol;Acc:HGNC:24023] // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009209 // circbase // Salzman2013 ANNOTATED, CDS, coding, INTERNAL, OVCODE, OVEXON best transcript NM_198317 // chr1 // 100 // 100 // 0 // --- // 0 /// uc001aca.3 // UCSC Genes // kelch-like family member 17 [Source:HGNC Symbol;Acc:HGNC:24023] // chr1 // 100 // 100 // 0 // --- // 0 /// uc001acb.2 // UCSC Genes // kelch-like family member 17 [Source:HGNC Symbol;Acc:HGNC:24023] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057ayg.1 // UCSC Genes // kelch-like family member 17 [Source:HGNC Symbol;Acc:HGNC:24023] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057ayh.1 // UCSC Genes // kelch-like family member 17 [Source:HGNC Symbol;Acc:HGNC:24023] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057ayi.1 // UCSC Genes // kelch-like family member 17 [Source:HGNC Symbol;Acc:HGNC:24023] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057ayj.1 // UCSC Genes // N/A // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000617073 // ENSEMBL // ncrna:novel chromosome:GRCh38:1:965110:965166:1 gene:ENSG00000277294 gene_biotype:miRNA transcript_biotype:miRNA // chr1 // 100 // 100 // 0 // --- // 0 /// NONHSAT000216 // lncRNAWiki // Non-coding transcript identified by NONCODE // chr1 // 100 // 100 // 0 // --- // 0 /// NONHSAT000216 // NONCODE // Non-coding transcript identified by NONCODE: Exonic // chr1 // 100 // 100 // 0 // --- // 0', 'NM_001160184 // RefSeq // Homo sapiens pleckstrin homology domain containing, family N member 1 (PLEKHN1), transcript variant 2, mRNA. // chr1 // 100 // 100 // 0 // --- // 0 /// NM_032129 // RefSeq // Homo sapiens pleckstrin homology domain containing, family N member 1 (PLEKHN1), transcript variant 1, mRNA. // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000379407 // ENSEMBL // pleckstrin homology domain containing, family N member 1 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000379409 // ENSEMBL // pleckstrin homology domain containing, family N member 1 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000379410 // ENSEMBL // pleckstrin homology domain containing, family N member 1 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000480267 // ENSEMBL // pleckstrin homology domain containing, family N member 1 [gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000491024 // ENSEMBL // pleckstrin homology domain containing, family N member 1 [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// BC101386 // GenBank // Homo sapiens pleckstrin homology domain containing, family N member 1, mRNA (cDNA clone MGC:120613 IMAGE:40026400), complete cds. // chr1 // 100 // 100 // 0 // --- // 0 /// BC101387 // GenBank // Homo sapiens pleckstrin homology domain containing, family N member 1, mRNA (cDNA clone MGC:120616 IMAGE:40026404), complete cds. // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097940 // Havana transcript // pleckstrin homology domain containing, family N member 1[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097941 // Havana transcript // pleckstrin homology domain containing, family N member 1[gene_biotype:protein_coding transcript_biotype:retained_intron] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097942 // Havana transcript // pleckstrin homology domain containing, family N member 1[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000473255 // Havana transcript // pleckstrin homology domain containing, family N member 1[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000473256 // Havana transcript // pleckstrin homology domain containing, family N member 1[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// CCDS4.1 // ccdsGene // pleckstrin homology domain containing, family N member 1 [Source:HGNC Symbol;Acc:HGNC:25284] // chr1 // 100 // 100 // 0 // --- // 0 /// CCDS53256.1 // ccdsGene // pleckstrin homology domain containing, family N member 1 [Source:HGNC Symbol;Acc:HGNC:25284] // chr1 // 100 // 100 // 0 // --- // 0 /// PLEKHN1.aAug10 // Ace View // Transcript Identified by AceView, Entrez Gene ID(s) 84069 // chr1 // 100 // 100 // 0 // --- // 0 /// PLEKHN1.bAug10 // Ace View // Transcript Identified by AceView, Entrez Gene ID(s) 84069, RefSeq ID(s) NM_032129 // chr1 // 100 // 100 // 0 // --- // 0 /// uc001acd.4 // UCSC Genes // pleckstrin homology domain containing, family N member 1 [Source:HGNC Symbol;Acc:HGNC:25284] // chr1 // 100 // 100 // 0 // --- // 0 /// uc001ace.4 // UCSC Genes // pleckstrin homology domain containing, family N member 1 [Source:HGNC Symbol;Acc:HGNC:25284] // chr1 // 100 // 100 // 0 // --- // 0 /// uc001acf.4 // UCSC Genes // pleckstrin homology domain containing, family N member 1 [Source:HGNC Symbol;Acc:HGNC:25284] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057ayk.1 // UCSC Genes // pleckstrin homology domain containing, family N member 1 [Source:HGNC Symbol;Acc:HGNC:25284] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057ayl.1 // UCSC Genes // pleckstrin homology domain containing, family N member 1 [Source:HGNC Symbol;Acc:HGNC:25284] // chr1 // 100 // 100 // 0 // --- // 0 /// NONHSAT000217 // lncRNAWiki // Non-coding transcript identified by NONCODE // chr1 // 100 // 100 // 0 // --- // 0 /// NONHSAT000217 // NONCODE // Non-coding transcript identified by NONCODE: Exonic // chr1 // 100 // 100 // 0 // --- // 0', 'NM_005101 // RefSeq // Homo sapiens ISG15 ubiquitin-like modifier (ISG15), mRNA. // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000379389 // ENSEMBL // ISG15 ubiquitin-like modifier [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000624652 // ENSEMBL // ISG15 ubiquitin-like modifier [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000624697 // ENSEMBL // ISG15 ubiquitin-like modifier [gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// BC009507 // GenBank // Homo sapiens ISG15 ubiquitin-like modifier, mRNA (cDNA clone MGC:3945 IMAGE:3545944), complete cds. // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000097989 // Havana transcript // ISG15 ubiquitin-like modifier[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000479384 // Havana transcript // ISG15 ubiquitin-like modifier[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000479385 // Havana transcript // ISG15 ubiquitin-like modifier[gene_biotype:protein_coding transcript_biotype:protein_coding] // chr1 // 100 // 100 // 0 // --- // 0 /// CCDS6.1 // ccdsGene // ISG15 ubiquitin-like modifier [Source:HGNC Symbol;Acc:HGNC:4053] // chr1 // 100 // 100 // 0 // --- // 0 /// hsa_circ_0009211 // circbase // Salzman2013 ANNOTATED, CDS, coding, OVCODE, OVEXON, UTR3 best transcript NM_005101 // chr1 // 100 // 100 // 0 // --- // 0 /// ISG15.bAug10 // Ace View // Transcript Identified by AceView, Entrez Gene ID(s) 9636 // chr1 // 100 // 100 // 0 // --- // 0 /// ISG15.cAug10 // Ace View // Transcript Identified by AceView, Entrez Gene ID(s) 9636 // chr1 // 100 // 100 // 0 // --- // 0 /// uc001acj.5 // UCSC Genes // ISG15 ubiquitin-like modifier [Source:HGNC Symbol;Acc:HGNC:4053] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057ayq.1 // UCSC Genes // ISG15 ubiquitin-like modifier [Source:HGNC Symbol;Acc:HGNC:4053] // chr1 // 100 // 100 // 0 // --- // 0 /// uc057ayr.1 // UCSC Genes // ISG15 ubiquitin-like modifier [Source:HGNC Symbol;Acc:HGNC:4053] // chr1 // 100 // 100 // 0 // --- // 0']}\n", "\n", "Column names in gene annotation data:\n", "['ID', 'probeset_id', 'seqname', 'strand', 'start', 'stop', 'total_probes', 'category', 'SPOT_ID', 'SPOT_ID.1']\n", "\n", "The dataset contains genomic regions (SPOT_ID) that could be used for location-based gene mapping.\n", "Example SPOT_ID format: Coding\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": "6f2abea1", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "1744999a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:52:31.614985Z", "iopub.status.busy": "2025-03-25T07:52:31.614879Z", "iopub.status.idle": "2025-03-25T07:52:54.675800Z", "shell.execute_reply": "2025-03-25T07:52:54.675477Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Looking for probe-to-gene mapping in the SOFT file...\n", "Sample probe IDs from expression data:\n", "['23064070', '23064071', '23064072', '23064073', '23064074']\n", "Dataset uses platform: GPL23159\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Found 3 platform sections\n", "No structured mapping found, searching for probe ID definitions...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Could not find direct probe-to-gene mappings.\n", "Using 27189 probe IDs directly as gene identifiers\n", "Using probe IDs as gene identifiers\n", "{'ID': ['23064070', '23064071', '23064072', '23064073', '23064074'], 'Gene': [['23064070'], ['23064071'], ['23064072'], ['23064073'], ['23064074']]}\n", "\n", "Applying gene mapping to 27189 probe measurements...\n", "Converted to gene expression data with 0 genes\n", "WARNING: No genes were mapped successfully!\n", "Using original 0 probe measurements as gene data\n", "Gene expression data saved to ../../output/preprocess/Lung_Cancer/gene_data/GSE280643.csv\n" ] } ], "source": [ "# Examining the gene expression data and annotation data\n", "# From previous steps:\n", "# - Gene expression data has IDs like '23064070', '23064071', etc.\n", "# - We need to map these probe IDs to gene symbols\n", "\n", "# First, let's search directly in the SOFT file for the probe-to-gene mapping\n", "print(\"Looking for probe-to-gene mapping in the SOFT file...\")\n", "print(\"Sample probe IDs from expression data:\")\n", "print(gene_data.index[:5].tolist())\n", "\n", "# Let's extract platform information first\n", "platform_id = None\n", "with gzip.open(soft_file, 'rt') as f:\n", " for line in f:\n", " if line.startswith(\"!Series_platform_id\"):\n", " platform_id = line.split(\"=\")[1].strip().strip('\"')\n", " break\n", "print(f\"Dataset uses platform: {platform_id}\")\n", "\n", "# Now let's directly search for probe-to-gene mappings in the SOFT file\n", "# We're looking for sections that define relations between probe IDs and gene symbols\n", "with gzip.open(soft_file, 'rt') as f:\n", " content = f.read()\n", " \n", " # Search for ID definitions that match our expression data probe IDs\n", " probe_mappings = []\n", " \n", " # Try to find the platform table sections which typically contain gene mappings\n", " platform_sections = re.findall(r'^\\^PLATFORM.+?(?=^\\^|\\Z)', content, re.MULTILINE | re.DOTALL)\n", " \n", " if platform_sections:\n", " print(f\"Found {len(platform_sections)} platform sections\")\n", " \n", " # Process each platform section\n", " for section in platform_sections:\n", " # Check for a table with gene info\n", " table_match = re.search(r'!platform_table_begin(.+?)!platform_table_end', section, re.DOTALL)\n", " if table_match:\n", " table_content = table_match.group(1)\n", " lines = table_content.strip().split('\\n')\n", " \n", " # Get header line to identify columns\n", " header = lines[0].split('\\t')\n", " \n", " # Find ID column and gene symbol column\n", " id_col = -1\n", " symbol_col = -1\n", " \n", " for i, col_name in enumerate(header):\n", " if col_name == 'ID':\n", " id_col = i\n", " elif 'GENE' in col_name.upper() or 'SYMBOL' in col_name.upper():\n", " symbol_col = i\n", " \n", " if id_col >= 0 and symbol_col >= 0:\n", " print(f\"Found ID column at {id_col} and symbol column at {symbol_col}\")\n", " # Process data rows\n", " for line in lines[1:]:\n", " cols = line.split('\\t')\n", " if len(cols) > max(id_col, symbol_col):\n", " probe_id = cols[id_col]\n", " gene_symbol = cols[symbol_col]\n", " if gene_symbol and probe_id:\n", " symbols = extract_human_gene_symbols(gene_symbol)\n", " if symbols:\n", " probe_mappings.append((probe_id, symbols))\n", " \n", " # If we didn't find a proper table, try a more aggressive approach\n", " # Look for ID lines in the entire file\n", " if not probe_mappings:\n", " print(\"No structured mapping found, searching for probe ID definitions...\")\n", " # Get a sample of probe IDs we need to match\n", " sample_ids = gene_data.index[:10].tolist()\n", " \n", " for probe_id in sample_ids:\n", " # Look for lines containing this probe ID\n", " probe_matches = re.findall(r'[^\\w]' + re.escape(probe_id) + r'[^\\w](.+?)$', content, re.MULTILINE)\n", " for match in probe_matches:\n", " symbols = extract_human_gene_symbols(match)\n", " if symbols:\n", " probe_mappings.append((probe_id, symbols))\n", " break\n", "\n", "# If we found mappings, create the mapping dataframe\n", "if probe_mappings:\n", " mapping_df = pd.DataFrame(probe_mappings, columns=['ID', 'Gene'])\n", " print(f\"Found {len(mapping_df)} probe-to-gene mappings\")\n", " print(\"Sample of extracted mappings:\")\n", " print(preview_df(mapping_df))\n", "else:\n", " print(\"Could not find direct probe-to-gene mappings.\")\n", " \n", " # As a fallback, create a simple mapping using numeric probe IDs only\n", " # This will at least allow data processing to continue\n", " probe_ids = gene_data.index.tolist()\n", " print(f\"Using {len(probe_ids)} probe IDs directly as gene identifiers\")\n", " \n", " # Create a simple mapping - each probe ID represents one \"gene\"\n", " # Map the ID to itself (as a list with one element, as expected by apply_gene_mapping)\n", " mapping_df = pd.DataFrame({'ID': probe_ids, 'Gene': [[id] for id in probe_ids]})\n", " print(\"Using probe IDs as gene identifiers\")\n", " print(preview_df(mapping_df))\n", "\n", "# Apply the gene mapping to convert probe-level data to gene expression data\n", "print(f\"\\nApplying gene mapping to {len(gene_data)} probe measurements...\")\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "\n", "# Apply gene symbol normalization to handle gene synonyms\n", "gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Converted to gene expression data with {len(gene_data.index)} genes\")\n", "if len(gene_data.index) > 0:\n", " print(\"First 20 genes:\")\n", " print(gene_data.index[:20])\n", "else:\n", " print(\"WARNING: No genes were mapped successfully!\")\n", " # In this case, use the original probe IDs directly\n", " gene_data = gene_data.rename_axis('Gene')\n", " print(f\"Using original {len(gene_data)} probe measurements as gene data\")\n", "\n", "# Save the gene expression data\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "gene_data.to_csv(out_gene_data_file)\n", "print(f\"Gene expression data saved to {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "198e2ac2", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "171319b2", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:52:54.677799Z", "iopub.status.busy": "2025-03-25T07:52:54.677649Z", "iopub.status.idle": "2025-03-25T07:52:54.800132Z", "shell.execute_reply": "2025-03-25T07:52:54.799809Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Normalizing gene symbols using NCBI Gene database...\n", "After normalization: 0 unique genes remain\n", "Normalized gene expression data saved to ../../output/preprocess/Lung_Cancer/gene_data/GSE280643.csv\n", "\n", "Extracting clinical data directly from the matrix file...\n", "\n", "Processing clinical data...\n", "Extracted clinical data with shape: (1, 24)\n", "Clinical data saved to: ../../output/preprocess/Lung_Cancer/clinical_data/GSE280643.csv\n", "\n", "Linking clinical and genetic data...\n", "Found 24 common samples between clinical and genetic data\n", "Linked data shape: (24, 1)\n", "\n", "Handling missing values in linked data...\n", "Data shape after handling missing values: (0, 1)\n", "\n", "Evaluating trait and demographic feature bias...\n", "Quartiles for 'Lung_Cancer':\n", " 25%: nan\n", " 50% (Median): nan\n", " 75%: nan\n", "Min: nan\n", "Max: nan\n", "The distribution of the feature 'Lung_Cancer' in this dataset is fine.\n", "\n", "Abnormality detected in the cohort: GSE280643. Preprocessing failed.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\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 }