| 1 | # esl_dsqdata: efficient sequence database format |
| 2 | |
| 3 | The `dsqdata` module implements a binary sequence data format. It |
| 4 | accelerates sequence data input in four ways, compared to Easel |
| 5 | flatfile parsers in `sqio`: |
| 6 | |
| 7 | * __Asynchronous input.__ |
| 8 | Disk and CPU resources are used concurrently, using POSIX threads. |
| 9 | A "loader" thread does essentially nothing but read chunks of |
| 10 | data. An "unpacker" thread does CPU work to prepare loaded |
| 11 | sequence data chunks for consumption. If it takes time $R$ to read |
| 12 | and $P$ to process the data, instead of overall time $R+P$, with |
| 13 | asynchronous input we only need time $\mathrm{max}(R,P)$. |
| 14 | |
| 15 | * __Predigitization.__ |
| 16 | Sequence data in the `dsqdata` format are already encoded in |
| 17 | Easel digital sequence format. User-oriented error checking is done |
| 18 | up front when the `dsqdata` file is created. |
| 19 | |
| 20 | * __Bit packing.__ |
| 21 | Disk read time is typically rate-limiting in HMMER and other |
| 22 | Easel-based programs, so minimizing data volume is critical. |
| 23 | Sequence data are packed bitwise in 32-bit packets to reduce volume |
| 24 | by a factor of 1.5 (protein) to 3.75 (nucleic). A packet contains |
| 25 | six 5-bit residues (protein or degenerate nucleic) or fifteen 2-bit |
| 26 | residues (canonical nucleic) and two control bits. |
| 27 | |
| 28 | * __Separate metadata.__ |
| 29 | Sequence data and metadata (name, accession, description, taxonomy |
| 30 | identifier) are stored separately in `.dsqs` and `.dsqm` |
| 31 | files. This streamlines unpacking, because these data are handled |
| 32 | differently. It also allows a deferred metadata read: sequences may |
| 33 | be identified simply by index number during an initial processing |
| 34 | sweep, and metadata can be loaded later by random access for a small |
| 35 | number of targets of interest. |
| 36 | |
| 37 | The following table lists the functions in the `dsqdata` API. |
| 38 | |
| 39 | | Function | Synopsis | |
| 40 | |--------------------------------|--------------------------------------------------------------| |
| 41 | | `esl_dsqdata_Open()` | Open a digital sequence database for reading | |
| 42 | | `esl_dsqdata_Read()` | Read next chunk of sequence data. | |
| 43 | | `esl_dsqdata_Recycle()` | Give a chunk back to the reader. | |
| 44 | | `esl_dsqdata_Close()` | Close a dsqdata reader. | |
| 45 | | `esl_dsqdata_Write()` | Create a dsqdata database | |
| 46 | |
| 47 | |
| 48 | ## dsqdata format's four files |
| 49 | |
| 50 | The format of a database `mydb` consists of four files: |
| 51 | |
| 52 | | File | Contents | Description | |
| 53 | |-------------|-----------|--------------------------------------------------------------| |
| 54 | | `mydb` | Stub | Human-readable information about the data | |
| 55 | | `mydb.dsqi` | Index | Disk offsets for each seq in metadata and sequence files | |
| 56 | | `mydb.dsqm` | Metadata | Name, accession, description, and taxonomy ids | |
| 57 | | `mydb.dsqs` | Sequence | Sequences (digitized, packed) | |
| 58 | |
| 59 | The database is specified on command lines by the name of the stub |
| 60 | file (`mydb`), without any suffix. For example, |
| 61 | |
| 62 | % myprogram mydb |
| 63 | |
| 64 | says to open `mydb`. The `esl_dsqdata_Open()` call then opens all four |
| 65 | files. |
| 66 | |
| 67 | |
| 68 | ## definition of dsqdata file formats |
| 69 | |
| 70 | ### the stub file |
| 71 | |
| 72 | An example stub file: |
| 73 | |
| 74 | ``` |
| 75 | Easel dsqdata v1 x4019752601 |
| 76 | |
| 77 | Original file: refprot.fa |
| 78 | Original format: FASTA |
| 79 | Type: amino |
| 80 | Sequences: 11432138 |
| 81 | Residues: 4358716588 |
| 82 | ``` |
| 83 | |
| 84 | The first line is the only line in the stub file that's parsed by the |
| 85 | reader. Its text format matches `/Easel dsqdata v(\d+) x(\d+)/`. The first |
| 86 | field is a version number for the format, $\geq$ 1. It is currently unused, but |
| 87 | in the future we might need it to parse different versions of the format, if we |
| 88 | need to update it. The second field is a 32-bit unsigned integer tag in the |
| 89 | range 0..$2^{32}-1$. Each of the four files carries the same randomly generated |
| 90 | tag. The tag is used to ascertain that the four files belong together in the |
| 91 | same database, as opposed to one or more of them being inadvertently clobbered |
| 92 | somehow by the user. |
| 93 | |
| 94 | After the first line, the rest of the stub file is ignored by the Easel reader, |
| 95 | and can contain anything -- even your own notes, if you want to add any. The |
| 96 | text here is the useful information that the Easel writer writes by default. |
| 97 | |
| 98 | ### the .dsqi index file |
| 99 | |
| 100 | The purpose of the .dsqi index file is to (quickly) give us data offsets (in |
| 101 | bytes) we need to randomly access the metadata and sequence data for sequence |
| 102 | number 0..nseq-1 in the .dsqm and .dsqs files, or for any specified range of |
| 103 | sequences. |
| 104 | |
| 105 | The header of the binary index file consists of: |
| 106 | |
| 107 | | name | type | description | |
| 108 | |--------------|------------|----------------------------------------------| |
| 109 | | magic | `uint32_t` | magic number (version, byte order) | |
| 110 | | uniquetag | `uint32_t` | random integer tag (0..$2^{32}-1$) | |
| 111 | | alphatype | `uint32_t` | alphabet type code (1,2,3 = RNA, DNA, amino) | |
| 112 | | flags | `uint32_t` | Currently 0. Reserved for future flags | |
| 113 | | max_namelen | `uint32_t` | Maximum seq name length in metadata | |
| 114 | | max_acclen | `uint32_t` | Maximum accession length in metadata | |
| 115 | | max_desclen | `uint32_t` | Maximum description length in metadata | |
| 116 | | max_seqlen | `uint64_t` | Maximum sequence length | |
| 117 | | nseq | `uint64_t` | Total number of sequences in database | |
| 118 | | nres | `uint64_t` | Total number of residues in database | |
| 119 | |
| 120 | The **magic** is used to check that the file is indeed a dsqdata |
| 121 | format file, and to detect byte order swapping. Valid values for the |
| 122 | magic version/byteorder number are: |
| 123 | |
| 124 | | value | derivation | description | |
| 125 | |------------|---------------------|--------------------------| |
| 126 | | 0xc4d3d1b1 | "dsq1" + 0x80808080 | dsqdata version 1 format | |
| 127 | | 0xb1d1d3c4 | above, byteswapped | above, byteswapped | |
| 128 | |
| 129 | The **uniquetag** matches the tag seen in the other files. |
| 130 | |
| 131 | The dsqdata packet format is only defined for biological sequence alphabets. |
| 132 | Valid integer values for the **alphatype** code come from a subset of the codes |
| 133 | used in `esl_alphabet.h`: |
| 134 | |
| 135 | | value | `esl_alphabet.h` | description | |
| 136 | |-------|------------------|-------------| |
| 137 | | 1 | `eslRNA` | RNA | |
| 138 | | 2 | `eslDNA` | DNA | |
| 139 | | 3 | `eslAMINO` | protein | |
| 140 | |
| 141 | The unused **flags** field gives us some flexibility for future |
| 142 | versions of the format. |
| 143 | |
| 144 | The maximum lengths of the names, accessions, and descriptions in the |
| 145 | metadata file might someday be useful (in making allocations, for |
| 146 | example) but they are currently unused by the Easel reader. |
| 147 | |
| 148 | Likewise, the maximum sequence length, total number of sequences, and |
| 149 | total number of residues in the database may someday be useful (for |
| 150 | making decisions about how to partition a parallel search, for |
| 151 | example), but they are currently unused too. |
| 152 | |
| 153 | After the header, the remainder of the file consists of `nseq` |
| 154 | records of type `ESL_DSQDATA_RECORD` (defined in |
| 155 | `esl_dsqdata.h`): |
| 156 | |
| 157 | | element | type | description | |
| 158 | |----------------|-----------|-------------------------------------------------------------------------| |
| 159 | | `metadata_end` | `int64_t` | Position of terminal `\0` of metadata for seq i in .dsqm file, in bytes | |
| 160 | | `psq_end` | `int64_t` | Position of final packet for sequence i in .dsqs file, in packets | |
| 161 | |
| 162 | Storing _end_ positions instead of _start_ positions allows |
| 163 | us to determine lengths, without needing an n+1'th sentinel record, |
| 164 | albeit at the cost of special casing what happens for the first |
| 165 | sequence i=0. For example: |
| 166 | |
| 167 | ``` |
| 168 | len[i] = (i == 0 ? r[i].end + 1 : r[i].end - r[i-1].end) |
| 169 | start[i] = (i == 0 ? 0 : r[i-1].end + 1) |
| 170 | ``` |
| 171 | |
| 172 | This is equivalent to treating `r[-1].end = -1`. Some of the Easel reader's code |
| 173 | tracks a `last_end` variable for the end of the previous metadata or packed |
| 174 | sequence field i-1, which is initialized to -1. This -1 boundary condition is |
| 175 | why we use _signed_ `int64_t` types. |
| 176 | |
| 177 | Packet sequence endpoints are stored in units of unsigned 32-bit binary |
| 178 | _packets_, not in bytes. To convert to a disk offset or a length in bytes you |
| 179 | multiply by 4 (`sizeof(uint32_t)`). |
| 180 | |
| 181 | Keeping the size of the dsqdata files as small as possible is critical |
| 182 | because the reading speed is limited by the raw size of the |
| 183 | data. Therefore we don't store separate positions for the different |
| 184 | metadata fields (name/accession/description/taxonomy id); only one |
| 185 | position for all the metadata associated with sequence i. The reader |
| 186 | reads all of it in one chunk, and parses it for the stored `\0` |
| 187 | sentinels. |
| 188 | |
| 189 | For the same reason, we don't store any information about _unpacked_ sequence |
| 190 | lengths, only the bare minimum of information that the dsqdata loader and |
| 191 | unpacker need to locate, load, and unpack the packed data for any given sequence |
| 192 | i. The unpacker determines the unpacked sequence length when it unpacks the |
| 193 | data. |
| 194 | |
| 195 | |
| 196 | ### the .dsqm metadata file |
| 197 | |
| 198 | The metadata file starts with two header fields, the same two that the |
| 199 | index file starts with: |
| 200 | |
| 201 | | name | type | description | |
| 202 | |-----------|------------|------------------------------------| |
| 203 | | magic | `uint32_t` | magic number (version, byte order) | |
| 204 | | uniquetag | `uint32_t` | random integer tag (0..$2^32-1$) | |
| 205 | |
| 206 | After the header, the remainder of the file consists of the following |
| 207 | data for each sequence i = 0..nseq-1: |
| 208 | |
| 209 | | field | type | description | |
| 210 | |--------------|-----------------------------|-----------------------------------------------------| |
| 211 | | name | `char` array ending in `\0` | sequence name (1 word, no whitespace); mandatory | |
| 212 | | accession | `char` array ending in `\0` | sequence accession (1 word, no whitespace); or "\0" | |
| 213 | | description | `char` array ending in `\0` | sequence description line; or "\0" | |
| 214 | | taxonomy id | `int32_t` | NCBI taxonomy identifier; or -1 | |
| 215 | |
| 216 | |
| 217 | The name, accession, and description are variable length strings. The name and |
| 218 | accession are single "words" with no whitespace (`\S+`). The description is one |
| 219 | line, may contain spaces, but may not contain any newlines. All sequences must |
| 220 | have a name, so `strlen(name) > 0`. The accession and description are optional; |
| 221 | if they are not present, these are 0-length strings ("\0"). |
| 222 | |
| 223 | The taxonomy identifier is an integer in NCBI's taxonomy. Valid taxonomy |
| 224 | identifiers are $\geq 1$. This field is optional; use a value of -1 to indicate |
| 225 | unset. |
| 226 | |
| 227 | (I cannot find any documentation at NCBI on the maximum range of the taxid, nor |
| 228 | can I find a clear statement of whether 0 is valid or not. 0 is currently unused |
| 229 | in the NCBI taxonomy. 1 indicates the top level. That makes it look like it's |
| 230 | safe to treat 0 as "unset" but it seems even safer to go with -1 and a signed |
| 231 | integer. Unless NCBI ends up having more than two billion species. Currently |
| 232 | there are about 1.8 million.) |
| 233 | |
| 234 | These names, types, and semantics match the corresponding fields in an `ESL_SQ`. |
| 235 | |
| 236 | ### the .dsqs sequence file |
| 237 | |
| 238 | The sequence file also starts with the same two header fields that the |
| 239 | index and metadata files started with: |
| 240 | |
| 241 | | name | type | description | |
| 242 | |-----------|------------|------------------------------------| |
| 243 | | magic | `uint32_t` | magic number (version, byte order) | |
| 244 | | uniquetag | `uint32_t` | random integer tag (0..$2^32-1$) | |
| 245 | |
| 246 | After the header, the remainder of the file consists of the packed |
| 247 | sequences, with one packet array for each sequence i = 0..nseq-1. Each |
| 248 | packet array ends with a specially marked sentinel packet. The packet |
| 249 | format is described next. |
| 250 | |
| 251 | #### packet format |
| 252 | |
| 253 | Each packet is an unsigned 32 bit integer. The two leading (most |
| 254 | significant) bits are control bits. Bit 31 signals EOD (end of data): |
| 255 | the last packet in a packed sequence. Bit 30 signals the packet |
| 256 | format: 1 for 5-bit, 0 for 2-bit. The remaining bits are the packed |
| 257 | residue codes: |
| 258 | |
| 259 | ``` |
| 260 | [31] [30] [29..25] [24..20] [19..15] [14..10] [ 9..5 ] [ 4..0 ] |
| 261 | ^ ^ |------------ 6 5-bit packed residues ------------------| |
| 262 | | | [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] |
| 263 | | | |----------- or 15 2-bit packed residues ----------------| |
| 264 | | | |
| 265 | | "packtype" bit 30 = 0 if packet is 2-bit packed; 1 if 5-bit packed |
| 266 | "sentinel" bit 31 = 1 if last packet in packed sequence; else 0 |
| 267 | |
| 268 | (packet & (1 << 31)) tests for end of sequence |
| 269 | (packet & (1 << 30)) tests for 5-bit packing vs. 2-bit |
| 270 | ((packet >> shift) && 31) decodes 5-bit, for shift=25..0 in steps of 5 |
| 271 | ((packet >> shift) && 3) decodes 2-bit, for shift=28..0 in steps of 2 |
| 272 | ``` |
| 273 | |
| 274 | Packets without the sentinel bit set are full. They unpack to 15 or 6 |
| 275 | residues. |
| 276 | |
| 277 | 5-bit EOD packets may be partial: they unpack to 0..6 residues. The |
| 278 | remaining residue codes are set to 0x1f (11111), indicating EOD within |
| 279 | the packet. The only case in which a partial EOD packet encodes 0 |
| 280 | residues is a zero-length sequence: there has to be at least one EOD |
| 281 | packet. |
| 282 | |
| 283 | 2-bit EOD packets must be full, because there is no way to signal EOD |
| 284 | locally within a 2-bit packet. It can't use 0x03 (11), because that |
| 285 | encodes U/T. Generally, therefore, the last packet(s) of a nucleic |
| 286 | acid sequence must be 5-bit encoded, solely to be able to use sentinel |
| 287 | residues in a partial packet, unless the end happens to come flush at |
| 288 | the end of a 2-bit packet. (If we ever needed to pack an alphabet of 2 |
| 289 | or 3 residues, we could use 0x03 as a sentinel. This seems unlikely |
| 290 | to ever happen, so I'm simply not going to include any code to read |
| 291 | EOD 2-bit partial packets.) |
| 292 | |
| 293 | A protein sequence of length L packs into exactly P $= \mathrm{max}(1, |
| 294 | (L+5)/6)$ 5-bit packets. A DNA sequence packs into P $\leq \mathrm{max}(1, |
| 295 | (L+14)/15)$ mixed 2- and 5-bit packets. P $\geq 1$ because even a |
| 296 | zero-length sequence ($L=0$) requires an EOD packet. |
| 297 | |
| 298 | A packed sequence consists of an integer number of packets, P, ending |
| 299 | with an EOD packet. |
| 300 | |
| 301 | A packed amino acid sequence unpacks to $\leq$ 6P residues. All its |
| 302 | packets are 5-bit encoded. |
| 303 | |
| 304 | A packed nucleic acid sequence unpacks to $\leq$ 15P residues. The |
| 305 | packets are a mix of 2-bit and 5-bit. Degenerate residues must be |
| 306 | 5-bit packed, and the EOD packet usually is too. A 5-bit packet does |
| 307 | not have to contain degenerate residues, because it might have been |
| 308 | necessary to get "in frame" to pack a downstream degenerate |
| 309 | residue. For example, the sequence ACGTACGTNNA... must be packed as |
| 310 | [ACGTAC][CGTNNA]... to get the N's packed correctly. |
| 311 | |
| 312 |