| 1 |
#include <pbcopper/data/Read.h> |
| 2 |
|
| 3 |
#include <pbcopper/data/Clipping.h> |
| 4 |
#include <pbcopper/data/internal/ClippingImpl.h> |
| 5 |
|
| 6 |
#include <bitset> |
| 7 |
#include <ios> |
| 8 |
#include <ostream> |
| 9 |
#include <stdexcept> |
| 10 |
#include <type_traits> |
| 11 |
#include <utility> |
| 12 |
|
| 13 |
namespace PacBio { |
| 14 |
namespace Data { |
| 15 |
|
| 16 |
Read::Read(Data::ReadId id, std::string seq, std::optional<Frames> pw, std::optional<Frames> ipd, |
| 17 |
LocalContextFlags flags, Accuracy readAccuracy, SNR snr, std::string model) |
| 18 |
: Id{std::move(id)} |
| 19 |
, Seq{std::move(seq)} |
| 20 |
, PulseWidth{std::move(pw)} |
| 21 |
, IPD{std::move(ipd)} |
| 22 |
, Flags{std::move(flags)} |
| 23 |
, ReadAccuracy{std::move(readAccuracy)} |
| 24 |
, SignalToNoise{std::move(snr)} |
| 25 |
, Model{std::move(model)} |
| 26 |
, FullLength{Flags & Data::ADAPTER_BEFORE && Flags & Data::ADAPTER_AFTER} |
| 27 |
{ |
| 28 |
if (Id.ZmwInterval) { |
| 29 |
QueryStart = Id.ZmwInterval->Start(); |
| 30 |
QueryEnd = Id.ZmwInterval->End(); |
| 31 |
} |
| 32 |
|
| 33 |
if (PulseWidth && PulseWidth->size() != Seq.size()) { |
| 34 |
throw std::invalid_argument("[pbcopper] read (name=" + std::string(Id) + |
| 35 |
") ERROR: features PW/seq are of mismatched length: " + |
| 36 |
std::to_string(PulseWidth->size()) + " vs " + |
| 37 |
std::to_string(Seq.size())); |
| 38 |
} |
| 39 |
|
| 40 |
if (IPD && (IPD->size() != Seq.size())) { |
| 41 |
throw std::invalid_argument( |
| 42 |
"[pbcopper] read (name=" + std::string(Id) + |
| 43 |
") ERROR: features IPD/seq are of mismatched length: " + std::to_string(IPD->size()) + |
| 44 |
" vs " + std::to_string(Seq.size())); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
Read::Read(const std::string& name, std::string seq, QualityValues qualities, SNR snr) |
| 49 |
: Id{name} |
| 50 |
, Seq{std::move(seq)} |
| 51 |
, Qualities{std::move(qualities)} |
| 52 |
, QueryStart{0} |
| 53 |
, QueryEnd(Seq.size()) |
| 54 |
, SignalToNoise{std::move(snr)} |
| 55 |
{} |
| 56 |
|
| 57 |
Read::Read(const std::string& name, std::string seq, QualityValues qualities, SNR snr, |
| 58 |
Position qStart, Position qEnd) |
| 59 |
: Id{name} |
| 60 |
, Seq{std::move(seq)} |
| 61 |
, Qualities{std::move(qualities)} |
| 62 |
, QueryStart{qStart} |
| 63 |
, QueryEnd{qEnd} |
| 64 |
, SignalToNoise{std::move(snr)} |
| 65 |
{} |
| 66 |
|
| 67 |
Read::Read(const std::string& name, std::string seq, QualityValues qualities, SNR snr, |
| 68 |
Position qStart, Position qEnd, Frames pulseWidths, Frames ipd) |
| 69 |
: Id{name} |
| 70 |
, Seq{std::move(seq)} |
| 71 |
, PulseWidth{std::move(pulseWidths)} |
| 72 |
, Qualities{std::move(qualities)} |
| 73 |
, IPD{std::move(ipd)} |
| 74 |
, QueryStart{qStart} |
| 75 |
, QueryEnd{qEnd} |
| 76 |
, SignalToNoise{std::move(snr)} |
| 77 |
{} |
| 78 |
|
| 79 |
Read Read::ClipTo(const int32_t begin, const int32_t end) const |
| 80 |
{ |
| 81 |
Read copy{this->Id, |
| 82 |
this->Seq.substr(begin, end - begin), |
| 83 |
(this->PulseWidth ? std::optional<Frames>{Frames(this->PulseWidth->cbegin() + begin, |
| 84 |
this->PulseWidth->cbegin() + end)} |
| 85 |
: std::nullopt), |
| 86 |
(this->IPD ? std::optional<Frames>{Frames(this->IPD->cbegin() + begin, |
| 87 |
this->IPD->cbegin() + end)} |
| 88 |
: std::nullopt), |
| 89 |
this->Flags, |
| 90 |
this->ReadAccuracy, |
| 91 |
this->SignalToNoise, |
| 92 |
this->Model}; |
| 93 |
return copy; |
| 94 |
} |
| 95 |
|
| 96 |
int32_t Read::Length() const { return Seq.length(); } |
| 97 |
|
| 98 |
std::string Read::FullName() const { return Id; } |
| 99 |
|
| 100 |
std::ostream& operator<<(std::ostream& os, const Read& read) |
| 101 |
{ |
| 102 |
os << std::boolalpha; |
| 103 |
os << "Read(Id=" << read.Id << ", Seq=" << read.Seq << ", PulseWidth="; |
| 104 |
if (read.IPD) { |
| 105 |
os << *read.PulseWidth; |
| 106 |
} else { |
| 107 |
os << "None"; |
| 108 |
} |
| 109 |
os << ", Qualities=" << read.Qualities << ", IPD="; |
| 110 |
if (read.IPD) { |
| 111 |
os << *read.IPD; |
| 112 |
} else { |
| 113 |
os << "None"; |
| 114 |
} |
| 115 |
os << ", QueryStart=" << read.QueryStart << ", QueryEnd=" << read.QueryEnd |
| 116 |
<< ", Flags=" << std::bitset<8>{read.Flags} << ", ReadAccuracy=" << read.ReadAccuracy |
| 117 |
<< ", SignalToNoise=" << read.SignalToNoise << ", Model=" << read.Model |
| 118 |
<< ", FullLength=" << read.FullLength << ')'; |
| 119 |
return os; |
| 120 |
} |
| 121 |
|
| 122 |
void ClipToQuery(Read& read, Position start, Position end) |
| 123 |
{ |
| 124 |
// skip out if clip not needed |
| 125 |
if (start <= read.QueryStart && end >= read.QueryEnd) { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
// calculate clipping |
| 130 |
ClipToQueryConfig clipConfig{read.Seq.size(), |
| 131 |
read.QueryStart, |
| 132 |
read.QueryEnd, |
| 133 |
start, |
| 134 |
end, |
| 135 |
// TODO: these next are unused for unmapped clip-to-query |
| 136 |
UNMAPPED_POSITION, |
| 137 |
Strand::FORWARD, |
| 138 |
{}, |
| 139 |
false}; |
| 140 |
auto result = ClipToQuery(clipConfig); |
| 141 |
|
| 142 |
// apply clipping |
| 143 |
internal::ClipRead(read, std::move(result), start, end); |
| 144 |
} |
| 145 |
|
| 146 |
} // namespace Data |
| 147 |
} // namespace PacBio |