GenoTEX / output /preprocess /Epilepsy /code /GSE273630.py
Liu-Hy's picture
Add files using upload-large-folder tool
9efdaa1 verified
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Epilepsy"
cohort = "GSE273630"
# Input paths
in_trait_dir = "../DATA/GEO/Epilepsy"
in_cohort_dir = "../DATA/GEO/Epilepsy/GSE273630"
# Output paths
out_data_file = "./output/z3/preprocess/Epilepsy/GSE273630.csv"
out_gene_data_file = "./output/z3/preprocess/Epilepsy/gene_data/GSE273630.csv"
out_clinical_data_file = "./output/z3/preprocess/Epilepsy/clinical_data/GSE273630.csv"
json_path = "./output/z3/preprocess/Epilepsy/cohort_info.json"
# Step 1: Initial Data Loading
from tools.preprocess import *
# 1. Identify the paths to the SOFT file and the matrix file
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
# 2. Read the matrix file to obtain background information and sample characteristics data
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
# 3. Obtain the sample characteristics dictionary from the clinical dataframe
sample_characteristics_dict = get_unique_values_by_row(clinical_data)
# 4. Explicitly print out all the background information and the sample characteristics dictionary
print("Background Information:")
print(background_info)
print("Sample Characteristics Dictionary:")
print(sample_characteristics_dict)
# Step 2: Dataset Analysis and Clinical Feature Extraction
# Step 1: Determine gene expression availability
is_gene_available = True # NanoString digital transcript panel indicates gene expression data
# Step 2: Variable availability and conversion functions
# Availability from the provided Sample Characteristics Dictionary and background info:
# - Trait (Epilepsy): Excluded by design; no sample-level field -> not available
# - Age: No per-sample age field provided -> not available
# - Gender: All participants are males by design (constant) -> not available
trait_row = None
age_row = None
gender_row = None
# Conversion functions (defined for interface completeness; they won't be used since rows are None)
def _extract_value(cell):
if cell is None:
return None
if isinstance(cell, str):
parts = cell.split(":", 1)
return parts[1].strip() if len(parts) == 2 else cell.strip()
return cell
def convert_trait(x):
# Map epilepsy-related information to binary (0/1); default to None if unknown
val = _extract_value(x)
if val is None:
return None
s = str(val).strip().lower()
# Positive indications
positive_kw = ["epilep", "seizure", "ictal", "sz"]
# Negative phrases
negative_kw = ["no epilepsy", "non-epilep", "without epilepsy", "seizure-free", "no seizure", "none"]
if any(k in s for k in negative_kw):
return 0
if any(k in s for k in positive_kw):
# avoid false positives if explicitly negated
if "no " in s or "not " in s:
return 0
return 1
return None
def convert_age(x):
val = _extract_value(x)
if val is None:
return None
s = str(val)
# Extract first number (integer or float)
import re
m = re.search(r"(-?\d+\.?\d*)", s)
if m:
try:
return float(m.group(1))
except:
return None
return None
def convert_gender(x):
val = _extract_value(x)
if val is None:
return None
s = str(val).strip().lower()
if s in ["male", "m", "man", "boy"]:
return 1
if s in ["female", "f", "woman", "girl"]:
return 0
# Try to infer from single letters embedded
if "male" in s:
return 1
if "female" in s:
return 0
return None
# Step 3: Save metadata using initial filtering
is_trait_available = trait_row is not None
_ = validate_and_save_cohort_info(is_final=False,
cohort=cohort,
info_path=json_path,
is_gene_available=is_gene_available,
is_trait_available=is_trait_available)
# Step 4: Clinical feature extraction
# Skipped because trait_row is None (no clinical trait data available for Epilepsy in this dataset)
# Step 3: Gene Data Extraction
# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
gene_data = get_genetic_data(matrix_file)
# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
print(gene_data.index[:20])
# Step 4: Gene Identifier Review
requires_gene_mapping = False
print(f"requires_gene_mapping = {requires_gene_mapping}")
# Step 5: Data Normalization and Linking
# 1. Normalize the obtained gene data and save
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
normalized_gene_data.to_csv(out_gene_data_file)
# 2-6. Proceed only if clinical data with trait exists (guard safely via locals().get)
if (locals().get('selected_clinical_data') is not None) and (locals().get('trait_row') is not None):
# Link clinical and genetic data
linked_data = geo_link_clinical_genetic_data(selected_clinical_data, normalized_gene_data)
# Handle missing values
linked_data = handle_missing_values(linked_data, trait)
# Bias check and remove biased demographic features
is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)
# Final validation and save cohort info
is_usable = validate_and_save_cohort_info(
is_final=True,
cohort=cohort,
info_path=json_path,
is_gene_available=True,
is_trait_available=True,
is_biased=is_trait_biased,
df=unbiased_linked_data,
note="INFO: Clinical features extracted and linked successfully."
)
# Save linked data only if usable
if is_usable:
unbiased_linked_data.to_csv(out_data_file)
else:
# No clinical trait data available; only gene data saved in this step
print("No clinical trait data available; skipping linking and final validation for this cohort.")