BioMod / model_dependencies /biomod_data_all /create_uncovered_biomods.py
beiken1201's picture
Unify BioMod names, flatten weight paths, and remove duplicate source directories
eaa8608 verified
Raw History Blame Contribute Delete
5.69 kB
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Create additional modules for genes not covered by existing modules
Ensure that all model genes can participate in training
Run from the repository root:
python model_dependencies/biomod_data_all/create_uncovered_biomods.py \
--merged_biomods model_dependencies/biomod_data_all/merged_biomods.json \
--output tutorial_runs/biomod_data/merged_modules_full.json \
--biomod_size 50
"""
import json
import os
import csv
import argparse
from pathlib import Path
PROJECT_ROOT = Path(__file__).resolve().parents[2]
ASSET_ROOT = PROJECT_ROOT / 'model_dependencies'
MODULE_DIR = Path(__file__).resolve().parent
def create_uncovered_biomods(
merged_modules_path,
gene_list_path,
gene_mapping_path,
output_path,
module_size=50,
verbose=True
):
"""
Create additional modules for uncovered genes
Args:
merged_modules_path: merged module file path
gene_list_path: model gene list path
gene_mapping_path: Gene mapping file path
output_path: output file path
module_size: the size of the new module
verbose: whether to print detailed information
"""
# Load model gene list
with open(gene_list_path, 'r') as f:
model_genes = set(line.strip() for line in f)
# Load existing modules
with open(merged_modules_path, 'r') as f:
modules = json.load(f)
# Load gene map (symbol -> ensembl)
symbol_to_ens = {}
ens_to_symbol = {}
with open(gene_mapping_path, 'r') as f:
reader = csv.DictReader(f)
for row in reader:
symbol = row.get('Approved symbol', '').strip()
ens_id = row.get('Ensembl gene ID', '').strip()
if symbol and ens_id:
symbol_to_ens[symbol] = ens_id
ens_to_symbol[ens_id] = symbol
# Find genes that are covered
covered_genes_ens = set()
for module_name, genes in modules.items():
for g in genes:
ens_id = symbol_to_ens.get(g)
if ens_id and ens_id in model_genes:
covered_genes_ens.add(ens_id)
elif g in model_genes:
covered_genes_ens.add(g)
# Find uncovered genes
uncovered_genes = model_genes - covered_genes_ens
uncovered_genes = sorted(list(uncovered_genes))
if verbose:
print(f"Total number of model genes: {len(model_genes)}")
print(f"Genes covered: {len(covered_genes_ens)} ({len(covered_genes_ens)/len(model_genes)*100:.1f}%)")
print(f"Genes not covered: {len(uncovered_genes)} ({len(uncovered_genes)/len(model_genes)*100:.1f}%)")
# Create new modules for uncovered genes
# Use Ensembl ID (because the model uses Ensembl ID)
new_modules = {}
num_new_modules = (len(uncovered_genes) + module_size - 1) // module_size
for i in range(num_new_modules):
start_idx = i * module_size
end_idx = min((i + 1) * module_size, len(uncovered_genes))
module_genes = uncovered_genes[start_idx:end_idx]
# Try converting to symbol if there is a mapping, otherwise keep the Ensembl ID
module_genes_final = []
for g in module_genes:
symbol = ens_to_symbol.get(g)
if symbol:
module_genes_final.append(symbol)
else:
module_genes_final.append(g)
new_modules[f"uncovered_module_{i+1:04d}"] = module_genes_final
if verbose:
print(f"\nCreated {len(new_modules)} new modules (each approx. {module_size} genes)")
# Merge existing modules and new modules
merged_modules = {**modules, **new_modules}
# save
os.makedirs(os.path.dirname(output_path) if os.path.dirname(output_path) else '.', exist_ok=True)
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(merged_modules, f, indent=2)
if verbose:
print(f"\nFinal result:")
print(f" Original modules: {len(modules)}")
print(f" New modules: {len(new_modules)}")
print(f" Total modules: {len(merged_modules)}")
print(f" Save to: {output_path}")
return merged_modules
def main():
parser = argparse.ArgumentParser(description='Create modules for uncovered genes')
parser.add_argument('--merged_biomods', '--merged_modules', type=str,
default=str(MODULE_DIR / 'merged_biomods.json'),
help='Path to merged modules JSON file', dest='merged_modules')
parser.add_argument('--gene_list', type=str,
default=str(ASSET_ROOT / 'selected_genes_27k.txt'),
help='Path to model gene list file')
parser.add_argument('--gene_mapping', type=str,
default=str(ASSET_ROOT / 'gene2ens.csv'),
help='Path to gene mapping CSV file')
parser.add_argument('--output', '-o', type=str,
default=str(MODULE_DIR / 'merged_modules_full.json'),
help='Output file path')
parser.add_argument('--biomod_size', '--module_size', type=int, default=50,
help='Size of new modules for uncovered genes', dest='module_size')
args = parser.parse_args()
create_uncovered_biomods(
merged_modules_path=args.merged_modules,
gene_list_path=args.gene_list,
gene_mapping_path=args.gene_mapping,
output_path=args.output,
module_size=args.module_size
)
if __name__ == '__main__':
main()