PacBio BAM toolkit
大家平时处理 PacBio 测序数据(HiFi、CCS、Subreads)时,经常需要把 BAM 转 FASTQ、提取 HiFi reads、建索引、合并 BAM、按 Barcode 拆分...... 这些操作不用写复杂脚本 ,PacBio 官方早就给了一套轻量工具:pbtk(PacBio BAM Toolkit)。
bash
https://github.com/PacificBiosciences/pbtk #官网
01 获取与安装
pbtk 是 PacBio 官方维护的 BAM 处理工具集,最新版本直接通过 bioconda 安装即可:
conda install -c bioconda pbtk
安装、支持、许可证、版权等完整信息,可直接查看 PacBio 官方 pbbioconda 页面。
02 介绍
pbtk 这个仓库替代了原来 pbbam 里的独立工具。在 bioconda 里,pbtk 是 pbbam 的依赖项,所以你不会直接看到 pbbam 提供这些二进制工具,实际都来自 pbtk。
包含工具一览:
bam2fastabam2fastqccs-kinetics-bystrandifyextracthifipbindexpbindexdumppbmergezmwfilter
03 使用
1. bam2fastx 系列(bam2fasta /bam2fastq)
这两个工具界面完全一样 ,分别把 PacBio BAM / XML 转成 压缩 FASTA / FASTQ。
基础用法
# 输出 out.fasta.gz
bam2fasta -o out in.bam
bam2fasta -o out in.xml
# 输出 out.fastq.gz
bam2fastq -o out in_1.bam in_2.bam in_3.xml in_4.bam
常用参数
-
-u:不压缩(去掉 .gz) -
-c <int>:设置 gzip 压缩等级 -
-p/--seqid-prefix <str>:给每条序列 ID 加前缀 -
--split-barcodes:按 barcode 对拆分输出文件按 barcode 拆分输出
bam2fasta --split-barcodes -o out in1.bam in2.bam
2. ccs-kinetics-bystrandify
把带 CCS 动力学标签的 BAM/XML,转成带 pw / ip 标签的伪链文件,用于需要动力学信息的分析。
ccs-kinetics-bystrandify in.bam out.bam
ccs-kinetics-bystrandify in.xml out.xml
参数:
--min-coverage <int>:设置每条链最低测序轮数(passes)
3. extracthifi
最简单的 HiFi reads 提取工具 自动抽取 准确率 > QV20(0.99) 的序列。
extracthifi in.bam out.bam
4. pbindex
给 PacBio BAM 建立索引(.pbi),实现随机访问。
pbindex in.bam
# 自动生成 in.bam.pbi
5. pbindexdump
把 PBI 索引文件转成 JSON 或 C++ 格式,方便查看 / 调试。
pbindexdump in.bam.pbi > out.json
pbindexdump --format cpp in.bam.pbi > out.cpp
参数:
-
--json-indent-level <int>:JSON 缩进 -
--json-raw:更贴近 PBI 原始结构 -
--zmws-only:只输出 ZMW hole number 文本列表pbindexdump --zmws-only in.bam.pbi > out.txt
6. pbmerge
合并多个 PacBio BAM/XML,支持直接输入文件、XML、文件列表(fofn)。
pbmerge in1.bam in2.bam in3.bam > out.bam
pbmerge -o out.bam in.xml
pbmerge in.fofn > out.bam
参数:
--no-pbi:不自动生成索引
7. zmwfilter
最强大的 BAM/FASTA 过滤工具,支持按 ZMW、reads 名、抽样、过滤低质量等。
按 ZMW 编号过滤(最常用)
# 保留指定 ZMW
zmwfilter --include 1,2,4,8,16 in.bam out.bam
zmwfilter --include hole_numbers.txt in.fasta out.fasta
# 排除指定 ZMW
zmwfilter --exclude 42 in.xml out.bam
zmwfilter --exclude hole_numbers.txt in.xml out.fastq
查看文件中所有 ZMW
zmwfilter --show-all in.bam > out.txt
按 reads 名称过滤(仅 BAM/XML)
zmwfilter --names read_names.txt in.bam out.bam
随机下采样(只支持 BAM/XML)
# 按比例抽样
zmwfilter --downsample 0.333 in.xml out.bam
# 按条数抽样 + 固定种子(可重复)
zmwfilter --downsample-count 1024 --downsample-seed 42 in.bam out.bam
按测序轮数(passes)过滤
zmwfilter --num-passes 2 --include hole_numbers.txt in.bam out.bam
重要注意事项
以下参数互斥,不能同时用 :--include / --exclude / --show-all / --names / --downsample / --downsample-count
04 总结
pbtk 是 PacBio 数据处理必备轻量工具集,不用复杂代码,日常高频需求全覆盖:
- BAM ↔ FASTQ/FASTA
- 提取 HiFi
- 建索引 / 查看索引
- 合并 / 拆分 / 过滤 BAM
- 按 ZMW、barcode、reads 名筛选
如果你做三代测序(HiFi / IsoSeq),这套工具基本是标配。