golaxy

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2023 License: GPL-2.0 Imports: 16 Imported by: 2

README

GoDoc

golaxy : interacting with Galaxy in Go

Golang API to interact with the Galaxy API.

Example of usage

Launch a single tool and monitor its execution

package main

import (
	"fmt"
	"time"

	"github.com/fredericlemoine/golaxy"
)

func main() {
	var err error
	var g *golaxy.Galaxy
	var historyid string
	var infileid string
	var jobids []string
	var outfiles map[string]string
	var jobstate string
	var filecontent []byte
	var tl *golaxy.ToolLaunch

	g = golaxy.NewGalaxy("http://galaxyip:port", "apikey", false)

	// Creates new history
	if historyid, err = g.CreateHistory("My history"); err != nil {
		panic(err)
	}

	// Uploads a file
	if infileid, _, err = g.UploadFile(historyid, "/path/to/file","auto"); err != nil {
		panic(err)
	}

	// Searches for the right tool id
	var my_tool string
	if tools, err := g.SearchToolID("my_tool"); err != nil {
		panic(err)
	} else {
		if len(tools)==0 {
			panic("no tool found")
		} else {
			fmt.Println(fmt.Sprintf("%d tools found",len(tools)))
			my_tool=tools[len(tools)-1]
		}
	}

	// Launches a new  Job
	tl = g.NewToolLauncher(historyid, my_tool)
	tl.AddParameter("option", "optionvalue")
	tl.AddFileInput("input", infileid, "hda")
	
	if _, jobids, err = g.LaunchTool(tl); err != nil {
		panic(err)
	}
	if len(jobids) < 1 {
		panic("No jobs")
	}

	end := false
	for !end {
		// Checks job status
		if jobstate, outfiles, err = g.CheckJob(jobids[0]); err != nil {
			panic(err)
		}

		if jobstate == "ok" {
			for _, id := range outfiles {
				// Downloads output files
				if filecontent, err = g.DownloadFile(historyid, id); err != nil {
					panic(err)
				}
				fmt.Println(string(filecontent))
			}
			end = true
		} else {
			fmt.Println("State:" + jobstate)
			for name, id := range outfiles {
				fmt.Println(name + "=>" + id)
			}
		}
		time.Sleep(2 * time.Second)
	}
	// Deletes history
	if jobstate, err = g.DeleteHistory(historyid); err != nil {
		panic(err)
	}
	fmt.Println(jobstate)
}

Launch a workflow and monitor its execution

package main

import (
	"fmt"
	"time"

	"github.com/fredericlemoine/golaxy"
)

func main() {
	g := golaxy.NewGalaxy("http://galaxyip:port", "apikey", false)
	var err error
	var historyid string
	var infileid string
	var wfids []string
	var wfinvocation *golaxy.WorkflowInvocation
	var workflowstate *golaxy.WorkflowStatus

	// Creates a new history
	if historyid, err = g.CreateHistory("My history"); err != nil {
		panic(err)
	}

	// Searches the workflow with given id (checks that it exists) 
	if wfids, err = g.SearchWorkflowIDs("cc1fde5b44055598"); err != nil {
		panic(err)
	}

	// Uploads input file
	if infileid, _, err = g.UploadFile(historyid, "/path/to/file", "auto"); err != nil {
		panic(err)
	}

	// Initializes a launcher
	l := g.NewWorkflowLauncher(historyid, wfids[0])
	l.AddFileInput("<input number>", infileid, "hda")
	l.AddParameter(<step number>, "param name", "param value")
	if wfinvocation, err = g.LaunchWorkflow(l); err != nil {
		panic(err)
	}

	// Now waits for the end of the execution
	end := false
	for !end {
		if workflowstate, err = g.CheckWorkflow(wfinvocation); err != nil {
			panic(err)
		}
		fmt.Println("Workflow Status: " + workflowstate.Status())
		// For all steps 
		for steprank := range workflowstate.ListStepRanks() {
			var status string
			var outfilenames []string
			var fileid string
			// Status of the given step
			if status, err = workflowstate.StepStatus(steprank); err == nil {
				fmt.Println("\t Job " + fmt.Sprintf("%d", steprank) + " = " + status)
				// Names and ids of outfiles of this step
				if outfilenames, err = workflowstate.StepOutFileNames(steprank); err != nil {
					panic(err)
				}
				for _, name := range outfilenames {
					if fileid, err = workflowstate.StepOutputFileId(steprank, name); err == nil {
						fmt.Println("\t\tFile " + name + " = " + fileid)
					}
				}
			}
		}
		end = (workflowstate.Status() == "ok" || workflowstate.Status() == "unknown" || workflowstate.Status()=="deleted")
		time.Sleep(2 * time.Second)
	}
	
	// Deletes history
	if jobstate, err = g.DeleteHistory(historyid); err != nil {
		panic(err)
	}

}

