Skip to content

Instantly share code, notes, and snippets.

@ctokheim
Created March 13, 2015 20:09
Show Gist options
  • Save ctokheim/effcf8a442f0c7393657 to your computer and use it in GitHub Desktop.
Save ctokheim/effcf8a442f0c7393657 to your computer and use it in GitHub Desktop.
SAM/BAM tricks
#########################################
# Convert Sam to Bam with a header:
# -b output bam
# -S input sam
#########################################
samtools view -bS sample.sam > sample.bam
#########################################
# convert Sam to Bam without a header
# -b output bam
# -t input sam without header
########################################
samtools faidx ref.fa # ref.fa is the fasta of reference genome
samtools view -bt ref.fa.fai aln.sam > aln.bam # convert
##########################################
# Convert between string and integer flag in sam file
# -X output flag in string format
# -x output flag in integer format
##########################################
samtools view -X -S in.sam > out.sam
##########################################
# Convert tophat bam to sam with string flag and unique reads
# -h include header
# -X string flag
# -q 255 unique reads
##########################################
samtools view -hX -q 255 -o unique.sam accepted_hits.bam
#########################################
# Sort bam file by name
# -n sort by read name
# -o for stdout
#########################################
samtools sort -n sample.bam outfile
##############################################
# Get unique and properly paired reads from paired-end bam
# -hX include header and string flag
# -q 255 unique mapped reads
###############################################
samtools view -hX -q 255 accepted_hits.bam | awk -F"\t" '$1~/^@/ || $2~/P/' > unique.properly_paired.sam
###########################################
# Convert name sorted bam file to bed or bedpe
# -i input file or stdin
# -bedpe paired-end bam
# -split use N cigar
###########################################
bamToBed -i infile -bedpe -split > sample.bed
##########################################
# Sort bed file by position
##########################################
bedSort <in.bed> <out.bed>
#########################################
# Make normalized wig file from sam file
#########################################
samtools view -bh -S Ot2119-75nt/unique.Ot2119.75nt.sam | genomeCoverageBed -ibam stdin -bg -split | python2.7 normalized_wig.py 6.9 wig/Ot2119.75nt.normalized.wig
#########################################
# Convert gtf file to bed file
#
# gtfToGenePred is from ucsc binaries:
# gtfToGenePred <in.gtf> <out.genepred>
#
# genePredToBed is a perl script taking from a ucsc person post:
# <out.genepred> | genePredToBed > <out.bed>
########################################
./gtfToGenePred sample.gtf stdout | ./genePredToBed > sample.bed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment