Sambamba是一个高性能,高度并行,健壮和快速的工具(和库),用D编程语言编写,用于处理SAM和BAM文件。与samtools相比,其优势在于并行BAM读和写。
conda安装
conda install sambamba -y
# github: https://github.com/biod/sambamba
基本用法
# 创建.bai index
samtools index sample.bam
# 计算1000kb窗口reads数和平均覆盖度
sambamba depth window -w 1000 sample.sorted.bam > /path/sample.bam_read_depths.txt
窗口reads计数的Python封装
调用封装程序
python编写,-b参数为排序后的bam文件路径,-w为统计的窗口大小(kb)。
python reads_depth.py -b /path/Sample.sorted.bam
主程序
# reads_depth.py
import os
import optparse
from pathlib import Path
# 创建类
class ReadsDepth(object):
def __init__(self, bam_path: str, window_size: int) -> None:
path_obj = Path(bam_path)
# bam文件目录路径
self.result_dir_path = path_obj.parent
self.bam_path = bam_path
self.window_size = window_size
self.sample_name = str(path_obj.stem).split('.')[0]
# 输出文件路径
self.output_path = os.path.join(self.result_dir_path, self.sample_name + '.bam_reads_depths.txt')
self.bam_index()
self.reads_depth()
def bam_index(self):
# 检查bam文件index是否存在,不存在则创建
if not os.path.exists(self.bam_path + '.bai'):
os.system("samtools index {}".format(self.bam_path))
def reads_depth(self):
# 获取窗口reads计数和平均覆盖度
run_status = os.system("sambamba depth window -w {0} {1} > {2}".format(self.window_size, self.bam_path, self.output_path))
if __name__ == '__main__':
parser = optparse.OptionParser(usage='"%prog"', version="%prog V1.0")
parser.add_option("-b", "--bam-path", dest="bam_path", type=str, help="")
parser.add_option("-w", "--window-size", dest="window_size", type=int, default=1000, help="bp")
options, args = parser.parse_args()
reads_depth = ReadsDepth(bam_path=options.bam_path, window_size=options.window_size)
|