|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Liver_cirrhosis" |
|
cohort = "GSE185529" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Liver_cirrhosis" |
|
in_cohort_dir = "../DATA/GEO/Liver_cirrhosis/GSE185529" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Liver_cirrhosis/GSE185529.csv" |
|
out_gene_data_file = "./output/preprocess/3/Liver_cirrhosis/gene_data/GSE185529.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Liver_cirrhosis/clinical_data/GSE185529.csv" |
|
json_path = "./output/preprocess/3/Liver_cirrhosis/cohort_info.json" |
|
|
|
|
|
soft_file_path, matrix_file_path = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
|
|
|
with gzip.open(soft_file_path, 'rt') as f: |
|
soft_content = f.read() |
|
|
|
|
|
subseries_match = re.search(r'!Series_relation = SuperSeries of: (GSE\d+)', soft_content) |
|
if subseries_match: |
|
subseries_id = subseries_match.group(1) |
|
subseries_files = [f for f in os.listdir(in_cohort_dir) if subseries_id in f] |
|
if subseries_files: |
|
subseries_soft = [f for f in subseries_files if 'soft' in f.lower()][0] |
|
subseries_matrix = [f for f in subseries_files if 'matrix' in f.lower()][0] |
|
soft_file_path = os.path.join(in_cohort_dir, subseries_soft) |
|
matrix_file_path = os.path.join(in_cohort_dir, subseries_matrix) |
|
|
|
|
|
background_info, clinical_data = get_background_and_clinical_data(soft_file_path) |
|
|
|
if len(clinical_data.columns) <= 2: |
|
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("-" * 80) |
|
print(background_info) |
|
print("\nSample Characteristics:") |
|
print("-" * 80) |
|
print(json.dumps(unique_values_dict, indent=2)) |
|
|
|
is_gene_available = True |
|
|
|
|
|
trait_row = None |
|
age_row = None |
|
gender_row = None |
|
|
|
|
|
|
|
def convert_trait(x): |
|
if x is None: |
|
return None |
|
value = x.split(': ')[1].lower() if ': ' in x else x.lower() |
|
|
|
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=False |
|
) |
|
|
|
|
|
|
|
genetic_data = get_genetic_data(matrix_file_path) |
|
|
|
|
|
print("First 20 gene/probe identifiers:") |
|
print(genetic_data.index[:20]) |
|
|
|
|
|
|
|
|
|
requires_gene_mapping = True |
|
|
|
gene_annotation = get_gene_annotation(soft_file_path) |
|
|
|
|
|
print("Column names and first few values in gene annotation data:") |
|
print(preview_df(gene_annotation)) |
|
|
|
mapping_data = gene_annotation[['probeset_id', 'gene_assignment']].copy() |
|
mapping_data = mapping_data.rename(columns={'probeset_id': 'ID', 'gene_assignment': 'Gene'}) |
|
mapping_data = mapping_data.astype({'ID': 'str'}) |
|
|
|
|
|
mapping_data['Gene'] = mapping_data['Gene'].apply(lambda x: re.search(r'//\s*(\w+)\s*//', str(x)).group(1) if pd.notnull(x) and '//' in str(x) else None) |
|
mapping_data = mapping_data.dropna() |
|
|
|
|
|
gene_data = apply_gene_mapping(genetic_data, mapping_data) |
|
|
|
|
|
print("\nFirst few gene identifiers after mapping:") |
|
print(gene_data.index[:20]) |
|
|
|
mapping_data = gene_annotation[['ID', 'gene_assignment']].copy() |
|
mapping_data['ID'] = mapping_data['ID'].astype(str) + '_st' |
|
|
|
def extract_gene(text): |
|
if pd.isna(text) or '//' not in str(text): |
|
return None |
|
matches = re.findall(r'//\s*(\w+)\s*//', str(text)) |
|
if matches: |
|
|
|
return matches[0].upper() |
|
return None |
|
|
|
mapping_data['Gene'] = mapping_data['gene_assignment'].apply(extract_gene) |
|
mapping_data = mapping_data[['ID', 'Gene']].dropna() |
|
|
|
|
|
gene_data = apply_gene_mapping(genetic_data, mapping_data) |
|
|
|
|
|
gene_data = normalize_gene_symbols_in_index(gene_data) |
|
|
|
|
|
print("\nFirst few gene identifiers after mapping:") |
|
print(gene_data.index[:20]) |