github链接:https://github.com/brentp/mosdepth
单样本运行
mosdepth -n --fast-mode -t 4 EXAMPLE EXAMPLE.bam
批量运行脚本
run_mosdepth.sh:
#!/bin/bash
# 默认参数
THREADS=8
BY_FILE=""
# 解析命令行参数(支持 --by)
while [[ $# -gt 0 ]]; do
case $1 in
--by)
BY_FILE="$2"
shift 2
;;
*)
echo "Unknown option: $1" >&2
exit 1
;;
esac
done
# 如果提供了 --by,检查文件是否存在
if [[ -n "$BY_FILE" ]]; then
if [[ ! -f "$BY_FILE" ]]; then
echo "Error: --by file '$BY_FILE' does not exist." >&2
exit 1
fi
echo "Running in WES mode with target regions: $BY_FILE"
else
echo "Running in WGS mode (no --by provided)"
fi
# 查找当前目录下所有 *bqsr.bam 文件
shopt -s nullglob # 避免无匹配时返回字面 "*bqsr.bam"
for bam in ./*bqsr.bam; do
[[ ! -f "$bam" ]] && continue
prefix=$(basename "$bam" .bam)
# 判断是否已完成:只要存在 global.dist.txt 就跳过
if [[ -f "${prefix}.mosdepth.global.dist.txt" ]]; then
echo "Skipping $bam (already processed)"
continue
fi
echo "Processing $bam ..."
# 构建 mosdepth 命令
CMD="mosdepth -n --fast-mode -t $THREADS"
if [[ -n "$BY_FILE" ]]; then
CMD="$CMD --by \"$BY_FILE\""
fi
CMD="$CMD \"$prefix\" \"$bam\""
# 执行命令
eval $CMD
if [[ $? -eq 0 && -f "${prefix}.mosdepth.global.dist.txt" ]]; then
echo "✓ Finished $bam"
else
echo "✗ ERROR: mosdepth failed on $bam" >&2
fi
done
echo "All done."
使用
# WES 模式
nohup ./run_mosdepth.sh --by AgilentV6_GRCh38_ex_region.sort.filtered.bed > run_mosdepth.log 2>&1 &
# WGS 模式
nohup ./run_mosdepth.sh > run_mosdepth.log 2>&1 &