| 1 |
The \eslmod{sqio} module handles input from unaligned sequence data |
| 2 |
files, such as FASTA files. |
| 3 |
|
| 4 |
\eslmod{sqio} can automatically recognize and parse several different |
| 5 |
file formats, including FASTA, UniProt, Genbank, DDBJ, and EMBL. |
| 6 |
Additionally, it can read individual unaligned sequences from multiple |
| 7 |
alignment files in several different formats, including Stockholm, |
| 8 |
Clustal, aligned FASTA, PSI-BLAST, and Phylip. |
| 9 |
|
| 10 |
Sequences can be read from normal files, directly from the |
| 11 |
\ccode{stdin} pipe, or from \ccode{gzip}-compressed files. |
| 12 |
|
| 13 |
Sequence files can be automatically looked for in a list of one or |
| 14 |
more database directories, specified by an environment variable (such |
| 15 |
as \ccode{HMMERDB}). |
| 16 |
|
| 17 |
Table~\ref{tbl:sqio_api} lists the functions in the \eslmod{sqio} API. |
| 18 |
The module uses an \ccode{ESL\_SQFILE} object which works much like an |
| 19 |
ANSI C \ccode{FILE}, maintaining information for an open sequence file |
| 20 |
while it's being read. |
| 21 |
|
| 22 |
% API table is auto generated by the Makefile, |
| 23 |
% using autodoc -t esl_sqio.c |
| 24 |
% |
| 25 |
\input{apitables/esl_sqio_api} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
\subsection{Example: reading sequences from a file} |
| 30 |
|
| 31 |
Figure~\ref{fig:sqio_example_text} shows a program that opens a file, reads |
| 32 |
sequences from it one at a time, then closes the file. |
| 33 |
|
| 34 |
\begin{figure} |
| 35 |
\input{cexcerpts/sqio_example_text} |
| 36 |
\caption{Example of reading sequences from a file.} |
| 37 |
\label{fig:sqio_example_text} |
| 38 |
\end{figure} |
| 39 |
|
| 40 |
A FASTA file named \ccode{seqfile} is opened for reading by calling |
| 41 |
\ccode{esl\_sqfile\_Open(filename, format, env, \&sqfp)}, which |
| 42 |
creates a new \ccode{ESL\_SQFILE} and returns it through the |
| 43 |
\ccode{sqfp} pointer. If the \ccode{format} is passed as |
| 44 |
\ccode{eslSQFILE\_UNKNOWN}, then the format of the file is |
| 45 |
autodetected. Here, we bypass autodetection by asserting that the file |
| 46 |
is in FASTA format by passing a \ccode{eslSQFILE\_FASTA} code. (See |
| 47 |
below for a list of valid codes and formats.) The optional \ccode{env} |
| 48 |
argument is described below too; here, we're passing \ccode{NULL} and |
| 49 |
not using it. |
| 50 |
|
| 51 |
Several things can go wrong in trying to open a sequence file that are |
| 52 |
beyond the control of Easel or your application, so it's important |
| 53 |
that you check the return code. \ccode{esl\_sqfile\_Open()} returns |
| 54 |
\ccode{eslENOTFOUND} if the file can't be opened, and |
| 55 |
\ccode{eslEFORMAT} if the file is empty, or if autodetection can't |
| 56 |
determine its format.\footnote{Additionally, internal errors might be |
| 57 |
thrown, which you should check for if you installed a nonfatal error |
| 58 |
handler.} |
| 59 |
|
| 60 |
The file is then read one sequence at a time by calling |
| 61 |
\ccode{esl\_sq\_Read(sqfp, sq)}. This function returns \ccode{eslOK} |
| 62 |
if it read a new sequence, and leaves that sequence in the \ccode{sq} |
| 63 |
object that the caller provided. When there is no more data in the |
| 64 |
file, \ccode{esl\_sq\_Read()} returns \ccode{eslEOF}. |
| 65 |
|
| 66 |
If at any point the file does not appear to be in the proper format, |
| 67 |
\ccode{esl\_sq\_Read()} returns \ccode{eslEFORMAT}. The application |
| 68 |
must check for this. The API can provide a little information about |
| 69 |
what went wrong and where. \ccode{sqfp->filename} is the name of the |
| 70 |
file that we were parsing (not necessarily the same as |
| 71 |
\ccode{seqfile}; \ccode{sqfp->filename} can be a full pathname if we |
| 72 |
used an \ccode{env} argument to look for \ccode{seqfile} in installed |
| 73 |
database directories). The function \ccode{esl\_sqfile\_GetErrorBuf()} |
| 74 |
should be called to get a pointer to the generated error message. The |
| 75 |
buffer is a brief explanatory message that gets filled in when a |
| 76 |
\ccode{eslEFORMAT} error occurs. |
| 77 |
\footnote{Unlike in the MSA module, you don't get access to the |
| 78 |
current line text; some of sqio's parsers use fast block-based |
| 79 |
(\ccode{fread()}) input instead of line-based input.} |
| 80 |
|
| 81 |
We can reuse the same \ccode{ESL\_SQ} object for subsequent sequences |
| 82 |
by calling \ccode{esl\_sq\_Reuse()} on it when we're done with the |
| 83 |
previous sequence. If we wanted to load a set of sequences, we'd |
| 84 |
\ccode{\_Create()} an array of \ccode{ESL\_SQ} objects. |
| 85 |
|
| 86 |
Finally, to clean up properly, a \ccode{ESL\_SQ} that was created is |
| 87 |
destroyed with \ccode{esl\_sq\_Destroy(sq)}, and a \ccode{ESL\_SQFILE} |
| 88 |
is closed with \ccode{esl\_sqfile\_Close()}. |
| 89 |
|
| 90 |
|
| 91 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 92 |
\subsection{Digital sequence input mode} |
| 93 |
|
| 94 |
Most Easel-based programs manipulate sequences in Easel's digital |
| 95 |
sequence format, using \eslmod{alphabet}, as opposed to manipulating |
| 96 |
them as plaintext. The \eslmod{sqio} reader can be used either in text |
| 97 |
mode or digital mode. In text mode, you get the \ccode{sq->seq} field |
| 98 |
of the \ccode{ESL\_SQ}; in digital mode, you get \ccode{sq->dsq}. |
| 99 |
|
| 100 |
To use digital mode, both the \ccode{ESL\_SQFILE} reader and the |
| 101 |
\ccode{ESL\_SQ} sequence object must be set to digital mode. The |
| 102 |
reader, because it has an input map that maps plaintext input |
| 103 |
characters to internal residue codes (plaintext or digital), or |
| 104 |
errors. The sequence object, because it needs to have either its |
| 105 |
\ccode{seq} or \ccode{dsq} field allocated. Both also carry a copy of |
| 106 |
the pointer to the alphabet. |
| 107 |
|
| 108 |
Figure~\ref{fig:sqio_example_digital} shows an example of the standard |
| 109 |
idiom for opening files in digital mode, autoguessing their format and |
| 110 |
alphabet by default, and allowing format and alphabet to be specified |
| 111 |
on the command line. |
| 112 |
|
| 113 |
\begin{figure} |
| 114 |
\input{cexcerpts/sqio_example_digital} |
| 115 |
\caption{Standard idiom for reading sequences in digital mode.} |
| 116 |
\label{fig:sqio_example_digital} |
| 117 |
\end{figure} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 123 |
\subsection{Accepted formats} |
| 124 |
|
| 125 |
Accepted unaligned sequence file formats (and their Easel format |
| 126 |
codes) include: |
| 127 |
|
| 128 |
\begin{tabular}{lll} |
| 129 |
\textbf{name} & \textbf{code} & \textbf{description} \\ |
| 130 |
fasta & \ccode{eslSQFILE\_FASTA} & FASTA format \\ |
| 131 |
embl & \ccode{eslSQFILE\_EMBL} & EMBL DNA database format \\ |
| 132 |
genbank & \ccode{eslSQFILE\_GENBANK} & GenBank DNA database format \\ |
| 133 |
ddbj & \ccode{eslSQFILE\_DDBJ} & DDBJ DNA database format \\ |
| 134 |
uniprot & \ccode{eslSQFILE\_UNIPROT} & UniProt protein database format \\ |
| 135 |
\end{tabular} |
| 136 |
|
| 137 |
The above names, case-insensitive, are what a user uses to specify a |
| 138 |
format on a command line: i.e.\ \ccode{--informat fasta} or |
| 139 |
\ccode{--informat FASTA}. |
| 140 |
|
| 141 |
The codes are what you use as a developer to specify a format to an |
| 142 |
Easel function call. |
| 143 |
|
| 144 |
Additionally, there is a code \ccode{eslSQFILE\_UNKNOWN}. It |
| 145 |
tells \ccode{esl\_sqfile\_Open()} to perform format |
| 146 |
autodetection.\footnote{There are some other formats as well, which we |
| 147 |
don't advertise because they're less well supported.} |
| 148 |
|
| 149 |
|
| 150 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 151 |
\subsection{Reading from stdin and compressed files} |
| 152 |
|
| 153 |
There are two special cases for input files. |
| 154 |
|
| 155 |
The module can read sequence input from a stdin pipe. If the |
| 156 |
\ccode{seqfile} argument is ``-'', \ccode{esl\_sqfile\_Open()} ``opens'' |
| 157 |
standard input (really, it just associates \ccode{stdin}, which is |
| 158 |
always open, with the \ccode{ESL\_SQFILE}). |
| 159 |
|
| 160 |
The module can read compressed sequence files. If the \ccode{seqfile} |
| 161 |
argument to \ccode{esl\_sqfile\_Open()} ends in \ccode{.gz}, the file |
| 162 |
is assumed to be compressed with \ccode{gzip}; instead of opening it |
| 163 |
normally, \ccode{esl\_sqfile\_Open()} opens it as a pipe from |
| 164 |
\ccode{gzip -dc}. Your system must support pipes to use |
| 165 |
this.\footnote{Specifically, it must support the \ccode{popen()} |
| 166 |
system call (POSIX.2 compliant operating systems do). The |
| 167 |
\ccode{configure} script automatically checks this at compile-time |
| 168 |
and defines \ccode{HAVE\_POPEN} appropriately.} Obviously, the user |
| 169 |
must also have \ccode{gzip} installed and in his PATH. |
| 170 |
|
| 171 |
For both special cases, the catch is that you can't use format |
| 172 |
autodetection; you must provide a valid known format code when you |
| 173 |
read from stdin or from a compressed file. Pipes are not rewindable, |
| 174 |
and format autodetection currently relies on a two-pass algorithm: it |
| 175 |
reads partway into the file to determine the format, then rewinds to |
| 176 |
start parsing for real.\footnote{The \eslmod{msafile} module is more |
| 177 |
advanced. Its parsers are based on the newer \eslmod{buffer} module |
| 178 |
which provides rewindable input buffers even for stdin and pipes.} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 183 |
%% SRE: commented NCBI section out. |
| 184 |
%% I don't believe that it works, or that it's up to date. |
| 185 |
%% Needs testing. |
| 186 |
|
| 187 |
%% \subsection{NCBI BLAST database support} |
| 188 |
|
| 189 |
%% If sqio is augmented with the ncbi module, then the sqio API gains the |
| 190 |
%% ability to read NCBI BLAST database formats in addition to ASCII file |
| 191 |
%% formats. The sqio API remains exactly the same (the caller doesn't |
| 192 |
%% have to use any msa module functions). |
| 193 |
|
| 194 |
%% To open a BLAST database, the format must be supplied. If a format |
| 195 |
%% of \ccode{eslSQFILE\_UNKNOWN} is specified, only ASCII based format, |
| 196 |
%% i.e. FASTA, GENBANK, etc. will be opened. |
| 197 |
|
| 198 |
%% When opening a BLAST database, the file name should not include any |
| 199 |
%% extensions. If the \ccode{ESL\_ALPHABET} is not specified, Easel |
| 200 |
%% will first try to open a protein database, followed by a DNA database |
| 201 |
%% and finally a multi-volume database. The handling of a multi-volume |
| 202 |
%% database is done through the alias file. The only directive in |
| 203 |
%% the alias file is the DBLIST line, listing all the database volumes. |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 208 |
\subsection{Adding a sequence parser} |
| 209 |
|
| 210 |
New parsers for new formats can be plugged into \eslmod{sqio} without |
| 211 |
any API changes. Existing Easel-based programs don't need code changes |
| 212 |
to use the new parser. |
| 213 |
|
| 214 |
A new parser will need a format type, a structure for parser specific |
| 215 |
data, API functions and a hook into the \ccode{sqfile\_open} function. |
| 216 |
|
| 217 |
The list of formats are defined in \ccode{esl\_sqio.h}. A new |
| 218 |
\ccode{\#define} will be added to the existing formats: |
| 219 |
|
| 220 |
\input{cexcerpts/sq_sqio_format} |
| 221 |
|
| 222 |
A data structure for parser specific data will need to be added |
| 223 |
to \ccode{ESL\_SQDATA}. This structure is a union of the |
| 224 |
different parser specific data structures. |
| 225 |
|
| 226 |
\input{cexcerpts/sq_sqio_data} |
| 227 |
|
| 228 |
Finally, a set of parser specific function pointers need to be |
| 229 |
defined. The functions in \ccode{esl\_sqio.c} in turn call these |
| 230 |
function pointers. The \ccode{esl\_sqfile\_Open} function initializes |
| 231 |
the function pointers to NULL, so if they are not set, an exception |
| 232 |
will occur when the function is called. At a minimum, the function |
| 233 |
should be defined to return an \ccode{eslEUNIMPLEMENTED}. Below is a |
| 234 |
map the the function pointers to their respective function. |
| 235 |
|
| 236 |
\begin{tabular}{ll} \\ |
| 237 |
Function pointer & \eslmod{sqio} function \\ \hline |
| 238 |
position & esl\_sqfile\_Position \\ |
| 239 |
close & esl\_sqfile\_Close \\ |
| 240 |
set\_digital & esl\_sqfile\_SetDigital \\ |
| 241 |
guess\_alphabet & esl\_sqfile\_GuessAlphabet \\ |
| 242 |
read & esl\_sqio\_Read \\ |
| 243 |
read\_info & esl\_sqio\_ReadInfo \\ |
| 244 |
read\_seq & esl\_sqio\_ReadSequence \\ |
| 245 |
read\_window & esl\_sqio\_ReadWindow \\ |
| 246 |
echo & esl\_sqio\_Echo \\ |
| 247 |
read\_block & esl\_sqio\_ReadBlock \\ |
| 248 |
open\_ssi & esl\_sqfile\_OpenSSI \\ |
| 249 |
pos\_by\_key & esl\_sqfile\_PositionByKey \\ |
| 250 |
pos\_by\_number & esl\_sqfile\_PositionByNumber \\ |
| 251 |
fetch & esl\_sqio\_Fetch \\ |
| 252 |
fetch\_info & esl\_sqio\_FetchInfo \\ |
| 253 |
fetch\_subseq & esl\_sqio\_FetchSubseq \\ |
| 254 |
is\_rewindable & esl\_sqfile\_IsRewindable \\ |
| 255 |
get\_error & esl\_sqfile\_GetErrorBuf \\ |
| 256 |
\end{tabular} |
| 257 |
|
| 258 |
\bigskip |
| 259 |
A hook needs to be added to the function \ccode{sqfile\_open}. |
| 260 |
This hook will try to open the specified file. If successful, |
| 261 |
the \ccode{ESL\_SQFILE} structure should be filled in with function |
| 262 |
pointers and the parser specific data and the open hook |
| 263 |
return \ccode{eslOK}. If the sequence files were not found for |
| 264 |
the specific parser, an \ccode{eslENOTFOUND} is returned and |
| 265 |
the next parser tries to open the file. Below is an example |
| 266 |
of code that tries to open an NCBI BLAST database if not |
| 267 |
successful, then the ASCII sequence parsers try to open the |
| 268 |
file. |
| 269 |
|
| 270 |
\begin{cchunk} |
| 271 |
if (format == eslSQFILE\_NCBI && status == eslENOTFOUND) |
| 272 |
status = esl\_sqncbi\_Open(sqfp->filename, sqfp->format, sqfp); |
| 273 |
if (status == eslENOTFOUND) |
| 274 |
status = esl\_sqascii\_Open(sqfp->filename, sqfp->format, sqfp); |
| 275 |
\end{cchunk} |