|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Epilepsy" |
|
cohort = "GSE199759" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Epilepsy" |
|
in_cohort_dir = "../DATA/GEO/Epilepsy/GSE199759" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Epilepsy/GSE199759.csv" |
|
out_gene_data_file = "./output/preprocess/3/Epilepsy/gene_data/GSE199759.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Epilepsy/clinical_data/GSE199759.csv" |
|
json_path = "./output/preprocess/3/Epilepsy/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) |
|
|
|
|
|
unique_values_dict = get_unique_values_by_row(clinical_data) |
|
|
|
|
|
print("=== Dataset Background Information ===") |
|
print(background_info) |
|
print("\n=== Sample Characteristics ===") |
|
print(json.dumps(unique_values_dict, indent=2)) |
|
|
|
is_gene_available = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
trait_row = None |
|
gender_row = 1 |
|
age_row = 2 |
|
|
|
|
|
def convert_trait(value): |
|
|
|
return None |
|
|
|
def convert_gender(value): |
|
|
|
if not value or ':' not in value: |
|
return None |
|
gender = value.split(':')[1].strip().lower() |
|
if gender == 'female': |
|
return 0 |
|
elif gender == 'male': |
|
return 1 |
|
return None |
|
|
|
def convert_age(value): |
|
|
|
if not value or ':' not in value: |
|
return None |
|
try: |
|
age = value.split(':')[1].strip() |
|
return float(age.replace('y', '')) |
|
except: |
|
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_df = get_genetic_data(matrix_file) |
|
|
|
|
|
print("DataFrame shape:", genetic_df.shape) |
|
print("\nFirst 20 row IDs:") |
|
print(genetic_df.index[:20]) |
|
|
|
print("\nPreview of first few rows and columns:") |
|
print(genetic_df.head().iloc[:, :5]) |
|
|
|
|
|
requires_gene_mapping = True |
|
|
|
from io import StringIO |
|
import gzip |
|
|
|
|
|
mRNA_annotation = [] |
|
in_mRNA_platform = False |
|
with gzip.open(soft_file, 'rt') as f: |
|
for line in f: |
|
|
|
if "!Platform_title" in line and "LncRNA+mRNA" in line: |
|
in_mRNA_platform = True |
|
|
|
if in_mRNA_platform and not line.startswith(("^", "!", "#")): |
|
mRNA_annotation.append(line) |
|
|
|
if in_mRNA_platform and line.startswith("^Platform"): |
|
break |
|
|
|
|
|
annotation_text = ''.join(mRNA_annotation) |
|
gene_metadata = pd.read_csv(StringIO(annotation_text), sep='\t', low_memory=False) |
|
|
|
|
|
print("Column names:") |
|
print(gene_metadata.columns) |
|
print("\nPreview of gene annotation data:") |
|
print(preview_df(gene_metadata)) |
|
|
|
gene_metadata = get_gene_annotation(soft_file) |
|
|
|
|
|
print("Column names:") |
|
print(gene_metadata.columns) |
|
print("\nDetailed view of first row:") |
|
print(gene_metadata.iloc[0].to_dict()) |
|
|
|
print("\nFirst 5 rows of ID and SystematicName columns:") |
|
print(gene_metadata[['ID', 'SystematicName']].head()) |
|
|
|
|
|
|
|
|
|
mapping_df = get_gene_mapping(gene_metadata, 'ID', 'SystematicName') |
|
|
|
|
|
gene_data = apply_gene_mapping(genetic_df, mapping_df) |
|
|
|
|
|
print("\nGene expression data shape:", gene_data.shape) |
|
print("\nFirst few genes and samples:") |
|
print(gene_data.head().iloc[:, :5]) |
|
|
|
from io import StringIO |
|
import gzip |
|
|
|
|
|
platform_data = [] |
|
in_platform = False |
|
columns_found = False |
|
|
|
with gzip.open(soft_file, 'rt') as f: |
|
for line in f: |
|
|
|
if line.startswith('^PLATFORM'): |
|
|
|
if in_platform and 'LncRNA+mRNA' in ''.join(platform_data): |
|
break |
|
platform_data = [] |
|
in_platform = True |
|
continue |
|
|
|
if in_platform: |
|
|
|
if "Reporter Name" in line or "Gene Symbol" in line or "Gene Name" in line: |
|
columns_found = True |
|
platform_data.append(line) |
|
|
|
|
|
if not columns_found: |
|
with gzip.open(soft_file, 'rt') as f: |
|
platform_data = f.readlines() |
|
|
|
|
|
filtered_lines = [] |
|
table_started = False |
|
for line in platform_data: |
|
if table_started: |
|
if not line.startswith(('^', '!', '#')): |
|
filtered_lines.append(line) |
|
elif "Reporter Name\tGene Symbol" in line or "ID\tGene Name" in line: |
|
table_started = True |
|
filtered_lines.append(line) |
|
|
|
|
|
gene_metadata = pd.read_csv(StringIO(''.join(filtered_lines)), sep='\t', low_memory=False) |
|
|
|
|
|
print("Column names:") |
|
print(gene_metadata.columns) |
|
print("\nPreview of first 5 rows:") |
|
print(gene_metadata.head().to_dict()) |
|
|
|
validate_and_save_cohort_info( |
|
is_final=False, |
|
cohort=cohort, |
|
info_path=json_path, |
|
is_gene_available=False, |
|
is_trait_available=False |
|
) |