Skip to content

KevinZ的小窝

Menu
  • Home
  • Categories
Menu

使用 mosdepth 统计 WES/WGS 测序深度

Posted on 2026年 2月 6日2026年 4月 16日 by KevinZhou

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 &
2026 年 2 月
一 二 三 四 五 六 日
 1
2345678
9101112131415
16171819202122
232425262728  
« 12 月    

俺家的猫~

胖达~

© 2026 KevinZ的小窝 |

粤ICP备2023017690号

|

粤公网安备 44010402003004号