{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "3a10f0e7", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:30:50.130747Z", "iopub.status.busy": "2025-03-25T07:30:50.130643Z", "iopub.status.idle": "2025-03-25T07:30:50.293280Z", "shell.execute_reply": "2025-03-25T07:30:50.292910Z" } }, "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 = \"Liver_Cancer\"\n", "cohort = \"GSE228783\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Liver_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Liver_Cancer/GSE228783\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Liver_Cancer/GSE228783.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Liver_Cancer/gene_data/GSE228783.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Liver_Cancer/clinical_data/GSE228783.csv\"\n", "json_path = \"../../output/preprocess/Liver_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "355d56ff", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "2cc1d86a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:30:50.294799Z", "iopub.status.busy": "2025-03-25T07:30:50.294650Z", "iopub.status.idle": "2025-03-25T07:30:50.637327Z", "shell.execute_reply": "2025-03-25T07:30:50.636933Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Inter-patient heterogeneity in the hepatic ischemia-reperfusion injury transcriptome: implications for research and diagnostics\"\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: liver'], 1: ['patient: 1', 'patient: 2', 'patient: 3', 'patient: 4', 'patient: 5', 'patient: 6', 'patient: 7', 'patient: 8', 'patient: 9', 'patient: 10', 'patient: 11', 'patient: pat 1', 'patient: pat 2', 'patient: pat 3', 'patient: pat 4', 'patient: pat 5', 'patient: pat 6', 'patient: pat 7', 'patient: pat 8', 'patient: pat 9', 'patient: pat 10', 'patient: pat 11', 'patient: pat 12', 'patient: pat 13', 'patient: pat 14', 'patient: pat 15', 'patient: pat 16', 'patient: pat 17', 'patient: pat 18', 'patient: pat 19'], 2: ['disease: CRC Met', 'disease: CCC', 'disease: HCC', 'disease: other'], 3: ['steatosis: NA', 'steatosis: <5%', 'steatosis: >20%'], 4: ['time point: 0', 'time point: 30', 'time point: 60', 'time point: 120', 'time point: 180', 'time point: 360', 'time point: 20', 'time point: 12', 'time point: 190w', 'time point: 230w', 'time point: 128', 'time point: 50w', 'time point: 125', 'time point: 93w', 'time point: 123w', 'time point: 150', 'time point: 195w', 'time point: 224w', 'time point: 1', 'time point: 3', 'time point: 2']}\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": "5189f4ea", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "7693e69e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:30:50.638784Z", "iopub.status.busy": "2025-03-25T07:30:50.638666Z", "iopub.status.idle": "2025-03-25T07:30:50.661155Z", "shell.execute_reply": "2025-03-25T07:30:50.660760Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of clinical data:\n", "{'GSM7136321': [0.0], 'GSM7136322': [0.0], 'GSM7136323': [0.0], 'GSM7136324': [0.0], 'GSM7136325': [0.0], 'GSM7136326': [0.0], 'GSM7136327': [0.0], 'GSM7136328': [0.0], 'GSM7136329': [0.0], 'GSM7136330': [0.0], 'GSM7136331': [0.0], 'GSM7136332': [0.0], 'GSM7136333': [0.0], 'GSM7136334': [0.0], 'GSM7136335': [0.0], 'GSM7136336': [0.0], 'GSM7136337': [0.0], 'GSM7136338': [0.0], 'GSM7136339': [0.0], 'GSM7136340': [0.0], 'GSM7136341': [0.0], 'GSM7136342': [0.0], 'GSM7136343': [0.0], 'GSM7136344': [0.0], 'GSM7136345': [0.0], 'GSM7136346': [0.0], 'GSM7136347': [0.0], 'GSM7136348': [0.0], 'GSM7136349': [0.0], 'GSM7136350': [0.0], 'GSM7136351': [0.0], 'GSM7136352': [0.0], 'GSM7136353': [0.0], 'GSM7136354': [0.0], 'GSM7136355': [0.0], 'GSM7136356': [0.0], 'GSM7136357': [0.0], 'GSM7136358': [0.0], 'GSM7136359': [0.0], 'GSM7136360': [0.0], 'GSM7136361': [0.0], 'GSM7136362': [0.0], 'GSM7136363': [0.0], 'GSM7136364': [0.0], 'GSM7136365': [0.0], 'GSM7136366': [0.0], 'GSM7136367': [0.0], 'GSM7136368': [0.0], 'GSM7136369': [0.0], 'GSM7136370': [0.0], 'GSM7136371': [0.0], 'GSM7136372': [0.0], 'GSM7136373': [0.0], 'GSM7136374': [0.0], 'GSM7136375': [0.0], 'GSM7136376': [0.0], 'GSM7136377': [0.0], 'GSM7136378': [0.0], 'GSM7136379': [0.0], 'GSM7136380': [0.0], 'GSM7136381': [0.0], 'GSM7136382': [0.0], 'GSM7136383': [0.0], 'GSM7136384': [0.0], 'GSM7136385': [0.0], 'GSM7136386': [0.0], 'GSM7136387': [0.0], 'GSM7136388': [0.0], 'GSM7136389': [0.0], 'GSM7136390': [0.0], 'GSM7136391': [0.0], 'GSM7136392': [0.0], 'GSM7136393': [0.0], 'GSM7136394': [0.0], 'GSM7136395': [0.0], 'GSM7136396': [0.0], 'GSM7136397': [0.0], 'GSM7136398': [0.0], 'GSM7136399': [0.0], 'GSM7136400': [0.0], 'GSM7136401': [1.0], 'GSM7136402': [1.0], 'GSM7136403': [1.0], 'GSM7136404': [1.0], 'GSM7136405': [0.0], 'GSM7136406': [0.0], 'GSM7136407': [0.0], 'GSM7136408': [0.0], 'GSM7136409': [0.0], 'GSM7136410': [0.0], 'GSM7136411': [0.0], 'GSM7136412': [0.0], 'GSM7136413': [0.0], 'GSM7136414': [0.0], 'GSM7136415': [0.0], 'GSM7136416': [0.0], 'GSM7136417': [0.0], 'GSM7136418': [0.0], 'GSM7136419': [0.0], 'GSM7136420': [0.0], 'GSM7136421': [0.0], 'GSM7136422': [0.0], 'GSM7136423': [0.0], 'GSM7136424': [0.0], 'GSM7136425': [0.0], 'GSM7136426': [0.0], 'GSM7136427': [0.0], 'GSM7136428': [0.0], 'GSM7136429': [0.0], 'GSM7136430': [0.0], 'GSM7136431': [0.0], 'GSM7136432': [0.0], 'GSM7136433': [0.0], 'GSM7136434': [0.0], 'GSM7136435': [0.0], 'GSM7136436': [0.0], 'GSM7136437': [0.0], 'GSM7136438': [0.0], 'GSM7136439': [0.0], 'GSM7136440': [0.0], 'GSM7136441': [0.0], 'GSM7136442': [0.0], 'GSM7136443': [0.0], 'GSM7136444': [0.0], 'GSM7136445': [0.0], 'GSM7136446': [0.0], 'GSM7136447': [0.0], 'GSM7136448': [0.0], 'GSM7136449': [0.0], 'GSM7136450': [0.0], 'GSM7136451': [0.0], 'GSM7136452': [0.0], 'GSM7136453': [0.0], 'GSM7136454': [0.0], 'GSM7136455': [0.0], 'GSM7136456': [0.0], 'GSM7136457': [1.0], 'GSM7136458': [1.0], 'GSM7136460': [1.0], 'GSM7136462': [1.0], 'GSM7136465': [0.0], 'GSM7136468': [0.0], 'GSM7136471': [0.0], 'GSM7136472': [0.0], 'GSM7136473': [0.0], 'GSM7136474': [0.0], 'GSM7136475': [0.0], 'GSM7136476': [0.0], 'GSM7136477': [1.0], 'GSM7136478': [1.0], 'GSM7136479': [1.0], 'GSM7136480': [1.0]}\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Looking at the metadata, this dataset seems to be about liver ischemia-reperfusion injury\n", "# and likely contains gene expression data for different time points and disease states\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "\n", "# For trait (Liver Cancer), we can use the disease information in row 2\n", "trait_row = 2\n", "\n", "# Age is not available in the sample characteristics\n", "age_row = None\n", "\n", "# Gender is not available in the sample characteristics\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value):\n", " \"\"\"\n", " Convert disease status to binary (1 for liver cancer, 0 for other)\n", " \"\"\"\n", " if not value or isinstance(value, float) and np.isnan(value):\n", " return None\n", " \n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " # HCC stands for Hepatocellular Carcinoma, which is primary liver cancer\n", " if value == \"HCC\":\n", " return 1\n", " # CRC Met (colorectal cancer metastasis), CCC (cholangiocarcinoma), \n", " # and other conditions are not primary liver cancer\n", " else:\n", " return 0\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age value to continuous numeric format\"\"\"\n", " if value is None or isinstance(value, float) and np.isnan(value):\n", " return None\n", " \n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " try:\n", " return float(value)\n", " except (ValueError, TypeError):\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender to binary (0 for female, 1 for male)\"\"\"\n", " if value is None or isinstance(value, float) and np.isnan(value):\n", " return None\n", " \n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip().lower()\n", " \n", " if value in [\"male\", \"m\"]:\n", " return 1\n", " elif value in [\"female\", \"f\"]:\n", " return 0\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Trait data is available since 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", "if trait_row is not None:\n", " # Clinical data seems to be available\n", " clinical_df = geo_select_clinical_features(\n", " clinical_df=clinical_data, # Assuming clinical_data is available from previous steps\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 clinical data\n", " print(\"Preview of clinical data:\")\n", " print(preview_df(clinical_df))\n", " \n", " # Save the clinical data to CSV\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " clinical_df.to_csv(out_clinical_data_file, index=False)\n" ] }, { "cell_type": "markdown", "id": "314bf17f", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "4465322a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:30:50.662314Z", "iopub.status.busy": "2025-03-25T07:30:50.662182Z", "iopub.status.idle": "2025-03-25T07:30:51.287280Z", "shell.execute_reply": "2025-03-25T07:30:51.286882Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix file found: ../../input/GEO/Liver_Cancer/GSE228783/GSE228783_series_matrix.txt.gz\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape: (49386, 152)\n", "First 20 gene/probe identifiers:\n", "Index(['11715100_at', '11715101_s_at', '11715102_x_at', '11715103_x_at',\n", " '11715104_s_at', '11715105_at', '11715106_x_at', '11715107_s_at',\n", " '11715108_x_at', '11715109_at', '11715110_at', '11715111_s_at',\n", " '11715112_at', '11715113_x_at', '11715114_x_at', '11715115_s_at',\n", " '11715116_s_at', '11715117_x_at', '11715118_s_at', '11715119_s_at'],\n", " dtype='object', name='ID')\n" ] } ], "source": [ "# 1. Get the SOFT and matrix file paths again \n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "print(f\"Matrix file found: {matrix_file}\")\n", "\n", "# 2. Use the get_genetic_data function from the library to get the gene_data\n", "try:\n", " gene_data = get_genetic_data(matrix_file)\n", " print(f\"Gene data shape: {gene_data.shape}\")\n", " \n", " # 3. Print the first 20 row IDs (gene or probe identifiers)\n", " print(\"First 20 gene/probe identifiers:\")\n", " print(gene_data.index[:20])\n", "except Exception as e:\n", " print(f\"Error extracting gene data: {e}\")\n" ] }, { "cell_type": "markdown", "id": "083289ce", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "16e7c9d3", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:30:51.288613Z", "iopub.status.busy": "2025-03-25T07:30:51.288488Z", "iopub.status.idle": "2025-03-25T07:30:51.290425Z", "shell.execute_reply": "2025-03-25T07:30:51.290134Z" } }, "outputs": [], "source": [ "# Review the gene identifiers\n", "# The identifiers like '11715100_at', '11715101_s_at' are probe IDs from microarray platforms,\n", "# not standard human gene symbols. These need to be mapped to gene symbols.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "e1b542a7", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "c5d9bf77", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:30:51.291577Z", "iopub.status.busy": "2025-03-25T07:30:51.291470Z", "iopub.status.idle": "2025-03-25T07:31:08.159211Z", "shell.execute_reply": "2025-03-25T07:31:08.158510Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene annotation preview:\n", "Columns in gene annotation: ['ID', 'GeneChip Array', 'Species Scientific Name', 'Annotation Date', 'Sequence Type', 'Sequence Source', 'Transcript ID(Array Design)', 'Target Description', 'Representative Public ID', 'Archival UniGene Cluster', 'UniGene ID', 'Genome Version', 'Alignments', 'Gene Title', 'Gene Symbol', 'Chromosomal Location', 'GB_LIST', 'SPOT_ID', 'Unigene Cluster Type', 'Ensembl', 'Entrez Gene', 'SwissProt', 'EC', 'OMIM', 'RefSeq Protein ID', 'RefSeq Transcript ID', 'FlyBase', 'AGI', 'WormBase', 'MGI Name', 'RGD Name', 'SGD accession number', 'Gene Ontology Biological Process', 'Gene Ontology Cellular Component', 'Gene Ontology Molecular Function', 'Pathway', 'InterPro', 'Trans Membrane', 'QTL', 'Annotation Description', 'Annotation Transcript Cluster', 'Transcript Assignments', 'Annotation Notes']\n", "{'ID': ['11715100_at', '11715101_s_at', '11715102_x_at', '11715103_x_at', '11715104_s_at'], 'GeneChip Array': ['Human Genome HG-U219 Array', 'Human Genome HG-U219 Array', 'Human Genome HG-U219 Array', 'Human Genome HG-U219 Array', 'Human Genome HG-U219 Array'], 'Species Scientific Name': ['Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens'], 'Annotation Date': ['20-Aug-10', '20-Aug-10', '20-Aug-10', '20-Aug-10', '20-Aug-10'], 'Sequence Type': ['Consensus sequence', 'Consensus sequence', 'Consensus sequence', 'Consensus sequence', 'Consensus sequence'], 'Sequence Source': ['Affymetrix Proprietary Database', 'Affymetrix Proprietary Database', 'Affymetrix Proprietary Database', 'Affymetrix Proprietary Database', 'Affymetrix Proprietary Database'], 'Transcript ID(Array Design)': ['g21264570', 'g21264570', 'g21264570', 'g22748780', 'g30039713'], 'Target Description': ['g21264570 /TID=g21264570 /CNT=1 /FEA=FLmRNA /TIER=FL /STK=0 /DEF=g21264570 /REP_ORG=Homo sapiens', 'g21264570 /TID=g21264570 /CNT=1 /FEA=FLmRNA /TIER=FL /STK=0 /DEF=g21264570 /REP_ORG=Homo sapiens', 'g21264570 /TID=g21264570 /CNT=1 /FEA=FLmRNA /TIER=FL /STK=0 /DEF=g21264570 /REP_ORG=Homo sapiens', 'g22748780 /TID=g22748780 /CNT=1 /FEA=FLmRNA /TIER=FL /STK=0 /DEF=g22748780 /REP_ORG=Homo sapiens', 'g30039713 /TID=g30039713 /CNT=1 /FEA=FLmRNA /TIER=FL /STK=0 /DEF=g30039713 /REP_ORG=Homo sapiens'], 'Representative Public ID': ['g21264570', 'g21264570', 'g21264570', 'g22748780', 'g30039713'], 'Archival UniGene Cluster': ['---', '---', '---', '---', '---'], 'UniGene ID': ['Hs.247813', 'Hs.247813', 'Hs.247813', 'Hs.465643', 'Hs.352515'], 'Genome Version': ['February 2009 (Genome Reference Consortium GRCh37)', 'February 2009 (Genome Reference Consortium GRCh37)', 'February 2009 (Genome Reference Consortium GRCh37)', 'February 2009 (Genome Reference Consortium GRCh37)', 'February 2009 (Genome Reference Consortium GRCh37)'], 'Alignments': ['chr6:26271145-26271612 (-) // 100.0 // p22.2', 'chr6:26271145-26271612 (-) // 100.0 // p22.2', 'chr6:26271145-26271612 (-) // 100.0 // p22.2', 'chr19:4639529-5145579 (+) // 48.53 // p13.3', 'chr17:72920369-72929640 (+) // 100.0 // q25.1'], 'Gene Title': ['histone cluster 1, H3g', 'histone cluster 1, H3g', 'histone cluster 1, H3g', 'tumor necrosis factor, alpha-induced protein 8-like 1', 'otopetrin 2'], 'Gene Symbol': ['HIST1H3G', 'HIST1H3G', 'HIST1H3G', 'TNFAIP8L1', 'OTOP2'], 'Chromosomal Location': ['chr6p21.3', 'chr6p21.3', 'chr6p21.3', 'chr19p13.3', 'chr17q25.1'], 'GB_LIST': ['NM_003534', 'NM_003534', 'NM_003534', 'NM_001167942,NM_152362', 'NM_178160'], 'SPOT_ID': [nan, nan, nan, nan, nan], 'Unigene Cluster Type': ['full length', 'full length', 'full length', 'full length', 'full length'], 'Ensembl': ['---', 'ENSG00000178458', '---', 'ENSG00000185361', 'ENSG00000183034'], 'Entrez Gene': ['8355', '8355', '8355', '126282', '92736'], 'SwissProt': ['P68431', 'P68431', 'P68431', 'Q8WVP5', 'Q7RTS6'], 'EC': ['---', '---', '---', '---', '---'], 'OMIM': ['602815', '602815', '602815', '---', '607827'], 'RefSeq Protein ID': ['NP_003525', 'NP_003525', 'NP_003525', 'NP_001161414 /// NP_689575', 'NP_835454'], 'RefSeq Transcript ID': ['NM_003534', 'NM_003534', 'NM_003534', 'NM_001167942 /// NM_152362', 'NM_178160'], 'FlyBase': ['---', '---', '---', '---', '---'], 'AGI': ['---', '---', '---', '---', '---'], 'WormBase': ['---', '---', '---', '---', '---'], 'MGI Name': ['---', '---', '---', '---', '---'], 'RGD Name': ['---', '---', '---', '---', '---'], 'SGD accession number': ['---', '---', '---', '---', '---'], 'Gene Ontology Biological Process': ['0006334 // nucleosome assembly // inferred from electronic annotation', '0006334 // nucleosome assembly // inferred from electronic annotation', '0006334 // nucleosome assembly // inferred from electronic annotation', '---', '---'], 'Gene Ontology Cellular Component': ['0000786 // nucleosome // inferred from electronic annotation /// 0005634 // nucleus // inferred from electronic annotation /// 0005694 // chromosome // inferred from electronic annotation', '0000786 // nucleosome // inferred from electronic annotation /// 0005634 // nucleus // inferred from electronic annotation /// 0005694 // chromosome // inferred from electronic annotation', '0000786 // nucleosome // inferred from electronic annotation /// 0005634 // nucleus // inferred from electronic annotation /// 0005694 // chromosome // inferred from electronic annotation', '---', '0016020 // membrane // inferred from electronic annotation /// 0016021 // integral to membrane // inferred from electronic annotation'], 'Gene Ontology Molecular Function': ['0003677 // DNA binding // inferred from electronic annotation /// 0005515 // protein binding // inferred from physical interaction', '0003677 // DNA binding // inferred from electronic annotation /// 0005515 // protein binding // inferred from physical interaction', '0003677 // DNA binding // inferred from electronic annotation /// 0005515 // protein binding // inferred from physical interaction', '---', '---'], 'Pathway': ['---', '---', '---', '---', '---'], 'InterPro': ['---', '---', '---', '---', 'IPR004878 // Protein of unknown function DUF270 // 1.0E-6 /// IPR004878 // Protein of unknown function DUF270 // 1.0E-13'], 'Trans Membrane': ['---', '---', '---', '---', 'NP_835454.1 // span:30-52,62-81,101-120,135-157,240-262,288-310,327-349,369-391,496-515,525-547 // numtm:10'], 'QTL': ['---', '---', '---', '---', '---'], 'Annotation Description': ['This probe set was annotated using the Matching Probes based pipeline to a Entrez Gene identifier using 1 transcripts. // false // Matching Probes // A', 'This probe set was annotated using the Matching Probes based pipeline to a Entrez Gene identifier using 2 transcripts. // false // Matching Probes // A', 'This probe set was annotated using the Matching Probes based pipeline to a Entrez Gene identifier using 1 transcripts. // false // Matching Probes // A', 'This probe set was annotated using the Matching Probes based pipeline to a Entrez Gene identifier using 5 transcripts. // false // Matching Probes // A', 'This probe set was annotated using the Matching Probes based pipeline to a Entrez Gene identifier using 3 transcripts. // false // Matching Probes // A'], 'Annotation Transcript Cluster': ['NM_003534(11)', 'BC079835(11),NM_003534(11)', 'NM_003534(11)', 'BC017672(11),BC044250(9),ENST00000327473(11),NM_001167942(11),NM_152362(11)', 'ENST00000331427(11),ENST00000426069(11),NM_178160(11)'], 'Transcript Assignments': ['NM_003534 // Homo sapiens histone cluster 1, H3g (HIST1H3G), mRNA. // refseq // 11 // ---', 'BC079835 // Homo sapiens histone cluster 1, H3g, mRNA (cDNA clone IMAGE:5935692). // gb_htc // 11 // --- /// ENST00000321285 // cdna:known chromosome:GRCh37:6:26271202:26271612:-1 gene:ENSG00000178458 // ensembl // 11 // --- /// GENSCAN00000044911 // cdna:Genscan chromosome:GRCh37:6:26271202:26271612:-1 // ensembl // 11 // --- /// NM_003534 // Homo sapiens histone cluster 1, H3g (HIST1H3G), mRNA. // refseq // 11 // ---', 'NM_003534 // Homo sapiens histone cluster 1, H3g (HIST1H3G), mRNA. // refseq // 11 // ---', 'BC017672 // Homo sapiens tumor necrosis factor, alpha-induced protein 8-like 1, mRNA (cDNA clone MGC:17791 IMAGE:3885999), complete cds. // gb // 11 // --- /// BC044250 // Homo sapiens tumor necrosis factor, alpha-induced protein 8-like 1, mRNA (cDNA clone IMAGE:5784807). // gb // 9 // --- /// ENST00000327473 // cdna:known chromosome:GRCh37:19:4639530:4653952:1 gene:ENSG00000185361 // ensembl // 11 // --- /// NM_001167942 // Homo sapiens tumor necrosis factor, alpha-induced protein 8-like 1 (TNFAIP8L1), transcript variant 1, mRNA. // refseq // 11 // --- /// NM_152362 // Homo sapiens tumor necrosis factor, alpha-induced protein 8-like 1 (TNFAIP8L1), transcript variant 2, mRNA. // refseq // 11 // ---', 'ENST00000331427 // cdna:known chromosome:GRCh37:17:72920370:72929640:1 gene:ENSG00000183034 // ensembl // 11 // --- /// ENST00000426069 // cdna:known chromosome:GRCh37:17:72920370:72929640:1 gene:ENSG00000183034 // ensembl // 11 // --- /// NM_178160 // Homo sapiens otopetrin 2 (OTOP2), mRNA. // refseq // 11 // ---'], 'Annotation Notes': ['BC079835 // gb_htc // 6 // Cross Hyb Matching Probes', '---', 'GENSCAN00000044911 // ensembl // 4 // Cross Hyb Matching Probes /// ENST00000321285 // ensembl // 4 // Cross Hyb Matching Probes /// BC079835 // gb_htc // 7 // Cross Hyb Matching Probes', '---', 'GENSCAN00000031612 // ensembl // 8 // Cross Hyb Matching Probes']}\n", "\n", "Examining potential gene mapping columns:\n" ] } ], "source": [ "# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "gene_annotation = get_gene_annotation(soft_file)\n", "\n", "# 2. Analyze the gene annotation dataframe to identify which columns contain the gene identifiers and gene symbols\n", "print(\"\\nGene annotation preview:\")\n", "print(f\"Columns in gene annotation: {gene_annotation.columns.tolist()}\")\n", "print(preview_df(gene_annotation, n=5))\n", "\n", "# Look more closely at columns that might contain gene information\n", "print(\"\\nExamining potential gene mapping columns:\")\n", "potential_gene_columns = ['gene_assignment', 'mrna_assignment', 'swissprot', 'unigene']\n", "for col in potential_gene_columns:\n", " if col in gene_annotation.columns:\n", " print(f\"\\nSample values from '{col}' column:\")\n", " print(gene_annotation[col].head(3).tolist())\n" ] }, { "cell_type": "markdown", "id": "56371b9e", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "b66a388b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:31:08.161138Z", "iopub.status.busy": "2025-03-25T07:31:08.161017Z", "iopub.status.idle": "2025-03-25T07:31:08.767565Z", "shell.execute_reply": "2025-03-25T07:31:08.767024Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene mapping dataframe created with shape: (49384, 2)\n", "Sample of gene mapping data:\n", " ID Gene\n", "0 11715100_at HIST1H3G\n", "1 11715101_s_at HIST1H3G\n", "2 11715102_x_at HIST1H3G\n", "3 11715103_x_at TNFAIP8L1\n", "4 11715104_s_at OTOP2\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data created with shape: (19521, 152)\n", "First 5 gene symbols after mapping:\n", "Index(['A1BG', 'A1CF', 'A2BP1', 'A2LD1', 'A2M'], dtype='object', name='Gene')\n" ] } ], "source": [ "# 1. Identify the columns for gene IDs and gene symbols in the annotation dataframe\n", "# From the gene annotation preview, we can see that:\n", "# - 'ID' column contains identifiers that match the gene expression data (e.g. 11715100_at)\n", "# - 'Gene Symbol' column contains the human gene symbols (e.g. HIST1H3G)\n", "\n", "# 2. Create a gene mapping dataframe with these two columns\n", "gene_mapping = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Gene Symbol')\n", "print(\"Gene mapping dataframe created with shape:\", gene_mapping.shape)\n", "print(\"Sample of gene mapping data:\")\n", "print(gene_mapping.head())\n", "\n", "# 3. Apply gene mapping to convert probe-level measurements to gene-level expression\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "print(\"Gene expression data created with shape:\", gene_data.shape)\n", "print(\"First 5 gene symbols after mapping:\")\n", "print(gene_data.index[:5])\n" ] }, { "cell_type": "markdown", "id": "dc5c7c68", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "671af442", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:31:08.769558Z", "iopub.status.busy": "2025-03-25T07:31:08.769443Z", "iopub.status.idle": "2025-03-25T07:31:26.704028Z", "shell.execute_reply": "2025-03-25T07:31:26.703569Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Normalizing gene symbols...\n", "Gene data shape after normalization: (19298, 152)\n", "First 10 gene identifiers after normalization:\n", "['A1BG', 'A1CF', 'A2M', 'A2ML1', 'A3GALT2', 'A4GALT', 'A4GNT', 'AAA1', 'AAAS', 'AACS']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Liver_Cancer/gene_data/GSE228783.csv\n", "\n", "Linking clinical and genetic data...\n", "Linked data shape: (152, 19299)\n", "Linked data preview:\n", " Liver_Cancer A1BG A1CF A2M A2ML1\n", "GSM7136321 0.0 12.33124 20.535768 12.32219 4.097415\n", "GSM7136322 0.0 12.40018 19.749593 12.34718 4.237847\n", "GSM7136323 0.0 12.22078 19.805032 12.32856 4.280063\n", "GSM7136324 0.0 12.31479 19.829038 12.21530 4.366853\n", "GSM7136325 0.0 12.32009 19.083230 12.43024 4.634051\n", "\n", "Handling missing values...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data shape after handling missing values: (152, 19299)\n", "\n", "Checking for bias in dataset features...\n", "For the feature 'Liver_Cancer', the least common label is '1.0' with 12 occurrences. This represents 7.89% of the dataset.\n", "The distribution of the feature 'Liver_Cancer' in this dataset is fine.\n", "\n", "\n", "Conducting final quality validation...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Liver_Cancer/GSE228783.csv\n" ] } ], "source": [ "# 1. Normalize gene symbols in the index\n", "print(\"\\nNormalizing gene symbols...\")\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Gene data shape after normalization: {normalized_gene_data.shape}\")\n", "print(\"First 10 gene identifiers after normalization:\")\n", "print(normalized_gene_data.index[:10].tolist())\n", "\n", "# Save the normalized gene data to CSV\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "normalized_gene_data.to_csv(out_gene_data_file)\n", "print(f\"Normalized gene data saved to {out_gene_data_file}\")\n", "\n", "# 2. Link clinical and genetic data\n", "print(\"\\nLinking clinical and genetic data...\")\n", "linked_data = geo_link_clinical_genetic_data(clinical_df, normalized_gene_data)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "print(\"Linked data preview:\")\n", "print(linked_data.iloc[:5, :5])\n", "\n", "# Check and rename the trait column if needed\n", "if trait not in linked_data.columns and '0.0' in linked_data.columns:\n", " # Rename the column to the expected trait name\n", " linked_data = linked_data.rename(columns={'0.0': trait})\n", " print(f\"Renamed column '0.0' to '{trait}'\")\n", "\n", "# Verify the trait column exists\n", "if trait not in linked_data.columns:\n", " print(f\"Error: Trait column '{trait}' not found in linked data. Available columns: {linked_data.columns[:10].tolist()}\")\n", " is_usable = False\n", "else:\n", " # 3. Handle missing values\n", " print(\"\\nHandling missing values...\")\n", " linked_data_clean = handle_missing_values(linked_data, trait)\n", " print(f\"Linked data shape after handling missing values: {linked_data_clean.shape}\")\n", "\n", " # 4. Check for bias in the dataset\n", " print(\"\\nChecking for bias in dataset features...\")\n", " trait_biased, linked_data_clean = judge_and_remove_biased_features(linked_data_clean, trait)\n", "\n", " # 5. Conduct final quality validation and save metadata\n", " print(\"\\nConducting final quality validation...\")\n", " note = \"This dataset contains liver tissue gene expression data with samples categorized as HCC (hepatocellular carcinoma) or other conditions (CRC Met, CCC, other).\"\n", " is_gene_available = len(normalized_gene_data) > 0\n", " is_trait_available = trait in linked_data_clean.columns\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=trait_biased,\n", " df=linked_data_clean,\n", " note=note\n", " )\n", "\n", " # 6. Save the linked data if it's usable\n", " if is_usable:\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " linked_data_clean.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", " else:\n", " print(\"Dataset deemed not usable for associative studies. Linked data not saved.\")" ] } ], "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 }