Documentation

Index

Constants

View Source
const (
	HISTORY   = "/api/histories"
	CHECK_JOB = "/api/jobs/"
	TOOLS     = "/api/tools"
	WORKFLOWS = "/api/workflows"
	VERSION   = "/api/version"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Galaxy

type Galaxy struct {
	// contains filtered or unexported fields
}

func NewGalaxy

func NewGalaxy(url, key string, trustcertificate bool) *Galaxy

Initializes a new Galaxy with given:

  • url of the form http(s)://ip:port
  • and an api key

func (*Galaxy) CheckJob

func (g *Galaxy) CheckJob(jobid string) (jobstate string, outfiles map[string]string, err error)

Queries the galaxy instance to check the job defined by its Id Returns:

  • job State
  • Output files: map : key: out filename value: out file id

func (*Galaxy) CheckWorkflow

func (g *Galaxy) CheckWorkflow(wfi *WorkflowInvocation) (wfstatus *WorkflowStatus, err error)

This function Checks the status of each step of the workflow

It returns workflowstatus: An indicator of the whole workflow status, its step status, and its step outputfiles. The whole status is computed as follows:

  • If all steps are "ok": then == "ok"
  • Else if one step is "error": then == "error"
  • Else if one step is "deleted": then == "deleted"
  • Else if one step is "running": then == "running"
  • Else if one step is "queued": then == "queued"
  • Else if one step is "waiting": then == "waiting"
  • Else if one is is "new": then == "new"
  • Else : Unknown state

func (*Galaxy) CreateHistory

func (g *Galaxy) CreateHistory(name string) (history HistoryFullInfo, err error)

Creates an history with given name on the Galaxy instance and returns its id

func (*Galaxy) DeleteHistory

func (g *Galaxy) DeleteHistory(historyid string) (state string, err error)

Deletes and purges an history defined by its id Returns:

  • The state of the deletion ("ok")
  • A potential error

func (*Galaxy) DeleteWorkflow

func (g *Galaxy) DeleteWorkflow(workflowid string) (state string, err error)

Deletes a Workflow defined by its id

  • The state of the deletion ("Workflow '<name>' successfully deleted" for example)
  • A potential error if the workflow cannot be deleted (server response does not contain "successfully deleted")

func (*Galaxy) DeleteWorkflowRun

func (g *Galaxy) DeleteWorkflowRun(wfi *WorkflowInvocation) (err error)

Cancels a running workflow.

TODO: handle json response from the server in case of success... nothing described.

func (*Galaxy) DownloadFile

func (g *Galaxy) DownloadFile(historyid, fileid string) (content []byte, err error)

Downloads a file defined by its id from the given history of the galaxy instance Returns:

  • The content of the file in []byte
  • A potential error

func (*Galaxy) GetToolById

func (g *Galaxy) GetToolById(id string) (tool ToolInfo, err error)

func (*Galaxy) GetWorkflowByID

func (g *Galaxy) GetWorkflowByID(inputid string, published bool) (wf WorkflowInfo, err error)

Call galaxy api to look for a workflow with the given ID

If published: then search the workflow in the published+imported workflows

func (*Galaxy) ImportSharedWorkflow

func (g *Galaxy) ImportSharedWorkflow(sharedworkflowid string) (workflow WorkflowInfo, err error)

Lists all the workflows imported in the user's account

func (*Galaxy) LaunchTool

func (g *Galaxy) LaunchTool(tl *ToolLaunch) (outfiles map[string]string, jobids []string, err error)

Launches a job at the given galaxy instance, with:

  • The tool given by its id (name)
  • Using the given history
  • Giving as input the files in the map : key: tool input name, value: dataset id

Returns:

  • Tool outputs : map[out file name]=out file id
  • Jobs: array of job ids

func (*Galaxy) LaunchWorkflow

func (g *Galaxy) LaunchWorkflow(launch *WorkflowLaunch) (answer *WorkflowInvocation, err error)

Launches the given workflow (defined by its ID), with given inputs and params.

Infiles are defined by their indexes on the workflow

Inparams are defined as key: step id of the workflow, value: map of key:param name/value: param value

func (*Galaxy) ListHistories

func (g *Galaxy) ListHistories() (histories []HistoryShortInfo, err error)

func (*Galaxy) ListWorkflows

func (g *Galaxy) ListWorkflows(published bool) (workflows []WorkflowInfo, err error)

Lists all the workflows imported in the user's account

If published: then lists published+imported workflows

func (*Galaxy) NewToolLauncher

func (g *Galaxy) NewToolLauncher(historyid string, toolid string) (tl *ToolLaunch)

Initializes a ToolLaunch that will be used to start a new job with the given tool on the given history

func (*Galaxy) NewWorkflowLauncher

func (g *Galaxy) NewWorkflowLauncher(historyid string, workflowid string) (launch *WorkflowLaunch)

func (*Galaxy) SearchToolID

func (g *Galaxy) SearchToolID(name string) (toolIds []string, err error)

This function returns ID of the tools corresponding to the name/ID in argument.

It first queryies the

api/tools/<name>

entry point. If The returned tool has its ID == given name then returns it. Otherwise, it queries the galaxy entry point

api/tools?q=<name>

func (*Galaxy) SearchWorkflowIDs

func (g *Galaxy) SearchWorkflowIDs(name string, published bool) (ids []string, err error)

Search a workflow on the glaaxy server.

It will first search for a workflow with ID exactly equal to the given name. If it does not exist, then will search a workflow having a lower(name) matching containing the given lower(name).

func (*Galaxy) SearchWorkflowIDsByName

func (g *Galaxy) SearchWorkflowIDsByName(name string, published bool) (ids []string, err error)

Searches workflow ids by name.

If published: then search the workflow in the published+imported workflows

func (*Galaxy) SetNbRequestAttempts

func (g *Galaxy) SetNbRequestAttempts(attempts int)

Sets the number of times golaxy will try to execute a request on the galaxy server if an error occurs (like a timeout).

Default: 1

It only applies on get, post and delete http request errors (timeout, etc.), and not Galaxy server errors.

If given attempts is <=0, will not change anything.

func (*Galaxy) UploadFile

func (g *Galaxy) UploadFile(historyid string, path string, ftype string) (fileid, jobid string, err error)

Uploads the given file to the galaxy instance in the history defined by its id and the given type (auto/txt/nhx/etc.)

Returns the file id, the job id and a potential error

func (*Galaxy) Version

func (g *Galaxy) Version() (version string, err error)

Returns the Version of the Galaxy Server

type HistoryFullInfo

type HistoryFullInfo struct {
	Importable        bool                `json:"importable"`
	Create_time       string              `json:"create_time"`
	Contents_url      string              `json:"contente_url"`
	Id                string              `json:"id"`
	Size              int                 `json:"size"`
	User_id           string              `json:"user_id"`
	Username_and_slug string              `json:"username_and_slug"`
	Annotation        string              `json:"annotation"`
	State_details     HistoryStateDetails `json:"state_details"`
	State             string              `json:"state"`
	Empty             bool                `json:"empty"`
	Update_time       string              `json:"update_time"`
	Tags              []string            `json:"tags"`
	Deleted           bool                `json:"deleted"`
	Genome_build      string              `json:"genome_build"`
	Slug              string              `json:"slug"`
	Name              string              `json:"name"`
	Url               string              `json:"url"`
	State_ids         HistoryStateIds     `json:"state_ids"`
	Published         bool                `json:"published"`
	Model_class       string              `json:"model_class"`
	Purged            bool                `json:"purged"`
	Err_msg           string              `json:"err_msg"`  // In case of error, this field is !=""
	Err_code          int                 `json:"err_code"` // In case of error, this field is !=0
}

Response after the creation/deletion of a history

type HistoryShortInfo

type HistoryShortInfo struct {
	Annotation  string   `json:"annotation"`
	Deleted     bool     `json:"deleted"`
	Id          string   `json:"id"`
	Model_class string   `json:"model_class"`
	Name        string   `json:"name"`
	Published   bool     `json:"published"`
	Purged      bool     `json:"purged"`
	Tags        []string `json:"tags"`
	Url         string   `json:"url"`
}

type HistoryStateDetails

type HistoryStateDetails struct {
	Paused           int `json:"paused"`
	Ok               int `json:"ok"`
	Failed_metadata  int `json:"failed_metadata"`
	Upload           int `json:"upload"`
	Discarded        int `json:"discarded"`
	Running          int `json:"running"`
	Setting_metadata int `json:"setting_metadata"`
	Error            int `json:"error"`
	New              int `json:"new"`
	Queued           int `json:"queued"`
	Empty            int `json:"empty"`
}

Detail of the job state

type HistoryStateIds

type HistoryStateIds struct {
	Paused           []string `json:"paused"`
	Ok               []string `json:"ok"`
	Failed_metadata  []string `json:"failed_metadata"`
	Upload           []string `json:"upload"`
	Discarded        []string `json:"discarded"`
	Running          []string `json:"running"`
	Setting_metadata []string `json:"setting_metadata"`
	Error            []string `json:"error"`
	New              []string `json:"new"`
	Queued           []string `json:"queued"`
	Empty            []string `json:"empty"`
}

Id of jobs in different states

type ToolInfo

type ToolInfo struct {
	Description          string             `json:"description"` // Description of the tool
	Edam_operations      []string           `json:"edam_operations"`
	Edam_topics          []string           `json:"edam_topics"`
	Form_style           string             `json:"form_style"`
	Id                   string             `json:"id"`
	Labels               []string           `json:"labels"`
	Model_class          string             `json:"model_class"`
	Name                 string             `json:"name"`
	Panel_Section_Id     string             `json:"panel_section_id"`
	Panel_Section_Name   string             `json:"panel_section_name"`
	Tool_Shed_Repository ToolShedRepository `json:"tool_shed_repository"`
	Version              string             `json:"version"`
	Traceboack           string             `json:"traceback"` // Set only if the server returns an error
	Err_Msg              string             `json:"err_msg"`   // Err_Msg =="" if no error
	Err_Code             string             `json:"err_code"`  // Err_Code=="" if no error
}

Informations about a specific tool

type ToolLaunch

type ToolLaunch struct {
	History_id string                 `json:"history_id"` // Id of history
	Tool_id    string                 `json:"tool_id"`    // Id of the tool
	Inputs     map[string]interface{} `json:"inputs"`     // Inputs: key name of the input, value dataset id
}

Request to call a tool

func (*ToolLaunch) AddFileInput

func (tl *ToolLaunch) AddFileInput(paramname string, fileid string, filescr string)

Add new input file to the Tool Launcher

  • inputIndex : index of this input in the workflow (see WorkflowInfo / GetWorkflowById)
  • fielId: id of input file
  • fielScr : one of ["ldda", "ld", "hda", "hdca"]

func (*ToolLaunch) AddParameter

func (tl *ToolLaunch) AddParameter(paramname, paramvalue string)

Add new parameter to Tool launcher

  • paramname: name of the tool parameter
  • paramvalue: value of the given parameter

type ToolShedRepository

type ToolShedRepository struct {
	Changeset_Revision string `json:"changeset_revision"`
	Name               string `json:"name"`
	Owner              string `json:"owner"`
	Tool_shed          string `json:"tool_shed"`
}

Informations about a toolshed

type WorkflowInfo

type WorkflowInfo struct {
	Annotation           string                   `json:"annotation"`
	Deleted              bool                     `json:"deleted"`
	Id                   string                   `json:"id"`
	Inputs               map[string]WorkflowInput `json:"inputs"`
	Latest_Workflow_UUID string                   `json:"latest_workflow_uuid"`
	Model_Class          string                   `json:"model_class"`
	Name                 string                   `json:"name"`
	Owner                string                   `json:"owner"`
	Published            bool                     `json:"published"`
	Steps                map[string]WorkflowStep  `json:"steps"`
	Tags                 []string                 `json:"tags"`
	Url                  string                   `json:"url"`
	Traceboack           string                   `json:"traceback"` // Set only if the server returns an error
	Err_Msg              string                   `json:"err_msg"`   // Err_Msg =="" if no error
	Err_Code             int                      `json:"err_code"`  // Err_Code==0 if no error
}

Informations about a workflow

type WorkflowInput

type WorkflowInput struct {
	Label string `json:label`
	Uuid  string `json:uuid`
	Value string `json:uuid`
}

type WorkflowInputStep

type WorkflowInputStep struct {
	Source_Step int    `json:"source_step"` // ID of the previous step serving as input here
	Step_Output string `json:"step_output"` // Name of the output  of the previous step serving as input here
}

type WorkflowInvocation

type WorkflowInvocation struct {
	History     string                   `json:"history"`
	History_Id  string                   `json:"history_id"`
	Id          string                   `json:"id"`
	Inputs      map[string]toolInput     `json:"inputs"`
	Model_Class string                   `json:"model_class"`
	Outputs     []string                 `json:"outputs"`
	State       string                   `json:"state"`
	Steps       []WorkflowInvocationStep `json:"steps"`
	Update_Time string                   `json:"update_time"`
	Uuid        string                   `json:"uuid"`
	Workflow_Id string                   `json:"workflow_id"`
	Traceboack  string                   `json:"traceback"` // Set only if the server returns an error
	Err_Msg     string                   `json:"err_msg"`   // Err_Msg =="" if no error
	Err_Code    int                      `json:"err_code"`  // Err_Code=="" if no error
}

When a workflow is launched, it is returned by the server

type WorkflowInvocationStep

type WorkflowInvocationStep struct {
	Action              string `json:"action"`
	Id                  string `json:"id"`
	Job_Id              string `json:"job_id"`
	Model_Class         string `json:"model_class"`
	Order_Index         int    `json:"order_index"`
	State               string `json:"state"`
	Update_Time         string `json:"update_time"`
	Workflow_Step_Id    string `json:"workflow_step_id"`
	Workflow_Step_Label string `json:"workflow_step_label"`
	Workflow_Step_Uuid  string `json:"workflow_step_uuid"`
}

One of the steps given after invocation of the workflow

type WorkflowLaunch

type WorkflowLaunch struct {
	History_id  string                    `json:"history_id"`  // Id of history
	Workflow_id string                    `json:"workflow_id"` // Id of the tool
	Inputs      map[string]toolInput      `json:"inputs"`      // Inputs: key name of the input, value dataset id
	Parameters  map[int]map[string]string `json:"parameters"`  // Parameters to the workflow : key: Step id, value: map of key:value parameters
}

func (*WorkflowLaunch) AddFileInput

func (wl *WorkflowLaunch) AddFileInput(inputIndex string, fileId string, fileSrc string)

Add new input file to the workflow

  • inputIndex : index of this input in the workflow (see WorkflowInfo / GetWorkflowById)
  • fielId: id of input file
  • fielScr : one of ["ldda", "ld", "hda", "hdca"]

func (*WorkflowLaunch) AddParameter

func (wl *WorkflowLaunch) AddParameter(stepIndex int, paramName string, paramValue string)

Add new parameter to workflow launcher

  • fielId: id of input file
  • fielScr : one of ["ldda", "ld", "hda", "hdca"]

type WorkflowStatus

type WorkflowStatus struct {
	// contains filtered or unexported fields
}

Information about status of a workflow run

  • General status
  • Status of each step
  • Output file ids of each steps

func (*WorkflowStatus) ListStepRanks

func (ws *WorkflowStatus) ListStepRanks() (stepRanks []int)

Gets the list of all the step ranks/numbers

func (*WorkflowStatus) Status

func (ws *WorkflowStatus) Status() string

Returns the global status of the workflow. It is computed as follows:

  • If all steps are "ok": then == "ok"
  • Else if one step is "deleted": then == "deleted"
  • Else if one step is "running": then == "running"
  • Else if one step is "queued": then == "queued"
  • Else if one step is "waiting": then == "waiting"
  • Else if one is is "new": then == "new"
  • Else : Unknown state

func (*WorkflowStatus) StepOutFileNames

func (ws *WorkflowStatus) StepOutFileNames(stepRank int) (fileNames []string, err error)

Gets the output file names of the given step number and having the given name

If the step does not exist, returns an error.

func (*WorkflowStatus) StepOutputFileId

func (ws *WorkflowStatus) StepOutputFileId(stepRank int, filename string) (fileId string, err error)

Gets the output file id of the given step number and having the given name

If the step does not exist or the file with the given name does not exist, returns an error.

func (*WorkflowStatus) StepStatus

func (ws *WorkflowStatus) StepStatus(stepRank int) (status string, err error)

Gets the status of the given step number

If the step number does not exist, then returns an error

type WorkflowStep

type WorkflowStep struct {
	Annotation   string                       `json:"annotation"`
	Id           int                          `json:"id"`
	Input_Steps  map[string]WorkflowInputStep `json:"input_steps"`
	Tool_Id      string                       `json:"tool_id"`
	Tool_Inputs  map[string]string            `json:"tool_inputs"`
	Tool_Version string                       `json:"tool_version"`
	Type         string                       `json:"type"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL