-
Notifications
You must be signed in to change notification settings - Fork 2
/
coding_dna.clj
306 lines (290 loc) · 12.9 KB
/
coding_dna.clj
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
(ns varity.vcf-to-hgvs.coding-dna
(:require [clojure.pprint :as pp]
[clojure.string :as string]
[clj-hgvs.coordinate :as coord]
[clj-hgvs.core :as hgvs]
[clj-hgvs.mutation :as mut]
[cljam.util.sequence :as util-seq]
[cljam.io.sequence :as cseq]
[proton.string :as pstring]
[varity.ref-gene :as rg]
[varity.vcf-to-hgvs.common :refer [diff-bases] :as common]))
(defn- repeat-info-forward
[seq-rdr rg pos alt type]
(->> (common/read-sequence-stepwise-backward
seq-rdr
{:chr (:chr rg)
:start (- (:tx-start rg) rg/max-tx-margin)
:end (dec (cond-> pos
(= type :del) (+ (count alt))))}
100)
(map (fn [seq*]
(let [nseq* (count seq*)
pos* (case type
:ins (inc nseq*)
:del (inc (- nseq* (count alt))))]
(if-let [[unit ref-repeat :as ri] (common/repeat-info
seq* pos* alt type)]
(let [nunit (count unit)]
(if (> (* nunit ref-repeat) (- nseq* nunit))
false
ri))))))
(remove false?)
(first)))
(defn- repeat-info-backward
[seq-rdr rg pos alt type]
(->> (common/read-sequence-stepwise
seq-rdr
{:chr (:chr rg)
:start pos
:end (+ (:tx-end rg) rg/max-tx-margin)}
100)
(map (fn [seq*]
(let [nseq* (count seq*)
pos* (case type
:ins (inc nseq*)
:del (inc (- nseq* (count alt))))]
(if-let [[unit ref-repeat :as ri] (common/repeat-info
(util-seq/revcomp seq*)
pos*
(util-seq/revcomp alt)
type)]
(let [nunit (count unit)]
(if (> (* nunit ref-repeat) (- nseq* nunit))
false
ri))))))
(remove false?)
(first)))
(defn- repeat-info*
[seq-rdr rg pos ref-only alt-only]
(when-let [[alt type] (cond
(and (empty? ref-only) (seq alt-only)) [alt-only :ins]
(and (seq ref-only) (empty? alt-only)) [ref-only :del])]
(case (:strand rg)
:forward (repeat-info-forward seq-rdr rg pos alt type)
:reverse (repeat-info-backward seq-rdr rg pos alt type))))
(defn- mutation-type
[seq-rdr rg pos ref alt {:keys [prefer-deletion? prefer-insertion?]}]
(if (re-matches #"[acgntACGNT]*" alt)
(let [[ref-only alt-only offset _] (diff-bases ref alt)
nrefo (count ref-only)
nalto (count alt-only)
[unit ref-repeat alt-repeat] (repeat-info* seq-rdr rg (+ pos offset)
ref-only alt-only)]
(cond
(or (= nrefo nalto 0) (= nrefo nalto 1)) :substitution
(and prefer-deletion? (pos? nrefo) (zero? nalto)) :deletion
(and prefer-insertion? (zero? nrefo) (pos? nalto)) :insertion
(= ref-only (util-seq/revcomp alt-only)) :inversion
(and (some? unit) (= ref-repeat 1) (= alt-repeat 2)) :duplication
(and (some? unit) (pos? alt-repeat)
;; In the protein coding region, repeat descriptions are used only
;; for repeat units with a length which is a multiple of 3, i.e.
;; which can not affect the reading frame.
;; See https://varnomen.hgvs.org/recommendations/DNA/variant/repeated/#note
(if (and (rg/in-exon? (+ pos offset) rg)
(rg/in-exon? (+ pos offset (count ref-only) -1) rg))
(zero? (mod (count unit) 3))
true)) :repeated-seqs
(and (pos? nrefo) (zero? nalto)) :deletion
(and (pos? nrefo) (pos? nalto)) :indel
(some? unit) :duplication
(and (zero? nrefo) (pos? nalto)) :insertion
:else (throw (ex-info "Unsupported variant" {:type ::unsupported-variant}))))
(throw (ex-info "Unsupported variant" {:type ::unsupported-variant}))))
(defn- dna-substitution
[rg pos ref alt]
(let [{:keys [strand]} rg
type (if (= ref alt) "=" ">")]
(mut/dna-substitution (rg/cds-coord pos rg)
(cond-> ref (= strand :reverse) util-seq/revcomp)
type
(when-not (= type "=")
(cond-> alt (= strand :reverse) util-seq/revcomp)))))
(defn- dna-deletion
[rg pos ref alt]
(let [{:keys [strand]} rg
[del _ offset _] (diff-bases ref alt)
ndel (count del)
left (+ pos offset)
right (+ pos offset ndel -1)]
(mut/dna-deletion (rg/cds-coord (case strand
:forward left
:reverse right)
rg)
(if (> ndel 1)
(rg/cds-coord (case strand
:forward right
:reverse left)
rg))
(cond-> (subs ref offset) (= strand :reverse) util-seq/revcomp))))
(defn- dna-duplication
[rg pos ref alt]
(let [{:keys [strand]} rg
[_ ins offset _] (diff-bases ref alt)
start (case strand
:forward (+ pos offset (- (count ins)))
:reverse (+ pos offset (count ins) -1))
end (case strand
:forward (dec (+ start (count ins)))
:reverse (inc (- start (count ins))))]
(mut/dna-duplication (rg/cds-coord start rg)
(if-not (= start end)
(rg/cds-coord end rg))
(cond-> ins (= strand :reverse) util-seq/revcomp))))
(defn- dna-insertion
[rg pos ref alt]
(let [{:keys [strand]} rg
[_ ins offset _] (diff-bases ref alt)
start (cond-> (+ pos offset) (= strand :forward) dec)
end (cond-> (+ pos offset) (= strand :reverse) dec)]
(mut/dna-insertion (rg/cds-coord start rg)
(if-not (= start end)
(rg/cds-coord end rg))
(cond-> ins (= strand :reverse) util-seq/revcomp))))
(defn- dna-inversion
[rg pos ref alt]
(let [{:keys [strand]} rg
[inv _ offset _] (diff-bases ref alt)
left (+ pos offset)
right (+ pos offset (count inv) -1)
start (case strand
:forward left
:reverse right)
end (case strand
:forward right
:reverse left)]
(mut/dna-inversion (rg/cds-coord start rg)
(if-not (= start end)
(rg/cds-coord end rg)))))
;; e.g. ref alt
;; CAG CTC
;; CA CTC
;; CAG CT
(defn- dna-indel
[rg pos ref alt]
(let [{:keys [strand]} rg
[del ins offset _] (diff-bases ref alt)
ndel (count del)
left (+ pos offset)
right (+ pos offset ndel -1)]
(mut/dna-indel (rg/cds-coord (case strand
:forward left
:reverse right)
rg)
(if (> ndel 1)
(rg/cds-coord (case strand
:forward right
:reverse left)
rg))
(cond-> del (= strand :reverse) util-seq/revcomp)
(cond-> ins (= strand :reverse) util-seq/revcomp))))
(defn- dna-repeated-seqs
[seq-rdr rg pos ref alt]
(let [{:keys [strand]} rg
[ref-only alt-only offset _] (diff-bases ref alt)
[unit ref-repeat alt-repeat] (repeat-info* seq-rdr rg (+ pos offset)
ref-only alt-only)
nunit (count unit)
start (case strand
:forward (+ pos offset (- (* nunit (min ref-repeat alt-repeat))))
:reverse (+ pos offset (* nunit ref-repeat) -1))
end (case strand
:forward (dec (+ start nunit))
:reverse (inc (- start nunit)))]
(mut/dna-repeated-seqs (rg/cds-coord start rg)
(if-not (= start end)
(rg/cds-coord end rg))
unit
alt-repeat)))
(defn- mutation
[seq-rdr rg pos ref alt options]
(case (mutation-type seq-rdr rg pos ref alt options)
:substitution (dna-substitution rg pos ref alt)
:deletion (dna-deletion rg pos ref alt)
:duplication (dna-duplication rg pos ref alt)
:insertion (dna-insertion rg pos ref alt)
:inversion (dna-inversion rg pos ref alt)
:indel (dna-indel rg pos ref alt)
:repeated-seqs (dna-repeated-seqs seq-rdr rg pos ref alt)))
(defn ->hgvs
([variant seq-rdr rg]
(->hgvs variant seq-rdr rg {}))
([{:keys [pos ref alt]} seq-rdr rg options]
(hgvs/hgvs (:name rg)
:coding-dna
(mutation seq-rdr rg pos ref alt options))))
(defn- sequence-pstring
[ref-seq alt-seq start end {:keys [pos ref alt]} rg]
(let [pos (case (:strand rg)
:forward pos
:reverse (if (= (count ref) (count alt))
(+ pos (count ref) -1)
(+ pos (count ref))))
pos* (case (:strand rg)
:forward (- pos start)
:reverse (- end pos))
ref-seq (cond-> ref-seq (= (:strand rg) :reverse) util-seq/revcomp)
alt-seq (cond-> alt-seq (= (:strand rg) :reverse) util-seq/revcomp)
[ref-up ref ref-down] (pstring/split-at ref-seq
[pos*
(+ pos* (count ref))])
[alt-up alt alt-down] (pstring/split-at alt-seq
[pos*
(+ pos* (count alt))])
nmut (max (count ref) (count alt))
ticks (->> (iterate #(+ % 10) (inc (- start (mod start 10))))
(take-while #(<= % end))
(remove #(< % start)))
ticks (cond->> ticks (= (:strand rg) :reverse) reverse)
ticks-cds (map #(coord/format (rg/cds-coord % rg)) ticks)
tick-intervals (conj
(case (:strand rg)
:forward (->> ticks
(map #(+ % (if (< pos %)
(max 0 (- (count alt) (count ref)))
0)))
(map #(- % start))
(partition 2 1)
(mapv #(- (second %) (first %))))
:reverse (->> ticks
(map #(+ % (if (> pos %)
(max 0 (- (count alt) (count ref)))
0)))
(map #(- end %))
(partition 2 1)
(mapv #(- (second %) (first %)))))
0)
ticks-offset (case (:strand rg)
:forward (- (first ticks) start)
:reverse (- end (first ticks)))]
(string/join
\newline
[(apply pp/cl-format nil
(apply str "~V@T" (repeat (count ticks-cds) "~VA"))
ticks-offset (interleave tick-intervals ticks-cds))
(pp/cl-format nil "~A~VA~A" ref-up nmut ref ref-down)
(pp/cl-format nil "~A~VA~A" alt-up nmut alt alt-down)
(pp/cl-format nil "~V@T~V@{~A~:*~}" pos* nmut "^")])))
(defn- lower-case-intron
[seq* start rg]
(->> (map vector seq* (range start (+ start (count seq*))))
(map (fn [[c i]]
(if (rg/in-exon? i rg) c (string/lower-case c))))
(apply str)))
(defn debug-string
[{:keys [pos ref alt]} seq-rdr rg]
(let [start (max 1 (- pos 20))
end (+ pos (count ref) 20)
ref (lower-case-intron ref pos rg)
alt (lower-case-intron alt pos rg)
ref-seq (-> (cseq/read-sequence seq-rdr {:chr (:chr rg), :start start, :end end})
(lower-case-intron start rg))
alt-seq (common/alt-sequence ref-seq start pos ref alt)
[ticks refs alts muts] (string/split-lines
(sequence-pstring ref-seq alt-seq start end
{:pos pos :ref ref :alt alt} rg))]
(string/join \newline [(str " " ticks)
(str " c.ref: " refs)
(str " c.alt: " alts)
(str " " muts)])))