g2nb / igv-jupyter

Extension for Jupyter which integrates igv.js
MIT License
154 stars 13 forks source link

IGV Browser with local reference genome fasta file #41

Closed Irsan88 closed 3 years ago

Irsan88 commented 3 years ago

Is there a possibility to use a local file as the genome assembly in a IGV browsers embedded in jupyter notebooks? What I have tried is below, but in my browser I get a pop-up saying "Error accessing resource: file:///path/to/my/genome_assembly.fasta.fai Status: 0

import igv

reference_config = {
    "id": "test",
    "name": "Local assembly",
    "fastaURL": "file://" + assembly_fasta_file,
    "indexURL": "file://" + assembly_fasta_file_index
  }

b = igv.Browser({"reference": reference_config})
b.show()

assembly_fasta_file is the full absolute path to my fasta file and assembly_fasta_file_index to the fasta.fai file

jrobinso commented 3 years ago

No, javascript cannot load from file:// urls, or know anything about your file system. There is a way to use "files" magic to let Jupyter server the files by relative URL, but you have to put them in a specific place. I forget the details of that, @tmtabor probably knows.

tmtabor commented 3 years ago

Assuming the file you wish to load is in the same directory as your notebook, you should be able load it with a Jupyter files URL. For example, if your notebook is running at http://localhost:8888/notebooks/foo.ipynb and your genome_assembly.fasta.fai is in the same directory, the URL for it would be http://localhost:8888/files/genome_assembly.fasta.fai. Note that it will only serve the file if the Referer on the request matches the Jupyter server.

jrobinso commented 3 years ago

@tmtabor So is the implication of your last remark is this only works if server and client (i.e. the web browser) are running on the same machine. If the Jupyter server is remote you would encounter a CORS issue.

tmtabor commented 3 years ago

It should work even if the Jupyter server is remote. It's just that /files/ endpoints check to make sure the request is actually coming from one of your notebooks before they serve the file's contents. I think this is meant to be a security measure to prevent Jupyter from becoming an arbitrary file host.

jrobinso commented 3 years ago

Ah yes, of course, "referer" will be the host for the page. Still this is a CORS restriction, it would fail regardless of any extra checks (if any) performed by Jupyter.

Irsan88 commented 3 years ago

I now keep the local files in the same directory as the .ipynb and use e.g. /files/genome.fasta , it works! Is there a possibility to convert the notebook to html and share it with others, including the packaging of embedded local files ?

jrobinso commented 3 years ago

@Irsan88 I can't think of anyway to do that but @tmtabor might have some ideas.

tmtabor commented 3 years ago

@Irsan88 There are some tools to do similar things, but I am unsure if any of them are exactly what you want. Either way, here are a couple links to help get you started: