{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "fc2e92cd", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:56:54.317054Z", "iopub.status.busy": "2025-03-25T07:56:54.316815Z", "iopub.status.idle": "2025-03-25T07:56:54.482407Z", "shell.execute_reply": "2025-03-25T07:56:54.481925Z" } }, "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 = \"GSE200904\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Melanoma\"\n", "in_cohort_dir = \"../../input/GEO/Melanoma/GSE200904\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Melanoma/GSE200904.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Melanoma/gene_data/GSE200904.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Melanoma/clinical_data/GSE200904.csv\"\n", "json_path = \"../../output/preprocess/Melanoma/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "6f955e92", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "98a427af", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:56:54.483664Z", "iopub.status.busy": "2025-03-25T07:56:54.483517Z", "iopub.status.idle": "2025-03-25T07:56:54.511778Z", "shell.execute_reply": "2025-03-25T07:56:54.511411Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"NanoString nCounter NF1-mutant melanoma gene expression\"\n", "!Series_summary\t\"Purpose: Digital spatial profiling was used to elucidate the interplay between the tumor cells and the surrounding microenvironment FFPE tissues.\"\n", "!Series_summary\t\"Methods: Two tissue microarrays (TMA) were generated from each FFPE block using the ISNET Galileo CK4500 computer-driven tissue microarray. Images of H&E-stained cores were demarcated by a pathologist and were utilized alongside NanoString immunofluorescent staining for morphology markers S100, CD45, CD3, and DAPI to draw region of interest.\"\n", "!Series_overall_design\t\"Multiplex gene expression analysis covers 73 endogenous genes from 24 different immune cell types and 48 gene-derived signatures measuring biological variables crucial to the tumor-immune interaction.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['scan_id: 21-002-A_4', 'scan_id: 21-002-A_5', 'scan_id: 21-002-B_1', 'scan_id: 21-002-B_2'], 1: ['roi_id: 1', 'roi_id: 2', 'roi_id: 3', 'roi_id: 4', 'roi_id: 5', 'roi_id: 6', 'roi_id: 7', 'roi_id: 8', 'roi_id: 9', 'roi_id: 10', 'roi_id: 11', 'roi_id: 12', 'roi_id: 13', 'roi_id: 14', 'roi_id: 15', 'roi_id: 16', 'roi_id: 17', 'roi_id: 18', 'roi_id: 19', 'roi_id: 20', 'roi_id: 21', 'roi_id: 22', 'roi_id: 23', 'roi_id: 24', 'roi_id: 25', 'roi_id: 26', 'roi_id: 27', 'roi_id: 28', 'roi_id: 29', 'roi_id: 30'], 2: ['segment: Segment 1', 'segment: Segment 2', 'segment: Geometric Segment'], 3: ['roi.x.coordinate: 10359.90039', 'roi.x.coordinate: 8991.327148', 'roi.x.coordinate: 11615.25586', 'roi.x.coordinate: 12646.64258', 'roi.x.coordinate: 9749.026367', 'roi.x.coordinate: 11445.7373', 'roi.x.coordinate: 9909.205078', 'roi.x.coordinate: 10998.18848', 'roi.x.coordinate: 9463.161133', 'roi.x.coordinate: 11279.61035', 'roi.x.coordinate: 13404.90625', 'roi.x.coordinate: 12361.25293', 'roi.x.coordinate: 14153.09668', 'roi.x.coordinate: 13170.2832', 'roi.x.coordinate: 11525.93848', 'roi.x.coordinate: 12108.08691', 'roi.x.coordinate: 9512.986328', 'roi.x.coordinate: 11928.55371', 'roi.x.coordinate: 11545.77734', 'roi.x.coordinate: 10010.12012', 'roi.x.coordinate: 8703.813477', 'roi.x.coordinate: 12297.97363', 'roi.x.coordinate: 8529.197266', 'roi.x.coordinate: 14428.71582', 'roi.x.coordinate: 14287.40332', 'roi.x.coordinate: 12181.82617', 'roi.x.coordinate: 12162.07617', 'roi.x.coordinate: 13810.98145', 'roi.x.coordinate: 14738.14648', 'roi.x.coordinate: 15060.3291'], 4: ['roi.y.coordinate: 7298.382324', 'roi.y.coordinate: 8328.254883', 'roi.y.coordinate: 7177.562012', 'roi.y.coordinate: 10168.41504', 'roi.y.coordinate: 29898.77148', 'roi.y.coordinate: 29995.19141', 'roi.y.coordinate: 31555.44922', 'roi.y.coordinate: 38907.26563', 'roi.y.coordinate: 38071.52734', 'roi.y.coordinate: 38142.59375', 'roi.y.coordinate: 43965.28125', 'roi.y.coordinate: 43365.45313', 'roi.y.coordinate: 44999.19141', 'roi.y.coordinate: 52913.5625', 'roi.y.coordinate: 52712.83984', 'roi.y.coordinate: 51698.98828', 'roi.y.coordinate: 23541.81836', 'roi.y.coordinate: 23644.9043', 'roi.y.coordinate: 25598.21875', 'roi.y.coordinate: 25131.61523', 'roi.y.coordinate: 17087.02734', 'roi.y.coordinate: 18723.10156', 'roi.y.coordinate: 15579.86426', 'roi.y.coordinate: 7695.199219', 'roi.y.coordinate: 10227.04004', 'roi.y.coordinate: 9229.053711', 'roi.y.coordinate: 23975.24414', 'roi.y.coordinate: 22693.30078', 'roi.y.coordinate: 24928.59961', 'roi.y.coordinate: 31931.12891'], 5: ['aoi: 1'], 6: ['aoi.surface.area: 140781.6009', 'aoi.surface.area: 6321.270391', 'aoi.surface.area: 24458.4625', 'aoi.surface.area: 2685.712817', 'aoi.surface.area: 52515.40299', 'aoi.surface.area: 3089.912277', 'aoi.surface.area: 75993.81366', 'aoi.surface.area: 7022.58602', 'aoi.surface.area: 67135.94793', 'aoi.surface.area: 3716.749082', 'aoi.surface.area: 108590.6061', 'aoi.surface.area: 7516.28793', 'aoi.surface.area: 27205.54843', 'aoi.surface.area: 23035.05389', 'aoi.surface.area: 25985.91772', 'aoi.surface.area: 2333.936222', 'aoi.surface.area: 154.711379', 'aoi.surface.area: 25096.16746', 'aoi.surface.area: 6448.012254', 'aoi.surface.area: 6767.663862', 'aoi.surface.area: 51370.09128', 'aoi.surface.area: 1291.072848', 'aoi.surface.area: 7230.998869', 'aoi.surface.area: 5324.117197', 'aoi.surface.area: 65520.90817', 'aoi.surface.area: 1319.521841', 'aoi.surface.area: 83976.95277', 'aoi.surface.area: 11481.40631', 'aoi.surface.area: 35987.17725', 'aoi.surface.area: 14704.45348'], 7: ['aoi.nuclei.count: 1264', 'aoi.nuclei.count: 81', 'aoi.nuclei.count: 205', 'aoi.nuclei.count: 34', 'aoi.nuclei.count: 477', 'aoi.nuclei.count: 49', 'aoi.nuclei.count: 647', 'aoi.nuclei.count: 141', 'aoi.nuclei.count: 410', 'aoi.nuclei.count: 53', 'aoi.nuclei.count: 674', 'aoi.nuclei.count: 113', 'aoi.nuclei.count: 181', 'aoi.nuclei.count: 338', 'aoi.nuclei.count: 210', 'aoi.nuclei.count: 48', 'aoi.nuclei.count: 0', 'aoi.nuclei.count: 173', 'aoi.nuclei.count: 63', 'aoi.nuclei.count: 60', 'aoi.nuclei.count: 347', 'aoi.nuclei.count: 19', 'aoi.nuclei.count: 80', 'aoi.nuclei.count: 77', 'aoi.nuclei.count: 302', 'aoi.nuclei.count: 4', 'aoi.nuclei.count: 497', 'aoi.nuclei.count: 66', 'aoi.nuclei.count: 289', 'aoi.nuclei.count: 59']}\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": "0aaf110a", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "0640d852", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:56:54.512740Z", "iopub.status.busy": "2025-03-25T07:56:54.512628Z", "iopub.status.idle": "2025-03-25T07:56:54.519375Z", "shell.execute_reply": "2025-03-25T07:56:54.519015Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 1. Gene Expression Data Availability\n", "# From the background information, this dataset contains gene expression data\n", "# \"Multiplex gene expression analysis covers 73 endogenous genes...\"\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# Looking at the sample characteristics dictionary:\n", "\n", "# There is no explicit melanoma data in sample characteristics\n", "# Since this is a study specifically about NF1-mutant melanoma (from title),\n", "# all samples are likely melanoma samples with no control group\n", "trait_row = None\n", "\n", "# No age information available in the sample characteristics\n", "age_row = None\n", "\n", "# No gender information available in the sample characteristics\n", "gender_row = None\n", "\n", "# Define conversion functions (even though we don't have data for these variables)\n", "def convert_trait(value):\n", " \"\"\"Convert trait data to binary (1 for melanoma, 0 for control).\"\"\"\n", " if not value or \":\" not in value:\n", " return None\n", " val = value.split(\":\", 1)[1].strip().lower()\n", " if \"melanoma\" in val:\n", " return 1\n", " elif \"control\" in val or \"normal\" in val or \"healthy\" in val:\n", " return 0\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age data to continuous numeric values.\"\"\"\n", " if not value or \":\" not in value:\n", " return None\n", " val = value.split(\":\", 1)[1].strip()\n", " try:\n", " return float(val)\n", " except ValueError:\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender data to binary (0 for female, 1 for male).\"\"\"\n", " if not value or \":\" not in value:\n", " return None\n", " val = value.split(\":\", 1)[1].strip().lower()\n", " if val in [\"female\", \"f\"]:\n", " return 0\n", " elif val in [\"male\", \"m\"]:\n", " return 1\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Initial filtering on dataset usability\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", "# Since trait_row is None, we'll skip the clinical feature extraction step\n" ] }, { "cell_type": "markdown", "id": "073b9c83", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "a669f44b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:56:54.520286Z", "iopub.status.busy": "2025-03-25T07:56:54.520179Z", "iopub.status.idle": "2025-03-25T07:56:54.559504Z", "shell.execute_reply": "2025-03-25T07:56:54.559132Z" } }, "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", "\n", "Gene data extraction result:\n", "Number of rows: 84\n", "First 20 gene/probe identifiers:\n", "Index(['AKT1', 'ARG1', 'B2M', 'BATF3', 'BCL2', 'CCL5', 'CCND1', 'CD27',\n", " 'CD274', 'CD276', 'CD3E', 'CD4', 'CD40', 'CD40LG', 'CD44', 'CD47',\n", " 'CD68', 'CD74', 'CD86', 'CD8A'],\n", " 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.\")\n" ] }, { "cell_type": "markdown", "id": "f70408c0", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "6bc660df", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:56:54.560439Z", "iopub.status.busy": "2025-03-25T07:56:54.560326Z", "iopub.status.idle": "2025-03-25T07:56:54.562328Z", "shell.execute_reply": "2025-03-25T07:56:54.561971Z" } }, "outputs": [], "source": [ "# Reviewing the gene identifiers in the gene expression data\n", "# These appear to be standard human gene symbols (like AKT1, CD8A, B2M, etc.)\n", "# rather than probe IDs or other identifiers that would need mapping\n", "\n", "requires_gene_mapping = False\n" ] }, { "cell_type": "markdown", "id": "f7cf9498", "metadata": {}, "source": [ "### Step 5: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 6, "id": "99c215cf", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:56:54.563359Z", "iopub.status.busy": "2025-03-25T07:56:54.563154Z", "iopub.status.idle": "2025-03-25T07:56:54.667617Z", "shell.execute_reply": "2025-03-25T07:56:54.667146Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Top 10 gene indices before normalization: ['AKT1', 'ARG1', 'B2M', 'BATF3', 'BCL2', 'CCL5', 'CCND1', 'CD27', 'CD274', 'CD276']\n", "Top 10 gene indices after normalization: ['AKT1', 'ARG1', 'B2M', 'BATF3', 'BCL2', 'CCL5', 'CCND1', 'CD27', 'CD274', 'CD276']\n", "Shape of normalized gene data: (76, 341)\n", "Saved normalized gene data to ../../output/preprocess/Melanoma/gene_data/GSE200904.csv\n", "No appropriate trait information available for case-control comparison.\n", "Abnormality detected in the cohort: GSE200904. Preprocessing failed.\n", "Dataset lacks appropriate trait information (no control samples). Final linked data not created.\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "print(f\"Top 10 gene indices before normalization: {gene_data.index[:10].tolist()}\")\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Top 10 gene indices after normalization: {normalized_gene_data.index[:10].tolist()}\")\n", "print(f\"Shape of normalized gene data: {normalized_gene_data.shape}\")\n", "\n", "# Create directory for gene data file if it doesn't exist\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "# Save the normalized gene data\n", "normalized_gene_data.to_csv(out_gene_data_file)\n", "print(f\"Saved normalized gene data to {out_gene_data_file}\")\n", "\n", "# Since we determined in Step 2 that trait_row is None (no appropriate trait data available),\n", "# we can't properly link clinical and gene data for trait association analysis\n", "print(\"No appropriate trait information available for case-control comparison.\")\n", "\n", "# Create a minimal DataFrame with expected structure for validation\n", "minimal_df = pd.DataFrame({trait: [float('nan')], 'dummy_gene': [0]})\n", "# Set is_biased to True since we lack appropriate trait data for comparison\n", "is_biased = True\n", "\n", "# Validate and save 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=False, # We confirmed trait data is not available\n", " is_biased=is_biased, # Dataset considered biased due to lack of controls\n", " df=minimal_df, # Minimal DataFrame with expected structure\n", " note=\"Dataset contains gene expression data from NF1-mutant melanoma samples, but lacks control samples for comparison.\"\n", ")\n", "\n", "print(\"Dataset lacks appropriate trait information (no control samples). Final linked data not created.\")" ] } ], "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 }