Skip to content

Instantly share code, notes, and snippets.

@dfjenkins3
Last active March 2, 2023 07:09
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save dfjenkins3/5c6dc1e55e78fa17b931 to your computer and use it in GitHub Desktop.
Save dfjenkins3/5c6dc1e55e78fa17b931 to your computer and use it in GitHub Desktop.
IGV Batch Screenshots
snapshotDirectory ./examples
goto chr1:25158588-25161609
snapshot chr1_25158688_25161509_slop100.png
goto chr1:26489713-26490238
snapshot chr1_26489813_26490138_slop100.png
goto chr1:27935252-27935613
snapshot chr1_27935352_27935513_slop100.png
goto chr1:28247700-28248208
snapshot chr1_28247800_28248108_slop100.png
goto chr1:31574966-31577431
snapshot chr1_31575066_31577331_slop100.png
goto chr1:35101322-35112076
snapshot chr1_35101422_35111976_slop100.png
chr1 25158688 25161509
chr1 26489813 26490138
chr1 27935352 27935513
chr1 28247800 28248108
chr1 31575066 31577331
chr1 35101422 35111976

IGV Batch Screenshots

IGV provides functionality that allows a user to create a script to take screenshots of regions of interest.

Table of Contents

  1. Requirements
  2. Workflow
  3. Create BED File
  4. bedToIgv
  5. Load IGV Session
  6. Optional: Save IGV Session
  7. Run Batch Script
  8. Screenshots Across Multiple BAM Files
  9. Additional Information

Requirements

  1. bedtools
  2. IGV version 1.5 or higher

Workflow

  1. Create a bed file of regions of interest
  2. Use bedToIgv to convert the bed file into an IGV batch script
  3. Open IGV and load the files that you want to visualize
  4. Run the IGV batch script

Create BED File

The easiest way to define the regions of interest is to create a BED file of the regions. IGV will navigate to the exact coordinates in the bed file. Often you will want to pad the regions for visualization. You can either pad the BED records directly or use the bedToIgv script below to pad the records.

Example:

chr1    25158688    25161509
chr1    26489813    26490138
chr1    27935352    27935513
chr1    28247800    28248108
chr1    31575066    31577331
chr1    35101422    35111976

bedToIgv

After creating the BED file, the bedtools igv (bedToIgv) tool can be used to convert the bed file into an IGV batch script. At a minimum, you should supply a BED file to bedToIgv, but you may want to specify an output directory (-path), how to sort the screenshot (-sort), or how much padding to add to the records (-slop).

Usage:

Tool:    bedtools igv (aka bedToIgv)
Version: v2.21.0
Summary: Creates a batch script to create IGV images
         at each interval defined in a BED/GFF/VCF file.

Usage:   bedtools igv [OPTIONS] -i <bed/gff/vcf>

Options:
        -path   The full path to which the IGV snapshots should be written.
                (STRING) Default: ./

        -sess   The full path to an existing IGV session file to be
                loaded prior to taking snapshots.

                (STRING) Default is for no session to be loaded.

        -sort   The type of BAM sorting you would like to apply to each image.
                Options: base, position, strand, quality, sample, and readGroup
                Default is to apply no sorting at all.

        -clps   Collapse the aligned reads prior to taking a snapshot.
                Default is to no collapse.

        -name   Use the "name" field (column 4) for each images filename.
                Default is to use the "chr:start-pos.ext".

        -slop   Number of flanking base pairs on the left & right of the image.
                - (INT) Default = 0.

        -img    The type of image to be created.
                Options: png, eps, svg
                Default is png.

Notes:
        (1)  The resulting script is meant to be run from within IGV.
        (2)  Unless you use the -sess option, it is assumed that prior to
                running the script, youve loaded the proper genome and tracks.

Example:

[dfj@helium]~/>bedToIgv -path ./examples -slop 100 -i example.bed > example.batch
[dfj@helium]~/>cat example.batch
snapshotDirectory ./examples
goto chr1:25158588-25161609
snapshot chr1_25158688_25161509_slop100.png
goto chr1:26489713-26490238
snapshot chr1_26489813_26490138_slop100.png
goto chr1:27935252-27935613
snapshot chr1_27935352_27935513_slop100.png
goto chr1:28247700-28248208
snapshot chr1_28247800_28248108_slop100.png
goto chr1:31574966-31577431
snapshot chr1_31575066_31577331_slop100.png
goto chr1:35101322-35112076
snapshot chr1_35101422_35111976_slop100.png

Load IGV Session

The IGV batch script is run within IGV. Load the bam, vcf, and BED files that you want to visualize.

example session

Optional: Save IGV Session

It is possible to load a saved IGV session, although in the past I have found this to sometimes behave erratically. This is useful if you are interested in creating screenshots from multiple bam files in the same script.

save session

Run Batch Script

Once you have the window in the desired form, you can run the batch script. Under the "Tools" menu select "Run Batch Script..."

run batch script

Screenshots Across Multiple BAM Files

It is also possible to take screenshots across several files by loading them sequentially in the batch file, but bedToIgv does not support this. In this case the IGV batch script should be created directly. The following example takes a screenshot of the BRCA1 region across three bam files:

new
snapshotDirectory /path/to/screenshot/directory
load /path/to/bam1/bam1.bam
goto chr17:41194312-41279500
snapshot bam1.BRCA1.png
new
load /path/to/bam2/bam2.bam
goto chr17:41194312-41279500
snapshot bam2.BRCA1.png
new
load /path/to/bam3/bam3.bam
goto chr17:41194312-41279500
snapshot bam3.BRCA1.png

Additional Information

Additional information can be found on the IGV website

If you want to modify the screenshot in a way that is not available using the bedToIgv tool, a list of available commands can be found here. These commands can be added directly to the IGV batch script.

@sentisci
Copy link

hey , when I am using this script on windows, I can see that snapshots are taken but I can't find them in snapshotDirectory. Where are they stored ultimately ?

@dfjenkins3
Copy link
Author

Note: I’m not maintaining this workflow anymore, check the IGV and bedtools docs for possible changes to this functionality and modern examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment