{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "c05bff31", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:49:05.096528Z", "iopub.status.busy": "2025-03-25T03:49:05.096424Z", "iopub.status.idle": "2025-03-25T03:49:05.264024Z", "shell.execute_reply": "2025-03-25T03:49:05.263619Z" } }, "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 = \"Retinoblastoma\"\n", "cohort = \"GSE25307\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Retinoblastoma\"\n", "in_cohort_dir = \"../../input/GEO/Retinoblastoma/GSE25307\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Retinoblastoma/GSE25307.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Retinoblastoma/gene_data/GSE25307.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Retinoblastoma/clinical_data/GSE25307.csv\"\n", "json_path = \"../../output/preprocess/Retinoblastoma/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "ff37e203", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "6de2258f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:49:05.265300Z", "iopub.status.busy": "2025-03-25T03:49:05.265137Z", "iopub.status.idle": "2025-03-25T03:49:05.511724Z", "shell.execute_reply": "2025-03-25T03:49:05.511347Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Files in the cohort directory:\n", "['GSE25307_family.soft.gz', 'GSE25307_series_matrix.txt.gz']\n", "Identified SOFT files: ['GSE25307_family.soft.gz']\n", "Identified matrix files: ['GSE25307_series_matrix.txt.gz']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Background Information:\n", "!Series_title\t\"The retinoblastoma gene is targeted for rearrangements in BRCA1-deficient basal-like breast cancer.\"\n", "!Series_summary\t\"Breast tumors from BRCA1 germ line mutation carriers typically exhibit features of the basal-like molecular subtype. However, the specific genes recurrently mutated as a consequence of BRCA1 dysfunction have not been fully elucidated. In this study, we utilized gene expression profiling to molecularly subtype 577 breast tumors, including 73 breast tumors from BRCA1/2 mutation carriers. Focusing on the RB1 locus, we analyzed 33 BRCA1-mutated, 36 BRCA2-mutated and 48 non-BRCA1/2-mutated breast tumors using a custom-designed high-density oligomicroarray covering the RB1 gene. We found a strong association between the basal-like subtype and BRCA1-mutated breast tumors and the luminal B subtype and BRCA2-mutated breast tumors. RB1 was identified as a major target for genomic disruption in tumors arising in BRCA1 mutation carriers and in sporadic tumors with BRCA1 promoter-methylation, but rarely in other breast cancers. Homozygous deletions, intragenic breaks, or microdeletions were found in 33% of BRCA1-mutant tumors, 36% of BRCA1 promoter-methylated basal-like tumors, 13% of non-BRCA1 deficient basal-like tumors, and 3% of BRCA2-mutated tumors. In addition, RB1 was frequently inactivated by gross gene disruption in BRCA1-related hereditary breast cancer and BRCA1-methylated sporadic basal-like breast cancer, but rarely in BRCA2-hereditary breast cancer and non-BRCA1-deficient sporadic breast cancers. Together, our findings demonstrate the existence of genetic heterogeneity within the basal-like breast cancer subtype that is based upon BRCA1-status.\"\n", "!Series_overall_design\t\"Gene expression profiling of breast tumors. Dual color common reference gene expression study using 55K oligonucleotide microarrays.\"\n", "\n", "Sample Characteristics Dictionary:\n", "{0: ['tissue: Breast tumor', 'tissue: non malignant'], 1: ['hu subtype: LumB', 'hu subtype: LumA', 'hu subtype: Normal', 'hu subtype: nonClassified', 'hu subtype: Basal', 'hu subtype: ERBB2'], 2: ['familial status: sporadic', 'familial status: brca1', 'familial status: familial', 'familial status: brca2', 'familial status: non malignant'], 3: ['er: er_pos', 'er: NA', 'er: er_neg'], 4: ['osbin: 1', 'osbin: 0', 'osbin: NA'], 5: ['os: 10.0958904109589', 'os: 16.8027397260274', 'os: 16.627397260274', 'os: 5.7041095890411', 'os: 15.7616438356164', 'os: 14.7232876712329', 'os: 5.49041095890411', 'os: 10.3178082191781', 'os: 2.51232876712329', 'os: 1.33150684931507', 'os: 15.7260273972603', 'os: 5.55890410958904', 'os: 17.0328767123288', 'os: 13.9342465753425', 'os: 3.00547945205479', 'os: 12.7671232876712', 'os: 18.4164383561644', 'os: 2.21369863013699', 'os: 5.03287671232877', 'os: 8.9972602739726', 'os: 7.78630136986301', 'os: 3.00821917808219', 'os: 11.9945205479452', 'os: 1.11232876712329', 'os: 15.2054794520548', 'os: 2.12602739726027', 'os: 7.35616438356164', 'os: 7.28767123287671', 'os: 15.013698630137', 'os: 14.3315068493151'], 6: ['pgr: pgr_pos', 'pgr: NA', 'pgr: pgr_neg'], 7: ['primary: 1', 'primary: 0'], 8: ['grade: NA', 'grade: 1', 'grade: 2', 'grade: 3'], 9: ['genomic subtype: mixed', 'genomic subtype: amplifier', 'genomic subtype: Luminal-simple', 'genomic subtype: Luminal-complex', 'genomic subtype: Basal-complex', 'genomic subtype: 17q12', 'brca status: sporadic', 'brca status: brcax', 'brca status: other', 'brca status: brca2', 'brca status: brca1'], 10: ['brca status: sporadic', 'brca status: brca1', 'brca status: brcax', 'brca status: other', 'brca status: brca2', 'pam50 classification: Normal', 'pam50 classification: Basal', 'pam50 classification: HER2enriched', 'pam50 classification: LumB', 'pam50 classification: LumA', 'pam50 classification: Unclassified'], 11: ['pam50 classification: LumA', 'pam50 classification: LumB', 'pam50 classification: Unclassified', 'pam50 classification: Normal', 'pam50 classification: Basal', 'pam50 classification: HER2enriched', 'pam50 correlation: 0.425917', 'pam50 correlation: 0.271825', 'pam50 correlation: 0.274597', 'pam50 correlation: 0.412352', 'pam50 correlation: 0.696178', 'pam50 correlation: 0.405406', 'pam50 correlation: 0.762758', 'pam50 correlation: 0.251069', 'pam50 correlation: 0.721505', 'pam50 correlation: 0.571304', 'pam50 correlation: 0.652412', 'pam50 correlation: 0.627204', 'pam50 correlation: 0.512484', 'pam50 correlation: 0.674854', 'pam50 correlation: 0.208585', 'pam50 correlation: 0.650855', 'pam50 correlation: 0.505452', 'pam50 correlation: 0.549201', 'pam50 correlation: 0.497965', 'pam50 correlation: 0.611026', 'pam50 correlation: 0.707419', 'pam50 correlation: 0.639102', 'pam50 correlation: 0.536733', 'pam50 correlation: 0.479839'], 12: ['pam50 correlation: 0.442775', 'pam50 correlation: 0.619622', 'pam50 correlation: 0.260755', 'pam50 correlation: -0.0363901', 'pam50 correlation: 0.507158', 'pam50 correlation: 0.348096', 'pam50 correlation: 0.159811', 'pam50 correlation: 0.518318', 'pam50 correlation: 0.719374', 'pam50 correlation: 0.60978', 'pam50 correlation: 0.689719', 'pam50 correlation: 0.677418', 'pam50 correlation: 0.0747315', 'pam50 correlation: 0.0983756', 'pam50 correlation: 0.260206', 'pam50 correlation: 0.34635', 'pam50 correlation: 0.39343', 'pam50 correlation: 0.745056', 'pam50 correlation: 0.376598', 'pam50 correlation: 0.297337', 'pam50 correlation: 0.625973', 'pam50 correlation: 0.556909', 'pam50 correlation: 0.555434', 'pam50 correlation: 0.6051', 'pam50 correlation: 0.295226', 'pam50 correlation: 0.170941', 'pam50 correlation: 0.359093', 'pam50 correlation: 0.308128', 'pam50 correlation: 0.367795', 'pam50 correlation: 0.248039'], 13: ['er: er_pos', 'er: non_available', 'er: er_neg', 'brca1 methylation status: negative', 'brca1 methylation status: non_available', 'brca1 methylation status: positive'], 14: ['brca1 methylation status: non_available', 'brca1 methylation status: negative', 'brca1 methylation status: positive', nan]}\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": "260a6eee", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "927fc1d6", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:49:05.512987Z", "iopub.status.busy": "2025-03-25T03:49:05.512771Z", "iopub.status.idle": "2025-03-25T03:49:05.538496Z", "shell.execute_reply": "2025-03-25T03:49:05.538111Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of clinical data:\n", "{'GSM551608': [0.0], 'GSM551609': [0.0], 'GSM551610': [0.0], 'GSM551611': [0.0], 'GSM551612': [0.0], 'GSM551613': [0.0], 'GSM551614': [0.0], 'GSM551615': [0.0], 'GSM551616': [0.0], 'GSM551617': [1.0], 'GSM551618': [0.0], 'GSM551619': [0.0], 'GSM551620': [0.0], 'GSM551621': [0.0], 'GSM551622': [0.0], 'GSM551623': [0.0], 'GSM551624': [0.0], 'GSM551625': [0.0], 'GSM551626': [0.0], 'GSM551627': [0.0], 'GSM551628': [0.0], 'GSM551629': [0.0], 'GSM551630': [0.0], 'GSM551631': [1.0], 'GSM551632': [0.0], 'GSM551633': [0.0], 'GSM551634': [0.0], 'GSM551635': [0.0], 'GSM551636': [0.0], 'GSM551637': [0.0], 'GSM551638': [0.0], 'GSM551639': [0.0], 'GSM551640': [0.0], 'GSM551641': [0.0], 'GSM551642': [0.0], 'GSM551643': [0.0], 'GSM551644': [0.0], 'GSM551645': [0.0], 'GSM551646': [0.0], 'GSM551647': [0.0], 'GSM551648': [0.0], 'GSM551649': [1.0], 'GSM551650': [0.0], 'GSM551651': [0.0], 'GSM551652': [0.0], 'GSM551653': [0.0], 'GSM551654': [0.0], 'GSM551655': [0.0], 'GSM551656': [0.0], 'GSM551657': [0.0], 'GSM551658': [0.0], 'GSM551659': [0.0], 'GSM551660': [0.0], 'GSM551661': [0.0], 'GSM551662': [0.0], 'GSM551663': [0.0], 'GSM551664': [0.0], 'GSM551665': [0.0], 'GSM551666': [0.0], 'GSM551667': [0.0], 'GSM551668': [0.0], 'GSM551669': [0.0], 'GSM551670': [1.0], 'GSM551671': [0.0], 'GSM551672': [0.0], 'GSM551673': [0.0], 'GSM551674': [0.0], 'GSM551675': [0.0], 'GSM551676': [0.0], 'GSM551677': [0.0], 'GSM551678': [0.0], 'GSM551679': [0.0], 'GSM551680': [0.0], 'GSM551681': [0.0], 'GSM551682': [0.0], 'GSM551683': [0.0], 'GSM551684': [0.0], 'GSM551685': [0.0], 'GSM551686': [0.0], 'GSM551687': [0.0], 'GSM551688': [0.0], 'GSM551689': [0.0], 'GSM551690': [0.0], 'GSM551691': [0.0], 'GSM551692': [0.0], 'GSM551693': [0.0], 'GSM551694': [0.0], 'GSM551695': [0.0], 'GSM551696': [0.0], 'GSM551697': [0.0], 'GSM551698': [0.0], 'GSM551699': [0.0], 'GSM551700': [0.0], 'GSM551701': [0.0], 'GSM551702': [0.0], 'GSM551703': [0.0], 'GSM551704': [0.0], 'GSM551705': [0.0], 'GSM551706': [0.0], 'GSM551707': [0.0], 'GSM551708': [0.0], 'GSM551709': [0.0], 'GSM551710': [0.0], 'GSM551711': [0.0], 'GSM551712': [0.0], 'GSM551713': [0.0], 'GSM551714': [0.0], 'GSM551715': [0.0], 'GSM551716': [0.0], 'GSM551717': [0.0], 'GSM551718': [0.0], 'GSM551719': [0.0], 'GSM551720': [0.0], 'GSM551721': [0.0], 'GSM551722': [0.0], 'GSM551723': [0.0], 'GSM551724': [0.0], 'GSM551725': [0.0], 'GSM551726': [0.0], 'GSM551727': [0.0], 'GSM551728': [0.0], 'GSM551729': [0.0], 'GSM551730': [0.0], 'GSM551731': [0.0], 'GSM551732': [0.0], 'GSM551733': [0.0], 'GSM551734': [0.0], 'GSM551735': [0.0], 'GSM551736': [0.0], 'GSM551737': [0.0], 'GSM551738': [0.0], 'GSM551739': [0.0], 'GSM551740': [0.0], 'GSM551741': [0.0], 'GSM551742': [0.0], 'GSM551743': [0.0], 'GSM551744': [0.0], 'GSM551745': [0.0], 'GSM551746': [0.0], 'GSM551747': [0.0], 'GSM551748': [0.0], 'GSM551749': [0.0], 'GSM551750': [0.0], 'GSM551751': [0.0], 'GSM551752': [0.0], 'GSM551753': [0.0], 'GSM551754': [0.0], 'GSM551755': [0.0], 'GSM551756': [0.0], 'GSM551757': [0.0], 'GSM551758': [0.0], 'GSM551759': [0.0], 'GSM551760': [0.0], 'GSM551761': [0.0], 'GSM551762': [0.0], 'GSM551763': [0.0], 'GSM551764': [0.0], 'GSM551765': [0.0], 'GSM551766': [0.0], 'GSM551767': [0.0], 'GSM551768': [0.0], 'GSM551769': [0.0], 'GSM551770': [0.0], 'GSM551771': [0.0], 'GSM551772': [0.0], 'GSM551773': [0.0], 'GSM551774': [0.0], 'GSM551775': [0.0], 'GSM551776': [0.0], 'GSM551777': [1.0], 'GSM551778': [0.0], 'GSM551779': [0.0], 'GSM551780': [0.0], 'GSM551781': [0.0], 'GSM551782': [0.0], 'GSM551783': [0.0], 'GSM551784': [0.0], 'GSM551785': [0.0], 'GSM551786': [0.0], 'GSM551787': [0.0], 'GSM551788': [0.0], 'GSM551789': [0.0], 'GSM551790': [0.0], 'GSM551791': [0.0], 'GSM551792': [0.0], 'GSM551793': [0.0], 'GSM551794': [0.0], 'GSM551795': [0.0], 'GSM551796': [0.0], 'GSM551797': [0.0], 'GSM551798': [0.0], 'GSM551799': [0.0], 'GSM551800': [0.0], 'GSM551801': [0.0], 'GSM551802': [0.0], 'GSM551803': [0.0], 'GSM551804': [0.0], 'GSM551805': [0.0], 'GSM551806': [0.0], 'GSM551807': [0.0]}\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Yes, this dataset contains gene expression data (based on the Series_summary/design)\n", "is_gene_available = True\n", "\n", "# 2.1 Data Availability\n", "# For trait - we identify retinoblastoma from BRCA status\n", "trait_row = 10 # \"brca status\" values in index 10\n", "# Age is not available in sample characteristics\n", "age_row = None\n", "# Gender is not available in sample characteristics\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value):\n", " \"\"\"Convert trait value to binary based on BRCA1 status\"\"\"\n", " if isinstance(value, str) and ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " if value.lower() == 'brca1':\n", " return 1 # BRCA1 mutated is our target condition\n", " elif value.lower() in ['sporadic', 'brca2', 'brcax', 'other']:\n", " return 0 # Non-BRCA1 as control\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age to continuous value\"\"\"\n", " # Not used since age data is not available, but function is defined for interface\n", " if isinstance(value, str) and ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " try:\n", " return float(value)\n", " except ValueError:\n", " pass\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender to binary value\"\"\"\n", " # Not used since gender data is not available, but function is defined for interface\n", " if isinstance(value, str) and ':' in value:\n", " value = value.split(':', 1)[1].strip().lower()\n", " if value in ['female', 'f']:\n", " return 0\n", " elif value in ['male', 'm']:\n", " return 1\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", " # Assuming clinical_data is available from a previous step\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 clinical data:\")\n", " print(preview)\n", " \n", " # Save to CSV\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " selected_clinical_df.to_csv(out_clinical_data_file, index=False)\n" ] }, { "cell_type": "markdown", "id": "25b01eae", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "456c3d94", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:49:05.539550Z", "iopub.status.busy": "2025-03-25T03:49:05.539441Z", "iopub.status.idle": "2025-03-25T03:49:06.104949Z", "shell.execute_reply": "2025-03-25T03:49:06.104488Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First 20 gene/probe identifiers:\n", "Index(['31', '32', '33', '34', '53', '55', '56', '61', '64', '66', '75', '77',\n", " '79', '80', '89', '91', '93', '95', '96', '97'],\n", " dtype='object', name='ID')\n", "\n", "Gene expression data shape: (10377, 577)\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": "fdb3c1bf", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "0e497215", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:49:06.106234Z", "iopub.status.busy": "2025-03-25T03:49:06.106110Z", "iopub.status.idle": "2025-03-25T03:49:06.108177Z", "shell.execute_reply": "2025-03-25T03:49:06.107822Z" } }, "outputs": [], "source": [ "# These identifiers appear to be numeric values rather than standard gene symbols\n", "# Human gene symbols typically follow a standardized nomenclature (e.g., TP53, BRCA1, etc.)\n", "# The numeric identifiers shown (31, 32, 33, etc.) are likely probe IDs or other platform-specific identifiers\n", "# that need to be mapped to proper gene symbols for meaningful biological interpretation\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "936a83e5", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "d15bf274", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:49:06.109209Z", "iopub.status.busy": "2025-03-25T03:49:06.109100Z", "iopub.status.idle": "2025-03-25T03:49:13.339729Z", "shell.execute_reply": "2025-03-25T03:49:13.339221Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Unicode decoding error: 'utf-8' codec can't decode byte 0xc9 in position 6449: invalid continuation byte\n", "Trying alternative approach...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview (alternative method):\n", "{'ID': ['1', '2', '3', '4', '5'], 'CLONEID': ['H200021335', 'H200008181', 'H300008355', 'H300007697', 'H300002929'], 'SPOT_ID': ['Operon oligo: H200021335', nan, nan, 'Operon oligo: H300007697', 'TMEM31'], 'SPOT_ID_DESCRIPTION': [nan, nan, nan, nan, nan], 'ReporterID': ['H200021335', 'H200008181', 'H300008355', 'H300007697', 'H300002929'], 'OligoSet_genelist': ['V2.1.3', 'V2.1.3', 'V4.0.2', 'V3.0.3', 'V4.0.2'], 'OligoSet_geneSymbol': [nan, 'EIF4A2', 'NP_078953.2', nan, 'TMEM31'], 'OligoSet_description': [nan, 'Eukaryotic initiation factor 4A-II (eIF4A-II) (eIF-4A-II). [Source:Uniprot/SWISSPROT;Acc:Q14240]', nan, nan, nan], 'OligoSet_ensemblGene': [nan, 'ENSG00000156976', 'ENSG00000179299', nan, 'ENSG00000179363'], 'OligoSet_ensemblTranscript': [nan, 'ENST00000356531,ENST00000323963', 'ENST00000333934;ENST00000381782;ENST00000316607;ENST00000381780', nan, 'ENST00000319560;ENST00000372615'], 'ARRAY_BLOCK': [1.0, 1.0, 1.0, 1.0, 1.0], 'BLOCK_COLUMN': [1.0, 2.0, 3.0, 4.0, 5.0], 'BLOCK_ROW': [1.0, 1.0, 1.0, 1.0, 1.0], 'GB_ACC': [nan, nan, nan, nan, nan], 'PT_ACC': [nan, 'Q14240', 'NP_078953.2', nan, nan]}\n" ] } ], "source": [ "# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n", "try:\n", " # Use the correct variable name from previous steps\n", " gene_annotation = get_gene_annotation(soft_file_path)\n", " \n", " # 2. Preview the gene annotation dataframe\n", " print(\"Gene annotation preview:\")\n", " print(preview_df(gene_annotation))\n", " \n", "except UnicodeDecodeError as e:\n", " print(f\"Unicode decoding error: {e}\")\n", " print(\"Trying alternative approach...\")\n", " \n", " # Read the file with Latin-1 encoding which is more permissive\n", " import gzip\n", " import pandas as pd\n", " \n", " # Manually read the file line by line with error handling\n", " data_lines = []\n", " with gzip.open(soft_file_path, 'rb') as f:\n", " for line in f:\n", " # Skip lines starting with prefixes we want to filter out\n", " line_str = line.decode('latin-1')\n", " if not line_str.startswith('^') and not line_str.startswith('!') and not line_str.startswith('#'):\n", " data_lines.append(line_str)\n", " \n", " # Create dataframe from collected lines\n", " if data_lines:\n", " gene_data_str = '\\n'.join(data_lines)\n", " gene_annotation = pd.read_csv(pd.io.common.StringIO(gene_data_str), sep='\\t', low_memory=False)\n", " print(\"Gene annotation preview (alternative method):\")\n", " print(preview_df(gene_annotation))\n", " else:\n", " print(\"No valid gene annotation data found after filtering.\")\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" ] }, { "cell_type": "markdown", "id": "6641b05f", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "dfd56805", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:49:13.340973Z", "iopub.status.busy": "2025-03-25T03:49:13.340853Z", "iopub.status.idle": "2025-03-25T03:49:13.895234Z", "shell.execute_reply": "2025-03-25T03:49:13.894541Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ID column sample: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']\n", "Gene symbol column sample: [nan, 'EIF4A2', 'NP_078953.2', nan, 'TMEM31', 'FAM79B', nan, nan, nan, nan]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Mapping data shape: (32962, 2)\n", "Mapping data preview:\n", "{'ID': ['2', '3', '5', '6', '12'], 'Gene': ['EIF4A2', 'NP_078953.2', 'TMEM31', 'FAM79B', 'Q8TB43_HUMAN']}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene expression data shape after mapping: (6991, 577)\n", "First 10 gene symbols:\n", "['A4GALT', 'AAAS', 'AACS', 'AADACL1', 'AADACL2', 'AADAT', 'AAMP', 'AARS', 'AASDHPPT', 'AATF']\n" ] } ], "source": [ "# 1. Identify columns for gene IDs and gene symbols in annotation data\n", "# From the preview, 'ID' column seems to contain numeric IDs matching the gene data\n", "# 'OligoSet_geneSymbol' appears to contain gene symbols where available\n", "\n", "# Print some samples to verify our mapping choices\n", "print(\"ID column sample:\", gene_annotation['ID'].head(10).tolist())\n", "print(\"Gene symbol column sample:\", gene_annotation['OligoSet_geneSymbol'].head(10).tolist())\n", "\n", "# 2. Get gene mapping dataframe\n", "mapping_data = get_gene_mapping(\n", " annotation=gene_annotation,\n", " prob_col='ID',\n", " gene_col='OligoSet_geneSymbol'\n", ")\n", "\n", "print(\"\\nMapping data shape:\", mapping_data.shape)\n", "print(\"Mapping data preview:\")\n", "print(preview_df(mapping_data))\n", "\n", "# 3. Apply gene mapping to convert probe-level measurements to gene expression data\n", "gene_data = apply_gene_mapping(gene_data, mapping_data)\n", "\n", "print(\"\\nGene expression data shape after mapping:\", gene_data.shape)\n", "print(\"First 10 gene symbols:\")\n", "print(gene_data.index[:10].tolist())\n" ] }, { "cell_type": "markdown", "id": "3229d41c", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "bca35301", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:49:13.897052Z", "iopub.status.busy": "2025-03-25T03:49:13.896922Z", "iopub.status.idle": "2025-03-25T03:49:21.348799Z", "shell.execute_reply": "2025-03-25T03:49:21.348179Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data shape: (6886, 577)\n", "First few normalized gene symbols: ['A4GALT', 'AAAS', 'AACS', 'AADACL2', 'AADAT', 'AAMP', 'AARS1', 'AASDHPPT', 'AATF', 'ABAT']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Retinoblastoma/gene_data/GSE25307.csv\n", "Clinical data shape before preparation: (1, 577)\n", "Clinical data prepared for linking:\n", "{'GSM551608': [0.0], 'GSM551609': [0.0], 'GSM551610': [0.0], 'GSM551611': [0.0], 'GSM551612': [0.0], 'GSM551613': [0.0], 'GSM551614': [0.0], 'GSM551615': [0.0], 'GSM551616': [0.0], 'GSM551617': [1.0], 'GSM551618': [0.0], 'GSM551619': [0.0], 'GSM551620': [0.0], 'GSM551621': [0.0], 'GSM551622': [0.0], 'GSM551623': [0.0], 'GSM551624': [0.0], 'GSM551625': [0.0], 'GSM551626': [0.0], 'GSM551627': [0.0], 'GSM551628': [0.0], 'GSM551629': [0.0], 'GSM551630': [0.0], 'GSM551631': [1.0], 'GSM551632': [0.0], 'GSM551633': [0.0], 'GSM551634': [0.0], 'GSM551635': [0.0], 'GSM551636': [0.0], 'GSM551637': [0.0], 'GSM551638': [0.0], 'GSM551639': [0.0], 'GSM551640': [0.0], 'GSM551641': [0.0], 'GSM551642': [0.0], 'GSM551643': [0.0], 'GSM551644': [0.0], 'GSM551645': [0.0], 'GSM551646': [0.0], 'GSM551647': [0.0], 'GSM551648': [0.0], 'GSM551649': [1.0], 'GSM551650': [0.0], 'GSM551651': [0.0], 'GSM551652': [0.0], 'GSM551653': [0.0], 'GSM551654': [0.0], 'GSM551655': [0.0], 'GSM551656': [0.0], 'GSM551657': [0.0], 'GSM551658': [0.0], 'GSM551659': [0.0], 'GSM551660': [0.0], 'GSM551661': [0.0], 'GSM551662': [0.0], 'GSM551663': [0.0], 'GSM551664': [0.0], 'GSM551665': [0.0], 'GSM551666': [0.0], 'GSM551667': [0.0], 'GSM551668': [0.0], 'GSM551669': [0.0], 'GSM551670': [1.0], 'GSM551671': [0.0], 'GSM551672': [0.0], 'GSM551673': [0.0], 'GSM551674': [0.0], 'GSM551675': [0.0], 'GSM551676': [0.0], 'GSM551677': [0.0], 'GSM551678': [0.0], 'GSM551679': [0.0], 'GSM551680': [0.0], 'GSM551681': [0.0], 'GSM551682': [0.0], 'GSM551683': [0.0], 'GSM551684': [0.0], 'GSM551685': [0.0], 'GSM551686': [0.0], 'GSM551687': [0.0], 'GSM551688': [0.0], 'GSM551689': [0.0], 'GSM551690': [0.0], 'GSM551691': [0.0], 'GSM551692': [0.0], 'GSM551693': [0.0], 'GSM551694': [0.0], 'GSM551695': [0.0], 'GSM551696': [0.0], 'GSM551697': [0.0], 'GSM551698': [0.0], 'GSM551699': [0.0], 'GSM551700': [0.0], 'GSM551701': [0.0], 'GSM551702': [0.0], 'GSM551703': [0.0], 'GSM551704': [0.0], 'GSM551705': [0.0], 'GSM551706': [0.0], 'GSM551707': [0.0], 'GSM551708': [0.0], 'GSM551709': [0.0], 'GSM551710': [0.0], 'GSM551711': [0.0], 'GSM551712': [0.0], 'GSM551713': [0.0], 'GSM551714': [0.0], 'GSM551715': [0.0], 'GSM551716': [0.0], 'GSM551717': [0.0], 'GSM551718': [0.0], 'GSM551719': [0.0], 'GSM551720': [0.0], 'GSM551721': [0.0], 'GSM551722': [0.0], 'GSM551723': [0.0], 'GSM551724': [0.0], 'GSM551725': [0.0], 'GSM551726': [0.0], 'GSM551727': [0.0], 'GSM551728': [0.0], 'GSM551729': [0.0], 'GSM551730': [0.0], 'GSM551731': [0.0], 'GSM551732': [0.0], 'GSM551733': [0.0], 'GSM551734': [0.0], 'GSM551735': [0.0], 'GSM551736': [0.0], 'GSM551737': [0.0], 'GSM551738': [0.0], 'GSM551739': [0.0], 'GSM551740': [0.0], 'GSM551741': [0.0], 'GSM551742': [0.0], 'GSM551743': [0.0], 'GSM551744': [0.0], 'GSM551745': [0.0], 'GSM551746': [0.0], 'GSM551747': [0.0], 'GSM551748': [0.0], 'GSM551749': [0.0], 'GSM551750': [0.0], 'GSM551751': [0.0], 'GSM551752': [0.0], 'GSM551753': [0.0], 'GSM551754': [0.0], 'GSM551755': [0.0], 'GSM551756': [0.0], 'GSM551757': [0.0], 'GSM551758': [0.0], 'GSM551759': [0.0], 'GSM551760': [0.0], 'GSM551761': [0.0], 'GSM551762': [0.0], 'GSM551763': [0.0], 'GSM551764': [0.0], 'GSM551765': [0.0], 'GSM551766': [0.0], 'GSM551767': [0.0], 'GSM551768': [0.0], 'GSM551769': [0.0], 'GSM551770': [0.0], 'GSM551771': [0.0], 'GSM551772': [0.0], 'GSM551773': [0.0], 'GSM551774': [0.0], 'GSM551775': [0.0], 'GSM551776': [0.0], 'GSM551777': [1.0], 'GSM551778': [0.0], 'GSM551779': [0.0], 'GSM551780': [0.0], 'GSM551781': [0.0], 'GSM551782': [0.0], 'GSM551783': [0.0], 'GSM551784': [0.0], 'GSM551785': [0.0], 'GSM551786': [0.0], 'GSM551787': [0.0], 'GSM551788': [0.0], 'GSM551789': [0.0], 'GSM551790': [0.0], 'GSM551791': [0.0], 'GSM551792': [0.0], 'GSM551793': [0.0], 'GSM551794': [0.0], 'GSM551795': [0.0], 'GSM551796': [0.0], 'GSM551797': [0.0], 'GSM551798': [0.0], 'GSM551799': [0.0], 'GSM551800': [0.0], 'GSM551801': [0.0], 'GSM551802': [0.0], 'GSM551803': [0.0], 'GSM551804': [0.0], 'GSM551805': [0.0], 'GSM551806': [0.0], 'GSM551807': [0.0]}\n", "Linked data shape: (577, 6887)\n", "Columns in linked data: ['Retinoblastoma', 'A4GALT', 'AAAS', 'AACS', 'AADACL2']...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data shape after handling missing values: (359, 6887)\n", "For the feature 'Retinoblastoma', the least common label is '1.0' with 21 occurrences. This represents 5.85% of the dataset.\n", "The distribution of the feature 'Retinoblastoma' in this dataset is fine.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Retinoblastoma/GSE25307.csv\n" ] } ], "source": [ "# 1. Normalize gene symbols in the obtained gene expression data\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Normalized gene data shape: {normalized_gene_data.shape}\")\n", "print(f\"First few normalized gene symbols: {list(normalized_gene_data.index[:10])}\")\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. Load the previously extracted clinical data and prepare it for linking\n", "try:\n", " # Load the clinical data we saved in Step 2\n", " clinical_data_df = pd.read_csv(out_clinical_data_file)\n", " print(f\"Clinical data shape before preparation: {clinical_data_df.shape}\")\n", " \n", " # Create a properly formatted clinical dataframe with the trait as a row, not a column\n", " # This matches what geo_link_clinical_genetic_data expects\n", " sample_ids = clinical_data_df.columns.tolist()\n", " trait_values = clinical_data_df.values[0].tolist() # Get values from first row\n", " \n", " # Create a new DataFrame with the right format (samples as columns, traits as rows)\n", " formatted_clinical_df = pd.DataFrame([trait_values], \n", " index=[trait], \n", " columns=sample_ids)\n", " \n", " print(\"Clinical data prepared for linking:\")\n", " print(preview_df(formatted_clinical_df))\n", " \n", " # Link clinical and genetic data\n", " linked_data = geo_link_clinical_genetic_data(formatted_clinical_df, normalized_gene_data)\n", " print(f\"Linked data shape: {linked_data.shape}\")\n", " \n", " # Print the columns to verify trait is present\n", " print(f\"Columns in linked data: {linked_data.columns.tolist()[:5]}...\")\n", " \n", " # 3. Handle missing values in the linked data\n", " linked_data = handle_missing_values(linked_data, trait)\n", " print(f\"Linked data shape after handling missing values: {linked_data.shape}\")\n", " \n", " # 4. Check if trait and demographic features are biased\n", " is_trait_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", " \n", " # 5. Validate the quality of the dataset and save metadata\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, # We have trait data (BRCA1 status)\n", " is_biased=is_trait_biased,\n", " df=linked_data,\n", " note=\"Dataset contains gene expression data from breast cancer samples with BRCA1 mutation status as the marker for Retinoblastoma gene targeting.\"\n", " )\n", " \n", " # 6. Save linked data if usable\n", " if is_usable:\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " linked_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", " else:\n", " print(f\"Data quality check failed. The dataset is not suitable for association studies.\")\n", " \n", "except Exception as e:\n", " print(f\"Error in data linking: {str(e)}\")\n", " print(f\"Exception type: {type(e)}\")\n", " # Provide more detail about the error\n", " import traceback\n", " traceback.print_exc()\n", " \n", " # Fallback plan if there's an issue with the clinical data\n", " # Create a minimal DataFrame with sample IDs from gene data\n", " minimal_df = pd.DataFrame(index=normalized_gene_data.columns)\n", " minimal_df[trait] = np.nan # Add trait column with NaN values\n", " \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, \n", " is_biased=True, \n", " df=minimal_df,\n", " note=\"Error occurred during data processing: \" + str(e)\n", " )\n", " print(f\"Data quality check result: {'Usable' if is_usable else 'Not usable'}\")" ] } ], "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 }