aboutsummaryrefslogtreecommitdiff
path: root/json-dump.scm
blob: ccb64bc6e12f4122931d16a7121ecf04f3cb9145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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)