Skip to content

Commit

Permalink
Merge pull request #6545 from jmchilton/rule_builder_use_group_tags
Browse files Browse the repository at this point in the history
Allow Consuming Tags in the Apply Rules Tool
  • Loading branch information
mvdbeek committed Aug 26, 2018
2 parents a3fc0b9 + 5807230 commit 0e69ca8
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 9 deletions.
24 changes: 22 additions & 2 deletions client/galaxy/scripts/components/RuleCollectionBuilder.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@
</select>
</label>
</rule-component>
<rule-component rule-type="add_column_group_tag_value"
:display-rule-type="displayRuleType"
:builder="this">
<label>
{{ l("Value") }}
<input type="text" v-model="addColumnGroupTagValueValue" />
</label>
<label>
{{ l("Default") }}
<input type="text" v-model="addColumnGroupTagValueDefault" />
</label>
</rule-component>
<rule-component rule-type="add_column_regex"
:display-rule-type="displayRuleType"
:builder="this">
Expand Down Expand Up @@ -286,6 +298,7 @@
<div class="dropdown-menu" role="menu">
<rule-target-component :builder="this" rule-type="add_column_basename" />
<rule-target-component :builder="this" rule-type="add_column_metadata" v-if="metadataOptions"/>
<rule-target-component :builder="this" rule-type="add_column_group_tag_value" v-if="hasTagsMetadata"/>
<rule-target-component :builder="this" rule-type="add_column_regex" />
<rule-target-component :builder="this" rule-type="add_column_concatenate" />
<rule-target-component :builder="this" rule-type="add_column_rownum" />
Expand Down Expand Up @@ -901,6 +914,8 @@ export default {
addColumnRegexGroupCount: null,
addColumnRegexType: "global",
addColumnMetadataValue: 0,
addColumnGroupTagValueValue: "",
addColumnGroupTagValueDefault: "",
addColumnConcatenateTarget0: 0,
addColumnConcatenateTarget1: 0,
addColumnRownumStart: 1,
Expand Down Expand Up @@ -1182,6 +1197,7 @@ export default {
metadataOptions["identifier" + index] = _l("Paired Identifier");
}
}
metadataOptions["tags"] = _l("Tags");
} else if (this.elementsType == "ftp") {
metadataOptions["path"] = _l("Path");
} else if (this.elementsType == "library_datasets") {
Expand All @@ -1194,6 +1210,10 @@ export default {
}
return metadataOptions;
},
hasTagsMetadata() {
// TODO: allow for dataset, library_datasets also - here and just above in metadataOptions.
return this.elementsType == "collection_contents";
},
collectionType() {
let identifierColumns = [];
if (this.mappingAsDict.list_identifiers) {
Expand Down Expand Up @@ -1680,9 +1700,9 @@ export default {
if (collectionTypeLevelSepIndex === -1) {
// Flat collection at this depth.
// sources are the elements
// TOOD: right thing is probably this: data.push([]);
data.push([]);
sources.push({ identifiers: identifiers, dataset: elementObject });
const source = { identifiers: identifiers, dataset: elementObject, tags: elementObject.tags };
sources.push(source);
} else {
const restCollectionType = collectionType.slice(collectionTypeLevelSepIndex + 1);
let elementObj = this.populateElementsFromCollectionDescription(
Expand Down
51 changes: 50 additions & 1 deletion client/galaxy/scripts/mvc/rules/rule-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,21 @@ const RULES = {
apply: (rule, data, sources, columns) => {
const ruleValue = rule.value;
let newRow;
if (ruleValue.startsWith("identifier")) {
if (ruleValue.indexOf("identifier") == 0) {
const identifierIndex = parseInt(ruleValue.substring("identifier".length));
newRow = (row, index) => {
const newRow = row.slice();
newRow.push(sources[index]["identifiers"][identifierIndex]);
return newRow;
};
} else if (ruleValue == "tags") {
newRow = (row, index) => {
const newRow = row.slice();
const tags = sources[index]["tags"];
tags.sort();
newRow.push(tags.join(","));
return newRow;
};
} else if (ruleValue == "hid" || ruleValue == "name" || ruleValue == "path") {
newRow = (row, index) => {
const newRow = row.slice();
Expand All @@ -188,6 +196,47 @@ const RULES = {
return { data, columns };
}
},
add_column_group_tag_value: {
title: _l("Add Column from Group Tag Value"),
display: (rule, colHeaders) => {
return `Add column for value of group tag ${rule.value}.`;
},
init: (component, rule) => {
if (!rule) {
component.addColumnGroupTagValueValue = null;
component.addColumnGroupTagValueDefault = '';
} else {
component.addColumnGroupTagValueValue = rule.value;
component.addColumnGroupTagValueDefault = rule.default_value;
}
},
save: (component, rule) => {
rule.value = component.addColumnGroupTagValueValue;
rule.default_value = component.addColumnGroupTagValueDefault;
},
apply: (rule, data, sources, columns) => {
const ruleValue = rule.value;
const groupTagPrefix = `group:${ruleValue}:`;
const newRow = (row, index) => {
const newRow = row.slice();
const tags = sources[index]["tags"];
tags.sort();
let groupTagValue = rule.default_value;
for (let index in tags) {
const tag = tags[index];
if ( tag.indexOf(groupTagPrefix) == 0 ) {
groupTagValue = tag.substr(groupTagPrefix.length);
break;
}
}
newRow.push(groupTagValue);
return newRow;
};
data = data.map(newRow);
columns.push(NEW_COLUMN);
return { data, columns };
}
},
add_column_regex: {
title: _l("Using a Regular Expression"),
display: (rule, colHeaders) => {
Expand Down
3 changes: 2 additions & 1 deletion lib/galaxy/managers/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,8 @@ def __init_rule_data(self, elements, collection_type_description, parent_identif
identifiers = parent_identifiers + [element.element_identifier]
if not element.is_collection:
data.append([])
sources.append({"identifiers": identifiers, "dataset": element_object})
source = {"identifiers": identifiers, "dataset": element_object, "tags": element_object.make_tag_string_list()}
sources.append(source)
else:
child_collection_type_description = collection_type_description.child_collection_type_description()
element_data, element_sources = self.__init_rule_data(
Expand Down
1 change: 0 additions & 1 deletion lib/galaxy/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2764,7 +2764,6 @@ class ApplyRulesTool(DatabaseOperationTool):
tool_type = 'apply_rules'

def produce_outputs(self, trans, out_data, output_collections, incoming, history, **kwds):
log.info(incoming)
hdca = incoming["input"]
rule_set = RuleSet(incoming["rules"])
copied_datasets = []
Expand Down
44 changes: 42 additions & 2 deletions lib/galaxy/util/rules_dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,50 @@ def validate_rule(self, rule):

def apply(self, rule, data, sources):
rule_value = rule["value"]
identifier_index = int(rule_value[len("identifier"):])
if rule_value.startswith("identifier"):
identifier_index = int(rule_value[len("identifier"):])

new_rows = []
for index, row in enumerate(data):
new_rows.append(row + [sources[index]["identifiers"][identifier_index]])

elif rule_value == "tags":

def sorted_tags(index):
tags = sorted(sources[index]["tags"])
return [",".join(tags)]

new_rows = []
for index, row in enumerate(data):
new_rows.append(row + sorted_tags(index))

return new_rows, sources


class AddColumnGroupTagValueRuleDefinition(BaseRuleDefinition):
rule_type = "add_column_group_tag_value"

def validate_rule(self, rule):
_ensure_rule_contains_keys(rule, {"value": six.string_types})

def apply(self, rule, data, sources):
rule_value = rule["value"]
tag_prefix = "group:%s:" % rule_value

new_rows = []
for index, row in enumerate(data):
new_rows.append(row + [sources[index]["identifiers"][identifier_index]])
group_tag_value = None
source = sources[index]
tags = source["tags"]
for tag in sorted(tags):
if tag.startswith(tag_prefix):
group_tag_value = tag[len(tag_prefix):]
break

if group_tag_value is None:
group_tag_value = rule.get("default_value", "")

new_rows.append(row + [group_tag_value])

return new_rows, sources

Expand Down Expand Up @@ -525,6 +564,7 @@ def display(self):

RULES_DEFINITION_CLASSES = [
AddColumnMetadataRuleDefinition,
AddColumnGroupTagValueRuleDefinition,
AddColumnConcatenateRuleDefinition,
AddColumnBasenameRuleDefinition,
AddColumnRegexRuleDefinition,
Expand Down
3 changes: 3 additions & 0 deletions test/api/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ def test_apply_rules_2(self):
def test_apply_rules_3(self):
self._apply_rules_and_check(rules_test_data.EXAMPLE_3)

def test_apply_rules_4(self):
self._apply_rules_and_check(rules_test_data.EXAMPLE_4)

@skip_without_tool("multi_select")
def test_multi_select_as_list(self):
with self.dataset_populator.test_history() as history_id:
Expand Down
62 changes: 62 additions & 0 deletions test/base/data/rules_dsl_spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@
final:
data: [["20", "dog"], ["30", "cat"]]
sources: [4, 5]

- rules:
- type: add_filter_compare
target_column: 0
Expand All @@ -336,6 +337,7 @@
final:
data: [["13", "rat"], ["20", "dog"], ["30", "cat"]]
sources: [3, 4, 5]

- rules:
- type: sort
numeric: false
Expand All @@ -346,6 +348,7 @@
final:
data: [["bark", "dog"], ["meow", "cat"], ["moo", "cow"]]
sources: [3, 2, 1]

- rules:
- type: sort
numeric: false
Expand All @@ -356,6 +359,7 @@
final:
data: [["meow", "cat"], ["moo", "cow"], ["bark", "dog"]]
sources: [2, 1, 3]

- rules:
- type: sort
numeric: false
Expand All @@ -366,6 +370,7 @@
final:
data: [["bark", "Dog"], ["meow", "cat"], ["moo", "cow"]]
sources: [3, 2, 1]

- rules:
- type: swap_columns
target_column_0: 0
Expand All @@ -376,6 +381,7 @@
final:
data: [["cow", "moo"], ["cat", "meow"], ["Dog", "bark"]]
sources: [1, 2, 3]

- rules:
- type: split_columns
target_columns_0: [0]
Expand All @@ -386,28 +392,84 @@
final:
data: [["moo", "A"], ["cow", "A"], ["meow", "B"], ["cat", "B"], ["bark", "C"], ["Dog", "C"]]
sources: [1, 1, 2, 2, 3, 3]

- rules:
- type: add_column_metadata
value: identifier0
initial:
data: [["moo"], ["meow"], ["bark"]]
sources: [{"identifiers": ["cow"]}, {"identifiers": ["cat"]}, {"identifiers": ["dog"]}]
final:
data: [["moo", "cow"], ["meow", "cat"], ["bark", "dog"]]

- rules:
- type: add_column_metadata
value: tags
initial:
data: [["moo"], ["meow"], ["bark"]]
sources: [{"identifiers": ["cow"], "tags": ["farm"]}, {"identifiers": ["cat"], "tags": ["house"]}, {"identifiers": ["dog"], "tags": ["house", "firestation"]}]
final:
data: [["moo", "farm"], ["meow", "house"], ["bark", "firestation,house"]]

- rules:
- type: add_column_group_tag_value
value: where
default_value: ''
initial:
data: [["moo"], ["meow"], ["bark"]]
sources: [{"identifiers": ["cow"], "tags": ["group:where:farm"]}, {"identifiers": ["cat"], "tags": ["group:where:house"]}, {"identifiers": ["dog"], "tags": ["group:where:house"]}]
final:
data: [["moo", "farm"], ["meow", "house"], ["bark", "house"]]

- doc: Test add_column_group_tag_value default value.
rules:
- type: add_column_group_tag_value
value: where
default_value: 'barn'
initial:
data: [["moo"], ["meow"], ["bark"]]
sources: [{"identifiers": ["cow"], "tags": []}, {"identifiers": ["cat"], "tags": ["group:where:house"]}, {"identifiers": ["dog"], "tags": ["group:where:firestation"]}]
final:
data: [["moo", "barn"], ["meow", "house"], ["bark", "firestation"]]

- doc: Test add_column_group_tag_value sorts and grabs first tag value if multiple present.
rules:
- type: add_column_group_tag_value
value: where
default_value: 'barn'
initial:
data: [["moo"], ["meow"], ["bark"]]
sources: [{"identifiers": ["cow"], "tags": []}, {"identifiers": ["cat"], "tags": ["group:where:house", "group:where:kittenpile"]}, {"identifiers": ["dog"], "tags": ["group:where:house", "group:where:firestation"]}]
final:
data: [["moo", "barn"], ["meow", "house"], ["bark", "firestation"]]

- rules:
- type: invalid_rule_type
error: true

- rules:
- type: add_filter_compare
target_column: 0
value: 13
compare_type: invalid_compare_type
error: true

- rules:
- type: add_column_concatenate
target_column: 0
error: true

- rules:
- type: add_column_basename
target_column_0: 0
error: true

- rules:
- type: add_column_regex
target_column: 0
regex: '(o)+'
error: true

- rules:
- type: add_column_regex
target_column: 0
Expand Down
11 changes: 9 additions & 2 deletions test/base/populators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,13 +1010,20 @@ def read_test_data(test_dict):
elements_data = value.get("elements", [])
elements = []
for element_data in elements_data:
identifier = element_data["identifier"]
# Adapt differences between test_data dict and fetch API description.
if "name" not in element_data:
identifier = element_data["identifier"]
element_data["name"] = identifier
input_type = element_data.get("type", "raw")
content = None
if input_type == "File":
content = read_test_data(element_data)
else:
content = element_data["content"]
elements.append((identifier, content))
if content is not None:
element_data["src"] = "pasted"
element_data["paste_content"] = content
elements.append(element_data)
# TODO: make this collection_type
collection_type = value["type"]
new_collection_kwds = {}
Expand Down
Loading

0 comments on commit 0e69ca8

Please sign in to comment.