-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.cljc
269 lines (222 loc) · 9.39 KB
/
core.cljc
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
(ns clj-hgvs.core
"Main functions for handling HGVS. See http://varnomen.hgvs.org/ for the
detail HGVS nomenclature."
(:refer-clojure :exclude [== #?(:clj format)])
(:require #?(:clj [clojure.pprint :as pp])
[clojure.spec.alpha :as s]
[clojure.string :as string]
[clj-hgvs.internal :as intl]
[clj-hgvs.mutation :as mut]
[clj-hgvs.repairer :as repairer]))
(defmacro with-validation-disabled
"Disables validation within a scope.
HGVS data are checked upon interpretation phases by default, but in the scope,
all validations will be skipped. This macro may improve the performance on
handling HGVS which validity is already known."
[& body]
`(binding [intl/*validation-enabled* nil]
~@body))
(defrecord HGVS [transcript kind mutation])
(s/def ::transcript
(s/and string? (s/or :N*_ #(re-matches #"N(C|G|M|R|P)_\d+(\.\d+)?" %)
:LRG_ #(re-matches #"LRG_\d+((t|p)\d+)?" %)
:J* #(re-matches #"J\d+(\.\d+)?" %))))
(s/def ::kind #{:genome
:mitochondria
:coding-dna
:non-coding-dna
:circular-dna
:rna
:protein})
(s/def :clj-hgvs.hgvs/transcript (s/nilable ::transcript))
(s/def ::hgvs (s/keys :req-un [:clj-hgvs.hgvs/transcript
::kind
::mut/mutation]))
(defn hgvs
"Creates HGVS data represented as a record.
transcript is nilable for conventional reasons, but you should supply it if
possible.
kind must be one of :genome, :mitochondria, :coding-dna, :non-coding-dna,
:circular-dna, :rna, and :protein.
mutation must be a clj-hgvs.mutation record or string. The string mutation
will be parsed by clj-hgvs.mutation/parse."
[transcript kind mutation]
{:post [(intl/valid? ::hgvs %)]}
(map->HGVS {:transcript transcript
:kind kind
:mutation (if (string? mutation)
(mut/parse mutation kind)
mutation)}))
(s/fdef hgvs
:args (s/cat :transcript (s/nilable ::transcript)
:kind ::kind
:mutation (s/or :s string?
:m ::mut/mutation))
:ret ::hgvs)
(def ^:private hgvs-re #"^(?:([^:]+):)?([gmcnorp])\.(.+)$")
(defn parse
"Parses a HGVS string s, returning a map representing the HGVS."
[s]
(let [[_ transcript kind mutation] (re-find hgvs-re s)
kind-k (intl/->kind-keyword kind)]
(hgvs transcript kind-k mutation)))
(s/fdef parse
:args (s/cat :s string?)
:ret ::hgvs)
(defn- format-transcript
[transcript]
(if transcript
(str transcript ":")))
(defn- format-kind
[kind]
(str (intl/->kind-str kind) "."))
(defn format
"Returns a HGVS string representing the given HGVS map.
The second argument is an optional map to specify style:
{:show-bases? Display additional bases, e.g. g.6_8delTGC, default
false.
:ins-format Bases style of insertion, default :auto.
<:auto|:bases|:count>
:range-format Range style, default :auto. <:auto|:bases|:coord>
:amino-acid-format Amino acid style of protein HGVS, default :long.
<:long|:short>
:show-ter-site? Display a new ter codon site of protein frame shift,
default false.
:ter-format Ter codon style of protein frame shift and extension,
default :long. <:long|:short>}"
([hgvs] (format hgvs {}))
([hgvs opts]
(apply str [(format-transcript (:transcript hgvs))
(format-kind (:kind hgvs))
(mut/format (:mutation hgvs) opts)])))
(s/def :clj-hgvs.spec.format-options/show-bases? #(or (true? %) (false? %)))
(s/def :clj-hgvs.spec.format-options/ins-format #{:auto :bases :count})
(s/def :clj-hgvs.spec.format-options/range-format #{:auto :bases :coord})
(s/def :clj-hgvs.spec.format-options/amino-acid-format #{:long :short})
(s/def :clj-hgvs.spec.format-options/show-ter-site? #(or (true? %) (false? %)))
(s/def :clj-hgvs.spec.format-options/ter-format #{:long :short})
(s/def ::format-options
(s/keys :opt-un [:clj-hgvs.spec.format-options/show-bases?
:clj-hgvs.spec.format-options/ins-format
:clj-hgvs.spec.format-options/range-format
:clj-hgvs.spec.format-options/amino-acid-format
:clj-hgvs.spec.format-options/show-ter-site?
:clj-hgvs.spec.format-options/ter-format]))
(s/fdef format
:args (s/cat :hgvs ::hgvs
:opts (s/? (s/nilable ::format-options)))
:ret string?)
(defn- transcript-equiv
[s1 s2]
(letfn [(trimver [s]
(when s
(string/replace s #"([^.pt]+)[.pt]\d+" "$1")))]
(= (trimver s1) (trimver s2))))
(defn ==
"Returns true if hgvs1 is equivalent to hgvs2, false otherwise.
This function compares the fundamental equivalence of the given HGVS, ignoring
the difference of the transcript version, the short/long amino acid style, and
the description of the same mutation."
([hgvs] true)
([hgvs1 hgvs2]
(and (transcript-equiv (:transcript hgvs1) (:transcript hgvs2))
(= (:kind hgvs1) (:kind hgvs2))
(mut/equiv (:mutation hgvs1) (:mutation hgvs2))))
([hgvs1 hgvs2 & more]
(if (== hgvs1 hgvs2)
(if (next more)
(recur hgvs2 (first more) (next more))
(== hgvs2 (first more)))
false)))
(defn plain
"Returns a plain map representing the given HGVS. This function is useful for
sending data through another codec. Use clj-hgvs.core/restore to retrieve
original HGVS data."
[hgvs]
{:transcript (:transcript hgvs)
:kind (name (:kind hgvs))
:mutation (mut/plain (:mutation hgvs))})
(s/def :clj-hgvs.plain-hgvs/transcript (s/nilable ::transcript))
(s/def :clj-hgvs.plain-hgvs/kind #{"genome"
"mitochondria"
"coding-dna"
"non-coding-dna"
"circular-dna"
"rna"
"protein"})
(s/def :clj-hgvs.plain-hgvs/mutation ::mut/plain-mutation)
(s/def ::plain-hgvs (s/keys :req-un [:clj-hgvs.plain-hgvs/transcript
:clj-hgvs.plain-hgvs/kind
:clj-hgvs.plain-hgvs/mutation]))
(s/fdef plain
:args (s/cat :hgvs ::hgvs)
:ret ::plain-hgvs)
(defn restore
"Restores a plain map to a suitable HGVS data structure. This function is
useful for sending data through another codec."
[m]
(-> m
(update :kind keyword)
(update :mutation mut/restore)
map->HGVS))
(s/fdef restore
:args (s/cat :m ::plain-hgvs)
:ret ::hgvs)
(defn normalize
"Reformats the HGVS string s, returning the normalized HGVS string.
The default style will be used for reformatting. See clj-hgvs.core/format
document for further details of the style options."
[s]
(format (parse s)))
(s/fdef clj-hgvs.core/normalize
:args (s/cat :s string?)
:ret string?)
(defn repair-hgvs-str
"Attempts to repair an invalid HGVS string, returning a correct HGVS string.
The repair rules are based on frequent mistakes in popular public-domain
databases such as dbSNP and ClinVar. See clj-hgvs.repairer/built-in-repairers
for details of the built-in repair rules.
You may supply custom repair rules to the second argument:
(defn lower-case-ext
[s kind]
(if (= kind :protein)
(clojure.string/replace s #\"EXT\" \"ext\")
s))
(def my-repairers (conj clj-hgvs.repairer/built-in-repairers
lower-case-ext))
(repair-hgvs-str \"p.*833EXT*?\" my-repairers)
=> \"p.*833ext*?\""
([s]
(repair-hgvs-str s repairer/built-in-repairers))
([s repairers]
(reduce #(%2 %1 (repairer/infer-kind %1)) s repairers)))
(s/fdef repair-hgvs-str
:args (s/cat :s string?
:repairers (s/?
(s/coll-of
#?(:clj (s/fspec :args (s/cat :s string?
:kind ::kind)
:ret string?)
;; NOTE: The above fspec is better than fn?, but
;; it causes an error on clojurescript
;; 1.10.520.
:cljs fn?)
:kind vector?)))
:ret string?)
#?(:clj (defmethod print-method HGVS [hgvs ^java.io.Writer w]
(.write w (str "#clj-hgvs/hgvs \""
(format hgvs {:show-bases? true
:amino-acid-format :short
:show-ter-site? true
:ter-format :short})
"\""))))
#?(:clj (defmethod pp/simple-dispatch HGVS [hgvs]
(.write *out* (str "#clj-hgvs/hgvs \""
(format hgvs {:show-bases? true
:amino-acid-format :short
:show-ter-site? true
:ter-format :short})
"\""))))
#?(:clj (def data-readers
"Tagged literal support if loader does not find \"data_readers.clj\"."
{'clj-hgvs/hgvs parse}))