| 1 |
CDB (Constant DataBase) indexing and retrieval tools for FASTA files |
| 2 |
|
| 3 |
This is a brief introduction to a couple of platform independent file-based |
| 4 |
hashing tools (cdbfasta and cdbyank) that can be used for creating indices for |
| 5 |
quick retrieval of any particular sequences from large multi-FASTA files. The |
| 6 |
last version has the option to compress data records in order to save space. |
| 7 |
The index files are now architecture independent, the same index file can be |
| 8 |
created and used on many different Unix platform (be it 32bit/64bit, |
| 9 |
big-endian or little-endian architectures) and even Windows. |
| 10 |
|
| 11 |
1.Install instructions |
| 12 |
2.Typical usage |
| 13 |
3.Retrieving sequence ranges or only the defline |
| 14 |
4.Data compression option |
| 15 |
5.Development notes |
| 16 |
|
| 17 |
|
| 18 |
1.Install instructions |
| 19 |
=============================== |
| 20 |
Clone this repository and run 'make' in the cdbfasta directory. |
| 21 |
Make sure you have the zlib library and development files installed on your linux system. |
| 22 |
|
| 23 |
Note: if no compression support is needed, change the BASEFLAGS variable in the Makefile |
| 24 |
to have -DENABLE_COMPRESSION=0 instead of -DENABLE_COMPRESSION=0 |
| 25 |
|
| 26 |
Running 'make' should produce the binaries 'cdbfasta' (the indexer program) |
| 27 |
and 'cdbyank' (the query program) in the current directory. |
| 28 |
|
| 29 |
2.Typical usage |
| 30 |
=============== |
| 31 |
|
| 32 |
Use cdbfasta to create the index file for a multi-FASTA file and cdbyank to |
| 33 |
pull records based on that index file. An usage message is displayed if the |
| 34 |
commands cdbyank or cdbyank are run without any parameters. |
| 35 |
|
| 36 |
In order to create an index file, only the name of the fasta file must be |
| 37 |
provided: |
| 38 |
|
| 39 |
cdbfasta <fasta_file> |
| 40 |
|
| 41 |
The fasta file can be specified with the whole path (if it's not in the current |
| 42 |
directory), e.g. |
| 43 |
|
| 44 |
cdbfasta /usr/local/db/GUDB.human |
| 45 |
|
| 46 |
By default cdbfasta creates an index file with the same name as the database |
| 47 |
file but with the .cidx suffix added to the original name. So in the example |
| 48 |
above, a file GUDB.human.cidx will be created in /usr/local/db/. The default |
| 49 |
usage considers the key for a FASTA record to be the first space-delimited |
| 50 |
token following the ">" starting character from the definition line. For |
| 51 |
example, if a FASTA record had a defline like this: |
| 52 |
|
| 53 |
>AA141526 |
| 54 |
|
| 55 |
Then we can use the string 'AA141526' with cdbyank to retrieve the full FASTA |
| 56 |
record associated to that sequence name: |
| 57 |
|
| 58 |
cdbyank -a 'AA141526' /usr/local/db/GUDB.human.cidx |
| 59 |
|
| 60 |
Sometimes all the space delimited tokens in the defline need to be declared as |
| 61 |
keys in the index file, pointing to the same fasta record. This can be |
| 62 |
accomplished by cdbfasta by using the "-m" switch. |
| 63 |
|
| 64 |
For long and complex fastA file accessions like this: |
| 65 |
EGAD|61|GP|186739|gb|AAA63210.1||M60828 there is an option to create the |
| 66 |
index file in such a way that there is no need to provide the full string to |
| 67 |
cdbyank in order to retrieve such a sequence, but only the first |
| 68 |
"<db>|<accession>" pair (i.e. a substring ending at the second '|' character) |
| 69 |
should be enough. (EGAD|61 in the example above). In order to enable this |
| 70 |
feature, there are two alternative options for cdbfasta: |
| 71 |
|
| 72 |
|
| 73 |
-c : the index file is built only by storing the "shortcut key" (the first |
| 74 |
"db|accession" pair found in the defline of each fasta record). In this |
| 75 |
case, cdbyank will only be able to accept these "shortcut" accessions for |
| 76 |
record retrieval. |
| 77 |
|
| 78 |
-C : the index file is built by storing both the "shortcut key" and the full |
| 79 |
keys (which are considered to end at the first space character in the |
| 80 |
defline). In this case, two strings are stored as keys for each fastA |
| 81 |
record so any of them can be used as an accession for retrieval of the |
| 82 |
same record with cdbyank. |
| 83 |
|
| 84 |
In order to retrieve records from the database file, cdbyank should be provided |
| 85 |
with the name of the index file created previously with cdbfasta, e.g.: |
| 86 |
|
| 87 |
cdbyank -a 'human|Z98492' /usr/local/db/GUDB.human.cidx |
| 88 |
|
| 89 |
A list of accessions is expected at stdin if -a option is not provided, e.g.: |
| 90 |
|
| 91 |
cat seq_list | cdbyank /usr/local/db/GUDB.human.cidx |
| 92 |
|
| 93 |
This way the output will be a series a fasta records at stdout. By redirecting |
| 94 |
this output to a file a multifasta file is obtained. cdbyank locates the |
| 95 |
database file by stripping the '.cidx' suffix off the index filename. But this |
| 96 |
is not enforced, because by using the -d option, cdbyank can make use of a |
| 97 |
user-provided database to be used by the given index file. In the example |
| 98 |
above, if the index file "GUDB.human.cidx" is moved into another directory, a |
| 99 |
cdbyank command (in that other directory) can be issued like that: |
| 100 |
|
| 101 |
cdbyank -a 'human|Z98492' -d /usr/local/db/GUDB.human GUDB.human.cidx |
| 102 |
|
| 103 |
The position of the index file in the list of arguments of cdbyank is not |
| 104 |
enforced. For the -a usage, the error status returned by cdbyank to the shell |
| 105 |
will be 1 if the given key was not found and 0 for success. |
| 106 |
|
| 107 |
The total number of fasta records indexed and the list of the keys stored in a |
| 108 |
specific cdb index file can be retrieved with cdbyank's -n and -l switches, |
| 109 |
respectively. This information is obtained from the index file directly (the |
| 110 |
database file is not needed for that). There is also a -s option that displays |
| 111 |
a summary of the indexing information stored in the index at index time. These |
| 112 |
are the initial name of the fastA file, its size, how the index was created |
| 113 |
(e.g. was -m (multiple keys) option given ? was -c or -C (shortcut keys) option |
| 114 |
given?), the number of keys stored in the file as well as the number of fasta |
| 115 |
records indexed - the latter being the same with what -n option returns. |
| 116 |
|
| 117 |
As an extra feature, cdbfasta and cdbyank can also be used for some special |
| 118 |
cases where databases may have different records but with the same key |
| 119 |
(non-unique keys). Although the performance will degrade a little, cdbfasta is |
| 120 |
able to index this kind of files, but by default cdbyank only outputs the first |
| 121 |
record found. If you want all the possible records sharing the same key |
| 122 |
(accession) to be retrieved and displayed, the -x option should be given to |
| 123 |
cdbyank. |
| 124 |
|
| 125 |
3.Retrieving sequence ranges or only the defline |
| 126 |
================================================ |
| 127 |
|
| 128 |
There are two cdbyank options added for convenience: -F option returns the |
| 129 |
definition line of each requested FASTA record (the first line for each |
| 130 |
record). The -R option of cdbyank is intended for FASTA files containing |
| 131 |
actual genetic sequences (nucleotide or protein) and expects each of the |
| 132 |
retrieval commands to have the following format (space delimited) |
| 133 |
|
| 134 |
<key> <right_coordinate> <left_coordinate> |
| 135 |
|
| 136 |
For example if we only want to retrieve the sequence range 24...178 (letter |
| 137 |
numbering starts at 1) from sequence with the name 'human|Z98492', then the |
| 138 |
cdbyank command would look like this: |
| 139 |
|
| 140 |
cdbyank -a 'human|Z98492 24 178' -R GUDB.human.cidx |
| 141 |
|
| 142 |
Multiple sequence ranges can be extracted this way by providing a file having |
| 143 |
each line following the format above (key followed by the two coordinates). |
| 144 |
Then, as before, such file can be piped into cdbyank with -R option to pull |
| 145 |
specific sequence ranges for each of the sequences specified in the input file. |
| 146 |
|
| 147 |
|
| 148 |
cat seqlistranges | cdbyank -R GUDB.human.cidx |
| 149 |
|
| 150 |
Note that this range option works by actually parsing and looping through the |
| 151 |
retrieved record characters internally - so the performance is poor when some |
| 152 |
terminal range is pulled from a very large record. |
| 153 |
|
| 154 |
4.Data compression option |
| 155 |
========================= |
| 156 |
(This only applies if the programs were built with compression support enabled) |
| 157 |
|
| 158 |
The indexing program cdbfasta has the -z <compressed_db> option which creates |
| 159 |
a compressed file from the input file and at the same time creates an index |
| 160 |
file for this compressed file. The original input file can then be discarded |
| 161 |
(if it is only needed for random access through cdbyank). The entire input file |
| 162 |
can be recovered from the resulting <compressed_db> by using the -z option of |
| 163 |
cdbyank. Because each record is compressed separately, compression is poor if |
| 164 |
the records are small. Compression is only advised when: |
| 165 |
|
| 166 |
* data records are large enough for the compression algorithm to adapt (at |
| 167 |
least 1KB, the more the better) |
| 168 |
|
| 169 |
* only random access is needed to the data records (so the original file can |
| 170 |
be discarded) |
| 171 |
|
| 172 |
The compression can be quite slow for large files and there is also some |
| 173 |
performance penalty for cdbyank as it has to decompress the retrieved records |
| 174 |
on the fly. |
| 175 |
|
| 176 |
The input data for cdbfasta compression can be collected from stdin if '-' is |
| 177 |
used instead of a file name: |
| 178 |
|
| 179 |
cat my_data_files* | cdbfasta - -z mydata.cdbz |
| 180 |
|
| 181 |
This option is useful especially when the total size of input data files is |
| 182 |
extremely large (over the file-system limits or over the 4GB internal limit of |
| 183 |
cdbfasta) while the compressed output can be small enough to fall under such |
| 184 |
limits. |
| 185 |
|
| 186 |
With compressed databases cdbyank can be used normally without extra options as |
| 187 |
it will auto-detect the compression (from the index file info) and activate |
| 188 |
on-the-fly decompression of the retrieved records. |
| 189 |
|
| 190 |
The -F and -R options are not yet accepted when working with compressed |
| 191 |
records. |
| 192 |
|
| 193 |
5.Development notes |
| 194 |
=================== |
| 195 |
|
| 196 |
These tools were developed in C++, based on the publicly available cdb |
| 197 |
("constant database") code written by D.J. Bernstein |
| 198 |
(http://cr.yp.to/djb.html). "Constant databases" are those that we don't need |
| 199 |
to add to or remove records from. The original C source was (rather crudely) |
| 200 |
wrapped into C++ classes and adjusted to automatically index fasta records and |
| 201 |
to create an external index instead of storing the original data records like |
| 202 |
the original cdb library code does. Also the "endianness" is now checked at |
| 203 |
runtime and the bytes are swapped accordingly such that the file offsets and |
| 204 |
record sizes are always read/written in the same way in the index file. |
| 205 |
|
| 206 |
The compression option uses zlib's "deflate" method. The program uses deflate() |
| 207 |
with Z_FULL_FLUSH after each record, such that random record decompression is |
| 208 |
possible after the first dummy record is decompressed. |
| 209 |
|
| 210 |
The index file contains an info chunk (actually stored at the end of the file) |
| 211 |
which maintains a summary data and flags about the indexing process (the -s |
| 212 |
option of cdbyank retrieves this information). Since the compression option |
| 213 |
was added, cdbyank is always trying to read this information first (before |
| 214 |
opening the data file) in order to determine if the data records are compressed |
| 215 |
or not. |
| 216 |
|
| 217 |
-- |
| 218 |
Geo Pertea |
| 219 |
[email protected] |
| 220 |
06/09/2003 |
| 221 |
|
| 222 |
Current author contact: |
| 223 |
[email protected] |
| 224 |
|
| 225 |
This software is Open Source Software, distributed under an Artistic License: |
| 226 |
https://opensource.org/licenses/artistic-license-2.0 |