Skills Agentes

Phylogenetics

Construye y analiza árboles filogenéticos con MAFFT, IQ-TREE 2 y FastTree, con visualización en ETE3 o FigTree, para evolución, filodinámica viral, familias de proteínas y reloj molecular.

Estrellas
34.8k

en todo el repo

Actividad
61

0–100, la ruta de este skill

Actualizado
hace 28 días

último commit aquí

Commits
5

últimos 90 días

Contexto
3.5k tok

71 tok en reposo

Paquete
3 archivos

28 KB

Instalar

Funciona con cualquier agente que lea SKILL.md

npx -y skills add K-Dense-AI/scientific-agent-skills --skill phylogenetics --agent claude-code

Se instala solo en este repositorio.

Qué hace

  • Ejecuta el pipeline estándar de filogenética: alineamiento con MAFFT, inferencia de árboles con IQ-TREE 2 o FastTree, y visualización con ETE3.
  • Selecciona el método de alineamiento de MAFFT (auto, einsi, linsi, fftnsi, fftns) según el tamaño del set de secuencias.
  • Cubre reconstrucción evolutiva, filodinámica viral, análisis de familias de proteínas y estudios de reloj molecular.

Úsalo cuando

  • Determinar qué organismo o gen está más relacionado con una secuencia dada.
  • Rastrear la propagación de un brote viral y estimar fechas de transmisión (filodinámica).
  • Analizar relaciones evolutivas dentro de una familia de genes o detectar transferencia horizontal.
  • Reconstruir secuencias ancestrales o estimar fechas de divergencia con reloj molecular.

