{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "de1c6c0c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:56:11.930037Z", "iopub.status.busy": "2025-03-25T07:56:11.929930Z", "iopub.status.idle": "2025-03-25T07:56:12.090786Z", "shell.execute_reply": "2025-03-25T07:56:12.090440Z" } }, "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 = \"Melanoma\"\n", "cohort = \"GSE146264\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Melanoma\"\n", "in_cohort_dir = \"../../input/GEO/Melanoma/GSE146264\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Melanoma/GSE146264.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Melanoma/gene_data/GSE146264.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Melanoma/clinical_data/GSE146264.csv\"\n", "json_path = \"../../output/preprocess/Melanoma/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "8abc9e5e", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "d2bd588d", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:56:12.092308Z", "iopub.status.busy": "2025-03-25T07:56:12.092149Z", "iopub.status.idle": "2025-03-25T07:56:12.281226Z", "shell.execute_reply": "2025-03-25T07:56:12.280711Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Files in the directory:\n", "['GSE146264_family.soft.gz', 'GSE146264_series_matrix.txt.gz']\n", "SOFT file: ../../input/GEO/Melanoma/GSE146264/GSE146264_family.soft.gz\n", "Matrix file: ../../input/GEO/Melanoma/GSE146264/GSE146264_series_matrix.txt.gz\n", "Background Information:\n", "!Series_title\t\"Single cell RNA-seq of psoriatic skin identifies pathogenic Tc17 subsets and reveals distinctions between CD8+ T cells in autoimmunity and cancer\"\n", "!Series_summary\t\"Psoriasis is an inflammatory, IL-17-driven skin disease in which autoantigen-induced CD8+ T cells have been identified as pathogenic drivers. In this study, we used single-cell RNA-seq to identify 11 transcriptionally diverse CD8+ T cell subsets in psoriatic and healthy skin. Among several inflammatory subsets enriched in psoriatic skin, we observed two Tc17 subsets that were metabolically divergent, developmentally related, and expressed CXCL13, which we found to be a biomarker of psoriasis severity. Despite high co-inhibitory receptor expression in the Tc17 clusters, a comparison of these cells with melanoma-infiltrating CD8+ T cells revealed upregulated cytokine, cytolytic, and metabolic transcriptional activity in the psoriatic cells that differed from an exhaustion program. Our findings provide a high-resolution view of cutaneous CD8+ T cells in psoriasis and healthy skin and shed light on their functional distinctions within the chronic pathologies of autoimmunity and cancer.\"\n", "!Series_overall_design\t\"transcriptomic comparison of CD8+ T cells in 11 psoriatic and 5 healthy skin samples\"\n", "Sample Characteristics Dictionary:\n", "{0: ['celltype: CD8+ T cell'], 1: ['subjectid: C13', 'subjectid: C4', 'subjectid: C5', 'subjectid: C6', 'subjectid: C7', 'subjectid: P11', 'subjectid: P13', 'subjectid: P14', 'subjectid: P15', 'subjectid: P16', 'subjectid: P17', 'subjectid: P18', 'subjectid: P19', 'subjectid: P5', 'subjectid: P9', 'subjectid: C14', 'subjectid: P10'], 2: ['plate: rep2', 'plate: rep3', 'plate: rep6', 'plate: rep4', 'plate: rep5', 'plate: rep7', 'plate: rep8', 'plate: rep1'], 3: ['well: A1', 'well: A10', 'well: A11', 'well: A12', 'well: A2', 'well: A3', 'well: A4', 'well: A5', 'well: A6', 'well: A7', 'well: A8', 'well: A9', 'well: B1', 'well: B10', 'well: B11', 'well: B12', 'well: B2', 'well: B3', 'well: B4', 'well: B5', 'well: B6', 'well: B7', 'well: B8', 'well: B9', 'well: C1', 'well: C10', 'well: C11', 'well: C12', 'well: C2', 'well: C3'], 4: ['sequencingbatch: b3', 'sequencingbatch: b7', 'sequencingbatch: b8', 'sequencingbatch: b9', 'sequencingbatch: b10', 'sequencingbatch: b11', 'sequencingbatch: b15', 'sequencingbatch: b6', 'sequencingbatch: b12', 'sequencingbatch: b13', 'sequencingbatch: b16BerkeleyCore', 'sequencingbatch: b18BerkeleyCore', 'sequencingbatch: b17BerkeleyCore', 'sequencingbatch: b19BerkeleyCore', 'sequencingbatch: b2', 'sequencingbatch: b5'], 5: ['passedqc: FALSE', 'passedqc: TRUE'], 6: ['cluster: NA', 'cluster: 2_Resid', 'cluster: 4_Act', 'cluster: 5', 'cluster: 1_Recirc', 'cluster: 7_RP', 'cluster: 6_Tc17', 'cluster: 10_Tc17_22', 'cluster: 0_Ctx', 'cluster: 3', 'cluster: 9_Tc1', 'cluster: 8_Recirc']}\n" ] } ], "source": [ "# 1. Check what files are actually in the directory\n", "import os\n", "print(\"Files in the directory:\")\n", "files = os.listdir(in_cohort_dir)\n", "print(files)\n", "\n", "# 2. Find appropriate files with more flexible pattern matching\n", "soft_file = None\n", "matrix_file = None\n", "\n", "for file in files:\n", " file_path = os.path.join(in_cohort_dir, file)\n", " # Look for files that might contain SOFT or matrix data with various possible extensions\n", " if 'soft' in file.lower() or 'family' in file.lower() or file.endswith('.soft.gz'):\n", " soft_file = file_path\n", " if 'matrix' in file.lower() or file.endswith('.txt.gz') or file.endswith('.tsv.gz'):\n", " matrix_file = file_path\n", "\n", "if not soft_file:\n", " print(\"Warning: Could not find a SOFT file. Using the first .gz file as fallback.\")\n", " gz_files = [f for f in files if f.endswith('.gz')]\n", " if gz_files:\n", " soft_file = os.path.join(in_cohort_dir, gz_files[0])\n", "\n", "if not matrix_file:\n", " print(\"Warning: Could not find a matrix file. Using the second .gz file as fallback if available.\")\n", " gz_files = [f for f in files if f.endswith('.gz')]\n", " if len(gz_files) > 1 and soft_file != os.path.join(in_cohort_dir, gz_files[1]):\n", " matrix_file = os.path.join(in_cohort_dir, gz_files[1])\n", " elif len(gz_files) == 1 and not soft_file:\n", " matrix_file = os.path.join(in_cohort_dir, gz_files[0])\n", "\n", "print(f\"SOFT file: {soft_file}\")\n", "print(f\"Matrix file: {matrix_file}\")\n", "\n", "# 3. Read files if found\n", "if soft_file and matrix_file:\n", " # 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", " \n", " try:\n", " background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)\n", " \n", " # Obtain the sample characteristics dictionary from the clinical dataframe\n", " sample_characteristics_dict = get_unique_values_by_row(clinical_data)\n", " \n", " # 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", " except Exception as e:\n", " print(f\"Error processing files: {e}\")\n", " # Try swapping files if first attempt fails\n", " print(\"Trying to swap SOFT and matrix files...\")\n", " temp = soft_file\n", " soft_file = matrix_file\n", " matrix_file = temp\n", " try:\n", " background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)\n", " sample_characteristics_dict = get_unique_values_by_row(clinical_data)\n", " print(\"Background Information:\")\n", " print(background_info)\n", " print(\"Sample Characteristics Dictionary:\")\n", " print(sample_characteristics_dict)\n", " except Exception as e:\n", " print(f\"Still error after swapping: {e}\")\n", "else:\n", " print(\"Could not find necessary files for processing.\")\n" ] }, { "cell_type": "markdown", "id": "2d8106d8", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "b6150e38", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:56:12.282699Z", "iopub.status.busy": "2025-03-25T07:56:12.282590Z", "iopub.status.idle": "2025-03-25T07:56:12.542452Z", "shell.execute_reply": "2025-03-25T07:56:12.541812Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical Data Preview:\n", "{'GSM4369177': [0.0], 'GSM4369178': [0.0], 'GSM4369179': [0.0], 'GSM4369180': [0.0], 'GSM4369181': [0.0], 'GSM4369182': [0.0], 'GSM4369183': [0.0], 'GSM4369184': [0.0], 'GSM4369185': [0.0], 'GSM4369186': [0.0], 'GSM4369187': [0.0], 'GSM4369188': [0.0], 'GSM4369189': [0.0], 'GSM4369190': [0.0], 'GSM4369191': [0.0], 'GSM4369192': [0.0], 'GSM4369193': [0.0], 'GSM4369194': [0.0], 'GSM4369195': [0.0], 'GSM4369196': [1.0], 'GSM4369197': [1.0], 'GSM4369198': [1.0], 'GSM4369199': [1.0], 'GSM4369200': [1.0], 'GSM4369201': [1.0], 'GSM4369202': [1.0], 'GSM4369203': [1.0], 'GSM4369204': [1.0], 'GSM4369205': [1.0], 'GSM4369206': [1.0], 'GSM4369207': [1.0], 'GSM4369208': [1.0], 'GSM4369209': [1.0], 'GSM4369210': [1.0], 'GSM4369211': [1.0], 'GSM4369212': [1.0], 'GSM4369213': [1.0], 'GSM4369214': [1.0], 'GSM4369215': [1.0], 'GSM4369216': [1.0], 'GSM4369217': [1.0], 'GSM4369218': [1.0], 'GSM4369219': [1.0], 'GSM4369220': [1.0], 'GSM4369221': [1.0], 'GSM4369222': [1.0], 'GSM4369223': [1.0], 'GSM4369224': [1.0], 'GSM4369225': [1.0], 'GSM4369226': [1.0], 'GSM4369227': [1.0], 'GSM4369228': [1.0], 'GSM4369229': [1.0], 'GSM4369230': [1.0], 'GSM4369231': [1.0], 'GSM4369232': [1.0], 'GSM4369233': [1.0], 'GSM4369234': [0.0], 'GSM4369235': [0.0], 'GSM4369236': [0.0], 'GSM4369237': [0.0], 'GSM4369238': [0.0], 'GSM4369239': [0.0], 'GSM4369240': [0.0], 'GSM4369241': [0.0], 'GSM4369242': [0.0], 'GSM4369243': [0.0], 'GSM4369244': [0.0], 'GSM4369245': [0.0], 'GSM4369246': [0.0], 'GSM4369247': [0.0], 'GSM4369248': [0.0], 'GSM4369249': [0.0], 'GSM4369250': [0.0], 'GSM4369251': [0.0], 'GSM4369252': [0.0], 'GSM4369253': [0.0], 'GSM4369254': [0.0], 'GSM4369255': [0.0], 'GSM4369256': [0.0], 'GSM4369257': [0.0], 'GSM4369258': [0.0], 'GSM4369259': [0.0], 'GSM4369260': [0.0], 'GSM4369261': [0.0], 'GSM4369262': [1.0], 'GSM4369263': [1.0], 'GSM4369264': [1.0], 'GSM4369265': [1.0], 'GSM4369266': [1.0], 'GSM4369267': [1.0], 'GSM4369268': [1.0], 'GSM4369269': [1.0], 'GSM4369270': [1.0], 'GSM4369271': [1.0], 'GSM4369272': [1.0], 'GSM4369273': [1.0], 'GSM4369274': [1.0], 'GSM4369275': [1.0], 'GSM4369276': [1.0], 'GSM4369277': [1.0], 'GSM4369278': [1.0], 'GSM4369279': [1.0], 'GSM4369280': [1.0], 'GSM4369281': [1.0], 'GSM4369282': [1.0], 'GSM4369283': [1.0], 'GSM4369284': [1.0], 'GSM4369285': [1.0], 'GSM4369286': [1.0], 'GSM4369287': [1.0], 'GSM4369288': [1.0], 'GSM4369289': [1.0], 'GSM4369290': [1.0], 'GSM4369291': [1.0], 'GSM4369292': [1.0], 'GSM4369293': [1.0], 'GSM4369294': [1.0], 'GSM4369295': [1.0], 'GSM4369296': [1.0], 'GSM4369297': [1.0], 'GSM4369298': [1.0], 'GSM4369299': [1.0], 'GSM4369300': [1.0], 'GSM4369301': [1.0], 'GSM4369302': [1.0], 'GSM4369303': [1.0], 'GSM4369304': [1.0], 'GSM4369305': [1.0], 'GSM4369306': [1.0], 'GSM4369307': [1.0], 'GSM4369308': [1.0], 'GSM4369309': [1.0], 'GSM4369310': [1.0], 'GSM4369311': [0.0], 'GSM4369312': [0.0], 'GSM4369313': [0.0], 'GSM4369314': [0.0], 'GSM4369315': [0.0], 'GSM4369316': [0.0], 'GSM4369317': [0.0], 'GSM4369318': [0.0], 'GSM4369319': [0.0], 'GSM4369320': [0.0], 'GSM4369321': [0.0], 'GSM4369322': [0.0], 'GSM4369323': [0.0], 'GSM4369324': [0.0], 'GSM4369325': [0.0], 'GSM4369326': [0.0], 'GSM4369327': [0.0], 'GSM4369328': [0.0], 'GSM4369329': [0.0], 'GSM4369330': [0.0], 'GSM4369331': [0.0], 'GSM4369332': [0.0], 'GSM4369333': [0.0], 'GSM4369334': [0.0], 'GSM4369335': [0.0], 'GSM4369336': [0.0], 'GSM4369337': [0.0], 'GSM4369338': [0.0], 'GSM4369339': [1.0], 'GSM4369340': [1.0], 'GSM4369341': [1.0], 'GSM4369342': [1.0], 'GSM4369343': [1.0], 'GSM4369344': [1.0], 'GSM4369345': [1.0], 'GSM4369346': [1.0], 'GSM4369347': [1.0], 'GSM4369348': [1.0], 'GSM4369349': [1.0], 'GSM4369350': [1.0], 'GSM4369351': [1.0], 'GSM4369352': [1.0], 'GSM4369353': [1.0], 'GSM4369354': [1.0], 'GSM4369355': [1.0], 'GSM4369356': [1.0], 'GSM4369357': [1.0], 'GSM4369358': [1.0], 'GSM4369359': [1.0], 'GSM4369360': [1.0], 'GSM4369361': [1.0], 'GSM4369362': [1.0], 'GSM4369363': [1.0], 'GSM4369364': [1.0], 'GSM4369365': [1.0], 'GSM4369366': [1.0], 'GSM4369367': [1.0], 'GSM4369368': [1.0], 'GSM4369369': [1.0], 'GSM4369370': [1.0], 'GSM4369371': [1.0], 'GSM4369372': [1.0], 'GSM4369373': [1.0], 'GSM4369374': [1.0], 'GSM4369375': [1.0], 'GSM4369376': [1.0]}\n", "Clinical data saved to: ../../output/preprocess/Melanoma/clinical_data/GSE146264.csv\n" ] } ], "source": [ "import pandas as pd\n", "import os\n", "from typing import Optional, Callable, Dict, Any\n", "import json\n", "import re\n", "\n", "# 1. Gene Expression Data Availability\n", "# Based on the background information, this appears to be single-cell RNA-seq data of CD8+ T cells\n", "# This is gene expression data, so we set is_gene_available to True\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "\n", "# For trait, looking at the sample IDs in row 1, we can see prefixes 'P' (likely psoriasis) and 'C' (likely control)\n", "# This can be used to determine disease status\n", "trait_row = 1 \n", "\n", "# No age information is provided in the sample characteristics dictionary\n", "age_row = None\n", "\n", "# No gender information is provided in the sample characteristics dictionary\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value: str) -> Optional[int]:\n", " \"\"\"\n", " Convert trait value to binary (0 for control, 1 for psoriasis)\n", " \n", " Args:\n", " value: String containing subject ID\n", " \n", " Returns:\n", " 1 if subject has psoriasis (P prefix), 0 if control (C prefix), None otherwise\n", " \"\"\"\n", " if not value or ':' not in value:\n", " return None\n", " \n", " # Extract the value after the colon and remove leading/trailing spaces\n", " subjectid = value.split(':', 1)[1].strip()\n", " \n", " # Check if the subject ID starts with 'P' (psoriasis) or 'C' (control)\n", " if subjectid.startswith('P'):\n", " return 1 # Psoriasis\n", " elif subjectid.startswith('C'):\n", " return 0 # Control\n", " else:\n", " return None\n", "\n", "# No convert_age function needed since age_row is None\n", "\n", "# No convert_gender function needed since gender_row is None\n", "\n", "# 3. Save Metadata\n", "# Determine trait availability\n", "is_trait_available = trait_row is not None\n", "\n", "# Save cohort info 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", "# 4. Clinical Feature Extraction\n", "if trait_row is not None:\n", " # Assume clinical_data is already loaded from a previous step\n", " # Extract clinical features using the library function\n", " selected_clinical_df = geo_select_clinical_features(\n", " clinical_df=clinical_data, # clinical_data from previous step\n", " trait=trait,\n", " trait_row=trait_row,\n", " convert_trait=convert_trait,\n", " age_row=age_row,\n", " convert_age=None, # Not needed since age_row is None\n", " gender_row=gender_row,\n", " convert_gender=None # Not needed since gender_row is None\n", " )\n", " \n", " # Preview the dataframe\n", " preview = preview_df(selected_clinical_df)\n", " print(\"Clinical Data Preview:\")\n", " print(preview)\n", " \n", " # Save the clinical data to CSV\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " selected_clinical_df.to_csv(out_clinical_data_file, index=False)\n", " print(f\"Clinical data saved to: {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "3a1fb6a2", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "d34c8e32", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:56:12.544178Z", "iopub.status.busy": "2025-03-25T07:56:12.544057Z", "iopub.status.idle": "2025-03-25T07:56:13.562053Z", "shell.execute_reply": "2025-03-25T07:56:13.561354Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This appears to be a SuperSeries. Looking at the SOFT file to find potential subseries:\n", "No subseries references found in the first 1000 lines of the SOFT file.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene data extraction result:\n", "Number of rows: 0\n", "First 20 gene/probe identifiers:\n", "Index([], dtype='object', name='ID')\n" ] } ], "source": [ "# 1. First get the path to the soft and matrix files\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. Looking more carefully at the background information\n", "# This is a SuperSeries which doesn't contain direct gene expression data\n", "# Need to investigate the soft file to find the subseries\n", "print(\"This appears to be a SuperSeries. Looking at the SOFT file to find potential subseries:\")\n", "\n", "# Open the SOFT file to try to identify subseries\n", "with gzip.open(soft_file, 'rt') as f:\n", " subseries_lines = []\n", " for i, line in enumerate(f):\n", " if 'Series_relation' in line and 'SuperSeries of' in line:\n", " subseries_lines.append(line.strip())\n", " if i > 1000: # Limit search to first 1000 lines\n", " break\n", "\n", "# Display the subseries found\n", "if subseries_lines:\n", " print(\"Found potential subseries references:\")\n", " for line in subseries_lines:\n", " print(line)\n", "else:\n", " print(\"No subseries references found in the first 1000 lines of the SOFT file.\")\n", "\n", "# Despite trying to extract gene data, we expect it might fail because this is a SuperSeries\n", "try:\n", " gene_data = get_genetic_data(matrix_file)\n", " print(\"\\nGene data extraction result:\")\n", " print(\"Number of rows:\", len(gene_data))\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", " print(\"This confirms the dataset is a SuperSeries without direct gene expression data.\")" ] } ], "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 }