{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0cbcd0b4", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:38:58.755488Z", "iopub.status.busy": "2025-03-25T08:38:58.755381Z", "iopub.status.idle": "2025-03-25T08:38:58.918004Z", "shell.execute_reply": "2025-03-25T08:38:58.917651Z" } }, "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 = \"Depression\"\n", "cohort = \"GSE99725\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Depression\"\n", "in_cohort_dir = \"../../input/GEO/Depression/GSE99725\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Depression/GSE99725.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Depression/gene_data/GSE99725.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Depression/clinical_data/GSE99725.csv\"\n", "json_path = \"../../output/preprocess/Depression/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "f9763bf6", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "229a40dc", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:38:58.919239Z", "iopub.status.busy": "2025-03-25T08:38:58.919095Z", "iopub.status.idle": "2025-03-25T08:38:59.040870Z", "shell.execute_reply": "2025-03-25T08:38:59.040515Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Transcriptomic Signaling Pathways involved in a naturalistic model of Inflammation-related Major Depressive Disorder and its remission\"\n", "!Series_summary\t\"This study aimed at identifying molecular biomarkers specific to inflammation-related subtype of MDD in order to improve diagnosis and treatment. For this, we performed whole-genome expression profiling from peripheral blood in a naturalistic model of inflammation-associated MDD represented by comorbid depression in obese patients. \"\n", "!Series_overall_design\t\"Depressed patients were diagnosed with the Mini-International Neuropsychiatric Interview and the 10-item, clinician administered, Montgomery-Asberg Depression Rating Scale. From a cohort of 100 massively obese patients we selected 33 of them for transcriptomic analysis with 24 patients that were again analyzed 4-12 months after bariatric surgery. \"\n", "Sample Characteristics Dictionary:\n", "{0: ['patient: CB291013', 'patient: TP100414', 'patient: JDF280314', 'patient: JA021214', 'patient: DC160914', 'patient: GMD170315', 'patient: MP220714', 'patient: SM260215', 'patient: MC261113', 'patient: SB091214', 'patient: CN220714', 'patient: AE170614', 'patient: AG121114', 'patient: SS150414', 'patient: TDC270115', 'patient: VF200115', 'patient: KP261113', 'patient: AC030215', 'patient: SM070415', 'patient: JMV220115', 'patient: NC130214', 'patient: SB221013', 'patient: MA021214', 'patient: DD101214', 'patient: LB141114', 'patient: CPP281113', 'patient: NR180314', 'patient: PP120315', 'patient: BB080414', 'patient: PM120914'], 1: ['time: M0', 'time: M6'], 2: ['MADRS: A', 'MADRS: B'], 3: ['tissue: Venous 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": "d425357d", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "edc3738a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:38:59.041923Z", "iopub.status.busy": "2025-03-25T08:38:59.041814Z", "iopub.status.idle": "2025-03-25T08:38:59.046471Z", "shell.execute_reply": "2025-03-25T08:38:59.046165Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical data file not found. Skip clinical feature extraction.\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# The dataset appears to contain transcriptomic data based on background information\n", "# \"whole-genome expression profiling from peripheral blood\"\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# Trait (Depression): From the sample characteristic dictionary, key 2 with 'MADRS: A', 'MADRS: B'\n", "# MADRS is Montgomery-Asberg Depression Rating Scale which is used to measure depression severity\n", "trait_row = 2\n", "\n", "# Age: Not explicitly available in the sample characteristics dictionary\n", "age_row = None\n", "\n", "# Gender: Not explicitly available in the sample characteristics dictionary\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value):\n", " \"\"\"\n", " Convert MADRS ratings to binary depression status.\n", " MADRS: A - likely represents patients with high MADRS scores (depressed)\n", " MADRS: B - likely represents patients with low MADRS scores (not depressed or remission)\n", " \"\"\"\n", " if isinstance(value, str):\n", " value = value.strip()\n", " if 'MADRS: A' in value:\n", " return 1 # Depressed\n", " elif 'MADRS: B' in value:\n", " return 0 # Not depressed or in remission\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"\n", " Convert age values to continuous data type.\n", " Not used in this dataset as age information is not available.\n", " \"\"\"\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"\n", " Convert gender values to binary (0 for female, 1 for male).\n", " Not used in this dataset as gender information is not available.\n", " \"\"\"\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", "# Proceed only if trait_row is not None\n", "if trait_row is not None:\n", " # Assuming clinical_data was loaded in a previous step\n", " try:\n", " # Get the clinical data path\n", " clinical_data_file = os.path.join(in_cohort_dir, \"clinical_data.csv\")\n", " clinical_data = pd.read_csv(clinical_data_file)\n", " \n", " # Extract clinical features\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 and save the clinical data\n", " print(\"Preview of selected clinical features:\")\n", " print(preview_df(selected_clinical_df))\n", " \n", " # Create directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " \n", " # Save to CSV\n", " selected_clinical_df.to_csv(out_clinical_data_file, index=False)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n", " except FileNotFoundError:\n", " print(\"Clinical data file not found. Skip clinical feature extraction.\")\n" ] }, { "cell_type": "markdown", "id": "011888b5", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "de24a9d3", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:38:59.047490Z", "iopub.status.busy": "2025-03-25T08:38:59.047385Z", "iopub.status.idle": "2025-03-25T08:38:59.233742Z", "shell.execute_reply": "2025-03-25T08:38:59.233310Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix file found: ../../input/GEO/Depression/GSE99725/GSE99725_series_matrix.txt.gz\n", "Gene data shape: (27202, 57)\n", "First 20 gene/probe identifiers:\n", "Index(['A_19_P00315452', 'A_19_P00315459', 'A_19_P00315493', 'A_19_P00315506',\n", " 'A_19_P00315524', 'A_19_P00315528', 'A_19_P00315529', 'A_19_P00315550',\n", " 'A_19_P00315551', 'A_19_P00315581', 'A_19_P00315583', 'A_19_P00315584',\n", " 'A_19_P00315593', 'A_19_P00315601', 'A_19_P00315603', 'A_19_P00315649',\n", " 'A_19_P00315651', 'A_19_P00315668', 'A_19_P00315691', 'A_19_P00315693'],\n", " dtype='object', name='ID')\n" ] } ], "source": [ "# 1. Get the SOFT and matrix file paths again \n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "print(f\"Matrix file found: {matrix_file}\")\n", "\n", "# 2. Use the get_genetic_data function from the library to get the gene_data\n", "try:\n", " gene_data = get_genetic_data(matrix_file)\n", " print(f\"Gene data shape: {gene_data.shape}\")\n", " \n", " # 3. Print the first 20 row IDs (gene or probe identifiers)\n", " print(\"First 20 gene/probe identifiers:\")\n", " print(gene_data.index[:20])\n", "except Exception as e:\n", " print(f\"Error extracting gene data: {e}\")\n" ] }, { "cell_type": "markdown", "id": "c253b3b9", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "9b7f9764", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:38:59.235264Z", "iopub.status.busy": "2025-03-25T08:38:59.235060Z", "iopub.status.idle": "2025-03-25T08:38:59.236974Z", "shell.execute_reply": "2025-03-25T08:38:59.236693Z" } }, "outputs": [], "source": [ "# Examining the gene identifiers\n", "# These identifiers (starting with A_19_P) are Agilent probe IDs, not human gene symbols.\n", "# They need to be mapped to standard gene symbols for analysis.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "a6d62e36", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "44053705", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:38:59.238337Z", "iopub.status.busy": "2025-03-25T08:38:59.238235Z", "iopub.status.idle": "2025-03-25T08:39:03.944894Z", "shell.execute_reply": "2025-03-25T08:39:03.944503Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Platform title found: Agilent-039494 SurePrint G3 Human GE v2 8x60K Microarray 039381 (Probe Name version)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene annotation preview:\n", "{'ID': ['GE_BrightCorner', 'DarkCorner', 'A_23_P117082', 'A_33_P3246448', 'A_33_P3318220', 'A_33_P3236322', 'A_33_P3319925', 'A_21_P0000509', 'A_21_P0000744', 'A_24_P215804'], 'SPOT_ID': ['CONTROL', 'CONTROL', 'A_23_P117082', 'A_33_P3246448', 'A_33_P3318220', 'A_33_P3236322', 'A_33_P3319925', 'A_21_P0000509', 'A_21_P0000744', 'A_24_P215804'], 'CONTROL_TYPE': ['pos', 'pos', 'FALSE', 'FALSE', 'FALSE', 'FALSE', 'FALSE', 'FALSE', 'FALSE', 'FALSE'], 'REFSEQ': [nan, nan, 'NM_015987', 'NM_080671', 'NM_178466', nan, 'XM_001133269', 'NR_024244', 'NR_038269', 'NM_016951'], 'GB_ACC': [nan, nan, 'NM_015987', 'NM_080671', 'NM_178466', 'AK128005', 'XM_001133269', 'NR_024244', 'NR_038269', 'NM_016951'], 'LOCUSLINK_ID': [nan, nan, 50865.0, 23704.0, 128861.0, 100129869.0, 730249.0, nan, nan, 51192.0], 'GENE_SYMBOL': [nan, nan, 'HEBP1', 'KCNE4', 'BPIFA3', 'LOC100129869', 'IRG1', 'SNAR-G2', 'LOC100506844', 'CKLF'], 'GENE_NAME': [nan, nan, 'heme binding protein 1', 'potassium voltage-gated channel, Isk-related family, member 4', 'BPI fold containing family A, member 3', 'uncharacterized LOC100129869', 'immunoresponsive 1 homolog (mouse)', 'small ILF3/NF90-associated RNA G2', 'uncharacterized LOC100506844', 'chemokine-like factor'], 'UNIGENE_ID': [nan, nan, 'Hs.642618', 'Hs.348522', 'Hs.360989', nan, 'Hs.160789', 'Hs.717308', 'Hs.90286', 'Hs.15159'], 'ENSEMBL_ID': [nan, nan, 'ENST00000014930', 'ENST00000281830', 'ENST00000375454', nan, 'ENST00000449753', nan, 'ENST00000551421', nan], 'ACCESSION_STRING': [nan, nan, 'ref|NM_015987|ens|ENST00000014930|gb|AF117615|gb|BC016277', 'ref|NM_080671|ens|ENST00000281830|tc|THC2655788', 'ref|NM_178466|ens|ENST00000375454|ens|ENST00000471233|tc|THC2478474', 'gb|AK128005|tc|THC2484382', 'ens|ENST00000449753|ens|ENST00000377462|ref|XM_001133269|ref|XM_003403661', 'ref|NR_024244', 'ref|NR_038269|ens|ENST00000551421|ens|ENST00000546580|ens|ENST00000553102', 'ref|NM_016951|ref|NM_181641|ref|NM_181640|ref|NM_016326'], 'CHROMOSOMAL_LOCATION': [nan, nan, 'chr12:13127906-13127847', 'chr2:223920197-223920256', 'chr20:31812208-31812267', 'chr20:56533874-56533815', 'chr13:77532009-77532068', 'chr19:49534993-49534934', 'chr12:58329728-58329669', 'chr16:66599900-66599959'], 'CYTOBAND': [nan, nan, 'hs|12p13.1', 'hs|2q36.1', 'hs|20q11.21', 'hs|20q13.32', 'hs|13q22.3', 'hs|19q13.33', 'hs|12q14.1', 'hs|16q21'], 'DESCRIPTION': [nan, nan, 'Homo sapiens heme binding protein 1 (HEBP1), mRNA [NM_015987]', 'Homo sapiens potassium voltage-gated channel, Isk-related family, member 4 (KCNE4), mRNA [NM_080671]', 'Homo sapiens BPI fold containing family A, member 3 (BPIFA3), transcript variant 1, mRNA [NM_178466]', 'Homo sapiens cDNA FLJ46124 fis, clone TESTI2040372. [AK128005]', 'immunoresponsive 1 homolog (mouse) [Source:HGNC Symbol;Acc:33904] [ENST00000449753]', 'Homo sapiens small ILF3/NF90-associated RNA G2 (SNAR-G2), small nuclear RNA [NR_024244]', 'Homo sapiens uncharacterized LOC100506844 (LOC100506844), non-coding RNA [NR_038269]', 'Homo sapiens chemokine-like factor (CKLF), transcript variant 1, mRNA [NM_016951]'], 'GO_ID': [nan, nan, 'GO:0005488(binding)|GO:0005576(extracellular region)|GO:0005737(cytoplasm)|GO:0005739(mitochondrion)|GO:0005829(cytosol)|GO:0007623(circadian rhythm)|GO:0020037(heme binding)', 'GO:0005244(voltage-gated ion channel activity)|GO:0005249(voltage-gated potassium channel activity)|GO:0006811(ion transport)|GO:0006813(potassium ion transport)|GO:0016020(membrane)|GO:0016021(integral to membrane)|GO:0016324(apical plasma membrane)', 'GO:0005576(extracellular region)|GO:0008289(lipid binding)', nan, 'GO:0019543(propionate catabolic process)|GO:0032496(response to lipopolysaccharide)|GO:0047547(2-methylcitrate dehydratase activity)', nan, nan, 'GO:0005576(extracellular region)|GO:0005615(extracellular space)|GO:0006935(chemotaxis)|GO:0008009(chemokine activity)|GO:0008283(cell proliferation)|GO:0016020(membrane)|GO:0016021(integral to membrane)|GO:0030593(neutrophil chemotaxis)|GO:0032940(secretion by cell)|GO:0048246(macrophage chemotaxis)|GO:0048247(lymphocyte chemotaxis)'], 'SEQUENCE': [nan, nan, 'AAGGGGGAAAATGTGATTTGTGCCTGATCTTTCATCTGTGATTCTTATAAGAGCTTTGTC', 'GCAAGTCTCTCTGCACCTATTAAAAAGTGATGTATATACTTCCTTCTTATTCTGTTGAGT', 'CATTCCATAAGGAGTGGTTCTCGGCAAATATCTCACTTGAATTTGACCTTGAATTGAGAC', 'ATTTATTTTCACAAGTGCATAGCGGCCAACACCACCAGCACTAACCAGAGTGGATTCTTG', 'AGAAGACCTAGAAGACTGTTCTGTGTTAACTACACTTCTCAAAGGACCCTCTCCACCAGA', 'AGGGGAGGGTTCGAGGGTACGAGTTCGAGGCCAACCGGGTCCACATTGGTTGAGAAAAAA', 'AGTCGTACCCTCTTGTTTTTCTCTGAGTCAGTCTTAAGGTGAAATGAAGTGTGGCCCAGT', 'AAAGAAGTTTTGTAATTTTATATTACTTTTTAGTTTGATACTAAGTATTAAACATATTTC']}\n" ] } ], "source": [ "# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n", "gene_annotation = get_gene_annotation(soft_file)\n", "\n", "# Check if there are any platforms defined in the SOFT file that might contain annotation data\n", "with gzip.open(soft_file, 'rt') as f:\n", " soft_content = f.read()\n", "\n", "# Look for platform sections in the SOFT file\n", "platform_sections = re.findall(r'^!Platform_title\\s*=\\s*(.+)$', soft_content, re.MULTILINE)\n", "if platform_sections:\n", " print(f\"Platform title found: {platform_sections[0]}\")\n", "\n", "# Try to extract more annotation data by reading directly from the SOFT file\n", "# Look for lines that might contain gene symbol mappings\n", "symbol_pattern = re.compile(r'ID_REF\\s+Symbol|ID\\s+Gene Symbol', re.IGNORECASE)\n", "annotation_lines = []\n", "with gzip.open(soft_file, 'rt') as f:\n", " for line in f:\n", " if symbol_pattern.search(line):\n", " annotation_lines.append(line)\n", " # Collect the next few lines to see the annotation structure\n", " for _ in range(10):\n", " annotation_lines.append(next(f, ''))\n", "\n", "if annotation_lines:\n", " print(\"Found potential gene symbol mappings:\")\n", " for line in annotation_lines:\n", " print(line.strip())\n", "\n", "# 2. Use the 'preview_df' function from the library to preview the data and print out the results.\n", "print(\"\\nGene annotation preview:\")\n", "print(preview_df(gene_annotation, n=10))\n", "\n", "# If we need an alternative source of mapping, check if there are any other annotation files in the cohort directory\n", "cohort_files = os.listdir(in_cohort_dir)\n", "annotation_files = [f for f in cohort_files if 'annotation' in f.lower() or 'platform' in f.lower()]\n", "if annotation_files:\n", " print(\"\\nAdditional annotation files found in the cohort directory:\")\n", " for file in annotation_files:\n", " print(file)\n" ] }, { "cell_type": "markdown", "id": "122dd2c9", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "a6bb5d26", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:39:03.946811Z", "iopub.status.busy": "2025-03-25T08:39:03.946667Z", "iopub.status.idle": "2025-03-25T08:39:04.633888Z", "shell.execute_reply": "2025-03-25T08:39:04.633498Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mapping dataframe shape: (46204, 2)\n", "First few rows of mapping dataframe:\n", " ID Gene\n", "2 A_23_P117082 HEBP1\n", "3 A_33_P3246448 KCNE4\n", "4 A_33_P3318220 BPIFA3\n", "5 A_33_P3236322 LOC100129869\n", "6 A_33_P3319925 IRG1\n", "Gene expression data shape after mapping: (14534, 57)\n", "First few rows of gene-mapped expression data:\n", " GSM2650879 GSM2650880 GSM2650881 GSM2650882 GSM2650883 \\\n", "Gene \n", "A1BG 22.991629 22.721831 23.722127 23.393494 23.642667 \n", "A1BG-AS1 30.547082 29.264743 30.395788 29.542070 29.606270 \n", "A2LD1 54.674383 55.100095 56.005337 56.411266 55.148358 \n", "A4GALT 26.780564 27.006581 27.308758 28.217594 27.636441 \n", "AAAS 57.085719 56.312345 56.760835 58.313588 57.552408 \n", "\n", " GSM2650884 GSM2650885 GSM2650886 GSM2650887 GSM2650888 ... \\\n", "Gene ... \n", "A1BG 23.244120 23.084527 23.308226 23.035896 22.572523 ... \n", "A1BG-AS1 28.079295 29.881238 29.968284 27.635672 28.161372 ... \n", "A2LD1 57.567032 57.069253 55.072270 56.293517 57.421690 ... \n", "A4GALT 27.031401 27.135090 27.011729 29.115011 29.267572 ... \n", "AAAS 56.584150 56.421620 57.328323 57.816075 58.283809 ... \n", "\n", " GSM2650926 GSM2650927 GSM2650928 GSM2650929 GSM2650930 \\\n", "Gene \n", "A1BG 22.976532 23.026887 22.164703 22.206372 22.667443 \n", "A1BG-AS1 29.339406 29.547107 29.163393 29.462364 29.870844 \n", "A2LD1 55.320653 56.141642 56.870281 56.185597 54.903119 \n", "A4GALT 27.218257 28.263681 27.366026 27.619946 28.651942 \n", "AAAS 56.970648 58.428042 55.490066 57.190939 58.525460 \n", "\n", " GSM2650931 GSM2650932 GSM2650933 GSM2650934 GSM2650935 \n", "Gene \n", "A1BG 22.585275 23.260373 24.324405 22.909509 23.279769 \n", "A1BG-AS1 29.653725 29.812618 30.878434 29.040221 29.119394 \n", "A2LD1 56.371397 55.302766 55.471024 55.532072 55.488470 \n", "A4GALT 27.160468 26.769595 27.700168 28.712601 27.559470 \n", "AAAS 59.262659 57.894621 59.194355 57.254557 55.047785 \n", "\n", "[5 rows x 57 columns]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Depression/gene_data/GSE99725.csv\n" ] } ], "source": [ "# 1. Identify the keys for gene identifiers and gene symbols\n", "# The 'ID' column in the gene_annotation matches the probe identifiers in gene_data\n", "# The 'GENE_SYMBOL' column contains the corresponding gene symbols\n", "prob_col = 'ID'\n", "gene_col = 'GENE_SYMBOL'\n", "\n", "# 2. Get a gene mapping dataframe\n", "mapping_df = get_gene_mapping(gene_annotation, prob_col, gene_col)\n", "print(f\"Mapping dataframe shape: {mapping_df.shape}\")\n", "print(\"First few rows of mapping dataframe:\")\n", "print(mapping_df.head())\n", "\n", "# 3. Apply gene mapping to convert probe-level measurements to gene expression data\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "print(f\"Gene expression data shape after mapping: {gene_data.shape}\")\n", "print(\"First few rows of gene-mapped expression data:\")\n", "print(gene_data.head())\n", "\n", "# Create the output directory if it doesn't exist\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "\n", "# Save the gene expression data to a CSV file\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": "fb319aa9", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "f008a500", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:39:04.635814Z", "iopub.status.busy": "2025-03-25T08:39:04.635668Z", "iopub.status.idle": "2025-03-25T08:39:10.595812Z", "shell.execute_reply": "2025-03-25T08:39:10.595175Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene data already normalized and saved to ../../output/preprocess/Depression/gene_data/GSE99725.csv\n", "Selected clinical data shape: (1, 57)\n", "Clinical data preview:\n", "{'GSM2650879': [1.0], 'GSM2650880': [1.0], 'GSM2650881': [0.0], 'GSM2650882': [0.0], 'GSM2650883': [1.0], 'GSM2650884': [0.0], 'GSM2650885': [1.0], 'GSM2650886': [1.0], 'GSM2650887': [1.0], 'GSM2650888': [1.0], 'GSM2650889': [1.0], 'GSM2650890': [0.0], 'GSM2650891': [0.0], 'GSM2650892': [0.0], 'GSM2650893': [0.0], 'GSM2650894': [0.0], 'GSM2650895': [1.0], 'GSM2650896': [1.0], 'GSM2650897': [1.0], 'GSM2650898': [1.0], 'GSM2650899': [0.0], 'GSM2650900': [0.0], 'GSM2650901': [0.0], 'GSM2650902': [1.0], 'GSM2650903': [1.0], 'GSM2650904': [1.0], 'GSM2650905': [1.0], 'GSM2650906': [1.0], 'GSM2650907': [0.0], 'GSM2650908': [0.0], 'GSM2650909': [0.0], 'GSM2650910': [0.0], 'GSM2650911': [0.0], 'GSM2650912': [0.0], 'GSM2650913': [0.0], 'GSM2650914': [0.0], 'GSM2650915': [1.0], 'GSM2650916': [0.0], 'GSM2650917': [1.0], 'GSM2650918': [1.0], 'GSM2650919': [1.0], 'GSM2650920': [1.0], 'GSM2650921': [1.0], 'GSM2650922': [1.0], 'GSM2650923': [1.0], 'GSM2650924': [1.0], 'GSM2650925': [1.0], 'GSM2650926': [1.0], 'GSM2650927': [1.0], 'GSM2650928': [0.0], 'GSM2650929': [0.0], 'GSM2650930': [0.0], 'GSM2650931': [0.0], 'GSM2650932': [0.0], 'GSM2650933': [0.0], 'GSM2650934': [1.0], 'GSM2650935': [1.0]}\n", "Clinical data saved to ../../output/preprocess/Depression/clinical_data/GSE99725.csv\n", "Linked data shape: (57, 14535)\n", "Linked data preview (first 5 rows, 5 columns):\n", " Depression A1BG A1BG-AS1 A2LD1 A4GALT\n", "GSM2650879 1.0 22.991629 30.547082 54.674383 26.780564\n", "GSM2650880 1.0 22.721831 29.264743 55.100095 27.006581\n", "GSM2650881 0.0 23.722127 30.395788 56.005337 27.308758\n", "GSM2650882 0.0 23.393494 29.542070 56.411266 28.217594\n", "GSM2650883 1.0 23.642667 29.606270 55.148358 27.636441\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (57, 14535)\n", "For the feature 'Depression', the least common label is '0.0' with 26 occurrences. This represents 45.61% of the dataset.\n", "The distribution of the feature 'Depression' in this dataset is fine.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Depression/GSE99725.csv\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data - we already did this in step 6\n", "# No need to do it again\n", "print(f\"Gene data already normalized and saved to {out_gene_data_file}\")\n", "\n", "# 2. Need to recreate clinical data since it wasn't properly saved in step 2\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "background_info, clinical_data = get_background_and_clinical_data(matrix_file)\n", "\n", "# Extract clinical features\n", "def convert_trait(value):\n", " \"\"\"\n", " Convert MADRS ratings to binary depression status.\n", " MADRS: A - likely represents patients with high MADRS scores (depressed)\n", " MADRS: B - likely represents patients with low MADRS scores (not depressed or remission)\n", " \"\"\"\n", " if isinstance(value, str):\n", " value = value.strip()\n", " if 'MADRS: A' in value:\n", " return 1 # Depressed\n", " elif 'MADRS: B' in value:\n", " return 0 # Not depressed or in remission\n", " return None\n", "\n", "# Use previously defined trait_row = 2 from step 2\n", "selected_clinical_df = geo_select_clinical_features(\n", " clinical_df=clinical_data,\n", " trait=trait,\n", " trait_row=2,\n", " convert_trait=convert_trait,\n", " age_row=None,\n", " convert_age=None,\n", " gender_row=None, \n", " convert_gender=None\n", ")\n", "\n", "print(f\"Selected clinical data shape: {selected_clinical_df.shape}\")\n", "print(\"Clinical data preview:\")\n", "print(preview_df(selected_clinical_df))\n", "\n", "# Save clinical data for future reference\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", "# 2. Link clinical and genetic data\n", "linked_data = geo_link_clinical_genetic_data(selected_clinical_df, gene_data)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "print(\"Linked data preview (first 5 rows, 5 columns):\")\n", "print(linked_data.iloc[:5, :5] if not linked_data.empty else \"Linked data is empty\")\n", "\n", "# 3. Handle missing values\n", "linked_data = handle_missing_values(linked_data, trait)\n", "print(f\"Data shape after handling missing values: {linked_data.shape}\")\n", "\n", "# 4. Check for bias in features\n", "is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "\n", "# 5. Validate and save cohort information\n", "is_usable = validate_and_save_cohort_info(\n", " is_final=True,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=True,\n", " is_trait_available=True,\n", " is_biased=is_biased,\n", " df=linked_data,\n", " note=\"Dataset contains gene expression data from peripheral blood of obese patients with and without depression.\"\n", ")\n", "\n", "# 6. Save the 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 is not usable for analysis. No linked data file 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 }