{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "89072f76", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:05:38.523442Z", "iopub.status.busy": "2025-03-25T06:05:38.523211Z", "iopub.status.idle": "2025-03-25T06:05:38.687564Z", "shell.execute_reply": "2025-03-25T06:05:38.687238Z" } }, "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 = \"Pancreatic_Cancer\"\n", "cohort = \"GSE130563\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Pancreatic_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Pancreatic_Cancer/GSE130563\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Pancreatic_Cancer/GSE130563.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Pancreatic_Cancer/gene_data/GSE130563.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Pancreatic_Cancer/clinical_data/GSE130563.csv\"\n", "json_path = \"../../output/preprocess/Pancreatic_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "8e172032", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "3b2fc7d7", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:05:38.688963Z", "iopub.status.busy": "2025-03-25T06:05:38.688823Z", "iopub.status.idle": "2025-03-25T06:05:38.782365Z", "shell.execute_reply": "2025-03-25T06:05:38.782073Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Skeletal Muscle Fibrosis in Pancreatic Cancer Patients with Respect to Survival\"\n", "!Series_summary\t\"Skeletal muscle wasting is a devastating consequence of cancer that may be responsible for nearly 30% of cancer-related deaths. In addition to muscle atrophy, we have identified significant muscle fiber damage and replacement of muscle with fibrotic tissue in rectus abdominis muscle biopsies from cachectic pancreatic ductal adenocarcinoma (PDAC) patients that associates with poor survival. Transcriptional profiling of muscle harvested from these same patients supported these findings by identifying gene clusters related to wounding, inflammation and cellular response to TGF-B upregulated in cachectic PDAC patients compared with non-cancer controls.\"\n", "!Series_summary\t\"In this dataset, we include the expression data obtained from rectus abdominis muscle biopsies fron non-cancer controls patients undergoing abdominal surgery for benign reasons and from PDAC patients undergoing tumor-resection surgery. PDAC patients were further classified as non-cachectic or cachectic. Cachexia was defined as a body weight loss of >5% during the 6 months prior to surgery. The purpose of this study was to identify the broader transcriptional networks changed in cachectic PDAC patients versus non-cancer controls, that may be associated with the histological changes observed in muscle biopsies harvested from these same patients.\"\n", "!Series_overall_design\t\"For microarray analysis, a total of 46 RNA samples across four groups are included. The groups are as follows: Non-cancer control patients (n = 16); Chronic pancreatitis patients (n = 8); Non-cachectic PDAC patients (n = 5); Cachectic PDAC patients (n = 17). To identify broader gene networks changed in cachectic PDAC patients that may be associated with histological findings of muscle damage and replacement of muscle with fat and fibrotic tissue, we performed differential expression analysis between non-cancer controls and cachectic PDAC patients, and between non-cancer controls and non-cachectic PDAC patients. PDAC patients receiving Stage IV diagnosis were excluded from analyses. Due to the inflammatory nature of chronic pancreatitis, patients diagnosed with chronic pancreatitis were not included in the non-cancer control group and were excluded from analyses.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['diagnosis: mucinous cystadenoma', 'diagnosis: squamoid cyst', 'diagnosis: IPMN', 'diagnosis: bile duct injury', 'diagnosis: choledocholithiasis s/p cholecystectomy', 'diagnosis: common bile duct stricture', 'diagnosis: cholecystitis', 'diagnosis: bile duct injury and acute pancreatic necrosis', 'diagnosis: open cholecystectomy', 'diagnosis: acute pancreatic necrosis', 'diagnosis: sclerosing cholangitis', 'diagnosis: stricture of choledochojejunostomy', 'diagnosis: common bile duct injury', 'diagnosis: choledochal cyst', 'diagnosis: pancreatic ductal adenocarcinoma', 'diagnosis: chronic pancreatitis'], 1: ['Sex: F', 'Sex: M'], 2: ['tnm: n/a', 'tnm: pT3N1M0', 'tnm: pT3N0M0', 'tnm: Stage IV', 'tnm: pT4N1M0', 'tnm: pT1N0M0', 'tnm: pT2N1M0'], 3: ['bw loss in 6 months prior to surgery: 0', 'bw loss in 6 months prior to surgery: 4', 'bw loss in 6 months prior to surgery: 3', 'bw loss in 6 months prior to surgery: 6.5', 'bw loss in 6 months prior to surgery: 11.1', 'bw loss in 6 months prior to surgery: 10', 'bw loss in 6 months prior to surgery: 18', 'bw loss in 6 months prior to surgery: 16', 'bw loss in 6 months prior to surgery: 12.8', 'bw loss in 6 months prior to surgery: 10.6', 'bw loss in 6 months prior to surgery: 17.8', 'bw loss in 6 months prior to surgery: 6.7', 'bw loss in 6 months prior to surgery: 16.6', 'bw loss in 6 months prior to surgery: 32.3', 'bw loss in 6 months prior to surgery: 14.6', 'bw loss in 6 months prior to surgery: 5.9', 'bw loss in 6 months prior to surgery: 9.7', 'bw loss in 6 months prior to surgery: 15.4', 'bw loss in 6 months prior to surgery: 14.7', 'bw loss in 6 months prior to surgery: 19.2', 'bw loss in 6 months prior to surgery: 11.8', 'bw loss in 6 months prior to surgery: 33.3', 'bw loss in 6 months prior to surgery: 29.4', 'bw loss in 6 months prior to surgery: n.d. (not determined)'], 4: ['age: 33', 'age: 68', 'age: 73', 'age: 49', 'age: 78', 'age: 57', 'age: 55', 'age: 50', 'age: 47', 'age: 63', 'age: 51', 'age: 69', 'age: 60', 'age: 66', 'age: 54', 'age: 64', 'age: 76', 'age: 56', 'age: 80', 'age: 79', 'age: 72', 'age: 52', 'age: 74', 'age: 77', 'age: 70', 'age: 59', 'age: 30', 'age: 45', 'age: 58'], 5: ['neoadjuvant therapy (y/n): n/a', 'neoadjuvant therapy (y/n): Y', 'neoadjuvant therapy (y/n): N'], 6: ['survival post-surgery (or days elapsed since surgery, if still alive at time of censor): n.d. (not determined)', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 187', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 178', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 170', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 268', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 220', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 1016', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 318', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 1097', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 73', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 802', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 55', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 637', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 620', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 15', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 505', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 449', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 305', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 851', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 403', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 366', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 132', 'survival post-surgery (or days elapsed since surgery, if still alive at time of censor): 367'], 7: ['tissue: rectus abdominis muscle']}\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": "90f4a224", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "7fc94166", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:05:38.783496Z", "iopub.status.busy": "2025-03-25T06:05:38.783391Z", "iopub.status.idle": "2025-03-25T06:05:38.788580Z", "shell.execute_reply": "2025-03-25T06:05:38.788310Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical data file not found at ../../input/GEO/Pancreatic_Cancer/GSE130563/clinical_data.csv\n", "Skipping clinical feature extraction step.\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the background information, this appears to be microarray data for gene expression analysis\n", "# from muscle biopsies, not just miRNA or methylation data\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "\n", "# For trait (Pancreatic_Cancer):\n", "# From the diagnosis field (key 0), we can identify patients with pancreatic ductal adenocarcinoma\n", "trait_row = 0\n", "\n", "# For age:\n", "# Age information is available in key 4\n", "age_row = 4\n", "\n", "# For gender:\n", "# Gender information is available in key 1\n", "gender_row = 1\n", "\n", "# 2.2 Data Type Conversion\n", "\n", "def convert_trait(value):\n", " \"\"\"Convert diagnosis data to binary trait data (0: control, 1: pancreatic cancer)\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract the value after colon\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Check if the diagnosis is pancreatic ductal adenocarcinoma\n", " if 'pancreatic ductal adenocarcinoma' in value.lower():\n", " return 1\n", " # Consider all other diagnoses as controls except chronic pancreatitis \n", " # (which was excluded from analyses per background info)\n", " elif 'chronic pancreatitis' not in value.lower():\n", " return 0\n", " else:\n", " return None # Chronic pancreatitis patients excluded from analysis\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age data to continuous numeric values\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract the value after colon\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " try:\n", " return float(value)\n", " except ValueError:\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender data to binary (0: female, 1: male)\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract the value after colon\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Convert to binary\n", " if value.upper() == 'F':\n", " return 0\n", " elif value.upper() == 'M':\n", " return 1\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Initial filtering based on availability of 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", "if trait_row is not None:\n", " try:\n", " # Try to load clinical data if it exists\n", " clinical_data = pd.read_csv(f\"{in_cohort_dir}/clinical_data.csv\", index_col=0)\n", " except FileNotFoundError:\n", " # If the file doesn't exist, we'll skip this step\n", " print(f\"Clinical data file not found at {in_cohort_dir}/clinical_data.csv\")\n", " print(\"Skipping clinical feature extraction step.\")\n", " clinical_data = None\n", " \n", " if clinical_data is not None:\n", " # Extract clinical features\n", " clinical_features = 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 extracted features\n", " preview = preview_df(clinical_features)\n", " print(\"Clinical Features Preview:\", preview)\n", " \n", " # Save the clinical features to a CSV file\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " clinical_features.to_csv(out_clinical_data_file)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "372645ad", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "cb14d037", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:05:38.789558Z", "iopub.status.busy": "2025-03-25T06:05:38.789458Z", "iopub.status.idle": "2025-03-25T06:05:38.916674Z", "shell.execute_reply": "2025-03-25T06:05:38.916308Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['100009613_at', '100009676_at', '10000_at', '10001_at', '10002_at',\n", " '100033413_at', '100033422_at', '100033423_at', '100033424_at',\n", " '100033425_at', '100033426_at', '100033427_at', '100033428_at',\n", " '100033430_at', '100033431_at', '100033432_at', '100033434_at',\n", " '100033435_at', '100033436_at', '100033437_at'],\n", " dtype='object', name='ID')\n" ] } ], "source": [ "# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.\n", "gene_data = get_genetic_data(matrix_file)\n", "\n", "# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.\n", "print(gene_data.index[:20])\n" ] }, { "cell_type": "markdown", "id": "58ecb3b3", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "180be348", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:05:38.917876Z", "iopub.status.busy": "2025-03-25T06:05:38.917768Z", "iopub.status.idle": "2025-03-25T06:05:38.919600Z", "shell.execute_reply": "2025-03-25T06:05:38.919335Z" } }, "outputs": [], "source": [ "# Review the gene identifiers in the gene expression data\n", "# These identifiers (like '100009613_at', '10000_at') appear to be Affymetrix probe IDs \n", "# rather than standard human gene symbols. Affymetrix probe IDs typically end with \"_at\".\n", "# Standard human gene symbols would be like \"BRCA1\", \"TP53\", etc.\n", "# Therefore, we need to map these probe IDs to human gene symbols.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "1df4df1d", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "407e2598", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:05:38.920648Z", "iopub.status.busy": "2025-03-25T06:05:38.920550Z", "iopub.status.idle": "2025-03-25T06:05:40.076404Z", "shell.execute_reply": "2025-03-25T06:05:40.075951Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['1_at', '10_at', '100_at', '1000_at', '10000_at'], 'ORF': ['1', '10', '100', '1000', '10000']}\n" ] } ], "source": [ "# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n", "gene_annotation = get_gene_annotation(soft_file)\n", "\n", "# 2. Use the 'preview_df' function from the library to preview the data and print out the results.\n", "print(\"Gene annotation preview:\")\n", "print(preview_df(gene_annotation))\n" ] }, { "cell_type": "markdown", "id": "1e038e9e", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "49904325", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:05:40.077895Z", "iopub.status.busy": "2025-03-25T06:05:40.077776Z", "iopub.status.idle": "2025-03-25T06:05:43.146697Z", "shell.execute_reply": "2025-03-25T06:05:43.146328Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Platform section found: True\n", "Platform table begin marker found: False\n", "Platform table end marker found: False\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene annotation columns:\n", "['ID', 'ORF']\n", "\n", "Gene annotation preview:\n", "{'ID': ['1_at', '10_at', '100_at', '1000_at', '10000_at'], 'ORF': ['1', '10', '100', '1000', '10000']}\n", "\n", "No obvious gene symbol column found. Examining column content...\n", "Column 'ORF' sample values: ['1', '10', '100', '1000', '10000']\n", "Selected 'ORF' as potential gene symbol column\n", "\n", "Gene mapping preview:\n", "{'ID': ['1_at', '10_at', '100_at', '1000_at', '10000_at'], 'Gene': ['1', '10', '100', '1000', '10000']}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene expression data preview (after mapping):\n", "Shape: (0, 46)\n", "Warning: Empty gene expression dataset after mapping!\n" ] } ], "source": [ "# 1. Look at the SOFT file structure to understand available gene annotation information\n", "with gzip.open(soft_file, 'rt') as f:\n", " # Read a sample of the file to understand its structure\n", " sample_lines = []\n", " platform_section_found = False\n", " platform_table_begin = False\n", " platform_table_end = False\n", " \n", " for i, line in enumerate(f):\n", " if i > 1000: # Read up to 1000 lines to understand structure\n", " break\n", " if line.startswith(\"^PLATFORM\"):\n", " platform_section_found = True\n", " if line.startswith(\"!Platform_table_begin\"):\n", " platform_table_begin = True\n", " if line.startswith(\"!Platform_table_end\"):\n", " platform_table_end = True\n", " sample_lines.append(line)\n", "\n", "# Print information about the file structure\n", "print(f\"Platform section found: {platform_section_found}\")\n", "print(f\"Platform table begin marker found: {platform_table_begin}\")\n", "print(f\"Platform table end marker found: {platform_table_end}\")\n", "\n", "# 2. Extract gene annotation data properly using the library function\n", "gene_annotation = get_gene_annotation(soft_file)\n", "\n", "# Look at the columns to identify which ones contain probe IDs and gene symbols\n", "print(\"\\nGene annotation columns:\")\n", "print(gene_annotation.columns.tolist())\n", "\n", "# Print a preview of the annotation data\n", "print(\"\\nGene annotation preview:\")\n", "print(preview_df(gene_annotation))\n", "\n", "# 3. Based on the preview, determine appropriate columns for mapping\n", "# We'll need to select the right columns after seeing the preview\n", "# Let's check if common gene symbol column names exist\n", "possible_gene_cols = [col for col in gene_annotation.columns \n", " if any(term in col.lower() for term in ['gene_symbol', 'gene symbol', 'symbol', 'gene name'])]\n", "\n", "if possible_gene_cols:\n", " gene_symbol_col = possible_gene_cols[0]\n", " print(f\"\\nUsing '{gene_symbol_col}' as gene symbol column\")\n", "else:\n", " # If no obvious gene symbol column is found, use a more general approach\n", " # We'll look for columns that might contain gene symbols based on their content\n", " print(\"\\nNo obvious gene symbol column found. Examining column content...\")\n", " \n", " # Sample a few rows from each column to check content\n", " for col in gene_annotation.columns:\n", " if col != 'ID': # Skip the ID column\n", " sample_values = gene_annotation[col].dropna().head(5).tolist()\n", " if sample_values:\n", " print(f\"Column '{col}' sample values: {sample_values}\")\n", " # Check if values look like gene symbols (usually uppercase letters with numbers)\n", " if any(isinstance(val, str) and re.match(r'^[A-Z0-9]+$', val) for val in sample_values):\n", " gene_symbol_col = col\n", " print(f\"Selected '{gene_symbol_col}' as potential gene symbol column\")\n", " break\n", " else:\n", " # If we still can't find a suitable column, use the first non-ID column\n", " non_id_cols = [col for col in gene_annotation.columns if col != 'ID']\n", " if non_id_cols:\n", " gene_symbol_col = non_id_cols[0]\n", " print(f\"Falling back to '{gene_symbol_col}' for gene mapping\")\n", " else:\n", " raise ValueError(\"Cannot find appropriate column for gene symbols\")\n", "\n", "# 4. Create mapping dataframe using identified columns\n", "mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col=gene_symbol_col)\n", "print(\"\\nGene mapping preview:\")\n", "print(preview_df(mapping_df))\n", "\n", "# 5. Apply the gene mapping to convert probe-level measurements to gene expression data\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "\n", "# Print preview of the gene-level expression data\n", "print(\"\\nGene expression data preview (after mapping):\")\n", "print(f\"Shape: {gene_data.shape}\")\n", "if not gene_data.empty:\n", " print(f\"First 5 gene symbols: {list(gene_data.index[:5])}\")\n", "else:\n", " print(\"Warning: Empty gene expression dataset after mapping!\")\n" ] }, { "cell_type": "markdown", "id": "e697693a", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "ce85ee71", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:05:43.148035Z", "iopub.status.busy": "2025-03-25T06:05:43.147912Z", "iopub.status.idle": "2025-03-25T06:05:43.232358Z", "shell.execute_reply": "2025-03-25T06:05:43.232015Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Re-extracted clinical data\n", "Clinical data shape: (8, 47)\n", "Extracted clinical data with shape: (3, 46)\n", "Clinical data preview:\n", "{'GSM3743555': [0.0, 33.0, 0.0], 'GSM3743556': [0.0, 68.0, 0.0], 'GSM3743557': [0.0, 73.0, 0.0], 'GSM3743558': [0.0, 49.0, 0.0], 'GSM3743559': [0.0, 78.0, 0.0], 'GSM3743560': [0.0, 57.0, 0.0], 'GSM3743561': [0.0, 55.0, 0.0], 'GSM3743562': [0.0, 50.0, 0.0], 'GSM3743563': [0.0, 47.0, 0.0], 'GSM3743564': [0.0, 63.0, 0.0], 'GSM3743565': [0.0, 51.0, 1.0], 'GSM3743566': [0.0, 50.0, 1.0], 'GSM3743567': [0.0, 69.0, 1.0], 'GSM3743568': [0.0, 50.0, 1.0], 'GSM3743569': [0.0, 60.0, 0.0], 'GSM3743570': [0.0, 68.0, 0.0], 'GSM3743571': [1.0, 66.0, 1.0], 'GSM3743572': [1.0, 54.0, 1.0], 'GSM3743573': [1.0, 64.0, 0.0], 'GSM3743574': [1.0, 76.0, 0.0], 'GSM3743575': [1.0, 68.0, 0.0], 'GSM3743576': [1.0, 73.0, 1.0], 'GSM3743577': [1.0, 56.0, 0.0], 'GSM3743578': [1.0, 80.0, 0.0], 'GSM3743579': [1.0, 68.0, 0.0], 'GSM3743580': [1.0, 79.0, 0.0], 'GSM3743581': [1.0, 72.0, 1.0], 'GSM3743582': [1.0, 52.0, 0.0], 'GSM3743583': [1.0, 74.0, 1.0], 'GSM3743584': [1.0, 74.0, 0.0], 'GSM3743585': [1.0, 55.0, 1.0], 'GSM3743586': [1.0, 56.0, 1.0], 'GSM3743587': [1.0, 77.0, 0.0], 'GSM3743588': [1.0, 70.0, 1.0], 'GSM3743589': [1.0, 70.0, 1.0], 'GSM3743590': [1.0, 63.0, 1.0], 'GSM3743591': [1.0, 59.0, 0.0], 'GSM3743592': [1.0, 74.0, 1.0], 'GSM3743593': [nan, 30.0, 1.0], 'GSM3743594': [nan, 51.0, 1.0], 'GSM3743595': [nan, 55.0, 1.0], 'GSM3743596': [nan, 55.0, 0.0], 'GSM3743597': [nan, 45.0, 0.0], 'GSM3743598': [nan, 58.0, 0.0], 'GSM3743599': [nan, 50.0, 1.0], 'GSM3743600': [nan, 54.0, 1.0]}\n", "Clinical data saved to ../../output/preprocess/Pancreatic_Cancer/clinical_data/GSE130563.csv\n", "\n", "Gene expression data overview:\n", "Number of probes: 0\n", "Number of samples: 46\n", "Probe-level data saved to ../../output/preprocess/Pancreatic_Cancer/gene_data/GSE130563.csv\n", "Linked data shape: (46, 3)\n", "Shape after handling missing values: (0, 2)\n", "Quartiles for 'Pancreatic_Cancer':\n", " 25%: nan\n", " 50% (Median): nan\n", " 75%: nan\n", "Min: nan\n", "Max: nan\n", "The distribution of the feature 'Pancreatic_Cancer' in this dataset is fine.\n", "\n", "Quartiles for 'Age':\n", " 25%: nan\n", " 50% (Median): nan\n", " 75%: nan\n", "Min: nan\n", "Max: nan\n", "The distribution of the feature 'Age' in this dataset is fine.\n", "\n", "Abnormality detected in the cohort: GSE130563. Preprocessing failed.\n", "Data quality check failed. Linked data not saved.\n" ] } ], "source": [ "# 1. We need to properly extract clinical features from the clinical_data obtained in Step 1\n", "# First, let's verify that clinical_data exists and contains actual data\n", "if 'clinical_data' not in locals() or clinical_data is None:\n", " # Re-extract the clinical data from the matrix file if needed\n", " background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']\n", " clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']\n", " _, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)\n", " print(\"Re-extracted clinical data\")\n", "\n", "print(f\"Clinical data shape: {clinical_data.shape}\")\n", "\n", "# Extract clinical features using the values defined in Step 2\n", "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", "print(f\"Extracted clinical data with shape: {clinical_df.shape}\")\n", "print(\"Clinical data preview:\")\n", "print(preview_df(clinical_df))\n", "\n", "# Save the clinical data\n", "os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", "clinical_df.to_csv(out_clinical_data_file)\n", "print(f\"Clinical data saved to {out_clinical_data_file}\")\n", "\n", "# Since the gene mapping step resulted in an empty dataset, we'll use the original probe data\n", "# This is likely due to mismatches between the probe IDs and the annotation format\n", "print(\"\\nGene expression data overview:\")\n", "print(f\"Number of probes: {gene_data.shape[0]}\")\n", "print(f\"Number of samples: {gene_data.shape[1]}\")\n", "\n", "# Save the probe-level data as our gene data\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "gene_data.to_csv(out_gene_data_file)\n", "print(f\"Probe-level data saved to {out_gene_data_file}\")\n", "\n", "# 2. Link the clinical and probe-level data\n", "linked_data = geo_link_clinical_genetic_data(clinical_df, gene_data)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "\n", "# 3. Handle missing values in the linked data\n", "linked_data = handle_missing_values(linked_data, trait)\n", "print(f\"Shape after handling missing values: {linked_data.shape}\")\n", "\n", "# 4. Determine whether the trait and demographic features are severely biased\n", "is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "\n", "# 5. Conduct quality check and save the cohort information\n", "is_usable = validate_and_save_cohort_info(\n", " is_final=True, \n", " cohort=cohort, \n", " info_path=json_path, \n", " is_gene_available=True, \n", " is_trait_available=True, \n", " is_biased=is_trait_biased, \n", " df=unbiased_linked_data,\n", " note=\"Dataset contains pancreatic cancer and control patients. Using probe-level data due to gene mapping issues.\"\n", ")\n", "\n", "# 6. If the linked data is usable, save it as a CSV file\n", "if is_usable:\n", " # Create directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " # Save the data\n", " unbiased_linked_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", "else:\n", " print(\"Data quality check failed. Linked data not saved.\")" ] } ], "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 }