Package Bio :: Module Seq :: Class MutableSeq
[hide private]
[frames] | no frames]

Class MutableSeq

source code

object --+
         |
        MutableSeq

An editable sequence object (with an alphabet).

Unlike normal python strings and our basic sequence object (the Seq class) which are immuatable, the MutableSeq lets you edit the sequence in place. However, this means you cannot use a MutableSeq object as a dictionary key.

>>> from Bio.Seq import MutableSeq
>>> from Bio.Alphabet import generic_dna
>>> my_seq = MutableSeq("ACTCGTCGTCG", generic_dna)
>>> my_seq
MutableSeq('ACTCGTCGTCG', DNAAlphabet())
>>> my_seq[5]
'T'
>>> my_seq[5] = "A"
>>> my_seq
MutableSeq('ACTCGACGTCG', DNAAlphabet())
>>> my_seq[5]
'A'
>>> my_seq[5:8] = "NNN"
>>> my_seq
MutableSeq('ACTCGNNNTCG', DNAAlphabet())
>>> len(my_seq)
11

Note that the MutableSeq object does not support as many string-like or biological methods as the Seq object.

Instance Methods [hide private]
 
__init__(self, data, alphabet=Alphabet())
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
__repr__(self)
Returns a (truncated) representation of the sequence for debugging.
source code
 
__str__(self)
Returns the full sequence as a python string.
source code
 
__eq__(self, other)
Compare the sequence to another sequence or a string (README).
source code
 
__ne__(self, other)
Not equal, see __eq__ documentation.
source code
 
__lt__(self, other)
Less than, see __eq__ documentation.
source code
 
__le__(self, other)
Less than or equal, see __eq__ documentation.
source code
 
__len__(self) source code
 
__getitem__(self, index) source code
 
__setitem__(self, index, value) source code
 
__delitem__(self, index) source code
 
__add__(self, other)
Add another sequence or string to this sequence.
source code
 
__radd__(self, other) source code
 
append(self, c) source code
 
insert(self, i, c) source code
 
pop(self, i=-1) source code
 
remove(self, item) source code
 
count(self, sub, start=0, end=9223372036854775807)
Non-overlapping count method, like that of a python string.
source code
 
index(self, item) source code
 
reverse(self)
Modify the mutable sequence to reverse itself.
source code
 
complement(self)
Modify the mutable sequence to take on its complement.
source code
 
reverse_complement(self)
Modify the mutable sequence to take on its reverse complement.
source code
 
extend(self, other) source code
 
tostring(self)
Returns the full sequence as a python string (DEPRECATED).
source code
 
toseq(self)
Returns the full sequence as a new immutable Seq object.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, data, alphabet=Alphabet())
(Constructor)

source code 
x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Overrides: object.__init__
(inherited documentation)

__repr__(self)
(Representation operator)

source code 
Returns a (truncated) representation of the sequence for debugging.
Overrides: object.__repr__

__str__(self)
(Informal representation operator)

source code 

Returns the full sequence as a python string.

Note that Biopython 1.44 and earlier would give a truncated version of repr(my_seq) for str(my_seq). If you are writing code which needs to be backwards compatible with old Biopython, you should continue to use my_seq.tostring() rather than str(my_seq).

Overrides: object.__str__

__eq__(self, other)
(Equality operator)

source code 

Compare the sequence to another sequence or a string (README).

Currently if compared to another sequence the alphabets must be compatible. Comparing DNA to RNA, or Nucleotide to Protein will raise an exception. Otherwise only the sequence itself is compared, not the precise alphabet.

A future release of Biopython will change this (and the Seq object etc) to use simple string comparison. The plan is that comparing sequences with incompatible alphabets (e.g. DNA to RNA) will trigger a warning but not an exception.

During this transition period, please just do explicit comparisons:

>>> seq1 = MutableSeq("ACGT")
>>> seq2 = MutableSeq("ACGT")
>>> id(seq1) == id(seq2)
False
>>> str(seq1) == str(seq2)
True

Biopython now does:

>>> seq1 == seq2
True
>>> seq1 == Seq("ACGT")
True
>>> seq1 == "ACGT"
True

__add__(self, other)
(Addition operator)

source code 

Add another sequence or string to this sequence.

Returns a new MutableSeq object.

count(self, sub, start=0, end=9223372036854775807)

source code 

Non-overlapping count method, like that of a python string.

This behaves like the python string method of the same name, which does a non-overlapping count!

Returns an integer, the number of occurrences of substring argument sub in the (sub)sequence given by [start:end]. Optional arguments start and end are interpreted as in slice notation.

Arguments:
  • sub - a string or another Seq object to look for
  • start - optional integer, slice start
  • end - optional integer, slice end

e.g.

>>> from Bio.Seq import MutableSeq
>>> my_mseq = MutableSeq("AAAATGA")
>>> print(my_mseq.count("A"))
5
>>> print(my_mseq.count("ATG"))
1
>>> print(my_mseq.count(Seq("AT")))
1
>>> print(my_mseq.count("AT", 2, -1))
1

HOWEVER, please note because that python strings, Seq objects and MutableSeq objects do a non-overlapping search, this may not give the answer you expect:

>>> "AAAA".count("AA")
2
>>> print(MutableSeq("AAAA").count("AA"))
2

An overlapping search would give the answer as three!

reverse(self)

source code 

Modify the mutable sequence to reverse itself.

No return value.

complement(self)

source code 

Modify the mutable sequence to take on its complement.

Trying to complement a protein sequence raises an exception.

No return value.

reverse_complement(self)

source code 

Modify the mutable sequence to take on its reverse complement.

Trying to reverse complement a protein sequence raises an exception.

No return value.

tostring(self)

source code 

Returns the full sequence as a python string (DEPRECATED).

You are now encouraged to use str(my_seq) instead of my_seq.tostring() as this method is officially deprecated.

Because str(my_seq) will give you the full sequence as a python string, there is often no need to make an explicit conversion. For example,

print("ID={%s}, sequence={%s}" % (my_name, my_seq))

On Biopython 1.44 or older you would have to have done this:

print("ID={%s}, sequence={%s}" % (my_name, my_seq.tostring()))

toseq(self)

source code 

Returns the full sequence as a new immutable Seq object.

>>> from Bio.Seq import Seq
>>> from Bio.Alphabet import IUPAC
>>> my_mseq = MutableSeq("MKQHKAMIVALIVICITAVVAAL",
...                      IUPAC.protein)
>>> my_mseq
MutableSeq('MKQHKAMIVALIVICITAVVAAL', IUPACProtein())
>>> my_mseq.toseq()
Seq('MKQHKAMIVALIVICITAVVAAL', IUPACProtein())

Note that the alphabet is preserved.