| 1 |
import json |
| 2 |
import logging |
| 3 |
import sys |
| 4 |
import warnings |
| 5 |
|
| 6 |
from pbcommand.models import (PipelineChunk, PipelineDataStoreViewRules, |
| 7 |
TaskOptionTypes, PacBioFloatChoiceOption, |
| 8 |
PacBioStringChoiceOption, PacBioFileOption, |
| 9 |
PacBioIntChoiceOption, PacBioStringOption, |
| 10 |
PacBioFloatOption, PacBioBooleanOption, |
| 11 |
PacBioIntOption, PipelinePreset, EntryPoint) |
| 12 |
from pbcommand.models.legacy import Pipeline |
| 13 |
from pbcommand.schemas import validate_datastore_view_rules, validate_presets |
| 14 |
from pbcommand import to_ascii, to_utf8 |
| 15 |
|
| 16 |
log = logging.getLogger(__name__) |
| 17 |
|
| 18 |
|
| 19 |
def json_path_or_d(value): |
| 20 |
if isinstance(value, dict): |
| 21 |
return value |
| 22 |
elif isinstance(value, ("""s""".__class__, u"""s""".__class__)): |
| 23 |
with open(value, 'r') as f: |
| 24 |
d = json.loads(f.read()) |
| 25 |
return d |
| 26 |
else: |
| 27 |
raise ValueError("Unsupported value. Expected dict, or string") |
| 28 |
|
| 29 |
|
| 30 |
def _json_path_or_d(func): |
| 31 |
def _wrapper(value): |
| 32 |
return func(json_path_or_d(value)) |
| 33 |
return _wrapper |
| 34 |
|
| 35 |
|
| 36 |
def write_pipeline_chunks(chunks, output_json_file, comment): |
| 37 |
|
| 38 |
_d = dict(nchunks=len(chunks), _version="0.1.0", |
| 39 |
chunks=[c.to_dict() for c in chunks]) |
| 40 |
|
| 41 |
if comment is not None: |
| 42 |
_d['_comment'] = comment |
| 43 |
|
| 44 |
with open(output_json_file, 'w') as f: |
| 45 |
f.write(json.dumps(_d, indent=4, separators=(',', ': '))) |
| 46 |
|
| 47 |
log.debug( |
| 48 |
"Write {n} chunks to {o}".format( |
| 49 |
n=len(chunks), |
| 50 |
o=output_json_file)) |
| 51 |
|
| 52 |
|
| 53 |
def load_pipeline_chunks_from_json(path): |
| 54 |
"""Returns a list of Pipeline Chunks |
| 55 |
|
| 56 |
|
| 57 |
:rtype: list[PipelineChunk] |
| 58 |
""" |
| 59 |
|
| 60 |
try: |
| 61 |
with open(path, 'r') as f: |
| 62 |
d = json.loads(f.read()) |
| 63 |
|
| 64 |
chunks = [] |
| 65 |
for cs in d['chunks']: |
| 66 |
chunk_id = cs['chunk_id'] |
| 67 |
chunk_datum = cs['chunk'] |
| 68 |
c = PipelineChunk(chunk_id, **chunk_datum) |
| 69 |
chunks.append(c) |
| 70 |
chunks.sort(key=lambda a: a.chunk_id) |
| 71 |
return chunks |
| 72 |
except Exception: |
| 73 |
msg = "Unable to load pipeline chunks from {f}".format(f=path) |
| 74 |
sys.stderr.write(msg + "\n") |
| 75 |
raise |
| 76 |
|
| 77 |
|
| 78 |
def load_pipeline_datastore_view_rules_from_json(path): |
| 79 |
"""Load pipeline presets from dict""" |
| 80 |
with open(path, 'r') as f: |
| 81 |
d = json.loads(f.read()) |
| 82 |
validate_datastore_view_rules(d) |
| 83 |
return PipelineDataStoreViewRules.from_dict(d) |
| 84 |
|
| 85 |
|
| 86 |
def _pacbio_choice_option_from_dict(d): |
| 87 |
""" |
| 88 |
Factory/dispatch method for returning a PacBio Choice Option Type |
| 89 |
|
| 90 |
:rtype: PacBioOption |
| 91 |
""" |
| 92 |
choices = d['choices'] |
| 93 |
default_value = d['default'] |
| 94 |
|
| 95 |
option_type_id = TaskOptionTypes.from_choice_str(d['optionTypeId']) |
| 96 |
|
| 97 |
opt_id = d['id'] |
| 98 |
name = d['name'] |
| 99 |
desc = to_utf8(d['description']) |
| 100 |
|
| 101 |
klass_map = {TaskOptionTypes.CHOICE_STR: PacBioStringChoiceOption, |
| 102 |
TaskOptionTypes.CHOICE_FLOAT: PacBioFloatChoiceOption, |
| 103 |
TaskOptionTypes.CHOICE_INT: PacBioIntChoiceOption} |
| 104 |
|
| 105 |
k = klass_map[option_type_id] |
| 106 |
|
| 107 |
|
| 108 |
if k is PacBioStringChoiceOption: |
| 109 |
default_value = to_ascii(default_value) |
| 110 |
choices = [to_ascii(i) for i in choices] |
| 111 |
|
| 112 |
opt = k(opt_id, name, default_value, desc, choices) |
| 113 |
|
| 114 |
return opt |
| 115 |
|
| 116 |
|
| 117 |
def __simple_option_by_type( |
| 118 |
option_id, name, default, description, option_type_id): |
| 119 |
|
| 120 |
option_type = TaskOptionTypes.from_simple_str(option_type_id) |
| 121 |
|
| 122 |
klass_map = {TaskOptionTypes.INT: PacBioIntOption, |
| 123 |
TaskOptionTypes.FLOAT: PacBioFloatOption, |
| 124 |
TaskOptionTypes.STR: PacBioStringOption, |
| 125 |
TaskOptionTypes.BOOL: PacBioBooleanOption, |
| 126 |
TaskOptionTypes.FILE: PacBioFileOption} |
| 127 |
|
| 128 |
k = klass_map[option_type] |
| 129 |
|
| 130 |
|
| 131 |
if k is PacBioStringOption: |
| 132 |
|
| 133 |
default = to_ascii(default) |
| 134 |
|
| 135 |
opt = k(option_id, name, default, description) |
| 136 |
return opt |
| 137 |
|
| 138 |
|
| 139 |
def _pacbio_legacy_option_from_dict(d): |
| 140 |
""" |
| 141 |
Load the legacy (jsonschema-ish format) |
| 142 |
|
| 143 |
Note, choice types are not supported here. |
| 144 |
|
| 145 |
:rtype: PacBioOption |
| 146 |
""" |
| 147 |
warnings.warn( |
| 148 |
"This is obsolete and will disappear soon", |
| 149 |
DeprecationWarning) |
| 150 |
|
| 151 |
opt_id = d['pb_option']['option_id'] |
| 152 |
name = d['pb_option']['name'] |
| 153 |
default = d['pb_option']['default'] |
| 154 |
desc = d['pb_option']['description'] |
| 155 |
option_type_id = to_ascii(d['pb_option']['type']) |
| 156 |
|
| 157 |
|
| 158 |
if option_type_id == "number": |
| 159 |
option_type_id = "float" |
| 160 |
|
| 161 |
return __simple_option_by_type(opt_id, name, default, desc, option_type_id) |
| 162 |
|
| 163 |
|
| 164 |
def _pacbio_option_from_dict(d): |
| 165 |
if "pb_option" in d: |
| 166 |
return _pacbio_legacy_option_from_dict(d) |
| 167 |
else: |
| 168 |
return __simple_option_by_type( |
| 169 |
d['id'], |
| 170 |
d['name'], |
| 171 |
d['default'], |
| 172 |
to_utf8(d['description']), |
| 173 |
d['optionTypeId']) |
| 174 |
|
| 175 |
|
| 176 |
def pacbio_option_from_dict(d): |
| 177 |
"""Fundamental API for loading any PacBioOption type from a dict """ |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
if "choices" in d and d.get('choices') is not None: |
| 182 |
|
| 183 |
|
| 184 |
return _pacbio_choice_option_from_dict(d) |
| 185 |
else: |
| 186 |
return _pacbio_option_from_dict(d) |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
@_json_path_or_d |
| 191 |
def load_pipeline_presets_from(d): |
| 192 |
""" |
| 193 |
Load pipeline presets from dictionary. This expects a schema where the |
| 194 |
options are arrays of type (id,value,optionTypeId), but it will also accept |
| 195 |
a shorthand where the options are dictionaries. |
| 196 |
""" |
| 197 |
validate_presets(d) |
| 198 |
options = d['options'] |
| 199 |
if isinstance(options, list): |
| 200 |
options = {o['id']: o['value'] for o in options} |
| 201 |
taskOptions = d['taskOptions'] |
| 202 |
if isinstance(taskOptions, list): |
| 203 |
taskOptions = {o['id']: o['value'] for o in taskOptions} |
| 204 |
presets = PipelinePreset( |
| 205 |
options=options, |
| 206 |
task_options=taskOptions, |
| 207 |
pipeline_id=d['pipelineId'], |
| 208 |
preset_id=d['presetId'], |
| 209 |
name=d.get('name', None), |
| 210 |
description=d.get('description', None)) |
| 211 |
return presets |
| 212 |
|
| 213 |
|
| 214 |
@_json_path_or_d |
| 215 |
def load_pipeline_interface_from(d): |
| 216 |
bindings = {} |
| 217 |
epts = [EntryPoint.from_dict(d) for d in d["entryPoints"]] |
| 218 |
opts = [pacbio_option_from_dict(o) for o in d["taskOptions"]] |
| 219 |
return Pipeline(d['id'], |
| 220 |
d['name'], |
| 221 |
d['version'], |
| 222 |
d['description'], |
| 223 |
bindings, |
| 224 |
epts, |
| 225 |
tags=d['tags'], |
| 226 |
task_options=opts) |