biopython v1.71.0 Bio.Seq.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.
Link to this section Summary
Functions
Add another sequence or string to this sequence
Delete a subsequence of single letter
Compare the sequence to another sequence or a string (README)
Return a subsequence of single letter, use my_seq[index]
Initialize the class
Implement the less-than or equal operand
Return the length of the sequence, use len(my_seq)
Implement the less-than operand
Implement the not-equal operand
Add a sequence on the left
Return (truncated) representation of the sequence for debugging
Set a subsequence of single letter via value parameter
Return the full sequence as a python string
Add a subsequence to the mutable sequence object
Modify the mutable sequence to take on its complement
Return a non-overlapping count, like that of a python string
Return an overlapping count
Add a sequence to the original mutable sequence object
Return the position of a subsequence of the first occurrence of a single letter
Add a subsequence to the mutable sequence object at a given index
Remove a subsequence of a single letter at given index
Remove a subsequence of a single letter from mutable sequence
Modify the mutable sequence to reverse itself
Modify the mutable sequence to take on its reverse complement
Return the full sequence as a new immutable Seq object
Return the full sequence as a python string (DEPRECATED)
Link to this section Functions
Add another sequence or string to this sequence.
Returns a new MutableSeq object.
Delete a subsequence of single letter.
>>> my_seq = MutableSeq('ACTCGACGTCG')
>>> del my_seq[0]
>>> my_seq
MutableSeq('CTCGACGTCG', Alphabet())
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
Return a subsequence of single letter, use my_seq[index].
>>> my_seq = MutableSeq('ACTCGACGTCG')
>>> my_seq[5]
'A'
Initialize the class.
Implement the less-than or equal operand.
Return the length of the sequence, use len(my_seq).
Implement the less-than operand.
Implement the not-equal operand.
Add a sequence on the left.
>>> from Bio.Seq import MutableSeq
>>> from Bio.Alphabet import generic_protein
>>> "LV" + MutableSeq("MELKI", generic_protein)
MutableSeq('LVMELKI', ProteinAlphabet())
Return (truncated) representation of the sequence for debugging.
Set a subsequence of single letter via value parameter.
>>> my_seq = MutableSeq('ACTCGACGTCG')
>>> my_seq[0] = 'T'
>>> my_seq
MutableSeq('TCTCGACGTCG', Alphabet())
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).
Add a subsequence to the mutable sequence object.
>>> my_seq = MutableSeq('ACTCGACGTCG')
>>> my_seq.append('A')
>>> my_seq
MutableSeq('ACTCGACGTCGA', Alphabet())
No return value.
Modify the mutable sequence to take on its complement.
Trying to complement a protein sequence raises an exception.
No return value.
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!
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.
Add a sequence to the original mutable sequence object.
>>> my_seq = MutableSeq('ACTCGACGTCG')
>>> my_seq.extend('A')
>>> my_seq
MutableSeq('ACTCGACGTCGA', Alphabet())
>>> my_seq.extend('TTT')
>>> my_seq
MutableSeq('ACTCGACGTCGATTT', Alphabet())
No return value.
Return the position of a subsequence of the first occurrence of a single letter.
>>> my_seq = MutableSeq('ACTCGACGTCG')
>>> my_seq.index('A')
0
>>> my_seq.index('T')
2
Add a subsequence to the mutable sequence object at a given index.
>>> my_seq = MutableSeq('ACTCGACGTCG')
>>> my_seq.insert(0,'A')
>>> my_seq
MutableSeq('AACTCGACGTCG', Alphabet())
>>> my_seq.insert(8,'G')
>>> my_seq
MutableSeq('AACTCGACGGTCG', Alphabet())
No return value.
Remove a subsequence of a single letter at given index.
>>> my_seq = MutableSeq('ACTCGACGTCG')
>>> my_seq.pop()
'G'
>>> my_seq
MutableSeq('ACTCGACGTC', Alphabet())
>>> my_seq.pop()
'C'
>>> my_seq
MutableSeq('ACTCGACGT', Alphabet())
Returns the last character of the sequence
Remove a subsequence of a single letter from mutable sequence.
>>> my_seq = MutableSeq('ACTCGACGTCG')
>>> my_seq.remove('C')
>>> my_seq
MutableSeq('ATCGACGTCG', Alphabet())
>>> my_seq.remove('A')
>>> my_seq
MutableSeq('TCGACGTCG', Alphabet())
No return value.
Modify the mutable sequence to reverse itself.
No return value.
Modify the mutable sequence to take on its reverse complement.
Trying to reverse complement a protein sequence raises an exception.
No return value.
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.
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}