@@ -333,46 +333,53 @@ def findseq(needle, haystack='*', selName=None, het=0, firstOnly=0):
333
333
return None
334
334
335
335
# remove hetero atoms (waters/ligands/etc) from consideration?
336
+ hstk = cmd .get_unused_name ()
336
337
if bool (int (het )):
337
- cmd .select ("__h" , "br. " + haystack )
338
+ cmd .select (hstk , f"byres { haystack } " )
338
339
else :
339
- cmd .select ("__h" , "br. " + haystack + " and not het" )
340
+ cmd .select (hstk , f"byres { haystack } and not het" )
340
341
341
342
# get the AAs in the haystack
342
343
IDs = defaultdict (list )
343
344
AAs = defaultdict (list )
344
345
for obj in cmd .get_object_list ():
345
- for atom in cmd .get_model (f"%{ obj } and (name ca) and __h " ).atom :
346
+ for atom in cmd .get_model (f"%{ obj } and (name ca) and { hstk } " ).atom :
346
347
IDs [(obj , atom .segi , atom .chain )].append (atom .resi )
347
348
AAs [(obj , atom .segi , atom .chain )].append (ONE_LETTER [atom .resn ])
348
349
349
350
reNeedle = re .compile (needle .upper ())
350
- # make an empty selection to which we add residues
351
- cmd .select (rSelName , 'None' )
352
351
353
- for key in AAs :
352
+ matches = defaultdict (list )
353
+ for key in sorted (AAs ):
354
354
obj , segi , chain = key
355
355
356
+ if int (firstOnly ) and len (matches [obj ]) >= 1 :
357
+ # ignore other chains
358
+ continue
359
+
356
360
chain_sequence = "" .join (AAs [key ])
357
361
it = reNeedle .finditer (chain_sequence )
358
362
for i in it :
359
363
start , stop = i .span ()
360
364
resi = "+" .join (IDs [key ][start :stop ])
361
365
362
- sel = f'__h and %{ obj } and resi { resi } '
366
+ sel = f'{ hstk } and %{ obj } and resi { resi } '
363
367
364
368
if chain :
365
369
sel += f' and chain { chain } '
366
370
if segi :
367
371
sel += f' and segi { segi } '
368
372
369
- sel = f'{ rSelName } or ({ sel } )'
370
- cmd .select (rSelName , sel )
373
+ matches [obj ].append (sel )
374
+
375
+ # remove multiple matches in the same chain
376
+ if int (firstOnly ):
377
+ for obj , v in matches .items ():
378
+ matches [obj ] = v [:1 ]
371
379
372
- if int ( firstOnly ):
373
- break
380
+ cmd . select ( rSelName , " or " . join ( f"( { s } )" for v in matches . values () for s in v ))
381
+ cmd . delete ( hstk )
374
382
375
- cmd .delete ("__h" )
376
383
cnt = cmd .count_atoms (rSelName )
377
384
if not cnt :
378
385
print ("Sequence was not found" )
0 commit comments