Skip to content

Commit

Permalink
Fix boption type.
Browse files Browse the repository at this point in the history
  • Loading branch information
niyarin committed Nov 2, 2023
1 parent 390b53b commit a19e9ba
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 36 deletions.
78 changes: 57 additions & 21 deletions src/cljam/io/sam/util/validator.clj
Original file line number Diff line number Diff line change
Expand Up @@ -92,28 +92,64 @@
(and (= file-type :bam) (not (re-matches #"\*|[=ACMGRSVTWYHKDBN]+" seq'))) (error :seq "Must not contain bad character.")
(and (= file-type :sam) (not (re-matches #"\*|[A-Za-z=.]+" seq'))) (error :seq "Must not contain bad character.")))

(defn- validate-option [{:keys [value] type' :type}]

(defn- validate-sam-option [{:keys [value] type' :type}]
(case type'
"A" (when-not (and (char? value) (<= (int \!) (int value) (int \~)))
["Must be a char [!-~]."])
"i" (when-not (and (integer? value) (<= -2147483648 value 4294967295))
["Must be 32 bit signed integer."])
"f" (when-not (or (float? value) (integer? value))
["Must be a float."])
"Z" (when-not (and (string? value) (re-matches #"[ !-~]*" value))
["Must be a printable string [ !-~]*"])
"H" (when-not (and (sequential? value)
(every? (every-pred integer? #(<= -255 (int %) 255))
value))
["Must be a byte array."])
"B" (when-not (and (string? value)
(re-matches #"[cCsSiIf](,[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)*"
value))
["Must be a string of comma-separated array of numbers."])
[(format "Type %s is invalid" (str type'))]))

(defn- validate-bam-option [{:keys [value] type' :type}]
(case type'
"A" (when-not (and (char? value) (<= (int \!) (int value) (int \~)))
["Must be a char [!-~]."])
"i" (when-not (and (integer? value) (<= -2147483648 value 4294967295))
["Must be 32 bit signed integer."])
"f" (when-not (or (float? value) (integer? value))
["Must be a float."])
"Z" (when-not (and (string? value) (re-matches #"[ !-~]*" value))
["Must be a printable string [ !-~]*"])
"H" (when-not (and (sequential? value)
(every? (every-pred integer? #(<= -255 (int %) 255))
value))
["Must be a byte array."])
"B" (when-not (and (string? value)
(re-matches #"[cCsSiIf](,[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)*"
value))
["Must be a string of comma-separated array of numbers."])
[(format "Type %s is invalid" (str type'))]))

(defn- validate-options [_ {:keys [options]}]
(map-indexed #(when-let [err (validate-option %2)]
"A" (when-not (and (char? value) (<= (int \!) (int value) (int \~)))
["Must be a char [!-~]."])
"c" (when-not (and (integer? value) (<= 0 value 127))
["Must be 8 bit signed integer."])
"C" (when-not (and (integer? value) (<= 0 value 255))
["Must be 8 bit unsigned integer."])
"s" (when-not (and (integer? value) (<= -32768 value 32767))
["Must be 16 bit signed integer."])
"S" (when-not (and (integer? value) (<= 0 value 65535))
["Must be 16 bit usgned integer."])
"i" (when-not (and (integer? value) (<= -2147483648 value 2147483647))
["Must be 32 bit signed integer."])
"I" (when-not (and (integer? value) (<= 0 value 4294967296))
["Must be 32 bit signed integer."])
"f" (when-not (or (float? value) (integer? value))
["Must be a float."])
"Z" (when-not (and (string? value) (re-matches #"[ !-~]*" value))
["Must be a printable string [ !-~]*"])
"H" (when-not (and (sequential? value)
(every? (every-pred integer? #(<= -255 (int %) 255))
value))
["Must be a byte array."])
"B" (when-not (and (string? value)
(re-matches #"[cCsSiIf](,[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)*"
value))
["Must be a string of comma-separated array of numbers."])
[(format "Type %s is invalid" (str type'))]))

(defn- validate-option [v file-type]
(case file-type
:sam (validate-sam-option v)
:bam (validate-bam-option v)))

(defn- validate-options [{:keys [file-type]} {:keys [options]}]
(map-indexed #(when-let [err (validate-option %2 file-type)]
(apply error [:options %1] err))
options))

Expand Down
35 changes: 20 additions & 15 deletions test/cljam/io/sam/util/validator_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,40 @@

(deftest validate-option-test
(testing "bad type"
(is (= (#'validator/validate-option {:type "!" :value \!})
(is (= (#'validator/validate-option {:type "!" :value \!} :sam)
["Type ! is invalid"])))
(testing "type A"
(is (nil? (#'validator/validate-option {:type "A" :value \!})))
(is (= (#'validator/validate-option {:type "A" :value 100})
(is (nil? (#'validator/validate-option {:type "A" :value \!} :sam)))
(is (= (#'validator/validate-option {:type "A" :value 100} :sam)
["Must be a char [!-~]."])))
(testing "type c bam"
(is (nil? (#'validator/validate-option {:type "c" :value 10} :bam)))
(is (= (#'validator/validate-option {:type "c" :value 300} :bam)
["Must be 8 bit signed integer."])))
(testing "type i"
(is (nil? (#'validator/validate-option {:type "i" :value 10})))
(is (= (#'validator/validate-option {:type "i" :value "10"})
(is (nil? (#'validator/validate-option {:type "i" :value 10} :sam)))
(is (= (#'validator/validate-option {:type "i" :value "10"} :sam)
["Must be 32 bit signed integer."]))
(is (= (#'validator/validate-option {:type "i" :value 100000000000})
(is (= (#'validator/validate-option {:type "i" :value 100000000000} :sam)
["Must be 32 bit signed integer."])))
(testing "type f"
(is (nil? (#'validator/validate-option {:type "f" :value 10})))
(is (nil? (#'validator/validate-option {:type "f" :value 10.1})))
(is (= (#'validator/validate-option {:type "f" :value "A"})
(is (nil? (#'validator/validate-option {:type "f" :value 10} :sam)))
(is (nil? (#'validator/validate-option {:type "f" :value 10.1} :sam)))
(is (= (#'validator/validate-option {:type "f" :value "A"} :sam)
["Must be a float."])))
(testing "type Z"
(is (nil? (#'validator/validate-option {:type "Z" :value "!@abc"})))
(is (= (#'validator/validate-option {:type "Z" :value 10})
(is (nil? (#'validator/validate-option {:type "Z" :value "!@abc"} :sam)))
(is (= (#'validator/validate-option {:type "Z" :value 10} :sam)
["Must be a printable string [ !-~]*"])))
(testing "type H"
(is (nil? (#'validator/validate-option {:type "H" :value [1,2]})))
(is (= (#'validator/validate-option {:type "H" :value "A"})
(is (nil? (#'validator/validate-option {:type "H" :value [1,2]} :sam)))
(is (= (#'validator/validate-option {:type "H" :value "A"} :sam)
["Must be a byte array."])))
(testing "type B"
(is (nil? (#'validator/validate-option
{:type "B" :value "f,-0.3,0.0,0.3"})))
(is (= (#'validator/validate-option {:type "B" :value "W"})
{:type "B" :value "f,-0.3,0.0,0.3"}
:sam)))
(is (= (#'validator/validate-option {:type "B" :value "W"} :sam)
["Must be a string of comma-separated array of numbers."]))))

(deftest validate-data-record-test
Expand Down

0 comments on commit a19e9ba

Please sign in to comment.