No lo uses cuando

    Qué lo activa

    Di cualquiera de estas frases y el agente debería cargar este skill.

    • Construye un árbol filogenético con estas secuencias de proteínas
    • Alinea estas secuencias con MAFFT y genera un árbol con IQ-TREE
    • Analiza la filodinámica de este brote viral
    • Sitúa estas variantes de SARS-CoV-2 en su contexto evolutivo

    SKILL.md

    En inglés

    Phylogenetics

    Overview

    Phylogenetic analysis reconstructs the evolutionary history of biological sequences (genes, proteins, genomes) by inferring the branching pattern of descent. This skill covers the standard pipeline:

    1. MAFFT — Multiple sequence alignment
    2. IQ-TREE 2 — Maximum likelihood tree inference with model selection
    3. FastTree — Fast approximate maximum likelihood (for large datasets)
    4. ETE3 — Python library for tree manipulation and visualization

    Installation:

    # Conda (recommended for CLI tools)
    conda install -c bioconda mafft iqtree fasttree
    uv pip install ete3
    
    # ete3's TreeStyle/NodeStyle rendering lives in its Qt backend, so image output
    # needs PyQt5 as well; tree parsing and statistics work without it.
    uv pip install PyQt5
    

    When to Use This Skill

    Use phylogenetics when:

    • Evolutionary relationships: Which organism/gene is most closely related to my sequence?
    • Viral phylodynamics: Trace outbreak spread and estimate transmission dates
    • Protein family analysis: Infer evolutionary relationships within a gene family
    • Horizontal gene transfer detection: Identify genes with discordant species/gene trees
    • Ancestral sequence reconstruction: Infer ancestral protein sequences
    • Molecular clock analysis: Estimate divergence dates using temporal sampling
    • GWAS companion: Place variants in evolutionary context (e.g., SARS-CoV-2 variants)
    • Microbiology: Species phylogeny from 16S rRNA or core genome phylogeny

    Standard Workflow

    1. Multiple Sequence Alignment with MAFFT

    import subprocess
    import os
    
    def run_mafft(input_fasta: str, output_fasta: str, method: str = "auto",
                   n_threads: int = 4) -> str:
        """
        Align sequences with MAFFT.
    
        Args:
            input_fasta: Path to unaligned FASTA file
            output_fasta: Path for aligned output
            method: 'auto' (auto-select), 'einsi' (accurate), 'linsi' (accurate, slow),
                    'fftnsi' (medium), 'fftns' (fast), 'retree2' (fast)
            n_threads: Number of CPU threads
    
        Returns:
            Path to aligned FASTA file
        """
        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}")
    
        # Count aligned sequences
        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 method selection guide:
    # Few sequences (<200), accurate: linsi or einsi
    # Many sequences (<1000), moderate: fftnsi
    # Large datasets (>1000): fftns or auto
    # Ultra-fast (>10000): mafft --retree 1
    

    2. Trim Alignment (Optional but Recommended)

    def trim_alignment_trimal(aligned_fasta: str, output_fasta: str,
                                method: str = "automated1") -> str:
        """
        Trim poorly aligned columns with TrimAl.
    
        Methods:
        - 'automated1': Automatic heuristic (recommended)
        - 'gappyout': Remove gappy columns
        - 'strict': Strict gap threshold
        """
        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}")
            # Fall back to using the untrimmed alignment
            import shutil
            shutil.copy(aligned_fasta, output_fasta)
        return output_fasta
    

    3. IQ-TREE 2 — Maximum Likelihood Tree

    def run_iqtree(aligned_fasta: str, output_prefix: str,
                    model: str = "TEST", bootstrap: int = 1000,
                    n_threads: int = 4, extra_args: list = None) -> dict:
        """
        Build a maximum likelihood tree with IQ-TREE 2.
    
        Args:
            aligned_fasta: Aligned FASTA file
            output_prefix: Prefix for output files
            model: 'TEST' for automatic model selection, or specify (e.g., 'GTR+G' for DNA,
                   'LG+G4' for proteins, 'JTT+G' for proteins)
            bootstrap: Number of ultrafast bootstrap replicates (1000 recommended)
            n_threads: Number of threads ('AUTO' to auto-detect)
            extra_args: Additional IQ-TREE arguments
    
        Returns:
            Dict with paths to output files
        """
        cmd = [
            "iqtree2",
            "-s", aligned_fasta,
            "--prefix", output_prefix,
            "-m", model,
            "-B", str(bootstrap),   # Ultrafast bootstrap
            "-T", str(n_threads),
            "--redo"                # Overwrite existing results
        ]
    
        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}")
    
        # Print model selection result
        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",  # Full report
            "model": f"{output_prefix}.model.gz",
        }
    
        print(f"IQ-TREE: Tree saved to {output_files['tree']}")
        return output_files
    
    # IQ-TREE model selection guide:
    # DNA:     TEST → GTR+G, HKY+G, TrN+G
    # Protein: TEST → LG+G4, WAG+G, JTT+G, Q.pfam+G
    # Codon:   TEST → MG+F3X4
    
    # For temporal (molecular clock) analysis, add:
    # extra_args = ["--date", "dates.txt", "--clock-test", "--date-CI", "95"]
    

    4. FastTree — Fast Approximate ML

    For large datasets (>1000 sequences) where IQ-TREE is too slow:

    def run_fasttree(aligned_fasta: str, output_tree: str,
                      sequence_type: str = "nt", model: str = "gtr",
                      n_threads: int = 4) -> str:
        """
        Build a fast approximate ML tree with FastTree.
    
        Args:
            sequence_type: 'nt' for nucleotide or 'aa' for amino acid
            model: For nt: 'gtr' (recommended) or 'jc'; for 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. Tree Analysis and Visualization with ETE3

    from ete3 import Tree, TreeStyle, NodeStyle, TextFace, PhyloTree
    import matplotlib.pyplot as plt
    
    def load_tree(tree_file: str) -> Tree:
        """Load a Newick tree file."""
        t = Tree(tree_file)
        print(f"Tree: {len(t)} leaves, {len(list(t.traverse()))} nodes")
        return t
    
    def basic_tree_stats(t: Tree) -> dict:
        """Compute basic tree statistics."""
        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:
        """Find the most recent common ancestor of a set of leaves."""
        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:
        """
        Render phylogenetic tree to image.
    
        Args:
            t: ETE3 Tree object
            color_groups: Dict mapping leaf_name → color (for coloring taxa)
            show_branch_support: Show bootstrap values
        """
        ts = TreeStyle()
        ts.show_leaf_name = True
        ts.show_branch_support = show_branch_support
        ts.mode = "r"  # 'r' = rectangular, 'c' = circular
    
        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:
        """Root tree at midpoint (use when outgroup unknown)."""
        t.set_outgroup(t.get_midpoint_outgroup())
        return t
    
    def prune_tree(t: Tree, keep_leaves: list) -> Tree:
        """Prune tree to keep only specified leaves."""
        t.prune(keep_leaves, preserve_branch_length=True)
        return t
    

    6. Complete Analysis Script

    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:
        """
        Complete phylogenetic pipeline: align → trim → tree → visualize.
    
        Args:
            input_fasta: Unaligned FASTA
            sequence_type: 'nt' (nucleotide) or 'aa' (amino acid/protein)
            use_fasttree: Use FastTree instead of IQ-TREE (faster for large datasets)
        """
        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)
    
        # Save rooted tree
        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 Model Guide

    DNA Models

    Model Description Use case
    GTR+G4 General Time Reversible + Gamma Most flexible DNA model
    HKY+G4 Hasegawa-Kishino-Yano + Gamma Two-rate model (common)
    TrN+G4 Tamura-Nei Unequal transitions
    JC Jukes-Cantor Simplest; all rates equal

    Protein Models

    Model Description Use case
    LG+G4 Le-Gascuel + Gamma Best average protein model
    WAG+G4 Whelan-Goldman Widely used
    JTT+G4 Jones-Taylor-Thornton Classical model
    Q.pfam+G4 pfam-trained For Pfam-like protein families
    Q.bird+G4 Bird-specific Vertebrate proteins

    Tip: Use -m TEST to let IQ-TREE automatically select the best model.

    Best Practices

    • Alignment quality first: Poor alignment → unreliable trees; check alignment manually
    • Use linsi for small (<200 seq), fftns or auto for large alignments
    • Model selection: Always use -m TEST for IQ-TREE unless you have a specific reason
    • Bootstrap: Use ≥1000 ultrafast bootstraps (-B 1000) for branch support
    • Root the tree: Unrooted trees can be misleading; use outgroup or midpoint rooting
    • FastTree for >5000 sequences: IQ-TREE becomes slow; FastTree is 10–100× faster
    • Trim long alignments: TrimAl removes unreliable columns; improves tree accuracy
    • Check for recombination in viral/bacterial sequences before building trees (RDP4, GARD)

    Additional Resources

    Reproducido de K-Dense-AI/scientific-agent-skills bajo licencia Unknown. Leer esta página en markdown.

    Archivos

    3 archivos en el paquete. Solo se lee SKILL.md al activarse — las referencias se cargan si el skill decide que las necesita.

    Antes de instalar

    Requiere instalar MAFFT, IQ-TREE 2 y FastTree vía conda, además de ete3 (con PyQt5 para renderizar imágenes).

    Detalles

    Creador
    K-Dense-AI
    Categoría
    Investigación
    Licencia
    Unknown
    Recursos incluidos
    scripts en python + referencias
    Código fuente
    Ver SKILL.md

    Etiquetas

    Más de K-Dense-AI/scientific-agent-skills

    Este repo incluye 163 skills. Si instalas uno, normalmente ya tienes los demás.

    Gestión integral de citas académicas: busca en OpenAlex, PubMed y Google Scholar, extrae metadatos precisos, valida citas y genera entradas BibTeX correctamente formateadas.

    Costo de contexto al activarse
    3.7k tok
    Tamaño del paquete
    21 archivos
    Última actualización
    hace 28 días
    investigacion

    Realiza revisiones bibliográficas sistemáticas y completas usando varias bases académicas (PubMed, arXiv, bioRxiv, Semantic Scholar). Genera markdown y PDF con citas verificadas en varios estilos (APA, Nature, Vancouver).

    Costo de contexto al activarse
    3.2k tok
    Tamaño del paquete
    12 archivos
    Última actualización
    hace 15 días
    investigacion

    Crea decks de diapositivas y presentaciones para charlas de investigación: PowerPoint, presentaciones de conferencia, seminarios, defensas de tesis. Da estructura, plantillas, guía de tiempos y validación visual.

    Costo de contexto al activarse
    5.1k tok
    Tamaño del paquete
    24 archivos
    Última actualización
    hace 15 días
    documentos

    Crea infografías profesionales con Nano Banana Pro AI y refinamiento iterativo inteligente. Usa Gemini 3.6 Flash para revisar la calidad e integra investigación con Perplexity Sonar. Soporta 10 tipos, 8 estilos y paletas para daltonismo.

    Costo de contexto al activarse
    2.7k tok
    Tamaño del paquete
    8 archivos
    Última actualización
    hace 15 días
    diseno ui

    Crea pósteres de investigación profesionales en LaTeX con beamerposter, tikzposter o baposter, para conferencias y comunicación científica: layout, colores, columnas múltiples e integración de figuras.

    Costo de contexto al activarse
    3.9k tok
    Tamaño del paquete
    17 archivos
    Última actualización
    hace 15 días
    documentos

    Crea diagramas científicos de calidad de publicación con la IA Nano Banana 2 y refinamiento iterativo inteligente. Gemini 3.6 Flash revisa la calidad y solo regenera si está por debajo del umbral de tu tipo de documento.

    Costo de contexto al activarse
    4.1k tok
    Tamaño del paquete
    6 archivos
    Última actualización
    hace 15 días
    diseno ui

    Skills relacionados

    Astropy

    34.8k

    Librería Python central para astronomía y astrofísica: unidades/cantidades, coordenadas, E/S de FITS, tablas, sistemas de tiempo, WCS y cosmología, para implementar o depurar código con Astropy.

    Costo de contexto al activarse
    3.6k tok
    Tamaño del paquete
    8 archivos
    Última actualización
    el mes pasado
    investigacion

    Integración con el SDK de Python de Benchling y su API REST para entidades del registro, inventario, entradas del cuaderno electrónico (ELN), workflows, Benchling Apps y consultas al Data Warehouse.

    Costo de contexto al activarse
    1.9k tok
    Tamaño del paquete
    6 archivos
    Última actualización
    el mes pasado
    investigacion

    Busca papers científicos y obtiene datos experimentales estructurados extraídos de estudios a texto completo vía el servidor MCP de BGPT: más de 25 campos por paper (métodos, resultados, muestras, calidad, conclusiones).

    Costo de contexto al activarse
    713 tok
    Tamaño del paquete
    1 archivo
    Última actualización
    el mes pasado
    investigacion