GangCaoLab / CoolBox

Jupyter notebook based genomic data visualization toolkit.
https://gangcaolab.github.io/CoolBox/index.html
GNU General Public License v3.0
224 stars 37 forks source link

Error when plotting SNP track with no SNPs in plot range #74

Open alexjcornish opened 1 year ago

alexjcornish commented 1 year ago

Hi there, thanks for such a useful package.

I have encountered a bug when I try to plot a SNP track containing no SNPs in the plotted region.

If I am working with a TSV file:

chr4    15730398    1.6493369536091678e-05
chr4    15730146    1.6493369536091678e-05
chr4    15737348    1.3897495444980216e-05
chr4    15737101    1.3897495444980216e-05

And I try to plot a track like this:

import coolbox
from coolbox.api import *

snp_path = 'data.snp.bgz'
#test_range_no_error = 'chr4:15730000-15738000'
test_range_error = 'chr4:15720000-15728000'

frame = XAxis() + SNP(snp_path, col_chrom=0, col_pos=1, col_pval=2)
frame.plot(test_range_error)

Then I get the error:

[ERROR:frame.py:215 -                 plot()] Error occured when plot track:
    track name: SNP.14
    track type:<class 'coolbox.core.track.hist.snp.SNP'>
    Error: <class 'KeyError'> 'score'
    occurred in "/opt/conda/lib/python3.8/site-packages/coolbox/core/frame/frame.py", line 209
[ERROR:frame.py:222 -                 plot()] 'score'
Traceback (most recent call last):
  File "/opt/conda/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3080, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas/_libs/index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 4554, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 4562, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'score'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/conda/lib/python3.8/site-packages/coolbox/core/frame/frame.py", line 209, in plot
    track.plot(ax, copy(gr), gr2=copy(gr2))
  File "/opt/conda/lib/python3.8/site-packages/coolbox/core/track/hist/base.py", line 101, in plot
    data = self.fetch_plot_data(gr, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/coolbox/core/track/hist/snp.py", line 75, in fetch_plot_data
    df['score'] = self.transform_fn()(df['score'])
  File "/opt/conda/lib/python3.8/site-packages/pandas/core/frame.py", line 3024, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/opt/conda/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3082, in get_loc
    raise KeyError(key) from err
KeyError: 'score'

Ideally, plotting a range containing no SNPs would just return an empty track?