File: ReadLibrary.hpp

package info (click to toggle)
salmon 0.7.2%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,352 kB
  • ctags: 5,243
  • sloc: cpp: 42,341; ansic: 6,252; python: 228; makefile: 207; sh: 190
file content (286 lines) | stat: -rw-r--r-- 10,957 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#ifndef READ_LIBRARY_HPP
#define READ_LIBRARY_HPP

#include <vector>
#include <exception>
#include <set>

#include <boost/filesystem.hpp>

#include "LibraryFormat.hpp"
#include "LibraryTypeDetector.hpp"

/**
 * This class represents the basic information about a library of reads, like
 * its paired-end status, the reads that should appear on the forward and reverse strand,
 * and the relative orientation of the reads.
 */
class ReadLibrary {
public:
    /**
     * Construct a new ReadLibrary of the given format
     */
    ReadLibrary(LibraryFormat& fmt) : fmt_(fmt),
                                      libTypeCounts_(std::vector<std::atomic<uint64_t>>(LibraryFormat::maxLibTypeID() + 1)),
                                      numCompat_(0)
    {}

    /**
     * Copy constructor
     */
    ReadLibrary(const ReadLibrary& rl) :
        fmt_(rl.fmt_),
        unmatedFilenames_(rl.unmatedFilenames_),
        mateOneFilenames_(rl.mateOneFilenames_),
        mateTwoFilenames_(rl.mateTwoFilenames_),
        libTypeCounts_(std::vector<std::atomic<uint64_t>>(LibraryFormat::maxLibTypeID() + 1)) {
            size_t mc = LibraryFormat::maxLibTypeID() + 1;
            for (size_t i = 0; i < mc; ++i) { libTypeCounts_[i].store(rl.libTypeCounts_[i].load()); }
            numCompat_.store(rl.numCompat());
	    if (rl.detector_) { detector_.reset(new LibraryTypeDetector(*(rl.detector_.get()))); }
        }

    /**
     * Move constructor
     */
    ReadLibrary(ReadLibrary&& rl) :
        fmt_(rl.fmt_),
        unmatedFilenames_(std::move(rl.unmatedFilenames_)),
        mateOneFilenames_(std::move(rl.mateOneFilenames_)),
        mateTwoFilenames_(std::move(rl.mateTwoFilenames_)),
        libTypeCounts_(std::vector<std::atomic<uint64_t>>(LibraryFormat::maxLibTypeID() + 1)) {
            size_t mc = LibraryFormat::maxLibTypeID() + 1;
            for (size_t i = 0; i < mc; ++i) { libTypeCounts_[i].store(rl.libTypeCounts_[i].load()); }
            numCompat_.store(rl.numCompat());
	    if (rl.detector_) { detector_ = std::move(detector_); }
        }

    /**
     * Add files containing mated reads (from pair 1 of the mates) to this library.
     */
    void addMates1(const std::vector<std::string>& mateOneFilenames) {
        mateOneFilenames_ = mateOneFilenames;
    }

    /**
     * Add files containing mated reads (from pair 2 of the mates) to this library.
     */
    void addMates2(const std::vector<std::string>& mateTwoFilenames) {
        mateTwoFilenames_ = mateTwoFilenames;
    }

    /**
     * Add files containing unmated reads.
     */
    void addUnmated(const std::vector<std::string>& unmatedFilenames) {
        unmatedFilenames_ = unmatedFilenames;
    }

    /**
     * Return true if this read library is for paired-end reads and false otherwise.
     */
    bool isPairedEnd() {
        return (fmt_.type == ReadType::PAIRED_END);
    }

    /**
    * If this is set, attempt to automatically detect this library's type
    */
    void enableAutodetect() {
      // if auto detection is not already enabled, and we're enabling it
      if (!detector_){
	detector_.reset(new LibraryTypeDetector(fmt_.type));
      }
    }

    bool autoDetect() const { return (detector_.get() != nullptr);}

    LibraryTypeDetector* getDetector() { return detector_.get(); }
    
    LibraryFormat& getFormat() { return fmt_; }
    const LibraryFormat& getFormat() const { return fmt_; }
  
    bool allExist_(std::vector<std::string>& filenames, std::stringstream& errorStream) {
        namespace bfs = boost::filesystem;
        bool allExist{true};
        for (auto& fn : filenames) {
            if (!bfs::exists(fn)) {
                errorStream << "ERROR: file [" << fn << "] does not appear to exist!\n\n";
                allExist = false;
            }
        }
        return allExist;
    }

    bool checkFileExtensions_(std::vector<std::string>& filenames, std::stringstream& errorStream) {
        namespace bfs = boost::filesystem;

        std::set<std::string> acceptableExensions = {".FASTA", ".FASTQ", ".FA", ".FQ",
                                                     ".fasta", ".fastq", ".fa", ".fq",
                                                     ".GZ", ".gz"};

        bool extensionsOK{true};
        for (auto& fn : filenames) {
            auto fpath = bfs::path(fn);
            auto ext = fpath.extension().string();
            if (bfs::is_regular_file(fpath)) {
                if (acceptableExensions.find(ext) == acceptableExensions.end()) {
                    errorStream << "ERROR: file [" << fn << "] has extension " << ext << ", "
                        << "which suggests it is neither a fasta nor a fastq file (or gzip compressed fasta/q).\n"
                        << "Is this file compressed in some other way?  If so, consider replacing: \n\n"
                        << fn << "\n\nwith\n\n"
                        << "<(decompressor " << fn << ")\n\n"
                        << "which will decompress the reads \"on-the-fly\"\n\n";
                    extensionsOK = false;
                } else if (bfs::is_empty(fpath)) {
                    errorStream << "ERROR: file [" << fn << "] appears to be empty "
                        "(i.e. it has size 0).  This is likely an error. "
                        "Please re-run salmon with a corrected input file.\n\n";
                        extensionsOK = false;
                }
            }
        }
        return extensionsOK;
    }

