Question: IGV Web App inside Flask Server
gravatar for milind_ag
15 months ago by
milind_ag10 wrote:


Has anyone here ever used the IGV javascript web app, and tried to embed it inside a flask server? I'm having trouble with uploading custom user defined BAM/BAI files.

igv bam alignment flask • 753 views
ADD COMMENTlink modified 3 months ago by Jim Robinson290 • written 15 months ago by milind_ag10

Hi, I was successful in running and using the igv.js for my purposes, however I didn't use BAM/BAI. I would suggest to read carefully and/or post an issue to From my experience, the response time is quite fast.

ADD REPLYlink written 15 months ago by massa.kassa.sc3na270

Hi, thanks for responding. They have indeed been responsive but this is something that they, unfortunately, don't support out of the box. IGV.js works great within the node app they built, but its hard to use the JS library inside custom servers, ex. Flask. Thanks again!

ADD REPLYlink written 15 months ago by milind_ag10

Well, I didn't made it clear, but I'm using the igv.js inside the Flask server ;).

So I've got the impression that the bam/bai was the main issue.

ADD REPLYlink modified 15 months ago • written 15 months ago by massa.kassa.sc3na270

Haha the bam/bai was indeed the main issue. The files are so huge that I can't perform an actual upload. IGV.js would need to "stream" these files from a cross origin resource/server, depending on the request window (chr1: 10000-10020) . I was hoping someone knew how to do that from scratch. Their web app supports this functionality, but in-housing it is preferred due to stability reasons.

And if you're not supporting bam/bai, then what does your flask/igv hybrid do? I'm curious. ;)

ADD REPLYlink written 15 months ago by milind_ag10

I need to show genomic loci with reference and custom annotations for multiple (eg 100) sequences at once (multiple ivg instances, one web page).

To the "stream", if you mean that igv needs to read only part of the file. I've done just that - but for indexed gff and fasta files. I've needed to be able to show genomes with automatically added tracks, so the data server is the same flask server. So no cross origin requests.

def igv_genome_load(genomefile):
    # serve genome files (from db, read only)
    # resolve file you want to load from <genomefile> somehow (this must be the file the igv.js is asking for) 
    range_header = request.headers.get('Range', None)

    if not range_header:
        return send_from_directory(pp, genomefile)

    return ranged_data_response(range_header, genomefile)

# from the igv.js server
def ranged_data_response(range_header, file_path):
    m ='(\d+)-(\d*)', range_header)
    if not m:
        return "Error: unexpected range header syntax: {}".format(range_header)
    size = os.path.getsize(file_path)
    offset = int(
    length = int( or size) - offset + 1
    data = None
    with open(file_path, 'rb') as f:
        data =
    rv = Response(data, 206, mimetype="application/octet-stream", direct_passthrough=True)
    rv.headers['Content-Range'] = 'bytes {0}-{1}/{2}'.format(offset, offset + length-1, size)
    return rv

From the igv.js documentation I gues that you will need the bam mimetype

ADD REPLYlink modified 15 months ago • written 15 months ago by massa.kassa.sc3na270

Oh wow, that's a lot of sequences at once! And thanks for sharing your code, this is super helpful. I will try to adapt it for BAM/BAI files. I do have to make cross-origin requests so we'll see how that works out.

ADD REPLYlink written 15 months ago by milind_ag10
gravatar for Jim Robinson
3 months ago by
Jim Robinson290
United States
Jim Robinson290 wrote:

Hi, sorry I missed this. Please post igv questions to one of our forums, we are very responsive and if we're not post again.

igv.js works fine with flask for BAM files or any other file type igv.js supports. We have a project with it embedded out-of-the box at

ADD COMMENTlink written 3 months ago by Jim Robinson290
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 823 users visited in the last hour