bam2sam
-h: include header in SAM output
-O: Specify output format (SAM, BAM, CRAM)
-S: input format is auto-detected
samtools view -h test.bam -O SAM -o test.sam
sam2bam
samtools view -S -O BAM test.sam -o test.bam
sam2fastq
-c: compression level
-@: threads
-t: copy RG, BC and QT tags to the FASTQ header line
~/miniconda3/bin/samtools fastq test.sam -c 6 -@ 10 -1 reads_1.fq.gz -2 reads_2.fq.gz
bam2fastq
~/miniconda3/bin/samtools fastq test.bam -c 6 -@ 10 -1 reads_1.fq.gz -2 reads_2.fq.gz
samtools fasta -F 4 test.sam -@ 20 -1 mapped_reads_1.fa -2 mapped_reads_2.fa
extract FLAG
# from sam
~/miniconda3/bin/samtools view -S -O BAM -f 4 test.sam -o unmapped.bam
~/miniconda3/bin/samtools view -S -O SAM -f 4 test.sam -o unmapped.sam
~/miniconda3/bin/samtools fastq -c 6 -@ 10 -f 4 test.sam -1 reads_unmapped_1.fq.gz -2 reads_unmapped_2.fq.gz
# from bam
~/miniconda3/bin/samtools view -S -O BAM -f 4 test.bam -o unmapped.bam
~/miniconda3/bin/samtools view -S -O SAM -f 4 test.bam -o unmapped.sam
~/miniconda3/bin/samtools fastq -c 6 -@ 10 -f 4 test.bam -1 reads_unmapped_1.fq.gz -2 reads_unmapped_2.fq.gz
generate text files with alignment regions
COLUMNS=200
COLUMNS=$COLUMNS ~/miniconda3/bin/samtools tview -d T -p chr1:1000 sorted.bam hap1_chrom.fa
Reference
https://en.wikipedia.org/wiki/Pileup_format
https://www.samformat.info/sam-format-flag