Debian Codegraph @

easel/esl_sq.tex

raw
1 The \eslmod{sq} module provides \Easel's object for single biological
2 sequences: an \ccode{ESL\_SQ}.
3
4 Sequence objects invariably become complicated, even though their
5 designer intends them to be simple. There's many things we want to do
6 with a sequence, and useful features naturally accrete over time. If a
7 library isn't careful to balance creeping featuritis against having an
8 easy way to start using the object in simple applications, then the
9 sequence object - possibly the most fundamental object of a
10 biosequence library - can become a barrier to anyone else actually
11 using the library. All those useful features won't matter much if you
12 can't figure out how to turn your sequence data into an object, or get
13 it back out. \Easel\ expects you to have your own preferred way of
14 dealing with sequence data that's not necessarily \Easel's way, so it
15 provides simple ways to create sequence objects from elemental (C
16 string) data, and simple ways to get elemental C strings back out.
17 This lets you minimize your exposure to \Easel's more complicated
18 capabilities if you like.
19
20 The most basic use of an \ccode{ESL\_SQ} object is to hold one
21 complete sequence, simply as a plain C string. A sequence may also
22 have a name, an accession, and a description line. This is called a
23 \esldef{text mode} sequence. In text mode, \Easel\ doesn't know
24 whether the sequence is DNA, RNA, protein, or something else; it's
25 just an ASCII character string. This limits some of \Easel's more
26 powerful abilities, such as the ability to check the sequence for
27 errors, or to automatically deal with degenerate residue codes; but
28 it's a simple mode that's easy to start using.
29
30 Alternatively, a sequence may be in \esldef{digital mode}. In digital
31 mode, sequences are predigested and encoded into \Easel's internal
32 format, which makes many sequence routines more robust, efficient, and
33 powerful.
34
35 In addition to storing a complete sequence, an \ccode{ESL\_SQ} is
36 designed to be used in three other situations:
37
38 \begin{itemize}
39 \item to hold a \esldef{subsequence} of a larger source sequence. The
40 object maintains source and coordinate information necessary for
41 crossreferencing the subsequence's coordinate system to the original
42 source coordinate system.
43
44 \item to hold a \esldef{window} of a larger source sequence. This is
45 like a subsequence, but is more specifically intended for reading a
46 sequence from a file in overlapping windows. This avoids having to
47 suck an entire chromosome (for example) into memory at any one
48 time. The stored subsequence is composed of two segments, a
49 \esldef{previous context} that gets saved from the previous window,
50 and a \esldef{new window} of fresh residues. The size of both the
51 context and the window are configurable at the time each new window
52 is read.
53
54 \item to hold only \esldef{information} about a sequence, such as its
55 name, its length, and its position in a file, excluding the sequence
56 (and optional secondary structure annotation) itself. This is handy
57 for example when indexing a sequence file, when we'd rather not read
58 any (possibly prohibitively large) sequence into memory until after
59 we've mapped out how big it is.
60 \end{itemize}
61
62 To keep all this straight, the object contains a bunch of internal
63 bookkeeping data.
64
65 Sequence objects are growable and reusable, for efficiency in memory
66 allocation. If you're going to go through many different sequences
67 sequentially, you would typically just allocate a single
68 \ccode{ESL\_SQ} object and \ccode{esl\_sq\_Reuse()} it for each new
69 sequence, rather than creating and destroying a lot of objects.
70
71 A sequence object can also store an optional secondary structure
72 annotation line for the sequence, one character per residue.
73
74 An interface to the \ccode{ESL\_MSA} multiple alignment object
75 provides the ability to extract single unaligned sequences from a
76 multiple alignment.
77
78 You would often use the \eslmod{sq} module in conjunction with
79 \eslmod{sqio}, which provides the ability to read and write
80 \ccode{ESL\_SQ} objects from and to files.
81
82 Table~\ref{tbl:sq_api} lists the functions in the \eslmod{sq} API.
83
84
85
86 % Table generated by autodoc -t esl_sq.c (so don't edit here, edit esl_sq.c:)
87 \input{apitables/esl_sq_api}
88
89
90
91
92 \subsection{Example of getting data in and out of an \ccodeincmd{ESL\_SQ}}
93
94 The easiest way to create a new \ccode{ESL\_SQ} object is with the
95 \ccode{esl\_sq\_CreateFrom()} function, which just takes character
96 strings for a sequence and its name (and also, optionally, an
97 accession, description, and/or secondary structure annotation string).
98
99 You can also build up (and/or change and manipulate) the contents of
100 an \ccode{ESL\_SQ} object by accessing the name, accession,
101 description, sequence, and structure annotation line more directly.
102
103 This code shows examples of both approaches:
104
105 \input{cexcerpts/sq_example}
106
107 A few things to notice about that code:
108
109 \begin{itemize}
110 \item Every sequence has a name and a sequence. If we didn't want to
111 add the optional accession, description, or structure annotation
112 line, we'd pass \ccode{NULL} for those arguments to
113 \ccode{esl\_sq\_CreateFrom()}.
114
115 \item An RNA secondary structure annotation line is shown here as part
116 of the example, but it's really sort of a more advanced
117 feature. It's good to know it's there (see the \eslmod{wuss} module
118 for more information about how \Easel\ annotates RNA structure) but
119 you can ignore it if you're getting started.
120
121 \item The \ccode{esl\_sq\_Set*} functions use the same syntax as C's
122 \ccode{*printf()} family, which gives you a flexible way to create
123 new sequence names, accessions, and descriptions automatically.
124
125 \item The sequence in \ccode{sq->seq} is just a C string. (Here it's a
126 copy of the \ccode{testseq} string.) That has a couple of
127 implications. One is that it's a verbatim copy of what you provided;
128 \Easel\ doesn't know (or care) whether it's DNA or protein sequence,
129 upper case or lower case, or if it contains illegal non-sequence
130 characters. With a text mode sequence, that's \emph{your} problem!
131 For more robustness and defined biosequence alphabets, read on below
132 about digital mode sequences. The second implication is that, as a C
133 string, the \ccode{n} residues are indexed \ccode{0..sq->n-1}, not
134 \ccode{1..sq->n}.
135
136 \item If you're going to directly copy a sequence of length \ccode{n}
137 into a \ccode{sq->seq} field, note the \ccode{esl\_sq\_GrowTo()}
138 call, which makes sure the sequence object is allocated with enough
139 space for \ccode{n} residues; and don't forget to set \ccode{sq->n}.
140
141 \item The structure annotation \ccode{sq->ss} is also a C string,
142 indexed identically to \ccode{sq->seq}, but it's optional, and isn't
143 allocated by default; \ccode{esl\_sq\_GrowTo()} calls will only
144 reallocate for the structure annotation string after it's been
145 allocated at least once. Hence the \ccode{esl\_strdup} call in the
146 example, which duplicates (allocates and copies) the annotation into
147 \ccode{sq->ss}.
148 \end{itemize}
149
150
151
152 To get simple character strings back out of an \ccode{ESL\_SQ} object,
153 you're encouraged to peek inside the object. (Yeah, I know, object
154 oriented design says that there should be methods for this,
155 independent of the object's implementation; but I balance that against
156 simplicity, and here, simplicity wins.) The object is defined and
157 documented in \ccode{esl\_sq.h}. It contains various information; the
158 stuff you need to know is:
159
160 \input{cexcerpts/sq_sq}
161
162 Ignore the \ccode{dsq} field for now; we're about to get to it, when
163 we talk about digital mode sequences.
164
165 The \ccode{ESL\_SQ} object itself doesn't particularly care about the
166 contents of the annotation text fields, so long as they're C strings, and so
167 long as \ccode{n} is the length of the \ccode{seq} (and optional
168 \ccode{ss}, if it's non-\ccode{NULL}) strings. However, sequence file
169 formats do impose some expectations on the annotation strings, and it
170 would be a Good Idea to adhere to them:
171
172 \begin{sreitems} {\emcode{desc}}
173 \item [\emcode{name}] A sequence name is almost always expected to be
174 a single ``word'' (no whitespace), like \ccode{SNRPA\_HUMAN}.
175
176 \item [\emcode{acc}] An accession is also usually expected to be a
177 single ``word'' with no whitespace, like \ccode{P09012}. Database
178 accessions only make sense if you know what database they're for, so
179 when sequences might be from different databases, you'll sometimes
180 see accessions prefixed with a code indicating the source database,
181 as in something like \ccode{UniProt:P09012}. Again, \Easel\ itself
182 isn't enforcing the format of this string, so your application is
183 free to create its own accession/version/database format as needed.
184
185 \item [\emcode{desc}] A description line is something like \ccode{U1
186 small nuclear ribonucleoprotein A (U1 snRNP protein A) (U1A protein)
187 (U1-A).}; a one-line summary of what the sequence is. You can expect
188 the description line to show up in the tabular output of sequence
189 analysis applications, so ideally you want it to be short and sweet
190 (so it fits on one line with a name, accession, score, coords, and
191 other information from an analysis app). You also don't want the
192 description line to end in a newline (\verb+\n+) character, or the
193 description line will introduce unexpected line breaks in these
194 tabular output files.
195 \end{sreitems}
196
197 You can reach into a \ccode{ESL\_SQ} and copy or modify any of these
198 strings, but don't try to overwrite them with a larger string unless
199 You Know What You're Doing. Their memory allocations are managed by
200 the \ccode{ESL\_SQ} object. Instead, use the appropriate
201 \ccode{esl\_sq\_Set*} function to overwrite an annotation field.
202
203 The \eslmod{sq} module isn't much use by itself; it's a building block
204 for several other modules. For example, one of the main things you'll
205 want to do with sequences is to read them from a file. For examples
206 and documentation of sequence input, see the \eslmod{sqio} module.
207
208
209 \subsection{Example of using a digital \ccodeincmd{ESL\_SQ}}
210
211 What follows might make more sense if you've read about the
212 \eslmod{alphabet} module first. \eslmod{alphabet}'s documentation
213 explains how \Easel uses an internal digital biosequence ``alphabet'',
214 where residues are encoded as small integers, suitable for direct use
215 as array indices. But here's an example anyway, of creating and
216 accessing a digital mode sequence:
217
218 \input{cexcerpts/sq_example2}
219
220 Things to notice about this code:
221
222 \begin{itemize}
223 \item An \ccode{ESL\_SQ} object has a \ccode{sq->seq} if it's in text
224 mode, and \ccode{sq->dsq} if its in digital mode. These two fields are
225 mutually exclusive; one of them is \ccode{NULL}.
226
227 \item If you looked at the contents of \ccode{sq->dsq} in either of
228 the objects, you'd see that each residue is encoded as a value
229 \ccode{0..3}, representing (for an RNA alphabet) the residues
230 \ccode{ACGU}.
231
232 \item That representation is defined by the digital RNA alphabet
233 \ccode{abc}, which was the first thing we created.
234
235 \item In digital mode, both the sequence residues and the optional
236 secondary structure characters are indexed \ccode{1..n}.
237
238 \item To make the digital sequence in the first sequence object, we
239 created a digital sequence \ccode{dsq} by encoding the
240 \ccode{testseq} using \ccode{esl\_abc\_CreateDsq()}; this
241 function allocated new memory for \ccode{dsq}, so we have to
242 free it. An \ccode{ESL\_DSQ *} is just a special character array;
243 it's not a full-fledged \Easel\ object, and so there's no
244 conventional \ccode{Create()},\ccode{Destroy()} function pair.
245
246 \item In the second sequence object, we used
247 \ccode{esl\_abc\_Digitize()} to encode the \ccode{testseq} directly
248 into space that the \ccode{sq2} object already had allocated, saving
249 us the temporary allocation of another \ccode{dsq}, because we
250 created it in digital mode (\ccode{esl\_sq\_CreateDigital()}) and
251 made it big enough to hold \ccode{n} digital residues with
252 \ccode{esl\_sq\_GrowTo()}. Notice that \ccode{esl\_sq\_GrowTo()} is
253 smart enough to know whether to grow the digital or the text mode
254 sequence field.
255
256 \item By convention, when using digital sequences, we usually keep
257 track of (and pass as arguments) both a digital sequence \ccode{dsq}
258 and its length \ccode{n}, and we also need to have the digital
259 alphabet itself \ccode{abc} available to know what the \ccode{dsq}
260 means; with text mode sequences, we usually just pass the string
261 pointer. Thus the \ccode{esl\_sq\_CreateDigitalFrom()} function
262 takes \ccode{abc}, \ccode{dsq}, and \ccode{n} as arguments, whereas
263 the counterpart text mode \ccode{esl\_sq\_CreateDigitalFrom()} only
264 took a C string \ccode{seq}. This is solely a convention - digital
265 sequences begin and end with a special sentinel character, so we
266 could always count the length of a \ccode{dsq} if we had to (using
267 \ccode{esl\_abc\_dsqlen()}, for example), much as we can use ANSI
268 C's \ccode{strlen()} to count the number of chars in a C string up
269 to the sentinel \verb+\0+ \ccode{NUL} character at the end.
270
271 \item To get the structure annotation to be indexed \ccode{1..n} for
272 consistency with the \ccode{dsq}, even though the annotation string
273 is still just an ASCII string, it's offset by one, and the leading
274 character is set by convention to a \verb+\0+. Therefore to access
275 the whole structure string (for printing, for instance), you want to
276 access \ccode{sq->ss+1}. This is a hack, but it's a simple one, so
277 long as you don't forget about the convention.
278
279 \item Because the original sequence has been encoded, you may not get
280 the original sequence back out when you decode the digital values as
281 alphabet symbols. \ccode{abc->sym[sq2->dsq[3]]}, for example, takes
282 the third digital residue and looks it up in the alphabet's symbol
283 table, returning the canonical character it's
284 representing. Upper/lower case distinctions are lost, for example;
285 digital alphabet symbol tables are uniformly upper case. And this
286 example shows another example, where the input \ccode{testseq}
287 contains T's, but since the digital alphabet was declared as RNA,
288 the symbol table represents those residues as U's when you access
289 them.
290
291 \item In that respect, a more careful example should have checked the
292 return status of the \ccode{esl\_abc\_CreateDsq()} and
293 \ccode{esl\_abc\_Digitize()} calls. These have a normal failure
294 mode, when the input text sequence contains one or more ASCII
295 characters that are unrecognized and therefore invalid in the
296 digital alphabet. If this had happened, these functions would have
297 returned \ccode{eslEINVAL} instead of \ccode{eslOK}. We can get away
298 without checking, however, because the functions just replace any
299 invalid character with an ``any'' character (representing \ccode{N}
300 for DNA or RNA, \ccode{X} for protein).
301 \end{itemize}
302
303 For more information about how digital sequence alphabets work, see
304 the \eslmod{alphabet} module.