-
Notifications
You must be signed in to change notification settings - Fork 0
/
msa_layout.py
executable file
·91 lines (67 loc) · 2.42 KB
/
msa_layout.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
from ete4 import Tree
from ete4.smartview import TreeLayout, SeqFace, SeqMotifFace, AlignmentFace
TREEFILE = 'data/tree.nw'
MSA = 'data/tree.aln.faa'
t = Tree(open(TREEFILE))
def get_seqs(fastafile):
"""Read a fasta file and return a dict with d[description] = sequence.
Example output: {'Phy003I7ZJ_CHICK': 'TMSQFNFSSAPAGGGFSFSTPKT...', ...}
"""
name2seq = {}
seq = ''
for line in open(fastafile):
if line.startswith('>'):
if seq:
name2seq[head] = seq
seq = ''
head = line.lstrip('>').rstrip()
else:
head = line.lstrip('>').rstrip()
else:
seq += line.rstrip()
name2seq[head] = seq
return name2seq
# get information alignment
name2seq = get_seqs(MSA)
for leaf in t:
leaf.add_prop('seq', name2seq[leaf.name])
def layout_alnface_gray(node):
if node.is_leaf:
seq_face = AlignmentFace(
node.props.get('seq'),
seqtype='aa', gap_format='line', seq_format='[]',
width=800, height=None,
fgcolor='black', bgcolor='#bcc3d0', gapcolor='gray',
gap_linewidth=0.2,
max_fsize=12, ftype='sans-serif',
padding_x=0, padding_y=0)
node.add_face(seq_face, position='aligned')
return
def layout_alnface_compact(node):
if node.is_leaf:
seq_face = AlignmentFace(
node.props.get('seq'),
seqtype='aa', gap_format='line', seq_format='compactseq',
width=800, height=None,
fgcolor='black', bgcolor='#bcc3d0', gapcolor='gray',
gap_linewidth=0.2,
max_fsize=12, ftype='sans-serif',
padding_x=0, padding_y=0)
node.add_face(seq_face, position='aligned')
return
def layout_seqface(node):
if node.is_leaf:
seq_face = SeqFace(
node.props.get('seq'),
seqtype='aa', poswidth=1,
draw_text=True, max_fsize=15, ftype='sans-serif',
padding_x=0, padding_y=0)
node.add_face(seq_face, position='aligned')
return
layouts = [
TreeLayout(name='compact_aln', ns=layout_alnface_compact, aligned_faces=True),
TreeLayout(name='gray_aln', ns=layout_alnface_gray, aligned_faces=True, active=False),
TreeLayout(name='seq', ns=layout_seqface, aligned_faces=True, active=False),
]
t.explore(layouts=layouts, keep_server=True)