{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "5961f1eb", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:56:43.339942Z", "iopub.status.busy": "2025-03-25T03:56:43.339725Z", "iopub.status.idle": "2025-03-25T03:56:43.506236Z", "shell.execute_reply": "2025-03-25T03:56:43.505882Z" } }, "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 = \"Schizophrenia\"\n", "cohort = \"GSE285666\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Schizophrenia\"\n", "in_cohort_dir = \"../../input/GEO/Schizophrenia/GSE285666\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Schizophrenia/GSE285666.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Schizophrenia/gene_data/GSE285666.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Schizophrenia/clinical_data/GSE285666.csv\"\n", "json_path = \"../../output/preprocess/Schizophrenia/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "b1762c35", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "d793a70e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:56:43.507479Z", "iopub.status.busy": "2025-03-25T03:56:43.507339Z", "iopub.status.idle": "2025-03-25T03:56:43.600926Z", "shell.execute_reply": "2025-03-25T03:56:43.600633Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Exon- and gene-Level transcriptional profiling in Lymphoblastoid Cell Lines (LCLs) from Williams syndrome patients and controls\"\n", "!Series_summary\t\"Williams syndrome (WS), characterized by positive sociality, provides a unique model for studying transcriptional networks underlying social dysfunction, relevant to disorders like autism spectrum disorder (ASD) and schizophrenia (SCHZ). In a cohort lymphoblastoid cell lines derived from 52 individuals (34 WS patients, 18 parental controls), genome-wide exon-level arrays identified a core set of differentially expressed genes (DEGs), with WS-deleted genes ranking among the top transcripts. Findings were validated by PCR, RNA-seq, and western blots.\"\n", "!Series_summary\t\"Network analyses revealed perturbed actin cytoskeletal signaling in excitatory dendritic spines, alongside interactions in MAPK, IGF1-PI3K-AKT-mTOR/insulin, and synaptic actin pathways. These transcriptional networks show parallels to ASD and SCHZ, highlighting shared mechanisms across social behavior disorders.\"\n", "!Series_overall_design\t\"Human lymphoblastoid cells immortailzed from WIlliams syndrome patients and non-affected parental controls were grown in RMPI 1640 with 10% FBS, 5% pen/strep, 5% L-glutamine and 0.5% gentamycin. Total RNA was extracted from each culture using the Qiagen RNeasy kit with DNase digestion. Prior to labeling, ribosomal RNA was removed from total RNA (1 μg per sample) using the RiboMinus Human/Mouse Transcriptome Isolation Kit (Invitrogen). Expression analysis was conducted using Affymetrix Human Exon 1.0 ST arrays following the Affymetrix hybridization protocols. Exon expression data were analyzed through Affymetrix Expression Console using exon- and gene-level PLIER (Affymetrix Power Tool with PM-GCBG background correction) summarization and sketch-quantile normalization methods.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['disease state: unaffected parental control', 'disease state: Williams syndrome patient']}\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": "3b055131", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "121110f8", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:56:43.602337Z", "iopub.status.busy": "2025-03-25T03:56:43.602232Z", "iopub.status.idle": "2025-03-25T03:56:43.607942Z", "shell.execute_reply": "2025-03-25T03:56:43.607675Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "import os\n", "import json\n", "from typing import Optional, Callable, Dict, Any\n", "\n", "# 1. Gene Expression Data Availability\n", "# Based on the background information, this dataset contains gene expression data from Affymetrix Human Exon arrays\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# Looking at the sample characteristics dictionary\n", "# The trait in this case is Schizophrenia, but the data appears to be about Williams syndrome vs controls\n", "# This dataset is comparing Williams syndrome patients to controls, not specifically looking at Schizophrenia\n", "trait_row = 0 # The disease state is in row 0, but it's for Williams syndrome, not Schizophrenia\n", "age_row = None # No age information provided\n", "gender_row = None # No gender information provided\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value):\n", " \"\"\"Convert trait information to binary (0 for control, 1 for case)\"\"\"\n", " if isinstance(value, str) and ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " if value.lower() in ['unaffected parental control', 'control']:\n", " return 0\n", " elif 'williams syndrome patient' in value.lower():\n", " return 1\n", " else:\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age information to continuous\"\"\"\n", " # Not used since age_row is None\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender information to binary (0 for female, 1 for male)\"\"\"\n", " # Not used since gender_row is None\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Trait data availability is determined by whether trait_row is not None\n", "# However, in this case trait_row is for Williams syndrome, not Schizophrenia\n", "# So we should set is_trait_available to False for our Schizophrenia study\n", "is_trait_available = False # The dataset doesn't contain Schizophrenia trait data\n", "\n", "# Initial filtering on usability and save metadata\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 the trait data for Schizophrenia is not available, we skip this step\n" ] }, { "cell_type": "markdown", "id": "c5d134c3", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "40383bb0", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:56:43.609089Z", "iopub.status.busy": "2025-03-25T03:56:43.608977Z", "iopub.status.idle": "2025-03-25T03:56:43.746098Z", "shell.execute_reply": "2025-03-25T03:56:43.745736Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix file found: ../../input/GEO/Schizophrenia/GSE285666/GSE285666_series_matrix.txt.gz\n", "Gene data shape: (22011, 52)\n", "First 20 gene/probe identifiers:\n", "Index(['2315554', '2315633', '2315674', '2315739', '2315894', '2315918',\n", " '2315951', '2316218', '2316245', '2316379', '2316558', '2316605',\n", " '2316746', '2316905', '2316953', '2317246', '2317317', '2317434',\n", " '2317472', '2317512'],\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": "c0997026", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "d0860da6", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:56:43.747775Z", "iopub.status.busy": "2025-03-25T03:56:43.747654Z", "iopub.status.idle": "2025-03-25T03:56:43.749595Z", "shell.execute_reply": "2025-03-25T03:56:43.749288Z" } }, "outputs": [], "source": [ "# These identifiers appear to be probe IDs/numeric identifiers rather than standard human gene symbols\n", "# Human gene symbols typically follow patterns like BRCA1, TP53, etc.\n", "# These numeric identifiers would need to be mapped to actual gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "e7189a07", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "6e70fe9c", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:56:43.750803Z", "iopub.status.busy": "2025-03-25T03:56:43.750692Z", "iopub.status.idle": "2025-03-25T03:56:46.886696Z", "shell.execute_reply": "2025-03-25T03:56:46.886135Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Gene annotation preview:\n", "Columns in gene annotation: ['ID', 'GB_LIST', 'SPOT_ID', 'seqname', 'RANGE_GB', 'RANGE_STRAND', 'RANGE_START', 'RANGE_STOP', 'total_probes', 'gene_assignment', 'mrna_assignment', 'category']\n", "{'ID': ['2315100', '2315106', '2315109', '2315111', '2315113'], 'GB_LIST': ['NR_024005,NR_034090,NR_024004,AK093685', 'DQ786314', nan, nan, 'DQ786265'], 'SPOT_ID': ['chr1:11884-14409', 'chr1:14760-15198', 'chr1:19408-19712', 'chr1:25142-25532', 'chr1:27563-27813'], 'seqname': ['chr1', 'chr1', 'chr1', 'chr1', 'chr1'], 'RANGE_GB': ['NC_000001.10', 'NC_000001.10', 'NC_000001.10', 'NC_000001.10', 'NC_000001.10'], 'RANGE_STRAND': ['+', '+', '+', '+', '+'], 'RANGE_START': ['11884', '14760', '19408', '25142', '27563'], 'RANGE_STOP': ['14409', '15198', '19712', '25532', '27813'], 'total_probes': ['20', '8', '4', '4', '4'], 'gene_assignment': ['NR_024005 // DDX11L2 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 // 2q13 // 84771 /// NR_034090 // DDX11L9 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 9 // 15q26.3 // 100288486 /// NR_024004 // DDX11L2 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 // 2q13 // 84771 /// AK093685 // DDX11L2 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 // 2q13 // 84771', '---', '---', '---', '---'], 'mrna_assignment': ['NR_024005 // RefSeq // Homo sapiens DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 (DDX11L2), transcript variant 2, non-coding RNA. // chr1 // 100 // 80 // 16 // 16 // 0 /// NR_034090 // RefSeq // Homo sapiens DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 9 (DDX11L9), non-coding RNA. // chr1 // 100 // 80 // 16 // 16 // 0 /// NR_024004 // RefSeq // Homo sapiens DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 (DDX11L2), transcript variant 1, non-coding RNA. // chr1 // 100 // 75 // 15 // 15 // 0 /// AK093685 // GenBank // Homo sapiens cDNA FLJ36366 fis, clone THYMU2007824. // chr1 // 94 // 80 // 15 // 16 // 0 /// ENST00000513886 // ENSEMBL // cdna:known chromosome:GRCh37:16:61555:64090:1 gene:ENSG00000233614 // chr1 // 100 // 80 // 16 // 16 // 0 /// ENST00000456328 // ENSEMBL // cdna:known chromosome:GRCh37:1:11869:14409:1 gene:ENSG00000223972 // chr1 // 100 // 80 // 16 // 16 // 0 /// ENST00000518655 // ENSEMBL // cdna:known chromosome:GRCh37:1:11869:14409:1 gene:ENSG00000253101 // chr1 // 100 // 80 // 16 // 16 // 0', 'DQ786314 // GenBank // Homo sapiens clone HLS_IMAGE_811138 mRNA sequence. // chr1 // 100 // 38 // 3 // 3 // 0', '---', '---', 'DQ786265 // GenBank // Homo sapiens clone HLS_IMAGE_298685 mRNA sequence. // chr1 // 100 // 100 // 4 // 4 // 0'], 'category': ['main', 'main', '---', '---', 'main']}\n", "\n", "First row as dictionary:\n", "ID: 2315100\n", "GB_LIST: NR_024005,NR_034090,NR_024004,AK093685\n", "SPOT_ID: chr1:11884-14409\n", "seqname: chr1\n", "RANGE_GB: NC_000001.10\n", "RANGE_STRAND: +\n", "RANGE_START: 11884\n", "RANGE_STOP: 14409\n", "total_probes: 20\n", "gene_assignment: NR_024005 // DDX11L2 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 // 2q13 // 84771 /// NR_034090 // DDX11L9 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 9 // 15q26.3 // 100288486 /// NR_024004 // DDX11L2 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 // 2q13 // 84771 /// AK093685 // DDX11L2 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 // 2q13 // 84771\n", "mrna_assignment: NR_024005 // RefSeq // Homo sapiens DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 (DDX11L2), transcript variant 2, non-coding RNA. // chr1 // 100 // 80 // 16 // 16 // 0 /// NR_034090 // RefSeq // Homo sapiens DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 9 (DDX11L9), non-coding RNA. // chr1 // 100 // 80 // 16 // 16 // 0 /// NR_024004 // RefSeq // Homo sapiens DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 (DDX11L2), transcript variant 1, non-coding RNA. // chr1 // 100 // 75 // 15 // 15 // 0 /// AK093685 // GenBank // Homo sapiens cDNA FLJ36366 fis, clone THYMU2007824. // chr1 // 94 // 80 // 15 // 16 // 0 /// ENST00000513886 // ENSEMBL // cdna:known chromosome:GRCh37:16:61555:64090:1 gene:ENSG00000233614 // chr1 // 100 // 80 // 16 // 16 // 0 /// ENST00000456328 // ENSEMBL // cdna:known chromosome:GRCh37:1:11869:14409:1 gene:ENSG00000223972 // chr1 // 100 // 80 // 16 // 16 // 0 /// ENST00000518655 // ENSEMBL // cdna:known chromosome:GRCh37:1:11869:14409:1 gene:ENSG00000253101 // chr1 // 100 // 80 // 16 // 16 // 0\n", "category: main\n", "\n", "Comparing gene data IDs with annotation IDs:\n", "First 5 gene data IDs: ['2315554', '2315633', '2315674', '2315739', '2315894']\n", "First 5 annotation IDs: ['2315100', '2315106', '2315109', '2315111', '2315113']\n", "\n", "Exact ID match between gene data and annotation:\n", "Matching IDs: 22011 out of 22011 (100.00%)\n", "\n", "Potential columns for gene symbols: ['seqname', 'gene_assignment']\n", "Column 'seqname': 316919 non-null values (21.68%)\n", "Column 'gene_assignment': 316481 non-null values (21.65%)\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", "# Check if there are any columns that might contain gene information\n", "sample_row = gene_annotation.iloc[0].to_dict()\n", "print(\"\\nFirst row as dictionary:\")\n", "for col, value in sample_row.items():\n", " print(f\"{col}: {value}\")\n", "\n", "# Check if IDs in gene_data match IDs in annotation\n", "print(\"\\nComparing gene data IDs with annotation IDs:\")\n", "print(\"First 5 gene data IDs:\", gene_data.index[:5].tolist())\n", "print(\"First 5 annotation IDs:\", gene_annotation['ID'].head().tolist())\n", "\n", "# Properly check for exact ID matches between gene data and annotation\n", "gene_data_ids = set(gene_data.index)\n", "annotation_ids = set(gene_annotation['ID'].astype(str))\n", "matching_ids = gene_data_ids.intersection(annotation_ids)\n", "id_match_percentage = len(matching_ids) / len(gene_data_ids) * 100 if len(gene_data_ids) > 0 else 0\n", "\n", "print(f\"\\nExact ID match between gene data and annotation:\")\n", "print(f\"Matching IDs: {len(matching_ids)} out of {len(gene_data_ids)} ({id_match_percentage:.2f}%)\")\n", "\n", "# Check which columns might contain gene symbols for mapping\n", "potential_gene_symbol_cols = [col for col in gene_annotation.columns \n", " if any(term in col.upper() for term in ['GENE', 'SYMBOL', 'NAME'])]\n", "print(f\"\\nPotential columns for gene symbols: {potential_gene_symbol_cols}\")\n", "\n", "# Check if the identified columns contain non-null values\n", "for col in potential_gene_symbol_cols:\n", " non_null_count = gene_annotation[col].notnull().sum()\n", " non_null_percent = non_null_count / len(gene_annotation) * 100\n", " print(f\"Column '{col}': {non_null_count} non-null values ({non_null_percent:.2f}%)\")\n" ] }, { "cell_type": "markdown", "id": "75f50dc7", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "2c8fd301", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:56:46.888115Z", "iopub.status.busy": "2025-03-25T03:56:46.887985Z", "iopub.status.idle": "2025-03-25T03:56:49.183377Z", "shell.execute_reply": "2025-03-25T03:56:49.182850Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Extracting gene mapping from gene annotation data...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene mapping dataframe shape: (316481, 2)\n", "First few rows of gene mapping:\n", " ID Gene\n", "0 2315100 NR_024005 // DDX11L2 // DEAD/H (Asp-Glu-Ala-As...\n", "1 2315106 ---\n", "2 2315109 ---\n", "3 2315111 ---\n", "4 2315113 ---\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data after mapping, shape: (48895, 52)\n", "First few gene symbols after mapping:\n", "['A-', 'A-2', 'A-52', 'A-E', 'A-I', 'A-II', 'A-IV', 'A-V', 'A0', 'A1']\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Schizophrenia/gene_data/GSE285666.csv\n" ] } ], "source": [ "# 1. Identify columns for mapping\n", "# ID column contains probe identifiers matching the gene expression data\n", "# gene_assignment column contains gene symbols information\n", "print(\"Extracting gene mapping from gene annotation data...\")\n", "\n", "# 2. Create gene mapping dataframe\n", "# Using get_gene_mapping function from the library to extract the mapping columns\n", "gene_mapping = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='gene_assignment')\n", "print(f\"Gene mapping dataframe shape: {gene_mapping.shape}\")\n", "print(\"First few rows of gene mapping:\")\n", "print(gene_mapping.head())\n", "\n", "# 3. Apply gene mapping to convert probe-level measurements to gene expression data\n", "# Use the apply_gene_mapping function from the library\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "print(f\"Gene expression data after mapping, shape: {gene_data.shape}\")\n", "print(\"First few gene symbols after mapping:\")\n", "print(gene_data.index[:10].tolist())\n", "\n", "# Save the processed gene data to file\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" ] }, { "cell_type": "markdown", "id": "013144a2", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "3868fc5a", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T03:56:49.184835Z", "iopub.status.busy": "2025-03-25T03:56:49.184699Z", "iopub.status.idle": "2025-03-25T03:56:55.543304Z", "shell.execute_reply": "2025-03-25T03:56:55.542777Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene data shape after normalization: (18418, 52)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene expression data saved to ../../output/preprocess/Schizophrenia/gene_data/GSE285666.csv\n", "Selected clinical data shape: (1, 52)\n", "Clinical data preview:\n", " GSM8706502 GSM8706503 GSM8706504 GSM8706505 GSM8706506 \\\n", "Schizophrenia 0.0 0.0 0.0 0.0 0.0 \n", "\n", " GSM8706507 GSM8706508 GSM8706509 GSM8706510 GSM8706511 \\\n", "Schizophrenia 0.0 0.0 0.0 0.0 0.0 \n", "\n", " ... GSM8706544 GSM8706545 GSM8706546 GSM8706547 \\\n", "Schizophrenia ... 1.0 1.0 1.0 1.0 \n", "\n", " GSM8706548 GSM8706549 GSM8706550 GSM8706551 GSM8706552 \\\n", "Schizophrenia 1.0 1.0 1.0 1.0 1.0 \n", "\n", " GSM8706553 \n", "Schizophrenia 1.0 \n", "\n", "[1 rows x 52 columns]\n", "Linked data shape: (52, 18419)\n", "Linked data preview (first 5 rows, 5 columns):\n", " Schizophrenia A1BG A1BG-AS1 A1CF A2M\n", "GSM8706502 0.0 38.534348 38.534348 53.078847 106.475358\n", "GSM8706503 0.0 50.069114 50.069114 44.858291 110.093250\n", "GSM8706504 0.0 47.107387 47.107387 53.772984 99.340176\n", "GSM8706505 0.0 54.198439 54.198439 49.542268 125.083757\n", "GSM8706506 0.0 35.837959 35.837959 63.008107 96.761368\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (52, 18419)\n", "For the feature 'Schizophrenia', the least common label is '0.0' with 18 occurrences. This represents 34.62% of the dataset.\n", "The distribution of the feature 'Schizophrenia' in this dataset is fine.\n", "\n", "Dataset is not usable for analysis. No linked data file saved.\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Gene data shape after normalization: {gene_data.shape}\")\n", "\n", "# Save the normalized gene data to file\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\"Normalized gene expression data saved to {out_gene_data_file}\")\n", "\n", "# 2. Link the clinical and genetic data\n", "# Using the selected_clinical_df directly for proper trait information\n", "selected_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", "print(f\"Selected clinical data shape: {selected_clinical_df.shape}\")\n", "print(\"Clinical data preview:\")\n", "print(selected_clinical_df)\n", "\n", "# Link clinical and genetic data directly using the selected clinical dataframe\n", "linked_data = geo_link_clinical_genetic_data(selected_clinical_df, gene_data)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "print(\"Linked data preview (first 5 rows, 5 columns):\")\n", "print(linked_data.iloc[:5, :5] if not linked_data.empty else \"Linked data is empty\")\n", "\n", "# 3. Handle missing values\n", "linked_data = handle_missing_values(linked_data, trait)\n", "print(f\"Data shape after handling missing values: {linked_data.shape}\")\n", "\n", "# 4. Check for bias in features\n", "try:\n", " is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "except Exception as e:\n", " print(f\"Error checking for bias: {e}\")\n", " is_biased = True # Assume biased if there's an error\n", "\n", "# 5. Validate and save cohort information - setting is_trait_available to False as this dataset \n", "# contains Williams syndrome data, not Schizophrenia data\n", "is_usable = validate_and_save_cohort_info(\n", " is_final=True,\n", " cohort=cohort,\n", " info_path=json_path,\n", " is_gene_available=True,\n", " is_trait_available=False, # Changed to False as dataset doesn't contain Schizophrenia\n", " is_biased=is_biased,\n", " df=linked_data,\n", " note=\"Dataset contains Williams syndrome patients vs controls, not Schizophrenia data.\"\n", ")\n", "\n", "# 6. Save the linked data if usable\n", "if is_usable and not linked_data.empty:\n", " os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n", " linked_data.to_csv(out_data_file)\n", " print(f\"Linked data saved to {out_data_file}\")\n", "else:\n", " print(\"Dataset is not usable for analysis. No linked data file 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 }