/
reader.clj
180 lines (159 loc) · 6.25 KB
/
reader.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
(ns cljam.io.vcf.reader
"A type of VCF reader and internal functions to read VCF contents. See
https://samtools.github.io/hts-specs/ for the detail VCF specifications."
(:require [clojure.string :as cstr]
[clojure.tools.logging :as logging]
[cljam.io.protocols :as protocols]
[camel-snake-kebab.core :refer [->kebab-case-keyword]]
[proton.core :refer [as-long]]
[cljam.io.vcf.util :as vcf-util])
(:import [java.io Closeable]
[clojure.lang LazilyPersistentVector]))
;; VCFReader
;; ---------
(declare read-variants)
(deftype VCFReader [url meta-info header reader]
Closeable
(close [this]
(.close ^Closeable (.reader this)))
protocols/IReader
(reader-url [this] (.url this))
(read [this] (read-variants this))
(read [this option] (read-variants this option))
(indexed? [_] false)
protocols/IRegionReader
(read-in-region [this region]
(protocols/read-in-region this region {}))
(read-in-region [this {:keys [chr start end]} option]
(logging/warn "May cause degradation of performance.")
(filter
(fn [v] (and (if chr (= (:chr v) chr) true)
(if start (<= start (:pos v)) true)
(if end (<= (+ (:pos v) (count (:ref v))) end) true)))
(read-variants this option))))
;; need dynamic extension for namespace issue.
(extend-type VCFReader
protocols/IVariantReader
(meta-info [this] (.meta-info this))
(header [this] (.header this))
(read-variants
([this] (protocols/read-variants this {}))
([this option] (read-variants this option))))
;; Utilities
;; ---------
(defn- dot->nil
[^String s]
;; Avoid calling equiv on strings.
(if (and (= 1 (.length s)) (= \. (.charAt s 0))) nil s))
;; Loading meta-information
;; ------------------------
(defn- meta-line?
[line]
(= (subs line 0 2) "##"))
(defn- parse-structured-line
"e.g. (parse-structured-line \"ID=NS,Number=1,Type=Integer,Description=\\\"Number of Samples With Data\\\"\")
=> {:id \"NS\" :number \"1\" :type \"Integer\"
:description \"Number of Samples With Data\"}"
[s]
(letfn [(parse-string-field [s]
(loop [s s, acc []]
(if-let [[_ pre c post] (re-matches #"([^\\\"]*?)([\\\"])(.*)" s)]
(if (= c "\\")
;; expecting one of the special characters occurs immediately after \\
(if (#{\\ \"} (first post))
(recur (subs post 1) (conj acc pre (first post)))
(let [msg (str "Either '\\' or '\"' was expected immediately after '\\', "
"but got '" (first post) "'")]
(throw (RuntimeException. msg))))
;; c == \", that is, we are now at the end of the string field
[nil (cstr/join (conj acc pre)) (cstr/replace post #"^," "")])
(throw (RuntimeException. "Unexpected end of string field")))))]
(loop [s s, m {}]
(if-let [[_ k more] (some->> s (re-matches #"([^=]+?)=(.*)"))]
(let [[_ v more] (if (= (first more) \")
(parse-string-field (subs more 1))
(re-matches #"([^,]*?)(?:,(.*))?" more))]
(recur more (assoc m (->kebab-case-keyword k) (dot->nil v))))
m))))
(defn- parse-meta-info-contig
[m]
(update m :length as-long))
(defn- parse-meta-info-info
[m]
(update m :number (fn [s]
(if (#{"A" "R" "G"} s)
s
(as-long s)))))
(defn parse-meta-info-line
[line]
(let [[_ k* v] (re-find #"^##([\w:/\.\?\-]*)=(.*)$" line)
k (->kebab-case-keyword k*)]
[k (if-let [[_ s] (re-find #"^<(.+)>$" v)]
(cond-> (parse-structured-line s)
(#{:info :format} k) parse-meta-info-info
(= k :contig) parse-meta-info-contig)
v)]))
(defn load-meta-info
[^java.io.BufferedReader rdr]
(loop [line (.readLine rdr), meta-info {}]
(if (meta-line? line)
(let [[k v] (parse-meta-info-line line)]
(recur (.readLine rdr)
(if (#{:contig :info :filter :format :alt :sample :pedigree} k)
(if (get meta-info k)
(update meta-info k conj v)
(assoc meta-info k [v]))
(assoc meta-info k v))))
meta-info)))
;; Loading header
;; --------------
(defn- header-line?
[line]
(not (nil? (re-find #"^#[^#]*$" line))))
(defn parse-header-line
[line]
(cstr/split (subs line 1) #"\t"))
(defn load-header
[^java.io.BufferedReader rdr]
(loop [line (.readLine rdr)]
(if (header-line? line)
(parse-header-line line)
(recur (.readLine rdr)))))
;; Reading data lines
;; ------------------
(defn- parse-data-line
[line kws]
;; When splitting a string with single-character delimiter,
;; `java.lang.String#split` is slightly faster than `clojure.string/split`.
;; For more details, please refer to https://github.com/chrovis/cljam/pull/29.
(let [[fields gt-fields] (->> (.split ^String line "\t")
LazilyPersistentVector/createOwning
(map dot->nil)
(split-at 8))]
(->> gt-fields
(interleave kws)
(concat [:chr (first fields)
:pos (as-long (nth fields 1))
:id (nth fields 2)
:ref (nth fields 3)
:alt (some-> (nth fields 4) (cstr/split #","))
:qual (some-> (nth fields 5) Double/parseDouble)
:filter (nth fields 6)
:info (nth fields 7)])
(apply hash-map))))
(defn- read-data-lines
[^java.io.BufferedReader rdr header kws]
(when-let [line (.readLine rdr)]
(if-not (or (meta-line? line) (header-line? line))
(cons (parse-data-line line kws)
(lazy-seq (read-data-lines rdr header kws)))
(read-data-lines rdr header kws))))
(defn read-variants
([rdr]
(read-variants rdr {}))
([^VCFReader rdr {:keys [depth] :or {depth :deep}}]
(let [kws (mapv keyword (drop 8 (.header rdr)))
parse-fn (case depth
:deep (vcf-util/variant-parser (.meta-info rdr) (.header rdr))
:vcf identity)]
(map parse-fn (read-data-lines (.reader rdr) (.header rdr) kws)))))