Embed IGV.js in your app to visualize genomics data.
Project description
gradio-igv
Embed IGV.js in your app to visualize genomics data.
Installation
pip install gradio_igv
Quickstart
Here is a quickstart to make an app which views a CRAM file in IGV and displays a table of the first 20 visible reads.
import gradio as gr
from gradio_igv import IGV, IGVContext, AlignmentTrackLoad, FeatureContext, parse_locus
import pandas as pd
public_cram = "https://s3.amazonaws.com/1000genomes/data/HG00103/alignment/HG00103.alt_bwamem_GRCh38DH.20150718.GBR.low_coverage.cram"
default_igv_context = IGVContext(
genome="hg38",
).update_locus("BRCA1").add_track(
AlignmentTrackLoad(
name="HG00103",
url=public_cram,
indexURL=f"{public_cram}.crai",
order=1,
height=200,
colorBy="strand",
oauthToken=None, # Public file so no auth needed; otherwise inferred by URL type using environment
)
)
def summarize_visible_alignments(igv_context):
loci = parse_locus(igv_context.locus)
feature_ctx = FeatureContext(
files=[public_cram],
names=["HG00103"],
loci=loci,
)
reads = list(feature_ctx.features["HG00103"])
df = pd.DataFrame({
"Read Name": [read.query_name for read in reads],
"Pos": [read.reference_start for read in reads],
"MAPQ": [read.mapq for read in reads],
}).sort_values(by='Pos')
return df.head(20)
with gr.Blocks() as demo:
with gr.Row():
with gr.Column(scale=3):
igv_component = IGV(value=default_igv_context, label="IGV Browser")
with gr.Column(scale=1):
alignment_summary = gr.DataFrame(value=pd.DataFrame(), label="Alignment Summary", max_height=800)
igv_component.locuschange(summarize_visible_alignments, [igv_component], [alignment_summary])
if __name__ == "__main__":
demo.launch()
Usage
The library contains a few fundamental classes that work together to manipulate the IGV.js instance in your app.
IGV
This is the main component that you will use to embed IGV.js in your app. It takes an IGVContext object as input and returns an IGVContext object as output. The IGVContext object is used to configure the IGV instance.
IGVContext
This object is used to configure the IGV instance. It should be initialized using a choice of reference genome (generally either hg19 or hg38, but you can also use the ReferenceGenome object to link to a custom fasta). You can then update the locus, add tracks, and configure the IGV instance using the following methods:
update_locus: Update the current locus of the IGV instance using a string.add_track: Add a track to the IGV instance using aTrackLoadobject.remove_track: Remove a track from the IGV instance using a string.update_genome: Update the reference genome of the IGV instance using a string or aReferenceGenomeobject.
TrackLoad
The TrackLoad class has multiple classes which inherit from it that are meant to be used to facilitate loading and configuring different tracks. The defaults used are meant to reflect those of IGV.js. The following classes are available:
AnnotationTrackLoad: Load an annotation track using e.g. GFF, GTF, or BED file.AlignmentTrackLoad: Load an alignment track using a CRAM or BAM file.VariantTrackLoad: Load a variant track using a VCF or BCF file.
At the moment, these are the only types of tracks supported to upload. Feel free to open an issue if you have a use case for more!
To see the full customizable parameters, check the input fields and compare with the IGV.js documentation. Note that OAuth token generation should be handled automatically based on the URL provided if it lives in GCloud, Azure, or AWS and assuming your environment is configured appropriately to access the file.
FeatureContext
This is a convenience object you can create using the same filenames and loci as the IGVContext to fetch features from the files. This is useful for fetching features from the files that are currently visible in the IGV instance, like in the Quickstart example above. It works well with the parse_locus method as in the Quickstart example. The features attribute is a dictionary of lists of features, where the keys are the names provided for each file and the values are iterables over the visible features. This uses pysam under the hood for the genomics files IO.
Recipes
Be sure to check out the recipes directory for more full examples of how to use the library, along with some tools that might be useful, including:
- SV-Breakpoint-Visualizer: A tool to visualize structural variant breakpoints in IGV with statistics on reads covering the edges.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gradio_igv-0.0.1.tar.gz.
File metadata
- Download URL: gradio_igv-0.0.1.tar.gz
- Upload date:
- Size: 3.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e005d728dee0f31b40f799a0fa159c17925d030ef66b664547f910b20d069f7
|
|
| MD5 |
fbce7f776fd1adcf71b4ca18bfae0831
|
|
| BLAKE2b-256 |
4469b8aec40e3fbe2bc8604e2e5eb846f3388ff3451eb9e1595c9b10215bfbee
|
File details
Details for the file gradio_igv-0.0.1-py3-none-any.whl.
File metadata
- Download URL: gradio_igv-0.0.1-py3-none-any.whl
- Upload date:
- Size: 1.6 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c7389aa0c8facee26dcb58601484e0f19d92aa234180a10f011a724cc9be45f0
|
|
| MD5 |
117aad235de78fba5f98c7f4a2c52869
|
|
| BLAKE2b-256 |
247057e9cfe467b14ee2e427e3d4b77372fbf8270913a7fef7474d602d3bbc9d
|