{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "e97a0c06", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:17:12.823855Z", "iopub.status.busy": "2025-03-25T06:17:12.823686Z", "iopub.status.idle": "2025-03-25T06:17:12.988436Z", "shell.execute_reply": "2025-03-25T06:17:12.988117Z" } }, "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 = \"Acute_Myeloid_Leukemia\"\n", "cohort = \"GSE121431\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Acute_Myeloid_Leukemia\"\n", "in_cohort_dir = \"../../input/GEO/Acute_Myeloid_Leukemia/GSE121431\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Acute_Myeloid_Leukemia/GSE121431.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Acute_Myeloid_Leukemia/gene_data/GSE121431.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Acute_Myeloid_Leukemia/clinical_data/GSE121431.csv\"\n", "json_path = \"../../output/preprocess/Acute_Myeloid_Leukemia/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "6330994e", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "63b9e0c4", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:17:12.989785Z", "iopub.status.busy": "2025-03-25T06:17:12.989650Z", "iopub.status.idle": "2025-03-25T06:17:13.127849Z", "shell.execute_reply": "2025-03-25T06:17:13.127511Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"SY-1365, a covalent, first in-class CDK7 inhibitor for cancer treatment\"\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: ['disease state: Acute Myeloid Leukemia'], 1: ['cell line: AML cell line THP-1'], 2: ['agent: DMSO', 'agent: SY-1365', 'agent: JQ1', 'agent: NVP2', 'agent: FLAVO'], 3: ['time: 2 hours', 'time: 6 hours']}\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": "513cec37", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "e27ef7d7", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:17:13.129208Z", "iopub.status.busy": "2025-03-25T06:17:13.129104Z", "iopub.status.idle": "2025-03-25T06:17:13.135773Z", "shell.execute_reply": "2025-03-25T06:17:13.135504Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical data preview:\n", "{'GSM3430924': [1.0], 'GSM3430925': [1.0], 'GSM3430926': [1.0], 'GSM3430927': [1.0], 'GSM3430928': [1.0], 'GSM3430929': [1.0], 'GSM3430930': [1.0], 'GSM3430931': [1.0], 'GSM3430932': [1.0], 'GSM3430933': [1.0], 'GSM3430934': [1.0], 'GSM3430935': [1.0], 'GSM3430936': [1.0], 'GSM3430937': [1.0], 'GSM3430938': [1.0], 'GSM3430939': [1.0], 'GSM3430940': [1.0], 'GSM3430941': [1.0], 'GSM3430942': [1.0], 'GSM3430943': [1.0], 'GSM3430944': [1.0], 'GSM3430945': [1.0], 'GSM3430946': [1.0], 'GSM3430947': [1.0], 'GSM3430948': [1.0], 'GSM3430949': [1.0], 'GSM3430950': [1.0], 'GSM3430951': [1.0], 'GSM3430952': [1.0], 'GSM3430953': [1.0]}\n", "Clinical data saved to ../../output/preprocess/Acute_Myeloid_Leukemia/clinical_data/GSE121431.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the background information, this dataset likely contains gene expression data\n", "# It's studying inhibitors for cancer treatment, which typically involves gene expression analysis\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# Looking at the sample characteristics dictionary:\n", "# - trait_row = 0 (disease state: Acute Myeloid Leukemia)\n", "# - age_row is None (not available)\n", "# - gender_row is None (not available)\n", "trait_row = 0\n", "age_row = None\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value):\n", " \"\"\"Convert trait value to binary (1 for AML, 0 for control)\"\"\"\n", " if value is None:\n", " return None\n", " # Extract the value after the colon\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " # In this dataset, all samples appear to be AML\n", " if \"Acute Myeloid Leukemia\" in value:\n", " return 1\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age value to continuous\"\"\"\n", " # Not used as age data is not available\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender value to binary (0 for female, 1 for male)\"\"\"\n", " # Not used as gender data is not available\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Initial filtering on the usability of the dataset\n", "# trait_row is not None, so trait data is available\n", "is_trait_available = trait_row is not None\n", "validate_and_save_cohort_info(is_final=False, cohort=cohort, info_path=json_path, \n", " is_gene_available=is_gene_available, \n", " is_trait_available=is_trait_available)\n", "\n", "# 4. Clinical Feature Extraction\n", "# Since trait_row is not None, we extract clinical features\n", "if trait_row is not None:\n", " # Use clinical_data from previous step (assumed to be available)\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 clinical data\n", " preview = preview_df(selected_clinical_df)\n", " print(\"Clinical data preview:\")\n", " print(preview)\n", " \n", " # Save the clinical data 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=True)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "afe17139", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "6560b2b1", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:17:13.137102Z", "iopub.status.busy": "2025-03-25T06:17:13.136996Z", "iopub.status.idle": "2025-03-25T06:17:13.291806Z", "shell.execute_reply": "2025-03-25T06:17:13.291437Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['11715100_at', '11715101_s_at', '11715102_x_at', '11715103_x_at',\n", " '11715104_s_at', '11715105_at', '11715106_x_at', '11715107_s_at',\n", " '11715108_x_at', '11715109_at', '11715110_at', '11715111_s_at',\n", " '11715112_at', '11715113_x_at', '11715114_x_at', '11715115_s_at',\n", " '11715116_s_at', '11715117_x_at', '11715118_s_at', '11715119_s_at'],\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": "077b7ac2", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "7a06e561", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:17:13.293189Z", "iopub.status.busy": "2025-03-25T06:17:13.293078Z", "iopub.status.idle": "2025-03-25T06:17:13.295017Z", "shell.execute_reply": "2025-03-25T06:17:13.294718Z" } }, "outputs": [], "source": [ "# The gene identifiers appear to be Affymetrix probe IDs (format: #########_at, #########_s_at, #########_x_at)\n", "# These are not human gene symbols and need to be mapped to gene symbols\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "3e6e8f67", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "a41e8fb9", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:17:13.296366Z", "iopub.status.busy": "2025-03-25T06:17:13.296266Z", "iopub.status.idle": "2025-03-25T06:17:16.346530Z", "shell.execute_reply": "2025-03-25T06:17:16.346168Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['11715100_at', '11715101_s_at', '11715102_x_at', '11715103_x_at', '11715104_s_at'], 'GeneChip Array': ['Human Genome PrimeView Array', 'Human Genome PrimeView Array', 'Human Genome PrimeView Array', 'Human Genome PrimeView Array', 'Human Genome PrimeView Array'], 'Species Scientific Name': ['Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens'], 'Annotation Date': [40780.0, 40780.0, 40780.0, 40780.0, 40780.0], 'Sequence Type': ['Consensus sequence', 'Consensus sequence', 'Consensus sequence', 'Consensus sequence', 'Consensus sequence'], 'Sequence Source': ['Affymetrix Proprietary Database', 'Affymetrix Proprietary Database', 'Affymetrix Proprietary Database', 'Affymetrix Proprietary Database', 'Affymetrix Proprietary Database'], 'Transcript ID(Array Design)': ['g21264570', 'g21264570', 'g21264570', 'g22748780', 'g30039713'], 'Target Description': ['g21264570 /TID=g21264570 /CNT=1 /FEA=FLmRNA /TIER=FL /STK=0 /DEF=g21264570 /REP_ORG=Homo sapiens', 'g21264570 /TID=g21264570 /CNT=1 /FEA=FLmRNA /TIER=FL /STK=0 /DEF=g21264570 /REP_ORG=Homo sapiens', 'g21264570 /TID=g21264570 /CNT=1 /FEA=FLmRNA /TIER=FL /STK=0 /DEF=g21264570 /REP_ORG=Homo sapiens', 'g22748780 /TID=g22748780 /CNT=1 /FEA=FLmRNA /TIER=FL /STK=0 /DEF=g22748780 /REP_ORG=Homo sapiens', 'g30039713 /TID=g30039713 /CNT=1 /FEA=FLmRNA /TIER=FL /STK=0 /DEF=g30039713 /REP_ORG=Homo sapiens'], 'GB_ACC': [nan, nan, nan, nan, nan], 'GI': [21264570.0, 21264570.0, 21264570.0, 22748780.0, 30039713.0], 'UniGene ID': ['Hs.247813', 'Hs.247813', 'Hs.247813', 'Hs.465643', 'Hs.352515'], 'Genome Version': ['February 2009 (Genome Reference Consortium GRCh37)', 'February 2009 (Genome Reference Consortium GRCh37)', 'February 2009 (Genome Reference Consortium GRCh37)', 'February 2009 (Genome Reference Consortium GRCh37)', 'February 2009 (Genome Reference Consortium GRCh37)'], 'Alignments': ['chr6:26271145-26271612 (-) // 100.0 // p22.2', 'chr6:26271145-26271612 (-) // 100.0 // p22.2', 'chr6:26271145-26271612 (-) // 100.0 // p22.2', 'chr19:4639529-5145579 (+) // 48.53 // p13.3', 'chr17:72920369-72929640 (+) // 100.0 // q25.1'], 'Gene Title': ['histone cluster 1, H3g', 'histone cluster 1, H3g', 'histone cluster 1, H3g', 'tumor necrosis factor, alpha-induced protein 8-like 1', 'otopetrin 2'], 'Gene Symbol': ['HIST1H3G', 'HIST1H3G', 'HIST1H3G', 'TNFAIP8L1', 'OTOP2'], 'Chromosomal Location': ['chr6p21.3', 'chr6p21.3', 'chr6p21.3', 'chr19p13.3', 'chr17q25.1'], 'Unigene Cluster Type': ['full length', 'full length', 'full length', 'full length', 'full length'], 'Ensembl': ['ENSG00000248541', 'ENSG00000248541', 'ENSG00000248541', 'ENSG00000185361', 'ENSG00000183034'], 'Entrez Gene': ['8355', '8355', '8355', '126282', '92736'], 'SwissProt': ['P68431', 'P68431', 'P68431', 'Q8WVP5', 'Q7RTS6'], 'OMIM': ['602815', '602815', '602815', '---', '607827'], 'RefSeq Protein ID': ['NP_003525', 'NP_003525', 'NP_003525', 'NP_001161414 /// NP_689575', 'NP_835454'], 'RefSeq Transcript ID': ['NM_003534', 'NM_003534', 'NM_003534', 'NM_001167942 /// NM_152362', 'NM_178160'], 'SPOT_ID': [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": "31f470c7", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "216c6a4b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:17:16.348275Z", "iopub.status.busy": "2025-03-25T06:17:16.348162Z", "iopub.status.idle": "2025-03-25T06:17:16.532605Z", "shell.execute_reply": "2025-03-25T06:17:16.532234Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mapped gene data preview - first 20 gene symbols:\n", "Index(['A1BG', 'A1CF', 'A2LD1', 'A2M', 'A2ML1', 'A4GALT', 'A4GNT', 'AAA1',\n", " 'AAAS', 'AACS', 'AACSP1', 'AADAC', 'AADACL2', 'AADACL3', 'AADACL4',\n", " 'AADAT', 'AAGAB', 'AAK1', 'AAMP', 'AANAT'],\n", " dtype='object', name='Gene')\n", "Shape of gene expression data: (19534, 30)\n" ] } ], "source": [ "# 1. Identify the columns for gene identifiers and gene symbols\n", "# From the preview, we can see that 'ID' column contains probe IDs matching the gene expression data\n", "# and 'Gene Symbol' column contains the gene symbols we need\n", "# These correspond to the identifiers seen in gene_data.index\n", "\n", "# 2. Get the gene mapping dataframe\n", "prob_col = 'ID'\n", "gene_col = 'Gene Symbol'\n", "mapping_data = get_gene_mapping(gene_annotation, prob_col, gene_col)\n", "\n", "# 3. Apply the gene mapping to convert probe-level measurements to gene expression\n", "gene_data = apply_gene_mapping(gene_data, mapping_data)\n", "\n", "# Preview the result\n", "print(\"Mapped gene data preview - first 20 gene symbols:\")\n", "print(gene_data.index[:20])\n", "print(f\"Shape of gene expression data: {gene_data.shape}\")\n" ] }, { "cell_type": "markdown", "id": "9a258fd0", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "28d6942a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:17:16.534359Z", "iopub.status.busy": "2025-03-25T06:17:16.534251Z", "iopub.status.idle": "2025-03-25T06:17:22.375971Z", "shell.execute_reply": "2025-03-25T06:17:22.375618Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Acute_Myeloid_Leukemia/gene_data/GSE121431.csv\n", "Clinical data loaded from ../../output/preprocess/Acute_Myeloid_Leukemia/clinical_data/GSE121431.csv\n", "Clinical data shape: (1, 30)\n", "Clinical data preview:\n", "{'GSM3430924': [1.0], 'GSM3430925': [1.0], 'GSM3430926': [1.0], 'GSM3430927': [1.0], 'GSM3430928': [1.0], 'GSM3430929': [1.0], 'GSM3430930': [1.0], 'GSM3430931': [1.0], 'GSM3430932': [1.0], 'GSM3430933': [1.0], 'GSM3430934': [1.0], 'GSM3430935': [1.0], 'GSM3430936': [1.0], 'GSM3430937': [1.0], 'GSM3430938': [1.0], 'GSM3430939': [1.0], 'GSM3430940': [1.0], 'GSM3430941': [1.0], 'GSM3430942': [1.0], 'GSM3430943': [1.0], 'GSM3430944': [1.0], 'GSM3430945': [1.0], 'GSM3430946': [1.0], 'GSM3430947': [1.0], 'GSM3430948': [1.0], 'GSM3430949': [1.0], 'GSM3430950': [1.0], 'GSM3430951': [1.0], 'GSM3430952': [1.0], 'GSM3430953': [1.0]}\n", "Linked data shape: (30, 19327)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (30, 19327)\n", "Unique values in trait column: [1.]\n", "Quartiles for 'Acute_Myeloid_Leukemia':\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 'Acute_Myeloid_Leukemia' in this dataset is severely biased.\n", "\n", "A new JSON file was created at: ../../output/preprocess/Acute_Myeloid_Leukemia/cohort_info.json\n", "Dataset not usable due to bias in trait distribution. Data 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 instead of re-extracting it\n", "clinical_df = pd.read_csv(out_clinical_data_file, index_col=0)\n", "print(f\"Clinical data loaded from {out_clinical_data_file}\")\n", "print(f\"Clinical data shape: {clinical_df.shape}\")\n", "print(\"Clinical data preview:\")\n", "print(preview_df(clinical_df))\n", "\n", "# 3. Link the clinical and genetic data\n", "linked_data = geo_link_clinical_genetic_data(clinical_df, normalized_gene_data)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "\n", "# 4. Handle missing values in the linked data\n", "linked_data = handle_missing_values(linked_data, trait)\n", "print(f\"Data shape after handling missing values: {linked_data.shape}\")\n", "\n", "# Verify that the trait column has at least two unique values\n", "unique_trait_values = linked_data[trait].unique()\n", "print(f\"Unique values in trait column: {unique_trait_values}\")\n", "\n", "# 5. Determine whether the trait and some demographic features are severely biased\n", "is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "\n", "# 6. Conduct quality check and save the cohort information\n", "note = \"Dataset contains only AML (Acute Myeloid Leukemia) samples, which may limit certain analyses.\"\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=note\n", ")\n", "\n", "# 7. If the linked data is usable, save it\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\"Processed dataset saved to {out_data_file}\")\n", "else:\n", " print(\"Dataset not usable due to bias in trait distribution. 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 }