{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "a0bb7318", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:06:11.727253Z", "iopub.status.busy": "2025-03-25T06:06:11.727039Z", "iopub.status.idle": "2025-03-25T06:06:11.891760Z", "shell.execute_reply": "2025-03-25T06:06:11.891432Z" } }, "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 = \"GSE183795\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Pancreatic_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Pancreatic_Cancer/GSE183795\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Pancreatic_Cancer/GSE183795.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Pancreatic_Cancer/gene_data/GSE183795.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Pancreatic_Cancer/clinical_data/GSE183795.csv\"\n", "json_path = \"../../output/preprocess/Pancreatic_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "d4ebc16c", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "3ea34eac", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:06:11.893145Z", "iopub.status.busy": "2025-03-25T06:06:11.893002Z", "iopub.status.idle": "2025-03-25T06:06:12.081312Z", "shell.execute_reply": "2025-03-25T06:06:12.080966Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Microarray gene-expression profiles of 139 pancreatic tumor,102 adjacent non-tumor tissue from patients with pancreatic ductal adenocarcinoma and 3 normal pancreas from donors.\"\n", "!Series_summary\t\"In order to identify key pathways associated with disease aggressiveness and therapeutics resistance in the most agrressive subset of PDAC, we analyzed gene expression profiling of tumor and adjacent non-tumor tissues from PDAC cases. Non-negative matrix factorization (NMF) clustering, using gene expression profile form PDAC tumors, revealed three patient subsets. A 142-gene signature specific to the subset with the worst patient survival, predicted prognosis and stratified patients with significantly different survival. Mechanistic and functional analyses of most aggressive subset revealed a HNF1B/Clusterin Axis negatively regulate pancreatic cancer progression and potentially be useful in designing novel strategies to attenuate disease progression. Affymetrix data from from these dataset were partially earlier submited by us as GEO accession#: GSE28735 and GSE 62452. The batch effect between the different sets of data was removed using Partek Genomic Suite and this normalized data was submitted to GEO in this submission.\"\n", "!Series_overall_design\t\"We selected probes with s.d. >0.6 as the intrinsically variable genes and performed non-negative matrix factorization (NMF) analysis with consensus clustering to identify subsets of the pancreatic adenocarcinoma with cophenetic coefficient > 0.94. This analysis discovered three molecular subsets. Integrative gene expression analysis of these subsets identified a 148 specific gene signature were further submitted to Ingenuity Pathway Analysis (IPA).\"\n", "Sample Characteristics Dictionary:\n", "{0: ['tissue: Tumor', 'tissue: adjacent non-tumor', 'tissue: Normal pancreas'], 1: ['grading: G2', 'grading: G3', 'grading: G?', 'survival status: ?', 'grading: G4', 'grading: G1', 'grading: 3', 'grading: 2', 'grading: 3-Feb', 'grading: ?', 'grading: Gx'], 2: ['Stage: IIA', 'Stage: IA', 'Stage: IIB', 'Stage: III', 'Stage: IB', 'Stage: ?', nan, 'Stage: IVA', 'Stage: IVB', 'Stage: IV', 'Stage: >IIB'], 3: ['resection margin: R1', 'resection margin: R0', 'resection margin: R2', 'resection margin: ?', nan, 'resection margin: 1'], 4: ['survival months: 51.1', 'survival months: 56.02185641', 'survival months: 13.90683407', 'survival months: ?', 'survival months: 3.945201156', 'survival months: 35.9', 'survival months: 4.668488035', 'survival months: 2.4', 'survival months: 8.35067578', 'survival months: 2.465750722', 'survival months: NA', 'survival months: 24.92052064', nan, 'survival months: 5.161638179', 'survival months: 19.5', 'survival months: 19.72600578', 'survival months: 12.6', 'survival months: 16', 'survival months: 8.876702601', 'survival months: 40.9', 'survival months: 30.77256902', 'survival months: 2.8', 'survival months: 34.29037338', 'survival months: 12.6246437', 'survival months: 11.6', 'survival months: 6.9', 'survival months: 15.05751775', 'survival months: 10.6849198', 'survival months: 35.34242702', 'survival months: 24.7'], 5: ['survival status: 1', 'survival status: 0', 'survival status: ?', nan]}\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": "60fe6f68", "metadata": {}, "source": [ "### Step 2: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 3, "id": "8a9e58cb", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:06:12.082623Z", "iopub.status.busy": "2025-03-25T06:06:12.082519Z", "iopub.status.idle": "2025-03-25T06:06:47.592589Z", "shell.execute_reply": "2025-03-25T06:06:47.592014Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data shape: (19245, 244)\n", "Example gene identifiers: ['7896748', '7896754', '7896756', '7896761', '7896798']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation shape: (4729321, 12)\n", "Gene annotation columns: ['ID', 'GB_LIST', 'SPOT_ID', 'seqname', 'RANGE_GB', 'RANGE_STRAND', 'RANGE_START', 'RANGE_STOP', 'total_probes', 'gene_assignment', 'mrna_assignment', 'category']\n", "Using gene symbol column: gene_assignment\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Mapped gene data shape: (100969, 244)\n", "Normalized gene data shape: (20022, 244)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Pancreatic_Cancer/gene_data/GSE183795.csv\n", "Clinical features saved to ../../output/preprocess/Pancreatic_Cancer/clinical_data/GSE183795.csv\n", "Linked data shape before handling missing values: (244, 20023)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data shape after handling missing values: (244, 20023)\n", "For the feature 'Pancreatic_Cancer', the least common label is '0.0' with 105 occurrences. This represents 43.03% of the dataset.\n", "The distribution of the feature 'Pancreatic_Cancer' in this dataset is fine.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Pancreatic_Cancer/GSE183795.csv\n" ] } ], "source": [ "# First, we need to extract gene expression data from the matrix file\n", "gene_data = get_genetic_data(matrix_file)\n", "print(f\"Gene expression data shape: {gene_data.shape}\")\n", "\n", "# Check what kind of identifiers are in the gene expression data\n", "print(f\"Example gene identifiers: {list(gene_data.index[:5])}\")\n", "\n", "# Extract gene annotation from the SOFT file to map probe IDs to gene symbols\n", "gene_annotation = get_gene_annotation(soft_file)\n", "print(f\"Gene annotation shape: {gene_annotation.shape}\")\n", "\n", "# Before mapping, let's see what columns are available in the annotation data\n", "print(f\"Gene annotation columns: {gene_annotation.columns.tolist()}\")\n", "\n", "# Get the mapping between probe IDs and gene symbols\n", "# Assuming 'ID' is the probe column and 'GENE_SYMBOL' or similar is the gene symbol column\n", "# Let's identify the gene symbol column\n", "gene_symbol_cols = [col for col in gene_annotation.columns if 'gene' in col.lower() or 'symbol' in col.lower()]\n", "if gene_symbol_cols:\n", " gene_col = gene_symbol_cols[0]\n", " print(f\"Using gene symbol column: {gene_col}\")\n", " gene_mapping = get_gene_mapping(gene_annotation, \"ID\", gene_col)\n", " \n", " # Apply gene mapping to convert probe IDs to gene symbols\n", " gene_data_mapped = apply_gene_mapping(gene_data, gene_mapping)\n", " print(f\"Mapped gene data shape: {gene_data_mapped.shape}\")\n", " \n", " # 1. Now normalize gene symbols in the gene expression data\n", " normalized_gene_data = normalize_gene_symbols_in_index(gene_data_mapped)\n", " print(f\"Normalized gene data shape: {normalized_gene_data.shape}\")\n", "else:\n", " # If we can't find a gene symbol column, try using the data as is\n", " print(\"Could not identify gene symbol column, using probe IDs directly\")\n", " normalized_gene_data = gene_data\n", " print(f\"Using original gene data shape: {normalized_gene_data.shape}\")\n", "\n", "# Save the normalized gene data\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "normalized_gene_data.to_csv(out_gene_data_file)\n", "print(f\"Normalized gene data saved to {out_gene_data_file}\")\n", "\n", "# 2. Extract clinical features based on the sample characteristics we observed\n", "def convert_tissue_type(value):\n", " \"\"\"Convert tissue type to binary values (0: non-tumor, 1: tumor)\"\"\"\n", " if pd.isna(value) or not isinstance(value, str):\n", " return None\n", " \n", " value = value.lower()\n", " if \"tumor\" in value and \"non-tumor\" not in value:\n", " return 1 # Tumor\n", " elif \"non-tumor\" in value or \"normal\" in value:\n", " return 0 # Non-tumor or normal\n", " else:\n", " return None\n", "\n", "# Extract clinical features - tissue type is our primary trait for pancreatic cancer\n", "clinical_features = geo_select_clinical_features(\n", " clinical_df=clinical_data,\n", " trait=\"Pancreatic_Cancer\",\n", " trait_row=0, # Tissue type is in position 0\n", " convert_trait=convert_tissue_type,\n", " age_row=None, # Age doesn't appear to be in the dataset\n", " convert_age=None,\n", " gender_row=None, # Gender doesn't appear to be in the dataset\n", " convert_gender=None\n", ")\n", "\n", "# Save clinical features\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 features saved to {out_clinical_data_file}\")\n", "\n", "# Link the clinical and genetic data\n", "linked_data = geo_link_clinical_genetic_data(clinical_features, normalized_gene_data)\n", "print(f\"Linked data shape before handling missing values: {linked_data.shape}\")\n", "\n", "# 3. Handle missing values in the linked data\n", "linked_data = handle_missing_values(linked_data, \"Pancreatic_Cancer\")\n", "print(f\"Linked data 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, \"Pancreatic_Cancer\")\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=\"Cohort contains pancreatic cancer tumor and non-tumor tissue samples.\"\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 }