Source file fasta.ml
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
module Result = Biocaml_result
open CFStream
type item = {
description : string;
sequence : string;
}
type fmt = {
allow_sharp_comments : bool;
allow_semicolon_comments : bool;
allow_empty_lines : bool;
comments_only_at_top : bool;
max_line_length : int option;
alphabet : string option;
}
let default_fmt = {
allow_sharp_comments = true;
allow_semicolon_comments = false;
allow_empty_lines = false;
comments_only_at_top = true;
max_line_length = None;
alphabet = None;
}
type item0 = [
| `Comment of string
| `Empty_line
| `Description of string
| `Partial_sequence of string
]
let sequence_to_int_list s =
String.split s ~on:' '
|> Result.List.map ~f:(fun x ->
try Ok (Int.of_string x)
with Failure _ -> error "invalid int" x sexp_of_string
)
let parse_item0
?(=true)
?(=false)
?(allow_empty_lines=false)
?max_line_length
?alphabet
line
=
let open Result.Monad_infix in
let s = (line : Line.t :> string) in
let n = String.length s in
(match max_line_length with
| None -> Ok ()
| Some x ->
if x <= n then Ok ()
else error
"max_line_length exceeded"
(x,n) [%sexp_of: int * int ]
) >>= fun () ->
if allow_empty_lines && (String.for_all s ~f:Char.is_whitespace) then
Ok `Empty_line
else if (not allow_empty_lines && n = 0) then
Or_error.error_string "allow_empty_lines is false but got empty line"
else if Char.equal s.[0] '>' then
Ok (`Description (String.slice s 1 n))
else
match allow_sharp_comments, allow_semicolon_comments, s.[0] with
| true,true,(';' | '#')
| true,false,'#'
| false,true,';' ->
Ok (`Comment (String.slice s 1 n))
| false,false,(';' | '#') ->
Or_error.error_string "comments lines are not allowed"
| _ ->
(match alphabet with
| None -> Ok (`Partial_sequence s)
| Some alphabet ->
if String.for_all s ~f:(String.mem alphabet) then
Ok (`Partial_sequence s)
else
error "sequence contains string outside allowed alphabet"
(s,alphabet) [%sexp_of: string * string ]
)
module Lines = Lines.MakeIO(Future_unix)
let read0
?(start=Pos.(incr_line unknown))
?(=true)
?(=false)
?(allow_empty_lines=false)
?max_line_length
?alphabet
r
=
let pos = ref start in
Stream.map (Lines.read r) ~f:(fun line ->
let current_pos = !pos in
pos := Pos.incr_line !pos;
parse_item0
line
~allow_sharp_comments
~allow_semicolon_comments
~allow_empty_lines
?max_line_length
?alphabet
|> fun x ->
Or_error.tag_arg x "position" current_pos Pos.sexp_of_t
)
(** Return the initial comment lines. Upon return, [item0s] will point
to first item0 that is not a `Comment, but there may still be
additional `Comment items later. *)
let
?(allow_empty_lines=false)
(item0s : item0 Or_error.t Stream.t)
: header Or_error.t
=
let rec loop accum : header Or_error.t =
match Stream.peek item0s with
| Some (Ok (`Comment x)) -> (
Stream.junk item0s;
loop (x::accum)
)
| Some (Ok `Empty_line) -> (
if allow_empty_lines then
loop accum
else
Or_error.error_string
"allow_empty_lines is false but got empty line in header"
)
| Some (Ok (`Description _))
| Some (Ok (`Partial_sequence _)) ->
Ok accum
| Some (Error _ as e) ->
e
| None ->
Ok accum
in
loop [] |> Result.map ~f:List.rev
let read ?start ?(fmt=default_fmt) r =
let {;
;
allow_empty_lines;
;
max_line_length;
alphabet} = fmt
in
let error_string s = Some (Or_error.error_string s) in
let item0s = read0 r
?start
~allow_sharp_comments
~allow_semicolon_comments
~allow_empty_lines
?max_line_length
?alphabet
in
match read_header ~allow_empty_lines item0s with
| Error _ as e -> e
| Ok ->
let rec f description partial_seqs : item Or_error.t option =
match Stream.peek item0s with
| Some (Ok (`Comment _)) -> begin
if comments_only_at_top then
error_string "comments_only_at_top = true but got comment later"
else (
Stream.junk item0s;
f description partial_seqs
)
end
| Some (Ok `Empty_line) -> begin
if allow_empty_lines then (
Stream.junk item0s;
f description partial_seqs
)
else
error_string "allow_empty_lines = false but got empty line"
end
| Some (Ok (`Description x)) -> begin
match description,partial_seqs with
| None, [] -> (
Stream.junk item0s;
f (Some x) []
)
| None, _::_ ->
assert false
| Some _, [] ->
error_string "previous description line not followed by sequence"
| Some description, partial_seqs ->
Some (Ok {
description;
sequence = partial_seqs |> List.rev |> String.concat ~sep:"";
})
end
| Some (Ok (`Partial_sequence x)) -> begin
match description,partial_seqs with
| None, _ ->
error_string "sequence not preceded by description line"
| Some _, partial_seqs -> (
Stream.junk item0s;
f description (x::partial_seqs)
)
end
| Some (Error _ as e) -> begin
Stream.junk item0s;
Some e
end
| None -> begin
match description,partial_seqs with
| None, [] -> None
| None, _::_ ->
assert false
| Some _, [] ->
error_string
"description line not followed by sequence, reached end-of-file"
| Some description, partial_seqs ->
Some (Ok {
description;
sequence = partial_seqs |> List.rev |> String.concat ~sep:"";
})
end
in
Ok (header, Stream.from (fun _ -> f None []))
let with_file ?fmt file ~f =
let start = Pos.make ~source:file ~line:1 () in
In_channel.with_file file ~f:(fun cin ->
match read ~start ?fmt cin with
| Error _ as e -> e
| Ok (,strm) -> f header strm
)