{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "7d07d8fb", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:41:43.906631Z", "iopub.status.busy": "2025-03-25T07:41:43.906402Z", "iopub.status.idle": "2025-03-25T07:41:44.075862Z", "shell.execute_reply": "2025-03-25T07:41:44.075407Z" } }, "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 = \"Lung_Cancer\"\n", "cohort = \"GSE244123\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Lung_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Lung_Cancer/GSE244123\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Lung_Cancer/GSE244123.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Lung_Cancer/gene_data/GSE244123.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Lung_Cancer/clinical_data/GSE244123.csv\"\n", "json_path = \"../../output/preprocess/Lung_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "236983ee", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "0c478cb3", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:41:44.077300Z", "iopub.status.busy": "2025-03-25T07:41:44.077151Z", "iopub.status.idle": "2025-03-25T07:41:44.124113Z", "shell.execute_reply": "2025-03-25T07:41:44.123711Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Olfactory neuroblastoma mimics molecular subtypes and lineage trajectories of small cell lung cancer\"\n", "!Series_summary\t\"This SuperSeries is composed of the SubSeries listed below.\"\n", "!Series_overall_design\t\"Refer to individual Series\"\n", "Sample Characteristics Dictionary:\n", "{0: ['tissue: olfactory neuroblastoma'], 1: [nan, 'grade: II', 'grade: III', 'grade: normal', 'grade: IV'], 2: [nan, 'Stage: C', 'Stage: A', 'Stage: D', 'Stage: na'], 3: [nan, 'location: brain', 'location: nasal cavity', 'location: Cribriform', 'location: cervical LN'], 4: [nan, 'Sex: F', 'Sex: M'], 5: [nan, 'age: 63', 'age: 69', 'age: 42', 'age: 43', 'age: 76', 'age: 73', 'age: 44', 'age: 54', 'age: 67', 'age: 48']}\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": "5b966be6", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "b71dc901", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:41:44.125337Z", "iopub.status.busy": "2025-03-25T07:41:44.125224Z", "iopub.status.idle": "2025-03-25T07:41:44.141230Z", "shell.execute_reply": "2025-03-25T07:41:44.140844Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of clinical features:\n", "{'GSM7807444': [nan, nan, nan], 'GSM7807445': [1.0, 63.0, 0.0], 'GSM7807446': [1.0, 63.0, 0.0], 'GSM7807447': [1.0, 63.0, 0.0], 'GSM7807448': [1.0, 63.0, 0.0], 'GSM7807449': [1.0, 63.0, 0.0], 'GSM7807450': [1.0, 63.0, 0.0], 'GSM7807451': [1.0, 63.0, 0.0], 'GSM7807452': [1.0, 63.0, 0.0], 'GSM7807453': [1.0, 63.0, 0.0], 'GSM7807454': [1.0, 69.0, 0.0], 'GSM7807455': [1.0, 69.0, 0.0], 'GSM7807456': [1.0, 69.0, 0.0], 'GSM7807457': [1.0, 69.0, 0.0], 'GSM7807458': [1.0, 42.0, 1.0], 'GSM7807459': [1.0, 42.0, 1.0], 'GSM7807460': [1.0, 42.0, 1.0], 'GSM7807461': [1.0, 43.0, 0.0], 'GSM7807462': [1.0, 43.0, 0.0], 'GSM7807463': [1.0, 43.0, 0.0], 'GSM7807464': [1.0, 43.0, 0.0], 'GSM7807465': [1.0, 43.0, 0.0], 'GSM7807466': [1.0, 43.0, 0.0], 'GSM7807467': [1.0, 76.0, 1.0], 'GSM7807468': [1.0, 76.0, 1.0], 'GSM7807469': [1.0, 76.0, 1.0], 'GSM7807470': [1.0, 76.0, 1.0], 'GSM7807471': [1.0, 76.0, 1.0], 'GSM7807472': [1.0, 76.0, 1.0], 'GSM7807473': [1.0, 76.0, 1.0], 'GSM7807474': [1.0, 76.0, 1.0], 'GSM7807475': [1.0, 76.0, 1.0], 'GSM7807476': [1.0, 76.0, 1.0], 'GSM7807477': [1.0, 76.0, 1.0], 'GSM7807478': [1.0, 73.0, 1.0], 'GSM7807479': [1.0, 73.0, 1.0], 'GSM7807480': [1.0, 73.0, 1.0], 'GSM7807481': [1.0, 73.0, 1.0], 'GSM7807482': [1.0, 44.0, 1.0], 'GSM7807483': [1.0, 44.0, 1.0], 'GSM7807484': [1.0, 44.0, 1.0], 'GSM7807485': [1.0, 44.0, 1.0], 'GSM7807486': [1.0, 44.0, 1.0], 'GSM7807487': [1.0, 44.0, 1.0], 'GSM7807488': [1.0, 44.0, 1.0], 'GSM7807489': [1.0, 44.0, 1.0], 'GSM7807490': [1.0, 44.0, 1.0], 'GSM7807491': [1.0, 44.0, 1.0], 'GSM7807492': [1.0, 44.0, 1.0], 'GSM7807493': [1.0, 44.0, 1.0], 'GSM7807494': [1.0, 44.0, 1.0], 'GSM7807495': [1.0, 54.0, 0.0], 'GSM7807496': [1.0, 54.0, 0.0], 'GSM7807497': [1.0, 54.0, 0.0], 'GSM7807498': [1.0, 54.0, 0.0], 'GSM7807499': [1.0, 54.0, 0.0], 'GSM7807500': [1.0, 54.0, 0.0], 'GSM7807501': [0.0, 42.0, 0.0], 'GSM7807502': [0.0, 42.0, 0.0], 'GSM7807503': [0.0, 42.0, 0.0], 'GSM7807504': [0.0, 42.0, 0.0], 'GSM7807505': [0.0, 42.0, 0.0], 'GSM7807506': [1.0, 67.0, 1.0], 'GSM7807507': [1.0, 67.0, 1.0], 'GSM7807508': [1.0, 67.0, 1.0], 'GSM7807509': [1.0, 67.0, 1.0], 'GSM7807510': [1.0, 67.0, 1.0], 'GSM7807511': [1.0, 67.0, 1.0], 'GSM7807512': [1.0, 69.0, 0.0], 'GSM7807513': [1.0, 69.0, 0.0], 'GSM7807514': [1.0, 69.0, 0.0], 'GSM7807515': [1.0, 69.0, 0.0], 'GSM7807516': [1.0, 69.0, 0.0], 'GSM7807517': [1.0, 69.0, 0.0], 'GSM7807518': [1.0, 69.0, 0.0], 'GSM7807519': [1.0, 69.0, 0.0], 'GSM7807520': [1.0, 69.0, 0.0], 'GSM7807521': [1.0, 69.0, 0.0], 'GSM7807522': [1.0, 69.0, 0.0], 'GSM7807523': [1.0, 69.0, 0.0], 'GSM7807524': [1.0, 69.0, 0.0], 'GSM7807525': [1.0, 69.0, 0.0], 'GSM7807526': [1.0, 69.0, 0.0], 'GSM7807527': [1.0, 69.0, 0.0], 'GSM7807528': [1.0, 48.0, 1.0], 'GSM7807529': [1.0, 48.0, 1.0], 'GSM7807530': [1.0, 48.0, 1.0], 'GSM7807531': [1.0, 48.0, 1.0], 'GSM7807532': [1.0, 48.0, 1.0], 'GSM7807533': [1.0, 48.0, 1.0], 'GSM7807534': [1.0, 48.0, 1.0], 'GSM7807535': [1.0, 48.0, 1.0], 'GSM7807536': [1.0, 48.0, 1.0], 'GSM7807537': [1.0, 48.0, 1.0], 'GSM7807538': [1.0, 48.0, 1.0], 'GSM7807539': [1.0, 48.0, 1.0]}\n", "Clinical data saved to ../../output/preprocess/Lung_Cancer/clinical_data/GSE244123.csv\n" ] } ], "source": [ "# Analyze the dataset\n", "\n", "# 1. Gene Expression Data Availability\n", "# This dataset appears to be related to olfactory neuroblastoma and lung cancer\n", "# Based on the title and characteristics, it likely contains gene expression data\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "\n", "# 2.1 Trait identification - Lung Cancer\n", "# Looking at sample characteristics, there's information about tumor/cancer grades (key 1)\n", "# We can infer cancer presence from the 'grade' field\n", "trait_row = 1\n", "\n", "# Function to convert trait data\n", "def convert_trait(value):\n", " if pd.isna(value):\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Normal samples considered as control (0), all cancer grades as cases (1)\n", " if 'normal' in value.lower():\n", " return 0\n", " elif any(grade in value.lower() for grade in ['i', 'ii', 'iii', 'iv']):\n", " return 1\n", " else:\n", " return None\n", "\n", "# 2.2 Age data is available in key 5\n", "age_row = 5\n", "\n", "def convert_age(value):\n", " if pd.isna(value):\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " try:\n", " # Convert to float for continuous age\n", " return float(value)\n", " except:\n", " return None\n", "\n", "# 2.3 Gender data is available in key 4\n", "gender_row = 4\n", "\n", "def convert_gender(value):\n", " if pd.isna(value):\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Convert to binary: female=0, male=1\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 - Initial Filtering\n", "# Determine trait data availability\n", "is_trait_available = trait_row is not None\n", "\n", "# Perform initial validation and save cohort info\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 (if trait_row is not None)\n", "if trait_row is not None:\n", " # Assuming clinical_data is available from a previous step\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 clinical features\n", " preview = preview_df(clinical_features)\n", " print(\"Preview of clinical features:\")\n", " print(preview)\n", " \n", " # Save clinical data\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": "b379e939", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "b9f101d8", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:41:44.142401Z", "iopub.status.busy": "2025-03-25T07:41:44.142290Z", "iopub.status.idle": "2025-03-25T07:41:44.218919Z", "shell.execute_reply": "2025-03-25T07:41:44.218458Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['A1BG', 'A1CF', 'A2M', 'A2ML1', 'A3GALT2', 'A4GALT', 'A4GNT', 'AAAS',\n", " 'AACS', 'AADAC', 'AADACL2', 'AADACL3', 'AADACL4', 'AADAT', 'AAGAB',\n", " 'AAK1', 'AAMDC', 'AAMP', 'AANAT', 'AAR2'],\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": "76cd9b2e", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "abe8fef4", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:41:44.220277Z", "iopub.status.busy": "2025-03-25T07:41:44.220153Z", "iopub.status.idle": "2025-03-25T07:41:44.222335Z", "shell.execute_reply": "2025-03-25T07:41:44.221952Z" } }, "outputs": [], "source": [ "# These are standard human gene symbols that don't require mapping\n", "# A1BG = Alpha-1-B Glycoprotein\n", "# A1CF = APOBEC1 Complementation Factor\n", "# A2M = Alpha-2-Macroglobulin\n", "# etc.\n", "\n", "requires_gene_mapping = False\n" ] }, { "cell_type": "markdown", "id": "ddc84bd0", "metadata": {}, "source": [ "### Step 5: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 6, "id": "708c90c3", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:41:44.223630Z", "iopub.status.busy": "2025-03-25T07:41:44.223519Z", "iopub.status.idle": "2025-03-25T07:41:55.387435Z", "shell.execute_reply": "2025-03-25T07:41:55.386583Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Shape of normalized gene data: (18669, 96)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Saved normalized gene data to ../../output/preprocess/Lung_Cancer/gene_data/GSE244123.csv\n", "Shape of linked data: (96, 18672)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Shape of linked data after handling missing values: (95, 18672)\n", "For the feature 'Lung_Cancer', the least common label is '0.0' with 5 occurrences. This represents 5.26% of the dataset.\n", "The distribution of the feature 'Lung_Cancer' in this dataset is fine.\n", "\n", "Quartiles for 'Age':\n", " 25%: 44.0\n", " 50% (Median): 63.0\n", " 75%: 69.0\n", "Min: 42.0\n", "Max: 76.0\n", "The distribution of the feature 'Age' in this dataset is fine.\n", "\n", "For the feature 'Gender', the least common label is '0.0' with 46 occurrences. This represents 48.42% of the dataset.\n", "The distribution of the feature 'Gender' in this dataset is fine.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Saved processed linked data to ../../output/preprocess/Lung_Cancer/GSE244123.csv\n" ] } ], "source": [ "# 1. Normalize the obtained gene data with the 'normalize_gene_symbols_in_index' function from the library.\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Shape of normalized gene data: {normalized_gene_data.shape}\")\n", "# Save to the gene data file\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\"Saved normalized gene data to {out_gene_data_file}\")\n", "\n", "# 2. Link the clinical and genetic data with the 'geo_link_clinical_genetic_data' function from the library.\n", "# Need to load the saved clinical data\n", "clinical_features = pd.read_csv(out_clinical_data_file, index_col=0)\n", "linked_data = geo_link_clinical_genetic_data(clinical_features, normalized_gene_data)\n", "print(f\"Shape of linked data: {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 of linked data after handling missing values: {linked_data.shape}\")\n", "\n", "# 4. Determine whether the trait and some demographic features are severely biased, and remove biased features.\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 gene expression from olfactory neuroblastoma patients relevant to lung cancer research\"\n", ")\n", "\n", "# 6. If the linked data is usable, save it as a CSV file to 'out_data_file'.\n", "if is_usable:\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " unbiased_linked_data.to_csv(out_data_file)\n", " print(f\"Saved processed linked data to {out_data_file}\")\n", "else:\n", " print(\"Dataset validation failed. 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 }