Communitygithub.com

系統解析

MAFFT(マルチプルアライメント)、IQ-TREE 2(最尤法)、FastTree(高速NJ/ML)を用いて系統樹を構築・解析します。ETE3 または FigTree で可視化します。進化解析、微生物ゲノミクス、ウイルス系統動態、タンパク質ファミリー解析、分子時計研究に対応。

系統解析 とは?

このスキルは、生物学的配列(遺伝子、タンパク質、ゲノム)の進化史を推定するための系統解析パイプラインを提供します。MAFFTによるマルチプルアライメント、IQ-TREE 2による最尤系統樹推定(自動モデル選択対応)、FastTreeによる大規模データ用高速近似最尤法、ETE3による系統樹操作・可視化をカバーしています。ウイルス系統動態、タンパク質ファミリー解析、水平伝達検出、祖先配列復元、分子時計解析など、進化関係を問うあらゆる場面で利用可能です。Condaとpipで簡単にインストールでき、Pythonコードとして全工程を統合可能。は、開発者やAIエージェントが系統解析をプログラムから直接実行できるよう設計されています。

対応~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/K-Dense-AI/scientific-agent-skills/tree/main/skills/phylogenetics

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

系統解析 は何をしますか?

概要

系統解析は、生物学的配列(遺伝子、タンパク質、ゲノム)の分岐パターンを推定することで、進化史を再構築します。このスキルは標準的なパイプラインをカバーします:

  1. MAFFT — マルチプル配列アライメント
  2. IQ-TREE 2 — モデル選択機能付き最尤系統樹推定
  3. FastTree — 高速近似最尤法(大規模データセット向け)
  4. ETE3 — 系統樹操作と可視化のためのPythonライブラリ

インストール:

# Conda(CLIツールに推奨)
conda install -c bioconda mafft iqtree fasttree
uv pip install ete3

# ete3のTreeStyle/NodeStyle描画はQtバックエンドに依存するため、画像出力にはPyQt5が必要。系統樹のパースや統計計算は不要。
uv pip install PyQt5

このスキルを使うべき時

系統解析は以下の場合に使用します:

  • 進化関係:私の配列に最も近縁な生物/遺伝子はどれか?
  • ウイルス系統動態:アウトブレイクの拡散を追跡し、伝播日付を推定
  • タンパク質ファミリー解析:遺伝子ファミリー内の進化関係を推定
  • 水平遺伝子伝達検出:不一致な種/遺伝子系統樹を持つ遺伝子を特定
  • 祖先配列再構築:祖先タンパク質配列を推定
  • 分子時計解析:時間サンプリングを用いた分岐年代の推定
  • GWASの補完:変異を進化的コンテキストに配置(例:SARS-CoV-2変異株)
  • 微生物学:16S rRNAまたはコアゲノム系統樹による種系統

標準ワークフロー

1. MAFFTによるマルチプル配列アライメント

import subprocess
import os

def run_mafft(input_fasta: str, output_fasta: str, method: str = "auto",
               n_threads: int = 4) -> str:
    """
    MAFFTで配列をアライメントします。

    Args:
        input_fasta: 未アライメントFASTAファイルのパス
        output_fasta: アライメント出力ファイルのパス
        method: 'auto'(自動選択)、'einsi'(高精度)、'linsi'(高精度、低速)、
                'fftnsi'(中程度)、'fftns'(高速)、'retree2'(高速)
        n_threads: CPUスレッド数

    Returns:
        アライメント済みFASTAファイルのパス
    """
    methods = {
        "auto": ["mafft", "--auto"],
        "einsi": ["mafft", "--genafpair", "--maxiterate", "1000"],
        "linsi": ["mafft", "--localpair", "--maxiterate", "1000"],
        "fftnsi": ["mafft", "--fftnsi"],
        "fftns": ["mafft", "--fftns"],
        "retree2": ["mafft", "--retree", "2"],
    }

    cmd = methods.get(method, methods["auto"])
    cmd += ["--thread", str(n_threads), "--inputorder", input_fasta]

    with open(output_fasta, 'w') as out:
        result = subprocess.run(cmd, stdout=out, stderr=subprocess.PIPE, text=True)

    if result.returncode != 0:
        raise RuntimeError(f"MAFFT failed:\n{result.stderr}")

    # アライメントされた配列数を数える
    with open(output_fasta) as f:
        n_seqs = sum(1 for line in f if line.startswith('>'))
    print(f"MAFFT: aligned {n_seqs} sequences → {output_fasta}")

    return output_fasta

