{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "ff14a8a2", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:16:05.759996Z", "iopub.status.busy": "2025-03-25T06:16:05.759895Z", "iopub.status.idle": "2025-03-25T06:16:05.916731Z", "shell.execute_reply": "2025-03-25T06:16:05.916378Z" } }, "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 = \"GSE77164\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Post-Traumatic_Stress_Disorder\"\n", "in_cohort_dir = \"../../input/GEO/Post-Traumatic_Stress_Disorder/GSE77164\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Post-Traumatic_Stress_Disorder/GSE77164.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Post-Traumatic_Stress_Disorder/gene_data/GSE77164.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Post-Traumatic_Stress_Disorder/clinical_data/GSE77164.csv\"\n", "json_path = \"../../output/preprocess/Post-Traumatic_Stress_Disorder/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "e0ee0c3e", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "f2c375b4", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:16:05.918169Z", "iopub.status.busy": "2025-03-25T06:16:05.918029Z", "iopub.status.idle": "2025-03-25T06:16:06.289350Z", "shell.execute_reply": "2025-03-25T06:16:06.289012Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Peripheral blood transcriptome profiles in Nepali child soldiers and civilians.\"\n", "!Series_summary\t\"Analysis of transcript abundance estimates as a function of child soldier status, PTSD symptoms, and psychological resilience.\"\n", "!Series_overall_design\t\"Gene expression profiling was conducted on dried blood spot (DBS) samples collected from community dwelling adolescents and young adults in Nepal. Approximatley half of the sample were former child soldiers in the Nepal People's War and the other half were demographically similiar civilian non-combatants. In addition to basic demographic characteristics (age, sex, ethnic minority status, social caste status, education level), participants were also assessed on syptoms of post-traumatic stress (PTS, assessed by a culturally adapted version of The Child PTSD Symptom Scale; Kohrt BA, et al. (2011) Validation of cross-cultural child mental health and psychosocial research instruments: adapting the Depression Self-Rating Scale and Child PTSD Symptom Scale in Nepal. BMC Psychiatry 11(1):e127, with higher values indicating greater PTSD symptoms) and psychological resilience (assessed by a culturally adapted version of the Resilience Scale; Wagnild GM & Young HM (1993) Development and psychometric evaluation of the Resilience Scale. Journal of Nursing Measurement, with higher values indicating greater resilience). Dichotomous variables were coded 0=no/absent and 1=yes/present. Valid gene expression data are available for 254 samples.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['childsoldier: 0', 'childsoldier: 1'], 1: ['female: 1', 'female: 0'], 2: ['age: 19', 'age: 22', 'age: 20', 'age: 18', 'age: 23', 'age: 21', 'age: 17', 'age: 16', 'age: 15', 'age: 24', 'age: 26'], 3: ['ethnicminority: 0', 'ethnicminority: 1'], 4: ['castelow: 1', 'castelow: 0'], 5: ['educationlevel: 3', 'educationlevel: 2', 'educationlevel: 5', 'educationlevel: 4', 'educationlevel: 0', 'educationlevel: 6', 'educationlevel: 1'], 6: ['pts: 0', 'pts: 1'], 7: ['resilience: 6', 'resilience: 12', 'resilience: 11', 'resilience: 19', 'resilience: 7', 'resilience: 15', 'resilience: 14', 'resilience: 13', 'resilience: 9', 'resilience: 21', 'resilience: 8', 'resilience: 16', 'resilience: 10', 'resilience: 4', 'resilience: 17', 'resilience: 18', 'resilience: 20', 'resilience: 5', 'resilience: 22', 'resilience: 2', 'resilience: 24'], 8: ['cd3d: 7.0075', 'cd3d: 7.2736', 'cd3d: 7.1577', 'cd3d: 7.0676', 'cd3d: 7.1673', 'cd3d: 7.0982', 'cd3d: 7.5201', 'cd3d: 7.2410', 'cd3d: 7.1874', 'cd3d: 6.9566', 'cd3d: 7.1541', 'cd3d: 7.0477', 'cd3d: 7.5926', 'cd3d: 7.2843', 'cd3d: 7.0757', 'cd3d: 7.4639', 'cd3d: 6.7728', 'cd3d: 7.2757', 'cd3d: 7.0564', 'cd3d: 6.5561', 'cd3d: 7.2033', 'cd3d: 7.7854', 'cd3d: 7.1314', 'cd3d: 7.3119', 'cd3d: 7.1484', 'cd3d: 7.5207', 'cd3d: 7.0901', 'cd3d: 6.9434', 'cd3d: 7.1241', 'cd3d: 7.0350'], 9: ['cd3e: 6.4452', 'cd3e: 6.5226', 'cd3e: 6.5730', 'cd3e: 6.4706', 'cd3e: 6.5379', 'cd3e: 6.5850', 'cd3e: 6.7151', 'cd3e: 6.5741', 'cd3e: 6.7673', 'cd3e: 6.4508', 'cd3e: 6.6955', 'cd3e: 6.5000', 'cd3e: 6.5744', 'cd3e: 6.6722', 'cd3e: 6.6954', 'cd3e: 6.4809', 'cd3e: 6.4037', 'cd3e: 6.7197', 'cd3e: 6.4634', 'cd3e: 6.4674', 'cd3e: 6.5046', 'cd3e: 6.5401', 'cd3e: 6.7358', 'cd3e: 6.6096', 'cd3e: 6.6626', 'cd3e: 6.5991', 'cd3e: 6.6733', 'cd3e: 6.5211', 'cd3e: 6.5184', 'cd3e: 6.6562'], 10: ['cd4: 6.6578', 'cd4: 6.8981', 'cd4: 6.5285', 'cd4: 6.5148', 'cd4: 6.5819', 'cd4: 6.9613', 'cd4: 6.3971', 'cd4: 6.4650', 'cd4: 6.7866', 'cd4: 6.8344', 'cd4: 6.6944', 'cd4: 6.4602', 'cd4: 6.5567', 'cd4: 6.5075', 'cd4: 6.7794', 'cd4: 6.4684', 'cd4: 6.5566', 'cd4: 6.5217', 'cd4: 6.3953', 'cd4: 6.5025', 'cd4: 6.4857', 'cd4: 6.5066', 'cd4: 6.6088', 'cd4: 6.8501', 'cd4: 6.5658', 'cd4: 6.7850', 'cd4: 6.7353', 'cd4: 6.5514', 'cd4: 6.5673', 'cd4: 6.7513'], 11: ['cd8a: 6.8284', 'cd8a: 7.0250', 'cd8a: 7.0793', 'cd8a: 6.8957', 'cd8a: 7.1241', 'cd8a: 6.8346', 'cd8a: 6.6557', 'cd8a: 7.1908', 'cd8a: 6.8679', 'cd8a: 6.8499', 'cd8a: 7.1032', 'cd8a: 6.5762', 'cd8a: 6.5000', 'cd8a: 6.9322', 'cd8a: 6.8806', 'cd8a: 6.9875', 'cd8a: 6.6315', 'cd8a: 6.7650', 'cd8a: 7.2516', 'cd8a: 6.5970', 'cd8a: 7.1161', 'cd8a: 6.4324', 'cd8a: 6.4040', 'cd8a: 6.8517', 'cd8a: 6.8565', 'cd8a: 7.2999', 'cd8a: 6.7364', 'cd8a: 6.9429', 'cd8a: 6.9458', 'cd8a: 6.8862'], 12: ['cd19: 6.4881', 'cd19: 6.4041', 'cd19: 6.4893', 'cd19: 6.5173', 'cd19: 6.4634', 'cd19: 6.7114', 'cd19: 6.3697', 'cd19: 6.4649', 'cd19: 6.5112', 'cd19: 6.3511', 'cd19: 6.4122', 'cd19: 6.4353', 'cd19: 6.4801', 'cd19: 6.6317', 'cd19: 6.4008', 'cd19: 6.4713', 'cd19: 6.4378', 'cd19: 6.3885', 'cd19: 6.3415', 'cd19: 6.4517', 'cd19: 6.4447', 'cd19: 6.4174', 'cd19: 6.4804', 'cd19: 6.5031', 'cd19: 6.4272', 'cd19: 6.4051', 'cd19: 6.5567', 'cd19: 6.5385', 'cd19: 6.5904', 'cd19: 6.6154'], 13: ['ncam1: 6.5414', 'ncam1: 6.5526', 'ncam1: 6.5921', 'ncam1: 6.6740', 'ncam1: 6.5585', 'ncam1: 6.5689', 'ncam1: 6.5420', 'ncam1: 6.6053', 'ncam1: 6.5858', 'ncam1: 6.4792', 'ncam1: 6.5208', 'ncam1: 6.5187', 'ncam1: 6.6200', 'ncam1: 6.5067', 'ncam1: 6.5945', 'ncam1: 6.5284', 'ncam1: 6.4951', 'ncam1: 6.5088', 'ncam1: 6.4840', 'ncam1: 6.7585', 'ncam1: 6.5251', 'ncam1: 6.6237', 'ncam1: 6.6054', 'ncam1: 6.5334', 'ncam1: 6.5937', 'ncam1: 6.6542', 'ncam1: 6.6023', 'ncam1: 6.5437', 'ncam1: 6.5402', 'ncam1: 6.5513'], 14: ['fcgr3a: 8.6804', 'fcgr3a: 8.0131', 'fcgr3a: 9.0202', 'fcgr3a: 8.1795', 'fcgr3a: 8.5348', 'fcgr3a: 8.2148', 'fcgr3a: 8.4218', 'fcgr3a: 8.4028', 'fcgr3a: 8.2684', 'fcgr3a: 8.5212', 'fcgr3a: 8.1244', 'fcgr3a: 8.4270', 'fcgr3a: 8.4931', 'fcgr3a: 8.2409', 'fcgr3a: 8.2065', 'fcgr3a: 8.1696', 'fcgr3a: 8.6144', 'fcgr3a: 8.2256', 'fcgr3a: 8.3073', 'fcgr3a: 8.4687', 'fcgr3a: 7.6951', 'fcgr3a: 8.6123', 'fcgr3a: 8.0688', 'fcgr3a: 7.6863', 'fcgr3a: 8.5599', 'fcgr3a: 8.1909', 'fcgr3a: 8.3472', 'fcgr3a: 8.5835', 'fcgr3a: 8.3395', 'fcgr3a: 8.7083'], 15: ['cd14: 8.0347', 'cd14: 8.5349', 'cd14: 8.2022', 'cd14: 8.1641', 'cd14: 8.0782', 'cd14: 8.1914', 'cd14: 7.4388', 'cd14: 8.3317', 'cd14: 8.2602', 'cd14: 8.5294', 'cd14: 8.2319', 'cd14: 9.2749', 'cd14: 8.9341', 'cd14: 8.2548', 'cd14: 7.9843', 'cd14: 7.1702', 'cd14: 8.6856', 'cd14: 8.4442', 'cd14: 7.7626', 'cd14: 7.8925', 'cd14: 7.7061', 'cd14: 6.5266', 'cd14: 6.8536', 'cd14: 8.7854', 'cd14: 8.3601', 'cd14: 7.7520', 'cd14: 8.6428', 'cd14: 8.1995', 'cd14: 7.8720', 'cd14: 8.0869'], 16: ['tissue: whole blood']}\n" ] } ], "source": [ "from tools.preprocess import *\n", "# 1. Identify the paths to the SOFT file and the matrix file\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. Read the matrix file to obtain background information and sample characteristics data\n", "background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']\n", "clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']\n", "background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)\n", "\n", "# 3. Obtain the sample characteristics dictionary from the clinical dataframe\n", "sample_characteristics_dict = get_unique_values_by_row(clinical_data)\n", "\n", "# 4. Explicitly print out all the background information and the sample characteristics dictionary\n", "print(\"Background Information:\")\n", "print(background_info)\n", "print(\"Sample Characteristics Dictionary:\")\n", "print(sample_characteristics_dict)\n" ] }, { "cell_type": "markdown", "id": "8579ee2a", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "b0a5ab06", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:16:06.290508Z", "iopub.status.busy": "2025-03-25T06:16:06.290402Z", "iopub.status.idle": "2025-03-25T06:16:06.300925Z", "shell.execute_reply": "2025-03-25T06:16:06.300611Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of selected clinical data:\n", "{0: [0.0, 19.0, 0.0], 1: [1.0, 22.0, 1.0], 2: [nan, 20.0, nan], 3: [nan, 18.0, nan], 4: [nan, 23.0, nan], 5: [nan, 21.0, nan], 6: [nan, 17.0, nan], 7: [nan, 16.0, nan], 8: [nan, 15.0, nan], 9: [nan, 24.0, nan], 10: [nan, 26.0, nan], 11: [nan, nan, nan], 12: [nan, nan, nan], 13: [nan, nan, nan], 14: [nan, nan, nan], 15: [nan, nan, nan], 16: [nan, nan, nan], 17: [nan, nan, nan], 18: [nan, nan, nan], 19: [nan, nan, nan], 20: [nan, nan, nan]}\n", "Clinical data saved to ../../output/preprocess/Post-Traumatic_Stress_Disorder/clinical_data/GSE77164.csv\n" ] } ], "source": [ "# Analyze the dataset and extract clinical features\n", "\n", "# 1. Gene Expression Data Availability\n", "# Examining cd3d, cd3e, cd4, cd8a, etc. in the sample characteristics\n", "# These are gene expression values, and this is likely gene expression data\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "\n", "# PTS (Post-Traumatic Stress) is our trait of interest (row 6)\n", "trait_row = 6 # PTS (Post-Traumatic Stress)\n", "age_row = 2 # Age data available in row 2\n", "gender_row = 1 # Gender data is in row 1 (female: 0/1)\n", "\n", "# 2.2 Data Type Conversion Functions\n", "\n", "def convert_trait(value):\n", " \"\"\"Convert PTS (Post-Traumatic Stress) data to binary values.\"\"\"\n", " if value is None:\n", " return None\n", " if \":\" in value:\n", " value = value.split(\":\")[1].strip()\n", " try:\n", " # The pts values are already 0 and 1\n", " return int(value)\n", " except (ValueError, TypeError):\n", " return None\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age data to continuous values.\"\"\"\n", " if value is None:\n", " return None\n", " if \":\" in value:\n", " value = value.split(\":\")[1].strip()\n", " try:\n", " return int(value)\n", " except (ValueError, TypeError):\n", " return None\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender data to binary values (0=female, 1=male).\"\"\"\n", " if value is None:\n", " return None\n", " if \":\" in value:\n", " value = value.split(\":\")[1].strip()\n", " # Based on the data, 'female: 1' means female, 'female: 0' means male\n", " try:\n", " # Invert the value since 1 corresponds to female in the data\n", " # but we want 0=female, 1=male\n", " return 1 - int(value)\n", " except (ValueError, TypeError):\n", " return None\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Trait data is available (trait_row is not None)\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. Clinical Feature Extraction\n", "if trait_row is not None:\n", " # Create clinical data from the sample characteristics dictionary\n", " sample_characteristics = {\n", " 0: ['childsoldier: 0', 'childsoldier: 1'],\n", " 1: ['female: 1', 'female: 0'],\n", " 2: ['age: 19', 'age: 22', 'age: 20', 'age: 18', 'age: 23', 'age: 21', 'age: 17', 'age: 16', 'age: 15', 'age: 24', 'age: 26'],\n", " 3: ['ethnicminority: 0', 'ethnicminority: 1'],\n", " 4: ['castelow: 1', 'castelow: 0'],\n", " 5: ['educationlevel: 3', 'educationlevel: 2', 'educationlevel: 5', 'educationlevel: 4', 'educationlevel: 0', 'educationlevel: 6', 'educationlevel: 1'],\n", " 6: ['pts: 0', 'pts: 1'],\n", " 7: ['resilience: 6', 'resilience: 12', 'resilience: 11', 'resilience: 19', 'resilience: 7', 'resilience: 15', 'resilience: 14', 'resilience: 13', 'resilience: 9', 'resilience: 21', 'resilience: 8', 'resilience: 16', 'resilience: 10', 'resilience: 4', 'resilience: 17', 'resilience: 18', 'resilience: 20', 'resilience: 5', 'resilience: 22', 'resilience: 2', 'resilience: 24']\n", " }\n", " \n", " # Convert sample characteristics into a DataFrame\n", " clinical_data = pd.DataFrame.from_dict(sample_characteristics, orient='index')\n", " \n", " # Extract clinical features using the geo_select_clinical_features function\n", " selected_clinical_df = geo_select_clinical_features(\n", " clinical_df=clinical_data,\n", " trait=trait,\n", " trait_row=trait_row,\n", " convert_trait=convert_trait,\n", " age_row=age_row,\n", " convert_age=convert_age,\n", " gender_row=gender_row,\n", " convert_gender=convert_gender\n", " )\n", " \n", " # Preview the clinical data\n", " print(\"Preview of selected clinical data:\")\n", " print(preview_df(selected_clinical_df))\n", " \n", " # Save the clinical data to the specified output file\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " selected_clinical_df.to_csv(out_clinical_data_file, index=False)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "4354de07", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "3e1a0bb1", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:16:06.301978Z", "iopub.status.busy": "2025-03-25T06:16:06.301877Z", "iopub.status.idle": "2025-03-25T06:16:07.038082Z", "shell.execute_reply": "2025-03-25T06:16:07.037701Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "First 20 gene/probe identifiers:\n", "Index(['7A5', 'A1BG', 'A1CF', 'A26C3', 'A2BP1', 'A2LD1', 'A2M', 'A2ML1',\n", " 'A3GALT2', 'A4GALT', 'A4GNT', 'AAA1', 'AAAS', 'AACS', 'AACSL', 'AADAC',\n", " 'AADACL1', 'AADACL2', 'AADACL3', 'AADACL4'],\n", " dtype='object', name='ID')\n", "\n", "Gene data dimensions: 34271 genes × 254 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": "e020b09d", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "b6d33893", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:16:07.039423Z", "iopub.status.busy": "2025-03-25T06:16:07.039311Z", "iopub.status.idle": "2025-03-25T06:16:07.041361Z", "shell.execute_reply": "2025-03-25T06:16:07.041060Z" } }, "outputs": [], "source": [ "# Based on the gene identifiers shown, let's analyze if they need mapping:\n", "\n", "# The identifiers include recognized human gene symbols like:\n", "# - A1BG (Alpha-1-B Glycoprotein)\n", "# - A2M (Alpha-2-Macroglobulin)\n", "# - AAAS (Aladin WD Repeat Nucleoporin)\n", "# - AACS (Acetoacetyl-CoA Synthetase)\n", "\n", "# These appear to be standard HUGO Gene Nomenclature Committee (HGNC) symbols\n", "# for human genes, not probe IDs that would need mapping.\n", "\n", "# Some identifiers like 7A5, AAA1 are less conventional but may be alternative gene symbols\n", "# or specific transcript variants. However, the majority appear to be standard gene symbols.\n", "\n", "# Therefore:\n", "requires_gene_mapping = False\n" ] }, { "cell_type": "markdown", "id": "6b16be5a", "metadata": {}, "source": [ "### Step 5: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 6, "id": "09cf8f7f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:16:07.042563Z", "iopub.status.busy": "2025-03-25T06:16:07.042463Z", "iopub.status.idle": "2025-03-25T06:16:35.845581Z", "shell.execute_reply": "2025-03-25T06:16:35.844931Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalizing gene symbols...\n", "Gene data shape after normalization: (20593, 254)\n", "First 5 normalized gene symbols: ['A1BG', 'A1BG-AS1', 'A1CF', 'A2M', 'A2ML1']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Post-Traumatic_Stress_Disorder/gene_data/GSE77164.csv\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Preview of clinical data:\n", "{'GSM2045182': [0.0, 19.0, 0.0], 'GSM2045183': [0.0, 22.0, 1.0], 'GSM2045184': [0.0, 20.0, 1.0], 'GSM2045185': [0.0, 18.0, 1.0], 'GSM2045186': [0.0, 22.0, 1.0], 'GSM2045187': [0.0, 23.0, 0.0], 'GSM2045188': [1.0, 21.0, 1.0], 'GSM2045189': [0.0, 22.0, 0.0], 'GSM2045190': [0.0, 19.0, 0.0], 'GSM2045191': [0.0, 22.0, 1.0], 'GSM2045192': [0.0, 20.0, 1.0], 'GSM2045193': [0.0, 18.0, 1.0], 'GSM2045194': [0.0, 20.0, 1.0], 'GSM2045195': [0.0, 21.0, 0.0], 'GSM2045196': [0.0, 21.0, 1.0], 'GSM2045197': [0.0, 21.0, 1.0], 'GSM2045198': [0.0, 18.0, 1.0], 'GSM2045199': [0.0, 22.0, 1.0], 'GSM2045200': [0.0, 23.0, 1.0], 'GSM2045201': [1.0, 22.0, 0.0], 'GSM2045202': [0.0, 17.0, 0.0], 'GSM2045203': [0.0, 19.0, 1.0], 'GSM2045204': [1.0, 18.0, 0.0], 'GSM2045205': [0.0, 19.0, 0.0], 'GSM2045206': [0.0, 19.0, 0.0], 'GSM2045207': [0.0, 21.0, 1.0], 'GSM2045208': [0.0, 20.0, 1.0], 'GSM2045209': [0.0, 21.0, 1.0], 'GSM2045210': [1.0, 20.0, 0.0], 'GSM2045211': [0.0, 21.0, 1.0], 'GSM2045212': [0.0, 19.0, 1.0], 'GSM2045213': [0.0, 17.0, 0.0], 'GSM2045214': [0.0, 19.0, 1.0], 'GSM2045215': [0.0, 20.0, 1.0], 'GSM2045216': [0.0, 21.0, 1.0], 'GSM2045217': [0.0, 20.0, 0.0], 'GSM2045218': [0.0, 19.0, 0.0], 'GSM2045219': [0.0, 16.0, 0.0], 'GSM2045220': [0.0, 18.0, 0.0], 'GSM2045221': [0.0, 20.0, 0.0], 'GSM2045222': [1.0, 21.0, 0.0], 'GSM2045223': [0.0, 19.0, 0.0], 'GSM2045224': [1.0, 19.0, 0.0], 'GSM2045225': [0.0, 23.0, 1.0], 'GSM2045226': [0.0, 21.0, 1.0], 'GSM2045227': [0.0, 20.0, 1.0], 'GSM2045228': [0.0, 20.0, 0.0], 'GSM2045229': [0.0, 21.0, 0.0], 'GSM2045230': [1.0, 21.0, 0.0], 'GSM2045231': [0.0, 22.0, 0.0], 'GSM2045232': [0.0, 21.0, 0.0], 'GSM2045233': [0.0, 19.0, 1.0], 'GSM2045234': [0.0, 21.0, 1.0], 'GSM2045235': [0.0, 21.0, 1.0], 'GSM2045236': [1.0, 19.0, 1.0], 'GSM2045237': [0.0, 19.0, 0.0], 'GSM2045238': [0.0, 22.0, 0.0], 'GSM2045239': [1.0, 20.0, 0.0], 'GSM2045240': [0.0, 19.0, 0.0], 'GSM2045241': [0.0, 15.0, 0.0], 'GSM2045242': [0.0, 24.0, 0.0], 'GSM2045243': [0.0, 17.0, 0.0], 'GSM2045244': [1.0, 22.0, 0.0], 'GSM2045245': [0.0, 19.0, 0.0], 'GSM2045246': [0.0, 20.0, 1.0], 'GSM2045247': [0.0, 22.0, 1.0], 'GSM2045248': [0.0, 22.0, 1.0], 'GSM2045249': [0.0, 22.0, 0.0], 'GSM2045250': [0.0, 22.0, 0.0], 'GSM2045251': [0.0, 18.0, 0.0], 'GSM2045252': [0.0, 23.0, 1.0], 'GSM2045253': [0.0, 18.0, 0.0], 'GSM2045254': [0.0, 17.0, 0.0], 'GSM2045255': [0.0, 20.0, 0.0], 'GSM2045256': [0.0, 18.0, 0.0], 'GSM2045257': [1.0, 22.0, 0.0], 'GSM2045258': [0.0, 20.0, 1.0], 'GSM2045259': [0.0, 20.0, 1.0], 'GSM2045260': [0.0, 19.0, 1.0], 'GSM2045261': [0.0, 18.0, 1.0], 'GSM2045262': [0.0, 20.0, 0.0], 'GSM2045263': [0.0, 17.0, 0.0], 'GSM2045264': [0.0, 20.0, 0.0], 'GSM2045265': [0.0, 19.0, 0.0], 'GSM2045266': [1.0, 20.0, 0.0], 'GSM2045267': [0.0, 23.0, 1.0], 'GSM2045268': [0.0, 19.0, 1.0], 'GSM2045269': [0.0, 21.0, 1.0], 'GSM2045270': [0.0, 22.0, 0.0], 'GSM2045271': [0.0, 19.0, 1.0], 'GSM2045272': [0.0, 19.0, 1.0], 'GSM2045273': [0.0, 20.0, 1.0], 'GSM2045274': [0.0, 19.0, 0.0], 'GSM2045275': [0.0, 21.0, 1.0], 'GSM2045276': [0.0, 22.0, 0.0], 'GSM2045277': [0.0, 20.0, 0.0], 'GSM2045278': [1.0, 22.0, 1.0], 'GSM2045279': [0.0, 20.0, 0.0], 'GSM2045280': [0.0, 20.0, 0.0], 'GSM2045281': [1.0, 21.0, 1.0], 'GSM2045282': [0.0, 19.0, 0.0], 'GSM2045283': [0.0, 22.0, 1.0], 'GSM2045284': [1.0, 20.0, 0.0], 'GSM2045285': [0.0, 20.0, 1.0], 'GSM2045286': [1.0, 17.0, 0.0], 'GSM2045287': [0.0, 21.0, 0.0], 'GSM2045288': [0.0, 21.0, 1.0], 'GSM2045289': [0.0, 22.0, 0.0], 'GSM2045290': [0.0, 20.0, 1.0], 'GSM2045291': [0.0, 23.0, 1.0], 'GSM2045292': [0.0, 22.0, 0.0], 'GSM2045293': [0.0, 20.0, 1.0], 'GSM2045294': [0.0, 19.0, 1.0], 'GSM2045295': [0.0, 23.0, 1.0], 'GSM2045296': [0.0, 22.0, 0.0], 'GSM2045297': [0.0, 22.0, 0.0], 'GSM2045298': [0.0, 22.0, 1.0], 'GSM2045299': [0.0, 19.0, 1.0], 'GSM2045300': [0.0, 20.0, 0.0], 'GSM2045301': [0.0, 20.0, 1.0], 'GSM2045302': [0.0, 20.0, 0.0], 'GSM2045303': [0.0, 22.0, 1.0], 'GSM2045304': [0.0, 21.0, 0.0], 'GSM2045305': [0.0, 17.0, 0.0], 'GSM2045306': [0.0, 20.0, 1.0], 'GSM2045307': [1.0, 20.0, 0.0], 'GSM2045308': [0.0, 18.0, 1.0], 'GSM2045309': [0.0, 20.0, 1.0], 'GSM2045310': [1.0, 20.0, 1.0], 'GSM2045311': [1.0, 20.0, 1.0], 'GSM2045312': [0.0, 24.0, 1.0], 'GSM2045313': [0.0, 20.0, 1.0], 'GSM2045314': [0.0, 21.0, 1.0], 'GSM2045315': [0.0, 18.0, 1.0], 'GSM2045316': [0.0, 21.0, 1.0], 'GSM2045317': [0.0, 20.0, 0.0], 'GSM2045318': [0.0, 19.0, 1.0], 'GSM2045319': [0.0, 19.0, 1.0], 'GSM2045320': [0.0, 23.0, 1.0], 'GSM2045321': [0.0, 18.0, 1.0], 'GSM2045322': [0.0, 20.0, 0.0], 'GSM2045323': [0.0, 20.0, 1.0], 'GSM2045324': [0.0, 20.0, 1.0], 'GSM2045325': [0.0, 18.0, 1.0], 'GSM2045326': [0.0, 21.0, 1.0], 'GSM2045327': [0.0, 20.0, 1.0], 'GSM2045328': [0.0, 20.0, 1.0], 'GSM2045329': [0.0, 22.0, 1.0], 'GSM2045330': [0.0, 22.0, 1.0], 'GSM2045331': [0.0, 20.0, 1.0], 'GSM2045332': [0.0, 21.0, 1.0], 'GSM2045333': [0.0, 22.0, 0.0], 'GSM2045334': [0.0, 19.0, 1.0], 'GSM2045335': [0.0, 19.0, 0.0], 'GSM2045336': [0.0, 21.0, 0.0], 'GSM2045337': [0.0, 19.0, 0.0], 'GSM2045338': [1.0, 21.0, 0.0], 'GSM2045339': [0.0, 22.0, 0.0], 'GSM2045340': [0.0, 19.0, 1.0], 'GSM2045341': [0.0, 18.0, 1.0], 'GSM2045342': [0.0, 19.0, 0.0], 'GSM2045343': [0.0, 22.0, 0.0], 'GSM2045344': [0.0, 21.0, 0.0], 'GSM2045345': [0.0, 21.0, 0.0], 'GSM2045346': [0.0, 18.0, 1.0], 'GSM2045347': [0.0, 21.0, 0.0], 'GSM2045348': [1.0, 19.0, 0.0], 'GSM2045349': [0.0, 21.0, 0.0], 'GSM2045350': [0.0, 21.0, 1.0], 'GSM2045351': [0.0, 21.0, 1.0], 'GSM2045352': [0.0, 20.0, 0.0], 'GSM2045353': [0.0, 20.0, 1.0], 'GSM2045354': [0.0, 19.0, 1.0], 'GSM2045355': [1.0, 21.0, 0.0], 'GSM2045356': [0.0, 22.0, 1.0], 'GSM2045357': [0.0, 21.0, 1.0], 'GSM2045358': [0.0, 22.0, 0.0], 'GSM2045359': [0.0, 21.0, 1.0], 'GSM2045360': [0.0, 21.0, 1.0], 'GSM2045361': [0.0, 20.0, 1.0], 'GSM2045362': [0.0, 20.0, 0.0], 'GSM2045363': [0.0, 19.0, 1.0], 'GSM2045364': [0.0, 21.0, 1.0], 'GSM2045365': [0.0, 16.0, 1.0], 'GSM2045366': [0.0, 21.0, 1.0], 'GSM2045367': [0.0, 17.0, 1.0], 'GSM2045368': [0.0, 21.0, 1.0], 'GSM2045369': [0.0, 21.0, 1.0], 'GSM2045370': [0.0, 21.0, 0.0], 'GSM2045371': [0.0, 20.0, 0.0], 'GSM2045372': [0.0, 22.0, 1.0], 'GSM2045373': [0.0, 17.0, 1.0], 'GSM2045374': [0.0, 19.0, 1.0], 'GSM2045375': [0.0, 19.0, 0.0], 'GSM2045376': [0.0, 17.0, 1.0], 'GSM2045377': [0.0, 16.0, 1.0], 'GSM2045378': [0.0, 18.0, 1.0], 'GSM2045379': [0.0, 20.0, 1.0], 'GSM2045380': [0.0, 19.0, 1.0], 'GSM2045381': [0.0, 22.0, 1.0]}\n", "Clinical data saved to ../../output/preprocess/Post-Traumatic_Stress_Disorder/clinical_data/GSE77164.csv\n", "Linked data shape: (254, 20596)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (254, 20596)\n", "\n", "Checking for bias in the trait variable:\n", "For the feature 'Post-Traumatic_Stress_Disorder', the least common label is '1.0' with 29 occurrences. This represents 11.42% 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%: 19.0\n", " 50% (Median): 20.0\n", " 75%: 21.75\n", "Min: 15.0\n", "Max: 26.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 113 occurrences. This represents 44.49% of the dataset.\n", "The distribution of the feature 'Gender' in this dataset is fine.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Post-Traumatic_Stress_Disorder/GSE77164.csv\n" ] } ], "source": [ "# 1. Re-extract the gene expression data to ensure we have it properly defined\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "gene_data = get_genetic_data(matrix_file)\n", "\n", "# Since requires_gene_mapping = False from Step 4, we'll skip mapping and directly normalize gene symbols\n", "print(\"Normalizing gene symbols...\")\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Gene data shape after normalization: {normalized_gene_data.shape}\")\n", "print(f\"First 5 normalized gene symbols: {normalized_gene_data.index[:5].tolist() if len(normalized_gene_data) > 0 else 'No genes after normalization'}\")\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. Re-extract clinical data - using the same approach as previous step\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", "# From Step 2, we identified the correct rows\n", "trait_row = 6 # PTS (Post-Traumatic Stress)\n", "age_row = 2 # Age data available in row 2\n", "gender_row = 1 # Gender data is in row 1 (female: 0/1)\n", "\n", "def convert_trait(value):\n", " \"\"\"Convert PTS (Post-Traumatic Stress) data to binary values.\"\"\"\n", " if value is None:\n", " return None\n", " if \":\" in value:\n", " value = value.split(\":\")[1].strip()\n", " try:\n", " # The pts values are already 0 and 1\n", " return int(value)\n", " except (ValueError, TypeError):\n", " return None\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age data to continuous values.\"\"\"\n", " if value is None:\n", " return None\n", " if \":\" in value:\n", " value = value.split(\":\")[1].strip()\n", " try:\n", " return int(value)\n", " except (ValueError, TypeError):\n", " return None\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender data to binary values (0=female, 1=male).\"\"\"\n", " if value is None:\n", " return None\n", " if \":\" in value:\n", " value = value.split(\":\")[1].strip()\n", " # Based on the data, 'female: 1' means female, 'female: 0' means male\n", " try:\n", " # Invert the value since 1 corresponds to female in the data\n", " # but we want 0=female, 1=male\n", " return 1 - int(value)\n", " except (ValueError, TypeError):\n", " return None\n", " return None\n", "\n", "# Extract clinical features with correct row indices and conversion functions\n", "selected_clinical_df = geo_select_clinical_features(\n", " clinical_df=clinical_data,\n", " trait=trait,\n", " trait_row=trait_row,\n", " convert_trait=convert_trait,\n", " age_row=age_row,\n", " convert_age=convert_age,\n", " gender_row=gender_row,\n", " convert_gender=convert_gender\n", ")\n", "\n", "# Debug: Show preview of clinical data\n", "print(\"Preview of clinical data:\")\n", "print(preview_df(selected_clinical_df))\n", "\n", "# Save clinical data\n", "os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", "selected_clinical_df.to_csv(out_clinical_data_file)\n", "print(f\"Clinical data saved to {out_clinical_data_file}\")\n", "\n", "# 3. Link clinical and genetic data\n", "linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "\n", "# 4. 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", "# 5. Determine if trait is biased\n", "print(\"\\nChecking for bias in the trait variable:\")\n", "# The trait in this dataset is binary (PTSD vs No PTSD)\n", "is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "\n", "# 6. Conduct final quality validation\n", "is_trait_available = True # We confirmed trait data is 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=is_trait_available,\n", " is_biased=is_biased,\n", " df=linked_data,\n", " note=\"Dataset studies gene expression in child soldiers with assessment of PTSD symptoms and psychological resilience.\"\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 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 }