Liu-Hy's picture
Add files using upload-large-folder tool
c192597 verified
raw
history blame
6.21 kB
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Chronic_obstructive_pulmonary_disease_(COPD)"
cohort = "GSE32030"
# Input paths
in_trait_dir = "../DATA/GEO/Chronic_obstructive_pulmonary_disease_(COPD)"
in_cohort_dir = "../DATA/GEO/Chronic_obstructive_pulmonary_disease_(COPD)/GSE32030"
# Output paths
out_data_file = "./output/preprocess/1/Chronic_obstructive_pulmonary_disease_(COPD)/GSE32030.csv"
out_gene_data_file = "./output/preprocess/1/Chronic_obstructive_pulmonary_disease_(COPD)/gene_data/GSE32030.csv"
out_clinical_data_file = "./output/preprocess/1/Chronic_obstructive_pulmonary_disease_(COPD)/clinical_data/GSE32030.csv"
json_path = "./output/preprocess/1/Chronic_obstructive_pulmonary_disease_(COPD)/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 background info indicating microarray gene expression data
# 2) Variable Availability
# Checking the sample characteristics dictionary, 'copd status' only appears as "yes", no variation.
# No explicit age or gender info is provided in the dictionary. Thus, all are considered unavailable.
trait_row = None
age_row = None
gender_row = None
# 2.2 Data Type Conversion Functions
def convert_trait(value: str) -> int:
# For demonstration, but will not be used because trait_row is None
# e.g., return 1 if the string (after the colon) indicates COPD, else 0 or None
parts = str(value).split(':', 1)
val = parts[1].strip().lower() if len(parts) > 1 else ''
if val == 'yes':
return 1
elif val == 'no':
return 0
return None
def convert_age(value: str) -> float:
# For demonstration, but will not be used because age_row is None
parts = str(value).split(':', 1)
val = parts[1].strip().lower() if len(parts) > 1 else ''
try:
return float(val)
except ValueError:
return None
def convert_gender(value: str) -> int:
# For demonstration, but will not be used because gender_row is None
parts = str(value).split(':', 1)
val = parts[1].strip().lower() if len(parts) > 1 else ''
if val in ['m', 'male']:
return 1
elif val in ['f', 'female']:
return 0
return None
# 3) Save Metadata with 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 - skip since trait_row is None
# 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])
# These identifiers (e.g., "1007_s_at", "1053_at") are probe set IDs (commonly used by Affymetrix microarray platforms).
# Therefore, they are not standard human gene symbols and do require mapping.
print("\nrequires_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. Identify the columns in gene_annotation corresponding to the same identifiers as in gene_data and the gene symbols.
# From the preview, "ID" matches the probe IDs in gene_data, and "Gene Symbol" holds the gene symbols.
# 2. Get the gene mapping dataframe using the 'get_gene_mapping' function.
mapping_df = get_gene_mapping(
annotation=gene_annotation,
prob_col="ID", # Probe ID column
gene_col="Gene Symbol" # Gene symbol column
)
# 3. Convert probe-level measurements to gene-level expression data by applying the mapping.
gene_data = apply_gene_mapping(gene_data, mapping_df)
# For verification, let's print the shape of the mapped gene_data and a small slice.
print("Mapped gene_data shape:", gene_data.shape)
print(gene_data.iloc[:5, :5])
# STEP7
# In a previous step, we determined that trait data is not available, which means
# is_trait_available = False. Hence, clinical feature extraction was skipped
# and "selected_clinical_df" was never created.
# 1) Normalize gene symbols using synonym information from NCBI, then save the result.
normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
normalized_gene_data.to_csv(out_gene_data_file)
# 2) Since the trait is unavailable, we skip linking clinical data (no "selected_clinical_df").
# 3), 4), 5) Provide final validation. The library requires 'is_biased' to be a boolean.
# Since the dataset has no trait, we set is_biased=False.
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=normalized_gene_data,
note="Trait data unavailable; only gene expression data was processed."
)
# 6) Without trait data, we cannot perform further trait-based analysis, so no final linked dataset is saved.