Skip to content

Working with file IO

Mason M Lai edited this page Jun 22, 2017 · 1 revision

File input

Each supported format has a specialized parser to handle input. All file parsers in this framework implement Iterator and come with a predefined stream() method. They also implement AutoCloseable, and are meant to be used in a try-with-resources block.

An simple example:

try (BedParser bp = new BedParser(Paths.get("/path/to/example.bed"))) {
    while (bp.hasNext()) {
        BedFileRecord rec = bp.next();
        // do something with the BED record here
    }
}

For information about a particular format, see the corresponding wiki page.

File output -- human readable data

Classes that implement the Formattable interface will have a toFormattedString() method. The default output format will generally be something sensible. For example, FastaSequence#toString will return a FASTA string. Annotation#toString will return a BED12 string. For other output, custom methods will generally exist.

Annotated annot = new Annotation("chr1", 100, 200, Strand.BOTH)
annot.toFormattedBedString(6)  // returns a String in the BED6 format

These strings can be written to file with a BufferedReader, printed to the terminal, etc.

File output -- binary data

Some classes (currently only BAM) require a special writer class for output. See the corresponding wiki page for details.

Clone this wiki locally