|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Intellectual_Disability" |
|
cohort = "GSE158385" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Intellectual_Disability" |
|
in_cohort_dir = "../DATA/GEO/Intellectual_Disability/GSE158385" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Intellectual_Disability/GSE158385.csv" |
|
out_gene_data_file = "./output/preprocess/3/Intellectual_Disability/gene_data/GSE158385.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Intellectual_Disability/clinical_data/GSE158385.csv" |
|
json_path = "./output/preprocess/3/Intellectual_Disability/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 = None |
|
|
|
|
|
def convert_trait(value): |
|
if pd.isna(value): |
|
return None |
|
value = value.split(': ')[-1].strip() |
|
if '47' in value and 'T21' in value: |
|
return 1 |
|
elif '46' in value and '2N' in value: |
|
return 0 |
|
return None |
|
|
|
convert_age = 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) |
|
|
|
|
|
if trait_row is not None: |
|
clinical_features = geo_select_clinical_features(clinical_data, |
|
trait=trait, |
|
trait_row=trait_row, |
|
convert_trait=convert_trait) |
|
print("Preview of clinical features:") |
|
print(preview_df(clinical_features)) |
|
|
|
clinical_features.to_csv(out_clinical_data_file) |
|
|
|
genetic_data = get_genetic_data(matrix_file_path) |
|
|
|
|
|
print("First 20 row IDs:") |
|
print(genetic_data.index[:20].tolist()) |
|
|
|
|
|
requires_gene_mapping = True |
|
|
|
with gzip.open(soft_file_path, 'rt') as f: |
|
platform_sections = [] |
|
current_platform = None |
|
for line in f: |
|
if line.startswith('^PLATFORM'): |
|
if current_platform: |
|
platform_sections.append(current_platform) |
|
current_platform = {'id': line.strip()} |
|
elif current_platform is not None and line.startswith('!Platform_title'): |
|
current_platform['title'] = line.strip() |
|
if 'human' in line.lower() or 'homo sapiens' in line.lower(): |
|
current_platform['is_human'] = True |
|
elif not line.startswith('^'): |
|
if current_platform: |
|
platform_sections.append(current_platform) |
|
current_platform = None |
|
if current_platform: |
|
platform_sections.append(current_platform) |
|
|
|
print("Found Platform Sections:") |
|
for platform in platform_sections: |
|
print(platform) |
|
|
|
|
|
with gzip.open(soft_file_path, 'rt') as f: |
|
human_data = [] |
|
is_human_section = False |
|
for line in f: |
|
if line.startswith('^PLATFORM'): |
|
is_human_section = False |
|
platform_id = line.strip() |
|
elif line.startswith('!Platform_title') and ('human' in line.lower() or 'homo sapiens' in line.lower()): |
|
is_human_section = True |
|
print(f"\nFound human platform section: {platform_id}") |
|
print(f"Platform title: {line.strip()}") |
|
elif is_human_section and not line.startswith(('!', '#', '^')): |
|
human_data.append(line) |
|
|
|
if human_data: |
|
|
|
human_annotation_df = pd.read_csv(io.StringIO(''.join(human_data)), sep='\t') |
|
|
|
print("\nColumn names:") |
|
print(human_annotation_df.columns.tolist()) |
|
|
|
print("\nData shape:", human_annotation_df.shape) |
|
|
|
print("\nPreview of the annotation data:") |
|
print(json.dumps(preview_df(human_annotation_df), indent=2)) |
|
else: |
|
print("\nNo human gene annotation data found in the SOFT file.") |
|
|
|
prob_col = 'ID' |
|
gene_col = 'gene_assignment' |
|
|
|
|
|
mapping_df = get_gene_mapping(human_annotation_df, prob_col, gene_col) |
|
|
|
|
|
gene_data = apply_gene_mapping(genetic_data, mapping_df) |
|
|
|
|
|
gene_data = normalize_gene_symbols_in_index(gene_data) |
|
|
|
|
|
print("\nGene expression data shape:", gene_data.shape) |
|
print("\nFirst few gene symbols:", gene_data.index[:5].tolist()) |
|
|
|
|
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
gene_data = normalize_gene_symbols_in_index(gene_data) |
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
linked_data = geo_link_clinical_genetic_data(clinical_features, gene_data) |
|
|
|
|
|
linked_data = handle_missing_values(linked_data, trait) |
|
|
|
|
|
if linked_data[trait].isna().all(): |
|
is_biased = True |
|
linked_data = None |
|
else: |
|
|
|
is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait) |
|
|
|
|
|
note = "Dataset contains gene expression data from pediatric AML samples, focusing on Down syndrome cases versus other AML types." |
|
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_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) |