-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.lisp
47 lines (39 loc) · 1.33 KB
/
input.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
(in-package :cl-user)
(defpackage #:fasta-notes.input
(:use #:cl
#:fasta-notes.utils)
(:export :fasta-file
:fasta-file-header
:fasta-file-content
:read-fasta-file))
(in-package #:fasta-notes.input)
(defstruct fasta-file
(header "" :type string)
(content nil :type list))
(defun open-file (path)
(let ((stream (open path :if-does-not-exist nil)))
(if (null stream)
'(ERROR . "file doesn't exist")
`(OK . ,stream))))
(defun newlinep (char)
(char= char #\Newline))
(defun char-to-symbol (char)
(if (newlinep char)
nil
(make-symbol (string char))))
(defun read-fasta-body (stream &optional (result nil))
(let ((next-char (read-char stream nil)))
(if (null next-char)
(zip-list (remove-if #'null result) 3)
(read-fasta-body stream (append result (list (char-to-symbol next-char)))))))
(defun read-fasta-file (path)
"Return list of symbol and reading result. fasta-file if result is OK or error string if ERROR"
(let ((stream (open-file path))
(file (make-fasta-file)))
(if (eq 'ERROR (car stream))
stream
(progn
(setf (fasta-file-header file) (read-line (cdr stream)))
(setf (fasta-file-content file) (read-fasta-body (cdr stream)))
(close (cdr stream))
`(OK . ,file)))))