{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "34d61bd8", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:41:56.021396Z", "iopub.status.busy": "2025-03-25T06:41:56.021215Z", "iopub.status.idle": "2025-03-25T06:41:56.191268Z", "shell.execute_reply": "2025-03-25T06:41:56.190912Z" } }, "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 = \"GSE230164\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Asthma\"\n", "in_cohort_dir = \"../../input/GEO/Asthma/GSE230164\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Asthma/GSE230164.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Asthma/gene_data/GSE230164.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Asthma/clinical_data/GSE230164.csv\"\n", "json_path = \"../../output/preprocess/Asthma/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "92bfef84", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "95c1d853", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:41:56.192760Z", "iopub.status.busy": "2025-03-25T06:41:56.192620Z", "iopub.status.idle": "2025-03-25T06:41:56.489929Z", "shell.execute_reply": "2025-03-25T06:41:56.489587Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Gene expression profiling of asthma\"\n", "!Series_summary\t\"This SuperSeries is composed of the SubSeries listed below.\"\n", "!Series_overall_design\t\"Refer to individual Series\"\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": "913b1076", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "b39b3aaf", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:41:56.491259Z", "iopub.status.busy": "2025-03-25T06:41:56.491144Z", "iopub.status.idle": "2025-03-25T06:41:56.498558Z", "shell.execute_reply": "2025-03-25T06:41:56.498241Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the background information, this is a SuperSeries about gene expression profiling of asthma\n", "# This indicates it likely contains gene expression data\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# From the sample characteristics dictionary, we can see gender information is available at index 0\n", "# There's no explicit trait (asthma) or age information in the sample characteristics\n", "trait_row = None # Trait information not directly available\n", "age_row = None # Age information not available\n", "gender_row = 0 # Gender information is at index 0\n", "\n", "# 2.2 Data Type Conversion\n", "# For trait (unavailable, but defining function for completeness)\n", "def convert_trait(value):\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip().lower()\n", " else:\n", " value = value.strip().lower()\n", " \n", " # Binary conversion for asthma\n", " if 'asthma' in value or 'yes' in value or 'positive' in value or 'case' in value:\n", " return 1\n", " elif 'control' in value or 'no' in value or 'negative' in value or 'healthy' in value:\n", " return 0\n", " return None\n", "\n", "# For age (unavailable, but defining function for completeness)\n", "def convert_age(value):\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " else:\n", " value = value.strip()\n", " \n", " # Try to convert to float for continuous age\n", " try:\n", " return float(value)\n", " except:\n", " return None\n", "\n", "# For gender\n", "def convert_gender(value):\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip().lower()\n", " else:\n", " value = value.strip().lower()\n", " \n", " # Binary conversion: female=0, male=1\n", " if 'female' in value or 'f' == value:\n", " return 0\n", " elif 'male' in value or 'm' == value:\n", " return 1\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Check if trait data is available (trait_row is not None)\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\n", "# Since trait_row is None, we skip the clinical feature extraction\n" ] }, { "cell_type": "markdown", "id": "e81b59e8", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "fcba5192", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:41:56.499749Z", "iopub.status.busy": "2025-03-25T06:41:56.499645Z", "iopub.status.idle": "2025-03-25T06:41:57.002029Z", "shell.execute_reply": "2025-03-25T06:41:57.001553Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix file found: ../../input/GEO/Asthma/GSE230164/GSE230164-GPL10558_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": "385d8636", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "bf8612dd", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:41:57.003453Z", "iopub.status.busy": "2025-03-25T06:41:57.003333Z", "iopub.status.idle": "2025-03-25T06:41:57.005462Z", "shell.execute_reply": "2025-03-25T06:41:57.005138Z" } }, "outputs": [], "source": [ "# The gene identifiers start with \"ILMN_\" which indicates they are Illumina probe IDs\n", "# from the Illumina BeadArray platform. These are not human gene symbols but are \n", "# platform-specific probe IDs that need to be mapped to gene symbols.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "1bcf6388", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "379708e7", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:41:57.006677Z", "iopub.status.busy": "2025-03-25T06:41:57.006567Z", "iopub.status.idle": "2025-03-25T06:42:06.393784Z", "shell.execute_reply": "2025-03-25T06:42:06.393386Z" } }, "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": "fef3f8cb", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "21c2ffae", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:42:06.395250Z", "iopub.status.busy": "2025-03-25T06:42:06.395122Z", "iopub.status.idle": "2025-03-25T06:42:06.770591Z", "shell.execute_reply": "2025-03-25T06:42:06.770198Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene mapping dataframe shape: (44837, 2)\n", "First few rows of gene mapping:\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 expression data shape after mapping: (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" ] } ], "source": [ "# 1. Identify the key columns from the gene annotation dictionary\n", "# The gene identifiers in the gene expression data are \"ILMN_\" IDs which map to the \"ID\" column in gene_annotation\n", "# The gene symbols are stored in the \"Symbol\" column in gene_annotation\n", "\n", "# 2. Get gene mapping dataframe by extracting the two identified columns\n", "gene_mapping = get_gene_mapping(gene_annotation, prob_col=\"ID\", gene_col=\"Symbol\")\n", "print(f\"Gene mapping dataframe shape: {gene_mapping.shape}\")\n", "print(\"First few rows of gene mapping:\")\n", "print(gene_mapping.head())\n", "\n", "# 3. Apply gene mapping to convert probe-level measurements to gene expression data\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "print(f\"Gene expression data shape after mapping: {gene_data.shape}\")\n", "print(\"First few gene symbols:\")\n", "print(gene_data.index[:10])\n" ] }, { "cell_type": "markdown", "id": "cc8557d0", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "fdf42161", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:42:06.772025Z", "iopub.status.busy": "2025-03-25T06:42:06.771903Z", "iopub.status.idle": "2025-03-25T06:42:08.167715Z", "shell.execute_reply": "2025-03-25T06:42:08.167323Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape after normalization: (20238, 99)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Asthma/gene_data/GSE230164.csv\n", "No trait information available in this dataset.\n", "Dataset cannot be used for analysis because trait information is missing.\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Gene data shape after normalization: {gene_data.shape}\")\n", "\n", "# Create directory and 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\"Normalized gene data saved to {out_gene_data_file}\")\n", "\n", "# Since trait_row was determined to be None in step 2, we cannot extract clinical features\n", "# This means the dataset lacks the necessary trait information for our analysis\n", "print(\"No trait information available in this dataset.\")\n", "\n", "# Use is_final=False for validation since we lack trait information\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=False # No trait information available\n", ")\n", "\n", "print(\"Dataset cannot be used for analysis because trait information is missing.\")" ] } ], "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 }