|
|
|
from tools.preprocess import * |
|
|
|
|
|
trait = "Melanoma" |
|
cohort = "GSE157738" |
|
|
|
|
|
in_trait_dir = "../DATA/GEO/Melanoma" |
|
in_cohort_dir = "../DATA/GEO/Melanoma/GSE157738" |
|
|
|
|
|
out_data_file = "./output/preprocess/3/Melanoma/GSE157738.csv" |
|
out_gene_data_file = "./output/preprocess/3/Melanoma/gene_data/GSE157738.csv" |
|
out_clinical_data_file = "./output/preprocess/3/Melanoma/clinical_data/GSE157738.csv" |
|
json_path = "./output/preprocess/3/Melanoma/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) |
|
|
|
|
|
unique_values_dict = get_unique_values_by_row(clinical_data) |
|
|
|
|
|
print("Dataset Background Information:") |
|
print(background_info) |
|
print("\nSample Characteristics:") |
|
for feature, values in unique_values_dict.items(): |
|
print(f"\n{feature}:") |
|
print(values) |
|
|
|
|
|
is_gene_available = True |
|
|
|
|
|
|
|
trait_row = 4 |
|
|
|
|
|
age_row = None |
|
gender_row = None |
|
|
|
|
|
def convert_trait(x): |
|
|
|
if not isinstance(x, str): |
|
return None |
|
value = x.split(':')[-1].strip() |
|
|
|
|
|
if value in ['NED1', 'NED2', 'PR']: |
|
return 1 |
|
|
|
elif value in ['PD', 'SD']: |
|
return 0 |
|
return None |
|
|
|
def convert_age(x): |
|
return None |
|
|
|
def convert_gender(x): |
|
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 |
|
) |
|
|
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
preview_result = preview_df(selected_clinical) |
|
|
|
|
|
selected_clinical.to_csv(out_clinical_data_file) |
|
|
|
genetic_data = get_genetic_data(matrix_file_path) |
|
|
|
|
|
print("First 20 row IDs:") |
|
print(list(genetic_data.index)[:20]) |
|
|
|
|
|
is_gene_available = True |
|
|
|
|
|
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) |
|
) |
|
|
|
genetic_data.to_csv(out_gene_data_file) |
|
|
|
|
|
requires_gene_mapping = True |
|
|
|
import gzip |
|
with gzip.open(soft_file_path, 'rt') as f: |
|
|
|
for i, line in enumerate(f): |
|
if i < 100: |
|
if 'table_begin' in line.lower(): |
|
print(f"Found table marker at line {i}:") |
|
print(line.strip()) |
|
else: |
|
break |
|
|
|
|
|
gene_metadata = get_gene_annotation(soft_file_path, prefixes=['!Platform_table_begin', '!platform_table_end']) |
|
|
|
|
|
print("\nGene annotation columns and sample values:") |
|
preview = preview_df(gene_metadata) |
|
print(preview) |
|
|
|
import gzip |
|
|
|
def parse_soft_file(file_path): |
|
probe_to_gene = {} |
|
within_platform = False |
|
with gzip.open(file_path, 'rt') as f: |
|
for line in f: |
|
line = line.strip() |
|
if line.startswith('!Platform_table_begin'): |
|
within_platform = True |
|
|
|
header = next(f).strip().split('\t') |
|
id_idx = header.index('ID') |
|
gene_idx = header.index('Gene Assignment') |
|
continue |
|
|
|
if within_platform: |
|
if line.startswith('!Platform_table_end'): |
|
break |
|
|
|
fields = line.split('\t') |
|
if len(fields) > max(id_idx, gene_idx): |
|
probe_id = fields[id_idx] |
|
gene_info = fields[gene_idx] |
|
if gene_info != '---': |
|
|
|
|
|
gene_parts = gene_info.split('//') |
|
if len(gene_parts) > 1: |
|
gene_symbol = gene_parts[1].strip() |
|
probe_to_gene[probe_id] = gene_symbol |
|
|
|
|
|
mapping_df = pd.DataFrame.from_dict(probe_to_gene.items()) |
|
mapping_df.columns = ['ID', 'Gene'] |
|
return mapping_df |
|
|
|
|
|
mapping_data = parse_soft_file(soft_file_path) |
|
|
|
|
|
gene_data = apply_gene_mapping(genetic_data, mapping_data) |
|
|
|
|
|
print("\nFirst few gene symbols:") |
|
print(list(gene_data.index)[:10]) |
|
|
|
|
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
def parse_soft_file(file_path): |
|
probe_to_gene = [] |
|
within_platform = False |
|
|
|
with gzip.open(file_path, 'rt') as f: |
|
|
|
print("First 10 lines of SOFT file:") |
|
for i, line in enumerate(f): |
|
if i < 10: |
|
print(line.strip()) |
|
if i == 10: |
|
break |
|
f.seek(0) |
|
|
|
for line in f: |
|
line = line.strip() |
|
if line.startswith('!Platform_table_begin'): |
|
within_platform = True |
|
|
|
print("\nPlatform table header:") |
|
header = next(f).strip() |
|
print(header) |
|
header = header.split('\t') |
|
try: |
|
id_idx = header.index('ID') |
|
gene_idx = header.index('Gene Symbol') |
|
except ValueError: |
|
|
|
print("\nAll column names found:") |
|
print(header) |
|
|
|
gene_idx = next((i for i, col in enumerate(header) |
|
if 'gene' in col.lower() and 'symbol' in col.lower()), -1) |
|
if gene_idx == -1: |
|
raise ValueError("Could not find gene symbol column") |
|
continue |
|
|
|
if within_platform: |
|
if line.startswith('!Platform_table_end'): |
|
break |
|
|
|
fields = line.split('\t') |
|
if len(fields) > max(id_idx, gene_idx): |
|
probe_id = fields[id_idx] |
|
gene_symbol = fields[gene_idx] |
|
if gene_symbol and gene_symbol != '---': |
|
probe_to_gene.append([probe_id, gene_symbol]) |
|
|
|
mapping_df = pd.DataFrame(probe_to_gene, columns=['ID', 'Gene']) |
|
print(f"\nFound {len(mapping_df)} probe-to-gene mappings") |
|
return mapping_df |
|
|
|
|
|
mapping_data = parse_soft_file(soft_file_path) |
|
|
|
|
|
gene_data = apply_gene_mapping(genetic_data, mapping_data) |
|
|
|
|
|
print("\nFirst few gene symbols:") |
|
print(list(gene_data.index)[:10]) |
|
|
|
|
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
def parse_soft_file(file_path): |
|
probe_to_gene = [] |
|
within_platform = False |
|
header_found = False |
|
|
|
with gzip.open(file_path, 'rt') as f: |
|
for line in f: |
|
line = line.strip() |
|
|
|
|
|
if line.startswith('^PLATFORM'): |
|
within_platform = True |
|
print(f"\nFound platform section: {line}") |
|
continue |
|
|
|
|
|
if within_platform and not header_found and line.startswith('!Platform_var'): |
|
header_line = line |
|
print(f"\nPotential header line found: {header_line}") |
|
if 'gene symbol' in header_line.lower(): |
|
print("Found gene symbol column info") |
|
|
|
|
|
if ' = ' in line: |
|
field_name = line.split(' = ')[1] |
|
print(f"Field name: {field_name}") |
|
if 'gene symbol' in field_name.lower(): |
|
gene_col = field_name |
|
print(f"Gene column found: {gene_col}") |
|
header_found = True |
|
continue |
|
|
|
|
|
if within_platform and header_found: |
|
if line.startswith('#') or line.startswith('!'): |
|
continue |
|
|
|
fields = line.split('\t') |
|
if len(fields) < 2: |
|
continue |
|
|
|
probe_id = fields[0] |
|
|
|
for field in fields[1:]: |
|
if '//' in field: |
|
parts = field.split('//') |
|
if len(parts) > 1: |
|
gene_symbol = parts[1].strip() |
|
if gene_symbol and gene_symbol not in ['---', '']: |
|
probe_to_gene.append([probe_id, gene_symbol]) |
|
break |
|
|
|
mapping_df = pd.DataFrame(probe_to_gene, columns=['ID', 'Gene']) |
|
print(f"\nFound {len(mapping_df)} probe-to-gene mappings") |
|
if len(mapping_df) > 0: |
|
print("\nFirst few mappings:") |
|
print(mapping_df.head()) |
|
return mapping_df |
|
|
|
|
|
print("Extracting probe-to-gene mappings from SOFT file...") |
|
mapping_data = parse_soft_file(soft_file_path) |
|
|
|
if len(mapping_data) > 0: |
|
|
|
print("\nConverting probe-level to gene-level expression data...") |
|
gene_data = apply_gene_mapping(genetic_data, mapping_data) |
|
|
|
|
|
print("\nSaving gene expression data...") |
|
os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True) |
|
gene_data.to_csv(out_gene_data_file) |
|
|
|
|
|
gene_data = normalize_gene_symbols_in_index(gene_data) |
|
clinical_features = pd.read_csv(out_clinical_data_file, index_col=0) |
|
linked_data = geo_link_clinical_genetic_data(clinical_features, gene_data) |
|
|
|
|
|
linked_data = handle_missing_values(linked_data, trait) |
|
|
|
|
|
trait_biased, linked_data = judge_and_remove_biased_features(linked_data, trait) |
|
|
|
|
|
is_usable = validate_and_save_cohort_info( |
|
is_final=True, |
|
cohort=cohort, |
|
info_path=json_path, |
|
is_gene_available=is_gene_available, |
|
is_trait_available=True, |
|
is_biased=trait_biased, |
|
df=linked_data, |
|
note="Gene expression data from melanoma patients receiving PD-1 immunotherapy, with long-term benefit as outcome." |
|
) |
|
|
|
|
|
if is_usable: |
|
os.makedirs(os.path.dirname(out_data_file), exist_ok=True) |
|
linked_data.to_csv(out_data_file) |
|
else: |
|
print("Failed to extract gene mappings. Cannot proceed with data processing.") |