package bio_io

  1. Overview
  2. Docs

An input channel for reading (biological) records.

Overview

  • To get a list of records, use records or with_file_records.
  • To iterate over records, use: iter_records or with_file_iter_records.
  • To fold over records, use: fold_records or with_file_fold_records.
  • To get a sequence of records, use: record_sequence.

Examples

For usage examples, check out Fasta.In_channel and the other In_channel modules.

with_file functions

Functions that have the with_file_ prefix in their name take a filename string rather than a t. They are a convenient way to avoid wrapping your all your code in with_file function calls.

Exceptions

Many functions will raise exceptions if one is encountered in the underlying I/O (Stdio), or if the format of the input file is bad. If you want to avoid exceptions, wrap the expression in Base.Or_error.try_with.

API

type t
type record
val stdin : t

create file_name opens an t on the standard input channel.

val create : Base.string -> t

create file_name opens an input channel on the file specified by file_name. You may want to use Base.Exn.protectx with this.

val close : t -> Base.unit

close t Close the t.

val with_file : Base.string -> f:(t -> 'a) -> 'a

with_file file_name ~f executes ~f on the channel created from file_name and ensures it is closed properly.

val equal : t -> t -> Base.bool

equal t1 t2 compares t1 and t2 for equality.

val input_record : t -> record Base.option

input_record t returns Some record if there is a record to return. If there are no more records, None is returned. Raises exceptions on bad input (e.g., bad file format).

Folding over records

val fold_records : t -> init:'a -> f:('a -> record -> 'a) -> 'a

fold_records t ~init ~f reduces all records from a t down to a single value of type 'a.

val foldi_records : t -> init:'a -> f:(Base.int -> 'a -> record -> 'a) -> 'a

fold'_records t ~init ~f is like fold_records except that f is provided the 0-based record index as its first argument.

Folding with file name

val with_file_fold_records : Base.string -> init:'a -> f:('a -> record -> 'a) -> 'a

with_file_fold_records file_name ~init ~f is like fold_records t ~init ~f except that it is passed a file name, and it manages t automatically. See with_file.

val with_file_foldi_records : Base.string -> init:'a -> f:(Base.int -> 'a -> record -> 'a) -> 'a

with_file_foldi_records file_name ~init ~f is like foldi_records t ~init ~f except that it is passed a file name, and it manages t automatically. See with_file.

Iterating over records

The iter functions are like the fold functions except they do not take an init value and the f function returns unit insead of some other value 'a, and thus return unit rather than a value 'a.

Use them for side-effects.

val iter_records : t -> f:(record -> Base.unit) -> Base.unit

iter_records t ~f calls f on each record in t. As f returns unit this is generally used for side effects.

val iteri_records : t -> f:(Base.int -> record -> Base.unit) -> Base.unit

iteri_records t ~f is like iteri_records t ~f except that f is passed in the 0-indexed record index as its first argument.

Iterating with file name

val with_file_iter_records : Base.string -> f:(record -> Base.unit) -> Base.unit

with_file_iter_records file_name ~init ~f is like iter_records t ~init ~f except that it is passed a file name, and it manages t automatically. See with_file.

val with_file_iteri_records : Base.string -> f:(Base.int -> record -> Base.unit) -> Base.unit

with_file_iteri_records file_name ~init ~f is like iteri_records t ~init ~f except that it is passed a file name, and it manages t automatically. See with_file.

Getting records as a list

These functions return record lists.

val records : t -> record Base.list

With file name

val with_file_records : Base.string -> record Base.list

Getting records as a sequence

These are a bit different:

* There are no with_file versions as you would have to do some fiddly things to keep the channel open, making them not so nice to use.

* If an exception is raised sometime during the pipeline, it will blow up, but any successful processing that happended, will have happened. So be careful if you are doing side-effecting things.

val record_sequence : t -> record Base.Sequence.t

record_sequence t returns a Sequence.t of record.