生信喵 发表于 2022-7-1 14:53:10

sam和bam处理案例

samtools使用案例演示
mkdir 52.samtools;cd 52.samtools;
cp ../52.bwa/mgh78578.sam all.sam

#1 sam文件验证
samtools quickcheck *.sam&& echo 'all ok' || echo 'fail!'

#2 sam和bam格式转换
samtools view -O bam -o all.bam all.sam
samtools view all.bam -o all.sam
#转换成cram格式,很少用
samtools view -O cram -o all.cram all.sam -T MGH78578.fasta

#3 bam排序
samtools sort -@ 4 -m 12G -O bam -o all.sorted.bam all.sam

#4 排序后建立索引
samtools index all.sorted.bam

#5 比对结果统计
samtools stats all.sorted.bam

#6 按照flag值进行统计
samtools flagstat all.sorted.bam

#7 按不同染色体统计
samtools idxstats all.sorted.bam

#8 统计目标区域覆盖度
samtools bedcov test.bed all.sorted.bam

#9 统计每个位点测序深度depth
samtools depth all.sorted.bam

#10计算覆盖比率
samtools coverage all.sorted.bam

#11 按照染色体拆分bam
bamtools split -in all.sorted.bam -reference
拆成双端和单端
bamtools split -in all.sorted.bam -paired -stub un

#12 合并bam
samtools cat -o cat.bam all.sorted.REF_contig_1_pilon.bam all.sorted.REF_contig_2_pilon.bam all.sorted.REF_contig_3_pilon.bam all.sorted.REF_unmapped.bam
samtools merge merge.bam all.sorted.REF_contig_1_pilon.bam all.sorted.REF_contig_2_pilon.bam all.sorted.REF_contig_3_pilon.bam all.sorted.REF_unmapped.bam
#13 统计bam并绘图
samtools stats all.sorted.bam >all.stats
plot-bamstats -p test all.stats

#14 重新打乱
samtools collate all.sorted.bam -o all.shuffle.bam

#15 过滤数据
#将没有比对上的reads筛选出来
samtools view -f 4 all.sorted.bam

#16 保留比对上的reads输出出来
samtools veiw -F 4 all.sorted.bam

#17 输出fastq格式
samtools fastq all.sorted.bam -1 A.1.fq.gz -2 A.2.fq.gz
#输出fasta格式
samtools fasta all.sorted.bam -1 A.1.fa.gz -2 A.2.fa.gz

#18 文本方式查看结果tview
samtools tview all.sorted.bam
samtools tview all.sorted.bam ref.fna

#19 bam转换为bed格式
bedtools bamtobed -i all.sorted.bam

#20 mpileup
samtools mpileup --reference ../data/mgh78578.fastaall.sorted.bam
页: [1]
查看完整版本: sam和bam处理案例