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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
/*
This file is part of BioD.
Copyright (C) 2012 Artem Tarasov <lomereiter@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
module bio.std.hts.bam.md.reconstruct;
import bio.std.hts.bam.cigar;
import bio.std.hts.bam.read;
import bio.std.hts.bam.md.core;
import std.conv;
import std.range;
import std.traits;
import std.algorithm;
import std.range;
/// Reconstruct read DNA.
/// Returns lazy sequence.
auto dna(T)(T read)
if(isBamRead!(Unqual!T))
{
debug {
/*
import std.stdio;
stderr.writeln("[dna] processing read ", read.name);
stderr.flush();
*/
}
static struct QueryChunk(S) {
S sequence;
CigarOperation operation;
}
static struct QueryChunksResult(R, S) {
this(R ops, S seq) {
_seq = seq;
_ops = ops;
}
auto front() @property {
auto op = _ops.front;
return QueryChunk!S(_seq[0 .. op.length], op);
}
bool empty() @property {
return _ops.empty;
}
void popFront() {
_seq = _seq[_ops.front.length .. _seq.length];
_ops.popFront();
}
private R _ops;
private S _seq;
}
static auto getQueryChunksResult(R, S)(S sequence, R cigar) {
return QueryChunksResult!(R, S)(cigar, sequence);
}
// Get read sequence chunks corresponding to query-consuming operations in read.sequence
static auto queryChunks(ref T read) {
return getQueryChunksResult(read.sequence, filter!"a.is_query_consuming"(read.cigar));
}
auto _read = read;
auto query_chunks = queryChunks(_read);
static struct Result(R, M) {
this(ref T read, R query_sequence, M md_operations) {
debug {
_initial_qseq = to!string(query_sequence);
}
_qseq = query_sequence;
_md = md_operations;
_fetchNextMdOperation();
}
bool empty() @property {
return _empty;
}
/*
MD operations -> match(N) ? consume N characters from query
mismatch(C) ? consume a character from query and replace it with C
deletion(S) ? consume S from MD
*/
char front() @property {
final switch (_cur_md_op.type) {
case MdOperationType.Match:
return cast(char)_qseq.front;
case MdOperationType.Mismatch:
return _cur_md_op.mismatch;
case MdOperationType.Deletion:
return cast(char)_cur_md_op.deletion.front;
}
}
private void _fetchNextMdOperation() {
if (_md.empty) {
_empty = true;
return;
}
_cur_md_op = _md.front;
_md.popFront();
}
private bool _qseqIsSuddenlyEmpty() {
if (!_qseq.empty) {
return false;
}
/* MD and CIGAR don't match */
debug {
import std.stdio;
stderr.writeln("Current MD operation: ", _cur_md_op);
stderr.writeln("Query sequence: ", _initial_qseq);
}
return true;
}
void popFront() {
final switch (_cur_md_op.type) {
case MdOperationType.Mismatch:
if (_qseqIsSuddenlyEmpty())
break;
_qseq.popFront();
_fetchNextMdOperation();
break;
case MdOperationType.Match:
if (_qseqIsSuddenlyEmpty())
break;
--_cur_md_op.match;
_qseq.popFront();
if (_cur_md_op.match == 0) {
_fetchNextMdOperation();
}
break;
case MdOperationType.Deletion:
_cur_md_op.deletion.popFront();
if (_cur_md_op.deletion.empty) {
_fetchNextMdOperation();
}
break;
}
}
private {
debug {
string _initial_qseq;
}
R _qseq;
M _md;
bool _empty;
MdOperation _cur_md_op;
}
}
auto md = _read["MD"];
string md_str;
if (!md.is_nothing) {
md_str = cast(string)_read["MD"];
}
static auto getResult(R, M)(ref T read, R query, M md_ops) {
return Result!(R, M)(read, query, md_ops);
}
auto result = getResult(_read,
joiner(map!"a.sequence"(filter!"a.operation.is_reference_consuming"(query_chunks))),
mdOperations(md_str));
debug {
import std.stdio;
if (result.empty) {
stderr.writeln("[dna] empty DNA!");
stderr.writeln(" read name: ", read.name);
stderr.writeln(" read sequence: ", read.sequence);
stderr.writeln(" read CIGAR: ", read.cigarString());
stderr.writeln(" read MD tag: ", read["MD"]);
stderr.flush();
}
}
return result;
}
unittest {
import std.stdio;
// stderr.writeln("Testing reconstruction of reference from MD tags and CIGAR");
// Test reference reconstruction from MD and CIGAR.
// (Tests are taken from http://davetang.org/muse/2011/01/28/perl-and-sam/)
BamRead read;
read = BamRead("r1",
"CGATACGGGGACATCCGGCCTGCTCCTTCTCACATG",
[CigarOperation(36, 'M')]);
read["MD"] = "1A0C0C0C1T0C0T27";
assert(equal(dna(read), "CACCCCTCTGACATCCGGCCTGCTCCTTCTCACATG"));
read = BamRead("r2",
"GAGACGGGGTGACATCCGGCCTGCTCCTTCTCACAT",
[CigarOperation(6, 'M'),
CigarOperation(1, 'I'),
CigarOperation(29, 'M')]);
read["MD"] = "0C1C0C1C0T0C27";
assert(equal(dna(read), "CACCCCTCTGACATCCGGCCTGCTCCTTCTCACAT"));
read = BamRead("r3",
"AGTGATGGGGGGGTTCCAGGTGGAGACGAGGACTCC",
[CigarOperation(9, 'M'),
CigarOperation(9, 'D'),
CigarOperation(27, 'M')]);
read["MD"] = "2G0A5^ATGATGTCA27";
assert(equal(dna(read), "AGGAATGGGATGATGTCAGGGGTTCCAGGTGGAGACGAGGACTCC"));
read = BamRead("r4",
"AGTGATGGGAGGATGTCTCGTCTGTGAGTTACAGCA",
[CigarOperation(2, 'M'),
CigarOperation(1, 'I'),
CigarOperation(7, 'M'),
CigarOperation(6, 'D'),
CigarOperation(26, 'M')]);
read["MD"] = "3C3T1^GCTCAG26";
assert(equal(dna(read), "AGGCTGGTAGCTCAGGGATGTCTCGTCTGTGAGTTACAGCA"));
}
/**
* Returns lazy sequence of reference bases. If some bases can't be determined from reads,
* they are replaced with 'N'.
*
* Reads must be a range of reads aligned to the same reference sequence, sorted by leftmost
* coordinate.
* Returned reference bases start from the leftmost position of the first read,
* and end at the rightmost position of all the reads.
*/
auto dna(R)(R reads)
if (isInputRange!R && isBamRead!(Unqual!(ElementType!R)))
{
static struct Result(F) {
alias Unqual!(ElementType!F) Read;
this(F reads) {
_reads = reads;
if (_reads.empty) {
_empty = true;
return;
}
auto read = _reads.front;
_chunk = dna(read);
_reference_pos = read.position;
_reads.popFront();
}
@property bool empty() {
return _empty;
}
@property char front() {
if (_bases_to_skip > 0) {
return 'N';
}
return _chunk.front;
}
private void setSkipMode(ref Read read) {
_reads.popFront();
_chunk = dna(read);
_bases_to_skip = read.position - _reference_pos;
}
void popFront() {
_reference_pos += 1;
if (_bases_to_skip > 0) {
--_bases_to_skip;
return;
}
_chunk.popFront();
/*
* If current chunk is empty, get the next one.
*
* Here's the reference:
* .........................*.......................................
* _reference_pos (we are here)
* Last chunk ended just now:
* [..........]
* Go through subsequent reads while their leftmost position is
* less or equal to _reference_pos, select the one which covers
* more bases to the right of _reference_pos.
* [...............]
* [....]
* [..........]
* [.........] <- this one is the best
*/
if (_chunk.empty) {
if (_reads.empty) {
_empty = true;
return;
}
auto next_read = _reads.front;
if (next_read.position > _reference_pos) {
setSkipMode(next_read);
return;
}
auto best_read = next_read;
// read covers half-open [position .. position + basesCovered) interval
auto best_end_pos = best_read.basesCovered() + best_read.position;
bool found_good = best_end_pos > _reference_pos;
while (true) {
if (_reads.empty) {
if (!found_good) {
_empty = true;
return;
}
break;
}
auto read = _reads.front;
if (read.position > _reference_pos) {
if (!found_good) {
setSkipMode(read);
return;
}
break;
}
auto end_pos = read.basesCovered() + read.position;
if (end_pos > _reference_pos) {
found_good = true;
if (end_pos > best_end_pos) {
best_end_pos = end_pos;
best_read = read;
}
}
_reads.popFront();
}
// If we're here, we've found a good read.
_chunk = dna(best_read);
debug {
/*
import std.stdio;
writeln("_reference_pos = ", _reference_pos,
"; best_read.position = ", best_read.position,
"; _chunk length = ", best_read.basesCovered());
*/
}
// However, we need to strip some bases from the left.
popFrontN(_chunk, _reference_pos - best_read.position);
}
}
private size_t _bases_to_skip;
private size_t _reference_pos;
private ReturnType!(dna!Read) _chunk;
private bool _empty = false;
private F _reads;
}
auto nonempty = filter!"a.basesCovered() > 0"(reads);
return Result!(typeof(nonempty))(nonempty);
}
unittest {
// reads are taken from HG00110.chrom20.ILLUMINA.bwa.GBR.exome.20111114.bam
auto r1 = BamRead("r1",
"AGGTTTTGTGAGTGGGACAGTTGCAGCAAAACACAACCATAGGTGCCCATCCACCAAGGCAGGCTCTCCATCTTGCTCAGAGTGGCTCTA",
[CigarOperation(89, 'M'),
CigarOperation(1, 'S')]);
r1.position = 60246;
r1["MD"] = "89";
auto r2 = BamRead("r2",
"TGTGAGTGGGACAGTTGCAGCAAAACACAACCATAGGTGCCCATCCACCAAGGCAGGCTCTCCATCTTGCTCAGAGTGGCTCCAGCCCTT",
[CigarOperation(83, 'M'),
CigarOperation(7, 'S')]);
r2.position = 60252;
r2["MD"] = "82T0";
auto r3 = BamRead("r3",
"CATAGGTGCCCATCCACCAAGGCAGGCTCTCCATCTTGCTCAGAGTGGCTCTAGCCCTTGCTGACTGCTGGGCAGGGAGAGAGCAGAGCT",
[CigarOperation(90, 'M')]);
r3.position = 60283;
r3["MD"] = "90";
auto r4 = BamRead("r4",
"CCCTTGCTGACTGCTGGGCAGGGAGAGAGCAGAGCTAACTTCCTCATGGGACCTGGGTGTGTCTGATCTGTGCACACCACTATCCAACCG",
[CigarOperation(90, 'M')]);
r4.position = 60337;
r4["MD"] = "90";
auto r5 = BamRead("r5",
"GAGGCTCCACCCTGGCCACTCTTGTGTGCACACAGCACAGCCTCTACTGCTACACCTGAGTACTTTGCCAGTGGCCTGGAAGCACTTTGT",
[CigarOperation(90, 'M')]);
r5.position = 60432;
r5["MD"] = "90";
auto reads = [r1, r2, r3, r4, r5];
assert(equal(dna(reads), "AGGTTTTGTGAGTGGGACAGTTGCAGCAAAACACAACCATAGGTGCCCATCCACCAAGGCAGGCTCTCCATCTTGCTCAGAGTGGCTCTAGCCCTTGCTGACTGCTGGGCAGGGAGAGAGCAGAGCTAACTTCCTCATGGGACCTGGGTGTGTCTGATCTGTGCACACCACTATCCAACCGNNNNNGAGGCTCCACCCTGGCCACTCTTGTGTGCACACAGCACAGCCTCTACTGCTACACCTGAGTACTTTGCCAGTGGCCTGGAAGCACTTTGT"));
}
|