|
| 1 | +#ifndef __FASTX_PARSER__ |
| 2 | +#define __FASTX_PARSER__ |
| 3 | + |
| 4 | +#include "fcntl.h" |
| 5 | +#include "unistd.h" |
| 6 | +#include <atomic> |
| 7 | +#include <cstdio> |
| 8 | +#include <cstdlib> |
| 9 | +#include <iostream> |
| 10 | +#include <thread> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +extern "C" { |
| 14 | +#include "kseq.h" |
| 15 | +} |
| 16 | + |
| 17 | +#include "concurrentqueue.h" |
| 18 | + |
| 19 | +#ifndef __FASTX_PARSER_PRECXX14_MAKE_UNIQUE__ |
| 20 | +#define __FASTX_PARSER_PRECXX14_MAKE_UNIQUE__ |
| 21 | + |
| 22 | +#if __cplusplus >= 201402L |
| 23 | +#include <memory> |
| 24 | +using std::make_unique |
| 25 | +#else |
| 26 | + |
| 27 | +#include <cstddef> |
| 28 | +#include <memory> |
| 29 | +#include <type_traits> |
| 30 | +#include <utility> |
| 31 | + |
| 32 | +template <class T> struct _Unique_if { |
| 33 | + using _Single_object = std::unique_ptr<T>; |
| 34 | +}; |
| 35 | + |
| 36 | +template <class T> struct _Unique_if<T[]> { |
| 37 | + using _Unknown_bound = std::unique_ptr<T[]>; |
| 38 | +}; |
| 39 | + |
| 40 | +template <class T, size_t N> struct _Unique_if<T[N]> { |
| 41 | + using _Known_bound = void; |
| 42 | +}; |
| 43 | + |
| 44 | +template <class T, class... Args> |
| 45 | +typename _Unique_if<T>::_Single_object make_unique(Args&&... args) { |
| 46 | + return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); |
| 47 | +} |
| 48 | + |
| 49 | +template <class T> |
| 50 | +typename _Unique_if<T>::_Unknown_bound make_unique(size_t n) { |
| 51 | + using U = typename std::remove_extent<T>::type; |
| 52 | + return std::unique_ptr<T>(new U[n]()); |
| 53 | +} |
| 54 | + |
| 55 | +template <class T, class... Args> |
| 56 | +typename _Unique_if<T>::_Known_bound make_unique(Args&&...) = delete; |
| 57 | + |
| 58 | +#endif // C++11 |
| 59 | +#endif //__FASTX_PARSER_PRECXX14_MAKE_UNIQUE__ |
| 60 | + |
| 61 | +namespace fastx_parser { |
| 62 | +struct ReadSeq { |
| 63 | + std::string seq; |
| 64 | + std::string name; |
| 65 | + ~ReadSeq() {} |
| 66 | +}; |
| 67 | + |
| 68 | +struct ReadPair { |
| 69 | + ReadSeq first; |
| 70 | + ReadSeq second; |
| 71 | +}; |
| 72 | + |
| 73 | +template <typename T> class ReadChunk { |
| 74 | +public: |
| 75 | + ReadChunk(size_t want) : group_(want), want_(want), have_(want) {} |
| 76 | + inline void have(size_t num) { have_ = num; } |
| 77 | + inline size_t size() { return have_; } |
| 78 | + inline size_t want() const { return want_; } |
| 79 | + T& operator[](size_t i) { return group_[i]; } |
| 80 | + typename std::vector<T>::iterator begin() { return group_.begin(); } |
| 81 | + typename std::vector<T>::iterator end() { return group_.begin() + have_; } |
| 82 | + |
| 83 | +private: |
| 84 | + std::vector<T> group_; |
| 85 | + size_t want_; |
| 86 | + size_t have_; |
| 87 | +}; |
| 88 | + |
| 89 | +template <typename T> class ReadGroup { |
| 90 | +public: |
| 91 | + ReadGroup(moodycamel::ProducerToken&& pt, moodycamel::ConsumerToken&& ct) |
| 92 | + : pt_(std::move(pt)), ct_(std::move(ct)) {} |
| 93 | + moodycamel::ConsumerToken& consumerToken() { return ct_; } |
| 94 | + moodycamel::ProducerToken& producerToken() { return pt_; } |
| 95 | + // get a reference to the chunk this ReadGroup owns |
| 96 | + std::unique_ptr<ReadChunk<T>>& chunkPtr() { return chunk_; } |
| 97 | + // get a *moveable* reference to the chunk this ReadGroup owns |
| 98 | + std::unique_ptr<ReadChunk<T>>&& takeChunkPtr() { return std::move(chunk_); } |
| 99 | + inline void have(size_t num) { chunk_->have(num); } |
| 100 | + inline size_t size() { return chunk_->size(); } |
| 101 | + inline size_t want() const { return chunk_->want(); } |
| 102 | + T& operator[](size_t i) { return (*chunk_)[i]; } |
| 103 | + typename std::vector<T>::iterator begin() { return chunk_->begin(); } |
| 104 | + typename std::vector<T>::iterator end() { |
| 105 | + return chunk_->begin() + chunk_->size(); |
| 106 | + } |
| 107 | + void setChunkEmpty() { chunk_.release(); } |
| 108 | + bool empty() const { return chunk_.get() == nullptr; } |
| 109 | + |
| 110 | +private: |
| 111 | + std::unique_ptr<ReadChunk<T>> chunk_{nullptr}; |
| 112 | + moodycamel::ProducerToken pt_; |
| 113 | + moodycamel::ConsumerToken ct_; |
| 114 | +}; |
| 115 | + |
| 116 | +template <typename T> class FastxParser { |
| 117 | +public: |
| 118 | + FastxParser(std::vector<std::string> files, uint32_t numConsumers, |
| 119 | + uint32_t numParsers = 1, uint32_t chunkSize = 1000); |
| 120 | + |
| 121 | + FastxParser(std::vector<std::string> files, std::vector<std::string> files2, |
| 122 | + uint32_t numConsumers, uint32_t numParsers = 1, |
| 123 | + uint32_t chunkSize = 1000); |
| 124 | + ~FastxParser(); |
| 125 | + bool start(); |
| 126 | + ReadGroup<T> getReadGroup(); |
| 127 | + bool refill(ReadGroup<T>& rg); |
| 128 | + void finishedWithGroup(ReadGroup<T>& s); |
| 129 | + |
| 130 | +private: |
| 131 | + moodycamel::ProducerToken getProducerToken_(); |
| 132 | + moodycamel::ConsumerToken getConsumerToken_(); |
| 133 | + |
| 134 | + std::vector<std::string> inputStreams_; |
| 135 | + std::vector<std::string> inputStreams2_; |
| 136 | + uint32_t numParsers_; |
| 137 | + std::atomic<uint32_t> numParsing_; |
| 138 | + std::vector<std::unique_ptr<std::thread>> parsingThreads_; |
| 139 | + size_t blockSize_; |
| 140 | + moodycamel::ConcurrentQueue<std::unique_ptr<ReadChunk<T>>> readQueue_, |
| 141 | + seqContainerQueue_; |
| 142 | + |
| 143 | + // holds the indices of files (file-pairs) to be processed |
| 144 | + moodycamel::ConcurrentQueue<uint32_t> workQueue_; |
| 145 | + |
| 146 | + std::vector<std::unique_ptr<moodycamel::ProducerToken>> produceReads_; |
| 147 | + std::vector<std::unique_ptr<moodycamel::ConsumerToken>> consumeContainers_; |
| 148 | +}; |
| 149 | +} |
| 150 | +#endif // __FASTX_PARSER__ |
0 commit comments