remotecomputer-bioinformatics

Step-by-Step Instructions: Exploring and Visualizing SAM/BAM Files

January 3, 2025 Off By admin
Shares

This guide provides instructions to set up, explore, and visualize SAM/BAM files using tools like samtools, IGV, and Tablet, as well as scripts in Python and R for custom exploration.


1. Install Necessary Tools

Ensure the required tools are installed on your system:

  • Samtools: Essential for handling SAM/BAM files.
    bash
    sudo apt update
    sudo apt install samtools
  • IGV (Integrative Genomics Viewer): Download and install from IGV Website.
  • Tablet: Download from Tablet Website.

2. View SAM/BAM Files Using Samtools

Use samtools tview for a lightweight, terminal-based viewer:

bash
samtools tview <file.bam> <reference.fasta>
  • To navigate to a specific position:
    php
    samtools tview <file.bam> <reference.fasta> -p chr1:10000

3. Visualization with IGV

Steps:

  1. Open IGV.
  2. Load a reference genome: Genome > Load Genome from File.
  3. Load the BAM file: File > Load from File.
  4. Navigate to regions of interest using the search bar (e.g., chr1:10000-20000).

4. Visualization with Tablet

  1. Open Tablet.
  2. Load your BAM file and associated index (.bai).
  3. Load your reference genome.
  4. Use navigation tools to explore mapped reads and SNPs.

5. Python Script for BAM File Parsing

You can parse and summarize BAM files using Python with pysam:

python
import pysam

# Open BAM file
bamfile = pysam.AlignmentFile("file.bam", "rb")

# Iterate through reads
for read in bamfile.fetch('chr1', 10000, 20000):
print(f"Read Name: {read.query_name}, Start: {read.reference_start}, CIGAR: {read.cigarstring}")

bamfile.close()


6. R Script for Visualization

Leverage R’s ggbio for BAM visualization:

R
library(GenomicAlignments)
library(ggbio)

# Load BAM file
bam_file <- "file.bam"
bam_data <- readGAlignments(bam_file)

# Plot coverage
autoplot(bam_data, layout = "linear")


7. Online and Cloud-Based Tools

  • UCSC Genome Browser: Upload BAM files for web-based visualization.
  • Genome Workbench (NCBI): Supports BAM files for integrated visualization.
  • ASCIIGenome: A command-line-based viewer suitable for lightweight visualization.

8. Additional Recommendations

Based on Use Cases:

  • For SNP Analysis: Use IGV or Tablet for detailed SNP visualization.
  • For Quick Checks: samtools tview is simple and efficient.
  • For Web Sharing: Use UCSC Genome Browser or install GBrowse2.

Recent and Updated Tools:


By following these steps, you can efficiently handle, explore, and visualize SAM/BAM files using both local and online tools. Each tool has its strengths, making them suitable for different tasks depending on your workflow.

Shares