{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "b98e8110", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:21:09.081716Z", "iopub.status.busy": "2025-03-25T06:21:09.081617Z", "iopub.status.idle": "2025-03-25T06:21:09.247422Z", "shell.execute_reply": "2025-03-25T06:21:09.247082Z" } }, "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 = \"Adrenocortical_Cancer\"\n", "cohort = \"GSE49278\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Adrenocortical_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Adrenocortical_Cancer/GSE49278\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Adrenocortical_Cancer/GSE49278.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Adrenocortical_Cancer/gene_data/GSE49278.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Adrenocortical_Cancer/clinical_data/GSE49278.csv\"\n", "json_path = \"../../output/preprocess/Adrenocortical_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "04223ae3", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "85d0172f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:21:09.248875Z", "iopub.status.busy": "2025-03-25T06:21:09.248731Z", "iopub.status.idle": "2025-03-25T06:21:09.416153Z", "shell.execute_reply": "2025-03-25T06:21:09.415796Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Expression profiling by array of 44 adrenocortical carcinomas\"\n", "!Series_summary\t\"Gene expression profiles of adrenocortical carcinomas were analyzed using Affymetrix Human Gene 2.0 ST Array to identify homogeneous molecular subgroups\"\n", "!Series_overall_design\t\"Gene expression profiles of 44 adrenocortical carcinomas were hybridized using Affymetrix Human Gene 2.0 ST Array\"\n", "Sample Characteristics Dictionary:\n", "{0: ['age (years): 70', 'age (years): 26', 'age (years): 53', 'age (years): 73', 'age (years): 15', 'age (years): 51', 'age (years): 63', 'age (years): 29', 'age (years): 79', 'age (years): 45', 'age (years): 43', 'age (years): 41', 'age (years): 37', 'age (years): 81', 'age (years): 68', 'age (years): 42', 'age (years): 59', 'age (years): 39', 'age (years): 25', 'age (years): 36', 'age (years): 24', 'age (years): 49', 'age (years): 75', 'age (years): 48', 'age (years): 54', 'age (years): 28', 'age (years): 40', 'age (years): 44', 'age (years): 52', 'age (years): 30'], 1: ['gender: F', 'gender: M'], 2: ['cell type: Adrenocortical carcinoma']}\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": "50fa822c", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "c93bd0ed", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:21:09.417294Z", "iopub.status.busy": "2025-03-25T06:21:09.417188Z", "iopub.status.idle": "2025-03-25T06:21:09.427771Z", "shell.execute_reply": "2025-03-25T06:21:09.427489Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical DataFrame Preview:\n", "{'GSM1196511': [1.0, 70.0, 0.0], 'GSM1196512': [1.0, 26.0, 0.0], 'GSM1196513': [1.0, 53.0, 0.0], 'GSM1196514': [1.0, 73.0, 1.0], 'GSM1196515': [1.0, 15.0, 0.0], 'GSM1196516': [1.0, 51.0, 0.0], 'GSM1196517': [1.0, 63.0, 1.0], 'GSM1196518': [1.0, 26.0, 0.0], 'GSM1196519': [1.0, 29.0, 1.0], 'GSM1196520': [1.0, 79.0, 0.0], 'GSM1196521': [1.0, 45.0, 0.0], 'GSM1196522': [1.0, 43.0, 0.0], 'GSM1196523': [1.0, 53.0, 0.0], 'GSM1196524': [1.0, 45.0, 0.0], 'GSM1196525': [1.0, 41.0, 0.0], 'GSM1196526': [1.0, 37.0, 0.0], 'GSM1196527': [1.0, 81.0, 0.0], 'GSM1196528': [1.0, 68.0, 1.0], 'GSM1196529': [1.0, 42.0, 0.0], 'GSM1196530': [1.0, 59.0, 0.0], 'GSM1196531': [1.0, 39.0, 0.0], 'GSM1196532': [1.0, 25.0, 0.0], 'GSM1196533': [1.0, 41.0, 0.0], 'GSM1196534': [1.0, 36.0, 0.0], 'GSM1196535': [1.0, 24.0, 0.0], 'GSM1196536': [1.0, 49.0, 0.0], 'GSM1196537': [1.0, 75.0, 0.0], 'GSM1196538': [1.0, 37.0, 0.0], 'GSM1196539': [1.0, 26.0, 0.0], 'GSM1196540': [1.0, 48.0, 0.0], 'GSM1196541': [1.0, 15.0, 0.0], 'GSM1196542': [1.0, 49.0, 0.0], 'GSM1196543': [1.0, 54.0, 1.0], 'GSM1196544': [1.0, 39.0, 1.0], 'GSM1196545': [1.0, 79.0, 0.0], 'GSM1196546': [1.0, 28.0, 0.0], 'GSM1196547': [1.0, 40.0, 0.0], 'GSM1196548': [1.0, 44.0, 0.0], 'GSM1196549': [1.0, 28.0, 0.0], 'GSM1196550': [1.0, 53.0, 0.0], 'GSM1196551': [1.0, 28.0, 1.0], 'GSM1196552': [1.0, 52.0, 1.0], 'GSM1196553': [1.0, 30.0, 0.0], 'GSM1196554': [1.0, 46.0, 0.0]}\n", "Clinical data saved to ../../output/preprocess/Adrenocortical_Cancer/clinical_data/GSE49278.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# From the background information: \"Expression profiling by array\" and \"Gene expression profiles...using Affymetrix Human Gene 2.0 ST Array\"\n", "# This indicates the dataset contains gene expression data\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "\n", "# Trait (Adrenocortical Cancer)\n", "# From characteristics dictionary, key 2 contains 'cell type: Adrenocortical carcinoma'\n", "# This is constant across all samples, but since it's the trait we're studying, we'll use it\n", "trait_row = 2\n", "\n", "# Age\n", "# From characteristics dictionary, key 0 contains age data\n", "age_row = 0\n", "\n", "# Gender\n", "# From characteristics dictionary, key 1 contains gender data\n", "gender_row = 1\n", "\n", "# 2.2 Data Type Conversion\n", "\n", "def convert_trait(value):\n", " \"\"\"Convert trait value to binary (1 for adrenocortical carcinoma, 0 for normal)\"\"\"\n", " if not value or ':' not in value:\n", " return None\n", " \n", " value = value.split(':', 1)[1].strip().lower()\n", " \n", " if 'adrenocortical carcinoma' in value:\n", " return 1\n", " else:\n", " return None # We don't have controls in this dataset\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age value to continuous\"\"\"\n", " if not value or ':' not in value:\n", " return None\n", " \n", " try:\n", " age_str = value.split(':', 1)[1].strip()\n", " return float(age_str)\n", " except (ValueError, TypeError):\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender value to binary (0 for female, 1 for male)\"\"\"\n", " if not value or ':' not in value:\n", " return None\n", " \n", " gender = value.split(':', 1)[1].strip().upper()\n", " \n", " if gender == 'F':\n", " return 0\n", " elif gender == 'M':\n", " return 1\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Determine trait data availability\n", "is_trait_available = trait_row is not None\n", "\n", "# Initial filtering and save metadata\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", " # Extract clinical features\n", " clinical_df = geo_select_clinical_features(\n", " clinical_df=clinical_data, # This variable should be defined in previous steps\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 dataframe\n", " preview = preview_df(clinical_df)\n", " print(\"Clinical DataFrame Preview:\")\n", " print(preview)\n", " \n", " # Save to CSV\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " clinical_df.to_csv(out_clinical_data_file, index=False)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "820392fa", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "6aecc97c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:21:09.428778Z", "iopub.status.busy": "2025-03-25T06:21:09.428675Z", "iopub.status.idle": "2025-03-25T06:21:09.695629Z", "shell.execute_reply": "2025-03-25T06:21:09.695285Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First 20 gene/probe identifiers:\n", "Index(['16650001', '16650003', '16650005', '16650007', '16650009', '16650011',\n", " '16650013', '16650015', '16650017', '16650019', '16650021', '16650023',\n", " '16650025', '16650027', '16650029', '16650031', '16650033', '16650035',\n", " '16650037', '16650041'],\n", " dtype='object', name='ID')\n" ] } ], "source": [ "# 1. First get the file paths again to access the matrix file\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. Use the get_genetic_data function from the library to get the gene_data from the matrix_file\n", "gene_data = get_genetic_data(matrix_file)\n", "\n", "# 3. Print the first 20 row IDs (gene or probe identifiers) for future observation\n", "print(\"First 20 gene/probe identifiers:\")\n", "print(gene_data.index[:20])\n" ] }, { "cell_type": "markdown", "id": "67d98e66", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "b02afc8b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:21:09.696854Z", "iopub.status.busy": "2025-03-25T06:21:09.696736Z", "iopub.status.idle": "2025-03-25T06:21:09.698968Z", "shell.execute_reply": "2025-03-25T06:21:09.698672Z" } }, "outputs": [], "source": [ "# Examining the gene identifiers to determine if they are human gene symbols\n", "# These identifiers (e.g., '16650001') appear to be numeric probe IDs, not human gene symbols\n", "# Human gene symbols typically have alphanumeric patterns like \"BRCA1\", \"TP53\", etc.\n", "# These appear to be probeset IDs from a microarray platform that need to be mapped to gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "ebc843e2", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "69a85a70", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:21:09.700020Z", "iopub.status.busy": "2025-03-25T06:21:09.699908Z", "iopub.status.idle": "2025-03-25T06:21:12.626191Z", "shell.execute_reply": "2025-03-25T06:21:12.625811Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['16657436', '16657440', '16657445', '16657447', '16657450'], 'RANGE_STRAND': ['+', '+', '+', '+', '+'], 'RANGE_START': [12190.0, 29554.0, 69091.0, 160446.0, 317811.0], 'RANGE_END': [13639.0, 31109.0, 70008.0, 161525.0, 328581.0], 'total_probes': [25.0, 28.0, 8.0, 13.0, 36.0], 'GB_ACC': ['NR_046018', nan, nan, nan, 'NR_024368'], 'SPOT_ID': ['chr1:12190-13639', 'chr1:29554-31109', 'chr1:69091-70008', 'chr1:160446-161525', 'chr1:317811-328581'], 'RANGE_GB': ['NC_000001.10', 'NC_000001.10', 'NC_000001.10', 'NC_000001.10', 'NC_000001.10']}\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": "ef7a893f", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "6f714bd5", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:21:12.627469Z", "iopub.status.busy": "2025-03-25T06:21:12.627346Z", "iopub.status.idle": "2025-03-25T06:21:13.943641Z", "shell.execute_reply": "2025-03-25T06:21:13.943259Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original gene expression data shape: (53617, 44)\n", "\n", "Using probe IDs as gene identifiers (first 5 rows):\n", " GSM1196511 GSM1196512 GSM1196513 GSM1196514 GSM1196515 \\\n", "Gene \n", "16650001 3.114460 2.761934 3.191700 2.981038 3.113831 \n", "16650003 2.070307 1.831540 2.303189 2.430376 1.507325 \n", "16650005 2.532754 3.371765 2.264750 2.647668 2.559651 \n", "16650007 1.968311 2.229541 1.762466 2.827752 1.626150 \n", "16650009 1.418189 1.314710 1.571579 1.233351 1.753973 \n", "\n", " GSM1196516 GSM1196517 GSM1196518 GSM1196519 GSM1196520 ... \\\n", "Gene ... \n", "16650001 2.687413 3.468881 2.411585 3.761057 2.974074 ... \n", "16650003 2.382929 2.808405 2.031501 2.797925 2.567698 ... \n", "16650005 3.508271 1.959297 2.764491 2.655120 1.712738 ... \n", "16650007 2.184046 1.214179 1.664709 1.559880 2.373817 ... \n", "16650009 1.033928 1.259945 1.239220 1.104874 1.285327 ... \n", "\n", " GSM1196545 GSM1196546 GSM1196547 GSM1196548 GSM1196549 \\\n", "Gene \n", "16650001 2.440173 2.954796 3.445082 3.388275 2.450530 \n", "16650003 1.833832 2.025689 2.493108 2.313400 1.594192 \n", "16650005 2.165530 4.164357 3.455904 4.223868 2.515237 \n", "16650007 3.381329 2.235444 2.027248 1.226888 1.948129 \n", "16650009 1.765765 0.843827 1.600953 1.369317 0.956487 \n", "\n", " GSM1196550 GSM1196551 GSM1196552 GSM1196553 GSM1196554 \n", "Gene \n", "16650001 2.293126 3.136449 2.748609 3.587116 3.194252 \n", "16650003 2.106784 3.733405 2.427485 3.297156 1.920650 \n", "16650005 2.956488 3.047515 1.870629 2.264684 4.401433 \n", "16650007 1.840212 2.096553 2.489499 2.316459 1.641595 \n", "16650009 1.137052 1.658009 1.689291 1.196682 1.994568 \n", "\n", "[5 rows x 44 columns]\n", "\n", "Number of genes/probes: 53617\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data saved to ../../output/preprocess/Adrenocortical_Cancer/gene_data/GSE49278.csv\n", "\n", "NOTE: For this dataset, probe IDs are being used directly as gene identifiers\n", "because appropriate gene mapping information was not available in the provided annotation.\n" ] } ], "source": [ "# Since we're facing difficulties in mapping the probe IDs to gene symbols,\n", "# we'll take a different approach for this platform (Affymetrix Human Gene 2.0 ST Array)\n", "\n", "print(\"Original gene expression data shape:\", gene_data.shape)\n", "\n", "# For this Affymetrix platform, we could try to load a proper annotation,\n", "# but as an alternative approach, we'll use the probe IDs directly as gene identifiers\n", "# This is a valid fallback when proper mapping isn't available\n", "\n", "# Clean and prepare the gene data\n", "gene_data.index.name = 'Gene' # Rename index to 'Gene' to maintain consistency with later steps\n", "\n", "# Since we're using probe IDs as gene identifiers, no mapping is applied\n", "# but we need to ensure the data is in the right format for subsequent steps\n", "\n", "# Preview the gene data\n", "print(\"\\nUsing probe IDs as gene identifiers (first 5 rows):\")\n", "print(gene_data.head())\n", "\n", "# Count the number of genes\n", "print(f\"\\nNumber of genes/probes: {len(gene_data)}\")\n", "\n", "# Create the output directory if it doesn't exist\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "\n", "# Save the gene data to a CSV file\n", "gene_data.to_csv(out_gene_data_file)\n", "print(f\"Gene data saved to {out_gene_data_file}\")\n", "\n", "# Add a note about the gene mapping approach\n", "print(\"\\nNOTE: For this dataset, probe IDs are being used directly as gene identifiers\")\n", "print(\"because appropriate gene mapping information was not available in the provided annotation.\")\n" ] }, { "cell_type": "markdown", "id": "a992b1e8", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "1b016c96", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:21:13.944949Z", "iopub.status.busy": "2025-03-25T06:21:13.944832Z", "iopub.status.idle": "2025-03-25T06:21:43.828158Z", "shell.execute_reply": "2025-03-25T06:21:43.827839Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loading original gene expression data...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Adrenocortical_Cancer/gene_data/GSE49278.csv\n", "Gene data shape: (53617, 44)\n", "Clinical data columns: ['GSM1196511', 'GSM1196512', 'GSM1196513', 'GSM1196514', 'GSM1196515', 'GSM1196516', 'GSM1196517', 'GSM1196518', 'GSM1196519', 'GSM1196520', 'GSM1196521', 'GSM1196522', 'GSM1196523', 'GSM1196524', 'GSM1196525', 'GSM1196526', 'GSM1196527', 'GSM1196528', 'GSM1196529', 'GSM1196530', 'GSM1196531', 'GSM1196532', 'GSM1196533', 'GSM1196534', 'GSM1196535', 'GSM1196536', 'GSM1196537', 'GSM1196538', 'GSM1196539', 'GSM1196540', 'GSM1196541', 'GSM1196542', 'GSM1196543', 'GSM1196544', 'GSM1196545', 'GSM1196546', 'GSM1196547', 'GSM1196548', 'GSM1196549', 'GSM1196550', 'GSM1196551', 'GSM1196552', 'GSM1196553', 'GSM1196554']\n", "Transposed clinical data shape: (43, 3)\n", "Linked data shape: (44, 53620)\n", "Linked data columns (first 5): ['Adrenocortical_Cancer', 'Age', 'Gender', '16650001', '16650003']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "After handling missing values, linked data shape: (43, 53620)\n", "Quartiles for 'Adrenocortical_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 'Adrenocortical_Cancer' in this dataset is severely biased.\n", "\n", "Quartiles for 'Age':\n", " 25%: 29.5\n", " 50% (Median): 43.0\n", " 75%: 53.0\n", "Min: 15.0\n", "Max: 81.0\n", "The distribution of the feature 'Age' in this dataset is fine.\n", "\n", "For the feature 'Gender', the least common label is '1.0' with 8 occurrences. This represents 18.60% of the dataset.\n", "The distribution of the feature 'Gender' in this dataset is fine.\n", "\n", "Is trait biased: True\n", "Dataset is not usable for trait-gene association studies.\n" ] } ], "source": [ "# 1. Since normalization of gene symbols failed (no IDs recognized), let's use the original gene data\n", "print(\"Loading original gene expression data...\")\n", "gene_data = get_genetic_data(matrix_file)\n", "\n", "# Save the gene data with original probe IDs\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "gene_data.to_csv(out_gene_data_file)\n", "print(f\"Gene expression data saved to {out_gene_data_file}\")\n", "print(f\"Gene data shape: {gene_data.shape}\")\n", "\n", "# 2. Load previously saved clinical data and link with genetic data\n", "clinical_data_path = out_clinical_data_file\n", "clinical_data = pd.read_csv(clinical_data_path)\n", "print(f\"Clinical data columns: {clinical_data.columns.tolist()}\")\n", "\n", "# The clinical data CSV likely has columns named by sample IDs from transpose\n", "# We need to set the columns as index before linking\n", "clinical_data = clinical_data.set_index(clinical_data.columns[0])\n", "\n", "# Transpose clinical data to have samples as rows and features as columns\n", "clinical_data = clinical_data.T\n", "print(f\"Transposed clinical data shape: {clinical_data.shape}\")\n", "\n", "# Rename the columns to standard names\n", "if len(clinical_data.columns) >= 3:\n", " clinical_data.columns = [trait, 'Age', 'Gender']\n", "elif len(clinical_data.columns) == 2:\n", " clinical_data.columns = [trait, 'Age']\n", "elif len(clinical_data.columns) == 1:\n", " clinical_data.columns = [trait]\n", "\n", "# Transpose gene data to have samples as rows and genes as columns\n", "gene_data_t = gene_data.T\n", "\n", "# Merge clinical and genetic data\n", "linked_data = pd.concat([clinical_data, gene_data_t], axis=1)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "\n", "# Check for the trait column\n", "print(f\"Linked data columns (first 5): {linked_data.columns[:5].tolist()}\")\n", "\n", "# 3. Handle missing values in the linked data\n", "linked_data = handle_missing_values(linked_data, trait)\n", "print(f\"After handling missing values, linked data shape: {linked_data.shape}\")\n", "\n", "# 4. Determine whether the trait and demographic features are severely biased\n", "is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "print(f\"Is trait biased: {is_biased}\")\n", "\n", "# 5. Conduct final quality validation and save cohort information\n", "note = \"Dataset containing gene expression profiles of adrenocortical carcinomas. All samples are cancer cases (no controls), making the trait binary variable biased.\"\n", "is_usable = validate_and_save_cohort_info(\n", " is_final=True, \n", " cohort=cohort, \n", " info_path=json_path, \n", " is_gene_available=is_gene_available, \n", " is_trait_available=is_trait_available,\n", " is_biased=is_biased,\n", " df=linked_data,\n", " note=note\n", ")\n", "\n", "# 6. 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", " linked_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", "else:\n", " print(\"Dataset is not usable for trait-gene association studies.\")" ] } ], "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 }