|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Underweight" |
|
cohort = "GSE84954" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Underweight" |
|
in_cohort_dir = "../DATA/GEO/Underweight/GSE84954" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Underweight/GSE84954.csv" |
|
out_gene_data_file = "./output/preprocess/3/Underweight/gene_data/GSE84954.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Underweight/clinical_data/GSE84954.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 = 1 |
|
|
|
def convert_trait(value: str) -> Optional[int]: |
|
"""Convert disease status to binary (0 for control, 1 for liver disease)""" |
|
if not value or ':' not in value: |
|
return None |
|
value = value.split(':', 1)[1].strip() |
|
if 'Crigler-Najjar' in value: |
|
return 0 |
|
elif 'chronic liver disease' in value or 'Alagille' in value: |
|
return 1 |
|
return None |
|
|
|
|
|
age_row = None |
|
convert_age = None |
|
|
|
|
|
gender_row = None |
|
convert_gender = 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 |
|
) |
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
print("Clinical Data Preview:") |
|
print(preview_df(selected_clinical_df)) |
|
|
|
|
|
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True) |
|
selected_clinical_df.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) |
|
|
|
|
|
print("Gene annotation preview:") |
|
print(preview_df(gene_annotation)) |
|
|
|
print("\nAll columns in annotation data:") |
|
print(list(gene_annotation.columns)) |
|
|
|
metadata_pattern = r'!platform_table_begin\n(.*?)\n!platform_table_end' |
|
with gzip.open(soft_file_path, 'rt') as f: |
|
content = f.read() |
|
|
|
matches = re.findall(metadata_pattern, content, re.DOTALL) |
|
if matches: |
|
platform_data = pd.read_csv(io.StringIO(matches[0]), sep='\t') |
|
|
|
|
|
mapping_data = platform_data[['ID', 'Gene Symbol']].copy() |
|
mapping_data = mapping_data.dropna() |
|
mapping_data = mapping_data.rename(columns={'Gene Symbol': 'Gene'}) |
|
|
|
|
|
gene_data = apply_gene_mapping(genetic_data, mapping_data) |
|
|
|
|
|
print("\nFirst 10 rows of mapped gene expression data:") |
|
print(preview_df(gene_data.head(10))) |
|
|
|
|
|
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True) |
|
gene_data.to_csv(out_gene_data_file) |
|
else: |
|
print("Could not find platform table in SOFT file") |
|
|
|
|
|
platform_pattern = r'#ID = (.*?)\n(.*?)!platform_table_begin' |
|
gene_pattern = r'#Gene_Symbol = (.*?)\n' |
|
|
|
with gzip.open(soft_file_path, 'rt') as f: |
|
content = f.read() |
|
|
|
|
|
platform_matches = re.search(platform_pattern, content, re.DOTALL) |
|
if platform_matches: |
|
platform_section = platform_matches.group(2) |
|
gene_matches = re.search(gene_pattern, platform_section) |
|
if gene_matches: |
|
|
|
platform_data = pd.read_csv(io.StringIO(platform_matches.group(2)), sep='\t') |
|
gene_col = gene_matches.group(1).strip() |
|
|
|
mapping_data = platform_data[['ID', gene_col]].copy() |
|
mapping_data = mapping_data.dropna() |
|
mapping_data = mapping_data.rename(columns={gene_col: 'Gene'}) |
|
|
|
|
|
gene_data = apply_gene_mapping(genetic_data, mapping_data) |
|
|
|
|
|
print("\nFirst 10 rows of mapped gene expression data:") |
|
print(preview_df(gene_data.head(10))) |
|
|
|
|
|
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True) |
|
gene_data.to_csv(out_gene_data_file) |
|
else: |
|
print("Could not find gene symbol column information in platform metadata") |
|
else: |
|
print("Could not find platform metadata section in SOFT file") |
|
|
|
with gzip.open(soft_file_path, 'rt') as f: |
|
platform_section = False |
|
gene_mapping_lines = [] |
|
for line in f: |
|
if line.startswith('!Platform_table_begin'): |
|
platform_section = True |
|
continue |
|
elif line.startswith('!Platform_table_end'): |
|
platform_section = False |
|
continue |
|
if platform_section: |
|
gene_mapping_lines.append(line) |
|
|
|
|
|
mapping_data = pd.read_csv(io.StringIO(''.join(gene_mapping_lines)), sep='\t') |
|
|
|
mapping_data = mapping_data[['ID', 'Symbol']].copy() |
|
mapping_data = mapping_data.dropna(subset=['Symbol']) |
|
mapping_data = mapping_data[mapping_data['Symbol'].str.strip() != ''] |
|
mapping_data = mapping_data.rename(columns={'Symbol': 'Gene'}) |
|
|
|
|
|
gene_data = apply_gene_mapping(genetic_data, mapping_data) |
|
|
|
|
|
gene_data = normalize_gene_symbols_in_index(gene_data) |
|
print("\nGene data shape (after normalization):", gene_data.shape) |
|
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True) |
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
|
|
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 liver disease patients and controls, with proper mapping to standardized gene symbols." |
|
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) |
|
|
|
gene_annotation = get_gene_annotation(soft_file_path) |
|
|
|
|
|
mapping_data = gene_annotation[['ID', 'Symbol']].copy() |
|
mapping_data = mapping_data.dropna() |
|
mapping_data = mapping_data.rename(columns={'Symbol': 'Gene'}) |
|
|
|
|
|
gene_data = apply_gene_mapping(genetic_data, mapping_data) |
|
|
|
|
|
print("\nFirst few rows of mapped gene expression data:") |
|
print(preview_df(gene_data.head())) |
|
|
|
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) |