/** ** 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 ClusterIndexWriter.hh ** ** \brief Writes cluster numbers out. ** ** \author Roman Petrovski **/ #ifndef CASAVA_DEMULTIPLEX_CLUSTER_INDEX_WRITER_HH #define CASAVA_DEMULTIPLEX_CLUSTER_INDEX_WRITER_HH namespace casava { namespace demultiplex { namespace fs = boost::filesystem; namespace cc = casava::common; /** ** \brief ofstream storing cluster index values cluster by cluster in text form. **/ class ClusterIndexWriter { public: typedef int ClusterIndex; ClusterIndexWriter(const fs::path &filePath, const cc::Compression &compressor) : filePath_(filePath.string() + compressor.getFileNameExtension()), os_(compressor), currentCluster_(0) {open();} ClusterIndexWriter(const fs::path &filePath) : filePath_(filePath), os_(cc::CompressionFactory::none()), currentCluster_(0) {open();} void open() { if (fs::exists(filePath_)) { BOOST_THROW_EXCEPTION(cc::IoException(EPERM, "File already exists: " + filePath_.string())); } os_.open(filePath_.string().c_str(), std::ios_base::out); if (!os_) { BOOST_THROW_EXCEPTION(cc::IoException(errno, "Couldn't open index file: " + filePath_.string())); } } ~ClusterIndexWriter(){ } void put(const ClusterIndex &index){ putClusterIndex(index); } void putClusterIndex(const ClusterIndex &index){ if (!(os_ << index << "\n")) { 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_; ClusterIndexWriter(); ClusterIndexWriter(const ClusterIndexWriter &); ClusterIndexWriter &operator=(const ClusterIndexWriter &); }; /** ** \brief generates sequence from 1 to clusterCount_ inclusive. **/ class ClusterIndexReader { public: typedef int ClusterIndex; typedef ClusterIndex RecordType; ClusterIndexReader(unsigned int clusterCount) : clusterCount_(clusterCount), currentCluster_(0){} unsigned int getClusterCount() const {return clusterCount_;} ClusterIndex &get(ClusterIndex &whereTo){ whereTo = getClusterIndex(); return whereTo; } std::string getDescription() const { return (boost::format("Cluster index generator for range 0 - %d, current %d") % clusterCount_ % currentCluster_).str(); } ClusterIndex getClusterIndex() { if (clusterCount_ <= currentCluster_) { BOOST_THROW_EXCEPTION(cc::PreConditionException( "Reading more clusters than expected. Expected: " + boost::lexical_cast(clusterCount_))); } return ++currentCluster_; } private: const unsigned int clusterCount_; unsigned int currentCluster_; }; } // namespace alignment } // namespace casava #endif // CASAVA_DEMULTIPLEX_CLUSTER_INDEX_WRITER_HH