{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "edc988e0", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:28:50.167420Z", "iopub.status.busy": "2025-03-25T07:28:50.167066Z", "iopub.status.idle": "2025-03-25T07:28:50.332538Z", "shell.execute_reply": "2025-03-25T07:28:50.332192Z" } }, "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 = \"GSE164760\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Liver_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Liver_Cancer/GSE164760\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Liver_Cancer/GSE164760.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Liver_Cancer/gene_data/GSE164760.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Liver_Cancer/clinical_data/GSE164760.csv\"\n", "json_path = \"../../output/preprocess/Liver_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "1a88094b", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "7c885aa9", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:28:50.334009Z", "iopub.status.busy": "2025-03-25T07:28:50.333861Z", "iopub.status.idle": "2025-03-25T07:28:50.820785Z", "shell.execute_reply": "2025-03-25T07:28:50.820451Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Molecular characterization of hepatocellular carcinoma in patients with non-alcoholic steatohepatitis\"\n", "!Series_summary\t\"Non-alcoholic steatohepatitis (NASH)-related hepatocellular carcinoma (HCC) is increasing globally, but its molecular features are not well defined. We aimed to identify unique molecular traits characterizing NASH-HCC compared to other HCC aetiologies.\"\n", "!Series_summary\t\"We collected 80 NASH-HCC and 125 NASH samples from 5 institutions. Expression array (n=53 NASH-HCC; n=74 NASH) and whole exome sequencing (n=50 NASH-HCC) data were compared to HCCs of other aetiologies (n=184). Three NASH-HCC mouse models were analysed with RNAseq/expression-array (n=20). ACVR2A was silenced in HCC cells and proliferation assessed by MTT and colony formation assays.\"\n", "!Series_summary\t\"Mutational profiling of NASH-HCC tumours revealed TERT-promoter (56%), CTNNB1 (28%), TP53 (18%) and ACVR2A (10%) as the most-frequently mutated genes. ACVR2A mutation rates were higher in NASH-HCC than in other HCC aetiologies (10% versus 3%, p<0.05). In vitro, ACVR2A silencing prompted a significant increase in cell proliferation in HCC cells. We identified a novel mutational signature (MutSig-NASH-HCC) significantly associated with NASH-HCC (16% vs 2% in viral/alcohol-HCC, p=0.03). Tumour mutational burden (TMB) was higher in non-cirrhotic than in cirrhotic NASH-HCCs (1.45 versus 0.94 mutations/Mb; p<0.0017). Compared to other aetiologies of HCC, NASH-HCCs were enriched in bile and fatty acid signalling, oxidative stress and inflammation, and presented a higher fraction of Wnt/TGF-β proliferation subclass tumours (42% versus 26%, p=0.01) and a lower prevalence of the CTNNB1 subclass. Compared to other aetiologies, NASH-HCC showed a significantly higher prevalence of immunosuppressive cancer field. In three murine models of NASH-HCC key features of human NASH-HCC were preserved.\"\n", "!Series_summary\t\"NASH-HCCs display unique molecular features including higher rates of ACVR2A mutations and the presence of a newly identified mutational signature.\"\n", "!Series_overall_design\t\"53 NASH-HCCs, 29 adjacent non-tumor NASH liver samples and 74 NASH samples were analysed, as well as 6 healthy livers and 8 cirrhotic livers as a control.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['tissue: Healthy liver', 'tissue: Cirrhotic liver', 'tissue: NASH liver', 'tissue: Non-tumoral NASH liver adjacent to HCC', 'tissue: NASH-HCC tumor']}\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": "55a32d8f", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "f5c4e0ba", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:28:50.822083Z", "iopub.status.busy": "2025-03-25T07:28:50.821961Z", "iopub.status.idle": "2025-03-25T07:28:50.834947Z", "shell.execute_reply": "2025-03-25T07:28:50.834636Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of selected clinical features:\n", "{'GSM5018268': [0.0], 'GSM5018269': [0.0], 'GSM5018270': [0.0], 'GSM5018271': [0.0], 'GSM5018272': [0.0], 'GSM5018273': [0.0], 'GSM5018274': [0.0], 'GSM5018275': [0.0], 'GSM5018276': [0.0], 'GSM5018277': [0.0], 'GSM5018278': [0.0], 'GSM5018279': [0.0], 'GSM5018280': [0.0], 'GSM5018281': [0.0], 'GSM5018282': [0.0], 'GSM5018283': [0.0], 'GSM5018284': [0.0], 'GSM5018285': [0.0], 'GSM5018286': [0.0], 'GSM5018287': [0.0], 'GSM5018288': [0.0], 'GSM5018289': [0.0], 'GSM5018290': [0.0], 'GSM5018291': [0.0], 'GSM5018292': [0.0], 'GSM5018293': [0.0], 'GSM5018294': [0.0], 'GSM5018295': [0.0], 'GSM5018296': [0.0], 'GSM5018297': [0.0], 'GSM5018298': [0.0], 'GSM5018299': [0.0], 'GSM5018300': [0.0], 'GSM5018301': [0.0], 'GSM5018302': [0.0], 'GSM5018303': [0.0], 'GSM5018304': [0.0], 'GSM5018305': [0.0], 'GSM5018306': [0.0], 'GSM5018307': [0.0], 'GSM5018308': [0.0], 'GSM5018309': [0.0], 'GSM5018310': [0.0], 'GSM5018311': [0.0], 'GSM5018312': [0.0], 'GSM5018313': [0.0], 'GSM5018314': [0.0], 'GSM5018315': [0.0], 'GSM5018316': [0.0], 'GSM5018317': [0.0], 'GSM5018318': [0.0], 'GSM5018319': [0.0], 'GSM5018320': [0.0], 'GSM5018321': [0.0], 'GSM5018322': [0.0], 'GSM5018323': [0.0], 'GSM5018324': [0.0], 'GSM5018325': [0.0], 'GSM5018326': [0.0], 'GSM5018327': [0.0], 'GSM5018328': [0.0], 'GSM5018329': [0.0], 'GSM5018330': [0.0], 'GSM5018331': [0.0], 'GSM5018332': [0.0], 'GSM5018333': [0.0], 'GSM5018334': [0.0], 'GSM5018335': [0.0], 'GSM5018336': [0.0], 'GSM5018337': [0.0], 'GSM5018338': [0.0], 'GSM5018339': [0.0], 'GSM5018340': [0.0], 'GSM5018341': [0.0], 'GSM5018342': [0.0], 'GSM5018343': [0.0], 'GSM5018344': [0.0], 'GSM5018345': [0.0], 'GSM5018346': [0.0], 'GSM5018347': [0.0], 'GSM5018348': [0.0], 'GSM5018349': [0.0], 'GSM5018350': [0.0], 'GSM5018351': [0.0], 'GSM5018352': [0.0], 'GSM5018353': [0.0], 'GSM5018354': [0.0], 'GSM5018355': [0.0], 'GSM5018356': [0.0], 'GSM5018357': [0.0], 'GSM5018358': [0.0], 'GSM5018359': [0.0], 'GSM5018360': [0.0], 'GSM5018361': [0.0], 'GSM5018362': [0.0], 'GSM5018363': [0.0], 'GSM5018364': [0.0], 'GSM5018365': [0.0], 'GSM5018366': [0.0], 'GSM5018367': [0.0], 'GSM5018368': [0.0], 'GSM5018369': [0.0], 'GSM5018370': [0.0], 'GSM5018371': [0.0], 'GSM5018372': [0.0], 'GSM5018373': [0.0], 'GSM5018374': [0.0], 'GSM5018375': [0.0], 'GSM5018376': [0.0], 'GSM5018377': [0.0], 'GSM5018378': [0.0], 'GSM5018379': [0.0], 'GSM5018380': [0.0], 'GSM5018381': [0.0], 'GSM5018382': [0.0], 'GSM5018383': [0.0], 'GSM5018384': [0.0], 'GSM5018385': [1.0], 'GSM5018386': [1.0], 'GSM5018387': [1.0], 'GSM5018388': [1.0], 'GSM5018389': [1.0], 'GSM5018390': [1.0], 'GSM5018391': [1.0], 'GSM5018392': [1.0], 'GSM5018393': [1.0], 'GSM5018394': [1.0], 'GSM5018395': [1.0], 'GSM5018396': [1.0], 'GSM5018397': [1.0], 'GSM5018398': [1.0], 'GSM5018399': [1.0], 'GSM5018400': [1.0], 'GSM5018401': [1.0], 'GSM5018402': [1.0], 'GSM5018403': [1.0], 'GSM5018404': [1.0], 'GSM5018405': [1.0], 'GSM5018406': [1.0], 'GSM5018407': [1.0], 'GSM5018408': [1.0], 'GSM5018409': [1.0], 'GSM5018410': [1.0], 'GSM5018411': [1.0], 'GSM5018412': [1.0], 'GSM5018413': [1.0], 'GSM5018414': [1.0], 'GSM5018415': [1.0], 'GSM5018416': [1.0], 'GSM5018417': [1.0], 'GSM5018418': [1.0], 'GSM5018419': [1.0], 'GSM5018420': [1.0], 'GSM5018421': [1.0], 'GSM5018422': [1.0], 'GSM5018423': [1.0], 'GSM5018424': [1.0], 'GSM5018425': [1.0], 'GSM5018426': [1.0], 'GSM5018427': [1.0], 'GSM5018428': [1.0], 'GSM5018429': [1.0], 'GSM5018430': [1.0], 'GSM5018431': [1.0], 'GSM5018432': [1.0], 'GSM5018433': [1.0], 'GSM5018434': [1.0], 'GSM5018435': [1.0], 'GSM5018436': [1.0], 'GSM5018437': [1.0]}\n", "Clinical data saved to ../../output/preprocess/Liver_Cancer/clinical_data/GSE164760.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# This appears to be a gene expression dataset studying NASH-related HCC\n", "# The Series_summary mentions \"Expression array\" and \"whole exome sequencing\"\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# Looking at the sample characteristics dictionary\n", "\n", "# 2.1 Trait (Liver Cancer)\n", "# From the characteristics dictionary, we can identify tissue type in row 0\n", "# This can be used to determine liver cancer status\n", "trait_row = 0\n", "\n", "def convert_trait(value):\n", " if value is None:\n", " return None\n", " \n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Based on the unique values, map to binary (1 for HCC tumor, 0 for non-tumor)\n", " if \"NASH-HCC tumor\" in value:\n", " return 1\n", " elif any(tissue_type in value for tissue_type in [\"Healthy liver\", \"Cirrhotic liver\", \"NASH liver\", \"Non-tumoral NASH liver\"]):\n", " return 0\n", " return None\n", "\n", "# 2.2 Age data\n", "# There is no age information available in the characteristics dictionary\n", "age_row = None\n", "\n", "def convert_age(value):\n", " # Since age data is not available, this function won't be used\n", " # But we define it for completeness\n", " if value is None:\n", " return None\n", " \n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " try:\n", " return float(value)\n", " except:\n", " return None\n", "\n", "# 2.3 Gender data\n", "# There is no gender information available in the characteristics dictionary\n", "gender_row = None\n", "\n", "def convert_gender(value):\n", " # Since gender data is not available, this function won't be used\n", " # But we define it for completeness\n", " if value is None:\n", " return None\n", " \n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip().lower()\n", " \n", " if value in ['female', 'f']:\n", " return 0\n", " elif value in ['male', 'm']:\n", " return 1\n", " return None\n", "\n", "# 3. Save Metadata\n", "# 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", "if trait_row is not None:\n", " # We have the trait data available, so we can extract clinical features\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 extracted clinical features\n", " preview = preview_df(selected_clinical_df)\n", " print(\"Preview of selected clinical features:\")\n", " print(preview)\n", " \n", " # Save the clinical data to CSV\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": "86e16b9d", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "bda79f93", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:28:50.836088Z", "iopub.status.busy": "2025-03-25T07:28:50.835972Z", "iopub.status.idle": "2025-03-25T07:28:52.838330Z", "shell.execute_reply": "2025-03-25T07:28:52.837985Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix file found: ../../input/GEO/Liver_Cancer/GSE164760/GSE164760_family.soft.gz\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Error extracting gene data: Marker '!series_matrix_table_begin' not found in the file.\n" ] } ], "source": [ "# Check if the dataset contains gene expression data based on previous assessment\n", "if not is_gene_available:\n", " print(\"This dataset does not contain gene expression data (only miRNA data).\")\n", " print(\"Skipping gene expression data extraction.\")\n", "else:\n", " # Get the matrix file directly rather than using geo_get_relevant_filepaths\n", " files = os.listdir(in_cohort_dir)\n", " if len(files) > 0:\n", " matrix_file = os.path.join(in_cohort_dir, files[0])\n", " print(f\"Matrix file found: {matrix_file}\")\n", " \n", " try:\n", " # Extract gene data\n", " gene_data = get_genetic_data(matrix_file)\n", " print(f\"Gene data shape: {gene_data.shape}\")\n", " \n", " # Print the first 20 gene/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", " else:\n", " print(\"No files found in the input directory.\")\n" ] }, { "cell_type": "markdown", "id": "c05a69f5", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "2879d092", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:28:52.839611Z", "iopub.status.busy": "2025-03-25T07:28:52.839496Z", "iopub.status.idle": "2025-03-25T07:28:52.841409Z", "shell.execute_reply": "2025-03-25T07:28:52.841125Z" } }, "outputs": [], "source": [ "# Observing the gene identifiers in the gene expression data\n", "# These identifiers (e.g., '11715100_at', '11715101_s_at') are not human gene symbols\n", "# They appear to be probe IDs from a microarray platform, likely Affymetrix\n", "# These will need to be mapped to standard human gene symbols for analysis\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "52e5b9a4", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "b35c7bee", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:28:52.842448Z", "iopub.status.busy": "2025-03-25T07:28:52.842338Z", "iopub.status.idle": "2025-03-25T07:29:14.023635Z", "shell.execute_reply": "2025-03-25T07:29:14.023074Z" } }, "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": "2c50c774", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "2904eb47", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:29:14.025193Z", "iopub.status.busy": "2025-03-25T07:29:14.025077Z", "iopub.status.idle": "2025-03-25T07:29:17.716661Z", "shell.execute_reply": "2025-03-25T07:29:17.716024Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mapping probes from 'ID' to genes in 'Gene Symbol'\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene mapping dataframe shape: (49384, 2)\n", "Sample of gene mapping:\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": [ "Genetic data shape before mapping: (49386, 170)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape after mapping: (19521, 170)\n", "Sample of gene expression data:\n", " GSM5018268 GSM5018269 GSM5018270 GSM5018271 GSM5018272\n", "Gene \n", "A1BG 26.193085 27.247516 22.545718 23.498937 22.978236\n", "A1CF 221.676246 219.701775 89.646004 143.134513 150.538559\n", "A2BP1 206.435882 244.144349 262.732617 274.779167 191.588350\n", "A2LD1 14.832444 18.875513 19.204097 18.426530 14.465543\n", "A2M 809.682278 238.468518 291.242092 635.645959 908.507654\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Liver_Cancer/gene_data/GSE164760.csv\n" ] } ], "source": [ "# 1. Identify the relevant columns for mapping probe IDs to gene symbols\n", "# Based on the annotation preview, we need to map 'ID' to 'Gene Symbol'\n", "probe_id_col = 'ID' # Column containing the probe identifiers\n", "gene_symbol_col = 'Gene Symbol' # Column containing the gene symbols\n", "\n", "print(f\"Mapping probes from '{probe_id_col}' to genes in '{gene_symbol_col}'\")\n", "\n", "# 2. Get the mapping dataframe using the identified columns\n", "gene_mapping = get_gene_mapping(gene_annotation, prob_col=probe_id_col, gene_col=gene_symbol_col)\n", "print(f\"Gene mapping dataframe shape: {gene_mapping.shape}\")\n", "print(\"Sample of gene mapping:\")\n", "print(gene_mapping.head())\n", "\n", "# 3. Apply the gene mapping to convert probe-level measurements to gene expression data\n", "# Reconfirm the genetic data is extracted correctly\n", "genetic_data = get_genetic_data(matrix_file)\n", "print(f\"Genetic data shape before mapping: {genetic_data.shape}\")\n", "\n", "# Apply the mapping to convert probe-level to gene-level expression\n", "gene_data = apply_gene_mapping(genetic_data, gene_mapping)\n", "print(f\"Gene data shape after mapping: {gene_data.shape}\")\n", "print(\"Sample of gene expression data:\")\n", "print(gene_data.iloc[:5, :5])\n", "\n", "# Save the gene expression data\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": "4578046d", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "63734316", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:29:17.718519Z", "iopub.status.busy": "2025-03-25T07:29:17.718370Z", "iopub.status.idle": "2025-03-25T07:29:38.151958Z", "shell.execute_reply": "2025-03-25T07:29:38.151558Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loaded clinical data from ../../output/preprocess/Liver_Cancer/clinical_data/GSE164760.csv\n", "Clinical data shape: (1, 170)\n", "Clinical data preview:\n", " GSM5018268 GSM5018269 GSM5018270 GSM5018271 GSM5018272 \\\n", "Liver_Cancer 0.0 0.0 0.0 0.0 0.0 \n", "\n", " GSM5018273 GSM5018274 GSM5018275 GSM5018276 GSM5018277 ... \\\n", "Liver_Cancer 0.0 0.0 0.0 0.0 0.0 ... \n", "\n", " GSM5018428 GSM5018429 GSM5018430 GSM5018431 GSM5018432 \\\n", "Liver_Cancer 1.0 1.0 1.0 1.0 1.0 \n", "\n", " GSM5018433 GSM5018434 GSM5018435 GSM5018436 GSM5018437 \n", "Liver_Cancer 1.0 1.0 1.0 1.0 1.0 \n", "\n", "[1 rows x 170 columns]\n", "\n", "Normalizing gene symbols...\n", "Gene data shape after normalization: (19298, 170)\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/GSE164760.csv\n", "\n", "Linking clinical and genetic data...\n", "Linked data shape: (170, 19299)\n", "Linked data preview (first 5 rows, first 5 columns):\n", " Liver_Cancer A1BG A1CF A2M A2ML1\n", "GSM5018268 0.0 26.193085 221.676246 809.682278 47.181279\n", "GSM5018269 0.0 27.247516 219.701775 238.468518 65.398284\n", "GSM5018270 0.0 22.545718 89.646004 291.242092 49.127122\n", "GSM5018271 0.0 23.498937 143.134513 635.645959 54.785169\n", "GSM5018272 0.0 22.978236 150.538559 908.507654 37.466176\n", "\n", "Handling missing values...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data shape after handling missing values: (170, 19299)\n", "\n", "Checking for bias in trait and demographic features...\n", "For the feature 'Liver_Cancer', the least common label is '1.0' with 53 occurrences. This represents 31.18% of the dataset.\n", "The distribution of the feature 'Liver_Cancer' in this dataset is fine.\n", "\n", "\n", "Conducting final quality validation...\n", "A new JSON file was created at: ../../output/preprocess/Liver_Cancer/cohort_info.json\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Liver_Cancer/GSE164760.csv\n" ] } ], "source": [ "# Load the clinical data from the file we saved in step 2\n", "clinical_data_file = out_clinical_data_file\n", "if os.path.exists(clinical_data_file):\n", " selected_clinical_df = pd.read_csv(clinical_data_file, index_col=0)\n", " print(f\"Loaded clinical data from {clinical_data_file}\")\n", " print(f\"Clinical data shape: {selected_clinical_df.shape}\")\n", " print(\"Clinical data preview:\")\n", " print(selected_clinical_df.head())\n", "else:\n", " print(f\"Clinical data file {clinical_data_file} not found. Re-extracting clinical features...\")\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", " print(\"Re-extracted clinical data preview:\")\n", " print(preview_df(selected_clinical_df))\n", "\n", "# 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 the clinical and genetic data\n", "print(\"\\nLinking clinical and genetic data...\")\n", "linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "print(\"Linked data preview (first 5 rows, first 5 columns):\")\n", "print(linked_data.iloc[:5, :5])\n", "\n", "# 3. Handle missing values in the linked data\n", "print(\"\\nHandling missing values...\")\n", "linked_data = handle_missing_values(linked_data, trait)\n", "print(f\"Linked data shape after handling missing values: {linked_data.shape}\")\n", "\n", "# 4. Determine if the trait and demographic features are biased\n", "print(\"\\nChecking for bias in trait and demographic features...\")\n", "is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "\n", "# 5. Conduct final quality validation and save relevant information\n", "print(\"\\nConducting final quality validation...\")\n", "is_gene_available = len(normalized_gene_data) > 0\n", "is_trait_available = True # We've confirmed trait data is available in previous steps\n", "\n", "note = \"This dataset contains gene expression data from NASH-HCC tumors vs. healthy/NASH/cirrhotic livers, appropriate for liver cancer analysis.\"\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. 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.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", "else:\n", " print(\"Linked data not saved as dataset is not usable for the current trait study.\")" ] } ], "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 }