Skip to content

Commit

Permalink
Dbbact metadata (#209)
Browse files Browse the repository at this point in the history
* add database data to databases attribute

* add minimal db version test

* fix databases default value
  • Loading branch information
amnona committed Jul 11, 2020
1 parent c516e73 commit 556bdf9
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 13 deletions.
10 changes: 7 additions & 3 deletions calour/amplicon_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class AmpliconExperiment(Experiment):
sparse : bool
store the data array in :class:`scipy.sparse.csr_matrix`
or :class:`numpy.ndarray`
databases: iterable of str, optional
database interface names to show by default in heatmap() function
by default use 'dbbact'
Attributes
----------
Expand All @@ -70,15 +73,16 @@ class AmpliconExperiment(Experiment):
information about the experiment (data md5, filenames, etc.)
description : str
name of the experiment
databases : iterable of str
databases for fetching and entering feature annotations
databases : dict
keys are the database names (i.e. 'dbbact' / 'gnps')
values are the database specific data for the experiment (i.e. annotations for dbbact)
See Also
--------
Experiment
'''
def __init__(self, *args, databases=('dbbact',), **kwargs):
super().__init__(*args, databases=('dbbact',), **kwargs)
super().__init__(*args, databases=databases, **kwargs)

def heatmap(self, *args, **kwargs):
'''Plot a heatmap for the amplicon experiment.
Expand Down
1 change: 1 addition & 0 deletions calour/calour.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class_name = DBBact
website = www.dbbact.org
installation = pip install git+git://github.com/amnona/dbbact-calour
description = manual annotations about bacterial amplicon sequences
min_version = 2020.0709

[sponge]
module_name = spongeworld_calour
Expand Down
9 changes: 8 additions & 1 deletion calour/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ def _get_database_class(dbname, exp=None, config_file_name=None):
'''
class_name = get_config_value('class_name', section=dbname, config_file_name=config_file_name)
module_name = get_config_value('module_name', section=dbname, config_file_name=config_file_name)
min_version = float(get_config_value('min_version', section=dbname, config_file_name=config_file_name, fallback='0.0'))
module_website = get_config_value('website', section=dbname, config_file_name=config_file_name, fallback='NA')

if class_name is not None and module_name is not None:
try:
# import the database module
db_module = importlib.import_module(module_name)
except ImportError:
module_website = get_config_value('website', section=dbname, config_file_name=config_file_name)
module_installation = get_config_value('installation', section=dbname, config_file_name=config_file_name)
logger.warning('Database interface %s not installed.\nSkipping.\n'
'You can install the database using:\n%s\n'
Expand All @@ -70,6 +72,11 @@ def _get_database_class(dbname, exp=None, config_file_name=None):
# get the class
DBClass = getattr(db_module, class_name)
cdb = DBClass(exp)
# test if database version is compatible
if min_version > 0:
db_version = cdb.version()
if db_version < min_version:
logger.warning('Please update %s database module. Current version (%f) not supported (minimal version %f).\nFor details see %s' % (dbname, db_version, min_version, module_website))
return cdb
# not found, so print available database names
databases = []
Expand Down
13 changes: 9 additions & 4 deletions calour/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class Experiment:
sparse : bool
store the data array in :class:`scipy.sparse.csr_matrix`
or :class:`numpy.ndarray`
databases: iterable of str, optional
database interface names to show by default in heatmap() function
Attributes
----------
Expand All @@ -76,8 +78,9 @@ class Experiment:
information about the experiment (data md5, filenames, etc.)
description : str
a short description of the experiment
databases : iterable of str
databases for fetching and entering feature annotations
databases : defaultdict(dict)
keys are the database names (i.e. 'dbbact' / 'gnps')
values are the database specific data for the experiment (i.e. annotations for dbbact)
See Also
--------
Expand All @@ -103,8 +106,10 @@ def __init__(self, data, sample_metadata, feature_metadata=None, databases=(),
# flag if data array is sparse (True) or dense (False)
self.sparse = sparse

# the default databases to use for feature information
self.databases = databases
# the database local specific data (to use for feature information)
self.databases = defaultdict(dict)
for cdatabase in databases:
self.databases[cdatabase] = {}

def validate(self):
'''Validate the Experiment object.
Expand Down
2 changes: 1 addition & 1 deletion calour/heatmap/heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def plot(exp: Experiment, title=None,
'''
# set the databases if default requested
if databases is None:
databases = exp.databases
databases = list(exp.databases.keys())

if tree is None:
gui_obj = _create_plot_gui(exp, gui, databases)
Expand Down
7 changes: 4 additions & 3 deletions calour/heatmap/plotgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class PlotGUI(ABC):
Axes for the dendrogram/tree
ax_legend : matplotlib.axes.Axes
Axes for the color legend
databases : list
the databases to interact with
databases : list on None, optional
the databases to interact with.
Parameters
----------
Expand All @@ -86,7 +86,8 @@ class PlotGUI(ABC):
the scaling factor for zooming
scroll_offset : float
The amount of columns/rows to scroll when arrow key pressed
databases : the databases to interact with
databases : list of str or None
the databases to interact with.
tree_size : int (>= 0)
the width of the axes to plot a tree. 7 is a good value to start.
Expand Down
3 changes: 2 additions & 1 deletion calour/heatmap/plotgui_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class PlotGUI_QT5(PlotGUI):
figure : ``matplotlib.figure.Figure``
app : QT5 App created
app_window : Windows belonging to the QT5 App
databases :
databases : list of str
The databases to interact with
'''

@ds.with_indent(8)
Expand Down
5 changes: 5 additions & 0 deletions calour/tests/mock_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,8 @@ def get_feature_terms(self, features, exp=None):
for cstr in cterms:
feature_terms[cseq].append(cstr)
return feature_terms

def version(self):
'''return the current database version
'''
return 2020.0709
15 changes: 15 additions & 0 deletions calour/tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from os.path import join
from tempfile import mkdtemp
import shutil
import logging

from calour._testing import Tests
from calour.tests.mock_database import MockDatabase
Expand Down Expand Up @@ -60,6 +61,20 @@ def test_get_database_class(self):

shutil.rmtree(d)

def test_get_database_class_version(self):
d = mkdtemp()
f = join(d, 'config.txt')
calour.util.set_config_value('class_name', 'MockDatabase', section='testdb', config_file_name=f)
calour.util.set_config_value('module_name', 'calour.tests.mock_database', section='testdb', config_file_name=f)
calour.util.set_config_value('min_version', '9999.9999', section='testdb', config_file_name=f)
# re-enable logging because it is disabled in setUp
logging.disable(logging.NOTSET)
with self.assertLogs(level='WARNING') as cm:
db = _get_database_class('testdb', config_file_name=f)
self.assertEqual(db.database_name, 'mock_db')
self.assertRegex(cm.output[0], 'Please update')
shutil.rmtree(d)


if __name__ == "__main__":
main()

0 comments on commit 556bdf9

Please sign in to comment.