Liu-Hy's picture
Add files using upload-large-folder tool
ee94703 verified
raw
history blame
6.23 kB
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Age-Related_Macular_Degeneration"
cohort = "GSE29801"
# Input paths
in_trait_dir = "../DATA/GEO/Age-Related_Macular_Degeneration"
in_cohort_dir = "../DATA/GEO/Age-Related_Macular_Degeneration/GSE29801"
# Output paths
out_data_file = "./output/preprocess/1/Age-Related_Macular_Degeneration/GSE29801.csv"
out_gene_data_file = "./output/preprocess/1/Age-Related_Macular_Degeneration/gene_data/GSE29801.csv"
out_clinical_data_file = "./output/preprocess/1/Age-Related_Macular_Degeneration/clinical_data/GSE29801.csv"
json_path = "./output/preprocess/1/Age-Related_Macular_Degeneration/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
is_gene_available = True # Based on transcriptome analysis in the series description
# 2. Variable Availability and Data Type Conversion
trait_row = 3 # Using "ocular disease: normal/AMD" as our binary trait indicator
age_row = 2 # "age (years): ..." entries
gender_row = 1 # "gender: male/female" entries
def convert_trait(value: str):
parts = value.split(":", 1)
if len(parts) < 2:
return None
val = parts[1].strip().lower()
if val == "normal":
return 0
elif val == "amd":
return 1
return None
def convert_age(value: str):
parts = value.split(":", 1)
if len(parts) < 2:
return None
val = parts[1].strip()
try:
return float(val)
except ValueError:
return None
def convert_gender(value: str):
parts = value.split(":", 1)
if len(parts) < 2:
return None
val = parts[1].strip().lower()
if val == "female":
return 0
elif val == "male":
return 1
return None
# 3. Save Metadata (initial filtering)
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. Clinical Feature Extraction if trait data is available
if trait_row is not None:
df_clinical = 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
)
print("Clinical Data Preview:", preview_df(df_clinical))
df_clinical.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 gene identifiers, they appear to be numeric (e.g., "12", "13", ... ),
# which are not standard human gene symbols. Therefore, these IDs would need to be mapped.
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. From earlier previews, the gene expression data indexes match the "ID" column in the annotation,
# and the gene symbols are in the "GENE_SYMBOL" column.
probe_id_col = "ID"
gene_symbol_col = "GENE_SYMBOL"
# 2. Build the gene mapping dataframe using these columns.
mapping_df = get_gene_mapping(gene_annotation, prob_col=probe_id_col, gene_col=gene_symbol_col)
# 3. Convert probe-level data to gene-level expression using the mapping.
gene_data = apply_gene_mapping(gene_data, mapping_df)
# (Optional) Print resulting shape for a quick check.
print("Mapped gene_data shape:", gene_data.shape)
# 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)