/** ** 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 QvalReader.hh ** ** \brief Reads Qval files. ** ** \author Roman Petrovski **/ #ifndef CASAVA_IO_QVAL_READER_HH #define CASAVA_IO_QVAL_READER_HH #include #include #include #include #include #include "common/Exceptions.hh" #include "common/FilteringStreams.hh" namespace casava { namespace io { namespace fs = boost::filesystem; namespace cc = casava::common; /** ** \brief ifstream providing qval information cluster by cluster. **/ class QvalReader { public: typedef std::vector Qvals; typedef Qvals RecordType; QvalReader(const fs::path &filePath, unsigned int clusterCount, unsigned int cyclesPerCluster) : filePath_(filePath), is_(filePath.string().c_str(), casava::common::CompressionFactory::get("gzip")) , clusterCount_(clusterCount), cyclesPerCluster_(cyclesPerCluster), currentCluster_(0) { } unsigned int getClusterCount() const {return clusterCount_;} Qvals &get(Qvals &whereTo){ return getQvals(whereTo); } std::string getDescription() const { return filePath_.string(); } Qvals &getQvals(Qvals &whereTo) { if (clusterCount_ <= currentCluster_) { BOOST_THROW_EXCEPTION(cc::PreConditionException( "Reading more clusters than expected in Qval file: " + filePath_.string() + boost::lexical_cast(clusterCount_))); } whereTo.clear(); std::transform(boost::make_counting_iterator(0u), boost::make_counting_iterator(cyclesPerCluster_), std::back_inserter(whereTo), boost::bind(&QvalReader::getline, this, boost::ref(is_))); ++currentCluster_; return whereTo; } // It is important in some cases to make sure that thre are no more strings in the Qval file. // However, if there are, the position does get invalidated. Use with care! bool checkEof(){std::string dummy; return (clusterCount_ <= currentCluster_) && !(is_ >> dummy) && is_.eof();} private: const fs::path filePath_; cc::Ifstream is_; const unsigned int clusterCount_; const unsigned int cyclesPerCluster_; unsigned int currentCluster_; std::string getline(std::istream &is) { std::string ret; if (std::getline(is, ret).fail()) { BOOST_THROW_EXCEPTION(cc::IoException(errno, "Could not read all Qvals for cluster: " + boost::lexical_cast(currentCluster_) + " from file: " + filePath_.string())); } return ret; } }; } // namespace io } // namespace casava #endif //CASAVA_IO_QVAL_READER_HH