|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Arrhythmia" |
|
cohort = "GSE53622" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Arrhythmia" |
|
in_cohort_dir = "../DATA/GEO/Arrhythmia/GSE53622" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Arrhythmia/GSE53622.csv" |
|
out_gene_data_file = "./output/preprocess/3/Arrhythmia/gene_data/GSE53622.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Arrhythmia/clinical_data/GSE53622.csv" |
|
json_path = "./output/preprocess/3/Arrhythmia/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) |
|
|
|
|
|
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 = 10 |
|
age_row = 1 |
|
gender_row = 2 |
|
|
|
|
|
def convert_trait(x): |
|
|
|
if not isinstance(x, str): |
|
return None |
|
value = x.split(': ')[-1].lower() |
|
if value == 'no': |
|
return 0 |
|
elif value == 'yes': |
|
return 1 |
|
return None |
|
|
|
def convert_age(x): |
|
|
|
if not isinstance(x, str): |
|
return None |
|
try: |
|
return float(x.split(': ')[-1]) |
|
except: |
|
return None |
|
|
|
def convert_gender(x): |
|
|
|
if not isinstance(x, str): |
|
return None |
|
value = x.split(': ')[-1].lower() |
|
if value == 'female': |
|
return 0 |
|
elif value == 'male': |
|
return 1 |
|
return 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: |
|
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 |
|
) |
|
|
|
|
|
print("Preview of extracted clinical features:") |
|
print(preview_df(selected_clinical)) |
|
|
|
|
|
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 |
|
|
|
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
|
|
|
gene_annotation = get_gene_annotation(soft_file) |
|
|
|
|
|
print("Platform Information:") |
|
with gzip.open(soft_file, 'rt') as f: |
|
for line in f: |
|
if '!Platform_title' in line or '!Platform_geo_accession' in line: |
|
print(line.strip()) |
|
|
|
|
|
import urllib.request |
|
import io |
|
|
|
|
|
platform_url = "https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?mode=raw&is_datatable=true&acc=GPL18109&id=18415&db=GeoDb_blob118" |
|
response = urllib.request.urlopen(platform_url) |
|
platform_data = response.read().decode('utf-8') |
|
|
|
|
|
platform_df = pd.read_csv(io.StringIO(platform_data), sep='\t', comment='#') |
|
|
|
print("\nPlatform Annotation Preview:") |
|
print("Column names:", platform_df.columns.tolist()) |
|
print("\nFirst few rows as dictionary:") |
|
print(preview_df(platform_df)) |
|
|
|
|
|
gene_annotation = platform_df |
|
|
|
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
|
|
|
with gzip.open(soft_file, 'rt') as f: |
|
platform_content = f.read() |
|
|
|
|
|
table_start = platform_content.find("!platform_table_begin") |
|
table_end = platform_content.find("!platform_table_end") |
|
platform_table = platform_content[table_start:table_end] |
|
|
|
|
|
gene_annotation = pd.read_csv(io.StringIO(platform_table), sep='\t', skiprows=1) |
|
|
|
|
|
print("Available columns:", gene_annotation.columns.tolist()) |
|
print("\nSample rows:") |
|
print(gene_annotation.head()) |
|
|
|
|
|
mapping_df = get_gene_mapping(gene_annotation, prob_col='ID_REF', gene_col='GENE') |
|
|
|
|
|
gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df) |
|
|
|
|
|
print("\nShape of mapped gene expression data:", gene_data.shape) |
|
print("\nFirst few rows after mapping:") |
|
print(gene_data.head()) |
|
|
|
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
|
|
|
gene_annotation = get_gene_annotation(soft_file) |
|
|
|
|
|
print("Looking for gene annotation in file:", soft_file) |
|
print("\nGene Annotation Preview:") |
|
print("Number of rows:", len(gene_annotation)) |
|
print("Column names:", gene_annotation.columns.tolist()) |
|
|
|
|
|
|
|
print("\nNOTE: This dataset uses GPL18109 platform (Agilent-038314 CBC Homo sapiens lncRNA + mRNA microarray)") |
|
print("The complete gene mapping information is not available in the SOFT file.") |
|
print("Manual annotation retrieval from GEO is required for this platform.") |
|
|
|
|
|
raise ValueError("Gene mapping information not found in SOFT file. Manual annotation retrieval required for GPL18109.") |