{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "fd60a164", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:46:55.751482Z", "iopub.status.busy": "2025-03-25T03:46:55.751313Z", "iopub.status.idle": "2025-03-25T03:46:55.917445Z", "shell.execute_reply": "2025-03-25T03:46:55.917024Z" } }, "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 = \"Rectal_Cancer\"\n", "cohort = \"GSE133057\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Rectal_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Rectal_Cancer/GSE133057\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Rectal_Cancer/GSE133057.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Rectal_Cancer/gene_data/GSE133057.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Rectal_Cancer/clinical_data/GSE133057.csv\"\n", "json_path = \"../../output/preprocess/Rectal_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "f92bfd5d", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "fd4ede40", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:46:55.918907Z", "iopub.status.busy": "2025-03-25T03:46:55.918772Z", "iopub.status.idle": "2025-03-25T03:46:56.039471Z", "shell.execute_reply": "2025-03-25T03:46:56.038963Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Transcriptomic analysis of pretreated rectal cancer biopsies and association to the tumor regression score.\"\n", "!Series_summary\t\"To determine a preditcive marker of treatment resistance for rectal cancer, we have employed a microarray gene profiling analysis on pretreated rectal biopsies and compared with their response to therapy as defined by the American Joint Commission on Cancer (AJCC) and the American College of Pathologists. \"\n", "!Series_overall_design\t\"Frozen rectal cancer biopsies utilized for the transcriptomic analysis were from 33 patients seen between 2006 and 2009 at Cleveland Clinic Main Campus in Cleveland, Ohio. After collection of biopsie and diagnosis, patients generally underwent surgery with curative intent approximately 8–12 weeks after completion of neoadjuvant chemoradiotherapy with 5-fluorouracil as radiation sensitizer and 50.4Gy in 25 fractions. The resected tumor is assessed by pathologists to determine AJCC score.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['subject status: patient with rectal adenocarcinoma'], 1: ['ajcc score: 0', 'ajcc score: 1', 'ajcc score: 3', 'ajcc score: 2'], 2: ['gender: Female', 'gender: Male'], 3: ['overall survival (in days): 3182', 'overall survival (in days): 4584', 'overall survival (in days): 4452', 'overall survival (in days): 3789', 'overall survival (in days): 2960', 'overall survival (in days): 125', 'overall survival (in days): 4027', 'overall survival (in days): 1201', 'overall survival (in days): 403', 'overall survival (in days): 372', 'overall survival (in days): 3949', 'overall survival (in days): 3591', 'overall survival (in days): 647', 'overall survival (in days): 3964', 'overall survival (in days): 3837', 'overall survival (in days): 426', 'overall survival (in days): 2085', 'overall survival (in days): 858', 'overall survival (in days): 1147', 'overall survival (in days): 163', 'overall survival (in days): 3073', 'overall survival (in days): 3741', 'overall survival (in days): 3108', 'overall survival (in days): 3536', 'overall survival (in days): 2251', 'overall survival (in days): 2954', 'overall survival (in days): 2432', 'overall survival (in days): 1470', 'overall survival (in days): 969', 'overall survival (in days): 2000'], 4: ['dead (1)/alive(0): 0', 'dead (1)/alive(0): 1'], 5: ['age: 66', 'age: 65', 'age: 51', 'age: 72', 'age: 62', 'age: 50', 'age: 46', 'age: 59', 'age: 63', 'age: 44', 'age: 69', 'age: 41', 'age: 70', 'age: 54', 'age: 48', 'age: 75', 'age: 40', 'age: 47', 'age: 60', 'age: 43', 'age: 57', 'age: 52', 'age: 82']}\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": "9b821f6c", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "7168f0a3", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:46:56.041215Z", "iopub.status.busy": "2025-03-25T03:46:56.040891Z", "iopub.status.idle": "2025-03-25T03:46:56.048233Z", "shell.execute_reply": "2025-03-25T03:46:56.047774Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical data file not found and sample characteristics dictionary format is not compatible with geo_select_clinical_features.\n", "Skipping clinical feature extraction step.\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the background information, this dataset appears to contain gene expression data\n", "# as it's described as \"transcriptomic analysis\" and mentions microarray gene profiling analysis\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "\n", "# 2.1 Data Availability\n", "# Trait: AJCC score is available in row 1\n", "trait_row = 1\n", "\n", "# Age: Available in row 5\n", "age_row = 5\n", "\n", "# Gender: Available in row 2\n", "gender_row = 2\n", "\n", "# 2.2 Data Type Conversion\n", "\n", "# For trait (AJCC score) - Ordinal/continuous data\n", "def convert_trait(value):\n", " try:\n", " if ':' in value:\n", " # Extract the value after the colon\n", " value = value.split(':', 1)[1].strip()\n", " # Convert AJCC score to integer\n", " return int(value)\n", " except (ValueError, TypeError):\n", " return None\n", "\n", "# For age - Continuous data\n", "def convert_age(value):\n", " try:\n", " if ':' in value:\n", " # Extract the value after the colon\n", " value = value.split(':', 1)[1].strip()\n", " # Convert age to integer\n", " return int(value)\n", " except (ValueError, TypeError):\n", " return None\n", "\n", "# For gender - Binary data (Female=0, Male=1)\n", "def convert_gender(value):\n", " try:\n", " if ':' in value:\n", " # Extract the value after the colon\n", " value = value.split(':', 1)[1].strip()\n", " # Convert gender to binary (0=Female, 1=Male)\n", " if value.lower() == 'female':\n", " return 0\n", " elif value.lower() == 'male':\n", " return 1\n", " else:\n", " return None\n", " except (ValueError, TypeError):\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Determine trait data availability\n", "is_trait_available = trait_row is not None\n", "# Perform initial filtering on usability\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", " # Since we don't have direct access to the clinical_data.csv file,\n", " # and because the format of the sample characteristics dictionary doesn't match\n", " # what's expected by geo_select_clinical_features, we need to:\n", " # 1. First check if the file exists through another path\n", " # 2. If not, reconstruct a properly formatted DataFrame\n", " \n", " clinical_data_path = f\"{in_cohort_dir}/clinical_data.csv\"\n", " \n", " try:\n", " # Try to load existing clinical data file if it exists\n", " clinical_data = pd.read_csv(clinical_data_path, index_col=0)\n", " except FileNotFoundError:\n", " # File doesn't exist, we need to manually construct the clinical data\n", " \n", " # Get the available sample characteristics\n", " sample_char_dict = {\n", " 0: ['subject status: patient with rectal adenocarcinoma'], \n", " 1: ['ajcc score: 0', 'ajcc score: 1', 'ajcc score: 3', 'ajcc score: 2'], \n", " 2: ['gender: Female', 'gender: Male'], \n", " 3: ['overall survival (in days): 3182', 'overall survival (in days): 4584', 'overall survival (in days): 4452', 'overall survival (in days): 3789', 'overall survival (in days): 2960', 'overall survival (in days): 125', 'overall survival (in days): 4027', 'overall survival (in days): 1201', 'overall survival (in days): 403', 'overall survival (in days): 372', 'overall survival (in days): 3949', 'overall survival (in days): 3591', 'overall survival (in days): 647', 'overall survival (in days): 3964', 'overall survival (in days): 3837', 'overall survival (in days): 426', 'overall survival (in days): 2085', 'overall survival (in days): 858', 'overall survival (in days): 1147', 'overall survival (in days): 163', 'overall survival (in days): 3073', 'overall survival (in days): 3741', 'overall survival (in days): 3108', 'overall survival (in days): 3536', 'overall survival (in days): 2251', 'overall survival (in days): 2954', 'overall survival (in days): 2432', 'overall survival (in days): 1470', 'overall survival (in days): 969', 'overall survival (in days): 2000'], \n", " 4: ['dead (1)/alive(0): 0', 'dead (1)/alive(0): 1'], \n", " 5: ['age: 66', 'age: 65', 'age: 51', 'age: 72', 'age: 62', 'age: 50', 'age: 46', 'age: 59', 'age: 63', 'age: 44', 'age: 69', 'age: 41', 'age: 70', 'age: 54', 'age: 48', 'age: 75', 'age: 40', 'age: 47', 'age: 60', 'age: 43', 'age: 57', 'age: 52', 'age: 82']\n", " }\n", " \n", " # Since we can't directly use this data with geo_select_clinical_features,\n", " # we'll inform about the limitation and proceed without the clinical feature extraction\n", " print(\"Clinical data file not found and sample characteristics dictionary format is not compatible with geo_select_clinical_features.\")\n", " print(\"Skipping clinical feature extraction step.\")\n", " \n", " # We can still save the trait information to reflect we did the analysis\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", " # Without the proper clinical data format, we can't proceed with feature extraction\n", " # However, we've documented our analysis of the available variables\n", "else:\n", " print(\"No trait data available for this cohort. Skipping clinical feature extraction.\")\n" ] }, { "cell_type": "markdown", "id": "d40e8108", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "56580b8f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:46:56.049544Z", "iopub.status.busy": "2025-03-25T03:46:56.049408Z", "iopub.status.idle": "2025-03-25T03:46:56.227451Z", "shell.execute_reply": "2025-03-25T03:46:56.227001Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['ILMN_1343289', 'ILMN_1343290', 'ILMN_1343291', 'ILMN_1343292',\n", " 'ILMN_1343293', 'ILMN_1343294', 'ILMN_1343295', 'ILMN_1651199',\n", " 'ILMN_1651209', 'ILMN_1651210', 'ILMN_1651217', 'ILMN_1651221',\n", " 'ILMN_1651228', 'ILMN_1651229', 'ILMN_1651232', 'ILMN_1651234',\n", " 'ILMN_1651235', 'ILMN_1651236', 'ILMN_1651237', 'ILMN_1651238'],\n", " dtype='object', name='ID')\n" ] } ], "source": [ "# 1. First get the file paths\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\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(gene_data.index[:20])\n" ] }, { "cell_type": "markdown", "id": "b2314b90", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "c1f800dc", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:46:56.228711Z", "iopub.status.busy": "2025-03-25T03:46:56.228591Z", "iopub.status.idle": "2025-03-25T03:46:56.230682Z", "shell.execute_reply": "2025-03-25T03:46:56.230308Z" } }, "outputs": [], "source": [ "# Examining gene identifiers\n", "# The gene identifiers shown (ILMN_*) are Illumina probe IDs, not human gene symbols\n", "# These are probe identifiers from Illumina microarray platforms and need to be mapped to gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "0db27c8f", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "0bc08a17", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:46:56.231979Z", "iopub.status.busy": "2025-03-25T03:46:56.231874Z", "iopub.status.idle": "2025-03-25T03:46:59.554041Z", "shell.execute_reply": "2025-03-25T03:46:59.553555Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['ILMN_1725881', 'ILMN_1910180', 'ILMN_1804174', 'ILMN_1810835', 'ILMN_1758197'], 'Species': ['Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens', 'Homo sapiens'], 'Source': ['RefSeq', 'Unigene', 'RefSeq', 'RefSeq', 'RefSeq'], 'Search_Key': ['ILMN_44919', 'ILMN_127219', 'ILMN_139282', 'ILMN_10478', 'ILMN_38756'], 'Transcript': ['ILMN_44919', 'ILMN_127219', 'ILMN_139282', 'ILMN_175835', 'ILMN_38756'], 'ILMN_Gene': ['LOC23117', 'HS.575038', 'FCGR2B', 'SPRR3', 'LOC653895'], 'Source_Reference_ID': ['XM_933824.1', 'Hs.575038', 'XM_938851.1', 'NM_005416.1', 'XM_936379.1'], 'RefSeq_ID': ['XM_933824.1', nan, 'XM_938851.1', 'NM_005416.1', 'XM_936379.1'], 'Unigene_ID': [nan, 'Hs.575038', nan, nan, nan], 'Entrez_Gene_ID': [23117.0, nan, 2213.0, 6707.0, 653895.0], 'GI': [89040007.0, 10437021.0, 88952550.0, 4885606.0, 89033487.0], 'Accession': ['XM_933824.1', 'AK024680', 'XM_938851.1', 'NM_005416.1', 'XM_936379.1'], 'Symbol': ['LOC23117', nan, 'FCGR2B', 'SPRR3', 'LOC653895'], 'Protein_Product': ['XP_938917.1', nan, 'XP_943944.1', 'NP_005407.1', 'XP_941472.1'], 'Array_Address_Id': [2000349.0, 2100682.0, 1500347.0, 2640692.0, 1440273.0], 'Probe_Type': ['I', 'S', 'I', 'S', 'S'], 'Probe_Start': [122.0, 1409.0, 1643.0, 683.0, 26.0], 'SEQUENCE': ['GGCTCCTCTTTGGGCTCCTACTGGAATTTATCAGCCATCAGTGCATCTCT', 'ACACCTTCAGGAGGGAAGCCCTTATTTCTGGGTTGAACTCCCCTTCCATG', 'TAGGGGCAATAGGCTATACGCTACAGCCTAGGTGTGTAGTAGGCCACACC', 'GAAGCCAACCACCAGATGCTGGACACCCTCTTCCCATCTGTTTCTGTGTC', 'TAGCAGGGAGCGGTGAGGGAGAGCGGCTGGATTTCTTGCGGGATCTGCAC'], 'Chromosome': ['16', nan, nan, '1', nan], 'Probe_Chr_Orientation': ['-', nan, nan, '+', nan], 'Probe_Coordinates': ['21766363-21766363:21769901-21769949', nan, nan, '151242655-151242704', nan], 'Definition': ['PREDICTED: Homo sapiens KIAA0220-like protein, transcript variant 11 (LOC23117), mRNA.', 'Homo sapiens cDNA: FLJ21027 fis, clone CAE07110', 'PREDICTED: Homo sapiens Fc fragment of IgG, low affinity IIb, receptor (CD32) (FCGR2B), mRNA.', 'Homo sapiens small proline-rich protein 3 (SPRR3), mRNA.', 'PREDICTED: Homo sapiens similar to protein geranylgeranyltransferase type I, beta subunit (LOC653895), mRNA.'], 'Ontology_Component': [nan, nan, nan, 'cornified envelope [goid 1533] [pmid 15232223] [evidence TAS]', nan], 'Ontology_Process': [nan, nan, nan, 'keratinocyte differentiation [goid 30216] [pmid 8325635] [evidence NAS]; wound healing [goid 42060] [pmid 10510474] [evidence TAS]; epidermis development [goid 8544] [pmid 8325635] [evidence NAS]; keratinization [goid 31424] [evidence IEA]', nan], 'Ontology_Function': [nan, nan, nan, 'structural molecule activity [goid 5198] [pmid 15232223] [evidence TAS]; protein binding [goid 5515] [pmid 10510474] [evidence IPI]', nan], 'Synonyms': [nan, nan, nan, nan, nan], 'GB_ACC': ['XM_933824.1', 'AK024680', 'XM_938851.1', 'NM_005416.1', 'XM_936379.1']}\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": "36ba7ac7", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "63716b7c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:46:59.555286Z", "iopub.status.busy": "2025-03-25T03:46:59.555163Z", "iopub.status.idle": "2025-03-25T03:46:59.715130Z", "shell.execute_reply": "2025-03-25T03:46:59.714673Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First few rows of gene expression data after mapping:\n", " GSM3899156 GSM3899157 GSM3899158 GSM3899159 GSM3899160 \\\n", "Gene \n", "A1BG -2.079415 2.469313 3.714346 -1.130043 1.139405 \n", "A2BP1 -7.374455 -5.796633 -4.384497 14.895651 -11.592012 \n", "A2M 3102.807000 822.027000 1090.359000 3902.472000 807.357700 \n", "A2ML1 5.905322 18.653390 7.037081 15.808300 11.359380 \n", "A3GALT2 62.152650 23.638210 27.884070 39.603730 33.517560 \n", "\n", " GSM3899161 GSM3899162 GSM3899163 GSM3899164 GSM3899165 ... \\\n", "Gene ... \n", "A1BG 8.019033 -0.400172 0.842883 1.740345 2.132458 ... \n", "A2BP1 2.240619 0.723286 -3.208117 33.192579 1.097275 ... \n", "A2M 1564.063000 1489.232000 4251.913000 5816.318000 3845.279000 ... \n", "A2ML1 19.115360 18.825440 8.273081 16.291040 14.401220 ... \n", "A3GALT2 54.589500 49.651930 39.549480 51.881950 76.996070 ... \n", "\n", " GSM3899179 GSM3899180 GSM3899181 GSM3899182 GSM3899183 \\\n", "Gene \n", "A1BG -4.250419 3.291248 -2.436937 -9.960571 5.832194 \n", "A2BP1 1.443925 -20.491803 -3.115439 8.959632 -24.100361 \n", "A2M 1346.345000 1339.646000 1242.412000 808.688900 2223.195000 \n", "A2ML1 9.634692 12.763670 6.886967 -0.115765 15.957160 \n", "A3GALT2 36.077400 24.539670 57.677240 20.876120 13.009720 \n", "\n", " GSM3899184 GSM3899185 GSM3899186 GSM3899187 GSM3899188 \n", "Gene \n", "A1BG -8.423207 -0.275387 -1.146233 -8.095574 0.949727 \n", "A2BP1 -23.093967 10.910762 -12.379070 2.428510 -25.621492 \n", "A2M 1417.869000 1552.090000 1231.369000 1954.387000 783.117200 \n", "A2ML1 8.474412 7.029641 6.405441 8.309864 7.036401 \n", "A3GALT2 11.619830 38.500820 11.514740 25.612070 15.510340 \n", "\n", "[5 rows x 33 columns]\n", "Shape of gene expression data: (18551, 33)\n" ] } ], "source": [ "# 1. Identify columns for gene identifier and gene symbol\n", "probe_col = 'ID' # This is the gene identifier column from the annotation\n", "gene_col = 'Symbol' # This is the gene symbol column from the annotation\n", "\n", "# 2. Extract gene mapping dataframe\n", "gene_mapping = get_gene_mapping(gene_annotation, probe_col, gene_col)\n", "\n", "# 3. Apply gene mapping to convert probe-level data to gene-level expression data\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "\n", "# Print the first few rows of the converted gene expression data\n", "print(\"First few rows of gene expression data after mapping:\")\n", "print(gene_data.head())\n", "print(f\"Shape of gene expression data: {gene_data.shape}\")\n" ] }, { "cell_type": "markdown", "id": "fe922780", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "143b3b05", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:46:59.716457Z", "iopub.status.busy": "2025-03-25T03:46:59.716346Z", "iopub.status.idle": "2025-03-25T03:47:07.396098Z", "shell.execute_reply": "2025-03-25T03:47:07.395691Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical features shape: (3, 33)\n", "Clinical features columns: Index(['GSM3899156', 'GSM3899157', 'GSM3899158', 'GSM3899159', 'GSM3899160',\n", " 'GSM3899161', 'GSM3899162', 'GSM3899163', 'GSM3899164', 'GSM3899165',\n", " 'GSM3899166', 'GSM3899167', 'GSM3899168', 'GSM3899169', 'GSM3899170',\n", " 'GSM3899171', 'GSM3899172', 'GSM3899173', 'GSM3899174', 'GSM3899175',\n", " 'GSM3899176', 'GSM3899177', 'GSM3899178', 'GSM3899179', 'GSM3899180',\n", " 'GSM3899181', 'GSM3899182', 'GSM3899183', 'GSM3899184', 'GSM3899185',\n", " 'GSM3899186', 'GSM3899187', 'GSM3899188'],\n", " dtype='object')\n", "Normalized gene data shape: (17736, 33)\n", "First few normalized gene symbols: ['A1BG', 'A2M', 'A2ML1', 'A3GALT2', 'A4GALT', 'A4GNT', 'AAA1', 'AAAS', 'AACS', 'AACSP1']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Rectal_Cancer/gene_data/GSE133057.csv\n", "Linked data shape: (33, 17739)\n", " Rectal_Cancer Age Gender A1BG A2M A2ML1 \\\n", "GSM3899156 0.0 66.0 0.0 -2.079415 3102.8070 5.905322 \n", "GSM3899157 1.0 65.0 0.0 2.469313 822.0270 18.653390 \n", "GSM3899158 0.0 51.0 0.0 3.714346 1090.3590 7.037081 \n", "GSM3899159 1.0 72.0 1.0 -1.130043 3902.4720 15.808300 \n", "GSM3899160 0.0 62.0 1.0 1.139405 807.3577 11.359380 \n", "\n", " A3GALT2 A4GALT A4GNT AAA1 ... ZWILCH \\\n", "GSM3899156 62.15265 67.30495 4.375281 21.173169 ... 87.248004 \n", "GSM3899157 23.63821 94.85796 23.498130 26.904985 ... 54.572260 \n", "GSM3899158 27.88407 46.07135 3.783252 25.711841 ... 114.227830 \n", "GSM3899159 39.60373 65.85915 7.583620 35.614006 ... 50.675996 \n", "GSM3899160 33.51756 50.62852 6.398035 11.417144 ... 70.019329 \n", "\n", " ZWINT ZXDA ZXDB ZXDC ZYG11A ZYG11B \\\n", "GSM3899156 470.137847 29.438866 362.0236 187.1541 5.918446 974.7338 \n", "GSM3899157 247.512435 2.853164 466.5786 166.0419 -0.929902 555.2162 \n", "GSM3899158 468.537190 10.334295 251.8780 188.2291 -6.795482 544.2828 \n", "GSM3899159 200.728082 27.033568 287.0269 130.5297 4.108545 1079.5270 \n", "GSM3899160 523.624952 1.451242 170.1016 114.5128 -5.410784 928.1462 \n", "\n", " ZYX ZZEF1 ZZZ3 \n", "GSM3899156 956.6224 613.2874 680.1956 \n", "GSM3899157 629.5185 249.9760 704.0856 \n", "GSM3899158 1085.9570 286.2926 678.5345 \n", "GSM3899159 1072.8950 746.2970 542.6454 \n", "GSM3899160 1923.6030 449.1529 699.5126 \n", "\n", "[5 rows x 17739 columns]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Shape after handling missing values: (33, 17739)\n", "Quartiles for 'Rectal_Cancer':\n", " 25%: 1.0\n", " 50% (Median): 2.0\n", " 75%: 2.0\n", "Min: 0.0\n", "Max: 3.0\n", "The distribution of the feature 'Rectal_Cancer' in this dataset is fine.\n", "\n", "Quartiles for 'Age':\n", " 25%: 48.0\n", " 50% (Median): 54.0\n", " 75%: 65.0\n", "Min: 40.0\n", "Max: 82.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 10 occurrences. This represents 30.30% of the dataset.\n", "The distribution of the feature 'Gender' in this dataset is fine.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Rectal_Cancer/GSE133057.csv\n" ] } ], "source": [ "# 1. Let's extract clinical data from the matrix file again to get the correct format\n", "# First get the file paths\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# Get background info and clinical 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", "# Extract clinical features using the correct row indices and conversion functions\n", "clinical_features = geo_select_clinical_features(\n", " clinical_df=clinical_data,\n", " trait=trait,\n", " trait_row=1, # AJCC score is in row 1\n", " convert_trait=convert_trait, # Use the previously defined function\n", " age_row=5,\n", " convert_age=convert_age,\n", " gender_row=2,\n", " convert_gender=convert_gender\n", ")\n", "\n", "# Save clinical features again with proper format\n", "os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", "clinical_features.to_csv(out_clinical_data_file)\n", "print(f\"Clinical features shape: {clinical_features.shape}\")\n", "print(f\"Clinical features columns: {clinical_features.columns}\")\n", "\n", "# 1. Normalize gene symbols in the gene expression data\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Normalized gene data shape: {normalized_gene_data.shape}\")\n", "print(f\"First few normalized gene symbols: {list(normalized_gene_data.index[:10])}\")\n", "\n", "# Save the normalized 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. Link the clinical and genetic data\n", "linked_data = geo_link_clinical_genetic_data(clinical_features, normalized_gene_data)\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_processed = handle_missing_values(linked_data, trait)\n", "print(f\"Shape after handling missing values: {linked_data_processed.shape}\")\n", "\n", "# Add validation check - if no samples remain, note the issue\n", "if linked_data_processed.shape[0] == 0:\n", " print(\"No samples remain after handling missing values. The dataset cannot be processed further.\")\n", " is_trait_biased = True # Mark as biased since we can't use it\n", " unbiased_linked_data = linked_data_processed\n", "else:\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_processed, 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=\"Dataset contains gene expression data from rectal cancer patients with AJCC scores, focusing on response to chemoradiotherapy.\"\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.\")" ] } ], "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 }