-
Notifications
You must be signed in to change notification settings - Fork 12
/
t_sorter.clj
158 lines (148 loc) · 9.49 KB
/
t_sorter.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
(ns cljam.t-sorter
(:require [clojure.test :refer :all]
[cljam.t-common :refer :all]
[cljam.sam :as sam]
[cljam.bam :as bam]
[cljam.sorter :as sorter]))
(def tmp-coordinate-sorted-sam-file (str temp-dir "/" "tmp.coordinate.sorted.sam"))
(def tmp-coordinate-sorted-bam-file (str temp-dir "/" "tmp.coordinate.sorted.bam"))
(def tmp-queryname-sorted-sam-file (str temp-dir "/" "tmp.queryname.sorted.sam"))
(def tmp-queryname-sorted-bam-file (str temp-dir "/" "tmp.queryname.sorted.bam"))
(def tmp-shuffled-sam-file (str temp-dir "/" "tmp.shuffled.sam"))
(def tmp-shuffled-bam-file (str temp-dir "/" "tmp.shuffled.bam"))
(def tmp-coordinate-sorted-sam-file-2 (str temp-dir "/" "tmp.coordinate.sorted.2.sam"))
(def tmp-coordinate-sorted-bam-file-2 (str temp-dir "/" "tmp.coordinate.sorted.2.bam"))
(def tmp-queryname-sorted-sam-file-2 (str temp-dir "/" "tmp.queryname.sorted.2.sam"))
(def tmp-queryname-sorted-bam-file-2 (str temp-dir "/" "tmp.queryname.sorted.2.bam"))
(defn- prepare-shuffled-files!
[]
(spit-sam-for-test tmp-shuffled-sam-file (get-shuffled-test-sam))
(spit-bam-for-test tmp-shuffled-bam-file (get-shuffled-test-sam)))
(defn- with-reader
[target-fn src-file & [dst-file]]
(let [src-rdr (cond
(re-find #"\.sam$" src-file) (sam/reader src-file)
(re-find #"\.bam$" src-file) (bam/reader src-file :ignore-index true)
:else (throw (RuntimeException.
(str "invalid file suffix " src-file))))
dst-wtr (if dst-file
(cond
(not dst-file) nil
(re-find #"\.sam$" dst-file) (sam/writer dst-file)
(re-find #"\.bam$" dst-file) (bam/writer dst-file)
:else (throw (RuntimeException.
(str "invalid file suffix " dst-file))))
nil)]
(if-not (nil? dst-wtr)
(with-open [src src-rdr
dst dst-wtr] (target-fn src dst))
(with-open [src src-rdr] (target-fn src)))))
(deftest about-sorting-a-sam-by-chromosomal-positions
(with-before-after {:before (do (prepare-cache!)
(prepare-shuffled-files!))
:after (clean-cache!)}
;; tests by test-sam-file and test-bam-file
(is (thrown? Exception
(with-reader sorter/sort-by-pos test-sam-file tmp-coordinate-sorted-sam-file)))
(is (not-throw? (with-reader sorter/sort-by-pos test-bam-file tmp-coordinate-sorted-bam-file)))
;; (is (not-throw? (with-reader sorter/sort-by-qname test-sam-file tmp-queryname-sorted-sam-file))) ; TODO: future
;; (is (not-throw? (with-reader sorter/sort-by-qname test-bam-file tmp-queryname-sorted-bam-file))) ; TODO: future
(is (not (with-reader sorter/sorted-by? test-sam-file)))
(is (not (with-reader sorter/sorted-by? test-bam-file)))
(is (not (with-reader sorter/sorted-by? tmp-coordinate-sorted-sam-file)))
(is (with-reader sorter/sorted-by? tmp-coordinate-sorted-bam-file))
;; (is (with-reader sorter/sorted-by? tmp-queryname-sorted-sam-file)) ; TODO: future
;; (is (with-reader sorter/sorted-by? tmp-queryname-sorted-bam-file)) ; TODO: future
(is (= (with-reader sorter/sort-order test-sam-file) sorter/order-unknown))
(is (= (with-reader sorter/sort-order test-bam-file) sorter/order-unknown))
(is (= (with-reader sorter/sort-order tmp-coordinate-sorted-sam-file)
sorter/order-unknown))
(is (= (with-reader sorter/sort-order tmp-coordinate-sorted-bam-file)
sorter/order-coordinate))
;; (is (= (with-reader sorter/sort-order tmp-queryname-sorted-sam-file)
;; sorter/order-queryname)) ; TODO: future
;; (is (= (with-reader sorter/sort-order tmp-queryname-sorted-bam-file)
;; sorter/order-queryname)) ; TODO: future
;; tests by shuffled files (its may sorted by chance)
(is (thrown? Exception
(with-reader sorter/sort-by-pos tmp-shuffled-sam-file tmp-coordinate-sorted-sam-file-2)))
(is (nil? (with-reader sorter/sort-by-pos tmp-shuffled-bam-file tmp-coordinate-sorted-bam-file-2)))
;; (is (not-throw? (with-reader sorter/sort-by-qname tmp-shuffled-sam-file tmp-coordinate-sorted-sam-file-2))) ; TODO: future
;; (is (not-throw? (with-reader sorter/sort-by-qname tmp-shuffled-bam-file tmp-coordinate-sorted-bam-file-2))) ; TODO: future
(is (not-throw? (with-reader sorter/sorted-by? tmp-shuffled-sam-file)))
(is (not-throw? (with-reader sorter/sorted-by? tmp-shuffled-bam-file)))
(is (not (with-reader sorter/sorted-by? tmp-coordinate-sorted-sam-file-2)))
(is (with-reader sorter/sorted-by? tmp-coordinate-sorted-bam-file-2))
;; (is (with-reader sorter/sorted-by? tmp-queryname-sorted-sam-file-2)) ; TODO: future
;; (is (with-reader sorter/sorted-by? tmp-queryname-sorted-bam-file-2)) ; TODO: future
(is (get #{:queryname :coordinate :unsorted :unknown}
(with-reader sorter/sort-order tmp-shuffled-sam-file)))
(is (get #{:queryname :coordinate :unsorted :unknown}
(with-reader sorter/sort-order tmp-shuffled-bam-file)))
(is (= (with-reader sorter/sort-order tmp-coordinate-sorted-sam-file-2)
sorter/order-unknown))
(is (= (with-reader sorter/sort-order tmp-coordinate-sorted-bam-file-2)
sorter/order-coordinate))
;; (is (= (with-reader sorter/sort-order tmp-queryname-sorted-sam-file-2)
;; sorter/order-queryname)) ; TODO: future
;; (is (= (with-reader sorter/sort-order tmp-queryname-sorted-bam-file-2)
;; sorter/order-queryname)) ; TODO: future
;; check sorting order
(is (not-throw? (check-sort-order (slurp-sam-for-test tmp-coordinate-sorted-sam-file-2) (slurp-sam-for-test tmp-coordinate-sorted-sam-file))))
(is (not-throw? (check-sort-order (slurp-bam-for-test tmp-coordinate-sorted-bam-file-2) (slurp-bam-for-test tmp-coordinate-sorted-bam-file))))
;; compare generated files
(is (= (slurp-sam-for-test tmp-coordinate-sorted-sam-file-2)
(slurp-sam-for-test tmp-coordinate-sorted-sam-file)))
(is (= (slurp-bam-for-test tmp-coordinate-sorted-bam-file-2)
(slurp-bam-for-test tmp-coordinate-sorted-bam-file)))
;; (is (= (slurp-sam-for-test tmp-queryname-sorted-sam-file-2)
;; (slurp-sam-for-test tmp-queryname-sorted-sam-file))) ; TODO: future
;; (is (= (slurp-bam-for-test tmp-queryname-sorted-bam-file-2)
;; (slurp-bam-for-test tmp-queryname-sorted-bam-file))) ; TODO: future
;; TODO: Add test sorter/sort-by-pos with bam file includes many blocks
;; (Currently, testing bam files have only one block)
;; (is (not-throw? (with-reader sorter/sort-by-pos many-blocks-bam-file (str temp-dir "/many.bam"))))
))
(deftest-slow about-sorting-medium-file
(with-before-after {:before (prepare-cache!)
:after (clean-cache!)}
(is (thrown? Exception
(with-reader sorter/sort-by-pos medium-bam-file tmp-coordinate-sorted-sam-file)))
(is (nil? (with-reader sorter/sort-by-pos medium-bam-file tmp-coordinate-sorted-bam-file)))
;; (is (not-throw? (with-reader sorter/sort-by-qname medium-bam-file tmp-queryname-sorted-sam-file))) ; TODO: future
;; (is (not-throw? (with-reader sorter/sort-by-qname medium-bam-file tmp-queryname-sorted-bam-file))) ; TODO: future
(is (not (with-reader sorter/sorted-by? medium-bam-file)))
(is (not (with-reader sorter/sorted-by? tmp-coordinate-sorted-sam-file)))
(is (with-reader sorter/sorted-by? tmp-coordinate-sorted-bam-file))
;; (is (with-reader sorter/sorted-by? tmp-queryname-sorted-sam-file)) ; TODO: future
;; (is (with-reader sorter/sorted-by? tmp-queryname-sorted-bam-file)) ; TODO: future
(is (= (with-reader sorter/sort-order medium-bam-file)
sorter/order-unknown))
(is (= (with-reader sorter/sort-order tmp-coordinate-sorted-sam-file)
sorter/order-unknown))
(is (= (with-reader sorter/sort-order tmp-coordinate-sorted-bam-file)
sorter/order-coordinate))
;; (is (= (with-reader sorter/sort-order tmp-queryname-sorted-sam-file)
;; sorter/order-queryname)) ; TODO: future
;; (is (= (with-reader sorter/sort-order tmp-queryname-sorted-bam-file)
;; sorter/order-queryname)) ; TODO: future
;; check sorting order
(is (not-throw? (check-sort-order (slurp-sam-for-test tmp-coordinate-sorted-sam-file))))
(is (not-throw? (check-sort-order (slurp-bam-for-test tmp-coordinate-sorted-bam-file))))))
(deftest-slow-heavy about-sorting-large-file
(with-before-after {:before (do (prepare-cavia!)
(prepare-cache!))
:after (clean-cache!)}
(is (nil? (with-reader sorter/sort-by-pos large-bam-file tmp-coordinate-sorted-bam-file)))
;; (is (not-throw? (with-reader sorter/sort-by-qname large-bam-file tmp-queryname-sorted-bam-file))) ; TODO: future
;; (is (not (with-reader sorter/sorted-by? large-bam-file)))
(is (with-reader sorter/sorted-by? tmp-coordinate-sorted-bam-file))
;; (is (with-reader sorter/sorted-by? tmp-queryname-sorted-bam-file)) ; TODO: future
;; (is (= (with-reader sorter/sort-order large-bam-file) sorter/order-unknown))
(is (= (with-reader sorter/sort-order tmp-coordinate-sorted-bam-file)
sorter/order-coordinate))
;; (is (= (with-reader sorter/sort-order tmp-queryname-sorted-bam-file)
;; sorter/order-queryname)) ; TODO: future
;; check sorting order
;; (is (not-throw? (check-sort-order (slurp-bam-for-test tmp-coordinate-sorted-bam-file))))
))