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
|
r"""
Sequences (:mod:`skbio.sequence`)
=================================
.. currentmodule:: skbio.sequence
This module provides classes for storing and working with sequences, including
generic/nonbiological sequences which have no alphabet restrictions
(``Sequence``) and sequences based on IUPAC-defined alphabets (``DNA``,
``RNA``, ``Protein``). Common operations are defined as methods, for example
computing the reverse complement of a DNA sequence, or searching for
N-glycosylation motifs in protein sequences. Class attributes provide valid
character sets, complement maps for different sequence types, and degenerate
character definitions. Additionally this module defines the ``GeneticCode``
class, which represents an immutable object that translates DNA or RNA
sequences into protein sequences.
The primary information stored for each different type of sequence object is
the underlying sequence data itself. This is stored as an immutable numpy
array. Additionally, each type of sequence may include optional metadata
and positional metadata. Note that metadata and positional metadata are
mutable.
Classes
-------
.. autosummary::
:toctree: generated/
Sequence
GrammaredSequence
DNA
RNA
Protein
GeneticCode
Subpackages
-----------
.. autosummary::
:toctree: generated/
distance
Examples
--------
New sequences are created with optional metadata and positional metadata.
Metadata is stored as a Python ``dict``, while positional metadata is stored as
a pandas ``DataFrame``.
>>> from skbio import DNA, RNA
>>> d = DNA('ACCGGGTA', metadata={'id':"my-sequence", 'description':"GFP"},
... positional_metadata={'quality':[22, 25, 22, 18, 23, 25, 25, 25]})
>>> d
DNA
-----------------------------
Metadata:
'description': 'GFP'
'id': 'my-sequence'
Positional metadata:
'quality': <dtype: int64>
Stats:
length: 8
has gaps: False
has degenerates: False
has definites: True
GC-content: 62.50%
-----------------------------
0 ACCGGGTA
New sequences can also be created from existing sequences, for example as their
reverse complement or degapped (i.e., unaligned) version.
>>> d1 = DNA('.ACC--GGG-TA...', metadata={'id':'my-sequence'})
>>> d2 = d1.degap()
>>> d2
DNA
--------------------------
Metadata:
'id': 'my-sequence'
Stats:
length: 8
has gaps: False
has degenerates: False
has definites: True
GC-content: 62.50%
--------------------------
0 ACCGGGTA
>>> d3 = d2.reverse_complement()
>>> d3
DNA
--------------------------
Metadata:
'id': 'my-sequence'
Stats:
length: 8
has gaps: False
has degenerates: False
has definites: True
GC-content: 62.50%
--------------------------
0 TACCCGGT
It's also straightforward to compute distances between sequences (optionally
using user-defined distance metrics, the default is Hamming distance which
requires that the sequences being compared are the same length) for use in
sequence clustering, phylogenetic reconstruction, etc.
>>> r1 = RNA('GACCCGCUUU')
>>> r2 = RNA('GCCCCCCUUU')
>>> r1.distance(r2)
0.2
Similarly, you can calculate the percent (dis)similarity between a pair of
aligned sequences.
>>> r3 = RNA('ACCGUUAGUC')
>>> r4 = RNA('ACGGGU--UC')
>>> r3.match_frequency(r4, relative=True)
0.6
>>> r3.mismatch_frequency(r4, relative=True)
0.4
Sequences can be searched for known motif types. This returns the slices that
describe the matches.
>>> r5 = RNA('AGG-GGACUGAA')
>>> for motif in r5.find_motifs('purine-run', min_length=2):
... motif
slice(0, 3, None)
slice(4, 7, None)
slice(9, 12, None)
Those slices can be used to extract the relevant subsequences.
>>> for motif in r5.find_motifs('purine-run', min_length=2):
... r5[motif]
... print('')
RNA
--------------------------
Stats:
length: 3
has gaps: False
has degenerates: False
has definites: True
GC-content: 66.67%
--------------------------
0 AGG
<BLANKLINE>
RNA
--------------------------
Stats:
length: 3
has gaps: False
has degenerates: False
has definites: True
GC-content: 66.67%
--------------------------
0 GGA
<BLANKLINE>
RNA
--------------------------
Stats:
length: 3
has gaps: False
has degenerates: False
has definites: True
GC-content: 33.33%
--------------------------
0 GAA
<BLANKLINE>
And gaps or other features can be ignored while searching, as these may disrupt
otherwise meaningful motifs.
>>> for motif in r5.find_motifs('purine-run', min_length=2, ignore=r5.gaps()):
... r5[motif]
... print('')
RNA
--------------------------
Stats:
length: 7
has gaps: True
has degenerates: False
has definites: True
GC-content: 66.67%
--------------------------
0 AGG-GGA
<BLANKLINE>
RNA
--------------------------
Stats:
length: 3
has gaps: False
has degenerates: False
has definites: True
GC-content: 33.33%
--------------------------
0 GAA
<BLANKLINE>
In the above example, removing gaps from the resulting motif matches is easily
achieved, as the sliced matches themselves are sequences of the same type as
the input.
>>> for motif in r5.find_motifs('purine-run', min_length=2, ignore=r5.gaps()):
... r5[motif].degap()
... print('')
RNA
--------------------------
Stats:
length: 6
has gaps: False
has degenerates: False
has definites: True
GC-content: 66.67%
--------------------------
0 AGGGGA
<BLANKLINE>
RNA
--------------------------
Stats:
length: 3
has gaps: False
has degenerates: False
has definites: True
GC-content: 33.33%
--------------------------
0 GAA
<BLANKLINE>
Sequences can similarly be searched for arbitrary patterns using regular
expressions.
>>> for match in r5.find_with_regex('(G+AC[UT])'):
... match
slice(4, 9, None)
DNA can be transcribed to RNA:
>>> dna = DNA('ATGTGTATTTGA')
>>> rna = dna.transcribe()
>>> rna
RNA
--------------------------
Stats:
length: 12
has gaps: False
has degenerates: False
has definites: True
GC-content: 25.00%
--------------------------
0 AUGUGUAUUU GA
Both DNA and RNA can be translated into a protein sequence. For example, let's
translate our DNA and RNA sequences using NCBI's standard genetic code (table
ID 1, the default genetic code in scikit-bio):
>>> protein_from_dna = dna.translate()
>>> protein_from_dna
Protein
--------------------------
Stats:
length: 4
has gaps: False
has degenerates: False
has definites: True
has stops: True
--------------------------
0 MCI*
>>> protein_from_rna = rna.translate()
>>> protein_from_rna
Protein
--------------------------
Stats:
length: 4
has gaps: False
has degenerates: False
has definites: True
has stops: True
--------------------------
0 MCI*
The two translations are equivalent:
>>> protein_from_dna == protein_from_rna
True
Class-level methods contain information about the molecule types.
>>> sorted(DNA.degenerate_map['B'])
['C', 'G', 'T']
>>> sorted(RNA.degenerate_map['B'])
['C', 'G', 'U']
"""
# ----------------------------------------------------------------------------
# Copyright (c) 2013--, scikit-bio development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
# ----------------------------------------------------------------------------
from skbio.util import TestRunner
from ._sequence import Sequence
from ._protein import Protein
from ._dna import DNA
from ._rna import RNA
from ._genetic_code import GeneticCode
from ._grammared_sequence import GrammaredSequence
__all__ = ['Sequence', 'Protein', 'DNA', 'RNA', 'GeneticCode',
'GrammaredSequence']
test = TestRunner(__file__).test
|