# MAFFTメソッド選択ガイド:
# 少数配列(<200)、高精度:linsi または einsi
# 中程度配列(<1000)、中程度:fftnsi
# 大規模データセット(>1000):fftns または auto
# 超高速(>10000):mafft --retree 1

2. アライメントのトリミング(推奨)

def trim_alignment_trimal(aligned_fasta: str, output_fasta: str,
                            method: str = "automated1") -> str:
    """
    TrimAlで不適切なアライメントカラムをトリミングします。

    Methods:
    - 'automated1': 自動ヒューリスティック(推奨)
    - 'gappyout': ギャップの多いカラムを除去
    - 'strict': 厳格なギャップ閾値
    """
    cmd = ["trimal", f"-{method}", "-in", aligned_fasta, "-out", output_fasta, "-fasta"]
    result = subprocess.run(cmd, capture_output=True, text=True)
    if result.returncode != 0:
        print(f"TrimAl warning: {result.stderr}")
        # トリミングされていないアライメントにフォールバック
        import shutil
        shutil.copy(aligned_fasta, output_fasta)
    return output_fasta

3. IQ-TREE 2 — 最尤系統樹

def run_iqtree(aligned_fasta: str, output_prefix: str,
                model: str = "TEST", bootstrap: int = 1000,
                n_threads: int = 4, extra_args: list = None) -> dict:
    """
    IQ-TREE 2で最尤系統樹を構築します。

    Args:
        aligned_fasta: アライメント済みFASTAファイル
        output_prefix: 出力ファイルのプレフィックス
        model: 'TEST'で自動モデル選択、または指定(例:DNAは'GTR+G'、
               タンパク質は'LG+G4'、'JTT+G'など)
        bootstrap: 超高速ブートストラップ反復回数(1000推奨)
        n_threads: スレッド数('AUTO'で自動検出)
        extra_args: 追加のIQ-TREE引数

    Returns:
        出力ファイルへのパスを含む辞書
    """
    cmd = [
        "iqtree2",
        "-s", aligned_fasta,
        "--prefix", output_prefix,
        "-m", model,
        "-B", str(bootstrap),   # 超高速ブートストラップ
        "-T", str(n_threads),
        "--redo"                # 既存の結果を上書き
    ]

    if extra_args:
        cmd.extend(extra_args)

    result = subprocess.run(cmd, capture_output=True, text=True)

    if result.returncode != 0:
        raise RuntimeError(f"IQ-TREE failed:\n{result.stderr}")

    # モデル選択結果を表示
    log_file = f"{output_prefix}.log"
    if os.path.exists(log_file):
        with open(log_file) as f:
            for line in f:
                if "Best-fit model" in line:
                    print(f"IQ-TREE: {line.strip()}")

    output_files = {
        "tree": f"{output_prefix}.treefile",
        "log": f"{output_prefix}.log",
        "iqtree": f"{output_prefix}.iqtree",  # 完全レポート
        "model": f"{output_prefix}.model.gz",
    }

    print(f"IQ-TREE: Tree saved to {output_files['tree']}")
    return output_files

# IQ-TREEモデル選択ガイド:
# DNA:     TEST → GTR+G, HKY+G, TrN+G
# タンパク質: TEST → LG+G4, WAG+G, JTT+G, Q.pfam+G
# コドン:   TEST → MG+F3X4

# 時間(分子時計)解析の場合、以下を追加:
# extra_args = ["--date", "dates.txt", "--clock-test", "--date-CI", "95"]

4. FastTree — 高速近似ML

大規模データセット(>1000配列)でIQ-TREEが遅すぎる場合:

def run_fasttree(aligned_fasta: str, output_tree: str,
                  sequence_type: str = "nt", model: str = "gtr",
                  n_threads: int = 4) -> str:
    """
    FastTreeで高速近似ML系統樹を構築します。

    Args:
        sequence_type: 'nt'(ヌクレオチド)または'aa'(アミノ酸)
        model: ntの場合:'gtr'(推奨)または'jc';aaの場合:'lg', 'wag', 'jtt'
    """
    if sequence_type == "nt":
        cmd = ["FastTree", "-nt", "-gtr"]
    else:
        cmd = ["FastTree", f"-{model}"]

    cmd += [aligned_fasta]

    with open(output_tree, 'w') as out:
        result = subprocess.run(cmd, stdout=out, stderr=subprocess.PIPE, text=True)

    if result.returncode != 0:
        raise RuntimeError(f"FastTree failed:\n{result.stderr}")

    print(f"FastTree: Tree saved to {output_tree}")
    return output_tree

