{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "ca0ecf26", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:34:40.034492Z", "iopub.status.busy": "2025-03-25T08:34:40.034279Z", "iopub.status.idle": "2025-03-25T08:34:40.197068Z", "shell.execute_reply": "2025-03-25T08:34:40.196761Z" } }, "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 = \"Crohns_Disease\"\n", "cohort = \"GSE83448\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Crohns_Disease\"\n", "in_cohort_dir = \"../../input/GEO/Crohns_Disease/GSE83448\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Crohns_Disease/GSE83448.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Crohns_Disease/gene_data/GSE83448.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Crohns_Disease/clinical_data/GSE83448.csv\"\n", "json_path = \"../../output/preprocess/Crohns_Disease/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "7dbf9eed", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "1bf33921", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:34:40.198467Z", "iopub.status.busy": "2025-03-25T08:34:40.198333Z", "iopub.status.idle": "2025-03-25T08:34:40.291557Z", "shell.execute_reply": "2025-03-25T08:34:40.291272Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Genome-wide transcriptional analysis in intestinal biopsies from Crohn's disease (CD) patients.\"\n", "!Series_summary\t\"Differential gene expression analysis between CD patients and controls to identify the transcriptional signature that defines the inflamed intestinal mucosa in CD.\"\n", "!Series_overall_design\t\"Intestinal biopsy samples were obtained from CD patients and healthy controls. RNA was subsequently extracted from each sample. Gene expression intensities were measured using GE Healthcare/Amersham Biosciences CodeLink Human Whole Genome Bioarray. After performing the gene expression quality control analysis, we characterized the transcriptional profile of the inflamed intestinal mucosa in CD.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['tissue: intestinal mucosa'], 1: ['inflammation: Control', 'inflammation: Inflamed margin', 'inflammation: Non-inflamed margin']}\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": "8ff34959", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "a6c98889", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:34:40.292660Z", "iopub.status.busy": "2025-03-25T08:34:40.292557Z", "iopub.status.idle": "2025-03-25T08:34:40.300806Z", "shell.execute_reply": "2025-03-25T08:34:40.300522Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical data preview:\n", "{'GSM2203115': [0.0], 'GSM2203116': [0.0], 'GSM2203117': [0.0], 'GSM2203118': [1.0], 'GSM2203119': [1.0], 'GSM2203120': [1.0], 'GSM2203121': [0.0], 'GSM2203122': [1.0], 'GSM2203123': [1.0], 'GSM2203124': [1.0], 'GSM2203125': [0.0], 'GSM2203126': [1.0], 'GSM2203127': [1.0], 'GSM2203128': [1.0], 'GSM2203129': [0.0], 'GSM2203130': [1.0], 'GSM2203131': [1.0], 'GSM2203132': [0.0], 'GSM2203133': [1.0], 'GSM2203134': [1.0], 'GSM2203135': [1.0], 'GSM2203136': [0.0], 'GSM2203137': [1.0], 'GSM2203138': [1.0], 'GSM2203139': [1.0], 'GSM2203140': [0.0], 'GSM2203141': [0.0], 'GSM2203142': [1.0], 'GSM2203143': [1.0], 'GSM2203144': [0.0], 'GSM2203145': [0.0], 'GSM2203146': [0.0], 'GSM2203147': [0.0], 'GSM2203148': [1.0], 'GSM2203149': [1.0], 'GSM2203150': [1.0], 'GSM2203151': [1.0], 'GSM2203152': [1.0], 'GSM2203153': [1.0], 'GSM2203154': [1.0], 'GSM2203155': [1.0], 'GSM2203156': [1.0], 'GSM2203157': [1.0], 'GSM2203158': [1.0], 'GSM2203159': [1.0], 'GSM2203160': [1.0], 'GSM2203161': [1.0], 'GSM2203162': [1.0], 'GSM2203163': [1.0], 'GSM2203164': [1.0], 'GSM2203165': [1.0], 'GSM2203166': [1.0], 'GSM2203167': [1.0]}\n", "Clinical data saved to ../../output/preprocess/Crohns_Disease/clinical_data/GSE83448.csv\n" ] } ], "source": [ "# 1. Analyze gene expression data availability\n", "# From the background info, we can see this is a study with gene expression data from GE Healthcare/Amersham Biosciences CodeLink Human Whole Genome Bioarray\n", "is_gene_available = True\n", "\n", "# 2.1 Data Availability\n", "# Looking at the dictionary, we can see that key 1 has inflammation status\n", "# We can use this to infer Crohn's Disease status (inflamed = CD patient, control = healthy control)\n", "trait_row = 1\n", "# Age data is not available in the dictionary\n", "age_row = None\n", "# Gender data is not available in the dictionary\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion\n", "# Define conversion functions for each variable\n", "\n", "def convert_trait(value):\n", " \"\"\"\n", " Convert inflammation status to binary Crohn's Disease indicator.\n", " 0 = No CD (Control), 1 = CD (Inflamed margin or Non-inflamed margin)\n", " \"\"\"\n", " if not isinstance(value, str):\n", " return None\n", " \n", " # Extract the value after the colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " if value == \"Control\":\n", " return 0\n", " elif value in [\"Inflamed margin\", \"Non-inflamed margin\"]:\n", " return 1\n", " else:\n", " return None\n", "\n", "# No age data, but define the function as required\n", "def convert_age(value):\n", " \"\"\"Convert age to continuous value.\"\"\"\n", " if not isinstance(value, str):\n", " return None\n", " \n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " try:\n", " return float(value)\n", " except (ValueError, TypeError):\n", " return None\n", "\n", "# No gender data, but define the function as required\n", "def convert_gender(value):\n", " \"\"\"Convert gender to binary value (0=female, 1=male).\"\"\"\n", " if not isinstance(value, str):\n", " return None\n", " \n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip().lower()\n", " \n", " if value in ['female', 'f']:\n", " return 0\n", " elif value in ['male', 'm']:\n", " return 1\n", " else:\n", " return None\n", "\n", "# 3. Save metadata - initial filtering check\n", "is_trait_available = trait_row is not None\n", "validate_and_save_cohort_info(\n", " is_final=False,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=is_gene_available,\n", " is_trait_available=is_trait_available\n", ")\n", "\n", "# 4. Extract clinical features if trait data is available\n", "if trait_row is not None:\n", " 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(clinical_df)\n", " print(\"Clinical data preview:\")\n", " print(preview)\n", " \n", " # Save clinical data to CSV\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " 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": "4665e22e", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "c06530fb", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:34:40.301772Z", "iopub.status.busy": "2025-03-25T08:34:40.301671Z", "iopub.status.idle": "2025-03-25T08:34:40.438609Z", "shell.execute_reply": "2025-03-25T08:34:40.438237Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "First 20 gene/probe identifiers:\n", "Index(['GE469557', 'GE469567', 'GE469590', 'GE469632', 'GE469690', 'GE469802',\n", " 'GE469817', 'GE469849', 'GE469866', 'GE469875', 'GE469953', 'GE470103',\n", " 'GE470130', 'GE470157', 'GE470169', 'GE470208', 'GE470218', 'GE470249',\n", " 'GE470296', 'GE470328'],\n", " dtype='object', name='ID')\n", "\n", "Gene data dimensions: 20902 genes × 53 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": "091d77b6", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "dfa92f1c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:34:40.439902Z", "iopub.status.busy": "2025-03-25T08:34:40.439793Z", "iopub.status.idle": "2025-03-25T08:34:40.441648Z", "shell.execute_reply": "2025-03-25T08:34:40.441381Z" } }, "outputs": [], "source": [ "# Examine the gene identifiers\n", "# These identifiers (like GE469557) are not standard human gene symbols\n", "# Standard human gene symbols would be like BRCA1, TP53, etc.\n", "# These look like custom probes/identifiers specific to a microarray platform\n", "# They would need to be mapped to standard gene symbols for biological interpretation\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "f8a29bd4", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "ffa0f3a0", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:34:40.442749Z", "iopub.status.busy": "2025-03-25T08:34:40.442650Z", "iopub.status.idle": "2025-03-25T08:34:41.776334Z", "shell.execute_reply": "2025-03-25T08:34:41.775955Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['GE469530', 'GE469548', 'GE469549', 'GE469555', 'GE469557'], 'GB_ACC': ['AI650595.1', 'BU686968.1', 'BU623208.1', 'BE045962.1', 'AY077696.1'], 'Probe_Name': ['GE469530', 'GE469548', 'GE469549', 'GE469555', 'GE469557'], 'Probe_Type': ['DISCOVERY', 'DISCOVERY', 'DISCOVERY', 'DISCOVERY', 'DISCOVERY'], 'DESCRIPTION': [\"wa92h11x1 NCI_CGAP_GC6 cDNA clone IMAGE:2303685 3'\", \"UI-CF-DU1-ado-i-08-0-UIs1 UI-CF-DU1 cDNA clone UI-CF-DU1-ado-i-08-0-UI 3'\", \"UI-H-FL1-bgd-j-14-0-UI.s1 NCI_CGAP_FL1 cDNA clone UI-H-FL1-bgd-j-14-0-UI 3', mRNA sequence\", \"hd90g04x4 NCI_CGAP_GC6 cDNA clone IMAGE:2916822 3'\", 'clone qd65g07 PRED16 protein (PRED16) mRNA'], 'SPOT_ID': [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": "f830b849", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "b2ecca5f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:34:41.777681Z", "iopub.status.busy": "2025-03-25T08:34:41.777563Z", "iopub.status.idle": "2025-03-25T08:34:44.226206Z", "shell.execute_reply": "2025-03-25T08:34:44.225820Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Checking for additional columns in annotation data:\n", "Column 'ID' sample: ['GE469530' 'GE469548' 'GE469549']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Column 'GB_ACC' sample: ['AI650595.1' 'BU686968.1' 'BU623208.1']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Column 'Probe_Name' sample: ['GE469530' 'GE469548' 'GE469549']\n", "Column 'Probe_Type' sample: ['DISCOVERY']\n", "Column 'DESCRIPTION' sample: [\"wa92h11x1 NCI_CGAP_GC6 cDNA clone IMAGE:2303685 3'\"\n", " \"UI-CF-DU1-ado-i-08-0-UIs1 UI-CF-DU1 cDNA clone UI-CF-DU1-ado-i-08-0-UI 3'\"\n", " \"UI-H-FL1-bgd-j-14-0-UI.s1 NCI_CGAP_FL1 cDNA clone UI-H-FL1-bgd-j-14-0-UI 3', mRNA sequence\"]\n", "Column 'SPOT_ID' sample: ['INCYTE UNIQUE']\n", "\n", "Using GenBank accessions as gene identifiers.\n", "\n", "Gene mapping dataframe shape: (1156663, 2)\n", "Sample of gene mapping:\n", "{'ID': ['GE469530', 'GE469548', 'GE469549', 'GE469555', 'GE469557'], 'Gene': ['AI650595.1', 'BU686968.1', 'BU623208.1', 'BE045962.1', 'AY077696.1']}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "After mapping - Gene data dimensions: (5353, 53)\n", "\n", "First few gene identifiers after mapping:\n", "Index(['AA010870', 'AA021186', 'AA029225', 'AA057423', 'AA058586', 'AA127601',\n", " 'AA149620', 'AA150617', 'AA166934', 'AA187037'],\n", " dtype='object', name='Gene')\n", "\n", "Note: The dataset is using GenBank accessions rather than standard gene symbols.\n", "This may affect downstream analysis that relies on gene symbol annotations.\n", "\n", "Gene expression data saved to ../../output/preprocess/Crohns_Disease/gene_data/GSE83448.csv\n" ] } ], "source": [ "# 1. Look for alternative gene symbol columns in the annotation data\n", "# First, check if there are any hidden/unprefixed columns that might contain gene symbols\n", "print(\"Checking for additional columns in annotation data:\")\n", "for col in gene_annotation.columns:\n", " unique_values = gene_annotation[col].dropna().unique()\n", " if len(unique_values) > 0:\n", " print(f\"Column '{col}' sample: {unique_values[:3]}\")\n", "\n", "# Since we don't see standard gene symbols, we'll use GB_ACC (GenBank accessions)\n", "# as identifiers for the gene expression data\n", "print(\"\\nUsing GenBank accessions as gene identifiers.\")\n", "gene_mapping = get_gene_mapping(gene_annotation, 'ID', 'GB_ACC')\n", "\n", "# Check the mapping dataframe\n", "print(f\"\\nGene mapping dataframe shape: {gene_mapping.shape}\")\n", "print(\"Sample of gene mapping:\")\n", "print(preview_df(gene_mapping))\n", "\n", "# 3. Convert probe-level measurements to gene expression\n", "# Note: We're working with accession numbers, not gene symbols\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "\n", "# Preview the results\n", "print(\"\\nAfter mapping - Gene data dimensions:\", gene_data.shape)\n", "print(\"\\nFirst few gene identifiers after mapping:\")\n", "print(gene_data.index[:10])\n", "\n", "# Skip normalization since these are not standard gene symbols\n", "# We'll keep the accession numbers as identifiers\n", "print(\"\\nNote: The dataset is using GenBank accessions rather than standard gene symbols.\")\n", "print(\"This may affect downstream analysis that relies on gene symbol annotations.\")\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\"\\nGene expression data saved to {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "f9445cb6", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "c9256f02", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:34:44.227605Z", "iopub.status.busy": "2025-03-25T08:34:44.227497Z", "iopub.status.idle": "2025-03-25T08:34:45.641361Z", "shell.execute_reply": "2025-03-25T08:34:45.640822Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Processing gene expression data...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Crohns_Disease/gene_data/GSE83448.csv\n", "Gene data shape: 5353 genes × 53 samples\n", "Extracting clinical features from original clinical data...\n", "Clinical features saved to ../../output/preprocess/Crohns_Disease/clinical_data/GSE83448.csv\n", "Clinical features preview:\n", "{'GSM2203115': [0.0], 'GSM2203116': [0.0], 'GSM2203117': [0.0], 'GSM2203118': [1.0], 'GSM2203119': [1.0], 'GSM2203120': [1.0], 'GSM2203121': [0.0], 'GSM2203122': [1.0], 'GSM2203123': [1.0], 'GSM2203124': [1.0], 'GSM2203125': [0.0], 'GSM2203126': [1.0], 'GSM2203127': [1.0], 'GSM2203128': [1.0], 'GSM2203129': [0.0], 'GSM2203130': [1.0], 'GSM2203131': [1.0], 'GSM2203132': [0.0], 'GSM2203133': [1.0], 'GSM2203134': [1.0], 'GSM2203135': [1.0], 'GSM2203136': [0.0], 'GSM2203137': [1.0], 'GSM2203138': [1.0], 'GSM2203139': [1.0], 'GSM2203140': [0.0], 'GSM2203141': [0.0], 'GSM2203142': [1.0], 'GSM2203143': [1.0], 'GSM2203144': [0.0], 'GSM2203145': [0.0], 'GSM2203146': [0.0], 'GSM2203147': [0.0], 'GSM2203148': [1.0], 'GSM2203149': [1.0], 'GSM2203150': [1.0], 'GSM2203151': [1.0], 'GSM2203152': [1.0], 'GSM2203153': [1.0], 'GSM2203154': [1.0], 'GSM2203155': [1.0], 'GSM2203156': [1.0], 'GSM2203157': [1.0], 'GSM2203158': [1.0], 'GSM2203159': [1.0], 'GSM2203160': [1.0], 'GSM2203161': [1.0], 'GSM2203162': [1.0], 'GSM2203163': [1.0], 'GSM2203164': [1.0], 'GSM2203165': [1.0], 'GSM2203166': [1.0], 'GSM2203167': [1.0]}\n", "Linking clinical and genetic data...\n", "Linked data shape: (53, 5354)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (53, 5354)\n", "\n", "Checking for bias in feature variables:\n", "For the feature 'Crohns_Disease', the least common label is '0.0' with 14 occurrences. This represents 26.42% of the dataset.\n", "The distribution of the feature 'Crohns_Disease' in this dataset is fine.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Crohns_Disease/GSE83448.csv\n" ] } ], "source": [ "# 1. Skip gene symbol normalization and use the accession numbers directly\n", "print(\"Processing gene expression data...\")\n", "# Don't normalize - these are GenBank accessions, not gene symbols\n", "gene_data_normalized = gene_data # Use the original gene data with accession numbers\n", "\n", "# Save the gene data (without normalization)\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", "print(f\"Gene data shape: {gene_data.shape[0]} genes × {gene_data.shape[1]} samples\")\n", "\n", "# 2. Extract clinical features from scratch\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)\n", " print(f\"Linked data shape: {linked_data.shape}\")\n", "\n", " # Check if the linked data has gene features\n", " if linked_data.shape[1] <= 1:\n", " print(\"Error: Linked data has no gene 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=False,\n", " is_trait_available=True,\n", " is_biased=True,\n", " df=linked_data,\n", " note=\"Failed to link gene expression data with clinical features.\"\n", " )\n", " else:\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", " # Check if there are still samples after missing value handling\n", " if linked_data.shape[0] == 0:\n", " print(\"Error: No samples remain after handling missing values.\")\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=True,\n", " df=pd.DataFrame(),\n", " note=\"All samples were removed during missing value handling.\"\n", " )\n", " else:\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 Crohn's Disease 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 }