Liu-Hy's picture
Add files using upload-large-folder tool
a3c6344 verified
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Allergies"
cohort = "GSE169149"
# Input paths
in_trait_dir = "../DATA/GEO/Allergies"
in_cohort_dir = "../DATA/GEO/Allergies/GSE169149"
# Output paths
out_data_file = "./output/preprocess/1/Allergies/GSE169149.csv"
out_gene_data_file = "./output/preprocess/1/Allergies/gene_data/GSE169149.csv"
out_clinical_data_file = "./output/preprocess/1/Allergies/clinical_data/GSE169149.csv"
json_path = "./output/preprocess/1/Allergies/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)
# Step 1: Determine gene expression availability
is_gene_available = True # Based on the background, we assume this dataset likely contains gene expression data.
# Step 2: Identify data availability for 'trait', 'age', and 'gender'
# According to the sample characteristics dictionary, there is no mention of "Allergies," "age," or "gender."
trait_row = None
age_row = None
gender_row = None
# Step 2.2: Define data type conversion functions
def convert_trait(value: str) -> Optional[int]:
# No actual data for 'Allergies' in this dataset
return None
def convert_age(value: str) -> Optional[float]:
# No age information in this dataset
return None
def convert_gender(value: str) -> Optional[int]:
# No gender information in this dataset
return None
# Step 3: Conduct initial filtering and save metadata
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: If trait data is available, extract clinical features; otherwise, skip.
if trait_row is not None:
selected_clinical_df = geo_select_clinical_features(
clinical_data,
trait=trait,
trait_row=trait_row,
convert_trait=convert_trait,
age_row=age_row,
convert_age=convert_age,
gender_row=gender_row,
convert_gender=convert_gender
)
preview = preview_df(selected_clinical_df)
selected_clinical_df.to_csv(out_clinical_data_file, index=False)
# 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 numeric nature of these identifiers, they do not appear to be conventional human gene symbols.
# Therefore, they require mapping to known 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. Decide which columns map the same kind of IDs as the gene expression data and which store the gene symbols
# From the annotation preview, the "ID" column matches the expression data identifiers (1, 2, 3, ...).
# The "Assay" column appears to contain the gene symbols.
# 2. Extract a gene mapping dataframe
mapping_df = get_gene_mapping(gene_annotation, prob_col="ID", gene_col="Assay")
# 3. Convert probe-level measurements to gene expression data
gene_data = apply_gene_mapping(gene_data, mapping_df)
# Display the first few rows of the resulting gene expression dataframe for verification
print(gene_data.head())
import pandas as pd
# STEP 7: Data Normalization and (Conditional) 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, index=True)
print(f"Saved normalized gene data to {out_gene_data_file}")
# Since trait_row was None in step 2, we have no clinical features extracted.
# Hence 'clinical_data_selected' does not exist, and there is no trait column to link or to analyze.
# We will proceed with final validation using the fact that trait data is unavailable.
is_trait_available = False
is_gene_available = True # As concluded in step 2, it is a gene expression dataset
if not is_trait_available:
# Without trait data, we cannot link or do the usual missing-value handling by trait.
# We still provide the normalized_gene_data to the validator (though it won't be used for trait analysis).
final_data = normalized_gene_data
is_biased = False # We must supply a boolean; no trait data => cannot assess bias
# 5. Final quality validation
is_usable = validate_and_save_cohort_info(
is_final=True,
cohort=cohort,
info_path=json_path,
is_gene_available=is_gene_available,
is_trait_available=is_trait_available,
is_biased=is_biased,
df=final_data,
note="No trait data available in this dataset."
)
# 6. If the dataset is usable, save final data; however, in this scenario it likely won't be.
if is_usable:
final_data.to_csv(out_data_file)
print(f"Saved final linked data to {out_data_file}")
else:
print("Data not usable; skipping final output.")
else:
# If trait data were available, we would link, handle missing values, check bias, and finalize.
# This branch is skipped because 'is_trait_available' is False.
pass