Download model_dependencies/module_data_all/merge_modules.py from ctcatlas/BioMod: direct link, hf CLI and curl.
- Browser
- Download file 3.16 kB
-
https://huggingface.co/ctcatlas/BioMod/resolve/07bb78a00ff0fd94b602707ee2e5ff99f7a84721/model_dependencies/module_data_all/merge_modules.py
- Command line
-
hf download hf://ctcatlas/BioMod@07bb78a00ff0fd94b602707ee2e5ff99f7a84721/model_dependencies/module_data_all/merge_modules.py
-
curl -L -o merge_modules.py https://huggingface.co/ctcatlas/BioMod/resolve/07bb78a00ff0fd94b602707ee2e5ff99f7a84721/model_dependencies/module_data_all/merge_modules.py
3.16 kB
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| """ | |
| 合并多个 module JSON 文件为一个文件 | |
| 用于将不同来源的 module 数据合并后进行训练 | |
| 使用方法: | |
| python merge_modules.py --output module_data_all/merged_modules.json | |
| """ | |
| import json | |
| import os | |
| import argparse | |
| def merge_module_files(input_files, output_file, verbose=True): | |
| """ | |
| 合并多个 module JSON 文件 | |
| Args: | |
| input_files: 输入文件路径列表 | |
| output_file: 输出文件路径 | |
| verbose: 是否打印详细信息 | |
| """ | |
| merged_modules = {} | |
| total_modules = 0 | |
| all_genes = set() | |
| for input_file in input_files: | |
| if not os.path.exists(input_file): | |
| print(f"Warning: File not found: {input_file}") | |
| continue | |
| with open(input_file, 'r', encoding='utf-8') as f: | |
| data = json.load(f) | |
| file_modules = 0 | |
| file_genes = set() | |
| # 获取文件名作为前缀,避免 key 冲突 | |
| basename = os.path.splitext(os.path.basename(input_file))[0] | |
| for key, genes in data.items(): | |
| if isinstance(genes, (list, tuple)): | |
| # 添加文件名前缀避免 key 冲突 | |
| new_key = f"{basename}__{key}" | |
| merged_modules[new_key] = list(genes) | |
| file_modules += 1 | |
| file_genes.update(genes) | |
| total_modules += file_modules | |
| all_genes.update(file_genes) | |
| if verbose: | |
| print(f"{os.path.basename(input_file)}:") | |
| print(f" Modules: {file_modules}") | |
| print(f" Unique genes: {len(file_genes)}") | |
| # 保存合并后的文件 | |
| os.makedirs(os.path.dirname(output_file) if os.path.dirname(output_file) else '.', exist_ok=True) | |
| with open(output_file, 'w', encoding='utf-8') as f: | |
| json.dump(merged_modules, f, indent=2) | |
| if verbose: | |
| print("=" * 50) | |
| print(f"Total modules: {total_modules}") | |
| print(f"Total unique genes: {len(all_genes)}") | |
| print(f"Saved to: {output_file}") | |
| return merged_modules | |
| def main(): | |
| parser = argparse.ArgumentParser(description='Merge multiple module JSON files') | |
| parser.add_argument('--input', '-i', nargs='+', | |
| default=[ | |
| 'module_data/module1_hallmarks.json', | |
| 'module_data/module2_reactome_2022_filtered.json', | |
| 'module_data_2/module3_hotspot_260k.json', | |
| # 添加外部文件时使用绝对路径 | |
| ], | |
| help='Input JSON files to merge') | |
| parser.add_argument('--output', '-o', | |
| default='module_data_all/merged_modules.json', | |
| help='Output merged JSON file') | |
| parser.add_argument('--add-external', type=str, default=None, | |
| help='Additional external module file to include') | |
| args = parser.parse_args() | |
| input_files = args.input | |
| if args.add_external: | |
| input_files.append(args.add_external) | |
| merge_module_files(input_files, args.output) | |
| if __name__ == '__main__': | |
| main() | |