File: qdna.py

package info (click to toggle)
python-bx 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,000 kB
  • sloc: python: 17,136; ansic: 2,326; makefile: 24; sh: 8
file content (272 lines) | stat: -rw-r--r-- 10,253 bytes parent folder | download
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
"""
Classes to support "quantum-DNA" files.

:Author: Bob Harris (rsharris@bx.psu.edu)

A quantum DNA sequence is a sequence of bytes, each representing a probability
distribution (vector) over A, C, G, T.  The QdnaFile class encapsulates the
sequence of bytes, while the mapping from byte value to probability vector is
encapsulated by the QdnaCodebook class.

qdna file format
~~~~~~~~~~~~~~~~

Fields can be in big- or little-endian format;  they must match the endianess
of the magic number.

============ ===========   ======================================================
offset 0x00: C4 B4 71 97   big endian magic number (97 71 B4 C4 => little endian)
offset 0x04: 00 00 02 00   version 2.0 (fourth byte is sub version)
offset 0x08: 00 00 00 14   header length (in bytes, including this field)
offset 0x0C: xx xx xx xx   S, offset (from file start) to data sequence
offset 0x10: xx xx xx xx   N, offset to name, 0 indicates no name
offset 0x14: xx xx xx xx   length of data sequence (counted in 'items')
offset 0x18: xx xx xx xx   (for version >= 2.0) P, offset to named
                           .. properties, 0 indicates no properties
offset    N: ...           name (zero-terminated string)
offset    S: ...           data sequence
offset    P: ...           named properties (see below)
============ ===========   ======================================================

The named properties section consists of a list of pairs of zero-terminated
strings.  The list itself is terminated by an empty string (i.e. another
zero).  In each pair, the first is the name of the property and the second
is its value.  Some names are recognized and handled in some specific manner
(see list below this paragraph).  Any unrecognized name is simply added as
an instance variable with that name, as long as it is not already an instance
variable (in which case it is an error).

Recognized properties (at present only one):
  - codebook: A string in qdna code file format (see QdnaCodebook class for details).
"""

import struct
from io import StringIO

from bx.seq.seq import (
    SeqFile,
    SeqReader,
)

qdnaMagic = 0xC4B47197  # big endian magic number for qdna files
qdnaMagicSwap = 0x9771B4C4


class QdnaFile(SeqFile):
    def __init__(self, file, revcomp=False, name="", gap=None, codebook=None):
        SeqFile.__init__(self, file, revcomp, name, gap)
        if gap is None:
            self.gap = chr(0)
        assert not revcomp, "reverse complement is not supported for qdna files"
        self.codebook = codebook

        self.byte_order = ">"
        magic = struct.unpack(">L", file.read(4))[0]
        if magic != qdnaMagic:
            if magic == qdnaMagicSwap:
                self.byte_order = "<"
            else:
                raise ValueError(f"not a quantum-dna file (magic={magic:08X})")

        self.magic = magic

        # process header

        self.version = struct.unpack(f"{self.byte_order}L", self.file.read(4))[0]
        if self.version not in [0x100, 0x200]:
            raise ValueError(f"unsupported quantum-dna (version={self.version:08X})")

        self.headerLength = struct.unpack(f"{self.byte_order}L", self.file.read(4))[0]
        if self.headerLength < 0x10:
            raise ValueError(f"unsupported quantum-dna (header len={self.headerLength:08X})")
        if self.version == 0x100 and self.headerLength != 0x10:
            raise ValueError(f"unsupported quantum-dna (version 1.0 header len={self.headerLength:08X})")

        self.seqOffset = struct.unpack(f"{self.byte_order}L", self.file.read(4))[0]
        self.nameOffset = struct.unpack(f"{self.byte_order}L", self.file.read(4))[0]
        self.length = struct.unpack(f"{self.byte_order}L", self.file.read(4))[0]

        self.propOffset = 0
        if self.headerLength >= 0x14:
            self.propOffset = struct.unpack(f"{self.byte_order}L", self.file.read(4))[0]

        self.name = ""
        if self.nameOffset != 0:
            self.file.seek(self.nameOffset)
            self.name = self.read_string()

        if self.propOffset != 0:
            self.file.seek(self.propOffset)
            while True:
                name = self.read_string()
                if len(name) == 0:
                    break
                value = self.read_string()
                self.set_property(name, value)

    def set_property(self, name, value):
        if name == "codebook":
            self.codebook = QdnaCodebook(StringIO(value))
        else:
            raise Exception("named properties as instance variables are not implemented yet")
            # $$$ do this by adding a properties dict and __getitem__/__setitem__
            # $$$ also need to write properties in QdnaWriter.write()

    def read_string(self):
        s = b""
        while True:
            ch = self.file.read(1)
            if ch == b"\0":
                break
            s += ch
        if not isinstance(s, str):
            return s.decode()
        return s

    def raw_fetch(self, start, length):
        self.file.seek(self.seqOffset + start)
        return self.file.read(length).decode()

    def get_quantum(self, start, length):
        assert self.codebook is not None, f"qdna sequence {self.name} has no code book"
        return [self.codebook[codeNum] for codeNum in self.raw_fetch(start, length)]


