{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "c4ec14a0", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:13:34.420767Z", "iopub.status.busy": "2025-03-25T08:13:34.420591Z", "iopub.status.idle": "2025-03-25T08:13:34.589431Z", "shell.execute_reply": "2025-03-25T08:13:34.589081Z" } }, "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 = \"Cervical_Cancer\"\n", "cohort = \"GSE137034\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Cervical_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Cervical_Cancer/GSE137034\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Cervical_Cancer/GSE137034.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Cervical_Cancer/gene_data/GSE137034.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Cervical_Cancer/clinical_data/GSE137034.csv\"\n", "json_path = \"../../output/preprocess/Cervical_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "28452637", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "7d581880", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:13:34.590914Z", "iopub.status.busy": "2025-03-25T08:13:34.590762Z", "iopub.status.idle": "2025-03-25T08:13:34.667527Z", "shell.execute_reply": "2025-03-25T08:13:34.667213Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Chromatin accessibility governs the differential response of cancer and T-cells to arginine starvation\"\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: THP1 cells', 'tissue: Stimulated human CD4 T-cells'], 1: ['treatment: Cells cultured in full RPMI', 'treatment: Cells cultured in RPMI without arginine']}\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": "95249088", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "4a1dca44", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:13:34.668770Z", "iopub.status.busy": "2025-03-25T08:13:34.668516Z", "iopub.status.idle": "2025-03-25T08:13:34.672367Z", "shell.execute_reply": "2025-03-25T08:13:34.672068Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "No relevant cervical cancer trait information found in this dataset.\n" ] } ], "source": [ "# Analysis of the dataset\n", "# 1. Gene Expression Data Availability\n", "# Based on the background information, this appears to be a SuperSeries about chromatin accessibility\n", "# While chromatin accessibility is related to gene regulation, this dataset may not contain direct gene expression data\n", "is_gene_available = False\n", "\n", "# 2. Variable Analysis for trait, age, and gender\n", "# 2.1 Data Availability\n", "# From the sample characteristics, we have:\n", "# - Row 0: tissue type (THP1 cells vs Stimulated human CD4 T-cells)\n", "# - Row 1: treatment condition (full RPMI vs RPMI without arginine)\n", "# \n", "# This dataset appears to be about immune cells and arginine starvation, not directly about cervical cancer.\n", "# None of the available characteristics directly map to cervical cancer status.\n", "trait_row = None # No direct cervical cancer trait information\n", "age_row = None # Age information is not available\n", "gender_row = None # Gender information is not available\n", "\n", "# 2.2 Data Type Conversion Functions\n", "# Since we don't have valid trait information for cervical cancer, we don't need these functions\n", "# but they are defined as None for completeness\n", "convert_trait = None\n", "convert_age = None\n", "convert_gender = None\n", "\n", "# 3. Save Metadata\n", "# Determine if trait data is available based on trait_row\n", "is_trait_available = trait_row is not None\n", "\n", "# Validate and save cohort information\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 skip this substep\n", "if trait_row is not None:\n", " # This block would only execute if trait_row was not None\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 extracted clinical features\n", " print(\"Clinical Features Preview:\")\n", " print(preview_df(selected_clinical_df))\n", " \n", " # Save the clinical data\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", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n", "else:\n", " print(\"No relevant cervical cancer trait information found in this dataset.\")\n" ] }, { "cell_type": "markdown", "id": "9836a114", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "5fc6ce51", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:13:34.673394Z", "iopub.status.busy": "2025-03-25T08:13:34.673288Z", "iopub.status.idle": "2025-03-25T08:13:34.736663Z", "shell.execute_reply": "2025-03-25T08:13:34.736293Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['ILMN_1343291', 'ILMN_1343295', 'ILMN_1651209', 'ILMN_1651228',\n", " 'ILMN_1651229', 'ILMN_1651230', 'ILMN_1651232', 'ILMN_1651236',\n", " 'ILMN_1651238', 'ILMN_1651253', 'ILMN_1651254', 'ILMN_1651259',\n", " 'ILMN_1651260', 'ILMN_1651262', 'ILMN_1651268', 'ILMN_1651278',\n", " 'ILMN_1651281', 'ILMN_1651282', 'ILMN_1651285', 'ILMN_1651286'],\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": "926ee17e", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "4bb3f263", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:13:34.737898Z", "iopub.status.busy": "2025-03-25T08:13:34.737783Z", "iopub.status.idle": "2025-03-25T08:13:34.739685Z", "shell.execute_reply": "2025-03-25T08:13:34.739395Z" } }, "outputs": [], "source": [ "# Based on the gene identifiers observed in the gene expression data, I can see these are\n", "# Illumina probe IDs (starting with ILMN_), not standard human gene symbols.\n", "# These are microarray probe identifiers that need to be mapped to gene symbols for biological interpretation.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "75f16d71", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "1d22fcfb", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:13:34.740849Z", "iopub.status.busy": "2025-03-25T08:13:34.740746Z", "iopub.status.idle": "2025-03-25T08:13:36.504768Z", "shell.execute_reply": "2025-03-25T08:13:36.504377Z" } }, "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": "84e4d61b", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "8e69c7ec", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:13:36.506150Z", "iopub.status.busy": "2025-03-25T08:13:36.506023Z", "iopub.status.idle": "2025-03-25T08:13:36.609112Z", "shell.execute_reply": "2025-03-25T08:13:36.608707Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene mapping dataframe contains 44837 rows.\n", "First few rows of gene mapping dataframe:\n", " ID Gene\n", "0 ILMN_1343048 phage_lambda_genome\n", "1 ILMN_1343049 phage_lambda_genome\n", "2 ILMN_1343050 phage_lambda_genome:low\n", "3 ILMN_1343052 phage_lambda_genome:low\n", "4 ILMN_1343059 thrB\n", "\n", "Gene expression data after mapping contains 19428 rows (genes) and 12 columns (samples).\n", "First few rows of gene expression data:\n", " GSM4066056 GSM4066057 GSM4066058 GSM4066059 GSM4066060 GSM4066061 \\\n", "Gene \n", "A1BG 104.734027 107.031137 106.886337 113.500383 102.537192 107.564250 \n", "A1CF 321.270674 307.527615 328.183334 308.400692 321.742142 309.907990 \n", "A26C3 316.660403 309.286984 312.437777 311.793639 332.664815 318.939242 \n", "A2BP1 412.924163 425.162126 436.675346 431.607999 435.392324 414.417847 \n", "A2LD1 672.827158 649.832542 549.741167 454.964517 548.334108 531.383400 \n", "\n", " GSM4066062 GSM4066063 GSM4066064 GSM4066065 GSM4066066 GSM4066067 \n", "Gene \n", "A1BG 102.604249 110.358317 111.931083 108.178350 105.403362 119.752683 \n", "A1CF 324.954404 313.219361 313.711137 332.812281 336.827117 317.123024 \n", "A26C3 308.638665 298.541888 316.146780 326.388599 315.097036 318.664994 \n", "A2BP1 424.703767 420.752160 424.171614 429.189257 406.857785 416.910436 \n", "A2LD1 125.979400 114.403167 150.408808 133.218425 111.299500 117.343208 \n" ] } ], "source": [ "# 1. Identify the columns in the gene annotation dataframe that contain gene identifiers and gene symbols\n", "# From the preview, the 'ID' column matches the gene expression identifiers (ILMN_*)\n", "# And the 'Symbol' column contains the gene symbols\n", "\n", "# 2. Create the gene mapping dataframe using the get_gene_mapping function\n", "gene_mapping = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol')\n", "\n", "# Print summary of the mapping to verify\n", "print(f\"Gene mapping dataframe contains {len(gene_mapping)} rows.\")\n", "print(\"First few rows of gene mapping dataframe:\")\n", "print(gene_mapping.head())\n", "\n", "# 3. Apply the gene mapping to convert probe-level measurements to gene expression data\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "\n", "# Print summary of the resulting gene expression data\n", "print(f\"\\nGene expression data after mapping contains {len(gene_data)} rows (genes) and {gene_data.shape[1]} columns (samples).\")\n", "print(\"First few rows of gene expression data:\")\n", "print(gene_data.head())\n" ] }, { "cell_type": "markdown", "id": "7c1498f2", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "55e16370", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T08:13:36.610521Z", "iopub.status.busy": "2025-03-25T08:13:36.610401Z", "iopub.status.idle": "2025-03-25T08:13:36.827601Z", "shell.execute_reply": "2025-03-25T08:13:36.827210Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Cervical_Cancer/gene_data/GSE137034.csv\n", "Abnormality detected in the cohort: GSE137034. Preprocessing failed.\n", "The dataset doesn't contain cervical cancer trait information and wasn't saved.\n" ] } ], "source": [ "# Since we determined in Step 2 that this dataset doesn't contain relevant cervical cancer trait information\n", "# we need to finalize with explicit values to satisfy function requirements\n", "\n", "# Save normalized gene data for completeness\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", "# Create a minimal dataframe for consistent API\n", "minimal_df = pd.DataFrame(index=normalized_gene_data.columns)\n", "\n", "# Validate and save with explicit values\n", "is_usable = validate_and_save_cohort_info(\n", " is_final=True, \n", " cohort=cohort, \n", " info_path=json_path, \n", " is_gene_available=False, # As determined in Step 2\n", " is_trait_available=False, # As determined in Step 2\n", " is_biased=False, # Providing an explicit value to satisfy the function requirement\n", " df=minimal_df,\n", " note=\"This dataset contains gene expression data from THP1 cells and stimulated human CD4 T-cells, but doesn't contain cervical cancer information.\"\n", ")\n", "\n", "print(\"The dataset doesn't contain cervical cancer trait information and wasn't 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 }