Liu-Hy's picture
Add files using upload-large-folder tool
ee94703 verified
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Adrenocortical_Cancer"
cohort = "GSE75415"
# Input paths
in_trait_dir = "../DATA/GEO/Adrenocortical_Cancer"
in_cohort_dir = "../DATA/GEO/Adrenocortical_Cancer/GSE75415"
# Output paths
out_data_file = "./output/preprocess/1/Adrenocortical_Cancer/GSE75415.csv"
out_gene_data_file = "./output/preprocess/1/Adrenocortical_Cancer/gene_data/GSE75415.csv"
out_clinical_data_file = "./output/preprocess/1/Adrenocortical_Cancer/clinical_data/GSE75415.csv"
json_path = "./output/preprocess/1/Adrenocortical_Cancer/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. Determine if gene expression data is available
is_gene_available = True # Based on the series title/summary indicating gene expression microarray data.
# 2. Identify rows and define conversion functions for trait, age, and gender.
trait_row = 1 # "histologic type: ..." key
age_row = None # No age info found
gender_row = 0 # "gender: ..." key
def convert_trait(value: str):
"""
Convert histologic type to a binary variable:
1 => adrenocortical carcinoma
0 => adenoma or normal
None => unknown
"""
parts = value.split(':', 1)
if len(parts) == 2:
val = parts[1].strip().lower()
if 'carcinoma' in val:
return 1
elif 'adenoma' in val or 'normal' in val:
return 0
elif 'unknown' in val:
return None
return None
def convert_age(value: str):
"""
Age data is not available, so return None.
"""
return None
def convert_gender(value: str):
"""
Convert gender to binary:
0 => female
1 => male
None => unknown
"""
parts = value.split(':', 1)
if len(parts) == 2:
val = parts[1].strip().lower()
if val == 'female':
return 0
elif val == 'male':
return 1
return None
# 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
)
# 4. Extract clinical features if trait data is available
if trait_row is not None:
selected_clinical_df = geo_select_clinical_features(
clinical_data, # Assuming clinical_data is the DataFrame with sample characteristics
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 and save
previewed = preview_df(selected_clinical_df)
print("Selected Clinical Features Preview:", previewed)
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])
# Observing the provided list of identifiers (e.g., "1007_s_at", "1053_at"), they are Affymetrix probe set IDs.
# These are not standard human gene symbols; hence they do require mapping.
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. Based on the annotation preview, the column "ID" in 'gene_annotation' matches the probe identifiers
# in the gene expression data (also labeled "ID"). The column "Gene Symbol" contains the actual gene symbols.
# 2. Extract the two columns from the gene annotation dataframe, "ID" (probe ID) and "Gene Symbol" (gene symbol),
# to create the mapping dataframe.
mapping_df = get_gene_mapping(gene_annotation, "ID", "Gene Symbol")
# 3. Convert probe-level measurements to gene-level expression data using the mapping.
gene_data = apply_gene_mapping(gene_data, mapping_df)
# (Optional) Print the resulting dataframe shape and some rows to verify.
print("Gene expression data shape after mapping:", gene_data.shape)
print(gene_data.head())
# STEP 7: Data Normalization and Linking
# 1. Normalize gene symbols and save the normalized gene data
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
normalized_gene_data.to_csv(out_gene_data_file, index=True)
# 2. Link clinical and genetic data on sample IDs
# "selected_clinical_df" was defined in a previous step, so we can use it directly.
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
# 3. Handle missing values systematically
processed_data = handle_missing_values(linked_data, trait)
# 4. Determine whether the trait or demographic features are severely biased
trait_biased, processed_data = judge_and_remove_biased_features(processed_data, trait)
# 5. Final quality 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=trait_biased,
df=processed_data,
note="Trait data present and mapped from step 2."
)
# 6. Save the final linked data only if usable
if is_usable:
processed_data.to_csv(out_data_file, index=True)