Search code examples
pythonpymol

Are there any solution for this <lambda>() missing 1 required positional argument: 'y'


I am doing some tutorial on protein modeling by using multiple templates to model the same protein. I am currently trying to run the align_all.py however the output turn like this

rmsd_list.sort(key=lambda x,y: cmp(x[1],y[1]))

TypeError: () missing 1 required positional argument: 'y'

I have try many way to correct it but still did not get the solution. I hope somebody can help me on solving this. I am currently using Python 2.7.

Below is the full codes

from pymol import cmd

def align_all(target=None,mobile_selection='name ca',target_selection='name ca',cutoff=2, cycles=5,cgo_object=0,method='align'):

cutoff = int(cutoff)
  cycles = int(cycles)
  cgo_object = int(cgo_object)

  object_list = cmd.get_names()
  object_list.remove(target)

  rmsd = {}
  rmsd_list = []
  for i in range(len(object_list)):
    if cgo_object:
      objectname = 'align_%s_on_%s' % (object_list[i],target)
      if method == 'align':
        rms = cmd.align('%s & %s'%(object_list[i],mobile_selection),'%s & %s'%(target,target_selection),cutoff=cutoff,cycles=cycles,object=objectname)
      elif method == 'super':
        rms = cmd.super('%s & %s'%(object_list[i],mobile_selection),'%s & %s'%(target,target_selection),cutoff=cutoff,cycles=cycles,object=objectname)
      elif method == 'cealign':
        rmsdict = cmd.cealign('%s & %s' % (target,target_selection),'%s & %s' % (object_list[i],mobile_selection))
        rms = [rmsdict['RMSD'],rmsdict['alignment_length'],1,0,0]
      else:
        print ("only 'align', 'super' and 'cealign' are accepted as methods")
        sys.exit(-1)
    else:
      if method == 'align':
        rms = cmd.align('%s & %s'%(object_list[i],mobile_selection),'%s & %s'%(target,target_selection),cutoff=cutoff,cycles=cycles)
      elif method == 'super':
        rms = cmd.super('%s & %s'%(object_list[i],mobile_selection),'%s & %s'%(target,target_selection),cutoff=cutoff,cycles=cycles)
      elif method == 'cealign':
        rmsdict = cmd.cealign('%s & %s' % (target,target_selection),'%s & %s' % (object_list[i],mobile_selection))
        rms = [rmsdict['RMSD'],rmsdict['alignment_length'],1,0,0]
      else:
        print ("only 'align', 'super' and 'cealign' are accepted as methods")
        sys.exit(-1)

    rmsd[object_list[i]] = (rms[0],rms[1])
    rmsd_list.append((object_list[i],rms[0],rms[1]))


  rmsd_list.sort(key=lambda x,y: cmp(x[1],y[1]))
# loop over dictionary and print out matrix of final rms values
  print ("Aligning against:",target)
  for object_name in object_list:
    print ("%s: %6.3f using %d atoms" % (object_name,rmsd[object_name][0],rmsd[object_name][1]))

  print ("\nSorted from best match to worst:")
  for r in rmsd_list:
    print ("%s: %6.3f using %d atoms" % r)

cmd.extend('align_all',align_all)

Thank you.


Solution

  • The sort function key keyword is passed the object and is expected to return the comparison key for that object. Older versions of python sort used a cmp keyword which compared two elements to be sorted.

    You want rmsd_list.sort(key=lambda e: e[1]) I suspect.