# Buggy code
def sliced_input_collection_structure(self, input_name):
    input_collection = self.example_params[input_name]
    collection_type_description = self.trans.app.dataset_collections_service.collection_type_descriptions.for_collection_type(
        input_collection.collection.collection_type
    )
    subcollection_mapping_type = None
    if self.is_implicit_input(input_name):
        subcollection_mapping_type = self.collection_info.subcollection_mapping_type(
            input_name
        )

    return get_structure(
        input_collection,
        collection_type_description,
        leaf_subcollection_type=subcollection_mapping_type,
    )


# Traceback (KeyError)
127.0.0.1 - - [16/May/2018:23:42:03 +0100] "POST /api/tools/toolshed.g2.bx.psu.edu/repos/iuc/sickle/sickle/1.33.1/build HTTP/1.1" 200 - "http://127.0.0.1:8080/?tool_id=toolshed.g2.bx.psu.edu%2Frepos%2Fiuc%2Fsickle%2Fsickle%2F1.33.1&amp;version=1.33.1&amp;__identifer=eqdjaao7u85" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"
galaxy.tools DEBUG 2018-05-16 23:42:05,300 Validated and populated state for tool request (43.218 ms)
galaxy.web.framework.decorators ERROR 2018-05-16 23:42:05,332 Uncaught exception in exposed API method:
Traceback (most recent call last):
File "/opt/galaxy/lib/galaxy/web/framework/decorators.py", line 281, in decorator
rval = func(self, trans, *args, **kwargs)
File "/opt/galaxy/lib/galaxy/webapps/galaxy/api/tools.py", line 354, in create
vars = tool.handle_input(trans, incoming, history=target_history, use_cached_job=use_cached_job)
File "/opt/galaxy/lib/galaxy/tools/__init__.py", line 1337, in handle_input
execution_tracker = execute_job(trans, self, mapping_params, history=request_context.history, rerun_remap_job_id=rerun_remap_job_id, collection_info=collection_info, completed_jobs=completed_jobs)
File "/opt/galaxy/lib/galaxy/tools/execute.py", line 81, in execute
execution_tracker.ensure_implicit_collections_populated(history, mapping_params.param_template)
File "/opt/galaxy/lib/galaxy/tools/execute.py", line 260, in ensure_implicit_collections_populated
self.precreate_output_collections(history, params)
File "/opt/galaxy/lib/galaxy/tools/execute.py", line 278, in precreate_output_collections
effective_structure = self._mapped_output_structure(trans, output)
File "/opt/galaxy/lib/galaxy/tools/execute.py", line 244, in _mapped_output_structure
output_structure = tool_output_to_structure(self.sliced_input_collection_structure, tool_output, collections_manager)
File "/opt/galaxy/lib/galaxy/dataset_collections/structure.py", line 183, in tool_output_to_structure
tree = get_sliced_input_collection_structure(structured_like)
File "/opt/galaxy/lib/galaxy/tools/execute.py", line 214, in sliced_input_collection_structure
input_collection = self.example_params[input_name]
KeyError: 'input_paired'
127.0.0.1 - - [16/May/2018:23:42:05 +0100] "POST /api/tools HTTP/1.1" 500 - "http://127.0.0.1:8080/?tool_id=toolshed.g2.bx.psu.edu%2Frepos%2Fiuc%2Fsickle%2Fsickle%2F1.33.1&amp;version=1.33.1&amp;__identifer=eqdjaao7u85" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"

# Fixed code
def sliced_input_collection_structure(self, input_name):
    unqualified_recurse = self.tool.profile < 18.09 and "|" not in input_name

    def find_collection(input_dict, input_name):
        for key, value in input_dict.items():
            if key == input_name:
                return value
            if isinstance(value, dict):
                if "|" in input_name:
                    prefix, rest_input_name = input_name.split("|", 1)
                    if key == prefix:
                        return find_collection(value, rest_input_name)
                elif unqualified_recurse:
                    # Looking for "input1" instead of "cond|input1" for instance.
                    # See discussion on https://github.com/galaxyproject/galaxy/issues/6157.
                    unqualified_match = find_collection(value, input_name)
                    if unqualified_match:
                        return unqualified_match

    input_collection = find_collection(self.example_params, input_name)
    if input_collection is None:
        raise Exception("Failed to find referenced collection in inputs.")

    if not hasattr(input_collection, "collection"):
        raise Exception("Referenced input parameter is not a collection.")

    collection_type_description = self.trans.app.dataset_collections_service.collection_type_descriptions.for_collection_type(
        input_collection.collection.collection_type
    )
    subcollection_mapping_type = None
    if self.is_implicit_input(input_name):
        subcollection_mapping_type = self.collection_info.subcollection_mapping_type(
            input_name
        )

    return get_structure(
        input_collection,
        collection_type_description,
        leaf_subcollection_type=subcollection_mapping_type,
    )