1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <seqan/bam_io.h>
using namespace seqan2;
int main(int, char const **)
{
CharString bamFileInName = getAbsolutePath("demos/tutorial/file_io_overview/example.bam");
CharString samFileOutName = getAbsolutePath("demos/tutorial/file_io_overview/example.sam");
// Open input BAM file.
BamFileIn bamFileIn;
if (!open(bamFileIn, toCString(bamFileInName)))
{
std::cerr << "ERROR: could not open input file " << bamFileInName << ".\n";
return 1;
}
// Open output SAM file.
BamFileOut samFileOut(context(bamFileIn), toCString(samFileOutName));
// Copy header.
BamHeader header;
try
{
readHeader(header, bamFileIn);
writeHeader(samFileOut, header);
}
catch (ParseError const & e)
{
std::cerr << "ERROR: input header is badly formatted. " << e.what() << "\n";
}
catch (IOError const & e)
{
std::cerr << "ERROR: could not copy header. " << e.what() << "\n";
}
// Copy all records.
BamAlignmentRecord record;
while (!atEnd(bamFileIn))
{
try
{
readRecord(record, bamFileIn);
writeRecord(samFileOut, record);
}
catch (ParseError const & e)
{
std::cerr << "ERROR: input record is badly formatted. " << e.what() << "\n";
}
catch (IOError const & e)
{
std::cerr << "ERROR: could not copy record. " << e.what() << "\n";
}
}
return 0;
}
|