-
Notifications
You must be signed in to change notification settings - Fork 21
/
cwl2awe.go
269 lines (212 loc) · 6.68 KB
/
cwl2awe.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package core
import (
"errors"
"fmt"
"github.com/MG-RAST/AWE/lib/acl"
"github.com/MG-RAST/AWE/lib/core/cwl"
"github.com/MG-RAST/AWE/lib/logger"
"github.com/MG-RAST/AWE/lib/user"
"github.com/davecgh/go-spew/spew"
//aw_sequences"os"
"path"
//"reflect"
//"strconv"
//"regexp/syntax"
"strings"
)
type Helper struct {
unprocessed_ws *map[string]*cwl.WorkflowStep
processed_ws *map[string]*cwl.WorkflowStep
collection *cwl.CWL_collection
job *Job
AWE_tasks *map[string]*Task
}
func parseSourceString(source string, id string) (linked_step_name string, fieldname string, err error) {
if !strings.HasPrefix(source, "#") {
err = fmt.Errorf("source has to start with # (%s)", id)
return
}
source = strings.TrimPrefix(source, "#")
source_array := strings.Split(source, "/")
switch len(source_array) {
case 0:
err = fmt.Errorf("source empty (%s)", id)
case 1:
fieldname = source_array[0]
case 2:
linked_step_name = source_array[0]
fieldname = source_array[1]
default:
err = fmt.Errorf("source has too many fields (%s)", id)
}
return
}
func CWL_input_check(job_input *cwl.Job_document, cwl_workflow *cwl.Workflow) (err error) {
//job_input := *(collection.Job_input)
job_input_map := job_input.GetMap() // map[string]CWLType
for _, input := range cwl_workflow.Inputs {
// input is a cwl.InputParameter object
spew.Dump(input)
id := input.Id
logger.Debug(3, "(CWL_input_check) Parsing workflow input %s", id)
id_base := path.Base(id)
expected_types := input.Type
if len(expected_types) == 0 {
err = fmt.Errorf("(CWL_input_check) (len(expected_types) == 0 ")
return
}
// find workflow input in job_input_map
input_obj_ref, ok := job_input_map[id_base] // returns CWLType
if !ok {
// not found, we can skip it it is optional anyway
input_obj_ref = cwl.NewNull(id_base)
logger.Debug(3, "input %s not found, replace with Null object")
}
if input_obj_ref == nil {
err = fmt.Errorf("(CWL_input_check) input_obj_ref == nil")
return
}
// Get type of CWL_Type we found
input_type := input_obj_ref.GetType()
if input_type == nil {
err = fmt.Errorf("(CWL_input_check) input_type == nil %s", spew.Sdump(input_obj_ref))
return
}
//input_type_str := "unknown"
logger.Debug(1, "(CWL_input_check) input_type: %s (%s)", input_type, input_type.Type2String())
// Check if type of input we have matches one of the allowed types
has_type, xerr := cwl.TypeIsCorrect(expected_types, input_obj_ref)
if xerr != nil {
err = fmt.Errorf("(CWL_input_check) (B) HasInputParameterType returns: %s", xerr.Error())
return
}
if !has_type {
//if strings.ToLower(obj_type) != strings.ToLower(expected_types) {
fmt.Printf("object found: ")
spew.Dump(input_obj_ref)
//for _, elem := range *expected_types {
// expected_types_str += "," + string(elem)
//}
fmt.Printf("cwl_workflow.Inputs")
spew.Dump(cwl_workflow.Inputs)
err = fmt.Errorf("Input %s has type %s, but this does not match the expected types)", id, input_type)
return
}
}
return
}
func CreateTasks(job *Job, workflow string, steps []cwl.WorkflowStep) (tasks []*Task, err error) {
tasks = []*Task{}
for s, _ := range steps {
step := steps[s] // I could not do "_, step := range", that leas to very strange behaviour ?!??!
//task_name := strings.Map(
// func(r rune) rune {
// if syntax.IsWordChar(r) || r == '/' || r == '-' { // word char: [0-9A-Za-z_]
// return r
// }
// return -1
// },
// step.Id)
if !strings.HasPrefix(step.Id, "#") {
err = fmt.Errorf("Workflow step name does not start with a #: %s", step.Id)
return
}
task_name := strings.TrimSuffix(step.Id, "/")
if task_name == "" {
err = fmt.Errorf("(CreateTasks) step_id is empty")
return
}
//task_name := strings.TrimPrefix(step.Id, "#main/")
//task_name = strings.TrimPrefix(task_name, "#")
var awe_task *Task
awe_task, err = NewTask(job, workflow, task_name)
if err != nil {
err = fmt.Errorf("(CreateTasks) NewTask returned: %s", err.Error())
return
}
awe_task.WorkflowStep = &step
//spew.Dump(step)
tasks = append(tasks, awe_task)
}
return
}
func CWL2AWE(_user *user.User, files FormFiles, job_input *cwl.Job_document, cwl_workflow *cwl.Workflow, collection *cwl.CWL_collection) (job *Job, err error) {
//CommandLineTools := collection.CommandLineTools
// check that all expected workflow inputs exist and that they have the correct type
logger.Debug(1, "CWL2AWE starting")
err = CWL_input_check(job_input, cwl_workflow)
if err != nil {
err = fmt.Errorf("(CWL2AWE) CWL_input_check returned: %s", err.Error())
return
}
//os.Exit(0)
job = NewJob()
job.setId()
//job.CWL_workflow = cwl_workflow
logger.Debug(1, "Job created")
found_ShockRequirement := false
for _, r := range cwl_workflow.Requirements { // TODO put ShockRequirement in Hints
req, ok := r.(cwl.Requirement)
if !ok {
err = fmt.Errorf("not a requirement")
return
}
switch req.GetClass() {
case "ShockRequirement":
sr, ok := req.(cwl.ShockRequirement)
if !ok {
err = fmt.Errorf("Could not assert ShockRequirement")
return
}
job.ShockHost = sr.Host
found_ShockRequirement = true
}
}
if !found_ShockRequirement {
//err = fmt.Errorf("ShockRequirement has to be provided in the workflow object")
//return
job.ShockHost = "http://shock:7445"
}
logger.Debug(1, "Requirements checked")
// Once, job has been created, set job owner and add owner to all ACL's
job.Acl.SetOwner(_user.Uuid)
job.Acl.Set(_user.Uuid, acl.Rights{"read": true, "write": true, "delete": true})
// TODO first check that all resources are available: local files and remote links
main_wi := WorkflowInstance{Id: "::main::", Inputs: *job_input, RemainTasks: len(cwl_workflow.Steps)}
//new_wis := []WorkflowInstance{main_wi} // Not using AddWorkflowInstance to avoid mongo
job.WorkflowInstances = make([]interface{}, 1)
job.WorkflowInstances[0] = main_wi
//if err != nil {
// return
//}
var tasks []*Task
tasks, err = CreateTasks(job, "", cwl_workflow.Steps)
if err != nil {
return
}
job.Tasks = tasks
_, err = job.Init()
if err != nil {
err = fmt.Errorf("job.Init() failed: %s", err.Error())
return
}
logger.Debug(1, "Init called")
err = job.Mkdir()
if err != nil {
err = errors.New("(CWL2AWE) error creating job directory, error=" + err.Error())
return
}
err = job.UpdateFile(files, "cwl") // TODO that may not make sense. Check if I should store AWE job.
if err != nil {
err = errors.New("error in UpdateFile, error=" + err.Error())
return
}
spew.Dump(job)
logger.Debug(1, "job.Id: %s", job.Id)
err = job.Save()
if err != nil {
err = errors.New("error in job.Save(), error=" + err.Error())
return
}
return
}