{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "07e91573", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:14:29.062400Z", "iopub.status.busy": "2025-03-25T06:14:29.062294Z", "iopub.status.idle": "2025-03-25T06:14:29.222111Z", "shell.execute_reply": "2025-03-25T06:14:29.221652Z" } }, "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 = \"Post-Traumatic_Stress_Disorder\"\n", "cohort = \"GSE199841\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Post-Traumatic_Stress_Disorder\"\n", "in_cohort_dir = \"../../input/GEO/Post-Traumatic_Stress_Disorder/GSE199841\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Post-Traumatic_Stress_Disorder/GSE199841.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Post-Traumatic_Stress_Disorder/gene_data/GSE199841.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Post-Traumatic_Stress_Disorder/clinical_data/GSE199841.csv\"\n", "json_path = \"../../output/preprocess/Post-Traumatic_Stress_Disorder/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "adfe75d3", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "6c324a58", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:14:29.223570Z", "iopub.status.busy": "2025-03-25T06:14:29.223424Z", "iopub.status.idle": "2025-03-25T06:14:29.373021Z", "shell.execute_reply": "2025-03-25T06:14:29.372411Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Gene expression signatures in patients with posttraumatic stress disorder (PTSD) and healthy controls\"\n", "!Series_summary\t\"To investigate gene expression signatures in PTSD, we used microarray and performed blood-based transcriptome profiling. Our analyses identified key genes and molecular pathways that can be centrally involved in the etiology of PTSD.\"\n", "!Series_overall_design\t\"The blood sampling was performed around noon (before lunch), between 11:30 AM and 12:30 PM. Gene expression in blood was measured for 32 women with PTSD and 16 healthy control women.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['gender: female'], 1: ['age(years): 55', 'age(years): 53', 'age(years): 22', 'age(years): 24', 'age(years): 27', 'age(years): 44', 'age(years): 41', 'age(years): 21', 'age(years): 42', 'age(years): 51', 'age(years): 43', 'age(years): 56', 'age(years): 26', 'age(years): 31', 'age(years): 45', 'age(years): 39', 'age(years): 38', 'age(years): 47', 'age(years): 29', 'age(years): 37', 'age(years): 35', 'age(years): 34', 'age(years): 50', 'age(years): 25', 'age(years): 40', 'age(years): 23', 'age(years): 59', 'age(years): 48', 'age(years): 30', 'age(years): 54'], 2: ['diagnostic group: healthy control', 'diagnostic group: PTSD patient']}\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": "ff890b63", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "00226c5b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:14:29.374791Z", "iopub.status.busy": "2025-03-25T06:14:29.374672Z", "iopub.status.idle": "2025-03-25T06:14:29.385193Z", "shell.execute_reply": "2025-03-25T06:14:29.384721Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical Features Preview:\n", "{'GSM5988345': [0.0, 55.0], 'GSM5988346': [0.0, 53.0], 'GSM5988347': [0.0, 22.0], 'GSM5988348': [0.0, 24.0], 'GSM5988349': [0.0, 27.0], 'GSM5988350': [0.0, 44.0], 'GSM5988351': [0.0, 41.0], 'GSM5988352': [0.0, 21.0], 'GSM5988353': [0.0, 27.0], 'GSM5988354': [0.0, 27.0], 'GSM5988355': [0.0, 42.0], 'GSM5988356': [0.0, 22.0], 'GSM5988357': [0.0, 21.0], 'GSM5988358': [0.0, 51.0], 'GSM5988359': [0.0, 43.0], 'GSM5988360': [0.0, 56.0], 'GSM5988361': [1.0, 26.0], 'GSM5988362': [1.0, 31.0], 'GSM5988363': [1.0, 45.0], 'GSM5988364': [1.0, 39.0], 'GSM5988365': [1.0, 38.0], 'GSM5988366': [1.0, 47.0], 'GSM5988367': [1.0, 29.0], 'GSM5988368': [1.0, 31.0], 'GSM5988369': [1.0, 39.0], 'GSM5988370': [1.0, 37.0], 'GSM5988371': [1.0, 35.0], 'GSM5988372': [1.0, 34.0], 'GSM5988373': [1.0, 50.0], 'GSM5988374': [1.0, 25.0], 'GSM5988375': [1.0, 27.0], 'GSM5988376': [1.0, 38.0], 'GSM5988377': [1.0, 40.0], 'GSM5988378': [1.0, 27.0], 'GSM5988379': [1.0, 44.0], 'GSM5988380': [1.0, 40.0], 'GSM5988381': [1.0, 37.0], 'GSM5988382': [1.0, 43.0], 'GSM5988383': [1.0, 23.0], 'GSM5988384': [1.0, 41.0], 'GSM5988385': [1.0, 59.0], 'GSM5988386': [1.0, 48.0], 'GSM5988387': [1.0, 24.0], 'GSM5988388': [1.0, 30.0], 'GSM5988389': [1.0, 55.0], 'GSM5988390': [1.0, 47.0], 'GSM5988391': [1.0, 42.0], 'GSM5988392': [1.0, 54.0]}\n", "Clinical data saved to ../../output/preprocess/Post-Traumatic_Stress_Disorder/clinical_data/GSE199841.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the Series_summary, this dataset contains microarray transcriptome profiling\n", "is_gene_available = True\n", "\n", "# 2.1 Data Availability\n", "# From the sample characteristics dictionary, we can identify the keys:\n", "trait_row = 2 # 'diagnostic group' indicates PTSD status\n", "age_row = 1 # Contains age information\n", "gender_row = None # This is constant (all female) so not useful for association study\n", "\n", "# 2.2 Data Type Conversion functions\n", "def convert_trait(value):\n", " \"\"\"Convert PTSD trait status to binary values.\"\"\"\n", " if pd.isna(value):\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() == 'ptsd patient':\n", " return 1\n", " elif value.lower() == 'healthy control':\n", " return 0\n", " else:\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age to numerical values.\"\"\"\n", " if pd.isna(value):\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Extract just the numerical part\n", " age_str = ''.join(c for c in value if c.isdigit())\n", " if age_str:\n", " return int(age_str)\n", " else:\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender to binary values.\"\"\"\n", " # This function is defined for completeness but won't be used\n", " if pd.isna(value):\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() == 'female':\n", " return 0\n", " elif value.lower() == 'male':\n", " return 1\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Initial filtering on data availability\n", "# Trait is available since trait_row is not None\n", "is_trait_available = trait_row is not None\n", "\n", "validate_and_save_cohort_info(\n", " is_final=False,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=is_gene_available,\n", " is_trait_available=is_trait_available\n", ")\n", "\n", "# 4. Clinical Feature Extraction\n", "# If trait data is available, extract clinical features\n", "if trait_row is not None:\n", " # Get clinical features\n", " clinical_features = 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 features\n", " clinical_preview = preview_df(clinical_features)\n", " print(\"Clinical Features Preview:\")\n", " print(clinical_preview)\n", " \n", " # Save clinical data to CSV\n", " clinical_features.to_csv(out_clinical_data_file)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "a2089108", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "2aff1adf", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:14:29.386790Z", "iopub.status.busy": "2025-03-25T06:14:29.386683Z", "iopub.status.idle": "2025-03-25T06:14:29.623906Z", "shell.execute_reply": "2025-03-25T06:14:29.623482Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "First 20 gene/probe identifiers:\n", "Index(['(+)E1A_r60_1', '(+)E1A_r60_3', '(+)E1A_r60_a104', '(+)E1A_r60_a107',\n", " '(+)E1A_r60_a135', '(+)E1A_r60_a20', '(+)E1A_r60_a22', '(+)E1A_r60_a97',\n", " '(+)E1A_r60_n11', '(+)E1A_r60_n9', '3xSLv1', 'A_19_P00315452',\n", " 'A_19_P00315492', 'A_19_P00315493', 'A_19_P00315502', 'A_19_P00315506',\n", " 'A_19_P00315518', 'A_19_P00315519', 'A_19_P00315529', 'A_19_P00315541'],\n", " dtype='object', name='ID')\n", "\n", "Gene data dimensions: 58341 genes × 48 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": "05f5ebb5", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "675eca6e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:14:29.625078Z", "iopub.status.busy": "2025-03-25T06:14:29.624931Z", "iopub.status.idle": "2025-03-25T06:14:29.627095Z", "shell.execute_reply": "2025-03-25T06:14:29.626804Z" } }, "outputs": [], "source": [ "# Examining the gene identifiers shown above, I can see they do not appear to be standard human gene symbols.\n", "# The identifiers starting with \"(+)E1A_r60_\" and \"A_19_P...\" are likely probe IDs from a microarray platform.\n", "# Standard human gene symbols would typically be like BRCA1, TP53, TNF, etc.\n", "# These appear to be Agilent microarray probe IDs that need to be mapped to standard gene symbols.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "0e6a8f95", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "8c479f38", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:14:29.628186Z", "iopub.status.busy": "2025-03-25T06:14:29.627952Z", "iopub.status.idle": "2025-03-25T06:14:34.052104Z", "shell.execute_reply": "2025-03-25T06:14:34.051643Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['GE_BrightCorner', 'DarkCorner', 'A_21_P0014386', 'A_33_P3396872', 'A_33_P3267760'], 'CONTROL_TYPE': ['pos', 'pos', 'FALSE', 'FALSE', 'FALSE'], 'REFSEQ': [nan, nan, nan, 'NM_001105533', nan], 'GB_ACC': [nan, nan, nan, 'NM_001105533', nan], 'LOCUSLINK_ID': [nan, nan, nan, 79974.0, 54880.0], 'GENE_SYMBOL': [nan, nan, nan, 'CPED1', 'BCOR'], 'GENE_NAME': [nan, nan, nan, 'cadherin-like and PC-esterase domain containing 1', 'BCL6 corepressor'], 'UNIGENE_ID': [nan, nan, nan, 'Hs.189652', nan], 'ENSEMBL_ID': [nan, nan, nan, nan, 'ENST00000378463'], 'ACCESSION_STRING': [nan, nan, nan, 'ref|NM_001105533|gb|AK025639|gb|BC030538|tc|THC2601673', 'ens|ENST00000378463'], 'CHROMOSOMAL_LOCATION': [nan, nan, 'unmapped', 'chr7:120901888-120901947', 'chrX:39909128-39909069'], 'CYTOBAND': [nan, nan, nan, 'hs|7q31.31', 'hs|Xp11.4'], 'DESCRIPTION': [nan, nan, nan, 'Homo sapiens cadherin-like and PC-esterase domain containing 1 (CPED1), transcript variant 2, mRNA [NM_001105533]', 'BCL6 corepressor [Source:HGNC Symbol;Acc:HGNC:20893] [ENST00000378463]'], 'GO_ID': [nan, nan, nan, 'GO:0005783(endoplasmic reticulum)', 'GO:0000122(negative regulation of transcription from RNA polymerase II promoter)|GO:0000415(negative regulation of histone H3-K36 methylation)|GO:0003714(transcription corepressor activity)|GO:0004842(ubiquitin-protein ligase activity)|GO:0005515(protein binding)|GO:0005634(nucleus)|GO:0006351(transcription, DNA-dependent)|GO:0007507(heart development)|GO:0008134(transcription factor binding)|GO:0030502(negative regulation of bone mineralization)|GO:0031072(heat shock protein binding)|GO:0031519(PcG protein complex)|GO:0035518(histone H2A monoubiquitination)|GO:0042476(odontogenesis)|GO:0042826(histone deacetylase binding)|GO:0044212(transcription regulatory region DNA binding)|GO:0045892(negative regulation of transcription, DNA-dependent)|GO:0051572(negative regulation of histone H3-K4 methylation)|GO:0060021(palate development)|GO:0065001(specification of axis polarity)|GO:0070171(negative regulation of tooth mineralization)'], 'SEQUENCE': [nan, nan, 'AATACATGTTTTGGTAAACACTCGGTCAGAGCACCCTCTTTCTGTGGAATCAGACTGGCA', 'GCTTATCTCACCTAATACAGGGACTATGCAACCAAGAAACTGGAAATAAAAACAAAGATA', 'CATCAAAGCTACGAGAGATCCTACACACCCAGATTTAAAAAATAATAAAAACTTAAGGGC'], 'SPOT_ID': ['GE_BrightCorner', 'DarkCorner', 'A_21_P0014386', 'A_33_P3396872', 'A_33_P3267760']}\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": "d890e295", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "8cf010a7", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:14:34.053575Z", "iopub.status.busy": "2025-03-25T06:14:34.053447Z", "iopub.status.idle": "2025-03-25T06:14:35.183300Z", "shell.execute_reply": "2025-03-25T06:14:35.182891Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Identifying probe ID to gene symbol mapping columns...\n", "Generated mapping dataframe with dimensions: (48862, 2)\n", "First few rows of the mapping:\n", " ID Gene\n", "3 A_33_P3396872 CPED1\n", "4 A_33_P3267760 BCOR\n", "5 A_32_P194264 CHAC2\n", "6 A_23_P153745 IFI30\n", "10 A_21_P0014180 GPR146\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Converted gene expression data dimensions: 29222 genes × 48 samples\n", "\n", "First 10 gene symbols in the converted data:\n", "Index(['A1BG', 'A1BG-AS1', 'A1CF', 'A1CF-2', 'A1CF-3', 'A2M', 'A2M-1',\n", " 'A2M-AS1', 'A2ML1', 'A2MP1'],\n", " dtype='object', name='Gene')\n", "\n", "Normalizing gene symbols...\n", "After normalization: 20778 genes × 48 samples\n", "\n", "Sample of normalized gene symbols:\n", "Index(['A1BG', 'A1BG-AS1', 'A1CF', 'A2M', 'A2M-AS1', 'A2ML1', 'A2MP1',\n", " 'A3GALT2', 'A4GALT', 'A4GNT'],\n", " dtype='object', name='Gene')\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene expression data saved to ../../output/preprocess/Post-Traumatic_Stress_Disorder/gene_data/GSE199841.csv\n" ] } ], "source": [ "# 1. Identify the appropriate columns from the gene annotation data\n", "# The 'ID' column in the gene annotation matches the index in gene_data (probe identifiers)\n", "# The 'GENE_SYMBOL' column contains the gene symbols we want to map to\n", "print(\"\\nIdentifying probe ID to gene symbol mapping columns...\")\n", "\n", "# 2. Get gene mapping dataframe using the get_gene_mapping function\n", "gene_mapping = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='GENE_SYMBOL')\n", "print(f\"Generated mapping dataframe with dimensions: {gene_mapping.shape}\")\n", "print(\"First few rows of the mapping:\")\n", "print(gene_mapping.head())\n", "\n", "# 3. Apply gene mapping to convert probe-level measurements to gene expression\n", "# This function will handle the many-to-many relationships as specified\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "print(f\"\\nConverted gene expression data dimensions: {gene_data.shape[0]} genes × {gene_data.shape[1]} samples\")\n", "print(\"\\nFirst 10 gene symbols in the converted data:\")\n", "print(gene_data.index[:10])\n", "\n", "# 4. Normalize gene symbols to handle synonyms and aggregate duplicate genes\n", "print(\"\\nNormalizing gene symbols...\")\n", "gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"After normalization: {gene_data.shape[0]} genes × {gene_data.shape[1]} samples\")\n", "print(\"\\nSample of normalized gene symbols:\")\n", "print(gene_data.index[:10])\n", "\n", "# Save the processed gene data to a CSV file\n", "gene_data.to_csv(out_gene_data_file)\n", "print(f\"\\nGene expression data saved to {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "892eff42", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "f1c43b64", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:14:35.184637Z", "iopub.status.busy": "2025-03-25T06:14:35.184513Z", "iopub.status.idle": "2025-03-25T06:14:45.636671Z", "shell.execute_reply": "2025-03-25T06:14:45.636192Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loaded clinical data shape: (2, 48)\n", "Clinical data columns: ['GSM5988345', 'GSM5988346', 'GSM5988347', 'GSM5988348', 'GSM5988349', 'GSM5988350', 'GSM5988351', 'GSM5988352', 'GSM5988353', 'GSM5988354', 'GSM5988355', 'GSM5988356', 'GSM5988357', 'GSM5988358', 'GSM5988359', 'GSM5988360', 'GSM5988361', 'GSM5988362', 'GSM5988363', 'GSM5988364', 'GSM5988365', 'GSM5988366', 'GSM5988367', 'GSM5988368', 'GSM5988369', 'GSM5988370', 'GSM5988371', 'GSM5988372', 'GSM5988373', 'GSM5988374', 'GSM5988375', 'GSM5988376', 'GSM5988377', 'GSM5988378', 'GSM5988379', 'GSM5988380', 'GSM5988381', 'GSM5988382', 'GSM5988383', 'GSM5988384', 'GSM5988385', 'GSM5988386', 'GSM5988387', 'GSM5988388', 'GSM5988389', 'GSM5988390', 'GSM5988391', 'GSM5988392']\n", "First few rows of clinical data:\n", " GSM5988345 GSM5988346 GSM5988347 \\\n", "Post-Traumatic_Stress_Disorder 0.0 0.0 0.0 \n", "Age 55.0 53.0 22.0 \n", "\n", " GSM5988348 GSM5988349 GSM5988350 \\\n", "Post-Traumatic_Stress_Disorder 0.0 0.0 0.0 \n", "Age 24.0 27.0 44.0 \n", "\n", " GSM5988351 GSM5988352 GSM5988353 \\\n", "Post-Traumatic_Stress_Disorder 0.0 0.0 0.0 \n", "Age 41.0 21.0 27.0 \n", "\n", " GSM5988354 ... GSM5988383 GSM5988384 \\\n", "Post-Traumatic_Stress_Disorder 0.0 ... 1.0 1.0 \n", "Age 27.0 ... 23.0 41.0 \n", "\n", " GSM5988385 GSM5988386 GSM5988387 \\\n", "Post-Traumatic_Stress_Disorder 1.0 1.0 1.0 \n", "Age 59.0 48.0 24.0 \n", "\n", " GSM5988388 GSM5988389 GSM5988390 \\\n", "Post-Traumatic_Stress_Disorder 1.0 1.0 1.0 \n", "Age 30.0 55.0 47.0 \n", "\n", " GSM5988391 GSM5988392 \n", "Post-Traumatic_Stress_Disorder 1.0 1.0 \n", "Age 42.0 54.0 \n", "\n", "[2 rows x 48 columns]\n", "Gene data shape: (20778, 48)\n", "First 5 gene symbols: ['A1BG', 'A1BG-AS1', 'A1CF', 'A2M', 'A2M-AS1']\n", "Linked data shape: (48, 20780)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (48, 20780)\n", "\n", "Checking for bias in the trait variable:\n", "For the feature 'Post-Traumatic_Stress_Disorder', the least common label is '0.0' with 16 occurrences. This represents 33.33% of the dataset.\n", "The distribution of the feature 'Post-Traumatic_Stress_Disorder' in this dataset is fine.\n", "\n", "Quartiles for 'Age':\n", " 25%: 27.0\n", " 50% (Median): 38.5\n", " 75%: 44.25\n", "Min: 21.0\n", "Max: 59.0\n", "The distribution of the feature 'Age' in this dataset is fine.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Post-Traumatic_Stress_Disorder/GSE199841.csv\n" ] } ], "source": [ "# 1. Load the clinical data previously saved\n", "# Since the gene_data is already normalized in Step 6, we can proceed with linking\n", "\n", "# Load the clinical data from Step 2\n", "clinical_features = pd.read_csv(out_clinical_data_file, index_col=0)\n", "print(f\"Loaded clinical data shape: {clinical_features.shape}\")\n", "print(f\"Clinical data columns: {clinical_features.columns.tolist()}\")\n", "print(f\"First few rows of clinical data:\")\n", "print(clinical_features.head())\n", "\n", "# Load the gene data (already normalized in Step 6)\n", "print(f\"Gene data shape: {gene_data.shape}\")\n", "print(f\"First 5 gene symbols: {gene_data.index[:5].tolist()}\")\n", "\n", "# Link clinical and genetic data\n", "linked_data = geo_link_clinical_genetic_data(clinical_features, gene_data)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "\n", "# Handle missing values\n", "linked_data = handle_missing_values(linked_data, trait_col=trait)\n", "print(f\"Data shape after handling missing values: {linked_data.shape}\")\n", "\n", "# Determine if trait is biased\n", "print(\"\\nChecking for bias in the trait variable:\")\n", "is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "\n", "# Conduct final quality validation\n", "is_trait_available = True # We confirmed trait data is available from Step 2\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=is_trait_available,\n", " is_biased=is_biased,\n", " df=linked_data,\n", " note=\"Dataset contains gene expression data for PTSD patients and healthy controls. All subjects are female.\"\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.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\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 }