{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "a421c1a3", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:17:30.205748Z", "iopub.status.busy": "2025-03-25T07:17:30.205611Z", "iopub.status.idle": "2025-03-25T07:17:30.371956Z", "shell.execute_reply": "2025-03-25T07:17:30.371606Z" } }, "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 = \"Kidney_Clear_Cell_Carcinoma\"\n", "cohort = \"GSE119958\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Kidney_Clear_Cell_Carcinoma\"\n", "in_cohort_dir = \"../../input/GEO/Kidney_Clear_Cell_Carcinoma/GSE119958\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Kidney_Clear_Cell_Carcinoma/GSE119958.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Kidney_Clear_Cell_Carcinoma/gene_data/GSE119958.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Kidney_Clear_Cell_Carcinoma/clinical_data/GSE119958.csv\"\n", "json_path = \"../../output/preprocess/Kidney_Clear_Cell_Carcinoma/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "9813a566", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "9b1a6139", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:17:30.373414Z", "iopub.status.busy": "2025-03-25T07:17:30.373275Z", "iopub.status.idle": "2025-03-25T07:17:30.692083Z", "shell.execute_reply": "2025-03-25T07:17:30.691749Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"APOL1 renal-risk variants induce mitochondrial fission\"\n", "!Series_summary\t\"To assess gene expression by APOL1 genotypes in primary proximal tubule cells (PTCs), global gene expression (mRNA) levels were examined on Affymetrix HTA 2.0 arrays in primary PTCs cultured from non-diseased kidney in African Americans without CKD who underwent nephrectomy for localized renal cell carcinoma.\"\n", "!Series_overall_design\t\"To detect gene profiles attributable to APOL1 renal-risk genotypes, African American primary proximal tubule cells with two APOL1 renal-risk alleles (N=5), one risk allele (N=23), and lacking renal-risk alleles (N=22) were compared for global gene expression when poly IC stimulation was absent (pre-) or present (post-) .\"\n", "Sample Characteristics Dictionary:\n", "{0: ['ethnicity: African American'], 1: ['genotype: APOL1 Genotype G0G0', 'genotype: APOL1 Genotype G1G2', 'genotype: APOL1 Genotype G2G0', 'genotype: APOL1 Genotype G1G0', 'genotype: APOL1 Genotype G1G1'], 2: ['time: pre-polyIC Stimulation', 'time: post-polyIC Stimulation'], 3: ['cell type: primary proximal tubule cells']}\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": "27d80582", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "4d7701e8", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:17:30.693313Z", "iopub.status.busy": "2025-03-25T07:17:30.693203Z", "iopub.status.idle": "2025-03-25T07:17:30.698185Z", "shell.execute_reply": "2025-03-25T07:17:30.697903Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 1. Gene Expression Data Availability\n", "# From the background info, we can see that this dataset contains gene expression data from Affymetrix arrays\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "\n", "# For trait (Kidney Clear Cell Carcinoma):\n", "# The data is from African Americans without CKD who underwent nephrectomy for localized renal cell carcinoma\n", "# This suggests we have data about renal cell carcinoma patients\n", "trait_row = None # Not directly available in the sample characteristics\n", "\n", "# For age:\n", "age_row = None # Not available in the sample characteristics\n", "\n", "# For gender:\n", "gender_row = None # Not available in the sample characteristics\n", "\n", "# 2.2 Data Type Conversion\n", "\n", "# For trait: \n", "# Since we don't have a direct trait row, we'll define a function for completeness\n", "def convert_trait(value):\n", " return None\n", "\n", "# For age:\n", "def convert_age(value):\n", " return None\n", "\n", "# For gender:\n", "def convert_gender(value):\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Trait data is not directly available in the sample characteristics\n", "is_trait_available = False\n", "\n", "# Validate and save cohort information\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 None, we'll skip this step\n" ] }, { "cell_type": "markdown", "id": "ba22d2a2", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "20f0216c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:17:30.699327Z", "iopub.status.busy": "2025-03-25T07:17:30.699221Z", "iopub.status.idle": "2025-03-25T07:17:31.254080Z", "shell.execute_reply": "2025-03-25T07:17:31.253685Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Examining matrix file structure...\n", "Line 0: !Series_title\t\"APOL1 renal-risk variants induce mitochondrial fission\"\n", "Line 1: !Series_geo_accession\t\"GSE119958\"\n", "Line 2: !Series_status\t\"Public on Sep 15 2018\"\n", "Line 3: !Series_submission_date\t\"Sep 14 2018\"\n", "Line 4: !Series_last_update_date\t\"Apr 04 2020\"\n", "Line 5: !Series_summary\t\"To assess gene expression by APOL1 genotypes in primary proximal tubule cells (PTCs), global gene expression (mRNA) levels were examined on Affymetrix HTA 2.0 arrays in primary PTCs cultured from non-diseased kidney in African Americans without CKD who underwent nephrectomy for localized renal cell carcinoma.\"\n", "Line 6: !Series_overall_design\t\"To detect gene profiles attributable to APOL1 renal-risk genotypes, African American primary proximal tubule cells with two APOL1 renal-risk alleles (N=5), one risk allele (N=23), and lacking renal-risk alleles (N=22) were compared for global gene expression when poly IC stimulation was absent (pre-) or present (post-) .\"\n", "Line 7: !Series_type\t\"Expression profiling by array\"\n", "Line 8: !Series_contributor\t\"Lijun,,Ma\"\n", "Line 9: !Series_contributor\t\"Hannah,C,Ainsworth\"\n", "Found table marker at line 77\n", "First few lines after marker:\n", "\"ID_REF\"\t\"GSM3389407\"\t\"GSM3389408\"\t\"GSM3389409\"\t\"GSM3389410\"\t\"GSM3389411\"\t\"GSM3389412\"\t\"GSM3389413\"\t\"GSM3389414\"\t\"GSM3389415\"\t\"GSM3389416\"\t\"GSM3389417\"\t\"GSM3389418\"\t\"GSM3389419\"\t\"GSM3389420\"\t\"GSM3389421\"\t\"GSM3389422\"\t\"GSM3389423\"\t\"GSM3389424\"\t\"GSM3389425\"\t\"GSM3389426\"\t\"GSM3389427\"\t\"GSM3389428\"\t\"GSM3389429\"\t\"GSM3389430\"\t\"GSM3389431\"\t\"GSM3389432\"\t\"GSM3389433\"\t\"GSM3389434\"\t\"GSM3389435\"\t\"GSM3389436\"\t\"GSM3389437\"\t\"GSM3389438\"\t\"GSM3389439\"\t\"GSM3389440\"\t\"GSM3389441\"\t\"GSM3389442\"\t\"GSM3389443\"\t\"GSM3389444\"\t\"GSM3389445\"\t\"GSM3389446\"\t\"GSM3389447\"\t\"GSM3389448\"\t\"GSM3389449\"\t\"GSM3389450\"\t\"GSM3389451\"\t\"GSM3389452\"\t\"GSM3389453\"\t\"GSM3389454\"\t\"GSM3389455\"\t\"GSM3389456\"\t\"GSM3389457\"\t\"GSM3389458\"\t\"GSM3389459\"\t\"GSM3389460\"\t\"GSM3389461\"\t\"GSM3389462\"\t\"GSM3389463\"\t\"GSM3389464\"\t\"GSM3389465\"\t\"GSM3389466\"\t\"GSM3389467\"\t\"GSM3389468\"\t\"GSM3389469\"\t\"GSM3389470\"\t\"GSM3389471\"\t\"GSM3389472\"\t\"GSM3389473\"\t\"GSM3389474\"\t\"GSM3389475\"\t\"GSM3389476\"\t\"GSM3389477\"\t\"GSM3389478\"\t\"GSM3389479\"\t\"GSM3389480\"\t\"GSM3389481\"\t\"GSM3389482\"\t\"GSM3389483\"\t\"GSM3389484\"\t\"GSM3389485\"\t\"GSM3389486\"\t\"GSM3389487\"\t\"GSM3389488\"\t\"GSM3389489\"\t\"GSM3389490\"\t\"GSM3389491\"\t\"GSM3389492\"\t\"GSM3389493\"\t\"GSM3389494\"\t\"GSM3389495\"\t\"GSM3389496\"\t\"GSM3389497\"\t\"GSM3389498\"\t\"GSM3389499\"\t\"GSM3389500\"\t\"GSM3389501\"\t\"GSM3389502\"\t\"GSM3389503\"\t\"GSM3389504\"\t\"GSM3389505\"\t\"GSM3389506\"\n", "\"TC01000001.hg.1\"\t4.284571\t4.482784\t4.188363\t4.444917\t4.46648\t4.701505\t4.526658\t4.364707\t4.182156\t4.546005\t4.379071\t4.972525\t4.182025\t4.565802\t4.180714\t4.849455\t4.257384\t5.032429\t4.03881\t4.574716\t4.430023\t4.464532\t4.720465\t4.771863\t4.302639\t3.994236\t4.306145\t4.446139\t4.894434\t4.655741\t4.451034\t4.341432\t4.48066\t4.290522\t4.465809\t4.412243\t4.333599\t4.226812\t4.964128\t4.158903\t4.030374\t4.716836\t4.171229\t4.547024\t4.152334\t4.394919\t4.97379\t4.222019\t4.276147\t4.678279\t4.549812\t4.334253\t3.955783\t4.326145\t4.592675\t4.357276\t4.119493\t4.652891\t4.387714\t4.533245\t4.531394\t4.732679\t4.102207\t4.159086\t4.210164\t4.636943\t4.302879\t4.660619\t4.338098\t4.418756\t4.165602\t4.732083\t4.738472\t4.659292\t4.095778\t4.429643\t3.953865\t4.331434\t4.470302\t4.409671\t4.640882\t4.238771\t4.169482\t4.434926\t4.223997\t4.427215\t4.269077\t4.257296\t4.432414\t4.200728\t4.069976\t4.499946\t4.296771\t4.314689\t4.160068\t4.34486\t4.62584\t4.21911\t4.226882\t4.508202\n", "\"TC01000002.hg.1\"\t3.736046\t3.930377\t3.865999\t4.065058\t3.897383\t3.782283\t3.883606\t3.654073\t3.952473\t3.835058\t3.805109\t3.821394\t3.873533\t3.932097\t3.759194\t3.71007\t3.873872\t4.013205\t3.839992\t3.822005\t4.002488\t3.823385\t4.026859\t3.633511\t3.923674\t3.978818\t3.966946\t3.772658\t3.955225\t3.965704\t3.809538\t3.920597\t3.988617\t3.90151\t4.06592\t4.257777\t3.900877\t3.771742\t3.789068\t3.845082\t3.869321\t3.736969\t3.952572\t3.942787\t3.895381\t4.145424\t4.112822\t4.045918\t4.088544\t3.821823\t4.489634\t3.987603\t3.988145\t4.169462\t4.03477\t3.844966\t4.170084\t3.722918\t3.911278\t3.820794\t4.09572\t3.680711\t3.760841\t3.840683\t3.815197\t3.662778\t3.738542\t3.833257\t3.87582\t3.787155\t3.864427\t3.65094\t3.781384\t3.936\t4.040408\t3.720034\t3.900034\t3.541129\t4.197486\t3.853785\t3.627111\t3.771534\t4.008109\t3.831229\t3.77791\t3.95981\t3.917386\t3.794299\t4.109248\t3.571129\t3.829005\t3.685052\t3.959323\t3.709968\t3.746334\t3.792409\t3.843079\t3.842018\t3.624762\t4.140109\n", "\"TC01000003.hg.1\"\t3.186868\t3.247248\t3.240343\t3.315707\t3.41179\t3.195617\t3.218482\t3.577764\t3.762699\t3.217139\t3.229784\t3.114992\t3.540658\t3.478461\t3.042209\t3.107738\t3.248306\t3.197014\t3.603436\t2.907497\t3.502851\t3.17635\t3.04979\t3.43052\t3.314839\t2.970968\t3.16937\t3.284149\t3.108593\t2.946233\t3.137872\t3.756454\t3.233887\t3.441939\t3.208256\t3.342031\t3.406709\t3.172066\t3.527566\t3.567638\t3.243581\t3.166682\t3.22247\t3.185664\t3.425646\t3.087175\t3.027751\t3.276125\t3.42844\t3.088595\t3.701828\t3.48301\t3.155495\t3.034668\t3.159643\t2.99282\t3.047134\t3.15766\t3.442824\t3.534089\t3.215965\t3.423816\t3.459312\t3.169425\t3.283985\t3.132058\t3.371918\t3.251329\t3.203713\t3.391977\t3.85357\t3.050817\t3.125653\t3.093138\t3.14946\t3.138103\t3.484857\t3.157344\t3.38881\t3.146036\t3.067063\t3.596055\t3.260791\t3.660132\t3.017293\t3.329315\t3.012968\t3.305694\t3.186095\t3.523115\t3.5295\t3.159202\t3.705574\t3.569835\t3.047901\t3.185582\t3.113494\t3.174276\t3.095068\t3.200349\n", "\"TC01000004.hg.1\"\t3.304653\t3.365509\t3.561024\t3.335495\t2.944269\t3.357819\t3.392707\t3.329005\t3.198023\t3.219487\t3.331098\t3.279108\t3.320253\t3.367643\t3.281683\t2.985276\t3.417412\t3.120373\t3.384672\t3.287429\t3.235313\t3.834597\t3.65219\t3.673536\t3.406224\t3.283592\t3.246716\t3.469626\t3.556079\t3.384197\t3.273224\t3.172921\t3.194215\t3.577574\t3.553818\t3.312646\t3.131124\t3.271734\t3.528352\t3.22437\t3.31292\t3.080853\t3.198657\t3.367013\t3.318923\t3.10916\t3.346148\t3.186339\t3.144727\t3.307437\t3.484458\t3.357441\t3.445868\t3.686201\t3.029917\t3.522041\t3.718924\t3.483118\t3.37641\t3.350209\t3.383018\t3.334386\t3.892405\t3.208039\t3.306808\t3.528741\t3.562289\t3.034875\t3.110322\t3.304547\t3.460205\t3.558666\t3.40565\t3.25641\t3.277044\t3.345867\t3.599622\t3.273199\t3.061186\t3.30474\t3.35805\t3.482816\t3.285954\t3.639941\t3.056392\t3.522896\t3.273758\t3.353102\t3.603281\t3.029619\t3.628126\t3.172132\t3.214247\t3.228302\t3.218106\t3.697277\t3.061388\t3.176854\t3.305855\t3.297683\n", "Total lines examined: 78\n", "\n", "Attempting to extract gene data from matrix file...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Successfully extracted gene data with 67528 rows\n", "First 20 gene IDs:\n", "Index(['TC01000001.hg.1', 'TC01000002.hg.1', 'TC01000003.hg.1',\n", " 'TC01000004.hg.1', 'TC01000005.hg.1', 'TC01000006.hg.1',\n", " 'TC01000007.hg.1', 'TC01000008.hg.1', 'TC01000009.hg.1',\n", " 'TC01000010.hg.1', 'TC01000011.hg.1', 'TC01000012.hg.1',\n", " 'TC01000013.hg.1', 'TC01000014.hg.1', 'TC01000015.hg.1',\n", " 'TC01000016.hg.1', 'TC01000017.hg.1', 'TC01000018.hg.1',\n", " 'TC01000019.hg.1', 'TC01000020.hg.1'],\n", " dtype='object', name='ID')\n", "\n", "Gene expression data available: True\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", "# Add diagnostic code to check file content and structure\n", "print(\"Examining matrix file structure...\")\n", "with gzip.open(matrix_file, 'rt') as file:\n", " table_marker_found = False\n", " lines_read = 0\n", " for i, line in enumerate(file):\n", " lines_read += 1\n", " if '!series_matrix_table_begin' in line:\n", " table_marker_found = True\n", " print(f\"Found table marker at line {i}\")\n", " # Read a few lines after the marker to check data structure\n", " next_lines = [next(file, \"\").strip() for _ in range(5)]\n", " print(\"First few lines after marker:\")\n", " for next_line in next_lines:\n", " print(next_line)\n", " break\n", " if i < 10: # Print first few lines to see file structure\n", " print(f\"Line {i}: {line.strip()}\")\n", " if i > 100: # Don't read the entire file\n", " break\n", " \n", " if not table_marker_found:\n", " print(\"Table marker '!series_matrix_table_begin' not found in first 100 lines\")\n", " print(f\"Total lines examined: {lines_read}\")\n", "\n", "# 2. Try extracting gene expression data from the matrix file again with better diagnostics\n", "try:\n", " print(\"\\nAttempting to extract gene data from matrix file...\")\n", " gene_data = get_genetic_data(matrix_file)\n", " if gene_data.empty:\n", " print(\"Extracted gene expression data is empty\")\n", " is_gene_available = False\n", " else:\n", " print(f\"Successfully extracted gene data with {len(gene_data.index)} rows\")\n", " print(\"First 20 gene IDs:\")\n", " print(gene_data.index[:20])\n", " is_gene_available = True\n", "except Exception as e:\n", " print(f\"Error extracting gene data: {str(e)}\")\n", " print(\"This dataset appears to have an empty or malformed gene expression matrix\")\n", " is_gene_available = False\n", "\n", "print(f\"\\nGene expression data available: {is_gene_available}\")\n", "\n", "# If data extraction failed, try an alternative approach using pandas directly\n", "if not is_gene_available:\n", " print(\"\\nTrying alternative approach to read gene expression data...\")\n", " try:\n", " with gzip.open(matrix_file, 'rt') as file:\n", " # Skip lines until we find the marker\n", " for line in file:\n", " if '!series_matrix_table_begin' in line:\n", " break\n", " \n", " # Try to read the data directly with pandas\n", " gene_data = pd.read_csv(file, sep='\\t', index_col=0)\n", " \n", " if not gene_data.empty:\n", " print(f\"Successfully extracted gene data with alternative method: {gene_data.shape}\")\n", " print(\"First 20 gene IDs:\")\n", " print(gene_data.index[:20])\n", " is_gene_available = True\n", " else:\n", " print(\"Alternative extraction method also produced empty data\")\n", " except Exception as e:\n", " print(f\"Alternative extraction failed: {str(e)}\")\n" ] }, { "cell_type": "markdown", "id": "ad682b68", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "b08c7236", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:17:31.255339Z", "iopub.status.busy": "2025-03-25T07:17:31.255225Z", "iopub.status.idle": "2025-03-25T07:17:31.257217Z", "shell.execute_reply": "2025-03-25T07:17:31.256908Z" } }, "outputs": [], "source": [ "# Based on the gene identifiers like \"TC01000001.hg.1\", I can determine these are not\n", "# standard human gene symbols but rather Affymetrix probe identifiers from the HTA 2.0 arrays.\n", "# This is confirmed by the series summary mentioning \"Affymetrix HTA 2.0 arrays\".\n", "# These probe IDs need to be mapped to human gene symbols for biological interpretation.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "1ca523bd", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "831fc7fd", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:17:31.258347Z", "iopub.status.busy": "2025-03-25T07:17:31.258244Z", "iopub.status.idle": "2025-03-25T07:17:43.514043Z", "shell.execute_reply": "2025-03-25T07:17:43.513663Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Extracting gene annotation data from SOFT file...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Successfully extracted gene annotation data with 6823653 rows\n", "\n", "Gene annotation preview (first few rows):\n", "{'ID': ['TC01000001.hg.1', 'TC01000002.hg.1', 'TC01000003.hg.1', 'TC01000004.hg.1', 'TC01000005.hg.1'], 'probeset_id': ['TC01000001.hg.1', 'TC01000002.hg.1', 'TC01000003.hg.1', 'TC01000004.hg.1', 'TC01000005.hg.1'], 'seqname': ['chr1', 'chr1', 'chr1', 'chr1', 'chr1'], 'strand': ['+', '+', '+', '+', '+'], 'start': ['11869', '29554', '69091', '160446', '317811'], 'stop': ['14409', '31109', '70008', '161525', '328581'], 'total_probes': [49.0, 60.0, 30.0, 30.0, 191.0], 'gene_assignment': ['NR_046018 // DDX11L1 // DEAD/H (Asp-Glu-Ala-Asp/His) box helicase 11 like 1 // 1p36.33 // 100287102 /// ENST00000456328 // DDX11L5 // DEAD/H (Asp-Glu-Ala-Asp/His) box helicase 11 like 5 // 9p24.3 // 100287596 /// ENST00000456328 // DDX11L1 // DEAD/H (Asp-Glu-Ala-Asp/His) box helicase 11 like 1 // 1p36.33 // 100287102', 'ENST00000408384 // MIR1302-11 // microRNA 1302-11 // --- // 100422919 /// ENST00000408384 // MIR1302-10 // microRNA 1302-10 // --- // 100422834 /// ENST00000408384 // MIR1302-9 // microRNA 1302-9 // --- // 100422831 /// ENST00000408384 // MIR1302-2 // microRNA 1302-2 // --- // 100302278 /// ENST00000469289 // MIR1302-11 // microRNA 1302-11 // --- // 100422919 /// ENST00000469289 // MIR1302-10 // microRNA 1302-10 // --- // 100422834 /// ENST00000469289 // MIR1302-9 // microRNA 1302-9 // --- // 100422831 /// ENST00000469289 // MIR1302-2 // microRNA 1302-2 // --- // 100302278 /// ENST00000473358 // MIR1302-11 // microRNA 1302-11 // --- // 100422919 /// ENST00000473358 // MIR1302-10 // microRNA 1302-10 // --- // 100422834 /// ENST00000473358 // MIR1302-9 // microRNA 1302-9 // --- // 100422831 /// ENST00000473358 // MIR1302-2 // microRNA 1302-2 // --- // 100302278 /// OTTHUMT00000002841 // OTTHUMG00000000959 // NULL // --- // --- /// OTTHUMT00000002841 // RP11-34P13.3 // NULL // --- // --- /// OTTHUMT00000002840 // OTTHUMG00000000959 // NULL // --- // --- /// OTTHUMT00000002840 // RP11-34P13.3 // NULL // --- // ---', 'NM_001005484 // OR4F5 // olfactory receptor, family 4, subfamily F, member 5 // 1p36.33 // 79501 /// ENST00000335137 // OR4F5 // olfactory receptor, family 4, subfamily F, member 5 // 1p36.33 // 79501 /// OTTHUMT00000003223 // OR4F5 // NULL // --- // ---', 'OTTHUMT00000007169 // OTTHUMG00000002525 // NULL // --- // --- /// OTTHUMT00000007169 // RP11-34P13.9 // NULL // --- // ---', 'NR_028322 // LOC100132287 // uncharacterized LOC100132287 // 1p36.33 // 100132287 /// NR_028327 // LOC100133331 // uncharacterized LOC100133331 // 1p36.33 // 100133331 /// ENST00000425496 // LOC101060495 // uncharacterized LOC101060495 // --- // 101060495 /// ENST00000425496 // LOC101060494 // uncharacterized LOC101060494 // --- // 101060494 /// ENST00000425496 // LOC101059936 // uncharacterized LOC101059936 // --- // 101059936 /// ENST00000425496 // LOC100996502 // uncharacterized LOC100996502 // --- // 100996502 /// ENST00000425496 // LOC100996328 // uncharacterized LOC100996328 // --- // 100996328 /// ENST00000425496 // LOC100287894 // uncharacterized LOC100287894 // 7q11.21 // 100287894 /// NR_028325 // LOC100132062 // uncharacterized LOC100132062 // 5q35.3 // 100132062 /// OTTHUMT00000346878 // OTTHUMG00000156968 // NULL // --- // --- /// OTTHUMT00000346878 // RP4-669L17.10 // NULL // --- // --- /// OTTHUMT00000346879 // OTTHUMG00000156968 // NULL // --- // --- /// OTTHUMT00000346879 // RP4-669L17.10 // NULL // --- // --- /// OTTHUMT00000346880 // OTTHUMG00000156968 // NULL // --- // --- /// OTTHUMT00000346880 // RP4-669L17.10 // NULL // --- // --- /// OTTHUMT00000346881 // OTTHUMG00000156968 // NULL // --- // --- /// OTTHUMT00000346881 // RP4-669L17.10 // NULL // --- // ---'], 'mrna_assignment': ['NR_046018 // RefSeq // Homo sapiens DEAD/H (Asp-Glu-Ala-Asp/His) box helicase 11 like 1 (DDX11L1), non-coding RNA. // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000456328 // ENSEMBL // cdna:known chromosome:GRCh37:1:11869:14409:1 gene:ENSG00000223972 gene_biotype:pseudogene transcript_biotype:processed_transcript // chr1 // 100 // 100 // 0 // --- // 0 /// uc001aaa.3 // UCSC Genes // --- // chr1 // 100 // 100 // 0 // --- // 0 /// uc010nxq.1 // UCSC Genes // --- // chr1 // 100 // 100 // 0 // --- // 0 /// uc010nxr.1 // UCSC Genes // --- // chr1 // 100 // 100 // 0 // --- // 0', 'ENST00000408384 // ENSEMBL // ncrna:miRNA chromosome:GRCh37:1:30366:30503:1 gene:ENSG00000221311 gene_biotype:miRNA transcript_biotype:miRNA // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000469289 // ENSEMBL // havana:lincRNA chromosome:GRCh37:1:30267:31109:1 gene:ENSG00000243485 gene_biotype:lincRNA transcript_biotype:lincRNA // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000473358 // ENSEMBL // havana:lincRNA chromosome:GRCh37:1:29554:31097:1 gene:ENSG00000243485 gene_biotype:lincRNA transcript_biotype:lincRNA // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000002841 // Havana transcript // cdna:all chromosome:VEGA52:1:30267:31109:1 Gene:OTTHUMG00000000959 // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000002840 // Havana transcript // cdna:all chromosome:VEGA52:1:29554:31097:1 Gene:OTTHUMG00000000959 // chr1 // 100 // 100 // 0 // --- // 0', 'NM_001005484 // RefSeq // Homo sapiens olfactory receptor, family 4, subfamily F, member 5 (OR4F5), mRNA. // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000335137 // ENSEMBL // cdna:known chromosome:GRCh37:1:69091:70008:1 gene:ENSG00000186092 gene_biotype:protein_coding transcript_biotype:protein_coding // chr1 // 100 // 100 // 0 // --- // 0 /// uc001aal.1 // UCSC Genes // --- // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000003223 // Havana transcript // cdna:all chromosome:VEGA52:1:69091:70008:1 Gene:OTTHUMG00000001094 // chr1 // 100 // 100 // 0 // --- // 0', 'ENST00000496488 // ENSEMBL // havana:lincRNA chromosome:GRCh37:1:160446:161525:1 gene:ENSG00000241599 gene_biotype:lincRNA transcript_biotype:lincRNA // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000007169 // Havana transcript // cdna:all chromosome:VEGA52:1:160446:161525:1 Gene:OTTHUMG00000002525 // chr1 // 100 // 100 // 0 // --- // 0', 'NR_028322 // RefSeq // Homo sapiens uncharacterized LOC100132287 (LOC100132287), non-coding RNA. // chr1 // 100 // 100 // 0 // --- // 0 /// NR_028327 // RefSeq // Homo sapiens uncharacterized LOC100133331 (LOC100133331), non-coding RNA. // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000425496 // ENSEMBL // ensembl:lincRNA chromosome:GRCh37:1:324756:328453:1 gene:ENSG00000237094 gene_biotype:lincRNA transcript_biotype:lincRNA // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000426316 // ENSEMBL // [retired] cdna:known chromosome:GRCh37:1:317811:328455:1 gene:ENSG00000240876 gene_biotype:processed_transcript transcript_biotype:processed_transcript // chr1 // 100 // 100 // 0 // --- // 0 /// NR_028325 // RefSeq // Homo sapiens uncharacterized LOC100132062 (LOC100132062), non-coding RNA. // chr1 // 100 // 100 // 0 // --- // 0 /// uc009vjk.2 // UCSC Genes // --- // chr1 // 100 // 100 // 0 // --- // 0 /// uc021oeh.1 // UCSC Genes // --- // chr1 // 100 // 100 // 0 // --- // 0 /// uc021oei.1 // UCSC Genes // --- // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000346906 // Havana transcript // [retired] cdna:all chromosome:VEGA50:1:317811:328455:1 Gene:OTTHUMG00000156972 // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000346878 // Havana transcript // cdna:all chromosome:VEGA52:1:320162:321056:1 Gene:OTTHUMG00000156968 // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000346879 // Havana transcript // cdna:all chromosome:VEGA52:1:320162:324461:1 Gene:OTTHUMG00000156968 // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000346880 // Havana transcript // cdna:all chromosome:VEGA52:1:317720:324873:1 Gene:OTTHUMG00000156968 // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000346881 // Havana transcript // cdna:all chromosome:VEGA52:1:322672:324955:1 Gene:OTTHUMG00000156968 // chr1 // 100 // 100 // 0 // --- // 0'], 'swissprot': ['NR_046018 // B7ZGX0 /// NR_046018 // B7ZGX2 /// NR_046018 // B7ZGX7 /// NR_046018 // B7ZGX8 /// ENST00000456328 // B7ZGX0 /// ENST00000456328 // B7ZGX2 /// ENST00000456328 // B7ZGX3 /// ENST00000456328 // B7ZGX7 /// ENST00000456328 // B7ZGX8 /// ENST00000456328 // Q6ZU42', '---', 'NM_001005484 // Q8NH21 /// ENST00000335137 // Q8NH21', '---', 'NR_028325 // B4DYM5 /// NR_028325 // B4E0H4 /// NR_028325 // B4E3X0 /// NR_028325 // B4E3X2 /// NR_028325 // Q6ZQS4'], 'unigene': ['NR_046018 // Hs.714157 // testis| normal| adult /// ENST00000456328 // Hs.719844 // brain| testis| normal /// ENST00000456328 // Hs.714157 // testis| normal| adult /// ENST00000456328 // Hs.618434 // testis| normal', 'ENST00000469289 // Hs.622486 // eye| normal| adult /// ENST00000469289 // Hs.729632 // testis| normal /// ENST00000469289 // Hs.742718 // testis /// ENST00000473358 // Hs.622486 // eye| normal| adult /// ENST00000473358 // Hs.729632 // testis| normal /// ENST00000473358 // Hs.742718 // testis', 'NM_001005484 // Hs.554500 // --- /// ENST00000335137 // Hs.554500 // ---', '---', 'NR_028322 // Hs.446409 // adrenal gland| blood| bone| brain| connective tissue| embryonic tissue| eye| intestine| kidney| larynx| lung| lymph node| mouth| pharynx| placenta| prostate| skin| testis| thymus| thyroid| uterus| bladder carcinoma| chondrosarcoma| colorectal tumor| germ cell tumor| head and neck tumor| kidney tumor| leukemia| lung tumor| normal| primitive neuroectodermal tumor of the CNS| uterine tumor|embryoid body| blastocyst| fetus| neonate| adult /// NR_028327 // Hs.733048 // ascites| bladder| blood| brain| embryonic tissue| eye| intestine| kidney| larynx| liver| lung| mammary gland| mouth| pancreas| placenta| prostate| skin| stomach| testis| thymus| thyroid| trachea| uterus| bladder carcinoma| breast (mammary gland) tumor| colorectal tumor| gastrointestinal tumor| head and neck tumor| kidney tumor| leukemia| liver tumor| lung tumor| normal| pancreatic tumor| prostate cancer| retinoblastoma| skin tumor| soft tissue/muscle tissue tumor| uterine tumor|embryoid body| blastocyst| fetus| adult /// ENST00000425496 // Hs.744556 // mammary gland| normal| adult /// ENST00000425496 // Hs.660700 // eye| placenta| testis| normal| adult /// ENST00000425496 // Hs.518952 // blood| brain| intestine| lung| mammary gland| mouth| muscle| pharynx| placenta| prostate| spleen| testis| thymus| thyroid| trachea| breast (mammary gland) tumor| colorectal tumor| head and neck tumor| leukemia| lung tumor| normal| prostate cancer| fetus| adult /// ENST00000425496 // Hs.742131 // testis| normal| adult /// ENST00000425496 // Hs.636102 // uterus| uterine tumor /// ENST00000425496 // Hs.646112 // brain| intestine| larynx| lung| mouth| prostate| testis| thyroid| colorectal tumor| head and neck tumor| lung tumor| normal| prostate cancer| adult /// ENST00000425496 // Hs.647795 // brain| lung| lung tumor| adult /// ENST00000425496 // Hs.684307 // --- /// ENST00000425496 // Hs.720881 // testis| normal /// ENST00000425496 // Hs.729353 // brain| lung| placenta| testis| trachea| lung tumor| normal| fetus| adult /// ENST00000425496 // Hs.735014 // ovary| ovarian tumor /// NR_028325 // Hs.732199 // ascites| blood| brain| connective tissue| embryonic tissue| eye| intestine| kidney| lung| ovary| placenta| prostate| stomach| testis| thymus| uterus| chondrosarcoma| colorectal tumor| gastrointestinal tumor| kidney tumor| leukemia| lung tumor| normal| ovarian tumor| fetus| adult'], 'category': ['main', 'main', 'main', 'main', 'main'], 'locus type': ['Coding', 'Coding', 'Coding', 'Coding', 'Coding'], 'notes': ['---', '---', '---', '---', '2 retired transcript(s) from ENSEMBL, Havana transcript'], 'SPOT_ID': ['chr1(+):11869-14409', 'chr1(+):29554-31109', 'chr1(+):69091-70008', 'chr1(+):160446-161525', 'chr1(+):317811-328581']}\n", "\n", "Column names in gene annotation data:\n", "['ID', 'probeset_id', 'seqname', 'strand', 'start', 'stop', 'total_probes', 'gene_assignment', 'mrna_assignment', 'swissprot', 'unigene', 'category', 'locus type', 'notes', 'SPOT_ID']\n", "\n", "The dataset contains genomic regions (SPOT_ID) that could be used for location-based gene mapping.\n", "Example SPOT_ID format: chr1(+):11869-14409\n" ] } ], "source": [ "# 1. Extract gene annotation data from the SOFT file\n", "print(\"Extracting gene annotation data from SOFT file...\")\n", "try:\n", " # Use the library function to extract gene annotation\n", " gene_annotation = get_gene_annotation(soft_file)\n", " print(f\"Successfully extracted gene annotation data with {len(gene_annotation.index)} rows\")\n", " \n", " # Preview the annotation DataFrame\n", " print(\"\\nGene annotation preview (first few rows):\")\n", " print(preview_df(gene_annotation))\n", " \n", " # Show column names to help identify which columns we need for mapping\n", " print(\"\\nColumn names in gene annotation data:\")\n", " print(gene_annotation.columns.tolist())\n", " \n", " # Check for relevant mapping columns\n", " if 'GB_ACC' in gene_annotation.columns:\n", " print(\"\\nThe dataset contains GenBank accessions (GB_ACC) that could be used for gene mapping.\")\n", " # Count non-null values in GB_ACC column\n", " non_null_count = gene_annotation['GB_ACC'].count()\n", " print(f\"Number of rows with GenBank accessions: {non_null_count} out of {len(gene_annotation)}\")\n", " \n", " if 'SPOT_ID' in gene_annotation.columns:\n", " print(\"\\nThe dataset contains genomic regions (SPOT_ID) that could be used for location-based gene mapping.\")\n", " print(\"Example SPOT_ID format:\", gene_annotation['SPOT_ID'].iloc[0])\n", " \n", "except Exception as e:\n", " print(f\"Error processing gene annotation data: {e}\")\n", " is_gene_available = False\n" ] }, { "cell_type": "markdown", "id": "0f5db813", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "ccafd02f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:17:43.515243Z", "iopub.status.busy": "2025-03-25T07:17:43.515113Z", "iopub.status.idle": "2025-03-25T07:17:49.137809Z", "shell.execute_reply": "2025-03-25T07:17:49.137458Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Processing gene mapping...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Created mapping data with 70753 rows\n", "\n", "First few rows of mapping data:\n", "{'ID': ['TC01000001.hg.1', 'TC01000002.hg.1', 'TC01000003.hg.1', 'TC01000004.hg.1', 'TC01000005.hg.1'], 'Gene': ['NR_046018 // DDX11L1 // DEAD/H (Asp-Glu-Ala-Asp/His) box helicase 11 like 1 // 1p36.33 // 100287102 /// ENST00000456328 // DDX11L5 // DEAD/H (Asp-Glu-Ala-Asp/His) box helicase 11 like 5 // 9p24.3 // 100287596 /// ENST00000456328 // DDX11L1 // DEAD/H (Asp-Glu-Ala-Asp/His) box helicase 11 like 1 // 1p36.33 // 100287102', 'ENST00000408384 // MIR1302-11 // microRNA 1302-11 // --- // 100422919 /// ENST00000408384 // MIR1302-10 // microRNA 1302-10 // --- // 100422834 /// ENST00000408384 // MIR1302-9 // microRNA 1302-9 // --- // 100422831 /// ENST00000408384 // MIR1302-2 // microRNA 1302-2 // --- // 100302278 /// ENST00000469289 // MIR1302-11 // microRNA 1302-11 // --- // 100422919 /// ENST00000469289 // MIR1302-10 // microRNA 1302-10 // --- // 100422834 /// ENST00000469289 // MIR1302-9 // microRNA 1302-9 // --- // 100422831 /// ENST00000469289 // MIR1302-2 // microRNA 1302-2 // --- // 100302278 /// ENST00000473358 // MIR1302-11 // microRNA 1302-11 // --- // 100422919 /// ENST00000473358 // MIR1302-10 // microRNA 1302-10 // --- // 100422834 /// ENST00000473358 // MIR1302-9 // microRNA 1302-9 // --- // 100422831 /// ENST00000473358 // MIR1302-2 // microRNA 1302-2 // --- // 100302278 /// OTTHUMT00000002841 // OTTHUMG00000000959 // NULL // --- // --- /// OTTHUMT00000002841 // RP11-34P13.3 // NULL // --- // --- /// OTTHUMT00000002840 // OTTHUMG00000000959 // NULL // --- // --- /// OTTHUMT00000002840 // RP11-34P13.3 // NULL // --- // ---', 'NM_001005484 // OR4F5 // olfactory receptor, family 4, subfamily F, member 5 // 1p36.33 // 79501 /// ENST00000335137 // OR4F5 // olfactory receptor, family 4, subfamily F, member 5 // 1p36.33 // 79501 /// OTTHUMT00000003223 // OR4F5 // NULL // --- // ---', 'OTTHUMT00000007169 // OTTHUMG00000002525 // NULL // --- // --- /// OTTHUMT00000007169 // RP11-34P13.9 // NULL // --- // ---', 'NR_028322 // LOC100132287 // uncharacterized LOC100132287 // 1p36.33 // 100132287 /// NR_028327 // LOC100133331 // uncharacterized LOC100133331 // 1p36.33 // 100133331 /// ENST00000425496 // LOC101060495 // uncharacterized LOC101060495 // --- // 101060495 /// ENST00000425496 // LOC101060494 // uncharacterized LOC101060494 // --- // 101060494 /// ENST00000425496 // LOC101059936 // uncharacterized LOC101059936 // --- // 101059936 /// ENST00000425496 // LOC100996502 // uncharacterized LOC100996502 // --- // 100996502 /// ENST00000425496 // LOC100996328 // uncharacterized LOC100996328 // --- // 100996328 /// ENST00000425496 // LOC100287894 // uncharacterized LOC100287894 // 7q11.21 // 100287894 /// NR_028325 // LOC100132062 // uncharacterized LOC100132062 // 5q35.3 // 100132062 /// OTTHUMT00000346878 // OTTHUMG00000156968 // NULL // --- // --- /// OTTHUMT00000346878 // RP4-669L17.10 // NULL // --- // --- /// OTTHUMT00000346879 // OTTHUMG00000156968 // NULL // --- // --- /// OTTHUMT00000346879 // RP4-669L17.10 // NULL // --- // --- /// OTTHUMT00000346880 // OTTHUMG00000156968 // NULL // --- // --- /// OTTHUMT00000346880 // RP4-669L17.10 // NULL // --- // --- /// OTTHUMT00000346881 // OTTHUMG00000156968 // NULL // --- // --- /// OTTHUMT00000346881 // RP4-669L17.10 // NULL // --- // ---']}\n", "\n", "Applying gene mapping to convert probe measurements to gene expression data...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Generated gene expression data with 71527 genes and 100 samples\n", "\n", "First few rows of gene expression data:\n", "{'GSM3389407': [18.932030916666665, 1.1775772, 5.371056666666667, 1.74514775, 1.5487551666666668], 'GSM3389408': [19.046464791666665, 1.1226058, 5.256726666666666, 1.71101625, 1.6268235], 'GSM3389409': [19.016802333333334, 1.1384794, 5.38398, 1.70044775, 1.5824381666666665], 'GSM3389410': [19.459400166666665, 1.1460666, 5.2680066666666665, 1.70947075, 1.5796898333333331], 'GSM3389411': [19.337880416666664, 1.114715, 5.25694, 1.70998025, 1.6002086666666666], 'GSM3389412': [19.337805958333334, 1.1405474, 5.269506666666667, 1.7845565, 1.5282928333333332], 'GSM3389413': [19.041272333333332, 1.1281822, 5.144073333333333, 1.76992025, 1.6196983333333335], 'GSM3389414': [19.33273375, 1.1147008, 5.259906666666667, 1.7724995, 1.702872], 'GSM3389415': [19.554414958333332, 1.1131541999999999, 5.1313466666666665, 1.73526125, 1.6858913333333334], 'GSM3389416': [19.23352925, 1.112511, 5.25888, 1.78251925, 1.5975251666666668], 'GSM3389417': [19.310543875, 1.1708934, 5.212723333333334, 1.850876, 1.6884543333333335], 'GSM3389418': [19.495374166666668, 1.0848488, 5.254906666666667, 1.708999, 1.6040799999999997], 'GSM3389419': [19.200653291666665, 1.1325352, 5.20019, 1.74956275, 1.7244915], 'GSM3389420': [19.080575208333332, 1.0996482, 5.244583333333334, 1.81544125, 1.5409048333333333], 'GSM3389421': [19.603107333333334, 1.1034738000000002, 5.223586666666667, 1.71059025, 1.6151131666666667], 'GSM3389422': [19.560628958333332, 1.0726258, 5.28102, 1.84164675, 1.6580125], 'GSM3389423': [19.481898083333334, 1.0962456, 5.357006666666667, 1.7247165, 1.59741], 'GSM3389424': [19.365880375, 1.1150233999999999, 5.333293333333333, 1.710458, 1.5906835], 'GSM3389425': [19.175204208333334, 1.1656982, 5.26863, 1.62626775, 1.6930386666666666], 'GSM3389426': [19.590046375, 1.1373442, 5.312763333333334, 1.835091, 1.7385995], 'GSM3389427': [18.963943541666666, 1.1479188, 5.257733333333333, 1.69535975, 1.6289251666666664], 'GSM3389428': [19.49600375, 1.0987498, 5.247513333333333, 1.64419725, 1.6588183333333335], 'GSM3389429': [19.197243333333333, 1.1127192000000001, 5.259596666666667, 1.78063925, 1.6471828333333334], 'GSM3389430': [19.50156554166667, 1.112885, 5.158093333333333, 1.66668375, 1.6193263333333334], 'GSM3389431': [19.509710041666665, 1.1101408, 5.300493333333333, 1.7834225, 1.7191611666666669], 'GSM3389432': [19.76350075, 1.1365767999999998, 5.322546666666667, 1.72323625, 1.6431928333333334], 'GSM3389433': [19.54686720833333, 1.103548, 5.40222, 1.71763475, 1.7072581666666666], 'GSM3389434': [19.712428083333332, 1.1384608, 5.358336666666666, 1.727943, 1.8085395], 'GSM3389435': [19.280600083333333, 1.131467, 5.349823333333333, 1.6476275, 1.6759655], 'GSM3389436': [19.498241125, 1.1262814, 5.35311, 1.82948075, 1.7267946666666667], 'GSM3389437': [19.542101083333336, 1.094676, 5.1648233333333335, 1.75363325, 1.638718], 'GSM3389438': [19.528969958333334, 1.1298413999999999, 5.2602, 1.736392, 1.6768138333333331], 'GSM3389439': [19.168587833333333, 1.1324932, 5.277256666666667, 1.79525075, 1.6276601666666666], 'GSM3389440': [19.399616541666667, 1.1279322, 5.1421833333333336, 1.67731225, 1.6551888333333333], 'GSM3389441': [19.355105541666667, 1.1582814000000001, 5.402053333333334, 1.7339935, 1.7719691666666666], 'GSM3389442': [19.289600041666667, 1.1395161999999999, 5.36277, 1.746937, 1.6218098333333333], 'GSM3389443': [19.286319416666664, 1.1114508, 5.242593333333333, 1.75169475, 1.6313016666666666], 'GSM3389444': [19.283298916666666, 1.1239734000000001, 5.223633333333333, 1.72399675, 1.8168421666666665], 'GSM3389445': [19.108742958333334, 1.1092194, 5.18547, 1.686882, 1.670621], 'GSM3389446': [19.391555333333333, 1.0816916, 5.281183333333334, 1.6531565, 1.6446733333333334], 'GSM3389447': [19.286418583333333, 1.0898987999999998, 5.302553333333333, 1.71876375, 1.6457325000000003], 'GSM3389448': [19.522515375, 1.1185262, 5.352506666666667, 1.6493145, 1.71151], 'GSM3389449': [19.578057041666668, 1.1266278, 5.31151, 1.7774335, 1.6792701666666665], 'GSM3389450': [18.877540625, 1.1270904, 5.16885, 1.92639225, 1.697554], 'GSM3389451': [19.362947916666666, 1.1066732, 5.355393333333333, 1.74019525, 1.646772], 'GSM3389452': [19.078110333333335, 1.100166, 5.099033333333334, 2.13423525, 1.6889660000000002], 'GSM3389453': [18.936735791666667, 1.0952416, 5.109243333333334, 1.93456125, 1.6013138333333332], 'GSM3389454': [19.262622583333332, 1.1406402, 5.1989600000000005, 1.78738425, 1.746907166666667], 'GSM3389455': [19.395090500000002, 1.1008904, 5.261326666666666, 1.7648335, 1.6144125], 'GSM3389456': [19.224593875, 1.1089114, 5.307083333333334, 1.8512425, 1.6197523333333332], 'GSM3389457': [18.320307208333332, 1.1460284, 5.15578, 1.878168, 1.6025558333333332], 'GSM3389458': [18.716716291666668, 1.12114, 5.031596666666666, 1.82755375, 1.8188771666666668], 'GSM3389459': [18.634140833333333, 1.1462021999999998, 5.092186666666667, 1.85796825, 1.7658863333333334], 'GSM3389460': [18.633358916666666, 1.1278424, 5.1607666666666665, 1.78226325, 1.7261163333333334], 'GSM3389461': [18.775709125, 1.150956, 5.18331, 1.7041845, 1.7106471666666665], 'GSM3389462': [19.079671041666668, 1.1329598, 5.206286666666666, 1.73045925, 1.718782], 'GSM3389463': [18.4423475, 1.1563114, 4.9302, 1.81671575, 1.7703328333333332], 'GSM3389464': [18.7496355, 1.1391354, 5.15804, 1.854845, 1.6964033333333335], 'GSM3389465': [19.15328841666667, 1.1261182, 5.10417, 1.76670975, 1.6836081666666667], 'GSM3389466': [18.791761333333334, 1.0995534, 5.212246666666666, 1.766327, 1.666493], 'GSM3389467': [18.740280375, 1.1548316, 5.196886666666667, 1.79278675, 1.7122646666666665], 'GSM3389468': [19.145122791666665, 1.082119, 5.228956666666667, 1.8029395, 1.8670101666666667], 'GSM3389469': [18.898090916666668, 1.1396238, 5.072963333333333, 1.80787325, 1.9688328333333334], 'GSM3389470': [18.645174583333333, 1.135412, 5.129286666666666, 1.82013575, 1.5642828333333332], 'GSM3389471': [19.10420166666667, 1.148887, 5.0947233333333335, 1.86757125, 1.5625166666666668], 'GSM3389472': [18.769400875000002, 1.0392348, 5.066263333333334, 1.8024345, 1.7738086666666666], 'GSM3389473': [19.100690541666665, 1.137125, 5.149963333333333, 1.77709925, 1.6887858333333334], 'GSM3389474': [18.941759625, 1.1584252, 5.24584, 1.7753745, 1.7691325], 'GSM3389475': [18.981845333333332, 1.2108716, 5.148923333333333, 1.78238675, 1.8768636666666665], 'GSM3389476': [18.915630791666665, 1.1552, 5.223633333333333, 1.83745, 1.8225348333333333], 'GSM3389477': [18.770355958333333, 1.1698904, 5.08846, 1.7976445, 1.5962211666666668], 'GSM3389478': [19.20123875, 1.14086, 5.243733333333333, 1.76525675, 1.6905780000000001], 'GSM3389479': [18.907136083333334, 1.128651, 5.167073333333334, 1.83496675, 1.6898871666666666], 'GSM3389480': [18.975954208333334, 1.147768, 5.119153333333333, 1.762133, 1.7553375], 'GSM3389481': [18.958092916666665, 1.1432042, 5.227506666666667, 1.766904, 1.9464155], 'GSM3389482': [19.208502416666665, 1.1484738, 5.24441, 1.83951725, 1.7087261666666667], 'GSM3389483': [19.011704625, 1.1649056, 5.27907, 1.7589545, 1.8222633333333333], 'GSM3389484': [18.915144041666668, 1.1552554000000002, 5.32187, 1.75694875, 1.9499106666666668], 'GSM3389485': [19.284386833333333, 1.1276785999999999, 5.214746666666667, 1.737854, 1.6634631666666664], 'GSM3389486': [19.21655575, 1.161606, 5.25099, 1.8906985, 1.9114381666666667], 'GSM3389487': [19.207478875, 1.1456632, 5.137826666666666, 1.780448, 1.6822685], 'GSM3389488': [18.951487125, 1.156711, 5.223583333333333, 1.812077, 1.7135243333333332], 'GSM3389489': [18.98848375, 1.1635572, 5.1963, 1.85242275, 1.6876030000000002], 'GSM3389490': [19.591248833333335, 1.1680338000000001, 5.116556666666667, 1.710934, 1.7491931666666667], 'GSM3389491': [18.984820083333332, 1.1958262, 5.240363333333334, 1.76662375, 1.9081126666666668], 'GSM3389492': [19.191926875, 1.2089904, 5.223366666666666, 1.8091555, 1.7585473333333335], 'GSM3389493': [18.95467675, 1.1678552, 5.17225, 1.76854875, 1.9477878333333332], 'GSM3389494': [19.206733958333334, 1.1176358, 5.258006666666667, 1.76766775, 1.7940874999999998], 'GSM3389495': [18.818138291666667, 1.1510076, 5.0364233333333335, 1.79079875, 1.8495238333333333], 'GSM3389496': [18.980685083333334, 1.1235396, 5.04607, 1.74930525, 1.7597588333333332], 'GSM3389497': [19.86061275, 1.1916932, 5.272036666666667, 1.69703575, 1.8642731666666665], 'GSM3389498': [18.92362575, 1.2231298000000002, 5.202223333333333, 1.74355725, 1.8335241666666664], 'GSM3389499': [19.093890916666666, 1.1665326, 5.271396666666667, 1.810219, 1.9657218333333333], 'GSM3389500': [18.473788125, 1.1817994, 5.163736666666667, 1.94441175, 1.7276643333333332], 'GSM3389501': [19.108870166666666, 1.1775228, 5.23292, 1.81481825, 1.7576093333333334], 'GSM3389502': [18.885767333333334, 1.1411494, 5.025283333333333, 1.9908665, 1.7917528333333332], 'GSM3389503': [19.035625583333335, 1.1299108, 5.112643333333334, 1.95113275, 1.8001241666666665], 'GSM3389504': [18.8857095, 1.2183515999999999, 4.9822733333333336, 1.948499, 1.7656766666666668], 'GSM3389505': [18.797221666666665, 1.1559534, 5.057366666666667, 1.95932475, 1.6441141666666668], 'GSM3389506': [18.800593791666667, 1.1292034, 5.04925, 2.02797875, 1.692393]}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Saved gene expression data to ../../output/preprocess/Kidney_Clear_Cell_Carcinoma/gene_data/GSE119958.csv\n" ] } ], "source": [ "# 1. Decide which columns in the gene annotation to use for mapping\n", "# From examining the data, I can see:\n", "# - 'ID' in gene_annotation matches the index of gene_data (TC01000001.hg.1, etc.)\n", "# - 'gene_assignment' contains gene symbols embedded within longer strings\n", "\n", "print(\"Processing gene mapping...\")\n", "# Extract mapping between probe IDs and gene symbols\n", "prob_col = 'ID'\n", "gene_col = 'gene_assignment'\n", "\n", "# 2. Get the gene mapping dataframe\n", "mapping_data = get_gene_mapping(gene_annotation, prob_col, gene_col)\n", "print(f\"Created mapping data with {len(mapping_data)} rows\")\n", "\n", "# Display sample of mapping to verify\n", "print(\"\\nFirst few rows of mapping data:\")\n", "print(preview_df(mapping_data.head()))\n", "\n", "# 3. Convert probe-level measurements to gene expression data\n", "print(\"\\nApplying gene mapping to convert probe measurements to gene expression data...\")\n", "gene_data = apply_gene_mapping(gene_data, mapping_data)\n", "print(f\"Generated gene expression data with {len(gene_data)} genes and {len(gene_data.columns)} samples\")\n", "\n", "# Display a sample of the gene expression data\n", "print(\"\\nFirst few rows of gene expression data:\")\n", "print(preview_df(gene_data.head()))\n", "\n", "# Save the gene expression 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": "95cbf3c7", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "0a06a614", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:17:49.139182Z", "iopub.status.busy": "2025-03-25T07:17:49.139058Z", "iopub.status.idle": "2025-03-25T07:17:50.831735Z", "shell.execute_reply": "2025-03-25T07:17:50.831341Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Normalizing gene symbols...\n", "After normalization: 24018 unique gene symbols\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene expression data saved to ../../output/preprocess/Kidney_Clear_Cell_Carcinoma/gene_data/GSE119958.csv\n", "\n", "Checking trait availability from previous analysis...\n", "Error loading clinical data: [Errno 2] No such file or directory: '../../output/preprocess/Kidney_Clear_Cell_Carcinoma/clinical_data/GSE119958.csv'\n", "\n", "Linking clinical and genetic data...\n", "Cannot link data: clinical or genetic data is missing.\n", "Reason: Trait column 'Kidney_Clear_Cell_Carcinoma' is not available in clinical data.\n", "\n", "Performing final validation...\n", "Abnormality detected in the cohort: GSE119958. Preprocessing failed.\n", "Dataset not usable for Kidney_Clear_Cell_Carcinoma association studies. Data not saved.\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "print(\"\\nNormalizing gene symbols...\")\n", "try:\n", " # Check if gene_data is empty before normalization\n", " if gene_data.empty:\n", " print(\"Gene data is empty. Skipping normalization.\")\n", " normalized_gene_data = gene_data\n", " else:\n", " normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", " print(f\"After normalization: {len(normalized_gene_data.index)} unique gene symbols\")\n", " \n", " # Save the normalized gene data (even if empty, to maintain consistent workflow)\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 expression data saved to {out_gene_data_file}\")\n", "except Exception as e:\n", " print(f\"Error normalizing gene symbols: {e}\")\n", " normalized_gene_data = gene_data # Use original data if normalization fails\n", "\n", "# Use is_trait_available from previous steps, don't recheck trait_row\n", "print(\"\\nChecking trait availability from previous analysis...\")\n", "# Load the clinical data from the previously saved file\n", "try:\n", " clinical_df = pd.read_csv(out_clinical_data_file)\n", " print(f\"Loaded clinical data with shape: {clinical_df.shape}\")\n", " # Rename the column '1' to the trait name if it exists\n", " if '1' in clinical_df.columns:\n", " clinical_df = clinical_df.rename(columns={'1': trait})\n", " print(f\"Renamed column '1' to '{trait}'\")\n", " is_trait_available = trait in clinical_df.columns\n", "except Exception as e:\n", " print(f\"Error loading clinical data: {e}\")\n", " clinical_df = pd.DataFrame()\n", " is_trait_available = False\n", "\n", "# 2. Link clinical and genetic data if available\n", "print(\"\\nLinking clinical and genetic data...\")\n", "try:\n", " if is_trait_available and not normalized_gene_data.empty:\n", " # Print sample IDs from both datasets for debugging\n", " print(\"Clinical columns:\", list(clinical_df.columns))\n", " print(\"First few genetic sample columns:\", list(normalized_gene_data.columns)[:5])\n", " \n", " # Transpose clinical data so samples are rows\n", " if clinical_df.shape[0] == 1: # If it's currently 1 row with samples as columns\n", " clinical_df = clinical_df.T\n", " clinical_df.columns = [trait]\n", " print(f\"Transposed clinical data to shape: {clinical_df.shape}\")\n", " \n", " # Now link clinical and genetic data\n", " linked_data = pd.concat([clinical_df, normalized_gene_data.T], axis=1)\n", " print(f\"Linked data shape: {linked_data.shape}\")\n", " \n", " # Check if we have at least one sample with trait value\n", " if trait in linked_data.columns:\n", " trait_count = linked_data[trait].count()\n", " print(f\"Number of samples with trait values: {trait_count}\")\n", " \n", " if trait_count > 0:\n", " # 3. Handle missing values systematically\n", " print(\"\\nHandling missing values...\")\n", " linked_data = handle_missing_values(linked_data, trait)\n", " print(f\"After handling missing values, data shape: {linked_data.shape}\")\n", " \n", " # Check if we still have samples after missing value handling\n", " if linked_data.shape[0] > 0:\n", " # 4. Determine whether the trait and demographic features are biased\n", " print(\"\\nChecking for bias in features...\")\n", " is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", " else:\n", " print(\"Error: All samples were removed during missing value handling.\")\n", " is_biased = True\n", " else:\n", " print(\"No samples have valid trait values. Dataset cannot be used.\")\n", " is_biased = True\n", " else:\n", " print(f\"Trait column '{trait}' not found in linked data.\")\n", " is_biased = True\n", " else:\n", " print(\"Cannot link data: clinical or genetic data is missing.\")\n", " if not is_trait_available:\n", " print(f\"Reason: Trait column '{trait}' is not available in clinical data.\")\n", " if normalized_gene_data.empty:\n", " print(\"Reason: Gene expression data is empty.\")\n", " linked_data = pd.DataFrame()\n", " is_biased = True\n", " \n", "except Exception as e:\n", " print(f\"Error in linking clinical and genetic data: {e}\")\n", " linked_data = pd.DataFrame()\n", " is_biased = True\n", "\n", "# 5. Final quality validation\n", "print(\"\\nPerforming final validation...\")\n", "note = \"\"\n", "if 'linked_data' in locals() and linked_data.empty:\n", " if not normalized_gene_data.empty and not clinical_df.empty:\n", " note = \"Dataset failed in linking phase. Clinical data structure could not be properly matched with gene expression data.\"\n", " elif normalized_gene_data.empty:\n", " note = \"Dataset failed in gene mapping phase. Could not properly map probe IDs to gene symbols.\"\n", " elif clinical_df.empty:\n", " note = \"Dataset failed in clinical data extraction. No valid trait information available.\"\n", "elif 'is_biased' in locals() and is_biased:\n", " note = \"Dataset passed initial processing but contains severely biased trait distribution.\"\n", "\n", "is_usable = validate_and_save_cohort_info(\n", " is_final=True,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=not normalized_gene_data.empty, # Set based on actual gene data availability\n", " is_trait_available=is_trait_available,\n", " is_biased=is_biased if 'is_biased' in locals() else True,\n", " df=linked_data if 'linked_data' in locals() and not linked_data.empty else pd.DataFrame(),\n", " note=note\n", ")\n", "\n", "# 6. Save linked data if usable\n", "if is_usable and 'linked_data' in locals() and not linked_data.empty:\n", " # Create directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " \n", " # Save linked data\n", " linked_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", "else:\n", " print(f\"Dataset not usable for {trait} association studies. Data not saved.\")" ] } ], "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.16" } }, "nbformat": 4, "nbformat_minor": 5 }