{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "ec21aeb6", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:11:08.343059Z", "iopub.status.busy": "2025-03-25T06:11:08.342935Z", "iopub.status.idle": "2025-03-25T06:11:08.511796Z", "shell.execute_reply": "2025-03-25T06:11:08.511403Z" } }, "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 = \"Pheochromocytoma_and_Paraganglioma\"\n", "cohort = \"GSE19422\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Pheochromocytoma_and_Paraganglioma\"\n", "in_cohort_dir = \"../../input/GEO/Pheochromocytoma_and_Paraganglioma/GSE19422\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Pheochromocytoma_and_Paraganglioma/GSE19422.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Pheochromocytoma_and_Paraganglioma/gene_data/GSE19422.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Pheochromocytoma_and_Paraganglioma/clinical_data/GSE19422.csv\"\n", "json_path = \"../../output/preprocess/Pheochromocytoma_and_Paraganglioma/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "cddd0884", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "bf8227bf", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:11:08.513246Z", "iopub.status.busy": "2025-03-25T06:11:08.513093Z", "iopub.status.idle": "2025-03-25T06:11:08.659028Z", "shell.execute_reply": "2025-03-25T06:11:08.658653Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Gene expression profiling of pheochromocytoma (PCC)/paraganglioma (PGL) tumors\"\n", "!Series_summary\t\"Transcriptional analysis of 84 primary pheochromocytoma (PCC)/paraganglioma tumors.\"\n", "!Series_overall_design\t\"84 samples (primary pheochromocytoma (PCC)/paraganglioma tumors) were hybridized onto a cDNA microarray in order to investigate possible heterogeneity within these tumors\"\n", "Sample Characteristics Dictionary:\n", "{0: ['tissue type: PCC primary tumor', 'tissue type: PGL primary tumor', 'tissue type: Normal adrenal tissue']}\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": "6d6142fa", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "773c3cbb", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:11:08.660536Z", "iopub.status.busy": "2025-03-25T06:11:08.660304Z", "iopub.status.idle": "2025-03-25T06:11:08.667818Z", "shell.execute_reply": "2025-03-25T06:11:08.667518Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of selected clinical features:\n", "{0: [1.0]}\n", "Saved clinical features to ../../output/preprocess/Pheochromocytoma_and_Paraganglioma/clinical_data/GSE19422.csv\n" ] } ], "source": [ "import pandas as pd\n", "import os\n", "import json\n", "from typing import Callable, Dict, Any, Optional\n", "\n", "# 1. Gene Expression Data Availability\n", "# Based on background information, this dataset contains gene expression data from primary PCC/PGL tumors\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# From the sample characteristics dictionary, we can see trait information in row 0\n", "trait_row = 0 # Contains 'tissue type: PCC primary tumor', 'tissue type: PGL primary tumor', etc.\n", "age_row = None # No age information available\n", "gender_row = None # No gender information available\n", "\n", "# 2.2 Data Type Conversion Functions\n", "def convert_trait(value: str) -> int:\n", " \"\"\"\n", " Convert tissue type to binary values:\n", " 1 for PCC or PGL tumor, 0 for Normal adrenal tissue\n", " Unknown values are converted to None\n", " \"\"\"\n", " if pd.isna(value):\n", " return None\n", " \n", " # Extract the value after the colon\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip().lower()\n", " else:\n", " value = value.strip().lower()\n", " \n", " # Assign binary values\n", " if 'pcc primary tumor' in value or 'pgl primary tumor' in value:\n", " return 1 # PCC or PGL tumor\n", " elif 'normal adrenal tissue' in value:\n", " return 0 # Normal tissue\n", " else:\n", " return None # Unknown value\n", "\n", "# No age or gender conversion functions needed since data not available\n", "\n", "# 3. Save Metadata - Initial Filtering\n", "# Trait data is available since trait_row is not None\n", "is_trait_available = trait_row is not None\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 not None, we need to create a DataFrame from the sample characteristics dictionary\n", "sample_characteristics = {0: ['tissue type: PCC primary tumor', 'tissue type: PGL primary tumor', 'tissue type: Normal adrenal tissue']}\n", "\n", "# Create a DataFrame from the sample characteristics\n", "# Assuming each item in the list is a different sample\n", "df_data = {}\n", "for row_idx, values in sample_characteristics.items():\n", " for idx, value in enumerate(values):\n", " if idx not in df_data:\n", " df_data[idx] = {}\n", " df_data[idx][row_idx] = value\n", "\n", "clinical_data = pd.DataFrame.from_dict(df_data, orient='index')\n", "\n", "# Extract clinical features\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=None,\n", " gender_row=gender_row,\n", " convert_gender=None\n", ")\n", "\n", "# Preview the extracted features\n", "preview = preview_df(selected_clinical_df)\n", "print(\"Preview of selected clinical features:\")\n", "print(preview)\n", "\n", "# Create output directory if it doesn't exist\n", "os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", "\n", "# Save the selected clinical features\n", "selected_clinical_df.to_csv(out_clinical_data_file, index=False)\n", "print(f\"Saved clinical features to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "893f2e6e", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "8a612da0", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:11:08.669109Z", "iopub.status.busy": "2025-03-25T06:11:08.668993Z", "iopub.status.idle": "2025-03-25T06:11:08.959439Z", "shell.execute_reply": "2025-03-25T06:11:08.959001Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['A_23_P100001', 'A_23_P100011', 'A_23_P100022', 'A_23_P100056',\n", " 'A_23_P100074', 'A_23_P100092', 'A_23_P100103', 'A_23_P100111',\n", " 'A_23_P100127', 'A_23_P100133', 'A_23_P100141', 'A_23_P100156',\n", " 'A_23_P100177', 'A_23_P100189', 'A_23_P100196', 'A_23_P100203',\n", " 'A_23_P100220', 'A_23_P100240', 'A_23_P10025', 'A_23_P100263'],\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": "c528f9c5", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "c95538f0", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:11:08.961012Z", "iopub.status.busy": "2025-03-25T06:11:08.960884Z", "iopub.status.idle": "2025-03-25T06:11:08.962807Z", "shell.execute_reply": "2025-03-25T06:11:08.962508Z" } }, "outputs": [], "source": [ "# Examining the gene identifiers\n", "# These identifiers starting with \"A_23_P\" appear to be Agilent microarray probe IDs,\n", "# not standard human gene symbols.\n", "# They are likely from an Agilent microarray platform and need to be mapped to gene symbols.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "762f5092", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "a923031c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:11:08.964065Z", "iopub.status.busy": "2025-03-25T06:11:08.963954Z", "iopub.status.idle": "2025-03-25T06:11:14.593569Z", "shell.execute_reply": "2025-03-25T06:11:14.593170Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['A_23_P100001', 'A_23_P100011', 'A_23_P100022', 'A_23_P100056', 'A_23_P100074'], 'SPOT_ID': ['A_23_P100001', 'A_23_P100011', 'A_23_P100022', 'A_23_P100056', 'A_23_P100074'], 'CONTROL_TYPE': ['FALSE', 'FALSE', 'FALSE', 'FALSE', 'FALSE'], 'REFSEQ': ['NM_207446', 'NM_005829', 'NM_014848', 'NM_194272', 'NM_020371'], 'GB_ACC': ['NM_207446', 'NM_005829', 'NM_014848', 'NM_194272', 'NM_020371'], 'GENE': [400451.0, 10239.0, 9899.0, 348093.0, 57099.0], 'GENE_SYMBOL': ['FAM174B', 'AP3S2', 'SV2B', 'RBPMS2', 'AVEN'], 'GENE_NAME': ['family with sequence similarity 174, member B', 'adaptor-related protein complex 3, sigma 2 subunit', 'synaptic vesicle glycoprotein 2B', 'RNA binding protein with multiple splicing 2', 'apoptosis, caspase activation inhibitor'], 'UNIGENE_ID': ['Hs.27373', 'Hs.632161', 'Hs.21754', 'Hs.436518', 'Hs.555966'], 'ENSEMBL_ID': ['ENST00000557398', nan, 'ENST00000557410', 'ENST00000300069', 'ENST00000306730'], 'TIGR_ID': [nan, nan, nan, nan, nan], 'ACCESSION_STRING': ['ref|NM_207446|ens|ENST00000557398|ens|ENST00000553393|ens|ENST00000327355', 'ref|NM_005829|ref|NM_001199058|ref|NR_023361|ref|NR_037582', 'ref|NM_014848|ref|NM_001167580|ens|ENST00000557410|ens|ENST00000330276', 'ref|NM_194272|ens|ENST00000300069|gb|AK127873|gb|AK124123', 'ref|NM_020371|ens|ENST00000306730|gb|AF283508|gb|BC010488'], 'CHROMOSOMAL_LOCATION': ['chr15:93160848-93160789', 'chr15:90378743-90378684', 'chr15:91838329-91838388', 'chr15:65032375-65032316', 'chr15:34158739-34158680'], 'CYTOBAND': ['hs|15q26.1', 'hs|15q26.1', 'hs|15q26.1', 'hs|15q22.31', 'hs|15q14'], 'DESCRIPTION': ['Homo sapiens family with sequence similarity 174, member B (FAM174B), mRNA [NM_207446]', 'Homo sapiens adaptor-related protein complex 3, sigma 2 subunit (AP3S2), transcript variant 1, mRNA [NM_005829]', 'Homo sapiens synaptic vesicle glycoprotein 2B (SV2B), transcript variant 1, mRNA [NM_014848]', 'Homo sapiens RNA binding protein with multiple splicing 2 (RBPMS2), mRNA [NM_194272]', 'Homo sapiens apoptosis, caspase activation inhibitor (AVEN), mRNA [NM_020371]'], 'GO_ID': ['GO:0016020(membrane)|GO:0016021(integral to membrane)', 'GO:0005794(Golgi apparatus)|GO:0006886(intracellular protein transport)|GO:0008565(protein transporter activity)|GO:0016020(membrane)|GO:0016192(vesicle-mediated transport)|GO:0030117(membrane coat)|GO:0030659(cytoplasmic vesicle membrane)|GO:0031410(cytoplasmic vesicle)', 'GO:0001669(acrosomal vesicle)|GO:0006836(neurotransmitter transport)|GO:0016020(membrane)|GO:0016021(integral to membrane)|GO:0022857(transmembrane transporter activity)|GO:0030054(cell junction)|GO:0030672(synaptic vesicle membrane)|GO:0031410(cytoplasmic vesicle)|GO:0045202(synapse)', 'GO:0000166(nucleotide binding)|GO:0003676(nucleic acid binding)', 'GO:0005515(protein binding)|GO:0005622(intracellular)|GO:0005624(membrane fraction)|GO:0006915(apoptosis)|GO:0006916(anti-apoptosis)|GO:0012505(endomembrane system)|GO:0016020(membrane)'], 'SEQUENCE': ['ATCTCATGGAAAAGCTGGATTCCTCTGCCTTACGCAGAAACACCCGGGCTCCATCTGCCA', 'TCAAGTATTGGCCTGACATAGAGTCCTTAAGACAAGCAAAGACAAGCAAGGCAAGCACGT', 'ATGTCGGCTGTGGAGGGTTAAAGGGATGAGGCTTTCCTTTGTTTAGCAAATCTGTTCACA', 'CCCTGTCAGATAAGTTTAATGTTTAGTTTGAGGCATGAAGAAGAAAAGGGTTTCCATTCT', 'GACCAGCCAGTTTACAAGCATGTCTCAAGCTAGTGTGTTCCATTATGCTCACAGCAGTAA']}\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": "2113cc5e", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "2d0eba3f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:11:14.594988Z", "iopub.status.busy": "2025-03-25T06:11:14.594855Z", "iopub.status.idle": "2025-03-25T06:11:15.752969Z", "shell.execute_reply": "2025-03-25T06:11:15.752523Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene mapping preview (first 5 rows):\n", " ID Gene\n", "0 A_23_P100001 FAM174B\n", "1 A_23_P100011 AP3S2\n", "2 A_23_P100022 SV2B\n", "3 A_23_P100056 RBPMS2\n", "4 A_23_P100074 AVEN\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene expression data after mapping (first 5 genes):\n", " GSM482992 GSM482993 GSM482994 GSM482995 GSM482996 GSM482997 \\\n", "Gene \n", "A1BG 1.455 1.304 1.272 0.324 0.232 1.483 \n", "A1BG-AS1 0.774 1.332 0.233 1.082 0.436 0.910 \n", "A1CF -2.990 -2.291 -3.169 -3.308 -2.957 -3.425 \n", "A2M 1.885 0.949 0.767 0.631 0.286 -0.035 \n", "A2ML1 1.244 2.873 0.937 -0.832 0.808 1.307 \n", "\n", " GSM482998 GSM482999 GSM483000 GSM483001 ... GSM483072 \\\n", "Gene ... \n", "A1BG 1.349 1.832 2.066 1.652 ... 2.118 \n", "A1BG-AS1 0.689 1.444 0.580 1.105 ... 0.871 \n", "A1CF -2.590 -3.168 -2.826 -2.438 ... -3.421 \n", "A2M -0.981 -0.337 0.703 1.285 ... 0.505 \n", "A2ML1 0.785 0.818 1.763 1.246 ... 0.238 \n", "\n", " GSM483073 GSM483074 GSM483075 GSM483076 GSM483077 GSM483078 \\\n", "Gene \n", "A1BG 1.722 0.117 0.991 -2.292 -2.492 -0.056 \n", "A1BG-AS1 1.275 0.986 1.197 -1.149 -0.972 -0.256 \n", "A1CF -2.144 -0.964 -2.832 -2.632 -2.549 -3.423 \n", "A2M 0.019 1.664 1.765 -0.486 -1.733 0.370 \n", "A2ML1 1.107 0.770 0.796 0.467 1.935 2.279 \n", "\n", " GSM483079 GSM483080 GSM483081 \n", "Gene \n", "A1BG 0.739 -1.580 -0.038 \n", "A1BG-AS1 0.143 -0.861 -0.274 \n", "A1CF -2.718 -2.869 -2.104 \n", "A2M 0.275 -1.265 -0.105 \n", "A2ML1 -0.096 1.066 3.420 \n", "\n", "[5 rows x 90 columns]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Saved gene expression data to ../../output/preprocess/Pheochromocytoma_and_Paraganglioma/gene_data/GSE19422.csv\n" ] } ], "source": [ "# 1. Determine which columns in gene_annotation contain probe IDs and gene symbols\n", "# Based on the preview, 'ID' contains probe IDs matching the gene expression data indices\n", "# 'GENE_SYMBOL' contains the human gene symbols we need to map to\n", "\n", "# 2. Get a gene mapping dataframe with the ID and GENE_SYMBOL columns\n", "gene_mapping = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='GENE_SYMBOL')\n", "print(f\"Gene mapping preview (first 5 rows):\")\n", "print(gene_mapping.head())\n", "\n", "# 3. Convert probe-level measurements to gene expression data\n", "# Apply the gene mapping to get gene-level expression data\n", "# This function handles many-to-many mapping by distributing probe values equally\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "\n", "# Normalize gene symbols to ensure consistency\n", "gene_data = normalize_gene_symbols_in_index(gene_data)\n", "\n", "# Preview the result\n", "print(f\"\\nGene expression data after mapping (first 5 genes):\")\n", "print(gene_data.head())\n", "\n", "# Save the processed gene data\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\"Saved gene expression data to {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "f2fc54e0", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "3a28bf80", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:11:15.754615Z", "iopub.status.busy": "2025-03-25T06:11:15.754486Z", "iopub.status.idle": "2025-03-25T06:11:21.151089Z", "shell.execute_reply": "2025-03-25T06:11:21.150695Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical data structure:\n", " 0\n", "0 1.0\n", "Number of samples from gene data: 90\n", "First few sample IDs: ['GSM482992', 'GSM482993', 'GSM482994', 'GSM482995', 'GSM482996']\n", "Reformatted clinical data shape: (90, 1)\n", " Pheochromocytoma_and_Paraganglioma\n", "GSM482992 1\n", "GSM482993 1\n", "GSM482994 1\n", "GSM482995 1\n", "GSM482996 1\n", "Linked data shape: (90, 18248)\n", " Pheochromocytoma_and_Paraganglioma A1BG A1BG-AS1 A1CF A2M \\\n", "GSM482992 1 1.455 0.774 -2.990 1.885 \n", "GSM482993 1 1.304 1.332 -2.291 0.949 \n", "GSM482994 1 1.272 0.233 -3.169 0.767 \n", "GSM482995 1 0.324 1.082 -3.308 0.631 \n", "GSM482996 1 0.232 0.436 -2.957 0.286 \n", "\n", " A2ML1 A4GALT A4GNT AAAS AACS ... ZW10 ZWILCH ZWINT \\\n", "GSM482992 1.244 0.426 0.333 -0.448 -0.174 ... -1.251 -2.665 -4.073 \n", "GSM482993 2.873 0.557 0.138 -0.046 -0.758 ... -1.106 -2.379 -3.435 \n", "GSM482994 0.937 0.343 0.123 -0.709 -0.792 ... -1.194 -1.972 -3.943 \n", "GSM482995 -0.832 -0.293 0.160 -0.224 -1.008 ... -0.339 -2.364 -4.751 \n", "GSM482996 0.808 -0.514 -0.049 0.007 -0.719 ... -0.783 -2.656 -3.305 \n", "\n", " ZXDA ZXDC ZYG11A ZYG11B ZYX ZZEF1 ZZZ3 \n", "GSM482992 1.185 -0.791 -1.396 1.172 -0.178 1.134 -0.611 \n", "GSM482993 1.268 0.047 -2.292 -1.714 -1.554 1.630 -0.496 \n", "GSM482994 1.705 -2.661 -1.683 3.401 -0.765 -1.072 -0.226 \n", "GSM482995 2.252 -0.507 -1.832 0.568 -1.260 -0.579 -0.593 \n", "GSM482996 2.842 -1.260 -2.108 0.718 -1.459 0.200 0.093 \n", "\n", "[5 rows x 18248 columns]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Shape after handling missing values: (90, 18248)\n", "Quartiles for 'Pheochromocytoma_and_Paraganglioma':\n", " 25%: 1.0\n", " 50% (Median): 1.0\n", " 75%: 1.0\n", "Min: 1\n", "Max: 1\n", "The distribution of the feature 'Pheochromocytoma_and_Paraganglioma' in this dataset is severely biased.\n", "\n", "A new JSON file was created at: ../../output/preprocess/Pheochromocytoma_and_Paraganglioma/cohort_info.json\n", "Data quality check failed. The dataset is not suitable for association studies.\n", "Updated clinical data saved to ../../output/preprocess/Pheochromocytoma_and_Paraganglioma/clinical_data/GSE19422.csv\n" ] } ], "source": [ "# 1. We already normalized gene symbols in the previous step, no need to redo it\n", "# Let's first verify the clinical data structure\n", "clinical_file = pd.read_csv(out_clinical_data_file)\n", "print(\"Clinical data structure:\")\n", "print(clinical_file.head())\n", "\n", "# The issue seems to be with the format of our clinical data\n", "# Let's reformat the clinical data from scratch to make sure we have the right structure\n", "# Use the raw matrix file to extract sample IDs\n", "\n", "# Get sample IDs from the gene expression data columns\n", "sample_ids = gene_data.columns.tolist()\n", "print(f\"Number of samples from gene data: {len(sample_ids)}\")\n", "print(f\"First few sample IDs: {sample_ids[:5]}\")\n", "\n", "# Create a clinical dataframe with trait values for each sample\n", "# For GSE19422, all samples appear to be tumor samples (value 1) based on the background info\n", "clinical_df = pd.DataFrame(index=sample_ids)\n", "clinical_df[trait] = 1 # Set all samples as tumor samples (value 1)\n", "\n", "print(f\"Reformatted clinical data shape: {clinical_df.shape}\")\n", "print(clinical_df.head())\n", "\n", "# 2. Link the clinical and genetic data\n", "# Since we're using consistent sample IDs, we can join them directly\n", "linked_data = clinical_df.join(gene_data.T)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "print(linked_data.head())\n", "\n", "# 3. Handle missing values in the linked data\n", "linked_data = handle_missing_values(linked_data, trait)\n", "print(f\"Shape after handling missing values: {linked_data.shape}\")\n", "\n", "# 4. Determine whether the trait and demographic features are severely biased\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=f\"Dataset contains gene expression data for {len(unbiased_linked_data)} pheochromocytoma/paraganglioma samples.\"\n", ")\n", "\n", "# 6. Save the data if it's usable\n", "if is_usable:\n", " # Create directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " # Save the data\n", " unbiased_linked_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", "else:\n", " print(f\"Data quality check failed. The dataset is not suitable for association studies.\")\n", "\n", "# Also save the clinical data in the proper format\n", "os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", "clinical_df.to_csv(out_clinical_data_file)\n", "print(f\"Updated clinical data saved to {out_clinical_data_file}\")" ] } ], "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 }