{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "b4883b93", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:49:40.528372Z", "iopub.status.busy": "2025-03-25T05:49:40.528190Z", "iopub.status.idle": "2025-03-25T05:49:40.694216Z", "shell.execute_reply": "2025-03-25T05:49:40.693850Z" } }, "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 = \"Hypertension\"\n", "cohort = \"GSE74144\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Hypertension\"\n", "in_cohort_dir = \"../../input/GEO/Hypertension/GSE74144\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Hypertension/GSE74144.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Hypertension/gene_data/GSE74144.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Hypertension/clinical_data/GSE74144.csv\"\n", "json_path = \"../../output/preprocess/Hypertension/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "79c5b2ae", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "d4fcac13", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:49:40.695677Z", "iopub.status.busy": "2025-03-25T05:49:40.695529Z", "iopub.status.idle": "2025-03-25T05:49:40.762672Z", "shell.execute_reply": "2025-03-25T05:49:40.762378Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Gene expression changes in white blood cells of hypertensive patients with left ventricular remodeling\"\n", "!Series_summary\t\"Using transcriptomic we looked for changes in large-scale gene expression profiling of leukocytes of hypertensive patients with left ventricular remodeling compared to hypertensive patients without left ventricular remodeling and to control and whether these changes reflect metabolic pathway regulation already shown by positron emission tomography. Genes encoding for glycolytic enzymes were found over-expressed in the group of hypertensive patients with left ventricular remodeling. Expression of master genes involved in fatty acids β-oxidation was unchanged.\"\n", "!Series_overall_design\t\"Transcriptomic analysis included 14 patients with hypertension and left ventricular hypertrophy, 14 patients with hypertension and normal left ventricular size and 8 control individuals.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['subject status: hypertensive patient with normal left ventricular size', 'subject status: hypertensive patient with left ventricular remodeling', 'subject status: control individual'], 1: ['tissue: white blood cells']}\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": "9de0464c", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "fe1e1b73", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:49:40.763857Z", "iopub.status.busy": "2025-03-25T05:49:40.763746Z", "iopub.status.idle": "2025-03-25T05:49:40.769771Z", "shell.execute_reply": "2025-03-25T05:49:40.769475Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical features preview: {'VALUE': [1.0]}\n", "Clinical data saved to ../../output/preprocess/Hypertension/clinical_data/GSE74144.csv\n" ] } ], "source": [ "# Step 1: Determine gene expression data availability\n", "is_gene_available = True # Based on series title and summary, this is gene expression data from white blood cells\n", "\n", "# Step 2: Determine variable availability and conversion functions\n", "# 2.1 Data Availability\n", "# For trait (Hypertension)\n", "trait_row = 0 # The subject status contains information about hypertension\n", "\n", "# Age and gender are not available in the sample characteristics\n", "age_row = None\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value):\n", " \"\"\"Convert trait data to binary format.\"\"\"\n", " if value is None:\n", " return None\n", " \n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " if 'hypertensive patient' in value.lower():\n", " return 1 # Hypertensive\n", " elif 'control' in value.lower():\n", " return 0 # Not hypertensive\n", " else:\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Placeholder function for age conversion.\"\"\"\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Placeholder function for gender conversion.\"\"\"\n", " return None\n", "\n", "# Step 3: Save metadata\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", "# Step 4: Clinical Feature Extraction (since trait_row is not None)\n", "if trait_row is not None:\n", " clinical_data = pd.DataFrame(\n", " {'VALUE': ['subject status: hypertensive patient with normal left ventricular size', \n", " 'subject status: hypertensive patient with left ventricular remodeling', \n", " 'subject status: control individual']},\n", " index=[0, 0, 0]\n", " )\n", " \n", " # Extract clinical features using the library 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 selected features\n", " preview_result = preview_df(selected_clinical_df)\n", " print(\"Clinical features preview:\", preview_result)\n", " \n", " # Save the clinical data to CSV\n", " selected_clinical_df.to_csv(out_clinical_data_file)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "10fdee60", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "282a9ed0", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:49:40.770812Z", "iopub.status.busy": "2025-03-25T05:49:40.770706Z", "iopub.status.idle": "2025-03-25T05:49:40.839634Z", "shell.execute_reply": "2025-03-25T05:49:40.839303Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['A_23_P100001', 'A_23_P100022', 'A_23_P100056', 'A_23_P100074',\n", " 'A_23_P100127', 'A_23_P100141', 'A_23_P100189', 'A_23_P100196',\n", " 'A_23_P100203', 'A_23_P100220', 'A_23_P100240', 'A_23_P10025',\n", " 'A_23_P100292', 'A_23_P100315', 'A_23_P100326', 'A_23_P100344',\n", " 'A_23_P100355', 'A_23_P100386', 'A_23_P100392', 'A_23_P100420'],\n", " dtype='object', name='ID')\n" ] } ], "source": [ "# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.\n", "gene_data = get_genetic_data(matrix_file)\n", "\n", "# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.\n", "print(gene_data.index[:20])\n" ] }, { "cell_type": "markdown", "id": "d33a85ea", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "6a486b4a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:49:40.840963Z", "iopub.status.busy": "2025-03-25T05:49:40.840852Z", "iopub.status.idle": "2025-03-25T05:49:40.842662Z", "shell.execute_reply": "2025-03-25T05:49:40.842380Z" } }, "outputs": [], "source": [ "# These identifiers (A_23_P...) are Agilent microarray probe IDs, not human gene symbols.\n", "# They need to be mapped to standard gene symbols for biological interpretation.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "38a88c41", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "0a0f09bf", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:49:40.843735Z", "iopub.status.busy": "2025-03-25T05:49:40.843633Z", "iopub.status.idle": "2025-03-25T05:49:42.544408Z", "shell.execute_reply": "2025-03-25T05:49:42.544015Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['(+)E1A_r60_1', '(+)E1A_r60_3', '(+)E1A_r60_a104', '(+)E1A_r60_a107', '(+)E1A_r60_a135'], 'SPOT_ID': ['(+)E1A_r60_1', '(+)E1A_r60_3', '(+)E1A_r60_a104', '(+)E1A_r60_a107', '(+)E1A_r60_a135'], 'CONTROL_TYPE': ['pos', 'pos', 'pos', 'pos', 'pos'], 'REFSEQ': [nan, nan, nan, nan, nan], 'GB_ACC': [nan, nan, nan, nan, nan], 'GENE': [nan, nan, nan, nan, nan], 'GENE_SYMBOL': [nan, nan, nan, nan, nan], 'GENE_NAME': [nan, nan, nan, nan, nan], 'UNIGENE_ID': [nan, nan, nan, nan, nan], 'ENSEMBL_ID': [nan, nan, nan, nan, nan], 'TIGR_ID': [nan, nan, nan, nan, nan], 'ACCESSION_STRING': [nan, nan, nan, nan, nan], 'CHROMOSOMAL_LOCATION': [nan, nan, nan, nan, nan], 'CYTOBAND': [nan, nan, nan, nan, nan], 'DESCRIPTION': [nan, nan, nan, nan, nan], 'GO_ID': [nan, nan, nan, nan, nan], 'SEQUENCE': [nan, nan, nan, nan, nan]}\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", "# 2. 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": "ee2aef0e", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "8b6bc5a5", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:49:42.545809Z", "iopub.status.busy": "2025-03-25T05:49:42.545676Z", "iopub.status.idle": "2025-03-25T05:49:43.078313Z", "shell.execute_reply": "2025-03-25T05:49:43.077925Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First few rows with non-null gene symbols:\n", "{'ID': ['A_23_P100001', 'A_23_P100022', 'A_23_P100056', 'A_23_P100074', 'A_23_P100127'], 'SPOT_ID': ['A_23_P100001', 'A_23_P100022', 'A_23_P100056', 'A_23_P100074', 'A_23_P100127'], 'CONTROL_TYPE': ['FALSE', 'FALSE', 'FALSE', 'FALSE', 'FALSE'], 'REFSEQ': ['NM_207446', 'NM_014848', 'NM_194272', 'NM_020371', 'NM_170589'], 'GB_ACC': ['NM_207446', 'NM_014848', 'NM_194272', 'NM_020371', 'NM_170589'], 'GENE': [400451.0, 9899.0, 348093.0, 57099.0, 57082.0], 'GENE_SYMBOL': ['FAM174B', 'SV2B', 'RBPMS2', 'AVEN', 'CASC5'], 'GENE_NAME': ['family with sequence similarity 174, member B', 'synaptic vesicle glycoprotein 2B', 'RNA binding protein with multiple splicing 2', 'apoptosis, caspase activation inhibitor', 'cancer susceptibility candidate 5'], 'UNIGENE_ID': ['Hs.27373', 'Hs.21754', 'Hs.436518', 'Hs.555966', 'Hs.181855'], 'ENSEMBL_ID': ['ENST00000557398', 'ENST00000557410', 'ENST00000300069', 'ENST00000306730', 'ENST00000260369'], 'TIGR_ID': [nan, nan, nan, nan, nan], 'ACCESSION_STRING': ['ref|NM_207446|ens|ENST00000557398|ens|ENST00000553393|ens|ENST00000327355', 'ref|NM_014848|ref|NM_001167580|ens|ENST00000557410|ens|ENST00000330276', 'ref|NM_194272|ens|ENST00000300069|gb|AK127873|gb|AK124123', 'ref|NM_020371|ens|ENST00000306730|gb|AF283508|gb|BC010488', 'ref|NM_170589|ref|NM_144508|ens|ENST00000260369|ens|ENST00000533001'], 'CHROMOSOMAL_LOCATION': ['chr15:93160848-93160789', 'chr15:91838329-91838388', 'chr15:65032375-65032316', 'chr15:34158739-34158680', 'chr15:40917525-40917584'], 'CYTOBAND': ['hs|15q26.1', 'hs|15q26.1', 'hs|15q22.31', 'hs|15q14', 'hs|15q15.1'], 'DESCRIPTION': ['Homo sapiens family with sequence similarity 174, member B (FAM174B), mRNA [NM_207446]', 'Homo sapiens synaptic vesicle glycoprotein 2B (SV2B), transcript variant 1, mRNA [NM_014848]', 'Homo sapiens RNA binding protein with multiple splicing 2 (RBPMS2), mRNA [NM_194272]', 'Homo sapiens apoptosis, caspase activation inhibitor (AVEN), mRNA [NM_020371]', 'Homo sapiens cancer susceptibility candidate 5 (CASC5), transcript variant 1, mRNA [NM_170589]'], 'GO_ID': ['GO:0016020(membrane)|GO:0016021(integral to membrane)', 'GO:0001669(acrosomal vesicle)|GO:0006836(neurotransmitter transport)|GO:0016020(membrane)|GO:0016021(integral to membrane)|GO:0022857(transmembrane transporter activity)|GO:0030054(cell junction)|GO:0030672(synaptic vesicle membrane)|GO:0031410(cytoplasmic vesicle)|GO:0045202(synapse)', 'GO:0000166(nucleotide binding)|GO:0003676(nucleic acid binding)', 'GO:0005515(protein binding)|GO:0005622(intracellular)|GO:0005624(membrane fraction)|GO:0006915(apoptosis)|GO:0006916(anti-apoptosis)|GO:0012505(endomembrane system)|GO:0016020(membrane)', 'GO:0000087(M phase of mitotic cell cycle)|GO:0000236(mitotic prometaphase)|GO:0000278(mitotic cell cycle)|GO:0000777(condensed chromosome kinetochore)|GO:0001669(acrosomal vesicle)|GO:0001675(acrosome assembly)|GO:0005515(protein binding)|GO:0005634(nucleus)|GO:0005654(nucleoplasm)|GO:0005694(chromosome)|GO:0005730(nucleolus)|GO:0005829(cytosol)|GO:0006334(nucleosome assembly)|GO:0007059(chromosome segregation)|GO:0008608(attachment of spindle microtubules to kinetochore)|GO:0010923(negative regulation of phosphatase activity)|GO:0034080(CenH3-containing nucleosome assembly at centromere)|GO:0051301(cell division)|GO:0071173(spindle assembly checkpoint)'], 'SEQUENCE': ['ATCTCATGGAAAAGCTGGATTCCTCTGCCTTACGCAGAAACACCCGGGCTCCATCTGCCA', 'ATGTCGGCTGTGGAGGGTTAAAGGGATGAGGCTTTCCTTTGTTTAGCAAATCTGTTCACA', 'CCCTGTCAGATAAGTTTAATGTTTAGTTTGAGGCATGAAGAAGAAAAGGGTTTCCATTCT', 'GACCAGCCAGTTTACAAGCATGTCTCAAGCTAGTGTGTTCCATTATGCTCACAGCAGTAA', 'CGGTCTCTAGCAAAGATTCAGGCATTGGATCTGTTGCAGGTAAACTGAACCTAAGTCCTT']}\n", "\n", "Sample probe IDs from gene_data:\n", "['A_23_P100001', 'A_23_P100022', 'A_23_P100056', 'A_23_P100074', 'A_23_P100127']\n", "\n", "Sample values from ID column in gene_annotation:\n", "['(+)E1A_r60_1', '(+)E1A_r60_3', '(+)E1A_r60_a104', '(+)E1A_r60_a107', '(+)E1A_r60_a135']\n", "\n", "Matching probe examples:\n", "{'ID': ['A_23_P100001', 'A_23_P100022', 'A_23_P100056', 'A_23_P100074', 'A_23_P100127'], 'SPOT_ID': ['A_23_P100001', 'A_23_P100022', 'A_23_P100056', 'A_23_P100074', 'A_23_P100127'], 'CONTROL_TYPE': ['FALSE', 'FALSE', 'FALSE', 'FALSE', 'FALSE'], 'REFSEQ': ['NM_207446', 'NM_014848', 'NM_194272', 'NM_020371', 'NM_170589'], 'GB_ACC': ['NM_207446', 'NM_014848', 'NM_194272', 'NM_020371', 'NM_170589'], 'GENE': [400451.0, 9899.0, 348093.0, 57099.0, 57082.0], 'GENE_SYMBOL': ['FAM174B', 'SV2B', 'RBPMS2', 'AVEN', 'CASC5'], 'GENE_NAME': ['family with sequence similarity 174, member B', 'synaptic vesicle glycoprotein 2B', 'RNA binding protein with multiple splicing 2', 'apoptosis, caspase activation inhibitor', 'cancer susceptibility candidate 5'], 'UNIGENE_ID': ['Hs.27373', 'Hs.21754', 'Hs.436518', 'Hs.555966', 'Hs.181855'], 'ENSEMBL_ID': ['ENST00000557398', 'ENST00000557410', 'ENST00000300069', 'ENST00000306730', 'ENST00000260369'], 'TIGR_ID': [nan, nan, nan, nan, nan], 'ACCESSION_STRING': ['ref|NM_207446|ens|ENST00000557398|ens|ENST00000553393|ens|ENST00000327355', 'ref|NM_014848|ref|NM_001167580|ens|ENST00000557410|ens|ENST00000330276', 'ref|NM_194272|ens|ENST00000300069|gb|AK127873|gb|AK124123', 'ref|NM_020371|ens|ENST00000306730|gb|AF283508|gb|BC010488', 'ref|NM_170589|ref|NM_144508|ens|ENST00000260369|ens|ENST00000533001'], 'CHROMOSOMAL_LOCATION': ['chr15:93160848-93160789', 'chr15:91838329-91838388', 'chr15:65032375-65032316', 'chr15:34158739-34158680', 'chr15:40917525-40917584'], 'CYTOBAND': ['hs|15q26.1', 'hs|15q26.1', 'hs|15q22.31', 'hs|15q14', 'hs|15q15.1'], 'DESCRIPTION': ['Homo sapiens family with sequence similarity 174, member B (FAM174B), mRNA [NM_207446]', 'Homo sapiens synaptic vesicle glycoprotein 2B (SV2B), transcript variant 1, mRNA [NM_014848]', 'Homo sapiens RNA binding protein with multiple splicing 2 (RBPMS2), mRNA [NM_194272]', 'Homo sapiens apoptosis, caspase activation inhibitor (AVEN), mRNA [NM_020371]', 'Homo sapiens cancer susceptibility candidate 5 (CASC5), transcript variant 1, mRNA [NM_170589]'], 'GO_ID': ['GO:0016020(membrane)|GO:0016021(integral to membrane)', 'GO:0001669(acrosomal vesicle)|GO:0006836(neurotransmitter transport)|GO:0016020(membrane)|GO:0016021(integral to membrane)|GO:0022857(transmembrane transporter activity)|GO:0030054(cell junction)|GO:0030672(synaptic vesicle membrane)|GO:0031410(cytoplasmic vesicle)|GO:0045202(synapse)', 'GO:0000166(nucleotide binding)|GO:0003676(nucleic acid binding)', 'GO:0005515(protein binding)|GO:0005622(intracellular)|GO:0005624(membrane fraction)|GO:0006915(apoptosis)|GO:0006916(anti-apoptosis)|GO:0012505(endomembrane system)|GO:0016020(membrane)', 'GO:0000087(M phase of mitotic cell cycle)|GO:0000236(mitotic prometaphase)|GO:0000278(mitotic cell cycle)|GO:0000777(condensed chromosome kinetochore)|GO:0001669(acrosomal vesicle)|GO:0001675(acrosome assembly)|GO:0005515(protein binding)|GO:0005634(nucleus)|GO:0005654(nucleoplasm)|GO:0005694(chromosome)|GO:0005730(nucleolus)|GO:0005829(cytosol)|GO:0006334(nucleosome assembly)|GO:0007059(chromosome segregation)|GO:0008608(attachment of spindle microtubules to kinetochore)|GO:0010923(negative regulation of phosphatase activity)|GO:0034080(CenH3-containing nucleosome assembly at centromere)|GO:0051301(cell division)|GO:0071173(spindle assembly checkpoint)'], 'SEQUENCE': ['ATCTCATGGAAAAGCTGGATTCCTCTGCCTTACGCAGAAACACCCGGGCTCCATCTGCCA', 'ATGTCGGCTGTGGAGGGTTAAAGGGATGAGGCTTTCCTTTGTTTAGCAAATCTGTTCACA', 'CCCTGTCAGATAAGTTTAATGTTTAGTTTGAGGCATGAAGAAGAAAAGGGTTTCCATTCT', 'GACCAGCCAGTTTACAAGCATGTCTCAAGCTAGTGTGTTCCATTATGCTCACAGCAGTAA', 'CGGTCTCTAGCAAAGATTCAGGCATTGGATCTGTTGCAGGTAAACTGAACCTAAGTCCTT']}\n", "\n", "Gene mapping preview:\n", "{'ID': ['A_23_P100001', 'A_23_P100022', 'A_23_P100056', 'A_23_P100074', 'A_23_P100127'], 'Gene': ['FAM174B', 'SV2B', 'RBPMS2', 'AVEN', 'CASC5']}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene expression data preview:\n", "{'GSM1911565': [15.200000000000001, 6.86, 6.1, 7.16, 6.06], 'GSM1911566': [15.059999999999999, 6.85, 6.12, 7.2, 6.04], 'GSM1911567': [15.17, 7.06, 6.04, 7.17, 6.04], 'GSM1911568': [14.190000000000001, 6.7, 6.01, 7.1, 6.13], 'GSM1911569': [15.43, 7.0, 6.15, 7.34, 6.05], 'GSM1911570': [15.129999999999999, 6.8, 6.12, 7.12, 6.12], 'GSM1911571': [15.3, 6.94, 6.04, 7.09, 6.17], 'GSM1911572': [15.64, 6.89, 6.05, 6.98, 6.15], 'GSM1911573': [14.82, 7.05, 6.14, 6.68, 6.05], 'GSM1911574': [15.219999999999999, 6.94, 6.07, 7.14, 6.09], 'GSM1911575': [14.71, 6.97, 6.03, 7.22, 6.14], 'GSM1911576': [15.89, 7.19, 6.13, 7.53, 6.14], 'GSM1911577': [14.72, 6.74, 6.1, 6.99, 6.24], 'GSM1911578': [15.46, 6.89, 6.1, 7.03, 6.05], 'GSM1911579': [14.75, 6.83, 5.99, 7.2, 6.45], 'GSM1911580': [15.149999999999999, 7.05, 6.01, 7.27, 6.13], 'GSM1911581': [15.66, 7.08, 6.06, 7.27, 6.09], 'GSM1911582': [15.280000000000001, 7.03, 6.01, 7.19, 5.97], 'GSM1911583': [15.459999999999999, 6.82, 6.02, 7.52, 6.06], 'GSM1911584': [14.18, 6.87, 6.05, 7.03, 5.98], 'GSM1911585': [15.46, 7.01, 6.04, 7.4, 6.09], 'GSM1911586': [15.8, 7.27, 6.08, 6.98, 6.04], 'GSM1911587': [14.829999999999998, 6.77, 6.06, 7.19, 6.05], 'GSM1911588': [14.780000000000001, 6.62, 6.07, 7.5, 6.13], 'GSM1911589': [14.46, 7.13, 6.05, 7.76, 6.01], 'GSM1911590': [15.239999999999998, 7.28, 6.01, 6.64, 6.16], 'GSM1911591': [15.35, 7.12, 5.97, 7.15, 6.01], 'GSM1911592': [15.84, 7.2, 6.04, 7.33, 6.06], 'GSM1911593': [14.190000000000001, 6.55, 6.1, 6.87, 6.01], 'GSM1911594': [14.49, 7.22, 6.19, 7.07, 6.15], 'GSM1911595': [14.990000000000002, 6.72, 6.07, 7.29, 6.12], 'GSM1911596': [15.18, 7.14, 6.11, 7.1, 6.08], 'GSM1911597': [14.97, 7.16, 6.05, 6.82, 6.04], 'GSM1911598': [15.34, 6.8, 6.07, 7.03, 6.02], 'GSM1911599': [14.14, 6.6, 6.14, 7.33, 6.0], 'GSM1911600': [15.509999999999998, 7.09, 6.13, 7.32, 6.09]}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Hypertension/gene_data/GSE74144.csv\n" ] } ], "source": [ "# 1. Determine which columns store the gene identifiers and gene symbols\n", "# Looking at the gene_data index which contains 'A_23_P...' probe IDs \n", "# and the gene_annotation preview, we need to find matching columns\n", "\n", "# Get a more comprehensive view of the annotation data to better identify relevant columns\n", "# Let's examine more rows to see if we can find some with non-null gene symbols\n", "print(\"First few rows with non-null gene symbols:\")\n", "non_null_samples = gene_annotation[~gene_annotation['GENE_SYMBOL'].isna()].head(5)\n", "print(preview_df(non_null_samples))\n", "\n", "# We need to check which column in the gene_annotation contains probe IDs that match \n", "# those in gene_data.index (like 'A_23_P100001')\n", "# The 'ID' column in gene_annotation is likely what we need, but let's verify\n", "print(\"\\nSample probe IDs from gene_data:\")\n", "print(list(gene_data.index[:5]))\n", "print(\"\\nSample values from ID column in gene_annotation:\")\n", "print(list(gene_annotation['ID'].head(5)))\n", "\n", "# Let's find if any of the gene_data probe IDs exist in gene_annotation\n", "matching_probes = gene_annotation[gene_annotation['ID'].isin(gene_data.index)].head(5)\n", "print(\"\\nMatching probe examples:\")\n", "print(preview_df(matching_probes))\n", "\n", "# 2. Get gene mapping dataframe\n", "# Based on examination, we'll use 'ID' for probe IDs and 'GENE_SYMBOL' for gene symbols\n", "gene_mapping = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='GENE_SYMBOL')\n", "print(\"\\nGene mapping preview:\")\n", "print(preview_df(gene_mapping))\n", "\n", "# 3. Apply gene mapping to convert probe-level data to gene expression data\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "print(\"\\nGene expression data preview:\")\n", "print(preview_df(gene_data))\n", "\n", "# Save the processed gene data to CSV\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": "910f0fab", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "861bad45", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:49:43.079771Z", "iopub.status.busy": "2025-03-25T05:49:43.079649Z", "iopub.status.idle": "2025-03-25T05:49:49.312971Z", "shell.execute_reply": "2025-03-25T05:49:49.312588Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Hypertension/gene_data/GSE74144.csv\n", "Number of samples in gene data: 36\n", "First few sample IDs: ['GSM1911565', 'GSM1911566', 'GSM1911567', 'GSM1911568', 'GSM1911569']\n", "Background Information:\n", "!Series_title\t\"Gene expression changes in white blood cells of hypertensive patients with left ventricular remodeling\"\n", "!Series_summary\t\"Using transcriptomic we looked for changes in large-scale gene expression profiling of leukocytes of hypertensive patients with left ventricular remodeling compared to hypertensive patients without left ventricular remodeling and to control and whether these changes reflect metabolic pathway regulation already shown by positron emission tomography. Genes encoding for glycolytic enzymes were found over-expressed in the group of hypertensive patients with left ventricular remodeling. Expression of master genes involved in fatty acids β-oxidation was unchanged.\"\n", "!Series_overall_design\t\"Transcriptomic analysis included 14 patients with hypertension and left ventricular hypertrophy, 14 patients with hypertension and normal left ventricular size and 8 control individuals.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['subject status: hypertensive patient with normal left ventricular size', 'subject status: hypertensive patient with left ventricular remodeling', 'subject status: control individual'], 1: ['tissue: white blood cells']}\n", "Created clinical dataframe with proper sample IDs:\n", "{'Hypertension': [1.0, 1.0, 1.0, 1.0, 1.0]}\n", "Updated clinical data saved to ../../output/preprocess/Hypertension/clinical_data/GSE74144.csv\n", "Linked data shape: (36, 19446)\n", "Linked data preview (first few columns):\n", " Hypertension A1BG A1BG-AS1 A1CF A2M\n", "GSM1911565 1.0 15.20 6.86 6.10 6.06\n", "GSM1911566 1.0 15.06 6.85 6.12 6.04\n", "GSM1911567 1.0 15.17 7.06 6.04 6.04\n", "GSM1911568 1.0 14.19 6.70 6.01 6.13\n", "GSM1911569 1.0 15.43 7.00 6.15 6.05\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (36, 19446)\n", "Quartiles for 'Hypertension':\n", " 25%: 1.0\n", " 50% (Median): 1.0\n", " 75%: 1.0\n", "Min: 1.0\n", "Max: 1.0\n", "The distribution of the feature 'Hypertension' in this dataset is severely biased.\n", "\n", "Dataset not usable due to bias in trait distribution. Data not saved.\n" ] } ], "source": [ "# 1. Normalize the obtained gene data with the 'normalize_gene_symbols_in_index' function from the library.\n", "normalized_gene_data = normalize_gene_symbols_in_index(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. The clinical data issue is that we extracted it wrong - it's a single row with a trait value\n", "# instead of a proper sample-by-feature matrix. Let's correct this by revisiting the original data\n", "\n", "# First, let's check what samples we have in the gene data\n", "sample_ids = normalized_gene_data.columns\n", "print(f\"Number of samples in gene data: {len(sample_ids)}\")\n", "print(f\"First few sample IDs: {list(sample_ids[:5])}\")\n", "\n", "# Re-extract clinical data from the matrix file\n", "# Get the 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", "# Looking at the background info to understand the trait\n", "print(\"Background Information:\")\n", "print(background_info)\n", "\n", "# Create clinical data with appropriate trait values based on sample IDs\n", "# From the background info, we know:\n", "# - Samples include hypertensive patients with left ventricular remodeling (1)\n", "# - Hypertensive patients with normal left ventricular size (1)\n", "# - Control individuals (0)\n", "# But we need to know which sample belongs to which group\n", "\n", "# Check if sample characteristics contain this information\n", "sample_characteristics_dict = get_unique_values_by_row(clinical_data)\n", "print(\"Sample Characteristics Dictionary:\")\n", "print(sample_characteristics_dict)\n", "\n", "# Since the previous code revealed the samples are all hypertensive (value 1.0),\n", "# We'll use this information to build a proper clinical dataframe\n", "new_clinical_df = pd.DataFrame(index=sample_ids)\n", "\n", "# Assign all samples the hypertension value of 1 based on our previous extraction\n", "new_clinical_df[trait] = 1.0\n", "print(\"Created clinical dataframe with proper sample IDs:\")\n", "print(preview_df(new_clinical_df))\n", "\n", "# Save the updated clinical data\n", "new_clinical_df.to_csv(out_clinical_data_file)\n", "print(f\"Updated clinical data saved to {out_clinical_data_file}\")\n", "\n", "# 3. Link the clinical and genetic data\n", "linked_data = geo_link_clinical_genetic_data(new_clinical_df.T, normalized_gene_data)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "print(\"Linked data preview (first few columns):\")\n", "print(linked_data.iloc[:5, :5])\n", "\n", "# 4. Handle missing values in the linked data\n", "linked_data = handle_missing_values(linked_data, trait)\n", "print(f\"Data shape after handling missing values: {linked_data.shape}\")\n", "\n", "# 5. Determine whether the trait and demographic features are severely biased\n", "is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "\n", "# 6. Conduct quality check and save the cohort information\n", "note = \"Dataset contains white blood cell samples from hypertensive patients with and without left ventricular remodeling and control individuals. All samples in the processed dataset are from hypertensive patients.\"\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_trait_biased, \n", " df=unbiased_linked_data,\n", " note=note\n", ")\n", "\n", "# 7. If the linked data is usable, save it\n", "if is_usable:\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " unbiased_linked_data.to_csv(out_data_file)\n", " print(f\"Processed dataset saved to {out_data_file}\")\n", "else:\n", " print(\"Dataset not usable due to bias in trait distribution. 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 }