aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMunyoki Kilyungi2023-02-08 12:13:46 +0300
committerBonfaceKilz2023-02-15 11:37:15 +0300
commitc79ac02bb27ed3036ddb4928bf723a59852ee24d (patch)
treede2bb2d103136414c36c992a6419064a9ca84790
parentdd647c753e150c22b12dff3c58fd65f1ccf6804d (diff)
downloadgn-transform-databases-c79ac02bb27ed3036ddb4928bf723a59852ee24d.tar.gz
Read metadata from a json file
* json-dump.scm: New file. Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
-rwxr-xr-xjson-dump.scm73
1 files changed, 73 insertions, 0 deletions
diff --git a/json-dump.scm b/json-dump.scm
new file mode 100755
index 0000000..ccb64bc
--- /dev/null
+++ b/json-dump.scm
@@ -0,0 +1,73 @@
+#! /usr/bin/env guile
+!#
+
+(use-modules (json)
+ (ice-9 ftw)
+ (ice-9 match)
+ (dump triples))
+
+
+
+(define %dump-directory
+ (list-ref (command-line) 2))
+
+(define %data-directory
+ (list-ref (command-line) 1))
+
+
+
+(define (json-metadata->rdf path)
+ "Given a PATH that contains a json file, fetch the metadata embedded
+inside it."
+ (if (access? path F_OK)
+ (let* ((data (assoc-ref (call-with-input-file
+ path
+ (lambda (port)
+ (json->scm port)))
+ "metadata"))
+ (name (or (assoc-ref data "name")
+ (assoc-ref data "displayName"))))
+ (match data
+ (((key . value) ...)
+ (map
+ (lambda (a b)
+ (format
+ #f "gn:sampledata_~a gn:sampledata:~A ~a ."
+ name a (format #f "~s"
+ (cond ((boolean? b)
+ (if b "True" "False"))
+ (else b)))))
+ key value))))))
+
+(define (run-proc-on-files path proc)
+ (define (enter? name stat result)
+ (not (member (basename name) '(".git" ".svn" "CVS"))))
+ (define (leaf name stat result)
+ (proc name))
+ (define (down name stat result) result)
+ (define (up name stat result) result)
+ (define (skip name stat result) result)
+
+ ;; Ignore unreadable files/directories but warn the user.
+ (define (error name stat errno result)
+ (format (current-error-port) "warning: ~a: ~a~%"
+ name (strerror errno))
+ result)
+ (file-system-fold enter? leaf down up skip error 0 path))
+
+(define (dump-rdf path)
+ (with-output-to-file
+ (string-append %dump-directory "/sampledata.ttl")
+ (lambda ()
+ (prefix "gn:" "<http://genenetwork.org/>")
+ (newline)
+ (run-proc-on-files
+ %data-directory
+ (lambda (file)
+ (when (string-suffix? "json" file)
+ (map (lambda (line)
+ (display line)
+ (newline))
+ (json-metadata->rdf file))))))))
+
+(dump-rdf %data-directory)