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
|
# Copyright 2022 by Michiel de Hoon. All rights reserved.
#
# This file is part of the Biopython distribution and governed by your
# choice of the "Biopython License Agreement" or the "BSD 3-Clause License".
# Please see the LICENSE file that should have been included as part of this
# package.
"""Bio.Align support for the "bigmaf" multiple alignment format.
The bigMaf format stores multiple alignments in a format compatible with
the MAF (Multiple Alignment Format) format. BigMaf files are binary and are
indexed as a bigBed file.
See https://genome.ucsc.edu/goldenPath/help/bigMaf.html
"""
import re
import struct
import zlib
import numpy as np
from Bio.Align import _aligncore # type: ignore
from Bio.Align import Alignment
from Bio.Align import Alignments
from Bio.Align import bigbed
from Bio.Align import maf
from Bio.Align.bigbed import AutoSQLTable
from Bio.Align.bigbed import Field
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
declaration = AutoSQLTable(
"bedMaf",
"Bed3 with MAF block",
[
Field(
as_type="string",
name="chrom",
comment="Reference sequence chromosome or scaffold",
),
Field(
as_type="uint",
name="chromStart",
comment="Start position in chromosome",
),
Field(
as_type="uint",
name="chromEnd",
comment="End position in chromosome",
),
Field(
as_type="lstring",
name="mafBlock",
comment="MAF block",
),
],
)
class AlignmentWriter(bigbed.AlignmentWriter):
"""Alignment file writer for the bigMaf file format."""
fmt = "bigMaf"
def __init__(
self,
target,
targets=None,
compress=True,
blockSize=256,
itemsPerSlot=512,
):
"""Create an AlignmentWriter object.
Arguments:
- target - output stream or file name.
- targets - A list of SeqRecord objects with the chromosomes in
the order as they appear in the alignments. The
sequence contents in each SeqRecord may be undefined,
but the sequence length must be defined, as in this
example:
SeqRecord(Seq(None, length=248956422), id="chr1")
If targets is None (the default value), the alignments
must have an attribute .targets providing the list of
SeqRecord objects.
- compress - If True (default), compress data using zlib.
If False, do not compress data.
Use compress=False for faster searching.
- blockSize - Number of items to bundle in r-tree.
See UCSC's bedToBigBed program for more information.
Default value is 256.
- itemsPerSlot - Number of data points bundled at lowest level.
See UCSC's bedToBigBed program for more information.
Use itemsPerSlot=1 for faster searching.
Default value is 512.
"""
super().__init__(
target,
bedN=3,
declaration=declaration,
targets=targets,
compress=compress,
blockSize=blockSize,
itemsPerSlot=itemsPerSlot,
)
def write_file(self, stream, alignments):
"""Write the file."""
fixed_alignments = Alignments()
for alignment in alignments:
if not isinstance(alignment, Alignment):
raise TypeError("Expected an Alignment object")
mafBlock = format(alignment, "maf")[:-1].replace("\n", ";")
coordinates = alignment.coordinates
if not coordinates.size: # alignment consists of gaps only
continue
alignment = alignment[:2]
reference, chromosome = alignment.target.id.split(".", 1)
alignment.target.id = chromosome
assert coordinates[0, 0] < coordinates[0, -1]
alignment.annotations = {}
alignment.annotations["mafBlock"] = mafBlock
fixed_alignments.append(alignment)
fixed_alignments.sort(
key=lambda alignment: (alignment.target.id, alignment.coordinates[0, 0])
)
record = alignments.targets[0]
reference, chromosome = record.id.split(".", 1)
targets = list(alignments.targets)
targets[0] = SeqRecord(record.seq, id=chromosome)
fixed_alignments.targets = targets
bigbed.AlignmentWriter(
stream, bedN=3, declaration=declaration, compress=self.compress
).write(fixed_alignments)
class AlignmentIterator(bigbed.AlignmentIterator, maf.AlignmentIterator):
"""Alignment iterator for bigMaf files.
The file may contain multiple alignments, which are loaded and returned
incrementally.
Alignment annotations are stored in the ``.annotations`` attribute of the
``Alignment`` object, except for the alignment score, which is stored as an
attribute. Sequence information of empty parts in the alignment block
(sequences that connect the previous alignment block to the next alignment
block, but do not align to the current alignment block) is stored in the
alignment annotations under the ``"empty"`` key. Annotations specific to
each line in the alignment are stored in the ``.annotations`` attribute of
the corresponding sequence record.
"""
fmt = "bigMaf"
mode = "b"
def __init__(self, source):
"""Create an AlignmentIterator object.
Arguments:
- source - input file stream, or path to input file
"""
self.reference = None
super().__init__(source)
def _read_reference(self, stream):
# Supplemental Table 12: Binary BED-data format
# chromId 4 bytes, unsigned
# chromStart 4 bytes, unsigned
# chromEnd 4 bytes, unsigned
# rest zero-terminated string in tab-separated format
formatter = struct.Struct(self.byteorder + "III")
size = formatter.size
node = self.tree
while True:
try:
children = node.children
except AttributeError:
break
else:
node = children[0]
filepos = stream.tell()
stream.seek(node.dataOffset)
dataSize = 256
data = b""
compressed_data = b""
while True:
chunk = stream.read(dataSize)
if self._compressed:
compressed_data += chunk
decompressor = zlib.decompressobj()
data = decompressor.decompress(compressed_data)
else:
data += chunk
try:
i = data.index(b";s", size)
except ValueError:
continue
words = data[i + 1 :].split()
if len(words) > 2:
break
name = words[1]
stream.seek(filepos)
reference, chromosome = name.split(b".", 1)
return reference.decode()
def _read_header(self, stream):
super()._read_header(stream)
if self.reference is None:
self.reference = self._read_reference(stream)
self._index = 0
self.targets[0].id = "%s.%s" % (self.reference, self.targets[0].id)
def _create_alignment(
self, chromId, chromStart, chromEnd, data, dataStart, dataEnd
):
assert data[dataEnd - 1] == 0
buffer = memoryview(data)
buffer = buffer[dataStart:dataEnd]
records = []
strands = []
annotations = {}
score = None
printed_alignment_parser = _aligncore.PrintedAlignmentParser(b";")
j = -1
sequences = []
while True:
i = j + 1
prefix = buffer[i : i + 1]
if prefix == b"#":
m = re.match(b"^[^;]*", buffer[i:])
j = i + m.span()[1]
elif prefix == b"a":
m = re.match(b"^[^;]*", buffer[i:])
j = i + m.span()[1]
line = buffer[i:j].tobytes()
words = line[1:].split()
for word in words:
key, value = word.split(b"=")
if key == b"score":
score = float(value)
elif key == b"pass":
value = int(value)
if value <= 0:
raise ValueError(
"pass value must be positive (found %d)" % value
)
annotations["pass"] = value
else:
raise ValueError(
"Unknown annotation variable '%s'" % key.decode()
)
elif prefix == b"s":
m = re.match(rb"^s\s*\S*\s*\d*\s*\d*\s*[+-]\s*\d*\s*", buffer[i:])
j = i + m.span()[1]
line = buffer[i:j].tobytes()
words = line.split(None, 5)
if len(words) != 6:
raise ValueError(
"Error parsing alignment - 's' line must have 7 fields"
)
src = words[1].decode()
start = int(words[2])
size = int(words[3])
strand = words[4]
srcSize = int(words[5])
i = j
n, sequence = printed_alignment_parser.feed(data, dataStart + i)
if len(sequence) != size:
raise ValueError(
"sequence size is incorrect (found %d, expected %d)"
% (len(sequence), size)
)
seq = Seq({start: sequence}, length=srcSize)
record = SeqRecord(seq, id=src, name="", description="")
records.append(record)
j = i + n
i = j + 1
strands.append(strand)
elif prefix == b"i":
m = re.match(b"^[^;]*", buffer[i:])
j = i + m.span()[1]
line = buffer[i:j].tobytes()
words = line.split(None, 5)
assert len(words) == 6
assert words[1].decode() == src # from the previous "s" line
leftStatus = words[2].decode()
leftCount = int(words[3])
rightStatus = words[4].decode()
rightCount = int(words[5])
assert leftStatus in AlignmentIterator.status_characters
assert rightStatus in AlignmentIterator.status_characters
record.annotations["leftStatus"] = leftStatus
record.annotations["leftCount"] = leftCount
record.annotations["rightStatus"] = rightStatus
record.annotations["rightCount"] = rightCount
elif prefix == b"e":
m = re.match(b"^[^;]*", buffer[i:])
j = i + m.span()[1]
line = buffer[i:j].tobytes()
words = line.split(None, 6)
assert len(words) == 7
src = words[1].decode()
start = int(words[2])
size = int(words[3])
strand = words[4]
srcSize = int(words[5])
status = words[6].decode()
assert status in AlignmentIterator.empty_status_characters
sequence = Seq(None, length=srcSize)
record = SeqRecord(sequence, id=src, name="", description="")
end = start + size
if strand == b"+":
segment = (start, end)
else:
segment = (srcSize - start, srcSize - end)
empty = (record, segment, status)
annotation = annotations.get("empty")
if annotation is None:
annotation = []
annotations["empty"] = annotation
annotation.append(empty)
elif prefix == b"q":
m = re.match(b"^[^;]*", buffer[i:])
j = i + m.span()[1]
line = buffer[i:j].tobytes()
words = line.split(None, 2)
assert len(words) == 3
assert words[1].decode() == src # from the previous "s" line
value = words[2].replace(b"-", b"")
record.annotations["quality"] = value.decode()
elif prefix == b"\00":
# reached the end of the alignment
break
else:
raise ValueError(f"Error parsing alignment - unexpected line:\n{line}")
shape = printed_alignment_parser.shape
coordinates = np.empty(shape, np.int64)
printed_alignment_parser.fill(coordinates)
for record, strand, row in zip(records, strands, coordinates):
if strand == b"+":
row += record.seq.defined_ranges[0][0]
else: # strand == b"-"
record.seq = record.seq.reverse_complement()
row[:] = record.seq.defined_ranges[0][1] - row
alignment = Alignment(records, coordinates)
if annotations is not None:
alignment.annotations = annotations
if score is not None:
alignment.score = score
return alignment
|