let item_to_bed_graph () =
let queue = Queue.create () in
let current_state = ref None in
Biocaml_transform.make ~name:"wig_to_variable_step" ()
~feed:(function
| `comment _ -> ()
| `bed_graph_value already_done ->
Queue.enqueue queue (`output (Ok already_done))
| `variable_step_state_change (chrom, span) ->
current_state := Some (`variable (chrom, span))
| `variable_step_value (pos, v) ->
begin match !current_state with
| Some (`variable (chrom, span)) ->
let stop = pos + Option.(value ~default:1 span) - 1 in
Queue.enqueue queue (`output (Ok (chrom, pos, stop, v)))
| other ->
Queue.enqueue queue (`output (Error (`not_in_variable_step_state)))
end
| `fixed_step_state_change (chrom, start, step, span) ->
current_state := Some (`fixed (chrom, start, step , span, 0))
| `fixed_step_value v ->
begin match !current_state with
| Some (`fixed (chrom, start, step, span, current)) ->
let pos = start + (step * current) in
let stop = pos + Option.(value ~default:1 span) - 1 in
Queue.enqueue queue (`output (Ok (chrom, pos, stop, v)));
current_state := Some (`fixed (chrom, start, step , span, current + 1))
| other ->
Queue.enqueue queue (`output (Error (`not_in_fixed_step_state)))
end)
~next:(fun stopped ->
match Queue.dequeue queue with
| None -> if stopped then `end_of_stream else `not_ready
| Some v -> v)