5. ETE3による系統樹解析と可視化

from ete3 import Tree, TreeStyle, NodeStyle, TextFace, PhyloTree
import matplotlib.pyplot as plt

def load_tree(tree_file: str) -> Tree:
    """Newick形式の系統樹ファイルを読み込みます。"""
    t = Tree(tree_file)
    print(f"Tree: {len(t)} leaves, {len(list(t.traverse()))} nodes")
    return t

def basic_tree_stats(t: Tree) -> dict:
    """基本的な系統樹統計量を計算します。"""
    leaves = t.get_leaves()
    distances = [t.get_distance(l1, l2) for l1 in leaves[:min(50, len(leaves))]
                 for l2 in leaves[:min(50, len(leaves))] if l1 != l2]

    stats = {
        "n_leaves": len(leaves),
        "n_internal_nodes": len(t) - len(leaves),
        "total_branch_length": sum(n.dist for n in t.traverse()),
        "max_leaf_distance": max(distances) if distances else 0,
        "mean_leaf_distance": sum(distances)/len(distances) if distances else 0,
    }
    return stats

def find_mrca(t: Tree, leaf_names: list) -> Tree:
    """指定したリーフ群の最新共通祖先(MRCA)を見つけます。"""
    return t.get_common_ancestor(*leaf_names)

def visualize_tree(t: Tree, output_file: str = "tree.png",
                    show_branch_support: bool = True,
                    color_groups: dict = None,
                    width: int = 800) -> None:
    """
    系統樹を画像としてレンダリングします。

    Args:
        t: ETE3 Treeオブジェクト
        color_groups: リーフ名→色の辞書(分類群の着色用)
        show_branch_support: ブートストラップ値を表示
    """
    ts = TreeStyle()
    ts.show_leaf_name = True
    ts.show_branch_support = show_branch_support
    ts.mode = "r"  # 'r' = 矩形, 'c' = 円形

    if color_groups:
        for node in t.traverse():
            if node.is_leaf() and node.name in color_groups:
                nstyle = NodeStyle()
                nstyle["fgcolor"] = color_groups[node.name]
                nstyle["size"] = 8
                node.set_style(nstyle)

    t.render(output_file, tree_style=ts, w=width, units="px")
    print(f"Tree saved to: {output_file}")

def midpoint_root(t: Tree) -> Tree:
    """系統樹を中点でルート付けします(アウトグループ不明時)。"""
    t.set_outgroup(t.get_midpoint_outgroup())
    return t

def prune_tree(t: Tree, keep_leaves: list) -> Tree:
    """指定したリーフのみを残して系統樹を刈り込みます。"""
    t.prune(keep_leaves, preserve_branch_length=True)
    return t

6. 完全な解析スクリプト

import subprocess, os
from ete3 import Tree

def full_phylogenetic_analysis(
    input_fasta: str,
    output_dir: str = "phylo_results",
    sequence_type: str = "nt",
    n_threads: int = 4,
    bootstrap: int = 1000,
    use_fasttree: bool = False
) -> dict:
    """
    完全な系統解析パイプライン:アライメント→トリミング→系統樹→可視化。

    Args:
        input_fasta: 未アライメントFASTA
        sequence_type: 'nt'(ヌクレオチド)または'aa'(アミノ酸/タンパク質)
        use_fasttree: IQ-TREEの代わりにFastTreeを使用(大規模データセットで高速)
    """
    os.makedirs(output_dir, exist_ok=True)
    prefix = os.path.join(output_dir, "phylo")

    print("=" * 50)
    print("Step 1: Multiple Sequence Alignment (MAFFT)")
    aligned = run_mafft(input_fasta, f"{prefix}_aligned.fasta",
                         method="auto", n_threads=n_threads)

    print("\nStep 2: Tree Inference")
    if use_fasttree:
        tree_file = run_fasttree(
            aligned, f"{prefix}.tree",
            sequence_type=sequence_type,
            model="gtr" if sequence_type == "nt" else "lg"
        )
    else:
        model = "TEST" if sequence_type == "nt" else "TEST"
        iqtree_files = run_iqtree(
            aligned, prefix,
            model=model,
            bootstrap=bootstrap,
            n_threads=n_threads
        )
        tree_file = iqtree_files["tree"]

    print("\nStep 3: Tree Analysis")
    t = Tree(tree_file)
    t = midpoint_root(t)

    stats = basic_tree_stats(t)
    print(f"Tree statistics: {stats}")

    print("\nStep 4: Visualization")
    visualize_tree(t, f"{prefix}_tree.png", show_branch_support=True)

    # ルート付けされた系統樹を保存
    rooted_tree_file = f"{prefix}_rooted.nwk"
    t.write(format=1, outfile=rooted_tree_file)

    results = {
        "aligned_fasta": aligned,
        "tree_file": tree_file,
        "rooted_tree": rooted_tree_file,
        "visualization": f"{prefix}_tree.png",
        "stats": stats
    }

    print("\n" + "=" * 50)
    print("Phylogenetic analysis complete!")
    print(f"Results in: {output_dir}/")
    return results

