Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge ShedToolLineage and StockLineage to ToolLineage #4119

Merged
merged 14 commits into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/galaxy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,11 +925,9 @@ def _configure_toolbox( self ):
from galaxy import tools
from galaxy.managers.citations import CitationsManager
from galaxy.tools.deps import containers
from galaxy.tools.toolbox.lineages.tool_shed import ToolVersionCache
import galaxy.tools.search

self.citations_manager = CitationsManager( self )
self.tool_version_cache = ToolVersionCache(self)

self._toolbox_lock = threading.RLock()
# Initialize the tools, making sure the list of tool configs includes the reserved migrated_tools_conf.xml file.
Expand Down
46 changes: 3 additions & 43 deletions lib/galaxy/model/tool_shed_install/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,51 +589,11 @@ def __init__( self, id=None, create_time=None, tool_id=None, tool_shed_repositor
self.tool_id = tool_id
self.tool_shed_repository = tool_shed_repository

def get_previous_version( self, app ):
parent_id = app.tool_version_cache.tool_id_to_parent_id.get(self.id, None)
if parent_id:
return app.tool_version_cache.tool_version_by_id[parent_id]
else:
return None

def get_next_version( self, app ):
child_id = app.tool_version_cache.parent_id_to_tool_id.get(self.id, None)
if child_id:
return app.tool_version_cache.tool_version_by_id[child_id]
else:
return None

def get_versions( self, app ):
tool_versions = []

# Prepend ancestors.
def __ancestors( app, tool_version ):
# Should we handle multiple parents at each level?
previous_version = tool_version.get_previous_version( app )
if previous_version:
if previous_version not in tool_versions:
tool_versions.insert( 0, previous_version )
__ancestors( app, previous_version )

# Append descendants.
def __descendants( app, tool_version ):
# Should we handle multiple child siblings at each level?
next_version = tool_version.get_next_version( app )
if next_version:
if next_version not in tool_versions:
tool_versions.append( next_version )
__descendants( app, next_version )

__ancestors( app, self )
if self not in tool_versions:
tool_versions.append( self )
__descendants( app, self )
return tool_versions

def get_version_ids( self, app, reverse=False ):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not used any more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I missed that! Up to you if you want to replace it or leave it as it is.

version_ids = [ tool_version.tool_id for tool_version in self.get_versions( app ) ]
lineage = app.toolbox._lineage_map.get(self.tool_id)
version_ids = lineage.get_versions()
if reverse:
version_ids.reverse()
version_ids = reversed(version_ids)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's not correct, reversed returns an iterator, while I think the expected return type of this method is a list.

return version_ids

def to_dict( self, view='element' ):
Expand Down
4 changes: 0 additions & 4 deletions lib/galaxy/queue_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,8 @@ def _get_new_toolbox(app):
"""
from galaxy import tools
from galaxy.tools.special_tools import load_lib_tools
from galaxy.tools.toolbox.lineages.tool_shed import ToolVersionCache
if hasattr(app, 'tool_shed_repository_cache'):
app.tool_shed_repository_cache.rebuild()
app.tool_version_cache = ToolVersionCache(app) # Load new tools into version cache
tool_configs = app.config.tool_configs
if app.config.migrated_tools_config not in tool_configs:
tool_configs.append(app.config.migrated_tools_config)
Expand All @@ -122,7 +120,6 @@ def _get_new_toolbox(app):
def reload_data_managers(app, **kwargs):
reload_timer = util.ExecutionTimer()
from galaxy.tools.data_manager.manager import DataManagers
from galaxy.tools.toolbox.lineages.tool_shed import ToolVersionCache
log.debug("Executing data managers reload on '%s'", app.config.server_name)
if hasattr(app, 'tool_shed_repository_cache'):
app.tool_shed_repository_cache.rebuild()
Expand All @@ -131,7 +128,6 @@ def reload_data_managers(app, **kwargs):
reload_count = app.data_managers._reload_count
app.data_managers = DataManagers(app)
app.data_managers._reload_count = reload_count + 1
app.tool_version_cache = ToolVersionCache(app)
if hasattr(app, 'tool_cache'):
app.tool_cache.reset_status()
log.debug("Data managers reloaded %s", reload_timer)
Expand Down
10 changes: 3 additions & 7 deletions lib/galaxy/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ def sa_session( self ):

@property
def tool_version( self ):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crazy idea: would it make sense to rename this property to lineage and remove:

tool.lineage = tool_lineage

from AbstractToolBox._load_tool_tag_set() method in lib/galaxy/tools/toolbox/base.py ?

I think this property is only used and would need to be renamed:

  • in the next method tool_versions
  • in this file at line1835
  • in lib/galaxy/webapps/galaxy/controllers/admin_toolshed.py at line 2064.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

absolutely, I think that makes it clearer.

"""Return a ToolVersion if one exists for our id"""
return self.app.tool_version_cache.tool_version_by_tool_id.get(self.id)
"""Return a StockLineage if one exists for our id."""
return self.app.toolbox._lineage_map.get(self.id)

@property
def tool_versions( self ):
Expand Down Expand Up @@ -1832,11 +1832,7 @@ def to_json( self, trans, kwd={}, job=None, workflow_building_mode=False ):
tool_help = unicodify( tool_help, 'utf-8' )

# create tool versions
tool_versions = []
tools = self.app.toolbox.get_loaded_tools_by_lineage( self.id )
for t in tools:
if t.version not in tool_versions:
tool_versions.append( t.version )
tool_versions = self.tool_version.tool_versions

# update tool model
tool_model.update({
Expand Down
15 changes: 3 additions & 12 deletions lib/galaxy/tools/toolbox/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,6 @@ def get_tool( self, tool_id, tool_version=None, get_all_versions=False, exact=Fa
# exact tool id match not found, or all versions requested, search for other options, e.g. migrated tools or different versions
rval = []
tool_lineage = self._lineage_map.get( tool_id )
if not tool_lineage:
tool_lineage = self._lineage_map.get_versionless( tool_id )
if tool_lineage:
lineage_tool_versions = tool_lineage.get_versions( )
for lineage_tool_version in lineage_tool_versions:
Expand Down Expand Up @@ -568,7 +566,7 @@ def _load_tool_tag_set( self, item, panel_dict, integrated_panel_dict, tool_path
tool.guid = guid
tool.version = item.elem.find( "version" ).text
# Make sure tools have a tool_version object.
tool_lineage = self._lineage_map.register( tool, from_toolshed=guid )
tool_lineage = self._lineage_map.register( tool )
tool.lineage = tool_lineage
if item.has_elem:
self._tool_tag_manager.handle_tags( tool.id, item.elem )
Expand Down Expand Up @@ -952,7 +950,7 @@ def _lineage_in_panel( self, panel_dict, tool=None, tool_lineage=None ):
if not hasattr( tool, "lineage" ):
return None
tool_lineage = tool.lineage
lineage_tool_versions = tool_lineage.get_versions( reverse=True )
lineage_tool_versions = reversed(tool_lineage.get_versions())
for lineage_tool_version in lineage_tool_versions:
lineage_tool = self._tool_from_lineage_version( lineage_tool_version )
if lineage_tool:
Expand All @@ -967,14 +965,7 @@ def _newer_tool( self, tool1, tool2 ):
"""
if not hasattr( tool1, "lineage" ):
return True
lineage_tool_versions = tool1.lineage.get_versions()
for lineage_tool_version in lineage_tool_versions:
lineage_tool = self._tool_from_lineage_version( lineage_tool_version )
if lineage_tool is tool1:
return False
if lineage_tool is tool2:
return True
return True
return tool1.version_object > tool2.version_object

def _tool_from_lineage_version( self, lineage_tool_version ):
if lineage_tool_version.id_based:
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/tools/toolbox/lineages/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from .factory import LineageMap
from .interface import ToolLineage
from .tool_shed import ToolVersionCache


__all__ = ("LineageMap", "ToolLineage", "ToolVersionCache")
__all__ = ("LineageMap", "ToolLineage")
42 changes: 22 additions & 20 deletions lib/galaxy/tools/toolbox/lineages/factory.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
from .stock import StockLineage
from .tool_shed import ToolShedLineage

from galaxy.util.tool_version import remove_version_from_guid

def remove_version_from_guid( guid ):
"""
Removes version from toolshed-derived tool_id(=guid).
"""
if "/repos/" not in guid:
return None
last_slash = guid.rfind('/')
return guid[:last_slash]
from .stock import StockLineage


class LineageMap(object):
Expand All @@ -20,28 +11,39 @@ def __init__(self, app):
self.lineage_map = {}
self.app = app

def register(self, tool, from_toolshed=False):
def register(self, tool):
tool_id = tool.id
versionless_tool_id = remove_version_from_guid( tool_id )
if from_toolshed:
lineage = ToolShedLineage.from_tool(self.app, tool)
else:
versionless_tool_id = remove_version_from_guid( tool_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant to also remove the space after the (

lineage = self.lineage_map.get(versionless_tool_id)
if not lineage:
lineage = StockLineage.from_tool( tool )
if versionless_tool_id and versionless_tool_id not in self.lineage_map:
self.lineage_map[versionless_tool_id] = lineage
if tool_id not in self.lineage_map:
self.lineage_map[tool_id] = lineage
lineage.register_version(tool.version)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's needed: register_version(tool.version) is already called inside StockLineage.from_tool( tool ), which is the only method used to create the StockLineage objects added to the self.lineage_map dictionary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first glance I agree with you, but removing this line breaks the lineages ... I'll have to dig into why that happens

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a threading issue? See my comment below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I think this is what happens:
When a tool is loaded the first time, it will enter self.lineage_map, and have its version registered.
When a related tool is added, it will be fetched from self.lineage_map via the versionless_tool_id, and not run through ToolLineage.from_tool. So we need to add the current tool's version via lineage.register_version(tool.version). I'll add a comment to clarify this.

Copy link
Member

@nsoranzo nsoranzo Jun 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see! Then you can move this up a bit changing lines 18-19 above to:

        if lineage:
            lineage.register_version(tool.version)
        else:
            lineage = ToolLineage.from_tool(tool)

return self.lineage_map[tool_id]

def get(self, tool_id):
"""
Get lineage for `tool_id`.

By preference the lineage for a version-agnostic tool_id is returned.
Falls back to fetching the lineage only when this fails.
This happens when the tool_id does not contain a version.
"""
lineage = self._get_versionless(tool_id)
if lineage:
return lineage
if tool_id not in self.lineage_map:
lineage = ToolShedLineage.from_tool_id( self.app, tool_id )
tool = self.app.toolbox._tools_by_id.get(tool_id)
if tool:
lineage = StockLineage.from_tool( tool )
if lineage:
self.lineage_map[tool_id] = lineage
return self.lineage_map.get(tool_id)

return self.lineage_map.get(tool_id, None)

def get_versionless(self, tool_id):
def _get_versionless(self, tool_id):
versionless_tool_id = remove_version_from_guid(tool_id)
return self.lineage_map.get(versionless_tool_id, None)

Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/tools/toolbox/lineages/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ToolLineage(object):
"""

@abstractmethod
def get_versions( self, reverse=False ):
def get_versions( self ):
""" Return an ordered list of lineages (ToolLineageVersion) in this
chain, from oldest to newest.
"""
Expand Down
31 changes: 19 additions & 12 deletions lib/galaxy/tools/toolbox/lineages/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from distutils.version import LooseVersion

from galaxy.util.tool_version import remove_version_from_guid

from .interface import ToolLineage
from .interface import ToolLineageVersion

Expand All @@ -15,7 +17,17 @@ class StockLineage(ToolLineage):

def __init__(self, tool_id, **kwds):
self.tool_id = tool_id
self.tool_versions = set()
self._tool_versions = set()

@property
def tool_versions(self):
return sorted(self._tool_versions, key=LooseVersion)

@property
def tool_ids(self):
versionless_tool_id = remove_version_from_guid(self.tool_id)
tool_id = versionless_tool_id or self.tool_id
return ["%s/%s" % (tool_id, version) for version in self.tool_versions]

@staticmethod
def from_tool( tool ):
Expand All @@ -30,22 +42,17 @@ def from_tool( tool ):

def register_version( self, tool_version ):
assert tool_version is not None
self.tool_versions.add( tool_version )
self._tool_versions.add( str(tool_version) )

def get_versions( self, reverse=False ):
versions = [ ToolLineageVersion( self.tool_id, v ) for v in self.tool_versions ]
# Sort using LooseVersion which defines an appropriate __cmp__
# method for comparing tool versions.
return sorted( versions, key=_to_loose_version, reverse=reverse )
def get_versions( self ):
return [ ToolLineageVersion( tool_id, tool_version ) for tool_id, tool_version in zip(self.tool_ids, self.tool_versions) ]

def get_version_ids(self):
return self.tool_ids

def to_dict(self):
return dict(
tool_id=self.tool_id,
tool_versions=list(self.tool_versions),
lineage_type='stock',
)


def _to_loose_version( tool_lineage_version ):
version = str( tool_lineage_version.version )
return LooseVersion( version )
Loading