package biocaml
The OCaml Bioinformatics Library
Install
Dune Dependency
Authors
Maintainers
Sources
biocaml-0.11.2.tbz
sha256=fae219e66db06f81f3fd7d9e44717ccf2d6d85701adb12004ab4ae6d3359dd2d
sha512=f6abd60dac2e02777be81ce3b5acdc0db23b3fa06731f5b2d0b32e6ecc9305fe64f407bbd95a3a9488b14d0a7ac7c41c73a7e18c329a8f18febfc8fd50eccbc6
doc/src/biocaml.base/fasta.ml.html
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 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
(* FIXME: max_line_length and alphabet format options are not implemented *) open Rresult type header = string list type item = { description : string; sequence : string; } [@@ deriving sexp] (* FIXME: should check there is no newline in the arguments *) let item ~description ~sequence = { description ; sequence ; } type fmt = { allow_sharp_comments : bool; allow_semicolon_comments : bool; allow_empty_lines : bool; max_line_length : int option; alphabet : string option; } let fmt ?(allow_sharp_comments = true) ?(allow_semicolon_comments = false) ?(allow_empty_lines = false) ?max_line_length ?alphabet () = { allow_sharp_comments ; allow_semicolon_comments ; allow_empty_lines ; max_line_length ; alphabet ; } let default_fmt = fmt () type item0 = [ | `Comment of string | `Empty_line | `Description of string | `Partial_sequence of string ] [@@deriving sexp] let sequence_to_int_list s = try String.split s ~on:' ' |> List.map ~f:Int.of_string |> R.ok with Failure msg -> R.error_msg msg type parser_error = [ `Fasta_parser_error of int * string ] [@@deriving sexp] module Parser0 = struct type state = { fmt : fmt ; line : int ; line_start : bool ; started_first_item : bool ; symbol : symbol ; } and symbol = | S (* start of comment or description *) | Comment of string | Description of string | Sequence of { empty : bool } | Terminal let initial_state ?(fmt = default_fmt) () = { fmt ; line = 0 ; line_start = true ; started_first_item = false ; symbol = S } let fail st msg = R.fail (`Fasta_parser_error (st.line, msg)) let failf st fmt = let k x = fail st x in Printf.ksprintf k fmt let newline ?sym st = { st with line = st.line + 1 ; line_start = true ; symbol = match sym with | None -> st.symbol | Some s -> s } let step st = function | None -> ( match st.symbol with | S -> R.ok ({ st with symbol = Terminal }, []) | Comment c -> assert (not st.started_first_item) ; R.ok ({ st with symbol = Terminal }, [ `Comment c ]) | Description _ -> fail st "Missing sequence in last item" | Sequence { empty = true } -> fail st "Missing sequence in last item" | Sequence { empty = false } -> R.ok ({ st with symbol = Terminal }, []) | Terminal -> R.ok (st, []) ) | Some buf -> ( let allowed_comment_char c = let open Char in (c = '#' && st.fmt.allow_sharp_comments) || (c = ';' && st.fmt.allow_semicolon_comments) in let n = String.length buf in let rec loop st accu i j = if j < n then match buf.[j], st.line_start, st.symbol with | _, _, Terminal -> R.ok (st, []) | _, false, S -> assert false (* unreachable state *) | '>', true, S -> loop { st with line_start = false ; started_first_item = true ; symbol = Description "" } accu (j + 1) (j + 1) | _, true, Comment _ | _, true, Description _ -> assert false (* unreachable states *) | '>', true, Sequence { empty = true } -> fail st "Expected sequence, not description" | '>', true, Sequence { empty = false } -> loop { st with line_start = false ; symbol = Description "" } accu (j + 1) (j + 1) | (';' | '#' as c), true, S -> assert (i = j && i = 0) ; if allowed_comment_char c then loop { st with line_start = false ; symbol = Comment "" } accu (i + 1) (j + 1) else failf st "Character %c not allowed for comments" c | (';' | '#'), true, Sequence _ -> fail st "Comment after first item" | '\n', true, (Sequence _ | S) -> if st.fmt.allow_empty_lines then loop (newline st) (`Empty_line :: accu) (j + 1) (j + 1) else fail st "Empty line" | c, true, S -> failf st "Unexpected character %c at beginning of line" c | '\n', false, Comment c -> let c' = String.sub buf ~pos:i ~len:(j - i) in loop (newline st ~sym:S) (`Comment (c ^ c') :: accu) (j + 1) (j + 1) | '\n', false, Description d -> let d' = String.sub buf ~pos:i ~len:(j - i) in loop (newline st ~sym:(Sequence { empty = true })) (`Description (d ^ d') :: accu) (j + 1) (j + 1) | '\n', false, Sequence _ -> let seq = String.sub buf ~pos:i ~len:(j - i) in loop (newline st ~sym:(Sequence { empty = false })) (`Partial_sequence seq :: accu) (j + 1) (j + 1) | _, false, (Comment _ | Description _ | Sequence { empty = false }) -> loop st accu i (j + 1) | _, false, Sequence { empty = true } -> assert false (* unreachable state *) | _, true, Sequence { empty = true } -> loop { st with line_start = false ; symbol = Sequence { empty = false } } accu i (j + 1) | _, true, Sequence { empty = false } -> loop { st with line_start = false } accu i (j + 1) else match st.symbol with | S | Terminal -> R.ok (st, accu) | Comment c -> let c' = String.sub buf ~pos:i ~len:(j - i) in R.ok ({ st with symbol = Comment (c ^ c') }, accu) | Description d -> let d' = String.sub buf ~pos:i ~len:(j - i) in R.ok ({ st with symbol = Description (d ^ d') }, accu) | Sequence _ as sym -> let symbol, res = if i = j then sym, accu else let seq = String.sub buf ~pos:i ~len:(j - i) in Sequence { empty = false }, (`Partial_sequence seq :: accu) in R.ok ({ st with symbol }, res) in loop st [] 0 0 >>| fun (st, res) -> st, List.rev res ) end let unparser0 = function | `Comment c -> "#" ^ c | `Empty_line -> "" | `Description d -> ">" ^ d | `Partial_sequence s -> s (* This could probably be optimized *) let rev_concat xs = String.concat ~sep:"" (List.rev xs) module Parser = struct type state = { state0 : Parser0.state ; symbol : symbol ; } and symbol = | Init | Item of string * string list | Terminal let initial_state ?fmt () = { state0 = Parser0.initial_state ?fmt () ; symbol = Init } let step_aux (sym, accu) item0 = match item0, sym with | _, Terminal -> Terminal, accu | (`Comment _ | `Empty_line), _ -> sym, accu | `Description d, Init -> Item (d, []), accu | `Description _, Item (_, []) -> assert false (* should be detected by Parser0.step *) | `Description d', Item (d, xs) -> let item = item ~description:d ~sequence:(rev_concat xs) in Item (d', []), item :: accu | `Partial_sequence _, Init -> assert false (* should be detected by Parser0.step *) | `Partial_sequence s, Item (d, xs) -> Item (d, s :: xs), accu let step_final input ((symbol, items) as res) = match input, symbol with | Some _, _ | None, (Init | Terminal) -> res | None, Item (d, xs) -> Terminal, item ~description:d ~sequence:(rev_concat xs) :: items let step st input = Parser0.step st.state0 input >>| fun (state0, items0) -> let init = st.symbol, [] in let symbol, items = List.fold_left items0 ~init ~f:step_aux |> step_final input in { state0 ; symbol }, List.rev items end let unparser item = Printf.sprintf ">%s\n%s\n" item.description item.sequence
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>