| |
| |
| |
| |
| |
| |
| """Bio.SeqIO support for the "gck" file format. |
| |
| The GCK binary format is generated by the Gene Construction Kit software |
| from Textco BioSoftware, Inc. |
| """ |
|
|
| from struct import unpack |
|
|
| from Bio.Seq import Seq |
| from Bio.SeqFeature import SeqFeature |
| from Bio.SeqFeature import SimpleLocation |
| from Bio.SeqRecord import SeqRecord |
|
|
| from .Interfaces import SequenceIterator |
|
|
|
|
| def _read(stream, length): |
| """Read the specified number of bytes from the given stream.""" |
| data = stream.read(length) |
| if len(data) < length: |
| raise ValueError(f"Cannot read {length} bytes from stream") |
| return data |
|
|
|
|
| def _read_packet(stream): |
| """Read a length-prefixed packet. |
| |
| Parts of a GCK file are made of "packets" comprising of 4 bytes |
| giving the packet's size, followed by the packet's data. |
| |
| There is no type tag. The type of a packet, and thus the type of data |
| it contains, is solely indicated by the position of the packet within |
| the GCK file. |
| """ |
| length = stream.read(4) |
| if len(length) == 0: |
| return |
| if len(length) < 4: |
| raise ValueError("Cannot read packet size from stream") |
| length = unpack(">I", length)[0] |
| data = _read(stream, length) |
| return data |
|
|
|
|
| def _read_pstring(stream): |
| """Read a Pascal string. |
| |
| A Pascal string is one byte for length followed by the actual string. |
| """ |
| length = _read(stream, 1) |
| length = unpack(">B", length)[0] |
| data = _read(stream, length).decode("ASCII") |
| return data |
|
|
|
|
| def _read_p4string(stream): |
| """Read a 32-bit Pascal string. |
| |
| Similar to a Pascal string but length is encoded on 4 bytes. |
| """ |
| length = _read(stream, 4) |
| length = unpack(">I", length)[0] |
| data = _read(stream, length).decode("ASCII") |
| return data |
|
|
|
|
| class GckIterator(SequenceIterator): |
| """Parser for GCK files.""" |
|
|
| modes = "b" |
|
|
| def __init__(self, source): |
| """Break up a GCK file into SeqRecord objects. |
| |
| Note that a GCK file can only contain one sequence, so this |
| iterator will always return a single record. |
| """ |
| super().__init__(source, fmt="GCK") |
| |
| |
| |
| |
| |
| data = self.stream.read(24) |
| if not data: |
| raise ValueError("Empty file.") |
| if len(data) < 24: |
| raise ValueError("Improper header, cannot read 24 bytes from stream") |
|
|
| def __next__(self): |
| stream = self.stream |
| |
| packet = _read_packet(stream) |
| if packet is None: |
| raise StopIteration |
| |
| |
| seq_length = unpack(">I", packet[:4])[0] |
| |
| |
| if seq_length > len(packet) - 4: |
| raise ValueError("Conflicting sequence length values") |
| sequence = packet[4:].decode("ASCII") |
| record = SeqRecord(Seq(sequence)) |
|
|
| |
| packet = _read_packet(stream) |
| if packet is None: |
| raise ValueError("Premature end of file") |
|
|
| |
| packet = _read_packet(stream) |
| if packet is None: |
| raise ValueError("Premature end of file") |
| (seq_length, num_features) = unpack(">IH", packet[:6]) |
| |
| |
| if seq_length != len(sequence): |
| raise ValueError("Conflicting sequence length values") |
| |
| if len(packet) - 6 != num_features * 92: |
| raise ValueError( |
| "Features packet size inconsistent with number of features" |
| ) |
| for i in range(num_features): |
| offset = 6 + i * 92 |
| feature_data = packet[offset : offset + 92] |
|
|
| |
| |
| (start, end, type, strand, has_name, has_comment, version) = unpack( |
| ">II6xH14xB17xII35xB", feature_data |
| ) |
|
|
| if strand == 1: |
| strand = -1 |
| else: |
| |
| |
| |
| strand = 1 |
| location = SimpleLocation(start, end, strand=strand) |
|
|
| |
| if type > 0: |
| type = "CDS" |
| else: |
| type = "misc_feature" |
|
|
| |
| |
| |
| |
| |
| qualifiers = {} |
| if has_name > 0: |
| name = _read_pstring(stream) |
| qualifiers["label"] = [name] |
| if has_comment > 0: |
| comment = _read_p4string(stream) |
| qualifiers["note"] = [comment] |
|
|
| |
| |
| if version > 0: |
| continue |
|
|
| feature = SeqFeature(location, type=type, qualifiers=qualifiers) |
| record.features.append(feature) |
|
|
| |
| |
| |
| |
| |
| packet = _read_packet(stream) |
| if packet is None: |
| raise ValueError("Premature end of file") |
| (seq_length, num_sites) = unpack(">IH", packet[:6]) |
| |
| if len(packet) - 6 != num_sites * 88: |
| raise ValueError("Sites packet size inconsistent with number of sites") |
| for i in range(num_sites): |
| offset = 6 + i * 88 |
| site_data = packet[offset : offset + 88] |
|
|
| (start, end, has_name, has_comment) = unpack(">II24xII48x", site_data) |
|
|
| |
| if has_name: |
| _read_pstring(stream) |
| if has_comment: |
| _read_p4string(stream) |
|
|
| |
| packet = _read_packet(stream) |
| if packet is None: |
| raise ValueError("Premature end of file") |
|
|
| |
| |
| |
| |
| |
| num_versions = _read(stream, 2) |
| num_versions = unpack(">H", num_versions)[0] |
| versions = _read(stream, num_versions * 260) |
| for i in range(num_versions): |
| offset = i * 260 |
| version_data = versions[offset : offset + 260] |
|
|
| |
| |
| has_comment = unpack(">I", version_data[-4:])[0] |
| if has_comment > 0: |
| _read_p4string(stream) |
|
|
| |
| |
| |
| _read(stream, 706) |
|
|
| |
| name = _read_pstring(stream) |
| record.name = record.id = name.split(" ")[0] |
| record.description = name |
|
|
| |
| |
| |
| flags = _read(stream, 17) |
| circularity = unpack(">16xB", flags)[0] |
| if circularity > 0: |
| record.annotations["topology"] = "circular" |
| else: |
| record.annotations["topology"] = "linear" |
|
|
| return record |
|
|