let parse_header_item line =
let parse_data tag tvl = match tag with
| `HD -> parse_header_line tvl >>| fun x -> `HD x
| `SQ -> parse_ref_seq tvl >>| fun x -> `SQ x
| `RG -> parse_read_group tvl >>| fun x -> `RG x
| `PG -> parse_program tvl >>| fun x -> `PG x
| `Other tag -> Ok (`Other (tag,tvl))
| `CO -> assert false
in
match String.lsplit2 ~on:'\t' (line : Line.t :> string) with
| None ->
error "header line contains no tabs" line Line.sexp_of_t
| Some (tag, data) ->
parse_header_item_tag tag >>= function
| `CO -> Ok (`CO data)
| tag ->
match String.split ~on:'\t' data with
| [] -> assert false
| ""::[] ->
error "header contains no data" tag sexp_of_header_item_tag
| tvl ->
Result.List.map tvl ~f:parse_tag_value >>= fun tvl ->
parse_data tag tvl