aboutsummaryrefslogtreecommitdiff
path: root/csv-dump.scm
blob: a2a91270a5c9a2b7ff027f3bdc4e6e26f40032c6 (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
(use-modules (srfi srfi-1)
             (ice-9 match)
             (ice-9 rdelim)
             (dsv))

(define (assoc-set alist key value)
  "Associate KEY with VALUE in ALIST, and return the new association
list. This is a functional setter and does not mutate ALIST. KEY is
compared with keys in ALIST using equal?."
  (acons key value
         (alist-delete key alist)))

(define (parse-aggregate-table table)
  "Convert TABLE from the provided aggregate file to an s-exp equivalent.
The GROUP forms the KEY of each alist."
  (match table
    ((header table-body ...)
     (fold (lambda (row results)
             (let ((alist (map cons header row)))
               (acons (string->symbol (assoc-ref alist "group"))
                      (filter-map (lambda (key value)
                                    (and (member key
                                                 (list "mean" "n" "sex " "SD" "day"))
                                         (cons (string->symbol (string-downcase key))
                                               value)))
                                  header row)
                      results)))
           '()
           table-body))))

(define (parse-raw-table table aggregate-table)
  "Given TABLE which represents the data from the raw file, and the
AGGREGATE-TABLE which is an ALIST containing aggregate data, add extra
metadata not present in the aggregate table and return this result."
  (match table
    ((header table-body ...)
     (fold (lambda (row results)
             (let* ((alist (map cons header row))
                    (key (string->symbol (string-join
                                          (list (assoc-ref alist "strain")
                                                (assoc-ref alist "sex")
                                                (assoc-ref alist "day"))
                                          "_")))
                    (aggregate-data (assq-ref aggregate-table key))
                    (folded-data (assq-ref results key)))
               ;; Return an updated version of the aggregate-table as
               ;; you loop through the raw table.  Do this while
               ;; collecting the values for 'bw and setting 'inf-dose
               (assoc-set results
                          key (assoc-set
                               ;; inf_dose is repetitive in raw table
                               ;; and it's common in the same vector
                               ;; of data.  If it isn't set, set it
                               ;; once.
                               (if (assq-ref folded-data 'aggregate-dose)
                                   aggregate-data
                                   (acons 'inf-dose (assoc-ref alist "inf_dose") aggregate-data))
                               'bw
                               ;; When looping in the raw table,
                               ;; collect all the 'bw values.
                               (if (assq-ref folded-data 'bw)
                                   (cons (assoc-ref alist "BW")
                                         (assq-ref folded-data 'bw))
                                   (list (assoc-ref alist "BW")))))))
           '()
           table-body))))