-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence.py
More file actions
72 lines (50 loc) · 1.88 KB
/
sequence.py
File metadata and controls
72 lines (50 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from textwrap import fill
class Sequence:
def __init__(self, info):
"""Amino-acid sequence abstract data type.
:param info: tuple containing description and aa-chain.
:returns: a new Sequence object
:rtype: Sequence
The param info is supposed to be in a structured manner
(obtained from the sequence parser). The description must be
in the first position and the aa-chain in the second.
"""
self.description = info[0]
self.amino_acid_chain = info[1]
@classmethod
def fromstring(cls, string):
"""This 'constructor' overload allows creating a sequence from a
simple string. The information does not need to be in a
FASTA-like format
:param cls: our class
:param string:
:returns: a new object created with the default constructor
:rtype: Sequence
"""
info = ["No description", string]
return cls(info)
def __getitem__(self, position):
"""Hook method called when using the "[]" operator to get a value at a
certain position in the string
:param position: string position
:returns: the residue letter
:rtype: string
"""
return self.amino_acid_chain[position]
def __len__(self):
"""Hook method. len() on objects with Sequence type will call this.
:returns: length of the aa-chain
:rtype: int
"""
return len(self.amino_acid_chain)
def __str__(self):
"""FASTA format representation of the sequence.
It wraps the amino acid chain to 80 characters, which is the
standard for the FASTA format. The description is left
unwrapped.
:returns:
:rtype: string
"""
aa = fill(self.amino_acid_chain + "\n", 80)
string = self.description + "\n" + aa
return string