RIPGREP

https://img.shields.io/badge/wrapper_version-v9.8.0-10785b https://img.shields.io/github/issues-pr/snakemake/snakemake-wrappers/utils/ripgrep?label=version%20update%20pull%20requests&color=1cb481

ripgrep is a line-oriented search tool that recursively searches the current directory for a regex pattern. Faster than GNU grep.

URL: https://github.com/BurntSushi/ripgrep

Example

This wrapper can be used in the following way:

rule test_ripgrep_plain_text:
    input:
        # Optional path to pattern file
        # pattern="",
        # Optional path to .gitignore-styled list of file to ignore
        # ignore="",
        target="data.txt",
    output:
        "test_ripgrep_plain_text.txt",
    log:
        "test_ripgrep_plain_text.log",
    threads: 1
    params:
        pattern="hello",
        extra="",
    wrapper:
        "v9.8.0/utils/ripgrep"


rule test_ripgrep_pattern_file:
    input:
        # Optional path to .gitignore-styled list of file to ignore
        # ignore="",
        target="data.txt",
        pattern="pattern.txt",
    output:
        "test_ripgrep_pattern_file.txt",
    log:
        "test_ripgrep_pattern_file.log",
    threads: 1
    params:
        # Optional pattern can be provided alongside with pattern file
        # pattern="",
        # Optional path to .gitignore-styled list of file to ignore
        # ignore="",
        extra="",
    wrapper:
        "v9.8.0/utils/ripgrep"


rule test_ripgrep_compressed_input:
    input:
        # Optional path to pattern file
        # pattern="",
        # Optional path to .gitignore-styled list of file to ignore
        # ignore="",
        target="data.txt.gz",
    output:
        "test_ripgrep_compressed_input.txt",
    log:
        "test_ripgrep_compressed_input.log",
    threads: 1
    params:
        pattern="hello",
        extra="",
    wrapper:
        "v9.8.0/utils/ripgrep"


rule test_ripgrep_search_in_directory:
    input:
        # Optional path to pattern file
        # pattern="",
        # Optional path to files or directories to search into
        # target="",
        ignore=".ignore",
    output:
        "test_ripgrep_search_in_directory.txt",
    log:
        "test_ripgrep_search_in_directory.log",
    threads: 1
    params:
        pattern="hello",
        extra="",
    wrapper:
        "v9.8.0/utils/ripgrep"

Note that input, output and log file paths can be chosen freely.

When running with

snakemake --use-conda

the software dependencies will be automatically deployed into an isolated environment before execution.

Notes

  • Either input.pattern or params.pattern has to be provided.

Software dependencies

  • ripgrep=15.1.0

Input/Output

Input:

  • target: Optional list of files and/or directories to process

  • pattern: Optional path to pattern file

  • ignore: Optional path to .gitignore-styled list of files to ignore

Output:

  • Path to ripgrep result, depending on the command line

Params

  • pattern: Optional pattern to search in files and/or directories.

  • extra: Optional command line arguments besides –json, –file, –search-zip, –threads

Authors

  • Thibault Dayris

Code

# coding: utf-8

"""Snakemake wrapper for ripgrep"""

__author__ = "Thibault Dayris"
__copyright__ = "Copyright 2026, Thibault Dayris"
__email__ = "thibault.dayris@gustaveroussy.fr"
__license__ = "MIT"

from snakemake.shell import shell

extra = snakemake.params.get("extra", "")
log = snakemake.log_fmt_shell(stdout=False, stderr=True, append=True)

pattern_file = snakemake.input.get("pattern")
if pattern_file:
    extra += f" --file='{pattern_file}'"

pattern = snakemake.params.get("pattern")
if pattern:
    extra += f" --regexp='{pattern}'"

if not (pattern or pattern_file):
    raise ValueError("Either a `input.pattern` or `params.pattern` has to be provided")

ignore = snakemake.input.get("ignore")
if ignore:
    extra += f" --ignore-file='{ignore}'"

outfile = snakemake.output[0]
if str(outfile).endswith(".json"):
    extra += " --json"

compression_fmts = (".gz", ".lzma", ".bz2", ".xz", ".lz4", ".zst")
if any(str(i).endswith(compression_fmts) for i in snakemake.input):
    extra += " --search-zip"

input_target = snakemake.input.get("target", "")


# rg returns status 1 for "no matches", so this currently fails the Snakemake job
# instead of producing a valid empty result file. Below, we handle  status 1 as
# success while preserving real errors (status 2 and above).
shell(
    "rg --threads {snakemake.threads} {extra} "
    "{input_target} > {snakemake.output[0]:q} {log} "
    "|| if [ $? -eq 1 ]; then echo 'No match' {log}; else exit $?; fi "
)