This file is indexed.

/usr/share/doc/python-htseq-doc/html/sequences.html is in python-htseq-doc 0.6.1p1-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Sequences and FASTA/FASTQ files &mdash; HTSeq 0.6.1p1 documentation</title>
    
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '0.6.1p1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="HTSeq 0.6.1p1 documentation" href="index.html" />
    <link rel="next" title="Genomic intervals and genomic arrays" href="genomic.html" />
    <link rel="prev" title="Counting reads" href="counting.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="genomic.html" title="Genomic intervals and genomic arrays"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="counting.html" title="Counting reads"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">HTSeq 0.6.1p1 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="sequences-and-fasta-fastq-files">
<span id="sequences"></span><h1>Sequences and FASTA/FASTQ files<a class="headerlink" href="#sequences-and-fasta-fastq-files" title="Permalink to this headline"></a></h1>
<div class="section" id="sequence">
<h2><code class="docutils literal"><span class="pre">Sequence</span></code><a class="headerlink" href="#sequence" title="Permalink to this headline"></a></h2>
<p>A <strong>Sequence</strong> object holds a DNA sequence. Besides the actual sequence, an object
may also hold a name.</p>
<dl class="docutils">
<dt>Instantiation</dt>
<dd><dl class="first class">
<dt id="HTSeq.Sequence">
<em class="property">class </em><code class="descclassname">HTSeq.</code><code class="descname">Sequence</code><span class="sig-paren">(</span><em>seq</em>, <em>name=&quot;unnamed&quot;</em><span class="sig-paren">)</span><a class="headerlink" href="#HTSeq.Sequence" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<p>Pass the DNA sequence and, optionally, a name or ID to the constructor:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">myseq</span> <span class="o">=</span> <span class="n">HTSeq</span><span class="o">.</span><span class="n">Sequence</span><span class="p">(</span> <span class="s">&quot;ACCGTTAC&quot;</span><span class="p">,</span> <span class="s">&quot;my_sequence&quot;</span> <span class="p">)</span>
</pre></div>
</div>
<p class="last">(If the name is omitted, the default <code class="docutils literal"><span class="pre">&quot;unnamed&quot;</span></code> is used.)</p>
</dd>
<dt>Attributes</dt>
<dd><dl class="first attribute">
<dt id="HTSeq.Sequence.seq">
<code class="descclassname">Sequence.</code><code class="descname">seq</code><a class="headerlink" href="#HTSeq.Sequence.seq" title="Permalink to this definition"></a></dt>
<dt id="HTSeq.Sequence.name">
<code class="descclassname">Sequence.</code><code class="descname">name</code><a class="headerlink" href="#HTSeq.Sequence.name" title="Permalink to this definition"></a></dt>
<dt id="HTSeq.Sequence.descr">
<code class="descclassname">Sequence.</code><code class="descname">descr</code><a class="headerlink" href="#HTSeq.Sequence.descr" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<p>The information can be accessed via the attributes <strong>seq</strong> and <strong>name</strong>, which are strings:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">myseq</span><span class="o">.</span><span class="n">seq</span>
<span class="go">&#39;ACCGTTAC&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">myseq</span><span class="o">.</span><span class="n">name</span>
<span class="go">&#39;my_sequence&#39;</span>
</pre></div>
</div>
<p class="last">There is a third attribute, called <strong>descr</strong>, which is by default <code class="docutils literal"><span class="pre">None</span></code> but may contain
a &#8220;description&#8221;. See class <a class="reference internal" href="#HTSeq.FastaReader" title="HTSeq.FastaReader"><code class="xref py py-class docutils literal"><span class="pre">FastaReader</span></code></a> for more information.</p>
</dd>
</dl>
<p>Representation and string conversion</p>
<blockquote>
<div><p>The <strong>__repr__</strong> method gives name and length:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">myseq</span>
<span class="go">&lt;Sequence object &#39;my_sequence&#39; (length 8)&gt;</span>
</pre></div>
</div>
<p>The <strong>__str__</strong> method returns just the sequence:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">myseq</span>
<span class="go">ACCGTTAC</span>
</pre></div>
</div>
<p>Note that the length of a sequence is the number of bases:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span> <span class="n">myseq</span> <span class="p">)</span>
<span class="go">8</span>
</pre></div>
</div>
</div></blockquote>
<dl class="docutils">
<dt>Subsetting</dt>
<dd><p class="first">Subsetting works as with strings:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">myseq2</span> <span class="o">=</span> <span class="n">myseq</span><span class="p">[</span><span class="mi">3</span><span class="p">:</span><span class="mi">5</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">myseq2</span><span class="o">.</span><span class="n">name</span>
<span class="go">&#39;my_sequence[part]&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">myseq2</span><span class="o">.</span><span class="n">seq</span>
<span class="go">&#39;GT&#39;</span>
</pre></div>
</div>
<p class="last">(Note that <code class="docutils literal"><span class="pre">&quot;[part]&quot;</span></code> is appended to the name of the subsetted copy.)</p>
</dd>
</dl>
<p>Reverse complement</p>
<blockquote>
<div><dl class="method">
<dt id="HTSeq.Sequence.get_reverse_complement">
<code class="descclassname">Sequence.</code><code class="descname">get_reverse_complement</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#HTSeq.Sequence.get_reverse_complement" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

