Skip to content

Commit

Permalink
Fix some validate-error mesages and coding styles.
Browse files Browse the repository at this point in the history
  • Loading branch information
niyarin committed Jul 19, 2023
1 parent 0a03b50 commit 41d578f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
18 changes: 10 additions & 8 deletions src/cljam/io/vcf/util/validator.clj
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@
type-check
(conj
(format
"Not match type declaration. Requires %s , but %s."
"Not match type declaration. Requires %s, but got %s."
type (str entries)))
number-check
(conj
(format "Invalid number of elements. Requires %s , but %d."
(format "Invalid number of elements. Requires %s, but got %d."
(str number) (count entries)))))))

(defn- check-each-samples [variant samples mformat]
Expand All @@ -100,7 +100,7 @@
#(or (nil? %) ((set (map (comp keyword :id) format)) %)))

(defn- make-info-validator [meta-info]
(let [id->info (map (comp keyword :id) meta-info)]
(let [id->info (into {} (map (juxt (comp keyword :id) identity)) meta-info)]
(fn [info alt-num]
(reduce
(fn [validated [id entry]]
Expand Down Expand Up @@ -131,13 +131,15 @@
((make-validator* meta-info header) variant))

(defn validate-variants
"Takes a variants list and throws an illegal variant
if it contains illegal content.
If there are no illegal mutations, the input list is returned as-is.
Validation is delayed."
"Checks if there is any invalid variant in the given sequence `variants`.
The validity of a variant is defined by `meta-info` and `header`.
Returns a lazy sequence of the same elements of the input if there are no
invalid variant. The validation is evaluated lazily and throws an exception
at the first invalid variant."
[meta-info header variants]
(let [validator (make-validator* meta-info header)]
(map (fn [v]
(when-let [info (validator v)]
(throw (ex-info "Invalid variant." (assoc info :variant v))))
(throw (ex-info (str "VCF validatoin failed: " (keys info))
(assoc info :variant v))))
v) variants)))
7 changes: 4 additions & 3 deletions test/cljam/io/vcf/util/validator_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
{:normal {:DP "str100"}} [:normal]
{:DP {:type "Integer" :number 1}})
[:normal :DP])
["Not match type declaration. Requires Integer , but [\"str100\"]."]))
[(str "Not match type declaration. "
"Requires Integer, but got [\"str100\"].")]))
(is (nil?
(#'validator/check-each-samples
{:normal {:GT "1|0"}} [:normal]
Expand Down Expand Up @@ -50,13 +51,13 @@
{:normal {:A [1]}} [:normal]
{:A {:type "Integer" :number 2}})
[:normal :A])
["Invalid number of elements. Requires 2 , but 1."]))
["Invalid number of elements. Requires 2, but got 1."]))
(is (= (get-in
(#'validator/check-each-samples
{:normal {:A [1]} :alt ["A" "T"]} [:normal]
{:A {:type "Integer" :number "A"}})
[:normal :A])
["Invalid number of elements. Requires A , but 1."])))
["Invalid number of elements. Requires A, but got 1."])))
(testing "not contain meta"
(is (= (get-in
(#'validator/check-each-samples
Expand Down

0 comments on commit 41d578f

Please sign in to comment.