-
Notifications
You must be signed in to change notification settings - Fork 2
/
sam-files.lisp
1193 lines (1099 loc) · 58.6 KB
/
sam-files.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(in-package :elprep)
(in-simple-base-string-syntax)
;;; input
(declaim (inline make-scanner))
(defstruct (scanner (:constructor make-scanner (&optional (string "")))
(:copier nil)
(:predicate nil))
"A scanner to scan/parse string of type simple-base-string.
The struct scanner has a constructor make-scanner that takes an optional string to scan/parse as a parameter.
Accessor scanner-index refers to the current index into the string.
Accessor scanner-string refers to the string being scanned/parsed."
(index 0 :type fixnum)
(string "" :type simple-base-string))
(setf (documentation 'make-scanner 'function)
"Create a scanner to scan/parse strings of type simple-base-string. Takes an optional string to scan/parse as a parameter."
(documentation 'scanner-index 'function)
"The current index into the string while scanning/parsing it."
(documentation 'scanner-string 'function)
"The string being scanned/parsed.")
(declaim (inline reinitialize-scanner))
(defun reinitialize-scanner (scanner string)
"Reinitialize the scanner so it can be reused."
(declare (scanner scanner) (simple-base-string string))
(setf (scanner-index scanner) 0
(scanner-string scanner) string)
scanner)
(declaim (inline advance peekc readc scanc end-of-entry-p))
(defun advance (scanner)
"Advance scanner by one character if possible."
(declare (scanner scanner) #.*optimization*)
(let ((index (scanner-index scanner))
(string (scanner-string scanner)))
(declare (fixnum index) (simple-base-string string))
(when (< index (length string))
(setf (scanner-index scanner) (the fixnum (1+ index))))))
(defun peekc (scanner)
"Peek current character in scanner if possible, or return #\EOT."
(declare (scanner scanner) #.*optimization*)
(let ((index (scanner-index scanner))
(string (scanner-string scanner)))
(declare (fixnum index) (simple-base-string string))
(if (< index (length string))
(schar string index)
#\EOT)))
(defun readc (scanner)
"Read current character in scanner if possible, or return #\EOT."
(declare (scanner scanner) #.*optimization*)
(let ((index (scanner-index scanner))
(string (scanner-string scanner)))
(declare (fixnum index) (simple-base-string string))
(if (< index (length string))
(prog1 (schar string index)
(setf (scanner-index scanner) (the fixnum (1+ index))))
#\EOT)))
(defun scanc (scanner char)
"Read current character in scanner, ensuring it is the given one."
(declare (scanner scanner) (base-char char) #.*optimization*)
(assert (char= (readc scanner) char))
char)
(defun end-of-entry-p (char)
"Is the character a tab or end of line, delimiting an entry in a tab-delimited text file?"
(declare (base-char) #.*optimization*)
(or (char= char #\Tab)
(char= char #\EOT)))
(defun scan-string (scanner)
"Read a tab-delimited entry as string from scanner."
(declare (scanner scanner) #.*optimization*)
(loop with index of-type fixnum = (scanner-index scanner)
with string of-type simple-base-string = (scanner-string scanner)
for end of-type fixnum from index below (length string)
until (char= (schar string end) #\Tab) finally
(let ((length (- end index)))
(declare (fixnum length))
(let ((result (if (> length 24)
(make-array length :element-type 'base-char
:displaced-to string
:displaced-index-offset (scanner-index scanner))
(let ((result (make-array length :element-type 'base-char)))
(declare (simple-base-string result))
(loop for j of-type fixnum from index below end
for i of-type fixnum from 0
do (setf (schar result i) (schar string j)))
result))))
(setf (scanner-index scanner) end)
(return result)))))
(defun scan-integer (scanner)
"Scan an integer from scanner."
(declare (scanner scanner) #.*optimization*)
(let ((index (scanner-index scanner))
(string (scanner-string scanner)))
(declare (fixnum index) (simple-base-string string))
(let* ((pos index) (char (schar string pos)))
(declare (fixnum pos) (base-char char))
(flet ((nextc () (if (= (incf pos) (length string))
(setq char #\EOT)
(setq char (schar string pos)))))
(declare (inline nextc))
(when (or (char= char #\-)
(char= char #\+))
(nextc))
(assert (and (char<= #\0 char) (char<= char #\9)))
(loop do (nextc) while (and (char<= #\0 char) (char<= char #\9))))
(setf (scanner-index scanner) pos)
(parse-integer string :start index :end pos))))
(defun scan-float (scanner)
"Scan a floating point number from scanner. Returns either an integer or a single-float."
(declare (scanner scanner) #.*optimization*)
(let ((index (scanner-index scanner))
(string (scanner-string scanner)))
(declare (fixnum index) (simple-base-string string))
(let* ((pos index) (char (schar string pos)))
(declare (fixnum pos) (base-char char))
(flet ((nextc () (if (= (incf pos) (length string))
(setq char #\EOT)
(setq char (schar string pos)))))
(declare (inline nextc))
(when (or (char= char #\-)
(char= char #\+))
(nextc))
(cond ((and (char<= #\0 char) (char<= char #\9))
(loop do (nextc) while (and (char<= #\0 char) (char<= char #\9)))
(when (char= char #\.)
(nextc)
(assert (and (char<= #\0 char) (char<= char #\9)))
(loop do (nextc) while (and (char<= #\0 char) (char<= char #\9)))))
(t (when (char= char #\.)
(nextc))
(assert (and (char<= #\0 char) (char<= char #\9)))
(loop do (nextc) while (and (char<= #\0 char) (char<= char #\9)))))
(when (or (char= char #\e)
(char= char #\E))
(nextc)
(when (or (char= char #\-)
(char= char #\+))
(nextc))
(assert (and (char<= #\0 char) (char<= char #\9)))
(loop do (nextc) while (and (char<= #\0 char) (char<= char #\9)))))
(setf (scanner-index scanner) pos)
(let ((*read-base* 10) (*read-default-float-format* 'single-float) (*read-eval* nil))
(let ((value (read-from-string string t nil :start index :end pos)))
(etypecase value
(integer value)
(single-float value)
(number (coerce value 'single-float))))))))
(defun parse-sam-tag (scanner &optional (tag-string (make-array 2 :element-type 'base-char)))
"Parse the TAG: portion of a SAM file tag."
(declare (scanner scanner) (simple-base-string tag-string) #.*optimization*)
(let ((index (scanner-index scanner))
(string (scanner-string scanner)))
(declare (fixnum index) (simple-base-string string))
(assert (<= index (- (length string) 3)))
(setf (schar tag-string 0) (schar string index))
(setf (schar tag-string 1) (schar string (the fixnum (+ index 1))))
(assert (char= (schar string (the fixnum (+ index 2))) #\:))
(setf (scanner-index scanner) (the fixnum (+ index 3)))
tag-string))
(defun parse-sam-byte-array (scanner)
"Parse a byte array in a SAM file.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.5, Type H."
(declare (scanner scanner) #.*fixnum-optimization*)
(flet ((hex-value (char)
(declare (base-char char))
(cond ((and (char<= #\0 char) (char<= char #\9))
(- (char-int char) #.(char-int #\0)))
((and (char<= #\a char) (char<= char #\f))
(- (char-int char) #.(- (char-int #\a) 10)))
((and (char<= #\A char) (char<= char #\F))
(- (char-int char) #.(- (char-int #\A) 10)))
(t (error "Not a hex digit ~S in ~S." char scanner)))))
(declare (inline hex-value))
(loop for count of-type fixnum from 1
collect (+ (ash (hex-value (readc scanner)) 4)
(hex-value (readc scanner)))
into list until (end-of-entry-p (peekc scanner))
finally (return (make-array count :element-type 'octet :initial-contents list)))))
(defun parse-sam-numeric-array (scanner)
"Parse a numeric array in a SAM file.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.5, Type B."
(declare (scanner scanner) #.*optimization*)
(advance scanner) ;; ignore type tag
(loop do (scanc scanner #\,)
collect (scan-float scanner)
until (end-of-entry-p (peekc scanner))))
(defun parse-sam-header-line (scanner)
"Parse an @HD line in a SAM file.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (scanner scanner) #.*optimization*)
(let ((tag (make-array 2 :element-type 'base-char)))
(declare (simple-base-string tag) (dynamic-extent tag))
(loop until (char= (peekc scanner) #\EOT)
nconc (progn
(scanc scanner #\Tab)
(parse-sam-tag scanner tag)
(string-case (tag :default (if (sam-header-user-tag-p tag)
(list (unique (intern-key/copy tag) record) (scan-string scanner))
(error "Unknown tag ~A in SAM header line ~S." tag scanner)))
("VN" (list (unique :VN record) (scan-string scanner)))
("SO" (list (unique :SO record) (scan-string scanner)))
("GO" (list (unique :GO record) (scan-string scanner)))))
into record finally
(advance scanner)
(unless (presentp :VN record)
(cerror "Ignore absence of VN tag and continue."
"VN tag missing in @HD line when reading ~S." scanner))
(return record))))
(defun parse-sam-reference-sequence-dictionary-entry (scanner)
"Parse an @SQ line in a SAM file.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (scanner scanner) #.*optimization*)
(let ((tag (make-array 2 :element-type 'base-char)))
(declare (simple-base-string tag) (dynamic-extent tag))
(loop until (char= (peekc scanner) #\EOT)
nconc (progn
(scanc scanner #\Tab)
(parse-sam-tag scanner tag)
(string-case (tag :default (if (sam-header-user-tag-p tag)
(list (unique (intern-key/copy tag) record) (scan-string scanner))
(error "Unknown tag ~A in SAM reference sequence dictionary line ~S." tag scanner)))
("SN" (list (unique :SN record) (scan-string scanner)))
("LN" (list (unique :LN record) (scan-integer scanner)))
("AS" (list (unique :AS record) (scan-string scanner)))
("M5" (list (unique :M5 record) (parse-sam-byte-array scanner)))
("SP" (list (unique :SP record) (scan-string scanner)))
("UR" (list (unique :UR record) (scan-string scanner)))))
into record finally
(advance scanner)
(unless (presentp :SN record)
(cerror "Ignore absence of SN tag and continue."
"SN tag missing in @SQ line when reading ~S." scanner))
(unless (presentp :LN record)
(cerror "Ignore absence of LN tag and continue."
"LN tag missing in @SQ line when reading ~S." scanner))
(return record))))
(defun parse-sam-read-group (scanner)
"Parse an @RG line in a SAM file.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (scanner scanner) #.*optimization*)
(let ((tag (make-array 2 :element-type 'base-char)))
(declare (simple-base-string tag) (dynamic-extent tag))
(loop until (char= (peekc scanner) #\EOT)
nconc (progn
(scanc scanner #\Tab)
(parse-sam-tag scanner tag)
(string-case (tag :default (if (sam-header-user-tag-p tag)
(list (unique (intern-key/copy tag) record) (scan-string scanner))
(error "Unknown tag ~A in SAM read group line ~S." tag scanner)))
("ID" (list (unique :ID record) (scan-string scanner)))
("CN" (list (unique :CN record) (scan-string scanner)))
("DS" (list (unique :DS record) (scan-string scanner)))
("DT" (list (unique :DT record) (parse-date-time (scan-string scanner))))
("FO" (list (unique :FO record) (scan-string scanner)))
("KS" (list (unique :KS record) (scan-string scanner)))
("LB" (list (unique :LB record) (scan-string scanner)))
("PG" (list (unique :PG record) (scan-string scanner)))
("PI" (list (unique :PI record) (scan-integer scanner)))
("PL" (list (unique :PL record) (scan-string scanner)))
("PU" (list (unique :PU record) (scan-string scanner)))
("SM" (list (unique :SM record) (scan-string scanner)))))
into record finally
(advance scanner)
(unless (presentp :ID record)
(cerror "Ignore absence of ID tag and continue."
"ID tag missing in @RG line when reading ~S." scanner))
(return record))))
(defun parse-sam-program (scanner)
"Parse an @PG line in a SAM file.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (scanner scanner) #.*optimization*)
(let ((tag (make-array 2 :element-type 'base-char)))
(declare (simple-base-string tag) (dynamic-extent tag))
(loop until (char= (peekc scanner) #\EOT)
nconc (progn
(scanc scanner #\Tab)
(parse-sam-tag scanner tag)
(string-case (tag :default (if (sam-header-user-tag-p tag)
(list (unique (intern-key/copy tag) record) (scan-string scanner))
(error "Unknown tag ~A in SAM program line ~S." tag scanner)))
("ID" (list (unique :ID record) (scan-string scanner)))
("PN" (list (unique :PN record) (scan-string scanner)))
("CL" (list (unique :CL record) (scan-string scanner)))
("PP" (list (unique :PP record) (scan-string scanner)))
("DS" (list (unique :DS record) (scan-string scanner)))
("VN" (list (unique :VN record) (scan-string scanner)))))
into record finally
(advance scanner)
(unless (presentp :ID record)
(cerror "Ignore absence of ID tag and continue."
"ID tag missing in @PG line when reading ~S." scanner))
(return record))))
(defun parse-sam-user-tag (scanner)
"Parse a user tag line in a SAM file.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (scanner scanner) #.*optimization*)
(let ((tag (make-array 2 :element-type 'base-char)))
(declare (simple-base-string tag) (dynamic-extent tag))
(loop until (char= (peekc scanner) #\EOT)
nconc (progn
(scanc scanner #\Tab)
(parse-sam-tag scanner tag)
(list (unique (intern-key/copy tag) record) (scan-string scanner)))
into record finally
(advance scanner)
(return record))))
(defun parse-sam-comment (scanner)
"Parse an @CO line in a SAM file.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (scanner scanner) #.*optimization*)
(let ((index (scanner-index scanner))
(string (scanner-string scanner)))
(declare (fixnum index) (simple-base-string string))
(when (char= (schar string index) #\Tab)
(incf index))
(let ((length (the fixnum (- (length string) index))))
(declare (fixnum length))
(let ((result (if (> length 24)
(make-array length :element-type 'base-char
:displaced-to string
:displaced-index-offset index)
(let ((result (make-array (- (length string) index) :element-type 'base-char)))
(declare (simple-base-string result))
(loop for j of-type fixnum from index below (length string)
for i of-type fixnum from 0
do (setf (schar result i) (schar string j)))
result))))
(setf (scanner-index scanner) (length string))
result))))
(defun parse-sam-header-code (scanner &optional (code-string (make-array 3 :element-type 'base-char)))
"Parse the record type code of a SAM header line."
(declare (scanner scanner) (simple-base-string code-string) #.*optimization*)
(let ((index (scanner-index scanner))
(string (scanner-string scanner)))
(declare (fixnum index) (simple-base-string string))
(assert (<= index (- (length string) 3)))
(setf (schar code-string 0) (schar string index))
(setf (schar code-string 1) (schar string (the fixnum (+ index 1))))
(setf (schar code-string 2) (schar string (the fixnum (+ index 2))))
(setf (scanner-index scanner) (the fixnum (+ index 3)))
code-string))
(defun parse-sam-header (stream)
"Parse a SAM file header section.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare #.*optimization*)
(let ((scanner (make-scanner))
(code (make-array 3 :element-type 'base-char))
hd sq rg pg co user-tags)
(declare (scanner scanner) (simple-base-string code) (dynamic-extent scanner code))
(loop while (char= (the base-char (peek-char nil stream nil #\EOT)) #\@) do
(reinitialize-scanner scanner (read-line stream))
(parse-sam-header-code scanner code)
(string-case (code :default (if (sam-header-user-tag-p code)
(push (parse-sam-user-tag scanner) (getf user-tags (intern-key/copy code)))
(error "Unknown SAM record type code ~A in header line ~S." code scanner)))
("@HD" (progn
(assert (null hd))
(setq hd (parse-sam-header-line scanner))))
("@SQ" (push (parse-sam-reference-sequence-dictionary-entry scanner) sq))
("@RG" (push (parse-sam-read-group scanner) rg))
("@PG" (push (parse-sam-program scanner) pg))
("@CO" (push (parse-sam-comment scanner) co)))
finally (return (make-sam-header
:hd hd
:sq (nreverse sq)
:rg (nreverse rg)
:pg (nreverse pg)
:co (nreverse co)
:user-tags (loop for cons on user-tags by #'cddr
do (setf (cadr cons) (nreverse (cadr cons)))
finally (return user-tags)))))))
(declaim (inline skip-sam-header))
(defun skip-sam-header (stream)
"Skip the SAM file header section."
(declare #.*optimization*)
(loop while (char= (the base-char (peek-char nil stream nil #\EOT)) #\@) do (skip-line stream))
(values))
(define-symbol-macro optional-field-type-tags "AifZHB")
(defconstant +min-optional-field-type-tag+ (reduce #'min optional-field-type-tags :key #'char-code)
"The smallest optional field type tag.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.5.")
(defconstant +max-optional-field-type-tag+ (reduce #'max optional-field-type-tags :key #'char-code)
"The largest optional field type tag.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.5.")
(defun scan-error (scanner)
"Signal invalid type tag for optional field in SAM alignment."
(declare (scanner scanner))
(error "Invalid type tag for optional field in SAM alignment ~S." scanner))
(defun make-optional-field-scan-table ()
"Create a dispatch table for scanning optional fields in a SAM file read alignment line.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.5."
(let ((table (make-array (1+ (- +max-optional-field-type-tag+
+min-optional-field-type-tag+))
:initial-element #'scan-error
#+lispworks :allocation #+lispworks :long-lived
#+lispworks :single-thread #+lispworks t)))
(flet ((s (index value) (setf (svref table (- (char-code index) +min-optional-field-type-tag+)) value)))
(s #\A #'readc)
(s #\i #'scan-integer)
(s #\f #'scan-float)
(s #\Z #'scan-string)
(s #\H #'parse-sam-byte-array)
(s #\B #'parse-sam-numeric-array))
table))
(declaim (notinline parse-sam-alignment))
(defun parse-sam-alignment (string)
"Parse a SAM file read alignment line.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.4."
(declare (simple-base-string string) #.*optimization*)
(let ((scanner (make-scanner string)))
(declare (scanner scanner) (dynamic-extent scanner))
(flet ((do-stringn ()
(scan-string scanner))
(do-string ()
(prog1 (scan-string scanner)
(scanc scanner #\Tab)))
(do-int32 ()
(prog1 (scan-integer scanner)
(scanc scanner #\Tab))))
(declare (inline do-stringn do-string do-int32))
(make-sam-alignment
:qname (do-string)
:flag (do-int32)
:rname (do-string)
:pos (do-int32)
:mapq (do-int32)
:cigar (do-string)
:rnext (do-string)
:pnext (do-int32)
:tlen (do-int32)
:seq (do-string)
:qual (do-stringn)
:tags (let ((tag-string (make-array 2 :element-type 'base-char)))
(declare (simple-base-string tag-string) (dynamic-extent tag-string))
(loop until (char= (peekc scanner) #\EOT)
nconc (progn
(scanc scanner #\Tab)
(let ((tag (intern-key/copy (parse-sam-tag scanner tag-string)))
(type (readc scanner)))
(declare (symbol tag) (base-char type))
(scanc scanner #\:)
(list tag (funcall (the function (svref (load-time-value (make-optional-field-scan-table) t)
(- (char-code type) +min-optional-field-type-tag+)))
scanner))))))))))
(defun parse-sam (stream)
"Parse a complete SAM file.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1."
(make-sam :header (parse-sam-header stream)
:alignments (loop for line = (read-line stream nil)
while line collect (parse-sam-alignment line))))
;;; output
(defun format-sam-string (out tag string)
"Write a SAM file TAG of type string."
(declare (stream out) (string tag string) #.*optimization*)
(write-tab out)
(writestr out tag)
(writec out #\:)
(writestr out string))
(defun sim-format-sam-string (out tag string)
"Write a SAM file TAG of type string."
(declare (sim-stream out) (string tag string) #.*optimization*)
(sim-write-tab out)
(sim-writestr out tag)
(sim-writec out #\:)
(sim-writestr out string))
(defun format-sam-integer (out tag value)
"Write a SAM file TAG of type integer."
(declare (stream out) (base-string tag) (integer value) #.*optimization*)
(write-tab out)
(writestr out tag)
(format out ":~D" value))
(defun sim-format-sam-integer (out tag value)
"Write a SAM file TAG of type integer."
(declare (sim-stream out) (base-string tag) (integer value) #.*optimization*)
(sim-write-tab out)
(sim-writestr out tag)
(sim-write-integer out value))
(defun format-sam-byte-array (out tag byte-array)
"Write a SAM file TAG of type byte array.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.5, Type H."
(declare (stream out) (base-string tag) (sequence byte-array) #.*optimization*)
(write-tab out)
(writestr out tag)
(writec out #\:)
(etypecase byte-array
(list (loop for byte in (the list byte-array)
do (format out "~2,'0X" byte)))
(vector (loop for byte across (the vector byte-array)
do (format out "~2,'0X" byte)))))
(defun sim-format-sam-byte-array (out tag byte-array)
"Write a SAM file TAG of type byte array.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.5, Type H."
(declare (sim-stream out) (base-string tag) (sequence byte-array) #.*optimization*)
(sim-write-tab out)
(sim-writestr out tag)
(sim-writec out #\:)
(etypecase byte-array
(list (loop for byte in (the list byte-array)
do (sim-write-byte out byte)))
(vector (loop for byte across (the vector byte-array)
do (sim-write-byte out byte)))))
(defun format-sam-datetime (out tag datetime)
"Write a SAM file TAG of type date/time.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3, Tag @RG, DT."
(declare (stream out) (base-string tag) (integer datetime) #.*optimization*)
(write-tab out)
(writestr out tag)
(multiple-value-bind
(sec min hour day month year)
(decode-universal-time datetime)
(format out ":~4,'0D-~2,'0D-~2,'0DT~2,'0D:~2,'0D:~2,'0DZ"
year month day hour min sec)))
(defun sim-format-sam-datetime (out tag datetime)
"Write a SAM file TAG of type date/time.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3, Tag @RG, DT."
(declare (sim-stream out) (base-string tag) (integer datetime) #.*optimization*)
(sim-write-tab out)
(sim-writestr out tag)
(sim-writec out #\:)
(multiple-value-bind
(sec min hour day month year)
(decode-universal-time datetime)
(sim-write-fixed-size-fixnum out year 4)
(sim-writec out #\-)
(sim-write-fixed-size-fixnum out month 2)
(sim-writec out #\-)
(sim-write-fixed-size-fixnum out day 2)
(sim-writec out #\T)
(sim-write-fixed-size-fixnum out hour 2)
(sim-writec out #\:)
(sim-write-fixed-size-fixnum out min 2)
(sim-writec out #\:)
(sim-write-fixed-size-fixnum out sec 2)
(sim-writec out #\Z)))
(defun format-sam-header-user-tag (out tag value)
"Write a user-defined SAM file TAG of type string."
(declare (stream out) (symbol tag) #.*optimization*)
(let ((tag-string (symbol-name tag)))
(if (sam-header-user-tag-p tag-string)
(format-sam-string out tag-string value)
(error "Unknown tag ~A in a SAM header line." tag))))
(defun sim-format-sam-header-user-tag (out tag value)
"Write a user-defined SAM file TAG of type string."
(declare (sim-stream out) (symbol tag) #.*optimization*)
(let ((tag-string (symbol-name tag)))
(if (sam-header-user-tag-p tag-string)
(sim-format-sam-string out tag-string value)
(error "Unknown tag ~A in a SAM header line." tag))))
(defun format-sam-header-line (out list)
"Write an @HD line in a SAM file header section.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (stream out) (list list) #.*optimization*)
(when list
(unless (presentp :VN list)
(cerror "Ignore absence of VN tag and continue."
"VN tag missing in @HD line when writing ~A." out))
(writestr out "@HD")
(loop for (tag value) of-type (symbol t) on list by #'cddr do
(case tag
(:VN (format-sam-string out "VN" value))
(:SO (format-sam-string out "SO" value))
(:GO (format-sam-string out "GO" value))
(t (format-sam-header-user-tag out tag value))))
(write-newline out)))
(defun sim-format-sam-header-line (out list)
"Write an @HD line in a SAM file header section.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (sim-stream out) (list list) #.*optimization*)
(when list
(unless (presentp :VN list)
(cerror "Ignore absence of VN tag and continue."
"VN tag missing in @HD line when writing ~A." out))
(sim-writestr out "@HD")
(loop for (tag value) of-type (symbol t) on list by #'cddr do
(case tag
(:VN (sim-format-sam-string out "VN" value))
(:SO (sim-format-sam-string out "SO" value))
(:GO (sim-format-sam-string out "GO" value))
(t (sim-format-sam-header-user-tag out tag value))))
(sim-write-newline out)))
(defun format-sam-reference-sequence-dictionary (out list)
"Write the @SQ lines in a SAM file header section.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (stream out) (list list) #.*optimization*)
(loop for plist in list do
(unless (presentp :SN plist)
(cerror "Ignore absence of SN tag and continue."
"SN tag missing in @SQ line when writing ~A." out))
(unless (presentp :LN plist)
(cerror "Ignore absence of LN tag and continue."
"LN tag missing in @SQ line when writing ~A." out))
(writestr out "@SQ")
(loop for (tag value) on plist by #'cddr do
(case tag
(:SN (format-sam-string out "SN" value))
(:LN (format-sam-integer out "LN" value))
(:AS (format-sam-string out "AS" value))
(:M5 (format-sam-byte-array out "M5" value))
(:SP (format-sam-string out "SP" value))
(:UR (format-sam-string out "UR" value))
(t (format-sam-header-user-tag out tag value))))
(write-newline out)))
(defun sim-format-sam-reference-sequence-dictionary (out list)
"Write the @SQ lines in a SAM file header section.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (sim-stream out) (list list) #.*optimization*)
(loop for plist in list do
(unless (presentp :SN plist)
(cerror "Ignore absence of SN tag and continue."
"SN tag missing in @SQ line when writing ~A." out))
(unless (presentp :LN plist)
(cerror "Ignore absence of LN tag and continue."
"LN tag missing in @SQ line when writing ~A." out))
(sim-writestr out "@SQ")
(loop for (tag value) on plist by #'cddr do
(case tag
(:SN (sim-format-sam-string out "SN" value))
(:LN (sim-format-sam-integer out "LN" value))
(:AS (sim-format-sam-string out "AS" value))
(:M5 (sim-format-sam-byte-array out "M5" value))
(:SP (sim-format-sam-string out "SP" value))
(:UR (sim-format-sam-string out "UR" value))
(t (sim-format-sam-header-user-tag out tag value))))
(sim-write-newline out)))
(defun format-sam-read-groups (out list)
"Write the @RG lines in a SAM file header section.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (stream out) (list list) #.*optimization*)
(loop for plist in list do
(unless (presentp :ID plist)
(cerror "Ignore absence of ID tag and continue."
"ID tag missing in @RG line when writing ~A." out))
(writestr out "@RG")
(loop for (tag value) on plist by #'cddr do
(case tag
(:ID (format-sam-string out "ID" value))
(:CN (format-sam-string out "CN" value))
(:DS (format-sam-string out "DS" value))
(:DT (format-sam-datetime out "DT" value))
(:FO (format-sam-string out "FO" value))
(:KS (format-sam-string out "KS" value))
(:LB (format-sam-string out "LB" value))
(:PG (format-sam-string out "PG" value))
(:PI (format-sam-integer out "PI" value))
(:PL (format-sam-string out "PL" value))
(:PU (format-sam-string out "PU" value))
(:SM (format-sam-string out "SM" value))
(t (format-sam-header-user-tag out tag value))))
(write-newline out)))
(defun sim-format-sam-read-groups (out list)
"Write the @RG lines in a SAM file header section.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (sim-stream out) (list list) #.*optimization*)
(loop for plist in list do
(unless (presentp :ID plist)
(cerror "Ignore absence of ID tag and continue."
"ID tag missing in @RG line when writing ~A." out))
(sim-writestr out "@RG")
(loop for (tag value) on plist by #'cddr do
(case tag
(:ID (sim-format-sam-string out "ID" value))
(:CN (sim-format-sam-string out "CN" value))
(:DS (sim-format-sam-string out "DS" value))
(:DT (sim-format-sam-datetime out "DT" value))
(:FO (sim-format-sam-string out "FO" value))
(:KS (sim-format-sam-string out "KS" value))
(:LB (sim-format-sam-string out "LB" value))
(:PG (sim-format-sam-string out "PG" value))
(:PI (sim-format-sam-integer out "PI" value))
(:PL (sim-format-sam-string out "PL" value))
(:PU (sim-format-sam-string out "PU" value))
(:SM (sim-format-sam-string out "SM" value))
(t (sim-format-sam-header-user-tag out tag value))))
(sim-write-newline out)))
(defun format-sam-programs (out list)
"Write the @PG lines in a SAM file header section.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (stream out) (list list) #.*optimization*)
(loop for plist in list do
(unless (presentp :ID plist)
(cerror "Ignore absence of ID tag and continue."
"ID tag missing in @PG line when writing ~A." out))
(writestr out "@PG")
(loop for (tag value) on plist by #'cddr do
(case tag
(:ID (format-sam-string out "ID" value))
(:PN (format-sam-string out "PN" value))
(:CL (format-sam-string out "CL" value))
(:PP (format-sam-string out "PP" value))
(:DS (format-sam-string out "DS" value))
(:VN (format-sam-string out "VN" value))
(t (format-sam-header-user-tag out tag value))))
(write-newline out)))
(defun sim-format-sam-programs (out list)
"Write the @PG lines in a SAM file header section.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (sim-stream out) (list list) #.*optimization*)
(loop for plist in list do
(unless (presentp :ID plist)
(cerror "Ignore absence of ID tag and continue."
"ID tag missing in @PG line when writing ~A." out))
(sim-writestr out "@PG")
(loop for (tag value) on plist by #'cddr do
(case tag
(:ID (sim-format-sam-string out "ID" value))
(:PN (sim-format-sam-string out "PN" value))
(:CL (sim-format-sam-string out "CL" value))
(:PP (sim-format-sam-string out "PP" value))
(:DS (sim-format-sam-string out "DS" value))
(:VN (sim-format-sam-string out "VN" value))
(t (sim-format-sam-header-user-tag out tag value))))
(sim-write-newline out)))
(defun format-sam-comments (out list)
"Write the @CO lines in a SAM file header section.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (stream out) (list list) #.*optimization*)
(loop for string in list do
(writestr out "@CO")
(write-tab out)
(writestr out string)
(write-newline out)))
(defun sim-format-sam-comments (out list)
"Write the @CO lines in a SAM file header section.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (sim-stream out) (list list) #.*optimization*)
(loop for string in list do
(sim-writestr out "@CO")
(sim-write-tab out)
(sim-writestr out string)
(sim-write-newline out)))
(defun format-sam-user-tags (out tags)
"Write the user-defined header lines.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (stream out) (list tags) #.*optimization*)
(loop for (code list) of-type (symbol list) on tags by #'cddr
for code-string = (symbol-name code) do
(loop for plist of-type list in list do
(writestr out code-string)
(loop for (tag value) on plist by #'cddr do
(format-sam-string out (symbol-name tag) value))
(write-newline out))))
(defun sim-format-sam-user-tags (out tags)
"Write the user-defined header lines.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (sim-stream out) (list tags) #.*optimization*)
(loop for (code list) of-type (symbol list) on tags by #'cddr
for code-string = (symbol-name code) do
(loop for plist of-type list in list do
(sim-writestr out code-string)
(loop for (tag value) on plist by #'cddr do
(sim-format-sam-string out (symbol-name tag) value))
(sim-write-newline out))))
(defun format-sam-header (out header)
"Write a SAM file header section.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (stream out) (sam-header header) #.*optimization*)
(format-sam-header-line out (sam-header-hd header))
(format-sam-reference-sequence-dictionary out (sam-header-sq header))
(format-sam-read-groups out (sam-header-rg header))
(format-sam-programs out (sam-header-pg header))
(format-sam-comments out (sam-header-co header))
(format-sam-user-tags out (sam-header-user-tags header)))
(defun sim-format-sam-header (out header)
"Write a SAM file header section.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.3."
(declare (sim-stream out) (sam-header header) #.*optimization*)
(sim-format-sam-header-line out (sam-header-hd header))
(sim-format-sam-reference-sequence-dictionary out (sam-header-sq header))
(sim-format-sam-read-groups out (sam-header-rg header))
(sim-format-sam-programs out (sam-header-pg header))
(sim-format-sam-comments out (sam-header-co header))
(sim-format-sam-user-tags out (sam-header-user-tags header)))
(eval-when (:compile-toplevel :execute)
(define-symbol-macro int8 #\c)
(define-symbol-macro uint8 #\C)
(define-symbol-macro int16 #\s)
(define-symbol-macro uint16 #\S)
(define-symbol-macro int32 #\i)
(define-symbol-macro uint32 #\I)
(define-symbol-macro num #\f)
(defvar *integer-types*
(list int8 uint8 int16 uint16 int32 uint32)
"Integer types supported in SAM file numeric arrays.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.5, Type B.")
(defun make-integer-type-descriptors (&rest types)
"Create descriptors for the integer types in SAM file numeric arrays.
Types with smaller indexes are preferred over types with larger indexes.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.5, Type B."
(declare (dynamic-extent types))
(loop for type in *integer-types*
for index from 0
when (member type types :test #'char=)
collect (cons type index))))
(declaim (inline common-number-type))
(defun common-number-type (numbers)
"Determine the smallest numeric type supported by SAM file numeric arrays that can represent all numbers in the given list.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.5, Type B."
(declare (list numbers) #.*optimization*)
(flet ((integer-types (number)
(declare (number number))
(etypecase number
(float '())
(fixnum (locally (declare (fixnum number))
(cond ((< number #.(- (expt 2 31))) '())
((> number #.(1- (expt 2 32))) '())
((> number #.(1- (expt 2 31))) '#.(make-integer-type-descriptors uint32))
((< number #.(- (expt 2 15))) '#.(make-integer-type-descriptors int32))
((> number #.(1- (expt 2 16))) '#.(make-integer-type-descriptors int32 uint32))
((> number #.(1- (expt 2 15))) '#.(make-integer-type-descriptors uint16 int32 uint32))
((< number #.(- (expt 2 7))) '#.(make-integer-type-descriptors int16 int32))
((> number #.(1- (expt 2 8))) '#.(make-integer-type-descriptors int16 uint16 int32 uint32))
((> number #.(1- (expt 2 7))) '#.(make-integer-type-descriptors uint8 int16 uint16 int32 uint32))
((< number 0) '#.(make-integer-type-descriptors int8 int16 int32))
(t '#.(make-integer-type-descriptors int8 uint8 int16 uint16 int32 uint32)))))
(integer (locally (declare (integer number))
(cond ((< number #.(- (expt 2 31))) '())
((> number #.(1- (expt 2 32))) '())
((> number #.(1- (expt 2 31))) '#.(make-integer-type-descriptors uint32))
((< number #.(- (expt 2 15))) '#.(make-integer-type-descriptors int32))
((> number #.(1- (expt 2 16))) '#.(make-integer-type-descriptors int32 uint32))
((> number #.(1- (expt 2 15))) '#.(make-integer-type-descriptors uint16 int32 uint32))
((< number #.(- (expt 2 7))) '#.(make-integer-type-descriptors int16 int32))
((> number #.(1- (expt 2 8))) '#.(make-integer-type-descriptors int16 uint16 int32 uint32))
((> number #.(1- (expt 2 7))) '#.(make-integer-type-descriptors uint8 int16 uint16 int32 uint32))
((< number 0) '#.(make-integer-type-descriptors int8 int16 int32))
(t '#.(make-integer-type-descriptors int8 uint8 int16 uint16 int32 uint32))))))))
(loop for number in numbers
for types = (copy-list (integer-types number))
then (nintersection types (integer-types number) :key #'car :test #'char=)
unless types return num
finally (return (caar (stable-sort types #'< :key #'cdr))))))
(defun format-sam-tag (out tag value)
"Write a SAM file TAG, dispatching on actual type of the given value.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.5."
(declare (stream out) (symbol tag) #.*optimization*)
(write-tab out)
(writestr out (symbol-name tag))
(etypecase value
(character (writestr out ":A:") (writec out value)) ; printable character
(int32 (format out ":i:~D" value)) ; signed 32-bit integer
(integer (format out ":f:~D" value)) ; single-precision floating point number
(single-float (format out ":f:~E" value)) ; single-precision floating point number
(double-float (format out ":f:~E" (coerce value 'single-float))) ; single-precision floating point number
(string (writestr out ":Z:") ; printable string
(writestr out value))
(vector (writestr out ":H:") ; byte array in hex format
(loop for byte across (the vector value)
do (format out "~2,'0X" byte)))
(list (writestr out ":B:") ; integer or numeric array
(let ((type (common-number-type value)))
(writec out type)
(if (char= type num)
(loop for number in value do
(if (integerp number) (format out ",~D" number)
(format out ",~E" (coerce number 'single-float))))
(loop for number in value do (format out ",~D" number)))))))
(defun sim-format-sam-tag (out tag value)
"Write a SAM file TAG, dispatching on actual type of the given value.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1.5."
(declare (sim-stream out) (symbol tag) #.*optimization*)
(sim-write-tab out)
(sim-writestr out (symbol-name tag))
(etypecase value
(character (sim-writestr out ":A:") (sim-writec out value)) ; printable character
(int32 (sim-writestr out ":i:") (sim-write-integer out value)) ; signed 32-bit integer
(integer (sim-writestr out ":f:") (sim-write-integer out value)) ; single-precision floating point number
(single-float (sim-writestr out ":f:") (sim-write-float out value)) ; single-precision floating point number
(double-float (sim-writestr out ":f:") (sim-write-float out (coerce value 'single-float))) ; single-precision floating point number
(string (sim-writestr out ":Z:") (sim-writestr out value)) ; printable string
(vector (sim-writestr out ":H:") ; byte array in hex format
(loop for byte across (the vector value)
do (sim-write-byte out byte)))
(list (sim-writestr out ":B:") ; integer or numeric array
(let ((type (common-number-type value)))
(sim-writec out type)
(if (char= type num)
(loop for number in value do
(sim-writec out #\,)
(if (integerp number) (sim-write-integer out number)
(sim-write-float out (coerce number 'single-float))))
(loop for number in value do
(sim-writec out #\,)
(sim-write-integer out number)))))))
(defun format-sam-alignment (out aln)
"Write a SAM file read alignment line.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Sections 1.4 and 1.5."
(declare (stream out) (sam-alignment aln) #.*optimization*)
(writestr out (sam-alignment-qname aln)) (write-tab out)
(format out "~D" (sam-alignment-flag aln)) (write-tab out)
(writestr out (sam-alignment-rname aln)) (write-tab out)
(format out "~D" (sam-alignment-pos aln)) (write-tab out)
(format out "~D" (sam-alignment-mapq aln)) (write-tab out)
(writestr out (sam-alignment-cigar aln)) (write-tab out)
(writestr out (sam-alignment-rnext aln)) (write-tab out)
(format out "~D" (sam-alignment-pnext aln)) (write-tab out)
(format out "~D" (sam-alignment-tlen aln)) (write-tab out)
(writestr out (sam-alignment-seq aln)) (write-tab out)
(writestr out (sam-alignment-qual aln))
(loop for (key value) on (sam-alignment-tags aln) by #'cddr
do (format-sam-tag out key value))
(write-newline out))
(defun sim-format-sam-alignment (out aln)
"Write a SAM file read alignment line.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Sections 1.4 and 1.5."
(declare (sim-stream out) (sam-alignment aln) #.*optimization*)
(sim-writestr out (sam-alignment-qname aln)) (sim-write-tab out)
(sim-write-integer out (sam-alignment-flag aln)) (sim-write-tab out)
(sim-writestr out (sam-alignment-rname aln)) (sim-write-tab out)
(sim-write-integer out (sam-alignment-pos aln)) (sim-write-tab out)
(sim-write-integer out (sam-alignment-mapq aln)) (sim-write-tab out)
(sim-writestr out (sam-alignment-cigar aln)) (sim-write-tab out)
(sim-writestr out (sam-alignment-rnext aln)) (sim-write-tab out)
(sim-write-integer out (sam-alignment-pnext aln)) (sim-write-tab out)
(sim-write-integer out (sam-alignment-tlen aln)) (sim-write-tab out)
(sim-writestr out (sam-alignment-seq aln)) (sim-write-tab out)
(sim-writestr out (sam-alignment-qual aln))
(loop for (key value) on (sam-alignment-tags aln) by #'cddr
do (sim-format-sam-tag out key value))
(sim-write-newline out))
(defun format-sam (out sam)
"Write a complete SAM file.
See http://samtools.github.io/hts-specs/SAMv1.pdf - Section 1."
(declare (stream out) (sam sam) #.*optimization*)
(format-sam-header out (sam-header sam))
(do-sam-alignments (aln (sam-alignments sam))
(format-sam-alignment out aln)))