Merge pull request #11732 from assuntad23/copy_collections_dbkey

Copy collections dbkey
This commit is contained in:
John Chilton
2021-03-29 09:27:06 -04:00
committed by GitHub
4 changed files with 35 additions and 6 deletions
+4 -2
View File
@@ -287,7 +287,7 @@ class DatasetCollectionManager:
changed = self._set_from_dict(trans, dataset_collection_instance, payload)
return changed
def copy(self, trans, parent, source, encoded_source_id, copy_elements=False):
def copy(self, trans, parent, source, encoded_source_id, copy_elements=False, dataset_instance_attributes=None):
"""
PRECONDITION: security checks on ability to add to parent occurred
during load.
@@ -296,7 +296,9 @@ class DatasetCollectionManager:
source_hdca = self.__get_history_collection_instance(trans, encoded_source_id)
copy_kwds = {}
if copy_elements:
copy_kwds["element_destination"] = parent
copy_kwds["element_destination"] = parent # e.g. a history
if dataset_instance_attributes is not None:
copy_kwds["dataset_instance_attributes"] = dataset_instance_attributes
new_hdca = source_hdca.copy(**copy_kwds)
new_hdca.copy_tags_from(target_user=trans.get_user(), source=source_hdca)
if not copy_elements:
+10 -3
View File
@@ -4289,7 +4289,7 @@ class DatasetCollection(Dictifiable, UsesAnnotations, RepresentById):
error_message = f"Dataset collection has no {get_by_attribute} with key {key}."
raise KeyError(error_message)
def copy(self, destination=None, element_destination=None, flush=True):
def copy(self, destination=None, element_destination=None, dataset_instance_attributes=None, flush=True):
new_collection = DatasetCollection(
collection_type=self.collection_type,
element_count=self.element_count
@@ -4299,6 +4299,7 @@ class DatasetCollection(Dictifiable, UsesAnnotations, RepresentById):
new_collection,
destination=destination,
element_destination=element_destination,
dataset_instance_attributes=dataset_instance_attributes,
flush=flush
)
object_session(self).add(new_collection)
@@ -4540,7 +4541,7 @@ class HistoryDatasetCollectionAssociation(DatasetCollectionInstance,
break
return matching_collection
def copy(self, element_destination=None):
def copy(self, element_destination=None, dataset_instance_attributes=None):
"""
Create a copy of this history dataset collection association. Copy
underlying collection.
@@ -4561,6 +4562,7 @@ class HistoryDatasetCollectionAssociation(DatasetCollectionInstance,
collection_copy = self.collection.copy(
destination=hdca,
element_destination=element_destination,
dataset_instance_attributes=dataset_instance_attributes,
flush=False,
)
hdca.collection = collection_copy
@@ -4722,17 +4724,22 @@ class DatasetCollectionElement(Dictifiable, RepresentById):
else:
return [element_object]
def copy_to_collection(self, collection, destination=None, element_destination=None, flush=True):
def copy_to_collection(self, collection, destination=None, element_destination=None, dataset_instance_attributes=None, flush=True):
dataset_instance_attributes = dataset_instance_attributes or {}
element_object = self.element_object
if element_destination:
if self.is_collection:
element_object = element_object.copy(
destination=destination,
element_destination=element_destination,
dataset_instance_attributes=dataset_instance_attributes,
flush=flush
)
else:
new_element_object = element_object.copy(flush=flush, copy_tags=element_object.tags)
for attribute, value in dataset_instance_attributes.items():
setattr(new_element_object, attribute, value)
new_element_object.visible = False
if destination is not None and element_object.hidden_beneath_collection_instance:
new_element_object.hidden_beneath_collection_instance = destination
@@ -530,13 +530,21 @@ class HistoryContentsController(BaseGalaxyAPIController, UsesLibraryMixin, UsesL
content = payload.get('content', None)
if content is None:
raise exceptions.RequestParameterMissingException("'content' id of target to copy is missing")
copy_elements = payload.get('copy_elements', False)
dbkey = payload.get("dbkey")
copy_required = dbkey is not None
copy_elements = payload.get('copy_elements', copy_required)
if copy_required and not copy_elements:
raise exceptions.RequestParameterMissingException("copy_elements passed as 'false' but it is required to change specified attributes")
dataset_instance_attributes = {}
if dbkey is not None:
dataset_instance_attributes["dbkey"] = dbkey
dataset_collection_instance = service.copy(
trans=trans,
parent=history,
source="hdca",
encoded_source_id=content,
copy_elements=copy_elements,
dataset_instance_attributes=dataset_instance_attributes,
)
else:
message = "Invalid 'source' parameter in request %s" % source
@@ -440,6 +440,18 @@ class HistoryContentsApiTestCase(ApiTestCase):
self._assert_has_keys(new_forward, "history_id")
assert new_forward["history_id"] == self.history_id
def test_hdca_copy_with_new_dbkey(self):
hdca = self.dataset_collection_populator.create_pair_in_history(self.history_id).json()
hdca_id = hdca["id"]
assert hdca["elements"][0]["object"]["metadata_dbkey"] == "?"
assert hdca["elements"][0]["object"]["genome_build"] == "?"
create_data = {'source': 'hdca', 'content': hdca_id, 'dbkey': 'hg19'}
create_response = self._post(f"histories/{self.history_id}/contents/dataset_collections", create_data)
collection = self.__check_create_collection_response(create_response)
new_forward = collection['elements'][0]['object']
assert new_forward["metadata_dbkey"] == "hg19"
assert new_forward["genome_build"] == "hg19"
def test_hdca_copy_and_elements(self):
hdca = self.dataset_collection_populator.create_pair_in_history(self.history_id).json()
hdca_id = hdca["id"]