{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "65cd3b86", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:07:37.603075Z", "iopub.status.busy": "2025-03-25T06:07:37.602894Z", "iopub.status.idle": "2025-03-25T06:07:37.768753Z", "shell.execute_reply": "2025-03-25T06:07:37.768361Z" } }, "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 = \"Parkinsons_Disease\"\n", "cohort = \"GSE202665\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Parkinsons_Disease\"\n", "in_cohort_dir = \"../../input/GEO/Parkinsons_Disease/GSE202665\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Parkinsons_Disease/GSE202665.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Parkinsons_Disease/gene_data/GSE202665.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Parkinsons_Disease/clinical_data/GSE202665.csv\"\n", "json_path = \"../../output/preprocess/Parkinsons_Disease/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "0bb69538", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "9caca049", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:07:37.769982Z", "iopub.status.busy": "2025-03-25T06:07:37.769833Z", "iopub.status.idle": "2025-03-25T06:07:38.104745Z", "shell.execute_reply": "2025-03-25T06:07:38.104166Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Time-resolved RNA signatures of CD4+ T cells in Parkinson's disease [mRNAarray]\"\n", "!Series_summary\t\"We employed time-resolved RNA expression analyses upon the activation of peripheral CD4+ T cells to track and functionally relate changes on cellular signaling in patients at different stages of PD\"\n", "!Series_overall_design\t\"A cohort of elderly male (age 53-85 yrs.; non-smokers), representative for different stages of Parkinson´s disease were tested (P1-5). Corresponding healthy controls (C1-5) (n=4) were matched for age (age 53-63 yrs.; non-smokers) and gender. One control was carried out twice (C1 and C1_2), representing independent replicates from the same healthy donor but from two different days of blood collection. CD4+ T cells were isolated from peripheral blood samples and were in vitro activated by αCD2/αCD3/αCD28 beads. Cellular samples were collected at 0, 2, 4, 8, 12 and 24 h after activation for subsequent RNA expression analyses.\"\n", "Sample Characteristics Dictionary:\n", "{0: [\"disease state: Parkinson's disease\", 'disease state: Healthy Control'], 1: ['cell type: CD4+ T cells'], 2: ['gender: male'], 3: ['age: 53', 'age: 57', 'age: 63', 'age: 75', 'age: 85', 'age: 76', 'age: 69', 'age: 66'], 4: ['Stage: 1', 'Stage: 0 (Healthy Control)', 'Stage: 4', 'Stage: 2.5'], 5: ['time-point post activation: 0 h', 'time-point post activation: 2 h', 'time-point post activation: 4 h', 'time-point post activation: 8 h', 'time-point post activation: 12 h', 'time-point post activation: 24 h']}\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": "d07e2926", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "dc6b85d4", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:07:38.106735Z", "iopub.status.busy": "2025-03-25T06:07:38.106585Z", "iopub.status.idle": "2025-03-25T06:07:38.118511Z", "shell.execute_reply": "2025-03-25T06:07:38.117970Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical Data Preview:\n", "{'GSM6128044': [1.0, 53.0], 'GSM6128045': [1.0, 53.0], 'GSM6128046': [1.0, 53.0], 'GSM6128047': [1.0, 53.0], 'GSM6128048': [1.0, 53.0], 'GSM6128049': [1.0, 53.0], 'GSM6128050': [1.0, 57.0], 'GSM6128051': [1.0, 57.0], 'GSM6128052': [1.0, 57.0], 'GSM6128053': [1.0, 57.0], 'GSM6128054': [1.0, 57.0], 'GSM6128055': [0.0, 63.0], 'GSM6128056': [0.0, 63.0], 'GSM6128057': [0.0, 63.0], 'GSM6128058': [0.0, 63.0], 'GSM6128059': [0.0, 63.0], 'GSM6128060': [0.0, 63.0], 'GSM6128061': [1.0, 75.0], 'GSM6128062': [1.0, 75.0], 'GSM6128063': [1.0, 75.0], 'GSM6128064': [1.0, 75.0], 'GSM6128065': [1.0, 75.0], 'GSM6128066': [1.0, 75.0], 'GSM6128067': [1.0, 85.0], 'GSM6128068': [1.0, 85.0], 'GSM6128069': [1.0, 85.0], 'GSM6128070': [1.0, 85.0], 'GSM6128071': [1.0, 85.0], 'GSM6128072': [1.0, 85.0], 'GSM6128073': [1.0, 76.0], 'GSM6128074': [1.0, 76.0], 'GSM6128075': [1.0, 76.0], 'GSM6128076': [1.0, 76.0], 'GSM6128077': [1.0, 76.0], 'GSM6128078': [1.0, 76.0], 'GSM6128079': [0.0, 63.0], 'GSM6128080': [0.0, 63.0], 'GSM6128081': [0.0, 63.0], 'GSM6128082': [0.0, 63.0], 'GSM6128083': [0.0, 63.0], 'GSM6128084': [0.0, 63.0], 'GSM6128085': [0.0, 69.0], 'GSM6128086': [0.0, 69.0], 'GSM6128087': [0.0, 69.0], 'GSM6128088': [0.0, 69.0], 'GSM6128089': [0.0, 69.0], 'GSM6128090': [0.0, 69.0], 'GSM6128091': [0.0, 66.0], 'GSM6128092': [0.0, 66.0], 'GSM6128093': [0.0, 66.0], 'GSM6128094': [0.0, 66.0], 'GSM6128095': [0.0, 66.0], 'GSM6128096': [0.0, 66.0], 'GSM6128097': [0.0, 53.0], 'GSM6128098': [0.0, 53.0], 'GSM6128099': [0.0, 53.0], 'GSM6128100': [0.0, 53.0], 'GSM6128101': [0.0, 53.0], 'GSM6128102': [0.0, 53.0]}\n", "Clinical data saved to ../../output/preprocess/Parkinsons_Disease/clinical_data/GSE202665.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the series title mentioning \"mRNAarray\" and description referring to \"RNA expression analyses\"\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# For trait: Key 0 contains \"disease state\" information with Parkinson's disease vs Control\n", "trait_row = 0\n", "\n", "# For age: Key 3 contains age information\n", "age_row = 3\n", "\n", "# For gender: Key 2 contains gender information, but it shows all subjects are male (constant)\n", "# Since this is a constant feature, we consider it not available for association studies\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion Functions\n", "def convert_trait(value):\n", " \"\"\"Convert disease state to binary format (0: Control, 1: PD)\"\"\"\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", " if \"parkinson\" in value.lower() or \"pd\" in value.lower():\n", " return 1\n", " elif \"healthy\" in value.lower() or \"control\" in value.lower():\n", " return 0\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age to continuous numeric 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", " try:\n", " return float(value)\n", " except (ValueError, TypeError):\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"\n", " Convert gender to binary format (0: Female, 1: Male)\n", " Not used in this dataset as gender is constant (all male)\n", " \"\"\"\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", " if value.lower() in [\"male\", \"m\"]:\n", " return 1\n", " elif value.lower() in [\"female\", \"f\"]:\n", " return 0\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Since trait_row is not None, trait data is available\n", "is_trait_available = trait_row is not None\n", "\n", "# Conduct initial filtering\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 trait_row is not None, we need to extract clinical features\n", "if trait_row is not None:\n", " # Extract clinical features\n", " clinical_df = geo_select_clinical_features(\n", " 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(clinical_df)\n", " print(\"Clinical Data Preview:\")\n", " print(preview)\n", " \n", " # Save the clinical features\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " clinical_df.to_csv(out_clinical_data_file)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "5c5ec44a", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "e7759176", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:07:38.120223Z", "iopub.status.busy": "2025-03-25T06:07:38.120086Z", "iopub.status.idle": "2025-03-25T06:07:38.658830Z", "shell.execute_reply": "2025-03-25T06:07:38.658203Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First 20 gene/probe identifiers:\n", "Index(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13',\n", " '14', '15', '16', '17', '18', '19', '20'],\n", " dtype='object', name='ID')\n" ] } ], "source": [ "# 1. First get the file paths again to access the matrix file\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. Use the get_genetic_data function from the library to get the gene_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) for future observation\n", "print(\"First 20 gene/probe identifiers:\")\n", "print(gene_data.index[:20])\n" ] }, { "cell_type": "markdown", "id": "ce1867fb", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "a92de5c8", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:07:38.660729Z", "iopub.status.busy": "2025-03-25T06:07:38.660596Z", "iopub.status.idle": "2025-03-25T06:07:38.663434Z", "shell.execute_reply": "2025-03-25T06:07:38.662914Z" } }, "outputs": [], "source": [ "# The gene identifiers appear to be numeric indices (1, 2, 3...) rather than actual gene symbols\n", "# These are likely probe IDs or some other internal identifiers that would need to be mapped to gene symbols\n", "# In human gene expression data, gene symbols would typically be named like 'BRCA1', 'TP53', etc.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "8df2e735", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "9407109b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:07:38.665139Z", "iopub.status.busy": "2025-03-25T06:07:38.665021Z", "iopub.status.idle": "2025-03-25T06:07:46.204869Z", "shell.execute_reply": "2025-03-25T06:07:46.204496Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['1', '2', '3', '4', '5'], 'COL': ['192', '192', '192', '192', '192'], 'ROW': [328.0, 326.0, 324.0, 322.0, 320.0], 'NAME': ['GE_BrightCorner', 'DarkCorner', 'DarkCorner', 'A_21_P0014386', 'A_33_P3396872'], 'SPOT_ID': ['GE_BrightCorner', 'DarkCorner', 'DarkCorner', 'A_21_P0014386', 'A_33_P3396872'], 'CONTROL_TYPE': ['pos', 'pos', 'pos', 'FALSE', 'FALSE'], 'REFSEQ': [nan, nan, nan, nan, 'NM_001105533'], 'GB_ACC': [nan, nan, nan, nan, 'NM_001105533'], 'LOCUSLINK_ID': [nan, nan, nan, nan, 79974.0], 'GENE_SYMBOL': [nan, nan, nan, nan, 'CPED1'], 'GENE_NAME': [nan, nan, nan, nan, 'cadherin-like and PC-esterase domain containing 1'], 'UNIGENE_ID': [nan, nan, nan, nan, 'Hs.189652'], 'ENSEMBL_ID': [nan, nan, nan, nan, nan], 'TIGR_ID': [nan, nan, nan, nan, nan], 'ACCESSION_STRING': [nan, nan, nan, nan, 'ref|NM_001105533|gb|AK025639|gb|BC030538|tc|THC2601673'], 'CHROMOSOMAL_LOCATION': [nan, nan, nan, 'unmapped', 'chr7:120901888-120901947'], 'CYTOBAND': [nan, nan, nan, nan, 'hs|7q31.31'], 'DESCRIPTION': [nan, nan, nan, nan, 'Homo sapiens cadherin-like and PC-esterase domain containing 1 (CPED1), transcript variant 2, mRNA [NM_001105533]'], 'GO_ID': [nan, nan, nan, nan, 'GO:0005783(endoplasmic reticulum)'], 'SEQUENCE': [nan, nan, nan, 'AATACATGTTTTGGTAAACACTCGGTCAGAGCACCCTCTTTCTGTGGAATCAGACTGGCA', 'GCTTATCTCACCTAATACAGGGACTATGCAACCAAGAAACTGGAAATAAAAACAAAGATA'], 'SPOT_ID.1': [nan, nan, nan, nan, nan]}\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": "773b3535", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "5a7c2b7c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:07:46.206188Z", "iopub.status.busy": "2025-03-25T06:07:46.206064Z", "iopub.status.idle": "2025-03-25T06:07:46.516939Z", "shell.execute_reply": "2025-03-25T06:07:46.516573Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene mapping dataframe preview:\n", "{'ID': ['5', '6', '7', '8', '12', '13', '14', '15', '16', '17'], 'Gene': ['CPED1', 'BCOR', 'CHAC2', 'IFI30', 'GPR146', 'SREK1IP1', 'lnc-ARMCX4-1', 'BAX', 'FPGT-TNNI3K', 'SYNGAP1']}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Number of genes after mapping: 29222\n", "First 10 gene symbols after mapping:\n", "Index(['A1BG', 'A1BG-AS1', 'A1CF', 'A1CF-2', 'A1CF-3', 'A2M', 'A2M-1',\n", " 'A2M-AS1', 'A2ML1', 'A2MP1'],\n", " dtype='object', name='Gene')\n" ] } ], "source": [ "# 1. Looking at the gene expression data and the gene annotation data, I need to identify the key columns for mapping\n", "# Based on the previews, the gene expression data has ID values (1, 2, 3, ...) that align with the 'ID' column \n", "# in the gene annotation data, and the 'GENE_SYMBOL' column contains the gene symbols we need\n", "\n", "# 2. Get a gene mapping dataframe\n", "# Extract the ID column and the GENE_SYMBOL column from the gene annotation dataframe\n", "mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='GENE_SYMBOL')\n", "\n", "print(\"Gene mapping dataframe preview:\")\n", "print(preview_df(mapping_df, n=10))\n", "\n", "# 3. Convert probe-level measurements to gene expression data using the mapping\n", "# This function handles many-to-many relations by splitting expression values when a probe maps to multiple genes\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "\n", "print(\"Number of genes after mapping:\", len(gene_data))\n", "print(\"First 10 gene symbols after mapping:\")\n", "print(gene_data.index[:10])\n" ] }, { "cell_type": "markdown", "id": "cde8e367", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "ca787cf4", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:07:46.518829Z", "iopub.status.busy": "2025-03-25T06:07:46.518721Z", "iopub.status.idle": "2025-03-25T06:07:59.003406Z", "shell.execute_reply": "2025-03-25T06:07:59.002766Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data shape after normalization: (20778, 59)\n", "First 5 normalized gene symbols:\n", "Index(['A1BG', 'A1BG-AS1', 'A1CF', 'A2M', 'A2M-AS1'], dtype='object', name='Gene')\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Parkinsons_Disease/gene_data/GSE202665.csv\n", "Loaded clinical data from file\n", "Clinical data shape: (2, 59)\n", "Clinical data preview:\n", "{'GSM6128044': [1.0, 53.0], 'GSM6128045': [1.0, 53.0], 'GSM6128046': [1.0, 53.0], 'GSM6128047': [1.0, 53.0], 'GSM6128048': [1.0, 53.0], 'GSM6128049': [1.0, 53.0], 'GSM6128050': [1.0, 57.0], 'GSM6128051': [1.0, 57.0], 'GSM6128052': [1.0, 57.0], 'GSM6128053': [1.0, 57.0], 'GSM6128054': [1.0, 57.0], 'GSM6128055': [0.0, 63.0], 'GSM6128056': [0.0, 63.0], 'GSM6128057': [0.0, 63.0], 'GSM6128058': [0.0, 63.0], 'GSM6128059': [0.0, 63.0], 'GSM6128060': [0.0, 63.0], 'GSM6128061': [1.0, 75.0], 'GSM6128062': [1.0, 75.0], 'GSM6128063': [1.0, 75.0], 'GSM6128064': [1.0, 75.0], 'GSM6128065': [1.0, 75.0], 'GSM6128066': [1.0, 75.0], 'GSM6128067': [1.0, 85.0], 'GSM6128068': [1.0, 85.0], 'GSM6128069': [1.0, 85.0], 'GSM6128070': [1.0, 85.0], 'GSM6128071': [1.0, 85.0], 'GSM6128072': [1.0, 85.0], 'GSM6128073': [1.0, 76.0], 'GSM6128074': [1.0, 76.0], 'GSM6128075': [1.0, 76.0], 'GSM6128076': [1.0, 76.0], 'GSM6128077': [1.0, 76.0], 'GSM6128078': [1.0, 76.0], 'GSM6128079': [0.0, 63.0], 'GSM6128080': [0.0, 63.0], 'GSM6128081': [0.0, 63.0], 'GSM6128082': [0.0, 63.0], 'GSM6128083': [0.0, 63.0], 'GSM6128084': [0.0, 63.0], 'GSM6128085': [0.0, 69.0], 'GSM6128086': [0.0, 69.0], 'GSM6128087': [0.0, 69.0], 'GSM6128088': [0.0, 69.0], 'GSM6128089': [0.0, 69.0], 'GSM6128090': [0.0, 69.0], 'GSM6128091': [0.0, 66.0], 'GSM6128092': [0.0, 66.0], 'GSM6128093': [0.0, 66.0], 'GSM6128094': [0.0, 66.0], 'GSM6128095': [0.0, 66.0], 'GSM6128096': [0.0, 66.0], 'GSM6128097': [0.0, 53.0], 'GSM6128098': [0.0, 53.0], 'GSM6128099': [0.0, 53.0], 'GSM6128100': [0.0, 53.0], 'GSM6128101': [0.0, 53.0], 'GSM6128102': [0.0, 53.0]}\n", "Linked data shape: (59, 20780)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (59, 20780)\n", "For the feature 'Parkinsons_Disease', the least common label is '1.0' with 29 occurrences. This represents 49.15% of the dataset.\n", "The distribution of the feature 'Parkinsons_Disease' in this dataset is fine.\n", "\n", "Quartiles for 'Age':\n", " 25%: 57.0\n", " 50% (Median): 66.0\n", " 75%: 75.0\n", "Min: 53.0\n", "Max: 85.0\n", "The distribution of the feature 'Age' in this dataset is fine.\n", "\n", "Data shape after removing biased features: (59, 20780)\n", "Is the trait biased: False\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Parkinsons_Disease/GSE202665.csv\n" ] } ], "source": [ "# 1. Normalize gene symbols from the already mapped gene expression data from Step 6\n", "# Apply normalization to standardize gene symbols\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Gene expression data shape after normalization: {normalized_gene_data.shape}\")\n", "print(\"First 5 normalized gene symbols:\")\n", "print(normalized_gene_data.index[:5])\n", "\n", "# Save the normalized gene data\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "normalized_gene_data.to_csv(out_gene_data_file)\n", "print(f\"Normalized gene data saved to {out_gene_data_file}\")\n", "\n", "# 2. Load the clinical data that was already processed in Step 2\n", "# We need to load the clinical data from the file that was saved in Step 2\n", "if os.path.exists(out_clinical_data_file):\n", " clinical_data_processed = pd.read_csv(out_clinical_data_file, index_col=0)\n", " print(\"Loaded clinical data from file\")\n", "else:\n", " # If for some reason the file wasn't saved, recreate the clinical features using the same parameters as in Step 2\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", " # Use the same conversion function and trait_row from Step 2\n", " def convert_trait(value):\n", " \"\"\"Convert occupation to binary trait (exposure risk for Parkinson's)\"\"\"\n", " if not isinstance(value, str):\n", " return None\n", " \n", " value = value.lower().split(\": \")[-1].strip()\n", " \n", " if \"farmworker\" in value:\n", " return 1 # Higher pesticide exposure (risk factor for Parkinson's)\n", " elif \"manual worker\" in value:\n", " return 0 # Lower pesticide exposure \n", " else:\n", " return None\n", " \n", " # Use the exact same parameters as we determined in Step 2\n", " clinical_data_processed = geo_select_clinical_features(\n", " clinical_df=clinical_data,\n", " trait=trait,\n", " trait_row=0, # From Step 2\n", " convert_trait=convert_trait,\n", " age_row=None, # From Step 2\n", " convert_age=None,\n", " gender_row=None, # From Step 2\n", " convert_gender=None\n", " )\n", " \n", " # Save it again just to be sure\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " clinical_data_processed.to_csv(out_clinical_data_file)\n", "\n", "print(\"Clinical data shape:\", clinical_data_processed.shape)\n", "print(\"Clinical data preview:\")\n", "print(preview_df(clinical_data_processed))\n", "\n", "# 3. Link clinical and genetic data\n", "# The clinical data is oriented with genes/traits as rows and samples as columns\n", "# Transpose the normalized gene data to match this orientation (samples as columns)\n", "genetic_data_t = normalized_gene_data\n", "\n", "# Link clinical and genetic data vertically\n", "linked_data = geo_link_clinical_genetic_data(clinical_data_processed, genetic_data_t)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "\n", "# 4. Handle missing values\n", "linked_data = handle_missing_values(linked_data, trait)\n", "print(f\"Data shape after handling missing values: {linked_data.shape}\")\n", "\n", "# 5. Determine if trait and demographic features are biased\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", "print(f\"Is the trait biased: {is_biased}\")\n", "\n", "# 6. Validate and save cohort info\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=\"Dataset contains gene expression data from blood samples comparing farmworkers (with higher pesticide exposure, a risk factor for Parkinson's) to manual workers.\"\n", ")\n", "\n", "# 7. Save 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 deemed not usable. Linked data was 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 }