|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Crohns_Disease" |
|
cohort = "GSE123088" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Crohns_Disease" |
|
in_cohort_dir = "../DATA/GEO/Crohns_Disease/GSE123088" |
|
|
|
|
|
out_data_file = "./output/preprocess/1/Crohns_Disease/GSE123088.csv" |
|
out_gene_data_file = "./output/preprocess/1/Crohns_Disease/gene_data/GSE123088.csv" |
|
out_clinical_data_file = "./output/preprocess/1/Crohns_Disease/clinical_data/GSE123088.csv" |
|
json_path = "./output/preprocess/1/Crohns_Disease/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) |
|
|
|
|
|
|
|
|
|
|
|
is_gene_available = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
trait_row = 1 |
|
age_row = 3 |
|
gender_row = 2 |
|
|
|
|
|
|
|
def convert_trait(x: str): |
|
""" |
|
Convert the diagnosis to a binary indicator: |
|
1 if it is 'CROHN_DISEASE', else 0. |
|
Return None for any unrecognized or empty value. |
|
""" |
|
|
|
parts = x.split(":") |
|
if len(parts) < 2: |
|
return None |
|
val = parts[1].strip().upper() |
|
if val == "CROHN_DISEASE": |
|
return 1 |
|
else: |
|
return 0 |
|
|
|
def convert_age(x: str): |
|
""" |
|
Parse 'age: number' from the string. |
|
Return the number as a float if valid. Otherwise return None. |
|
""" |
|
parts = x.split(":") |
|
if len(parts) < 2: |
|
return None |
|
val = parts[1].strip() |
|
|
|
try: |
|
return float(val) |
|
except ValueError: |
|
return None |
|
|
|
def convert_gender(x: str): |
|
""" |
|
Convert 'Sex: Female' -> 0, 'Sex: Male' -> 1. |
|
Return None for unrecognized or empty values. |
|
""" |
|
parts = x.split(":") |
|
if len(parts) < 2: |
|
return None |
|
val = parts[1].strip().upper() |
|
if val == "MALE": |
|
return 1 |
|
elif val == "FEMALE": |
|
return 0 |
|
else: |
|
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 |
|
) |
|
|
|
|
|
|
|
if trait_row is not None: |
|
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_info = preview_df(selected_clinical_df) |
|
print("Clinical feature preview:", preview_info) |
|
|
|
selected_clinical_df.to_csv(out_clinical_data_file, index=False) |
|
|
|
|
|
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)) |
|
|
|
|
|
import re |
|
|
|
def apply_gene_mapping_entrez(expression_df: pd.DataFrame, mapping_df: pd.DataFrame) -> pd.DataFrame: |
|
""" |
|
Convert probe-level expression measurements to gene-level measurements using numeric (Entrez) IDs. |
|
For any probe that maps to multiple Entrez IDs, the expression is divided equally among them. |
|
Then we sum up contributions for each gene across all probes. |
|
""" |
|
|
|
mapping_df = mapping_df[mapping_df['ID'].isin(expression_df.index)].copy() |
|
|
|
|
|
|
|
mapping_df['Gene'] = mapping_df['Gene'].astype(str).fillna('') |
|
|
|
|
|
def split_gene_ids(x: str): |
|
return [val.strip() for val in re.split(r'[;,]', x) if val.strip()] |
|
|
|
mapping_df['Gene'] = mapping_df['Gene'].apply(split_gene_ids) |
|
|
|
|
|
mapping_df['num_genes'] = mapping_df['Gene'].apply(len) |
|
|
|
|
|
mapping_df = mapping_df.explode('Gene').dropna(subset=['Gene']) |
|
|
|
|
|
mapping_df.set_index('ID', inplace=True) |
|
|
|
|
|
merged_df = mapping_df.join(expression_df, how='inner') |
|
|
|
|
|
expr_cols = expression_df.columns |
|
|
|
|
|
merged_df[expr_cols] = merged_df[expr_cols].div( |
|
merged_df['num_genes'].replace(0, 1), axis=0 |
|
) |
|
|
|
|
|
gene_expression_df = merged_df.groupby('Gene')[expr_cols].sum() |
|
|
|
return gene_expression_df |
|
|
|
|
|
probe_col = 'ID' |
|
symbol_col = 'ENTREZ_GENE_ID' |
|
|
|
|
|
mapping_df = get_gene_mapping(gene_annotation, prob_col=probe_col, gene_col=symbol_col) |
|
|
|
|
|
gene_data = apply_gene_mapping_entrez(gene_data, mapping_df) |
|
|
|
|
|
print("Mapped gene_data shape:", gene_data.shape) |
|
print("First 5 gene identifiers after mapping:", gene_data.index[:5]) |
|
import os |
|
import pandas as pd |
|
|
|
|
|
|
|
|
|
normalized_gene_data = normalize_gene_symbols_in_index(gene_data) |
|
normalized_gene_data.to_csv(out_gene_data_file) |
|
|
|
|
|
|
|
tmp = pd.read_csv(out_clinical_data_file, header=0) |
|
|
|
|
|
row_names = tmp.iloc[:, 0].astype(str).str.strip().values |
|
|
|
|
|
samples = [col.strip() for col in tmp.columns[1:].tolist()] |
|
|
|
|
|
selected_clinical_df = tmp.iloc[:, 1:].copy() |
|
selected_clinical_df.index = row_names |
|
selected_clinical_df.columns = samples |
|
|
|
|
|
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data) |
|
|
|
|
|
linked_data = handle_missing_values(linked_data, 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="Data from GSE123088, trait is Crohn's disease." |
|
) |
|
|
|
|
|
if is_usable: |
|
linked_data.to_csv(out_data_file) |