Liu-Hy's picture
Add files using upload-large-folder tool
24fe0da verified
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Endometriosis"
cohort = "GSE165004"
# Input paths
in_trait_dir = "../DATA/GEO/Endometriosis"
in_cohort_dir = "../DATA/GEO/Endometriosis/GSE165004"
# Output paths
out_data_file = "./output/preprocess/1/Endometriosis/GSE165004.csv"
out_gene_data_file = "./output/preprocess/1/Endometriosis/gene_data/GSE165004.csv"
out_clinical_data_file = "./output/preprocess/1/Endometriosis/clinical_data/GSE165004.csv"
json_path = "./output/preprocess/1/Endometriosis/cohort_info.json"
# STEP1
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)
# 1. Gene Expression Data Availability
# Based on the metadata ("RNA expression"), we assume gene expression data is available.
is_gene_available = True
# 2. Variable Availability and Data Type Conversion
# From the sample characteristics, there is no mention of Endometriosis status, age, or gender.
# Thus all rows for these variables are considered unavailable.
trait_row = None
age_row = None
gender_row = None
# Define the required conversion functions (though they won't be used since rows are None).
def convert_trait(value: str):
# No data available; return None
return None
def convert_age(value: str):
# No data available; return None
return None
def convert_gender(value: str):
# No data available; return None
return None
# 3. Save Metadata (initial filtering)
is_trait_available = (trait_row is not None) # This will be False
_ = 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
# Since trait_row is None, we skip clinical feature extraction.
# 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])
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 to use for mapping
# Based on the annotation preview, we see that "ID" in the annotation matches the "ID" used in the gene expression data.
# The column storing gene symbols in the annotation is "GENE_SYMBOL".
probe_col = "ID"
gene_symbol_col = "GENE_SYMBOL"
# 2. Get a dataframe mapping probe IDs to gene symbols
mapping_df = get_gene_mapping(gene_annotation, prob_col=probe_col, gene_col=gene_symbol_col)
# 3. Convert probe-level measurements to gene expression data
gene_data = apply_gene_mapping(gene_data, mapping_df)
# For observation, let's print the shape and the first 10 index entries of the mapped gene expression data.
print("Mapped Gene Expression Data shape:", gene_data.shape)
print("First 10 Gene Symbols in Mapped Expression Data:", gene_data.index[:10].tolist())
# STEP 7
import pandas as pd
# 1. Normalize the gene expression data to standard gene symbols.
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
normalized_gene_data.to_csv(out_gene_data_file)
print("Normalized gene expression data saved to:", out_gene_data_file)
# Since in Step 2 we determined that trait_row = None (no trait data),
# we cannot proceed with linking or final analysis based on trait.
is_trait_available = False
if not is_trait_available:
print("Trait data is not available -> skipping clinical-data linking and subsequent steps.")
# 2. Perform final validation with an empty DataFrame and a placeholder is_biased=False
# because the library requires these parameters in final mode.
empty_df = pd.DataFrame()
is_biased_placeholder = False
is_usable = validate_and_save_cohort_info(
is_final=True,
cohort=cohort,
info_path=json_path,
is_gene_available=True, # Gene data is available
is_trait_available=False,
is_biased=is_biased_placeholder, # Arbitrary value to satisfy the function requirement
df=empty_df,
note="No trait data available. Performed final validation with empty DataFrame."
)
# 3. The dataset is not usable due to missing trait data, so do not save any final linked data.
if is_usable:
print("Unexpected: dataset marked usable despite missing trait. No final data saved.")
else:
print("Dataset is not usable (missing trait). No final data saved.")
else:
# This block would handle linking, missing values, etc. if trait were available.
pass