From 41d578f786c75f63f508dbc9828f89b5a1c3ba8a Mon Sep 17 00:00:00 2001 From: niyarin Date: Tue, 18 Jul 2023 16:27:48 +0900 Subject: [PATCH] Fix some validate-error mesages and coding styles. --- src/cljam/io/vcf/util/validator.clj | 18 ++++++++++-------- test/cljam/io/vcf/util/validator_test.clj | 7 ++++--- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/cljam/io/vcf/util/validator.clj b/src/cljam/io/vcf/util/validator.clj index 377a0de1..fed45d98 100644 --- a/src/cljam/io/vcf/util/validator.clj +++ b/src/cljam/io/vcf/util/validator.clj @@ -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] @@ -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]] @@ -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))) diff --git a/test/cljam/io/vcf/util/validator_test.clj b/test/cljam/io/vcf/util/validator_test.clj index 7b206dd0..13e7ea3c 100644 --- a/test/cljam/io/vcf/util/validator_test.clj +++ b/test/cljam/io/vcf/util/validator_test.clj @@ -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] @@ -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