{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "a74c1e64", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:00:54.849965Z", "iopub.status.busy": "2025-03-25T06:00:54.849533Z", "iopub.status.idle": "2025-03-25T06:00:55.015282Z", "shell.execute_reply": "2025-03-25T06:00:55.014922Z" } }, "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 = \"Osteoporosis\"\n", "cohort = \"GSE152073\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Osteoporosis\"\n", "in_cohort_dir = \"../../input/GEO/Osteoporosis/GSE152073\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Osteoporosis/GSE152073.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Osteoporosis/gene_data/GSE152073.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Osteoporosis/clinical_data/GSE152073.csv\"\n", "json_path = \"../../output/preprocess/Osteoporosis/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "b6fc1562", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "f3431ced", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:00:55.016771Z", "iopub.status.busy": "2025-03-25T06:00:55.016627Z", "iopub.status.idle": "2025-03-25T06:00:55.254751Z", "shell.execute_reply": "2025-03-25T06:00:55.254383Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Gene expression data from Brazilian SPAH study\"\n", "!Series_summary\t\"This study is part of previous epidemiologic project, including a population-based survey (Sao Paulo Ageing & Health study (SPAH Study). The data from this study was collected between 2015 to 2016 and involved elderly women (ages ≥65 yeas) living in the Butanta district, Sao Paulo. The purpose of the study was identification of association between transcriptome and the osteo metabolism diseases phenotype, like osteoporosis, vertebral fracture and coronary calcification.\"\n", "!Series_summary\t\"Peripheral blood cells suffer alterations in the gene expression pattern in response to perturbations caused by calcium metabolism diseases. The purpose of this study is to identify possible molecular markers associated with osteoporosis, vertebral fractures and coronary calcification in elderly women from community from Brazilian SPAH study. Vertebral fractures were the most common clinical manifestation of osteoporosis and coronary calcifications were associated with high morbimortality.\"\n", "!Series_overall_design\t\"Fasting blood samples were withdrawn from community elderly women with osteo metabolism diseases. RNA was extracted from peripheral total blood, and hybridized into Affymetrix microarrays.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['gender: female'], 1: ['age (years): 76', 'age (years): 77', 'age (years): 75', 'age (years): 80', 'age (years): 82', 'age (years): 83', 'age (years): 78', 'age (years): 74', 'age (years): 81', 'age (years): 91', 'age (years): 79', 'age (years): 88', 'age (years): 87', 'age (years): 86', 'age (years): 70', 'age (years): 85', 'age (years): 73', 'age (years): 84'], 2: [nan, 'height (cm): 153']}\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": "c60f4afb", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "b0ee682e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:00:55.256058Z", "iopub.status.busy": "2025-03-25T06:00:55.255921Z", "iopub.status.idle": "2025-03-25T06:00:55.262227Z", "shell.execute_reply": "2025-03-25T06:00:55.261921Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A new JSON file was created at: ../../output/preprocess/Osteoporosis/cohort_info.json\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Analyze the dataset and set availability flags\n", "\n", "# 1. Gene expression availability\n", "# Based on the Series Title and Summary, this dataset contains gene expression data\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Determine row keys for trait, age, and gender\n", "# Looking at sample characteristics dictionary\n", "\n", "# For trait (osteoporosis), there's no direct mention in the sample characteristics.\n", "# Based on the background information, this study is specifically about osteoporosis in elderly women.\n", "# The entire dataset is about osteoporosis, but we don't have a specific indicator for which \n", "# subjects have osteoporosis and which don't. Without this differentiation, we can't use the dataset\n", "# for association studies.\n", "trait_row = None\n", "\n", "# For age, looking at the sample characteristics, we can see age data is in row 1\n", "age_row = 1\n", "\n", "# For gender, we can see from sample characteristics that all samples are female (row 0)\n", "# Since this is a constant feature (all female), we consider it not available\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion Functions\n", "def convert_trait(value):\n", " # Since we couldn't identify trait data that differentiates subjects, this function won't be used\n", " return None\n", "\n", "def convert_age(value):\n", " # Extract age value which comes after \"age (years): \"\n", " if pd.isna(value):\n", " return None\n", " try:\n", " age_str = value.split(\": \")[1]\n", " return float(age_str) # Convert to continuous value\n", " except (IndexError, ValueError):\n", " return None\n", "\n", "def convert_gender(value):\n", " # This function won't be used as gender is constant, but for completeness:\n", " if pd.isna(value):\n", " return None\n", " try:\n", " gender = value.split(\": \")[1].lower()\n", " if \"female\" in gender:\n", " return 0\n", " elif \"male\" in gender:\n", " return 1\n", " else:\n", " return None\n", " except (IndexError, ValueError):\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Determine trait data availability\n", "is_trait_available = trait_row is not None\n", "\n", "# Validate and save cohort info (initial filtering)\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 should skip this step\n" ] }, { "cell_type": "markdown", "id": "b695fa89", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "280bd824", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:00:55.263363Z", "iopub.status.busy": "2025-03-25T06:00:55.263254Z", "iopub.status.idle": "2025-03-25T06:00:55.989925Z", "shell.execute_reply": "2025-03-25T06:00:55.989530Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix table marker not found in first 100 lines\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "KeyError: \"Only a column name can be used for the key in a dtype mappings argument. 'ID' not found in columns.\"\n", "\n", "Trying alternative approach to read the gene data:\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Column names: Index(['GSM4602151', 'GSM4602152', 'GSM4602153', 'GSM4602154', 'GSM4602155'], dtype='object')\n", "First 20 row IDs: Index(['TC01000005.hg.1', 'TC01000006.hg.1', 'TC01000007.hg.1',\n", " 'TC01000008.hg.1', 'TC01000009.hg.1', 'TC01000010.hg.1',\n", " 'TC01000011.hg.1', 'TC01000012.hg.1', 'TC01000013.hg.1',\n", " 'TC01000014.hg.1', 'TC01000015.hg.1', 'TC01000016.hg.1',\n", " 'TC01000017.hg.1', 'TC01000018.hg.1', 'TC01000019.hg.1',\n", " 'TC01000020.hg.1', 'TC01000021.hg.1', 'TC01000022.hg.1',\n", " 'TC01000023.hg.1', 'TC01000024.hg.1'],\n", " dtype='object', name='ID_REF')\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": "2b24a638", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "2d4467a2", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:00:55.991265Z", "iopub.status.busy": "2025-03-25T06:00:55.991135Z", "iopub.status.idle": "2025-03-25T06:00:55.993171Z", "shell.execute_reply": "2025-03-25T06:00:55.992873Z" } }, "outputs": [], "source": [ "# Analyzing the gene identifiers\n", "# The identifiers like 'TC01000005.hg.1' appear to be Affymetrix transcript cluster IDs\n", "# These are probe set IDs from the Affymetrix Human Gene ST or similar arrays\n", "# and need to be mapped to standard human gene symbols for analysis\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "15dafe62", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "01bbb6f2", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:00:55.994329Z", "iopub.status.busy": "2025-03-25T06:00:55.994223Z", "iopub.status.idle": "2025-03-25T06:01:04.626520Z", "shell.execute_reply": "2025-03-25T06:01:04.626137Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\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" ] } ], "source": [ "# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n", "gene_annotation = get_gene_annotation(soft_file)\n", "\n", "# 2. Use the 'preview_df' function from the library to preview the data and print out the results.\n", "print(\"Gene annotation preview:\")\n", "print(preview_df(gene_annotation))\n" ] }, { "cell_type": "markdown", "id": "42038dae", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "0c8a724b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:01:04.627824Z", "iopub.status.busy": "2025-03-25T06:01:04.627693Z", "iopub.status.idle": "2025-03-25T06:01:05.718960Z", "shell.execute_reply": "2025-03-25T06:01:05.718527Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data after mapping (first 5 rows, 5 columns):\n", " GSM4602151 GSM4602152 GSM4602153 GSM4602154 GSM4602155\n", "Gene \n", "A- 11.755882 11.890018 12.163401 11.986312 11.689942\n", "A-I 1.880696 1.872455 1.861592 1.794649 1.839120\n", "A-II 1.415920 1.302811 1.289576 1.258598 1.275517\n", "A-IV 0.680075 0.685423 0.664525 0.681213 0.689146\n", "A-V 1.070617 1.043819 1.006562 1.083745 1.040385\n", "Shape of gene expression data: (49166, 90)\n" ] } ], "source": [ "# First properly define gene_data from the previous step\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 data into a DataFrame\n", " gene_data = pd.read_csv(file, sep='\\t', index_col=0)\n", "\n", "# 1. Identify the columns in gene_annotation that match our needs\n", "# The 'ID' column in gene_annotation contains the probe identifiers\n", "# The 'gene_assignment' column contains the gene symbol information in a complex format\n", "\n", "# 2. Create gene mapping dataframe\n", "mapping_data = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='gene_assignment')\n", "\n", "# 3. Apply gene mapping to convert probe-level measurements to gene expression data\n", "# This function will extract human gene symbols from the gene_assignment text\n", "# and handle many-to-many relationships between probes and genes\n", "gene_data = apply_gene_mapping(gene_data, mapping_data)\n", "\n", "# Preview the gene expression data to verify the mapping worked\n", "print(\"Gene expression data after mapping (first 5 rows, 5 columns):\")\n", "print(gene_data.iloc[:5, :5])\n", "print(f\"Shape of gene expression data: {gene_data.shape}\")\n" ] }, { "cell_type": "markdown", "id": "04144f88", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "6471f16f", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:01:05.720443Z", "iopub.status.busy": "2025-03-25T06:01:05.720326Z", "iopub.status.idle": "2025-03-25T06:01:06.778333Z", "shell.execute_reply": "2025-03-25T06:01:06.777947Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Osteoporosis/gene_data/GSE152073.csv\n", "Dataset contains gene expression data but lacks sample-level trait information.\n", "Only the normalized gene data was saved. No linked data was created.\n" ] } ], "source": [ "# 1. Normalize the obtained gene data with the 'normalize_gene_symbols_in_index' function from the library.\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "normalized_gene_data.to_csv(out_gene_data_file)\n", "print(f\"Normalized gene data saved to {out_gene_data_file}\")\n", "\n", "# Since trait_row is None, trait data is not available\n", "is_trait_available = trait_row is not None\n", "is_gene_available = True\n", "\n", "# Create a minimal valid dataframe with the gene data columns\n", "# Using a subset of the gene data to create a valid dataframe structure\n", "minimal_df = pd.DataFrame(index=normalized_gene_data.index[:5], columns=normalized_gene_data.columns[:5])\n", "\n", "# Conduct final validation with proper parameters\n", "is_usable = validate_and_save_cohort_info(\n", " is_final=True, \n", " cohort=cohort, \n", " info_path=json_path, \n", " is_gene_available=is_gene_available, \n", " is_trait_available=is_trait_available, \n", " is_biased=True, # Without trait data, the dataset is considered biased/unusable\n", " df=minimal_df, # Minimal but valid dataframe\n", " note=\"Dataset contains gene expression data but lacks per-sample osteoporosis classification.\"\n", ")\n", "\n", "print(\"Dataset contains gene expression data but lacks sample-level trait information.\")\n", "print(\"Only the normalized gene data was saved. No linked data was created.\")" ] } ], "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 }