|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Congestive_heart_failure" |
|
cohort = "GSE182600" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Congestive_heart_failure" |
|
in_cohort_dir = "../DATA/GEO/Congestive_heart_failure/GSE182600" |
|
|
|
|
|
out_data_file = "./output/preprocess/1/Congestive_heart_failure/GSE182600.csv" |
|
out_gene_data_file = "./output/preprocess/1/Congestive_heart_failure/gene_data/GSE182600.csv" |
|
out_clinical_data_file = "./output/preprocess/1/Congestive_heart_failure/clinical_data/GSE182600.csv" |
|
json_path = "./output/preprocess/1/Congestive_heart_failure/cohort_info.json" |
|
|
|
|
|
from tools.preprocess import * |
|
|
|
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
|
|
|
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) |
|
|
|
|
|
sample_characteristics_dict = get_unique_values_by_row(clinical_data) |
|
|
|
|
|
print("Background Information:") |
|
print(background_info) |
|
print("Sample Characteristics Dictionary:") |
|
print(sample_characteristics_dict) |
|
import pandas as pd |
|
import os |
|
import json |
|
|
|
|
|
is_gene_available = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
trait_row = 0 |
|
age_row = 1 |
|
gender_row = 2 |
|
|
|
|
|
|
|
def convert_trait(x: str): |
|
""" |
|
Convert disease state into binary: 1 if the disease state is 'Congestive heart failure', else 0. |
|
If unknown, return None. |
|
""" |
|
|
|
parts = x.split(':') |
|
if len(parts) < 2: |
|
return None |
|
disease_str = parts[1].strip().lower() |
|
if disease_str == 'congestive heart failure': |
|
return 1 |
|
else: |
|
return 0 |
|
|
|
def convert_age(x: str): |
|
""" |
|
Convert age string into a continuous float value. |
|
If unknown, return None. |
|
""" |
|
parts = x.split(':') |
|
if len(parts) < 2: |
|
return None |
|
val_str = parts[1].strip() |
|
try: |
|
return float(val_str) |
|
except ValueError: |
|
return None |
|
|
|
def convert_gender(x: str): |
|
""" |
|
Convert gender string into binary: female -> 0, male -> 1. |
|
If unknown, return None. |
|
""" |
|
parts = x.split(':') |
|
if len(parts) < 2: |
|
return None |
|
gender_str = parts[1].strip().lower() |
|
if gender_str == 'f': |
|
return 0 |
|
elif gender_str == 'm': |
|
return 1 |
|
else: |
|
return 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 |
|
) |
|
|
|
|
|
if trait_row is not None: |
|
|
|
|
|
|
|
|
|
clinical_data_dict = { |
|
0: [ |
|
'disease state: Acute myocarditis', |
|
'disease state: Acute myocardial infarction', |
|
'disease state: Dilated cardiomyopathy, DCMP', |
|
'disease state: Congestive heart failure', |
|
'disease state: Dilated cardiomyopathy', |
|
'disease state: Arrhythmia', |
|
'disease state: Aortic dissection' |
|
], |
|
1: [ |
|
'age: 33.4', 'age: 51.2', 'age: 51.9', 'age: 47.8', |
|
'age: 41.5', 'age: 67.3', 'age: 52.8', 'age: 16.1', |
|
'age: 78.9', 'age: 53.2', 'age: 70.9', 'age: 59.9', |
|
'age: 21.9', 'age: 45.2', 'age: 52.4', 'age: 32.3', |
|
'age: 55.8', 'age: 47', 'age: 57.3', 'age: 31.7', |
|
'age: 49.3', 'age: 66.1', 'age: 55.9', 'age: 49.1', |
|
'age: 63', 'age: 21', 'age: 53.6', 'age: 50.1', |
|
'age: 37.4', 'age: 71.5' |
|
], |
|
2: ['gender: F', 'gender: M'], |
|
3: ['outcome: Success', 'outcome: Failure', 'outcome: failure'], |
|
4: ['cell type: PBMC'], |
|
5: ['time: 0hr', 'time: 2hr', 'time: Removal'] |
|
} |
|
|
|
|
|
clinical_data = pd.DataFrame.from_dict(clinical_data_dict, orient='index').fillna('') |
|
|
|
selected_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 = preview_df(selected_clinical_df) |
|
print("Preview of selected clinical features:") |
|
print(preview) |
|
|
|
|
|
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True) |
|
selected_clinical_df.to_csv(out_clinical_data_file) |
|
|
|
|
|
gene_data = get_genetic_data(matrix_file) |
|
|
|
|
|
print(gene_data.index[:20]) |
|
|
|
|
|
|
|
print("requires_gene_mapping = True") |
|
|
|
|
|
gene_annotation = get_gene_annotation(soft_file) |
|
|
|
|
|
print("Gene annotation preview:") |
|
print(preview_df(gene_annotation)) |
|
|
|
|
|
|
|
|
|
|
|
mapping_data = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol') |
|
|
|
|
|
gene_data = apply_gene_mapping(gene_data, mapping_data) |
|
|
|
|
|
|
|
normalized_gene_data = normalize_gene_symbols_in_index(gene_data) |
|
normalized_gene_data.to_csv(out_gene_data_file) |
|
|
|
|
|
|
|
num_clinical_samples = selected_clinical_df.shape[1] |
|
num_gene_samples = normalized_gene_data.shape[1] |
|
common_samples = min(num_clinical_samples, num_gene_samples) |
|
selected_clinical_df.columns = normalized_gene_data.columns[:common_samples] |
|
selected_clinical_df = selected_clinical_df.iloc[:, :common_samples] |
|
|
|
|
|
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data) |
|
|
|
|
|
linked_data = handle_missing_values(linked_data, trait_col=trait) |
|
|
|
|
|
trait_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=True, |
|
is_trait_available=True, |
|
is_biased=trait_biased, |
|
df=linked_data, |
|
note="Processed dataset for congestive heart failure." |
|
) |
|
|
|
|
|
if is_usable: |
|
linked_data.to_csv(out_data_file) |