{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0f8b4d34", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:33:57.360300Z", "iopub.status.busy": "2025-03-25T08:33:57.360070Z", "iopub.status.idle": "2025-03-25T08:33:57.525603Z", "shell.execute_reply": "2025-03-25T08:33:57.525274Z" } }, "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 = \"Crohns_Disease\"\n", "cohort = \"GSE207022\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Crohns_Disease\"\n", "in_cohort_dir = \"../../input/GEO/Crohns_Disease/GSE207022\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Crohns_Disease/GSE207022.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Crohns_Disease/gene_data/GSE207022.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Crohns_Disease/clinical_data/GSE207022.csv\"\n", "json_path = \"../../output/preprocess/Crohns_Disease/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "3697486b", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "3f907236", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:33:57.527022Z", "iopub.status.busy": "2025-03-25T08:33:57.526873Z", "iopub.status.idle": "2025-03-25T08:33:57.719591Z", "shell.execute_reply": "2025-03-25T08:33:57.719251Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Efficacy and safety of ustekinumab treatment in patients with Crohn's disease\"\n", "!Series_summary\t\"UNITI-2 was a phase 3 clinical trial (ClinicalTrials.gov Identifier: NCT01369342) comparing the effects (both positive and negative) of an initial treatment with ustekinumab to a placebo over 8 weeks in patients with moderately to severely active Crohn's disease.\"\n", "!Series_overall_design\t\"A gene expression profiling study was conducted in which rectum biopsy samples were collected for RNA extraction and hybridization to microarrays from patients (n=125) with moderate-to-severe Crohn's disease and from non-IBD subjects (n=23).\"\n", "Sample Characteristics Dictionary:\n", "{0: ['tissue: rectum'], 1: ['donor id: CNTO1275CRD3002-20554', 'donor id: CNTO1275CRD3002-20667', 'donor id: CNTO1275CRD3002-20449', 'donor id: CNTO1275CRD3002-20927', 'donor id: CNTO1275CRD3002-20270', 'donor id: CNTO1275CRD3002-20072', 'donor id: CNTO1275CRD3002-20109', 'donor id: CNTO1275CRD3002-20346', 'donor id: HC-1', 'donor id: HC-2', 'donor id: HC-3', 'donor id: HC-4', 'donor id: HC-5', 'donor id: HC-6', 'donor id: HC-7', 'donor id: HC-8', 'donor id: HC-9', 'donor id: HC-10', 'donor id: HC-11', 'donor id: HC-12', 'donor id: HC-13', 'donor id: HC-14', 'donor id: HC-15', 'donor id: HC-16', 'donor id: HC-17', 'donor id: HC-18', 'donor id: HC-19', 'donor id: HC-20', 'donor id: HC-21', 'donor id: HC-22'], 2: ['visit: I-WK0'], 3: [\"diagnosis: Crohn's disease\", 'diagnosis: healthy control'], 4: ['treatment: Ustekinumab 130 mg IV', 'treatment: Ustekinumab 6 mg/kg (520 mg)', 'treatment: Placebo IV', 'treatment: Ustekinumab 6 mg/kg (390 mg)', 'treatment: NA', 'treatment: Ustekinumab 6 mg/kg (260 mg)'], 5: ['inflamed area at week 0: Ileum and colon', 'inflamed area at week 0: Colon only', 'inflamed area at week 0: NA'], 6: ['mucosal healing at week 8: N', 'mucosal healing at week 8: NA', 'mucosal healing at week 8: Y']}\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": "8d858784", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "8b35b647", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:33:57.720882Z", "iopub.status.busy": "2025-03-25T08:33:57.720776Z", "iopub.status.idle": "2025-03-25T08:33:57.735229Z", "shell.execute_reply": "2025-03-25T08:33:57.734940Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of selected clinical features:\n", "{'CNTO1275CRD3002-20554': [1.0], 'CNTO1275CRD3002-20667': [1.0], 'CNTO1275CRD3002-20449': [1.0], 'CNTO1275CRD3002-20927': [1.0], 'CNTO1275CRD3002-20270': [1.0], 'CNTO1275CRD3002-20072': [1.0], 'CNTO1275CRD3002-20109': [1.0], 'CNTO1275CRD3002-20346': [1.0], 'HC-1': [0.0], 'HC-2': [0.0], 'HC-3': [0.0], 'HC-4': [0.0], 'HC-5': [0.0], 'HC-6': [0.0], 'HC-7': [0.0], 'HC-8': [0.0], 'HC-9': [0.0], 'HC-10': [0.0], 'HC-11': [0.0], 'HC-12': [0.0], 'HC-13': [0.0], 'HC-14': [0.0], 'HC-15': [0.0], 'HC-16': [0.0], 'HC-17': [0.0], 'HC-18': [0.0], 'HC-19': [0.0], 'HC-20': [0.0], 'HC-21': [0.0], 'HC-22': [0.0]}\n", "Clinical data saved to ../../output/preprocess/Crohns_Disease/clinical_data/GSE207022.csv\n" ] } ], "source": [ "import pandas as pd\n", "import os\n", "import json\n", "from typing import Optional, Callable, Dict, Any\n", "\n", "# 1. Determine gene expression data availability\n", "is_gene_available = True # This appears to be gene expression data from microarrays\n", "\n", "# 2. Determine clinical data availability and create conversion functions\n", "\n", "# 2.1 Identify rows containing relevant clinical data\n", "trait_row = 3 # 'diagnosis: Crohn's disease', 'diagnosis: healthy control'\n", "age_row = None # Age is not available in the sample characteristics\n", "gender_row = None # Gender is not available in the sample characteristics\n", "\n", "# 2.2 Create conversion functions for clinical variables\n", "\n", "def convert_trait(val: str) -> int:\n", " \"\"\"Convert Crohn's disease status to binary.\"\"\"\n", " if val is None:\n", " return None\n", " if ':' in val:\n", " val = val.split(':', 1)[1].strip().lower()\n", " else:\n", " val = val.strip().lower()\n", " \n", " if \"crohn\" in val:\n", " return 1 # Has Crohn's disease\n", " elif \"healthy\" in val or \"control\" in val:\n", " return 0 # Healthy control\n", " return None\n", "\n", "def convert_age(val: str) -> Optional[float]:\n", " \"\"\"Convert age to continuous value.\"\"\"\n", " # Not used since age data is not available\n", " return None\n", "\n", "def convert_gender(val: str) -> Optional[int]:\n", " \"\"\"Convert gender to binary (0 for female, 1 for male).\"\"\"\n", " # Not used since gender data is not available\n", " return None\n", "\n", "# 3. Save initial metadata about dataset usability\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. Extract clinical features if trait data is available\n", "if trait_row is not None:\n", " # Create the clinical data DataFrame from the sample characteristics dictionary\n", " # Based on the previous output, we have samples in columns and characteristics in rows\n", " sample_characteristics = {\n", " 0: ['tissue: rectum'], \n", " 1: ['donor id: CNTO1275CRD3002-20554', 'donor id: CNTO1275CRD3002-20667', 'donor id: CNTO1275CRD3002-20449', \n", " 'donor id: CNTO1275CRD3002-20927', 'donor id: CNTO1275CRD3002-20270', 'donor id: CNTO1275CRD3002-20072', \n", " 'donor id: CNTO1275CRD3002-20109', 'donor id: CNTO1275CRD3002-20346', 'donor id: HC-1', 'donor id: HC-2', \n", " 'donor id: HC-3', 'donor id: HC-4', 'donor id: HC-5', 'donor id: HC-6', 'donor id: HC-7', 'donor id: HC-8', \n", " 'donor id: HC-9', 'donor id: HC-10', 'donor id: HC-11', 'donor id: HC-12', 'donor id: HC-13', 'donor id: HC-14', \n", " 'donor id: HC-15', 'donor id: HC-16', 'donor id: HC-17', 'donor id: HC-18', 'donor id: HC-19', 'donor id: HC-20', \n", " 'donor id: HC-21', 'donor id: HC-22'], \n", " 2: ['visit: I-WK0'], \n", " 3: [\"diagnosis: Crohn's disease\", 'diagnosis: healthy control'], \n", " 4: ['treatment: Ustekinumab 130 mg IV', 'treatment: Ustekinumab 6 mg/kg (520 mg)', 'treatment: Placebo IV', \n", " 'treatment: Ustekinumab 6 mg/kg (390 mg)', 'treatment: NA', 'treatment: Ustekinumab 6 mg/kg (260 mg)'], \n", " 5: ['inflamed area at week 0: Ileum and colon', 'inflamed area at week 0: Colon only', 'inflamed area at week 0: NA'], \n", " 6: ['mucosal healing at week 8: N', 'mucosal healing at week 8: NA', 'mucosal healing at week 8: Y']\n", " }\n", " \n", " # Let's convert this dictionary to a DataFrame that can be used with geo_select_clinical_features\n", " # The function expects rows as sample characteristics and columns as samples\n", " \n", " # Create a list of sample IDs from the donor id row (row 1)\n", " sample_ids = []\n", " for donor_id in sample_characteristics[1]:\n", " if ':' in donor_id:\n", " sample_id = donor_id.split(':', 1)[1].strip()\n", " sample_ids.append(sample_id)\n", " \n", " # Initialize a DataFrame with characteristics as rows and samples as columns\n", " clinical_data = pd.DataFrame(index=range(len(sample_characteristics)), columns=sample_ids)\n", " \n", " # Fill the DataFrame with characteristic values\n", " for row_idx, values in sample_characteristics.items():\n", " # Handle the case where one row has multiple values (different for different samples)\n", " if len(values) == 1:\n", " # Same value for all samples\n", " for col in clinical_data.columns:\n", " clinical_data.iloc[row_idx, clinical_data.columns.get_loc(col)] = values[0]\n", " else:\n", " # Different values for different samples or less values than samples\n", " # For diagnosis (row 3), we need to infer values from donor IDs\n", " if row_idx == 3: # Diagnosis row\n", " for col_idx, col in enumerate(clinical_data.columns):\n", " if 'HC-' in col: # Healthy control\n", " clinical_data.iloc[row_idx, col_idx] = 'diagnosis: healthy control'\n", " else: # Crohn's disease\n", " clinical_data.iloc[row_idx, col_idx] = \"diagnosis: Crohn's disease\"\n", " else:\n", " # For other rows, we'll set to None as we don't have enough information to map\n", " for col_idx, col in enumerate(clinical_data.columns):\n", " clinical_data.iloc[row_idx, col_idx] = None\n", " \n", " # Extract clinical features\n", " selected_clinical = 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)\n", " print(\"Preview of selected clinical features:\")\n", " print(preview)\n", " \n", " # Save the clinical data\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " selected_clinical.to_csv(out_clinical_data_file)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "24cf5a7b", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "55da15c9", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:33:57.736353Z", "iopub.status.busy": "2025-03-25T08:33:57.736247Z", "iopub.status.idle": "2025-03-25T08:33:58.128283Z", "shell.execute_reply": "2025-03-25T08:33:58.127909Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "First 20 gene/probe identifiers:\n", "Index(['1007_PM_s_at', '1053_PM_at', '117_PM_at', '121_PM_at', '1255_PM_g_at',\n", " '1294_PM_at', '1316_PM_at', '1320_PM_at', '1405_PM_i_at', '1431_PM_at',\n", " '1438_PM_at', '1487_PM_at', '1494_PM_f_at', '1552256_PM_a_at',\n", " '1552257_PM_a_at', '1552258_PM_at', '1552261_PM_at', '1552263_PM_at',\n", " '1552264_PM_a_at', '1552266_PM_at'],\n", " dtype='object', name='ID')\n", "\n", "Gene data dimensions: 54715 genes × 148 samples\n" ] } ], "source": [ "# 1. Re-identify the SOFT and matrix files to ensure we have the correct paths\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. Extract the gene expression data from the matrix file\n", "gene_data = get_genetic_data(matrix_file)\n", "\n", "# 3. Print the first 20 row IDs (gene or probe identifiers)\n", "print(\"\\nFirst 20 gene/probe identifiers:\")\n", "print(gene_data.index[:20])\n", "\n", "# 4. Print the dimensions of the gene expression data\n", "print(f\"\\nGene data dimensions: {gene_data.shape[0]} genes × {gene_data.shape[1]} samples\")\n", "\n", "# Note: we keep is_gene_available as True since we successfully extracted gene expression data\n", "is_gene_available = True\n" ] }, { "cell_type": "markdown", "id": "fdecb4fe", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "5762dbd0", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:33:58.129510Z", "iopub.status.busy": "2025-03-25T08:33:58.129392Z", "iopub.status.idle": "2025-03-25T08:33:58.131254Z", "shell.execute_reply": "2025-03-25T08:33:58.130975Z" } }, "outputs": [], "source": [ "# Looking at the identifiers: '1007_PM_s_at', '1053_PM_at', etc.\n", "# These are Affymetrix probe IDs from a microarray platform, not human gene symbols\n", "# They need to be mapped to standard gene symbols for analysis\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "ec50cac0", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "bbe63921", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:33:58.132321Z", "iopub.status.busy": "2025-03-25T08:33:58.132220Z", "iopub.status.idle": "2025-03-25T08:34:07.025674Z", "shell.execute_reply": "2025-03-25T08:34:07.025341Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['1007_PM_s_at', '1053_PM_at', '117_PM_at', '121_PM_at', '1255_PM_g_at'], 'GB_ACC': ['U48705', 'M87338', 'X51757', 'X69699', 'L36861'], 'SPOT_ID': [nan, nan, nan, nan, nan], 'Species Scientific Name': ['Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens'], 'Annotation Date': ['Aug 20, 2010', 'Aug 20, 2010', 'Aug 20, 2010', 'Aug 20, 2010', 'Aug 20, 2010'], 'Sequence Type': ['Exemplar sequence', 'Exemplar sequence', 'Exemplar sequence', 'Exemplar sequence', 'Exemplar sequence'], 'Sequence Source': ['Affymetrix Proprietary Database', 'GenBank', 'Affymetrix Proprietary Database', 'GenBank', 'Affymetrix Proprietary Database'], 'Target Description': ['U48705 /FEATURE=mRNA /DEFINITION=HSU48705 Human receptor tyrosine kinase DDR gene, complete cds', 'M87338 /FEATURE= /DEFINITION=HUMA1SBU Human replication factor C, 40-kDa subunit (A1) mRNA, complete cds', \"X51757 /FEATURE=cds /DEFINITION=HSP70B Human heat-shock protein HSP70B' gene\", 'X69699 /FEATURE= /DEFINITION=HSPAX8A H.sapiens Pax8 mRNA', 'L36861 /FEATURE=expanded_cds /DEFINITION=HUMGCAPB Homo sapiens guanylate cyclase activating protein (GCAP) gene exons 1-4, complete cds'], 'Representative Public ID': ['U48705', 'M87338', 'X51757', 'X69699', 'L36861'], 'Gene Title': ['discoidin domain receptor tyrosine kinase 1', 'replication factor C (activator 1) 2, 40kDa', \"heat shock 70kDa protein 6 (HSP70B')\", 'paired box 8', 'guanylate cyclase activator 1A (retina)'], 'Gene Symbol': ['DDR1', 'RFC2', 'HSPA6', 'PAX8', 'GUCA1A'], 'ENTREZ_GENE_ID': ['780', '5982', '3310', '7849', '2978'], 'RefSeq Transcript ID': ['NM_001954 /// NM_013993 /// NM_013994', 'NM_002914 /// NM_181471', 'NM_002155', 'NM_003466 /// NM_013951 /// NM_013952 /// NM_013953 /// NM_013992', 'NM_000409'], 'Gene Ontology Biological Process': ['0001558 // regulation of cell growth // inferred from electronic annotation /// 0001952 // regulation of cell-matrix adhesion // inferred from electronic annotation /// 0006468 // protein amino acid phosphorylation // inferred from electronic annotation /// 0007155 // cell adhesion // inferred from electronic annotation /// 0007155 // cell adhesion // traceable author statement /// 0007169 // transmembrane receptor protein tyrosine kinase signaling pathway // inferred from electronic annotation /// 0007566 // embryo implantation // inferred from electronic annotation /// 0008285 // negative regulation of cell proliferation // inferred from electronic annotation /// 0018108 // peptidyl-tyrosine phosphorylation // inferred from electronic annotation /// 0031100 // organ regeneration // inferred from electronic annotation /// 0043583 // ear development // inferred from electronic annotation /// 0043588 // skin development // inferred from electronic annotation /// 0051789 // response to protein stimulus // inferred from electronic annotation /// 0060444 // branching involved in mammary gland duct morphogenesis // inferred from electronic annotation /// 0060749 // mammary gland alveolus development // inferred from electronic annotation', '0006260 // DNA replication // not recorded /// 0006260 // DNA replication // inferred from electronic annotation /// 0006297 // nucleotide-excision repair, DNA gap filling // not recorded /// 0015979 // photosynthesis // inferred from electronic annotation /// 0015995 // chlorophyll biosynthetic process // inferred from electronic annotation', '0006950 // response to stress // inferred from electronic annotation /// 0006986 // response to unfolded protein // traceable author statement', '0001656 // metanephros development // inferred from electronic annotation /// 0006350 // transcription // inferred from electronic annotation /// 0007275 // multicellular organismal development // inferred from electronic annotation /// 0009653 // anatomical structure morphogenesis // traceable author statement /// 0030154 // cell differentiation // inferred from electronic annotation /// 0030878 // thyroid gland development // inferred from electronic annotation /// 0045449 // regulation of transcription // inferred from electronic annotation /// 0045893 // positive regulation of transcription, DNA-dependent // inferred from sequence or structural similarity /// 0045893 // positive regulation of transcription, DNA-dependent // inferred from direct assay /// 0045944 // positive regulation of transcription from RNA polymerase II promoter // inferred from electronic annotation', '0007165 // signal transduction // non-traceable author statement /// 0007601 // visual perception // inferred from electronic annotation /// 0007601 // visual perception // traceable author statement /// 0007602 // phototransduction // inferred from electronic annotation /// 0031282 // regulation of guanylate cyclase activity // inferred from electronic annotation /// 0050896 // response to stimulus // inferred from electronic annotation'], 'Gene Ontology Cellular Component': ['0005576 // extracellular region // inferred from electronic annotation /// 0005886 // plasma membrane // inferred from electronic annotation /// 0005887 // integral to plasma membrane // traceable author statement /// 0016020 // membrane // inferred from electronic annotation /// 0016021 // integral to membrane // inferred from electronic annotation /// 0016323 // basolateral plasma membrane // inferred from electronic annotation', '0005634 // nucleus // inferred from electronic annotation /// 0005654 // nucleoplasm // not recorded /// 0005663 // DNA replication factor C complex // inferred from direct assay /// 0005663 // DNA replication factor C complex // inferred from electronic annotation', nan, '0005634 // nucleus // inferred from electronic annotation /// 0005654 // nucleoplasm // inferred from sequence or structural similarity /// 0005654 // nucleoplasm // inferred from electronic annotation', '0016020 // membrane // inferred from electronic annotation'], 'Gene Ontology Molecular Function': ['0000166 // nucleotide binding // inferred from electronic annotation /// 0004672 // protein kinase activity // inferred from electronic annotation /// 0004713 // protein tyrosine kinase activity // inferred from electronic annotation /// 0004714 // transmembrane receptor protein tyrosine kinase activity // inferred from electronic annotation /// 0004714 // transmembrane receptor protein tyrosine kinase activity // traceable author statement /// 0004872 // receptor activity // inferred from electronic annotation /// 0005515 // protein binding // inferred from physical interaction /// 0005515 // protein binding // inferred from electronic annotation /// 0005524 // ATP binding // inferred from electronic annotation /// 0016301 // kinase activity // inferred from electronic annotation /// 0016740 // transferase activity // inferred from electronic annotation', '0000166 // nucleotide binding // inferred from electronic annotation /// 0003677 // DNA binding // inferred from electronic annotation /// 0003689 // DNA clamp loader activity // inferred from electronic annotation /// 0005515 // protein binding // inferred from physical interaction /// 0005524 // ATP binding // inferred from electronic annotation /// 0005524 // ATP binding // traceable author statement /// 0016851 // magnesium chelatase activity // inferred from electronic annotation /// 0017111 // nucleoside-triphosphatase activity // inferred from electronic annotation', '0000166 // nucleotide binding // inferred from electronic annotation /// 0005524 // ATP binding // inferred from electronic annotation', '0003677 // DNA binding // inferred from direct assay /// 0003677 // DNA binding // inferred from electronic annotation /// 0003700 // transcription factor activity // traceable author statement /// 0004996 // thyroid-stimulating hormone receptor activity // traceable author statement /// 0005515 // protein binding // inferred from sequence or structural similarity /// 0005515 // protein binding // inferred from electronic annotation /// 0005515 // protein binding // inferred from physical interaction /// 0016563 // transcription activator activity // inferred from sequence or structural similarity /// 0016563 // transcription activator activity // inferred from direct assay /// 0016563 // transcription activator activity // inferred from electronic annotation /// 0043565 // sequence-specific DNA binding // inferred from electronic annotation', '0005509 // calcium ion binding // inferred from electronic annotation /// 0008048 // calcium sensitive guanylate cyclase activator activity // traceable author statement /// 0008048 // calcium sensitive guanylate cyclase activator activity // inferred from electronic annotation /// 0030249 // guanylate cyclase regulator activity // inferred from electronic annotation']}\n" ] } ], "source": [ "# 1. First get the file paths using geo_get_relevant_filepaths function\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. 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", "# 3. Use the 'preview_df' function from the library to preview the data and print out the results.\n", "print(\"Gene annotation preview:\")\n", "print(preview_df(gene_annotation))\n" ] }, { "cell_type": "markdown", "id": "1598ccdc", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "77cfe9de", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:34:07.026996Z", "iopub.status.busy": "2025-03-25T08:34:07.026878Z", "iopub.status.idle": "2025-03-25T08:34:07.597872Z", "shell.execute_reply": "2025-03-25T08:34:07.597547Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original probe data shape: 18989 probes × 148 samples\n", "Mapped gene data shape: 18989 genes × 148 samples\n", "\n", "First 10 gene symbols after mapping:\n", "Index(['A1BG', 'A1CF', 'A2BP1', 'A2LD1', 'A2M', 'A2ML1', 'A4GALT', 'A4GNT',\n", " 'AAA1', 'AAAS'],\n", " dtype='object', name='Gene')\n", "\n", "Preview of gene expression data after mapping:\n", "{'GSM6268367': [2.71, 6.53, 12.6, 6.15, 14.239999999999998], 'GSM6268368': [2.99, 8.16, 12.69, 6.15, 13.29], 'GSM6268369': [3.27, 6.86, 12.58, 7.17, 14.47], 'GSM6268370': [2.81, 8.51, 13.43, 6.57, 12.69], 'GSM6268371': [2.65, 8.66, 14.03, 6.19, 13.02], 'GSM6268372': [2.63, 7.27, 12.760000000000002, 6.52, 13.45], 'GSM6268373': [2.73, 8.3, 13.530000000000001, 6.13, 13.530000000000001], 'GSM6268374': [2.74, 8.21, 13.59, 6.619999999999999, 13.52], 'GSM6268375': [2.91, 7.81, 13.21, 6.03, 13.809999999999999], 'GSM6268376': [2.74, 8.43, 14.37, 6.51, 13.33], 'GSM6268377': [2.65, 8.33, 13.76, 6.779999999999999, 12.84], 'GSM6268378': [2.6, 8.3, 15.07, 6.8, 12.850000000000001], 'GSM6268379': [2.83, 8.06, 13.290000000000001, 6.46, 13.68], 'GSM6268380': [2.68, 8.12, 12.83, 7.4, 13.8], 'GSM6268381': [2.94, 8.49, 14.209999999999999, 6.75, 12.48], 'GSM6268382': [2.7, 8.4, 13.98, 6.220000000000001, 13.120000000000001], 'GSM6268383': [2.97, 7.87, 13.620000000000001, 6.2, 13.84], 'GSM6268384': [2.74, 8.27, 14.34, 6.41, 12.91], 'GSM6268385': [2.74, 8.08, 13.17, 6.47, 13.780000000000001], 'GSM6268386': [2.8, 8.25, 13.32, 6.55, 12.83], 'GSM6268387': [2.67, 8.16, 13.83, 6.57, 12.440000000000001], 'GSM6268388': [2.89, 8.4, 13.12, 6.42, 13.79], 'GSM6268389': [2.91, 8.09, 13.23, 6.800000000000001, 13.740000000000002], 'GSM6268390': [2.9, 7.57, 13.55, 6.62, 14.2], 'GSM6268391': [2.87, 7.78, 13.71, 6.66, 13.99], 'GSM6268392': [2.9, 8.16, 12.36, 6.220000000000001, 14.23], 'GSM6268393': [2.45, 8.24, 14.059999999999999, 6.4399999999999995, 14.05], 'GSM6268394': [2.81, 8.51, 13.129999999999999, 6.77, 12.94], 'GSM6268395': [3.03, 8.0, 14.52, 6.75, 13.68], 'GSM6268396': [3.05, 8.14, 13.48, 6.5600000000000005, 13.73], 'GSM6268397': [2.97, 7.79, 14.03, 6.57, 13.73], 'GSM6268398': [2.53, 8.01, 13.73, 6.12, 12.93], 'GSM6268399': [2.94, 8.1, 13.19, 7.1899999999999995, 13.42], 'GSM6268400': [2.68, 7.64, 12.8, 7.01, 13.73], 'GSM6268401': [3.21, 7.99, 13.28, 6.83, 12.850000000000001], 'GSM6268402': [2.82, 7.71, 12.8, 7.37, 13.350000000000001], 'GSM6268403': [2.8, 7.44, 12.870000000000001, 6.49, 14.829999999999998], 'GSM6268404': [2.72, 7.67, 13.84, 6.18, 13.24], 'GSM6268405': [2.61, 8.64, 13.54, 6.45, 12.370000000000001], 'GSM6268406': [3.03, 7.98, 13.27, 6.46, 12.41], 'GSM6268407': [3.21, 8.42, 12.64, 6.67, 13.09], 'GSM6268408': [3.28, 8.7, 12.83, 6.27, 12.959999999999999], 'GSM6268409': [2.8, 7.58, 12.1, 6.6, 14.27], 'GSM6268410': [2.63, 7.38, 13.05, 6.869999999999999, 14.73], 'GSM6268411': [3.06, 6.91, 12.57, 6.74, 13.94], 'GSM6268412': [2.96, 5.02, 12.56, 5.88, 14.68], 'GSM6268413': [3.09, 7.74, 13.56, 6.61, 12.57], 'GSM6268414': [2.65, 8.06, 12.879999999999999, 6.3100000000000005, 13.82], 'GSM6268415': [2.71, 8.29, 13.51, 7.369999999999999, 12.669999999999998], 'GSM6268416': [2.89, 7.45, 14.09, 6.21, 14.25], 'GSM6268417': [2.96, 8.46, 12.58, 6.390000000000001, 13.36], 'GSM6268418': [2.81, 8.29, 12.91, 6.5600000000000005, 13.18], 'GSM6268419': [2.65, 6.16, 12.49, 6.380000000000001, 13.030000000000001], 'GSM6268420': [3.15, 7.91, 13.620000000000001, 6.54, 13.47], 'GSM6268421': [2.7, 7.97, 14.31, 6.390000000000001, 12.57], 'GSM6268422': [2.84, 6.57, 12.93, 6.390000000000001, 14.169999999999998], 'GSM6268423': [2.83, 7.66, 15.92, 6.18, 12.77], 'GSM6268424': [2.59, 7.83, 13.629999999999999, 6.800000000000001, 13.1], 'GSM6268425': [2.62, 7.74, 13.440000000000001, 6.92, 12.95], 'GSM6268426': [2.75, 8.56, 12.97, 7.1, 11.940000000000001], 'GSM6268427': [2.86, 7.86, 13.25, 7.07, 13.07], 'GSM6268428': [2.82, 6.98, 13.649999999999999, 6.17, 13.29], 'GSM6268429': [3.02, 7.75, 13.47, 7.16, 13.2], 'GSM6268430': [2.99, 6.42, 12.55, 5.91, 14.71], 'GSM6268431': [2.68, 7.97, 14.05, 6.45, 13.13], 'GSM6268432': [2.9, 3.65, 13.09, 6.25, 11.05], 'GSM6268433': [2.69, 6.51, 12.73, 6.54, 13.41], 'GSM6268434': [2.88, 8.72, 13.08, 6.5600000000000005, 13.120000000000001], 'GSM6268435': [2.66, 8.07, 12.42, 6.72, 13.62], 'GSM6268436': [3.04, 8.48, 14.29, 6.34, 12.07], 'GSM6268437': [3.1, 5.77, 12.74, 6.3, 14.27], 'GSM6268438': [2.66, 8.31, 13.24, 6.59, 11.8], 'GSM6268439': [2.69, 8.58, 13.32, 5.88, 13.64], 'GSM6268440': [3.05, 7.88, 13.81, 6.470000000000001, 13.57], 'GSM6268441': [2.79, 7.45, 13.21, 6.390000000000001, 13.170000000000002], 'GSM6268442': [2.59, 7.91, 13.57, 6.41, 13.18], 'GSM6268443': [2.91, 3.8, 12.629999999999999, 6.4, 14.53], 'GSM6268444': [3.52, 8.23, 13.43, 6.78, 13.629999999999999], 'GSM6268445': [2.63, 8.27, 14.0, 6.24, 12.459999999999999], 'GSM6268446': [3.06, 8.2, 14.1, 6.630000000000001, 13.55], 'GSM6268447': [2.86, 8.03, 12.979999999999999, 6.779999999999999, 13.84], 'GSM6268448': [2.76, 7.38, 13.15, 6.279999999999999, 13.96], 'GSM6268449': [2.9, 8.01, 13.72, 6.01, 12.09], 'GSM6268450': [2.82, 7.69, 13.6, 6.43, 13.3], 'GSM6268451': [2.82, 7.26, 13.05, 6.66, 13.420000000000002], 'GSM6268452': [3.13, 7.68, 13.75, 6.0, 13.459999999999999], 'GSM6268453': [2.75, 7.81, 13.459999999999999, 6.45, 13.370000000000001], 'GSM6268454': [2.84, 7.35, 13.45, 6.609999999999999, 14.88], 'GSM6268455': [2.73, 7.83, 13.28, 5.890000000000001, 13.12], 'GSM6268456': [2.76, 7.26, 12.3, 6.75, 14.44], 'GSM6268457': [3.32, 7.47, 13.450000000000001, 7.33, 14.46], 'GSM6268458': [2.95, 8.37, 12.5, 6.470000000000001, 12.89], 'GSM6268459': [2.99, 8.55, 13.54, 6.220000000000001, 13.379999999999999], 'GSM6268460': [2.68, 7.9, 14.07, 6.87, 12.260000000000002], 'GSM6268461': [2.86, 7.47, 13.190000000000001, 6.5, 13.99], 'GSM6268462': [2.7, 7.25, 12.99, 6.529999999999999, 12.95], 'GSM6268463': [2.82, 8.24, 13.549999999999999, 6.17, 13.82], 'GSM6268464': [2.65, 8.64, 12.64, 6.39, 12.84], 'GSM6268465': [2.63, 7.74, 13.6, 6.470000000000001, 13.7], 'GSM6268466': [2.78, 8.22, 13.74, 6.26, 13.68], 'GSM6268467': [2.72, 8.45, 12.940000000000001, 6.3, 13.469999999999999], 'GSM6268468': [2.82, 7.63, 13.39, 6.59, 13.190000000000001], 'GSM6268469': [2.91, 7.89, 12.870000000000001, 6.199999999999999, 12.01], 'GSM6268470': [2.96, 7.04, 12.78, 6.54, 13.48], 'GSM6268471': [2.95, 8.31, 12.700000000000001, 6.52, 12.850000000000001], 'GSM6268472': [3.05, 5.96, 12.48, 6.42, 14.13], 'GSM6268473': [2.69, 7.73, 13.14, 6.73, 14.35], 'GSM6268474': [2.67, 7.72, 13.219999999999999, 6.67, 13.469999999999999], 'GSM6268475': [2.89, 8.18, 15.41, 7.279999999999999, 12.04], 'GSM6268476': [2.85, 7.82, 13.01, 6.0, 14.33], 'GSM6268477': [3.18, 7.96, 14.59, 6.41, 13.35], 'GSM6268478': [2.8, 7.73, 13.3, 6.34, 13.83], 'GSM6268479': [2.67, 8.03, 12.93, 6.45, 13.32], 'GSM6268480': [2.96, 8.3, 13.68, 6.75, 13.0], 'GSM6268481': [2.67, 8.57, 13.74, 6.6899999999999995, 13.0], 'GSM6268482': [2.67, 8.35, 13.41, 5.85, 13.57], 'GSM6268483': [2.87, 6.76, 12.53, 6.0, 14.39], 'GSM6268484': [2.79, 7.2, 12.63, 6.67, 13.8], 'GSM6268485': [2.93, 8.39, 12.66, 6.22, 13.2], 'GSM6268486': [2.83, 8.27, 12.62, 6.369999999999999, 13.509999999999998], 'GSM6268487': [2.87, 7.13, 13.23, 6.279999999999999, 13.75], 'GSM6268488': [2.71, 8.23, 14.3, 6.789999999999999, 10.920000000000002], 'GSM6268489': [2.71, 7.88, 13.540000000000001, 6.8, 13.42], 'GSM6268490': [2.52, 8.52, 13.100000000000001, 6.85, 13.290000000000001], 'GSM6268491': [2.7, 8.16, 13.43, 5.91, 13.02], 'GSM6268492': [3.03, 7.76, 14.18, 7.24, 14.29], 'GSM6268493': [2.73, 8.11, 13.38, 6.43, 12.18], 'GSM6268494': [3.01, 6.74, 14.459999999999999, 6.63, 13.330000000000002], 'GSM6268495': [2.99, 8.2, 13.18, 6.32, 13.43], 'GSM6268496': [2.71, 8.72, 13.47, 6.54, 12.59], 'GSM6268497': [2.72, 7.4, 13.17, 6.86, 14.06], 'GSM6268498': [2.66, 7.93, 13.39, 6.66, 13.26], 'GSM6268499': [3.0, 7.56, 12.57, 6.97, 13.86], 'GSM6268500': [2.68, 7.34, 13.79, 6.74, 12.98], 'GSM6268501': [2.96, 8.11, 12.66, 6.37, 13.469999999999999], 'GSM6268502': [2.62, 7.96, 13.219999999999999, 6.42, 13.129999999999999], 'GSM6268503': [3.15, 7.0, 12.77, 6.21, 14.77], 'GSM6268504': [3.15, 8.07, 14.370000000000001, 6.24, 13.08], 'GSM6268505': [2.74, 8.12, 13.100000000000001, 6.32, 13.79], 'GSM6268506': [2.92, 7.86, 13.4, 6.300000000000001, 12.94], 'GSM6268507': [2.94, 6.79, 12.97, 6.75, 14.0], 'GSM6268508': [2.94, 8.18, 14.0, 6.44, 13.82], 'GSM6268509': [2.82, 7.98, 13.56, 6.720000000000001, 13.23], 'GSM6268510': [2.89, 7.84, 13.16, 6.6899999999999995, 13.68], 'GSM6268511': [2.73, 8.45, 13.7, 6.29, 12.86], 'GSM6268512': [2.74, 8.19, 13.120000000000001, 6.9399999999999995, 12.370000000000001], 'GSM6268513': [2.93, 6.18, 13.03, 5.98, 15.200000000000001], 'GSM6268514': [2.84, 8.4, 12.61, 6.4, 11.92]}\n" ] } ], "source": [ "# 1. Observe gene identifiers in gene expression data and annotation data\n", "# From previous steps we see:\n", "# - Gene expression data has identifiers like '1007_PM_s_at' in the index\n", "# - Gene annotation data has 'ID' column with the same format identifiers\n", "# - Gene symbols are stored in the 'Gene Symbol' column in the annotation data\n", "\n", "# 2. Extract gene mapping from annotation dataframe\n", "mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Gene Symbol')\n", "\n", "# 3. Convert probe-level measurements to gene-level expression data\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "\n", "# Print out information about the mapping process\n", "print(f\"Original probe data shape: {gene_data.shape[0]} probes × {gene_data.shape[1]} samples\")\n", "print(f\"Mapped gene data shape: {gene_data.shape[0]} genes × {gene_data.shape[1]} samples\")\n", "print(\"\\nFirst 10 gene symbols after mapping:\")\n", "print(gene_data.index[:10])\n", "\n", "# Verify first few rows of the mapped gene data\n", "print(\"\\nPreview of gene expression data after mapping:\")\n", "print(preview_df(gene_data))\n" ] }, { "cell_type": "markdown", "id": "50b9c720", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "4b61ac1f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:34:07.599244Z", "iopub.status.busy": "2025-03-25T08:34:07.599136Z", "iopub.status.idle": "2025-03-25T08:34:22.495251Z", "shell.execute_reply": "2025-03-25T08:34:22.494868Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalizing gene symbols...\n", "Gene data shape after normalization: 18622 genes × 148 samples\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene expression data saved to ../../output/preprocess/Crohns_Disease/gene_data/GSE207022.csv\n", "Loading clinical features...\n", "Clinical features shape: (1, 30)\n", "Clinical features preview:\n", "{'CNTO1275CRD3002-20554': [1.0], 'CNTO1275CRD3002-20667': [1.0], 'CNTO1275CRD3002-20449': [1.0], 'CNTO1275CRD3002-20927': [1.0], 'CNTO1275CRD3002-20270': [1.0], 'CNTO1275CRD3002-20072': [1.0], 'CNTO1275CRD3002-20109': [1.0], 'CNTO1275CRD3002-20346': [1.0], 'HC-1': [0.0], 'HC-2': [0.0], 'HC-3': [0.0], 'HC-4': [0.0], 'HC-5': [0.0], 'HC-6': [0.0], 'HC-7': [0.0], 'HC-8': [0.0], 'HC-9': [0.0], 'HC-10': [0.0], 'HC-11': [0.0], 'HC-12': [0.0], 'HC-13': [0.0], 'HC-14': [0.0], 'HC-15': [0.0], 'HC-16': [0.0], 'HC-17': [0.0], 'HC-18': [0.0], 'HC-19': [0.0], 'HC-20': [0.0], 'HC-21': [0.0], 'HC-22': [0.0]}\n", "\n", "Gene data columns (first 5): ['GSM6268367', 'GSM6268368', 'GSM6268369', 'GSM6268370', 'GSM6268371']\n", "Clinical data rows: ['Crohns_Disease']\n", "Re-extracting clinical data from the original source...\n", "Re-extracted clinical features preview:\n", "{'GSM6268367': [1.0], 'GSM6268368': [1.0], 'GSM6268369': [1.0], 'GSM6268370': [1.0], 'GSM6268371': [1.0], 'GSM6268372': [1.0], 'GSM6268373': [1.0], 'GSM6268374': [1.0], 'GSM6268375': [0.0], 'GSM6268376': [0.0], 'GSM6268377': [0.0], 'GSM6268378': [0.0], 'GSM6268379': [0.0], 'GSM6268380': [0.0], 'GSM6268381': [0.0], 'GSM6268382': [0.0], 'GSM6268383': [0.0], 'GSM6268384': [0.0], 'GSM6268385': [0.0], 'GSM6268386': [0.0], 'GSM6268387': [0.0], 'GSM6268388': [0.0], 'GSM6268389': [0.0], 'GSM6268390': [0.0], 'GSM6268391': [0.0], 'GSM6268392': [0.0], 'GSM6268393': [0.0], 'GSM6268394': [0.0], 'GSM6268395': [0.0], 'GSM6268396': [0.0], 'GSM6268397': [0.0], 'GSM6268398': [1.0], 'GSM6268399': [1.0], 'GSM6268400': [1.0], 'GSM6268401': [1.0], 'GSM6268402': [1.0], 'GSM6268403': [1.0], 'GSM6268404': [1.0], 'GSM6268405': [1.0], 'GSM6268406': [1.0], 'GSM6268407': [1.0], 'GSM6268408': [1.0], 'GSM6268409': [1.0], 'GSM6268410': [1.0], 'GSM6268411': [1.0], 'GSM6268412': [1.0], 'GSM6268413': [1.0], 'GSM6268414': [1.0], 'GSM6268415': [1.0], 'GSM6268416': [1.0], 'GSM6268417': [1.0], 'GSM6268418': [1.0], 'GSM6268419': [1.0], 'GSM6268420': [1.0], 'GSM6268421': [1.0], 'GSM6268422': [1.0], 'GSM6268423': [1.0], 'GSM6268424': [1.0], 'GSM6268425': [1.0], 'GSM6268426': [1.0], 'GSM6268427': [1.0], 'GSM6268428': [1.0], 'GSM6268429': [1.0], 'GSM6268430': [1.0], 'GSM6268431': [1.0], 'GSM6268432': [1.0], 'GSM6268433': [1.0], 'GSM6268434': [1.0], 'GSM6268435': [1.0], 'GSM6268436': [1.0], 'GSM6268437': [1.0], 'GSM6268438': [1.0], 'GSM6268439': [1.0], 'GSM6268440': [1.0], 'GSM6268441': [1.0], 'GSM6268442': [1.0], 'GSM6268443': [1.0], 'GSM6268444': [1.0], 'GSM6268445': [1.0], 'GSM6268446': [1.0], 'GSM6268447': [1.0], 'GSM6268448': [1.0], 'GSM6268449': [1.0], 'GSM6268450': [1.0], 'GSM6268451': [1.0], 'GSM6268452': [1.0], 'GSM6268453': [1.0], 'GSM6268454': [1.0], 'GSM6268455': [1.0], 'GSM6268456': [1.0], 'GSM6268457': [1.0], 'GSM6268458': [1.0], 'GSM6268459': [1.0], 'GSM6268460': [1.0], 'GSM6268461': [1.0], 'GSM6268462': [1.0], 'GSM6268463': [1.0], 'GSM6268464': [1.0], 'GSM6268465': [1.0], 'GSM6268466': [1.0], 'GSM6268467': [1.0], 'GSM6268468': [1.0], 'GSM6268469': [1.0], 'GSM6268470': [1.0], 'GSM6268471': [1.0], 'GSM6268472': [1.0], 'GSM6268473': [1.0], 'GSM6268474': [1.0], 'GSM6268475': [1.0], 'GSM6268476': [1.0], 'GSM6268477': [1.0], 'GSM6268478': [1.0], 'GSM6268479': [1.0], 'GSM6268480': [1.0], 'GSM6268481': [1.0], 'GSM6268482': [1.0], 'GSM6268483': [1.0], 'GSM6268484': [1.0], 'GSM6268485': [1.0], 'GSM6268486': [1.0], 'GSM6268487': [1.0], 'GSM6268488': [1.0], 'GSM6268489': [1.0], 'GSM6268490': [1.0], 'GSM6268491': [1.0], 'GSM6268492': [1.0], 'GSM6268493': [1.0], 'GSM6268494': [1.0], 'GSM6268495': [1.0], 'GSM6268496': [1.0], 'GSM6268497': [1.0], 'GSM6268498': [1.0], 'GSM6268499': [1.0], 'GSM6268500': [1.0], 'GSM6268501': [1.0], 'GSM6268502': [1.0], 'GSM6268503': [1.0], 'GSM6268504': [1.0], 'GSM6268505': [1.0], 'GSM6268506': [1.0], 'GSM6268507': [1.0], 'GSM6268508': [1.0], 'GSM6268509': [1.0], 'GSM6268510': [1.0], 'GSM6268511': [1.0], 'GSM6268512': [1.0], 'GSM6268513': [1.0], 'GSM6268514': [1.0]}\n", "Re-extracted clinical data shape: (1, 148)\n", "Updated clinical features saved to ../../output/preprocess/Crohns_Disease/clinical_data/GSE207022.csv\n", "Linking clinical and genetic data...\n", "Linked data shape: (148, 18623)\n", "Handling missing values...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (148, 18623)\n", "\n", "Checking for bias in feature variables:\n", "For the feature 'Crohns_Disease', the least common label is '0.0' with 23 occurrences. This represents 15.54% of the dataset.\n", "The distribution of the feature 'Crohns_Disease' in this dataset is fine.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Crohns_Disease/GSE207022.csv\n", "Final dataset shape: (148, 18623)\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "print(\"Normalizing gene symbols...\")\n", "gene_data_normalized = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Gene data shape after normalization: {gene_data_normalized.shape[0]} genes × {gene_data_normalized.shape[1]} samples\")\n", "\n", "# Save the normalized gene data\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "gene_data_normalized.to_csv(out_gene_data_file)\n", "print(f\"Normalized gene expression data saved to {out_gene_data_file}\")\n", "\n", "# 2. Read the clinical features from the previously saved file\n", "print(\"Loading clinical features...\")\n", "clinical_features = pd.read_csv(out_clinical_data_file, index_col=0)\n", "print(f\"Clinical features shape: {clinical_features.shape}\")\n", "print(\"Clinical features preview:\")\n", "print(preview_df(clinical_features))\n", "\n", "# First, let's look at the column names of both datasets to ensure proper linking\n", "print(\"\\nGene data columns (first 5):\", gene_data_normalized.columns[:5].tolist())\n", "print(\"Clinical data rows:\", clinical_features.index.tolist())\n", "\n", "# Since we've detected issues with data linking, let's manually inspect the data formats\n", "# and make necessary adjustments for proper alignment\n", "if clinical_features.shape[0] == 0:\n", " print(\"Error: Clinical features dataframe is empty. Cannot proceed with linking.\")\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,\n", " df=pd.DataFrame(),\n", " note=\"Clinical features dataframe is empty, cannot link with gene data.\"\n", " )\n", "else:\n", " # Re-extract the clinical data directly from the matrix file\n", " print(\"Re-extracting clinical data from the original source...\")\n", " # Get background information and clinical data again\n", " background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']\n", " clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']\n", " background_info, original_clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)\n", " \n", " # Extract clinical features properly\n", " selected_clinical_df = geo_select_clinical_features(\n", " clinical_df=original_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(\"Re-extracted clinical features preview:\")\n", " print(preview_df(selected_clinical_df))\n", " print(f\"Re-extracted clinical data shape: {selected_clinical_df.shape}\")\n", " \n", " # Save the properly extracted clinical features\n", " selected_clinical_df.to_csv(out_clinical_data_file)\n", " print(f\"Updated clinical features saved to {out_clinical_data_file}\")\n", " \n", " # 2. Link clinical and genetic data using the re-extracted clinical data\n", " print(\"Linking clinical and genetic data...\")\n", " linked_data = geo_link_clinical_genetic_data(selected_clinical_df, gene_data_normalized)\n", " print(f\"Linked data shape: {linked_data.shape}\")\n", " \n", " # Check if the linked data has adequate data\n", " if linked_data.shape[0] == 0 or linked_data.shape[1] <= 4: # 4 is an arbitrary small number\n", " print(\"Error: Linked data has insufficient samples or features. Dataset cannot be processed further.\")\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=True,\n", " df=linked_data,\n", " note=\"Failed to properly link gene expression data with clinical features.\"\n", " )\n", " else:\n", " # 3. Handle missing values systematically\n", " print(\"Handling missing values...\")\n", " linked_data_clean = handle_missing_values(linked_data, trait_col=trait)\n", " print(f\"Data shape after handling missing values: {linked_data_clean.shape}\")\n", " \n", " # Check if there are still samples after missing value handling\n", " if linked_data_clean.shape[0] == 0:\n", " print(\"Error: No samples remain after handling missing values.\")\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=True,\n", " df=pd.DataFrame(),\n", " note=\"All samples were removed during missing value handling.\"\n", " )\n", " else:\n", " # 4. Check if the dataset is biased\n", " print(\"\\nChecking for bias in feature variables:\")\n", " is_biased, linked_data_final = judge_and_remove_biased_features(linked_data_clean, trait)\n", " \n", " # 5. Conduct final quality validation\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_final,\n", " note=\"Dataset contains gene expression data for Crohn's Disease subtypes (penetrating vs stricturing).\"\n", " )\n", " \n", " # 6. Save linked data if usable\n", " if is_usable:\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " linked_data_final.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", " print(f\"Final dataset shape: {linked_data_final.shape}\")\n", " else:\n", " print(\"Dataset deemed not usable for trait association 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 }