class QdnaReader(SeqReader):
    def __init__(self, file, revcomp=False, name="", gap=None, codebook=None):
        SeqReader.__init__(self, file, revcomp, name, gap)
        self.codebook = codebook

    def __next__(self):
        if self.seqs_read != 0:
            return  # qdna files have just one sequence
        seq = QdnaFile(self.file, self.revcomp, self.name, self.gap, self.codebook)
        self.seqs_read += 1
        return seq


"""
A QdnaCodebook maps code numbers to the corresponding probability vector.  The
latter is a hash from symbols (usually "A", "C", "G", or "T") to the
corresponsing probability.  Note that code numbers are of type string.

qdna code file format:

   The file is ascii text and looks something like what's shown below.  Lines
   beginning with # are comments, and columns are assumed to represent A, C, G
   and T (in that order).  Anything other than five columns is an error.  Note
   that code number zero is usually reserved for gaps in quantum sequences, and
   thus usually won't appear in a code file.  Note that code numbers are
   two-digit hexadecimal (to match the textual displays of quantum sequences).

      01  0.111002  0.072588  0.127196  0.689214
      02  0.081057  0.023799  0.098657  0.796487
      03  0.000260  0.003823  0.000336  0.995581
       ... more lines, usually a total of 255 ...
      FF  0.465900  0.008602  0.482301  0.043197
"""


class QdnaCodebook:
    def __init__(self, file):
        (self.alphabet, self.codeToProbs) = self.read_codebook(file)

    def __str__(self):
        codeSet = sorted(codeNum for codeNum in self.codeToProbs)
        return "\n".join([self.vector_text(codeNum) for codeNum in codeSet])

    def vector_text(self, codeNum):
        if codeNum in self.codeToProbs:
            vec = self.codeToProbs[codeNum]
        else:
            vec = {}
        for sym in self.alphabet:
            if sym not in vec:
                vec[sym] = 0.0
        return (f"{ord(codeNum):02X}\t") + "\t".join([f"{vec[sym]:.6f}" for sym in self.alphabet])

    def __getitem__(self, codeNum):
        return self.codeToProbs[codeNum]

    def __setitem__(self, codeNum, value):
        self.codeToProbs[codeNum] = value  # value should be hash from symbol to probability

    def read_codebook(self, codeF):
        alphabet = "ACGT"
        codeToProbs = {}

        for lineNum, line in enumerate(codeF):
            lineNum += 1
            line = line.rstrip()
            stripped = line.strip()
            if stripped == "" or stripped.startswith("#"):
                continue

            fields = line.split(None)
            if len(fields) != 5:
                raise ValueError("wrong vector size (line %d)" % lineNum)

            try:
                codeNum = int(fields[0], 16)
            except ValueError:
                raise ValueError("bad character code %s (line %d)" % (fields[0], lineNum))

            if not 0 <= codeNum <= 255:
                raise ValueError("character code %s is outside the valid range (line %d)" % (fields[0], lineNum))

            if chr(codeNum) in codeToProbs:
                raise ValueError("character code %s appears more than once (line %d)" % (fields[0], lineNum))

            try:
                vec = {}
                for ix in range(1, 5):
                    p = float(fields[ix])
                    if p < 0 or p > 1:
                        raise ValueError
                    vec[alphabet[ix - 1]] = p
            except Exception:
                raise ValueError("%s is a bad probability value (line %d)" % (fields[ix], lineNum))

            codeToProbs[chr(codeNum)] = vec

        return (alphabet, codeToProbs)


class QdnaWriter:
    def __init__(self, file):
        self.file = file

    def write(self, seq):
        text = seq.text
        if text is None:
            text = ""

        version = 0x200
        headerLen = 0x014
        offset = headerLen + 8

        nameOffset = 0
        if seq.name is not None and seq.name != "":
            nameOffset = 0x01C
            offset += len(seq.name) + 1
            name = seq.name + chr(0)

        dataOffset = offset
        offset += len(text)

        assert seq.codebook is None, "QdnaWriter.write() does not support codebooks yet"
        propOffset = 0

        self.file.write(struct.pack(f"{seq.byte_order}L", qdnaMagic))
        self.file.write(struct.pack(f"{seq.byte_order}L", version))
        self.file.write(struct.pack(f"{seq.byte_order}L", headerLen))
        self.file.write(struct.pack(f"{seq.byte_order}L", dataOffset))
        self.file.write(struct.pack(f"{seq.byte_order}L", nameOffset))
        self.file.write(struct.pack(f"{seq.byte_order}L", len(text)))
        self.file.write(struct.pack(f"{seq.byte_order}L", propOffset))
        if nameOffset != 0:
            self.file.write(name)
        self.file.write(text)

    def close(self):
        self.file.close()