Repository navigation
Expand file tree
/
Copy pathseqalign.py
More file actions
executable file
·192 lines (155 loc) · 5.28 KB
/
Copy pathseqalign.py
File metadata and controls
executable file
·192 lines (155 loc) · 5.28 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'''
API only sequence alignment stuff.
Only load on demand to prevent unnecessary "Bio" import (biopython).
(c) 2011 Thomas Holder, MPI for Developmental Biology
License: BSD-2-Clause
'''
from Bio.SeqIO import _FormatToIterator
from pymol import cmd, CmdException
def needle_alignment(s1, s2):
'''
DESCRIPTION
Does a Needleman-Wunsch Alignment of sequence s1 and s2 and
returns a Bio.Align.MultipleSeqAlignment object.
'''
from Bio import pairwise2
from Bio.Align import MultipleSeqAlignment
from Bio.SubsMat.MatrixInfo import blosum62
def match_callback(c1, c2):
return blosum62.get((c1, c2), 1 if c1 == c2 else -4)
alns = pairwise2.align.globalcs(s1, s2,
match_callback, -10., -.5,
one_alignment_only=True)
a = MultipleSeqAlignment([])
a.add_sequence("s1", alns[0][0])
a.add_sequence("s2", alns[0][1])
return a
def needle_alignment_emboss(s1, s2):
import subprocess
from Bio.Emboss.Applications import NeedleCommandline
from Bio import AlignIO
cline = NeedleCommandline(auto=True, sprotein=True, stdout=True, gapopen=10, gapextend=1)
cline.asequence = "asis:" + s1
cline.bsequence = "asis:" + s2
process = subprocess.Popen(str(cline), shell=True, stdout=subprocess.PIPE)
return AlignIO.read(process.stdout, "emboss")
def alignment_mapping(seq1, seq2):
'''
DESCRIPTION
Returns an iterator with seq1 indices mapped to seq2 indices
>>> mapping = dict(alignment_mapping(s1, s2))
'''
i, j = -1, -1
for a, b in zip(seq1, seq2):
if a != '-': i += 1
if b != '-': j += 1
if a != '-' and b != '-': yield i, j
def aln_magic_format(infile):
'''
DESCRIPTION
Guess alignment file format.
'''
for line in open(infile):
if len(line.rstrip()) > 0:
break
if line.startswith('CLUSTAL') or line.startswith('MUSCLE'):
informat = 'clustal'
elif line.startswith('>P1;'):
informat = 'pir'
elif line.startswith('>'):
informat = 'fasta'
elif line.startswith('# STOCKHOLM'):
informat = 'stockholm'
elif line.startswith('Align '):
informat = 'fatcat'
elif line.startswith('# ProSMART Alignment File'):
informat = 'prosmart'
else:
informat = 'emboss'
return informat
def aln_magic_read(infile, format=None):
'''
DESCRIPTION
Wrapper for Bio.AlignIO.read that guesses alignment file format.
'''
from Bio import AlignIO
if not format:
format = aln_magic_format(infile)
return AlignIO.read(open(infile), format)
def FatCatIterator(handle):
'''
DESCRIPTION
Biopython FatCat alignment file support
'''
from Bio.Alphabet import single_letter_alphabet
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
try:
line = handle.readline()
a = line.split()
ids = a[1], a[4]
ids = [(id[:-4] if id.endswith('.pdb') else id) for id in ids]
except ImportError:
ids = 'chain1', 'chain2'
seqs = [[], []]
for line in handle:
if line.startswith('Chain 1:'): seqs[0].append(line[14:].rstrip())
if line.startswith('Chain 2:'): seqs[1].append(line[14:].rstrip())
for seq, id in zip(seqs, ids):
yield SeqRecord(Seq(''.join(seq), single_letter_alphabet), id=id, name=id)
def ProSMARTIterator(handle):
'''
DESCRIPTION
Biopython ProSMART alignment file support
Warning: ProSMART alignment files do not contain gaps, so they only
contain the residues which are actually aligned!
'''
from Bio.Alphabet import single_letter_alphabet
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from . import one_letter
try:
code1, chain1, code2, chain2 = handle.name.rsplit('.', 1)[0].split('_')
ids = [code1 + '_' + chain1, code2 + '_' + chain2]
except:
ids = 'chain1', 'chain2'
seqs = [[], []]
for line in handle:
if line.startswith('#') or line.startswith('Res1'):
continue
a = line.split('\t')
try:
aa1 = one_letter[a[2]]
aa2 = one_letter[a[3]]
except:
print(' Warning: Exception while parsing ProSMART alignment')
continue
seqs[0].append(aa1)
seqs[1].append(aa2)
for seq, id in zip(seqs, ids):
yield SeqRecord(Seq(''.join(seq), single_letter_alphabet), id=id, name=id)
def POAIterator(handle):
'''
DESCRIPTION
Biopython POA (POSA) alignment file support
Warning: POA alignment files do not contain gaps, so they only
contain the residues which are actually aligned!
'''
import re
from Bio.Alphabet import single_letter_alphabet
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
pattern = re.compile(r'\{ *\d+\}')
for line in handle:
if line.startswith('#'):
continue
id, seq = line.split(None, 1)
seq = pattern.sub('-', seq.rstrip())
yield SeqRecord(Seq(''.join(seq), single_letter_alphabet), id=id, name=id)
if 'fatcat' not in _FormatToIterator:
_FormatToIterator['fatcat'] = FatCatIterator
if 'prosmart' not in _FormatToIterator:
_FormatToIterator['prosmart'] = ProSMARTIterator
if 'poa' not in _FormatToIterator:
_FormatToIterator['poa'] = POAIterator
# vi:expandtab:smarttab