{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "b399e2f5", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:41:38.324562Z", "iopub.status.busy": "2025-03-25T06:41:38.324455Z", "iopub.status.idle": "2025-03-25T06:41:38.492988Z", "shell.execute_reply": "2025-03-25T06:41:38.492630Z" } }, "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 = \"Asthma\"\n", "cohort = \"GSE188424\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Asthma\"\n", "in_cohort_dir = \"../../input/GEO/Asthma/GSE188424\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Asthma/GSE188424.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Asthma/gene_data/GSE188424.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Asthma/clinical_data/GSE188424.csv\"\n", "json_path = \"../../output/preprocess/Asthma/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "0dd7ac45", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "3f796413", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:41:38.494465Z", "iopub.status.busy": "2025-03-25T06:41:38.494328Z", "iopub.status.idle": "2025-03-25T06:41:38.789532Z", "shell.execute_reply": "2025-03-25T06:41:38.789157Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Gene expression profiling of peripheral blood from uncontrolled and controlled asthma\"\n", "!Series_summary\t\"We analyzed the transcriptomes of children with controlled and uncontrolled asthma in Taiwanese Consortium of Childhood Asthma Study (TCCAS). Hierarchical clustering, differentially expressed gene (DEG), weighted gene co-expression network analysis (WGCNA) and pathway enrichment methods were performed, to investigate important genes between two groups.\"\n", "!Series_overall_design\t\"Analysis of gene expression obtained from human whole blood comparing uncontrolled and controlled asthma.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['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": "d6284a22", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "6de9ed63", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:41:38.790897Z", "iopub.status.busy": "2025-03-25T06:41:38.790786Z", "iopub.status.idle": "2025-03-25T06:41:38.797220Z", "shell.execute_reply": "2025-03-25T06:41:38.796891Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Based on the provided information, let's analyze this dataset:\n", "\n", "# 1. Gene Expression Data Availability\n", "# The series summary mentions \"transcriptomes\" and \"gene expression profiling\"\n", "# which strongly indicates gene expression data is available\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "\n", "# 2.1 Data Availability\n", "# Trait (Asthma control status) is mentioned in the background information\n", "# However, we cannot locate it in the sample characteristics dictionary\n", "trait_row = None # Cannot find in sample characteristics\n", "is_trait_available = False # Since trait_row is None\n", "\n", "# Age data is not available in the sample characteristics\n", "age_row = None\n", "\n", "# Gender data is available at key 0\n", "gender_row = 0\n", "\n", "# 2.2 Data Type Conversion\n", "# For trait (when we locate it):\n", "def convert_trait(val):\n", " if val is None:\n", " return None\n", " val = val.lower().split(': ')[-1].strip()\n", " if 'uncontrolled' in val:\n", " return 1\n", " elif 'controlled' in val:\n", " return 0\n", " return None\n", "\n", "# For age (if we found it, which we didn't):\n", "def convert_age(val):\n", " if val is None:\n", " return None\n", " try:\n", " # Extract the value after the colon and convert to float\n", " return float(val.split(': ')[-1].strip())\n", " except:\n", " return None\n", "\n", "# For gender:\n", "def convert_gender(val):\n", " if val is None:\n", " return None\n", " val = val.lower().split(': ')[-1].strip()\n", " if 'female' in val:\n", " return 0\n", " elif 'male' in val:\n", " return 1\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Trait data is not available in the sample characteristics\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", "# Skip this substep since trait_row is None\n" ] }, { "cell_type": "markdown", "id": "73a8faf5", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "181ccba0", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:41:38.798444Z", "iopub.status.busy": "2025-03-25T06:41:38.798339Z", "iopub.status.idle": "2025-03-25T06:41:39.307150Z", "shell.execute_reply": "2025-03-25T06:41:39.306739Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix file found: ../../input/GEO/Asthma/GSE188424/GSE188424_series_matrix.txt.gz\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape: (47235, 99)\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" ] } ], "source": [ "# 1. Get the SOFT and matrix file paths again \n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "print(f\"Matrix file found: {matrix_file}\")\n", "\n", "# 2. Use the get_genetic_data function from the library to get the gene_data\n", "try:\n", " gene_data = get_genetic_data(matrix_file)\n", " print(f\"Gene data shape: {gene_data.shape}\")\n", " \n", " # 3. Print the first 20 row IDs (gene or probe identifiers)\n", " print(\"First 20 gene/probe identifiers:\")\n", " print(gene_data.index[:20])\n", "except Exception as e:\n", " print(f\"Error extracting gene data: {e}\")\n" ] }, { "cell_type": "markdown", "id": "acb57858", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "f484fbb0", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:41:39.308589Z", "iopub.status.busy": "2025-03-25T06:41:39.308462Z", "iopub.status.idle": "2025-03-25T06:41:39.310567Z", "shell.execute_reply": "2025-03-25T06:41:39.310241Z" } }, "outputs": [], "source": [ "# The identifiers starting with ILMN_ are Illumina probe IDs, not human gene symbols\n", "# These are specific to Illumina microarray platforms and need to be mapped to human gene symbols\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "1b0775a3", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "ec2b68cd", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:41:39.311678Z", "iopub.status.busy": "2025-03-25T06:41:39.311572Z", "iopub.status.idle": "2025-03-25T06:41:48.651871Z", "shell.execute_reply": "2025-03-25T06:41:48.651510Z" } }, "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. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n", "gene_annotation = get_gene_annotation(soft_file)\n", "\n", "# 2. Use the 'preview_df' function from the library to preview the data and print out the results.\n", "print(\"Gene annotation preview:\")\n", "print(preview_df(gene_annotation))\n" ] }, { "cell_type": "markdown", "id": "014ebb6b", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "ea26d19c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:41:48.653254Z", "iopub.status.busy": "2025-03-25T06:41:48.653125Z", "iopub.status.idle": "2025-03-25T06:41:50.435635Z", "shell.execute_reply": "2025-03-25T06:41:50.435236Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mapping dataframe shape: (44837, 2)\n", "First few rows of mapping dataframe:\n", " ID Gene\n", "0 ILMN_1343048 phage_lambda_genome\n", "1 ILMN_1343049 phage_lambda_genome\n", "2 ILMN_1343050 phage_lambda_genome:low\n", "3 ILMN_1343052 phage_lambda_genome:low\n", "4 ILMN_1343059 thrB\n", "Gene-level expression data shape: (21440, 99)\n", "First few 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": [ "Normalized gene expression data shape: (20238, 99)\n", "First few normalized gene symbols:\n", "Index(['A1BG', 'A1BG-AS1', 'A1CF', 'A2M', 'A2ML1', 'A3GALT2', 'A4GALT',\n", " 'A4GNT', 'AAA1', 'AAAS'],\n", " dtype='object', name='Gene')\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Saved gene expression data to ../../output/preprocess/Asthma/gene_data/GSE188424.csv\n" ] } ], "source": [ "# 1. Identify the relevant columns for gene mapping\n", "# From examining the preview, we can see:\n", "# - 'ID' column contains identifiers matching those in the gene expression data (ILMN_*)\n", "# - 'Symbol' column contains gene symbols we want to map to\n", "\n", "# 2. Get the gene mapping dataframe by extracting the identifier and symbol columns\n", "mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol')\n", "print(f\"Mapping dataframe shape: {mapping_df.shape}\")\n", "print(\"First few rows of mapping dataframe:\")\n", "print(mapping_df.head())\n", "\n", "# 3. Apply gene mapping to convert probe-level data to gene-level expression\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "print(f\"Gene-level expression data shape: {gene_data.shape}\")\n", "print(\"First few gene symbols:\")\n", "print(gene_data.index[:10])\n", "\n", "# 4. Normalize gene symbols to ensure consistency (optional but recommended)\n", "gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Normalized gene expression data shape: {gene_data.shape}\")\n", "print(\"First few normalized gene symbols:\")\n", "print(gene_data.index[:10])\n", "\n", "# 5. Save the processed 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\"Saved gene expression data to {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "30c21032", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "ade5f9e8", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:41:50.437053Z", "iopub.status.busy": "2025-03-25T06:41:50.436918Z", "iopub.status.idle": "2025-03-25T06:41:53.975418Z", "shell.execute_reply": "2025-03-25T06:41:53.975020Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical data shape: (1, 100)\n", "Clinical data column names: ['!Sample_geo_accession', 'GSM5681954', 'GSM5681955', 'GSM5681956', 'GSM5681957', 'GSM5681958', 'GSM5681959', 'GSM5681960', 'GSM5681961', 'GSM5681962', 'GSM5681963', 'GSM5681964', 'GSM5681965', 'GSM5681966', 'GSM5681967', 'GSM5681968', 'GSM5681969', 'GSM5681970', 'GSM5681971', 'GSM5681972', 'GSM5681973', 'GSM5681974', 'GSM5681975', 'GSM5681976', 'GSM5681977', 'GSM5681978', 'GSM5681979', 'GSM5681980', 'GSM5681981', 'GSM5681982', 'GSM5681983', 'GSM5681984', 'GSM5681985', 'GSM5681986', 'GSM5681987', 'GSM5681988', 'GSM5681989', 'GSM5681990', 'GSM5681991', 'GSM5681992', 'GSM5681993', 'GSM5681994', 'GSM5681995', 'GSM5681996', 'GSM5681997', 'GSM5681998', 'GSM5681999', 'GSM5682000', 'GSM5682001', 'GSM5682002', 'GSM5682003', 'GSM5682004', 'GSM5682005', 'GSM5682006', 'GSM5682007', 'GSM5682008', 'GSM5682009', 'GSM5682010', 'GSM5682011', 'GSM5682012', 'GSM5682013', 'GSM5682014', 'GSM5682015', 'GSM5682016', 'GSM5682017', 'GSM5682018', 'GSM5682019', 'GSM5682020', 'GSM5682021', 'GSM5682022', 'GSM5682023', 'GSM5682024', 'GSM5682025', 'GSM5682026', 'GSM5682027', 'GSM5682028', 'GSM5682029', 'GSM5682030', 'GSM5682031', 'GSM5682032', 'GSM5682033', 'GSM5682034', 'GSM5682035', 'GSM5682036', 'GSM5682037', 'GSM5682038', 'GSM5682039', 'GSM5682040', 'GSM5682041', 'GSM5682042', 'GSM5682043', 'GSM5682044', 'GSM5682045', 'GSM5682046', 'GSM5682047', 'GSM5682048', 'GSM5682049', 'GSM5682050', 'GSM5682051', 'GSM5682052']\n", "Sample characteristics: {0: ['gender: female', 'gender: male']}\n", "Gene data shape: (47235, 99)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data saved to ../../output/preprocess/Asthma/gene_data/GSE188424.csv\n", "Dataset usability status: False\n", "No linked data file saved since trait data is unavailable.\n" ] } ], "source": [ "# First, re-extract the necessary files from the cohort directory\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# Get the gene data again \n", "gene_data = get_genetic_data(matrix_file)\n", "\n", "# Read background information and clinical data again\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", "# Examine the clinical data structure to see what's actually available\n", "print(\"Clinical data shape:\", clinical_data.shape)\n", "print(\"Clinical data column names:\", clinical_data.columns.tolist())\n", "sample_characteristics_dict = get_unique_values_by_row(clinical_data)\n", "print(\"Sample characteristics:\", sample_characteristics_dict)\n", "\n", "# Since we previously determined trait data is not available (trait_row = None),\n", "# we can't create proper clinical data for this dataset\n", "is_trait_available = False\n", "\n", "# The gene data has already been normalized and saved in previous steps\n", "print(f\"Gene data shape: {gene_data.shape}\")\n", "\n", "# Save the normalized gene 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 data saved to {out_gene_data_file}\")\n", "\n", "# Since trait data is not available, use is_final=False in validate_and_save_cohort_info\n", "# This bypasses the need for the is_biased parameter\n", "is_usable = validate_and_save_cohort_info(\n", " is_final=False,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=True,\n", " is_trait_available=is_trait_available\n", ")\n", "\n", "print(f\"Dataset usability status: {is_usable}\")\n", "print(\"No linked data file saved since trait data is unavailable.\")" ] } ], "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 }