-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnp.py
executable file
·221 lines (173 loc) · 6.46 KB
/
snp.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
'''
Show UniProt-SNPs within PyMOL
Show dbSNP-SNPs within PyMOL
Commands: snp_uniprot, snp_ncbi
(c) 2010 Thomas Holder
License: BSD-2-Clause
'''
from __future__ import print_function
from pymol import cmd, CmdException
# Email for Entrez connections
emailaddress = "pymol@psico.snp"
def snp_common(record, selection, label, name, quiet):
'''
Common part of snp_uniprot and snp_ncbi.
Argument `record' must be a Bio.SwissProt.Record object with `sequence',
`entry_name' and `features' fields defined.
'''
from . import one_letter
from .seqalign import needle_alignment, alignment_mapping
label = int(label)
quiet = int(quiet)
pdbids = cmd.get_object_list(selection)
chains = cmd.get_chains(selection)
if len(pdbids) != 1:
print('please select one object')
return
snpi = set()
snpi_str = []
labels = dict()
for chain in chains:
print('chain ' + chain)
res_list = []
cmd.iterate('(%s) and chain %s and name CA' % (selection, chain),
'res_list.append((resn,resv))', space=locals())
seq = ''.join([one_letter.get(res[0], 'X') for res in res_list])
align = needle_alignment(record.sequence, seq)
if not quiet:
align._records[0].id = record.entry_name
align._records[1].id = pdbids[0] + '_' + chain
print(align.format('clustal'))
map1 = dict(alignment_mapping(*align))
for feature in record.features:
if feature[0] != 'VARIANT' or feature[1] != feature[2]:
continue
i = feature[1]
if (i-1) not in map1:
if not quiet:
print('not mapped', feature)
continue
resi = res_list[map1[i-1]][1]
snpi.add(resi)
if not quiet:
print('%s`%d' % res_list[map1[i-1]], feature[2:4])
if label:
labels.setdefault((chain, resi), []).append(feature[3].split(' (')[0])
if len(snpi) > 0:
snpi_str.append('(chain %s and resi %s)' % (chain, '+'.join(map(str, snpi))))
for chain, resi in labels:
lab = ', '.join(labels[(chain, resi)])
cmd.label('(%s) and chain %s and resi %d and name CA' % (selection, chain, resi), repr(lab))
if len(snpi_str) == 0:
print('no missense variants')
return
if name == '':
name = cmd.get_unused_name('nsSNPs')
cmd.select(name, '(%s) and (%s)' % (selection, ' or '.join(snpi_str)))
def snp_uniprot(uniprotname, selection='(all)', label=1, name='', quiet=0):
'''
DESCRIPTION
Selects all UniProt annotated nsSNPs (natural variants) in given
structure. Does a sequence alignment of UniProt sequence and PDB
sequence.
USAGE
snp_uniprot uniprotname [, selection [, label [, name [, quiet]]]]
ARGUMENTS
uniprotname = string: UniProt reference (like HBB_HUMAN or P68871)
selection = string: atom selection
label = 0 or 1: Label CA atoms of nsSNPs with mutation {default: 1}
name = string: name of new selection {default: nsSNPs}
EXAMPLE
fetch 3HBT
snp_uniprot ACTG_HUMAN, chain A
SEE ALSO
snp_ncbi
'''
from Bio import ExPASy
from Bio import SwissProt
handle = ExPASy.get_sprot_raw(uniprotname)
record = SwissProt.read(handle)
snp_common(record, selection, label, name, quiet)
def snp_ncbi(query, selection='(all)', label=1, name='', quiet=0):
'''
DESCRIPTION
Selects all nsSNPs from NCBI (dbSNP) in given structure. Does a sequence
alignment of reference protein sequence and PDB sequence.
USAGE
snp_ncbi query [, selection [, label [, name [, quiet]]]]
ARGUMENTS
query = string: Entrez query, for example a protein accession
... see snp_uniprot
EXAMPLE
fetch 3HBT
snp_ncbi NP_001092.1[accn], chain A
SEE ALSO
snp_uniprot
'''
from Bio import Entrez, SeqIO
from Bio.SwissProt import Record
from lxml import etree
Entrez.email = emailaddress
ns = {'ds': 'http://www.ncbi.nlm.nih.gov/SNP/docsum'}
features = set()
# get first protein that matches query
handle = Entrez.esearch(db="protein", term='(%s) AND srcdb_refseq[PROP]' % (query), retmax=1)
record = Entrez.read(handle)
if int(record['Count']) == 0:
print('no such protein')
return
id = record['IdList'][0]
handle = Entrez.efetch(db="protein", id=id, rettype="gb", retmode="text")
seq = SeqIO.read(handle, 'gb')
print('Protein match: %s (%s)' % (seq.id, seq.description))
accn = seq.id
try:
protein_acc, protein_ver = accn.split('.')
except:
print('no refseq accession found')
return
# get snp-list for protein
handle = Entrez.esearch(db="snp", term="%s[accn]" % (accn), retmax=100)
record = Entrez.read(handle)
print('Number of SNP records: ' + record['Count'])
if int(record['Count']) > int(record['RetMax']):
print('Warning: Maximum number of records exceeded (%s)' % (record['RetMax']))
idlist = ','.join(record['IdList'])
# xml path to fxnSet nodes
addr = 'ds:Assembly[@reference="true"]//ds:FxnSet[@protAcc="%s" and @protVer="%s" and @fxnClass="%%s"]' % \
(protein_acc, protein_ver)
# download SNPs
handle = Entrez.efetch(db="snp", id=idlist, rettype="xml", mode="xml")
document = etree.parse(handle)
root = document.getroot()
for rs in root.xpath('/ds:ExchangeSet/ds:Rs', namespaces=ns):
rsId = rs.get('rsId')
# reference allele
try:
node = rs.xpath(addr % ('reference'), namespaces=ns)[0]
refRes = node.get('residue')
refPos = node.get('aaPosition')
intPos = int(refPos) + 1
except:
print('no ref')
continue
# snp alleles
for node in rs.xpath(addr % ('missense'), namespaces=ns):
pos = node.get('aaPosition')
assert pos == refPos
features.add(('VARIANT', intPos, intPos, '%s -> %s (in dbSNP:rs%s)' % \
(refRes, node.get('residue'), rsId)))
# make SwissProt like record
record = Record()
record.entry_name = seq.id
record.sequence = str(seq.seq)
record.features = sorted(features)
snp_common(record, selection, label, name, quiet)
cmd.extend('snp_uniprot', snp_uniprot)
cmd.extend('snp_ncbi', snp_ncbi)
# tab-completion of arguments
cmd.auto_arg[1].update({
'snp_uniprot' : cmd.auto_arg[0]['zoom'],
'snp_ncbi' : cmd.auto_arg[0]['zoom'],
})
# vi:expandtab