|
1 | 1 | from pymol import cmd
|
2 | 2 | import re
|
| 3 | +from collections import defaultdict |
3 | 4 |
|
4 | 5 | ONE_LETTER = {
|
5 | 6 | '00C': 'C', '01W': 'X', '0A0': 'D', '0A1': 'Y', '0A2': 'K',
|
@@ -332,43 +333,32 @@ def findseq(needle, haystack='*', selName=None, het=0, firstOnly=0):
|
332 | 333 | return None
|
333 | 334 |
|
334 | 335 | # remove hetero atoms (waters/ligands/etc) from consideration?
|
335 |
| - if het: |
| 336 | + if bool(int(het)): |
336 | 337 | cmd.select("__h", "br. " + haystack)
|
337 | 338 | else:
|
338 | 339 | cmd.select("__h", "br. " + haystack + " and not het")
|
339 | 340 |
|
340 | 341 | # 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]) |
349 | 348 |
|
350 | 349 | reNeedle = re.compile(needle.upper())
|
351 |
| - |
352 | 350 | # make an empty selection to which we add residues
|
353 | 351 | cmd.select(rSelName, 'None')
|
354 | 352 |
|
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 |
359 | 355 |
|
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]) |
370 | 361 |
|
371 |
| - resi = '+'.join(IDs[i] for i in range(start, stop)) |
372 | 362 | sel = f'__h and %{obj} and resi {resi}'
|
373 | 363 |
|
374 | 364 | if chain:
|
|
0 commit comments