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