{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "f3b05f6f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:31:38.086381Z", "iopub.status.busy": "2025-03-25T04:31:38.085963Z", "iopub.status.idle": "2025-03-25T04:31:38.251742Z", "shell.execute_reply": "2025-03-25T04:31:38.251416Z" } }, "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 = \"Uterine_Carcinosarcoma\"\n", "cohort = \"GSE36138\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Uterine_Carcinosarcoma\"\n", "in_cohort_dir = \"../../input/GEO/Uterine_Carcinosarcoma/GSE36138\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Uterine_Carcinosarcoma/GSE36138.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Uterine_Carcinosarcoma/gene_data/GSE36138.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Uterine_Carcinosarcoma/clinical_data/GSE36138.csv\"\n", "json_path = \"../../output/preprocess/Uterine_Carcinosarcoma/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "7ebad336", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "24f43135", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:31:38.253157Z", "iopub.status.busy": "2025-03-25T04:31:38.253018Z", "iopub.status.idle": "2025-03-25T04:31:38.773357Z", "shell.execute_reply": "2025-03-25T04:31:38.773011Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Files in the cohort directory:\n", "['GSE36138_family.soft.gz', 'GSE36138_series_matrix.txt.gz']\n", "Identified SOFT files: ['GSE36138_family.soft.gz']\n", "Identified matrix files: ['GSE36138_series_matrix.txt.gz']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Background Information:\n", "!Series_title\t\"SNP array data from the Cancer Cell Line Encyclopedia (CCLE)\"\n", "!Series_summary\t\"The Cancer Cell Line Encyclopedia (CCLE) project is a collaboration between the Broad Institute, the Novartis Institutes for Biomedical Research and the Genomics Novartis Foundation to conduct a detailed genetic and pharmacologic characterization of a large panel of human cancer models\"\n", "!Series_summary\t\"It consists of a compilation of gene expression, chromosomal copy number, and massively parallel sequencing data from nearly 1,000 human cancer cell lines. All raw and processed data are available through an integrated portal on www.broadinstitute.org/ccle\"\n", "!Series_overall_design\t\"The final cell line collection spans 36 cancer types. Representation of cell lines for each cancer type was mainly driven by cancer mortality in the United States, as a surrogate of unmet medical need, as well as availability.\"\n", "\n", "Sample Characteristics Dictionary:\n", "{0: ['primary site: central_nervous_system', 'primary site: bone', 'primary site: prostate', 'primary site: stomach', 'primary site: urinary_tract', 'primary site: ovary', 'primary site: haematopoietic_and_lymphoid_tissue', 'primary site: kidney', 'primary site: thyroid', 'primary site: skin', 'primary site: soft_tissue', 'primary site: salivary_gland', 'primary site: lung', 'primary site: pleura', 'primary site: liver', 'primary site: endometrium', 'primary site: pancreas', 'primary site: breast', 'primary site: upper_aerodigestive_tract', 'primary site: large_intestine', 'primary site: autonomic_ganglia', 'primary site: oesophagus', 'primary site: biliary_tract', 'primary site: small_intestine'], 1: ['histology: glioma', 'histology: osteosarcoma', 'histology: carcinoma', 'histology: haematopoietic_neoplasm', 'histology: malignant_melanoma', 'histology: rhabdomyosarcoma', 'histology: lymphoid_neoplasm', 'histology: Ewings_sarcoma-peripheral_primitive_neuroectodermal_tumour', 'histology: mesothelioma', 'histology: chondrosarcoma', 'histology: neuroblastoma', 'histology: sex_cord-stromal_tumour', 'histology: primitive_neuroectodermal_tumour-medulloblastoma', 'histology: rhabdoid_tumour', 'histology: malignant_fibrous_histiocytoma-pleomorphic_sarcoma', 'histology: giant_cell_tumour', 'histology: other', 'histology: fibrosarcoma', 'histology: carcinoid-endocrine_tumour', 'histology: leiomyosarcoma', 'histology: sarcoma'], 2: ['histology subtype1: astrocytoma', nan, 'histology subtype1: adenocarcinoma', 'histology subtype1: astrocytoma_Grade_IV', 'histology subtype1: transitional_cell_carcinoma', 'histology subtype1: acute_lymphoblastic_leukaemia', 'histology subtype1: clear_cell_renal_cell_carcinoma', 'histology subtype1: anaplastic_carcinoma', 'histology subtype1: mucoepidermoid_carcinoma', 'histology subtype1: diffuse_large_B_cell_lymphoma', 'histology subtype1: renal_cell_carcinoma', 'histology subtype1: non_small_cell_carcinoma', 'histology subtype1: hepatocellular_carcinoma', 'histology subtype1: acute_myeloid_leukaemia', 'histology subtype1: plasma_cell_myeloma', 'histology subtype1: ductal_carcinoma', 'histology subtype1: B_cell_lymphoma_unspecified', 'histology subtype1: papillary_carcinoma', 'histology subtype1: acute_lymphoblastic_B_cell_leukaemia', 'histology subtype1: squamous_cell_carcinoma', 'histology subtype1: Burkitt_lymphoma', 'histology subtype1: blast_phase_chronic_myeloid_leukaemia', 'histology subtype1: dedifferentiated', 'histology subtype1: undifferentiated_carcinoma', 'histology subtype1: follicular_carcinoma', 'histology subtype1: small_cell_carcinoma', 'histology subtype1: large_cell_carcinoma', 'histology subtype1: granulosa_cell_tumour', 'histology subtype1: acute_lymphoblastic_T_cell_leukaemia', 'histology subtype1: small_cell_adenocarcinoma']}\n" ] } ], "source": [ "# 1. Let's first list the directory contents to understand what files are available\n", "import os\n", "\n", "print(\"Files in the cohort directory:\")\n", "files = os.listdir(in_cohort_dir)\n", "print(files)\n", "\n", "# Adapt file identification to handle different naming patterns\n", "soft_files = [f for f in files if 'soft' in f.lower() or '.soft' in f.lower() or '_soft' in f.lower()]\n", "matrix_files = [f for f in files if 'matrix' in f.lower() or '.matrix' in f.lower() or '_matrix' in f.lower()]\n", "\n", "# If no files with these patterns are found, look for alternative file types\n", "if not soft_files:\n", " soft_files = [f for f in files if f.endswith('.txt') or f.endswith('.gz')]\n", "if not matrix_files:\n", " matrix_files = [f for f in files if f.endswith('.txt') or f.endswith('.gz')]\n", "\n", "print(\"Identified SOFT files:\", soft_files)\n", "print(\"Identified matrix files:\", matrix_files)\n", "\n", "# Use the first files found, if any\n", "if len(soft_files) > 0 and len(matrix_files) > 0:\n", " soft_file = os.path.join(in_cohort_dir, soft_files[0])\n", " matrix_file = os.path.join(in_cohort_dir, matrix_files[0])\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(\"\\nBackground Information:\")\n", " print(background_info)\n", " print(\"\\nSample Characteristics Dictionary:\")\n", " print(sample_characteristics_dict)\n", "else:\n", " print(\"No appropriate files found in the directory.\")\n" ] }, { "cell_type": "markdown", "id": "33e968fb", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "84d673aa", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:31:38.774548Z", "iopub.status.busy": "2025-03-25T04:31:38.774439Z", "iopub.status.idle": "2025-03-25T04:31:38.780365Z", "shell.execute_reply": "2025-03-25T04:31:38.780093Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Analyze dataset for gene expression data\n", "# Looking at the background information, this appears to be SNP array data from cancer cell lines\n", "# rather than gene expression data suitable for our analysis\n", "is_gene_available = False\n", "\n", "# For trait data analysis: looking for Uterine_Carcinosarcoma in the dataset\n", "# After reviewing the sample characteristics dictionary, we need to check if the data includes our trait of interest\n", "trait_row = None\n", "\n", "# From the sample characteristics, we can see \"primary site: endometrium\" is present\n", "# But there's no specific mention of carcinosarcoma in the provided characteristics\n", "# Looking through histology and histology subtype fields reveals no clear indicator of carcinosarcoma\n", "\n", "# Age information is not provided in the sample characteristics\n", "age_row = None\n", "\n", "# Gender information is not provided in the sample characteristics\n", "gender_row = None\n", "\n", "# Define conversion functions (though they won't be used for this dataset)\n", "def convert_trait(value):\n", " \"\"\"Convert trait value to binary format.\"\"\"\n", " if not value or pd.isna(value):\n", " return None\n", " value = value.split(':')[-1].strip().lower()\n", " if 'carcinosarcoma' in value:\n", " return 1\n", " else:\n", " return 0\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age value to continuous format.\"\"\"\n", " if not value or pd.isna(value):\n", " return None\n", " value = value.split(':')[-1].strip()\n", " try:\n", " return float(value)\n", " except:\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender value to binary format.\"\"\"\n", " if not value or pd.isna(value):\n", " return None\n", " value = value.split(':')[-1].strip().lower()\n", " if 'female' in value:\n", " return 0\n", " elif 'male' in value:\n", " return 1\n", " else:\n", " return None\n", "\n", "# Check if trait data is available\n", "is_trait_available = trait_row is not None\n", "\n", "# Save metadata 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", "# Skip clinical feature extraction since trait_row is None\n" ] }, { "cell_type": "markdown", "id": "ddf165d0", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "87ab728a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:31:38.781425Z", "iopub.status.busy": "2025-03-25T04:31:38.781326Z", "iopub.status.idle": "2025-03-25T04:31:40.314688Z", "shell.execute_reply": "2025-03-25T04:31:40.314314Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First 20 gene/probe identifiers:\n", "Index(['14q(0)', '14q(I-1)', '14q(I-2)', '14q(I-3)', '14q(I-4)', '14q(I-5)',\n", " '14q(I-6)', '14q(I-7)', '14q(I-8)', '14q(I-9)', '14q(II-1)',\n", " '14q(II-10)', '14q(II-11)', '14q(II-12)', '14q(II-13)', '14q(II-14)',\n", " '14q(II-15)', '14q(II-16)', '14q(II-17)', '14q(II-18)'],\n", " dtype='object', name='ID')\n", "\n", "Gene expression data shape: (22519, 947)\n" ] } ], "source": [ "# Use the helper function to get the proper file paths\n", "soft_file_path, matrix_file_path = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# Extract gene expression data\n", "try:\n", " gene_data = get_genetic_data(matrix_file_path)\n", " \n", " # Print the first 20 row IDs (gene or probe identifiers)\n", " print(\"First 20 gene/probe identifiers:\")\n", " print(gene_data.index[:20])\n", " \n", " # Print shape to understand the dataset dimensions\n", " print(f\"\\nGene expression data shape: {gene_data.shape}\")\n", " \n", "except Exception as e:\n", " print(f\"Error extracting gene data: {e}\")\n" ] }, { "cell_type": "markdown", "id": "8875f4d8", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "b9d9f249", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:31:40.315937Z", "iopub.status.busy": "2025-03-25T04:31:40.315821Z", "iopub.status.idle": "2025-03-25T04:31:40.317730Z", "shell.execute_reply": "2025-03-25T04:31:40.317446Z" } }, "outputs": [], "source": [ "# Upon examining the gene identifiers, these are not standard human gene symbols\n", "# These appear to be probe identifiers, possibly specific to a particular microarray platform\n", "# Identifiers like '14q(0)', '14q(I-1)' are not standard gene symbols, which would typically be names like 'BRCA1', 'TP53', etc.\n", "# These would need to be mapped to standard gene symbols for meaningful analysis\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "7a9aae54", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "f6276c45", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:31:40.318812Z", "iopub.status.busy": "2025-03-25T04:31:40.318711Z", "iopub.status.idle": "2025-03-25T04:31:50.430923Z", "shell.execute_reply": "2025-03-25T04:31:50.430488Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sample content from SOFT file:\n", "^DATABASE = GeoMiame\n", "!Database_name = Gene Expression Omnibus (GEO)\n", "!Database_institute = NCBI NLM NIH\n", "!Database_web_link = http://www.ncbi.nlm.nih.gov/geo\n", "!Database_email = geo@ncbi.nlm.nih.gov\n", "^SERIES = GSE36138\n", "!Series_title = SNP array data from the Cancer Cell Line Encyclopedia (CCLE)\n", "!Series_geo_accession = GSE36138\n", "!Series_status = Public on Mar 20 2012\n", "!Series_submission_date = Feb 28 2012\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Platform/Series information:\n", "^SERIES = GSE36138\n", "^PLATFORM = GPL15315\n", "!Platform_title = Affymetrix Genome-Wide Human SNP 6.0 Array\n", "!Platform_geo_accession = GPL15315\n", "!Platform_status = Public on Mar 20 2012\n", "!Platform_submission_date = Mar 06 2012\n", "!Platform_last_update_date = Jan 17 2013\n", "!Platform_technology = in situ oligonucleotide\n", "!Platform_distribution = commercial\n", "!Platform_organism = Homo sapiens\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Found 948 annotation sections\n", "\n", "Section 0 preview:\n", "^PLATFORM = GPL15315\n", "!Platform_title = Affymetrix Genome-Wide Human SNP 6.0 Array\n", "!Platform_geo_accession = GPL15315\n", "!Platform_status = Public on Mar 20 2012\n", "!Platform_submission_date = Mar 06 2012\n", "\n", "Section 1 preview:\n", "^SAMPLE = GSM887898\n", "!Sample_title = 1321N1\n", "!Sample_geo_accession = GSM887898\n", "!Sample_status = Public on Mar 20 2012\n", "!Sample_submission_date = Mar 06 2012\n", "\n", "Section 2 preview:\n", "^SAMPLE = GSM887899\n", "!Sample_title = 143B\n", "!Sample_geo_accession = GSM887899\n", "!Sample_status = Public on Mar 20 2012\n", "!Sample_submission_date = Mar 06 2012\n", "\n", "Note: Based on the file preview, this appears to be an SNP array dataset (not gene expression)\n", "The probe IDs seem to represent SNP locations not gene probes, which aligns with the dataset's\n", "description as 'SNP array data from the Cancer Cell Line Encyclopedia (CCLE)'\n" ] } ], "source": [ "# 1. Extract gene annotation data from the SOFT file that will contain the probe-to-gene mapping\n", "try:\n", " # First, let's examine the SOFT file content to better understand its structure\n", " import gzip\n", " \n", " # Read the first 100 lines of the file to analyze its structure\n", " probe_id_pattern = None\n", " with gzip.open(soft_file_path, 'rt', encoding='latin-1') as f:\n", " sample_content = [f.readline().strip() for _ in range(100)]\n", " print(\"Sample content from SOFT file:\")\n", " for line in sample_content[:10]: # Print first 10 lines\n", " print(line)\n", " \n", " # Look for the platform or series information sections that might contain our probe mappings\n", " platform_lines = []\n", " with gzip.open(soft_file_path, 'rt', encoding='latin-1') as f:\n", " for line in f:\n", " if \"^PLATFORM\" in line or \"^SERIES\" in line or \"!Platform_\" in line:\n", " platform_lines.append(line.strip())\n", " \n", " print(\"\\nPlatform/Series information:\")\n", " for line in platform_lines[:10]: # Print first 10 platform-related lines\n", " print(line)\n", " \n", " # Try using a different approach to get annotation data\n", " # Let's read the file line by line to find sections with probe mappings\n", " annotation_sections = []\n", " current_section = None\n", " with gzip.open(soft_file_path, 'rt', encoding='latin-1') as f:\n", " for line in f:\n", " if line.startswith('^PLATFORM') or line.startswith('^SAMPLE'):\n", " if current_section:\n", " annotation_sections.append(current_section)\n", " current_section = [line.strip()]\n", " elif current_section is not None:\n", " current_section.append(line.strip())\n", " \n", " if current_section:\n", " annotation_sections.append(current_section)\n", " \n", " # Print the beginning of each section to identify which might contain probe mappings\n", " print(f\"\\nFound {len(annotation_sections)} annotation sections\")\n", " if annotation_sections:\n", " for i, section in enumerate(annotation_sections[:3]): # Print first 3 sections\n", " print(f\"\\nSection {i} preview:\")\n", " for line in section[:5]: # Print first 5 lines of each section\n", " print(line)\n", " \n", " # For now, create an empty annotation dataframe\n", " # Once we identify the correct section, we can correctly parse the mapping\n", " gene_annotation = pd.DataFrame()\n", " \n", "except Exception as e:\n", " print(f\"Error extracting gene annotation data: {e}\")\n", " gene_annotation = pd.DataFrame()\n", "\n", "print(\"\\nNote: Based on the file preview, this appears to be an SNP array dataset (not gene expression)\")\n", "print(\"The probe IDs seem to represent SNP locations not gene probes, which aligns with the dataset's\")\n", "print(\"description as 'SNP array data from the Cancer Cell Line Encyclopedia (CCLE)'\")" ] } ], "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 }