{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "6e6c9a15", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:22:25.268228Z", "iopub.status.busy": "2025-03-25T05:22:25.267904Z", "iopub.status.idle": "2025-03-25T05:22:25.459477Z", "shell.execute_reply": "2025-03-25T05:22:25.459024Z" } }, "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 = \"Glioblastoma\"\n", "cohort = \"GSE178236\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Glioblastoma\"\n", "in_cohort_dir = \"../../input/GEO/Glioblastoma/GSE178236\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Glioblastoma/GSE178236.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Glioblastoma/gene_data/GSE178236.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Glioblastoma/clinical_data/GSE178236.csv\"\n", "json_path = \"../../output/preprocess/Glioblastoma/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "b8e5aba6", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "67d818a4", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:22:25.460956Z", "iopub.status.busy": "2025-03-25T05:22:25.460802Z", "iopub.status.idle": "2025-03-25T05:22:25.644117Z", "shell.execute_reply": "2025-03-25T05:22:25.643502Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Genome-wide gene expression analysis in 84 neurospheres derived from as many primary glioblastomas\"\n", "!Series_summary\t\"Gene expression analysis of neurosphere from primary glioblastoma tissue\"\n", "!Series_overall_design\t\"Total RNA obtained from neurospheres. Gene expression profile has been performed to investigate biological properties in neurospheres isolated from independent patients\"\n", "Sample Characteristics Dictionary:\n", "{0: ['cell type: neurosphere'], 1: ['gender: male', 'gender: female'], 2: ['age: 57', 'age: 66', 'age: 17', 'age: 76', 'age: 73', 'age: 63', 'age: 67', 'age: 72', 'age: 49', 'age: 54', 'age: 62', 'age: 53', 'age: 27', 'age: 52', 'age: 48', 'age: 74', 'age: 55', 'age: 50', 'age: 65', 'age: 70', 'age: 61', 'age: 71', 'age: 47', 'age: 64', 'age: 68', 'age: 60', 'age: 45', 'age: 37', 'age: 58', 'age: 75'], 3: ['neurosphere derivation time: First surgery'], 4: ['previous treatments: Treatment naive'], 5: ['idh status: wt', 'idh status: mut'], 6: ['tert promoter status: c.1-124C>T', 'tert promoter status: c.1-146C>T', 'tert promoter status: wt', 'tert promoter status: NA']}\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": "97ef9a89", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "8b9a3bab", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:22:25.645419Z", "iopub.status.busy": "2025-03-25T05:22:25.645304Z", "iopub.status.idle": "2025-03-25T05:22:25.661293Z", "shell.execute_reply": "2025-03-25T05:22:25.660813Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical Data Preview:\n", "{'GSM5385026': [1.0, 57.0, 1.0], 'GSM5385027': [1.0, 57.0, 1.0], 'GSM5385028': [1.0, 66.0, 0.0], 'GSM5385029': [1.0, 66.0, 0.0], 'GSM5385030': [1.0, 17.0, 0.0], 'GSM5385031': [1.0, 17.0, 0.0], 'GSM5385032': [1.0, 76.0, 1.0], 'GSM5385033': [1.0, 76.0, 1.0], 'GSM5385034': [1.0, 73.0, 1.0], 'GSM5385035': [1.0, 73.0, 1.0], 'GSM5385036': [1.0, 63.0, 1.0], 'GSM5385037': [1.0, 63.0, 1.0], 'GSM5385038': [1.0, 67.0, 1.0], 'GSM5385039': [1.0, 67.0, 1.0], 'GSM5385040': [1.0, 72.0, 1.0], 'GSM5385041': [1.0, 72.0, 1.0], 'GSM5385042': [1.0, 49.0, 1.0], 'GSM5385043': [1.0, 49.0, 1.0], 'GSM5385044': [1.0, 54.0, 1.0], 'GSM5385045': [1.0, 54.0, 1.0], 'GSM5385046': [1.0, 62.0, 1.0], 'GSM5385047': [1.0, 62.0, 0.0], 'GSM5385048': [1.0, 62.0, 0.0], 'GSM5385049': [1.0, 53.0, 1.0], 'GSM5385050': [1.0, 53.0, 1.0], 'GSM5385051': [1.0, 63.0, 1.0], 'GSM5385052': [1.0, 63.0, 1.0], 'GSM5385053': [1.0, 27.0, 1.0], 'GSM5385054': [1.0, 27.0, 1.0], 'GSM5385055': [1.0, 52.0, 1.0], 'GSM5385056': [1.0, 52.0, 1.0], 'GSM5385057': [1.0, 48.0, 1.0], 'GSM5385058': [1.0, 48.0, 1.0], 'GSM5385059': [1.0, 74.0, 1.0], 'GSM5385060': [1.0, 74.0, 1.0], 'GSM5385061': [1.0, 62.0, 0.0], 'GSM5385062': [1.0, 57.0, 1.0], 'GSM5385063': [1.0, 27.0, 1.0], 'GSM5385064': [1.0, 17.0, 0.0], 'GSM5385065': [1.0, 57.0, 1.0], 'GSM5385066': [1.0, 55.0, 1.0], 'GSM5385067': [1.0, 50.0, 1.0], 'GSM5385068': [1.0, 65.0, 1.0], 'GSM5385069': [1.0, 65.0, 1.0], 'GSM5385070': [1.0, 70.0, 1.0], 'GSM5385071': [1.0, 57.0, 1.0], 'GSM5385072': [1.0, 52.0, 1.0], 'GSM5385073': [1.0, 63.0, 1.0], 'GSM5385074': [1.0, 62.0, 0.0], 'GSM5385075': [1.0, 17.0, 0.0], 'GSM5385076': [1.0, 76.0, 1.0], 'GSM5385077': [1.0, 61.0, 0.0], 'GSM5385078': [1.0, 71.0, 0.0], 'GSM5385079': [1.0, 65.0, 0.0], 'GSM5385080': [1.0, 47.0, 1.0], 'GSM5385081': [1.0, 67.0, 0.0], 'GSM5385082': [1.0, 64.0, 1.0], 'GSM5385083': [1.0, 68.0, 1.0], 'GSM5385084': [1.0, 48.0, 1.0], 'GSM5385085': [1.0, 65.0, 0.0], 'GSM5385086': [1.0, 73.0, 1.0], 'GSM5385087': [1.0, 48.0, 1.0], 'GSM5385088': [1.0, 64.0, 1.0], 'GSM5385089': [1.0, 70.0, 0.0], 'GSM5385090': [1.0, 74.0, 1.0], 'GSM5385091': [1.0, 60.0, 1.0], 'GSM5385092': [1.0, 45.0, 1.0], 'GSM5385093': [1.0, 37.0, 0.0], 'GSM5385094': [1.0, 58.0, 1.0], 'GSM5385095': [1.0, 75.0, 1.0], 'GSM5385096': [1.0, 60.0, 1.0], 'GSM5385097': [1.0, 69.0, 1.0], 'GSM5385098': [1.0, 66.0, 1.0], 'GSM5385099': [1.0, 72.0, 1.0], 'GSM5385100': [1.0, 55.0, 1.0], 'GSM5385101': [1.0, 41.0, 1.0], 'GSM5385102': [1.0, 36.0, 1.0], 'GSM5385103': [1.0, 59.0, 1.0], 'GSM5385104': [1.0, 45.0, 0.0], 'GSM5385105': [1.0, 39.0, 0.0], 'GSM5385106': [1.0, 61.0, 1.0], 'GSM5385107': [1.0, 69.0, 1.0], 'GSM5385108': [1.0, 58.0, 0.0], 'GSM5385109': [1.0, 68.0, 1.0], 'GSM5385110': [1.0, 53.0, 1.0], 'GSM5385111': [1.0, 63.0, 1.0], 'GSM5385112': [1.0, 68.0, 0.0], 'GSM5385113': [1.0, 70.0, 1.0], 'GSM5385114': [1.0, 73.0, 0.0], 'GSM5385115': [1.0, 43.0, 1.0], 'GSM5385116': [1.0, 64.0, 0.0], 'GSM5385117': [1.0, 57.0, 1.0], 'GSM5385118': [1.0, 58.0, 1.0], 'GSM5385119': [1.0, 37.0, 0.0], 'GSM5385120': [1.0, 46.0, 0.0], 'GSM5385121': [1.0, 48.0, 1.0], 'GSM5385122': [1.0, 37.0, 0.0], 'GSM5385123': [1.0, 67.0, 1.0], 'GSM5385124': [1.0, 54.0, 1.0], 'GSM5385125': [1.0, 68.0, 1.0], 'GSM5385126': [1.0, 53.0, 0.0], 'GSM5385127': [1.0, 61.0, 1.0], 'GSM5385128': [1.0, 63.0, 1.0], 'GSM5385129': [1.0, 63.0, 1.0], 'GSM5385130': [1.0, 7.0, 1.0], 'GSM5385131': [1.0, 58.0, 0.0], 'GSM5385132': [1.0, 44.0, 0.0], 'GSM5385133': [1.0, 74.0, 1.0], 'GSM5385134': [1.0, 70.0, 1.0], 'GSM5385135': [1.0, 54.0, 1.0], 'GSM5385136': [1.0, 74.0, 1.0], 'GSM5385137': [1.0, 47.0, 1.0], 'GSM5385138': [1.0, 48.0, 1.0]}\n", "Clinical data saved to ../../output/preprocess/Glioblastoma/clinical_data/GSE178236.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the Series information, this dataset contains gene expression data from glioblastoma neurospheres\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# For trait: We can infer that all samples have glioblastoma from the Series description\n", "# For gender: It's available at key 1\n", "# For age: It's available at key 2\n", "trait_row = 0 # We'll use cell type: neurosphere as a proxy for trait\n", "gender_row = 1\n", "age_row = 2\n", "\n", "# 2.2 Data Type Conversion Functions\n", "def convert_trait(trait_str):\n", " # Since all samples are glioblastoma neurospheres\n", " return 1 # All samples have the trait\n", "\n", "def convert_gender(gender_str):\n", " # Extract value after colon\n", " if gender_str is None:\n", " return None\n", " \n", " gender_value = gender_str.split(\":\")[-1].strip().lower()\n", " \n", " if \"female\" in gender_value:\n", " return 0\n", " elif \"male\" in gender_value:\n", " return 1\n", " else:\n", " return None\n", "\n", "def convert_age(age_str):\n", " # Extract the numeric age value\n", " if age_str is None:\n", " return None\n", " \n", " try:\n", " # Extract number after colon\n", " age_value = age_str.split(\":\")[-1].strip()\n", " return float(age_value)\n", " except:\n", " return None\n", "\n", "# 3. Save Metadata \n", "# All samples have glioblastoma as indicated by the series description\n", "is_trait_available = True\n", "\n", "# Initial filtering of dataset 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", "# Extract available clinical features\n", "clinical_features = 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 data\n", "preview = preview_df(clinical_features)\n", "print(\"Clinical Data Preview:\")\n", "print(preview)\n", "\n", "# Save the clinical data\n", "clinical_features.to_csv(out_clinical_data_file, index=True)\n", "print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "c10f98f0", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "0f386d85", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:22:25.662465Z", "iopub.status.busy": "2025-03-25T05:22:25.662359Z", "iopub.status.idle": "2025-03-25T05:22:26.060320Z", "shell.execute_reply": "2025-03-25T05:22:26.059757Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found data marker at line 63\n", "Header line: \"ID_REF\"\t\"GSM5385026\"\t\"GSM5385027\"\t\"GSM5385028\"\t\"GSM5385029\"\t\"GSM5385030\"\t\"GSM5385031\"\t\"GSM5385032\"\t\"GSM5385033\"\t\"GSM5385034\"\t\"GSM5385035\"\t\"GSM5385036\"\t\"GSM5385037\"\t\"GSM5385038\"\t\"GSM5385039\"\t\"GSM5385040\"\t\"GSM5385041\"\t\"GSM5385042\"\t\"GSM5385043\"\t\"GSM5385044\"\t\"GSM5385045\"\t\"GSM5385046\"\t\"GSM5385047\"\t\"GSM5385048\"\t\"GSM5385049\"\t\"GSM5385050\"\t\"GSM5385051\"\t\"GSM5385052\"\t\"GSM5385053\"\t\"GSM5385054\"\t\"GSM5385055\"\t\"GSM5385056\"\t\"GSM5385057\"\t\"GSM5385058\"\t\"GSM5385059\"\t\"GSM5385060\"\t\"GSM5385061\"\t\"GSM5385062\"\t\"GSM5385063\"\t\"GSM5385064\"\t\"GSM5385065\"\t\"GSM5385066\"\t\"GSM5385067\"\t\"GSM5385068\"\t\"GSM5385069\"\t\"GSM5385070\"\t\"GSM5385071\"\t\"GSM5385072\"\t\"GSM5385073\"\t\"GSM5385074\"\t\"GSM5385075\"\t\"GSM5385076\"\t\"GSM5385077\"\t\"GSM5385078\"\t\"GSM5385079\"\t\"GSM5385080\"\t\"GSM5385081\"\t\"GSM5385082\"\t\"GSM5385083\"\t\"GSM5385084\"\t\"GSM5385085\"\t\"GSM5385086\"\t\"GSM5385087\"\t\"GSM5385088\"\t\"GSM5385089\"\t\"GSM5385090\"\t\"GSM5385091\"\t\"GSM5385092\"\t\"GSM5385093\"\t\"GSM5385094\"\t\"GSM5385095\"\t\"GSM5385096\"\t\"GSM5385097\"\t\"GSM5385098\"\t\"GSM5385099\"\t\"GSM5385100\"\t\"GSM5385101\"\t\"GSM5385102\"\t\"GSM5385103\"\t\"GSM5385104\"\t\"GSM5385105\"\t\"GSM5385106\"\t\"GSM5385107\"\t\"GSM5385108\"\t\"GSM5385109\"\t\"GSM5385110\"\t\"GSM5385111\"\t\"GSM5385112\"\t\"GSM5385113\"\t\"GSM5385114\"\t\"GSM5385115\"\t\"GSM5385116\"\t\"GSM5385117\"\t\"GSM5385118\"\t\"GSM5385119\"\t\"GSM5385120\"\t\"GSM5385121\"\t\"GSM5385122\"\t\"GSM5385123\"\t\"GSM5385124\"\t\"GSM5385125\"\t\"GSM5385126\"\t\"GSM5385127\"\t\"GSM5385128\"\t\"GSM5385129\"\t\"GSM5385130\"\t\"GSM5385131\"\t\"GSM5385132\"\t\"GSM5385133\"\t\"GSM5385134\"\t\"GSM5385135\"\t\"GSM5385136\"\t\"GSM5385137\"\t\"GSM5385138\"\n", "First data line: \"ILMN_1343291\"\t55044.8\t52638.8\t58064.3\t53104.6\t57417.7\t51822.7\t57244.3\t50600.3\t57467.7\t54333.4\t52795.8\t52189.6\t38947.2\t54822.8\t52492.7\t65739.8\t53561.7\t55831.7\t52884.1\t69366.8\t53279\t51899.4\t54446.1\t53694.8\t55462.8\t46816.1\t51147.9\t57329.4\t55495.2\t52799.1\t53556.1\t58991.4\t59270.8\t55320\t55137.5\t49339\t39994.9\t46960.5\t42735.7\t45297.9\t53603.7\t46113.1\t49536\t47905.1\t50710.3\t48135.4\t50390.6\t51987.9\t51313.4\t47958\t58541.9\t62422.8\t60673\t55116.2\t53824.9\t52682.4\t52705.2\t50983.5\t56191.3\t55081.7\t53368.1\t49620.5\t53317.5\t58133.5\t62620.9\t55067.7\t58496.2\t58959.6\t53173.3\t53606.9\t53477.9\t58678.9\t58095.1\t42485.5\t51447.7\t35548\t52251\t49665.9\t42354\t52042.2\t55165.3\t46464.6\t53019.7\t56478.6\t51746.6\t52383.9\t55665.4\t55546.8\t53825.6\t55296.5\t53414.2\t56448\t51110.9\t51787.2\t53686.6\t55460.5\t59456\t53863.2\t51921.6\t50641.1\t55506.5\t52564.9\t63259.5\t54883.6\t53348\t54686.9\t55042.5\t55543.1\t54981.2\t66467.6\t54492.4\t55537.9\t54727.6\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Index(['ILMN_1343291', 'ILMN_1343295', 'ILMN_1651199', 'ILMN_1651209',\n", " 'ILMN_1651210', 'ILMN_1651221', 'ILMN_1651228', 'ILMN_1651229',\n", " 'ILMN_1651230', 'ILMN_1651232', 'ILMN_1651235', 'ILMN_1651236',\n", " 'ILMN_1651237', 'ILMN_1651238', 'ILMN_1651249', 'ILMN_1651253',\n", " 'ILMN_1651254', 'ILMN_1651259', 'ILMN_1651260', 'ILMN_1651262'],\n", " dtype='object', name='ID')\n" ] } ], "source": [ "# 1. Get the file paths for the SOFT file and matrix file\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. First, let's examine the structure of the matrix file to understand its format\n", "import gzip\n", "\n", "# Peek at the first few lines of the file to understand its structure\n", "with gzip.open(matrix_file, 'rt') as file:\n", " # Read first 100 lines to find the header structure\n", " for i, line in enumerate(file):\n", " if '!series_matrix_table_begin' in line:\n", " print(f\"Found data marker at line {i}\")\n", " # Read the next line which should be the header\n", " header_line = next(file)\n", " print(f\"Header line: {header_line.strip()}\")\n", " # And the first data line\n", " first_data_line = next(file)\n", " print(f\"First data line: {first_data_line.strip()}\")\n", " break\n", " if i > 100: # Limit search to first 100 lines\n", " print(\"Matrix table marker not found in first 100 lines\")\n", " break\n", "\n", "# 3. Now try to get the genetic data with better error handling\n", "try:\n", " gene_data = get_genetic_data(matrix_file)\n", " print(gene_data.index[:20])\n", "except KeyError as e:\n", " print(f\"KeyError: {e}\")\n", " \n", " # Alternative approach: manually extract the data\n", " print(\"\\nTrying alternative approach to read the gene data:\")\n", " with gzip.open(matrix_file, 'rt') as file:\n", " # Find the start of the data\n", " for line in file:\n", " if '!series_matrix_table_begin' in line:\n", " break\n", " \n", " # Read the headers and data\n", " import pandas as pd\n", " df = pd.read_csv(file, sep='\\t', index_col=0)\n", " print(f\"Column names: {df.columns[:5]}\")\n", " print(f\"First 20 row IDs: {df.index[:20]}\")\n", " gene_data = df\n" ] }, { "cell_type": "markdown", "id": "5bdfcb07", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "6231c2d7", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:22:26.061568Z", "iopub.status.busy": "2025-03-25T05:22:26.061456Z", "iopub.status.idle": "2025-03-25T05:22:26.063800Z", "shell.execute_reply": "2025-03-25T05:22:26.063353Z" } }, "outputs": [], "source": [ "# Looking at the gene identifiers in the previous output\n", "# These identifiers (ILMN_1343291, ILMN_1343295, etc.) are Illumina probe IDs,\n", "# not standard human gene symbols.\n", "# The \"ILMN_\" prefix indicates these are Illumina BeadArray probes\n", "# that need to be mapped to human gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "b9f372d3", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "972a8fb0", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:22:26.064825Z", "iopub.status.busy": "2025-03-25T05:22:26.064723Z", "iopub.status.idle": "2025-03-25T05:22:26.957183Z", "shell.execute_reply": "2025-03-25T05:22:26.956606Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Examining SOFT file structure:\n", "Line 0: ^DATABASE = GeoMiame\n", "Line 1: !Database_name = Gene Expression Omnibus (GEO)\n", "Line 2: !Database_institute = NCBI NLM NIH\n", "Line 3: !Database_web_link = http://www.ncbi.nlm.nih.gov/geo\n", "Line 4: !Database_email = geo@ncbi.nlm.nih.gov\n", "Line 5: ^SERIES = GSE178236\n", "Line 6: !Series_title = Genome-wide gene expression analysis in 84 neurospheres derived from as many primary glioblastomas\n", "Line 7: !Series_geo_accession = GSE178236\n", "Line 8: !Series_status = Public on Jul 27 2021\n", "Line 9: !Series_submission_date = Jun 15 2021\n", "Line 10: !Series_last_update_date = Oct 27 2021\n", "Line 11: !Series_pubmed_id = 34320350\n", "Line 12: !Series_summary = Gene expression analysis of neurosphere from primary glioblastoma tissue\n", "Line 13: !Series_overall_design = Total RNA obtained from neurospheres. Gene expression profile has been performed to investigate biological properties in neurospheres isolated from independent patients\n", "Line 14: !Series_type = Expression profiling by array\n", "Line 15: !Series_contributor = Carla,,Boccaccio\n", "Line 16: !Series_contributor = Francesca,,De Bacco\n", "Line 17: !Series_contributor = Francesca,,Orzan\n", "Line 18: !Series_sample_id = GSM5385026\n", "Line 19: !Series_sample_id = GSM5385027\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene annotation preview:\n", "{'ID': ['ILMN_1343048', 'ILMN_1343049', 'ILMN_1343050', 'ILMN_1343052', 'ILMN_1343059'], 'Species': [nan, nan, nan, nan, nan], 'Source': [nan, nan, nan, nan, nan], 'Search_Key': [nan, nan, nan, nan, nan], 'Transcript': [nan, nan, nan, nan, nan], 'ILMN_Gene': [nan, nan, nan, nan, nan], 'Source_Reference_ID': [nan, nan, nan, nan, nan], 'RefSeq_ID': [nan, nan, nan, nan, nan], 'Unigene_ID': [nan, nan, nan, nan, nan], 'Entrez_Gene_ID': [nan, nan, nan, nan, nan], 'GI': [nan, nan, nan, nan, nan], 'Accession': [nan, nan, nan, nan, nan], 'Symbol': ['phage_lambda_genome', 'phage_lambda_genome', 'phage_lambda_genome:low', 'phage_lambda_genome:low', 'thrB'], 'Protein_Product': [nan, nan, nan, nan, 'thrB'], 'Probe_Id': [nan, nan, nan, nan, nan], 'Array_Address_Id': [5090180, 6510136, 7560739, 1450438, 1240647], 'Probe_Type': [nan, nan, nan, nan, nan], 'Probe_Start': [nan, nan, nan, nan, nan], 'SEQUENCE': ['GAATAAAGAACAATCTGCTGATGATCCCTCCGTGGATCTGATTCGTGTAA', 'CCATGTGATACGAGGGCGCGTAGTTTGCATTATCGTTTTTATCGTTTCAA', 'CCGACAGATGTATGTAAGGCCAACGTGCTCAAATCTTCATACAGAAAGAT', 'TCTGTCACTGTCAGGAAAGTGGTAAAACTGCAACTCAATTACTGCAATGC', 'CTTGTGCCTGAGCTGTCAAAAGTAGAGCACGTCGCCGAGATGAAGGGCGC'], 'Chromosome': [nan, nan, nan, nan, nan], 'Probe_Chr_Orientation': [nan, nan, nan, nan, nan], 'Probe_Coordinates': [nan, nan, nan, nan, nan], 'Cytoband': [nan, nan, nan, nan, nan], 'Definition': [nan, nan, nan, nan, nan], 'Ontology_Component': [nan, nan, nan, nan, nan], 'Ontology_Process': [nan, nan, nan, nan, nan], 'Ontology_Function': [nan, nan, nan, nan, nan], 'Synonyms': [nan, nan, nan, nan, nan], 'Obsolete_Probe_Id': [nan, nan, nan, nan, nan], 'GB_ACC': [nan, nan, nan, nan, nan]}\n" ] } ], "source": [ "# 1. Let's first examine the structure of the SOFT file before trying to parse it\n", "import gzip\n", "\n", "# Look at the first few lines of the SOFT file to understand its structure\n", "print(\"Examining SOFT file structure:\")\n", "try:\n", " with gzip.open(soft_file, 'rt') as file:\n", " # Read first 20 lines to understand the file structure\n", " for i, line in enumerate(file):\n", " if i < 20:\n", " print(f\"Line {i}: {line.strip()}\")\n", " else:\n", " break\n", "except Exception as e:\n", " print(f\"Error reading SOFT file: {e}\")\n", "\n", "# 2. Now let's try a more robust approach to extract the gene annotation\n", "# Instead of using the library function which failed, we'll implement a custom approach\n", "try:\n", " # First, look for the platform section which contains gene annotation\n", " platform_data = []\n", " with gzip.open(soft_file, 'rt') as file:\n", " in_platform_section = False\n", " for line in file:\n", " if line.startswith('^PLATFORM'):\n", " in_platform_section = True\n", " continue\n", " if in_platform_section and line.startswith('!platform_table_begin'):\n", " # Next line should be the header\n", " header = next(file).strip()\n", " platform_data.append(header)\n", " # Read until the end of the platform table\n", " for table_line in file:\n", " if table_line.startswith('!platform_table_end'):\n", " break\n", " platform_data.append(table_line.strip())\n", " break\n", " \n", " # If we found platform data, convert it to a DataFrame\n", " if platform_data:\n", " import pandas as pd\n", " import io\n", " platform_text = '\\n'.join(platform_data)\n", " gene_annotation = pd.read_csv(io.StringIO(platform_text), delimiter='\\t', \n", " low_memory=False, on_bad_lines='skip')\n", " print(\"\\nGene annotation preview:\")\n", " print(preview_df(gene_annotation))\n", " else:\n", " print(\"Could not find platform table in SOFT file\")\n", " \n", " # Try an alternative approach - extract mapping from other sections\n", " with gzip.open(soft_file, 'rt') as file:\n", " for line in file:\n", " if 'ANNOTATION information' in line or 'annotation information' in line:\n", " print(f\"Found annotation information: {line.strip()}\")\n", " if line.startswith('!Platform_title') or line.startswith('!platform_title'):\n", " print(f\"Platform title: {line.strip()}\")\n", " \n", "except Exception as e:\n", " print(f\"Error processing gene annotation: {e}\")\n" ] }, { "cell_type": "markdown", "id": "1991c4d7", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "31fd23d1", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:22:26.958680Z", "iopub.status.busy": "2025-03-25T05:22:26.958556Z", "iopub.status.idle": "2025-03-25T05:22:28.217736Z", "shell.execute_reply": "2025-03-25T05:22:28.217274Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene mapping dataframe shape: (44837, 2)\n", "Gene mapping preview:\n", "{'ID': ['ILMN_1343048', 'ILMN_1343049', 'ILMN_1343050', 'ILMN_1343052', 'ILMN_1343059'], 'Gene': ['phage_lambda_genome', 'phage_lambda_genome', 'phage_lambda_genome:low', 'phage_lambda_genome:low', 'thrB']}\n", "Gene expression dataframe shape after mapping: (21464, 113)\n", "Gene expression data preview:\n", "{'GSM5385026': [478.5, 578.9, 558.5, 749.8, 254.4], 'GSM5385027': [517.7, 558.9, 612.1, 718.3, 270.9], 'GSM5385028': [401.29999999999995, 678.7, 565.0, 709.4, 543.8], 'GSM5385029': [396.5, 642.4, 610.3, 705.2, 621.0], 'GSM5385030': [515.6, 593.4, 619.8, 755.2, 883.0], 'GSM5385031': [454.6, 581.8, 615.3, 691.5, 802.5], 'GSM5385032': [570.9, 556.4, 541.0, 750.7, 183.8], 'GSM5385033': [428.2, 559.3, 673.6, 714.6, 171.9], 'GSM5385034': [483.20000000000005, 583.5, 573.9, 721.6, 162.6], 'GSM5385035': [647.5, 571.8, 639.4, 728.7, 545.8], 'GSM5385036': [439.70000000000005, 541.6, 612.5, 778.4, 208.5], 'GSM5385037': [446.20000000000005, 634.7, 667.8, 710.6, 196.1], 'GSM5385038': [424.29999999999995, 547.8, 568.6, 758.5, 154.5], 'GSM5385039': [358.29999999999995, 619.8, 545.0, 742.3, 178.1], 'GSM5385040': [483.5, 587.8, 647.4, 794.2, 351.7], 'GSM5385041': [501.1, 560.4, 754.6, 715.6, 183.6], 'GSM5385042': [425.5, 593.2, 602.2, 732.4, 358.4], 'GSM5385043': [430.79999999999995, 553.6, 554.5999999999999, 794.2, 235.8], 'GSM5385044': [402.70000000000005, 557.8, 611.9, 716.5, 406.4], 'GSM5385045': [481.8, 570.0, 585.3, 721.4, 269.6], 'GSM5385046': [610.8, 573.9, 656.2, 750.0, 378.4], 'GSM5385047': [419.9, 5257.9, 593.0, 712.4, 269.1], 'GSM5385048': [362.2, 589.5, 536.9, 746.6, 179.9], 'GSM5385049': [430.0, 534.3, 642.9, 772.9, 380.6], 'GSM5385050': [477.1, 535.9, 606.8, 796.0, 415.2], 'GSM5385051': [481.3, 585.7, 608.4, 724.5, 179.9], 'GSM5385052': [466.6, 591.1, 618.3, 733.5, 179.1], 'GSM5385053': [445.9, 645.5, 537.1, 677.2, 275.0], 'GSM5385054': [485.3, 569.4, 588.3, 742.4, 190.6], 'GSM5385055': [500.5, 563.0, 563.5, 780.3, 566.1], 'GSM5385056': [435.5, 600.4, 558.7, 794.1, 407.2], 'GSM5385057': [548.2, 533.3, 568.9, 708.7, 207.0], 'GSM5385058': [418.70000000000005, 619.7, 782.1, 772.8, 187.8], 'GSM5385059': [377.2, 628.7, 680.9, 745.3, 291.1], 'GSM5385060': [468.79999999999995, 677.0999999999999, 592.2, 808.2, 166.1], 'GSM5385061': [380.4, 5097.2, 528.2, 729.9, 257.7], 'GSM5385062': [480.5, 562.0, 527.9, 738.5, 286.2], 'GSM5385063': [515.9, 619.6, 632.5, 731.5, 267.7], 'GSM5385064': [431.79999999999995, 621.8, 590.3, 710.6, 1014.9], 'GSM5385065': [410.1, 572.4000000000001, 570.8, 741.0, 231.3], 'GSM5385066': [562.4, 540.5, 550.1, 739.5, 239.8], 'GSM5385067': [541.4000000000001, 591.7, 586.2, 726.5, 501.4], 'GSM5385068': [436.3, 574.9, 544.4, 788.0, 178.8], 'GSM5385069': [568.7, 576.9, 548.6, 714.5, 398.2], 'GSM5385070': [484.6, 557.6, 608.2, 699.7, 276.3], 'GSM5385071': [522.7, 611.6, 540.6, 702.8, 754.7], 'GSM5385072': [560.0, 560.6, 600.9, 723.6, 209.8], 'GSM5385073': [420.8, 569.3, 537.2, 741.5, 210.1], 'GSM5385074': [565.0, 563.3, 606.9, 719.4, 535.8], 'GSM5385075': [460.6, 593.0, 578.8, 730.2, 739.0], 'GSM5385076': [467.6, 569.7, 527.9, 701.5, 143.5], 'GSM5385077': [478.7, 547.0, 563.2, 701.4, 207.1], 'GSM5385078': [516.6, 578.8, 539.5, 695.3, 204.4], 'GSM5385079': [446.0, 539.4, 558.8, 720.1, 380.8], 'GSM5385080': [493.70000000000005, 600.1, 557.6, 780.1, 226.9], 'GSM5385081': [459.5, 588.6, 574.4, 719.4000000000001, 432.7], 'GSM5385082': [470.79999999999995, 564.3, 557.2, 771.5999999999999, 295.4], 'GSM5385083': [560.5999999999999, 583.4, 558.4, 707.9, 665.8], 'GSM5385084': [681.6, 592.4, 579.0, 687.6, 195.1], 'GSM5385085': [495.7, 526.0, 556.1, 734.2, 220.8], 'GSM5385086': [450.7, 580.8, 549.6, 721.7, 165.8], 'GSM5385087': [559.3, 527.6, 638.7, 672.7, 167.7], 'GSM5385088': [462.8, 587.0, 547.7, 761.1, 237.4], 'GSM5385089': [430.4, 565.7, 603.3, 776.3, 442.1], 'GSM5385090': [440.8, 710.3, 617.2, 780.8, 230.1], 'GSM5385091': [467.4, 602.8, 589.3, 740.3, 189.4], 'GSM5385092': [474.5, 611.1, 583.6, 690.7, 176.5], 'GSM5385093': [454.8, 589.4, 523.4, 755.6, 180.9], 'GSM5385094': [475.9, 620.3, 526.8, 776.4, 189.1], 'GSM5385095': [444.5, 570.2, 590.3, 731.0, 346.0], 'GSM5385096': [414.4, 583.3, 552.4, 736.8, 224.8], 'GSM5385097': [543.6, 566.2, 569.8, 726.1, 333.4], 'GSM5385098': [425.9, 612.9, 611.4, 734.3, 153.4], 'GSM5385099': [497.8, 615.8, 556.1, 762.5, 321.4], 'GSM5385100': [491.40000000000003, 544.2, 573.3, 918.1, 875.0], 'GSM5385101': [665.9, 620.5, 612.9, 820.3, 244.5], 'GSM5385102': [518.0, 584.9, 585.7, 756.8, 177.1], 'GSM5385103': [644.6, 618.2, 568.9, 745.5, 677.3], 'GSM5385104': [435.6, 613.9, 575.3, 730.7, 785.0], 'GSM5385105': [437.0, 669.3, 593.7, 769.5, 225.0], 'GSM5385106': [526.5, 605.3, 535.5, 728.7, 261.9], 'GSM5385107': [451.0, 527.2, 534.1, 714.2, 239.9], 'GSM5385108': [462.2, 604.2, 590.5, 748.8, 215.5], 'GSM5385109': [471.8, 597.5, 537.7, 699.4, 429.5], 'GSM5385110': [465.4, 558.8, 573.1, 712.3, 687.4], 'GSM5385111': [478.2, 629.6, 542.8, 701.9, 223.3], 'GSM5385112': [412.4, 550.9, 581.7, 718.6, 1004.9], 'GSM5385113': [418.0, 530.2, 526.7, 731.6, 881.3], 'GSM5385114': [467.7, 579.6, 550.9, 768.3, 178.4], 'GSM5385115': [859.9, 571.3, 505.3, 753.6, 177.9], 'GSM5385116': [437.2, 585.5, 530.9, 754.9, 305.3], 'GSM5385117': [611.0, 528.8, 531.7, 753.0, 490.9], 'GSM5385118': [446.1, 553.1, 577.1, 761.9, 524.6], 'GSM5385119': [654.3, 584.6, 597.6, 746.3, 413.8], 'GSM5385120': [471.0, 584.3, 570.2, 698.4, 317.9], 'GSM5385121': [457.1, 570.8, 572.4, 716.4, 486.2], 'GSM5385122': [519.0, 571.5, 582.4, 713.4, 236.4], 'GSM5385123': [439.6, 579.4, 500.8, 734.7, 218.1], 'GSM5385124': [463.5, 540.0, 575.9, 725.1, 1189.2], 'GSM5385125': [423.29999999999995, 552.7, 526.1, 725.7, 286.0], 'GSM5385126': [484.3, 570.7, 580.8, 691.4, 328.0], 'GSM5385127': [421.29999999999995, 542.3, 537.2, 732.3, 278.0], 'GSM5385128': [493.0, 527.1, 525.5, 701.6, 173.3], 'GSM5385129': [455.5, 643.7, 598.4, 702.0, 378.8], 'GSM5385130': [594.6, 572.9, 543.1, 781.3, 1061.7], 'GSM5385131': [433.1, 581.1, 574.1, 763.5, 236.0], 'GSM5385132': [472.8, 512.0, 551.1, 695.4, 254.4], 'GSM5385133': [478.0, 586.3, 580.5, 702.0, 371.1], 'GSM5385134': [420.9, 538.8, 568.2, 734.2, 355.0], 'GSM5385135': [472.6, 547.0999999999999, 635.3, 762.4, 181.4], 'GSM5385136': [526.2, 612.8, 584.8, 807.0, 175.7], 'GSM5385137': [481.1, 594.8, 569.8, 775.4, 568.0], 'GSM5385138': [501.9, 610.0, 555.2, 741.9, 230.4]}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Glioblastoma/gene_data/GSE178236.csv\n" ] } ], "source": [ "# 1. Determine which columns in the gene annotation map to probe IDs and gene symbols\n", "# From previous output:\n", "# 'ID' column contains Illumina probe IDs like ILMN_1343048\n", "# 'Symbol' column contains gene symbols like 'phage_lambda_genome', 'thrB'\n", "\n", "# The gene expression data has row IDs in the format ILMN_XXXXXXX (Illumina probe IDs)\n", "# which match the 'ID' column in the gene annotation dataframe\n", "\n", "prob_col = 'ID' # Column containing probe IDs\n", "gene_col = 'Symbol' # Column containing gene symbols\n", "\n", "# 2. Extract the mapping dataframe\n", "mapping_df = get_gene_mapping(gene_annotation, prob_col=prob_col, gene_col=gene_col)\n", "print(f\"Gene mapping dataframe shape: {mapping_df.shape}\")\n", "print(\"Gene mapping preview:\")\n", "print(preview_df(mapping_df))\n", "\n", "# 3. Apply the gene mapping to convert probe-level measurements to gene expression data\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "print(f\"Gene expression dataframe shape after mapping: {gene_data.shape}\")\n", "print(\"Gene expression data preview:\")\n", "print(preview_df(gene_data))\n", "\n", "# Save the gene expression data to CSV\n", "gene_data.to_csv(out_gene_data_file)\n", "print(f\"Gene expression data saved to {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "d3cb481e", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "4829cc88", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T05:22:28.219267Z", "iopub.status.busy": "2025-03-25T05:22:28.218939Z", "iopub.status.idle": "2025-03-25T05:22:35.717363Z", "shell.execute_reply": "2025-03-25T05:22:35.716977Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loaded gene data shape: (21464, 113)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape after normalization: (20259, 113)\n", "Sample gene symbols after normalization: ['A1BG', 'A1BG-AS1', 'A1CF', 'A2M', 'A2ML1', 'A3GALT2', 'A4GALT', 'A4GNT', 'AAA1', 'AAAS']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Glioblastoma/gene_data/GSE178236.csv\n", "Clinical data shape: (3, 113)\n", "Clinical data preview:\n", " GSM5385026 GSM5385027 GSM5385028 GSM5385029 GSM5385030 \\\n", "Glioblastoma 1.0 1.0 1.0 1.0 1.0 \n", "Age 57.0 57.0 66.0 66.0 17.0 \n", "Gender 1.0 1.0 0.0 0.0 0.0 \n", "\n", " GSM5385031 GSM5385032 GSM5385033 GSM5385034 GSM5385035 ... \\\n", "Glioblastoma 1.0 1.0 1.0 1.0 1.0 ... \n", "Age 17.0 76.0 76.0 73.0 73.0 ... \n", "Gender 0.0 1.0 1.0 1.0 1.0 ... \n", "\n", " GSM5385129 GSM5385130 GSM5385131 GSM5385132 GSM5385133 \\\n", "Glioblastoma 1.0 1.0 1.0 1.0 1.0 \n", "Age 63.0 7.0 58.0 44.0 74.0 \n", "Gender 1.0 1.0 0.0 0.0 1.0 \n", "\n", " GSM5385134 GSM5385135 GSM5385136 GSM5385137 GSM5385138 \n", "Glioblastoma 1.0 1.0 1.0 1.0 1.0 \n", "Age 70.0 54.0 74.0 47.0 48.0 \n", "Gender 1.0 1.0 1.0 1.0 1.0 \n", "\n", "[3 rows x 113 columns]\n", "Linked data shape: (113, 20262)\n", "Linked data preview (first 5 rows, first 5 columns):\n", " Glioblastoma Age Gender A1BG A1BG-AS1\n", "GSM5385026 1.0 57.0 1.0 478.5 163.7\n", "GSM5385027 1.0 57.0 1.0 517.7 186.6\n", "GSM5385028 1.0 66.0 0.0 401.3 176.1\n", "GSM5385029 1.0 66.0 0.0 396.5 177.7\n", "GSM5385030 1.0 17.0 0.0 515.6 174.4\n", "\n", "Missing values before handling:\n", " Trait (Glioblastoma) missing: 0 out of 113\n", " Age missing: 0 out of 113\n", " Gender missing: 0 out of 113\n", " Genes with >20% missing: 0\n", " Samples with >5% missing genes: 0\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (113, 20262)\n", "Quartiles for 'Glioblastoma':\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 'Glioblastoma' in this dataset is severely biased.\n", "\n", "Quartiles for 'Age':\n", " 25%: 49.0\n", " 50% (Median): 61.0\n", " 75%: 67.0\n", "Min: 7.0\n", "Max: 76.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 29 occurrences. This represents 25.66% of the dataset.\n", "The distribution of the feature 'Gender' in this dataset is fine.\n", "\n", "Data was determined to be unusable or empty and was not saved\n" ] } ], "source": [ "# 1. Load the gene expression data saved in step 6\n", "gene_data = pd.read_csv(out_gene_data_file, index_col=0)\n", "print(f\"Loaded gene data shape: {gene_data.shape}\")\n", "\n", "# Normalize gene symbols using NCBI Gene database\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Gene data shape after normalization: {normalized_gene_data.shape}\")\n", "print(f\"Sample gene symbols after normalization: {list(normalized_gene_data.index[:10])}\")\n", "\n", "# Save the normalized gene data (overwrite the previous file with normalized 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. Generate and save clinical data if it doesn't exist\n", "if not os.path.exists(out_clinical_data_file):\n", " print(\"Clinical data file not found, generating it now\")\n", " # Get the SOFT and matrix files again\n", " soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", " \n", " # Get the 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", " # Define conversion functions based on the sample characteristics from step 1\n", " def convert_trait(value):\n", " if not value or ':' not in value:\n", " return None\n", " # Extract tumorsphere ID\n", " ts_id = value.split(\":\", 1)[1].strip()\n", " # For simplicity in this study, return 1 since all samples are glioblastoma\n", " return 1\n", "\n", " def convert_age(value):\n", " if not value or ':' not in value:\n", " return None\n", " # Extract age value after colon\n", " age_str = value.split(\":\", 1)[1].strip()\n", " try:\n", " # Convert to integer (continuous value)\n", " return int(age_str)\n", " except ValueError:\n", " return None\n", "\n", " def convert_gender(value):\n", " if not value or ':' not in value:\n", " return None\n", " # Extract gender value after colon\n", " gender = value.split(\":\", 1)[1].strip().lower()\n", " # Convert to binary: 0 for female, 1 for male\n", " if gender == 'female':\n", " return 0\n", " elif gender == 'male':\n", " return 1\n", " else:\n", " return None\n", " \n", " # Define row indices based on sample characteristics from step 1\n", " trait_row = 3 # tumorsphere ID\n", " age_row = 2\n", " gender_row = 1\n", " \n", " # Extract clinical features\n", " clinical_features = geo_select_clinical_features(\n", " 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", " # Save the clinical features\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " clinical_features.to_csv(out_clinical_data_file)\n", " clinical_df = clinical_features\n", "else:\n", " clinical_df = pd.read_csv(out_clinical_data_file, index_col=0)\n", "\n", "print(f\"Clinical data shape: {clinical_df.shape}\")\n", "print(\"Clinical data preview:\")\n", "print(clinical_df.head())\n", "\n", "# 3. Link 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", "print(\"Linked data preview (first 5 rows, first 5 columns):\")\n", "if linked_data.shape[1] >= 5:\n", " print(linked_data.iloc[:5, :5])\n", "else:\n", " print(linked_data.head())\n", "\n", "# 4. Handle missing values\n", "print(\"\\nMissing values before handling:\")\n", "print(f\" Trait ({trait}) missing: {linked_data[trait].isna().sum()} out of {len(linked_data)}\")\n", "if 'Age' in linked_data.columns:\n", " print(f\" Age missing: {linked_data['Age'].isna().sum()} out of {len(linked_data)}\")\n", "if 'Gender' in linked_data.columns:\n", " print(f\" Gender missing: {linked_data['Gender'].isna().sum()} out of {len(linked_data)}\")\n", "\n", "gene_cols = [col for col in linked_data.columns if col not in [trait, 'Age', 'Gender']]\n", "if gene_cols:\n", " print(f\" Genes with >20% missing: {sum(linked_data[gene_cols].isna().mean() > 0.2)}\")\n", " print(f\" Samples with >5% missing genes: {sum(linked_data[gene_cols].isna().mean(axis=1) > 0.05)}\")\n", "\n", "cleaned_data = handle_missing_values(linked_data, trait)\n", "print(f\"Data shape after handling missing values: {cleaned_data.shape}\")\n", "\n", "# 5. Evaluate bias in trait and demographic features\n", "is_trait_biased = False\n", "if len(cleaned_data) > 0:\n", " trait_biased, cleaned_data = judge_and_remove_biased_features(cleaned_data, trait)\n", " is_trait_biased = trait_biased\n", "else:\n", " print(\"No data remains after handling missing values.\")\n", " is_trait_biased = True\n", "\n", "# 6. Final validation and save\n", "is_usable = validate_and_save_cohort_info(\n", " is_final=True, \n", " cohort=cohort, \n", " info_path=json_path, \n", " is_gene_available=len(normalized_gene_data) > 0, \n", " is_trait_available=True, \n", " is_biased=is_trait_biased, \n", " df=cleaned_data,\n", " note=f\"Dataset contains gene expression data for {trait} analysis.\"\n", ")\n", "\n", "# 7. Save if usable\n", "if is_usable and len(cleaned_data) > 0:\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " cleaned_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", "else:\n", " print(\"Data was determined to be unusable or empty and was 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 }