{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "4ecf26ac", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:15:46.786320Z", "iopub.status.busy": "2025-03-25T06:15:46.786141Z", "iopub.status.idle": "2025-03-25T06:15:46.952482Z", "shell.execute_reply": "2025-03-25T06:15:46.952106Z" } }, "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 = \"GSE67663\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Post-Traumatic_Stress_Disorder\"\n", "in_cohort_dir = \"../../input/GEO/Post-Traumatic_Stress_Disorder/GSE67663\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Post-Traumatic_Stress_Disorder/GSE67663.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Post-Traumatic_Stress_Disorder/gene_data/GSE67663.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Post-Traumatic_Stress_Disorder/clinical_data/GSE67663.csv\"\n", "json_path = \"../../output/preprocess/Post-Traumatic_Stress_Disorder/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "cb5771e8", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "1422b09b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:15:46.953963Z", "iopub.status.busy": "2025-03-25T06:15:46.953816Z", "iopub.status.idle": "2025-03-25T06:15:47.095216Z", "shell.execute_reply": "2025-03-25T06:15:47.094882Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Gene expression profiles in comorbid PTSD and depression\"\n", "!Series_summary\t\"This is a genome-wide differential gene expression survey of cases of comorbid PTSD and depression versus controls\"\n", "!Series_overall_design\t\"We compared genome-wide gene expression profiles of cases of comorbid PTSD and depression to those of controls, adjusting for sex, age, and ancestry via genotypic principal components\"\n", "Sample Characteristics Dictionary:\n", "{0: ['Sex: female', 'Sex: male'], 1: ['age: 44', 'age: 74', 'age: 47', 'age: 38', 'age: 37', 'age: 48', 'age: 52', 'age: 56', 'age: 21', 'age: 20', 'age: 54', 'age: 55', 'age: 36', 'age: 31', 'age: 39', 'age: 40', 'age: 57', 'age: 45', 'age: 50', 'age: 24', 'age: 30', 'age: 26', 'age: 43', 'age: 34', 'age: 49', 'age: 19', 'age: 18', 'age: 60', 'age: 27', 'age: 29'], 2: ['comorbid ptsd and depression status: 1', 'comorbid ptsd and depression status: 0'], 3: ['rin: 6.1', 'rin: 6.7', 'rin: 7.3', 'rin: 6.3', 'rin: 7.5', 'rin: 7', 'rin: 6.9', 'rin: 6.8', 'rin: 7.6', 'rin: 7.1', 'rin: 6.4', 'rin: 7.2', 'rin: 8.5', 'rin: 8.4', 'rin: 7.7', 'rin: 7.4', 'rin: 7.9', 'rin: 6.6', 'rin: 6', 'rin: 6.2', 'rin: 6.5', 'rin: 8.9', 'rin: 7.8', 'rin: 9.2', 'rin: 8.7', 'rin: 8.3', 'rin: 8.2', 'rin: 8', 'rin: 9', 'rin: 8.6'], 4: ['hybridization batch: U', 'hybridization batch: T', 'hybridization batch: V', 'hybridization batch: X', 'hybridization batch: W', 'hybridization batch: Y', 'hybridization batch: Z'], 5: ['principal component 1: 0.00064052', 'principal component 1: -0.004024057', 'principal component 1: 0.00400313', 'principal component 1: 0.005182475', 'principal component 1: -0.004299016', 'principal component 1: 0.020441295', 'principal component 1: -0.005166542', 'principal component 1: -0.013896797', 'principal component 1: -0.012590207', 'principal component 1: -0.005074668', 'principal component 1: 0.001896521', 'principal component 1: -0.009531315', 'principal component 1: 0.006986564', 'principal component 1: -0.009590131', 'principal component 1: 0.00369822', 'principal component 1: -0.005452484', 'principal component 1: -0.012004578', 'principal component 1: -0.004129164', 'principal component 1: 0.068929457', 'principal component 1: 0.036987418', 'principal component 1: -0.00713661', 'principal component 1: -0.004882323', 'principal component 1: -0.006662223', 'principal component 1: -0.008174661', 'principal component 1: -0.005462835', 'principal component 1: 0.007218702', 'principal component 1: -0.009335024', 'principal component 1: -0.000642624', 'principal component 1: 0.008406524', 'principal component 1: -0.011182903'], 6: ['principal component 2: -0.009636385', 'principal component 2: -7.37e-05', 'principal component 2: -0.001420011', 'principal component 2: 0.105402372', 'principal component 2: 0.003823651', 'principal component 2: -0.005525821', 'principal component 2: -0.011394157', 'principal component 2: -0.000823133', 'principal component 2: -0.008599074', 'principal component 2: 0.008091664', 'principal component 2: -0.006029345', 'principal component 2: -0.011084218', 'principal component 2: -0.002302079', 'principal component 2: -0.005006827', 'principal component 2: -0.005960422', 'principal component 2: 0.019529965', 'principal component 2: 0.004408126', 'principal component 2: 0.003817923', 'principal component 2: -0.028726053', 'principal component 2: 0.008670096', 'principal component 2: -0.002574051', 'principal component 2: 0.002944092', 'principal component 2: 0.009572324', 'principal component 2: 0.003282601', 'principal component 2: 0.009844893', 'principal component 2: 0.034626817', 'principal component 2: 0.015969032', 'principal component 2: -0.004556864', 'principal component 2: -0.011744604', 'principal component 2: 0.00037844'], 7: ['principal component 3: 0.018810647', 'principal component 3: -0.013388551', 'principal component 3: -0.002627828', 'principal component 3: -0.01008267', 'principal component 3: -0.003528338', 'principal component 3: -0.043553025', 'principal component 3: -0.012875765', 'principal component 3: -0.007559506', 'principal component 3: -0.016538175', 'principal component 3: 0.005767151', 'principal component 3: 0.003227387', 'principal component 3: -0.025169087', 'principal component 3: 0.052331068', 'principal component 3: -0.027964962', 'principal component 3: -0.008209349', 'principal component 3: 0.012975797', 'principal component 3: -0.016512606', 'principal component 3: 0.03125578', 'principal component 3: 0.003062415', 'principal component 3: -0.028340935', 'principal component 3: -0.014658814', 'principal component 3: -0.016397604', 'principal component 3: -0.010863882', 'principal component 3: 0.016054345', 'principal component 3: 0.004973674', 'principal component 3: -0.013551577', 'principal component 3: -0.023260507', 'principal component 3: -0.003003507', 'principal component 3: -0.009350211', 'principal component 3: -0.002287871'], 8: ['principal component 4: 0.029445582', 'principal component 4: -0.01214791', 'principal component 4: 0.009746017', 'principal component 4: 0.018967498', 'principal component 4: 0.002619033', 'principal component 4: -0.003851752', 'principal component 4: -0.017558693', 'principal component 4: 0.005845702', 'principal component 4: -0.014657264', 'principal component 4: 0.020219537', 'principal component 4: 0.008347379', 'principal component 4: 0.009263676', 'principal component 4: -0.005063147', 'principal component 4: -0.006492829', 'principal component 4: 0.001431845', 'principal component 4: -0.025799787', 'principal component 4: 0.010451212', 'principal component 4: 0.015847339', 'principal component 4: -0.001445286', 'principal component 4: 0.010264738', 'principal component 4: -0.016374807', 'principal component 4: 0.004165921', 'principal component 4: -0.010680726', 'principal component 4: 0.013103456', 'principal component 4: 0.032929024', 'principal component 4: 0.005107311', 'principal component 4: 0.008528535', 'principal component 4: 0.060047055', 'principal component 4: 0.034431674', 'principal component 4: -0.003429314'], 9: ['principal component 5: -0.002988936', 'principal component 5: 0.01930017', 'principal component 5: -0.012250154', 'principal component 5: -0.01717444', 'principal component 5: 0.006075268', 'principal component 5: -0.00141205', 'principal component 5: 0.005984654', 'principal component 5: -0.017679911', 'principal component 5: 0.002501597', 'principal component 5: -0.003372006', 'principal component 5: 0.000127373', 'principal component 5: 0.002850273', 'principal component 5: -0.012942377', 'principal component 5: 0.02107106', 'principal component 5: 0.004667388', 'principal component 5: 0.004840928', 'principal component 5: -0.00064525', 'principal component 5: 0.002850885', 'principal component 5: 0.007188341', 'principal component 5: 0.00848048', 'principal component 5: -0.00278483', 'principal component 5: -0.008191519', 'principal component 5: -0.01643553', 'principal component 5: -0.002725706', 'principal component 5: 0.017524106', 'principal component 5: 0.013500244', 'principal component 5: -0.01906777', 'principal component 5: 0.0049637', 'principal component 5: 0.016701229', 'principal component 5: 0.017446162'], 10: ['tissue: whole peripheral 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": "32572829", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "f9d508f5", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:15:47.096557Z", "iopub.status.busy": "2025-03-25T06:15:47.096439Z", "iopub.status.idle": "2025-03-25T06:15:47.107329Z", "shell.execute_reply": "2025-03-25T06:15:47.107009Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of clinical features:\n", "{0: [nan, nan, 0.0], 1: [nan, 74.0, nan], 2: [1.0, 0.0, nan]}\n", "Clinical data saved to ../../output/preprocess/Post-Traumatic_Stress_Disorder/clinical_data/GSE67663.csv\n" ] } ], "source": [ "import pandas as pd\n", "from typing import Optional, Callable, Dict, Any\n", "import os\n", "import json\n", "\n", "# 1. Gene Expression Data Availability\n", "# Based on background info, this dataset contains genome-wide gene expression profiles\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "\n", "# Trait: Post-Traumatic Stress Disorder\n", "# From the sample characteristics, we can see row 2 has \"comorbid ptsd and depression status\"\n", "trait_row = 2\n", "\n", "# Age: Available in row 1\n", "age_row = 1\n", "\n", "# Gender: Available in row 0 (Sex)\n", "gender_row = 0\n", "\n", "# 2.2 Data Type Conversion Functions\n", "\n", "def convert_trait(value: str) -> Optional[int]:\n", " \"\"\"Convert PTSD trait values to binary format (0/1).\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Based on the sample characteristics, \"comorbid ptsd and depression status: 1\" is case (PTSD)\n", " # and \"comorbid ptsd and depression status: 0\" is control (no PTSD)\n", " if value == '1':\n", " return 1 # PTSD case\n", " elif value == '0':\n", " return 0 # Control\n", " else:\n", " return None # Unknown or invalid value\n", "\n", "def convert_age(value: str) -> Optional[float]:\n", " \"\"\"Convert age values to continuous format.\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon\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: str) -> Optional[int]:\n", " \"\"\"Convert gender values to binary format (0=female, 1=male).\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip().lower()\n", " \n", " if value == 'female':\n", " return 0\n", " elif value == 'male':\n", " return 1\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata for Initial Filtering\n", "# Determine trait data availability (if trait_row is not None)\n", "is_trait_available = trait_row is not None\n", "\n", "# Validate and save cohort info for 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", "# Only if trait_row is not None (i.e., clinical data is available)\n", "if trait_row is not None:\n", " # Create a DataFrame from the sample characteristics dictionary that was shown in the previous step\n", " # First, we need to transform the dictionary into a proper format for DataFrame creation\n", " sample_characteristics = {\n", " 0: ['Sex: female', 'Sex: male'], \n", " 1: ['age: 44', 'age: 74', 'age: 47', 'age: 38', 'age: 37', 'age: 48', 'age: 52', 'age: 56', 'age: 21', 'age: 20', 'age: 54', 'age: 55', 'age: 36', 'age: 31', 'age: 39', 'age: 40', 'age: 57', 'age: 45', 'age: 50', 'age: 24', 'age: 30', 'age: 26', 'age: 43', 'age: 34', 'age: 49', 'age: 19', 'age: 18', 'age: 60', 'age: 27', 'age: 29'], \n", " 2: ['comorbid ptsd and depression status: 1', 'comorbid ptsd and depression status: 0']\n", " # Additional fields from the sample characteristics dictionary are omitted as they're not needed\n", " }\n", " \n", " # Determine the number of samples based on the longest list in the dictionary\n", " num_samples = max(len(values) for values in sample_characteristics.values())\n", " \n", " # Create a DataFrame with one column per row in the sample characteristics\n", " clinical_data = pd.DataFrame()\n", " \n", " # Fill the DataFrame with sample characteristics data\n", " for row_idx, values in sample_characteristics.items():\n", " # Create a series with length = number of samples, filled with the values from the list\n", " # If there are fewer values than samples, the values will be recycled\n", " clinical_data[row_idx] = pd.Series([values[i % len(values)] for i in range(num_samples)])\n", " \n", " # Extract 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 clinical features\n", " preview = preview_df(clinical_features)\n", " print(\"Preview of clinical features:\")\n", " print(preview)\n", " \n", " # Save clinical data to output file\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " clinical_features.to_csv(out_clinical_data_file, index=False)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "5a4cc0d5", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "0c6d7814", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:15:47.108509Z", "iopub.status.busy": "2025-03-25T06:15:47.108401Z", "iopub.status.idle": "2025-03-25T06:15:47.357913Z", "shell.execute_reply": "2025-03-25T06:15:47.357542Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "First 20 gene/probe identifiers:\n", "Index(['ILMN_1343291', 'ILMN_1343295', 'ILMN_1651228', 'ILMN_1651229',\n", " 'ILMN_1651262', 'ILMN_1651278', 'ILMN_1651279', 'ILMN_1651315',\n", " 'ILMN_1651316', 'ILMN_1651328', 'ILMN_1651336', 'ILMN_1651346',\n", " 'ILMN_1651347', 'ILMN_1651354', 'ILMN_1651358', 'ILMN_1651364',\n", " 'ILMN_1651373', 'ILMN_1651378', 'ILMN_1651385', 'ILMN_1651403'],\n", " dtype='object', name='ID')\n", "\n", "Gene data dimensions: 13048 genes × 184 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": "dd453c27", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "342a5781", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:15:47.359308Z", "iopub.status.busy": "2025-03-25T06:15:47.359185Z", "iopub.status.idle": "2025-03-25T06:15:47.361140Z", "shell.execute_reply": "2025-03-25T06:15:47.360817Z" } }, "outputs": [], "source": [ "# Analyzing the gene identifiers\n", "# These are Illumina BeadArray identifiers (ILMN_) which are not human gene symbols\n", "# They are probe IDs for the Illumina microarray platform that need to be mapped to gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "1ad6156f", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "0f1304a0", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:15:47.362323Z", "iopub.status.busy": "2025-03-25T06:15:47.362214Z", "iopub.status.idle": "2025-03-25T06:15:52.622980Z", "shell.execute_reply": "2025-03-25T06:15:52.622608Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['ILMN_1825594', 'ILMN_1810803', 'ILMN_1722532', 'ILMN_1884413', 'ILMN_1906034'], 'Species': ['Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens'], 'Source': ['Unigene', 'RefSeq', 'RefSeq', 'Unigene', 'Unigene'], 'Search_Key': ['ILMN_89282', 'ILMN_35826', 'ILMN_25544', 'ILMN_132331', 'ILMN_105017'], 'Transcript': ['ILMN_89282', 'ILMN_35826', 'ILMN_25544', 'ILMN_132331', 'ILMN_105017'], 'ILMN_Gene': ['HS.388528', 'LOC441782', 'JMJD1A', 'HS.580150', 'HS.540210'], 'Source_Reference_ID': ['Hs.388528', 'XM_497527.2', 'NM_018433.3', 'Hs.580150', 'Hs.540210'], 'RefSeq_ID': [nan, 'XM_497527.2', 'NM_018433.3', nan, nan], 'Unigene_ID': ['Hs.388528', nan, nan, 'Hs.580150', 'Hs.540210'], 'Entrez_Gene_ID': [nan, 441782.0, 55818.0, nan, nan], 'GI': [23525203.0, 89042416.0, 46358420.0, 7376124.0, 5437312.0], 'Accession': ['BU678343', 'XM_497527.2', 'NM_018433.3', 'AW629334', 'AI818233'], 'Symbol': [nan, 'LOC441782', 'JMJD1A', nan, nan], 'Protein_Product': [nan, 'XP_497527.2', 'NP_060903.2', nan, nan], 'Array_Address_Id': [1740241.0, 1850750.0, 1240504.0, 4050487.0, 2190598.0], 'Probe_Type': ['S', 'S', 'S', 'S', 'S'], 'Probe_Start': [349.0, 902.0, 4359.0, 117.0, 304.0], 'SEQUENCE': ['CTCTCTAAAGGGACAACAGAGTGGACAGTCAAGGAACTCCACATATTCAT', 'GGGGTCAAGCCCAGGTGAAATGTGGATTGGAAAAGTGCTTCCCTTGCCCC', 'CCAGGCTGTAAAAGCAAAACCTCGTATCAGCTCTGGAACAATACCTGCAG', 'CCAGACAGGAAGCATCAAGCCCTTCAGGAAAGAATATGCGAGAGTGCTGC', 'TGTGCAGAAAGCTGATGGAAGGGAGAAAGAATGGAAGTGGGTCACACAGC'], 'Chromosome': [nan, nan, '2', nan, nan], 'Probe_Chr_Orientation': [nan, nan, '+', nan, nan], 'Probe_Coordinates': [nan, nan, '86572991-86573040', nan, nan], 'Cytoband': [nan, nan, '2p11.2e', nan, nan], 'Definition': ['UI-CF-EC0-abi-c-12-0-UI.s1 UI-CF-EC0 Homo sapiens cDNA clone UI-CF-EC0-abi-c-12-0-UI 3, mRNA sequence', 'PREDICTED: Homo sapiens similar to spectrin domain with coiled-coils 1 (LOC441782), mRNA.', 'Homo sapiens jumonji domain containing 1A (JMJD1A), mRNA.', 'hi56g05.x1 Soares_NFL_T_GBC_S1 Homo sapiens cDNA clone IMAGE:2976344 3, mRNA sequence', 'wk77d04.x1 NCI_CGAP_Pan1 Homo sapiens cDNA clone IMAGE:2421415 3, mRNA sequence'], 'Ontology_Component': [nan, nan, 'nucleus [goid 5634] [evidence IEA]', nan, nan], 'Ontology_Process': [nan, nan, 'chromatin modification [goid 16568] [evidence IEA]; transcription [goid 6350] [evidence IEA]; regulation of transcription, DNA-dependent [goid 6355] [evidence IEA]', nan, nan], 'Ontology_Function': [nan, nan, 'oxidoreductase activity [goid 16491] [evidence IEA]; oxidoreductase activity, acting on single donors with incorporation of molecular oxygen, incorporation of two atoms of oxygen [goid 16702] [evidence IEA]; zinc ion binding [goid 8270] [evidence IEA]; metal ion binding [goid 46872] [evidence IEA]; iron ion binding [goid 5506] [evidence IEA]', nan, nan], 'Synonyms': [nan, nan, 'JHMD2A; JMJD1; TSGA; KIAA0742; DKFZp686A24246; DKFZp686P07111', nan, nan], 'GB_ACC': ['BU678343', 'XM_497527.2', 'NM_018433.3', 'AW629334', 'AI818233']}\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": "e53a5e34", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "b3bba626", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:15:52.624382Z", "iopub.status.busy": "2025-03-25T06:15:52.624249Z", "iopub.status.idle": "2025-03-25T06:15:53.785029Z", "shell.execute_reply": "2025-03-25T06:15:53.784659Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "After mapping, gene data dimensions: 8689 genes × 184 samples\n", "First 10 gene symbols:\n", "Index(['A1BG', 'AACS', 'AAMDC', 'AAMP', 'AAR2', 'AARS1', 'AARSD1', 'AASDH',\n", " 'AASDHPPT', 'AATF'],\n", " dtype='object', name='Gene')\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Post-Traumatic_Stress_Disorder/gene_data/GSE67663.csv\n" ] } ], "source": [ "# 1. Identify which columns in gene_annotation contain probe IDs and gene symbols\n", "# Based on the preview, 'ID' contains probe IDs matching our gene expression data\n", "# 'Symbol' contains gene symbols\n", "\n", "# 2. Get the gene mapping dataframe by extracting the relevant columns\n", "gene_mapping = get_gene_mapping(gene_annotation, 'ID', 'Symbol')\n", "\n", "# 3. Apply the gene mapping to convert probe-level measurements to gene-level expression data\n", "# The function handles probes that map to multiple genes by dividing expression equally\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "\n", "# Normalize gene symbols in the index for consistency\n", "gene_data = normalize_gene_symbols_in_index(gene_data)\n", "\n", "# Print information about the gene data after mapping\n", "print(f\"\\nAfter mapping, gene data dimensions: {gene_data.shape[0]} genes × {gene_data.shape[1]} samples\")\n", "print(\"First 10 gene symbols:\")\n", "print(gene_data.index[:10])\n", "\n", "# Save the gene expression data to file\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": "39112a54", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "47c1d02a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:15:53.786392Z", "iopub.status.busy": "2025-03-25T06:15:53.786265Z", "iopub.status.idle": "2025-03-25T06:16:04.901646Z", "shell.execute_reply": "2025-03-25T06:16:04.901265Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape after normalization: (8689, 184)\n", "First 5 normalized gene symbols: ['A1BG', 'AACS', 'AAMDC', 'AAMP', 'AAR2']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Post-Traumatic_Stress_Disorder/gene_data/GSE67663.csv\n", "Preview of clinical data:\n", "{'GSM1652966': [1.0, 44.0, 0.0], 'GSM1652967': [0.0, 74.0, 0.0], 'GSM1652968': [1.0, 47.0, 1.0], 'GSM1652969': [1.0, 38.0, 0.0], 'GSM1652970': [1.0, 37.0, 0.0], 'GSM1652971': [0.0, 48.0, 0.0], 'GSM1652972': [0.0, 52.0, 1.0], 'GSM1652973': [1.0, 56.0, 0.0], 'GSM1652974': [0.0, 21.0, 0.0], 'GSM1652975': [0.0, 20.0, 0.0], 'GSM1652976': [0.0, 54.0, 0.0], 'GSM1652977': [1.0, 55.0, 1.0], 'GSM1652978': [0.0, 56.0, 0.0], 'GSM1652979': [0.0, 54.0, 0.0], 'GSM1652980': [1.0, 47.0, 1.0], 'GSM1652981': [0.0, 36.0, 0.0], 'GSM1652982': [0.0, 56.0, 0.0], 'GSM1652983': [0.0, 44.0, 1.0], 'GSM1652984': [1.0, 31.0, 0.0], 'GSM1652985': [1.0, 39.0, 0.0], 'GSM1652986': [1.0, 40.0, 0.0], 'GSM1652987': [1.0, 57.0, 1.0], 'GSM1652988': [1.0, 39.0, 0.0], 'GSM1652989': [1.0, 47.0, 1.0], 'GSM1652990': [1.0, 39.0, 1.0], 'GSM1652991': [1.0, 45.0, 0.0], 'GSM1652992': [1.0, 50.0, 0.0], 'GSM1652993': [1.0, 24.0, 1.0], 'GSM1652994': [0.0, 50.0, 0.0], 'GSM1652995': [1.0, 45.0, 0.0], 'GSM1652996': [0.0, 30.0, 0.0], 'GSM1652997': [0.0, 39.0, 0.0], 'GSM1652998': [0.0, 47.0, 1.0], 'GSM1652999': [1.0, 26.0, 0.0], 'GSM1653000': [1.0, 20.0, 0.0], 'GSM1653001': [1.0, 52.0, 0.0], 'GSM1653002': [1.0, 43.0, 0.0], 'GSM1653003': [1.0, 50.0, 0.0], 'GSM1653004': [0.0, 34.0, 0.0], 'GSM1653005': [1.0, 50.0, 0.0], 'GSM1653006': [1.0, 49.0, 0.0], 'GSM1653007': [0.0, 19.0, 0.0], 'GSM1653008': [0.0, 18.0, 0.0], 'GSM1653009': [0.0, 60.0, 1.0], 'GSM1653010': [1.0, 45.0, 0.0], 'GSM1653011': [1.0, 27.0, 0.0], 'GSM1653012': [0.0, 29.0, 0.0], 'GSM1653013': [0.0, 18.0, 0.0], 'GSM1653014': [0.0, 57.0, 0.0], 'GSM1653015': [1.0, 46.0, 1.0], 'GSM1653016': [0.0, 59.0, 0.0], 'GSM1653017': [0.0, 52.0, 0.0], 'GSM1653018': [0.0, 46.0, 0.0], 'GSM1653019': [1.0, 53.0, 0.0], 'GSM1653020': [0.0, 50.0, 0.0], 'GSM1653021': [0.0, 21.0, 1.0], 'GSM1653022': [0.0, 57.0, 0.0], 'GSM1653023': [1.0, 37.0, 1.0], 'GSM1653024': [0.0, 38.0, 0.0], 'GSM1653025': [1.0, 32.0, 0.0], 'GSM1653026': [1.0, 51.0, 0.0], 'GSM1653027': [0.0, 24.0, 0.0], 'GSM1653028': [1.0, 56.0, 1.0], 'GSM1653029': [1.0, 49.0, 0.0], 'GSM1653030': [0.0, 61.0, 1.0], 'GSM1653031': [1.0, 63.0, 1.0], 'GSM1653032': [1.0, 45.0, 0.0], 'GSM1653033': [1.0, 30.0, 0.0], 'GSM1653034': [1.0, 43.0, 0.0], 'GSM1653035': [1.0, 50.0, 0.0], 'GSM1653036': [1.0, 59.0, 1.0], 'GSM1653037': [1.0, 36.0, 1.0], 'GSM1653038': [0.0, 48.0, 0.0], 'GSM1653039': [1.0, 54.0, 0.0], 'GSM1653040': [1.0, 20.0, 0.0], 'GSM1653041': [0.0, 44.0, 0.0], 'GSM1653042': [0.0, 46.0, 0.0], 'GSM1653043': [0.0, 25.0, 0.0], 'GSM1653044': [0.0, 52.0, 1.0], 'GSM1653045': [1.0, 30.0, 0.0], 'GSM1653046': [1.0, 25.0, 0.0], 'GSM1653047': [1.0, 51.0, 0.0], 'GSM1653048': [0.0, 40.0, 0.0], 'GSM1653049': [0.0, 18.0, 1.0], 'GSM1653050': [1.0, 53.0, 1.0], 'GSM1653051': [0.0, 20.0, 0.0], 'GSM1653052': [0.0, 34.0, 1.0], 'GSM1653053': [1.0, 25.0, 0.0], 'GSM1653054': [0.0, 46.0, 0.0], 'GSM1653055': [1.0, 38.0, 0.0], 'GSM1653056': [0.0, 58.0, 1.0], 'GSM1653057': [0.0, 65.0, 1.0], 'GSM1653058': [1.0, 29.0, 1.0], 'GSM1653059': [1.0, 34.0, 0.0], 'GSM1653060': [0.0, 34.0, 0.0], 'GSM1653061': [0.0, 59.0, 1.0], 'GSM1653062': [1.0, 52.0, 1.0], 'GSM1653063': [0.0, 19.0, 0.0], 'GSM1653064': [0.0, 64.0, 0.0], 'GSM1653065': [1.0, 30.0, 0.0], 'GSM1653066': [1.0, 33.0, 0.0], 'GSM1653067': [0.0, 39.0, 1.0], 'GSM1653068': [0.0, 22.0, 0.0], 'GSM1653069': [0.0, 45.0, 0.0], 'GSM1653070': [1.0, 49.0, 0.0], 'GSM1653071': [1.0, 24.0, 0.0], 'GSM1653072': [1.0, 29.0, 0.0], 'GSM1653073': [0.0, 20.0, 0.0], 'GSM1653074': [1.0, 53.0, 1.0], 'GSM1653075': [1.0, 22.0, 0.0], 'GSM1653076': [1.0, 54.0, 0.0], 'GSM1653077': [0.0, 47.0, 1.0], 'GSM1653078': [0.0, 33.0, 1.0], 'GSM1653079': [1.0, 45.0, 0.0], 'GSM1653080': [1.0, 48.0, 0.0], 'GSM1653081': [0.0, 59.0, 0.0], 'GSM1653082': [1.0, 45.0, 0.0], 'GSM1653083': [0.0, 54.0, 0.0], 'GSM1653084': [1.0, 33.0, 0.0], 'GSM1653085': [0.0, 49.0, 0.0], 'GSM1653086': [1.0, 43.0, 0.0], 'GSM1653087': [1.0, 21.0, 0.0], 'GSM1653088': [0.0, 62.0, 0.0], 'GSM1653089': [0.0, 50.0, 0.0], 'GSM1653090': [0.0, 51.0, 0.0], 'GSM1653091': [1.0, 55.0, 0.0], 'GSM1653092': [0.0, 32.0, 0.0], 'GSM1653093': [1.0, 49.0, 0.0], 'GSM1653094': [1.0, 42.0, 0.0], 'GSM1653095': [1.0, 21.0, 0.0], 'GSM1653096': [1.0, 54.0, 0.0], 'GSM1653097': [1.0, 51.0, 1.0], 'GSM1653098': [1.0, 50.0, 0.0], 'GSM1653099': [0.0, 52.0, 1.0], 'GSM1653100': [1.0, 53.0, 0.0], 'GSM1653101': [0.0, 31.0, 0.0], 'GSM1653102': [1.0, 26.0, 0.0], 'GSM1653103': [1.0, 44.0, 0.0], 'GSM1653104': [1.0, 41.0, 0.0], 'GSM1653105': [1.0, 26.0, 0.0], 'GSM1653106': [0.0, 56.0, 1.0], 'GSM1653107': [1.0, 58.0, 0.0], 'GSM1653108': [1.0, 32.0, 0.0], 'GSM1653109': [1.0, 25.0, 0.0], 'GSM1653110': [1.0, 57.0, 1.0], 'GSM1653111': [0.0, 48.0, 0.0], 'GSM1653112': [1.0, 47.0, 1.0], 'GSM1653113': [0.0, 21.0, 0.0], 'GSM1653114': [1.0, 28.0, 0.0], 'GSM1653115': [1.0, 41.0, 0.0], 'GSM1653116': [1.0, 41.0, 0.0], 'GSM1653117': [1.0, 38.0, 0.0], 'GSM1653118': [1.0, 27.0, 0.0], 'GSM1653119': [1.0, 50.0, 1.0], 'GSM1653120': [1.0, 41.0, 0.0], 'GSM1653121': [0.0, 47.0, 0.0], 'GSM1653122': [1.0, 44.0, 0.0], 'GSM1653123': [1.0, 51.0, 0.0], 'GSM1653124': [1.0, 28.0, 0.0], 'GSM1653125': [1.0, 36.0, 0.0], 'GSM1653126': [1.0, 50.0, 0.0], 'GSM1653127': [0.0, 64.0, 1.0], 'GSM1653128': [1.0, 50.0, 1.0], 'GSM1653129': [1.0, 34.0, 0.0], 'GSM1653130': [1.0, 54.0, 1.0], 'GSM1653131': [1.0, 43.0, 0.0], 'GSM1653132': [1.0, 49.0, 0.0], 'GSM1653133': [0.0, 56.0, 0.0], 'GSM1653134': [0.0, 38.0, 1.0], 'GSM1653135': [1.0, 47.0, 1.0], 'GSM1653136': [1.0, 51.0, 1.0], 'GSM1653137': [1.0, 53.0, 0.0], 'GSM1653138': [1.0, 56.0, 0.0], 'GSM1653139': [1.0, 35.0, 0.0], 'GSM1653140': [0.0, 57.0, 1.0], 'GSM1653141': [1.0, 43.0, 0.0], 'GSM1653142': [1.0, 41.0, 0.0], 'GSM1653143': [1.0, 37.0, 0.0], 'GSM1653144': [1.0, 28.0, 0.0], 'GSM1653145': [1.0, 50.0, 0.0], 'GSM1653146': [1.0, 52.0, 0.0], 'GSM1653147': [0.0, 62.0, 1.0], 'GSM1653148': [1.0, 27.0, 0.0], 'GSM1653149': [0.0, 24.0, 0.0]}\n", "Clinical data saved to ../../output/preprocess/Post-Traumatic_Stress_Disorder/clinical_data/GSE67663.csv\n", "Linked data shape: (184, 8692)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (184, 8692)\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 72 occurrences. This represents 39.13% 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%: 32.75\n", " 50% (Median): 45.0\n", " 75%: 52.0\n", "Min: 18.0\n", "Max: 74.0\n", "The distribution of the feature 'Age' in this dataset is fine.\n", "\n", "For the feature 'Gender', the least common label is '1.0' with 46 occurrences. This represents 25.00% 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/GSE67663.csv\n" ] } ], "source": [ "# 1. Gene expression data extraction and mapping (using the results from previous steps)\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "gene_data = get_genetic_data(matrix_file)\n", "gene_annotation = get_gene_annotation(soft_file)\n", "\n", "# 2. Gene mapping (since requires_gene_mapping = True from Step 4)\n", "gene_mapping = get_gene_mapping(gene_annotation, 'ID', 'Symbol')\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "\n", "# 3. Normalize gene symbols in gene expression data\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", "# 4. Re-extract clinical 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", "# From Step 2, we identified the correct rows\n", "trait_row = 2 # comorbid ptsd and depression status\n", "age_row = 1 # age row\n", "gender_row = 0 # gender row\n", "\n", "def convert_trait(value):\n", " \"\"\"Convert PTSD trait values to binary format (0/1).\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Based on the sample characteristics, \"comorbid ptsd and depression status: 1\" is case (PTSD)\n", " # and \"comorbid ptsd and depression status: 0\" is control (no PTSD)\n", " if value == '1':\n", " return 1 # PTSD case\n", " elif value == '0':\n", " return 0 # Control\n", " else:\n", " return None # Unknown or invalid value\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age values to continuous format.\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " try:\n", " return float(value) # Keep age values positive\n", " except (ValueError, TypeError):\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender values to binary format (0=female, 1=male).\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip().lower()\n", " \n", " if value == 'female':\n", " return 0\n", " elif value == 'male':\n", " return 1\n", " else:\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", "# 5. 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", "# 6. 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", "# 7. 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", "# 8. 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 comorbid PTSD and depression versus controls.\"\n", ")\n", "\n", "# 9. 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 }