|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Obesity" |
|
cohort = "GSE158237" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Obesity" |
|
in_cohort_dir = "../DATA/GEO/Obesity/GSE158237" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Obesity/GSE158237.csv" |
|
out_gene_data_file = "./output/preprocess/3/Obesity/gene_data/GSE158237.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Obesity/clinical_data/GSE158237.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 = 10 |
|
age_row = 1 |
|
gender_row = 2 |
|
|
|
|
|
def convert_trait(value): |
|
|
|
if pd.isna(value): |
|
return None |
|
try: |
|
bmi = float(value.split(': ')[1]) |
|
return 1 if bmi >= 30 else 0 |
|
except: |
|
return None |
|
|
|
def convert_age(value): |
|
|
|
if pd.isna(value): |
|
return None |
|
try: |
|
age = float(value.split(': ')[1]) |
|
return age |
|
except: |
|
return None |
|
|
|
def convert_gender(value): |
|
|
|
if pd.isna(value): |
|
return None |
|
try: |
|
sex = int(value.split(': ')[1]) |
|
return 1 if sex == 1 else 0 |
|
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=(trait_row is not None)) |
|
|
|
|
|
|
|
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) |
|
|
|
|
|
preview = preview_df(clinical_features) |
|
print("Preview of clinical features:", preview) |
|
|
|
|
|
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 = filter_content_by_prefix(soft_file, |
|
prefixes_a=['!Platform_table_begin'], |
|
unselect=False, |
|
source_type='file', |
|
return_df_a=True)[0] |
|
|
|
|
|
print("Gene annotation shape:", gene_annotation.shape) |
|
print("\nGene annotation columns:", list(gene_annotation.columns)) |
|
print("\nGene annotation preview:") |
|
print(preview_df(gene_annotation)) |
|
|
|
|
|
print("\nNumber of non-null values in each column:") |
|
print(gene_annotation.count()) |
|
|
|
|
|
print("\nExample rows with ID and gene symbol information:") |
|
print(gene_annotation[['ID', 'Symbol']].head(10).to_string()) |
|
|
|
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
|
|
|
import gzip |
|
platform_start = False |
|
header_line = None |
|
first_data_line = None |
|
|
|
with gzip.open(soft_file, 'rt') as f: |
|
for line in f: |
|
if '!Platform_table_begin' in line: |
|
platform_start = True |
|
|
|
header_line = next(f).strip() |
|
first_data_line = next(f).strip() |
|
break |
|
|
|
print("Header line found:") |
|
print(header_line) |
|
print("\nFirst data line example:") |
|
print(first_data_line) |
|
|
|
|
|
from io import StringIO |
|
platform_data = [] |
|
platform_start = False |
|
|
|
with gzip.open(soft_file, 'rt') as f: |
|
for line in f: |
|
if '!Platform_table_begin' in line: |
|
platform_start = True |
|
continue |
|
elif '!Platform_table_end' in line: |
|
break |
|
elif platform_start: |
|
platform_data.append(line.strip()) |
|
|
|
|
|
gene_annotation = pd.read_csv(StringIO('\n'.join(platform_data)), sep='\t') |
|
|
|
|
|
print("\nGene annotation shape:", gene_annotation.shape) |
|
print("\nGene annotation columns:", gene_annotation.columns.tolist()) |
|
print("\nFirst few rows preview:") |
|
print(gene_annotation.head().to_string()) |
|
|
|
|
|
symbol_candidates = [col for col in gene_annotation.columns |
|
if any(term in col.lower() |
|
for term in ['gene', 'symbol', 'entrez', 'refseq'])] |
|
print("\nPotential gene symbol columns:", symbol_candidates) |
|
from io import StringIO |
|
|
|
|
|
import gzip |
|
print("Examining SOFT file content...") |
|
with gzip.open(soft_file, 'rt') as f: |
|
for line in f: |
|
|
|
if "!Platform_table_begin" in line: |
|
header = next(f).strip() |
|
print("\nFound platform table with header:") |
|
print(header) |
|
print("\nFirst few data lines:") |
|
for _ in range(5): |
|
print(next(f).strip()) |
|
break |
|
|
|
|
|
gene_metadata_str = filter_content_by_prefix(soft_file, |
|
prefixes_a=['^', '#'], |
|
unselect=True, |
|
source_type='file', |
|
return_df_a=False)[0] |
|
|
|
|
|
annotation_lines = [] |
|
capture = False |
|
for line in gene_metadata_str.split('\n'): |
|
if 'Reporter Database Entry [gene symbol]' in line: |
|
|
|
capture = True |
|
continue |
|
if capture and line.strip(): |
|
if line.startswith('!'): |
|
break |
|
annotation_lines.append(line) |
|
|
|
if annotation_lines: |
|
|
|
gene_metadata = pd.read_csv(StringIO('\n'.join(annotation_lines)), sep='\t') |
|
|
|
print("\nAvailable columns in gene annotation data:") |
|
print(gene_metadata.columns.tolist()) |
|
|
|
|
|
mapping_df = get_gene_mapping(gene_metadata, prob_col='ID', gene_col='GENE_SYMBOL') |
|
|
|
|
|
gene_data = apply_gene_mapping(gene_data, mapping_df) |
|
|
|
|
|
print(f"\nShape of mapped gene expression data: {gene_data.shape}") |
|
print("\nFirst few gene symbols:") |
|
print(gene_data.index[:10]) |
|
else: |
|
print("\nNo gene symbol annotation section found in the SOFT file.") |
|
|
|
selected_clinical = pd.read_csv(out_clinical_data_file, index_col=0) |
|
|
|
|
|
minimal_df = selected_clinical.copy() |
|
|
|
|
|
is_biased, minimal_df = judge_and_remove_biased_features(minimal_df, trait) |
|
|
|
|
|
is_usable = validate_and_save_cohort_info( |
|
is_final=True, |
|
cohort=cohort, |
|
info_path=json_path, |
|
is_gene_available=False, |
|
is_trait_available=True, |
|
is_biased=is_biased, |
|
df=minimal_df, |
|
note="Failed to extract gene symbol annotations from SOFT file" |
|
) |
|
|
|
|