|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Melanoma" |
|
cohort = "GSE144296" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Melanoma" |
|
in_cohort_dir = "../DATA/GEO/Melanoma/GSE144296" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Melanoma/GSE144296.csv" |
|
out_gene_data_file = "./output/preprocess/3/Melanoma/gene_data/GSE144296.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Melanoma/clinical_data/GSE144296.csv" |
|
json_path = "./output/preprocess/3/Melanoma/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("Dataset Background Information:") |
|
print(background_info) |
|
print("\nSample Characteristics:") |
|
for feature, values in unique_values_dict.items(): |
|
print(f"\n{feature}:") |
|
print(values) |
|
|
|
|
|
is_gene_available = True |
|
|
|
|
|
|
|
trait_row = 1 |
|
|
|
age_row = None |
|
|
|
gender_row = None |
|
|
|
|
|
def convert_trait(x): |
|
"""Convert cell type to binary melanoma indicator""" |
|
if not isinstance(x, str): |
|
return None |
|
x = x.lower().split(': ')[-1] |
|
if 'melanoma' in x: |
|
return 1 |
|
elif 'colorectal' in x: |
|
return 0 |
|
return None |
|
|
|
def convert_age(x): |
|
"""Placeholder for age conversion""" |
|
return None |
|
|
|
def convert_gender(x): |
|
"""Placeholder for gender conversion""" |
|
return None |
|
|
|
|
|
validate_and_save_cohort_info( |
|
is_final=False, |
|
cohort=cohort, |
|
info_path=json_path, |
|
is_gene_available=is_gene_available, |
|
is_trait_available=trait_row is not None |
|
) |
|
|
|
|
|
if trait_row is not None: |
|
selected_clinical = 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_df(selected_clinical) |
|
|
|
|
|
selected_clinical.to_csv(out_clinical_data_file) |
|
|
|
genetic_data = get_genetic_data(matrix_file_path, marker="!series_matrix_table_begin".lower()) |
|
|
|
|
|
if len(genetic_data.index) == 0: |
|
|
|
genetic_data = get_genetic_data(matrix_file_path, marker="!Series_Matrix_Table_Begin") |
|
|
|
if len(genetic_data.index) == 0: |
|
print("Warning: No data was extracted from the matrix file. Please check the matrix file formatting.") |
|
is_gene_available = False |
|
else: |
|
print("First 20 row IDs:") |
|
print(list(genetic_data.index)[:20]) |
|
is_gene_available = True |
|
|
|
|
|
validate_and_save_cohort_info( |
|
is_final=False, |
|
cohort=cohort, |
|
info_path=json_path, |
|
is_gene_available=is_gene_available, |
|
is_trait_available=(trait_row is not None) |
|
) |
|
|
|
if is_gene_available: |
|
genetic_data.to_csv(out_gene_data_file) |
|
|
|
with gzip.open(soft_file_path, 'rt') as f: |
|
print("\nSample of unfiltered SOFT file content (first 20 lines):") |
|
for i, line in enumerate(f): |
|
if i < 20: |
|
print(line.strip()) |
|
elif i == 20: |
|
print("...") |
|
break |
|
|
|
|
|
with gzip.open(matrix_file_path, 'rt') as f: |
|
print("\nSample of matrix file content (first 20 lines):") |
|
for i, line in enumerate(f): |
|
if i < 20: |
|
print(line.strip()) |
|
elif i == 20: |
|
print("...") |
|
break |
|
|
|
|
|
probe_info_found = False |
|
with gzip.open(matrix_file_path, 'rt') as f: |
|
lines = [] |
|
for line in f: |
|
if line.startswith('!Platform_organism'): |
|
probe_info_found = True |
|
lines.append(line) |
|
elif probe_info_found and line.startswith('!'): |
|
lines.append(line) |
|
elif probe_info_found and not any(line.startswith(p) for p in ['!', '#', '^']): |
|
break |
|
gene_metadata_text = '\n'.join(lines) |
|
|
|
print("\nExtracted probe/gene information:") |
|
print(gene_metadata_text) |
|
|
|
genetic_data = get_genetic_data(matrix_file_path) |
|
|
|
|
|
genetic_data.index = genetic_data.index.astype(str) |
|
|
|
|
|
print("\nSample identifiers from genetic data:") |
|
print(list(genetic_data.index)[:5]) |
|
|
|
|
|
try: |
|
with gzip.open(matrix_file_path, 'rt') as f: |
|
for line in f: |
|
if '!series_matrix_table_begin' in line.lower(): |
|
|
|
break |
|
if line.startswith('!Sample_platform_id'): |
|
|
|
platform_line = line.strip() |
|
|
|
|
|
ids = genetic_data.index.tolist() |
|
annotation_df = pd.DataFrame({ |
|
'ID': ids, |
|
'Gene': ids |
|
}) |
|
|
|
print("\nSample rows from annotation mapping:") |
|
print(annotation_df.head()) |
|
|
|
|
|
gene_data = apply_gene_mapping(genetic_data, annotation_df) |
|
|
|
|
|
gene_data.index = gene_data.index.astype(str) |
|
|
|
|
|
gene_data = normalize_gene_symbols_in_index(gene_data) |
|
|
|
print("\nFinal gene data shape:", gene_data.shape) |
|
print("Sample gene names after normalization:") |
|
print(list(gene_data.index)[:5]) |
|
|
|
|
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
except Exception as e: |
|
print(f"Error during gene mapping: {str(e)}") |
|
|
|
genetic_data.to_csv(out_gene_data_file) |
|
|
|
with gzip.open(matrix_file_path, 'rt') as f: |
|
lines = f.readlines() |
|
|
|
start_idx = None |
|
end_idx = None |
|
for i, line in enumerate(lines): |
|
if '!series_matrix_table_begin' in line.lower(): |
|
start_idx = i + 1 |
|
elif '!series_matrix_table_end' in line.lower(): |
|
end_idx = i |
|
break |
|
|
|
genetic_data = None |
|
if start_idx and end_idx: |
|
|
|
genetic_data = pd.read_csv(io.StringIO(''.join(lines[start_idx:end_idx])), |
|
sep='\t', index_col=0) |
|
|
|
|
|
if genetic_data is not None and len(genetic_data) > 0: |
|
print("\nFirst 20 row IDs:") |
|
print(list(genetic_data.index)[:20]) |
|
is_gene_available = True |
|
else: |
|
print("\nWarning: No gene expression data could be extracted") |
|
is_gene_available = False |
|
|
|
|
|
validate_and_save_cohort_info( |
|
is_final=False, |
|
cohort=cohort, |
|
info_path=json_path, |
|
is_gene_available=is_gene_available, |
|
is_trait_available=(trait_row is not None) |
|
) |
|
|
|
if is_gene_available: |
|
genetic_data.to_csv(out_gene_data_file) |