0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

系統樹OTU名のリネーム処理

Posted at

系統樹ファイルのOTUをリストに従って、変換するスクリプト

tree_rename.py
#!/usr/bin/env python3

import argparse
from Bio import Phylo

def parse_args():
    parser = argparse.ArgumentParser(description="Newick系統樹のOTU名を指定リストに基づいて置換します。")
    parser.add_argument("-t", "--tree", required=True, help="入力Newickファイル (.nwk)")
    parser.add_argument("-r", "--replace", required=True, help="OTU名の置換リスト(タブ区切りTSV)")
    parser.add_argument("-o", "--output", required=True, help="出力Newickファイル名")
    return parser.parse_args()

def read_replace_table(file_path):
    replace_dict = {}
    with open(file_path, "r") as f:
        for line in f:
            parts = line.strip().split("\t")
            if len(parts) == 2:
                old, new = parts
                replace_dict[old] = new
    return replace_dict

def rename_otu(tree_path, replace_dict, output_path):
    tree = Phylo.read(tree_path, "newick")
    for terminal in tree.get_terminals():
        if terminal.name in replace_dict:
            terminal.name = replace_dict[terminal.name]
    Phylo.write(tree, output_path, "newick")
    print(f"変換済みの系統樹を保存しました: {output_path}")

def main():
    args = parse_args()
    replace_dict = read_replace_table(args.replace)
    rename_otu(args.tree, replace_dict, args.output)

if __name__ == "__main__":
    main()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?