|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Osteoarthritis" |
|
cohort = "GSE98460" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Osteoarthritis" |
|
in_cohort_dir = "../DATA/GEO/Osteoarthritis/GSE98460" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Osteoarthritis/GSE98460.csv" |
|
out_gene_data_file = "./output/preprocess/3/Osteoarthritis/gene_data/GSE98460.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Osteoarthritis/clinical_data/GSE98460.csv" |
|
json_path = "./output/preprocess/3/Osteoarthritis/cohort_info.json" |
|
|
|
|
|
soft_file_path, matrix_file_path = geo_get_relevant_filepaths(in_cohort_dir) |
|
|
|
|
|
background_info, clinical_data = get_background_and_clinical_data(matrix_file_path) |
|
print("Background Information:") |
|
print(background_info) |
|
print("\nSample Characteristics:") |
|
|
|
|
|
unique_values_dict = get_unique_values_by_row(clinical_data) |
|
for row, values in unique_values_dict.items(): |
|
print(f"\n{row}:") |
|
print(values) |
|
|
|
is_gene_available = True |
|
|
|
|
|
|
|
trait_row = 1 |
|
def convert_trait(x): |
|
if not x or ':' not in x: |
|
return None |
|
value = x.split(':')[1].strip().lower() |
|
if 'osteoarthritis' in value or 'oa' in value: |
|
return 1 |
|
return 0 |
|
|
|
|
|
age_row = 2 |
|
def convert_age(x): |
|
if not x or ':' not in x: |
|
return None |
|
try: |
|
return float(x.split(':')[1].strip().split()[0]) |
|
except: |
|
return None |
|
|
|
|
|
gender_row = 3 |
|
def convert_gender(x): |
|
if not x or ':' not in x: |
|
return None |
|
value = x.split(':')[1].strip().lower() |
|
if 'female' in value: |
|
return 0 |
|
elif 'male' in value: |
|
return 1 |
|
return None |
|
|
|
|
|
is_trait_available = trait_row is not 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=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 selected clinical features:") |
|
print(preview_df(selected_clinical)) |
|
selected_clinical.to_csv(out_clinical_data_file) |
|
|
|
genetic_data = get_genetic_data(matrix_file_path) |
|
|
|
|
|
print("Data structure and head:") |
|
print(genetic_data.head()) |
|
|
|
print("\nShape:", genetic_data.shape) |
|
|
|
print("\nFirst 20 row IDs (gene/probe identifiers):") |
|
print(list(genetic_data.index)[:20]) |
|
|
|
|
|
print("\nFirst 5 column names:") |
|
print(list(genetic_data.columns)[:5]) |
|
|
|
|
|
|
|
|
|
|
|
requires_gene_mapping = True |
|
|
|
with gzip.open(soft_file_path, 'rt') as f: |
|
platform_found = False |
|
table_start = False |
|
first_row = None |
|
gene_rows = [] |
|
|
|
for line in f: |
|
if '!Platform_table_begin' in line: |
|
table_start = True |
|
continue |
|
elif '!Platform_table_end' in line: |
|
break |
|
elif table_start: |
|
if first_row is None: |
|
first_row = line.strip() |
|
else: |
|
gene_rows.append(line.strip()) |
|
|
|
|
|
import io |
|
header = first_row.split('\t') |
|
gene_data = '\n'.join(gene_rows) |
|
gene_annotation = pd.read_csv(io.StringIO(gene_data), sep='\t', names=header) |
|
|
|
print("Column names:") |
|
print(gene_annotation.columns) |
|
|
|
print("\nPreview of gene annotation data:") |
|
print(preview_df(gene_annotation)) |
|
|
|
with gzip.open(soft_file_path, 'rt') as f: |
|
found_table = False |
|
header = None |
|
first_five_rows = [] |
|
for line in f: |
|
if '!Platform_title' in line: |
|
print("Platform title:", line.strip()) |
|
elif '!Platform_organism' in line: |
|
print("Platform organism:", line.strip()) |
|
elif '!Platform_table_begin' in line: |
|
found_table = True |
|
continue |
|
elif found_table: |
|
if header is None: |
|
header = line.strip() |
|
print("\nPlatform table header:") |
|
print(header) |
|
elif len(first_five_rows) < 5: |
|
first_five_rows.append(line.strip()) |
|
else: |
|
break |
|
|
|
print("\nFirst few rows:") |
|
for row in first_five_rows: |
|
print(row) |
|
|
|
|
|
print("\nSplitting first row by tabs to check all fields:") |
|
if first_five_rows: |
|
print(first_five_rows[0].split('\t')) |
|
|
|
|
|
platform_data = pd.read_csv(gzip.open(soft_file_path, 'rt'), |
|
sep='\t', |
|
skiprows=lambda x: x == 0 or not found_table, |
|
comment='!') |
|
|
|
print("\nFull column names found:") |
|
print(platform_data.columns.tolist()) |
|
|
|
print("\nPreview of complete annotation data:") |
|
print(preview_df(platform_data)) |
|
|
|
gene_annotation = get_gene_annotation(soft_file_path) |
|
|
|
|
|
print("Available columns:", gene_annotation.columns.tolist()) |
|
|
|
|
|
probe_data = gene_annotation.head() |
|
print("\nFirst few rows:") |
|
print(preview_df(probe_data)) |
|
|
|
|
|
mapping_df = get_gene_mapping(gene_annotation, |
|
prob_col='ID', |
|
gene_col='Gene Title') |
|
|
|
|
|
gene_data = apply_gene_mapping(genetic_data, mapping_df) |
|
|
|
print("\nPreview of gene expression data after mapping:") |
|
print(preview_df(gene_data)) |
|
|
|
selected_clinical_df = pd.read_csv(out_clinical_data_file, index_col=0) |
|
|
|
|
|
genetic_data = normalize_gene_symbols_in_index(genetic_data) |
|
genetic_data.to_csv(out_gene_data_file) |
|
|
|
|
|
linked_data = geo_link_clinical_genetic_data(selected_clinical_df, genetic_data) |
|
|
|
|
|
linked_data = handle_missing_values(linked_data, trait) |
|
|
|
|
|
trait_biased, linked_data = judge_and_remove_biased_features(linked_data, trait) |
|
|
|
|
|
note = "This dataset contains cartilage tissue samples from OA patients, with gene expression data and demographic information." |
|
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=trait_biased, |
|
df=linked_data, |
|
note=note |
|
) |
|
|
|
|
|
if is_usable: |
|
os.makedirs(os.path.dirname(out_data_file), exist_ok=True) |
|
linked_data.to_csv(out_data_file) |
|
|
|
print("Examining platform information in SOFT file...") |
|
with gzip.open(soft_file_path, 'rt') as f: |
|
platform_lines = [] |
|
capture = False |
|
for line in f: |
|
if line.startswith(('!Platform_title', '!Platform_organism', '!Platform_technology')): |
|
print(line.strip()) |
|
elif '!platform_table_begin' in line.lower(): |
|
capture = True |
|
continue |
|
elif '!platform_table_end' in line.lower(): |
|
break |
|
elif capture: |
|
platform_lines.append(line.strip()) |
|
|
|
|
|
print("\nExtracting complete platform annotation...") |
|
platform_df = pd.read_csv(io.StringIO('\n'.join(platform_lines)), sep='\t') |
|
|
|
print("\nFound columns:") |
|
print(platform_df.columns.tolist()) |
|
|
|
print("\nPreview of annotation data:") |
|
print(preview_df(platform_df)) |