IQ-TREEモデルガイド

DNAモデル

モデル説明使用例
GTR+G4General Time Reversible + Gamma最も柔軟なDNAモデル
HKY+G4Hasegawa-Kishino-Yano + Gamma二速度モデル(一般的)
TrN+G4Tamura-Nei不等な遷移
JCJukes-Cantor最も単純;全ての速度が等しい

タンパク質モデル

モデル説明使用例
LG+G4Le-Gascuel + Gamma平均的な最良のタンパク質モデル
WAG+G4Whelan-Goldman広く使用されている
JTT+G4Jones-Taylor-Thornton古典的なモデル
Q.pfam+G4pfamトレーニング済みPfam様タンパク質ファミリー向け
Q.bird+G4鳥類特化脊椎動物タンパク質

ヒント: -m TEST を使用してIQ-TREEに最適なモデルを自動選択させましょう。

ベストプラクティス

  • アライメントの品質を最優先:アライメントが悪いと信頼できない系統樹に繋がります。アライメントを手動で確認しましょう。
  • 小規模アライメント(<200配列)にはlinsi、大規模アライメントにはfftnsまたはauto
  • モデル選択:特別な理由がない限り、IQ-TREEでは常に-m TESTを使用
  • ブートストラップ:枝の支持値を評価するために1000回以上の超高速ブートストラップ(-B 1000)を使用
  • 系統樹のルート付け:無根系統樹は誤解を招く可能性があります。アウトグループまたは中点ルート付けを使用。
  • 5000配列以上ではFastTree:IQ-TREEは遅くなります。FastTreeは10~100倍高速です。
  • 長いアライメントをトリミング:TrimAlで信頼性の低いカラムを除去。系統樹の精度が向上します。
  • 組換えの確認:ウイルスや細菌の配列では、系統樹構築前に組換えをチェック(RDP4、GARD)。

参考資料

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

adaptyv

How to use the Adaptyv Bio Foundry API and Python SDK for protein experiment design, submission, and results retrieval. Use this skill whenever the user mentions Adaptyv, Foundry API, protein binding assays, protein screening experiments, BLI/SPR assays, thermostability assays, or wants to submit protein sequences for experimental characterization. Also trigger when code imports `adaptyv`, `adaptyv_sdk`, or `FoundryClient`, or references `foundry-api-public.adaptyvbio.com`.

aeon

This skill should be used for time series machine learning tasks including classification, regression, clustering, forecasting, anomaly detection, segmentation, and similarity search. Use when working with temporal data, sequential patterns, or time-indexed observations requiring specialized algorithms beyond standard ML approaches. Particularly suited for univariate and multivariate time series analysis with scikit-learn compatible APIs.

alphagenome

Look up precomputed AlphaGenome Atlas effects for any GRCh38 single-nucleotide variant (AVI score with Phred and 18 SHAP feature attributions, plus raw and quantile scores for RNA-seq, DNase, ATAC, ChIP-TF, ChIP-histone, CAGE, PRO-cap, splicing, polyadenylation and contact-map tracks), score variants or scan windows on demand with the AlphaGenome model for human and mouse (variant scoring, in silico mutagenesis, REF-versus-ALT track prediction), and build Atlas website deep links. Use when the user mentions AlphaGenome, AlphaGenome Atlas, AVI or AlphaGenome Variant Impact, DeepMind variant effect prediction, or wants to prioritise or mechanistically interpret non-coding, regulatory, splicing, enhancer, promoter, or chromatin-accessibility effects of SNVs from a VCF, credible set, or region. Research use only; not a clinical tool.

analytical-method-validation

Plan, execute, and document validation, verification, and transfer of analytical procedures under the governing framework - ICH Q2(R2) and Q14, USP <1220>/<1225>/<1226>, ICH M10 bioanalytical, CLSI EP, or ISO/IEC 17025. Use for HPLC, LC-MS/MS, GC, CE, ICP-MS, dissolution, qNMR, qPCR, NIR, and ligand binding or cell-based assays whenever the question is whether a procedure is fit for its intended purpose. Triggers include

