{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "623260bd", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:58:45.918053Z", "iopub.status.busy": "2025-03-25T03:58:45.917864Z", "iopub.status.idle": "2025-03-25T03:58:46.082639Z", "shell.execute_reply": "2025-03-25T03:58:46.082293Z" } }, "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 = \"Sjögrens_Syndrome\"\n", "cohort = \"GSE51092\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Sjögrens_Syndrome\"\n", "in_cohort_dir = \"../../input/GEO/Sjögrens_Syndrome/GSE51092\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Sjögrens_Syndrome/GSE51092.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Sjögrens_Syndrome/gene_data/GSE51092.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Sjögrens_Syndrome/clinical_data/GSE51092.csv\"\n", "json_path = \"../../output/preprocess/Sjögrens_Syndrome/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "8eaca476", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "dbe9aefd", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:58:46.084043Z", "iopub.status.busy": "2025-03-25T03:58:46.083888Z", "iopub.status.idle": "2025-03-25T03:58:46.280549Z", "shell.execute_reply": "2025-03-25T03:58:46.280196Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Variants at multiple loci implicated in both innate and adaptive immune responses are associated with Sjögren’s syndrome\"\n", "!Series_summary\t\"This is a genome-wide association study performed in Sjogrens syndrome in which gene expression data was used in conjunction with genotype data to perform expression quantitative trait loci (eQTL) analysis.\"\n", "!Series_overall_design\t\"This is a case/control study.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['disease state: none', 'disease state: Sjögrens syndrome']}\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": "8e94e470", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "ce8614a0", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:58:46.281739Z", "iopub.status.busy": "2025-03-25T03:58:46.281629Z", "iopub.status.idle": "2025-03-25T03:58:46.296372Z", "shell.execute_reply": "2025-03-25T03:58:46.296086Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of selected clinical features:\n", "{'GSM1238429': [0.0], 'GSM1238430': [0.0], 'GSM1238431': [0.0], 'GSM1238432': [0.0], 'GSM1238433': [0.0], 'GSM1238434': [0.0], 'GSM1238435': [0.0], 'GSM1238436': [0.0], 'GSM1238437': [0.0], 'GSM1238438': [0.0], 'GSM1238439': [0.0], 'GSM1238440': [0.0], 'GSM1238441': [0.0], 'GSM1238442': [0.0], 'GSM1238443': [0.0], 'GSM1238444': [0.0], 'GSM1238445': [0.0], 'GSM1238446': [0.0], 'GSM1238447': [0.0], 'GSM1238448': [0.0], 'GSM1238449': [0.0], 'GSM1238450': [0.0], 'GSM1238451': [0.0], 'GSM1238452': [0.0], 'GSM1238453': [0.0], 'GSM1238454': [0.0], 'GSM1238455': [0.0], 'GSM1238456': [0.0], 'GSM1238457': [0.0], 'GSM1238458': [0.0], 'GSM1238459': [0.0], 'GSM1238460': [0.0], 'GSM1238461': [1.0], 'GSM1238462': [1.0], 'GSM1238463': [1.0], 'GSM1238464': [1.0], 'GSM1238465': [1.0], 'GSM1238466': [1.0], 'GSM1238467': [1.0], 'GSM1238468': [1.0], 'GSM1238469': [1.0], 'GSM1238470': [1.0], 'GSM1238471': [1.0], 'GSM1238472': [1.0], 'GSM1238473': [1.0], 'GSM1238474': [1.0], 'GSM1238475': [1.0], 'GSM1238476': [1.0], 'GSM1238477': [1.0], 'GSM1238478': [1.0], 'GSM1238479': [1.0], 'GSM1238480': [1.0], 'GSM1238481': [1.0], 'GSM1238482': [1.0], 'GSM1238483': [1.0], 'GSM1238484': [1.0], 'GSM1238485': [1.0], 'GSM1238486': [1.0], 'GSM1238487': [1.0], 'GSM1238488': [1.0], 'GSM1238489': [1.0], 'GSM1238490': [1.0], 'GSM1238491': [1.0], 'GSM1238492': [1.0], 'GSM1238493': [1.0], 'GSM1238494': [1.0], 'GSM1238495': [1.0], 'GSM1238496': [1.0], 'GSM1238497': [1.0], 'GSM1238498': [1.0], 'GSM1238499': [1.0], 'GSM1238500': [1.0], 'GSM1238501': [1.0], 'GSM1238502': [1.0], 'GSM1238503': [1.0], 'GSM1238504': [1.0], 'GSM1238505': [1.0], 'GSM1238506': [1.0], 'GSM1238507': [1.0], 'GSM1238508': [1.0], 'GSM1238509': [1.0], 'GSM1238510': [1.0], 'GSM1238511': [1.0], 'GSM1238512': [1.0], 'GSM1238513': [1.0], 'GSM1238514': [1.0], 'GSM1238515': [1.0], 'GSM1238516': [1.0], 'GSM1238517': [1.0], 'GSM1238518': [1.0], 'GSM1238519': [1.0], 'GSM1238520': [1.0], 'GSM1238521': [1.0], 'GSM1238522': [1.0], 'GSM1238523': [1.0], 'GSM1238524': [1.0], 'GSM1238525': [1.0], 'GSM1238526': [1.0], 'GSM1238527': [1.0], 'GSM1238528': [1.0], 'GSM1238529': [1.0], 'GSM1238530': [1.0], 'GSM1238531': [1.0], 'GSM1238532': [1.0], 'GSM1238533': [1.0], 'GSM1238534': [1.0], 'GSM1238535': [1.0], 'GSM1238536': [1.0], 'GSM1238537': [1.0], 'GSM1238538': [1.0], 'GSM1238539': [1.0], 'GSM1238540': [1.0], 'GSM1238541': [1.0], 'GSM1238542': [1.0], 'GSM1238543': [1.0], 'GSM1238544': [1.0], 'GSM1238545': [1.0], 'GSM1238546': [1.0], 'GSM1238547': [1.0], 'GSM1238548': [1.0], 'GSM1238549': [1.0], 'GSM1238550': [1.0], 'GSM1238551': [1.0], 'GSM1238552': [1.0], 'GSM1238553': [1.0], 'GSM1238554': [1.0], 'GSM1238555': [1.0], 'GSM1238556': [1.0], 'GSM1238557': [1.0], 'GSM1238558': [1.0], 'GSM1238559': [1.0], 'GSM1238560': [1.0], 'GSM1238561': [1.0], 'GSM1238562': [1.0], 'GSM1238563': [1.0], 'GSM1238564': [1.0], 'GSM1238565': [1.0], 'GSM1238566': [1.0], 'GSM1238567': [1.0], 'GSM1238568': [1.0], 'GSM1238569': [1.0], 'GSM1238570': [1.0], 'GSM1238571': [1.0], 'GSM1238572': [1.0], 'GSM1238573': [1.0], 'GSM1238574': [1.0], 'GSM1238575': [1.0], 'GSM1238576': [1.0], 'GSM1238577': [1.0], 'GSM1238578': [1.0], 'GSM1238579': [1.0], 'GSM1238580': [1.0], 'GSM1238581': [1.0], 'GSM1238582': [1.0], 'GSM1238583': [1.0], 'GSM1238584': [1.0], 'GSM1238585': [1.0], 'GSM1238586': [1.0], 'GSM1238587': [1.0], 'GSM1238588': [1.0], 'GSM1238589': [1.0], 'GSM1238590': [1.0], 'GSM1238591': [1.0], 'GSM1238592': [1.0], 'GSM1238593': [1.0], 'GSM1238594': [1.0], 'GSM1238595': [1.0], 'GSM1238596': [1.0], 'GSM1238598': [1.0], 'GSM1238599': [1.0], 'GSM1238601': [1.0], 'GSM1238603': [1.0], 'GSM1238605': [1.0], 'GSM1238607': [1.0], 'GSM1238608': [1.0], 'GSM1238610': [1.0], 'GSM1238611': [1.0], 'GSM1238612': [1.0], 'GSM1238613': [1.0], 'GSM1238614': [1.0], 'GSM1238615': [1.0], 'GSM1238616': [1.0], 'GSM1238617': [1.0], 'GSM1238618': [1.0], 'GSM1238619': [1.0], 'GSM1238620': [1.0], 'GSM1238621': [1.0], 'GSM1238622': [1.0], 'GSM1238623': [1.0], 'GSM1238624': [1.0], 'GSM1238625': [1.0], 'GSM1238626': [1.0], 'GSM1238627': [1.0], 'GSM1238628': [1.0], 'GSM1238629': [1.0], 'GSM1238630': [1.0], 'GSM1238631': [1.0], 'GSM1238632': [1.0], 'GSM1238633': [1.0], 'GSM1238634': [1.0]}\n", "Clinical data saved to ../../output/preprocess/Sjögrens_Syndrome/clinical_data/GSE51092.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the background information and series title, this study involves gene expression data\n", "# alongside genotype data for eQTL analysis, so this dataset likely contains gene expression data.\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# Looking at the sample characteristics dictionary, there's only one key (0) with disease state information\n", "trait_row = 0 # The disease state is recorded in key 0\n", "age_row = None # No age information is available in the sample characteristics\n", "gender_row = None # No gender information is available in the sample characteristics\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value):\n", " \"\"\"Convert trait values to binary format (0 for control, 1 for case)\"\"\"\n", " if pd.isna(value) or value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Convert to binary based on presence of Sjögren's syndrome\n", " if 'none' in value.lower():\n", " return 0 # Control\n", " elif 'sjogren' in value.lower() or 'sjögren' in value.lower() or 'sjogrens' in value.lower():\n", " return 1 # Case with Sjögren's syndrome\n", " else:\n", " return None # Unknown or undefined\n", "\n", "# No age data available, but define the conversion function for completeness\n", "def convert_age(value):\n", " \"\"\"Convert age values to continuous format\"\"\"\n", " if pd.isna(value) or value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " try:\n", " return float(value)\n", " except:\n", " return None\n", "\n", "# No gender data available, but define the conversion function for completeness\n", "def convert_gender(value):\n", " \"\"\"Convert gender values to binary format (0 for female, 1 for male)\"\"\"\n", " if pd.isna(value) or value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip().lower()\n", " \n", " if 'female' in value or 'f' == value:\n", " return 0\n", " elif 'male' in value or 'm' == value:\n", " return 1\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Trait data is available since trait_row is not None\n", "is_trait_available = trait_row is not None\n", "validate_and_save_cohort_info(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", "# 4. Clinical Feature Extraction\n", "# Since trait_row is not None, we proceed with clinical feature extraction\n", "if trait_row is not None:\n", " # Get clinical features using the provided function\n", " clinical_selected = 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 if age_row is not None else None,\n", " gender_row=gender_row,\n", " convert_gender=convert_gender if gender_row is not None else None\n", " )\n", " \n", " # Preview the selected clinical features\n", " preview = preview_df(clinical_selected)\n", " print(\"Preview of selected clinical features:\")\n", " print(preview)\n", " \n", " # Save the clinical data to a CSV file\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " clinical_selected.to_csv(out_clinical_data_file)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "a8742585", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "903cd838", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:58:46.297423Z", "iopub.status.busy": "2025-03-25T03:58:46.297312Z", "iopub.status.idle": "2025-03-25T03:58:46.659245Z", "shell.execute_reply": "2025-03-25T03:58:46.658870Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['ILMN_1343291', 'ILMN_1343295', 'ILMN_1651228', 'ILMN_1651229',\n", " 'ILMN_1651232', 'ILMN_1651254', 'ILMN_1651262', 'ILMN_1651278',\n", " 'ILMN_1651282', 'ILMN_1651315', 'ILMN_1651316', 'ILMN_1651336',\n", " 'ILMN_1651341', 'ILMN_1651346', 'ILMN_1651347', 'ILMN_1651354',\n", " 'ILMN_1651373', 'ILMN_1651378', 'ILMN_1651385', 'ILMN_1651403'],\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": "77438d10", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "5fa00ed1", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:58:46.660561Z", "iopub.status.busy": "2025-03-25T03:58:46.660431Z", "iopub.status.idle": "2025-03-25T03:58:46.662287Z", "shell.execute_reply": "2025-03-25T03:58:46.662014Z" } }, "outputs": [], "source": [ "# These identifiers (ILMN_*) are Illumina microarray probe IDs, not human gene symbols.\n", "# They are used on Illumina microarray platforms and need to be mapped to human gene symbols.\n", "# The \"ILMN_\" prefix is specific to Illumina's BeadArray technology.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "f7cf953d", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "fee26970", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:58:46.663372Z", "iopub.status.busy": "2025-03-25T03:58:46.663270Z", "iopub.status.idle": "2025-03-25T03:58:54.303194Z", "shell.execute_reply": "2025-03-25T03:58:54.302566Z" } }, "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. 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": "00951768", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "4a246d9a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:58:54.305163Z", "iopub.status.busy": "2025-03-25T03:58:54.305030Z", "iopub.status.idle": "2025-03-25T03:58:54.656325Z", "shell.execute_reply": "2025-03-25T03:58:54.655742Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene mapping preview:\n", "{'ID': ['ILMN_1810803', 'ILMN_1722532', 'ILMN_1708805', 'ILMN_1672526', 'ILMN_2185604'], 'Gene': ['LOC441782', 'JMJD1A', 'NCOA3', 'LOC389834', 'C17orf77']}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene expression data preview (first 5 genes):\n", " GSM1238429 GSM1238430 GSM1238431 GSM1238432 GSM1238433 GSM1238434 \\\n", "Gene \n", "AAAS 5.032245 6.004850 5.591386 5.376855 5.559783 5.522240 \n", "AACS 5.024746 5.294624 5.537792 4.939043 5.455228 5.237456 \n", "AAMDC 5.134898 5.628010 5.274912 5.831238 3.939284 5.555536 \n", "AAMP 5.796785 6.223586 6.184308 6.635248 5.909207 6.629351 \n", "AAR2 8.115394 8.362359 8.134386 7.866097 8.300355 7.922875 \n", "\n", " GSM1238435 GSM1238436 GSM1238437 GSM1238438 ... GSM1238647 \\\n", "Gene ... \n", "AAAS 5.372631 5.275120 5.577633 5.642134 ... 5.661638 \n", "AACS 4.428036 5.213866 5.271462 4.650578 ... 4.487466 \n", "AAMDC 6.733012 6.498110 6.253775 5.790463 ... 5.044327 \n", "AAMP 6.576775 6.523064 6.884937 6.629351 ... 6.543248 \n", "AAR2 8.143686 8.114382 8.116378 7.608932 ... 7.834358 \n", "\n", " GSM1238648 GSM1238649 GSM1238650 GSM1238651 GSM1238652 GSM1238653 \\\n", "Gene \n", "AAAS 4.799281 5.649854 5.317044 4.945427 5.264468 5.479907 \n", "AACS 4.013825 4.916185 5.054372 4.945509 5.246371 5.166415 \n", "AAMDC 6.177571 6.421430 5.637555 6.567191 6.147673 6.193895 \n", "AAMP 6.003556 7.032677 6.152582 6.058066 6.077363 5.936665 \n", "AAR2 8.008882 8.128582 8.036780 7.902293 7.596354 8.167810 \n", "\n", " GSM1238654 GSM1238655 GSM1238656 \n", "Gene \n", "AAAS 4.987590 4.830165 5.445184 \n", "AACS 4.485669 5.322654 4.865831 \n", "AAMDC 6.186997 6.189468 5.503927 \n", "AAMP 6.527694 6.252885 5.967655 \n", "AAR2 8.317251 8.002150 8.120343 \n", "\n", "[5 rows x 222 columns]\n" ] } ], "source": [ "# 1. Identify which columns contain the probe IDs and gene symbols\n", "# Looking at the previewed data, we see 'ID' contains ILMN_* identifiers matching our gene expression data\n", "# 'Symbol' column has gene symbols, although some values are NaN\n", "\n", "# 2. Get the gene mapping dataframe using the identified columns\n", "gene_mapping = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol')\n", "\n", "# Print a preview of the mapping to verify\n", "print(\"Gene mapping preview:\")\n", "print(preview_df(gene_mapping))\n", "\n", "# 3. Apply the gene mapping to convert probe-level measurements to gene expression data\n", "# The function already handles the equal distribution of expression values among multiple genes\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "\n", "# Normalize gene symbols to standard format\n", "gene_data = normalize_gene_symbols_in_index(gene_data)\n", "\n", "# Print the first few rows of the resulting gene expression data\n", "print(\"\\nGene expression data preview (first 5 genes):\")\n", "print(gene_data.head())\n" ] }, { "cell_type": "markdown", "id": "ea098857", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "106e812d", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:58:54.658259Z", "iopub.status.busy": "2025-03-25T03:58:54.658105Z", "iopub.status.idle": "2025-03-25T03:59:02.936889Z", "shell.execute_reply": "2025-03-25T03:59:02.936230Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data shape: (10038, 222)\n", "First few normalized gene symbols: ['AAAS', 'AACS', 'AAMDC', 'AAMP', 'AAR2', 'AARS1', 'AARS2', 'AARSD1', 'AASDH', 'AASDHPPT']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Sjögrens_Syndrome/gene_data/GSE51092.csv\n", "Loaded clinical data shape: (1, 222)\n", " GSM1238429 GSM1238430 GSM1238431 GSM1238432 GSM1238433 \\\n", "Sjögrens_Syndrome 0.0 0.0 0.0 0.0 0.0 \n", "\n", " GSM1238434 GSM1238435 GSM1238436 GSM1238437 GSM1238438 \\\n", "Sjögrens_Syndrome 0.0 0.0 0.0 0.0 0.0 \n", "\n", " ... GSM1238647 GSM1238648 GSM1238649 GSM1238650 \\\n", "Sjögrens_Syndrome ... 1.0 1.0 1.0 1.0 \n", "\n", " GSM1238651 GSM1238652 GSM1238653 GSM1238654 GSM1238655 \\\n", "Sjögrens_Syndrome 1.0 1.0 1.0 1.0 1.0 \n", "\n", " GSM1238656 \n", "Sjögrens_Syndrome 1.0 \n", "\n", "[1 rows x 222 columns]\n", "Linked data shape: (222, 10039)\n", " Sjögrens_Syndrome AAAS AACS AAMDC AAMP \\\n", "GSM1238429 0.0 5.032245 5.024746 5.134898 5.796785 \n", "GSM1238430 0.0 6.004850 5.294624 5.628010 6.223586 \n", "GSM1238431 0.0 5.591386 5.537792 5.274912 6.184308 \n", "GSM1238432 0.0 5.376855 4.939043 5.831238 6.635248 \n", "GSM1238433 0.0 5.559783 5.455228 3.939284 5.909207 \n", "\n", " AAR2 AARS1 AARS2 AARSD1 AASDH ... ZW10 \\\n", "GSM1238429 8.115394 9.119034 6.251509 7.090169 6.638629 ... 6.762811 \n", "GSM1238430 8.362359 9.619993 6.788939 6.952850 5.769205 ... 7.083691 \n", "GSM1238431 8.134386 9.153284 6.575404 6.711160 6.594359 ... 7.051679 \n", "GSM1238432 7.866097 9.116186 6.613627 6.999731 6.447584 ... 6.864088 \n", "GSM1238433 8.300355 9.802246 6.592670 6.508237 7.334083 ... 7.171763 \n", "\n", " ZWILCH ZWINT ZXDA ZXDB ZXDC ZYG11B \\\n", "GSM1238429 6.111123 2.294652 1.938593 4.858994 10.627158 10.183720 \n", "GSM1238430 5.740006 3.453000 1.792537 5.589325 11.630163 9.974142 \n", "GSM1238431 5.959515 3.298358 2.623629 4.738325 10.478923 9.772414 \n", "GSM1238432 5.864485 2.273748 2.255280 4.667191 10.552452 9.691907 \n", "GSM1238433 6.333155 2.736002 3.192722 4.545043 11.150927 10.174678 \n", "\n", " ZYX ZZEF1 ZZZ3 \n", "GSM1238429 21.876499 8.693583 15.319149 \n", "GSM1238430 22.956394 8.758791 12.910529 \n", "GSM1238431 21.878270 8.666014 14.529787 \n", "GSM1238432 21.814754 8.595559 14.520781 \n", "GSM1238433 22.172928 8.480226 15.273666 \n", "\n", "[5 rows x 10039 columns]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Shape after handling missing values: (222, 10039)\n", "For the feature 'Sjögrens_Syndrome', the least common label is '0.0' with 32 occurrences. This represents 14.41% of the dataset.\n", "The distribution of the feature 'Sjögrens_Syndrome' in this dataset is fine.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Sjögrens_Syndrome/GSE51092.csv\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Normalized gene data shape: {normalized_gene_data.shape}\")\n", "print(f\"First few normalized gene symbols: {list(normalized_gene_data.index[:10])}\")\n", "\n", "# Save the normalized gene data\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "normalized_gene_data.to_csv(out_gene_data_file)\n", "print(f\"Normalized gene data saved to {out_gene_data_file}\")\n", "\n", "# 2. Load the previously saved clinical data\n", "clinical_df = pd.read_csv(out_clinical_data_file, index_col=0)\n", "print(f\"Loaded clinical data shape: {clinical_df.shape}\")\n", "print(clinical_df.head())\n", "\n", "# 3. Link the clinical and genetic data\n", "linked_data = geo_link_clinical_genetic_data(clinical_df, normalized_gene_data)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "print(linked_data.head())\n", "\n", "# 4. Handle missing values in the linked data\n", "linked_data = handle_missing_values(linked_data, trait)\n", "print(f\"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", "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=f\"Dataset contains gene expression data from CD4 T-cells of pSS patients and healthy controls.\"\n", ")\n", "\n", "# 7. Save the data if it's usable\n", "if is_usable:\n", " # Create directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " # Save the data\n", " unbiased_linked_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", "else:\n", " print(f\"Data quality check failed. The dataset is not suitable for association studies.\")" ] } ], "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 }