Liu-Hy's picture
Add files using upload-large-folder tool
f426016 verified
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Anxiety_disorder"
cohort = "GSE94119"
# Input paths
in_trait_dir = "../DATA/GEO/Anxiety_disorder"
in_cohort_dir = "../DATA/GEO/Anxiety_disorder/GSE94119"
# Output paths
out_data_file = "./output/preprocess/1/Anxiety_disorder/GSE94119.csv"
out_gene_data_file = "./output/preprocess/1/Anxiety_disorder/gene_data/GSE94119.csv"
out_clinical_data_file = "./output/preprocess/1/Anxiety_disorder/clinical_data/GSE94119.csv"
json_path = "./output/preprocess/1/Anxiety_disorder/cohort_info.json"
# STEP 1
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("\nSample Characteristics Dictionary:")
print(sample_characteristics_dict)
# 1. Gene Expression Data Availability
is_gene_available = True # Based on the background info (Illumina HT-12v4 BeadChip microarray)
# 2. Variable Availability and Data Type Conversion
# From the sample characteristics, we see:
# - trait (Anxiety_disorder): Not explicitly in the dictionary, and all are anxiety patients => no variation
# - age: Not found in the dictionary
# - gender: Key 0 with 'FEMALE' and 'MALE'
trait_row = None # No variation or row for Anxiety_disorder
age_row = None # No row for age
gender_row = 0 # gender is stored in key 0
# Define converter functions
def convert_trait(value: str):
# Not used here because trait is not available; return None
return None
def convert_age(value: str):
# Not used here because age_row is None; return None
return None
def convert_gender(value: str):
# Example values like "gender: FEMALE" or "gender: MALE"
parts = value.split(":")
if len(parts) < 2:
return None
gender_str = parts[1].strip().upper()
if gender_str == "FEMALE":
return 0
elif gender_str == "MALE":
return 1
else:
return None
# 3. Save Metadata (initial filtering)
is_trait_available = (trait_row is not None)
is_usable = 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
)
# 4. Clinical Feature Extraction
# Only if trait_row is not None (which it isn't). So skip.
# STEP3
# 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])
# Based on the listed Illumina probe IDs (e.g., ILMN_1651228), these are not human gene symbols.
# They will require mapping to get the official gene symbols.
print("requires_gene_mapping = True")
# STEP5
# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
gene_annotation = get_gene_annotation(soft_file)
# 2. Use the 'preview_df' function from the library to preview the data and print out the results.
print("Gene annotation preview:")
print(preview_df(gene_annotation))
# STEP: Gene Identifier Mapping
# 1. Identify the relevant columns in the gene annotation dataframe.
# From the preview, we see "ID" holds the Illumina identifiers (matching the expression data index),
# and "Symbol" holds the gene symbols.
# 2. Extract the mapping between probe IDs and gene symbols.
mapping_df = get_gene_mapping(gene_annotation, prob_col="ID", gene_col="Symbol")
# 3. Convert probe-level measurements to gene-level expression using apply_gene_mapping.
gene_data = apply_gene_mapping(gene_data, mapping_df)
# (Optionally, you might preview or inspect the resulting gene_data here if needed)
# STEP 7: Data Normalization and Linking
# 1. Normalize gene symbols in the obtained gene expression data
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
normalized_gene_data.to_csv(out_gene_data_file)
print(f"Saved normalized gene data to {out_gene_data_file}")
# Since we do not have a trait_row (it was None), there's no separate "selected_clinical".
# We'll just reuse the clinical_data from previous steps.
selected_clinical = clinical_data
# 2. Link the clinical and genetic data on sample IDs
linked_data = geo_link_clinical_genetic_data(selected_clinical, normalized_gene_data)
# 3 & 4. We skip trait-based missing-value handling and bias checks since there's no trait.
# 5. Conduct final quality validation and save metadata
# Since the trait is not available, set is_trait_available=False.
# We must also provide is_biased=False to comply with validate_and_save_cohort_info's requirement when is_final=True.
is_usable = validate_and_save_cohort_info(
is_final=True,
cohort=cohort,
info_path=json_path,
is_gene_available=True,
is_trait_available=False,
is_biased=False, # No trait to judge bias.
df=linked_data,
note="No trait data available in this cohort."
)
# 6. If the dataset is deemed usable (unlikely here without trait), save the final linked data
if is_usable:
linked_data.to_csv(out_data_file, index=True)
print(f"Saved final linked data to {out_data_file}")
else:
print("The dataset is not usable for trait-based association. Skipping final output.")