int bamsplit(libmaus2::util::ArgInfo const & arginfo) { if ( isatty(STDIN_FILENO) ) { ::libmaus2::exception::LibMausException se; se.getStream() << "Refusing read binary data from terminal, please redirect standard input to pipe or file." << std::endl; se.finish(); throw se; } int const level = libmaus2::bambam::BamBlockWriterBaseFactory::checkCompressionLevel(arginfo.getValue<int>("level",getDefaultLevel())); int const verbose = arginfo.getValue<int>("verbose",getDefaultVerbose()); uint64_t const n = arginfo.getValue<int>("n",getDefaultN()); std::string const prefix = arginfo.getUnparsedValue("prefix",getDefaultFilePrefix(arginfo)); libmaus2::bambam::BamDecoder bamdec(std::cin); libmaus2::bambam::BamAlignment const & algn = bamdec.getAlignment(); libmaus2::bambam::BamHeader const & header = bamdec.getHeader(); ::libmaus2::bambam::BamHeader::unique_ptr_type uphead(updateHeader(arginfo,header)); libmaus2::aio::OutputStreamInstance::unique_ptr_type COS; libmaus2::bambam::BamWriter::unique_ptr_type writer; uint64_t c = 0; uint64_t f = 0; while ( bamdec.readAlignment() ) { if ( c++ % n == 0 ) { writer.reset(); if ( COS ) COS->flush(); COS.reset(); std::ostringstream fnostr; fnostr << prefix << "_" << std::setw(6) << std::setfill('0') << f++ << std::setw(0) << ".bam"; std::string const fn = fnostr.str(); libmaus2::aio::OutputStreamInstance::unique_ptr_type tCOS(new libmaus2::aio::OutputStreamInstance(fn)); COS = UNIQUE_PTR_MOVE(tCOS); libmaus2::bambam::BamWriter::unique_ptr_type twriter(new libmaus2::bambam::BamWriter(*COS,*uphead,level)); writer = UNIQUE_PTR_MOVE(twriter); if ( verbose ) std::cerr << "[V] opened file " << fn << std::endl; } algn.serialise(writer->getStream()); } writer.reset(); if ( COS ) COS->flush(); COS.reset(); return EXIT_SUCCESS; }
int main(int argc, char * argv[]) { try { ::libmaus::util::ArgInfo const arginfo(argc,argv); for ( uint64_t i = 0; i < arginfo.restargs.size(); ++i ) if ( arginfo.restargs[i] == "-v" || arginfo.restargs[i] == "--version" ) { std::cerr << ::biobambam::Licensing::license(); return EXIT_SUCCESS; } else if ( arginfo.restargs[i] == "-h" || arginfo.restargs[i] == "--help" ) { std::cerr << ::biobambam::Licensing::license(); std::cerr << std::endl; std::cerr << "Key=Value pairs:" << std::endl; std::cerr << std::endl; std::vector< std::pair<std::string,std::string> > V; V.push_back ( std::pair<std::string,std::string> ( "div=<["+::biobambam::Licensing::formatNumber(getDefaultDiv())+"]>", "divisor" ) ); V.push_back ( std::pair<std::string,std::string> ( "level=<["+::biobambam::Licensing::formatNumber(getDefaultLevel())+"]>", libmaus::bambam::BamBlockWriterBaseFactory::getBamOutputLevelHelpText() ) ); V.push_back ( std::pair<std::string,std::string> ( "verbose=<["+::biobambam::Licensing::formatNumber(getDefaultVerbose())+"]>", "print progress report" ) ); V.push_back ( std::pair<std::string,std::string> ( "prefix=<["+getDefaultFilePrefix(arginfo)+"]>", "default output file prefix" ) ); ::biobambam::Licensing::printMap(std::cerr,V); std::cerr << std::endl; return EXIT_SUCCESS; } return bamsplitmod(arginfo); } catch(std::exception const & ex) { std::cerr << ex.what() << std::endl; return EXIT_FAILURE; } }
int bamsplitmod(libmaus::util::ArgInfo const & arginfo) { if ( isatty(STDIN_FILENO) ) { ::libmaus::exception::LibMausException se; se.getStream() << "Refusing read binary data from terminal, please redirect standard input to pipe or file." << std::endl; se.finish(); throw se; } int const level = libmaus::bambam::BamBlockWriterBaseFactory::checkCompressionLevel(arginfo.getValue<int>("level",getDefaultLevel())); int const verbose = arginfo.getValue<int>("verbose",getDefaultVerbose()); uint64_t const div = arginfo.getValue<int>("div",getDefaultDiv()); std::string const prefix = arginfo.getUnparsedValue("prefix",getDefaultFilePrefix(arginfo)); if ( ! div ) { ::libmaus::exception::LibMausException se; se.getStream() << "div cannot be 0." << std::endl; se.finish(); throw se; } libmaus::bambam::BamDecoder bamdec(std::cin); libmaus::bambam::BamAlignment const & algn = bamdec.getAlignment(); libmaus::bambam::BamHeader const & header = bamdec.getHeader(); ::libmaus::bambam::BamHeader::unique_ptr_type uphead(updateHeader(arginfo,header)); libmaus::autoarray::AutoArray<libmaus::aio::CheckedOutputStream::unique_ptr_type> COS(div); libmaus::autoarray::AutoArray<libmaus::bambam::BamWriter::unique_ptr_type> writers(div); std::vector < std::string > filenames; for ( uint64_t i = 0; i < div; ++i ) { std::ostringstream ostr; ostr << prefix << "_" << std::setw(6) << std::setfill('0') << i << std::setw(0) << ".bam"; libmaus::aio::CheckedOutputStream::unique_ptr_type tCOS(new libmaus::aio::CheckedOutputStream(ostr.str())); COS[i] = UNIQUE_PTR_MOVE(tCOS); libmaus::bambam::BamWriter::unique_ptr_type twriter(new libmaus::bambam::BamWriter(*COS[i],*uphead,level)); writers[i] = UNIQUE_PTR_MOVE(twriter); } uint64_t c = 0; if ( verbose ) { while ( bamdec.readAlignment() ) { algn.serialise ( writers [ (c++) % div ] -> getStream() ); if ( ((c) & ((1ull<<20)-1)) == 0 ) std::cerr << "[V] " << c << std::endl; } std::cerr << "[V] " << c << std::endl; } else { while ( bamdec.readAlignment() ) algn.serialise ( writers [ (c++) % div ] -> getStream() ); } for ( uint64_t i = 0; i < div; ++i ) { writers[i].reset(); COS[i]->flush(); COS[i].reset(); } return EXIT_SUCCESS; }