Skip to content

Commit

Permalink
Make direct HTTP requests using clj-http instead of using blend4j (#2)
Browse files Browse the repository at this point in the history
* Replace blend4j calls with direct API calls with http-kit

* Use clj-http instead of http-kit

* Update

* Changed namespace to org.galaxyproject.clj-blend

* Changed namespace to org.galaxyproject.clj-blend

* Fix namespaces

* Organizing namespaces

* Add more users methods

* Reorganize histories

* update users

* Update user namespace

* Work on downloading history contents

* Added simple test for users namespace

* Remove core ns test and append /api/ to GALAXY_URL when constructing client

* Remove unused reloaded.repl functions

* Remove references to blend4j from README
  • Loading branch information
dfornika authored Nov 24, 2019
1 parent 57c3162 commit e055ac3
Show file tree
Hide file tree
Showing 16 changed files with 365 additions and 181 deletions.
16 changes: 4 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
# clj-blend

Clojure library for interacting with Galaxy, CloudMan, and BioCloudCentral. This
builds on [blend4j][0] with lots of help from [blend][1]. Provides high level
functionality on top of blend4j, with a focus on smooth interaction with Galaxy.

[0]: https://github.com/jmchilton/blend4j
[1]: https://github.com/afgane/blend
Clojure library for interacting with the Galaxy bioinformatics workflow platform.

## Usage

Requires Java 1.6 or better and [Leiningen 2.x][u1].
Requires Java 8 or better and [Leiningen 2.x][u1].

$ lein repl
> (require '[blend.galaxy.core :as galaxy])
> (require '[org.galaxyproject.clj-blend.galaxy.core :as galaxy])
> (def c (galaxy/get-client "https://main.g2.bx.psu.edu/" "your-api-key"))
> (def ds (galaxy/get-datasets-by-type c :bed))
> (galaxy/download-dataset c (first ds) "/where/to/put/your/file.bed")
> (galaxy/upload-to-history c "http://www.yoursite.com/data" :hg19 :vcf)

> (def user (galaxy/get-user-info c))

[u1]: https://github.com/technomancy/leiningen

Expand Down
13 changes: 8 additions & 5 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
(defproject clj-blend "0.1.1-SNAPSHOT"
:description "Clojure library for interacting with Galaxy, CloudMan, and BioCloudCentral, built on blend4j"
(defproject clj-blend "0.2.0-SNAPSHOT"
:description "Clojure library for interacting with the Galaxy Bioinformatics Workflow Platform"
:url "http://github.com/chapmanb/clj-blend"
:license {:name "MIT"
:url "http://www.opensource.org/licenses/mit-license.html"}
:dependencies [[org.clojure/clojure "1.4.0"]
[fs "1.3.2"]
[com.github.jmchilton.blend4j/blend4j "0.1-alpha-1"]])
:dependencies [[org.clojure/clojure "1.10.0"]
[clj-http "3.9.1"]
[org.clojure/data.json "0.2.6"]
[org.clojure/data.codec "0.1.1"]
[fs "1.3.2"]]
:profiles {:dev {:dependencies [[reloaded.repl "0.2.4"]]}})
23 changes: 0 additions & 23 deletions src/blend/galaxy/core.clj

This file was deleted.

82 changes: 0 additions & 82 deletions src/blend/galaxy/histories.clj

This file was deleted.

30 changes: 0 additions & 30 deletions src/blend/galaxy/tools.clj

This file was deleted.

13 changes: 0 additions & 13 deletions src/blend/galaxy/users.clj

This file was deleted.

13 changes: 13 additions & 0 deletions src/org/galaxyproject/clj_blend/auth.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(ns org.galaxyproject.clj-blend.auth
"Galaxy Authentication."
(:require [clj-http.client :as client]
[clojure.data.json :as json]
[org.galaxyproject.clj-blend.util :as util]))

(defn authenticate
[server user]
(let [b64 (util/b64-encode-str (str (:email user) ":" (:password user)))]
(json/read-str
(:body (client/get (str (:api-root server) "authenticate/baseauth")
{:headers {"Authorization" (str "Basic " b64)}}))
:key-fn util/key-fn)))
33 changes: 33 additions & 0 deletions src/org/galaxyproject/clj_blend/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns org.galaxyproject.clj-blend.core
"Top level Galaxy interaction"
(:require [clj-http.client :as client]
[org.galaxyproject.clj-blend.users :as users]
[org.galaxyproject.clj-blend.histories :as histories]
[org.galaxyproject.clj-blend.tools :as tools]))


(defn authenticate
[server user]
(client/get (str (:api-root server) "authenticate/baseauth")
:headers {"Authorization" (str "Basic " )}))


(defn get-client
[galaxy-url api-key]
{:url (str galaxy-url "/api/") :api-key api-key})

(def get-user-info users/get-current-user)

(defn list-histories
[client]
[(histories/get-history-most-recently-used client)])

(def get-history-contents-by-type histories/get-history-contents-by-type)
(def get-history-contents-by-id histories/get-history-contents-by-id)

(def download-history-contents histories/download-history-contents)
(def upload-to-history tools/upload-to-history)

(comment
(def client (get-client "http://localhost:8080" "admin"))
)
97 changes: 97 additions & 0 deletions src/org/galaxyproject/clj_blend/histories.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
(ns org.galaxyproject.clj-blend.histories
"Retrieve Galaxy history information."
(:use [clojure.java.io])
(:require [clojure.string :as string]
[clj-http.client :as client]
[clojure.data.json :as json]
[org.galaxyproject.clj-blend.util :as util]
[fs.core :as fs]))

(defn get-histories
[client]
(json/read-str
(:body (client/get (str (client :url) "histories")
{:headers {"x-api-key" (client :api-key)}}))
:key-fn util/key-fn))

(defn get-history-most-recently-used
[client]
(json/read-str
(:body (client/get (str (client :url) "histories/most_recently_used")
{:headers {"x-api-key" (client :api-key)}}))
:key-fn util/key-fn))

(defn get-history-by-id
[client history-id]
(json/read-str
(:body (client/get (str (client :url) "histories/" history-id)
{:headers {"x-api-key" (client :api-key)}}))
:key-fn util/key-fn))

(defn create-history
[client history]
(json/read-str
(:body (client/post (str (client :url) "histories")
{:headers {"x-api-key" (client :api-key)}
:content-type :json
:body (json/write-str history)}))
:key-fn util/key-fn))

(defn delete-history
[client history-id]
(json/read-str
(:body (client/delete (str (client :url) "histories/" history-id)
{:headers {"x-api-key" (client :api-key)}}))
:key-fn util/key-fn))

(defn get-history-contents
"Retrieve history datasets, flattened into Clojure maps."
[client history-id]
(json/read-str
(:body (client/get (str (client :url) "histories/" history-id "/contents")
{:headers {"x-api-key" (client :api-key)}}))
:key-fn util/key-fn))

(defn get-history-contents-by-id
"Retrieve a history dataset converted into a clojure map."
[client history-id content-id]
(json/read-str
(:body (client/get (str (client :url) "histories/" history-id "/contents/" content-id)
{:headers {"x-api-key" (client :api-key)}}))
:key-fn util/key-fn))

(defn get-history-status
[client history-id]
(select-keys (get-history-by-id client history-id) [:state :state-details]))

(defn- is-ftype?
"Check if a dataset is the given filetype, cleanly handling keywords"
[ftype dataset]
(= (keyword ftype)
(keyword (:data-type dataset))))

(defn get-history-contents
[client history-id]
(json/read-str
(:body (client/get (str (client :url) "histories/" history-id "/contents")
{:headers {"x-api-key" (client :api-key)}}))
:key-fn util/key-fn))

(defn get-history-contents-by-id
[client history-id content-id]
(json/read-str
(:body (client/get (str (client :url) "histories/" history-id "/contents/" content-id)
{:headers {"x-api-key" (client :api-key)}}))
:key-fn util/key-fn))

(defn get-history-contents-by-type
"Retrieve datasets from the current active history by filetype."
[client history-id ftype]
(filter (partial is-ftype? ftype)
(get-history-contents client history-id)))

(defn download-history-contents
[client history-id content-id download-path]
(spit download-path
(:body (client/get (str (client :url) "histories/" history-id "/contents/" content-id "/display")
{:headers {"x-api-key" (client :api-key)}}))))
32 changes: 32 additions & 0 deletions src/org/galaxyproject/clj_blend/tools.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(ns org.galaxyproject.clj-blend.tools
"Run Galaxy tools through the remote API"
(:require [clj-http.client :as client]
[clojure.data.json :as json]
[clojure.string :as string]
[org.galaxyproject.clj-blend.histories :as histories]
[org.galaxyproject.clj-blend.util :as util]))

(defn list-tools
[client]
(json/read-str
(:body (client/get (str (client :url) "tools")
{:headers {"x-api-key" (client :api-key)}}))
:key-fn util/key-fn))

(defn run-tool
"Run a remote tool on Galaxy server"
[client tool-id params & {:keys [history-id]}]
(json/read-str
(:body (client/post (str (client :url) "tools")
{:headers {"x-api-key" (client :api-key)}}))
:key-fn util/key-fn))

(defn upload-to-history
"Upload a file via URL to a Galaxy history, defaulting to the current."
[client file-url dbkey ftype & {:keys [history-id display-name]}]
(run-tool client "upload1"
{:file-type (name ftype)
:dbkey (name dbkey)
"files_0|url_paste" file-url
"files_0|NAME" (or display-name (last (string/split file-url #"/")))}
:history-id history-id))
Loading

0 comments on commit e055ac3

Please sign in to comment.