Skip to content

Commit

Permalink
Galaxy client connection plus query and download of files from history
Browse files Browse the repository at this point in the history
  • Loading branch information
chapmanb committed Sep 27, 2012
0 parents commit 0fad262
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/target
/lib
/classes
/checkouts
pom.xml
*.jar
*.class
.lein-deps-sum
.lein-failures
.lein-plugins
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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 useful functionality utilizing
the Galaxy API.

[0]: https://github.com/jmchilton/blend4j
[1]: https://github.com/afgane/blend

## Usage

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

$ lein repl
> (require '[blend.galaxy.core :as blend])
> (def c (blend/get-client "https://main.g2.bx.psu.edu/" "your-api-key"))
> (def ds (blend/get-datasets-by-type c :bed))
> (blend/download-dataset c (first ds) "/where/to/put/your/file.bed")


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

## License

The code is freely available under the [MIT license][l1].

[l1]: http://www.opensource.org/licenses/mit-license.html
9 changes: 9 additions & 0 deletions project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(defproject clj-blend "0.1.0-SNAPSHOT"
:description "Clojure library for interacting with Galaxy, CloudMan, and BioCloudCentral, built on blend4j"
: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-SNAPSHOT"]]
:repositories {"msi-artifactory" {:url "http://artifactory.msi.umn.edu/libs-snapshot-local"}})
11 changes: 11 additions & 0 deletions src/blend/galaxy/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(ns blend.galaxy.core
"Top level Galaxy interaction"
(:import [com.github.jmchilton.blend4j.galaxy GalaxyInstanceFactory])
(:require [blend.galaxy.histories :as histories]))

(defn get-client
[galaxy-url api-key]
(GalaxyInstanceFactory/get galaxy-url api-key))

(def get-datasets-by-type histories/get-datasets-by-type)
(def download-dataset histories/download-dataset)
56 changes: 56 additions & 0 deletions src/blend/galaxy/histories.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
(ns blend.galaxy.histories
"Retrieve Galaxy history information."
(:use [clojure.java.io])
(:require [clojure.string :as string]
[fs.core :as fs]))

(defn- get-history-dataset
"Retrieve a history dataset converted into a clojure map."
[hist-client hist hist-contents]
(let [ds (.showDataset hist-client (.getId hist) (.getId hist-contents))]
{:id (.getId ds)
:name (.getName ds)
:data-type (.getDataType ds)
:genome-build (.getGenomeBuild ds)
:file-size (.getFileSize ds)
:deleted (.getDeleted ds)
:visible (.getVisible ds)
:state (.getState ds)
:download-url (.getDownloadUrl ds)}))

(defn- get-history-datasets
"Retrieve history datasets, flattened into Clojure maps."
[hist-client hist]
(->> (.showHistoryContentsRequest hist-client (.getId hist))
(filter #(= (.getType %) "file"))
(map (partial get-history-dataset hist-client hist))))

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

(defn get-datasets-by-type
"Retrieve datasets from the current active history by filetype."
[client ftype]
(let [hist-client (.getHistoriesClient client)]
(->> (.getHistories hist-client)
first
(get-history-datasets hist-client)
(filter (partial is-ftype? ftype)))))

(defn download-dataset
"Retrieve remote history dataset to a local file or directory"
[client dataset path]
(let [api-url (-> client .getWebResource .getURI)
query (.getQuery api-url)
base-url (first (string/split (.toString api-url) #"/api"))
fname (if (fs/directory? path)
(str (file path (:name dataset)))
path)]
(when (or (not (fs/exists? fname))
(not= (fs/size fname) (:file-size dataset)))
(with-open [rdr (reader (str base-url (:download-url dataset)))]
(copy rdr (file fname))))
fname))

0 comments on commit 0fad262

Please sign in to comment.