{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "5297b80b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:35:31.648169Z", "iopub.status.busy": "2025-03-25T06:35:31.647967Z", "iopub.status.idle": "2025-03-25T06:35:31.813160Z", "shell.execute_reply": "2025-03-25T06:35:31.812706Z" } }, "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 = \"Arrhythmia\"\n", "cohort = \"GSE47727\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Arrhythmia\"\n", "in_cohort_dir = \"../../input/GEO/Arrhythmia/GSE47727\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Arrhythmia/GSE47727.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Arrhythmia/gene_data/GSE47727.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Arrhythmia/clinical_data/GSE47727.csv\"\n", "json_path = \"../../output/preprocess/Arrhythmia/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "0c7e3502", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "9cc057d9", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:35:31.814610Z", "iopub.status.busy": "2025-03-25T06:35:31.814467Z", "iopub.status.idle": "2025-03-25T06:35:32.142321Z", "shell.execute_reply": "2025-03-25T06:35:32.141794Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Global peripheral blood gene expression study [HumanHT-12 V3.0]\"\n", "!Series_summary\t\"Samples were collected from 'control participants' of the Heart and Vascular Health (HVH) study that constitutes a group of population based case control studies of myocardial infarction (MI), stroke, venous thromboembolism (VTE), and atrial fibrillation (AF) conducted among 30-79 year old members of Group Health, a large integrated health care organization in Washington State.\"\n", "!Series_overall_design\t\"Total RNA was isolated from peripheral collected using PAXgene tubes and gene expression was profiled using the Illumina platform.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['age (yrs): 67', 'age (yrs): 54', 'age (yrs): 73', 'age (yrs): 52', 'age (yrs): 75', 'age (yrs): 59', 'age (yrs): 74', 'age (yrs): 76', 'age (yrs): 58', 'age (yrs): 60', 'age (yrs): 66', 'age (yrs): 70', 'age (yrs): 78', 'age (yrs): 77', 'age (yrs): 72', 'age (yrs): 57', 'age (yrs): 63', 'age (yrs): 62', 'age (yrs): 64', 'age (yrs): 61', 'age (yrs): 69', 'age (yrs): 68', 'age (yrs): 82', 'age (yrs): 71', 'age (yrs): 56', 'age (yrs): 53', 'age (yrs): 49', 'age (yrs): 51', 'age (yrs): 79', 'age (yrs): 80'], 1: ['gender: male', 'gender: female'], 2: ['tissue: blood']}\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": "c25cd1b3", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "d4d58c3f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:35:32.143880Z", "iopub.status.busy": "2025-03-25T06:35:32.143769Z", "iopub.status.idle": "2025-03-25T06:35:32.159613Z", "shell.execute_reply": "2025-03-25T06:35:32.159159Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of clinical features:\n", "{'GSM1298251': [0.0, 67.0, 1.0], 'GSM1298252': [0.0, 54.0, 1.0], 'GSM1298253': [0.0, 73.0, 1.0], 'GSM1298254': [0.0, 52.0, 0.0], 'GSM1298255': [0.0, 75.0, 1.0], 'GSM1298256': [0.0, 59.0, 1.0], 'GSM1298257': [0.0, 74.0, 0.0], 'GSM1298258': [0.0, 75.0, 0.0], 'GSM1298259': [0.0, 74.0, 0.0], 'GSM1298260': [0.0, 76.0, 0.0], 'GSM1298261': [0.0, 73.0, 1.0], 'GSM1298262': [0.0, 67.0, 0.0], 'GSM1298263': [0.0, 58.0, 1.0], 'GSM1298264': [0.0, 60.0, 1.0], 'GSM1298265': [0.0, 66.0, 0.0], 'GSM1298266': [0.0, 70.0, 0.0], 'GSM1298267': [0.0, 75.0, 1.0], 'GSM1298268': [0.0, 70.0, 0.0], 'GSM1298269': [0.0, 78.0, 0.0], 'GSM1298270': [0.0, 77.0, 1.0], 'GSM1298271': [0.0, 72.0, 0.0], 'GSM1298272': [0.0, 78.0, 0.0], 'GSM1298273': [0.0, 57.0, 1.0], 'GSM1298274': [0.0, 77.0, 0.0], 'GSM1298275': [0.0, 63.0, 1.0], 'GSM1298276': [0.0, 62.0, 0.0], 'GSM1298277': [0.0, 52.0, 1.0], 'GSM1298278': [0.0, 74.0, 0.0], 'GSM1298279': [0.0, 59.0, 1.0], 'GSM1298280': [0.0, 64.0, 0.0], 'GSM1298281': [0.0, 60.0, 0.0], 'GSM1298282': [0.0, 60.0, 0.0], 'GSM1298283': [0.0, 63.0, 0.0], 'GSM1298284': [0.0, 67.0, 0.0], 'GSM1298285': [0.0, 61.0, 0.0], 'GSM1298286': [0.0, 69.0, 0.0], 'GSM1298287': [0.0, 61.0, 0.0], 'GSM1298288': [0.0, 69.0, 0.0], 'GSM1298289': [0.0, 60.0, 0.0], 'GSM1298290': [0.0, 62.0, 0.0], 'GSM1298291': [0.0, 66.0, 0.0], 'GSM1298292': [0.0, 60.0, 0.0], 'GSM1298293': [0.0, 63.0, 0.0], 'GSM1298294': [0.0, 77.0, 0.0], 'GSM1298295': [0.0, 78.0, 0.0], 'GSM1298296': [0.0, 78.0, 0.0], 'GSM1298297': [0.0, 76.0, 0.0], 'GSM1298298': [0.0, 69.0, 0.0], 'GSM1298299': [0.0, 68.0, 0.0], 'GSM1298300': [0.0, 70.0, 0.0], 'GSM1298301': [0.0, 72.0, 1.0], 'GSM1298302': [0.0, 68.0, 1.0], 'GSM1298303': [0.0, 75.0, 1.0], 'GSM1298304': [0.0, 76.0, 1.0], 'GSM1298305': [0.0, 72.0, 1.0], 'GSM1298306': [0.0, 72.0, 0.0], 'GSM1298307': [0.0, 73.0, 1.0], 'GSM1298308': [0.0, 67.0, 0.0], 'GSM1298309': [0.0, 62.0, 1.0], 'GSM1298310': [0.0, 76.0, 1.0], 'GSM1298311': [0.0, 82.0, 0.0], 'GSM1298312': [0.0, 76.0, 1.0], 'GSM1298313': [0.0, 73.0, 0.0], 'GSM1298314': [0.0, 75.0, 0.0], 'GSM1298315': [0.0, 78.0, 1.0], 'GSM1298316': [0.0, 57.0, 1.0], 'GSM1298317': [0.0, 77.0, 0.0], 'GSM1298318': [0.0, 60.0, 1.0], 'GSM1298319': [0.0, 75.0, 0.0], 'GSM1298320': [0.0, 75.0, 1.0], 'GSM1298321': [0.0, 77.0, 0.0], 'GSM1298322': [0.0, 72.0, 0.0], 'GSM1298323': [0.0, 73.0, 0.0], 'GSM1298324': [0.0, 72.0, 0.0], 'GSM1298325': [0.0, 74.0, 0.0], 'GSM1298326': [0.0, 78.0, 0.0], 'GSM1298327': [0.0, 71.0, 0.0], 'GSM1298328': [0.0, 70.0, 0.0], 'GSM1298329': [0.0, 76.0, 0.0], 'GSM1298330': [0.0, 74.0, 0.0], 'GSM1298331': [0.0, 76.0, 0.0], 'GSM1298332': [0.0, 71.0, 1.0], 'GSM1298333': [0.0, 61.0, 0.0], 'GSM1298334': [0.0, 63.0, 0.0], 'GSM1298335': [0.0, 68.0, 1.0], 'GSM1298336': [0.0, 67.0, 1.0], 'GSM1298337': [0.0, 64.0, 0.0], 'GSM1298338': [0.0, 56.0, 0.0], 'GSM1298339': [0.0, 52.0, 0.0], 'GSM1298340': [0.0, 72.0, 0.0], 'GSM1298341': [0.0, 73.0, 0.0], 'GSM1298342': [0.0, 53.0, 0.0], 'GSM1298343': [0.0, 63.0, 1.0], 'GSM1298344': [0.0, 49.0, 0.0], 'GSM1298345': [0.0, 54.0, 1.0], 'GSM1298346': [0.0, 54.0, 0.0], 'GSM1298347': [0.0, 52.0, 0.0], 'GSM1298348': [0.0, 52.0, 0.0], 'GSM1298349': [0.0, 51.0, 0.0], 'GSM1298350': [0.0, 63.0, 0.0], 'GSM1298351': [0.0, 71.0, 1.0], 'GSM1298352': [0.0, 76.0, 0.0], 'GSM1298353': [0.0, 73.0, 0.0], 'GSM1298354': [0.0, 68.0, 1.0], 'GSM1298355': [0.0, 73.0, 0.0], 'GSM1298356': [0.0, 76.0, 0.0], 'GSM1298357': [0.0, 64.0, 1.0], 'GSM1298358': [0.0, 79.0, 0.0], 'GSM1298359': [0.0, 58.0, 1.0], 'GSM1298360': [0.0, 67.0, 1.0], 'GSM1298361': [0.0, 71.0, 1.0], 'GSM1298362': [0.0, 80.0, 0.0], 'GSM1298363': [0.0, 71.0, 0.0], 'GSM1298364': [0.0, 73.0, 1.0], 'GSM1298365': [0.0, 71.0, 1.0], 'GSM1298366': [0.0, 69.0, 1.0], 'GSM1298367': [0.0, 70.0, 1.0], 'GSM1298368': [0.0, 63.0, 1.0], 'GSM1298369': [0.0, 65.0, 1.0], 'GSM1298370': [0.0, 64.0, 0.0], 'GSM1298371': [0.0, 67.0, 1.0], 'GSM1298372': [0.0, 67.0, 0.0]}\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# From the background information, we can see that this dataset contains gene expression data from peripheral blood\n", "# profiled using the Illumina platform. This is not miRNA or methylation data, so gene expression data is available.\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# From the sample characteristics dictionary:\n", "# - Age is available at key 0\n", "# - Gender is available at key 1\n", "# - Trait (Arrhythmia/Atrial fibrillation) is not directly available in the sample characteristics,\n", "# but from the background information, we know these are \"control participants\" for atrial fibrillation (AF)\n", "# which means they don't have arrhythmia (value 0)\n", "\n", "trait_row = 0 # We'll use the age row as a placeholder for adding our synthetic trait data\n", "age_row = 0 # Age data available at key 0\n", "gender_row = 1 # Gender data available at key 1\n", "\n", "# 2.2 Data Type Conversion\n", "# For trait - all subjects are controls (0) for arrhythmia\n", "def convert_trait(input_str):\n", " return 0 # All subjects are controls (don't have arrhythmia)\n", "\n", "# Age conversion function - extract numeric age value\n", "def convert_age(age_str):\n", " try:\n", " if age_str and \":\" in age_str:\n", " age_value = age_str.split(\":\")[1].strip()\n", " return float(age_value) # Convert to continuous numeric\n", " return None\n", " except:\n", " return None\n", "\n", "# Gender conversion function - convert to binary (0 for female, 1 for male)\n", "def convert_gender(gender_str):\n", " if gender_str and \":\" in gender_str:\n", " gender_value = gender_str.split(\":\")[1].strip().lower()\n", " if 'female' in gender_value:\n", " return 0\n", " elif 'male' in gender_value:\n", " return 1\n", " return None\n", "\n", "# Since we can infer trait data (all subjects are controls), set is_trait_available to True\n", "is_trait_available = True\n", "\n", "# 3. Save Metadata\n", "# Initial filtering on dataset usability\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 we have inferred trait data, we can perform clinical feature extraction\n", "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 clinical data\n", "preview_clinical = preview_df(clinical_df)\n", "print(\"Preview of clinical features:\")\n", "print(preview_clinical)\n", "\n", "# Save the clinical data\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": "32ce2c5d", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "d2e3210c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:35:32.161141Z", "iopub.status.busy": "2025-03-25T06:35:32.161031Z", "iopub.status.idle": "2025-03-25T06:35:32.759264Z", "shell.execute_reply": "2025-03-25T06:35:32.758692Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix file found: ../../input/GEO/Arrhythmia/GSE47727/GSE47727_series_matrix.txt.gz\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape: (48803, 122)\n", "First 20 gene/probe identifiers:\n", "Index(['ILMN_1343291', 'ILMN_1343295', 'ILMN_1651199', 'ILMN_1651209',\n", " 'ILMN_1651210', 'ILMN_1651221', 'ILMN_1651228', 'ILMN_1651229',\n", " 'ILMN_1651230', 'ILMN_1651232', 'ILMN_1651235', 'ILMN_1651236',\n", " 'ILMN_1651237', 'ILMN_1651238', 'ILMN_1651249', 'ILMN_1651253',\n", " 'ILMN_1651254', 'ILMN_1651259', 'ILMN_1651260', 'ILMN_1651262'],\n", " dtype='object', name='ID')\n" ] } ], "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": "29313697", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "6aa3344f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:35:32.761028Z", "iopub.status.busy": "2025-03-25T06:35:32.760898Z", "iopub.status.idle": "2025-03-25T06:35:32.763316Z", "shell.execute_reply": "2025-03-25T06:35:32.762862Z" } }, "outputs": [], "source": [ "# The gene identifiers have the format \"ILMN_xxxxx\" which are Illumina probe IDs\n", "# These are not human gene symbols but rather probe identifiers specific to Illumina microarray platforms\n", "# These will need to be mapped to standard gene symbols for proper analysis\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "f1c6cca7", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "94de901a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:35:32.764977Z", "iopub.status.busy": "2025-03-25T06:35:32.764874Z", "iopub.status.idle": "2025-03-25T06:36:42.307926Z", "shell.execute_reply": "2025-03-25T06:36:42.307403Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene annotation preview:\n", "Columns in gene annotation: ['ID', 'nuID', 'Species', 'Source', 'Search_Key', 'Transcript', 'ILMN_Gene', 'Source_Reference_ID', 'RefSeq_ID', 'Unigene_ID', 'Entrez_Gene_ID', 'GI', 'Accession', 'Symbol', 'Protein_Product', 'Array_Address_Id', 'Probe_Type', 'Probe_Start', 'SEQUENCE', 'Chromosome', 'Probe_Chr_Orientation', 'Probe_Coordinates', 'Cytoband', 'Definition', 'Ontology_Component', 'Ontology_Process', 'Ontology_Function', 'Synonyms', 'Obsolete_Probe_Id', 'GB_ACC']\n", "{'ID': ['ILMN_1725881', 'ILMN_1910180', 'ILMN_1804174', 'ILMN_1796063', 'ILMN_1811966'], 'nuID': ['rp13_p1x6D80lNLk3c', 'NEX0oqCV8.er4HVfU4', 'KyqQynMZxJcruyylEU', 'xXl7eXuF7sbPEp.KFI', '9ckqJrioiaej9_ajeQ'], 'Species': ['Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens'], 'Source': ['RefSeq', 'Unigene', 'RefSeq', 'RefSeq', 'RefSeq'], 'Search_Key': ['ILMN_44919', 'ILMN_127219', 'ILMN_139282', 'ILMN_5006', 'ILMN_38756'], 'Transcript': ['ILMN_44919', 'ILMN_127219', 'ILMN_139282', 'ILMN_5006', 'ILMN_38756'], 'ILMN_Gene': ['LOC23117', 'HS.575038', 'FCGR2B', 'TRIM44', 'LOC653895'], 'Source_Reference_ID': ['XM_933824.1', 'Hs.575038', 'XM_938851.1', 'NM_017583.3', 'XM_936379.1'], 'RefSeq_ID': ['XM_933824.1', nan, 'XM_938851.1', 'NM_017583.3', 'XM_936379.1'], 'Unigene_ID': [nan, 'Hs.575038', nan, nan, nan], 'Entrez_Gene_ID': [23117.0, nan, 2213.0, 54765.0, 653895.0], 'GI': [89040007.0, 10437021.0, 88952550.0, 29029528.0, 89033487.0], 'Accession': ['XM_933824.1', 'AK024680', 'XM_938851.1', 'NM_017583.3', 'XM_936379.1'], 'Symbol': ['LOC23117', nan, 'FCGR2B', 'TRIM44', 'LOC653895'], 'Protein_Product': ['XP_938917.1', nan, 'XP_943944.1', 'NP_060053.2', 'XP_941472.1'], 'Array_Address_Id': [1710221.0, 5900364.0, 2480717.0, 1300239.0, 4480719.0], 'Probe_Type': ['I', 'S', 'I', 'S', 'S'], 'Probe_Start': [122.0, 1409.0, 1643.0, 2901.0, 25.0], 'SEQUENCE': ['GGCTCCTCTTTGGGCTCCTACTGGAATTTATCAGCCATCAGTGCATCTCT', 'ACACCTTCAGGAGGGAAGCCCTTATTTCTGGGTTGAACTCCCCTTCCATG', 'TAGGGGCAATAGGCTATACGCTACAGCCTAGGTGTGTAGTAGGCCACACC', 'CCTGCCTGTCTGCCTGTGACCTGTGTACGTATTACAGGCTTTAGGACCAG', 'CTAGCAGGGAGCGGTGAGGGAGAGCGGCTGGATTTCTTGCGGGATCTGCA'], 'Chromosome': ['16', nan, nan, '11', nan], 'Probe_Chr_Orientation': ['-', nan, nan, '+', nan], 'Probe_Coordinates': ['21766363-21766363:21769901-21769949', nan, nan, '35786070-35786119', nan], 'Cytoband': ['16p12.2a', nan, '1q23.3b', '11p13a', '10q11.23b'], 'Definition': ['PREDICTED: Homo sapiens KIAA0220-like protein, transcript variant 11 (LOC23117), mRNA.', 'Homo sapiens cDNA: FLJ21027 fis, clone CAE07110', 'PREDICTED: Homo sapiens Fc fragment of IgG, low affinity IIb, receptor (CD32) (FCGR2B), mRNA.', 'Homo sapiens tripartite motif-containing 44 (TRIM44), mRNA.', 'PREDICTED: Homo sapiens similar to protein geranylgeranyltransferase type I, beta subunit (LOC653895), mRNA.'], 'Ontology_Component': [nan, nan, nan, 'intracellular [goid 5622] [evidence IEA]', nan], 'Ontology_Process': [nan, nan, nan, nan, nan], 'Ontology_Function': [nan, nan, nan, 'zinc ion binding [goid 8270] [evidence IEA]; metal ion binding [goid 46872] [evidence IEA]', nan], 'Synonyms': [nan, nan, nan, 'MGC3490; MC7; HSA249128; DIPB', nan], 'Obsolete_Probe_Id': [nan, nan, nan, 'MGC3490; MC7; HSA249128; DIPB', nan], 'GB_ACC': ['XM_933824.1', 'AK024680', 'XM_938851.1', 'NM_017583.3', 'XM_936379.1']}\n", "\n", "Analyzing SPOT_ID.1 column for gene symbols:\n", "\n", "Gene data ID prefix: ILMN\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Column 'ID' contains values matching gene data ID pattern\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Column 'Source' contains values matching gene data ID pattern\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Column 'Search_Key' contains values matching gene data ID pattern\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Column 'Transcript' contains values matching gene data ID pattern\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Checking for columns containing transcript or gene related terms:\n", "Column 'Transcript' may contain gene-related information\n", "Sample values: ['ILMN_44919', 'ILMN_127219', 'ILMN_139282']\n", "Column 'ILMN_Gene' may contain gene-related information\n", "Sample values: ['LOC23117', 'HS.575038', 'FCGR2B']\n", "Column 'Unigene_ID' may contain gene-related information\n", "Sample values: [nan, 'Hs.575038', nan]\n", "Column 'Entrez_Gene_ID' may contain gene-related information\n", "Sample values: [23117.0, nan, 2213.0]\n", "Column 'Symbol' may contain gene-related information\n", "Sample values: ['LOC23117', nan, 'FCGR2B']\n" ] } ], "source": [ "# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\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", "# Check for gene information in the SPOT_ID.1 column which appears to contain gene names\n", "print(\"\\nAnalyzing SPOT_ID.1 column for gene symbols:\")\n", "if 'SPOT_ID.1' in gene_annotation.columns:\n", " # Extract a few sample values\n", " sample_values = gene_annotation['SPOT_ID.1'].head(3).tolist()\n", " for i, value in enumerate(sample_values):\n", " print(f\"Sample {i+1} excerpt: {value[:200]}...\") # Print first 200 chars\n", " # Test the extract_human_gene_symbols function on these values\n", " symbols = extract_human_gene_symbols(value)\n", " print(f\" Extracted gene symbols: {symbols}\")\n", "\n", "# Try to find the probe IDs in the gene annotation\n", "gene_data_id_prefix = gene_data.index[0].split('_')[0] # Get prefix of first gene ID\n", "print(f\"\\nGene data ID prefix: {gene_data_id_prefix}\")\n", "\n", "# Look for columns that might match the gene data IDs\n", "for col in gene_annotation.columns:\n", " if gene_annotation[col].astype(str).str.contains(gene_data_id_prefix).any():\n", " print(f\"Column '{col}' contains values matching gene data ID pattern\")\n", "\n", "# Check if there's any column that might contain transcript or gene IDs\n", "print(\"\\nChecking for columns containing transcript or gene related terms:\")\n", "for col in gene_annotation.columns:\n", " if any(term in col.upper() for term in ['GENE', 'TRANSCRIPT', 'SYMBOL', 'NAME', 'DESCRIPTION']):\n", " print(f\"Column '{col}' may contain gene-related information\")\n", " # Show sample values\n", " print(f\"Sample values: {gene_annotation[col].head(3).tolist()}\")\n" ] }, { "cell_type": "markdown", "id": "3f2d9ec5", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "9bad7589", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:36:42.309506Z", "iopub.status.busy": "2025-03-25T06:36:42.309379Z", "iopub.status.idle": "2025-03-25T06:36:44.573940Z", "shell.execute_reply": "2025-03-25T06:36:44.573414Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sample probe IDs from gene_annotation['ID']:\n", "0 ILMN_1725881\n", "1 ILMN_1910180\n", "2 ILMN_1804174\n", "3 ILMN_1796063\n", "4 ILMN_1811966\n", "Name: ID, dtype: object\n", "\n", "Sample gene symbols from gene_annotation['Symbol']:\n", "0 LOC23117\n", "1 NaN\n", "2 FCGR2B\n", "3 TRIM44\n", "4 LOC653895\n", "Name: Symbol, dtype: object\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene mapping dataframe shape: (36157, 2)\n", "Gene mapping preview:\n", "{'ID': ['ILMN_1725881', 'ILMN_1804174', 'ILMN_1796063', 'ILMN_1811966', 'ILMN_1668162'], 'Gene': ['LOC23117', 'FCGR2B', 'TRIM44', 'LOC653895', 'DGAT2L3']}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene expression data shape: (19120, 122)\n", "First 5 genes:\n", "Index(['A1BG', 'A1CF', 'A26A1', 'A26B1', 'A26C1B'], dtype='object', name='Gene')\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene expression data saved to ../../output/preprocess/Arrhythmia/gene_data/GSE47727.csv\n" ] } ], "source": [ "# 1. Decide which columns in gene_annotation store probe IDs and gene symbols\n", "probe_id_col = 'ID' # This contains the ILMN_xxxx identifiers that match gene_data\n", "gene_symbol_col = 'Symbol' # This contains standard gene symbols\n", "\n", "# Verify the column choices by showing a few examples\n", "print(f\"Sample probe IDs from gene_annotation['{probe_id_col}']:\")\n", "print(gene_annotation[probe_id_col].head(5))\n", "print(f\"\\nSample gene symbols from gene_annotation['{gene_symbol_col}']:\")\n", "print(gene_annotation[gene_symbol_col].head(5))\n", "\n", "# 2. Get gene mapping dataframe\n", "mapping_df = get_gene_mapping(gene_annotation, probe_id_col, gene_symbol_col)\n", "print(f\"\\nGene mapping dataframe shape: {mapping_df.shape}\")\n", "print(\"Gene mapping preview:\")\n", "print(preview_df(mapping_df, n=5))\n", "\n", "# 3. Convert probe-level measurements to gene expression data\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "print(f\"\\nGene expression data shape: {gene_data.shape}\")\n", "print(\"First 5 genes:\")\n", "print(gene_data.index[:5])\n", "\n", "# Write the gene expression data to a file\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "gene_data.to_csv(out_gene_data_file)\n", "print(f\"\\nGene expression data saved to {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "d90c4fe1", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "66347d82", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:36:44.575842Z", "iopub.status.busy": "2025-03-25T06:36:44.575718Z", "iopub.status.idle": "2025-03-25T06:36:51.772770Z", "shell.execute_reply": "2025-03-25T06:36:51.772442Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape before normalization: (19120, 122)\n", "Gene data shape after normalization: (18326, 122)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene expression data saved to ../../output/preprocess/Arrhythmia/gene_data/GSE47727.csv\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Original clinical data preview:\n", " !Sample_geo_accession GSM1298251 GSM1298252 GSM1298253 \\\n", "0 !Sample_characteristics_ch1 age (yrs): 67 age (yrs): 54 age (yrs): 73 \n", "1 !Sample_characteristics_ch1 gender: male gender: male gender: male \n", "2 !Sample_characteristics_ch1 tissue: blood tissue: blood tissue: blood \n", "\n", " GSM1298254 GSM1298255 GSM1298256 GSM1298257 \\\n", "0 age (yrs): 52 age (yrs): 75 age (yrs): 59 age (yrs): 74 \n", "1 gender: female gender: male gender: male gender: female \n", "2 tissue: blood tissue: blood tissue: blood tissue: blood \n", "\n", " GSM1298258 GSM1298259 ... GSM1298363 GSM1298364 \\\n", "0 age (yrs): 75 age (yrs): 74 ... age (yrs): 71 age (yrs): 73 \n", "1 gender: female gender: female ... gender: female gender: male \n", "2 tissue: blood tissue: blood ... tissue: blood tissue: blood \n", "\n", " GSM1298365 GSM1298366 GSM1298367 GSM1298368 GSM1298369 \\\n", "0 age (yrs): 71 age (yrs): 69 age (yrs): 70 age (yrs): 63 age (yrs): 65 \n", "1 gender: male gender: male gender: male gender: male gender: male \n", "2 tissue: blood tissue: blood tissue: blood tissue: blood tissue: blood \n", "\n", " GSM1298370 GSM1298371 GSM1298372 \n", "0 age (yrs): 64 age (yrs): 67 age (yrs): 67 \n", "1 gender: female gender: male gender: female \n", "2 tissue: blood tissue: blood tissue: blood \n", "\n", "[3 rows x 123 columns]\n", "Selected clinical data shape: (3, 122)\n", "Clinical data preview:\n", " GSM1298251 GSM1298252 GSM1298253 GSM1298254 GSM1298255 \\\n", "Arrhythmia 0.0 0.0 0.0 0.0 0.0 \n", "Age 67.0 54.0 73.0 52.0 75.0 \n", "Gender 1.0 1.0 1.0 0.0 1.0 \n", "\n", " GSM1298256 GSM1298257 GSM1298258 GSM1298259 GSM1298260 ... \\\n", "Arrhythmia 0.0 0.0 0.0 0.0 0.0 ... \n", "Age 59.0 74.0 75.0 74.0 76.0 ... \n", "Gender 1.0 0.0 0.0 0.0 0.0 ... \n", "\n", " GSM1298363 GSM1298364 GSM1298365 GSM1298366 GSM1298367 \\\n", "Arrhythmia 0.0 0.0 0.0 0.0 0.0 \n", "Age 71.0 73.0 71.0 69.0 70.0 \n", "Gender 0.0 1.0 1.0 1.0 1.0 \n", "\n", " GSM1298368 GSM1298369 GSM1298370 GSM1298371 GSM1298372 \n", "Arrhythmia 0.0 0.0 0.0 0.0 0.0 \n", "Age 63.0 65.0 64.0 67.0 67.0 \n", "Gender 1.0 1.0 0.0 1.0 0.0 \n", "\n", "[3 rows x 122 columns]\n", "Linked data shape before processing: (122, 18329)\n", "Linked data preview (first 5 rows, 5 columns):\n", " Arrhythmia Age Gender A1BG A1CF\n", "GSM1298251 0.0 67.0 1.0 10.825611 16.422848\n", "GSM1298252 0.0 54.0 1.0 11.188162 16.182496\n", "GSM1298253 0.0 73.0 1.0 11.070092 16.291996\n", "GSM1298254 0.0 52.0 0.0 10.885305 16.149145\n", "GSM1298255 0.0 75.0 1.0 10.925528 16.580949\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (122, 18329)\n", "Quartiles for 'Arrhythmia':\n", " 25%: 0.0\n", " 50% (Median): 0.0\n", " 75%: 0.0\n", "Min: 0.0\n", "Max: 0.0\n", "The distribution of the feature 'Arrhythmia' in this dataset is severely biased.\n", "\n", "Quartiles for 'Age':\n", " 25%: 63.0\n", " 50% (Median): 70.0\n", " 75%: 74.0\n", "Min: 49.0\n", "Max: 82.0\n", "The distribution of the feature 'Age' in this dataset is fine.\n", "\n", "For the feature 'Gender', the least common label is '1.0' with 45 occurrences. This represents 36.89% of the dataset.\n", "The distribution of the feature 'Gender' in this dataset is fine.\n", "\n", "Data shape after removing biased features: (122, 18329)\n", "Dataset is not usable for analysis. No linked data file saved.\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "# Use normalize_gene_symbols_in_index to standardize gene symbols\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Gene data shape before normalization: {gene_data.shape}\")\n", "print(f\"Gene data shape after normalization: {normalized_gene_data.shape}\")\n", "\n", "# Save the normalized gene data to file\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 expression data saved to {out_gene_data_file}\")\n", "\n", "# Load the actual clinical data from the matrix file that was previously obtained in Step 1\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "background_info, clinical_data = get_background_and_clinical_data(matrix_file)\n", "\n", "# Get preview of clinical data to understand its structure\n", "print(\"Original clinical data preview:\")\n", "print(clinical_data.head())\n", "\n", "# 2. If we have trait data available, proceed with linking\n", "if trait_row is not None:\n", " # Extract clinical features using the original clinical data\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", " print(f\"Selected clinical data shape: {selected_clinical_df.shape}\")\n", " print(\"Clinical data preview:\")\n", " print(selected_clinical_df.head())\n", "\n", " # Link the clinical and genetic data\n", " linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)\n", " print(f\"Linked data shape before processing: {linked_data.shape}\")\n", " print(\"Linked data preview (first 5 rows, 5 columns):\")\n", " print(linked_data.iloc[:5, :5] if not linked_data.empty else \"Empty dataframe\")\n", "\n", " # 3. Handle missing values\n", " try:\n", " linked_data = handle_missing_values(linked_data, trait)\n", " print(f\"Data shape after handling missing values: {linked_data.shape}\")\n", " except Exception as e:\n", " print(f\"Error handling missing values: {e}\")\n", " linked_data = pd.DataFrame() # Create empty dataframe if error occurs\n", "\n", " # 4. Check for bias in features\n", " if not linked_data.empty and linked_data.shape[0] > 0:\n", " is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", " print(f\"Data shape after removing biased features: {linked_data.shape}\")\n", " else:\n", " is_biased = True\n", " print(\"Cannot check for bias as dataframe is empty or has no rows after missing value handling\")\n", "\n", " # 5. Validate and save cohort information\n", " note = \"\"\n", " if linked_data.empty or linked_data.shape[0] == 0:\n", " note = \"Dataset contains gene expression data related to liver fibrosis progression, but linking clinical and genetic data failed, possibly due to mismatched sample IDs.\"\n", " else:\n", " note = \"Dataset contains gene expression data for liver fibrosis progression, which is relevant to liver cirrhosis research.\"\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=True,\n", " is_trait_available=True,\n", " is_biased=is_biased,\n", " df=linked_data,\n", " note=note\n", " )\n", "\n", " # 6. Save the linked data if 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(\"Dataset is not usable for analysis. No linked data file saved.\")\n", "else:\n", " # If no trait data available, validate with trait_available=False\n", " is_usable = validate_and_save_cohort_info(\n", " is_final=True,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=True,\n", " is_trait_available=False,\n", " is_biased=True, # Set to True since we can't use data without trait\n", " df=pd.DataFrame(), # Empty DataFrame\n", " note=\"Dataset contains gene expression data but lacks proper clinical trait information for liver cirrhosis analysis.\"\n", " )\n", " \n", " print(\"Dataset is not usable for liver cirrhosis analysis due to lack of clinical trait data. No linked data file 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 }