{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "b686e511", "metadata": {}, "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 = \"Age-Related_Macular_Degeneration\"\n", "cohort = \"GSE38662\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Age-Related_Macular_Degeneration\"\n", "in_cohort_dir = \"../../input/GEO/Age-Related_Macular_Degeneration/GSE38662\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Age-Related_Macular_Degeneration/GSE38662.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Age-Related_Macular_Degeneration/gene_data/GSE38662.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Age-Related_Macular_Degeneration/clinical_data/GSE38662.csv\"\n", "json_path = \"../../output/preprocess/Age-Related_Macular_Degeneration/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "109f45fe", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": null, "id": "f6e32145", "metadata": {}, "outputs": [], "source": [ "# This step has already been executed successfully. There's no code to debug.\n", "# The output shows we have retrieved:\n", "# - Background information about human embryonic stem cells\n", "# - Sample characteristics showing cell types, cell lines, passages, and gender\n" ] }, { "cell_type": "markdown", "id": "132c7fca", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": null, "id": "f2ab62eb", "metadata": {}, "outputs": [], "source": [ "```python\n", "import pandas as pd\n", "import json\n", "import os\n", "from typing import Optional, Callable, Dict, Any\n", "import glob\n", "\n", "# Let's first check what files are actually available in the cohort directory\n", "print(f\"Looking for files in: {in_cohort_dir}\")\n", "available_files = glob.glob(os.path.join(in_cohort_dir, \"*\"))\n", "print(\"Available files:\", available_files)\n", "\n", "# Check for gene expression data (looking for common file patterns)\n", "gene_file_patterns = [\"*_series_matrix.txt\", \"*gene*.txt\", \"*expression*.txt\", \"*.CEL\", \"*.gpr\"]\n", "gene_files = []\n", "for pattern in gene_file_patterns:\n", " gene_files.extend(glob.glob(os.path.join(in_cohort_dir, pattern)))\n", "\n", "is_gene_available = len(gene_files) > 0\n", "print(f\"Gene expression files found: {gene_files}\")\n", "print(f\"is_gene_available: {is_gene_available}\")\n", "\n", "# Look for clinical data files (could be in various formats)\n", "clinical_file_patterns = [\"*clinical*.txt\", \"*pheno*.txt\", \"*sample*.txt\", \"*_series_matrix.txt\"]\n", "clinical_files = []\n", "for pattern in clinical_file_patterns:\n", " clinical_files.extend(glob.glob(os.path.join(in_cohort_dir, pattern)))\n", "\n", "# Initialize variables\n", "trait_row = None\n", "age_row = None\n", "gender_row = None\n", "clinical_data = None\n", "\n", "# Check if any clinical files were found\n", "if clinical_files:\n", " print(f\"Potential clinical data files: {clinical_files}\")\n", " \n", " # Try to read the first available clinical file\n", " # Start with series matrix file if available as it often contains sample characteristics\n", " series_matrix_files = glob.glob(os.path.join(in_cohort_dir, \"*_series_matrix.txt\"))\n", " \n", " if series_matrix_files:\n", " try:\n", " # For series matrix files, we need to extract the sample characteristics\n", " with open(series_matrix_files[0], 'r') as f:\n", " lines = f.readlines()\n", " \n", " # Extract sample characteristic lines\n", " char_lines = [line for line in lines if line.startswith(\"!Sample_characteristics_ch\")]\n", " \n", " if char_lines:\n", " # Convert to dataframe\n", " data = []\n", " for line in char_lines:\n", " parts = line.strip().split('\\t')\n", " if len(parts) > 1:\n", " data.append(parts[1:]) # Skip the first part which is the header\n", " \n", " if data:\n", " clinical_data = pd.DataFrame(data)\n", " print(\"Clinical data shape from series matrix:\", clinical_data.shape)\n", " print(clinical_data.head())\n", " \n", " # Print unique values for each row to identify trait, age, and gender\n", " for i in range(len(clinical_data.index)):\n", " unique_values = clinical_data.iloc[i].unique()\n", " print(f\"Row {i} unique values: {unique_values}\")\n", " \n", " # Look for trait-related terms in the unique values\n", " values_str = ' '.join(str(v).lower() for v in unique_values)\n", " if any(term in values_str for term in ['amd', 'macular degeneration', 'disease', 'diagnosis', 'status']):\n", " trait_row = i\n", " print(f\"Potential trait row found at index {i}\")\n", " \n", " # Look for age-related terms\n", " if any(term in values_str for term in ['age', 'years']):\n", " age_row = i\n", " print(f\"Potential age row found at index {i}\")\n", " \n", " # Look for gender-related terms\n", " if any(term in values_str for term in ['gender', 'sex', 'male', 'female']):\n", " gender_row = i\n", " print(f\"Potential gender row found at index {i}\")\n", " except Exception as e:\n", " print(f\"Error reading series matrix file: {e}\")\n", "\n", "# Check the background information file if it exists\n", "background_path = os.path.join(in_cohort_dir, \"background.txt\")\n", "if os.path.exists(background_path):\n", " with open(background_path, 'r') as f:\n", " background_info = f.read()\n", " print(\"\\nBackground Information:\")\n", " print(background_info)\n", " \n", " # Look for clues in background info about trait, age, and gender\n", " bg_lower = background_info.lower()\n", " \n", " # If we haven't found trait info yet, check background\n", " if trait_row is None and ('amd' in bg_lower or 'macular degeneration' in bg_lower):\n", " print(\"Background information suggests this dataset is related to AMD.\")\n", " \n", " # Check for mentions of age uniformity or restrictions\n", " if 'all patients are' in bg_lower and 'age' in bg_lower:\n", " print(\"Background suggests age might be uniform across samples.\")\n", " \n", " # Check for mentions of gender uniformity or restrictions\n", " if 'all patients are' in bg_lower and ('male' in bg_lower or 'female' in bg_lower):\n", " print(\"Background suggests gender might be uniform across samples.\")\n", "\n", "# Data type conversion functions\n", "def convert_trait(value):\n", " \"\"\"Convert trait value to binary (0: control, 1: case)\"\"\"\n", " if pd.isna(value) or value is None:\n", " return None\n", " \n", " # Extract value after colon if it exists\n", " if ':' in str(value):\n", " value = value.split(':', 1)[1].strip()\n", " \n", " value = str(value).lower()\n", " if 'control' in value or 'normal' in value or 'healthy' in value:\n", " return 0\n", " elif 'amd' in value or 'case' in value or 'patient' in value or 'disease' in value or 'macular degeneration' in value:\n", " return 1\n", " else:\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age to continuous value\"\"\"\n", " if pd.isna(value) or value is None:\n", " return None\n", " \n", " # Extract value after colon if it exists\n", " if ':' in str(value):\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Try to convert to float\n", " try:\n", " # Extract numbers if mixed with text\n", " import re\n", " numbers = re.findall(r'\\d+', str(value))\n", " if numbers:\n", " return float(numbers[0])\n", " return float(value)\n", " except (ValueError, TypeError):\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender to binary (0: female, 1: male)\"\"\"\n", " if pd.isna(value) or value is None:\n", " return None\n", " \n", " # Extract value after colon if it exists\n", " if ':' in str(value):\n", " value = value.split(':', 1)[1].strip()\n", " \n", " value = str(value).lower()\n", " if 'female' in value or 'f' == value:\n", " return 0\n", " elif 'male' in value or 'm' == value:\n", " return 1\n", " else:\n", " return None\n", "\n", "# Determine if trait data is available\n", "is_trait_available = trait_row is not None\n", "\n", "# Save metadata for initial filtering\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", "# Extract clinical features if trait data is available\n", "if is_trait_available and clinical_data is not None:\n", " # Use the geo_select_clinical_features function to 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 selected clinical features\n", " print(\"\\nSelected Clinical Features Preview:\")\n", " preview = preview_df(selected_clinical_df)\n", " print(preview)\n", " \n", " #\n" ] }, { "cell_type": "markdown", "id": "d74492f5", "metadata": {}, "source": [ "### Step 3: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": null, "id": "6fc4d5f3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "c3ec6f90", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": null, "id": "a7bec5dc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "d8715b9f", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": null, "id": "5b68e34c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "e6e5380a", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": null, "id": "2f23df00", "metadata": {}, "outputs": [], "source": [] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 5 }