{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "b424ed33", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:55:24.041787Z", "iopub.status.busy": "2025-03-25T07:55:24.041571Z", "iopub.status.idle": "2025-03-25T07:55:24.230164Z", "shell.execute_reply": "2025-03-25T07:55:24.229805Z" } }, "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 = \"Lupus_(Systemic_Lupus_Erythematosus)\"\n", "cohort = \"GSE165004\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Lupus_(Systemic_Lupus_Erythematosus)\"\n", "in_cohort_dir = \"../../input/GEO/Lupus_(Systemic_Lupus_Erythematosus)/GSE165004\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Lupus_(Systemic_Lupus_Erythematosus)/GSE165004.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Lupus_(Systemic_Lupus_Erythematosus)/gene_data/GSE165004.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Lupus_(Systemic_Lupus_Erythematosus)/clinical_data/GSE165004.csv\"\n", "json_path = \"../../output/preprocess/Lupus_(Systemic_Lupus_Erythematosus)/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "ef29ffad", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "4608314d", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:55:24.231623Z", "iopub.status.busy": "2025-03-25T07:55:24.231486Z", "iopub.status.idle": "2025-03-25T07:55:24.513858Z", "shell.execute_reply": "2025-03-25T07:55:24.513530Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Endometrial Tissue RNA expression in recurrent pregnancy losses and unexplained infertility vs. conrol\"\n", "!Series_summary\t\"Recent studies are directed to decode the genetic signature of endometrial receptivity for better outcomes in assisted reproductive technologies. In this study, we aimed to understand the transcriptomic profile of midsecretory phase endometria of patients with recurrent pregnancy losses (RPL) and unexplained infertility (UI) by comparing with the endometria of healthy fertile women (Controls).\"\n", "!Series_summary\t\"In this prospective cohort study, we took endometrial samples from 24 patients with RPL, 24 patients with UI and 24 Controls at day 19-21 of the menstrual cycle. By performing genomic analysis, we assessed for differentially expressed genes (DEGs) and pathway analysis.\"\n", "!Series_overall_design\t\"All patients involved in this prospective cohort study were recruited from Istanbul University School of Medicine between August 2014 and August 2015. Three cohorts (fertile controls, patients with RPL and UI) were studied and 24 patients were included in each cohort. None of the patients has received a prior infertility treatment and were not under a current treatment. The first cohort comprised fertile control patients who presented to our gynecology department for well woman examinations. The inclusion criteria were regularly cycling women aged under 35 years with at least one live birth, no history of infertility/treatment, no previous miscarriages and no associated gynecologic (endometriosis, fibroids, active or history of pelvic inflammatory disease) or other medical comorbidities (hyperprolactinemia, thyroid disease etc). The remaining cohorts constituted patients who presented to our in vitro fertilization (IVF) unit. The second cohort included patients with RPL with no history of successful pregnancies. The inclusion criteria for this group were regularly cycling women aged under 35 years with at least two consecutive pregnancy losses of 20 weeks or less, normal follicle-stimulating hormone (FSH), luteinizing hormone (LH), estradiol (E2), prolactin (PRL), and thyroid-stimulating hormone (TSH) levels at day 2-3, normal uterine cavity shape and size, and bilateral tubal patency observed on hysterosalpingogram, no mutations detected in Factor V (Leiden) and prothrombin gene analysis, normal antithrombin III, protein C and S activity, negative results for lupus anticoagulant evaluation, cardiolipin antibody (IgM and IgG), and beta2-glycoprotein antibody (IgM and IgG) and normal karyotype. Their partners have normal spermiogram results and normal karyotype. The third cohort was formed by women with UI at least of 18 months of duration. The inclusion criteria for this group were regularly cycling women aged under 35 years with normal FSH, LH, E2, PRL, and TSH levels at day 2-3, normal uterine cavity shape and size, and bilateral tubal patency observed on a hysterosalpingogram. Their partners have normal spermiogram results.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['subject status/group: Control', 'subject status/group: patient with RPL', 'subject status/group: patient with UIF'], 1: ['tissue: Endometrial tissue']}\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": "975312ae", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "ac67d582", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:55:24.515227Z", "iopub.status.busy": "2025-03-25T07:55:24.515115Z", "iopub.status.idle": "2025-03-25T07:55:24.522541Z", "shell.execute_reply": "2025-03-25T07:55:24.522261Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Load the data from the previous step\n", "clinical_data = pd.DataFrame({'sample': range(1, 73)}) # Creating a placeholder DataFrame\n", "for i, values in {0: ['subject status/group: Control', 'subject status/group: patient with RPL', 'subject status/group: patient with UIF'], 1: ['tissue: Endometrial tissue']}.items():\n", " clinical_data[i] = values[0] # Just fill with the first value, to be modified next\n", "\n", "# 1. Gene Expression Data Availability\n", "# Based on the background information, this appears to be a gene expression study analyzing\n", "# differentially expressed genes in endometrial tissue, so we should have gene expression data\n", "is_gene_available = True\n", "\n", "# 2.1 Data Availability\n", "# This dataset is about recurrent pregnancy losses and unexplained infertility,\n", "# not Lupus (Systemic Lupus Erythematosus). There is no Lupus trait data here.\n", "trait_row = None\n", "\n", "# Age is not mentioned in the sample characteristics\n", "age_row = None\n", "\n", "# Gender is not explicitly mentioned, but from the background this is a study on female reproductive health\n", "# (endometrial tissue), so all subjects are female, making it a constant feature\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion functions\n", "# These functions are included for completeness but won't be used since the required data is not available\n", "def convert_trait(value):\n", " \"\"\"\n", " Convert trait value to binary (0 for control, 1 for disease)\n", " \"\"\"\n", " if \":\" in value:\n", " value = value.split(\":\")[1].strip()\n", " \n", " # The dataset doesn't contain information about Lupus\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"\n", " Convert age value to continuous number\n", " \"\"\"\n", " if \":\" in value:\n", " value = value.split(\":\")[1].strip()\n", " \n", " try:\n", " return float(value)\n", " except:\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"\n", " Convert gender value to binary (0 for female, 1 for male)\n", " \"\"\"\n", " if \":\" in value:\n", " value = value.split(\":\")[1].strip()\n", " \n", " if value.lower() in [\"female\", \"f\"]:\n", " return 0\n", " elif value.lower() in [\"male\", \"m\"]:\n", " return 1\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Trait data is NOT available for Lupus in this dataset\n", "is_trait_available = trait_row is not None\n", "\n", "# Save cohort info\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", "# Skip this substep since trait_row is None (no Lupus trait data available)\n", "# The dataset is studying pregnancy loss and infertility, not Lupus\n" ] }, { "cell_type": "markdown", "id": "5698af68", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "c29c2695", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:55:24.523705Z", "iopub.status.busy": "2025-03-25T07:55:24.523603Z", "iopub.status.idle": "2025-03-25T07:55:25.009497Z", "shell.execute_reply": "2025-03-25T07:55:25.009105Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This appears to be a SuperSeries. Looking at the SOFT file to find potential subseries:\n", "No subseries references found in the first 1000 lines of the SOFT file.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene data extraction result:\n", "Number of rows: 62976\n", "First 20 gene/probe identifiers:\n", "Index(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13',\n", " '14', '15', '16', '17', '18', '19', '20'],\n", " dtype='object', name='ID')\n" ] } ], "source": [ "# 1. First get the path to the soft and matrix files\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. Looking more carefully at the background information\n", "# This is a SuperSeries which doesn't contain direct gene expression data\n", "# Need to investigate the soft file to find the subseries\n", "print(\"This appears to be a SuperSeries. Looking at the SOFT file to find potential subseries:\")\n", "\n", "# Open the SOFT file to try to identify subseries\n", "with gzip.open(soft_file, 'rt') as f:\n", " subseries_lines = []\n", " for i, line in enumerate(f):\n", " if 'Series_relation' in line and 'SuperSeries of' in line:\n", " subseries_lines.append(line.strip())\n", " if i > 1000: # Limit search to first 1000 lines\n", " break\n", "\n", "# Display the subseries found\n", "if subseries_lines:\n", " print(\"Found potential subseries references:\")\n", " for line in subseries_lines:\n", " print(line)\n", "else:\n", " print(\"No subseries references found in the first 1000 lines of the SOFT file.\")\n", "\n", "# Despite trying to extract gene data, we expect it might fail because this is a SuperSeries\n", "try:\n", " gene_data = get_genetic_data(matrix_file)\n", " print(\"\\nGene data extraction result:\")\n", " print(\"Number of rows:\", len(gene_data))\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", " print(\"This confirms the dataset is a SuperSeries without direct gene expression data.\")\n" ] }, { "cell_type": "markdown", "id": "b0f284ac", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "72889a35", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:55:25.010957Z", "iopub.status.busy": "2025-03-25T07:55:25.010835Z", "iopub.status.idle": "2025-03-25T07:55:25.013027Z", "shell.execute_reply": "2025-03-25T07:55:25.012694Z" } }, "outputs": [], "source": [ "# Based on the gene identifiers shown, these appear to be numeric indices (1, 2, 3, etc.) rather than\n", "# human gene symbols or standard identifiers like Entrez IDs or Ensembl IDs.\n", "# \n", "# These numeric identifiers need to be mapped to actual gene symbols for meaningful analysis.\n", "# In GEO datasets, such numeric identifiers often correspond to probe IDs that require mapping\n", "# to gene symbols using platform annotation files.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "98df7817", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "b944230c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:55:25.014267Z", "iopub.status.busy": "2025-03-25T07:55:25.014157Z", "iopub.status.idle": "2025-03-25T07:55:31.529163Z", "shell.execute_reply": "2025-03-25T07:55:31.528772Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['1', '2', '3', '4', '5'], 'COL': ['192', '192', '192', '192', '192'], 'ROW': [328.0, 326.0, 324.0, 322.0, 320.0], 'NAME': ['GE_BrightCorner', 'DarkCorner', 'DarkCorner', 'A_23_P117082', 'A_33_P3246448'], 'SPOT_ID': ['CONTROL', 'CONTROL', 'CONTROL', 'A_23_P117082', 'A_33_P3246448'], 'CONTROL_TYPE': ['pos', 'pos', 'pos', 'FALSE', 'FALSE'], 'REFSEQ': [nan, nan, nan, 'NM_015987', 'NM_080671'], 'GB_ACC': [nan, nan, nan, 'NM_015987', 'NM_080671'], 'LOCUSLINK_ID': [nan, nan, nan, 50865.0, 23704.0], 'GENE_SYMBOL': [nan, nan, nan, 'HEBP1', 'KCNE4'], 'GENE_NAME': [nan, nan, nan, 'heme binding protein 1', 'potassium voltage-gated channel, Isk-related family, member 4'], 'UNIGENE_ID': [nan, nan, nan, 'Hs.642618', 'Hs.348522'], 'ENSEMBL_ID': [nan, nan, nan, 'ENST00000014930', 'ENST00000281830'], 'ACCESSION_STRING': [nan, nan, nan, 'ref|NM_015987|ens|ENST00000014930|gb|AF117615|gb|BC016277', 'ref|NM_080671|ens|ENST00000281830|tc|THC2655788'], 'CHROMOSOMAL_LOCATION': [nan, nan, nan, 'chr12:13127906-13127847', 'chr2:223920197-223920256'], 'CYTOBAND': [nan, nan, nan, 'hs|12p13.1', 'hs|2q36.1'], 'DESCRIPTION': [nan, 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]'], 'GO_ID': [nan, 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)'], 'SEQUENCE': [nan, nan, nan, 'AAGGGGGAAAATGTGATTTGTGCCTGATCTTTCATCTGTGATTCTTATAAGAGCTTTGTC', 'GCAAGTCTCTCTGCACCTATTAAAAAGTGATGTATATACTTCCTTCTTATTCTGTTGAGT']}\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": "9f519c8a", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "acd2b0b5", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:55:31.530537Z", "iopub.status.busy": "2025-03-25T07:55:31.530414Z", "iopub.status.idle": "2025-03-25T07:55:31.938885Z", "shell.execute_reply": "2025-03-25T07:55:31.938462Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene mapping dataframe created with shape: (54295, 2)\n", "Gene mapping preview:\n", "{'ID': ['4', '5', '6', '7', '8'], 'Gene': ['HEBP1', 'KCNE4', 'BPIFA3', 'LOC100129869', 'IRG1']}\n", "Gene expression data after mapping:\n", "Number of genes: 20353\n", "Number of samples: 72\n", "First few genes and samples:\n", "{'GSM5024320': [16.175948293, 7.516155648, 46.507521084, 15.217321422, 149.14750401999999], 'GSM5024321': [16.011941074, 8.217093594, 48.623684219, 17.167041386, 154.14509097], 'GSM5024322': [16.572305297, 8.622670281, 49.566614717, 16.896298113, 160.74762947], 'GSM5024323': [15.295619113, 7.726044368, 50.172972701, 17.398087139, 147.61739465], 'GSM5024324': [15.083688478, 7.957276484, 48.823144464, 16.810108987, 147.75492186], 'GSM5024325': [16.328166471, 8.197432653, 50.503169901999996, 14.990370204000001, 146.82150985], 'GSM5024326': [16.823341796, 8.457233429, 46.53437868, 17.392785906, 158.77921269], 'GSM5024327': [16.669563058, 8.552464361, 48.166319294000004, 15.09498574, 153.30795133], 'GSM5024328': [16.354194291, 8.478245126, 48.463737826, 15.895852237, 147.72828052], 'GSM5024329': [16.683895776, 8.233580635, 47.958823960000004, 15.556160404, 147.19422251], 'GSM5024330': [16.543175341, 7.286097393, 47.050416186999996, 15.909210288, 146.36459429], 'GSM5024331': [16.476693762, 8.503945159, 47.050343283000004, 16.551534987, 155.70651467], 'GSM5024332': [16.084254901, 8.23760259, 47.42295553, 15.516187293, 148.31563724], 'GSM5024333': [16.467412023, 8.329962204, 47.723583424, 16.261962087, 157.41158476], 'GSM5024334': [16.632711573, 8.61765436, 45.911571618, 18.498515158, 159.10502192], 'GSM5024335': [16.016125682, 8.230531777, 47.1180278, 17.869744908, 148.63909564], 'GSM5024336': [15.861111583, 7.895883523, 50.494044699, 15.769426885, 150.73275386], 'GSM5024337': [16.175426854999998, 8.658045992, 46.958349159, 16.172092964, 152.54364548], 'GSM5024338': [15.485400293000001, 6.936337515, 49.045773843, 17.673025259, 140.43661006], 'GSM5024339': [17.294097028, 8.284609722, 50.044545443, 16.952362169, 152.60340407], 'GSM5024340': [16.478222186, 8.010812742, 48.253801646, 15.958907461999999, 148.0057184], 'GSM5024341': [15.905303830000001, 8.005715263, 49.594593732, 17.939329276, 152.78523741], 'GSM5024342': [16.587276482, 8.34078747, 46.512966293000005, 16.975509178, 154.56534263], 'GSM5024343': [16.866060116, 8.907544316, 49.32091064, 16.641235482, 154.63880556], 'GSM5024344': [17.799854071, 8.463107871, 50.493786874, 15.637664882, 150.98671841], 'GSM5024345': [16.206126911, 7.360318368, 48.809242991, 17.396940918, 143.47021231], 'GSM5024346': [17.358228862, 7.768199356, 50.518540834999996, 16.123007339, 145.6925782], 'GSM5024347': [16.945005617, 9.083965445, 47.343980616, 16.754724766000002, 157.87048531], 'GSM5024348': [16.305579816999998, 7.837160408, 48.357386572, 15.857946901999998, 143.13718863], 'GSM5024349': [16.518407459, 8.440619098, 50.265816764, 17.426578028, 153.45916846], 'GSM5024350': [16.456898665, 7.943097878, 47.060957447999996, 17.528439032999998, 153.09201905], 'GSM5024351': [15.554059839, 7.4935401, 48.147537039, 17.410874407999998, 143.21306974], 'GSM5024352': [15.726719328000001, 7.642218556, 48.668967366000004, 17.899631645, 146.30691443], 'GSM5024353': [14.949905706, 7.649372308, 48.412781744, 17.620906390000002, 143.75868739], 'GSM5024354': [15.640949629, 8.131225592, 46.156312403, 17.10632361, 154.05690888], 'GSM5024355': [17.137464918, 8.181439658, 48.219861875, 17.319002578, 159.04052865], 'GSM5024356': [16.684601646, 8.161633241, 50.788072138000004, 15.220115415, 152.51444947], 'GSM5024357': [16.043323178, 7.657682033, 50.492763764, 16.752427768, 145.33670969], 'GSM5024358': [15.546449055, 7.800757342, 48.935380734999995, 17.523063344, 155.77222258], 'GSM5024359': [17.168120727, 8.566881919, 51.035059276, 17.669932434, 157.77155006], 'GSM5024360': [16.637982092999998, 7.37113089, 47.902334532, 16.177301597, 152.0590372], 'GSM5024361': [15.443152173, 7.929682813, 49.662613023, 16.219609622, 139.86267389], 'GSM5024362': [16.785683886, 8.109588627, 47.343803227, 15.979384309, 153.09867223], 'GSM5024363': [16.574338975, 6.502315468, 49.681100103, 16.349717251, 145.98601414], 'GSM5024364': [16.818199033, 8.425691683, 47.183900749, 17.381712786, 155.04989512], 'GSM5024365': [15.879660818, 7.856115404, 44.976502868, 17.250903834, 139.7070564], 'GSM5024366': [18.291142154, 8.103481869, 47.7720977, 16.615693397, 154.70819447], 'GSM5024367': [16.364880864, 6.958256821, 48.690998414, 17.379992979, 155.21462387], 'GSM5024368': [16.470964891999998, 7.973152319, 49.186234155, 15.251887122, 146.20462119], 'GSM5024369': [16.271969488, 8.208550284, 45.821841705, 15.901732389000001, 153.55904793], 'GSM5024370': [14.474491711999999, 7.970498477, 47.876216624, 17.144408178, 156.1889915], 'GSM5024371': [16.757985621, 7.639909788, 47.941679053, 16.936369081, 143.78485729], 'GSM5024372': [15.168150827, 8.705556245, 44.859027267, 16.387060554, 155.57425972000001], 'GSM5024373': [16.030387564, 8.06640045, 47.291720881, 16.88106342, 143.40615126], 'GSM5024374': [14.382542926000001, 7.206222578, 50.267553594, 17.019771327, 124.18906918], 'GSM5024375': [15.559213466, 8.47251674, 44.238866896, 17.017121407, 147.53181071], 'GSM5024376': [17.433950737, 8.54516096, 45.792379927, 16.06354851, 150.21611807], 'GSM5024377': [15.867835534000001, 8.417803506, 48.054632926, 17.323748738, 157.36381416], 'GSM5024378': [14.544952948, 7.85393013, 44.625112556, 16.353133814, 145.21576758], 'GSM5024379': [15.414147748000001, 8.764406902, 47.887693232000004, 14.053811473, 149.77348244], 'GSM5024380': [14.540774082, 7.492316502, 49.706907412999996, 15.882476309, 142.83240491], 'GSM5024381': [17.254833218, 7.818197224, 48.137395174, 14.832233991999999, 152.11208479], 'GSM5024382': [17.319139773, 8.107156292, 47.487755140000004, 15.874827577000001, 151.54490742], 'GSM5024383': [16.974310573, 8.803790906, 41.169731545, 12.007061884999999, 155.66033594], 'GSM5024384': [16.580382207, 8.419940493, 49.456174883, 17.414356345999998, 149.32935473999999], 'GSM5024385': [15.345795255, 8.237984259, 47.771294832, 15.552435227, 155.44479358], 'GSM5024386': [16.913078327, 8.066662052, 47.408999777, 17.070240257000002, 150.46234911], 'GSM5024387': [16.923380163, 8.468067556, 47.638320697, 15.634584088, 152.83907624], 'GSM5024388': [16.804482744, 8.570127856, 52.049876413999996, 16.377546007, 154.25332425], 'GSM5024389': [16.243998632, 8.082198101, 47.394251597, 17.336162568, 144.68359099], 'GSM5024390': [15.65130067, 7.886027909, 46.301649024, 18.069094486, 155.44176998], 'GSM5024391': [15.911735297, 7.919663149, 50.665590921, 17.381404702, 160.27326031]}\n" ] } ], "source": [ "# 1. Identify the right columns for mapping between gene expression data and gene annotations\n", "# The gene expression data has numeric indices that match the 'ID' column in gene_annotation\n", "# The 'GENE_SYMBOL' column in gene_annotation contains the human gene symbols we want to map to\n", "\n", "# 2. Create the gene mapping dataframe using the appropriate columns\n", "gene_mapping = get_gene_mapping(gene_annotation, 'ID', 'GENE_SYMBOL')\n", "print(\"Gene mapping dataframe created with shape:\", gene_mapping.shape)\n", "print(\"Gene mapping preview:\")\n", "print(preview_df(gene_mapping))\n", "\n", "# 3. Apply the gene mapping to convert probe-level expression to gene-level expression\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "print(\"Gene expression data after mapping:\")\n", "print(\"Number of genes:\", len(gene_data))\n", "print(\"Number of samples:\", len(gene_data.columns))\n", "print(\"First few genes and samples:\")\n", "print(preview_df(gene_data))\n" ] }, { "cell_type": "markdown", "id": "f54f9ba8", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "6c1010df", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:55:31.940244Z", "iopub.status.busy": "2025-03-25T07:55:31.940124Z", "iopub.status.idle": "2025-03-25T07:55:33.059010Z", "shell.execute_reply": "2025-03-25T07:55:33.058611Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Top 10 gene indices before normalization: ['A1BG', 'A1BG-AS1', 'A1CF', 'A2LD1', 'A2M', 'A2ML1', 'A2MP1', 'A4GALT', 'A4GNT', 'AA06']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Top 10 gene indices after normalization: ['A1BG', 'A1BG-AS1', 'A1CF', 'A2M', 'A2ML1', 'A2MP1', 'A4GALT', 'A4GNT', 'AA06', 'AAA1']\n", "Shape of normalized gene data: (19847, 72)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Saved normalized gene data to ../../output/preprocess/Lupus_(Systemic_Lupus_Erythematosus)/gene_data/GSE165004.csv\n", "Clinical data not available for Lupus_(Systemic_Lupus_Erythematosus) in this dataset\n", "Shape of linked data: (72, 19847)\n", "Dataset validation failed: No lupus trait data available. Final linked data not saved.\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "print(f\"Top 10 gene indices before normalization: {gene_data.index[:10].tolist()}\")\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Top 10 gene indices after normalization: {normalized_gene_data.index[:10].tolist()}\")\n", "print(f\"Shape of normalized gene data: {normalized_gene_data.shape}\")\n", "\n", "# Create directory for gene data file if it doesn't exist\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "# Save the normalized gene data\n", "normalized_gene_data.to_csv(out_gene_data_file)\n", "print(f\"Saved normalized gene data to {out_gene_data_file}\")\n", "\n", "# 2. Link the clinical and genetic data\n", "# Based on our analysis in Step 2, we determined that this dataset does not contain \n", "# lupus trait data. The dataset is about recurrent pregnancy losses and unexplained infertility,\n", "# not Lupus (Systemic Lupus Erythematosus).\n", "\n", "# Create a dummy clinical dataframe to properly document the absence of trait data\n", "sample_ids = normalized_gene_data.columns.tolist()\n", "clinical_df = pd.DataFrame(index=sample_ids)\n", "# We'll create an empty clinical dataframe to accurately represent that we don't have lupus data\n", "print(f\"Clinical data not available for {trait} in this dataset\")\n", "\n", "# 3. Prepare linked data - in this case, it will just be the gene expression data\n", "# with missing clinical information\n", "linked_data = normalized_gene_data.T\n", "print(f\"Shape of linked data: {linked_data.shape}\")\n", "\n", "# 4. We cannot properly handle missing values for trait because the trait data \n", "# doesn't exist in this dataset\n", "\n", "# 5. Since the trait data is completely missing, we shouldn't proceed with bias analysis\n", "is_trait_biased = True # Mark as biased because we have no trait data for lupus\n", "unbiased_linked_data = linked_data # Keep the gene data for reference\n", "\n", "# 6. Conduct quality check and save the cohort information - report that trait data is not available\n", "is_trait_available = False # No trait data for lupus\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_trait_biased,\n", " df=unbiased_linked_data,\n", " note=\"Dataset contains gene expression data from endometrial tissue study of recurrent pregnancy losses \" +\n", " \"and unexplained infertility, not lupus. No lupus trait data available.\"\n", ")\n", "\n", "# 7. Since the linked data is not usable for lupus analysis, we won't save it as a final dataset\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\"Saved processed linked data to {out_data_file}\")\n", "else:\n", " print(\"Dataset validation failed: No lupus trait data available. Final 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 }