seq
– Sequences and Alignments¶
The seq
module helps you working with sequence data of various kinds. It
has classes for single sequences
, lists of
sequences
and alignments
of two or
more sequences.
Attaching Structures to Sequences¶
As OpenStructure is a computational structural biology framework, it is not
surprising that the sequence classes have been designed to work together with
structural data. Each sequence can have an attached EntityView
allowing for fast mapping between residues in the entity view and position in
the sequence.
Sequence Offset¶
When using sequences and structures together, often the start of the structure and the beginning of the sequence do not fall together. In the following case, the alignment of sequences B and C only covers a subsequence of structure A:
A acefghiklmnpqrstuvwy
B ghiklm
C 123-45
We would now like to know which residue in protein A is aligned to which residue in sequence C. This is achieved by setting the sequence offset of sequence C to 4. In essence, the sequence offset influences all the mapping operations from position in the sequence to residue index and vice versa. By default, the sequence offset is 0.
Loading and Saving Sequences and Alignments¶
The io
module supports input and output of common sequence formats.
Single sequences can be loaded from disk with io.LoadSequence()
,
alignments are loaded with io.LoadAlignment()
and lists of sequences are loaded with io.LoadSequenceList()
. In addition to the file based input
methods, sequences can also be loaded from a string:
seq_string='''>sequence
abcdefghiklmnop'''
s=io.SequenceFromString(seq_string, 'fasta')
print s.name, s # will print "sequence abcdefghiklmnop"
Note that, in that case specifying the format is mandatory.
The SequenceHandle¶
-
CreateSequence
(name, sequence)¶ Create a new
SequenceHandle
with the given name and sequence.Parameters: - name (str) – name of the sequence
- sequence (str) – String of characters representing the sequence. Only ‘word’ characters (no digits), ‘?’, ‘-‘ and ‘.’ are allowed. In an upcoming release, ‘?’ and ‘.’ will also be forbidden so its best to translate those to ‘X’ or ‘-‘.
Raises InvalidSequence: When the sequence string contains forbidden characters. In the future, ‘?’ and ‘.’ will also raise this exception.
-
class
SequenceHandle
¶ Represents a sequence. New instances are created with
CreateSequence()
.-
GetPos
(residue_index)¶ Get position of residue with index in sequence. This is best illustrated in the following example:
s=seq.CreateSequence("A", "abc---def") print s.GetPos(1) # prints 1 print s.GetPos(3) # prints 6
The reverse mapping, that is from position in the sequence to residue index can be achieved with
GetResidueIndex()
.
-
GetResidueIndex
(pos)¶ Get residue index of character at given position. This method is the inverse of
GetPos()
. If the sequence contains a gap at that position, anError
is raised. Admires the sequence offset.s=seq.CreateSequence("A", "abc--def") print s.GetResidueIndex(1) # prints 1 print s.GetResidueIndex(6) # prints 4 # the following line raises an exception of type # Error with the message "requested position contains # a gap" print s.GetResidueIndex(3)
-
GetResidue
(pos)¶ As,
GetResidueIndex()
, but directly returns the residue view. If no view is attached, or if the position is a gap, an invalid residue view is returned.Return type: ResidueView
-
GetLastNonGap
()¶ Get position of last non-gap character in sequence. In case of an empty sequence, or, a sequence only consisting of hyphens, -1 is returned
-
GetFirstNonGap
()¶ Get position of first non-gap character in sequence. In case of an empty sequence, or, a sequence only consisting of hyphens, -1 is returned.
-
AttachView
(view)¶ -
AttachView
(view[, chain_name]) Attach an
EntityView
to sequence. The first signature requires that the view contains one chain. If not, anIntegrityError
is raised. The second signature will select the chain with the given name. If no such chain exists, anIntegrityError
is raised.
-
HasAttachedView
()¶ Returns True when the sequence has a view attached, False if not.
-
GetAttachedView
()¶ Returns the attached
EntityView
, or an invalidEntityView
if no view has been attached. Also available as the propertyattached_view
.
-
SetOffset
()¶ Set the sequence offset. By default, the offset is 0. Also available as the property
offset
.
-
GetOffset
()¶ Returns the sequence offset. Also available as
offset
.
-
GetGaplessString
()¶ Returns a string version of this sequence with all hyphens removed. Also available as the property
gapless_string
.
-
Normalise
()¶ Remove ‘-‘ and ‘.’ as gaps from the sequence and make it all upper case. Works in place.
-
gapless_string
¶ Shorthand for
GetGaplessString()
-
attached_view
¶ Shorthand for
GetAttachedView()
.
-
offset
¶ Shorthand for
GetOffset()
/SetOffset()
-
__len__
()¶ Returns the length of the sequence (including insertions and deletions)
-
__str__
()¶ Returns the sequence as a string.
-
-
Match
(s1, s2)¶ Parameters: - s1 (
SequenceHandle
, orstr
) – The first sequence - s2 (
SequenceHandle
, orstr
) – The second sequence
Check whether the two sequences s1 and s2 match. This function performs are case-insensitive comparison of the two sequences. The character ‘X’ is interpreted as a wild card character that always matches the other sequence.
- s1 (
The SequenceList¶
-
class
SequenceList
¶ Represents a list of sequences. The class provides a row-based interface. New instances are created with
CreateSequenceList()
.
The AlignmentHandle¶
The AlignmentHandle
represents a list of aligned sequences. In
contrast to SequenceList
, an alignment requires all sequences to be of
the same length. New instances of alignments are created with
CreateAlignment()
and AlignmentFromSequenceList()
.
Typically sequence alignments are used column-based, i.e by looking at an
aligned columns in the sequence alignment. To get a row-based (sequence) view
on the sequence list, use GetSequenceList()
.
All functions that operate on an alignment will again produce a valid alignment. This mean that it is not possible to change the length of one sequence, without adjusting the other sequences, too.
The following example shows how to iterate over the columns and sequences of an alignment:
aln=io.LoadAlignment('aln.fasta')
# iterate over the columns
for col in aln:
print col
# iterate over the sequences
for s in aln.sequences:
print s
-
CreateAlignment
()¶ Creates and returns a new
AlignmentHandle
with no sequences.
-
AlignmentFromSequenceList
(sequences)¶ Create a new alignment from the given list of sequences
Parameters: sequences ( ConstSequenceList
) – the list of sequencesRaises: InvalidAlignment
if the sequences do not have the same length.
-
class
AlignmentHandle
¶ Note
Several of these methods just forward calls to the sequence. For more detailed information, have a look at the
SequenceHandle
documentation.-
GetSequence
(index)¶ Returns the sequence at the given index, raising an IndexError when trying to access an inexistent sequence.
-
GetSequenceList
()¶ Returns a list of all sequence of the alignment.
-
GetLength
()¶ Returns the length of the alignment.
-
GetCount
()¶ Returns the number of sequences in the alignment.
-
ToString
(width=80)¶ Returns a formatted string version of the alignment. The sequences are split into smaller parts to fit into the number columns specified.
aln=seq.CreateAlignment() aln.AddSequence(seq.CreateSequence("A", "abcdefghik")) aln.AddSequence(seq.CreateSequence("B", "1234567890")) # The following command will print the output given below print aln.ToString(7) # A abcde # B 12345 # # A fghik # B 67890
-
FindSequence
(name)¶ Find sequence with given name. If the alignment contains several sequences with the same name, the first sequence is returned.
-
SetSequenceName
(seq_index, name)¶ Set the name of the sequence at index seq_index to name
-
SetSequenceOffset
(seq_index, offset)¶ Set the sequence offset of sequence at index seq_index
-
Copy
()¶ Create a deep copy of the alignment
-
GetPos
(seq_index, res_index)¶ Get position of residue with index equal to res_index in sequence at index seq_index.
-
GetResidueIndex
(seq_index, pos)¶ Get residue index of residue at position pos in sequence at index seq_index.
-
AttachView
(seq_index, view)¶ -
AttachView
(seq_index, view, chain_name) Attach the given view to the sequence at index seq_index.
-
Cut
(start, end)¶ Removes the columns in the half-closed interval start, end from the alignment.
aln=seq.CreateAlignment() aln.AddSequence(seq.CreateSequence("A", "abcd---hik")) aln.AddSequence(seq.CreateSequence("B", "1234567890")) aln.Cut(4, 7) print aln.ToString(80) # will print # A abcdhik # B 1234890
-
Replace
(new_region, start, end)¶ Replace the columns in the half-closed interval start, end with the columns in new_region.
Parameters: new_region ( AlignedRegion
orAlignmentHandle
) – The region to be inserted
-
ShiftRegion
(start, end, amount, master=-1)¶ Shift columns in the half-closed interval start, end. If amount is a positive number, the columns are shifted to the right, if negative, the columns are shifted to the left.
If master is set to -1, all sequences in the region are affected, otherwise only the sequence at index equal to master is shifted.
-
GetMatchingBackboneViews
(index1=0, index2=1)¶ Returns a tuple of entity views containing matching backbone atoms for the two sequences at index1 and index2, respectively. For each aligned column in the alignment, backbone atoms are added to the view if both aligned residues have them. It is guaranteed that the two views contain the same number of atoms and that the order of the atoms in the two views is the same.
The output of this function can be used to superpose two structures with
SuperposeSVD()
.Parameters: - index1 – The index of the first sequence
- index2 – The index of the second sequence.
Raises: In case one of the two sequences doesn’t have an attached view, a
RuntimeError
is raised.
-
AddSequence
(sequence)¶ Append a sequence to the alignment. The sequence must have the same length as sequences already present in the alignment.
Raises: RuntimeError
if the sequence length does not matchParameters: sequence ( ConstSequenceHandle
) – Sequence to be added
-
GetSequenceOffset
(index)¶ -
SetSequenceOffset
(index, offset) Get/set the offset for sequence at index.
Parameters: - index (
int
) – The index of the sequence - offset (
int
) – The new offset
Return type: int
- index (
-
GetSequenceRole
(index)¶ -
SetSequenceRole
(index, role)¶ Get/Set the sequence role for sequence at index.
Parameters: - index (
int
) – The index of the sequence - role (
str
) – The new role
Return type: str
- index (
-
GetCoverage
(index)¶ Get coverage of sequence at index to the first sequence.
Parameters: index ( int
) – The index of the sequenceReturns: Coverage as a number between 0 and 1.
-
RemoveSequence
(index)¶ Remove sequence at index from the alignment.
-
Handling Sequence Profiles¶
The ProfileHandle
provides a simple container for profiles for each
residue. It mainly contains:
- N
ProfileColumn
objects (N = number of residues in sequence) which each contains 20 amino acid frequencies - a
sequence
(str
) of length N - a
null_model
to use for this profile
-
class
ProfileColumn
¶ -
BLOSUMNullModel
()¶ Static method, that returns a new
ProfileColumn
with amino acid frequencies given from the BLOSUM62 substitution matrix.
-
GetFreq
(aa)¶ Parameters: aa ( str
) – One letter code of standard amino acidReturns: Frequency of aa
-
SetFreq
(aa, freq)¶ Parameters: - aa (
str
) – One letter code of standard amino acid - freq (
float
) – The frequency of the given amino acid
- aa (
-
entropy
¶ Shannon entropy based on the columns amino acid frequencies
-
-
class
ProfileHandle
¶ -
__len__
()¶ Returns the length of the sequence for which we have profile.
Return type: int
-
AddColumn
(col)¶ Appends column in the internal column list.
Parameters: col ( ProfileColumn
) – Column to add
-
Extract
(from, to)¶ Parameters: - from (
int
) – Col Idx to start from - to (
int
) – End Idx, not included in sub-ProfileHandle
Returns: sub-profile as defined by given indices (
null_model
is copied)Return type: Raises: Error
if if to <= from or to >__len__()
.- from (
-
SetNullModel
(null_model)¶ Sets
null_model
.
-
sequence
¶ Sequence for which we have this profile. Note: user must enforce consistency between sequence length and number of profile columns.
-
columns
¶ Iterable columns of the profile
-
null_model
¶ Null model of the profile
-
avg_entropy
¶ Average entropy of all the columns
-
-
class
ProfileDB
¶ A simple database to gather
ProfileHandle
objects. It is possible to save them to disk in a compressed format with limited accuracy (4 digits for each frequency).-
Save
(filename)¶ Parameters: filename ( str
) – Name of file that will be generated on disk.
-
Load
(filename)¶ Static loading method
Parameters: filename ( str
) – Name of file from which the database should be loaded.Returns: The loaded database
-
AddProfile
(name, prof)¶ Parameters: - name (
str
) – Name of profile to be added - prof (
ProfileHandle
) – Profile to be added
Raises: Exception
when filename is longer than 255 characters.- name (
-
GetProfile
(name)¶ Parameters: name ( str
) – Name of profile to be returnedReturns: The requested ProfileHandle
Raises: Exception
when noProfileHandle
for name exists.
-
Size
()¶ Returns: Number of ProfileHandle
objects in the database
-
GetNames
()¶ Returns: A nonsorted list of the names of all ProfileHandle
objects in the database
-