Skip to content

Commit 96c7c56

Browse files
jrom99pslacerda
authored andcommitted
Fix support for multiple objects selection
1 parent 7b56fe2 commit 96c7c56

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

findseq.py

+15-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from pymol import cmd
22
import re
3+
from collections import defaultdict
34

45
ONE_LETTER = {
56
'00C': 'C', '01W': 'X', '0A0': 'D', '0A1': 'Y', '0A2': 'K',
@@ -332,43 +333,32 @@ def findseq(needle, haystack='*', selName=None, het=0, firstOnly=0):
332333
return None
333334

334335
# remove hetero atoms (waters/ligands/etc) from consideration?
335-
if het:
336+
if bool(int(het)):
336337
cmd.select("__h", "br. " + haystack)
337338
else:
338339
cmd.select("__h", "br. " + haystack + " and not het")
339340

340341
# get the AAs in the haystack
341-
aaDict = {'aaList': []}
342-
cmd.iterate("(name ca) and __h", "aaList.append((resi,resn,chain,segi))", space=aaDict)
343-
344-
IDs = [x[0] for x in aaDict['aaList']]
345-
AAs = ''.join([ONE_LETTER[x[1]] for x in aaDict['aaList']])
346-
chains = [x[2] for x in aaDict['aaList']]
347-
segis = [x[3] for x in aaDict['aaList']]
348-
objs = cmd.get_object_list('__h')
342+
IDs = defaultdict(list)
343+
AAs = defaultdict(list)
344+
for obj in cmd.get_object_list():
345+
for atom in cmd.get_model(f"%{obj} and (name ca) and __h").atom:
346+
IDs[(obj, atom.segi, atom.chain)].append(atom.resi)
347+
AAs[(obj, atom.segi, atom.chain)].append(ONE_LETTER[atom.resn])
349348

350349
reNeedle = re.compile(needle.upper())
351-
352350
# make an empty selection to which we add residues
353351
cmd.select(rSelName, 'None')
354352

355-
for obj in objs:
356-
it = reNeedle.finditer(AAs)
357-
for i in it:
358-
(start, stop) = i.span()
353+
for key in AAs:
354+
obj, segi, chain = key
359355

360-
# we found some residues, which segi/chains are they from?
361-
i_chains = chains[start:stop]
362-
i_segis = segis[start:stop]
363-
# are all residues from one chain?
364-
if len(set(i_chains)) != 1 or len(set(i_segis)) != 1:
365-
# now they are not, this match is not really a match, skip it
366-
continue
367-
368-
chain = i_chains[0]
369-
segi = i_segis[0]
356+
chain_sequence = "".join(AAs[key])
357+
it = reNeedle.finditer(chain_sequence)
358+
for i in it:
359+
start, stop = i.span()
360+
resi = "+".join(IDs[key][start:stop])
370361

371-
resi = '+'.join(IDs[i] for i in range(start, stop))
372362
sel = f'__h and %{obj} and resi {resi}'
373363

374364
if chain:

0 commit comments

Comments
 (0)