-
Notifications
You must be signed in to change notification settings - Fork 21
/
cwl2awe.go
350 lines (268 loc) · 8.99 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package core
import (
"errors"
"fmt"
"reflect"
"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"
)
// Helper _
type Helper struct {
unprocessedWS *map[string]*cwl.WorkflowStep
processedWS *map[string]*cwl.WorkflowStep
//collection *cwl.CWL_collection
job *Job
AWETasks *map[string]*Task
}
func parseSourceString(source string, id string) (linkedStepName string, fieldname string, err error) {
if !strings.HasPrefix(source, "#") {
err = fmt.Errorf("source has to start with # (%s)", id)
return
}
source = strings.TrimPrefix(source, "#")
sourceArray := strings.Split(source, "/")
switch len(sourceArray) {
case 0:
err = fmt.Errorf("source empty (%s)", id)
case 1:
fieldname = sourceArray[0]
case 2:
linkedStepName = sourceArray[0]
fieldname = sourceArray[1]
default:
err = fmt.Errorf("source has too many fields (%s)", id)
}
return
}
// CWLInputCheck _
func CWLInputCheck(jobInput *cwl.Job_document, cwlWorkflow *cwl.Workflow, context *cwl.WorkflowContext) (jobInputNew *cwl.Job_document, err error) {
//jobInput := *(collection.Job_input)
jobInputMap := jobInput.GetMap() // map[string]CWLType
for _, input := range cwlWorkflow.Inputs {
// input is a cwl.InputParameter object
//spew.Dump(input)
id := input.Id
logger.Debug(3, "(CWLInputCheck) Parsing workflow input %s", id)
idBase := path.Base(id)
expectedTypes := input.Type
if len(expectedTypes) == 0 {
err = fmt.Errorf("(CWLInputCheck) (len(expected_types) == 0 ")
return
}
// find workflow input in jobInputMap
inputObjRef, ok := jobInputMap[idBase] // returns CWLType
if !ok {
// not found, we can skip it it is optional anyway
if input.Default != nil {
logger.Debug(3, "input %s not found, replace with Default object", idBase)
inputObjRef = input.Default
//input_obj_ref_file, ok := input_obj_ref.(*cwl.File)
jobInput = jobInput.Add(idBase, inputObjRef)
} else {
inputObjRef = cwl.NewNull()
logger.Debug(3, "input %s not found, replace with Null object (no Default found)", idBase)
}
}
if inputObjRef == nil {
err = fmt.Errorf("(CWLInputCheck) input_obj_ref == nil")
return
}
// Get type of CWL_Type we found
inputType := inputObjRef.GetType()
if inputType == nil {
err = fmt.Errorf("(CWLInputCheck) input_type == nil %s", spew.Sdump(inputObjRef))
return
}
//input_type_str := "unknown"
logger.Debug(1, "(CWLInputCheck) input_type: %s (%s)", inputType, inputType.Type2String())
// Check if type of input we have matches one of the allowed types
hasType, xerr := cwl.TypeIsCorrect(expectedTypes, inputObjRef, context)
if xerr != nil {
err = fmt.Errorf("(CWLInputCheck) (B) HasInputParameterType returns: %s", xerr.Error())
return
}
if !hasType {
if inputObjRef.GetType() == cwl.CWLNull && input.Default != nil {
// work-around to make sure ExpressionTool can use Default if it gets Null
} else {
//if strings.ToLower(obj_type) != strings.ToLower(expected_types) {
fmt.Printf("object found: ")
spew.Dump(inputObjRef)
expectedTypesStr := ""
for _, elem := range expectedTypes {
expectedTypesStr += "," + elem.Type2String()
}
//fmt.Printf("cwl_workflow.Inputs")
//spew.Dump(cwl_workflow.Inputs)
err = fmt.Errorf("(CWLInputCheck) Input %s has type %s, but this does not match the expected types(%s)", id, inputType, expectedTypesStr)
return
}
}
}
jobInputNew = jobInput
return
}
// CreateWorkflowTasks _
func CreateWorkflowTasks(job *Job, namePrefix string, steps []cwl.WorkflowStep, stepPrefix string, parentID *Task_Unique_Identifier) (tasks []*Task, err error) {
tasks = []*Task{}
if parentID == nil {
err = fmt.Errorf("(CreateWorkflowTasks) parent_id == nil")
return
}
if !strings.HasPrefix(namePrefix, "#main") {
err = fmt.Errorf("(CreateWorkflowTasks) prefix_name does not start with _entrypoint: %s", namePrefix)
return
}
for s := range steps {
step := steps[s]
if namePrefix == "" {
err = fmt.Errorf("(CreateWorkflowTasks) name_prefix empty")
return
}
if !strings.HasPrefix(step.Id, "#") {
err = fmt.Errorf("(CreateWorkflowTasks) Workflow step name does not start with a #: %s", step.Id)
return
}
fmt.Println("(CreateWorkflowTasks) step.Id: " + step.Id)
// remove prefix
taskName := path.Base(step.Id)
//taskName := strings.TrimPrefix(step.Id, step_prefix)
//taskName = strings.TrimSuffix(taskName, "/")
//taskName = strings.TrimPrefix(taskName, "/")
//fmt.Println("(CreateWorkflowTasks) taskName: " + taskName)
//fmt.Println("(CreateWorkflowTasks) name_prefix: " + name_prefix)
fmt.Println("(CreateWorkflowTasks) new task name will be: " + namePrefix + " / " + taskName)
//taskName := strings.TrimPrefix(step.Id, "#main/")
//taskName = strings.TrimPrefix(taskName, "#")
fmt.Printf("(CreateWorkflowTasks) creating task: %s %s\n", namePrefix, taskName)
var aweTask *Task
aweTask, err = NewTask(job, namePrefix, taskName)
if err != nil {
err = fmt.Errorf("(CreateWorkflowTasks) NewTask returned: %s", err.Error())
return
}
//aweTask.WorkflowParent = parent_id
if step.Id == "" {
err = fmt.Errorf("(CreateWorkflowTasks) step.Id empty")
return
}
aweTask.WorkflowStep = &step
//spew.Dump(step)
tasks = append(tasks, aweTask)
}
return
}
// CWL2AWE _
func CWL2AWE(_user *user.User, files FormFiles, jobInput *cwl.Job_document, cwlWorkflow *cwl.Workflow, context *cwl.WorkflowContext) (job *Job, err error) {
// check that all expected workflow inputs exist and that they have the correct type
logger.Debug(1, "(CWL2AWE) CWL2AWE starting..")
defer logger.Debug(1, "(CWL2AWE) CWL2AWE leaving...")
var jobInputNew *cwl.Job_document
jobInputNew, err = CWLInputCheck(jobInput, cwlWorkflow, context)
if err != nil {
err = fmt.Errorf("(CWL2AWE) CWLInputCheck returned: %s", err.Error())
return
}
//os.Exit(0)
job = NewJob()
job.setId()
job.WorkflowContext = context
//job.CWL_workflow = cwl_workflow
logger.Debug(1, "(CWL2AWE) Job created")
foundShockRequirement := false
if cwlWorkflow.Requirements != nil {
for _, r := range cwlWorkflow.Requirements { // TODO put ShockRequirement in Hints
req, ok := r.(cwl.Requirement)
if !ok {
err = fmt.Errorf("(CWL2AWE) not a requirement")
return
}
switch req.GetClass() {
case "ShockRequirement":
sr, ok := r.(*cwl.ShockRequirement)
if !ok {
err = fmt.Errorf("(CWL2AWE) Could not assert ShockRequirement (type: %s)", reflect.TypeOf(r))
return
}
job.ShockHost = sr.Shock_api_url
foundShockRequirement = true
}
}
}
if !foundShockRequirement {
err = fmt.Errorf("(CWL2AWE) ShockRequirement has to be provided in the workflow object")
return
//job.ShockHost = "http://shock:7445" // TODO make this different
}
logger.Debug(1, "(CWL2AWE) 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})
logger.Debug(1, "(CWL2AWE) ACLs set")
// TODO first check that all resources are available: local files and remote links
// *** create WorkflowInstance
var wi *WorkflowInstance
wi, err = NewWorkflowInstance("#main", job.ID, cwlWorkflow.Id, job, "") // Not using AddWorkflowInstance to avoid mongo
if err != nil {
err = fmt.Errorf("(CWL2AWE) NewWorkflowInstance returned: %s", err.Error())
return
}
wi.Inputs = *jobInputNew
logger.Debug(1, "(CWL2AWE) WorkflowInstance #main created")
// create path
//for i, _ := range wi.Inputs {
// new_id := cwl_workflow.Id + "/" + wi.Inputs[i].Id
//wi.Inputs[i].Id = new_id
//err = context.Add(new_id, wi.Inputs[i].Value, "CWL2AWE")
//if err != nil {
// err = fmt.Errorf("(CWL2AWE) context.Add returned: %s", err.Error())
// return
//}
//}
err = wi.Save(false)
if err != nil {
err = fmt.Errorf("(CWL2AWE) wi.Save returned: %s", err.Error())
return
}
logger.Debug(1, "(CWL2AWE) wi saved")
err = job.AddWorkflowInstance(wi, DbSyncFalse, false) // adding _root
if err != nil {
err = fmt.Errorf("(CWL2AWE) AddWorkflowInstance returned: %s", err.Error())
return
}
logger.Debug(1, "(CWL2AWE) wi added")
_, err = job.Init()
if err != nil {
err = fmt.Errorf("(CWL2AWE) job.Init() failed: %s", err.Error())
return
}
logger.Debug(1, "(CWL2AWE) 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("(CWL2AWE) error in UpdateFile, error=" + err.Error())
return
}
//spew.Dump(job)
//panic("done")
logger.Debug(1, "(CWL2AWE) job.Id: %s", job.ID)
err = job.Save()
if err != nil {
err = errors.New("(CWL2AWE) error in job.Save(), error=" + err.Error())
return
}
return
}