/** ** Copyright (c) 2007-2009 Illumina, Inc. ** ** This software is covered by the "Illumina Genome Analyzer Software ** License Agreement" and the "Illumina Source Code License Agreement", ** and certain third party copyright/licenses, and any user of this ** source file is bound by the terms therein (see accompanying files ** Illumina_Genome_Analyzer_Software_License_Agreement.pdf and ** Illumina_Source_Code_License_Agreement.pdf and third party ** copyright/license notices). ** ** This file is part of the Consensus Assessment of Sequence And VAriation ** (CASAVA) software package. ** ** \file FastqWriter.hh ** ** \brief Writes Qval files and other associated files (filter, position). ** ** \author Roman Petrovski **/ #ifndef CASAVA_IO_FASTQ_WRITER_HH #define CASAVA_IO_FASTQ_WRITER_HH #include #include #include #include #include #include #include "common/Compression.hh" #include "common/Exceptions.hh" #include "common/FastIo.hh" #include "common/FilteringStreams.hh" namespace casava { namespace io { namespace fs = boost::filesystem; namespace cc = casava::common; /** ** \brief ofstream storing sequences and qualities in fastq format. **/ class FastqWriter { public: struct RecordType { RecordType(const std::string &instrumentName, const unsigned int runNumber, const std::string &flowCellId, const unsigned int lane, const unsigned int tile, const std::pair &position, const std::string &barcode, const unsigned int readNumber, const bool filter, const unsigned short int control, const std::pair &cluster) : readId_(instrumentName + ':' + (boost::format("%d") % runNumber).str() + ':' + flowCellId + ':' + boost::lexical_cast(lane) + ':' + boost::lexical_cast(tile)) , tile_(tile) , position_(position), barcode_(barcode), readNumber_(readNumber), filter_(filter), control_(control), cluster_(cluster) {} std::string readId_; unsigned int tile_; std::pair position_; std::string barcode_; unsigned int readNumber_; bool filter_; unsigned short int control_; std::pair cluster_; }; FastqWriter(const fs::path &filePath, const cc::Compression &compressor, const bool overwrite) : filePath_(filePath.string() + compressor.getFileNameExtension()) , os_(compressor) , currentCluster_(0) { open(overwrite); } FastqWriter(const fs::path &filePath, const bool overwrite) : filePath_(filePath) , os_(cc::CompressionFactory::none()) , currentCluster_(0) { open(overwrite); } void open(const bool overwrite) { if (!overwrite && fs::exists(filePath_)) { BOOST_THROW_EXCEPTION(cc::IoException(EPERM, "File already exists: " + filePath_.string())); } std::cerr << "opening " << filePath_.string() << std::endl; os_.open(filePath_.string().c_str()); if (!fs::exists(filePath_)) { //if (!os_.is_complete()) { BOOST_THROW_EXCEPTION(cc::PostConditionException("File does not exist after having been created: " + filePath_.string())); } if (!os_) { BOOST_THROW_EXCEPTION(cc::IoException(errno, "Couldn't open fastq file: " + filePath_.string())); } } void put(const RecordType &read){ if (!(os_ << "@" << read.readId_ << ":" << read.position_.first << ":" << read.position_.second << " " << read.readNumber_ << ":" << (read.filter_ ? "N:" : "Y:") << read.control_ << ":" << read.barcode_ << std::endl << read.cluster_.first << std::endl << "+" << std::endl << read.cluster_.second << std::endl)) { BOOST_THROW_EXCEPTION(casava::common::IoException(errno, "Failed to write data into: " + filePath_.string())); } ++currentCluster_; } void close() { os_.close(); } private: const fs::path filePath_; cc::Ofstream os_; unsigned int currentCluster_; FastqWriter(); FastqWriter(const FastqWriter &); FastqWriter &operator=(const FastqWriter &); }; } // namespce io } // namespace casava #endif // CASAVA_IO_FASTQ_WRITER_HH