{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "e9bc5b45", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:10:41.442047Z", "iopub.status.busy": "2025-03-25T04:10:41.441929Z", "iopub.status.idle": "2025-03-25T04:10:41.626107Z", "shell.execute_reply": "2025-03-25T04:10:41.625659Z" } }, "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 = \"Thyroid_Cancer\"\n", "cohort = \"GSE151181\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Thyroid_Cancer\"\n", "in_cohort_dir = \"../../input/GEO/Thyroid_Cancer/GSE151181\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Thyroid_Cancer/GSE151181.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Thyroid_Cancer/gene_data/GSE151181.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Thyroid_Cancer/clinical_data/GSE151181.csv\"\n", "json_path = \"../../output/preprocess/Thyroid_Cancer/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "3c7c84b4", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "64964192", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:10:41.627524Z", "iopub.status.busy": "2025-03-25T04:10:41.627386Z", "iopub.status.idle": "2025-03-25T04:10:41.805948Z", "shell.execute_reply": "2025-03-25T04:10:41.805549Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Gene and miRNA expression in radioiodine refractory and avid papillary thyroid carcinomas\"\n", "!Series_summary\t\"This SuperSeries is composed of the SubSeries listed below.\"\n", "!Series_overall_design\t\"Refer to individual Series\"\n", "Sample Characteristics Dictionary:\n", "{0: ['histological variant: Classical', 'histological variant: Follicular', 'histological variant: NA', 'histological variant: non-neoplastic thyroid'], 1: ['tissue type: Primary tumor', 'tissue type: synchronous lymph node metastasis', 'tissue type: lymph node metastasis post RAI', 'tissue type: lymph node metastasis_2 post RAI', 'tissue type: lymph node metastasis_1 post RAI', 'tissue type: non-neoplastic thyroid'], 2: ['collection before/after rai: Before', 'collection before/after rai: After'], 3: ['patient id: pt_1', 'patient id: pt_2', 'patient id: pt_3', 'patient id: pt_5', 'patient id: pt_7', 'patient id: pt_8', 'patient id: pt_11', 'patient id: pt_12', 'patient id: pt_13', 'patient id: pt_14', 'patient id: pt_15', 'patient id: pt_19', 'patient id: pt_21', 'patient id: pt_22', 'patient id: pt_23', 'patient id: pt_25', 'patient id: pt_27', 'patient id: pt_28', 'patient id: pt_29', 'patient id: pt_32', 'patient id: pt_34', 'patient id: pt_35', 'patient id: pt_37', 'patient id: pt_39', 'patient id: pt_40', 'patient id: pt_41', 'patient id: pt_42', 'patient id: pt_44', 'patient id: pt_45', 'patient id: pt_46'], 4: ['patient rai responce: Avid', 'patient rai responce: Refractory'], 5: ['rai uptake at the metastatic site: Yes', 'rai uptake at the metastatic site: No'], 6: ['disease: Remission', 'disease: Persistence'], 7: ['lesion by ptc-ma: WT', 'lesion by ptc-ma: BRAFV600E', 'lesion by ptc-ma: RET/PTC1', 'lesion by ptc-ma: RET/PTC1 e NTRK-T1', 'lesion by ptc-ma: RET/PTC3', 'lesion by ptc-ma: NTRK', 'lesion by ptc-ma: TERT228', 'lesion by ptc-ma: TERT250', 'lesion by ptc-ma: BRAFV600E + TERT228', 'lesion by ptc-ma: non-neoplastic thyroid'], 8: ['lesion class: WT', 'lesion class: BRAFV600E', 'lesion class: Fusion', 'lesion class: pTERT', 'lesion class: BRAFV600E +pTERT', 'lesion class: non-neoplastic thyroid'], 9: ['patients with available multiple tumor tissues: No', 'patients with available multiple tumor tissues: pz_7', 'patients with available multiple tumor tissues: pz_22', 'patients with available multiple tumor tissues: pz_34', 'patients with available multiple tumor tissues: pz_40', 'patients with available multiple tumor tissues: pz_41', 'patients with available multiple tumor tissues: pz_42'], 10: ['tumor purity class by cibersort: high purity', 'tumor purity class by cibersort: low purity']}\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": "37e28e31", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "8ad0dbae", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:10:41.807010Z", "iopub.status.busy": "2025-03-25T04:10:41.806892Z", "iopub.status.idle": "2025-03-25T04:10:41.814810Z", "shell.execute_reply": "2025-03-25T04:10:41.814483Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Clinical Data Preview:\n", "{'GSM4567964': [0.0], 'GSM4567965': [0.0], 'GSM4567966': [0.0], 'GSM4567967': [0.0], 'GSM4567968': [1.0], 'GSM4567969': [1.0], 'GSM4567970': [1.0], 'GSM4567971': [1.0], 'GSM4567972': [1.0], 'GSM4567973': [1.0], 'GSM4567974': [1.0], 'GSM4567975': [1.0], 'GSM4567976': [1.0], 'GSM4567977': [1.0], 'GSM4567978': [1.0], 'GSM4567979': [1.0], 'GSM4567980': [1.0], 'GSM4567981': [1.0], 'GSM4567982': [1.0], 'GSM4567983': [1.0], 'GSM4567984': [1.0], 'GSM4567985': [1.0], 'GSM4567986': [1.0], 'GSM4567987': [1.0], 'GSM4567988': [1.0], 'GSM4567989': [1.0], 'GSM4567990': [1.0], 'GSM4567991': [1.0], 'GSM4567992': [1.0], 'GSM4567993': [1.0], 'GSM4567994': [1.0], 'GSM4567995': [1.0], 'GSM4567996': [1.0], 'GSM4567997': [1.0], 'GSM4567998': [1.0], 'GSM4567999': [1.0], 'GSM4568000': [0.0], 'GSM4568001': [0.0], 'GSM4568002': [1.0], 'GSM4568003': [1.0], 'GSM4568004': [1.0], 'GSM4568005': [1.0], 'GSM4568006': [1.0], 'GSM4568007': [1.0], 'GSM4568008': [1.0], 'GSM4568009': [1.0], 'GSM4568010': [1.0]}\n", "Clinical data saved to ../../output/preprocess/Thyroid_Cancer/clinical_data/GSE151181.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the title and summary, this dataset likely contains gene expression data\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "\n", "# For trait data, looking at the characteristics:\n", "# We can use \"patient rai responce\" (row 4) which indicates whether the patient is Avid or Refractory for radioiodine\n", "trait_row = 4\n", "\n", "# Age data is not available in the sample characteristics\n", "age_row = None\n", "\n", "# Gender data is not available in the sample characteristics\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value):\n", " \"\"\"Convert thyroid cancer trait (radioiodine response) to binary format\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Avid = 0 (responsive to radioiodine treatment)\n", " # Refractory = 1 (non-responsive to radioiodine treatment)\n", " if value.lower() == 'avid':\n", " return 0\n", " elif value.lower() == 'refractory':\n", " return 1\n", " else:\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age to continuous format\"\"\"\n", " # Not used since age data is not available\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender to binary format\"\"\"\n", " # Not used since gender data is not available\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Determine if trait data is available\n", "is_trait_available = trait_row is not None\n", "\n", "# Save the 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", "if trait_row is not None:\n", " # Use the library function to extract clinical features\n", " clinical_df = geo_select_clinical_features(\n", " clinical_df=clinical_data,\n", " trait=trait,\n", " trait_row=trait_row,\n", " convert_trait=convert_trait,\n", " age_row=age_row,\n", " convert_age=convert_age,\n", " gender_row=gender_row,\n", " convert_gender=convert_gender\n", " )\n", " \n", " # Preview the clinical dataframe\n", " preview = preview_df(clinical_df)\n", " print(\"Clinical Data Preview:\")\n", " print(preview)\n", " \n", " # Save clinical data to CSV\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " clinical_df.to_csv(out_clinical_data_file, index=False)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "9ee9c99f", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "beacc9c5", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:10:41.816054Z", "iopub.status.busy": "2025-03-25T04:10:41.815946Z", "iopub.status.idle": "2025-03-25T04:10:42.088694Z", "shell.execute_reply": "2025-03-25T04:10:42.088166Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SOFT file: ../../input/GEO/Thyroid_Cancer/GSE151181/GSE151181_family.soft.gz\n", "Matrix file: ../../input/GEO/Thyroid_Cancer/GSE151181/GSE151181-GPL21575_series_matrix.txt.gz\n", "Found the matrix table marker in the file.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape: (62976, 47)\n", "First 20 gene/probe identifiers:\n", "['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20']\n" ] } ], "source": [ "# 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", "print(f\"SOFT file: {soft_file}\")\n", "print(f\"Matrix file: {matrix_file}\")\n", "\n", "# Set gene availability flag\n", "is_gene_available = True # Initially assume gene data is available\n", "\n", "# First check if the matrix file contains the expected marker\n", "found_marker = False\n", "try:\n", " with gzip.open(matrix_file, 'rt') as file:\n", " for line in file:\n", " if \"!series_matrix_table_begin\" in line:\n", " found_marker = True\n", " break\n", " \n", " if found_marker:\n", " print(\"Found the matrix table marker in the file.\")\n", " else:\n", " print(\"Warning: Could not find '!series_matrix_table_begin' marker in the file.\")\n", " \n", " # Try to extract gene data from the matrix file\n", " gene_data = get_genetic_data(matrix_file)\n", " \n", " if gene_data.shape[0] == 0:\n", " print(\"Warning: Extracted gene data has 0 rows.\")\n", " is_gene_available = False\n", " else:\n", " print(f\"Gene data shape: {gene_data.shape}\")\n", " # Print the first 20 gene/probe identifiers\n", " print(\"First 20 gene/probe identifiers:\")\n", " print(gene_data.index[:20].tolist())\n", " \n", "except Exception as e:\n", " print(f\"Error extracting gene data: {e}\")\n", " is_gene_available = False\n", " \n", " # Try to diagnose the file format\n", " print(\"Examining file content to diagnose the issue:\")\n", " try:\n", " with gzip.open(matrix_file, 'rt') as file:\n", " for i, line in enumerate(file):\n", " if i < 10: # Print first 10 lines to diagnose\n", " print(f\"Line {i}: {line.strip()[:100]}...\") # Print first 100 chars of each line\n", " else:\n", " break\n", " except Exception as e2:\n", " print(f\"Error examining file: {e2}\")\n", "\n", "if not is_gene_available:\n", " print(\"Gene expression data could not be successfully extracted from this dataset.\")\n" ] }, { "cell_type": "markdown", "id": "0479301d", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "8fef340c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:10:42.090170Z", "iopub.status.busy": "2025-03-25T04:10:42.090053Z", "iopub.status.idle": "2025-03-25T04:10:42.092209Z", "shell.execute_reply": "2025-03-25T04:10:42.091854Z" } }, "outputs": [], "source": [ "# Reviewing gene identifiers in the dataset\n", "# These don't appear to be standard human gene symbols\n", "# They look like probe IDs from a microarray platform (GPL23159) that need mapping\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "f8063ad9", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "6349ed8f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:10:42.093339Z", "iopub.status.busy": "2025-03-25T04:10:42.093232Z", "iopub.status.idle": "2025-03-25T04:10:47.985926Z", "shell.execute_reply": "2025-03-25T04:10:47.985428Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene annotation preview:\n", "Columns in gene annotation: ['ID', 'COL', 'ROW', 'SPOT_ID', 'CONTROL_TYPE', 'miRNA_ID', 'GENE_SYMBOL', 'GENE_NAME', 'ACCESSION_STRING', 'CHROMOSOMAL_LOCATION']\n", "{'ID': ['1', '2', '3', '4', '5'], 'COL': ['192', '192', '192', '192', '192'], 'ROW': ['328', '326', '324', '322', '320'], 'SPOT_ID': ['miRNABrightCorner30', 'Blank', 'Blank', 'Blank', 'Blank'], 'CONTROL_TYPE': ['pos', 'pos', 'pos', 'pos', 'pos'], 'miRNA_ID': [nan, nan, nan, nan, nan], 'GENE_SYMBOL': [nan, nan, nan, nan, nan], 'GENE_NAME': [nan, nan, nan, nan, nan], 'ACCESSION_STRING': [nan, nan, nan, nan, nan], 'CHROMOSOMAL_LOCATION': [nan, nan, nan, nan, nan]}\n", "\n", "Complete sample of a few rows:\n", " ID COL ROW SPOT_ID CONTROL_TYPE miRNA_ID GENE_SYMBOL GENE_NAME ACCESSION_STRING CHROMOSOMAL_LOCATION\n", "0 1 192 328 miRNABrightCorner30 pos NaN NaN NaN NaN NaN\n", "1 2 192 326 Blank pos NaN NaN NaN NaN NaN\n", "2 3 192 324 Blank pos NaN NaN NaN NaN NaN\n", "\n", "Potential gene-related columns: ['ID', 'SPOT_ID', 'miRNA_ID', 'GENE_SYMBOL', 'GENE_NAME']\n", "\n", "Sample of probe ID to gene symbol mappings:\n", " ID Gene\n", "10 11 hsa-miR-3132\n", "12 13 hsa-miR-449c-5p\n", "13 14 hsa-miR-4481\n", "14 15 hsa-miR-6788-5p\n", "15 16 hsa-miR-514a-3p\n", "16 17 hsa-miR-3907\n", "17 18 hsa-miR-516b-5p\n", "20 21 hsa-miR-7153-3p\n", "22 23 hsa-miR-7843-3p\n", "23 24 hsa-miR-6845-3p\n", "\n", "Total number of probe-to-gene mappings: 75332\n" ] } ], "source": [ "# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "gene_annotation = get_gene_annotation(soft_file)\n", "\n", "# 2. Analyze the gene annotation dataframe to identify which columns contain the gene identifiers and gene symbols\n", "print(\"\\nGene annotation preview:\")\n", "print(f\"Columns in gene annotation: {gene_annotation.columns.tolist()}\")\n", "print(preview_df(gene_annotation, n=5))\n", "\n", "# Get a more complete view to understand the annotation structure\n", "print(\"\\nComplete sample of a few rows:\")\n", "print(gene_annotation.iloc[:3].to_string())\n", "\n", "# Check for columns that might contain gene information\n", "potential_gene_columns = [col for col in gene_annotation.columns if \n", " any(term in col.upper() for term in [\"GENE\", \"SYMBOL\", \"NAME\", \"ID\"])]\n", "print(f\"\\nPotential gene-related columns: {potential_gene_columns}\")\n", "\n", "# Correctly identify the columns for probe ID and gene symbols\n", "gene_id_col = 'ID' # This is the probe identifier column\n", "gene_symbol_col = 'GENE_SYMBOL' # Corrected to match the actual column name in the data\n", "\n", "# Verify columns exist before mapping\n", "if gene_id_col in gene_annotation.columns and gene_symbol_col in gene_annotation.columns:\n", " # Create the mapping using the library function\n", " mapping_data = get_gene_mapping(gene_annotation, gene_id_col, gene_symbol_col)\n", " \n", " # Print sample of the mapping to confirm\n", " print(\"\\nSample of probe ID to gene symbol mappings:\")\n", " print(mapping_data.head(10))\n", " \n", " # Check the size of the mapping data\n", " print(f\"\\nTotal number of probe-to-gene mappings: {len(mapping_data)}\")\n", "else:\n", " missing_cols = []\n", " if gene_id_col not in gene_annotation.columns:\n", " missing_cols.append(gene_id_col)\n", " if gene_symbol_col not in gene_annotation.columns:\n", " missing_cols.append(gene_symbol_col)\n", " print(f\"\\nError: The following columns are missing from the annotation data: {missing_cols}\")\n" ] }, { "cell_type": "markdown", "id": "d4fc050d", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "a5f3b356", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:10:47.987247Z", "iopub.status.busy": "2025-03-25T04:10:47.987122Z", "iopub.status.idle": "2025-03-25T04:10:54.425199Z", "shell.execute_reply": "2025-03-25T04:10:54.424759Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data ID format: ['1', '2', '3', '4', '5']\n", "Sample of gene annotation data:\n", " ID miRNA_ID GENE_SYMBOL\n", "0 1 NaN NaN\n", "1 2 NaN NaN\n", "2 3 NaN NaN\n", "3 4 NaN NaN\n", "4 5 NaN NaN\n", "5 6 NaN NaN\n", "6 7 NaN NaN\n", "7 8 NaN NaN\n", "8 9 NaN NaN\n", "9 10 NaN NaN\n", "\n", "Non-null count for potential ID columns:\n", "miRNA_ID: 75332\n", "GENE_SYMBOL: 75332\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Direct matches between expression IDs and annotation IDs: 62976\n", "\n", "Sample of mapping data (before conversion):\n", " ID Gene\n", "10 11 hsa-miR-3132\n", "12 13 hsa-miR-449c-5p\n", "13 14 hsa-miR-4481\n", "14 15 hsa-miR-6788-5p\n", "15 16 hsa-miR-514a-3p\n", "16 17 hsa-miR-3907\n", "17 18 hsa-miR-516b-5p\n", "20 21 hsa-miR-7153-3p\n", "22 23 hsa-miR-7843-3p\n", "23 24 hsa-miR-6845-3p\n", "\n", "Overlap between expression IDs and mappable IDs: 50980\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Shape of the gene expression data after mapping: (0, 47)\n", "First few gene symbols after mapping:\n", "No genes mapped\n", "\n", "This dataset does not contain sufficient gene expression data for our analysis.\n", "Setting is_gene_available to False.\n" ] } ], "source": [ "# 1. Determine which columns store probe IDs and gene symbols in the annotation data\n", "# Print a sample of the gene annotation data to understand the structure\n", "gene_annotation = get_gene_annotation(soft_file)\n", "\n", "# Get a sample of gene expression data IDs\n", "gene_expr_data = get_genetic_data(matrix_file)\n", "print(\"Gene expression data ID format:\", gene_expr_data.index[:5].tolist())\n", "\n", "# Sample annotation data to understand the structure\n", "print(\"Sample of gene annotation data:\")\n", "print(gene_annotation[['ID', 'miRNA_ID', 'GENE_SYMBOL']].head(10))\n", "\n", "# Check how many non-null values we have for miRNA_ID vs GENE_SYMBOL\n", "print(\"\\nNon-null count for potential ID columns:\")\n", "print(f\"miRNA_ID: {gene_annotation['miRNA_ID'].notna().sum()}\")\n", "print(f\"GENE_SYMBOL: {gene_annotation['GENE_SYMBOL'].notna().sum()}\")\n", "\n", "# Based on the observations from Step 5, this appears to be miRNA data, not gene expression\n", "# Let's check for matches between expression data IDs and annotation IDs\n", "expr_ids = set(gene_expr_data.index.astype(str))\n", "ann_ids = set(gene_annotation['ID'].astype(str))\n", "overlap = expr_ids.intersection(ann_ids)\n", "print(f\"\\nDirect matches between expression IDs and annotation IDs: {len(overlap)}\")\n", "\n", "# Create a mapping dataframe that considers both miRNA_ID and GENE_SYMBOL\n", "mapping_data = gene_annotation[['ID', 'miRNA_ID']].copy()\n", "mapping_data.rename(columns={'miRNA_ID': 'Gene'}, inplace=True)\n", "mapping_data = mapping_data.dropna(subset=['Gene'])\n", "\n", "# Print a sample of the mapping data\n", "print(\"\\nSample of mapping data (before conversion):\")\n", "print(mapping_data.head(10))\n", "\n", "# Check how many expression IDs can be mapped\n", "mappable_ids = set(mapping_data['ID'].astype(str))\n", "mappable_overlap = expr_ids.intersection(mappable_ids)\n", "print(f\"\\nOverlap between expression IDs and mappable IDs: {len(mappable_overlap)}\")\n", "\n", "# Convert IDs to strings for proper matching\n", "mapping_data['ID'] = mapping_data['ID'].astype(str)\n", "\n", "# Apply the gene mapping function\n", "try:\n", " gene_data = apply_gene_mapping(gene_expr_data, mapping_data)\n", " print(f\"\\nShape of the gene expression data after mapping: {gene_data.shape}\")\n", " print(\"First few gene symbols after mapping:\")\n", " print(gene_data.index[:10].tolist() if not gene_data.empty else \"No genes mapped\")\n", "except Exception as e:\n", " print(f\"\\nError during gene mapping: {e}\")\n", " # Since this appears to be miRNA data rather than gene expression data,\n", " # we should update is_gene_available\n", " is_gene_available = False\n", " gene_data = pd.DataFrame()\n", " print(\"This dataset contains miRNA data rather than gene expression data.\")\n", " print(\"Setting is_gene_available to False.\")\n", "\n", "# Even if the mapping fails, let's reassess whether this is actually gene expression data\n", "if gene_data.empty or len(gene_data.index) < 100: # Arbitrary threshold for minimum mapped genes\n", " is_gene_available = False\n", " print(\"\\nThis dataset does not contain sufficient gene expression data for our analysis.\")\n", " print(\"Setting is_gene_available to False.\")\n", " \n", " # Update this information in the JSON file\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=(trait_row is not None)\n", " )" ] } ], "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 }