    bool isRegularFile() {
        if (isPairedEnd()) {
            for (auto& m1 : mateOneFilenames_) {
                if (!boost::filesystem::is_regular_file(m1)) { return false; }
            }
            for (auto& m2: mateTwoFilenames_) {
                if (!boost::filesystem::is_regular_file(m2)) { return false; }
            }
        } else {
            for (auto& um : unmatedFilenames_) {
                if (!boost::filesystem::is_regular_file(um)) { return false; }
            }
        }
        return true;
    }

    std::string readFilesAsString() {
        std::stringstream sstr;
        if (isPairedEnd()) {
            size_t n1 = mateOneFilenames_.size();
            size_t n2 = mateTwoFilenames_.size();
            if (n1 == 0 or n2 == 0 or n1 != n2) {
                sstr << "LIBRARY INVALID --- You must provide #1 and #2 mated read files with a paired-end library type";
            } else {
                for (size_t i = 0; i < n1; ++i) {
                    sstr << "( " << mateOneFilenames_[i] << ", " <<
                            mateTwoFilenames_[i] << " )";
                    if (i != n1 - 1) { sstr << ", "; }
                }
            }
        } else { // single end
            size_t n = unmatedFilenames_.size();
            if (n == 0) {
                sstr << "LIBRARY INVALID --- You must provide unmated read files with a single-end library type";
            } else {
                for (size_t i = 0; i < n; ++i) {
                    sstr << unmatedFilenames_[i];
                    if (i != n - 1) { sstr << ", "; }
                }
            }
        } // end else
        return sstr.str();
    }

    /**
     * Checks if this read library is valid --- if it's paired-end, it should have mate1/2 reads and the same
     * number of files for each; if it's unpaired it should have only unpaired files.
     * NOTE: This function throws an exception if this is not a valid read library!
     */
    void checkValid() {

        bool readsOK{true};

        std::stringstream errorStream;
        errorStream << "\nThe following errors were detected with the read files\n";
        errorStream << "======================================================\n";

        if (isPairedEnd()) {
            size_t n1 = mateOneFilenames_.size();
            size_t n2 = mateTwoFilenames_.size();
            if (n1 == 0 or n2 == 0 or n1 != n2) {
                errorStream << "You must provide #1 and #2 mated read files with a paired-end library type\n";
                readsOK = false;
            }
        } else {
            size_t n = unmatedFilenames_.size();
            if (n == 0) {
                errorStream << "You must provide unmated read files with a single-end library type\n";
                readsOK = false;
            }

        }

        // Check if the user tried to pass in non-fast{a,q} files.  If so,
        // throw an exception with the appropriate error messages.
        // NOTE: This check currently does nothing useful for non-regular files
        // (i.e. named-pipes).  If the user passed in a non-regular file, we should
        // have some other mechanism to check if it's of an expected format and provide
        // a reasonable error message otherwise.
        readsOK = readsOK && allExist_(mateOneFilenames_, errorStream) && checkFileExtensions_(mateOneFilenames_, errorStream);
        readsOK = readsOK && allExist_(mateTwoFilenames_, errorStream) && checkFileExtensions_(mateTwoFilenames_, errorStream);
        readsOK = readsOK && allExist_(unmatedFilenames_, errorStream) && checkFileExtensions_(unmatedFilenames_, errorStream);

        if (!readsOK) {
            throw std::invalid_argument(errorStream.str());
        }
    }

    /**
     * Return the vector of files containing the mate1 reads for this library.
     */
    const std::vector<std::string>& mates1() const { return mateOneFilenames_; }

    /**
     * Return the vector of files containing the mate2 reads for this library.
     */
    const std::vector<std::string>& mates2() const { return mateTwoFilenames_; }

    /**
     * Return the vector of files containing the unmated reads for the library.
     */
    const std::vector<std::string>& unmated() const { return unmatedFilenames_; }

    /**
     * Return the LibraryFormat object describing the format of this read library.
     */
    const LibraryFormat& format() const { return fmt_; }

    /**
     * Update the number of fragments compatible with this library type
     */
    inline void updateCompatCounts(uint64_t numCompat) {
        numCompat_ += numCompat;
    }

    uint64_t numCompat() const { return numCompat_; }

    /**
    * Update the library type counts for this read library given the counts
    * in the vector `counts` which has been passed in.
    */
    inline void updateLibTypeCounts(const std::vector<uint64_t>& counts) {
        size_t lc{counts.size()};
        for (size_t i = 0; i < lc; ++i) { libTypeCounts_[i] += counts[i]; }
    }

    std::vector<std::atomic<uint64_t>>& libTypeCounts() {
        return libTypeCounts_;
    }

private:
    LibraryFormat fmt_;
    std::vector<std::string> unmatedFilenames_;
    std::vector<std::string> mateOneFilenames_;
    std::vector<std::string> mateTwoFilenames_;
    std::vector<std::atomic<uint64_t>> libTypeCounts_;
    std::atomic<uint64_t> numCompat_;
    std::unique_ptr<LibraryTypeDetector> detector_{nullptr};
};

#endif // READ_LIBRARY_HPP