skbio.sequence.DNA#

class skbio.sequence.DNA(sequence, metadata=None, positional_metadata=None, interval_metadata=None, lowercase=False, validate=True, copy=None)[source]#

Store DNA sequence data and optional associated metadata.

Parameters:
sequencestr, bytes-like, 1D ndarray (uint8 or ‘|S1’), or Sequence

Characters representing the DNA sequence.

metadatadict, optional

Arbitrary metadata which applies to the entire sequence.

positional_metadatapd.DataFrame consumable, optional

Arbitrary per-character metadata. For example, quality scores of sequencing reads. Must be able to pass directly to the pd.DataFrame constructor.

interval_metadataIntervalMetadata, optional

Arbitrary interval metadata which applies to intervals within the a sequence to store interval features (such as genes on the DNA sequence).

lowercasebool or str, optional

If True, lowercase sequence characters will be converted to uppercase to ensure they are valid IUPAC DNA characters. If False (default), characters will not be converted. If a string, in addition to the uppercase conversion, a boolean array indicating which positions were originally lowercase will be stored in the positional metadata under this key.

validatebool, optional

If True (default), validation will be performed to ensure that all sequence characters are in the IUPAC DNA character set. Turning off validation (False) will improve performance. If invalid characters are present, however, there is no guarantee that subsequent operations will retain the expected behavior. Only turn off validation if you are certain that the sequence characters are valid. To store sequence data that is not IUPAC-compliant, use Sequence.

copybool, optional

Control copying of sequence data. See Sequence for details.

Added in version 0.7.5.

Notes

According to the IUPAC DNA character set [1] , a DNA sequence may contain the following four definite characters (canonical nucleotides):

Code

Nucleobase

A

Adenine

C

Cytosine

G

Guanine

T

Thymine

And the following 11 degenerate characters, each of which representing 2-4 nucleotides:

Code

Nucleobases

Meaning

R

A or G

Purine

Y

C or T

Pyrimidine

S

G or C

Strong

W

A or T

Weak

K

G or T

Keto

M

A or C

Amino

B

C, G or T

Not A

D

A, G or T

Not C

H

A, C or T

Not G

V

A, C or G

Not T

N

A, C, G or T

Any

Plus two gap characters: - and ..

Characters other than the above 17 are not allowed. If you intend to use additional characters to represent non-canonical nucleobases, such as I (Inosine), you may create a custom alphabet using GrammaredSequence. Directly modifying the alphabet of DNA may break methods that rely on the IUPAC alphabet.

It should be noted that some functions do not support degenerate characters. In such cases, they will be replaced with the wildcard character N to represent any of the canonical nucleotides.

References

[1]

Cornish-Bowden, A. (1985). Nomenclature for incompletely specified bases in nucleic acid sequences: recommendations 1984. Nucleic Acids Res, 13(9), 3021.

Examples

>>> from skbio import DNA
>>> DNA('ACCGAAT')
DNA
--------------------------
Stats:
    length: 7
    has gaps: False
    has degenerates: False
    has definites: True
    GC-content: 42.86%
--------------------------
0 ACCGAAT

Convert lowercase characters to uppercase:

>>> DNA('AcCGaaT', lowercase=True)
DNA
--------------------------
Stats:
    length: 7
    has gaps: False
    has degenerates: False
    has definites: True
    GC-content: 42.86%
--------------------------
0 ACCGAAT

Attributes

complement_map

Return mapping of nucleotide characters to their complements.

default_gap_char

Gap character to use when constructing a new gapped sequence.

definite_chars

Characters representing definite states.

degenerate_map

Mapping of degenerate to definite characters.

gap_chars

Characters representing gaps in the sequence.

wildcard_char

Character representing any other non-gap character in the alphabet.

Attributes (inherited)

alphabet

All valid characters in the alphabet.

canonical_chars

Characters in the conventional core alphabet.

default_write_format

Default write format for this object: fasta.

degenerate_chars

Degenerate characters representing sets of definite characters.

interval_metadata

IntervalMetadata object containing info about interval features.

metadata

dict containing metadata which applies to the entire object.

noncanonical_chars

Non-canonical characters.

nondegenerate_chars

Non-degenerate characters.

observed_chars

Set of observed characters in the sequence.

positional_metadata

pd.DataFrame containing metadata along an axis.

values

Array containing underlying sequence characters.

Methods

find_motifs

Search the biological sequence for motifs.

read

Create a new DNA instance from a file.

transcribe