anndata

Data structure for annotated matrices in single-cell analysis. Use when working with .h5ad files or integrating with the scverse ecosystem. This is the data format skill—for analysis workflows use scanpy; for probabilistic models use scvi-tools; for population-scale queries use cellxgene-census.

arbor

Autonomously improve a real artifact (code, training recipe, agent harness, data pipeline, prompt) against an objective and an evaluator, using Hypothesis Tree Refinement (HTR) from the Arbor paper. Use this whenever someone wants to iteratively optimize something over many experiments without overfitting — e.g.

arboreto

Infer gene regulatory networks (GRNs) from gene expression data using scalable algorithms (GRNBoost2, GENIE3). Use when analyzing transcriptomics data (bulk RNA-seq, single-cell RNA-seq) to identify transcription factor-target gene relationships and regulatory interactions. Supports distributed computation for large-scale datasets.

astropy

Core Python library for astronomy and astrophysics workflows that need Astropy APIs, including units/quantities, coordinates, FITS I/O, tables, time systems, WCS, and cosmology. Use when implementing or debugging astronomical data analysis code with Astropy.

autoskill

Observe the user

benchling-integration

Benchling Python SDK and REST API integration for registry entities, inventory, ELN entries, workflows, Benchling Apps, and Data Warehouse queries. Use when automating lab data with benchling-sdk or the v2 API.

bgpt-paper-search

Search scientific papers and retrieve structured experimental data extracted from full-text studies via the BGPT MCP server. Returns 25+ fields per paper including methods, results, sample sizes, quality scores, and conclusions. Use for literature reviews, evidence synthesis, and finding experimental details not available in abstracts alone.

bids

>

biopython

Comprehensive molecular biology toolkit. Use for sequence manipulation, file parsing (FASTA/GenBank/PDB), phylogenetics, and programmatic NCBI/PubMed access (Bio.Entrez). Best for batch processing, custom bioinformatics pipelines, BLAST automation. For quick lookups use gget; for multi-service integration use bioservices.

bioservices

Unified Python interface to 40+ bioinformatics services. Use when querying multiple databases (UniProt, KEGG, ChEMBL, Reactome) in a single workflow with consistent API. Best for cross-database analysis, ID mapping across services. For quick single-database lookups use gget; for sequence/file manipulation use biopython.

bulk-rnaseq

End-to-end bulk RNA-seq orchestrator — takes raw FASTQ reads through QC and trimming (FastQC, fastp/Trim Galore), alignment and quantification (STAR, Salmon, featureCounts), assembles a gene-level counts matrix, then hands off to differential expression (pydeseq2), pathway/GSEA enrichment (pathway-enrichment), and publication figures (scientific-visualization). Use whenever the user has bulk RNA-seq reads or quant output and wants a complete, reproducible differential-expression workflow — e.g.

cellxgene-census

Query the CZ CELLxGENE Census programmatically for versioned public single-cell and spatial transcriptomics data. Use when you need population-scale cell metadata, gene expression slices, Census summary counts, source H5AD URIs/downloads, embeddings, spatial Census data, or reference atlas comparisons across organisms, tissues, diseases, assays, and cell types. For analyzing your own local single-cell data use scanpy, anndata, or scvi-tools.

cirq

Google quantum computing framework. Use when targeting Google Quantum AI hardware, designing noise-aware circuits, or running quantum characterization experiments. Best for Google hardware, noise modeling, and low-level circuit design. For IBM hardware use qiskit; for quantum ML with autodiff use pennylane; for physics simulations use qutip.

citation-management

Comprehensive citation management for academic research. Search OpenAlex, PubMed, and Google Scholar for papers, extract accurate metadata, validate citations, and generate properly formatted BibTeX entries. This skill should be used when you need to find papers, verify citation information, convert DOIs to BibTeX, or ensure reference accuracy in scientific writing.

clinical-decision-support

Prepare and validate research-only clinical decision-support evaluation, evidence-profile, cohort, survival, biomarker/model, privacy, and governance artifacts. Use for aggregate or synthetic research documentation and traceability—not patient care or live clinical operation.

clinical-reports

Create safety-bounded draft structures and run local deterministic checks for clinical case, diagnostic, trial, safety, and aggregate research reports. Use only with synthetic, de-identified, or aggregate inputs and verified source-fact manifests; every output requires qualified review.

関連スキル