g2nb / igv-jupyter

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

igv.js in a notebook: specify track from, eg, a pandas (bed-like) data.frame #2

Closed paul-shannon closed 8 years ago

paul-shannon commented 8 years ago

If, in a notebook, I manipulate a bed file, creating a subset, can I ask igv.js to load a local file, or perhaps even pass the data directly it a properly structured (bed formatted) pandas DataFrame or, in R, a traditional data.frame?

Thanks -

jrobinso commented 8 years ago

I assume you are running standalone Jupyter (i.e. python and its web server are running on your computer)? If so this should be possible using the python web server, but I don't know the details. You need a URL to the file that matches the protocol of the page itself (i.e. if the page is http the protocol can't be "file"). Javascript on a web page cannot load local files directly from a file path, there are some tricks using a special Load button but that wouldn't help here.

@tmtabor @turner -- any comments on this?

tmtabor commented 8 years ago

If you create a subset of a bed file and save it locally to your Jupyter workspace, you should be able load the file into igv.js easily enough. The URL for the file will be something like http://localhost:8888/files/foo.bed, assuming that you're running Jupyter on your local machine.

If you're working with your data in a pandas DataFrame, you'll need to write the data to disk first so it can be served up to igv.js as a file. (pandas.DataFrame.to_csv(sep='\t'))

paul-shannon commented 8 years ago

Thanks, Thorin and Jim,

Your advice got me pretty far, but not all the way. I write out a 13-line bed file, make sure that it loads and displays properly (even without an index) in igv desktop, then try in igv.js in a notebook.

There is no apparent error in the igv.js widget, and while the javascript console complains about the missing index, ti appears to be looking the bed file:

GET http://whovian:9999/edit/misc/foo.bed.idx 404 // clear failure XHR finished loading: GET "http://whovian:9999/edit/misc/foo.bed.idx Error accessing resource: 404
GET "http://whovian:9999/edit/misc/foo.bed” // appears to work without error, nothing displayed // except the track label

My code: myLocus = "chr21:25,860,335-25,862,720” igv = IGV(locus=myLocus, reference=Reference(id="hg19"), tracks=[Track( name="Genes", url="//s3.amazonaws.com/igv.broadinstitute.org/annotations/hg19/genes/gencode.v18.collapsed.bed", index_url="//s3.amazonaws.com/igv.broadinstitute.org/annotations/hg19/genes/gencode.v18.collapsed.bed.idx", display_mode="EXPANDED")])

