|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Height" |
|
cohort = "GSE181339" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Height" |
|
in_cohort_dir = "../DATA/GEO/Height/GSE181339" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Height/GSE181339.csv" |
|
out_gene_data_file = "./output/preprocess/3/Height/gene_data/GSE181339.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Height/clinical_data/GSE181339.csv" |
|
json_path = "./output/preprocess/3/Height/cohort_info.json" |
|
|
|
|
|
soft_file_path, matrix_file_path = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
|
|
|
background_info, clinical_data = get_background_and_clinical_data(matrix_file_path) |
|
|
|
|
|
unique_values_dict = get_unique_values_by_row(clinical_data) |
|
|
|
|
|
print("Background Information:") |
|
print(background_info) |
|
print("\nSample Characteristics:") |
|
print(json.dumps(unique_values_dict, indent=2)) |
|
|
|
is_gene_available = True |
|
|
|
|
|
trait_row = 1 |
|
age_row = 2 |
|
gender_row = 0 |
|
|
|
|
|
def convert_trait(x: str) -> Optional[float]: |
|
"""Convert BMI group to binary (0 for normal weight, 1 for overweight/obese)""" |
|
if not x or not isinstance(x, str): |
|
return None |
|
group = x.split(': ')[-1].strip().upper() |
|
if 'NW' in group: |
|
return 0 |
|
elif 'OW' in group or 'OB' in group: |
|
return 1 |
|
return None |
|
|
|
def convert_age(x: str) -> Optional[float]: |
|
"""Convert age string to float""" |
|
if not x or not isinstance(x, str): |
|
return None |
|
try: |
|
return float(x.split(': ')[-1]) |
|
except: |
|
return None |
|
|
|
def convert_gender(x: str) -> Optional[float]: |
|
"""Convert gender to binary (0 for female, 1 for male)""" |
|
if not x or not isinstance(x, str): |
|
return None |
|
gender = x.split(': ')[-1].strip().lower() |
|
if 'woman' in gender or 'female' in gender: |
|
return 0 |
|
elif 'man' in gender or 'male' in gender: |
|
return 1 |
|
return None |
|
|
|
|
|
validate_and_save_cohort_info( |
|
is_final=False, |
|
cohort=cohort, |
|
info_path=json_path, |
|
is_gene_available=is_gene_available, |
|
is_trait_available=trait_row is not None |
|
) |
|
|
|
|
|
clinical_df = geo_select_clinical_features( |
|
clinical_df=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 |
|
) |
|
|
|
preview_result = preview_df(clinical_df) |
|
print("Preview of clinical data:") |
|
print(preview_result) |
|
|
|
clinical_df.to_csv(out_clinical_data_file) |
|
|
|
genetic_data = get_genetic_data(matrix_file_path) |
|
|
|
|
|
print("First few rows of the genetic data:") |
|
print(genetic_data.head()) |
|
print("\nShape of genetic data:", genetic_data.shape) |
|
print("\nColumn names:", genetic_data.columns.tolist()) |
|
|
|
|
|
requires_gene_mapping = True |
|
|
|
gene_metadata = get_gene_annotation(soft_file_path) |
|
|
|
|
|
print("Column names:") |
|
print(gene_metadata.columns.tolist()) |
|
print("\nPreview of first few rows:") |
|
print(json.dumps(preview_df(gene_metadata), indent=2)) |
|
|
|
|
|
|
|
mapping_data = get_gene_mapping(gene_metadata, 'ID', 'GENE_SYMBOL') |
|
|
|
|
|
gene_data = apply_gene_mapping(genetic_data, mapping_data) |
|
|
|
|
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
|
|
print("Shape of gene expression data:", gene_data.shape) |
|
print("\nFirst few rows of gene expression data:") |
|
print(gene_data.head()) |
|
|
|
gene_data = normalize_gene_symbols_in_index(gene_data) |
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
|
|
linked_data = geo_link_clinical_genetic_data(clinical_df, gene_data) |
|
|
|
|
|
linked_data = handle_missing_values(linked_data, trait) |
|
|
|
|
|
is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait) |
|
|
|
|
|
note = "Dataset contains gene expression data from PBMCs and height measurements from 40 subjects" |
|
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=is_biased, |
|
df=linked_data, |
|
note=note |
|
) |
|
|
|
|
|
if is_usable: |
|
os.makedirs(os.path.dirname(out_data_file), exist_ok=True) |
|
linked_data.to_csv(out_data_file) |