{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "77c27aad", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:53:26.937307Z", "iopub.status.busy": "2025-03-25T06:53:26.937200Z", "iopub.status.idle": "2025-03-25T06:53:27.093363Z", "shell.execute_reply": "2025-03-25T06:53:27.093022Z" } }, "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 = \"Autism_spectrum_disorder_(ASD)\"\n", "cohort = \"GSE87847\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Autism_spectrum_disorder_(ASD)\"\n", "in_cohort_dir = \"../../input/GEO/Autism_spectrum_disorder_(ASD)/GSE87847\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Autism_spectrum_disorder_(ASD)/GSE87847.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Autism_spectrum_disorder_(ASD)/gene_data/GSE87847.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Autism_spectrum_disorder_(ASD)/clinical_data/GSE87847.csv\"\n", "json_path = \"../../output/preprocess/Autism_spectrum_disorder_(ASD)/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "e4542924", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "00bb3d64", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:53:27.094720Z", "iopub.status.busy": "2025-03-25T06:53:27.094586Z", "iopub.status.idle": "2025-03-25T06:53:27.206225Z", "shell.execute_reply": "2025-03-25T06:53:27.205931Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"A Putative Blood-Based Biomarker for Autism Spectrum Disorder-Associated Ileocolitis\"\n", "!Series_summary\t\"Analysis of gene expression in inflamed gastrointestinal tissue and blood from GI-symptomatic children with ASD compared to non-inflamed tissue and blood from typically developing GI-syptomatic children. The hypothesis being tested was that peripheral blood would yield a surrogate biomarker for GI inflammation in children with ASD.\"\n", "!Series_overall_design\t\"Total RNA was isolated from inflamed gastrointestinal tissue (terminal ileum and/or colon) and peripheral blood from children with ASD and corresponding (non-iflamed) tissue and blood from typically developing children.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['sample type: Autism Spectrum Disorder', 'sample type: typically developing'], 1: ['disease state: inflamed', 'disease state: non-inflamed'], 2: ['Sex: male', 'Sex: female']}\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": "c4f7cd57", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "249f80d5", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:53:27.207230Z", "iopub.status.busy": "2025-03-25T06:53:27.207128Z", "iopub.status.idle": "2025-03-25T06:53:27.212453Z", "shell.execute_reply": "2025-03-25T06:53:27.212178Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Error in clinical feature extraction: [Errno 2] No such file or directory: '../../input/GEO/Autism_spectrum_disorder_(ASD)/GSE87847/clinical_data.csv'\n", "Clinical data might not be available yet from previous steps.\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "import os\n", "import json\n", "from typing import Optional, Callable, Dict, Any\n", "\n", "# 1. Determine if gene expression data is available\n", "# From the background information, we can see this dataset contains gene expression data from tissues and blood\n", "is_gene_available = True # Gene expression data is available\n", "\n", "# 2. Define variables for trait, age, and gender availability\n", "\n", "# 2.1 Data Availability\n", "# From the sample characteristics dictionary:\n", "# Key 0 contains info about ASD vs typically developing (trait)\n", "# Key 1 contains info about inflammation state (not our target trait)\n", "# Key 2 contains gender information\n", "# No age information is available\n", "\n", "trait_row = 0 # ASD status is in row 0\n", "age_row = None # Age information is not available\n", "gender_row = 2 # Gender information is in row 2\n", "\n", "# 2.2 Data Type Conversion Functions\n", "\n", "def convert_trait(value):\n", " \"\"\"Convert ASD trait values to binary (1 for ASD, 0 for typically developing)\"\"\"\n", " if pd.isna(value):\n", " return None\n", " \n", " # Extract value after the colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " if 'autism' in value.lower() or 'asd' in value.lower():\n", " return 1\n", " elif 'typically developing' in value.lower():\n", " return 0\n", " else:\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age values to continuous numbers\"\"\"\n", " # We don't have age data in this dataset\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender values to binary (0 for female, 1 for male)\"\"\"\n", " if pd.isna(value):\n", " return None\n", " \n", " # Extract value after the colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " if 'male' in value.lower():\n", " return 1\n", " elif 'female' in value.lower():\n", " return 0\n", " else:\n", " return None\n", "\n", "# 3. Save metadata - Initial filtering\n", "# trait_row is not None, so trait data is available\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. Clinical Feature Extraction (if trait data is available)\n", "if trait_row is not None:\n", " # Load the clinical data\n", " # Assuming clinical_data was obtained in a previous step and is available\n", " try:\n", " # This is a placeholder - the actual clinical_data should come from a previous step\n", " clinical_data = pd.read_csv(os.path.join(in_cohort_dir, \"clinical_data.csv\"))\n", " \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 extracted clinical features\n", " preview = preview_df(selected_clinical_df)\n", " print(\"Preview of selected clinical features:\")\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", " except Exception as e:\n", " print(f\"Error in clinical feature extraction: {e}\")\n", " print(\"Clinical data might not be available yet from previous steps.\")\n" ] }, { "cell_type": "markdown", "id": "0f085a2c", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "cbde29b8", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:53:27.213311Z", "iopub.status.busy": "2025-03-25T06:53:27.213212Z", "iopub.status.idle": "2025-03-25T06:53:27.413447Z", "shell.execute_reply": "2025-03-25T06:53:27.413152Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['7A5', 'A1BG', 'A1CF', 'A26C3', 'A2BP1', 'A2LD1', 'A2M', 'A2ML1',\n", " 'A3GALT2', 'A4GALT', 'A4GNT', 'AAA1', 'AAAS', 'AACS', 'AADAC',\n", " 'AADACL1', 'AADACL2', 'AADACL4', 'AADAT', 'AAGAB'],\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": "7e1f98bc", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "94b155ca", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:53:27.414657Z", "iopub.status.busy": "2025-03-25T06:53:27.414541Z", "iopub.status.idle": "2025-03-25T06:53:27.416397Z", "shell.execute_reply": "2025-03-25T06:53:27.416137Z" } }, "outputs": [], "source": [ "# I need to review the gene identifiers from the gene expression data\n", "\n", "# These appear to be human gene symbols, not probe IDs or other identifiers that would require mapping.\n", "# The list includes well-known gene symbols like A1BG, A2M, AAAS, AADAC, etc.\n", "# These are standard HGNC gene symbols and don't require mapping to other identifiers.\n", "\n", "requires_gene_mapping = False\n" ] }, { "cell_type": "markdown", "id": "5ff7e960", "metadata": {}, "source": [ "### Step 5: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 6, "id": "9c245ea3", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:53:27.417486Z", "iopub.status.busy": "2025-03-25T06:53:27.417382Z", "iopub.status.idle": "2025-03-25T06:53:40.080124Z", "shell.execute_reply": "2025-03-25T06:53:40.079269Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of selected clinical features:\n", "{'GSM2341810': [1.0, 1.0], 'GSM2341811': [1.0, 1.0], 'GSM2341812': [1.0, 1.0], 'GSM2341813': [1.0, 1.0], 'GSM2341814': [1.0, 1.0], 'GSM2341815': [1.0, 1.0], 'GSM2341816': [1.0, 1.0], 'GSM2341817': [1.0, 1.0], 'GSM2341818': [1.0, 1.0], 'GSM2341819': [1.0, 1.0], 'GSM2341820': [1.0, 1.0], 'GSM2341821': [1.0, 1.0], 'GSM2341822': [1.0, 1.0], 'GSM2341823': [1.0, 1.0], 'GSM2341824': [1.0, 1.0], 'GSM2341825': [1.0, 1.0], 'GSM2341826': [1.0, 1.0], 'GSM2341827': [1.0, 1.0], 'GSM2341828': [1.0, 1.0], 'GSM2341829': [1.0, 1.0], 'GSM2341830': [1.0, 1.0], 'GSM2341831': [1.0, 1.0], 'GSM2341832': [1.0, 1.0], 'GSM2341833': [1.0, 1.0], 'GSM2341834': [1.0, 1.0], 'GSM2341835': [1.0, 1.0], 'GSM2341836': [1.0, 1.0], 'GSM2341837': [1.0, 1.0], 'GSM2341838': [1.0, 1.0], 'GSM2341839': [1.0, 1.0], 'GSM2341840': [1.0, 1.0], 'GSM2341841': [1.0, 1.0], 'GSM2341842': [1.0, 1.0], 'GSM2341843': [1.0, 1.0], 'GSM2341844': [1.0, 1.0], 'GSM2341845': [1.0, 1.0], 'GSM2341846': [1.0, 1.0], 'GSM2341847': [1.0, 1.0], 'GSM2341848': [1.0, 1.0], 'GSM2341849': [1.0, 1.0], 'GSM2341850': [1.0, 1.0], 'GSM2341851': [1.0, 1.0], 'GSM2341852': [1.0, 1.0], 'GSM2341853': [1.0, 1.0], 'GSM2341854': [1.0, 1.0], 'GSM2341855': [0.0, 1.0], 'GSM2341856': [0.0, 1.0], 'GSM2341857': [0.0, 1.0], 'GSM2341858': [0.0, 1.0], 'GSM2341859': [0.0, 1.0], 'GSM2341860': [0.0, 1.0], 'GSM2341861': [0.0, 1.0], 'GSM2341862': [0.0, 1.0], 'GSM2341863': [0.0, 1.0], 'GSM2341864': [0.0, 1.0], 'GSM2341865': [0.0, 1.0], 'GSM2341866': [0.0, 1.0], 'GSM2341867': [0.0, 1.0], 'GSM2341868': [0.0, 1.0], 'GSM2341869': [0.0, 1.0], 'GSM2341870': [0.0, 1.0], 'GSM2341871': [0.0, 1.0], 'GSM2341872': [0.0, 1.0], 'GSM2341873': [0.0, 1.0], 'GSM2341874': [0.0, 1.0], 'GSM2341875': [0.0, 1.0], 'GSM2341876': [0.0, 1.0], 'GSM2341877': [0.0, 1.0], 'GSM2341878': [0.0, 1.0], 'GSM2341879': [0.0, 1.0], 'GSM2341880': [0.0, 1.0], 'GSM2341881': [0.0, 1.0], 'GSM2341882': [0.0, 1.0], 'GSM2341883': [0.0, 1.0], 'GSM2341884': [0.0, 1.0], 'GSM2341885': [0.0, 1.0], 'GSM2341886': [0.0, 1.0], 'GSM2341887': [0.0, 1.0], 'GSM2341888': [0.0, 1.0], 'GSM2341889': [0.0, 1.0], 'GSM2341890': [0.0, 1.0], 'GSM2341891': [0.0, 1.0], 'GSM2341892': [0.0, 1.0], 'GSM2341893': [0.0, 1.0], 'GSM2341894': [0.0, 1.0], 'GSM2341895': [0.0, 1.0], 'GSM2341896': [0.0, 1.0], 'GSM2341897': [0.0, 1.0], 'GSM2341898': [0.0, 1.0], 'GSM2341899': [0.0, 1.0], 'GSM2341900': [0.0, 1.0], 'GSM2341901': [0.0, 1.0], 'GSM2341902': [0.0, 1.0]}\n", "Clinical data saved to ../../output/preprocess/Autism_spectrum_disorder_(ASD)/clinical_data/GSE87847.csv\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Autism_spectrum_disorder_(ASD)/gene_data/GSE87847.csv\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "For the feature 'Autism_spectrum_disorder_(ASD)', the least common label is '1.0' with 45 occurrences. This represents 48.39% of the dataset.\n", "The distribution of the feature 'Autism_spectrum_disorder_(ASD)' in this dataset is fine.\n", "\n", "For the feature 'Gender', the least common label is '1.0' with 93 occurrences. This represents 100.00% of the dataset.\n", "The distribution of the feature 'Gender' in this dataset is severely biased.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Autism_spectrum_disorder_(ASD)/GSE87847.csv\n" ] } ], "source": [ "# 1. We need to first create the selected_clinical_df using clinical_data from Step 1\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 extracted clinical features\n", "preview = preview_df(selected_clinical_df)\n", "print(\"Preview of selected clinical features:\")\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)\n", "print(f\"Clinical data saved to {out_clinical_data_file}\")\n", "\n", "# 1. Normalize the obtained gene data with the 'normalize_gene_symbols_in_index' function from the library.\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "# Create directory if it doesn't exist\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. Link the clinical and genetic data with the 'geo_link_clinical_genetic_data' function from the library.\n", "linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)\n", "\n", "# 3. Handle missing values in the linked data\n", "linked_data = handle_missing_values(linked_data, trait)\n", "\n", "# 4. Determine whether the trait and some demographic features are severely biased, and remove biased features.\n", "is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "\n", "# 5. 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=\"Dataset contains gene expression data from both blood and GI tissue of ASD and typically developing children.\"\n", ")\n", "\n", "# 6. If the linked data is usable, save it as a CSV file to 'out_data_file'.\n", "if is_usable:\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " unbiased_linked_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", "else:\n", " print(\"The dataset was determined to be not usable for analysis.\")" ] } ], "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 }