Skip to content

Enable src/sink comparisons for records #116

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

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
24 changes: 24 additions & 0 deletions cwltool/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def can_assign_src_to_sink(src, sink): # type: (Any, Any) -> bool
if isinstance(src, dict) and isinstance(sink, dict):
if src["type"] == "array" and sink["type"] == "array":
return can_assign_src_to_sink(src["items"], sink["items"])
elif src["type"] == "record" and sink["type"] == "record":
return _compare_records(src, sink)
elif isinstance(src, list):
for t in src:
if can_assign_src_to_sink(t, sink):
Expand All @@ -108,6 +110,28 @@ def can_assign_src_to_sink(src, sink): # type: (Any, Any) -> bool
return src == sink
return False

def _compare_records(rec1, rec2):
"""Compare two records, ensuring they have compatible fields.

This handles normalizing record names, which will be relative to workflow
step, so that they can be compared.
"""
def _rec_fields(rec):
out = {}
for field in rec["fields"]:
name = field["name"].replace(rec["name"], "")
out[name] = field["type"]
return out
fields1 = _rec_fields(rec1)
fields2 = _rec_fields(rec2)
for key in set(fields1.keys() + fields2.keys()):
if fields1.get(key) != fields2.get(key):
_logger.info("Record comparison failure for %s and %s\n"
"Did not match fields for %s: %s and %s" %
(rec1["name"], rec2["name"], key, fields1.get(key), fields2.get(key)))
return False
return True

def object_from_state(state, parms, frag_only, supportsMultipleInput, sourceField):
# type: (Dict[unicode, WorkflowStateItem], List[Dict[unicode, Any]], bool, bool, unicode) -> Dict[unicode, Any]
inputobj = {} # type: Dict[unicode, Any]
Expand Down
6 changes: 6 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ def test_typecompare(self):
{'items': ['string'], 'type': 'array'},
{'items': ['int'], 'type': 'array'}))

def test_recordcompare(self):
src = {'fields': [{'type': {'items': 'string', 'type': 'array'}, 'name': u'file:///home/chapmanb/drive/work/cwl/test_bcbio_cwl/run_info-cwl-workflow/wf-variantcall.cwl#vc_rec/vc_rec/description'}, {'type': {'items': 'File', 'type': 'array'}, 'name': u'file:///home/chapmanb/drive/work/cwl/test_bcbio_cwl/run_info-cwl-workflow/wf-variantcall.cwl#vc_rec/vc_rec/vrn_file'}], 'type': 'record', 'name': u'file:///home/chapmanb/drive/work/cwl/test_bcbio_cwl/run_info-cwl-workflow/wf-variantcall.cwl#vc_rec/vc_rec'}
sink = {'fields': [{'type': {'items': 'string', 'type': 'array'}, 'name': u'file:///home/chapmanb/drive/work/cwl/test_bcbio_cwl/run_info-cwl-workflow/steps/vc_output_record.cwl#vc_rec/vc_rec/description'}, {'type': {'items': 'File', 'type': 'array'}, 'name': u'file:///home/chapmanb/drive/work/cwl/test_bcbio_cwl/run_info-cwl-workflow/steps/vc_output_record.cwl#vc_rec/vc_rec/vrn_file'}], 'type': 'record', 'name': u'file:///home/chapmanb/drive/work/cwl/test_bcbio_cwl/run_info-cwl-workflow/steps/vc_output_record.cwl#vc_rec/vc_rec'}
self.assertTrue(cwltool.workflow.can_assign_src_to_sink(src, sink))



if __name__ == '__main__':
unittest.main()