|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Underweight" |
|
cohort = "GSE130563" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Underweight" |
|
in_cohort_dir = "../DATA/GEO/Underweight/GSE130563" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Underweight/GSE130563.csv" |
|
out_gene_data_file = "./output/preprocess/3/Underweight/gene_data/GSE130563.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Underweight/clinical_data/GSE130563.csv" |
|
json_path = "./output/preprocess/3/Underweight/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) |
|
|
|
|
|
print("Background Information:") |
|
print(background_info) |
|
print("\nClinical Data Shape:", clinical_data.shape) |
|
print("\nFirst few rows of Clinical Data:") |
|
print(clinical_data.head()) |
|
|
|
print("\nSample Characteristics:") |
|
|
|
unique_values_dict = get_unique_values_by_row(clinical_data) |
|
for row, values in unique_values_dict.items(): |
|
print(f"\n{row}:") |
|
print(values) |
|
|
|
is_gene_available = True |
|
|
|
|
|
trait_row = 3 |
|
age_row = 4 |
|
gender_row = 1 |
|
|
|
|
|
def convert_trait(val): |
|
|
|
if ':' in val: |
|
val = val.split(':')[1].strip() |
|
|
|
try: |
|
if val == '0': |
|
return 0 |
|
elif val == 'n.d. (not determined)': |
|
return None |
|
else: |
|
weight_loss = float(val) |
|
return 1 if weight_loss >= 5 else 0 |
|
except: |
|
return None |
|
|
|
def convert_age(val): |
|
|
|
if ':' in val: |
|
val = val.split(':')[1].strip() |
|
try: |
|
return float(val) |
|
except: |
|
return None |
|
|
|
def convert_gender(val): |
|
|
|
if ':' in val: |
|
val = val.split(':')[1].strip() |
|
if val == 'F': |
|
return 0 |
|
elif val == 'M': |
|
return 1 |
|
return None |
|
|
|
|
|
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) |
|
|
|
|
|
clinical_features = 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 |
|
) |
|
|
|
|
|
print(preview_df(clinical_features)) |
|
|
|
|
|
clinical_features.to_csv(out_clinical_data_file) |
|
|
|
genetic_data = get_genetic_data(matrix_file_path) |
|
|
|
|
|
print("First 20 gene/probe IDs:") |
|
print(list(genetic_data.index[:20])) |
|
|
|
|
|
requires_gene_mapping = True |
|
|
|
gene_annotation = get_gene_annotation(soft_file_path, prefixes=['!', '#']) |
|
|
|
|
|
print("All annotation columns:") |
|
print(list(gene_annotation.columns)) |
|
|
|
|
|
print("\nGene annotation preview (first few rows):") |
|
print(gene_annotation.head()) |
|
|
|
gene_annotation = get_gene_annotation(soft_file_path, prefixes=['!Series', '!Sample', '^']) |
|
|
|
|
|
print("Gene annotation preview:") |
|
print(gene_annotation.head()) |
|
print("\nAnnotation shape:", gene_annotation.shape) |
|
print("\nAnnotation columns:", list(gene_annotation.columns)) |
|
|
|
|
|
mapping_data = get_gene_mapping(gene_annotation, prob_col='ID_REF', gene_col='Gene Symbol') |
|
|
|
|
|
print("\nMapping data preview:") |
|
print(mapping_data.head()) |
|
print("\nMapping data shape:", mapping_data.shape) |
|
|
|
|
|
gene_data = apply_gene_mapping(genetic_data, mapping_data) |
|
|
|
|
|
print("\nFirst 20 gene symbols:") |
|
print(list(gene_data.index[:20])) |
|
|
|
print("\nShape of gene expression data:") |
|
print(gene_data.shape) |
|
|
|
|
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
platform_files = [f for f in os.listdir(in_cohort_dir) if 'annot' in f.lower()] |
|
platform_file_path = os.path.join(in_cohort_dir, platform_files[0]) |
|
|
|
|
|
platform_annotation = pd.read_csv(platform_file_path, sep='\t', skiprows=0, low_memory=False) |
|
|
|
|
|
print("Platform annotation columns:") |
|
print(list(platform_annotation.columns)) |
|
|
|
|
|
print("\nPlatform annotation preview:") |
|
print(platform_annotation[['probeset_id', 'gene_assignment']].head()) |
|
|
|
|
|
mapping_data = platform_annotation[['probeset_id', 'gene_assignment']].copy() |
|
mapping_data = mapping_data.rename(columns={'probeset_id': 'ID', 'gene_assignment': 'Gene'}) |
|
|
|
|
|
print("\nMapping data shape:", mapping_data.shape) |
|
print("\nMapping data preview:") |
|
print(mapping_data.head()) |
|
|
|
gene_annotation = get_gene_annotation(soft_file_path, prefixes=['!Platform']) |
|
|
|
|
|
print("Gene annotation preview:") |
|
print(gene_annotation.head()) |
|
print("\nAnnotation columns:", list(gene_annotation.columns)) |
|
|
|
|
|
|
|
probe_ids = genetic_data.index.tolist() |
|
mapping_data = pd.DataFrame({'ID': probe_ids}) |
|
mapping_data['Gene'] = mapping_data['ID'].str.extract(r'([A-Za-z0-9]+)_at') |
|
|
|
|
|
print("\nMapping data preview:") |
|
print(mapping_data.head()) |
|
print("\nMapping data shape:", mapping_data.shape) |
|
|
|
|
|
gene_data = apply_gene_mapping(genetic_data, mapping_data) |
|
|
|
print("\nGene data preview:") |
|
print(gene_data.head()) |
|
print("\nGene data shape:", gene_data.shape) |
|
|
|
|
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
genetic_data = get_genetic_data(matrix_file_path) |
|
|
|
|
|
print("First 20 gene/probe IDs:") |
|
print(list(genetic_data.index[:20])) |
|
|
|
prefixes_to_exclude = ['!Series', '!Sample', '^SERIES', '^SAMPLE'] |
|
gene_annotation = get_gene_annotation(soft_file_path, prefixes=prefixes_to_exclude) |
|
|
|
|
|
probe_gene_lines = [] |
|
in_mapping = False |
|
with gzip.open(soft_file_path, 'rt') as f: |
|
for line in f: |
|
if '!platform_table_begin' in line: |
|
in_mapping = True |
|
continue |
|
elif '!platform_table_end' in line: |
|
break |
|
elif in_mapping: |
|
probe_gene_lines.append(line) |
|
|
|
|
|
if probe_gene_lines: |
|
mapping_df = pd.read_csv(io.StringIO(''.join(probe_gene_lines)), sep='\t') |
|
print("Available columns in platform table:") |
|
print(mapping_df.columns) |
|
print("\nFirst few rows of platform table:") |
|
print(mapping_df.head()) |
|
|
|
|
|
id_column = [col for col in mapping_df.columns if 'id' in col.lower()][0] |
|
gene_column = [col for col in mapping_df.columns if 'gene' in col.lower()][0] |
|
|
|
mapping_data = pd.DataFrame({ |
|
'ID': mapping_df[id_column], |
|
'Gene': mapping_df[gene_column] |
|
}) |
|
else: |
|
|
|
probe_ids = genetic_data.index.tolist() |
|
mapping_data = pd.DataFrame({'ID': probe_ids, 'Gene': [x.split('_')[0] for x in probe_ids]}) |
|
|
|
|
|
gene_data = apply_gene_mapping(genetic_data, mapping_data) |
|
|
|
|
|
gene_data = normalize_gene_symbols_in_index(gene_data) |
|
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True) |
|
gene_data.to_csv(out_gene_data_file) |
|
print("\nGene data shape (normalized gene-level):", gene_data.shape) |
|
|
|
|
|
selected_clinical_df = pd.read_csv(out_clinical_data_file, index_col=0) |
|
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, gene_data) |
|
|
|
|
|
linked_data = handle_missing_values(linked_data, trait) |
|
|
|
|
|
is_trait_biased, linked_data = judge_and_remove_biased_features(linked_data, trait) |
|
|
|
|
|
note = "Dataset contains gene expression data from rectus abdominis muscle biopsies, along with weight loss and clinical information from pancreatic cancer patients." |
|
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_trait_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) |