{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "c4b53075", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:18:00.534321Z", "iopub.status.busy": "2025-03-25T08:18:00.534145Z", "iopub.status.idle": "2025-03-25T08:18:00.700155Z", "shell.execute_reply": "2025-03-25T08:18:00.699798Z" } }, "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 = \"Chronic_kidney_disease\"\n", "cohort = \"GSE127136\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Chronic_kidney_disease\"\n", "in_cohort_dir = \"../../input/GEO/Chronic_kidney_disease/GSE127136\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Chronic_kidney_disease/GSE127136.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Chronic_kidney_disease/gene_data/GSE127136.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Chronic_kidney_disease/clinical_data/GSE127136.csv\"\n", "json_path = \"../../output/preprocess/Chronic_kidney_disease/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "28dee1d6", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "db0d0956", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:18:00.701603Z", "iopub.status.busy": "2025-03-25T08:18:00.701462Z", "iopub.status.idle": "2025-03-25T08:18:00.830205Z", "shell.execute_reply": "2025-03-25T08:18:00.829816Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Single-cell RNA-seq profiling reveals novel insights in immune-complex deposition and epithelium transition in IgA nephropathy\"\n", "!Series_summary\t\"IgA nephropathy represents the most prevalent chronic nephrosis worldwide. However, pathogenesis about IgA deposition and end-stage renal failure is still not well defined. Using single-cell RNA-seq, we identified the mesangial membrane receptor for IgA, which collaborates with increased extracellular matrix proteins and protease inhibitor to facilitate IgA deposition. Meanwhile, cell-cell interaction analysis revealed increased communications between mesangium and other cell types, uncovering how morbidity inside glomerulus spreads to whole kidney, which results in the genetic changes of kidney resident immune cells. Prominent interaction decreasing in intercalated cells leads to the discovery of a transitional cell type, which exhibited significant EMT and fibrosis features. Our work comprehensively characterized the pathological mesangial signatures, highlighting the step-by-step pathogenic process of IgA nephropathy from mesangium to epithelium.\"\n", "!Series_overall_design\t\"In this study, we collected single cells from 13 IgAN patients’ renal biopsies and normal renal cells from 6 kidney cancer patients’ paracancerous tissues. As glomerulus are difficult to digest, we separately dissociated the glomerulus and the rest renal tissues. We applied CD326+ and CD14+ MACS to capture epithelium and macrophages, to cover the entire renal cell types, negative selected cells from MACS were also collected. Meanwhile, we isolated monocytes from 5 of the 13 IgAN patients and another 5 normal persons’ peripheral blood using CD14+ MACS\"\n", "!Series_overall_design\t\"\"\n", "!Series_overall_design\t\"**Submitter declares that the raw data have been deposited in the Genome Sequence Archive for Human (https://bigd.big.ac.cn/gsa-human/) under submission number PRJCA003506.**\"\n", "Sample Characteristics Dictionary:\n", "{0: ['patients: IgAN_01', 'patients: IgAN_06', 'patients: IgAN_07', 'patients: IgAN_09', 'patients: IgAN_10', 'patients: IgAN_11', 'patients: IgAN_12', 'patients: IgAN_15', 'patients: IgAN_16', 'patients: IgAN_17', 'patients: IgAN_18', 'patients: IgAN_19', 'patients: IgAN_20', 'patients: NM_01', 'patients: NM_02', 'patients: NM_03', 'patients: NM_07', 'patients: NM_08', 'patients: NM_09', 'patients: PBM_IgAN_10', 'patients: PBM_IgAN_12', 'patients: PBM_IgAN_17', 'patients: PBM_IgAN_19', 'patients: PBM_IgAN_20', 'patients: PBM_NM_01', 'patients: PBM_NM_02', 'patients: PBM_NM_03', 'patients: PBM_NM_04', 'patients: PBM_NM_05'], 1: ['disease state: IgAN', 'disease state: kidney cancer', 'disease state: normal'], 2: ['tissue: renal biopsies', 'tissue: paracancerous tissues', 'cell type: monocytes']}\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": "30b7a907", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "995d55db", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:18:00.831590Z", "iopub.status.busy": "2025-03-25T08:18:00.831464Z", "iopub.status.idle": "2025-03-25T08:18:00.974269Z", "shell.execute_reply": "2025-03-25T08:18:00.973889Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of selected clinical features:\n", "{'GSM3625775': [1.0], 'GSM3625776': [1.0], 'GSM3625777': [1.0], 'GSM3625778': [1.0], 'GSM3625779': [1.0], 'GSM3625780': [1.0], 'GSM3625781': [1.0], 'GSM3625782': [1.0], 'GSM3625783': [1.0], 'GSM3625784': [1.0], 'GSM3625785': [1.0], 'GSM3625786': [1.0], 'GSM3625787': [1.0], 'GSM3625788': [1.0], 'GSM3625789': [1.0], 'GSM3625790': [1.0], 'GSM3625791': [1.0], 'GSM3625792': [1.0], 'GSM3625793': [1.0], 'GSM3625794': [1.0], 'GSM3625795': [1.0], 'GSM3625796': [1.0], 'GSM3625797': [1.0], 'GSM3625798': [1.0], 'GSM3625799': [1.0], 'GSM3625800': [1.0], 'GSM3625801': [1.0], 'GSM3625802': [1.0], 'GSM3625803': [1.0], 'GSM3625804': [1.0], 'GSM3625805': [1.0], 'GSM3625806': [1.0], 'GSM3625807': [1.0], 'GSM3625808': [1.0], 'GSM3625809': [1.0], 'GSM3625810': [1.0], 'GSM3625811': [1.0], 'GSM3625812': [1.0], 'GSM3625813': [1.0], 'GSM3625814': [1.0], 'GSM3625815': [1.0], 'GSM3625816': [1.0], 'GSM3625817': [1.0], 'GSM3625818': [1.0], 'GSM3625819': [1.0], 'GSM3625820': [1.0], 'GSM3625821': [1.0], 'GSM3625822': [1.0], 'GSM3625823': [1.0], 'GSM3625824': [1.0], 'GSM3625825': [1.0], 'GSM3625826': [1.0], 'GSM3625827': [1.0], 'GSM3625828': [1.0], 'GSM3625829': [1.0], 'GSM3625830': [1.0], 'GSM3625831': [1.0], 'GSM3625832': [1.0], 'GSM3625833': [1.0], 'GSM3625834': [1.0], 'GSM3625835': [1.0], 'GSM3625836': [1.0], 'GSM3625837': [1.0], 'GSM3625838': [1.0], 'GSM3625839': [1.0], 'GSM3625840': [1.0], 'GSM3625841': [1.0], 'GSM3625842': [1.0], 'GSM3625843': [1.0], 'GSM3625844': [1.0], 'GSM3625845': [1.0], 'GSM3625846': [1.0], 'GSM3625847': [1.0], 'GSM3625848': [1.0], 'GSM3625849': [1.0], 'GSM3625850': [1.0], 'GSM3625851': [1.0], 'GSM3625852': [1.0], 'GSM3625853': [1.0], 'GSM3625854': [1.0], 'GSM3625855': [1.0], 'GSM3625856': [1.0], 'GSM3625857': [1.0], 'GSM3625858': [1.0], 'GSM3625859': [1.0], 'GSM3625860': [1.0], 'GSM3625861': [1.0], 'GSM3625862': [1.0], 'GSM3625863': [1.0], 'GSM3625864': [1.0], 'GSM3625865': [1.0], 'GSM3625866': [1.0], 'GSM3625867': [1.0], 'GSM3625868': [1.0], 'GSM3625869': [1.0], 'GSM3625870': [1.0], 'GSM3625871': [1.0], 'GSM3625872': [1.0], 'GSM3625873': [1.0], 'GSM3625874': [1.0], 'GSM3625875': [1.0], 'GSM3625876': [1.0], 'GSM3625877': [1.0], 'GSM3625878': [1.0], 'GSM3625879': [1.0], 'GSM3625880': [1.0], 'GSM3625881': [1.0], 'GSM3625882': [1.0], 'GSM3625883': [1.0], 'GSM3625884': [1.0], 'GSM3625885': [1.0], 'GSM3625886': [1.0], 'GSM3625887': [1.0], 'GSM3625888': [1.0], 'GSM3625889': [1.0], 'GSM3625890': [1.0], 'GSM3625891': [1.0], 'GSM3625892': [1.0], 'GSM3625893': [1.0], 'GSM3625894': [1.0], 'GSM3625895': [1.0], 'GSM3625896': [1.0], 'GSM3625897': [1.0], 'GSM3625898': [1.0], 'GSM3625899': [1.0], 'GSM3625900': [1.0], 'GSM3625901': [1.0], 'GSM3625902': [1.0], 'GSM3625903': [1.0], 'GSM3625904': [1.0], 'GSM3625905': [1.0], 'GSM3625906': [1.0], 'GSM3625907': [1.0], 'GSM3625908': [1.0], 'GSM3625909': [1.0], 'GSM3625910': [1.0], 'GSM3625911': [1.0], 'GSM3625912': [1.0], 'GSM3625913': [1.0], 'GSM3625914': [1.0], 'GSM3625915': [1.0], 'GSM3625916': [1.0], 'GSM3625917': [1.0], 'GSM3625918': [1.0], 'GSM3625919': [1.0], 'GSM3625920': [1.0], 'GSM3625921': [1.0], 'GSM3625922': [1.0], 'GSM3625923': [1.0], 'GSM3625924': [1.0], 'GSM3625925': [1.0], 'GSM3625926': [1.0], 'GSM3625927': [1.0], 'GSM3625928': [1.0], 'GSM3625929': [1.0], 'GSM3625930': [1.0], 'GSM3625931': [1.0], 'GSM3625932': [1.0], 'GSM3625933': [1.0], 'GSM3625934': [1.0], 'GSM3625935': [1.0], 'GSM3625936': [1.0], 'GSM3625937': [1.0], 'GSM3625938': [1.0], 'GSM3625939': [1.0], 'GSM3625940': [1.0], 'GSM3625941': [1.0], 'GSM3625942': [1.0], 'GSM3625943': [1.0], 'GSM3625944': [1.0], 'GSM3625945': [1.0], 'GSM3625946': [1.0], 'GSM3625947': [1.0], 'GSM3625948': [1.0], 'GSM3625949': [1.0], 'GSM3625950': [1.0], 'GSM3625951': [1.0], 'GSM3625952': [1.0], 'GSM3625953': [1.0], 'GSM3625954': [1.0], 'GSM3625955': [1.0], 'GSM3625956': [1.0], 'GSM3625957': [1.0], 'GSM3625958': [1.0], 'GSM3625959': [1.0], 'GSM3625960': [1.0], 'GSM3625961': [1.0], 'GSM3625962': [1.0], 'GSM3625963': [1.0], 'GSM3625964': [1.0], 'GSM3625965': [1.0], 'GSM3625966': [1.0], 'GSM3625967': [1.0], 'GSM3625968': [1.0], 'GSM3625969': [1.0], 'GSM3625970': [1.0], 'GSM3625971': [1.0], 'GSM3625972': [1.0], 'GSM3625973': [1.0], 'GSM3625974': [1.0]}\n", "Unique trait values in the dataset: [1. 0.]\n", "Clinical data saved to ../../output/preprocess/Chronic_kidney_disease/clinical_data/GSE127136.csv\n" ] } ], "source": [ "import pandas as pd\n", "import os\n", "import json\n", "from typing import Optional, Callable, Dict, Any\n", "\n", "# 1. Determine Gene Expression Data Availability\n", "# Based on the background information, this dataset contains single-cell RNA-seq profiling data\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "\n", "# Trait: IgA nephropathy status can be inferred from row 1 'disease state'\n", "trait_row = 1 \n", "\n", "# Age: There is no information about age in the sample characteristics\n", "age_row = None\n", "\n", "# Gender: There is no information about gender in the sample characteristics\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value: str) -> Optional[int]:\n", " \"\"\"Convert trait value to binary (0 for control, 1 for IgA nephropathy)\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # The values in the sample characteristics dictionary are already in the format\n", " # \"disease state: IgAN\", so we need to extract the actual value\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip().lower()\n", " else:\n", " value = value.lower()\n", " \n", " # Based on the data, \"igan\" indicates having the disease\n", " if value == \"igan\":\n", " return 1\n", " # \"normal\" or \"kidney cancer\" indicates not having IgA nephropathy\n", " elif value in [\"normal\", \"kidney cancer\"]:\n", " return 0\n", " else:\n", " return None\n", "\n", "def convert_age(value: str) -> Optional[float]:\n", " \"\"\"Convert age value to float (not used in this dataset)\"\"\"\n", " return None\n", "\n", "def convert_gender(value: str) -> Optional[int]:\n", " \"\"\"Convert gender value to binary (not used in this dataset)\"\"\"\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Check if trait data is available\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", "# 4. Clinical Feature Extraction\n", "if trait_row is not None:\n", " # 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 resulting dataframe\n", " preview = preview_df(selected_clinical_df)\n", " print(\"Preview of selected clinical features:\")\n", " print(preview)\n", " \n", " # Check if we have both control and case values\n", " unique_values = selected_clinical_df.iloc[0].unique()\n", " print(f\"Unique trait values in the dataset: {unique_values}\")\n", " \n", " # Ensure output directory exists\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " \n", " # Save to CSV\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": "96a552ad", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "9fde275b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:18:00.975645Z", "iopub.status.busy": "2025-03-25T08:18:00.975529Z", "iopub.status.idle": "2025-03-25T08:18:01.540352Z", "shell.execute_reply": "2025-03-25T08:18:01.539970Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SOFT file: ../../input/GEO/Chronic_kidney_disease/GSE127136/GSE127136_family.soft.gz\n", "Matrix file: ../../input/GEO/Chronic_kidney_disease/GSE127136/GSE127136_series_matrix.txt.gz\n", "Found the matrix table marker in the file.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Warning: Extracted gene data has 0 rows.\n", "Gene expression data could not be successfully extracted from this dataset.\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", "try:\n", " with gzip.open(matrix_file, 'rt') as file:\n", " for line in file:\n", " if \"!series_matrix_table_begin\" in line:\n", " found_marker = True\n", " break\n", " \n", " if found_marker:\n", " print(\"Found the matrix table marker in the file.\")\n", " else:\n", " print(\"Warning: Could not find '!series_matrix_table_begin' marker in the file.\")\n", " \n", " # Try to extract gene data from the matrix file\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", " \n", "except Exception as e:\n", " print(f\"Error extracting gene data: {e}\")\n", " is_gene_available = False\n", " \n", " # Try to diagnose the file format\n", " print(\"Examining file content to diagnose the issue:\")\n", " try:\n", " with gzip.open(matrix_file, 'rt') as file:\n", " for i, line in enumerate(file):\n", " if i < 10: # Print first 10 lines to diagnose\n", " print(f\"Line {i}: {line.strip()[:100]}...\") # Print first 100 chars of each line\n", " else:\n", " break\n", " except Exception as e2:\n", " print(f\"Error examining file: {e2}\")\n", "\n", "if not is_gene_available:\n", " print(\"Gene expression data could not be successfully extracted from this dataset.\")" ] } ], "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 }