-
Notifications
You must be signed in to change notification settings - Fork 1
/
seqel-util.el
400 lines (318 loc) · 14.3 KB
/
seqel-util.el
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
;;; seqel-util.el --- Util functions and variables. -*- lexical-binding: t; -*-
;; Copyright (C) 2021 Zech Xu
;; Author: Zech Xu
;; Version: 1.0
;; Package-Requires: ((emacs "24.3"))
;; License: BSD-3
;; URL: https://github.com/RNAer/seqel
;;; Commentary:
;; Utility functions and variables for both nucleotide and protein minor modes
;;; Code:
(require 'color)
;; valid characters as alignment gaps in the sequences
(defvar seqel-gap
'(?- ?.)
"*Chars of '.' and '-' that represent a gap.")
(defvar seqel-space
'(? ?\t ?\n ?\r)
"*Chars of whitespaces that may appear in sequences.
It will be skipped during move or search or anything
involving counting.")
(defvar seqel-cruft-regexp
(regexp-opt (mapcar #'char-to-string
(concat seqel-gap seqel-space)))
"A regexp that matches cruft, including `seqel-gap' and `seqel-space'.")
(defvar seqel-cruft-set
(let ((alphabet-set (make-hash-table :test 'eq
:size (+ (length seqel-gap)
(length seqel-space)))))
(mapc (lambda (i) (puthash i t alphabet-set)) seqel-gap)
(mapc (lambda (i) (puthash i t alphabet-set)) seqel-space)
alphabet-set)
"The set of alphabets for `seqel-gap' and `seqel-space' in sequences.
This is a hash table: keys are char and values are t. It serves
like a set object similar in Python language.")
(defun seqel-region-or-line ()
"Return (BEG END) positions of region (if active) or current line."
(if (use-region-p)
(list (region-beginning) (region-end))
(list (line-beginning-position) (line-end-position))))
(defun seqel-forward-char (count legal-alphbet-set)
"Move the cursor for COUNT times repeatedly from the point on.
The next char has to belong to LEGAL-ALPHBET-SET to be
counted. The `seqel-cruft-regexp' char will be skipped, i.e., not
counted. Return the actual count of legal char. COUNT can be
either positive or negative integer - if it is positive, move
forward; if it is negative, move backward; if zero, don't move."
(let ((direction (if (< count 0) -1 1))
(fetch-char (if (< count 0) 'char-before 'char-after)))
(dotimes (x (abs count))
(while (gethash (funcall fetch-char) seqel-cruft-set)
(forward-char direction))
(if (gethash (funcall fetch-char) legal-alphbet-set)
(forward-char direction)
(error "Failed! Moved %d residues. You have illegal char here!" (* direction x))))
count))
(defun seqel-summary (beg end &optional legal-char-set)
"Count the number of all the characters in the region.
Summarize the sequence marked between BEG and END. Ignore char
that does not belong to LEGAL-CHAR-SET. Use hash table to create
dictionary-like data type. Return the hash table. This is
fast (only 2 seconds for 5M base pairs)."
(interactive (seqel-region-or-line))
(let (my-hash size char count)
(if legal-char-set
(progn (setq my-hash (make-hash-table :test 'equal :size (hash-table-count legal-char-set)))
(maphash (lambda (k v) (puthash k 0 my-hash)) legal-char-set))
;; any char is allowed if legal char is not provided
(progn (setq my-hash (make-hash-table :test 'equal :size 255))
(mapc (lambda (i) (puthash i 0 my-hash)) (number-sequence 0 255))))
(save-mark-and-excursion
(goto-char beg)
(dotimes (x (- end beg))
(setq char (char-after))
(if (or (not legal-char-set)
(gethash char legal-char-set))
(puthash char (1+ (gethash char my-hash)) my-hash))
(forward-char))
;; print out the summary table
(maphash (lambda (x y) (or (= y 0) (princ (format "%c:%d " x y) t))) my-hash)
my-hash)))
(defun seqel-count (beg end &optional legal-char-set)
"Count the chars that belong to LEGAL-CHAR-REGEXP.
Count the residues of sequence region marked between BEG and
END. Chars of `seqel-cruft-regexp' will be skipped. Return the
count if the region contains only legal characters (if
LEGAL-CHAR-SET is provided); otherwise return nil and report the
location of the invalid characters. This function is used by
`nuc-count' and `seqel-pro-count'."
(let ((count 0) char)
(save-mark-and-excursion
(goto-char beg)
(dotimes (i (- end beg))
(setq char (char-after))
(cond ((gethash char legal-char-set) (setq count (1+ count)))
;; allow any char if legal-char-set is not provided
((and legal-char-set
(not (gethash char seqel-cruft-set)))
(error "Bad char '%c' found at line %d column %d"
char (line-number-at-pos) (current-column))))
(forward-char)))
count))
(defun seqel-color-gradient-hsl (start stop step-number &optional s l)
"Return a list with (STEP-NUMBER + 1) number of colors in hex code.
START and STOP should are the start and ending hue in the color gradient
to create. And S and L are saturation and lightness.
For example, \"(seqel-color-gradient-hsl 0 0.333 20)\" will produce color
gradient from red to yellow to green. Please be aware that there is a
`color-gradient' function defined in color.el, which produces color
gradient in RGB scales (for the example here, it will create gradient
from red to green without yellow) besides other differences."
(let* ((incremental (/ (- stop start) step-number)))
(or s (setq s -1))
(or l (setq l 128))
(mapcar #'(lambda (rgb) (format "#%02x%02x%02x"
(nth 0 rgb)
(nth 1 rgb)
(nth 2 rgb)))
(mapcar #'(lambda (hue) (color-hsl-to-rgb hue s l))
(number-sequence start stop incremental)))))
(defvar seqel-color-pairs
'(("#ffffff" "#000000") ; white on black
("#ff0000" "#000000") ; red
("#00ff00" "#000000") ; green
("#00ffff" "#000000") ; cyan
("#ff00ff" "#000000") ; magenta
("#ffff00" "#000000") ; yellow
("#ff6600" "#000000") ; orange
("#0066ff" "#000000") ; ~blue
("#000000" "#00ff00") ; on green
("#ff0000" "#00ff00")
("#ffff00" "#00ff00")
("#000000" "#00aaff") ; on ~blue
("#ffffff" "#00aaff")
("#00ff00" "#00aaff")
("#000000" "#ff0000") ; on red
("#00ff00" "#ff0000")
("#ffffff" "#ff0000")
("#000000" "#ff00ff") ; on magenta
("#ffffff" "#ff00ff")
("#000000" "#ffff00") ; on yellow
("#ff0000" "#ffff00")
("#000000" "#00ffff") ; on cyan
("#ff0000" "#00ffff")
("#000000" "#ffffff") ; black on white
("#ff0000" "#ffffff") ; red
("#0000ff" "#ffffff") ; blue
("#ff00ff" "#ffffff") ; magenta
("#00ffff" "#ffffff") ; cyan
)
"Color pairs that pass WCAG AAA test.
The first one is the text color and the second is the background.")
(defvar seqel-color-pairs-cycle
(setcdr (last seqel-color-pairs) seqel-color-pairs)
"Color pairs that pass WCAG AAA test.
The first one is the text color and the second is the background.")
(defmacro seqel--def-char-face (letter backgrnd foregrnd grp)
"A macro used to define faces.
This will define a face named GRP-LETTER for character LETTER
that belongs to the face group named GRP, with BACKGRND as
background and FOREGRND as foreground colors."
`(defface ,(intern (concat grp "-" letter))
'((((type tty) (class color))
(:background ,backgrnd :foreground ,foregrnd))
(((type tty) (class color)) (:inverse-video t))
(((class color) (background dark))
(:background ,backgrnd :foreground ,foregrnd))
(((class color) (background light))
(:background ,backgrnd :foreground ,foregrnd))
(t (:background "gray")))
,(concat "Face for marking up " (upcase letter) "'s")))
(defun seqel-paint (beg end face-group &optional case)
"Color the sequences in the region BEG to END.
If CASE is nil, upcase and lowercase chars will be colored the same;
otherwise, not. FACE-GROUP decides which face groups ('base-face' or
'aa-face') to use.
TODO: this is slow for long sequences."
(save-mark-and-excursion
(let (char face)
(goto-char beg)
(dotimes (i (- end beg))
(setq char (char-after))
;; skip whitespaces and gap symbols
(if (not (gethash char seqel-cruft-set))
(progn (if case
(setq face (format "%s-%c" face-group char))
;; let upcase base use the color of lowercase base color
(setq face (format "%s-%c" face-group (upcase char))))
;; use font-lock-face instead of face for font-lock-mode is enabled
(with-silent-modifications
(put-text-property (+ beg i) (+ beg i 1) 'font-lock-face (intern face)))))
(forward-char)))))
(defun seqel-unpaint (beg end)
"Uncolor the sequences from BEG to END or the current line."
(interactive (seqel-region-or-line))
(with-silent-modifications
(remove-text-properties beg end '(font-lock-face nil))))
;;;;;; isearch pattern
;; (defun bioseq-isearch-transform-string ()
;; (interactive)
;; (let* ((string (seqel-isearch-mangle-str isearch-string)))
;; (setq isearch-string string
;; isearch-message (mapconcat 'isearch-text-char-description string ""))
;; (isearch-search-and-update)))
;; (define-key isearch-mode-map (kbd "C-c C-t") 'bioseq-isearch-transform-string)
(defun seqel-isearch-mangle-str (str)
"Mangle the string STR into a regexp to search over cruft in sequence.
Inserts a regexp between each base which matches sequence
formatting cruft, namely, you don't need to worry about if there
is any spaces separating between 'A' and 'T' if you'd like to
find all the 'AT's in the sequence. More technically, if
`seqel-cruft-regexp' is '[ ]', the search string 'acgt' would be
transformed into 'a[ ]*c[ ]*g[ ]*t'."
(mapconcat 'identity (split-string str "" 'omit-empty) (concat seqel-cruft-regexp "*")))
(defun seqel-isearch-forward (pattern &optional bound noerror)
"Search forward for PATTERN.
BOUND and NOERROR passes to function `re-search-forward'."
(let ((string (seqel-isearch-mangle-str pattern)))
(re-search-forward string bound noerror)))
(defun seqel-isearch-backward (pattern &optional bound noerror)
"Search backward for PATTERN.
BOUND and NOERROR passes to function `re-search-backward'."
(let ((string (seqel-isearch-mangle-str pattern)))
(re-search-backward string bound noerror)))
(defvar seqel-isearch-p nil
"Whether biological sequence pattern isearch is enabled.")
(defun seqel-toggle-isearch ()
"Toggle the sequence isearch."
(interactive)
(cond (seqel-isearch-p
(setq seqel-isearch-p nil)
(message "bio sequence pattern isearch is off"))
(t (setq-local seqel-isearch-p t)
(message "bio sequence pattern isearch is on"))))
(defun seqel--isearch-search-fun ()
"This function will be assigned to `isearch-search-fun-function'."
(if seqel-isearch-p
(if isearch-forward 'seqel-isearch-forward 'seqel-isearch-backward)
(isearch-search-fun-default)))
(setq isearch-search-fun-function 'seqel--isearch-search-fun)
(defun seqel-entry-forward (count entry-regexp)
"Move forward to the beginning of next entry.
It works in the style of `forward-paragraph'. COUNT needs to be
positive integer. ENTRY-REGEXP defines the boundary of
entries. Return current point if it moved over COUNT of entries;
otherwise return nil. See also `seqel-fasta-forward'."
(if (looking-at entry-regexp)
(setq count (1+ count)))
(if (< count 1)
(error "The parameter COUNT should be positive integer!"))
(if (re-search-forward entry-regexp nil 'move-to-point-max count)
(progn (beginning-of-line) (point))
nil))
(defun seqel-entry-backward (count entry-regexp)
"Move the point to the beginning of previous entry.
It works in the style of `backward-paragraph'. COUNT needs to be
positive integer. ENTRY-REGEXP defines the boundary of entries.
Return current point if it moved over COUNT of entries; otherwise
return nil. See also `seqel-fasta-backward'."
(if (> count 0)
(re-search-backward entry-regexp nil 'move-to-point-min count)
(error "The argument COUNT should be positive integer!")))
(defun seqel-entry-last (entry-regexp)
"Go to the beginning of last entry.
ENTRY-REGEXP defines the boundary of entries."
;; (while (seqel-entry-forward 1))
(goto-char (point-max))
(seqel-entry-backward 1 entry-regexp))
(defun seqel-entry-first (entry-regexp)
"Go to the beginning of first entry.
ENTRY-REGEXP defines the boundary of entries."
;; (while (seqel-entry-backward 1)))
(goto-char (point-min))
(or (looking-at entry-regexp)
(seqel-entry-forward 1 entry-regexp)))
(defun seqel-entry-count (entry-regexp)
"Count the number of entries in the buffer.
ENTRY-REGEXP defines the boundary of entries."
(let ((total 0))
(save-mark-and-excursion
(goto-char (point-max))
(while (seqel-entry-backward 1 entry-regexp)
(setq total (1+ total))))
(message "Total %d sequences." total)
total))
(defun seqel-hash-alist (alist)
"Convert association list ALIST to a hash table and return it.
The car will be the key and the cdr will be the value. If
there are multiple items with the same car, error will be
reported."
(let ((my-hash (make-hash-table :test 'equal :size (length alist))))
(dolist (entry alist)
(if (gethash (car entry) my-hash)
(error "The same key already exists!"))
(puthash (car entry) (cdr entry) my-hash))
my-hash))
(defun seqel-hash-equal (hash1 hash2)
"Compare 2 hash tables HASH1 and HASH2 to see whether they are equal."
(and (= (hash-table-count hash1)
(hash-table-count hash2))
(catch 'flag
(maphash (lambda (x y)
;; (message "%c" x)
(or (equal (gethash x hash2) y)
(throw 'flag nil)))
hash1)
(throw 'flag t))))
(defun seqel--zip (function &rest args)
"Apply FUNCTION to successive cars of all ARGS.
Return the list of results. This is similar to the Python zip function."
;; If no list is exhausted,
(if (not (memq nil args))
;; apply function to cars.
(cons (apply function (mapcar 'car args))
(apply 'seqel--zip function
;; Recurse for rest of elements.
(mapcar 'cdr args)))))
(provide 'seqel-util)
;;; seqel-util.el ends here