|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Obesity" |
|
cohort = "GSE123086" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Obesity" |
|
in_cohort_dir = "../DATA/GEO/Obesity/GSE123086" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Obesity/GSE123086.csv" |
|
out_gene_data_file = "./output/preprocess/3/Obesity/gene_data/GSE123086.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Obesity/clinical_data/GSE123086.csv" |
|
json_path = "./output/preprocess/3/Obesity/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 = 1 |
|
|
|
|
|
age_row = 3 |
|
|
|
|
|
gender_row = 2 |
|
|
|
|
|
def convert_trait(x): |
|
|
|
if pd.isna(x): |
|
return None |
|
value = x.split(': ')[1] |
|
if value == 'OBESITY': |
|
return 1 |
|
elif value == 'HEALTHY_CONTROL': |
|
return 0 |
|
return None |
|
|
|
def convert_age(x): |
|
|
|
if pd.isna(x): |
|
return None |
|
if not x.startswith('age:'): |
|
return None |
|
try: |
|
return float(x.split(': ')[1]) |
|
except: |
|
return None |
|
|
|
def convert_gender(x): |
|
|
|
if pd.isna(x): |
|
return None |
|
if not x.startswith('Sex:'): |
|
return None |
|
value = x.split(': ')[1] |
|
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: |
|
clinical_features = geo_select_clinical_features( |
|
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(clinical_features)) |
|
clinical_features.to_csv(out_clinical_data_file) |
|
|
|
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 = pd.read_csv(soft_file, compression='gzip', sep='\t', header=None, |
|
comment='!', on_bad_lines='skip') |
|
|
|
|
|
annotation_starts = False |
|
annotation_lines = [] |
|
with gzip.open(soft_file, 'rt', encoding='utf-8') as f: |
|
for line in f: |
|
if "!platform_table_begin" in line: |
|
annotation_starts = True |
|
|
|
header = next(f).strip() |
|
annotation_lines.append(header) |
|
continue |
|
if annotation_starts: |
|
if "!platform_table_end" in line: |
|
break |
|
annotation_lines.append(line.strip()) |
|
|
|
|
|
annotation_content = '\n'.join(annotation_lines) |
|
gene_annotation = pd.read_csv(io.StringIO(annotation_content), sep='\t') |
|
|
|
|
|
print("Gene annotation shape:", gene_annotation.shape) |
|
print("\nAll column names:") |
|
print(gene_annotation.columns.tolist()) |
|
|
|
print("\nFirst few rows:") |
|
print(gene_annotation.head().to_string()) |
|
|
|
print("\nNumber of non-null values in each column:") |
|
print(gene_annotation.count()) |
|
|
|
|
|
print("\nSample rows showing the ID and gene symbol mapping:") |
|
symbol_col = [col for col in gene_annotation.columns if 'symbol' in col.lower()][0] |
|
print(gene_annotation[['ID', symbol_col]].head(10)) |
|
|
|
platform_lines = [] |
|
annotation_starts = False |
|
with gzip.open(soft_file, 'rt', encoding='utf-8') as f: |
|
for line in f: |
|
if line.startswith('^PLATFORM'): |
|
platform_lines.append(line) |
|
elif line.startswith('!Platform_data_row_count'): |
|
platform_lines.append(line) |
|
elif line.startswith('!Platform_table_begin'): |
|
annotation_starts = True |
|
header = next(f).strip() |
|
platform_lines.append(header) |
|
continue |
|
elif annotation_starts: |
|
if line.startswith('!Platform_table_end'): |
|
break |
|
platform_lines.append(line.strip()) |
|
|
|
|
|
platform_content = '\n'.join(platform_lines) |
|
gene_annotation = pd.read_csv(io.StringIO(platform_content), sep='\t', comment='!', skiprows=2) |
|
|
|
|
|
mapping_df = gene_annotation[['ID', 'GB_ACC']].copy() |
|
mapping_df = mapping_df.rename(columns={'GB_ACC': 'Gene'}) |
|
|
|
|
|
mapping_df['Gene'] = mapping_df['Gene'].astype(str).apply(lambda x: x.split(';')[0].split()[0]) |
|
mapping_df = mapping_df.dropna() |
|
|
|
|
|
gene_data = apply_gene_mapping(gene_data, mapping_df) |
|
|
|
|
|
gene_data = normalize_gene_symbols_in_index(gene_data) |
|
|
|
|
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
|
|
print("Shape of gene expression data after mapping:", gene_data.shape) |
|
print("\nFirst few mapped genes and their expression values:") |
|
print(gene_data.head()) |
|
|
|
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
|
|
|
with gzip.open(soft_file, 'rt', encoding='utf-8') as f: |
|
|
|
print("First 20 lines of SOFT file:") |
|
for i, line in enumerate(f): |
|
if i < 20: |
|
print(line.strip()) |
|
else: |
|
break |
|
|
|
|
|
f.seek(0) |
|
|
|
|
|
print("\nPlatform table header:") |
|
for line in f: |
|
if "!Platform_table_begin" in line: |
|
|
|
print(next(f).strip()) |
|
break |
|
|
|
|
|
annotation_lines = [] |
|
table_started = False |
|
with gzip.open(soft_file, 'rt', encoding='utf-8') as f: |
|
for line in f: |
|
if "!Platform_table_begin" in line: |
|
header = next(f).strip() |
|
annotation_lines.append(header) |
|
table_started = True |
|
continue |
|
if table_started: |
|
if "!Platform_table_end" in line: |
|
break |
|
annotation_lines.append(line.strip()) |
|
|
|
|
|
annotation_text = '\n'.join(annotation_lines) |
|
gene_annotation = pd.read_csv(io.StringIO(annotation_text), sep='\t') |
|
|
|
|
|
print("\nAnnotation data shape:", gene_annotation.shape) |
|
print("\nColumn names:") |
|
print(gene_annotation.columns.tolist()) |
|
print("\nFirst few rows:") |
|
print(gene_annotation.head()) |
|
|
|
with gzip.open(soft_file, 'rt', encoding='utf-8') as f: |
|
for line in f: |
|
if '!Platform_table_begin' in line: |
|
header = next(f).strip() |
|
print("Platform table header:") |
|
print(header) |
|
print("\nFirst few data rows:") |
|
for i in range(5): |
|
print(next(f).strip()) |
|
break |
|
|
|
|
|
gene_metadata = get_gene_annotation(soft_file) |
|
|
|
|
|
print("\nAvailable annotation columns:") |
|
print(gene_metadata.columns.tolist()) |
|
|
|
print("\nPreview of gene metadata:") |
|
print(gene_metadata.head()) |
|
|
|
|
|
gene_data = get_genetic_data(matrix_file) |
|
|
|
|
|
print("\nShape of gene expression data before mapping:", gene_data.shape) |
|
print("\nPreview of gene expression data before mapping:") |
|
print(gene_data.head()) |
|
|
|
gene_metadata = get_gene_annotation(soft_file) |
|
|
|
|
|
mapping_df = gene_metadata[['ID', 'ENTREZ_GENE_ID']].copy() |
|
mapping_df = mapping_df.rename(columns={'ENTREZ_GENE_ID': 'Gene'}) |
|
|
|
|
|
mapping_df['ID'] = mapping_df['ID'].astype(str) |
|
mapping_df['Gene'] = mapping_df['Gene'].astype(str) |
|
mapping_df = mapping_df.dropna() |
|
|
|
|
|
gene_data = apply_gene_mapping(gene_data, mapping_df) |
|
|
|
|
|
gene_data = normalize_gene_symbols_in_index(gene_data) |
|
|
|
|
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
print("\nShape of gene expression data after mapping:", gene_data.shape) |
|
print("\nFirst few mapped genes and their expression values:") |
|
print(gene_data.head()) |
|
|
|
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
|
|
|
gene_annotation = get_gene_annotation(soft_file) |
|
|
|
|
|
print("Gene annotation shape:", gene_annotation.shape) |
|
print("\nGene annotation preview:") |
|
print(preview_df(gene_annotation)) |
|
|
|
print("\nNumber of non-null values in each column:") |
|
print(gene_annotation.count()) |
|
|
|
|
|
print("\nSample mapping columns ('ID' and 'GENE_SYMBOL'):") |
|
print("\nFirst 5 rows:") |
|
print(gene_annotation[['ID', 'GENE_SYMBOL']].head().to_string()) |
|
|
|
print("\nNote: Gene mapping will use:") |
|
print("'ID' column: Probe identifiers") |
|
print("'GENE_SYMBOL' column: Contains gene symbol information") |