I haven't found any straight forward method, but if someone has similar problem in the future; below is my proposed solution:
Note: Check and make sure the mapped alignment is sorted.
samtools view -h mapped.bam -o mapped.sam
grep '^@' mapped.sam > RG.header # this is important when there is header info in the sam/bam file
grep -v '^@' mapped.sam > SAM_file.sam # retains the non header part of the mapped file
awk '($5 < 40)' SAM_file.sam > Below.mapQ40.sam
cat RG.header Below.mapQ40.sam > Below.mapQ40_mapped.sam
samtools view -h -S -b Below.mapQ40_mapped.sam -o Below.mapQ40_mapped.bam
samtools index Below.mapQ40_mapped.bam
If you don't care about header info (or if its absent):
samtools view -h mapped.bam -o mapped.sam
awk '($5 < 40)' mapped.sam > Below.mapQ40_mapped.sam
samtools view -h -S -b Below.mapQ40_mapped.sam -o Below.mapQ40_mapped.bam
samtools index Below.mapQ40_mapped.bam
It may be possible to do this with one of the many programs from BBMap (perhaps reformat.sh). Brian Bushnell may be along later to provide that solution.