# Spec BamFileOut
Class for writing SAM and BAM files.

Extends | FormattedFileOut
All Extended | FormattedFile, FormattedFileOut
Defined in | <seqan/bam_io.h>
Signature | `typedef FormattedFile<Bam, Output> BamFileOut;`

## Member Function Overview

### Member Functions Inherited From FormattedFile

- `getFileExtensions`
- `operatorTDependentContext`

## Interface Function Overview

### Interface Functions Inherited From FormattedFile

- `atEnd`
- `close`
- `context`
- `format`
- `FormattedFile`
- `getFileExtensions`
- `guessFormat`
- `isOpen`
- `open`
- `setFormat`

### Interface Functions Inherited From FormattedFileOut

- `writeHeader`
- `writeRecord`

## Interface Metafunction Overview

### Interface Metafunctions Inherited From FormattedFile

- `FileFormat`
- `FormattedFileContext`

## Detailed Description

### Example

Access SAM or BAM files.

    #include <seqan/bam_io.h>

    using namespace seqan;

    int main()
    {
        CharString bamFileName = getAbsolutePath("demos/tutorial/sam_and_bam_io/example.sam");

        // Open input file, BamFileIn can read SAM and BAM files.
        BamFileIn bamFileIn;
        if (!open(bamFileIn, toCString(bamFileName)))
        {
            std::cerr << "ERROR: Could not open " << bamFileName << std::endl;
            return 1;
        }
        // Open output file, BamFileOut accepts also an ostream and a format tag.
        BamFileOut bamFileOut(context(bamFileIn), std::cout, Sam());

        try
        {
            // Copy header.
            BamHeader header;
            readHeader(header, bamFileIn);
            writeHeader(bamFileOut, header);

            // Copy records.
            BamAlignmentRecord record;
            while (!atEnd(bamFileIn))
            {
                readRecord(record, bamFileIn);
                writeRecord(bamFileOut, record);
            }
        }
        catch (Exception const & e)
        {
            std::cout << "ERROR: " << e.what() << std::endl;
            return 1;
        }

        return 0;
    }

Demo: demos/tutorial/sam_and_bam_io/solution1.cpp

The output is as follows:

    @HD	VN:1.3	SO:coordinate
    @SQ	SN:ref	LN:45
    @SQ	SN:ref2	LN:40
    r001	163	ref	7	30	8M4I4M1D3M	=	37	39	TTAGATAAAGAGGATACTG	*	XX:B:S,12561,2,20,112
    r002	0	ref	9	30	1S2I6M1P1I1P1I4M2I	*	0	0	AAAAGATAAGGGATAAA	*
    r003	0	ref	9	30	5H6M	*	0	0	AGCTAA	*
    r004	0	ref	16	30	6M14N1I5M	*	0	0	ATAGCTCTCAGC	*
    r003	16	ref	29	30	6H5M	*	0	0	TAGGC	*
    r001	83	ref	37	30	9M	=	7	-39	CAGCGCCAT	*

## See Also

- BamAlignmentRecord
- BamHeader

#### Dox Sources

    /*!
     * @class BamFileOut
     *
     * @extends FormattedFileOut
     *
     * @headerfile <seqan/bam_io.h>
     *
     * @brief Class for writing SAM and BAM files.
     *
     * @signature typedef FormattedFile<Bam, Output> BamFileOut;
     *
     * @section Example
     *
     * Access SAM or BAM files.
     *
     * @include demos/tutorial/sam_and_bam_io/solution1.cpp
     *
     * The output is as follows:
     *
     * @include demos/tutorial/sam_and_bam_io/example.sam
     *
     *
     *
     * @see BamHeader
     * @see BamAlignmentRecord
     */