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

Problem with displaying Ideograms #61

Closed nneretti closed 2 years ago

nneretti commented 2 years ago

There appears to be a problem when plotting ideograms. I did not see any examples for that in the tutorial, but when I try to use the available test data I get the following error, which seems to be linked to the 'band_name' key:

import coolbox
from coolbox.api import *
DATA_DIR = "../../../data/coolbox_test_data"
TEST_RANGE = "chr9:4000000-6000000"
ideogram_path = f"{DATA_DIR}/hg19_ideogram.txt"
frame = XAxis() + Ideogram(ideogram_path,show_name=False) # input a file path
frame.plot(TEST_RANGE)  # input a genome range

[ERROR:frame.py:217 - plot()] Error occured when plot track: track name: Ideogram.11 track type:<class 'coolbox.core.track.ideogram.Ideogram'> Error: <class 'KeyError'> 'band_name' occurred in "/Users/nicolaneretti/anaconda3/envs/gisim/lib/python3.7/site-packages/coolbox/core/frame/frame.py", line 206 [ERROR:frame.py:219 - plot()] 'band_name' Traceback (most recent call last): File "/Users/nicolaneretti/anaconda3/envs/gisim/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 3361, in get_loc return self._engine.get_loc(casted_key) File "pandas/_libs/index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas/_libs/hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'band_name'

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

Traceback (most recent call last): File "/Users/nicolaneretti/anaconda3/envs/gisim/lib/python3.7/site-packages/coolbox/core/frame/frame.py", line 206, in plot track.plot(ax, copy(gr), gr2=copy(gr2)) File "/Users/nicolaneretti/anaconda3/envs/gisim/lib/python3.7/site-packages/coolbox/core/track/ideogram.py", line 97, in plot band_name, band_type = row['band_name'], row['band_type'] File "/Users/nicolaneretti/anaconda3/envs/gisim/lib/python3.7/site-packages/pandas/core/series.py", line 942, in getitem return self._get_value(key) File "/Users/nicolaneretti/anaconda3/envs/gisim/lib/python3.7/site-packages/pandas/core/series.py", line 1051, in _get_value loc = self.index.get_loc(label) File "/Users/nicolaneretti/anaconda3/envs/gisim/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 3363, in get_loc raise KeyError(key) from err KeyError: 'band_name'

nneretti commented 2 years ago

So, I looked into it a little more and I got it to work with the following modifications:

1) Line 86 in coolbox/core/track/ideogram.py needs to be modified to the following: fields = ['chrom', 'start', 'end', 'band_name', 'band_type']

2) The optional show_name argument appears not to be working properly, and a show_band_name argument appears to be required instead (e.g. it is used explicitly in line line 101 of ideogram.py). show_band_name needs to be set to 'no' for no band names, and any other string to include band names in the ideogram. Also, it does not display nicely when using band names, as the x-range is stretched to accommodate the band names. E.g. with:

TEST_RANGE = "chr9:4000000-6000000"
ideogram_path = f"{DATA_DIR}/hg19_ideogram.txt"
frame = XAxis() + Ideogram(ideogram_path,show_band_name='yes')
Nanguage commented 2 years ago

Thanks for report this problem, I have fixed the bug and added an ideogram usage example in the gallery.

You can update from the source to use this fix.

$ git clone --depth=1 https://github.com/GangCaoLab/CoolBox.git
$ cd Coolbox
$ pip install .