bwa-mem2 / bwa-mem2

The next version of bwa-mem
Other
721 stars 101 forks source link

bwa mem: resulting BAM "NM tag does not match reality" #175

Open reliscu opened 3 years ago

reliscu commented 3 years ago

I ran Picard's ValidateSamFile on my aligned human exome sequencing data, and encountered the following:

ERROR::INVALID_TAG_NM:Record 1, Read name HWI-D00328:58:H7EAEADXX:2:1108:10580:81442, NM tag (nucleotide differences) in file [2] does not match reality [4]
ERROR::INVALID_TAG_NM:Record 170972, Read name HWI-D00328:58:H7EAEADXX:2:2211:14392:16693, NM tag (nucleotide differences) in file [0] does not match reality [19]
ERROR::INVALID_TAG_NM:Record 1437790, Read name HWI-D00328:58:H7EAEADXX:1:2104:5326:19260, NM tag (nucleotide differences) in file [1] does not match reality [3]

... And so on for some 1500 records in total for this particular BAM file.

Looking at the first offending read in the BAM file (i.e. record 1):

HWI-D00328:58:H7EAEADXX:2:1108:10580:81442      177     chr1    9998    0       101M    =       198728581       198718584       CCATAACCATAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTA   ##############################B@;;2CD@??===)6(=8C>C9@C?3?9B?>AHEFDJJGHHFJIGHGBIJIGF?JIIHFFHHFDDBFFCC@      MC:Z:101M       MD:Z:1G6C92     RG:Z:1.1        NM:i:2  AS:i:94 XS:i:93

I then ran samtools calmd -bAr sorted.bam $ref > sorted_calmd.bam. E.g. First offending record:

[bam_fillmd1] different NM for read 'HWI-D00328:58:H7EAEADXX:2:1108:10580:81442': 2 -> 4
[bam_fillmd1] different MD for read 'HWI-D00328:58:H7EAEADXX:2:1108:10580:81442': '1G6C92' -> '0N0N0N5C92'

... and so forth for the remaining problem reads.

While samtools calmd seems to resolve the problem, I'd like to understand why this happens in the first place and whether this may point to a bug.

To be sure this NM/MD tag discrepancy wasn't the result of the merging process vs. the alignment, I ran ValidateSamFile on one of the BAM files prior to merging, and still encountered these errors.


Commands resulting in the offending BAM:

## Align
ref="/home/shared/hg_align_db/GRCh38_gencode_primary/GRCh38.primary_assembly.genome.fa"
bwa="/home/shared/programs/bwa-mem2/./bwa-mem2"
for sample in Sample*
  do cd /mnt/bdata/shared/SF9495/exome/raw_data/$sample
  for lane in "L001" "L002"
    do
    for ea in $(ls *R1* | grep "$lane"); 
      do 
      R1=$ea
      R2=$(echo $R1 | sed "s/R1/R2/")
      $bwa mem -t 10 -T 0 -R "@RG\tID:1\tLB:$sample\tPL:illumina\tSM:$sample\tPU:$lane" $ref $R1 $R2 | 
      samtools view --threads 10 -hb -o ../../bam/$(echo $ea | sed "s/fastq.gz/bam/")
    done;
  done;
done;

## Merge/sort
cd /mnt/bdata/shared/SF9495/exome/bam
picard="/home/shared/programs/Picard/picard.jar"
for sample in $(ls ../raw_data | grep Sample.* | sed "s/Sample_//")
  do sem -j 3
  bamlist=$(for f in "$sample"*; do echo -n "I=$f "; done)
  java -jar $picard MergeSamFiles $bamlist O=merged_sorted/"$sample"_sorted.bam
done;

Using bwa mem v. 2.2.1 Reference file is Gencode primary assembly (GRCh38)

cpuinfo:

processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 85
model name  : Intel(R) Xeon(R) W-2155 CPU @ 3.30GHz
stepping    : 4
microcode   : 0x2006906
cpu MHz     : 3600.205
cache size  : 14080 KB
physical id : 0
siblings    : 20
core id     : 0
cpu cores   : 10
apicid      : 0
initial apicid  : 0
fpu     : yes
fpu_exception   : yes
cpuid level : 22
wp      : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single pti intel_ppin ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts md_clear flush_l1d
bugs        : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs taa itlb_multihit
bogomips    : 6600.00
clflush size    : 64
cache_alignment : 64
address sizes   : 46 bits physical, 48 bits virtual
power management:

gcc -v

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 8.3.0-6' --with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-8 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 8.3.0 (Debian 8.3.0-6) 
robmaz commented 2 years ago

Have you checked if this NM count is already present in the original bwa mem v0.7.17 output, or is this a difference between the original mem and mem2?