Debian Codegraph @

easel/esl_ssi.tex

raw
1 The \eslmod{ssi} module is for creating and using ``SSI''
2 (sequence/subsequence index) files. SSI indexes flatfile databases by
3 names and/or accessions, enabling fast sequence record retrieval.
4
5 An SSI index is a binary file that stores sequence names or accessions
6 as \emph{keys}, associating them with information about the sequence
7 record, including its location (file name and disk offset), so that it
8 can be looked up rapidly. It differentiates between \emph{primary
9 keys} and \emph{secondary keys} (aliases). There is one and only one
10 primary key per record. There can be more than one secondary key
11 (alias) per sequence. Both primary and secondary keys must be unique
12 identifiers (no two records have the same key). For example, a program
13 for sequence retrieval might create an SSI index with accessions as
14 primary keys and names as secondary keys (or the other way around).
15
16 Records can also be retrieved by number from the list of primary keys.
17 This may be useful for distributed data-parallel applications, which
18 can use SSI to rapidly position individual processes at different
19 record ranges in a flatfile database.
20
21 A single SSI file can index a sequence database that consists of more
22 than one individual sequence file. For example, the GenBank database
23 is distributed as a large number of flatfiles; one SSI file can index
24 them all.
25
26 When records are consistently formatted, SSI indices can allow a
27 specific subsequence in a sequence record to be identified
28 rapidly. This is useful when the sequence records are very large, such
29 as whole assembled genomes or chromosomes.
30
31 Although SSI indices are designed with sequence databases in mind, SSI
32 can also be used to index records in other types of flatfile
33 databases. For example, HMMER uses SSI to index HMM databases, and the
34 \eslmod{msa} module can use SSI to index Stockholm format multiple
35 alignment databases like Pfam and Rfam.
36
37 SSI can handle large amounts of data. It is capable of indexing tens
38 of thousands of files and hundreds of trillions of sequence records.
39 The lengths of primary keys, secondary keys, or filenames are
40 effectively unlimited, and individual sequence records may be many
41 trillions of residues long, orders of magnitude larger than the
42 largest complete chromosomes. SSI indexing is effectively limited only
43 by the size of the SSI index file itself (up to 8 exabytes, on 64-bit
44 filesystems).
45
46 Binary SSI indices are portable between different
47 machines.\footnote{The sole exception is that SSI indices built for
48 64-bit filesystems might not be readable on a fully 32-bit
49 filesystem.}
50
51 Table~\ref{tbl:ssi_api} lists the functions in the \eslmod{ssi} API.
52 A \eslmod{ESL\_SSI} object is used for reading an index, and a
53 \eslmod{ESL\_NEWSSI} object is used for creating one. There is also a
54 set of functions for portable binary file i/o.
55
56
57 % Table generated by autodoc -t esl_ssi.c (so don't edit here, edit esl_ssi.c:)
58 \begin{table}[hbp]
59 \begin{center}
60 {\small
61 \begin{tabular}{|ll|}\hline
62 \apisubhead{Using (reading) an SSI index.}\\
63 \hyperlink{func:esl_ssi_Open()}{\ccode{esl\_ssi\_Open()}} & Open an SSI index as an \ccode{ESL\_SSI}.\\
64 \hyperlink{func:esl_ssi_FindName()}{\ccode{esl\_ssi\_FindName()}} & Look up a primary or secondary key.\\
65 \hyperlink{func:esl_ssi_FindNumber()}{\ccode{esl\_ssi\_FindNumber()}} & Look up the n'th primary key.\\
66 \hyperlink{func:esl_ssi_FindSubseq()}{\ccode{esl\_ssi\_FindSubseq()}} & Look up a specific subsequence's start.\\
67 \hyperlink{func:esl_ssi_FileInfo()}{\ccode{esl\_ssi\_FileInfo()}} & Retrieve a file name and format code.\\
68 \hyperlink{func:esl_ssi_Close()}{\ccode{esl\_ssi\_Close()}} & Close an SSI index.\\
69 \apisubhead{Creating (writing) new SSI files.}\\
70 \hyperlink{func:esl_newssi_Open()}{\ccode{esl\_newssi\_Open()}} & Open a new \ccode{ESL\_NEWSSI}.\\
71 \hyperlink{func:esl_newssi_AddFile()}{\ccode{esl\_newssi\_AddFile()}} & Add a filename to a growing index.\\
72 \hyperlink{func:esl_newssi_SetSubseq()}{\ccode{esl\_newssi\_SetSubseq()}} & Declare that file is suitable for fast subseq lookup.\\
73 \hyperlink{func:esl_newssi_AddKey()}{\ccode{esl\_newssi\_AddKey()}} & Add a primary key to a growing index.\\
74 \hyperlink{func:esl_newssi_AddAlias()}{\ccode{esl\_newssi\_AddAlias()}} & Add a secondary key (alias) to a growing index.\\
75 \hyperlink{func:esl_newssi_Write()}{\ccode{esl\_newssi\_Write()}} & Save a new index to an SSI file.\\
76 \hyperlink{func:esl_newssi_Close()}{\ccode{esl\_newssi\_Destroy()}} & Close an \ccode{ESL\_NEWSSI}.\\
77 \apisubhead{Portable binary i/o}\\
78 \hyperlink{func:esl_byteswap()}{\ccode{esl\_byteswap()}} & Description.\\
79 \hyperlink{func:esl_ntoh16()}{\ccode{esl\_ntoh16()}} & Description.\\
80 \hyperlink{func:esl_hton16()}{\ccode{esl\_hton16()}} & Description.\\
81 %\hyperlink{func:esl_fread_i16()}{\ccode{esl\_fread\_i16()}} & Description.\\
82 %\hyperlink{func:esl_fwrite_i16()}{\ccode{esl\_fwrite\_i16()}} & Description.\\
83 \ccode{esl\_fread\_i16()} & Description.\\
84 \ccode{esl\_fwrite\_i16()} & Description.\\
85 \hyperlink{func:esl_fread_offset()}{\ccode{esl\_fread\_offset()}} & Description.\\
86 \hyperlink{func:esl_fwrite_offset()}{\ccode{esl\_fwrite\_offset()}} & Description.\\
87 \hline
88 \end{tabular}
89 }
90 \end{center}
91 \caption{The \eslmod{ssi} API.}
92 \label{tbl:ssi_api}
93 \end{table}
94
95 \subsection{Example: creating an SSI index}
96
97 Figure~\ref{fig:ssi_example} shows a program that creates an SSI index
98 for a FASTA sequence file, in which sequence records start with a line
99 like:
100 \begin{cchunk}
101 >SEQ_NAME Rest of the line is a free-text description.
102 \end{cchunk}
103
104 \begin{figure}
105 \input{cexcerpts/ssi_example}
106 \caption{An example of indexing the sequence records in a FASTA file.}
107 \label{fig:ssi_example}
108 \end{figure}
109
110 \begin{itemize}
111 \item A new index is created (\ccode{esl\_newssi\_Open()}). You can
112 name an SSI index anything you want, but for an index of a
113 single file, \Easel\ generally defaults to assuming an
114 \ccode{.ssi} suffix is appended to the filename. That's what
115 the example does here.
116
117 \item Each file to be indexed is added to the index by a call to
118 \ccode{esl\_newssi\_AddFile()}. This returns a \emph{file handle}
119 (\ccode{fh}) that you will need when you add primary keys. In
120 this example, there is only one file and only one file handle.
121
122 \item You need to determine the disk offset at the exact beginning of
123 each sequence record. You retrieve your current position in the
124 file using an \ccode{ftello()} call.
125
126 \item You add each primary key to the index with a
127 \ccode{esl\_newssi\_AddKey()} call. You provide the handle of the
128 file that key is in, and the offset to the start of this key's
129 sequence record.
130
131 \item The \ccode{esl\_fgets()} function (part of the \eslmod{easel}
132 foundation module) is a way of reading text files line by line,
133 no matter how long each line might be: \ccode{esl\_fgets()}
134 reallocates its buffer as needed.
135
136 \item \ccode{esl\_newssi\_Write()} saves the index to an open file.
137
138 \item Finally, the index structure is freed by
139 \ccode{esl\_newssi\_Close()}.
140 \end{itemize}
141
142 To compile and run the program, given a FASTA file \ccode{foo.fa} that
143 you provide:
144
145 \begin{cchunk}
146 % cc -o example -DeslSSI_EXAMPLE esl_ssi.c -leasel -lm
147 % ./example foo.fa
148 \end{cchunk}
149
150 This will create a new SSI file called \ccode{foo.fa.ssi}.
151
152 \subsection{An example of using an SSI index}
153
154 Figure~\ref{fig:ssi_example2} shows a program that retrieves a FASTA
155 sequence record by its name, using an SSI index.
156
157 \begin{figure}
158 \input{cexcerpts/ssi_example2}
159 \caption{Example of using an SSI index to retrieve a sequence by name from a FASTA
160 file.}
161 \label{fig:ssi_example2}
162 \end{figure}
163
164 \begin{itemize}
165 \item \ccode{esl\_ssi\_Open()} opens the SSI index file.
166
167 \item \ccode{esl\_ssi\_FindName()} looks up the record by its name.
168 Primary keys are checked first, then secondary keys. If it is
169 found, \ccode{fh} contains a file handle (what file it's in),
170 and \ccode{offset} contains the position of the desired record
171 in that file.
172
173 \item The file handle \ccode{fh} is looked up in the file index with
174 \ccode{esl\_ssi\_FileInfo()}, and the name of the file and a
175 format code are returned. The format code is useful if you need
176 to hand the filename off to different kinds of file parsers,
177 depending on what file type it is. (SSI can index files in
178 heterogenous formats.)
179
180 \item After that, you use the retrieved information however you need,
181 independent of the SSI index. The example emphasizes this, by
182 freeing the SSI index immediately with \ccode{esl\_ssi\_Destroy()}
183 after it knows \ccode{fafile} and \ccode{offset}. The example
184 opens the file, positions the disk with \ccode{fseeko()}, and
185 reads a sequence record out of it one line at a time, until it
186 reaches EOF or the start of the next sequence record.
187 \end{itemize}
188
189
190
191
192
193 \subsection{SSI file format}
194
195 There are four sections to the SSI file:
196 \begin{sreitems}{\textbf{Secondary keys}}
197 \item[\textbf{Header}]
198 Contains a magic number indicating SSI version number, followed by
199 information about the number and sizes of items in the index.
200
201 \item[\textbf{Files}]
202 Contains one or more \emph{file records}, one per sequence file that's
203 indexed. These contain information about the individual files.
204
205 \item[\textbf{Primary keys}]
206 Contains one or more \emph{primary key records}, one per primary key.
207
208 \item[\textbf{Secondary keys}]
209 Contains one or more \emph{secondary key records}, one per secondary key.
210 \end{sreitems}
211
212 All numeric quantities are stored as fixed-width unsigned integers in
213 network (bigendian) order, for crossplatform portability of the index
214 files, using types \ccode{uint16\_t}, \ccode{uint32\_t}, and
215 \ccode{uint64\_t}.\footnote{These types are available on C99-compliant
216 systems. On other systems, \Easel\ automatically defines appropriate
217 substitutes at configuration time.} Values may need to be cast to
218 signed quantities elsewhere in \Easel, so only half of their dynamic
219 range is valid (e.g. 0..32,767 for values of type \ccode{uint16\_t};
220 0..2,146,483,647 (2 billion) for \ccode{uint32\_t}; and 0..9.22e18 (9
221 million trillion) for \ccode{uint64\_t}).
222
223 File offsets (type \ccode{off\_t}) are assumed to be either 32-bit or
224 64-bit signed integers. Easel uses 64-bit offsets if at all possible
225 on your system. The size of \ccode{off\_t} for the system that created
226 the SSI file is stored in the SSI header, for portability to other
227 systems that try to read the SSI file.
228
229 \subsubsection{Header section}
230
231 The header section contains:
232
233 \vspace{1em}
234 \begin{tabular}{llrr}
235 Variable & Description & Bytes & Type \\\hline
236 \ccode{magic} & SSI version magic number. & 4 & \ccode{uint32\_t}\\
237 \ccode{flags} & Optional behavior flags (see below) & 4 & \ccode{uint32\_t}\\
238 \ccode{offsz} & \ccode{off\_t} size on system that made index & 4 & \ccode{uint32\_t}\\
239 \ccode{nfiles} & Number of files in file section. & 2 & \ccode{uint16\_t}\\
240 \ccode{nprimary} & Number of primary keys indexed. & 8 & \ccode{uint64\_t}\\
241 \ccode{nsecondary} & Number of secondary keys indexed. & 8 & \ccode{uint64\_t}\\
242 \ccode{flen} & Length of filenames (incl. '\verb+\0+') & 4 & \ccode{uint32\_t}\\
243 \ccode{plen} & Length of primary key names (incl. '\verb+\0+') & 4 & \ccode{uint32\_t}\\
244 \ccode{slen} & Length of sec. key names (incl. '\verb+\0+') & 4 & \ccode{uint32\_t}\\
245 \ccode{frecsize} & \# of bytes in a file record & 4 & \ccode{uint32\_t}\\
246 \ccode{precsize} & \# of bytes in a primary key record & 4 & \ccode{uint32\_t}\\
247 \ccode{srecsize} & \# of bytes in a sec. key record & 4 & \ccode{uint32\_t}\\
248 \ccode{foffset} & disk offset, start of file records & \dag & \ccode{off\_t}\\
249 \ccode{poffset} & disk offset, start of primary key recs & \dag & \ccode{off\_t}\\
250 \ccode{soffset} & disk offset, start of sec. key records & \dag & \ccode{off\_t}\\
251 \end{tabular}
252 \vspace{1em}
253
254 The \ccode{flags} field is currently unused. It is stored for possible
255 future use, for any optional behaviors that may need to be
256 implemented.
257
258 The reason to explicitly record various record sizes
259 (\ccode{frecsize}, \ccode{precsize}, \ccode{srecsize}) and index file
260 positions (\ccode{foffset}, \ccode{poffset}, \ccode{soffset}) is to
261 allow for future extensions. Using explicit offsets means we can add
262 more fields in future versions of SSI without breaking older SSI
263 parsers. The format is meant to be both forwards- and
264 backwards-compatible.
265
266 \subsubsection{File section}
267
268 The file section consists of \ccode{nfiles} file records. Each record
269 is \ccode{frecsize} bytes long, and contains:
270
271 \vspace{1em}
272 \begin{tabular}{llrr}
273 Variable & Description & Bytes & Type \\\hline
274 \ccode{filename} & Name of file (possibly including full path) & \ccode{flen} & \ccode{char *}\\
275 \ccode{format} & Format code for file & 4 & \ccode{uint32\_t} \\
276 \ccode{flags} & Optional behavior flags & 4 & \ccode{uint32\_t} \\
277 \ccode{bpl} & Bytes per sequence data line & 4 & \ccode{uint32\_t} \\
278 \ccode{rpl} & Residues per sequence data line & 4 & \ccode{uint32\_t} \\\hline
279 \end{tabular}
280 \vspace{1em}
281
282 When a SSI file is written, \ccode{frecsize} is equal to the sum of
283 the sizes above. When a SSI file is read by a parser, it is possible
284 that \ccode{frecsize} is larger than the parser expects, if the parser
285 is expecting an older version of the SSI format, because additional
286 fields might be present. The parser will only try to parse data up to
287 the \ccode{frecsize} it expected to see, but still knows the (possibly
288 larger) \ccode{frecsize} that is operative in this SSI file, for
289 purposes of skipping around in the index file.
290
291 An SSI index might reside in the same directory as the data file(s) it
292 indexes, so \ccode{filename} might be relative to the location of the
293 SSI index. Alternatively, \ccode{filename} might be a full path. These
294 semantics are not enforced by the \eslmod{ssi} module. Rather, this is
295 an issue for an SSI-enabled application to define for
296 itself. SSI-enabled applications would typically include program(s)
297 for creating indices and program(s) for using them. Different
298 applications might employ different conventions for where the indices
299 are expected to be, relative to the sequence files, so long as that
300 convention is consistently applied by both index creator and index
301 user.
302
303 Similarly, the \eslmod{ssi} module does not specify the meaning of the
304 \ccode{format} code. An SSI-enabled application may use this field to
305 associate any useful format code (or indeed, any other number) with
306 each indexed file. A typical use, though, would be sequence file
307 format codes like \ccode{eslSQFILE\_FASTA} or
308 \ccode{eslMSAFILE\_STOCKHOLM} from the \eslmod{sqio} or \eslmod{msa}
309 modules.
310
311 Only one possible optional behavior flag is currently defined:
312
313 \vspace{1em}
314 \begin{tabular}{lll}
315 Flag & Value& Note\\ \hline
316 \ccode{eslSSI\_FASTSUBSEQ} & $1 \ll 0$ & Fast subseq retrieval is possible for this file.\\\hline
317 \end{tabular}
318 \vspace{1em}
319
320 When \ccode{eslSSI\_FASTSUBSEQ} is set, \ccode{bpl} and \ccode{rpl}
321 are nonzero. These can then be used to calculate the offset of
322 subsequence positions in the data file. This optional behavior is
323 described in detail a bit later.
324
325 \subsubsection{Primary key section}
326
327 The primary key section consists of \ccode{nprimary} records. Each
328 record is \ccode{precsize} bytes long, and contains:
329
330 \vspace{1em}
331 \begin{tabular}{llrr}
332 Variable & Description & Bytes & Type \\\hline
333 \ccode{key} & Key name (seq name, identifier, accession) & \ccode{plen}& \ccode{char *}\\
334 \ccode{fnum} & File number (0..nfiles-1) & 2 & \ccode{uint16\_t}\\
335 \ccode{r\_off} & Offset to start of record & \ddag & \ccode{off\_t}\\
336 \ccode{d\_off} & Offset to start of sequence data & \ddag & \ccode{off\_t}\\
337 \ccode{len} & Length of data (e.g. seq length, residues) & 8 & \ccode{uint64\_t} \\\hline
338 \end{tabular}
339 \vspace{1em}
340
341 The two offsets are sequence file offsets that may be either 8 or 4
342 bytes (indicated by \ddag above). They are usually 64-bit (8 byte)
343 signed integers. If an SSI index is created on an older system that
344 only allows 32-bit offsets (and hence cannot have files $>$2 GB), they
345 are 32-bit (4-byte) signed integers.
346
347 \ccode{r\_off} (the \emph{record offset}) indicates the position of
348 the first byte of the record.
349
350 \ccode{d\_off} (the \emph{data offset}) is optional. It indicated the
351 position of the first byte of the data in the record (the sequence
352 itself, for example), after any header information. If
353 \ccode{eslSSI\_FASTSUBSEQ} is set on this key's file, \ccode{d\_off}
354 can be used to calculate a disk position close to (and possibly
355 exactly at) the start of any subsequence.
356
357 \ccode{len} is the length of the data record. It is optional, because
358 some kinds of records that SSI might be used to index may not have a
359 meaningful length. The units of length are application-defined (i.e.\
360 defined by whatever creates the SSI index for a particular file); but
361 for sequences, \ccode{len} is almost certainly in residues. In
362 subsequence retrieval, a \ccode{len} in residues is necessary for
363 bounds checking.
364
365 \subsubsection{Secondary key section}
366
367 The secondary key section consists of \ccode{nsecondary} records. Each
368 record is \ccode{srecsize} bytes long, and contains:
369
370 \vspace{1em}
371 \begin{tabular}{llrr}
372 Variable & Description & Bytes & Type \\\hline
373 \ccode{key} & Key name (seq name, identifier, accession) & \ccode{slen}& \ccode{char *}\\
374 \ccode{pkey} & Primary key &
375 \ccode{plen}& \ccode{char *}\\\hline
376 \end{tabular}
377 \vspace{1em}
378
379 That is, secondary keys are simply associated with primary keys as
380 \emph{aliases}. There can be many secondary keys for a given record.
381 However, all keys (primary and secondary) must be unique: no key can
382 occur more than once in the index.
383
384 \subsection{Fast subsequence retrieval}
385
386 In some files (notably whole chromosomal DNA sequences) the size of
387 each sequence is large. It may be slow (even prohibitively slow) to
388 extract a desired subsequence, even if an SSI index says how to find
389 the sequence record quickly, if you had to read the entire sequence
390 into memory just to extract the right part of it.
391
392 SSI uses a simple but effective technique to find subsequences.
393 Provided that he sequence data file is consistently formatted so that
394 each line in each record (except the last one) is of the same length,
395 in both bytes and residues, we can determine a disk offset of the
396 start of any subsequence by arithmetic. \Easel\ refers to such a file
397 as ``well-formatted''. For example, a simple well-formatted FASTA file
398 with 50 residues per line might have 51 bytes on every sequence line
399 (counting the '\verb+\0+') but for the last line in each record
400 (\ccode{bpl}=51, \ccode{rpl}=50). Position $i$ in a sequence $1..L$
401 will be on line $l = (i-1)/\mbox{\ccode{rpl}}$, and line $l$ starts at
402 disk offset $l * \mbox{\ccode{bpl}}$ relative to the start of the
403 sequence data.
404
405 If there are no nonsequence characters in the data line except the
406 terminal '\verb+\0+' (which is true iff \ccode{bpl} = \ccode{rpl}+1
407 and 1 residue = 1 byte), we can precisely identify the disk position
408 of any residue $i$ (\emph{single residue resolution}):
409
410 \[
411 \mbox{relative offset of residue $i$} =
412 \left((i-1)/\mbox{\ccode{rpl}}\right)*\mbox{\ccode{bpl}} + (i-1) \% \mbox{ \ccode{rpl}}
413 \]
414
415 Even for sequence data lines with extra characters (e.g. spaces,
416 coordinates, whatever), we can still identify the start of the text
417 line that residue $i$ is on (\emph{line resolution}). A parser can be
418 positioned at the beginning of the appropriate line $l$, which starts
419 at residue $(l*\mbox{\ccode{rpl}}) + 1$, and it can start reading from
420 there (e.g. the line that $i$ is on) rather than the beginning of the
421 whole sequence record.
422
423 When creating an index, your application is responsible for
424 determining if \ccode{bpl} and \ccode{rpl} are consistent throughout a
425 file. If so, you may call \ccode{esl\_newssi\_SetSubseq()} on that
426 file's handle to set \ccode{bpl}, \ccode{rpl}, and the
427 \ccode{eslSSI\_FASTSUBSEQ} flag. Then, when using that index, you can
428 use the \ccode{esl\_ssi\_FindSubseq()} call to retrieve not only the
429 filehandle \ccode{fh} and record offset \ccode{r\_off} for a key; you
430 also provide a desired start position \ccode{requested\_start} for the
431 subsequence you want to retrieve, and the routine gives you back a
432 data offset \ccode{d\_off}, which corresponds to a actual starting
433 position \ccode{actual\_start} that is also returned. For single
434 residue resolution, \ccode{actual\_start} is \ccode{requested\_start},
435 and the data offset \ccode{d\_off} will position you right at the
436 residue you want; you position the file with \ccode{fseeko()} and
437 start reading your subsequence immediately. When we can only achieve
438 line resolution, \ccode{actual\_start} is $\leq$
439 \ccode{requested\_start}; you position the disk to the start of the
440 appropriate line with \ccode{fseeko()}, start reading, and skip zero
441 or more residues to reach your \ccode{requested\_start}. Your
442 application should be prepared to deal with line resolution; it should
443 not assume that \ccode{requested\_start} and \ccode{actual\_start} are
444 identical.
445
446 Data is always read ``left to right''. To read a reverse complemented
447 strand in DNA files, you must read your subsequence in forward
448 orientation first, and reverse complement it later.
449
450
451