Project

General

Profile

Feature #16424 » main.go

Nico César, 05/11/2020 08:13 PM

 
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
)

// CWL is the toplevel object that has all the grpah
type CWL struct {
Graph []Graph `json:"$graph"`
CwlVersion string `json:"cwlVersion"`
}

type Hints struct {
Class string `json:"class"`
LoadListing string `json:"loadListing"`
}

// Inputs are the pre-requisites for a step to run. Usually, files and directories
// some of them coming from other steps
type Inputs struct {
ID string `json:"id"`
Label string `json:"label"`
//most types are just string, but sometimes you'll find things like:
// "type": ["null", "Directory"]
// "type": { "items": "string", "type": "array" }
// or even:
// "type": {
// "items": { "items": ["File", "Directory"], "type": "array" },
// "type": "array"
// }

Type interface{} `json:"type"`
}

/*
// this is for outputs
type Type struct {
Items string `json:"items"`
Type string `json:"type"`
}*/

// Outputs are the produced files, directories, etc. from Steps
type Outputs struct {
ID string `json:"id"`
Label string `json:"label"`
OutputSource string `json:"outputSource"`
// most of the time this will be a {"items":...,"type":....}
// but sometimes you'll find:
// "type": "Directory"
// or even:
// "type": {
// "items": { "items": "File", "type": "array" },
// "type": "array"
// }
Type interface{} `json:"type"`
}

// Requirements is a list of requirements, usually a DockerPull will be signaling an image
type Requirements struct {
Class string `json:"class"`
DockerPull string `json:"dockerPull,omitempty"`
HTTPArvadosOrgCwlDockerCollectionPDH string `json:"http://arvados.org/cwl#dockerCollectionPDH,omitempty"`
}
type In struct {
ID string `json:"id"`
Source string `json:"source"`
}

// Steps is the central struct to define the acyclic graph of steps using In's and Out's
type Steps struct {
ID string `json:"id"`
In []In `json:"in"`
Out []string `json:"out"`
Run string `json:"run"`
// Scatter this could be a string for one step or an array for several steps
Scatter interface{} `json:"scatter,omitempty"`
}
type Graph struct {
Namespaces map[string]string `json:"$namespaces,omitempty"`
Class string `json:"class"`
Hints []Hints `json:"hints,omitempty"`
ID string `json:"id"`
Inputs []Inputs `json:"inputs"`
Label string `json:"label,omitempty"`
Outputs []Outputs `json:"outputs"`
Requirements []Requirements `json:"requirements,omitempty"`
Steps []Steps `json:"steps,omitempty"`

// this is usually a []string with all the arguments,but
// sometimes we can find things like:
//"arguments": [
// "$(inputs.bashscript)",
// { "prefix": "-s", "valueFrom": "$(inputs.srclib)" },
// { "prefix": "-n", "valueFrom": "$(inputs.newlib)" },
// "bar",
// "$(inputs.foo)"
// ],
Arguments []interface{} `json:"arguments,omitempty"`
BaseCommand string `json:"baseCommand,omitempty"`
Expression string `json:"expression,omitempty"`
}

//Dotty will give back a graph to print with dotty
func Dotty(result CWL) string {
const stepStyle = `[fillcolor="#FFD700", style="rounded,filled", shape=box]`
const cmdStyle = `[fillcolor="#FF9912", style="rounded,filled", shape=box]`
var nodes string
var edges string

for _, g := range result.Graph {

if g.Class == "CommandLineTool" {
nodes += fmt.Sprintf("\"cmd %s\" %s;\n", g.ID, cmdStyle)

for _, input := range g.Inputs {
edges += fmt.Sprintf("\"%s\" -> \"cmd %s\";\n", input.ID, g.ID)
}
for _, output := range g.Outputs {
if output.OutputSource != "" {
edges += fmt.Sprintf("\"cmd %s\" -> \"%s\";\n", g.ID, output.OutputSource)
} else {
edges += fmt.Sprintf("\"cmd %s\" -> \"%s\";\n", g.ID, output.ID)
}
}

}

for _, step := range g.Steps {
nodes += fmt.Sprintf("\"step %s\" %s;\n", step.ID, stepStyle)

for _, in := range step.In {
if in.Source != "" {
edges += fmt.Sprintf("\"%s\" -> \"step %s\";\n", in.Source, step.ID)
} else {
edges += fmt.Sprintf("\"%s\" -> \"step %s\";\n", in.ID, step.ID)
}
}

// make all dependencies for outputs
for _, out := range step.Out {
edges += fmt.Sprintf("\"step %s\" -> \"%s\";\n", step.ID, out)
}
}
}

return fmt.Sprintf("digraph cwlgraph {\nrankdir=LR;\n\n%s\n\n%s}\n", nodes, edges)
}

func main() {

// Open our jsonFile
jsonFile, err := os.Open("su92l-xvhdp-3gri0mi1vtakaf4.json")
// if we os.Open returns an error then handle it
if err != nil {
fmt.Println(err)
}
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()

byteValue, _ := ioutil.ReadAll(jsonFile)

var result CWL
err = json.Unmarshal([]byte(byteValue), &result)
if err != nil {
log.Fatalf("Error parsing json %v", err.Error())
}

fmt.Printf("%s\n", Dotty(result))

}
(1-1/5)