{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "e0933817", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:11:09.346344Z", "iopub.status.busy": "2025-03-25T07:11:09.346178Z", "iopub.status.idle": "2025-03-25T07:11:09.513099Z", "shell.execute_reply": "2025-03-25T07:11:09.512640Z" } }, "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 = \"Intellectual_Disability\"\n", "cohort = \"GSE89594\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Intellectual_Disability\"\n", "in_cohort_dir = \"../../input/GEO/Intellectual_Disability/GSE89594\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Intellectual_Disability/GSE89594.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Intellectual_Disability/gene_data/GSE89594.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Intellectual_Disability/clinical_data/GSE89594.csv\"\n", "json_path = \"../../output/preprocess/Intellectual_Disability/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "b1e9259d", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "c9ba9662", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:11:09.514640Z", "iopub.status.busy": "2025-03-25T07:11:09.514483Z", "iopub.status.idle": "2025-03-25T07:11:09.886511Z", "shell.execute_reply": "2025-03-25T07:11:09.885840Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Integrated network analysis reveals genotype-phenotype correlations in Williams syndrome\"\n", "!Series_summary\t\"Williams Syndrome (WS) is a rare neurodevelopmental disorder caused by heterozygous deletions in a chromosome 7q11.23 region typically encompassing 26-28 genes. WS patients exhibit a wide spectrum of symptoms, including cardiovascular disease, intellectual disability, visuospatial deficits and hypersociability a behavioral profile that contrasts with autism spectrum disorder (ASD). However, the relationship between neuropsychiatric phenotypes and dysregulated gene networks caused by the 7q11.23 deletion is unknown. We report results from a large-scale integrated transcriptome analysis of peripheral blood in clinically evaluated subjects with WS, ASD and matched controls. We identified significantly differential expressed genes in WS as compared with ASD or controls, even after removing genes spanning the 7q11.23 region. Using weighted gene co-expression network analysis (WGCNA), we found that three co-expression modules were upregulated in WS, and were significantly associated with the intermediate phenotypes such as anxiety and attention problems. Notably, these three co-expression modules were only composed of genes located outside of 7q11.23 critical region. One module was associated with immune systems and B cell proliferation. Its top hub gene, BCL11A, is implicated in ASD and chromatin modification. Another module was enriched with genes associated with astrocytes and oligodendrocytes, and the third module was associated with RNA processing and neurons. MicroRNA (miRNA) profiling revealed differentially expressed miRNAs whose targets were enriched in each co-expression module associated with WS. These results identify genes and potential driver miRNAs, located outside of 7q11.23 critical region, that are novel candidates for mediating the neuropsychiatric phenotypes in WS.\"\n", "!Series_overall_design\t\"We profiled gene expression from 32 WS patients, 32 ASD patients and 30 controls using peripheral blood.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['diagnosis: control', 'diagnosis: autism spectrum disorder (ASD)', 'diagnosis: Williams Syndrome (WS)'], 1: ['tissue: whole blood'], 2: ['age: 22y', 'age: 23y', 'age: 24y', 'age: 33y', 'age: 21y', 'age: 20y', 'age: 28y', 'age: 25y', 'age: 32y', 'age: 36y', 'age: 30y', 'age: 27y', 'age: 31y', 'age: 35y', 'age: 10y', 'age: 16y', 'age: 11y', 'age: 12y', 'age: 38y', 'age: 34y', 'age: 29y', 'age: 19y', 'age: 13y', 'age: 15y', 'age: 43y', 'age: 14y', 'age: 17y', 'age: 39y', 'age: 26y'], 3: ['gender: female', 'gender: male']}\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": "6a7a55c0", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "98aaf20b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:11:09.887907Z", "iopub.status.busy": "2025-03-25T07:11:09.887784Z", "iopub.status.idle": "2025-03-25T07:11:09.914595Z", "shell.execute_reply": "2025-03-25T07:11:09.914094Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical Data Preview:\n", "{'GSM2384988': [0.0, 22.0, 0.0], 'GSM2384989': [0.0, 23.0, 0.0], 'GSM2384990': [0.0, 24.0, 0.0], 'GSM2384991': [0.0, 24.0, 0.0], 'GSM2384992': [0.0, 33.0, 1.0], 'GSM2384993': [0.0, 22.0, 1.0], 'GSM2384994': [0.0, 24.0, 0.0], 'GSM2384995': [0.0, 21.0, 1.0], 'GSM2384996': [0.0, 24.0, 1.0], 'GSM2384997': [0.0, 20.0, 0.0], 'GSM2384998': [0.0, 28.0, 0.0], 'GSM2384999': [0.0, 21.0, 1.0], 'GSM2385000': [0.0, 21.0, 0.0], 'GSM2385001': [0.0, 22.0, 1.0], 'GSM2385002': [0.0, 25.0, 1.0], 'GSM2385003': [0.0, 23.0, 0.0], 'GSM2385004': [0.0, 20.0, 0.0], 'GSM2385005': [0.0, 21.0, 1.0], 'GSM2385006': [0.0, 20.0, 0.0], 'GSM2385007': [0.0, 32.0, 1.0], 'GSM2385008': [0.0, 36.0, 0.0], 'GSM2385009': [0.0, 24.0, 1.0], 'GSM2385010': [0.0, 21.0, 1.0], 'GSM2385011': [0.0, 30.0, 0.0], 'GSM2385012': [0.0, 28.0, 1.0], 'GSM2385013': [0.0, 22.0, 1.0], 'GSM2385014': [0.0, 24.0, 0.0], 'GSM2385015': [0.0, 21.0, 1.0], 'GSM2385016': [0.0, 22.0, 1.0], 'GSM2385017': [0.0, 20.0, 0.0], 'GSM2385018': [0.0, 27.0, 0.0], 'GSM2385019': [0.0, 22.0, 0.0], 'GSM2385020': [0.0, 23.0, 1.0], 'GSM2385021': [0.0, 20.0, 1.0], 'GSM2385022': [0.0, 31.0, 1.0], 'GSM2385023': [0.0, 27.0, 0.0], 'GSM2385024': [0.0, 32.0, 1.0], 'GSM2385025': [0.0, 20.0, 1.0], 'GSM2385026': [0.0, 36.0, 1.0], 'GSM2385027': [0.0, 22.0, 0.0], 'GSM2385028': [0.0, 28.0, 0.0], 'GSM2385029': [0.0, 25.0, 0.0], 'GSM2385030': [0.0, 35.0, 1.0], 'GSM2385031': [0.0, 22.0, 0.0], 'GSM2385032': [0.0, 22.0, 1.0], 'GSM2385033': [0.0, 10.0, 1.0], 'GSM2385034': [0.0, 16.0, 0.0], 'GSM2385035': [0.0, 10.0, 0.0], 'GSM2385036': [0.0, 33.0, 1.0], 'GSM2385037': [0.0, 21.0, 0.0], 'GSM2385038': [0.0, 11.0, 1.0], 'GSM2385039': [0.0, 10.0, 1.0], 'GSM2385040': [0.0, 35.0, 0.0], 'GSM2385041': [0.0, 12.0, 1.0], 'GSM2385042': [0.0, 38.0, 0.0], 'GSM2385043': [0.0, 24.0, 1.0], 'GSM2385044': [0.0, 34.0, 1.0], 'GSM2385045': [0.0, 32.0, 0.0], 'GSM2385046': [0.0, 21.0, 0.0], 'GSM2385047': [0.0, 29.0, 0.0], 'GSM2385048': [0.0, 20.0, 1.0], 'GSM2385049': [0.0, 19.0, 0.0], 'GSM2385050': [1.0, 24.0, 1.0], 'GSM2385051': [1.0, 13.0, 0.0], 'GSM2385052': [1.0, 23.0, 0.0], 'GSM2385053': [1.0, 15.0, 0.0], 'GSM2385054': [1.0, 43.0, 0.0], 'GSM2385055': [1.0, 10.0, 1.0], 'GSM2385056': [1.0, 13.0, 0.0], 'GSM2385057': [1.0, 16.0, 0.0], 'GSM2385058': [1.0, 27.0, 1.0], 'GSM2385059': [1.0, 24.0, 0.0], 'GSM2385060': [1.0, 11.0, 1.0], 'GSM2385061': [1.0, 24.0, 1.0], 'GSM2385062': [1.0, 32.0, 0.0], 'GSM2385063': [1.0, 24.0, 0.0], 'GSM2385064': [1.0, 27.0, 0.0], 'GSM2385065': [1.0, 16.0, 1.0], 'GSM2385066': [1.0, 14.0, 0.0], 'GSM2385067': [1.0, 11.0, 1.0], 'GSM2385068': [1.0, 24.0, 1.0], 'GSM2385069': [1.0, 28.0, 1.0], 'GSM2385070': [1.0, 17.0, 1.0], 'GSM2385071': [1.0, 15.0, 0.0], 'GSM2385072': [1.0, 34.0, 0.0], 'GSM2385073': [1.0, 39.0, 1.0], 'GSM2385074': [1.0, 12.0, 1.0], 'GSM2385075': [1.0, 15.0, 0.0], 'GSM2385076': [1.0, 21.0, 0.0], 'GSM2385077': [1.0, 29.0, 0.0], 'GSM2385078': [1.0, 23.0, 1.0], 'GSM2385079': [1.0, 26.0, 1.0], 'GSM2385080': [1.0, 19.0, 1.0], 'GSM2385081': [1.0, 21.0, 1.0]}\n", "Clinical data saved to ../../output/preprocess/Intellectual_Disability/clinical_data/GSE89594.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# The background information suggests this dataset contains gene expression data\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Identify rows for each variable in the sample characteristics\n", "\n", "# For trait (Intellectual Disability):\n", "# The diagnosis row (0) contains Williams Syndrome, ASD, and control status\n", "# Williams Syndrome is associated with intellectual disability according to the background info\n", "trait_row = 0\n", "\n", "# For age:\n", "# Age information is available in row 2\n", "age_row = 2\n", "\n", "# For gender:\n", "# Gender information is available in row 3\n", "gender_row = 3\n", "\n", "# 2.2 Data Type Conversion Functions\n", "\n", "def convert_trait(value):\n", " \"\"\"\n", " Convert diagnosis to intellectual disability status\n", " Williams Syndrome (WS) has intellectual disability = 1\n", " Control and ASD are set to 0\n", " \"\"\"\n", " if not value or ':' not in value:\n", " return None\n", " \n", " diagnosis = value.split(':', 1)[1].strip().lower()\n", " \n", " if 'williams syndrome' in diagnosis or 'ws' in diagnosis:\n", " return 1 # Williams Syndrome patients have intellectual disability\n", " elif 'control' in diagnosis or 'asd' in diagnosis or 'autism' in diagnosis:\n", " return 0 # Controls and ASD patients are the reference group\n", " else:\n", " return None # Unknown values\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age string to numeric value in years\"\"\"\n", " if not value or ':' not in value:\n", " return None\n", " \n", " try:\n", " # Extract the age value, typically in format \"age: XXy\"\n", " age_str = value.split(':', 1)[1].strip()\n", " # Remove 'y' and convert to integer\n", " if 'y' in age_str:\n", " age = int(age_str.replace('y', '').strip())\n", " return age\n", " else:\n", " return None\n", " except:\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender to binary: female=0, male=1\"\"\"\n", " if not value or ':' not in value:\n", " return None\n", " \n", " gender = value.split(':', 1)[1].strip().lower()\n", " \n", " if 'female' in gender:\n", " return 0\n", " elif 'male' in gender:\n", " return 1\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Check if trait data is available\n", "is_trait_available = trait_row is not None\n", "\n", "# Conduct initial filtering\n", "validate_and_save_cohort_info(\n", " is_final=False,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=is_gene_available,\n", " is_trait_available=is_trait_available\n", ")\n", "\n", "# 4. Clinical Feature Extraction\n", "if trait_row is not None:\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 the clinical data\n", " preview = preview_df(selected_clinical_df)\n", " print(\"Clinical Data Preview:\")\n", " print(preview)\n", " \n", " # Save the clinical data\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " selected_clinical_df.to_csv(out_clinical_data_file, index=False)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "c6c512db", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "0d851347", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:11:09.915978Z", "iopub.status.busy": "2025-03-25T07:11:09.915862Z", "iopub.status.idle": "2025-03-25T07:11:10.524470Z", "shell.execute_reply": "2025-03-25T07:11:10.523908Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Extracting gene data from matrix file:\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Successfully extracted gene data with 62976 rows\n", "First 20 gene IDs:\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", "\n", "Gene expression data available: True\n" ] } ], "source": [ "# 1. Get the file paths for the SOFT file and matrix file\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. Extract gene expression data from the matrix file\n", "try:\n", " print(\"Extracting gene data from matrix file:\")\n", " gene_data = get_genetic_data(matrix_file)\n", " if gene_data.empty:\n", " print(\"Extracted gene expression data is empty\")\n", " is_gene_available = False\n", " else:\n", " print(f\"Successfully extracted gene data with {len(gene_data.index)} rows\")\n", " print(\"First 20 gene IDs:\")\n", " print(gene_data.index[:20])\n", " is_gene_available = True\n", "except Exception as e:\n", " print(f\"Error extracting gene data: {e}\")\n", " print(\"This dataset appears to have an empty or malformed gene expression matrix\")\n", " is_gene_available = False\n", "\n", "print(f\"\\nGene expression data available: {is_gene_available}\")\n" ] }, { "cell_type": "markdown", "id": "9cd7aa4b", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "2149d5d8", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:11:10.526225Z", "iopub.status.busy": "2025-03-25T07:11:10.526088Z", "iopub.status.idle": "2025-03-25T07:11:10.528414Z", "shell.execute_reply": "2025-03-25T07:11:10.527975Z" } }, "outputs": [], "source": [ "# The extracted gene IDs appear to be numeric identifiers (1, 2, 3, etc.)\n", "# These are not standard human gene symbols, which would typically be alphanumeric \n", "# identifiers like BRCA1, TP53, etc.\n", "# Therefore, these identifiers need to be mapped to proper gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "09b9bb93", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "32ec005c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:11:10.529720Z", "iopub.status.busy": "2025-03-25T07:11:10.529603Z", "iopub.status.idle": "2025-03-25T07:11:19.022905Z", "shell.execute_reply": "2025-03-25T07:11:19.022265Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Extracting gene annotation data from SOFT file...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Successfully extracted gene annotation data with 5982814 rows\n", "\n", "Gene annotation preview (first few rows):\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", "\n", "Column names in gene annotation data:\n", "['ID', 'COL', 'ROW', 'NAME', 'SPOT_ID', 'CONTROL_TYPE', 'REFSEQ', 'GB_ACC', 'LOCUSLINK_ID', 'GENE_SYMBOL', 'GENE_NAME', 'UNIGENE_ID', 'ENSEMBL_ID', 'ACCESSION_STRING', 'CHROMOSOMAL_LOCATION', 'CYTOBAND', 'DESCRIPTION', 'GO_ID', 'SEQUENCE']\n", "\n", "The dataset contains GenBank accessions (GB_ACC) that could be used for gene mapping.\n", "Number of rows with GenBank accessions: 46262 out of 5982814\n", "\n", "The dataset contains genomic regions (SPOT_ID) that could be used for location-based gene mapping.\n", "Example SPOT_ID format: CONTROL\n" ] } ], "source": [ "# 1. Extract gene annotation data from the SOFT file\n", "print(\"Extracting gene annotation data from SOFT file...\")\n", "try:\n", " # Use the library function to extract gene annotation\n", " gene_annotation = get_gene_annotation(soft_file)\n", " print(f\"Successfully extracted gene annotation data with {len(gene_annotation.index)} rows\")\n", " \n", " # Preview the annotation DataFrame\n", " print(\"\\nGene annotation preview (first few rows):\")\n", " print(preview_df(gene_annotation))\n", " \n", " # Show column names to help identify which columns we need for mapping\n", " print(\"\\nColumn names in gene annotation data:\")\n", " print(gene_annotation.columns.tolist())\n", " \n", " # Check for relevant mapping columns\n", " if 'GB_ACC' in gene_annotation.columns:\n", " print(\"\\nThe dataset contains GenBank accessions (GB_ACC) that could be used for gene mapping.\")\n", " # Count non-null values in GB_ACC column\n", " non_null_count = gene_annotation['GB_ACC'].count()\n", " print(f\"Number of rows with GenBank accessions: {non_null_count} out of {len(gene_annotation)}\")\n", " \n", " if 'SPOT_ID' in gene_annotation.columns:\n", " print(\"\\nThe dataset contains genomic regions (SPOT_ID) that could be used for location-based gene mapping.\")\n", " print(\"Example SPOT_ID format:\", gene_annotation['SPOT_ID'].iloc[0])\n", " \n", "except Exception as e:\n", " print(f\"Error processing gene annotation data: {e}\")\n", " is_gene_available = False\n" ] }, { "cell_type": "markdown", "id": "f28fb34d", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "5c910e0a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:11:19.024365Z", "iopub.status.busy": "2025-03-25T07:11:19.024227Z", "iopub.status.idle": "2025-03-25T07:11:20.723431Z", "shell.execute_reply": "2025-03-25T07:11:20.722883Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Creating gene mapping dataframe...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Created mapping dataframe with 54295 rows\n", "\n", "Mapping preview (first few rows):\n", "{'ID': ['4', '5', '6', '7', '8'], 'Gene': ['HEBP1', 'KCNE4', 'BPIFA3', 'LOC100129869', 'IRG1']}\n", "\n", "Applying gene mapping to convert probe data to gene expression data...\n", "Converted gene expression data has 20353 genes and 94 samples\n", "\n", "Gene expression data preview (first few genes):\n", "{'GSM2384988': [14.514778639, 7.770994141, 50.618383177, 15.846223135999999, 54.533775519], 'GSM2384989': [16.325146179, 8.384069535, 50.382498582, 13.472051642, 52.761437181], 'GSM2384990': [15.077915928, 8.309437624, 53.497208234, 12.255831167, 53.993246103], 'GSM2384991': [16.024857048999998, 8.676199029, 52.469042418, 13.262691837, 53.174300299], 'GSM2384992': [14.205910164, 7.552051737, 54.605664255, 12.584846452, 54.944290563], 'GSM2384993': [15.044565859999999, 8.225743264, 52.634522866, 15.048242242, 56.156791729], 'GSM2384994': [15.585593495000001, 8.360763022, 53.430031596999996, 14.791714749, 54.135796581], 'GSM2384995': [15.414802946, 8.531130458, 52.296378531, 14.445646477, 53.741951262], 'GSM2384996': [15.46544158, 8.563681049, 53.578278128, 12.548410853, 54.320390376999995], 'GSM2384997': [15.188334992, 7.645462936, 51.604801099, 13.380607658, 54.028049941999996], 'GSM2384998': [15.208071473, 8.279509301, 51.628808728, 13.351027239, 54.965215073], 'GSM2384999': [14.384843081, 7.334482451, 52.500885618, 13.299840404000001, 53.994465181], 'GSM2385000': [16.228508343, 8.891434682, 52.677444221, 13.885018113000001, 53.644934982], 'GSM2385001': [15.820834405, 7.60677528, 52.941566787, 13.558493768999998, 53.682858379], 'GSM2385002': [15.542246628000001, 8.092374769, 54.296462787, 16.713950541, 55.817701374], 'GSM2385003': [15.606203765, 8.264801891, 51.017499604, 13.733707238000001, 51.878050808], 'GSM2385004': [14.788525489000001, 7.739471785, 51.129183428, 14.822868383, 53.632104864], 'GSM2385005': [15.588141816, 7.589906046, 51.438637842, 14.973570751, 55.54198422], 'GSM2385006': [15.221691556, 7.791332601, 53.387527762, 13.350007868999999, 58.38351915], 'GSM2385007': [15.182614898, 7.681608464, 51.602260008, 12.809864700999999, 54.336897011], 'GSM2385008': [15.064500563, 7.910942444, 50.564818677, 13.777841789, 53.042786054], 'GSM2385009': [14.415959057, 7.374549275, 50.234938056000004, 15.405982466, 60.094031696], 'GSM2385010': [15.252155449, 7.847153958, 50.769850901, 15.405021224, 54.049617201000004], 'GSM2385011': [14.325024884000001, 7.654060804, 51.234199409, 14.613404854999999, 52.574568599], 'GSM2385012': [14.574575793, 7.471843766, 50.988408294, 13.599394983, 56.197646129], 'GSM2385013': [14.612004424000002, 7.520175579, 50.412711681000005, 15.167934099, 54.784828061], 'GSM2385014': [15.034733809999999, 7.499608395, 50.915542939, 14.391980108000002, 53.135274698], 'GSM2385015': [15.441442778999999, 7.183445276, 50.006158191000004, 12.7894979, 51.740860614999995], 'GSM2385016': [14.808675599, 7.394731718, 50.677859092, 13.127278580999999, 56.663544262], 'GSM2385017': [16.028678433, 7.285790726, 51.17731261, 13.748385419000002, 54.180724794999996], 'GSM2385018': [14.819206488999999, 8.212299725, 52.16098627, 12.798530397, 53.765180505000004], 'GSM2385019': [15.876402774999999, 8.576060111, 51.28516764, 12.668516589, 54.191956889], 'GSM2385020': [14.48658735, 7.58231813, 53.670389927, 11.781253303, 55.029052283], 'GSM2385021': [15.65494353, 8.111493618, 52.78105333, 14.261516819, 55.185443042], 'GSM2385022': [14.577264930999998, 7.75022229, 53.30248322, 14.130206511, 55.229279775], 'GSM2385023': [15.529973493, 8.82109375, 51.710371759, 14.739540308, 57.263331858], 'GSM2385024': [15.111038657999998, 8.073654432, 52.179198576000005, 12.496934136, 56.002521074], 'GSM2385025': [13.541145989, 7.306880922, 52.245013148, 13.048745054000001, 55.083300868], 'GSM2385026': [15.111187894, 7.992965413, 54.328526858, 14.013631092999999, 56.702920637], 'GSM2385027': [15.213319873, 7.941871715, 51.302119452, 15.751812274, 54.158003797], 'GSM2385028': [15.70413072, 8.198860323, 52.332991231, 13.49559169, 53.631963457], 'GSM2385029': [15.485183658, 8.347006356, 52.136574784, 13.469957613, 55.116181289000004], 'GSM2385030': [14.900638195, 7.720622632, 52.923258712, 13.20840544, 52.269659554], 'GSM2385031': [16.611080557999998, 8.092669967, 53.112361763, 14.623842935999999, 53.425837365999996], 'GSM2385032': [15.571421111, 7.443864903, 55.0994147, 13.007856664, 53.155939671], 'GSM2385033': [14.686113411000001, 7.618498707, 50.083039758, 13.311484055, 50.499093965], 'GSM2385034': [15.364851178, 7.499608395, 50.35582029, 12.613507816, 52.173573264], 'GSM2385035': [15.132000505, 8.120731429, 51.171741685, 14.353948901999999, 54.278229549], 'GSM2385036': [14.505476922, 7.331412445, 51.088138344, 12.913108055999999, 52.938893421], 'GSM2385037': [16.046297227, 7.322498675, 51.105411921, 14.117723706, 53.187425196], 'GSM2385038': [14.857020556, 7.940107347, 50.375326833, 14.061970324999999, 54.62659122], 'GSM2385039': [15.846563862, 8.227280502, 50.889484698, 15.666275854, 54.143308817], 'GSM2385040': [15.559565976, 7.907830307, 51.604173284, 16.268928844, 53.174938775], 'GSM2385041': [15.413747365999999, 7.589686956, 50.903912476, 14.218158823, 53.794679445999996], 'GSM2385042': [14.5670214, 7.637700066, 50.308158782, 13.009492926, 53.870992255000004], 'GSM2385043': [14.756374899, 7.178712463, 50.080431688, 13.262197439000001, 55.507489918], 'GSM2385044': [14.376650598000001, 7.655983527, 50.933661795, 13.182086607, 55.343419631], 'GSM2385045': [14.738657127, 7.3039701, 51.710737219, 12.983294851, 54.536021452], 'GSM2385046': [15.170870818000001, 8.458502577, 53.408662614, 14.829188833, 52.704166898], 'GSM2385047': [14.620981407, 8.073910768, 52.955015965, 12.570423017, 53.11236176], 'GSM2385048': [14.808742795, 8.372133338, 53.919817755, 15.728971741999999, 52.872015266999995], 'GSM2385049': [14.585242728, 7.886828321, 55.70574901, 13.222675299999999, 51.968207482], 'GSM2385050': [15.101490652999999, 7.928672676, 50.499033555, 13.090782861000001, 54.664384543000004], 'GSM2385051': [15.717994099000002, 8.31032027, 52.93781385, 14.500482727000001, 55.806470327], 'GSM2385052': [16.547174634, 9.100800699, 54.982450951, 14.876630493, 56.438342568], 'GSM2385053': [15.739588716, 8.954960387, 52.483743328, 13.077261503999999, 53.909325028], 'GSM2385054': [14.237906667, 7.155351085, 51.006174055, 16.444504097, 55.934922519], 'GSM2385055': [14.92436364, 8.157209816, 53.376186268, 13.512893534, 55.938399993], 'GSM2385056': [15.840823648, 8.858657247, 53.2102955, 13.598115105, 54.589459178], 'GSM2385057': [14.564718556, 7.638663085, 50.984540842, 13.361839088, 55.202378098000004], 'GSM2385058': [15.386636787999999, 7.829753055, 52.953470342, 12.772344315, 54.99083641], 'GSM2385059': [14.972720339999999, 7.516198718, 51.643701233, 13.23635557, 57.343746733], 'GSM2385060': [14.658821205, 7.717508676, 52.560277625, 12.114741736, 54.035343806], 'GSM2385061': [14.214400051, 7.513471379, 51.568264626, 12.719771122000001, 55.358105896], 'GSM2385062': [14.099161602999999, 7.343204566, 51.333106698, 13.716296771, 55.902731573000004], 'GSM2385063': [15.334620407, 8.396146981, 53.735794094, 13.191376559, 54.960219734], 'GSM2385064': [15.574329182, 8.153710995, 52.845437511, 13.487588744, 55.318984872], 'GSM2385065': [14.690375161999999, 7.204149609, 53.828852575, 13.491544694, 56.926083055], 'GSM2385066': [15.674146073, 8.415919254, 53.877589587, 13.203544693, 55.434212141], 'GSM2385067': [15.149495808000001, 8.544201954, 52.485083438000004, 13.216461777, 54.366214208], 'GSM2385068': [15.527440821999999, 7.909698395, 51.248131147, 13.172575583, 53.454378778], 'GSM2385069': [15.32988939, 7.658055674, 51.906220305, 12.7001431, 53.870374436999995], 'GSM2385070': [16.440549845, 7.486017617, 51.619788862, 13.935205700000001, 52.352218275], 'GSM2385071': [14.989884606, 7.827727788, 51.543886938, 14.69256489, 52.948318205], 'GSM2385072': [15.455404185999999, 8.050743046, 51.290870891, 15.097122635, 56.457179194], 'GSM2385073': [15.187629216000001, 7.920919738, 51.823229574, 12.541705622999999, 56.659672981], 'GSM2385074': [14.091874915, 6.556090105, 50.49071153, 15.219592824, 55.679745118], 'GSM2385075': [15.2774536, 8.118551773, 49.802184146, 13.507086705999999, 54.369096243], 'GSM2385076': [15.865966708, 7.787725419, 52.468968021, 12.332554507, 53.90200186], 'GSM2385077': [16.074090873000003, 7.971234696, 53.28191384, 12.811633103, 54.668493264], 'GSM2385078': [15.386969381, 7.947276281, 52.113959101999995, 12.754715299, 51.153337782], 'GSM2385079': [14.906581883000001, 7.715410709, 52.443090622, 12.996738338, 52.117884591], 'GSM2385080': [15.105451233, 8.344718475, 52.865962033, 11.570669122, 52.788539824], 'GSM2385081': [15.717026044, 8.208283628, 54.361542026, 12.567853789, 53.555846783]}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Intellectual_Disability/gene_data/GSE89594.csv\n" ] } ], "source": [ "# 1. Identifying the correct columns for mapping\n", "# From the annotation preview, we can see:\n", "# 'ID' in gene annotation corresponds to gene identifiers in the expression data\n", "# 'GENE_SYMBOL' contains the gene symbols we want to map to\n", "\n", "# 2. Getting the gene mapping dataframe\n", "print(\"Creating gene mapping dataframe...\")\n", "mapping_df = get_gene_mapping(gene_annotation, 'ID', 'GENE_SYMBOL')\n", "print(f\"Created mapping dataframe with {len(mapping_df)} rows\")\n", "\n", "# Preview the mapping\n", "print(\"\\nMapping preview (first few rows):\")\n", "print(preview_df(mapping_df))\n", "\n", "# 3. Apply the gene mapping to convert probe-level data to gene expression data\n", "print(\"\\nApplying gene mapping to convert probe data to gene expression data...\")\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "print(f\"Converted gene expression data has {len(gene_data)} genes and {gene_data.shape[1]} samples\")\n", "\n", "# Preview the gene expression data\n", "print(\"\\nGene expression data preview (first few genes):\")\n", "print(preview_df(gene_data))\n", "\n", "# Save the gene expression data\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "gene_data.to_csv(out_gene_data_file)\n", "print(f\"Gene expression data saved to {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "d6047985", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "5caa25a0", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:11:20.725192Z", "iopub.status.busy": "2025-03-25T07:11:20.725058Z", "iopub.status.idle": "2025-03-25T07:11:34.174666Z", "shell.execute_reply": "2025-03-25T07:11:34.174006Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalizing gene symbols in the expression data...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Intellectual_Disability/gene_data/GSE89594.csv\n", "Normalized gene data shape: (19847, 94)\n", "\n", "Loading clinical data from file...\n", "Clinical data shape: (3, 94)\n", "\n", "Linking clinical and genetic data...\n", "Linked data shape: (94, 19850)\n", "\n", "Handling missing values...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "After handling missing values, data shape: (94, 19850)\n", "\n", "Checking for bias in features...\n", "For the feature 'Intellectual_Disability', the least common label is '1.0' with 32 occurrences. This represents 34.04% of the dataset.\n", "The distribution of the feature 'Intellectual_Disability' in this dataset is fine.\n", "\n", "Quartiles for 'Age':\n", " 25%: 20.0\n", " 50% (Median): 22.5\n", " 75%: 27.0\n", "Min: 10.0\n", "Max: 43.0\n", "The distribution of the feature 'Age' in this dataset is fine.\n", "\n", "For the feature 'Gender', the least common label is '0.0' with 47 occurrences. This represents 50.00% of the dataset.\n", "The distribution of the feature 'Gender' in this dataset is fine.\n", "\n", "\n", "Performing final validation...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Intellectual_Disability/GSE89594.csv\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "print(\"Normalizing gene symbols in the expression data...\")\n", "try:\n", " # If previous steps have already loaded gene_data\n", " normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", " \n", " # Create directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", " \n", " # Save normalized gene data\n", " normalized_gene_data.to_csv(out_gene_data_file)\n", " print(f\"Normalized gene data saved to {out_gene_data_file}\")\n", " print(f\"Normalized gene data shape: {normalized_gene_data.shape}\")\n", "except Exception as e:\n", " print(f\"Error normalizing gene data: {e}\")\n", " is_gene_available = False\n", "\n", "# 2. Load clinical data from file and link with genetic data\n", "print(\"\\nLoading clinical data from file...\")\n", "try:\n", " # Load the previously saved clinical data\n", " selected_clinical_df = pd.read_csv(out_clinical_data_file)\n", " \n", " # Set is_trait_available based on whether the clinical data contains the trait\n", " is_trait_available = True\n", " print(f\"Clinical data shape: {selected_clinical_df.shape}\")\n", " \n", " print(\"\\nLinking clinical and genetic data...\")\n", " # Format clinical data for linking - transpose it so samples are rows\n", " clinical_df_t = selected_clinical_df.T\n", " clinical_df_t.columns = [trait, 'Age', 'Gender']\n", " \n", " # Link clinical and genetic data\n", " linked_data = pd.merge(clinical_df_t, normalized_gene_data.T, \n", " left_index=True, right_index=True)\n", " print(f\"Linked data shape: {linked_data.shape}\")\n", " \n", " # 3. Handle missing values systematically\n", " print(\"\\nHandling missing values...\")\n", " linked_data = handle_missing_values(linked_data, trait)\n", " print(f\"After handling missing values, data shape: {linked_data.shape}\")\n", " \n", " # 4. Determine whether features are biased\n", " print(\"\\nChecking for bias in features...\")\n", " is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", " \n", " # 5. Final validation and save metadata\n", " print(\"\\nPerforming final validation...\")\n", " is_usable = validate_and_save_cohort_info(\n", " is_final=True,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=is_gene_available,\n", " is_trait_available=is_trait_available,\n", " is_biased=is_biased,\n", " df=linked_data,\n", " note=\"Williams Syndrome patients as intellectual disability cases, with ASD and controls as reference group.\"\n", " )\n", " \n", " # 6. Save the linked data if 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", " \n", " # Save linked data\n", " linked_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", " else:\n", " print(f\"Dataset not usable for {trait} association studies. Data not saved.\")\n", "\n", "except Exception as e:\n", " print(f\"Error in data linking or processing: {e}\")\n", " # Create a minimal dataframe for validation purposes\n", " linked_data = pd.DataFrame({trait: [0, 1]})\n", " \n", " # Perform final validation with appropriate flags\n", " is_usable = validate_and_save_cohort_info(\n", " is_final=True,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=is_gene_available, \n", " is_trait_available=is_trait_available,\n", " is_biased=True, # Not relevant since data isn't usable\n", " df=linked_data,\n", " note=\"Failed to link gene and clinical data: \" + str(e)\n", " )\n", " print(f\"Dataset usability: {is_usable}\")" ] } ], "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 }