{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "43ecd915", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:30:29.950669Z", "iopub.status.busy": "2025-03-25T06:30:29.950419Z", "iopub.status.idle": "2025-03-25T06:30:30.120173Z", "shell.execute_reply": "2025-03-25T06:30:30.119711Z" } }, "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 = \"Ankylosing_Spondylitis\"\n", "cohort = \"GSE73754\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Ankylosing_Spondylitis\"\n", "in_cohort_dir = \"../../input/GEO/Ankylosing_Spondylitis/GSE73754\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Ankylosing_Spondylitis/GSE73754.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Ankylosing_Spondylitis/gene_data/GSE73754.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Ankylosing_Spondylitis/clinical_data/GSE73754.csv\"\n", "json_path = \"../../output/preprocess/Ankylosing_Spondylitis/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "ae914a4a", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "0dc1198a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:30:30.121409Z", "iopub.status.busy": "2025-03-25T06:30:30.121262Z", "iopub.status.idle": "2025-03-25T06:30:30.312129Z", "shell.execute_reply": "2025-03-25T06:30:30.311643Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Sexual Dimorphism in the Th17 Signature of Ankylosing Spondylitis\"\n", "!Series_summary\t\"Male AS patients have an elevated Th17 cell frequency vs. female AS patients (Gracey et al, Arthritis and Rheumatology, 2015). This analysis was performed to further examine differences between male and female AS patients\"\n", "!Series_overall_design\t\"AS patients were compared to healthy controls (HC). For sex-specific anaylsis, three groups were compared: F-HC vs. M-HC, M-AS vs. M-HC and F-AS vs. F-HC. A one way ANOVA was performed to identify genes differentially regulated in male and female AS patients\"\n", "Sample Characteristics Dictionary:\n", "{0: ['Sex: Male', 'Sex: Female'], 1: ['age (yr): 53', 'age (yr): 26', 'age (yr): 29', 'age (yr): 50', 'age (yr): 35', 'age (yr): 48', 'age (yr): 18', 'age (yr): 39', 'age (yr): 49', 'age (yr): 43', 'age (yr): 59', 'age (yr): 51', 'age (yr): 45', 'age (yr): 52', 'age (yr): 77', 'age (yr): 34', 'age (yr): 31', 'age (yr): 23', 'age (yr): 46', 'age (yr): 40', 'age (yr): 55', 'age (yr): 54', 'age (yr): 41', 'age (yr): 38', 'age (yr): 21', 'age (yr): 47', 'age (yr): 60', 'age (yr): 27', 'age (yr): 37', 'age (yr): 28'], 2: ['hla-b27 (1=positive, 0=negative): 1', 'hla-b27 (1=positive, 0=negative): 0', 'hla-b27 (1=positive, 0=negative): unknown'], 3: ['disease: Ankylosing Spondylitis', 'disease: healthy control']}\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": "8f024619", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "0071dc68", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:30:30.313996Z", "iopub.status.busy": "2025-03-25T06:30:30.313874Z", "iopub.status.idle": "2025-03-25T06:30:30.319453Z", "shell.execute_reply": "2025-03-25T06:30:30.318989Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Initial validation for GSE73754 completed.\n", "Gene expression data available: True\n", "Trait data available: True\n", "Trait conversion function and row identifier have been prepared.\n", "Age conversion function and row identifier have been prepared.\n", "Gender conversion function and row identifier have been prepared.\n", "To extract clinical features, the original clinical_data.csv file would be needed.\n" ] } ], "source": [ "import pandas as pd\n", "import os\n", "from typing import Dict, Any, Optional, Callable\n", "import json\n", "\n", "# 1. Gene Expression Data Availability\n", "# Based on the background information, this appears to be a gene expression study\n", "# comparing AS patients to healthy controls with a focus on sexual dimorphism\n", "is_gene_available = True\n", "\n", "# 2.1 Data Availability\n", "# Trait information is in row 3 'disease: Ankylosing Spondylitis' or 'disease: healthy control'\n", "trait_row = 3\n", "\n", "# Age information is in row 1 'age (yr): XX'\n", "age_row = 1\n", "\n", "# Gender information is in row 0 'Sex: Male' or 'Sex: Female'\n", "gender_row = 0\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value):\n", " \"\"\"Convert trait value to binary (1 for AS, 0 for healthy control)\"\"\"\n", " if pd.isna(value):\n", " return None\n", " value = value.split(':', 1)[1].strip() if ':' in value else value.strip()\n", " if 'ankylosing spondylitis' in value.lower():\n", " return 1\n", " elif 'healthy control' in value.lower():\n", " return 0\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age value to continuous\"\"\"\n", " if pd.isna(value):\n", " return None\n", " value = value.split(':', 1)[1].strip() if ':' in value else value.strip()\n", " try:\n", " return float(value)\n", " except ValueError:\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender value to binary (0 for female, 1 for male)\"\"\"\n", " if pd.isna(value):\n", " return None\n", " value = value.split(':', 1)[1].strip() if ':' in value else value.strip()\n", " if 'female' in value.lower():\n", " return 0\n", " elif 'male' in value.lower():\n", " return 1\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Determine trait data availability\n", "is_trait_available = trait_row is not None\n", "\n", "# Conduct initial filtering and save metadata\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 - Note that we don't have the actual clinical data\n", "# We can only proceed with initial validation at this stage\n", "print(f\"Initial validation for {cohort} completed.\")\n", "print(f\"Gene expression data available: {is_gene_available}\")\n", "print(f\"Trait data available: {is_trait_available}\")\n", "print(f\"Trait conversion function and row identifier have been prepared.\")\n", "print(f\"Age conversion function and row identifier have been prepared.\")\n", "print(f\"Gender conversion function and row identifier have been prepared.\")\n", "print(f\"To extract clinical features, the original clinical_data.csv file would be needed.\")\n" ] }, { "cell_type": "markdown", "id": "066cddda", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "26364c5a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:30:30.321189Z", "iopub.status.busy": "2025-03-25T06:30:30.321075Z", "iopub.status.idle": "2025-03-25T06:30:30.638038Z", "shell.execute_reply": "2025-03-25T06:30:30.637377Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "First 20 gene/probe identifiers:\n", "Index(['ILMN_1343291', 'ILMN_1343295', 'ILMN_1651199', 'ILMN_1651209',\n", " 'ILMN_1651210', 'ILMN_1651221', 'ILMN_1651228', 'ILMN_1651229',\n", " 'ILMN_1651230', 'ILMN_1651232', 'ILMN_1651235', 'ILMN_1651236',\n", " 'ILMN_1651237', 'ILMN_1651238', 'ILMN_1651249', 'ILMN_1651253',\n", " 'ILMN_1651254', 'ILMN_1651259', 'ILMN_1651260', 'ILMN_1651262'],\n", " dtype='object', name='ID')\n", "\n", "Gene data dimensions: 47323 genes × 72 samples\n" ] } ], "source": [ "# 1. Re-identify the SOFT and matrix files to ensure we have the correct paths\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. Extract the gene expression data from the matrix file\n", "gene_data = get_genetic_data(matrix_file)\n", "\n", "# 3. Print the first 20 row IDs (gene or probe identifiers)\n", "print(\"\\nFirst 20 gene/probe identifiers:\")\n", "print(gene_data.index[:20])\n", "\n", "# 4. Print the dimensions of the gene expression data\n", "print(f\"\\nGene data dimensions: {gene_data.shape[0]} genes × {gene_data.shape[1]} samples\")\n", "\n", "# Note: we keep is_gene_available as True since we successfully extracted gene expression data\n", "is_gene_available = True\n" ] }, { "cell_type": "markdown", "id": "b6c9bcc9", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "4e1ce941", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:30:30.639777Z", "iopub.status.busy": "2025-03-25T06:30:30.639658Z", "iopub.status.idle": "2025-03-25T06:30:30.641925Z", "shell.execute_reply": "2025-03-25T06:30:30.641486Z" } }, "outputs": [], "source": [ "# These are Illumina probe identifiers (ILMN_), not human gene symbols\n", "# They need to be mapped to gene symbols for proper gene expression analysis\n", "# ILMN_ prefixes indicate Illumina BeadArray platform probe IDs\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "33c54ac7", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "d4c95ed9", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:30:30.643599Z", "iopub.status.busy": "2025-03-25T06:30:30.643491Z", "iopub.status.idle": "2025-03-25T06:30:37.642966Z", "shell.execute_reply": "2025-03-25T06:30:37.642300Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['ILMN_1343048', 'ILMN_1343049', 'ILMN_1343050', 'ILMN_1343052', 'ILMN_1343059'], 'Species': [nan, nan, nan, nan, nan], 'Source': [nan, nan, nan, nan, nan], 'Search_Key': [nan, nan, nan, nan, nan], 'Transcript': [nan, nan, nan, nan, nan], 'ILMN_Gene': [nan, nan, nan, nan, nan], 'Source_Reference_ID': [nan, nan, nan, nan, nan], 'RefSeq_ID': [nan, nan, nan, nan, nan], 'Unigene_ID': [nan, nan, nan, nan, nan], 'Entrez_Gene_ID': [nan, nan, nan, nan, nan], 'GI': [nan, nan, nan, nan, nan], 'Accession': [nan, nan, nan, nan, nan], 'Symbol': ['phage_lambda_genome', 'phage_lambda_genome', 'phage_lambda_genome:low', 'phage_lambda_genome:low', 'thrB'], 'Protein_Product': [nan, nan, nan, nan, 'thrB'], 'Probe_Id': [nan, nan, nan, nan, nan], 'Array_Address_Id': [5090180.0, 6510136.0, 7560739.0, 1450438.0, 1240647.0], 'Probe_Type': [nan, nan, nan, nan, nan], 'Probe_Start': [nan, nan, nan, nan, nan], 'SEQUENCE': ['GAATAAAGAACAATCTGCTGATGATCCCTCCGTGGATCTGATTCGTGTAA', 'CCATGTGATACGAGGGCGCGTAGTTTGCATTATCGTTTTTATCGTTTCAA', 'CCGACAGATGTATGTAAGGCCAACGTGCTCAAATCTTCATACAGAAAGAT', 'TCTGTCACTGTCAGGAAAGTGGTAAAACTGCAACTCAATTACTGCAATGC', 'CTTGTGCCTGAGCTGTCAAAAGTAGAGCACGTCGCCGAGATGAAGGGCGC'], 'Chromosome': [nan, nan, nan, nan, nan], 'Probe_Chr_Orientation': [nan, nan, nan, nan, nan], 'Probe_Coordinates': [nan, nan, nan, nan, nan], 'Cytoband': [nan, nan, nan, nan, nan], 'Definition': [nan, nan, nan, nan, nan], 'Ontology_Component': [nan, nan, nan, nan, nan], 'Ontology_Process': [nan, nan, nan, nan, nan], 'Ontology_Function': [nan, nan, nan, nan, nan], 'Synonyms': [nan, nan, nan, nan, nan], 'Obsolete_Probe_Id': [nan, nan, nan, nan, nan], 'GB_ACC': [nan, nan, nan, nan, nan]}\n" ] } ], "source": [ "# 1. First get the file paths using geo_get_relevant_filepaths function\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. 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", "# 3. 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": "f3deb068", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "7e566264", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:30:37.644925Z", "iopub.status.busy": "2025-03-25T06:30:37.644793Z", "iopub.status.idle": "2025-03-25T06:30:38.786946Z", "shell.execute_reply": "2025-03-25T06:30:38.786303Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "After mapping to gene symbols:\n", "Gene data dimensions: 21464 genes × 72 samples\n", "First 10 gene symbols:\n", "Index(['A1BG', 'A1CF', 'A26C3', 'A2BP1', 'A2LD1', 'A2M', 'A2ML1', 'A3GALT2',\n", " 'A4GALT', 'A4GNT'],\n", " dtype='object', name='Gene')\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Processed gene expression data saved to ../../output/preprocess/Ankylosing_Spondylitis/gene_data/GSE73754.csv\n" ] } ], "source": [ "# 1. Identify the correct columns for mapping from the gene annotation data\n", "# ID column contains the probe IDs that match with gene_data indices\n", "# Symbol column contains the gene symbols we want to map to\n", "\n", "# 2. Get gene mapping using the appropriate columns from gene annotation\n", "gene_mapping = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol')\n", "\n", "# 3. Apply the gene mapping to convert probe-level measurements to gene expression data\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "\n", "# Preview the mapped gene expression data\n", "print(\"\\nAfter mapping to gene symbols:\")\n", "print(f\"Gene data dimensions: {gene_data.shape[0]} genes × {gene_data.shape[1]} samples\")\n", "print(\"First 10 gene symbols:\")\n", "print(gene_data.index[:10])\n", "\n", "# Save the processed gene data to file\n", "# Create directory if it doesn't exist\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\"\\nProcessed gene expression data saved to {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "47ffac9d", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "67c53510", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:30:38.789111Z", "iopub.status.busy": "2025-03-25T06:30:38.788982Z", "iopub.status.idle": "2025-03-25T06:30:51.522836Z", "shell.execute_reply": "2025-03-25T06:30:51.522163Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalizing gene symbols in the gene expression data...\n", "Original gene data shape: 21464 genes × 72 samples\n", "Normalized gene data shape: 20259 genes × 72 samples\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene expression data saved to ../../output/preprocess/Ankylosing_Spondylitis/gene_data/GSE73754.csv\n", "Extracting clinical features from original clinical data...\n", "Clinical features saved to ../../output/preprocess/Ankylosing_Spondylitis/clinical_data/GSE73754.csv\n", "Clinical features preview:\n", "{'GSM1902130': [1.0, 53.0, 1.0], 'GSM1902131': [1.0, 26.0, 1.0], 'GSM1902132': [1.0, 29.0, 1.0], 'GSM1902133': [1.0, 50.0, 1.0], 'GSM1902134': [1.0, 35.0, 1.0], 'GSM1902135': [1.0, 48.0, 1.0], 'GSM1902136': [1.0, 18.0, 1.0], 'GSM1902137': [1.0, 39.0, 1.0], 'GSM1902138': [1.0, 49.0, 1.0], 'GSM1902139': [1.0, 43.0, 1.0], 'GSM1902140': [1.0, 43.0, 1.0], 'GSM1902141': [1.0, 18.0, 1.0], 'GSM1902142': [1.0, 59.0, 1.0], 'GSM1902143': [1.0, 51.0, 1.0], 'GSM1902144': [1.0, 18.0, 1.0], 'GSM1902145': [1.0, 45.0, 1.0], 'GSM1902146': [1.0, 52.0, 1.0], 'GSM1902147': [1.0, 77.0, 1.0], 'GSM1902148': [1.0, 34.0, 1.0], 'GSM1902149': [1.0, 31.0, 1.0], 'GSM1902150': [1.0, 51.0, 1.0], 'GSM1902151': [1.0, 23.0, 1.0], 'GSM1902152': [1.0, 52.0, 0.0], 'GSM1902153': [1.0, 46.0, 0.0], 'GSM1902154': [1.0, 40.0, 0.0], 'GSM1902155': [1.0, 55.0, 1.0], 'GSM1902156': [1.0, 54.0, 0.0], 'GSM1902157': [1.0, 41.0, 0.0], 'GSM1902158': [1.0, 38.0, 0.0], 'GSM1902159': [1.0, 45.0, 0.0], 'GSM1902160': [1.0, 52.0, 0.0], 'GSM1902161': [1.0, 43.0, 0.0], 'GSM1902162': [1.0, 41.0, 0.0], 'GSM1902163': [1.0, 21.0, 0.0], 'GSM1902164': [1.0, 47.0, 0.0], 'GSM1902165': [1.0, 60.0, 0.0], 'GSM1902166': [1.0, 46.0, 0.0], 'GSM1902167': [1.0, 27.0, 0.0], 'GSM1902168': [1.0, 37.0, 0.0], 'GSM1902169': [1.0, 28.0, 0.0], 'GSM1902170': [1.0, 37.0, 0.0], 'GSM1902171': [1.0, 48.0, 0.0], 'GSM1902172': [1.0, 41.0, 0.0], 'GSM1902173': [1.0, 53.0, 0.0], 'GSM1902174': [1.0, 39.0, 0.0], 'GSM1902175': [1.0, 18.0, 0.0], 'GSM1902176': [1.0, 50.0, 0.0], 'GSM1902177': [1.0, 22.0, 1.0], 'GSM1902178': [1.0, 48.0, 1.0], 'GSM1902179': [1.0, 57.0, 1.0], 'GSM1902180': [1.0, 23.0, 1.0], 'GSM1902181': [1.0, 56.0, 0.0], 'GSM1902182': [0.0, 28.0, 1.0], 'GSM1902183': [0.0, 26.0, 1.0], 'GSM1902184': [0.0, 65.0, 1.0], 'GSM1902185': [0.0, 41.0, 1.0], 'GSM1902186': [0.0, 32.0, 1.0], 'GSM1902187': [0.0, 56.0, 1.0], 'GSM1902188': [0.0, 47.0, 1.0], 'GSM1902189': [0.0, 71.0, 1.0], 'GSM1902190': [0.0, 24.0, 1.0], 'GSM1902191': [0.0, 24.0, 1.0], 'GSM1902192': [0.0, 27.0, 0.0], 'GSM1902193': [0.0, 37.0, 0.0], 'GSM1902194': [0.0, 42.0, 0.0], 'GSM1902195': [0.0, 63.0, 0.0], 'GSM1902196': [0.0, 61.0, 0.0], 'GSM1902197': [0.0, 20.0, 0.0], 'GSM1902198': [0.0, 31.0, 0.0], 'GSM1902199': [0.0, 25.0, 0.0], 'GSM1902200': [0.0, 29.0, 0.0], 'GSM1902201': [0.0, 65.0, 0.0]}\n", "Linking clinical and genetic data...\n", "Linked data shape: (72, 20262)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (72, 20262)\n", "\n", "Checking for bias in feature variables:\n", "For the feature 'Ankylosing_Spondylitis', the least common label is '0.0' with 20 occurrences. This represents 27.78% of the dataset.\n", "The distribution of the feature 'Ankylosing_Spondylitis' in this dataset is fine.\n", "\n", "Quartiles for 'Age':\n", " 25%: 28.75\n", " 50% (Median): 41.5\n", " 75%: 51.25\n", "Min: 18.0\n", "Max: 77.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 35 occurrences. This represents 48.61% of the dataset.\n", "The distribution of the feature 'Gender' in this dataset is fine.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Ankylosing_Spondylitis/GSE73754.csv\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "print(\"Normalizing gene symbols in the gene expression data...\")\n", "# From the previous step output, we can see the data already contains gene symbols\n", "# like 'A1BG', 'A1CF', 'A2M' which need to be normalized\n", "gene_data_normalized = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Original gene data shape: {gene_data.shape[0]} genes × {gene_data.shape[1]} samples\")\n", "print(f\"Normalized gene data shape: {gene_data_normalized.shape[0]} genes × {gene_data_normalized.shape[1]} samples\")\n", "\n", "# Save the normalized gene data\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "gene_data_normalized.to_csv(out_gene_data_file)\n", "print(f\"Normalized gene expression data saved to {out_gene_data_file}\")\n", "\n", "# 2. Extract clinical features from scratch instead of loading the empty file\n", "print(\"Extracting clinical features from original clinical data...\")\n", "clinical_features = geo_select_clinical_features(\n", " clinical_data, \n", " trait, \n", " trait_row,\n", " convert_trait,\n", " age_row,\n", " convert_age,\n", " gender_row,\n", " convert_gender\n", ")\n", "\n", "# Save the extracted clinical features\n", "os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", "clinical_features.to_csv(out_clinical_data_file)\n", "print(f\"Clinical features saved to {out_clinical_data_file}\")\n", "\n", "print(\"Clinical features preview:\")\n", "print(preview_df(clinical_features))\n", "\n", "# Check if clinical features were successfully extracted\n", "if clinical_features.empty:\n", " print(\"Failed to extract clinical features. Dataset cannot be processed further.\")\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=False,\n", " is_biased=True,\n", " df=pd.DataFrame(),\n", " note=\"Clinical features could not be extracted from the dataset.\"\n", " )\n", " print(\"Dataset deemed not usable due to lack of clinical features.\")\n", "else:\n", " # 2. Link clinical and genetic data\n", " print(\"Linking clinical and genetic data...\")\n", " linked_data = geo_link_clinical_genetic_data(clinical_features, gene_data_normalized)\n", " print(f\"Linked data shape: {linked_data.shape}\")\n", "\n", " # 3. Handle missing values systematically\n", " linked_data = handle_missing_values(linked_data, trait_col=trait)\n", " print(f\"Data shape after handling missing values: {linked_data.shape}\")\n", "\n", " # 4. Check if the dataset is biased\n", " print(\"\\nChecking for bias in feature variables:\")\n", " is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "\n", " # 5. Conduct final quality 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=True,\n", " is_trait_available=True,\n", " is_biased=is_biased,\n", " df=linked_data,\n", " note=\"Dataset contains gene expression data for aniridia patients and healthy controls.\"\n", " )\n", "\n", " # 6. Save linked data if usable\n", " if is_usable:\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " linked_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", " else:\n", " print(\"Dataset deemed not usable for trait association studies, 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 }