Skip to content

Commit d50d35c

Browse files
authored
Merge pull request #20926 from bernt-matthias/nest-tool
Add tool to add nesting level to collection
2 parents 5feec4a + 4502d1a commit d50d35c

File tree

4 files changed

+1115
-0
lines changed

4 files changed

+1115
-0
lines changed

lib/galaxy/config/sample/tool_conf.xml.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<tool file="${model_tools_path}/keep_success_collection.xml" />
4141
<tool file="${model_tools_path}/keep_success_collection_1.1.0.xml" />
4242
<tool file="${model_tools_path}/flatten_collection.xml" />
43+
<tool file="${model_tools_path}/nest_collection.xml" />
4344
<tool file="${model_tools_path}/merge_collection.xml" />
4445
<tool file="${model_tools_path}/relabel_from_file.xml" />
4546
<tool file="${model_tools_path}/filter_from_file.xml" />

lib/galaxy/tools/__init__.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4174,6 +4174,38 @@ def add_elements(collection, prefix=""):
41744174
)
41754175

41764176

4177+
class NestTool(DatabaseOperationTool):
4178+
tool_type = "nest_collection"
4179+
require_terminal_states = False
4180+
require_dataset_ok = False
4181+
4182+
def produce_outputs(self, trans, out_data, output_collections, incoming, history, **kwds):
4183+
hdca = incoming["input"]
4184+
new_elements = {}
4185+
copied_datasets = []
4186+
4187+
for dce in hdca.collection.elements:
4188+
element_identifier = dce.element_identifier
4189+
4190+
if getattr(dce.element_object, "history_content_type", None) == "dataset":
4191+
copied_dataset = dce.element_object.copy(copy_tags=dce.element_object.tags, flush=False)
4192+
else:
4193+
copied_dataset = dce.element_object.copy(flush=False)
4194+
copied_datasets.append(copied_dataset)
4195+
4196+
sub_collection: dict[str, Any] = {}
4197+
sub_collection["src"] = "new_collection"
4198+
sub_collection["collection_type"] = "list"
4199+
sub_collection["elements"] = {element_identifier: copied_dataset}
4200+
4201+
new_elements[element_identifier] = sub_collection
4202+
4203+
self._add_datasets_to_history(history, copied_datasets)
4204+
output_collections.create_collection(
4205+
self.outputs["output"], "output", elements=new_elements, propagate_hda_tags=False
4206+
)
4207+
4208+
41774209
class SortTool(DatabaseOperationTool):
41784210
tool_type = "sort_collection"
41794211
require_terminal_states = True
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<tool id="__NEST__"
2+
name="Nest collection"
3+
version="1.0.0"
4+
tool_type="nest_collection">
5+
<description></description>
6+
<type class="NestTool" module="galaxy.tools" />
7+
<action module="galaxy.tools.actions.model_operations"
8+
class="ModelOperationToolAction"/>
9+
<edam_operations>
10+
<edam_operation>operation_2409</edam_operation>
11+
</edam_operations>
12+
<macros>
13+
<import>model_operation_macros.xml</import>
14+
</macros>
15+
<inputs>
16+
<param type="data_collection" collection_type="list,paired" name="input" label="Input Collection" />
17+
</inputs>
18+
<outputs>
19+
<collection name="output" format_source="input" type="list:list" label="${on_string} (flattened)" >
20+
</collection>
21+
</outputs>
22+
<tests>
23+
<test>
24+
<param name="input">
25+
<collection type="list">
26+
<element name="sample1" value="simple_line.txt"/>
27+
<element name="sample2" value="simple_line_alternative.txt"/>
28+
</collection>
29+
</param>
30+
<output_collection name="output" type="list:list">
31+
<element name="sample1">
32+
<element name="sample1">
33+
<expand macro="assert_is_simple_line" />
34+
</element>
35+
</element>
36+
<element name="sample2">
37+
<element name="sample2">
38+
<expand macro="assert_is_simple_line_alt" />
39+
</element>
40+
</element>
41+
</output_collection>
42+
</test>
43+
<test>
44+
<param name="input">
45+
<collection type="list:paired">
46+
<element name="sample1">
47+
<collection type="paired">
48+
<element name="forward" value="simple_line.txt" />
49+
<element name="reverse" value="simple_line.txt" />
50+
</collection>
51+
</element>
52+
<element name="sample2">
53+
<collection type="paired">
54+
<element name="forward" value="simple_line_alternative.txt" />
55+
<element name="reverse" value="simple_line_alternative.txt" />
56+
</collection>
57+
</element>
58+
</collection>
59+
</param>
60+
<output_collection name="output" type="list:list" count="2">
61+
<element name="sample1" count="1">
62+
<element name="sample1">
63+
<element name="forward">
64+
<expand macro="assert_is_simple_line" />
65+
</element>
66+
<element name="reverse">
67+
<expand macro="assert_is_simple_line" />
68+
</element>
69+
</element>
70+
</element>
71+
<element name="sample2" count="1">
72+
<element name="sample2">
73+
<element name="forward">
74+
<expand macro="assert_is_simple_line_alt" />
75+
</element>
76+
<element name="reverse">
77+
<expand macro="assert_is_simple_line_alt" />
78+
</element>
79+
</element>
80+
</element>
81+
</output_collection>
82+
</test>
83+
</tests>
84+
<help><![CDATA[
85+
86+
========
87+
Synopsis
88+
========
89+
90+
Nest a collection, i.e. add a nesting level to a collection.
91+
92+
===========
93+
Description
94+
===========
95+
96+
This tool takes a collection and creates a collection of collections where the top level elements are lists each containing one element of the input collection.
97+
For instance, for a simple list (of datasets) the tool will produce a list of lists where the inner lists each contain one of the datasets.
98+
99+
.. image:: ${static_path}/images/tools/collection_ops/nest.svg
100+
:width: 500
101+
:alt: Nest operation
102+
103+
=======
104+
Purpose
105+
=======
106+
107+
Assume a tool processes an item of type X (where X can be a dataset, a dataset pair, a collection). Galaxy also allows to input a collection of items of type X.
108+
In this case Galaxy will map-over the elements of the collection, i.e. create separete jobs (each processing one item of type X).
109+
If X is a list (of datasets, pairs, or lists) then one needs to add an extra nesting level to the collection, such that Galaxy maps-over the elements of X instead
110+
of processing the elements of X jointly.
111+
112+
----
113+
114+
.. class:: infomark
115+
116+
This tool will create new history datasets from your collection but your quota usage will not increase.
117+
118+
]]></help>
119+
</tool>

0 commit comments

Comments
 (0)