|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Psoriasis" |
|
cohort = "GSE123088" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Psoriasis" |
|
in_cohort_dir = "../DATA/GEO/Psoriasis/GSE123088" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Psoriasis/GSE123088.csv" |
|
out_gene_data_file = "./output/preprocess/3/Psoriasis/gene_data/GSE123088.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Psoriasis/clinical_data/GSE123088.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 |
|
|
|
def convert_trait(value: str) -> Optional[float]: |
|
if not isinstance(value, str): |
|
return None |
|
parts = value.lower().split(': ') |
|
if len(parts) != 2: |
|
return None |
|
|
|
value = parts[1] |
|
if 'psoriasis' in value: |
|
return 1.0 |
|
elif 'control' in value or 'healthy_control' in value: |
|
return 0.0 |
|
return None |
|
|
|
|
|
age_row = 3 |
|
|
|
def convert_age(value: str) -> Optional[float]: |
|
if not isinstance(value, str): |
|
return None |
|
parts = value.split(': ') |
|
if len(parts) != 2: |
|
return None |
|
try: |
|
return float(parts[1]) |
|
except: |
|
return None |
|
|
|
|
|
gender_row = 2 |
|
|
|
def convert_gender(value: str) -> Optional[float]: |
|
if not isinstance(value, str): |
|
return None |
|
parts = value.lower().split(': ') |
|
if len(parts) != 2: |
|
return None |
|
value = parts[1] |
|
if 'female' in value: |
|
return 0.0 |
|
elif 'male' in value: |
|
return 1.0 |
|
return None |
|
|
|
|
|
is_usable = 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_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 |
|
) |
|
|
|
|
|
preview = preview_df(selected_clinical_df) |
|
print("Preview of selected clinical features:") |
|
print(preview) |
|
|
|
|
|
selected_clinical_df.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 |
|
|
|
gene_metadata = get_gene_annotation(soft_file) |
|
|
|
|
|
mapping_df = get_gene_mapping(gene_metadata, "ID", "ENTREZ_GENE_ID") |
|
|
|
|
|
print("Column names:", mapping_df.columns.tolist()) |
|
print("\nFirst few rows preview:") |
|
print(preview_df(mapping_df)) |
|
|
|
|
|
import gzip |
|
with gzip.open(soft_file, 'rt', encoding='utf-8') as f: |
|
annotation_preview = [] |
|
for i, line in enumerate(f): |
|
if line.startswith('!Platform_table_begin'): |
|
|
|
next(f) |
|
for _ in range(5): |
|
annotation_preview.append(next(f).strip()) |
|
break |
|
print("\nRaw annotation preview:") |
|
for line in annotation_preview: |
|
print(line) |
|
|
|
gene_metadata = get_gene_annotation(soft_file) |
|
|
|
|
|
print("Available annotation columns:") |
|
print(gene_metadata.columns.tolist()) |
|
print("\nPreview of first few rows:") |
|
for col in gene_metadata.columns: |
|
print(f"\n{col}:") |
|
print(gene_metadata[col].head()) |
|
|
|
|
|
mapping_df = pd.DataFrame() |
|
mapping_df['ID'] = gene_metadata['ID'].astype(str) |
|
mapping_df['Gene'] = gene_metadata['Gene Symbol'].astype(str) |
|
|
|
|
|
gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df) |
|
|
|
|
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
|
|
print(f"\nOriginal probe data shape: {gene_data.shape}") |
|
print("\nFirst few rows of mapped gene expression data:") |
|
print(gene_data.head()) |
|
|
|
import gzip |
|
platform_info_lines = [] |
|
with gzip.open(soft_file, 'rt', encoding='utf-8') as f: |
|
in_platform = False |
|
for line in f: |
|
if line.startswith('!Platform_table_begin'): |
|
in_platform = True |
|
|
|
platform_info_lines = [next(f).strip() for _ in range(5)] |
|
break |
|
|
|
|
|
header = platform_info_lines[0].split('\t') |
|
print("Platform table columns:", header) |
|
|
|
|
|
gene_metadata = get_gene_annotation(soft_file) |
|
|
|
|
|
mapping_df = pd.DataFrame() |
|
mapping_df['ID'] = gene_metadata['ID'].astype(str) |
|
|
|
with open("./metadata/gene_synonym.json", "r") as f: |
|
synonym_dict = json.load(f) |
|
mapping_df['Gene'] = gene_metadata['ENTREZ_GENE_ID'].astype(str).map(synonym_dict) |
|
|
|
|
|
mapping_df = mapping_df.dropna(subset=['Gene']) |
|
|
|
|
|
gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df) |
|
|
|
|
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
|
|
print(f"\nOriginal probe data shape: {gene_data.shape}") |
|
print("\nFirst few rows of mapped gene expression data:") |
|
print(gene_data.head()) |
|
|
|
|
|
|
|
|
|
selected_clinical_df = pd.read_csv(out_clinical_data_file, index_col=0) |
|
|
|
|
|
gene_data = pd.DataFrame(gene_data, dtype=float) |
|
gene_data.index = gene_data.index.astype(str) |
|
|
|
|
|
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, gene_data) |
|
|
|
|
|
linked_data = handle_missing_values(linked_data, trait) |
|
|
|
|
|
is_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=is_biased, |
|
df=linked_data, |
|
note="Contains numerical probe-level expression data (gene mapping failed) and clinical data." |
|
) |
|
|
|
|
|
if is_usable: |
|
linked_data.to_csv(out_data_file) |