{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "ea4daee4", "metadata": {}, "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 = \"Multiple_sclerosis\"\n", "cohort = \"GSE131279\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Multiple_sclerosis\"\n", "in_cohort_dir = \"../../input/GEO/Multiple_sclerosis/GSE131279\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Multiple_sclerosis/GSE131279.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Multiple_sclerosis/gene_data/GSE131279.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Multiple_sclerosis/clinical_data/GSE131279.csv\"\n", "json_path = \"../../output/preprocess/Multiple_sclerosis/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "61816c89", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": null, "id": "b7d01515", "metadata": {}, "outputs": [], "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": "a448df6e", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": null, "id": "9da21b76", "metadata": {}, "outputs": [], "source": [ "Looking at the task, I need to analyze the dataset and extract clinical features.\n", "\n", "```python\n", "# Analyze dataset and extract clinical features\n", "\n", "# Step 1: Gene Expression Data Availability\n", "# Based on the background information, this dataset contains gene expression data (not just miRNA or methylation)\n", "is_gene_available = True\n", "\n", "# Step 2: Variable Availability and Data Type Conversion\n", "\n", "# 2.1 Data Availability\n", "# Trait availability - MS type is in row 4\n", "trait_row = 4\n", "\n", "# Age availability - Age at death is in row 2\n", "age_row = 2\n", "\n", "# Gender availability - Sex is in row 1\n", "gender_row = 1\n", "\n", "# 2.2 Data Type Conversion Functions\n", "\n", "def convert_trait(value):\n", " \"\"\"Convert MS type to binary: 1 for all MS types (PPMS, SPMS, PRMS)\"\"\"\n", " if not isinstance(value, str):\n", " return None\n", " \n", " # Extract the value after the colon\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " # All are MS patients in this dataset, return 1\n", " if value in [\"PPMS\", \"SPMS\", \"PRMS\"]:\n", " return 1\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age at death to continuous value\"\"\"\n", " if not isinstance(value, str):\n", " return None\n", " \n", " # Extract the value after the colon\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " try:\n", " return float(value)\n", " except (ValueError, TypeError):\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender to binary: 0 for female, 1 for male\"\"\"\n", " if not isinstance(value, str):\n", " return None\n", " \n", " # Extract the value after the colon\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " if value.upper() in [\"F\", \"FEMALE\"]:\n", " return 0\n", " elif value.upper() in [\"M\", \"MALE\"]:\n", " return 1\n", " return None\n", "\n", "# Step 3: Save Metadata\n", "# Determine if trait data is available\n", "is_trait_available = trait_row is not None\n", "\n", "# Save initial 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", "# Step 4: Clinical Feature Extraction\n", "# Only execute if trait data is available\n", "if trait_row is not None:\n", " # Create a DataFrame from the sample characteristics dictionary provided in previous output\n", " sample_characteristics_dict = {\n", " 0: ['patient id: M02', 'patient id: M60', 'patient id: M44', 'patient id: M13', 'patient id: M53', 'patient id: M14', 'patient id: M56', 'patient id: M46', 'patient id: M61', 'patient id: M42', 'patient id: M28', 'patient id: M23', 'patient id: M52', 'patient id: M12', 'patient id: M32', 'patient id: M06', 'patient id: M01', 'patient id: M36', 'patient id: M59', 'patient id: M34', 'patient id: M26', 'patient id: M03', 'patient id: M54', 'patient id: M30', 'patient id: M57', 'patient id: M43', 'patient id: M48', 'patient id: M51', 'patient id: M10', 'patient id: M24'],\n", " 1: ['Sex: F', 'Sex: M'],\n", " 2: ['age at death: 58', 'age at death: 59', 'age at death: 80', 'age at death: 63', 'age at death: 47', 'age at death: 78', 'age at death: 88', 'age at death: 45', 'age at death: 61', 'age at death: 50', 'age at death: 54', 'age at death: 69', 'age at death: 39', 'age at death: 56', 'age at death: 44', 'age at death: 42', 'age at death: 92', 'age at death: 71', 'age at death: 77', 'age at death: 34', 'age at death: 49', 'age at death: 70'],\n", " 3: ['post mortem interval: 16', 'post mortem interval: 9', 'post mortem interval: 13', 'post mortem interval: 11', 'post mortem interval: 22', 'post mortem interval: 28', 'post mortem interval: 10', 'post mortem interval: 12', 'post mortem interval: 5', 'post mortem interval: 24', 'post mortem interval: 18', 'post mortem interval: 6', 'post mortem interval: 8', 'post mortem interval: 7', 'post mortem interval: 17', 'post mortem interval: 31'],\n", " 4: ['ms type: PPMS', 'ms type: SPMS', 'ms type: PRMS'],\n", " 5: ['disease duration: 22', 'disease duration: 4', 'disease duration: 36', 'disease duration: 39', 'disease duration: 17', 'disease duration: 47', 'disease duration: 30', 'disease duration: 6', 'disease duration: 26', 'disease duration: ?', 'disease duration: 20', 'disease duration: 42', 'disease duration: 25', 'disease duration: 31', 'disease duration: 21', 'disease duration: 11', 'disease duration: 54', 'disease duration: 35', 'disease duration: 33', 'disease duration: 16', 'disease duration: 27', 'disease duration: 41', 'disease duration: 18'],\n", " 6: ['tissue: Grey Matter Lesion', 'tissue: Grey Matter'],\n", " 7: ['tissue region: Frontal', 'tissue region: Parietal'],\n", " 8: ['block: 1', 'block: 2', 'block: 5', 'block: 3', 'block: 4', 'block: 6', 'block: 8'],\n", " 9: ['sample: M02-1_GL4', 'sample: M60-2_G1', 'sample: M44-2_GL1', 'sample: M13-2_GL2', 'sample: M53-1_G1', 'sample: M14-2_GL1', 'sample: M60-1_G1', 'sample: M56-1_G1', 'sample: M46-1_G1', 'sample: M46-1_GL1', 'sample: M61-1_G3', 'sample: M42-2_GL1', 'sample: M28-5_GL1', 'sample: M23-1_GL2', 'sample: M44-1_G1', 'sample: M61-1_G2', 'sample: M52-1_GL2', 'sample: M12-2_GL1', 'sample: M32-3_G4', 'sample: M06-1_G4', 'sample: M23-3_G1', 'sample: M01-3_G1', 'sample: M36-5_GL2', 'sample: M06-1_G3', 'sample: M14-4_G2', 'sample: M06-6_G2', 'sample: M06-4_GL1', 'sample: M44-2_GL2', 'sample: M02-3_GL1', 'sample: M01-6_GL1'],\n", " 10: ['rin: 8.5', 'rin: 7.9', 'rin: 8.7', 'rin: 8.9', 'rin: 8.8', 'rin: 9.2', 'rin: 8.2', 'rin: 8\n" ] }, { "cell_type": "markdown", "id": "392af5e5", "metadata": {}, "source": [ "### Step 3: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": null, "id": "90af050d", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import os\n", "import json\n", "import numpy as np\n", "\n", "# Load the example data from the previous step\n", "clinical_data = pd.DataFrame({\n", " 1: {\n", " \"GSM3782097\": \"disease state: MS\",\n", " \"GSM3782098\": \"disease state: MS\",\n", " \"GSM3782099\": \"disease state: MS\",\n", " \"GSM3782100\": \"disease state: MS\",\n", " \"GSM3782101\": \"disease state: MS\",\n", " \"GSM3782102\": \"disease state: MS\",\n", " \"GSM3782103\": \"disease state: MS\",\n", " \"GSM3782104\": \"disease state: MS\",\n", " \"GSM3782105\": \"disease state: control\",\n", " \"GSM3782106\": \"disease state: control\",\n", " \"GSM3782107\": \"disease state: control\",\n", " \"GSM3782108\": \"disease state: control\",\n", " \"GSM3782109\": \"disease state: control\"\n", " },\n", " 2: {\n", " \"GSM3782097\": \"gender: female\",\n", " \"GSM3782098\": \"gender: female\",\n", " \"GSM3782099\": \"gender: female\",\n", " \"GSM3782100\": \"gender: female\",\n", " \"GSM3782101\": \"gender: female\",\n", " \"GSM3782102\": \"gender: female\",\n", " \"GSM3782103\": \"gender: female\",\n", " \"GSM3782104\": \"gender: female\",\n", " \"GSM3782105\": \"gender: female\",\n", " \"GSM3782106\": \"gender: female\",\n", " \"GSM3782107\": \"gender: female\",\n", " \"GSM3782108\": \"gender: female\",\n", " \"GSM3782109\": \"gender: female\"\n", " },\n", " 3: {\n", " \"GSM3782097\": \"age: 65\",\n", " \"GSM3782098\": \"age: 38\",\n", " \"GSM3782099\": \"age: 43\",\n", " \"GSM3782100\": \"age: 61\",\n", " \"GSM3782101\": \"age: 61\",\n", " \"GSM3782102\": \"age: 45\",\n", " \"GSM3782103\": \"age: 45\",\n", " \"GSM3782104\": \"age: 43\",\n", " \"GSM3782105\": \"age: 49\",\n", " \"GSM3782106\": \"age: 42\",\n", " \"GSM3782107\": \"age: 30\",\n", " \"GSM3782108\": \"age: 59\",\n", " \"GSM3782109\": \"age: 51\"\n", " }\n", "})\n", "\n", "# 1. Gene Expression Data Availability\n", "# Based on the dataset name (GSE131279), this appears to be a gene expression dataset, not just miRNA or methylation\n", "is_gene_available = True\n", "\n", "# 2.1 Data Availability\n", "# Identify rows containing trait, age, and gender information\n", "trait_row = 1 # Disease state (MS vs control)\n", "age_row = 3 # Age information\n", "gender_row = 2 # Gender information, but all samples are female (constant)\n", "\n", "# Since gender is constant (all female), we'll mark it as not available for our analysis\n", "gender_row = None \n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value):\n", " if value is None:\n", " return None\n", " # Extract value after colon\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " # Convert to binary (MS = 1, control = 0)\n", " if value.lower() == \"ms\":\n", " return 1\n", " elif value.lower() == \"control\":\n", " return 0\n", " else:\n", " return None\n", "\n", "def convert_age(value):\n", " if value is None:\n", " return None\n", " # Extract value after colon\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " # Convert to integer\n", " try:\n", " return int(value)\n", " except:\n", " return None\n", "\n", "def convert_gender(value):\n", " # Not used since gender is constant\n", " if value is None:\n", " return None\n", " # Extract value after colon\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " # Convert to binary (female = 0, male = 1)\n", " if value.lower() == \"female\":\n", " return 0\n", " elif value.lower() == \"male\":\n", " return 1\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata\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 (since trait_row is not None)\n", "if trait_row is not None:\n", " # Manually create DataFrame for clinical features\n", " # Initialize a DataFrame with sample IDs as index\n", " samples = clinical_data[trait_row].keys()\n", " clinical_features = pd.DataFrame(index=samples)\n", " \n", " # Extract and convert trait data\n", " clinical_features[trait] = [convert_trait(clinical_data[trait_row][sample]) for sample in samples]\n", " \n", " # Extract and convert age data\n", " if age_row is not None:\n", " clinical_features['Age'] = [convert_age(clinical_data[age_row][sample]) for sample in samples]\n", " \n", " # Extract and convert gender data (None in this case as all samples are female)\n", " if gender_row is not None:\n", " clinical_features['Gender'] = [convert_gender(clinical_data[gender_row][sample]) for sample in samples]\n", " \n", " # Preview the extracted features\n", " preview = preview_df(clinical_features)\n", " print(\"Clinical features preview:\", preview)\n", " print(\"Full clinical features shape:\", clinical_features.shape)\n", " \n", " # Save clinical data to CSV\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " clinical_features.to_csv(out_clinical_data_file)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "de885131", "metadata": {}, "source": [ "### Step 4: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": null, "id": "4098eafd", "metadata": {}, "outputs": [], "source": [ "# 1. Re-identify the SOFT and matrix files to ensure we have the correct paths\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. Extract the gene expression data from the matrix file\n", "gene_data = get_genetic_data(matrix_file)\n", "\n", "# 3. Print the first 20 row IDs (gene or probe identifiers)\n", "print(\"\\nFirst 20 gene/probe identifiers:\")\n", "print(gene_data.index[:20])\n", "\n", "# 4. Print the dimensions of the gene expression data\n", "print(f\"\\nGene data dimensions: {gene_data.shape[0]} genes × {gene_data.shape[1]} samples\")\n", "\n", "# Note: we keep is_gene_available as True since we successfully extracted gene expression data\n", "is_gene_available = True\n" ] }, { "cell_type": "markdown", "id": "8ced8f23", "metadata": {}, "source": [ "### Step 5: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": null, "id": "7b87eb4b", "metadata": {}, "outputs": [], "source": [ "# Looking at the gene identifiers, I can see these are Illumina probe IDs (ILMN_prefix)\n", "# rather than standard human gene symbols (which would look like: BRCA1, TP53, etc.)\n", "# These are microarray probe identifiers that need to be mapped to standard gene symbols\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "0c93d5d2", "metadata": {}, "source": [ "### Step 6: Gene Annotation" ] }, { "cell_type": "code", "execution_count": null, "id": "19e473bf", "metadata": {}, "outputs": [], "source": [ "# 1. First get the file paths using geo_get_relevant_filepaths function\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. 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", "# 3. 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": "be423778", "metadata": {}, "source": [ "### Step 7: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": null, "id": "a33b316c", "metadata": {}, "outputs": [], "source": [ "# 1. Identify which columns in gene_annotation to use for mapping\n", "# Based on the preview, we need to use:\n", "# - 'ID' column: contains the probe identifiers (ILMN_*)\n", "# - 'Symbol' column: contains the gene symbols\n", "\n", "# 2. Get the gene mapping dataframe\n", "mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol')\n", "print(f\"Mapping dataframe shape: {mapping_df.shape}\")\n", "print(\"First 5 rows of mapping dataframe:\")\n", "print(mapping_df.head())\n", "\n", "# 3. Apply the gene mapping to convert probe-level measurements to gene-level expression\n", "gene_data = apply_gene_mapping(gene_data, mapping_df)\n", "print(f\"\\nConverted gene expression data shape: {gene_data.shape}\")\n", "print(\"First 5 genes in the gene expression data:\")\n", "print(gene_data.index[:5])\n", "\n", "# Save the gene expression data to a CSV 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\"\\nGene expression data saved to {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "7420572b", "metadata": {}, "source": [ "### Step 8: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": null, "id": "f35bc9b5", "metadata": {}, "outputs": [], "source": [ "# 1. Re-define variables from previous steps\n", "# Based on sample characteristics, define which rows contain relevant information\n", "trait_row = 6 # 'tissue: Grey Matter Lesion' vs 'tissue: Grey Matter'\n", "age_row = 2 # 'age at death'\n", "gender_row = 1 # 'Sex: F' vs 'Sex: M'\n", "\n", "# Re-define conversion functions\n", "def convert_trait(value):\n", " \"\"\"Convert tissue type to binary: 1 for Grey Matter Lesion, 0 for Grey Matter\"\"\"\n", " if not isinstance(value, str):\n", " return None\n", " \n", " # Extract the value after the colon\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " if \"lesion\" in value.lower():\n", " return 1\n", " elif \"grey matter\" == value.lower():\n", " return 0\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age at death to continuous value\"\"\"\n", " if not isinstance(value, str):\n", " return None\n", " \n", " # Extract the value after the colon\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " try:\n", " return float(value)\n", " except (ValueError, TypeError):\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender to binary: 0 for female, 1 for male\"\"\"\n", " if not isinstance(value, str):\n", " return None\n", " \n", " # Extract the value after the colon\n", " if \":\" in value:\n", " value = value.split(\":\", 1)[1].strip()\n", " \n", " if value.upper() in [\"F\", \"FEMALE\"]:\n", " return 0\n", " elif value.upper() in [\"M\", \"MALE\"]:\n", " return 1\n", " return None\n", "\n", "# Re-load the gene data that was saved in a previous step\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "if os.path.exists(out_gene_data_file):\n", " gene_data = pd.read_csv(out_gene_data_file, index_col=0)\n", " print(\"Loaded gene data from file\")\n", "else:\n", " # Re-extract the data\n", " raw_gene_data = get_genetic_data(matrix_file)\n", " gene_annotation = get_gene_annotation(soft_file)\n", " mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol')\n", " gene_data = apply_gene_mapping(raw_gene_data, mapping_df)\n", " print(\"Re-extracted gene data\")\n", "\n", "# 1. Normalize gene symbols in the index of gene expression data\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Gene data shape after normalization: {normalized_gene_data.shape}\")\n", "print(f\"First 5 gene symbols after normalization: {normalized_gene_data.index[:5]}\")\n", "\n", "# Save the normalized 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", "# Reload the clinical_data\n", "background_info, clinical_data = get_background_and_clinical_data(matrix_file)\n", "\n", "# Print the sample IDs to understand the data structure\n", "print(\"Sample IDs in clinical data:\")\n", "print(list(clinical_data.columns[:5]), \"...\") # Show first 5 sample IDs\n", "\n", "# Print the sample IDs in gene expression data\n", "print(\"Sample IDs in gene expression data:\")\n", "print(list(normalized_gene_data.columns[:5]), \"...\") # Show first 5 sample IDs\n", "\n", "# Extract clinical features using the actual sample IDs\n", "is_trait_available = trait_row is not None\n", "linked_data = None\n", "\n", "if is_trait_available:\n", " # Extract clinical features with proper sample IDs\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 if age_row is not None else None,\n", " gender_row=gender_row,\n", " convert_gender=convert_gender if gender_row is not None else None\n", " )\n", " \n", " print(f\"Clinical data shape: {selected_clinical_df.shape}\")\n", " print(f\"Clinical data preview: {preview_df(selected_clinical_df, n=3)}\")\n", " \n", " # Save the clinical data\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " selected_clinical_df.to_csv(out_clinical_data_file)\n", " print(f\"Clinical data saved to {out_clinical_data_file}\")\n", " \n", " # Link clinical and genetic data\n", " linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)\n", " print(f\"Linked data shape before handling missing values: {linked_data.shape}\")\n", " \n", " if linked_data.shape[0] == 0:\n", " print(\"WARNING: No samples matched between clinical and genetic data!\")\n", " is_trait_available = False\n", " is_biased = True\n", " linked_data = pd.DataFrame() # Empty dataframe as fallback\n", " else:\n", " # 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", " # Determine if trait and demographic features are biased\n", " is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n", " print(f\"Data shape after removing biased features: {linked_data.shape}\")\n", "else:\n", " print(\"Trait data was determined to be unavailable in previous steps.\")\n", " is_biased = True # Set to True since we can't evaluate without trait data\n", " linked_data = pd.DataFrame() # Empty dataframe as fallback\n", "\n", "# Validate and save cohort info\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=is_trait_available,\n", " is_biased=is_biased,\n", " df=linked_data,\n", " note=\"Dataset contains gene expression data from multiple sclerosis patients comparing grey matter lesions to normal grey matter.\"\n", ")\n", "\n", "# Save linked data if usable\n", "if is_usable and linked_data is not None 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 deemed not usable for associational studies.\")" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 5 }