</div></blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">myseq</span><span class="o">.</span><span class="n">get_reverse_complement</span><span class="p">()</span>
<span class="go">GTAACGGT</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rc</span> <span class="o">=</span> <span class="n">myseq</span><span class="o">.</span><span class="n">get_reverse_complement</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rc</span><span class="o">.</span><span class="n">name</span>
<span class="go">&#39;revcomp_of_my_sequence&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rc</span><span class="o">.</span><span class="n">seq</span>
<span class="go">&#39;GTAACGGT&#39;</span>
</pre></div>
</div>
<p>Writing to FASTA file</p>
<blockquote>
<div><p>To write <strong>Sequence</strong> objects into a FASTA file, open a text file for writing,
then call <strong>write_to_fasta_file</strong> for each sequence, providing the open
file handle as only argument, and close the file:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">my_fasta_file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span> <span class="s">&quot;test.fa&quot;</span><span class="p">,</span> <span class="s">&quot;w&quot;</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">myseq</span><span class="o">.</span><span class="n">write_to_fasta_file</span><span class="p">(</span> <span class="n">my_fasta_file</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">my_fasta_file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>To read from a FASTA file, see class <a class="reference internal" href="#HTSeq.FastaReader" title="HTSeq.FastaReader"><code class="xref py py-class docutils literal"><span class="pre">FastaReader</span></code></a>.</p>
</div></blockquote>
<dl class="docutils">
<dt>Extended UIPAC letters</dt>
<dd>These are not (yet) supported. A sequence should only contain A, C, G, T
and N.</dd>
</dl>
<p>Counting bases</p>
<blockquote>
<div><p>For read quality assessment, it is often helpful to count the proportions
of called bases, stratified by position in the read. To obtain such counts,
the following idiom is helpful:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">reads</span> <span class="o">=</span> <span class="n">HTSeq</span><span class="o">.</span><span class="n">FastqReader</span><span class="p">(</span> <span class="s">&quot;yeast_RNASeq_excerpt_sequence.txt&quot;</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">counts</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span> <span class="p">(</span> <span class="mi">36</span><span class="p">,</span> <span class="mi">5</span> <span class="p">),</span> <span class="n">numpy</span><span class="o">.</span><span class="n">int</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">read</span> <span class="ow">in</span> <span class="n">reads</span><span class="p">:</span>
<span class="gp">... </span>    <span class="n">read</span><span class="o">.</span><span class="n">add_bases_to_count_array</span><span class="p">(</span> <span class="n">counts</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">counts</span>        
<span class="go">array([[16194,  2048,  4017,  2683,    57],</span>
<span class="go">       [10716,  3321,  4933,  6029,     0],</span>
<span class="go">       [ 7816,  5024,  5946,  6213,     0],</span>
<span class="go">       ...</span>
<span class="go">       [ 8526,  4812,  5460,  6197,     4],</span>
<span class="go">       [ 8088,  4915,  5531,  6464,     1]])</span>
</pre></div>
</div>
<p>Here, a two-dimensional numpy array of integer zeroes is defined and then
passed to the <strong>add_bases_to_count_array</strong> method of each Sequence object obtained
from the Fastq file. The method <em>add_bases_to_count_array</em> adds, for each base,
a one to one of the array elements such that, in the end, the 36 rows of the array
correspond to the positions in the reads (all of length 36 bp in this example), and
the 5 columns correspond to the base letters &#8216;A&#8217;, &#8216;C&#8217;, &#8216;G&#8217;, &#8216;T&#8217;, and &#8216;N&#8217;, as given by
the constant <strong>base_to_columns</strong></p>
<dl class="data">
<dt>
<code class="descname">base_to_column = { 'A': 0, 'C': 1, 'G': 2, 'T': 3, 'N': 4 }</code></dt>
<dd></dd></dl>

<p>Hence, we can get the proportion of &#8216;C&#8217;s in each position as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">counts</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="n">counts</span><span class="p">,</span> <span class="n">numpy</span><span class="o">.</span><span class="n">float</span> <span class="p">)</span> 
<span class="gp">&gt;&gt;&gt; </span><span class="c">#counts[ : , HTSeq.base_to_column[&#39;C&#39;] ] / counts.sum(1)</span>
<span class="go">array([ 0.08192328,  0.13284531,  0.20096804,  0.16872675,  0.21200848,</span>
<span class="go">        ...</span>
<span class="go">        0.18560742,  0.19236769,  0.19088764,  0.17872715,  0.1924877 ,</span>
<span class="go">        0.19660786])</span>
</pre></div>
</div>
<p>(Here, we first convert the count array to type <code class="docutils literal"><span class="pre">float</span></code> to allow to proper
division, and then divide the second column (<code class="docutils literal"><span class="pre">HTSeq.base_to_column['C']</span></code>) by
the row-wise sums (<code class="docutils literal"><span class="pre">counts.sum(1)</span></code>; the <code class="docutils literal"><span class="pre">1</span></code> requests summing along rows).)</p>
</div></blockquote>
<p>Trimming reads</p>
<blockquote>
<div><dl class="method">
<dt id="HTSeq.Sequence.trim_left_end">
<code class="descclassname">Sequence.</code><code class="descname">trim_left_end</code><span class="sig-paren">(</span><em>pattern</em>, <em>mismatch_prop = 0.</em><span class="sig-paren">)</span><a class="headerlink" href="#HTSeq.Sequence.trim_left_end" title="Permalink to this definition"></a></dt>
<dt id="HTSeq.Sequence.trim_right_end">
<code class="descclassname">Sequence.</code><code class="descname">trim_right_end</code><span class="sig-paren">(</span><em>pattern</em>, <em>mismatch_prop = 0.</em><span class="sig-paren">)</span><a class="headerlink" href="#HTSeq.Sequence.trim_right_end" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<p>In high-throughput sequencing, reads are sometimes contaminated with adapters
or sequencing primers. These function take a pattern and attempt to match either
the right end of the pattern to the left end of the sequence (<code class="docutils literal"><span class="pre">trim_left_end</span></code>)
or the left end of the pattern to the right end of the sequence (<code class="docutils literal"><span class="pre">trim_right_end</span></code>).
The match is the trimmed off.</p>
<p>Here is an example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">seq2</span> <span class="o">=</span> <span class="n">HTSeq</span><span class="o">.</span><span class="n">Sequence</span><span class="p">(</span> <span class="s">&quot;ACGTAAAGCGGTACGGGGGG&quot;</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">left_seq</span> <span class="o">=</span> <span class="n">HTSeq</span><span class="o">.</span><span class="n">Sequence</span><span class="p">(</span> <span class="s">&quot;CCCACG&quot;</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">seq2</span><span class="o">.</span><span class="n">trim_left_end</span><span class="p">(</span> <span class="n">left_seq</span> <span class="p">)</span>
<span class="go">TAAAGCGGTACGGGGGG</span>
</pre></div>
</div>
<p>The right end of the pattern (&#8220;ACG&#8221;) matched the left end of the sequence, and
has hence been trimmed off.</p>
<p>The optional argument <code class="docutils literal"><span class="pre">mismatch_prop</span></code> is the number of allowed mismatches as
proportion of the length of the match:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">right_seq</span> <span class="o">=</span> <span class="n">HTSeq</span><span class="o">.</span><span class="n">Sequence</span><span class="p">(</span> <span class="s">&quot;GGGTGGG&quot;</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">seq2</span><span class="o">.</span><span class="n">trim_right_end</span><span class="p">(</span> <span class="n">right_seq</span> <span class="p">)</span>
<span class="go">ACGTAAAGCGGTACGGG</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">seq2</span><span class="o">.</span><span class="n">trim_right_end</span><span class="p">(</span> <span class="n">right_seq</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="mf">6.</span> <span class="p">)</span>
<span class="go">ACGTAAAGCGGTAC</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">seq2</span><span class="o">.</span><span class="n">trim_right_end</span><span class="p">(</span> <span class="n">right_seq</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="mf">7.</span> <span class="p">)</span>
<span class="go">ACGTAAAGCGGTACGGG</span>
</pre></div>
</div>
<p>Here, if we allow at least one mismatch per six bases, the whole pattern gets cut off.</p>
<p>If you have quality information, you can use this, too, to specify the allowed amount
of mismatch. See <a class="reference internal" href="#HTSeq.SequenceWithQualities.trim_left_end_with_quals" title="HTSeq.SequenceWithQualities.trim_left_end_with_quals"><code class="xref py py-meth docutils literal"><span class="pre">SequenceWithQualities.trim_left_end_with_quals()</span></code></a> and
<a class="reference internal" href="#HTSeq.SequenceWithQualities.trim_left_end_with_quals" title="HTSeq.SequenceWithQualities.trim_left_end_with_quals"><code class="xref py py-meth docutils literal"><span class="pre">SequenceWithQualities.trim_left_end_with_quals()</span></code></a>.</p>
</div></blockquote>
</div>
<div class="section" id="sequencewithqualities">
<h2><code class="docutils literal"><span class="pre">SequenceWithQualities</span></code><a class="headerlink" href="#sequencewithqualities" title="Permalink to this headline"></a></h2>
<p>The sequences obtained from high-throughput sequencing devices (in the following also
referred to as &#8220;reads&#8221;) typically come with <cite>base-call quality scores</cite>, which indicate
how sure the software was that the right base was called. The class <code class="docutils literal"><span class="pre">SequenceWithQualities</span></code> represents such reads.</p>
<p><code class="docutils literal"><span class="pre">SequenceWithQualities</span></code> is a daughter class of <a class="reference internal" href="#HTSeq.Sequence" title="HTSeq.Sequence"><code class="xref py py-class docutils literal"><span class="pre">Sequence</span></code></a> and inherits all its features.</p>
<p>Instantiation</p>
<blockquote>
<div><dl class="class">
<dt id="HTSeq.SequenceWithQualities">
<em class="property">class </em><code class="descclassname">HTSeq.</code><code class="descname">SequenceWithQualities</code><span class="sig-paren">(</span><em>seq</em>, <em>name qualstr</em>, <em>qualscale=&quot;phred&quot;</em><span class="sig-paren">)</span><a class="headerlink" href="#HTSeq.SequenceWithQualities" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<p>A <code class="docutils literal"><span class="pre">SequenceWithQualities</span></code> can be instantiated as a <code class="docutils literal"><span class="pre">Sequence</span></code>, but now with
a third argument, the quality string:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">myread</span> <span class="o">=</span> <span class="n">HTSeq</span><span class="o">.</span><span class="n">SequenceWithQualities</span><span class="p">(</span> <span class="s">&quot;ACGACTGACC&quot;</span><span class="p">,</span> <span class="s">&quot;my_read&quot;</span><span class="p">,</span> <span class="s">&quot;IICGAB##(!&quot;</span> <span class="p">)</span>
</pre></div>
</div>
<p>The quality string is interpreted as Sanger-encoded string of Phred values, as
defined in the <a class="reference external" href="http://maq.sourceforge.net/fastq.shtml">FASTQ format specification</a>, i.e., each letter in the quality
string corresponds to one base in the sequence and if the value 33 is subtracted
from the quality characters ASCII value, the Phred score is obtained.</p>
<p>The Phred scores can then be found in the slot <code class="docutils literal"><span class="pre">qual</span></code>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">myread</span><span class="o">.</span><span class="n">qualstr</span>
<span class="go">&#39;IICGAB##(!&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">myread</span><span class="o">.</span><span class="n">qual</span>
<span class="go">array([40, 40, 34, 38, 32, 33,  2,  2,  7,  0])</span>
</pre></div>
</div>
<p>If the quality string follows the <cite>Solexa FASTQ</cite> specification, the value to be
subtracted is not 33 but 64. If you pass a quality string in this format, set
<code class="docutils literal"><span class="pre">qualscale=&quot;solexa&quot;</span></code>.</p>
<p>Prior to version 1.3, the SolexaPipeline software used a yet another style of encoding
quality string. If you want to use this one, specify <code class="docutils literal"><span class="pre">qualscale=&quot;solexa-old&quot;</span></code></p>
</div></blockquote>
<p>Attributes</p>
<blockquote>
<div><p>As for <code class="docutils literal"><span class="pre">Sequence</span></code> objects, there are attributes <code class="docutils literal"><span class="pre">name</span></code>, <code class="docutils literal"><span class="pre">seq</span></code>, and <code class="docutils literal"><span class="pre">descr</span></code>.</p>
<p>Furthermore, we now have the attributes <code class="docutils literal"><span class="pre">qual</span></code> and <code class="docutils literal"><span class="pre">qualstr</span></code>, already mentioned
above.</p>
<dl class="attribute">
<dt id="HTSeq.SequenceWithQuality.qual">
<code class="descclassname">SequenceWithQuality.</code><code class="descname">qual</code><a class="headerlink" href="#HTSeq.SequenceWithQuality.qual" title="Permalink to this definition"></a></dt>
<dd><p><code class="docutils literal"><span class="pre">qual</span></code> is a <code class="docutils literal"><span class="pre">numpy</span></code> array of data type <em>integer</em>, with as many elements
as there are bases. Each element is a <cite>Phred score</cite>. A Phred score <em>S</em> is
defined to mean that the base caller estimates the probability <em>p</em> of the
base call being wrong as <em>p</em> = -log10 ( <em>S</em>/10 ).</p>
<p>Note that <code class="docutils literal"><span class="pre">qual</span></code> is always the probability, even if the <code class="docutils literal"><span class="pre">solexa-old</span></code> quality
string format has been used, which encodes the odds <em>p</em> ( 1 - <em>p</em> ), i.e., in that case,
the odds are converted to probabilities.</p>
</dd></dl>

<dl class="attribute">
<dt id="HTSeq.SequenceWithQuality.qualstr">
<code class="descclassname">SequenceWithQuality.</code><code class="descname">qualstr</code><a class="headerlink" href="#HTSeq.SequenceWithQuality.qualstr" title="Permalink to this definition"></a></dt>
<dd><p>The quality string according to Sanger Phred encoding. In case the quality was
originally given in <code class="docutils literal"><span class="pre">solexa</span></code> or <code class="docutils literal"><span class="pre">solexa-old</span></code> format, it is converted:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">read2</span> <span class="o">=</span> <span class="n">HTSeq</span><span class="o">.</span><span class="n">SequenceWithQualities</span><span class="p">(</span> <span class="s">&quot;ACGACTGACC&quot;</span><span class="p">,</span> <span class="s">&quot;my_read&quot;</span><span class="p">,</span> <span class="s">&quot;hhgddaZVFF&quot;</span><span class="p">,</span> <span class="s">&quot;solexa&quot;</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">read2</span><span class="o">.</span><span class="n">qual</span>
<span class="go">array([40, 40, 39, 36, 36, 33, 26, 22,  6,  6])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">read2</span><span class="o">.</span><span class="n">qualstr</span>
<span class="go">&quot;IIHEEB;7&#39;&#39;&quot;</span>
</pre></div>
</div>
</dd></dl>

</div></blockquote>
<p>Writing to FASTQ file</p>
<blockquote>
<div><dl class="method">
<dt id="HTSeq.SequenceWithQuality.write_to_fastq_file">
<code class="descclassname">SequenceWithQuality.</code><code class="descname">write_to_fastq_file</code><span class="sig-paren">(</span><em>fasta_file</em><span class="sig-paren">)</span><a class="headerlink" href="#HTSeq.SequenceWithQuality.write_to_fastq_file" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<p>To write <code class="docutils literal"><span class="pre">SequenceWithQualities</span></code> objects into a FASTQ file, open a text file for writing,
then call <code class="docutils literal"><span class="pre">write_to_fastq_file</span></code> for each sequence, providing the open
file handle as only argument, and close the file:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">my_fastq_file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span> <span class="s">&quot;test.fq&quot;</span><span class="p">,</span> <span class="s">&quot;w&quot;</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">myread</span><span class="o">.</span><span class="n">write_to_fastq_file</span><span class="p">(</span> <span class="n">my_fastq_file</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">my_fastq_file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>Note that the reads will always be written with quality strings in Sanger encoding.</p>
<p>To read from a FASTQ file, see class <a class="reference internal" href="#HTSeq.FastqReader" title="HTSeq.FastqReader"><code class="xref py py-class docutils literal"><span class="pre">FastqReader</span></code></a>.</p>
</div></blockquote>
<p>Counting quality values</p>
<blockquote>
<div><p>Similar to <code class="xref py py-meth docutils literal"><span class="pre">Sequence.add_bases_to_count_array()</span></code>, this method counts the
occuring quality values stratified by position. This then allows to calculate
average qualities as well as histograms.</p>
<p>Here is a usage example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span>       
<span class="gp">&gt;&gt;&gt; </span><span class="n">reads</span> <span class="o">=</span> <span class="n">HTSeq</span><span class="o">.</span><span class="n">FastqReader</span><span class="p">(</span> <span class="s">&quot;yeast_RNASeq_excerpt_sequence.txt&quot;</span><span class="p">,</span> <span class="s">&quot;solexa&quot;</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">counts</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span> <span class="p">(</span> <span class="mi">36</span><span class="p">,</span> <span class="mi">41</span> <span class="p">),</span> <span class="n">numpy</span><span class="o">.</span><span class="n">int</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">read</span> <span class="ow">in</span> <span class="n">reads</span><span class="p">:</span>
<span class="gp">... </span>   <span class="n">read</span><span class="o">.</span><span class="n">add_qual_to_count_array</span><span class="p">(</span> <span class="n">counts</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c">#counts</span>
<span class="go">array([[   0,    0,   64, ...,    0,    0,    0],</span>
<span class="go">       [   0,    0,   93, ...,    0,    0,    0],</span>
<span class="go">       ...,</span>
<span class="go">       [   0,    0, 2445, ...,    0,    0,    0],</span>
<span class="go">       [   0,    0, 2920, ...,    0,    0,    0]])</span>
</pre></div>
</div>
<p>The value <code class="docutils literal"><span class="pre">counts[i,j]</span></code> is then the number of reads for which the base at
position <code class="docutils literal"><span class="pre">i</span></code> hat the  quality scores <code class="docutils literal"><span class="pre">j</span></code>. According to the Fastq standard,
quality scores range from 0 to 40; hence, the array is initialized to have
41 columns.</p>
</div></blockquote>
<p>Trimming reads</p>
<blockquote>
<div><dl class="method">
<dt id="HTSeq.SequenceWithQualities.trim_left_end_with_quals">
<code class="descclassname">SequenceWithQualities.</code><code class="descname">trim_left_end_with_quals</code><span class="sig-paren">(</span><em>pattern</em>, <em>max_mm_qual = 5</em><span class="sig-paren">)</span><a class="headerlink" href="#HTSeq.SequenceWithQualities.trim_left_end_with_quals" title="Permalink to this definition"></a></dt>
<dt id="HTSeq.SequenceWithQualities.trim_right_end_with_quals">
<code class="descclassname">SequenceWithQualities.</code><code class="descname">trim_right_end_with_quals</code><span class="sig-paren">(</span><em>pattern</em>, <em>max_mm_qual = 5</em><span class="sig-paren">)</span><a class="headerlink" href="#HTSeq.SequenceWithQualities.trim_right_end_with_quals" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<p>These methods work as <a class="reference internal" href="#HTSeq.Sequence.trim_left_end" title="HTSeq.Sequence.trim_left_end"><code class="xref py py-meth docutils literal"><span class="pre">Sequence.trim_left_end()</span></code></a> and <a class="reference internal" href="#HTSeq.Sequence.trim_right_end" title="HTSeq.Sequence.trim_right_end"><code class="xref py py-meth docutils literal"><span class="pre">Sequence.trim_right_end()</span></code></a>
(which are, of course, avilable for <code class="docutils literal"><span class="pre">SequenceWithQualities</span></code> objects, too). The difference
is, that for the <code class="docutils literal"><span class="pre">_with_quals</span></code> trimming methods, the maximum amount of allowed mismatch is
specified as the maximum value that the sum of the quality scores of the mismatched bases
may take.</p>
<p><em>TODO</em>: Add example</p>
</div></blockquote>
</div>
<div class="section" id="fastareader">
<h2><code class="docutils literal"><span class="pre">FastaReader</span></code><a class="headerlink" href="#fastareader" title="Permalink to this headline"></a></h2>
<p>The FastaReader class allows to read and parse a FASTA file. It can generates an
iterator of <code class="docutils literal"><span class="pre">Sequence</span></code> objects.</p>
<dl class="class">
<dt id="HTSeq.FastaReader">
<em class="property">class </em><code class="descclassname">HTSeq.</code><code class="descname">FastaReader</code><span class="sig-paren">(</span><em>filename_or_sequence</em><span class="sig-paren">)</span><a class="headerlink" href="#HTSeq.FastaReader" title="Permalink to this definition"></a></dt>
<dd><p>As daughter class of <code class="docutils literal"><span class="pre">FileOrSequence</span></code>, <code class="docutils literal"><span class="pre">FastaReader</span></code> can be instantiated
with either a filename, or with a sequence. See <a class="reference internal" href="misc.html#HTSeq.FileOrSequence" title="HTSeq.FileOrSequence"><code class="xref py py-class docutils literal"><span class="pre">FileOrSequence</span></code></a> for details.</p>
</dd></dl>

<dl class="docutils">
<dt>Example 1</dt>
<dd><p class="first">The typical use for FastaReader is to go through a FASTA file and do something with
each sequence, e.g.:</p>
<div class="last highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">HTSeq</span><span class="o">.</span><span class="n">FastaReader</span><span class="p">(</span> <span class="s">&quot;fastaEx.fa&quot;</span> <span class="p">):</span>
<span class="gp">... </span>    <span class="k">print</span> <span class="s">&quot;Sequence &#39;</span><span class="si">%s</span><span class="s">&#39; has length </span><span class="si">%d</span><span class="s">.&quot;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">s</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="p">)</span>
<span class="go">Sequence &#39;sequence1&#39; has length 72.</span>
<span class="go">Sequence &#39;sequence2&#39; has length 70.</span>
</pre></div>
</div>
</dd>
<dt>Example 2</dt>
<dd><p class="first">Often, one might to read a whole Fasta file into memory to access it as a dict.
This is a good idiom for this purpose:</p>
<div class="last highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">sequences</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span> <span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">HTSeq</span><span class="o">.</span><span class="n">FastaReader</span><span class="p">(</span><span class="s">&quot;fastaEx.fa&quot;</span><span class="p">)</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sequences</span><span class="p">[</span><span class="s">&quot;sequence1&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">seq</span>
<span class="go">&#39;AGTACGTAGTCGCTGCTGCTACGGGCGCTAGCTAGTACGTCACGACGTAGATGCTAGCTGACTAAACGATGC&#39;</span>
</pre></div>
</div>
</dd>
</dl>
</div>
<div class="section" id="fastqreader">
<h2><code class="docutils literal"><span class="pre">FastqReader</span></code><a class="headerlink" href="#fastqreader" title="Permalink to this headline"></a></h2>
<p>The <strong>FastqReader</strong> class works similar to <a class="reference internal" href="#HTSeq.FastaReader" title="HTSeq.FastaReader"><code class="xref py py-class docutils literal"><span class="pre">FastaReader</span></code></a>. It reads a Fastq file
and generates an iterator over <a class="reference internal" href="#HTSeq.SequenceWithQualities" title="HTSeq.SequenceWithQualities"><code class="xref py py-class docutils literal"><span class="pre">SequenceWithQualities</span></code></a> objects.</p>
<dl class="class">
<dt id="HTSeq.FastqReader">
<em class="property">class </em><code class="descclassname">HTSeq.</code><code class="descname">FastqReader</code><span class="sig-paren">(</span><em>filename_or_sequence</em>, <em>qual_scale=&quot;phred&quot;</em><span class="sig-paren">)</span><a class="headerlink" href="#HTSeq.FastqReader" title="Permalink to this definition"></a></dt>
<dd><p>As daughter class of <code class="docutils literal"><span class="pre">FileOrSequence</span></code>, <code class="docutils literal"><span class="pre">FastaReader</span></code> can be instantiated
with either a filename, or with a sequence. See <a class="reference internal" href="misc.html#HTSeq.FileOrSequence" title="HTSeq.FileOrSequence"><code class="xref py py-class docutils literal"><span class="pre">FileOrSequence</span></code></a> for details.</p>
<p>By default, the quality strings are assumed to be encoded according to the
Sanger/Phred standard. You may alternatively specify <code class="docutils literal"><span class="pre">&quot;solexa&quot;</span></code> or <code class="docutils literal"><span class="pre">&quot;solexa-old&quot;</span></code>
(see <code class="xref py py-class docutils literal"><span class="pre">SequenceWithQuality</span></code>).</p>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Sequences and FASTA/FASTQ files</a><ul>
<li><a class="reference internal" href="#sequence"><code class="docutils literal"><span class="pre">Sequence</span></code></a></li>
<li><a class="reference internal" href="#sequencewithqualities"><code class="docutils literal"><span class="pre">SequenceWithQualities</span></code></a></li>
<li><a class="reference internal" href="#fastareader"><code class="docutils literal"><span class="pre">FastaReader</span></code></a></li>
<li><a class="reference internal" href="#fastqreader"><code class="docutils literal"><span class="pre">FastqReader</span></code></a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="counting.html"
                        title="previous chapter">Counting reads</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="genomic.html"
                        title="next chapter">Genomic intervals and genomic arrays</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/sequences.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="genomic.html" title="Genomic intervals and genomic arrays"
             >next</a> |</li>
        <li class="right" >
          <a href="counting.html" title="Counting reads"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">HTSeq 0.6.1p1 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &copy; Copyright 2010, Simon Anders.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.3.
    </div>
  </body>
</html>