{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "c6a160ff", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:20:06.743672Z", "iopub.status.busy": "2025-03-25T08:20:06.743321Z", "iopub.status.idle": "2025-03-25T08:20:06.909471Z", "shell.execute_reply": "2025-03-25T08:20:06.909025Z" } }, "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 = \"Chronic_obstructive_pulmonary_disease_(COPD)\"\n", "cohort = \"GSE175616\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Chronic_obstructive_pulmonary_disease_(COPD)\"\n", "in_cohort_dir = \"../../input/GEO/Chronic_obstructive_pulmonary_disease_(COPD)/GSE175616\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Chronic_obstructive_pulmonary_disease_(COPD)/GSE175616.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Chronic_obstructive_pulmonary_disease_(COPD)/gene_data/GSE175616.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Chronic_obstructive_pulmonary_disease_(COPD)/clinical_data/GSE175616.csv\"\n", "json_path = \"../../output/preprocess/Chronic_obstructive_pulmonary_disease_(COPD)/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "50dbef14", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "72997c53", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:20:06.910882Z", "iopub.status.busy": "2025-03-25T08:20:06.910737Z", "iopub.status.idle": "2025-03-25T08:20:07.090075Z", "shell.execute_reply": "2025-03-25T08:20:07.089624Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Clinical study of the effect of combined treatment of aspirin and zileuton on biomarkers of tobacco-related carcinogenesis in current smokers\"\n", "!Series_summary\t\"A chemopreventive effect of aspirin (ASA) on lung cancer risk is supported by epidemiologic and preclinical studies. Zileuton, a 5-LOX inhibitor has single agent activity and adds to the activity of NSAIDs in preclinical models of tobacco carcinogenesis We hypothesized that COX inhibitor + 5-LOX inhibitor may be more effective than placebo in modulating nasal epithelium gene signatures of tobacco exposure and lung cancer. We conducted a randomized, double-blinded study of low dose ASA plus zileuton vs. double placebo in current smokers to compare modulating effects on nasal epithelium gene expression and arachidonic acid (AA) metabolism. Sixty-three participants were randomized to combined treatment of ASA (81 mg QD) and zileuton (Zyflo CR) two 600 mg extended release tablets BID or placebo pills for 12 weeks. Combined ASA plus zileuton had minimal effects on nasal gene expression of nasal or bronchial gene expression signatures associated with smoking, lung cancer and chronic obstructive pulmonary disease but did favorably modulate a bronchial gene signature of squamous dysplasia. Combined ASA plus zileuton suppressed urinary leukotriene (LTE4) (change of 89.867±68.35 from baseline to 32.25±23.25, p <0.001), a surrogate of 5-LOX mediated AA metabolism but did not suppress urinary prostaglandin E2 metabolite (PGEM), a surrogate of cyclooxygenase-mediated AA metabolism.\"\n", "!Series_summary\t\"In conclusion, combined COX and 5-LOX inhibition by combined low dose ASA with zileuton in smokers favorably modulated a bronchial squamous dysplasia gene expression signature in the nasal epithelium of current smokers but had minimal effects on other carcinogenesis gene signatures. This combination decreased 5-LOX but not COX-2 mediated AA metabolism. Nasal gene expression signature determination is a novel approach to biomarker analysis, giving an approximation of the pulmonary milieu without having to perform invasive tissue sampling.\"\n", "!Series_overall_design\t\"The study was a single center randomized, double-blinded, placebo controlled trial to determine the modulatory effects of combined treatment of ASA and zileuton on nasal epithelium gene expression and arachidonic acid metabolism in current smokers\"\n", "Sample Characteristics Dictionary:\n", "{0: ['included in analysis: TRUE'], 1: ['patient: ZIL01001', 'patient: ZIL01005', 'patient: ZIL01008', 'patient: ZIL01009', 'patient: ZIL01011', 'patient: ZIL01013', 'patient: ZIL01014', 'patient: ZIL01017', 'patient: ZIL01020', 'patient: ZIL01022', 'patient: ZIL01028', 'patient: ZIL01029', 'patient: ZIL01035', 'patient: ZIL01038', 'patient: ZIL01042', 'patient: ZIL01043', 'patient: ZIL01045', 'patient: ZIL01055', 'patient: ZIL01058', 'patient: ZIL01061', 'patient: ZIL01063', 'patient: ZIL01068', 'patient: ZIL01070', 'patient: ZIL01071', 'patient: ZIL01074', 'patient: ZIL01077', 'patient: ZIL01092', 'patient: ZIL01102', 'patient: ZIL01103', 'patient: ZIL01104'], 2: ['visit: visit 2', 'visit: visit 5', 'visit: visit 6'], 3: ['treatment: Placebo', 'treatment: Aspirin+Zileuton'], 4: ['site: UAZ Tucson', 'site: UAZ Phoenix'], 5: ['Sex: male', 'Sex: female'], 6: ['age: 35', 'age: 63', 'age: 50', 'age: 60', 'age: 52', 'age: 45', 'age: 71', 'age: 46', 'age: 65', 'age: 53', 'age: 47', 'age: 44', 'age: 55', 'age: 54', 'age: 48', 'age: 51', 'age: 37', 'age: 58', 'age: 56', 'age: 38', 'age: 64', 'age: 69', 'age: 66', 'age: 57', 'age: 72', 'age: 49'], 7: ['smoking status: current smoker'], 8: ['pack years: 23', 'pack years: 33', 'pack years: 38', 'pack years: 47', 'pack years: 27', 'pack years: 28', 'pack years: 25', 'pack years: 40', 'pack years: 31', 'pack years: 52', 'pack years: 35', 'pack years: 65', 'pack years: 62', 'pack years: 26', 'pack years: 32', 'pack years: 56', 'pack years: 36', 'pack years: 39', 'pack years: 30', 'pack years: 21', 'pack years: 60', 'pack years: 44', 'pack years: 20', 'pack years: 42'], 9: ['urinary cotinine: creatinine: 5095.54', 'urinary cotinine: creatinine: 7168.83', 'urinary cotinine: creatinine: 4748.39', 'urinary cotinine: creatinine: 5333.33', 'urinary cotinine: creatinine: 5140.98', 'urinary cotinine: creatinine: 5520', 'urinary cotinine: creatinine: 3537.93', 'urinary cotinine: creatinine: 3581.15', 'urinary cotinine: creatinine: 1314.29', 'urinary cotinine: creatinine: BLQ (< 5 ng/nL)', 'urinary cotinine: creatinine: 900', 'urinary cotinine: creatinine: 537.17', 'urinary cotinine: creatinine: 1075.47', 'urinary cotinine: creatinine: 6.2', 'urinary cotinine: creatinine: 9.73', 'urinary cotinine: creatinine: 8.86', 'urinary cotinine: creatinine: 5606.56', 'urinary cotinine: creatinine: 7772.73', 'urinary cotinine: creatinine: 8321.17', 'urinary cotinine: creatinine: 5518.47', 'urinary cotinine: creatinine: 2026.67', 'urinary cotinine: creatinine: 1378.13', 'urinary cotinine: creatinine: 7651.01', 'urinary cotinine: creatinine: 1148.51', 'urinary cotinine: creatinine: 7215.19', 'urinary cotinine: creatinine: 2622', 'urinary cotinine: creatinine: 1672.44', 'urinary cotinine: creatinine: 7612.9', 'urinary cotinine: creatinine: 7108.43', 'urinary cotinine: creatinine: 5429.45'], 10: ['rin: 6.7', 'rin: 7', 'rin: 5.8', 'rin: 4', 'rin: 3', 'rin: 6.5', 'rin: 6.6', 'rin: 5.7', 'rin: 7.2', 'rin: 6.3', 'rin: 5.1', 'rin: 4.2', 'rin: 7.1', 'rin: 3.8', 'rin: 7.6', 'rin: 7.9', 'rin: 3.3', 'rin: 5.4', 'rin: 8.1', 'rin: 8', 'rin: 8.5', 'rin: 7.7', 'rin: 6', 'rin: 8.3', 'rin: 2.6', 'rin: 3.4', 'rin: 4.5', 'rin: 7.8', 'rin: 2.5', 'rin: 6.4'], 11: ['dv200: 71', 'dv200: 68', 'dv200: 66', 'dv200: 69', 'dv200: 65', 'dv200: 75', 'dv200: 67', 'dv200: 72', 'dv200: 64', 'dv200: 73', 'dv200: 63', 'dv200: 78', 'dv200: 80', 'dv200: 62', 'dv200: 44', 'dv200: 55', 'dv200: 56', 'dv200: 76', 'dv200: 79', 'dv200: 70', 'dv200: 81', 'dv200: 74', 'dv200: 57', 'dv200: NA', 'dv200: 77', 'dv200: 31', 'dv200: 83', 'dv200: 82'], 12: ['mean rin: 6.5', 'mean rin: 4.5', 'mean rin: 6.3', 'mean rin: 6.666666667', 'mean rin: 4.433333333', 'mean rin: 5.833333333', 'mean rin: 7.533333333', 'mean rin: 4.366666667', 'mean rin: 3.533333333', 'mean rin: 8.2', 'mean rin: 6.9', 'mean rin: 6.933333333', 'mean rin: 8.3', 'mean rin: 7.6', 'mean rin: 2.8', 'mean rin: 6.8', 'mean rin: 7.566666667', 'mean rin: 7.1', 'mean rin: 4.966666667', 'mean rin: 7.066666667', 'mean rin: 5.733333333', 'mean rin: 6.366666667', 'mean rin: 4.533333333', 'mean rin: 5.7', 'mean rin: 7.4', 'mean rin: 6.833333333', 'mean rin: 3.433333333', 'mean rin: 7.233333333', 'mean rin: 6.533333333', 'mean rin: 4.8'], 13: ['batch: 1', 'batch: 2', 'batch: 3'], 14: ['lte4 pg/ml: 48.782', 'lte4 pg/ml: 19.018', 'lte4 pg/ml: 37.334', 'lte4 pg/ml: 76.441', 'lte4 pg/ml: 32.643', 'lte4 pg/ml: 95.078', 'lte4 pg/ml: 87.315', 'lte4 pg/ml: 30.793', 'lte4 pg/ml: 116.707', 'lte4 pg/ml: 78.654', 'lte4 pg/ml: 86.703', 'lte4 pg/ml: 190.304', 'lte4 pg/ml: 49.551', 'lte4 pg/ml: 56.359', 'lte4 pg/ml: 165.85', 'lte4 pg/ml: 49.515', 'lte4 pg/ml: 287.782', 'lte4 pg/ml: 67.852', 'lte4 pg/ml: 142.957', 'lte4 pg/ml: 73.08', 'lte4 pg/ml: 133.287', 'lte4 pg/ml: 45.603', 'lte4 pg/ml: 30.479', 'lte4 pg/ml: 29.283', 'lte4 pg/ml: 73.971', 'lte4 pg/ml: 85.21', 'lte4 pg/ml: 84.895', 'lte4 pg/ml: 81.64', 'lte4 pg/ml: 110.813', 'lte4 pg/ml: 34.703'], 15: ['pgem ng/ml: 20.059', 'pgem ng/ml: 23.856', 'pgem ng/ml: 38.782', 'pgem ng/ml: 16.034', 'pgem ng/ml: 12.631', 'pgem ng/ml: 18.809', 'pgem ng/ml: 23.669', 'pgem ng/ml: 28.236', 'pgem ng/ml: 17.937', 'pgem ng/ml: 16.211', 'pgem ng/ml: 26.445', 'pgem ng/ml: 10.169', 'pgem ng/ml: 29.384', 'pgem ng/ml: 11.545', 'pgem ng/ml: 18.787', 'pgem ng/ml: 15.47', 'pgem ng/ml: 27.109', 'pgem ng/ml: 24.808', 'pgem ng/ml: 17.218', 'pgem ng/ml: 11.574', 'pgem ng/ml: 17.056', 'pgem ng/ml: 4.134', 'pgem ng/ml: 7.756', 'pgem ng/ml: 16.252', 'pgem ng/ml: 12.363', 'pgem ng/ml: 17.439', 'pgem ng/ml: 12.883', 'pgem ng/ml: 14.392', 'pgem ng/ml: 15.19', 'pgem ng/ml: 12.697']}\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": "034b2dd8", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "51b0ca90", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:20:07.091177Z", "iopub.status.busy": "2025-03-25T08:20:07.091064Z", "iopub.status.idle": "2025-03-25T08:20:07.108833Z", "shell.execute_reply": "2025-03-25T08:20:07.108496Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A new JSON file was created at: ../../output/preprocess/Chronic_obstructive_pulmonary_disease_(COPD)/cohort_info.json\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the series title and summary, this dataset appears to be focused on gene expression\n", "# from nasal epithelium of smokers. It mentions gene expression signatures, which indicates\n", "# gene expression data should be available.\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability for trait (COPD)\n", "# Looking at the sample characteristics dictionary, there's no direct mention of COPD.\n", "# From the background information, it mentions that all participants are current smokers and\n", "# the study mentions \"chronic obstructive pulmonary disease\" as one of the signatures being studied,\n", "# but it doesn't classify patients as having COPD or not.\n", "trait_row = None # COPD status is not explicitly available in the data\n", "\n", "# 2.1 Data Availability for age\n", "# Age is available at index 6\n", "age_row = 6\n", "\n", "# 2.1 Data Availability for gender\n", "# Gender (Sex) is available at index 5\n", "gender_row = 5\n", "\n", "# 2.2 Data Type Conversion for trait\n", "def convert_trait(value):\n", " # Since trait data is not available, this function won't be used\n", " # But we'll define it for completeness\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " # No specific conversion logic since trait data not present\n", " return None\n", "\n", "# 2.2 Data Type Conversion for age\n", "def convert_age(value):\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " # Convert to integer if possible\n", " try:\n", " return int(value)\n", " except:\n", " return None\n", "\n", "# 2.2 Data Type Conversion for gender\n", "def convert_gender(value):\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip().lower()\n", " \n", " # Convert to binary (0 for female, 1 for male)\n", " if value == \"female\":\n", " return 0\n", " elif value == \"male\":\n", " return 1\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Initial filtering based on trait and gene data availability\n", "is_trait_available = trait_row is not None\n", "validate_and_save_cohort_info(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", "# 4. Clinical Feature Extraction\n", "# Since trait_row is None, we skip this substep\n" ] }, { "cell_type": "markdown", "id": "96359467", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "a29158ad", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:20:07.109876Z", "iopub.status.busy": "2025-03-25T08:20:07.109768Z", "iopub.status.idle": "2025-03-25T08:20:07.399498Z", "shell.execute_reply": "2025-03-25T08:20:07.399023Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix file found: ../../input/GEO/Chronic_obstructive_pulmonary_disease_(COPD)/GSE175616/GSE175616_series_matrix.txt.gz\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape: (20777, 123)\n", "First 20 gene/probe identifiers:\n", "Index(['100009676_at', '10000_at', '10001_at', '10002_at', '100033413_at',\n", " '100033414_at', '100033418_at', '100033420_at', '100033422_at',\n", " '100033423_at', '100033424_at', '100033425_at', '100033426_at',\n", " '100033427_at', '100033428_at', '100033430_at', '100033431_at',\n", " '100033432_at', '100033433_at', '100033434_at'],\n", " dtype='object', name='ID')\n" ] } ], "source": [ "# 1. Get the SOFT and matrix file paths again \n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "print(f\"Matrix file found: {matrix_file}\")\n", "\n", "# 2. Use the get_genetic_data function from the library to get the gene_data\n", "try:\n", " gene_data = get_genetic_data(matrix_file)\n", " print(f\"Gene data shape: {gene_data.shape}\")\n", " \n", " # 3. Print the first 20 row IDs (gene or probe identifiers)\n", " print(\"First 20 gene/probe identifiers:\")\n", " print(gene_data.index[:20])\n", "except Exception as e:\n", " print(f\"Error extracting gene data: {e}\")\n" ] }, { "cell_type": "markdown", "id": "5ffd6488", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "74b3c58c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:20:07.401319Z", "iopub.status.busy": "2025-03-25T08:20:07.401190Z", "iopub.status.idle": "2025-03-25T08:20:07.403540Z", "shell.execute_reply": "2025-03-25T08:20:07.403090Z" } }, "outputs": [], "source": [ "# Analyzing the gene identifiers in the gene expression data\n", "# These identifiers (e.g., '100009676_at', '10000_at') are Affymetrix probe IDs\n", "# They're not standard human gene symbols and need to be mapped to gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "6d821170", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "19375149", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:20:07.405308Z", "iopub.status.busy": "2025-03-25T08:20:07.405184Z", "iopub.status.idle": "2025-03-25T08:20:11.230121Z", "shell.execute_reply": "2025-03-25T08:20:11.229413Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene annotation preview:\n", "Columns in gene annotation: ['ID', 'SPOT_ID', 'DESCRIPTION']\n", "{'ID': ['1_at', '10_at', '100_at', '1000_at', '10000_at'], 'SPOT_ID': ['1', '10', '100', '1000', '10000'], 'DESCRIPTION': ['alpha-1-B glycoprotein', 'N-acetyltransferase 2', 'adenosine deaminase', 'cadherin 2', 'AKT serine/threonine kinase 3']}\n", "\n", "Searching for platform information in SOFT file:\n", "Platform ID not found in first 100 lines\n", "\n", "Searching for gene symbol information in SOFT file:\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "No explicit gene symbol references found in first 1000 lines\n", "\n", "Checking for additional annotation files in the directory:\n", "[]\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", "# Let's look for platform information in the SOFT file to understand the annotation better\n", "print(\"\\nSearching for platform information in SOFT file:\")\n", "with gzip.open(soft_file, 'rt') as f:\n", " for i, line in enumerate(f):\n", " if '!Series_platform_id' in line:\n", " print(line.strip())\n", " break\n", " if i > 100: # Limit search to first 100 lines\n", " print(\"Platform ID not found in first 100 lines\")\n", " break\n", "\n", "# Check if the SOFT file includes any reference to gene symbols\n", "print(\"\\nSearching for gene symbol information in SOFT file:\")\n", "with gzip.open(soft_file, 'rt') as f:\n", " gene_symbol_lines = []\n", " for i, line in enumerate(f):\n", " if 'GENE_SYMBOL' in line or 'gene_symbol' in line.lower() or 'symbol' in line.lower():\n", " gene_symbol_lines.append(line.strip())\n", " if i > 1000 and len(gene_symbol_lines) > 0: # Limit search but ensure we found something\n", " break\n", " \n", " if gene_symbol_lines:\n", " print(\"Found references to gene symbols:\")\n", " for line in gene_symbol_lines[:5]: # Show just first 5 matches\n", " print(line)\n", " else:\n", " print(\"No explicit gene symbol references found in first 1000 lines\")\n", "\n", "# Look for alternative annotation files or references in the directory\n", "print(\"\\nChecking for additional annotation files in the directory:\")\n", "all_files = os.listdir(in_cohort_dir)\n", "print([f for f in all_files if 'annotation' in f.lower() or 'platform' in f.lower() or 'gpl' in f.lower()])\n" ] }, { "cell_type": "markdown", "id": "75f8429e", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "d2396c19", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:20:11.232060Z", "iopub.status.busy": "2025-03-25T08:20:11.231921Z", "iopub.status.idle": "2025-03-25T08:20:11.614858Z", "shell.execute_reply": "2025-03-25T08:20:11.614328Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene mapping dataframe shape: (20770, 2)\n", "Gene mapping preview:\n", "{'ID': ['1_at', '10_at', '100_at', '1000_at', '10000_at'], 'Gene': ['alpha-1-B glycoprotein', 'N-acetyltransferase 2', 'adenosine deaminase', 'cadherin 2', 'AKT serine/threonine kinase 3']}\n", "Gene expression data shape after mapping: (2377, 123)\n", "First few gene symbols after mapping:\n", "['A-', 'A-52', 'A0', 'A1', 'A1-', 'A10', 'A11', 'A12', 'A13', 'A14']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data shape after normalization: (1639, 123)\n", "First few normalized gene symbols:\n", "['A4GALT', 'AAA1', 'AAR2', 'ABCC11', 'ABCE1', 'ABO', 'ABR', 'ABRA', 'ACD', 'ACOXL']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Chronic_obstructive_pulmonary_disease_(COPD)/gene_data/GSE175616.csv\n" ] } ], "source": [ "# 1. Determine which columns in gene annotation contain identifiers and gene symbols\n", "# From the preview, we can see:\n", "# - 'ID' column in gene_annotation contains identifiers like '1_at', '10_at', which match the format in gene_data\n", "# - 'DESCRIPTION' column appears to contain the gene symbols/names like 'alpha-1-B glycoprotein'\n", "\n", "# 2. Extract gene mapping dataframe with the relevant columns\n", "mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='DESCRIPTION')\n", "print(f\"Gene mapping dataframe shape: {mapping_df.shape}\")\n", "print(\"Gene mapping preview:\")\n", "print(preview_df(mapping_df, n=5))\n", "\n", "# 3. Apply gene mapping to convert probe-level measurements to gene expression data\n", "# The apply_gene_mapping function will:\n", "# - Divide probe values equally among mapped genes\n", "# - Sum values across all probes for each gene\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "print(f\"Gene expression data shape after mapping: {gene_data.shape}\")\n", "print(\"First few gene symbols after mapping:\")\n", "print(gene_data.index[:10].tolist())\n", "\n", "# Normalize gene symbols to standard format\n", "gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Gene expression data shape after normalization: {gene_data.shape}\")\n", "print(\"First few normalized gene symbols:\")\n", "print(gene_data.index[:10].tolist())\n", "\n", "# Save the processed gene data\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "gene_data.to_csv(out_gene_data_file)\n", "print(f\"Gene expression data saved to {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "a3658288", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "70ce194e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:20:11.616782Z", "iopub.status.busy": "2025-03-25T08:20:11.616630Z", "iopub.status.idle": "2025-03-25T08:20:11.931658Z", "shell.execute_reply": "2025-03-25T08:20:11.931141Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data shape: (1639, 123)\n", "Gene data column names (sample IDs):\n", "Index(['GSM5342551', 'GSM5342552', 'GSM5342553', 'GSM5342554', 'GSM5342555'], dtype='object')\n", "\n", "No trait information available for COPD in this dataset\n", "Created clinical features dataframe with demographic info only:\n", "Shape: (2, 123)\n", " GSM5342551 GSM5342552 GSM5342553 GSM5342554 GSM5342555\n", "Age 35.0 35.0 35.0 63.0 63.0\n", "Gender 1.0 1.0 1.0 1.0 1.0\n", "\n", "Linked data shape before handling missing values: (123, 1641)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data shape after handling missing values: (123, 1641)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Quartiles for 'placeholder_trait':\n", " 25%: 1.0\n", " 50% (Median): 1.0\n", " 75%: 1.0\n", "Min: 1\n", "Max: 1\n", "The distribution of the feature 'placeholder_trait' in this dataset is severely biased.\n", "\n", "Quartiles for 'Age':\n", " 25%: 47.5\n", " 50% (Median): 52.0\n", " 75%: 57.0\n", "Min: 35.0\n", "Max: 72.0\n", "The distribution of the feature 'Age' in this dataset is fine.\n", "\n", "For the feature 'Gender', the least common label is '0.0' with 60 occurrences. This represents 48.78% of the dataset.\n", "The distribution of the feature 'Gender' in this dataset is fine.\n", "\n", "Dataset deemed not usable due to quality issues - linked data not saved\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data \n", "# (This was already done in the previous step, so no need to repeat)\n", "print(f\"Normalized gene data shape: {gene_data.shape}\")\n", "\n", "# 2. Examine the sample IDs in the gene expression data to understand the structure\n", "print(\"Gene data column names (sample IDs):\")\n", "print(gene_data.columns[:5]) # Print first 5 for brevity\n", "\n", "# Since trait information is not available (trait_row is None), we skip clinical feature extraction\n", "# and proceed directly with gene expression data\n", "print(\"\\nNo trait information available for COPD in this dataset\")\n", "\n", "# Create clinical features with age and gender information only\n", "sample_ids = gene_data.columns.tolist()\n", "\n", "# Extract age data if available\n", "clinical_features_list = []\n", "if age_row is not None:\n", " age_data = get_feature_data(clinical_data, age_row, 'Age', convert_age)\n", " clinical_features_list.append(age_data)\n", " \n", "# Extract gender data if available\n", "if gender_row is not None:\n", " gender_data = get_feature_data(clinical_data, gender_row, 'Gender', convert_gender)\n", " clinical_features_list.append(gender_data)\n", "\n", "# If we have any clinical features, create the dataframe and link with gene data\n", "if clinical_features_list:\n", " clinical_features = pd.concat(clinical_features_list, axis=0)\n", " print(f\"Created clinical features dataframe with demographic info only:\")\n", " print(f\"Shape: {clinical_features.shape}\")\n", " print(clinical_features.iloc[:, :5]) # Show first 5 columns\n", " \n", " # Link clinical and genetic data\n", " linked_data = geo_link_clinical_genetic_data(clinical_features, gene_data)\n", " print(f\"\\nLinked data shape before handling missing values: {linked_data.shape}\")\n", " \n", " # Handle missing values - no trait column, so we use the first column \n", " # for the mandatory trait parameter in handle_missing_values\n", " first_col = linked_data.columns[0]\n", " linked_data_clean = handle_missing_values(linked_data, first_col)\n", " print(f\"Linked data shape after handling missing values: {linked_data_clean.shape}\")\n", " \n", " # Since we don't have trait information, we don't evaluate trait bias\n", " # We'll just check for bias in demographic features\n", " # Creating a temporary dataframe for bias checking with the first column as 'placeholder_trait'\n", " temp_df = linked_data_clean.copy()\n", " temp_df['placeholder_trait'] = 1 # All samples get the same value\n", " is_biased, linked_data_clean = judge_and_remove_biased_features(temp_df, 'placeholder_trait')\n", " linked_data_clean = linked_data_clean.drop('placeholder_trait', axis=1, errors='ignore')\n", "else:\n", " # If no clinical features, just use gene expression data\n", " print(\"No clinical features available, proceeding with gene expression data only\")\n", " linked_data = gene_data.T # Transpose to match expected format\n", " linked_data_clean = linked_data\n", " is_biased = True # Mark as biased since no trait information\n", "\n", "# Conduct final quality validation\n", "note = \"Dataset contains gene expression data from nasal epithelium of smokers in a study of combined aspirin and zileuton treatment. No COPD trait information available.\"\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, # No trait information for COPD\n", " is_biased=is_biased,\n", " df=linked_data_clean,\n", " note=note\n", ")\n", "\n", "# Save linked data if usable\n", "if is_usable:\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " linked_data_clean.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", "else:\n", " print(\"Dataset deemed not usable due to quality issues - 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 }