{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "bfefb0be", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:04:12.149972Z", "iopub.status.busy": "2025-03-25T04:04:12.149717Z", "iopub.status.idle": "2025-03-25T04:04:12.334406Z", "shell.execute_reply": "2025-03-25T04:04:12.334078Z" } }, "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 = \"Stroke\"\n", "cohort = \"GSE125771\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Stroke\"\n", "in_cohort_dir = \"../../input/GEO/Stroke/GSE125771\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Stroke/GSE125771.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Stroke/gene_data/GSE125771.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Stroke/clinical_data/GSE125771.csv\"\n", "json_path = \"../../output/preprocess/Stroke/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "ec40cd90", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "463e70ce", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:04:12.335732Z", "iopub.status.busy": "2025-03-25T04:04:12.335587Z", "iopub.status.idle": "2025-03-25T04:04:12.451036Z", "shell.execute_reply": "2025-03-25T04:04:12.450704Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"RNA expression data from calcified human carotid atherosclerotic plaques\"\n", "!Series_summary\t\"Although unstable atherosclerosis in the carotid bifurcation is a significant etiology behind ischemic stroke, clinical imaging methods to distinguish stable from vulnerable lesions are lacking and selection of patients for stroke-preventive intervention still relies on surrogate variables with moderate predictive power, such as the degree of luminal narrowing. Here we combined clinical and diagnostic imaging information by comuted tomography to select patients with calcified plaques for large scale molecular analysis, in an effort to increase our understanding of the pathophysiology behind carotid plaque instability as related to patient- and plaque- phenotype.\"\n", "!Series_overall_design\t\"Patients undergoing surgery for high-grade (>50% NASCET) carotid stenosis at the Department of Vascular Surgery, Karolinska University Hospital, Stockholm, Sweden were consecutively enrolled in the study and clinical data recorded on admission. Carotid computed tomography angiography (CTA) was performed as a pre-operative routine at the admitting hospital using site-specific image acquisition protocols. Carotid endarterectomies (carotid plaques) were collected at surgery and retained within the Biobank of Karolinska Endarterectomies (BiKE). Tissues were frozen at -80°C immediately after surgery and RNA was prepared using Qiazol Lysis Reagent (Qiagen, Hilden, Germany) and purified by RNeasy Mini kit (Qiagen), including DNase digestion. The RNA concentration was measured using Nanodrop ND-1000 (Thermo Scientific, Waltham, MA) and quality estimated by a Bioanalyzer capillary electrophoresis system (Agilent Technologies, Santa Clara, CA). For microarrays, only RNA of good integrity with RIN>7, A260/A280 ratio between 1.8-2.1, A260/230 0.7-1.5 and concentration about 50-500 ng/μl was used, as per standards recommended for whole transcript arrays. Robust multi-array average normalization was performed and processed gene expression data was returned in log2-scale. All human samples were collected with informed consent from patients or organ donors’ guardians; studies were approved by the regional Ethical Committee and follow the guidelines of the Declaration of Helsinki.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['tissue: carotid-atherosclerotic-plaque'], 1: ['ID: sample1', 'ID: sample2', 'ID: sample3', 'ID: sample4', 'ID: sample5', 'ID: sample6', 'ID: sample7', 'ID: sample8', 'ID: sample9', 'ID: sample10', 'ID: sample11', 'ID: sample12', 'ID: sample13', 'ID: sample14', 'ID: sample15', 'ID: sample16', 'ID: sample17', 'ID: sample18', 'ID: sample19', 'ID: sample20', 'ID: sample21', 'ID: sample22', 'ID: sample23', 'ID: sample24', 'ID: sample25', 'ID: sample26', 'ID: sample27', 'ID: sample28', 'ID: sample29', 'ID: sample30'], 2: ['Sex: Male', 'Sex: Female'], 3: ['age: 73', 'age: 60', 'age: 81', 'age: 85', 'age: 84', 'age: 76', 'age: 57', 'age: 71', 'age: 69', 'age: 79', 'age: 78', 'age: 54', 'age: 72', 'age: 64', 'age: 67', 'age: 63', 'age: 75', 'age: 62', 'age: 74', 'age: 65', 'age: 83', 'age: 61']}\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": "7cf611ca", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "b42ae0cb", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:04:12.452238Z", "iopub.status.busy": "2025-03-25T04:04:12.452134Z", "iopub.status.idle": "2025-03-25T04:04:12.459025Z", "shell.execute_reply": "2025-03-25T04:04:12.458711Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A new JSON file was created at: ../../output/preprocess/Stroke/cohort_info.json\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the background information, this dataset contains RNA expression data from calcified human carotid atherosclerotic plaques\n", "# It is likely to contain gene expression data as it mentions \"RNA expression data\" and \"microarrays\"\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "\n", "# For trait (Stroke):\n", "# This is a study on atherosclerotic plaques in carotid arteries, which are associated with stroke\n", "# All samples are from carotid plaques, and the study mentions relation to ischemic stroke\n", "# However, there's no explicit indication in the sample characteristics about which patients had stroke\n", "# The study only mentions patients with high-grade carotid stenosis, not specifically stroke events\n", "trait_row = None # No explicit stroke data available in the sample characteristics\n", "\n", "# For age:\n", "# Age data is available at row 3\n", "age_row = 3\n", "\n", "# For gender:\n", "# Gender data is available at row 2\n", "gender_row = 2\n", "\n", "# 2.2 Data Type Conversion\n", "\n", "# Convert trait function (not used since trait_row is None)\n", "def convert_trait(value):\n", " if value is None:\n", " return None\n", " value = value.split(': ')[-1].lower().strip()\n", " if value in ['yes', 'stroke', 'positive', '1']:\n", " return 1\n", " elif value in ['no', 'control', 'negative', '0']:\n", " return 0\n", " return None\n", "\n", "# Convert age function\n", "def convert_age(value):\n", " if value is None:\n", " return None\n", " try:\n", " # Extract the numeric value after the colon\n", " age_value = value.split(': ')[-1].strip()\n", " return float(age_value) # Convert to float for continuous variable\n", " except (ValueError, AttributeError):\n", " return None\n", "\n", "# Convert gender function\n", "def convert_gender(value):\n", " if value is None:\n", " return None\n", " value = value.split(': ')[-1].lower().strip()\n", " if value in ['male', 'm']:\n", " return 1\n", " elif value in ['female', 'f']:\n", " return 0\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Trait data is not available\n", "is_trait_available = trait_row is not None\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 (trait data is not available), we should skip this substep entirely\n", "# per the instructions: \"If trait_row is not None, it means clinical data is available, then you MUST DO this substep. \n", "# Otherwise, you should skip this substep.\"\n" ] }, { "cell_type": "markdown", "id": "25edf557", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "b3c5d9d7", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:04:12.460115Z", "iopub.status.busy": "2025-03-25T04:04:12.460013Z", "iopub.status.idle": "2025-03-25T04:04:12.611022Z", "shell.execute_reply": "2025-03-25T04:04:12.610607Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix file found: ../../input/GEO/Stroke/GSE125771/GSE125771_series_matrix.txt.gz\n", "Gene data shape: (65535, 40)\n", "First 20 gene/probe identifiers:\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" ] } ], "source": [ "# 1. Get the SOFT and matrix file paths again \n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "print(f\"Matrix file found: {matrix_file}\")\n", "\n", "# 2. Use the get_genetic_data function from the library to get the gene_data\n", "try:\n", " gene_data = get_genetic_data(matrix_file)\n", " print(f\"Gene data shape: {gene_data.shape}\")\n", " \n", " # 3. Print the first 20 row IDs (gene or probe identifiers)\n", " print(\"First 20 gene/probe identifiers:\")\n", " print(gene_data.index[:20])\n", "except Exception as e:\n", " print(f\"Error extracting gene data: {e}\")\n" ] }, { "cell_type": "markdown", "id": "059d2471", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "4b54c955", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:04:12.612464Z", "iopub.status.busy": "2025-03-25T04:04:12.612358Z", "iopub.status.idle": "2025-03-25T04:04:12.614309Z", "shell.execute_reply": "2025-03-25T04:04:12.614014Z" } }, "outputs": [], "source": [ "# These identifiers appear to be microarray probe IDs, not standard human gene symbols\n", "# They follow the format \"TC01000001.hg.1\" which is consistent with the Affymetrix Transcriptome Array probe IDs\n", "# These need to be mapped to human gene symbols for proper analysis\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "390e4e9a", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "2dae5c02", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:04:12.615428Z", "iopub.status.busy": "2025-03-25T04:04:12.615332Z", "iopub.status.idle": "2025-03-25T04:04:19.412289Z", "shell.execute_reply": "2025-03-25T04:04:19.411798Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene annotation preview:\n", "Columns in gene annotation: ['ID', 'probeset_id', 'seqname', 'strand', 'start', 'stop', 'total_probes', 'gene_assignment', 'mrna_assignment', 'swissprot', 'unigene', 'category', 'locus type', 'notes', 'SPOT_ID']\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", "Searching for platform information in SOFT file:\n", "!Series_platform_id = GPL17586\n", "\n", "Searching for gene symbol information in SOFT file:\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "No explicit gene symbol references found in first 1000 lines\n", "\n", "Checking for additional annotation files in the directory:\n", "[]\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. 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", "# Let's look for platform information in the SOFT file to understand the annotation better\n", "print(\"\\nSearching for platform information in SOFT file:\")\n", "with gzip.open(soft_file, 'rt') as f:\n", " for i, line in enumerate(f):\n", " if '!Series_platform_id' in line:\n", " print(line.strip())\n", " break\n", " if i > 100: # Limit search to first 100 lines\n", " print(\"Platform ID not found in first 100 lines\")\n", " break\n", "\n", "# Check if the SOFT file includes any reference to gene symbols\n", "print(\"\\nSearching for gene symbol information in SOFT file:\")\n", "with gzip.open(soft_file, 'rt') as f:\n", " gene_symbol_lines = []\n", " for i, line in enumerate(f):\n", " if 'GENE_SYMBOL' in line or 'gene_symbol' in line.lower() or 'symbol' in line.lower():\n", " gene_symbol_lines.append(line.strip())\n", " if i > 1000 and len(gene_symbol_lines) > 0: # Limit search but ensure we found something\n", " break\n", " \n", " if gene_symbol_lines:\n", " print(\"Found references to gene symbols:\")\n", " for line in gene_symbol_lines[:5]: # Show just first 5 matches\n", " print(line)\n", " else:\n", " print(\"No explicit gene symbol references found in first 1000 lines\")\n", "\n", "# Look for alternative annotation files or references in the directory\n", "print(\"\\nChecking for additional annotation files in the directory:\")\n", "all_files = os.listdir(in_cohort_dir)\n", "print([f for f in all_files if 'annotation' in f.lower() or 'platform' in f.lower() or 'gpl' in f.lower()])\n" ] }, { "cell_type": "markdown", "id": "2fd0a44f", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "e65396ee", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:04:19.413871Z", "iopub.status.busy": "2025-03-25T04:04:19.413743Z", "iopub.status.idle": "2025-03-25T04:04:20.964918Z", "shell.execute_reply": "2025-03-25T04:04:20.964335Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene mapping shape: (70753, 2)\n", "Sample of gene mapping data:\n", " ID Gene\n", "0 TC01000001.hg.1 NR_046018 // DDX11L1 // DEAD/H (Asp-Glu-Ala-As...\n", "1 TC01000002.hg.1 ENST00000408384 // MIR1302-11 // microRNA 1302...\n", "2 TC01000003.hg.1 NM_001005484 // OR4F5 // olfactory receptor, f...\n", "3 TC01000004.hg.1 OTTHUMT00000007169 // OTTHUMG00000002525 // NU...\n", "4 TC01000005.hg.1 NR_028322 // LOC100132287 // uncharacterized L...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "After mapping, gene data shape: (24012, 40)\n", "First 10 gene symbols after mapping:\n", "Index(['A1BG', 'A1BG-AS1', 'A1CF', 'A2M', 'A2M-AS1', 'A2ML1', 'A2ML1-AS1',\n", " 'A2ML1-AS2', 'A2MP1', 'A4GALT'],\n", " dtype='object', name='Gene')\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene data saved to: ../../output/preprocess/Stroke/gene_data/GSE125771.csv\n" ] } ], "source": [ "# 1. Observe gene identifiers and determine mapping columns\n", "# Based on the gene identifiers, we see that 'ID' column in the gene_annotation matches the gene expression data index\n", "# The 'gene_assignment' column contains the gene symbols information\n", "\n", "# 2. Get gene mapping dataframe by extracting relevant columns\n", "# The probe identifiers are in the 'ID' column\n", "# The gene symbols are in the 'gene_assignment' column\n", "mapping_df = get_gene_mapping(gene_annotation, 'ID', 'gene_assignment')\n", "\n", "print(f\"Gene mapping shape: {mapping_df.shape}\")\n", "print(\"Sample of gene mapping data:\")\n", "print(mapping_df.head())\n", "\n", "# 3. Convert probe-level measurements to gene expression data\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "\n", "# Normalize gene symbols\n", "gene_data = normalize_gene_symbols_in_index(gene_data) \n", "\n", "print(f\"After mapping, gene data shape: {gene_data.shape}\")\n", "print(\"First 10 gene symbols after mapping:\")\n", "print(gene_data.index[:10])\n", "\n", "# Save the processed gene 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\"Gene data saved to: {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "6e159e06", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "ad60a9f7", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:04:20.967305Z", "iopub.status.busy": "2025-03-25T04:04:20.967184Z", "iopub.status.idle": "2025-03-25T04:04:21.518296Z", "shell.execute_reply": "2025-03-25T04:04:21.517742Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape already normalized: (24012, 40)\n", "\n", "Exploring clinical data structure:\n", "Row 0: ['!Sample_characteristics_ch1', 'tissue: carotid-atherosclerotic-plaque']\n", "Row 1: ['!Sample_characteristics_ch1', 'ID: sample1', 'ID: sample2', 'ID: sample3', 'ID: sample4']\n", "Row 2: ['!Sample_characteristics_ch1', 'Sex: Male', 'Sex: Female']\n", "Row 3: ['!Sample_characteristics_ch1', 'age: 73', 'age: 60', 'age: 81', 'age: 85']\n", "\n", "Sample characteristics dictionary:\n", "{0: ['tissue: carotid-atherosclerotic-plaque'], 1: ['ID: sample1', 'ID: sample2', 'ID: sample3', 'ID: sample4', 'ID: sample5', 'ID: sample6', 'ID: sample7', 'ID: sample8', 'ID: sample9', 'ID: sample10', 'ID: sample11', 'ID: sample12', 'ID: sample13', 'ID: sample14', 'ID: sample15', 'ID: sample16', 'ID: sample17', 'ID: sample18', 'ID: sample19', 'ID: sample20', 'ID: sample21', 'ID: sample22', 'ID: sample23', 'ID: sample24', 'ID: sample25', 'ID: sample26', 'ID: sample27', 'ID: sample28', 'ID: sample29', 'ID: sample30'], 2: ['Sex: Male', 'Sex: Female'], 3: ['age: 73', 'age: 60', 'age: 81', 'age: 85', 'age: 84', 'age: 76', 'age: 57', 'age: 71', 'age: 69', 'age: 79', 'age: 78', 'age: 54', 'age: 72', 'age: 64', 'age: 67', 'age: 63', 'age: 75', 'age: 62', 'age: 74', 'age: 65', 'age: 83', 'age: 61']}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Stroke/gene_data/GSE125771.csv\n", "Dataset processing complete. The dataset has gene expression data but lacks stroke trait information.\n" ] } ], "source": [ "# 1. Normalize gene symbols - this was already done in Step 6\n", "print(f\"Gene data shape already normalized: {gene_data.shape}\")\n", "\n", "# 2. Explore the clinical data structure to identify relevant rows\n", "print(\"\\nExploring clinical data structure:\")\n", "for i in range(min(10, len(clinical_data))): # Check up to first 10 rows\n", " print(f\"Row {i}:\", list(clinical_data.iloc[i].unique())[:5]) # Show first 5 unique values\n", "\n", "# Extract the sample characteristics dictionary again to analyze content\n", "sample_characteristics_dict = get_unique_values_by_row(clinical_data)\n", "print(\"\\nSample characteristics dictionary:\")\n", "print(sample_characteristics_dict)\n", "\n", "# Based on the exploration, we can see that clinical data doesn't have stroke information\n", "# All samples are from carotid atherosclerotic plaques but without explicit stroke status\n", "# We'll have to mark this dataset as not having the trait data available\n", "\n", "# Save the gene expression data (already normalized in Step 6)\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\"Gene expression data saved to {out_gene_data_file}\")\n", "\n", "# Mark dataset as not having trait data available\n", "is_trait_available = False\n", "is_gene_available = True\n", "\n", "# Initial validation with is_final=False since we don't have trait data\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", "print(\"Dataset processing complete. The dataset has gene expression data but lacks stroke trait information.\")" ] } ], "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 }