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 = "GSE19776"
# Input paths
in_trait_dir = "../DATA/GEO/Adrenocortical_Cancer"
in_cohort_dir = "../DATA/GEO/Adrenocortical_Cancer/GSE19776"
# Output paths
out_data_file = "./output/preprocess/1/Adrenocortical_Cancer/GSE19776.csv"
out_gene_data_file = "./output/preprocess/1/Adrenocortical_Cancer/gene_data/GSE19776.csv"
out_clinical_data_file = "./output/preprocess/1/Adrenocortical_Cancer/clinical_data/GSE19776.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)
# Step 1: Decide if the dataset contains gene expression data
# Based on the series title "Adrenocortical Carcinoma Gene Expression Profiling",
# we conclude that it is likely to contain gene expression data.
is_gene_available = True
# Step 2: Variable Availability and Data Type Conversion
# 2.1 Identify Rows
# - trait: We see only "tissue: adrenocortical carcinoma" under key 0. This is a single unique value,
# which is uninformative for association. Hence treat it as not available for the trait.
trait_row = None
# - age: Found under key 5 (multiple distinct values, some are "age: Unknown").
age_row = 5
# - gender: Found under key 4 (M/F). Multiple values, not constant.
gender_row = 4
# 2.2 Define Conversion Functions
def convert_trait(x: str) -> int:
"""
Returns None because trait is not available (single unique value in dataset).
This function is a placeholder to adhere to the required interface.
"""
return None
def convert_age(x: str) -> float:
"""
Convert the substring after 'age:' to float if possible.
If it's 'Unknown' or non-parsable, return None.
"""
val = x.split(':')[-1].strip()
if val.lower() == "unknown":
return None
try:
return float(val)
except ValueError:
return None
def convert_gender(x: str) -> int:
"""
Convert 'gender: F' -> 0, 'gender: M' -> 1.
If the value is unknown or doesn't match, return None.
"""
val = x.split(':')[-1].strip().upper()
if val == 'F':
return 0
elif val == 'M':
return 1
return None
# Step 3: Save initial filtering metadata
# Trait data is not available if trait_row is None
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
)
# Step 4: Extract clinical features only if trait_row is not None
# Since trait_row = 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])
# The provided gene identifiers are all numeric, which are not standard human gene symbols.
# They likely refer to probe IDs or some other numeric format.
# Therefore, gene mapping to human gene symbols is required.
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))
# STEP6: Gene Identifier Mapping
# Reviewer feedback indicates a mismatch between the numeric row IDs in the gene expression dataframe
# (e.g., "3", "4", "5") and the probe IDs in the annotation file (e.g., "1007_s_at", "1053_at").
# Because there is no overlap, a direct mapping is not possible with the provided annotation.
# We'll demonstrate a fallback approach: we attempt to match, but if no overlap is found, we skip mapping.
# 1. Decide which columns in the annotation *would* store the probe IDs and gene symbols if they matched.
probe_col = "ID"
gene_col = "Gene Symbol"
# 2. Extract the potential mapping dataframe.
mapping_df = get_gene_mapping(gene_annotation, prob_col=probe_col, gene_col=gene_col)
# 3. Check for any intersection in identifiers before applying the mapping.
common_ids = set(gene_data.index).intersection(mapping_df['ID'])
if len(common_ids) == 0:
print("No matching identifiers found between gene expression data and annotation. Skipping gene mapping.")
else:
gene_data = apply_gene_mapping(gene_data, mapping_df)
print("Gene mapping applied successfully.")
# STEP 7: Data Normalization and Linking
# In this dataset, we determined in Step 2 that trait data is not available (trait_row = None).
# Therefore, we cannot link clinical and genetic data or perform trait-based processing.
# Nonetheless, we can still normalize probe-level data to standard gene symbols and finalize validation.
# 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)
# 2. Since trait data is missing, skip linking clinical and genetic data,
# skip missing-value handling and bias detection for the trait.
# 3. Conduct final validation and record info.
# Since trait data is unavailable, set is_trait_available=False,
# pass a dummy/empty DataFrame and is_biased=False (it won't be used).
dummy_df = pd.DataFrame()
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,
df=dummy_df,
note="No trait data found; skipped clinical-linking steps."
)
# 4. If the dataset were usable, save. In this scenario, it's not usable due to missing trait data.
if is_usable:
dummy_df.to_csv(out_data_file, index=True)