about summary refs log tree commit diff
path: root/gn/data/dataset.scm
blob: afe75babf978bb901191dffeda6f2ddf92b7331b (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
74
75
76
77
78
79
80
81
82
83
84
85
(define-module (gn data dataset)
  #:use-module (ice-9 match)
  #:use-module (ice-9 format)
  #:use-module (ice-9 iconv)
  #:use-module (ice-9 receive)
  #:use-module (ice-9 string-fun)
  #:use-module (srfi srfi-1)
  #:use-module (dbi dbi)
  #:use-module (gn db mysql)
  #:use-module (gn data genotype)
  #:use-module (gn data group)
  #:use-module (gn util convert)
  #:use-module (web gn-uri)
  #:use-module (rnrs base) ; for assert

  #:export (
            dataset-name
            get-bxd-publish-list
            get-bxd-publish-values-list
            get-bxd-publish-name-value-dict
            get-bxd-publish-dataid-name-value-dict
            ))

(define (get-dataset db probesetfreeze-id)
  (dbi-query db
             (string-append
              "select Name,Name2,FullName from ProbeSetFreeze where Id=" (int-to-string probesetfreeze-id) " limit 1;"))
  (get-row db))

(define (dataset-name db probesetfreeze-id)
  (assoc-ref (get-dataset db probesetfreeze-id) "Name"))

(define (get-dataid-from-publishxrefid id)
  "Get the internal dataid from publishxref - which is the same as used in the GN2 web interface"
  (call-with-db
   (lambda (db)
     (let [(query (string-append "SELECT Id,PhenotypeId,DataId FROM PublishXRef WHERE Id=" id " AND InbredSetId=1 LIMIT 1"))]
       (dbi-query db query)
       (pk (int-to-string (assoc-ref (get-row db) "DataId")))))))

(define (get-bxd-publish-list)
  (call-with-db
   (lambda (db)
     (let [(query "SELECT Id,PhenotypeId,DataId FROM PublishXRef WHERE InbredSetId=1")]
       (dbi-query db query)
       (get-rows db '())))))

(define* (get-bxd-publish-values-list dataid #:optional used-for-mapping?)
  "Returns dict of name values , e.g. [{\"Name\":\"C57BL/6J\",\"value\":9.136},{\"Name\":\"DBA/2J\",\"value\":4.401},{\"Name\":\"BXD9\",\"value\":4.36}, ... used-for-mapping? skips the founders and maybe other unmappable inds. Note, currently unused."
  (call-with-db
   (lambda (db)
     (let [(query (string-append "SELECT Strain.Name, PublishData.value FROM Strain, PublishData WHERE PublishData.Id=" dataid " and Strain.Id=StrainID;"))]
       (dbi-query db query)
       (if used-for-mapping?
           (remove null? (pk (get-rows-apply db
                                             (lambda (r)
                                               (if (string-contains (assoc-ref r "Name") "BXD")
                                                   `(("Name" . ,(assoc-ref r "Name")) ("value" . ,(assoc-ref r "value")))
                                                   '() ) ;; return empty on no match
                                               ) '())))
           (get-rows db '())
           )))))

(define* (get-bxd-publish-dataid-name-value-dict dataid #:optional used-for-mapping?)
  "Returns dict of name values, e.g. (((\"C57BL/6J\" . 9.136) (\"DBA/2J\" . 4.401) (\"BXD9\" . 4.36) ... used-for-mapping? skips the founders and maybe other unmappable inds."
  (call-with-db
   (lambda (db)
     (let [(query (string-append "SELECT Strain.Name, PublishData.value FROM Strain, PublishData WHERE PublishData.Id=" dataid " and Strain.Id=StrainID;"))]
       (dbi-query db query)
       (if used-for-mapping?
           (remove null? (pk (get-rows-apply db
                                             (lambda (r)
                                               (if (string-contains (assoc-ref r "Name") "BXD")
                                                   `(,(assoc-ref r "Name") . ,(assoc-ref r "value"))
                                                   '() ) ;; return empty on no match
                                               ) '())))
           (remove null? (pk (get-rows-apply db
                                             (lambda (r)
                                               `(,(assoc-ref r "Name") . ,(assoc-ref r "value"))
                                               ) '())))
           )))))

(define* (get-bxd-publish-name-value-dict id #:optional used-for-mapping?)
  "Same as above function, but starting from data id"
  (get-bxd-publish-dataid-name-value-dict (get-dataid-from-publishxrefid id) used-for-mapping?))