{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "1946716d", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:54:05.146977Z", "iopub.status.busy": "2025-03-25T05:54:05.146749Z", "iopub.status.idle": "2025-03-25T05:54:05.311329Z", "shell.execute_reply": "2025-03-25T05:54:05.311022Z" } }, "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 = \"Multiple_sclerosis\"\n", "cohort = \"GSE193442\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Multiple_sclerosis\"\n", "in_cohort_dir = \"../../input/GEO/Multiple_sclerosis/GSE193442\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Multiple_sclerosis/GSE193442.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Multiple_sclerosis/gene_data/GSE193442.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Multiple_sclerosis/clinical_data/GSE193442.csv\"\n", "json_path = \"../../output/preprocess/Multiple_sclerosis/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "a0f96dc6", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "4a303513", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:54:05.312723Z", "iopub.status.busy": "2025-03-25T05:54:05.312592Z", "iopub.status.idle": "2025-03-25T05:54:05.402430Z", "shell.execute_reply": "2025-03-25T05:54:05.402121Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Transcriptional profiling of human KIR+ CD8 T cells\"\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: ['tissue: PBMC'], 1: ['cell type: KIR+ CD8 T']}\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": "d15bf249", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "f12ef706", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:54:05.403486Z", "iopub.status.busy": "2025-03-25T05:54:05.403375Z", "iopub.status.idle": "2025-03-25T05:54:05.409769Z", "shell.execute_reply": "2025-03-25T05:54:05.409484Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "import os\n", "from typing import Optional, Callable\n", "\n", "# Check gene expression data availability\n", "# Based on the Series title and description, this appears to be a transcriptional profiling dataset\n", "# This suggests gene expression data is likely available\n", "is_gene_available = True\n", "\n", "# Analyze clinical feature availability\n", "# From the Sample Characteristics Dictionary, we don't have explicit trait (Multiple_sclerosis), age, or gender information\n", "# The data only shows tissue (PBMC) and cell type (KIR+ CD8 T) information\n", "\n", "# Set availability of trait, age, and gender\n", "trait_row = None # No explicit trait information available\n", "age_row = None # No age information available\n", "gender_row = None # No gender information available\n", "\n", "# Define conversion functions (these won't be used but defined for completeness)\n", "def convert_trait(value):\n", " if value is None:\n", " return None\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " if value.lower() in ['ms', 'multiple sclerosis']:\n", " return 1\n", " elif value.lower() in ['control', 'healthy', 'normal']:\n", " return 0\n", " return None\n", "\n", "def convert_age(value):\n", " if value is None:\n", " return None\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " try:\n", " return float(value)\n", " except:\n", " return None\n", "\n", "def convert_gender(value):\n", " if value is None:\n", " return None\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " value = value.lower()\n", " if value in ['female', 'f']:\n", " return 0\n", " elif value in ['male', 'm']:\n", " return 1\n", " return None\n", "\n", "# Initial validation of dataset usability\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", "# No need to extract clinical features since trait data is not available (trait_row is None)\n" ] }, { "cell_type": "markdown", "id": "af486d18", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "f5efe565", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:54:05.410705Z", "iopub.status.busy": "2025-03-25T05:54:05.410602Z", "iopub.status.idle": "2025-03-25T05:54:06.033605Z", "shell.execute_reply": "2025-03-25T05:54:06.033261Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Checking for SubSeries information in the SuperSeries...\n", "SubSeries found: []\n", "\n", "Attempting direct extraction with debugging:\n", "First 10 lines of the matrix file:\n", "Line 1: !Series_title\t\"Transcriptional profiling of human KIR+ CD8 T cells\"\n", "Line 2: !Series_geo_accession\t\"GSE193442\"\n", "Line 3: !Series_status\t\"Public on Mar 08 2022\"\n", "Line 4: !Series_submission_date\t\"Jan 11 2022\"\n", "Line 5: !Series_last_update_date\t\"Apr 20 2022\"\n", "Line 6: !Series_pubmed_id\t\"35258337\"\n", "Line 7: !Series_summary\t\"This SuperSeries is composed of the SubSeries listed below.\"\n", "Line 8: !Series_overall_design\t\"Refer to individual Series\"\n", "Line 9: !Series_type\t\"Expression profiling by high throughput sequencing\"\n", "Line 10: !Series_type\t\"Other\"\n", "Found table marker at line 69\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene data extraction stats:\n", "Number of rows: 0\n", "Number of columns: 4512\n", "No gene data rows found. This confirms this is a SuperSeries without direct gene expression data.\n", "\n", "Updated gene data availability: False\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The SuperSeries nature of GSE193442 is causing issues with our standard data extraction\n", "# Let's try to check if we can find any SubSeries information\n", "\n", "import gzip\n", "import re\n", "\n", "def extract_subseries_info(soft_file_path):\n", " \"\"\"Extract SubSeries information from a SuperSeries SOFT file\"\"\"\n", " subseries_ids = []\n", " \n", " try:\n", " with gzip.open(soft_file_path, 'rt') as f:\n", " for line in f:\n", " if line.startswith('!Series_relation'):\n", " # Look for SubSeries relation entries\n", " match = re.search(r'SubSeries of:(\\S+)', line)\n", " if match:\n", " subseries_ids.append(match.group(1))\n", " # Also check for \"SuperSeries of\" pattern which lists the component series\n", " match = re.search(r'SuperSeries of:(\\S+)', line)\n", " if match:\n", " subseries_ids.append(match.group(1))\n", " except Exception as e:\n", " print(f\"Error reading SOFT file: {e}\")\n", " \n", " return subseries_ids\n", "\n", "# 1. First get the file paths again to access the matrix file\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. Check if we can find subseries information\n", "print(\"Checking for SubSeries information in the SuperSeries...\")\n", "subseries = extract_subseries_info(soft_file)\n", "print(f\"SubSeries found: {subseries}\")\n", "\n", "# 3. Try direct extraction method with additional debugging\n", "print(\"\\nAttempting direct extraction with debugging:\")\n", "try:\n", " # Modified approach to print more information about the file\n", " with gzip.open(matrix_file, 'rt') as file:\n", " # Print first 10 lines to understand file structure\n", " print(\"First 10 lines of the matrix file:\")\n", " for i, line in enumerate(file):\n", " if i < 10:\n", " print(f\"Line {i+1}: {line.strip()}\")\n", " if i == 0 and \"SuperSeries\" in line:\n", " print(\"This confirms the file is a SuperSeries metadata file\")\n", " if \"!series_matrix_table_begin\" in line:\n", " print(f\"Found table marker at line {i+1}\")\n", " break\n", " else:\n", " print(\"No table marker found in the file\")\n", " \n", " # Try standard extraction again but with error handling\n", " try:\n", " gene_data = get_genetic_data(matrix_file)\n", " print(\"\\nGene data extraction stats:\")\n", " print(f\"Number of rows: {gene_data.shape[0]}\")\n", " print(f\"Number of columns: {gene_data.shape[1]}\")\n", " \n", " if gene_data.shape[0] > 0:\n", " print(\"First 20 gene/probe identifiers:\")\n", " print(gene_data.index[:20])\n", " else:\n", " print(\"No gene data rows found. This confirms this is a SuperSeries without direct gene expression data.\")\n", " except Exception as e:\n", " print(f\"Error during gene data extraction: {e}\")\n", " \n", "except Exception as e:\n", " print(f\"Error examining matrix file: {e}\")\n", "\n", "# 4. Update data availability flag based on our findings\n", "is_gene_available = False # Updating based on our inspection\n", "print(f\"\\nUpdated gene data availability: {is_gene_available}\")\n", "\n", "# 5. Re-validate cohort info with updated gene availability\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", ")" ] } ], "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 }