| 1 |
#include <pbcopper/data/Clipping.h> |
| 2 |
|
| 3 |
#include <pbcopper/data/MappedRead.h> |
| 4 |
#include <pbcopper/data/Read.h> |
| 5 |
#include <pbcopper/data/internal/ClippingImpl.h> |
| 6 |
|
| 7 |
#include <algorithm> |
| 8 |
|
| 9 |
#include <cassert> |
| 10 |
|
| 11 |
namespace PacBio { |
| 12 |
namespace Data { |
| 13 |
namespace { |
| 14 |
|
| 15 |
template <typename T> |
| 16 |
T clipContainer(const T& input, const size_t pos, const size_t len) |
| 17 |
{ |
| 18 |
if (input.empty()) { |
| 19 |
return {}; |
| 20 |
} |
| 21 |
assert(input.size() >= pos + len); |
| 22 |
return T{input.cbegin() + pos, input.cbegin() + pos + len}; |
| 23 |
} |
| 24 |
|
| 25 |
// returns reference positions removed from beginning |
| 26 |
size_t ClipToQueryImpl(Cigar& cigar, size_t startOffset, size_t endOffset) |
| 27 |
{ |
| 28 |
size_t refPosRemoved = 0; |
| 29 |
size_t remaining = startOffset; |
| 30 |
|
| 31 |
// clip CIGAR ops from beginning of query sequence |
| 32 |
while ((remaining > 0) && !cigar.empty()) { |
| 33 |
CigarOperation& op = cigar.front(); |
| 34 |
const auto opLength = op.Length(); |
| 35 |
const bool consumesQuery = ConsumesQuery(op.Type()); |
| 36 |
const bool consumesRef = ConsumesReference(op.Type()); |
| 37 |
|
| 38 |
if (opLength <= remaining) { |
| 39 |
cigar.erase(cigar.begin()); |
| 40 |
if (consumesQuery) { |
| 41 |
remaining -= opLength; |
| 42 |
} |
| 43 |
if (consumesRef) { |
| 44 |
refPosRemoved += opLength; |
| 45 |
} |
| 46 |
} else { |
| 47 |
op.Length(opLength - remaining); |
| 48 |
if (consumesRef) { |
| 49 |
refPosRemoved += remaining; |
| 50 |
} |
| 51 |
remaining = 0; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// clip CIGAR ops from end of query sequence |
| 56 |
remaining = endOffset; |
| 57 |
while ((remaining > 0) && !cigar.empty()) { |
| 58 |
CigarOperation& op = cigar.back(); |
| 59 |
const auto opLength = op.Length(); |
| 60 |
const bool consumesQuery = ConsumesQuery(op.Type()); |
| 61 |
|
| 62 |
if (opLength <= remaining) { |
| 63 |
cigar.pop_back(); |
| 64 |
if (consumesQuery) { |
| 65 |
remaining -= opLength; |
| 66 |
} |
| 67 |
} else { |
| 68 |
op.Length(opLength - remaining); |
| 69 |
remaining = 0; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
return refPosRemoved; |
| 74 |
} |
| 75 |
|
| 76 |
void ClipToReferenceImpl(ClipToReferenceConfig& config, size_t* queryPosRemovedFront, |
| 77 |
size_t* queryPosRemovedBack) |
| 78 |
{ |
| 79 |
|
| 80 |
const Position newTStart = std::max(config.original_tStart_, config.target_tStart_); |
| 81 |
const Position newTEnd = std::min(config.original_tEnd_, config.target_tEnd_); |
| 82 |
|
| 83 |
// fetch a 'working copy' of CIGAR data |
| 84 |
Cigar& cigar = config.cigar_; |
| 85 |
|
| 86 |
// ------------------------ |
| 87 |
// clip leading CIGAR ops |
| 88 |
// ------------------------ |
| 89 |
|
| 90 |
size_t remaining = newTStart - config.original_tStart_; |
| 91 |
while (remaining > 0 && !cigar.empty()) { |
| 92 |
CigarOperation& firstOp = cigar.front(); |
| 93 |
const auto firstOpLength = firstOp.Length(); |
| 94 |
const bool consumesQuery = ConsumesQuery(firstOp.Type()); |
| 95 |
const bool consumesRef = ConsumesReference(firstOp.Type()); |
| 96 |
|
| 97 |
if (!consumesRef) { |
| 98 |
|
| 99 |
// e.g. softclip - just pop it completely |
| 100 |
cigar.erase(cigar.begin()); |
| 101 |
if (consumesQuery) { |
| 102 |
*queryPosRemovedFront += firstOpLength; |
| 103 |
} |
| 104 |
|
| 105 |
} else { |
| 106 |
assert(consumesRef); |
| 107 |
|
| 108 |
// CIGAR ends at or before clip |
| 109 |
if (firstOpLength <= remaining) { |
| 110 |
cigar.erase(cigar.begin()); |
| 111 |
if (consumesQuery) { |
| 112 |
*queryPosRemovedFront += firstOpLength; |
| 113 |
} |
| 114 |
if (consumesRef) { |
| 115 |
remaining -= firstOpLength; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// CIGAR straddles clip |
| 120 |
else { |
| 121 |
assert(firstOpLength > remaining); |
| 122 |
firstOp.Length(firstOpLength - remaining); |
| 123 |
if (consumesQuery) { |
| 124 |
*queryPosRemovedFront += remaining; |
| 125 |
} |
| 126 |
remaining = 0; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
// ------------------------- |
| 132 |
// clip trailing CIGAR ops |
| 133 |
// ------------------------- |
| 134 |
|
| 135 |
remaining = config.original_tEnd_ - newTEnd; |
| 136 |
while (remaining > 0 && !cigar.empty()) { |
| 137 |
CigarOperation& lastOp = cigar.back(); |
| 138 |
const auto lastOpLength = lastOp.Length(); |
| 139 |
const bool consumesQuery = ConsumesQuery(lastOp.Type()); |
| 140 |
const bool consumesRef = ConsumesReference(lastOp.Type()); |
| 141 |
|
| 142 |
if (!consumesRef) { |
| 143 |
|
| 144 |
// e.g. softclip - just pop it completely |
| 145 |
cigar.pop_back(); |
| 146 |
if (consumesQuery) { |
| 147 |
*queryPosRemovedBack += lastOpLength; |
| 148 |
} |
| 149 |
|
| 150 |
} else { |
| 151 |
assert(consumesRef); |
| 152 |
|
| 153 |
// CIGAR ends at or after clip |
| 154 |
if (lastOpLength <= remaining) { |
| 155 |
cigar.pop_back(); |
| 156 |
if (consumesQuery) { |
| 157 |
*queryPosRemovedBack += lastOpLength; |
| 158 |
} |
| 159 |
if (consumesRef) { |
| 160 |
remaining -= lastOpLength; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
// CIGAR straddles clip |
| 165 |
else { |
| 166 |
assert(lastOpLength > remaining); |
| 167 |
lastOp.Length(lastOpLength - remaining); |
| 168 |
if (consumesQuery) { |
| 169 |
*queryPosRemovedBack += remaining; |
| 170 |
} |
| 171 |
remaining = 0; |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if (config.exciseFlankingInserts_) { |
| 177 |
// check for leading insertion |
| 178 |
if (!cigar.empty()) { |
| 179 |
const CigarOperation& op = cigar.front(); |
| 180 |
if (op.Type() == CigarOperationType::INSERTION) { |
| 181 |
*queryPosRemovedFront += op.Length(); |
| 182 |
cigar.erase(cigar.begin()); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
// check for trailing insertion |
| 187 |
if (!cigar.empty()) { |
| 188 |
const CigarOperation& op = cigar.back(); |
| 189 |
if (op.Type() == CigarOperationType::INSERTION) { |
| 190 |
*queryPosRemovedBack += op.Length(); |
| 191 |
cigar.pop_back(); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
} // namespace |
| 198 |
|
| 199 |
namespace internal { |
| 200 |
|
| 201 |
void ClipRead(Read& read, const ClipResult& result, size_t start, size_t end) |
| 202 |
{ |
| 203 |
const auto clipFrom = result.clipOffset_; |
| 204 |
const auto clipLength = (end - start); |
| 205 |
read.Seq = clipContainer(read.Seq, clipFrom, clipLength); |
| 206 |
read.Qualities = clipContainer(read.Qualities, clipFrom, clipLength); |
| 207 |
read.QueryStart = result.qStart_; |
| 208 |
read.QueryEnd = result.qEnd_; |
| 209 |
if (read.PulseWidth) { |
| 210 |
read.PulseWidth = clipContainer(read.PulseWidth->Data(), clipFrom, clipLength); |
| 211 |
} |
| 212 |
if (read.IPD) { |
| 213 |
read.IPD = clipContainer(read.IPD->Data(), clipFrom, clipLength); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
// NOTE: 'result' is moved into here, so we can take the CIGAR |
| 218 |
void ClipMappedRead(MappedRead& read, ClipResult result) |
| 219 |
{ |
| 220 |
// clip common data |
| 221 |
ClipRead(read, result, result.qStart_, result.qEnd_); |
| 222 |
|
| 223 |
// clip mapped data |
| 224 |
read.Cigar = std::move(result.cigar_); |
| 225 |
read.TemplateStart = result.refPos_; |
| 226 |
read.TemplateEnd = read.TemplateStart + ReferenceLength(read.Cigar); |
| 227 |
} |
| 228 |
|
| 229 |
} // namespace internal |
| 230 |
|
| 231 |
ClipResult::ClipResult(size_t clipOffset, Position qStart, Position qEnd) |
| 232 |
: clipOffset_{clipOffset}, qStart_{qStart}, qEnd_{qEnd} |
| 233 |
{} |
| 234 |
|
| 235 |
ClipResult::ClipResult(size_t clipOffset, Position qStart, Position qEnd, Position refPos, |
| 236 |
Cigar cigar) |
| 237 |
: clipOffset_{clipOffset} |
| 238 |
, qStart_{qStart} |
| 239 |
, qEnd_{qEnd} |
| 240 |
, refPos_{refPos} |
| 241 |
, cigar_{std::move(cigar)} |
| 242 |
{} |
| 243 |
|
| 244 |
ClipToReferenceConfig::ClipToReferenceConfig(const ClipToQueryConfig& queryConfig, |
| 245 |
Position originalTEnd, Position targetTStart, |
| 246 |
Position targetTEnd, bool exciseFlankingInserts) |
| 247 |
: ClipToQueryConfig(queryConfig) // icc 17 hack |
| 248 |
, original_tEnd_{originalTEnd} |
| 249 |
, target_tStart_{targetTStart} |
| 250 |
, target_tEnd_{targetTEnd} |
| 251 |
, exciseFlankingInserts_{exciseFlankingInserts} |
| 252 |
{} |
| 253 |
|
| 254 |
ClipResult ClipToQuery(ClipToQueryConfig& config) |
| 255 |
{ |
| 256 |
// easy out for unmapped reads |
| 257 |
const size_t startOffset = (config.target_qStart_ - config.original_qStart_); |
| 258 |
if (!config.isMapped_) { |
| 259 |
return ClipResult{startOffset, config.target_qStart_, config.target_qEnd_}; |
| 260 |
} |
| 261 |
|
| 262 |
// fetch CIGAR (in query orientation) |
| 263 |
Cigar cigar = std::move(config.cigar_); |
| 264 |
if (config.strand_ == Strand::REVERSE) { |
| 265 |
std::reverse(cigar.begin(), cigar.end()); |
| 266 |
} |
| 267 |
|
| 268 |
// do main clipping |
| 269 |
const size_t endOffset = config.original_qEnd_ - config.target_qEnd_; |
| 270 |
const size_t refPosRemoved = ClipToQueryImpl(cigar, startOffset, endOffset); |
| 271 |
|
| 272 |
// maybe restore CIGAR |
| 273 |
if (config.strand_ == Strand::REVERSE) { |
| 274 |
std::reverse(cigar.begin(), cigar.end()); |
| 275 |
} |
| 276 |
|
| 277 |
// return result |
| 278 |
const Position newPosition = (config.original_tStart_ + refPosRemoved); |
| 279 |
return ClipResult{startOffset, config.target_qStart_, config.target_qEnd_, newPosition, |
| 280 |
std::move(cigar)}; |
| 281 |
} |
| 282 |
|
| 283 |
ClipResult ClipToReference(ClipToReferenceConfig& config) |
| 284 |
{ |
| 285 |
assert(config.isMapped_); |
| 286 |
|
| 287 |
size_t queryPosRemovedFront = 0; |
| 288 |
size_t queryPosRemovedBack = 0; |
| 289 |
if (config.strand_ == Strand::FORWARD) { |
| 290 |
ClipToReferenceImpl(config, &queryPosRemovedFront, &queryPosRemovedBack); |
| 291 |
} else { |
| 292 |
ClipToReferenceImpl(config, &queryPosRemovedBack, &queryPosRemovedFront); |
| 293 |
} |
| 294 |
|
| 295 |
const size_t clipOffset = queryPosRemovedFront; |
| 296 |
const Position qStart = config.original_qStart_ + queryPosRemovedFront; |
| 297 |
const Position qEnd = config.original_qEnd_ - queryPosRemovedBack; |
| 298 |
const Position newPos = std::max(config.original_tStart_, config.target_tStart_); |
| 299 |
return ClipResult{clipOffset, qStart, qEnd, newPos, config.cigar_}; |
| 300 |
} |
| 301 |
|
| 302 |
} // namespace Data |
| 303 |
} // namespace PacBio |