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 immutable, 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())
Initialize the class.
source code
 
__repr__(self)
Return (truncated) representation of the sequence for debugging.
source code
 
__str__(self)
Return 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)
Implement the not-equal operand.
source code
 
__lt__(self, other)
Implement the less-than operand.
source code
 
__le__(self, other)
Implement the less-than or equal operand.
source code
 
__len__(self)
Return the length of the sequence, use len(my_seq).
source code
 
__getitem__(self, index)
Return a subsequence of single letter, use my_seq[index].
source code
 
__setitem__(self, index, value)
Set a subsequence of single letter via value parameter.
source code
 
__delitem__(self, index)
Delete a subsequence of single letter.
source code
 
__add__(self, other)
Add another sequence or string to this sequence.
source code
 
__radd__(self, other)
Add a sequence on the left.
source code
 
append(self, c)
Add a subsequence to the mutable sequence object.
source code
 
insert(self, i, c)
Add a subsequence to the mutable sequence object at a given index.
source code
 
pop(self, i=-1)
Remove a subsequence of a single letter at given index.
source code
 
remove(self, item)
Remove a subsequence of a single letter from mutable sequence.
source code
 
count(self, sub, start=0, end=9223372036854775807)
Return a non-overlapping count, like that of a python string.
source code
 
count_overlap(self, sub, start=0, end=9223372036854775807)
Return an overlapping count.
source code
 
index(self, item)
Return the position of a subsequence of a single letter.
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)
Add a sequence to the original mutable sequence object.
source code
 
tostring(self)
Return the full sequence as a python string (DEPRECATED).
source code
 
toseq(self)
Return 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 
Initialize the class.
Overrides: object.__init__

__repr__(self)
(Representation operator)

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

__str__(self)
(Informal representation operator)

source code 

Return 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 

Return a non-overlapping count, like that of a python string.

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

For an overlapping search use the newer count_overlap() method.

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!

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

source code 

Return an overlapping count.

For a non-overlapping search use the count() method.

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
>>> print(MutableSeq("AAAA").count_overlap("AA"))
3
>>> print(MutableSeq("ATATATATA").count_overlap("ATA"))
4
>>> print(MutableSeq("ATATATATA").count_overlap("ATA", 3, -1))
1

Where substrings do not overlap, should behave the same as the count() method:

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

HOWEVER, do not use this method for such cases because the count() method is much for efficient.

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 

Return 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,

>>> my_seq = Seq("ATCGTG")
>>> my_name = "seq_1"
>>> print("ID={%s}, sequence={%s}" % (my_name, my_seq))
ID={seq_1}, sequence={ATCGTG}

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

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

toseq(self)

source code 

Return 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.