footprintTrack = Track(name="foo", format="bed", url="http://whovian:9999/edit/misc/foo.bed”, display_mode='EXPANDED’)

igv.load_track(footprintTrack)

// Not finding an igv “goto” in the API, I manually set the display window so that it includes only the // objects in the bedfile.

Thanks for this great code. Sorry if it’s my own myopia which has gotten me confused.

On Aug 19, 2016, at 3:42 PM, Thorin Tabor notifications@github.com wrote:

If you create a subset of a bed file and save it locally to your Jupyter workspace, you should be able load the file into igv.js easily enough. The URL for the file will be something like http://localhost:8888/files/foo.bed, assuming that you're running Jupyter on your local machine.

If you're working with your data in a pandas DataFrame, you'll need to write the data to disk first so it can be served up to igv.js as a file. (pandas.DataFrame.to_csv(sep='\t'))

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

jrobinso commented 8 years ago

Hi, I'm not a Jupyter expert but here's a few suggestions for the next step (1) the search for the index (404) is harmless, but you can suppress it with the property "indexed: false" (2) do you have developer tools open? There could be network errors. Look at the server response to the bed "get" on the network tab of developer tools and see if there's anything there.
(3) is the page itself served from http://whovian:9999? If not you will have to deal with CORS. My Jupyter ignorance is showing here

The lack of a "goto" in the API is an oversight. I'll open an issue specifically for that and we'll add it.

jrobinso commented 8 years ago

Actually @paul-shannon the "goto" function is their and called "search_tracks". The name is misleading, we will change that to "search". The argument can be a gene name, e.g. MYC, or a locus in the format chr2:1000-2000.

paul-shannon commented 8 years ago

Thanks, Jim (and Tobin).

Here is the error message I see (odd - it’s from require.js), a few lines of code which distill the problem, with the actual notebook attached.

Maybe it will work for you!

require.js:900 ReferenceError: testTrack is not defined require.js:900enable require.js:1177init require.js:783(anonymous function) require.js:1453

In[1]:

from igv import IGV, Reference, Track

In[2]:

import pandas as pd

In[3]:

igv = IGV(locus="chr1:1-200", reference=Reference(id="hg19"), tracks=[Track( name="Genes", url="//s3.amazonaws.com/igv.broadinstitute.org/annotations/hg19/genes/gencode.v18.collapsed.bed", index_url="//s3.amazonaws.com/igv.broadinstitute.org/annotations/hg19/genes/gencode.v18.collapsed.bed.idx", display_mode="EXPANDED")])

In[4]:

tbl = pd.DataFrame([["chr1", "10", "100", "fp1", 9.9], ["chr1", 90, 130, "fp2", 0.5]])

In[12]:

tbl.to_csv("testTrack.bed", sep="\t", header=False, index=False)

In[13]:

newTrack = Track(name="testTrack", format="bed", indexed=False, url="http://localhost:8890/notebooks/testTrack.bed", display_mode='EXPANDED')

In[9]:

igv

In[14]:

igv.load_track("testTrack.bed”)

And as ipynb:

On Aug 19, 2016, at 8:28 PM, Jim Robinson notifications@github.com wrote:

Hi, I'm not a Jupyter expert but here's a few suggestions for the next step (1) the search for the index (404) is harmless, but you can suppress it with the property "indexed: false" (2) do you have developer tools open? There could be network errors. Look at the server response to the bed "get" on the network tab of developer tools and see if there's anything there.

(3) is the page itself served from http://whovian:9999? If not you will have to deal with CORS. My Jupyter ignorance is showing here

The lack of a "goto" in the API is an oversight. I'll open an issue specifically for that and we'll add it.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

paul-shannon commented 8 years ago

Forgive me: not Tobin, of course, but Thorin.

On Aug 20, 2016, at 8:39 AM, Paul Shannon paul.thurmond.shannon@gmail.com wrote:

Thanks, Jim (and Tobin).

Here is the error message I see (odd - it’s from require.js), a few lines of code which distill the problem, with the actual notebook attached.

Maybe it will work for you!

  • Paul

require.js:900 ReferenceError: testTrack is not defined require.js:900enable require.js:1177init require.js:783(anonymous function) require.js:1453

In[1]:

from igv import IGV, Reference, Track

In[2]:

import pandas as pd

In[3]:

igv = IGV(locus="chr1:1-200", reference=Reference(id="hg19"), tracks=[Track( name="Genes", url="//s3.amazonaws.com/igv.broadinstitute.org/annotations/hg19/genes/gencode.v18.collapsed.bed", index_url="//s3.amazonaws.com/igv.broadinstitute.org/annotations/hg19/genes/gencode.v18.collapsed.bed.idx", display_mode="EXPANDED")])

In[4]:

tbl = pd.DataFrame([["chr1", "10", "100", "fp1", 9.9], ["chr1", 90, 130, "fp2", 0.5]])

In[12]:

tbl.to_csv("testTrack.bed", sep="\t", header=False, index=False)

In[13]:

newTrack = Track(name="testTrack", format="bed", indexed=False, url="http://localhost:8890/notebooks/testTrack.bed", display_mode='EXPANDED')

In[9]:

igv

In[14]:

igv.load_track("testTrack.bed”)

And as ipynb:

On Aug 19, 2016, at 8:28 PM, Jim Robinson notifications@github.com wrote:

Hi, I'm not a Jupyter expert but here's a few suggestions for the next step (1) the search for the index (404) is harmless, but you can suppress it with the property "indexed: false" (2) do you have developer tools open? There could be network errors. Look at the server response to the bed "get" on the network tab of developer tools and see if there's anything there.

(3) is the page itself served from http://whovian:9999? If not you will have to deal with CORS. My Jupyter ignorance is showing here

The lack of a "goto" in the API is an oversight. I'll open an issue specifically for that and we'll add it.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

turner commented 8 years ago

Hi Paul, Your comment where you construct the URL http://localhost:8890/notebooks/testTrack.bed will work if you: change notebooks to files assuming you launched Jupyter from the directory containing testTrack.bed

The general solution:

  1. locate saved file on disk
  2. identify the directory you launch Jupyter from. When Jupyter launches it defines a server directory tree rooted at this location. Saved data files can reside anywhere within this tree.
  3. Create a URL with the following pattern: url="//host:port/files/path-from-server-launch-to-datafile-directory/myfile.bed"

Let me know if you have any further questions.

-Doug

paul-shannon commented 8 years ago

This works perfectly. Thanks for setting me on the right course.

On Aug 25, 2016, at 3:34 AM, Douglass Turner notifications@github.com wrote:

Closed #2.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.