Transcribe DNA into RNA.

translate

Translate DNA sequence into protein sequence.

translate_six_frames

Translate DNA into protein using six possible reading frames.

write

Write an instance of DNA to a file.

Methods (inherited)

complement

Return the complement of the nucleotide sequence.

concat

Concatenate an iterable of Sequence objects.

count

Count occurrences of a subsequence in this sequence.

definites

Find positions containing definite characters in the sequence.

degap

Return a new sequence with gap characters removed.

degenerates

Find positions containing degenerate characters in the sequence.

distance

Compute the distance to another sequence.

expand_degenerates

Yield all possible definite versions of the sequence.

find_with_regex

Generate slices for patterns matched by a regular expression.

frequencies

Compute frequencies of characters in the sequence.

gaps

Find positions containing gaps in the biological sequence.

gc_content

Calculate the relative frequency of G's and C's in the sequence.

gc_frequency

Calculate frequency of G's and C's in the sequence.

has_definites

Determine if sequence contains one or more definite characters.

has_degenerates

Determine if sequence contains one or more degenerate characters.

has_gaps

Determine if the sequence contains one or more gap characters.

has_interval_metadata

Determine if the object has interval metadata.

has_metadata

Determine if the object has metadata.

has_nondegenerates

Determine if sequence contains one or more non-degenerate characters.

has_positional_metadata

Determine if the object has positional metadata.

index

Find position where subsequence first occurs in the sequence.

is_reverse_complement

Determine if a sequence is the reverse complement of this sequence.

iter_contiguous

Yield contiguous subsequences based on included.

iter_kmers

Generate k-mers of length k from this sequence.

kmer_frequencies

Return counts of words of length k from this sequence.

lowercase

Return a case-sensitive string representation of the sequence.

match_frequency

Return count of positions that are the same between two sequences.

matches

Find positions that match with another sequence.

mismatch_frequency

Return count of positions that differ between two sequences.

mismatches

Find positions that do not match with another sequence.

nondegenerates

Find positions containing non-degenerate characters in the sequence.

replace

Replace values in this sequence with a different character.

reverse_complement

Return the reverse complement of the nucleotide sequence.

to_definites

Convert degenerate and noncanonical characters to alternative characters.

to_indices

Convert the sequence into indices of characters.

to_regex

Return regular expression object that accounts for degenerate chars.

Special methods (inherited)

__bool__

Return truth value (truthiness) of sequence.

__contains__

Determine if a subsequence is contained in this sequence.

__copy__

Return a shallow copy of this sequence.

__deepcopy__

Return a deep copy of this sequence.

__eq__

Determine if this sequence is equal to another.

__ge__

Return self>=value.

__getitem__

Slice this sequence.

__getstate__

Helper for pickle.

__gt__

Return self>value.

__iter__

Iterate over positions in this sequence.

__le__

Return self<=value.

__len__

Return the number of characters in this sequence.

__lt__

Return self<value.

__ne__

Determine if this sequence is not equal to another.

__reversed__

Iterate over positions in this sequence in reverse order.

__str__

Return sequence characters as a string.

Details

complement_map[source]#

Return mapping of nucleotide characters to their complements.

Returns:
dict

Mapping of each character to its complement.

Notes

Complements cannot be defined for a generic nucleotide sequence because the complement of A is ambiguous. Thanks, nature…

default_gap_char[source]#

Gap character to use when constructing a new gapped sequence.

This character is used when it is necessary to represent gap characters in a new sequence. For example, a majority consensus sequence will use this character to represent gaps.

Returns:
str or None

Default gap character, or None if gaps are not defined.

See also

gap_chars

Notes

When a subclass defines a non-empty gap_chars without defining this property, the first gap character in sorted order will be designated as the default gap character during class creation.

definite_chars[source]#

Characters representing definite states.

Returns:
set

Definite characters.

Notes

This character set is the minimum requirement for creating a subclass of GrammaredSequence.

degenerate_map[source]#

Mapping of degenerate to definite characters.

Returns:
dict of set

Mapping of each degenerate character to the set of definite characters it represents. Default is an empty dictionary.

Notes

Each degenerate character may represent an arbitrary number of definite characters.

gap_chars[source]#

Characters representing gaps in the sequence.

Returns:
set

Characters defined as gaps. Default is an empty set.

wildcard_char[source]#

Character representing any other non-gap character in the alphabet.

Returns:
str of length 1

Wildcard character. Default is None. When set, it must be a definite or degenerate character in the alphabet.