{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "a02231ce", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:30:17.265262Z", "iopub.status.busy": "2025-03-25T08:30:17.265152Z", "iopub.status.idle": "2025-03-25T08:30:17.423688Z", "shell.execute_reply": "2025-03-25T08:30:17.423325Z" } }, "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 = \"COVID-19\"\n", "cohort = \"GSE211378\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/COVID-19\"\n", "in_cohort_dir = \"../../input/GEO/COVID-19/GSE211378\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/COVID-19/GSE211378.csv\"\n", "out_gene_data_file = \"../../output/preprocess/COVID-19/gene_data/GSE211378.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/COVID-19/clinical_data/GSE211378.csv\"\n", "json_path = \"../../output/preprocess/COVID-19/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "d7caed5d", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "c571c381", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:30:17.425142Z", "iopub.status.busy": "2025-03-25T08:30:17.424993Z", "iopub.status.idle": "2025-03-25T08:30:17.462470Z", "shell.execute_reply": "2025-03-25T08:30:17.462154Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Whole Blood profiling of COVID convalescent and Healthy donors with nCounter\"\n", "!Series_summary\t\"This study investigated the cellular immune response in people who had recovered from SARS-CoV2 infection (COVID-19).\"\n", "!Series_overall_design\t\"264 Whole Blood samples from 160 COVID convalescent donors, and 40 from Healthy donors.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['sample.id: host response panel 1 wb', 'sample.id: host response panel 2 wb', 'sample.id: host response panel 3 wb', 'sample.id: host response panel 4 wb', 'sample.id: host response panel plate 25 wb 11292020', 'sample.id: host response panel plate 5 wb 11 19 2020', 'sample.id: host response panel plate 6 wb 11192020', 'sample.id: host response panel plate 7 wb 11 20 2020', 'sample.id: host response panel plate 8 wb 11202020', 'sample.id: host response panel plate 26 wb 11302020', 'sample.id: host response panel plate 9 wb 11212020', 'sample.id: host response panel plate 10 wb 11212020', 'sample.id: host response panel plate 11 wb 11222020', 'sample.id: host response panel plate 12 wb 11222020', 'sample.id: host response panel plate 13 11232020', 'sample.id: host response panel plate 14 wb 11232020', 'sample.id: host response panel plate 15 wb 11242020', 'sample.id: host response panel plate 16 wb 11242020', 'sample.id: host response panel plate 17 wb 11252020', 'sample.id: host response panel plate 18 wb 11252020', 'sample.id: host response panel plate 19 wb 11262020', 'sample.id: host response panel plate 20 wb 11262020', 'sample.id: host response panel plate 21 wb 11272020', 'sample.id: host response panel plate 22 wb 11272020', 'sample.id: host response panel plate 23 11282020', 'sample.id: host response panel plate 24 wb 11292020'], 1: ['date: 20201018', 'date: 20201019', 'date: 20201129', 'date: 20201119', 'date: 20201120', 'date: 20201130', 'date: 20201121', 'date: 20201122', 'date: 20201123', 'date: 20201124', 'date: 20201125', 'date: 20201126', 'date: 20201127', 'date: 20201128'], 2: ['generlf: NS_Hs_HostResponse_v1.0'], 3: ['systemapf: n6_vDV1'], 4: ['lane.number: 1', 'lane.number: 2', 'lane.number: 3', 'lane.number: 4', 'lane.number: 5', 'lane.number: 6', 'lane.number: 7', 'lane.number: 8', 'lane.number: 9', 'lane.number: 10', 'lane.number: 11', 'lane.number: 12'], 5: ['fovcount: 555'], 6: ['fovcounted: 551', 'fovcounted: 549', 'fovcounted: 544', 'fovcounted: 535', 'fovcounted: 546', 'fovcounted: 541', 'fovcounted: 540', 'fovcounted: 538', 'fovcounted: 532', 'fovcounted: 543', 'fovcounted: 536', 'fovcounted: 537', 'fovcounted: 534', 'fovcounted: 542', 'fovcounted: 545', 'fovcounted: 528', 'fovcounted: 547', 'fovcounted: 526', 'fovcounted: 550', 'fovcounted: 554', 'fovcounted: 552', 'fovcounted: 539', 'fovcounted: 530', 'fovcounted: 548', 'fovcounted: 553', 'fovcounted: 555', 'fovcounted: 515', 'fovcounted: 522', 'fovcounted: 521', 'fovcounted: 533'], 7: ['scannerid: 1906C0614'], 8: ['stageposition: 1', 'stageposition: 2'], 9: ['bindingdensity: 0.92', 'bindingdensity: 1.1', 'bindingdensity: 1.52', 'bindingdensity: 1.75', 'bindingdensity: 1.94', 'bindingdensity: 2.49', 'bindingdensity: 1.98', 'bindingdensity: 1.69', 'bindingdensity: 1.44', 'bindingdensity: 2.91', 'bindingdensity: 1.81', 'bindingdensity: 2.18', 'bindingdensity: 1.82', 'bindingdensity: 1.72', 'bindingdensity: 2.09', 'bindingdensity: 1.66', 'bindingdensity: 1.87', 'bindingdensity: 1.51', 'bindingdensity: 2.27', 'bindingdensity: 2.51', 'bindingdensity: 1.88', 'bindingdensity: 2.15', 'bindingdensity: 2.1', 'bindingdensity: 1.54', 'bindingdensity: 1.33', 'bindingdensity: 1.04', 'bindingdensity: 1.45', 'bindingdensity: 1.63', 'bindingdensity: 1.7', 'bindingdensity: 3.1'], 10: ['cartridgeid: host response panel wb 1', 'cartridgeid: host response panel 2 wb', 'cartridgeid: host response panel 3 wb', 'cartridgeid: host response panel 4 wb', 'cartridgeid: host response panel plate 25 wb 11292020', 'cartridgeid: host response panel plate 5 wb 11 19 2020', 'cartridgeid: host response panel plate 6 wb 11 19 2020', 'cartridgeid: host response panel plate 7 wb 11 20 2020', 'cartridgeid: host response panel plate 8 wb 11202020', 'cartridgeid: host response panel plate 26 wb 11302020', 'cartridgeid: host response panel plate 9 wb 11212020', 'cartridgeid: host response panel plate 10 wb 11212020', 'cartridgeid: host response panel plate 11 wb 11222020', 'cartridgeid: host response panel plate 12 wb 11222020', 'cartridgeid: host response panel plate 13 wb 11232020', 'cartridgeid: host response panel plate 14 wb 11232020', 'cartridgeid: host response panel plate 15 wb 11242020', 'cartridgeid: host response panel plate 16 wb 11242020', 'cartridgeid: host response panel plate 17 wb 11252020', 'cartridgeid: host response panel plate 18 wb 11252020', 'cartridgeid: host response panel plate 19 wb 11262020', 'cartridgeid: host response panel plate 20 wb 11262020', 'cartridgeid: host response panel plate 21 wb 11272020', 'cartridgeid: host response panel plate 22 wb 11272020', 'cartridgeid: host response panel plate 23 wb 11282020', 'cartridgeid: host response panel plate 24 wb 11292020'], 11: ['cartridgebarcode: NA'], 12: ['nanostring_id: 12590', 'nanostring_id: 12591_51', 'nanostring_id: 12645_21', 'nanostring_id: 12650', 'nanostring_id: 12672', 'nanostring_id: 12688_41', 'nanostring_id: 12693_21', 'nanostring_id: 12694_21', 'nanostring_id: 12700_21', 'nanostring_id: 12707_31', 'nanostring_id: 12708 _51', 'nanostring_id: 12709_21', 'nanostring_id: 12721', 'nanostring_id: 12726_21', 'nanostring_id: 12727', 'nanostring_id: 12733', 'nanostring_id: 12736', 'nanostring_id: 12745', 'nanostring_id: 12751_41', 'nanostring_id: 12766', 'nanostring_id: 12772_31', 'nanostring_id: 12774', 'nanostring_id: 12781', 'nanostring_id: 12786_21', 'nanostring_id: 12792_21', 'nanostring_id: 12812_31', 'nanostring_id: 12830_51', 'nanostring_id: 12862_21', 'nanostring_id: 12889_31', 'nanostring_id: 12896_52']}\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": "b3bdde7f", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "56c3816e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:30:17.463571Z", "iopub.status.busy": "2025-03-25T08:30:17.463463Z", "iopub.status.idle": "2025-03-25T08:30:17.486520Z", "shell.execute_reply": "2025-03-25T08:30:17.486220Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 1. Gene Expression Data Availability\n", "is_gene_available = True # The background suggests this is gene expression profiling data\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "\n", "# 2.1 Data Availability\n", "# There are no clear clinical data columns in the sample characteristics\n", "# From the background information: \"264 Whole Blood samples from 160 COVID convalescent donors, \n", "# and 40 from Healthy donors.\" implies there is COVID-19 status information\n", "# However, it's not available in the sample characteristics dictionary\n", "trait_row = None # No clear trait information in characteristics\n", "age_row = None # No age information found\n", "gender_row = None # No gender information found\n", "\n", "# 2.2 Data Type Conversion Functions\n", "def convert_trait(value):\n", " # Function to convert COVID-19 status, though not used in this dataset\n", " if value is None:\n", " return None\n", " \n", " value = value.lower().split(': ')[-1].strip()\n", " if 'covid' in value or 'convalescent' in value or 'infected' in value or 'positive' in value:\n", " return 1\n", " elif 'healthy' in value or 'control' in value or 'negative' in value:\n", " return 0\n", " return None\n", "\n", "def convert_age(value):\n", " # Function to convert age to continuous values, though not used in this dataset\n", " if value is None:\n", " return None\n", " \n", " try:\n", " # Extract the value after the colon and convert to float\n", " age_value = value.split(': ')[-1].strip()\n", " return float(age_value)\n", " except (ValueError, AttributeError):\n", " return None\n", "\n", "def convert_gender(value):\n", " # Function to convert gender to binary, though not used in this dataset\n", " if value is None:\n", " return None\n", " \n", " value = value.lower().split(': ')[-1].strip()\n", " if value in ['female', 'f', 'woman']:\n", " return 0\n", " elif value in ['male', 'm', 'man']:\n", " return 1\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Initial validation - check if the dataset has both gene and trait data\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=(trait_row is not None)\n", ")\n", "\n", "# 4. Clinical Feature Extraction\n", "# Since trait_row is None, we skip the clinical feature extraction step\n" ] }, { "cell_type": "markdown", "id": "b7805f8e", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "d21ce447", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:30:17.487587Z", "iopub.status.busy": "2025-03-25T08:30:17.487482Z", "iopub.status.idle": "2025-03-25T08:30:17.538074Z", "shell.execute_reply": "2025-03-25T08:30:17.537762Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SOFT file: ../../input/GEO/COVID-19/GSE211378/GSE211378_family.soft.gz\n", "Matrix file: ../../input/GEO/COVID-19/GSE211378/GSE211378_series_matrix.txt.gz\n", "Found the matrix table marker at line 84\n", "Gene data shape: (773, 304)\n", "First 20 gene/probe identifiers:\n", "['ACE', 'ACKR2', 'ACKR3', 'ACKR4', 'ACOX1', 'ACSL1', 'ACSL3', 'ACSL4', 'ACVR1', 'ADAR', 'ADGRE5', 'ADGRG3', 'ADORA2A', 'AGT', 'AHR', 'AIF1', 'AIM2', 'AKT1', 'AKT2', 'AKT3']\n" ] } ], "source": [ "# 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", "print(f\"SOFT file: {soft_file}\")\n", "print(f\"Matrix file: {matrix_file}\")\n", "\n", "# Set gene availability flag\n", "is_gene_available = True # Initially assume gene data is available\n", "\n", "# First check if the matrix file contains the expected marker\n", "found_marker = False\n", "marker_row = None\n", "try:\n", " with gzip.open(matrix_file, 'rt') as file:\n", " for i, line in enumerate(file):\n", " if \"!series_matrix_table_begin\" in line:\n", " found_marker = True\n", " marker_row = i\n", " print(f\"Found the matrix table marker at line {i}\")\n", " break\n", " \n", " if not found_marker:\n", " print(\"Warning: Could not find '!series_matrix_table_begin' marker in the file.\")\n", " is_gene_available = False\n", " \n", " # If marker was found, try to extract gene data\n", " if is_gene_available:\n", " try:\n", " # Try using the library function\n", " gene_data = get_genetic_data(matrix_file)\n", " \n", " if gene_data.shape[0] == 0:\n", " print(\"Warning: Extracted gene data has 0 rows.\")\n", " is_gene_available = False\n", " else:\n", " print(f\"Gene data shape: {gene_data.shape}\")\n", " # Print the first 20 gene/probe identifiers\n", " print(\"First 20 gene/probe identifiers:\")\n", " print(gene_data.index[:20].tolist())\n", " except Exception as e:\n", " print(f\"Error extracting gene data with get_genetic_data(): {e}\")\n", " is_gene_available = False\n", " \n", " # If gene data extraction failed, examine file content to diagnose\n", " if not is_gene_available:\n", " print(\"Examining file content to diagnose the issue:\")\n", " try:\n", " with gzip.open(matrix_file, 'rt') as file:\n", " # Print lines around the marker if found\n", " if marker_row is not None:\n", " for i, line in enumerate(file):\n", " if i >= marker_row - 2 and i <= marker_row + 10:\n", " print(f\"Line {i}: {line.strip()[:100]}...\")\n", " if i > marker_row + 10:\n", " break\n", " else:\n", " # If marker not found, print first 10 lines\n", " for i, line in enumerate(file):\n", " if i < 10:\n", " print(f\"Line {i}: {line.strip()[:100]}...\")\n", " else:\n", " break\n", " except Exception as e2:\n", " print(f\"Error examining file: {e2}\")\n", " \n", "except Exception as e:\n", " print(f\"Error processing file: {e}\")\n", " is_gene_available = False\n", "\n", "# Update validation information if gene data extraction failed\n", "if not is_gene_available:\n", " print(\"Gene expression data could not be successfully extracted from this dataset.\")\n", " # Update the validation record since gene data isn't available\n", " is_trait_available = False # We already determined trait data isn't available in step 2\n", " validate_and_save_cohort_info(is_final=False, cohort=cohort, info_path=json_path,\n", " is_gene_available=is_gene_available, is_trait_available=is_trait_available)\n" ] }, { "cell_type": "markdown", "id": "e6399076", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "c3f4cf66", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:30:17.539166Z", "iopub.status.busy": "2025-03-25T08:30:17.539060Z", "iopub.status.idle": "2025-03-25T08:30:17.540809Z", "shell.execute_reply": "2025-03-25T08:30:17.540516Z" } }, "outputs": [], "source": [ "# Review the gene identifiers based on biomedical knowledge\n", "# The identifiers appear to be standard human gene symbols (ACE, ACKR2, AKT1, etc.)\n", "# These are official HGNC gene symbols and do not need to be mapped\n", "\n", "requires_gene_mapping = False\n" ] }, { "cell_type": "markdown", "id": "b167cfa2", "metadata": {}, "source": [ "### Step 5: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 6, "id": "8759dfa4", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:30:17.541818Z", "iopub.status.busy": "2025-03-25T08:30:17.541714Z", "iopub.status.idle": "2025-03-25T08:30:17.772820Z", "shell.execute_reply": "2025-03-25T08:30:17.772451Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape after normalization: (758, 304)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/COVID-19/gene_data/GSE211378.csv\n", "Empty clinical features saved to ../../output/preprocess/COVID-19/clinical_data/GSE211378.csv\n", "Abnormality detected in the cohort: GSE211378. Preprocessing failed.\n", "Linked data not created or saved due to missing trait information.\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Gene data shape after normalization: {normalized_gene_data.shape}\")\n", "\n", "# Create output directory if it doesn't exist\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "\n", "# Save the normalized gene data\n", "normalized_gene_data.to_csv(out_gene_data_file)\n", "print(f\"Normalized gene data saved to {out_gene_data_file}\")\n", "\n", "# Create empty clinical data for record-keeping purposes\n", "empty_clinical_df = pd.DataFrame()\n", "\n", "# Create directory for clinical data output\n", "os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", "\n", "# Save the empty clinical features\n", "empty_clinical_df.to_csv(out_clinical_data_file)\n", "print(f\"Empty clinical features saved to {out_clinical_data_file}\")\n", "\n", "# Since we don't have trait data, we can't perform:\n", "# - linking with gene data\n", "# - handling missing values\n", "# - determining trait bias\n", "# - creating usable linked data\n", "\n", "# For validation purposes, mark the dataset as unusable due to lack of trait data\n", "is_trait_available = False # No trait data available as determined in Step 2\n", "is_biased = True # Mark as biased (unusable) since we can't analyze trait-related bias\n", "\n", "# Validate and save cohort info - mark as unusable due to missing trait data\n", "is_usable = validate_and_save_cohort_info(\n", " is_final=True,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=is_gene_available,\n", " is_trait_available=is_trait_available,\n", " is_biased=is_biased,\n", " df=empty_clinical_df, # Use empty DataFrame as placeholder\n", " note=\"Dataset contains gene expression data but lacks necessary trait information for COVID-19 analysis.\"\n", ")\n", "\n", "print(\"Linked data not created or saved due to missing trait information.\")" ] } ], "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 }