-
Notifications
You must be signed in to change notification settings - Fork 21
/
cwl.go
195 lines (160 loc) · 4.27 KB
/
cwl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package cwl
import (
"encoding/json"
"errors"
"fmt"
"github.com/MG-RAST/AWE/lib/logger"
"github.com/davecgh/go-spew/spew"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v2"
//"io/ioutil"
//"os"
_ "reflect"
"strings"
)
// this is used by YAML or JSON library for inital parsing
type CWL_document_generic struct {
CwlVersion string `yaml:"cwlVersion"`
Graph []CWL_object_generic `yaml:"graph"`
}
type CWL_object interface {
GetClass() string
GetId() string
SetId(string)
}
type CWL_object_generic map[string]interface{}
// CWLType
// httpCWL_objectmonwl.org/v1.0/CommandLineTool.html#CWLType
// null, boolean, int, long, float, double, string, File, Directory
type CWLType interface {
is_CWLType()
}
type CWLVersion interface{} // TODO
// generic class to represent Files and Directories
type CWL_location interface {
GetLocation() string
}
type Any interface {
CWL_object
String() string
}
type LinkMergeMethod string // merge_nested or merge_flattened
func Parse_cwl_document(collection *CWL_collection, yaml_str string) (err error) {
// TODO check cwlVersion
// TODO screen for "$import": // this might break the YAML parser !
// this yaml parser (gopkg.in/yaml.v2) has problems with the CWL yaml format. We skip the header aand jump directly to "$graph" because of that.
graph_pos := strings.Index(yaml_str, "$graph:")
if graph_pos == -1 {
err = errors.New("yaml parisng error. keyword $graph missing")
return
}
yaml_str = strings.Replace(yaml_str, "$graph", "graph", -1) // remove dollar sign
cwl_gen := CWL_document_generic{}
err = Unmarshal([]byte(yaml_str), &cwl_gen)
if err != nil {
logger.Debug(1, "CWL unmarshal error")
logger.Error("error: " + err.Error())
}
fmt.Println("-------------- raw CWL")
spew.Dump(cwl_gen)
fmt.Println("-------------- Start real parsing")
// iterated over Graph
for _, elem := range cwl_gen.Graph {
cwl_object_type, ok := elem["class"].(string)
if !ok {
err = errors.New("object has no member class")
return
}
cwl_object_id := elem["id"].(string)
if !ok {
err = errors.New("object has no member id")
return
}
_ = cwl_object_id
switch elem["hints"].(type) {
case map[interface{}]interface{}:
// Convert map of outputs into array of outputs
err, elem["hints"] = CreateRequirementArray(elem["hints"])
if err != nil {
return
}
}
switch cwl_object_type {
case "CommandLineTool":
result, xerr := getCommandLineTool(elem)
if xerr != nil {
err = xerr
return
}
//*** check if "inputs"" is an array or a map"
//collection.CommandLineTools[result.Id] = result
err = collection.Add(result)
if err != nil {
return
}
//collection = append(collection, result)
case "Workflow":
workflow, xerr := getWorkflow(elem)
if xerr != nil {
err = xerr
return
}
// some checks and add inputs to collection
for _, input := range workflow.Inputs {
// input is InputParameter
if input.Id == "" {
err = fmt.Errorf("input has no ID")
return
}
if !strings.HasPrefix(input.Id, "inputs.") {
input.Id = "inputs." + input.Id
}
err = collection.Add(input)
if err != nil {
return
}
}
//fmt.Println("WORKFLOW")
//spew.Dump(workflow)
err = collection.Add(&workflow)
if err != nil {
return
}
//collection.Workflows = append(collection.Workflows, workflow)
//collection = append(collection, result)
case "File":
var cwl_file File
err = mapstructure.Decode(elem, &cwl_file)
if err != nil {
return
}
if cwl_file.Id == "" {
cwl_file.Id = cwl_object_id
}
//collection.Files[cwl_file.Id] = cwl_file
err = collection.Add(&cwl_file)
if err != nil {
return
}
default:
err = errors.New("object unknown")
return
} // end switch
fmt.Printf("----------------------------------------------\n")
} // end for
return
}
func Unmarshal(data []byte, v interface{}) (err error) {
err_yaml := yaml.Unmarshal(data, v)
if err_yaml != nil {
logger.Debug(1, "CWL YAML unmarshal error, (try json...) : "+err_yaml.Error())
err_json := json.Unmarshal(data, v)
if err_json != nil {
logger.Debug(1, "CWL JSON unmarshal error: "+err_json.Error())
}
}
if err != nil {
err = errors.New("Could not parse document as JSON or YAML")
}
return
}