comparison value_from_yaml_or_json.xml @ 0:47839d77a281 draft default tip

"planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/value_from_yaml_or_json commit 28fa7f14add96533ed9538b675a5fb58438566d5-dirty"
author jjohnson
date Thu, 14 Jul 2022 17:02:53 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:47839d77a281
1 <tool id="value_from_yaml_or_json" name="Value from YAML or JSON" version="0.1.0" python_template_version="3.5">
2 <requirements>
3 </requirements>
4 <command detect_errors="exit_code"><![CDATA[
5 #def getValue(contents, path)
6 #set $rslt = $contents
7 #set $keys = path.split(':')
8 #for k in $keys:
9 #if isinstance($rslt,dict)
10 #if k in $rslt.keys()
11 #set $rslt = $rslt[k]
12 #else
13 #for dk in $rslt.keys():
14 #if str(dk) == k
15 #set $rslt = $rslt[dk]
16 #end if
17 #end for
18 #end if
19 #elif isinstance($rslt,list)
20 #set $rslt = $rslt[int(k)]
21 #end if
22 #end for
23 #return $rslt
24 #end def
25 #set $fh = open(str($input),'r')
26 #if $input.is_of_type('yaml')
27 #import yaml
28 #set $data = $yaml.safe_load(fh.read())
29 #else
30 #import json
31 #set $data = $json.load($fh)
32 #end if
33 #silent $fh.close()
34 #set $value = $getValue($data, $path)
35 echo "$value" > '$output'
36 ]]></command>
37 <inputs>
38 <param name="input" type="data" format="yaml,json" label="YAML or JSON from which to extract value"/>
39 <param name="path" type="text" value="" label="path to value via dict keys or list indices"
40 help="separate keys/indexes with the colon char : ">
41 <sanitizer>
42 <valid initial="string.printable">
43 </valid>
44 </sanitizer>
45 </param>
46 </inputs>
47 <outputs>
48 <data name="output" format="txt" />
49 </outputs>
50 <tests>
51 <!-- Test-1 json dict -->
52 <test>
53 <param name="input" ftype="json" value="input.json"/>
54 <param name="path" value="beam:energy"/>
55 <output name="output">
56 <assert_contents>
57 <has_line line="41.9906" />
58 </assert_contents>
59 </output>
60 </test>
61 <!-- Test-2 json list -->
62 <test>
63 <param name="input" ftype="json" value="input.json"/>
64 <param name="path" value="calibration_crystal:0:orientation:1"/>
65 <output name="output">
66 <assert_contents>
67 <has_line line="0.6945507909200794" />
68 </assert_contents>
69 </output>
70 </test>
71 <!-- Test-3 yaml dict -->
72 <test>
73 <param name="input" ftype="yaml" value="input.yaml"/>
74 <param name="path" value="beam:energy"/>
75 <output name="output">
76 <assert_contents>
77 <has_line line="41.9906" />
78 </assert_contents>
79 </output>
80 </test>
81 <!-- Test-4 yaml list -->
82 <test>
83 <param name="input" ftype="yaml" value="input.yaml"/>
84 <param name="path" value="calibration_crystal:0:orientation:1"/>
85 <output name="output">
86 <assert_contents>
87 <has_line line="0.6945507909200794" />
88 </assert_contents>
89 </output>
90 </test>
91 </tests>
92 <help><![CDATA[
93 Extract a value from a YAML or JSON dataset and save to the output dataset.
94 It is expected that this will be most useful in workflows for extracting values that can be linked to parameters.
95
96 *If more elaborate filtering of JSON datasets is required, the* `JQ tool`_ *is available.*
97
98 .. _JQ tool: https://toolshed.g2.bx.psu.edu/view/iuc/jq/5ff75eb1a893
99
100 -----
101
102 With a JSON or YAML input dataset, specify the path for the value you wish to output by listing the key/index of the elements separated by the colon character.
103
104 **Example Input YAML File** ::
105
106 beam:
107 energy: 41.9906
108 vector:
109 azimuth: 90.0
110 polar_angle: 90.0
111 calibration_crystal:
112 0:
113 orientation:
114 - 1.2148784044413221
115 - 0.6945507909200794
116 - 0.765702046701113
117 position:
118 - 0.06060121926310595
119 - 0.1312120981443553
120 - -0.06694059935413639
121 1:
122 orientation:
123 - 1.4105172207115466
124 - -0.14336674130145605
125 - -0.37238882282338215
126 position:
127 - 0.016940740257706914
128 - -0.1183857552099456
129 - 0.34767784259162654
130
131 *Specifying path:* **beam:energy**
132 *outputs:* **41.9906**
133
134 *Specifying path:* **calibration_crystal:1:position:2**
135 *outputs:* **0.34767784259162654**
136
137 *Specifying path:* **calibration_crystal:1:position**
138 *outputs:* **[0.016940740257706914, -0.1183857552099456, 0.34767784259162654]**
139
140 ]]></help>
141 </tool>