{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "f80836bc", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:56:31.548379Z", "iopub.status.busy": "2025-03-25T06:56:31.548269Z", "iopub.status.idle": "2025-03-25T06:56:31.712506Z", "shell.execute_reply": "2025-03-25T06:56:31.712136Z" } }, "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 = \"Bladder_Cancer\"\n", "cohort = \"GSE145261\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Bladder_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Bladder_Cancer/GSE145261\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Bladder_Cancer/GSE145261.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Bladder_Cancer/gene_data/GSE145261.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Bladder_Cancer/clinical_data/GSE145261.csv\"\n", "json_path = \"../../output/preprocess/Bladder_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "25dc6cb3", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "4b5a561a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:56:31.713956Z", "iopub.status.busy": "2025-03-25T06:56:31.713817Z", "iopub.status.idle": "2025-03-25T06:56:31.815053Z", "shell.execute_reply": "2025-03-25T06:56:31.814728Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Urothelial-to-Neural Lineage Plasticity Drives Progression to Small Cell Bladder Cancer\"\n", "!Series_summary\t\"This SuperSeries is composed of the SubSeries listed below.\"\n", "!Series_overall_design\t\"Small cell carcinoma (SCC) of the bladder displays a high propensity for distant metastasis and is associated with short survival. We report a comprehensive molecular analysis of 34 cases of SCC and 84 cases of conventional urothelial carcinoma (UC)\"\n", "Sample Characteristics Dictionary:\n", "{0: ['subject age: 72 years', 'subject age: 76 years', 'subject age: 79 years', 'subject age: 60 years', 'subject age: 65 years', 'subject age: 41 years', 'subject age: 67 years', 'subject age: 71 years', 'subject age: 57 years', 'subject age: 34 years', 'subject age: 62 years', 'subject age: 90 years', 'subject age: 58 years'], 1: ['subject gender: male', 'subject gender: female'], 2: ['tissue: bladder'], 3: ['tissue type: small cell carinoma (SCC)']}\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": "da4a235f", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "d545fee7", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:56:31.816154Z", "iopub.status.busy": "2025-03-25T06:56:31.816042Z", "iopub.status.idle": "2025-03-25T06:56:31.837459Z", "shell.execute_reply": "2025-03-25T06:56:31.837159Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical data preview:\n", "{'GSM4310302': [1.0, 72.0, 1.0], 'GSM4310303': [1.0, 76.0, 1.0], 'GSM4310304': [1.0, 72.0, 1.0], 'GSM4310305': [1.0, 79.0, 1.0], 'GSM4310306': [1.0, 60.0, 1.0], 'GSM4310307': [1.0, 65.0, 1.0], 'GSM4310308': [1.0, 41.0, 1.0], 'GSM4310309': [1.0, 76.0, 0.0], 'GSM4310310': [1.0, 76.0, 0.0], 'GSM4310311': [1.0, 67.0, 1.0], 'GSM4310312': [1.0, 71.0, 1.0], 'GSM4310313': [1.0, 65.0, 1.0], 'GSM4310314': [1.0, 71.0, 1.0], 'GSM4310315': [1.0, 72.0, 1.0], 'GSM4310316': [1.0, 57.0, 1.0], 'GSM4310317': [1.0, 71.0, 1.0], 'GSM4310318': [1.0, 67.0, 1.0], 'GSM4310319': [1.0, 34.0, 1.0], 'GSM4310320': [1.0, 62.0, 1.0], 'GSM4310321': [1.0, 90.0, 0.0], 'GSM4310322': [1.0, 72.0, 1.0], 'GSM4310323': [1.0, 58.0, 1.0]}\n", "Clinical data saved to ../../output/preprocess/Bladder_Cancer/clinical_data/GSE145261.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on background information, this is a study on bladder cancer with molecular analysis,\n", "# likely to contain gene expression data\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# For trait: Based on sample characteristics dict, tissue type is indicated in key 3\n", "trait_row = 3\n", "# For age: Age information is in key 0\n", "age_row = 0\n", "# For gender: Gender information is in key 1\n", "gender_row = 1\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value):\n", " \"\"\"Convert bladder cancer type to binary (0=not SCC, 1=SCC)\"\"\"\n", " if pd.isna(value) or value is None:\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\n", " if 'small cell' in value.lower() or 'scc' in value.lower():\n", " return 1 # SCC bladder cancer\n", " else:\n", " return 0 # Not SCC bladder cancer\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age value to continuous numeric\"\"\"\n", " if pd.isna(value) or value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Extract numeric age value\n", " import re\n", " match = re.search(r'(\\d+)', value)\n", " if match:\n", " return int(match.group(1))\n", " else:\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender to binary (0=female, 1=male)\"\"\"\n", " if pd.isna(value) or value is None:\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\n", " value = value.lower()\n", " if 'female' in value:\n", " return 0\n", " elif 'male' in value:\n", " return 1\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Trait data is available if trait_row is not None\n", "is_trait_available = trait_row is not None\n", "\n", "# Save initial filtering 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\n", "if trait_row is not None:\n", " # Use the library function to extract clinical features\n", " 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 data\n", " print(\"Clinical data preview:\")\n", " print(preview_df(clinical_df))\n", " \n", " # Save the clinical data\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " clinical_df.to_csv(out_clinical_data_file, index=True)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "9ef51899", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "4698d57b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:56:31.838487Z", "iopub.status.busy": "2025-03-25T06:56:31.838382Z", "iopub.status.idle": "2025-03-25T06:56:31.957539Z", "shell.execute_reply": "2025-03-25T06:56:31.957174Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['ILMN_1343291', 'ILMN_1343295', 'ILMN_1651199', 'ILMN_1651209',\n", " 'ILMN_1651210', 'ILMN_1651221', 'ILMN_1651228', 'ILMN_1651229',\n", " 'ILMN_1651230', 'ILMN_1651232', 'ILMN_1651235', 'ILMN_1651236',\n", " 'ILMN_1651237', 'ILMN_1651238', 'ILMN_1651249', 'ILMN_1651253',\n", " 'ILMN_1651254', 'ILMN_1651259', 'ILMN_1651260', 'ILMN_1651262'],\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": "ae538d6b", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "7cb15dc6", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:56:31.958798Z", "iopub.status.busy": "2025-03-25T06:56:31.958675Z", "iopub.status.idle": "2025-03-25T06:56:31.960546Z", "shell.execute_reply": "2025-03-25T06:56:31.960248Z" } }, "outputs": [], "source": [ "# These are Illumina BeadArray identifiers (ILMN_*), not human gene symbols\n", "# They need to be mapped to proper gene symbols for analysis\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "4b169b6f", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "4e4ea583", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:56:31.961662Z", "iopub.status.busy": "2025-03-25T06:56:31.961558Z", "iopub.status.idle": "2025-03-25T06:56:34.603507Z", "shell.execute_reply": "2025-03-25T06:56:34.603139Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['ILMN_1343048', 'ILMN_1343049', 'ILMN_1343050', 'ILMN_1343052', 'ILMN_1343059'], 'Species': [nan, nan, nan, nan, nan], 'Source': [nan, nan, nan, nan, nan], 'Search_Key': [nan, nan, nan, nan, nan], 'Transcript': [nan, nan, nan, nan, nan], 'ILMN_Gene': [nan, nan, nan, nan, nan], 'Source_Reference_ID': [nan, nan, nan, nan, nan], 'RefSeq_ID': [nan, nan, nan, nan, nan], 'Unigene_ID': [nan, nan, nan, nan, nan], 'Entrez_Gene_ID': [nan, nan, nan, nan, nan], 'GI': [nan, nan, nan, nan, nan], 'Accession': [nan, nan, nan, nan, nan], 'Symbol': ['phage_lambda_genome', 'phage_lambda_genome', 'phage_lambda_genome:low', 'phage_lambda_genome:low', 'thrB'], 'Protein_Product': [nan, nan, nan, nan, 'thrB'], 'Probe_Id': [nan, nan, nan, nan, nan], 'Array_Address_Id': [5090180.0, 6510136.0, 7560739.0, 1450438.0, 1240647.0], 'Probe_Type': [nan, nan, nan, nan, nan], 'Probe_Start': [nan, nan, nan, nan, nan], 'SEQUENCE': ['GAATAAAGAACAATCTGCTGATGATCCCTCCGTGGATCTGATTCGTGTAA', 'CCATGTGATACGAGGGCGCGTAGTTTGCATTATCGTTTTTATCGTTTCAA', 'CCGACAGATGTATGTAAGGCCAACGTGCTCAAATCTTCATACAGAAAGAT', 'TCTGTCACTGTCAGGAAAGTGGTAAAACTGCAACTCAATTACTGCAATGC', 'CTTGTGCCTGAGCTGTCAAAAGTAGAGCACGTCGCCGAGATGAAGGGCGC'], 'Chromosome': [nan, nan, nan, nan, nan], 'Probe_Chr_Orientation': [nan, nan, nan, nan, nan], 'Probe_Coordinates': [nan, nan, nan, nan, nan], 'Cytoband': [nan, nan, nan, nan, nan], 'Definition': [nan, nan, nan, nan, nan], 'Ontology_Component': [nan, nan, nan, nan, nan], 'Ontology_Process': [nan, nan, nan, nan, nan], 'Ontology_Function': [nan, nan, nan, nan, nan], 'Synonyms': [nan, nan, nan, nan, nan], 'Obsolete_Probe_Id': [nan, nan, nan, nan, nan], 'GB_ACC': [nan, nan, nan, nan, nan]}\n" ] } ], "source": [ "# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n", "gene_annotation = get_gene_annotation(soft_file)\n", "\n", "# 2. Use the 'preview_df' function from the library to preview the data and print out the results.\n", "print(\"Gene annotation preview:\")\n", "print(preview_df(gene_annotation))\n" ] }, { "cell_type": "markdown", "id": "c3add7e5", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "58050e01", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:56:34.604905Z", "iopub.status.busy": "2025-03-25T06:56:34.604687Z", "iopub.status.idle": "2025-03-25T06:56:34.749805Z", "shell.execute_reply": "2025-03-25T06:56:34.749440Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data after mapping (first 5 rows, 5 columns):\n", " GSM4310302 GSM4310303 GSM4310304 GSM4310305 GSM4310306\n", "Gene \n", "A1BG 18.653255 18.667788 18.862924 18.728951 18.664749\n", "A1CF 27.961683 28.422059 27.961613 27.960573 27.959180\n", "A26C3 27.959176 29.677706 28.149562 28.236884 27.964586\n", "A2BP1 37.291573 38.591834 37.474717 38.672140 41.538859\n", "A2LD1 9.361814 9.418669 9.316703 9.596790 9.376270\n", "Shape after mapping: (21464, 22)\n" ] } ], "source": [ "# 1. Identify the relevant columns in gene_annotation for mapping\n", "# From the previous output:\n", "# - 'ID' is the column with the same identifiers (ILMN_*) as in gene_expression data\n", "# - 'Symbol' contains the gene symbols we need to map to\n", "\n", "# 2. Create a gene mapping dataframe using get_gene_mapping function\n", "# The function extracts and processes these two columns\n", "gene_mapping = get_gene_mapping(gene_annotation, 'ID', 'Symbol')\n", "\n", "# 3. Apply the gene mapping to convert probe-level measurements to gene expression data\n", "# This function handles the many-to-many relationships between probes and genes\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "\n", "# Show a preview of the new gene expression data\n", "print(\"Gene expression data after mapping (first 5 rows, 5 columns):\")\n", "preview_cols = min(5, len(gene_data.columns))\n", "print(gene_data.iloc[:5, :preview_cols])\n", "print(f\"Shape after mapping: {gene_data.shape}\")\n" ] }, { "cell_type": "markdown", "id": "df0d3a86", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "68867eb4", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:56:34.751094Z", "iopub.status.busy": "2025-03-25T06:56:34.750970Z", "iopub.status.idle": "2025-03-25T06:56:41.119679Z", "shell.execute_reply": "2025-03-25T06:56:41.119371Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Bladder_Cancer/gene_data/GSE145261.csv\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Quartiles for 'Bladder_Cancer':\n", " 25%: 1.0\n", " 50% (Median): 1.0\n", " 75%: 1.0\n", "Min: 1.0\n", "Max: 1.0\n", "The distribution of the feature 'Bladder_Cancer' in this dataset is severely biased.\n", "\n", "Quartiles for 'Age':\n", " 25%: 62.75\n", " 50% (Median): 71.0\n", " 75%: 72.0\n", "Min: 34.0\n", "Max: 90.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 3 occurrences. This represents 13.64% of the dataset.\n", "The distribution of the feature 'Gender' in this dataset is fine.\n", "\n", "Data was determined to be unusable and was not saved\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", "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 saved clinical data and link with genetic data\n", "clinical_df = pd.read_csv(out_clinical_data_file, index_col=0)\n", "linked_data = geo_link_clinical_genetic_data(clinical_df, normalized_gene_data)\n", "\n", "# 3. Handle missing values in the linked data\n", "linked_data = handle_missing_values(linked_data, trait)\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(True, cohort, json_path, True, True, is_trait_biased, unbiased_linked_data)\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\"Linked data saved to {out_data_file}\")\n", "else:\n", " print(\"Data was determined to be unusable and was 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 }