|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Heart_rate" |
|
cohort = "GSE18583" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Heart_rate" |
|
in_cohort_dir = "../DATA/GEO/Heart_rate/GSE18583" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Heart_rate/GSE18583.csv" |
|
out_gene_data_file = "./output/preprocess/3/Heart_rate/gene_data/GSE18583.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Heart_rate/clinical_data/GSE18583.csv" |
|
json_path = "./output/preprocess/3/Heart_rate/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 = 2 |
|
age_row = None |
|
gender_row = 0 |
|
|
|
|
|
def convert_trait(value): |
|
if pd.isna(value): |
|
return None |
|
try: |
|
|
|
return float(value.split(": ")[1]) |
|
except: |
|
return None |
|
|
|
def convert_age(value): |
|
|
|
return None |
|
|
|
def convert_gender(value): |
|
if pd.isna(value): |
|
return None |
|
|
|
return 1 |
|
|
|
|
|
initial_validation = 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) |
|
) |
|
|
|
|
|
if trait_row is not None: |
|
clinical_features = 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 |
|
) |
|
|
|
|
|
preview = preview_df(clinical_features) |
|
print("Preview of clinical features:") |
|
print(preview) |
|
|
|
|
|
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True) |
|
clinical_features.to_csv(out_clinical_data_file) |
|
|
|
genetic_data = get_genetic_data(matrix_file_path) |
|
|
|
|
|
print("First 20 row IDs (gene/probe identifiers):") |
|
print(genetic_data.index[:20].tolist()) |
|
|
|
|
|
requires_gene_mapping = True |
|
|
|
with gzip.open(soft_file_path, 'rt') as f: |
|
for i, line in enumerate(f): |
|
if i < 100: |
|
print(line.strip()) |
|
else: |
|
break |
|
|
|
|
|
gene_metadata = get_gene_annotation(soft_file_path, prefixes=['#', '!', '^', '@']) |
|
|
|
|
|
print("\nAll annotation columns:") |
|
print(list(gene_metadata.columns)) |
|
|
|
|
|
preview = preview_df(gene_metadata, max_items=1000) |
|
print("\nGene annotation preview:") |
|
print(preview) |
|
|
|
with gzip.open(soft_file_path, 'rt') as f: |
|
chip_annotation = '' |
|
reading = False |
|
for line in f: |
|
if '!platform_table_begin' in line: |
|
reading = True |
|
continue |
|
if reading: |
|
if '!platform_table_end' in line: |
|
break |
|
chip_annotation += line |
|
|
|
|
|
annotation_df = pd.read_csv(io.StringIO(chip_annotation), sep='\t') |
|
print("\nAvailable annotation columns:") |
|
print(list(annotation_df.columns)) |
|
|
|
|
|
print("\nFirst 10 rows of annotation:") |
|
print(annotation_df.head(10)) |
|
|
|
|
|
|
|
def extract_gene_symbol(transcript_id): |
|
|
|
parts = transcript_id.split('_') |
|
if len(parts) > 2: |
|
return parts[1] |
|
return transcript_id |
|
|
|
|
|
mapping_data = pd.DataFrame({ |
|
'ID': genetic_data.index, |
|
'Gene': [extract_gene_symbol(id) for id in genetic_data.index] |
|
}) |
|
|
|
|
|
gene_data = apply_gene_mapping(genetic_data, mapping_data) |
|
|
|
|
|
print("\nFirst few genes and their expression values:") |
|
print(preview_df(gene_data)) |
|
|
|
|
|
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True) |
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
gene_data.index = gene_data.index.str.replace('_at', '') |
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
|
|
clinical_data = pd.read_csv(out_clinical_data_file, index_col=0) |
|
|
|
|
|
print("Clinical data samples:", clinical_data.columns.tolist()[:5]) |
|
print("Genetic data samples:", gene_data.columns.tolist()[:5]) |
|
|
|
|
|
clinical_data.columns = clinical_data.columns.str.replace('GSM', '') |
|
gene_data.columns = gene_data.columns.str.replace('GSM', '') |
|
|
|
|
|
linked_data = geo_link_clinical_genetic_data(clinical_data, gene_data) |
|
|
|
|
|
linked_data = linked_data.rename(index={'Heart_rate': trait}) |
|
|
|
|
|
linked_data = handle_missing_values(linked_data, trait) |
|
|
|
|
|
is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait) |
|
|
|
|
|
is_usable = validate_and_save_cohort_info( |
|
is_final=True, |
|
cohort=cohort, |
|
info_path=json_path, |
|
is_gene_available=is_gene_available, |
|
is_trait_available=(trait_row is not None), |
|
is_biased=is_biased, |
|
df=linked_data, |
|
note="This dataset contains gene expression from skeletal muscle tissue measured with Ensembl transcript IDs. Gene symbol mapping was not possible. Gender is constant (all male). Heart rate measurement is available for most but not all samples." |
|
) |
|
|
|
|
|
if is_usable: |
|
os.makedirs(os.path.dirname(out_data_file), exist_ok=True) |
|
linked_data.to_csv(out_data_file) |