|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Psoriasis" |
|
cohort = "GSE183134" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Psoriasis" |
|
in_cohort_dir = "../DATA/GEO/Psoriasis/GSE183134" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Psoriasis/GSE183134.csv" |
|
out_gene_data_file = "./output/preprocess/3/Psoriasis/gene_data/GSE183134.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Psoriasis/clinical_data/GSE183134.csv" |
|
json_path = "./output/preprocess/3/Psoriasis/cohort_info.json" |
|
|
|
|
|
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
|
|
|
background_info, clinical_data = get_background_and_clinical_data( |
|
matrix_file, |
|
prefixes_a=['!Series_title', '!Series_summary', '!Series_overall_design'], |
|
prefixes_b=['!Sample_geo_accession', '!Sample_characteristics_ch1'] |
|
) |
|
|
|
|
|
sample_characteristics = get_unique_values_by_row(clinical_data) |
|
|
|
|
|
print("Dataset Background Information:") |
|
print(f"{background_info}\n") |
|
|
|
|
|
print("Sample Characteristics:") |
|
for feature, values in sample_characteristics.items(): |
|
print(f"Feature: {feature}") |
|
print(f"Values: {values}\n") |
|
|
|
|
|
is_gene_available = True |
|
|
|
|
|
|
|
trait_row = 1 |
|
age_row = None |
|
gender_row = None |
|
|
|
|
|
def convert_trait(value: str) -> int: |
|
"""Convert trait value to binary (0: not trait, 1: has trait)""" |
|
if not isinstance(value, str): |
|
return None |
|
value = value.split(': ')[-1].strip() |
|
if value == 'Psoriasis': |
|
return 1 |
|
elif value == 'Pityriasis_Rubra_Pilaris': |
|
return 0 |
|
return None |
|
|
|
def convert_age(value: str) -> float: |
|
"""Convert age value to float (Not used since age not available)""" |
|
return None |
|
|
|
def convert_gender(value: str) -> int: |
|
"""Convert gender value to binary (Not used since gender not available)""" |
|
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) |
|
|
|
|
|
|
|
selected_clinical = geo_select_clinical_features(clinical_df=clinical_data, |
|
trait=trait, |
|
trait_row=trait_row, |
|
convert_trait=convert_trait) |
|
|
|
|
|
print("Preview of extracted clinical features:") |
|
print(preview_df(selected_clinical)) |
|
|
|
|
|
os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True) |
|
selected_clinical.to_csv(out_clinical_data_file) |
|
|
|
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
|
|
|
gene_data = get_genetic_data(matrix_file) |
|
|
|
|
|
print("Shape of gene expression data:", gene_data.shape) |
|
print("\nFirst few rows of data:") |
|
print(gene_data.head()) |
|
print("\nFirst 20 gene/probe identifiers:") |
|
print(gene_data.index[:20]) |
|
|
|
|
|
import gzip |
|
with gzip.open(matrix_file, 'rt', encoding='utf-8') as f: |
|
lines = [] |
|
for i, line in enumerate(f): |
|
if "!series_matrix_table_begin" in line: |
|
|
|
for _ in range(5): |
|
lines.append(next(f).strip()) |
|
break |
|
print("\nFirst few lines after matrix marker in raw file:") |
|
for line in lines: |
|
print(line) |
|
|
|
|
|
|
|
requires_gene_mapping = True |
|
|
|
import gzip |
|
print("Preview of platform data section:") |
|
with gzip.open(soft_file, 'rt', encoding='utf-8') as f: |
|
in_platform_section = False |
|
for line in f: |
|
if '!platform_table_begin' in line: |
|
in_platform_section = True |
|
|
|
next(f) |
|
|
|
for _ in range(10): |
|
print(next(f).strip()) |
|
break |
|
|
|
|
|
def extract_platform_data(file_path): |
|
data_lines = [] |
|
with gzip.open(file_path, 'rt') as f: |
|
in_platform_section = False |
|
for line in f: |
|
if '!platform_table_begin' in line: |
|
in_platform_section = True |
|
|
|
next(f) |
|
continue |
|
if '!platform_table_end' in line: |
|
break |
|
if in_platform_section: |
|
data_lines.append(line.strip()) |
|
|
|
|
|
import io |
|
df = pd.read_csv(io.StringIO('\n'.join(data_lines)), delimiter='\t') |
|
return df |
|
|
|
gene_metadata = extract_platform_data(soft_file) |
|
|
|
|
|
print("\nColumn names:", gene_metadata.columns.tolist()) |
|
print("\nFirst few rows preview:") |
|
print(preview_df(gene_metadata)) |
|
|
|
print("Examining SOFT file content for probe mapping:") |
|
with gzip.open(soft_file, 'rt', encoding='utf-8') as f: |
|
in_platform_section = False |
|
header_found = False |
|
platform_data = [] |
|
for line in f: |
|
if line.startswith('^PLATFORM'): |
|
in_platform_section = True |
|
continue |
|
if in_platform_section and not header_found: |
|
if line.startswith('#') and 'ID' in line: |
|
|
|
print(line.strip()) |
|
header_found = True |
|
if in_platform_section and line.startswith('!platform_table_begin'): |
|
header = next(f).strip().split('\t') |
|
|
|
for data_line in f: |
|
if data_line.startswith('!platform_table_end'): |
|
break |
|
platform_data.append(data_line.strip().split('\t')) |
|
break |
|
|
|
|
|
gene_metadata = pd.DataFrame(platform_data, columns=header) |
|
|
|
print("\nColumn names of platform annotation:", gene_metadata.columns.tolist()) |
|
print("\nFirst few rows of platform annotation:") |
|
print(preview_df(gene_metadata)) |
|
|
|
|
|
print("\nExpression data IDs (first 5):", gene_data.index[:5].tolist()) |
|
|
|
|
|
|
|
if 'Gene Symbol' in gene_metadata.columns: |
|
mapping_df = get_gene_mapping(gene_metadata, prob_col='ID', gene_col='Gene Symbol') |
|
else: |
|
print("\nWARNING: Could not find gene symbol mapping in platform annotation.") |
|
print("Using expression data IDs directly as gene symbols.") |
|
|
|
mapping_df = pd.DataFrame({ |
|
'ID': gene_data.index, |
|
'Gene': gene_data.index |
|
}) |
|
|
|
|
|
gene_data = apply_gene_mapping(gene_data, mapping_df) |
|
|
|
|
|
print("\nShape of gene expression data after mapping:", gene_data.shape) |
|
print("\nFirst few genes and their expression values:") |
|
print(gene_data.head()) |
|
|
|
|
|
gene_data = normalize_gene_symbols_in_index(gene_data) |
|
|
|
print("\nShape of gene expression data after normalization:", gene_data.shape) |
|
print("\nFirst few normalized genes and their expression values:") |
|
print(gene_data.head()) |
|
|
|
|
|
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True) |
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
clinical_data = pd.read_csv(out_clinical_data_file, index_col=0) |
|
|
|
|
|
_, matrix_file = geo_get_relevant_filepaths(in_cohort_dir) |
|
gene_data = get_genetic_data(matrix_file) |
|
|
|
|
|
mapping_df = pd.DataFrame({ |
|
'ID': gene_data.index, |
|
'Gene': gene_data.index |
|
}) |
|
|
|
|
|
gene_data = apply_gene_mapping(gene_data, mapping_df) |
|
|
|
|
|
linked_data = geo_link_clinical_genetic_data(clinical_data, 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="Contains both gene expression data and clinical information. Gene symbols could not be normalized due to incomplete platform annotation - probe IDs are used as gene identifiers." |
|
) |
|
|
|
|
|
if is_usable: |
|
os.makedirs(os.path.dirname(out_data_file), exist_ok=True) |
|
linked_data.to_csv(out_data_file) |