-
Notifications
You must be signed in to change notification settings - Fork 1
/
2bit.lisp
486 lines (424 loc) · 17.8 KB
/
2bit.lisp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
;;; org-davep-2bit --- 2bit file reader for Common Lisp.
;;
;; 2bit.lisp --- Main code for org-davep-2bit.
;; Copyright 2020 by Dave Pearson <[email protected]>
;;
;; This software is Copyright (C) Dave Pearson <[email protected]> 2020
;;
;; Dave Pearson grants you the rights to distribute and use this software as
;; governed by the terms of the Lisp Lesser GNU Public License
;; <URL:http://opensource.franz.com/preamble.html>, known as the LLGPL.
;;; Commentary:
;;
;; The following code provides a set of classes and functions for reading
;; data from 2bit files. The format of such a file is described here:
;;
;; http://genome.ucsc.edu/FAQ/FAQformat.html#format7
;;
;; You can always find the latest version of this code here:
;;
;; https://github.com/davep/org-davep-2bit
;;; Code:
(in-package :org.davep.2bit)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Useful constants.
(defconstant +byte+ '(unsigned-byte 8)
"Type of a byte array element.")
(defconstant +2bit-version+ 0
"The only valid version number of 2bit data.")
(defconstant +signature+ #x1a412743
"2bit file signature.")
(defconstant +bases+ #("T" "C" "A" "G")
"Vector of the bases.
Note that the positions of each base in the vector map to the 2bit decoding
for them.")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; General utility functions and macros.
(defmacro with-saved-location ((reader pos) &rest body)
"Helper macro to save position while visiting elsewhere in the data."
(let ((old-pos (gensym)))
`(let ((,old-pos (pos ,reader)))
(unwind-protect
(progn
(setf (pos ,reader) ,pos)
,@body)
(setf (pos ,reader) ,old-pos)))))
(declaim (inline swap-long))
(defun swap-long (value)
"Swap the endianness of a long integer VALUE."
(logior
(logand (ash value -24) #xff)
(logand (ash value -8) #xff00)
(logand (ash value 8) #xff0000)
(logand (ash value 24) #xff000000)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The sequence access class.
(defclass block-collection ()
((count
:accessor block-count
:type integer
:documentation "The number of blocks in the sequence.")
(starts
:accessor block-starts
:type list
:initform nil
:documentation "List of 0-based integers indicating the start position of each block")
(sizes
:accessor block-sizes
:type list
:documentation "List of integers indicating the length of each block"))
(:documentation "Holds details of a collection of blocks in a sequence."))
(defun make-block-collection (reader)
"Create a block collection based on the current position in the reader."
(let ((blocks (make-instance 'block-collection)))
;; Get the count of blocks.
(setf (block-count blocks) (long-read reader))
;; Next up is the list of block start positions.
(setf (block-starts blocks) (longs-read reader (block-count blocks)))
;; Finally there's the lengths of the blocks.
(setf (block-sizes blocks) (longs-read reader (block-count blocks)))
;; Return the new object.
blocks))
(defmethod relevant-blocks ((sequence-start integer) (sequence-end integer) (blocks block-collection))
"Return a list of relevant start/end positions from BLOCKS.
The return value isn't all the blocks in BLOCKS, but is all the blocks in
BLOCKS that intersect with the sequence bounded by START and END."
(loop for start in (block-starts blocks)
for size in (block-sizes blocks)
if (and (>= sequence-end start) (> (+ start size) sequence-start))
collect (cons start (+ start size))))
(defclass 2bit-sequence ()
((reader
:accessor reader
:initarg :reader
:type reader
:documentation "The reader object to use to read the sequence from the 2bit data.")
(name
:accessor name
:initarg :name
:type srtring
:documentation "The name of the sequence.")
(offset
:accessor offset
:initarg :offset
:type integer
:documentation "The offset of the sequence in the 2bit data.")
(dna-size
:accessor dna-size
:type integer
:documentation "Size of the DNA in the sequence.")
(n-blocks
:accessor n-blocks
:type block-collection
:documentation "Details of the blocks of Ns in the sequence.")
(mask-blocks
:accessor mask-blocks
:type block-collection
:documentation "Details of the masked blocks in the sequence.")
(dna-offset
:accessor dna-offset
:type integer
:documentation "The location in the data where the actual DNA data starts."))
(:documentation "Class that provides access to a specific sequence."))
(defmethod print-object ((sequence 2bit-sequence) stream)
"Format SEQUENCE for easy reading when output to STREAM."
(print-unreadable-object (sequence stream :type t)
(format stream "~S ~S" (name sequence) (offset sequence))))
(defun make-2bit-sequence (reader name offset)
"Crete a new 2bit sequence object."
;; Create a sequence object.
(let ((seq (make-instance '2bit-sequence
:reader reader
:name name
:offset offset)))
(with-saved-location (reader offset)
;; Get the size of the DNA.
(setf (dna-size seq) (long-read reader))
;; Get the N-block information.
(setf (n-blocks seq) (make-block-collection reader))
;; Get the mask block information.
(setf (mask-blocks seq) (make-block-collection reader))
;; Skip a reserved value.
(raw-long-read reader)
;; Finally, record where we ended up as this is where the actual
;; sequence data starts and we'll want to constantly revisit this
;; location.
(setf (dna-offset seq) (pos reader))
;; Return the new sequence.
seq)))
(define-condition invalid-location (error)
((reason
:initarg :reason
:initform ""
:reader reason)
(start
:initarg :start
:initform nil
:reader start)
(end
:initarg :end
:initform nil
:reader end))
(:documentation "Error thrown when a request for bases has a location problem."))
(defmethod bases :before ((sequence 2bit-sequence) (start integer) (end integer))
"Perform checks on START and END in relation to SEQUENCE.
This :before method ensures that START and END are within bounds."
(when (>= start end)
(error 'invalid-location
:reason "Start is greater than or equal to the end"
:start start
:end end))
(when (< start 0)
(error 'invalid-location
:reason "Start is less than 0"
:start start
:end end))
(when (>= start (dna-size sequence))
(error 'invalid-location
:reason "Start is beyond the end of the sequence"
:start start
:end end))
(when (> end (dna-size sequence))
(error 'invalid-location
:reason "End is beyond the end of the sequence"
:start start
:end end)))
(defmethod bases ((sequence 2bit-sequence) (start integer) (end integer))
"Get the bases between START and END from SEQUENCE."
(let* ((start-byte (+ (dna-offset sequence) (floor (/ start 4))))
(end-byte (+ (dna-offset sequence) (floor (/ (1- end) 4))))
(position (* (- start-byte (dna-offset sequence)) 4))
(buffer (with-saved-location ((reader sequence) start-byte)
(bytes-read (reader sequence) (1+ (- end-byte start-byte)))))
(n-blocks (relevant-blocks start end (n-blocks sequence)))
(mask-blocks (relevant-blocks start end (mask-blocks sequence)))
(out (make-string-output-stream)))
(loop
;; For each byte in the buffer..
for byte across buffer
do (loop
;; For each 2 bits in the byte...
for shift from 6 downto 0 by 2
;; ...while we've not hit the end of what we're interested in...
while (< position end)
;; ...if we're interested in this particular base...
if (>= position start)
;; ...collect it
do (princ
;; If the position we're looking at is within an N block...
(if (loop for block in n-blocks thereis (and (< (1- (car block)) position (cdr block))))
;; ...it's an N, obviously.
"N"
;; ...otherwise decode the base.
(let ((base (elt +bases+ (ash (logand (ash #b11 shift) byte) (- shift)))))
;; If we're masking and this base is within a masked region...
(if (and (masking (reader sequence))
(loop for block in mask-blocks thereis (and (< (1- (car block)) position (cdr block)))))
;; ...downcase the base...
(string-downcase base)
;; ...otherwise go with it as-is.
base)))
out)
do (incf position))) ;; Bump the base position along one.
(get-output-stream-string out)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The main reader class. This does the work of pulling information out of
;; the 2bit data. This specific class does not implement the code to
;; actually do the reading -- the aim down the line is to allow reading via
;; different methods (file IO, via HTTP, etc) -- it just implements the base
;; class that other usable classes will inherit from.
(defclass reader ()
((source
:accessor source
:initarg :source
:type string
:documentation "The name of the source of the 2bit data.")
(signature
:accessor signature
:type integer
:documentation "The signature of the 2bit data.")
(version
:accessor version
:type integer
:documentation "The version ID of the 2bit data.")
(sequence-count
:accessor sequence-count
:type integer
:documentation "The count of sequences inside the 2bit data.")
(masking
:accessor masking
:initarg :masking
:initform t
:type boolean
:documentation "Should masking be taken into account?")
(index
:accessor index
:type hash-table
:documentation "Hash table that holds the index of the 2bit data."))
(:documentation "Class that handles the basics of reading from 2bit data."))
(define-condition invalid-signature (error)
((signature
:initarg :signature
:initform nil
:reader signature))
(:documentation "Error thrown when an invalid signature value is found in a 2bit header."))
(define-condition invalid-version (error)
((version
:initarg :version
:initform nil
:reader version))
(:documentation "Error thrown when an invalid version value is found in a 2bit header."))
(define-condition not-implemented (error)
()
(:documentation "Type of error raised if a method hasn't been implemented."))
(defmethod valid-signature-p ((reader reader))
"Does the 2bit data have a valid signature?"
(or (= (signature reader) +signature+)
(= (swap-long (signature reader)) +signature+)))
(defmethod swapped-p ((reader reader))
"Is the data in READER byte-swapped?"
(= (swap-long (signature reader)) +signature+))
(defmethod pos ((reader reader))
"Get the current data position."
(error 'not-implemented))
(defmethod (setf pos) (pos (reader reader))
"Set the current data position."
(error 'not-implemented))
(defmethod byte-read ((reader reader))
"Read a byte from READER"
(error 'not-implemented))
(defmethod raw-long-read ((reader reader))
"Read a long (4-byte) numeric value from READER."
(error 'not-implemented))
(defmethod long-read ((reader reader))
"Read a long (4-byte) numeric value from READER."
(let ((value (raw-long-read reader)))
(if (swapped-p reader)
(swap-long value)
value)))
(defmethod bytes-read ((reader reader) (len integer))
"Read an array of bytes from READER."
(error 'not-implemented))
(defmethod string-read ((reader reader) (len integer))
"Read a string of LEN length from READER."
(map 'string #'code-char (bytes-read reader len)))
(defmethod longs-read ((reader reader) (count integer))
"Read in a list of COUNT long (32-bit) values from READER."
(let ((buffer (bytes-read reader (* count 4))))
(loop for n from 0 below count
for pos = (* n 4)
for value = (logior
(aref buffer pos)
(ash (aref buffer (1+ pos )) 8)
(ash (aref buffer (+ 2 pos)) 16)
(ash (aref buffer (+ 3 pos)) 24))
collect (if (swapped-p reader) (swap-long value) value))))
(defmethod load-index-entry ((reader reader) (index hash-table))
"Read a single entry from READER and add to INDEX.
This method reads from the current position, reading the name of the entry
in the index of sequences, and then reads its offset and adds that to
INDEX."
;; We should be looking at the size of the name of the next entry in the
;; index. Grab that and make a buffer big enough to load it up.
(let ((name (string-read reader (byte-read reader))))
(setf (gethash name index)
(make-2bit-sequence reader name (long-read reader)))))
(defmethod read-index ((reader reader))
"Read the 2bit data index from READER."
;; Set up a new hash table to hold the index.
(let ((index (make-hash-table :test #'equal)))
;; Now loop over all the entries in the index and add them to the table.
(loop for n from 1 to (sequence-count reader)
do (load-index-entry reader index)
finally (return index))))
(defmethod open-reader ((reader reader))
"Open READER for further reading."
;; Pull the signature out of the header.
(setf (signature reader) (raw-long-read reader))
;; Does the signature look valid?
(unless (valid-signature-p reader)
(error 'invalid-signature :signature (signature reader)))
;; Pull the version out of the header.
(setf (version reader) (long-read reader))
;; Does the version look valid? Note that only one valid version number
;; has ever been defined, so we test exactly for that. Yeah, I know...
(unless (= (version reader) +2bit-version+)
(error 'invalid-version :version (version reader)))
;; Finally, load up the sequence count.
(setf (sequence-count reader) (long-read reader))
;; Skip a reserved log value.
(raw-long-read reader)
;; Now load up the index of the file.
(setf (index reader) (read-index reader))
;; Finally, just to allow easy chaining of calls, return the reader
;; object.
reader)
(defmethod close-reader ((reader reader))
"Close READER."
(error 'not-implemented))
(defmethod sequences ((reader reader))
"Returns a list of the names of all sequences found in READER."
(loop for name being the hash-keys of (index reader) collect name))
(defmethod seq ((reader reader) (name string))
"Get the sequence named NAME from READER."
(values (gethash name (index reader))))
(defmethod seq ((reader reader) (name symbol))
"Get the sequence named NAME from READER."
(seq reader (let ((*print-case* :downcase)) (format nil "~S" name))))
(defmethod print-object ((reader reader) stream)
"Format READER for easy reading when output to STREAM."
(print-unreadable-object (reader stream :type t)
(format stream "~S :masking ~S" (source reader) (masking reader))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 2bit data reader class that reads from a local file.
(defclass file-reader (reader)
((file :accessor file))
(:documentation "Class that handles reading from 2bit data held in a local file."))
(defun make-file-reader (source &key (masking t))
"Create a new instance of a 2bit file-reader, reading from SOURCE."
(make-instance 'file-reader :source source :masking masking))
(defmethod pos ((reader file-reader))
"Get the current data position."
(file-position (file reader)))
(defmethod (setf pos) (pos (reader file-reader))
"Set the current data position."
(file-position (file reader) pos))
(defmethod byte-read ((reader file-reader))
"Read a byte from READER."
(read-byte (file reader)))
(defmethod raw-long-read ((reader file-reader))
"Read a long (4-byte) numeric value from READER."
(let ((buffer (make-array 4 :element-type +byte+ :initial-element 0)))
(read-sequence buffer (file reader))
(logior (aref buffer 0)
(ash (aref buffer 1) 8)
(ash (aref buffer 2) 16)
(ash (aref buffer 3) 24))))
(defmethod bytes-read ((reader file-reader) (len integer))
"Read an array of bytes of LEN length from READER."
(let ((buffer (make-array len :element-type +byte+)))
(read-sequence buffer (file reader))
buffer))
(defmethod open-reader :before ((reader file-reader))
"Open READER for further reading."
;; Start out by opening the source file for reading and hanging on to the
;; stream. Note that we don't do any sort of file checks here; we'll
;; simply let the normal file I/O errors bubble up.
(setf (file reader) (open (source reader) :element-type +byte+)))
(defmethod close-reader ((reader file-reader))
"Close READER."
(close (file reader)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; High-level utility stuff.
(defmacro with-2bit-file ((handle file &rest rest) &body body)
"Perform BODY against data from FILE, naming reader as HANDLE."
`(let ((,handle (make-file-reader ,file ,@rest)))
(open-reader ,handle)
(unwind-protect
(progn ,@body)
(close-reader ,handle))))
;; Local Variables:
;; eval: (put 'with-saved-location 'common-lisp-indent-function 1)
;; eval: (put 'with-2bit-file 'common-lisp-indent-function 1)
;; End:
;;; 2bit.lisp ends here