[Biopython-dev] [Bug 2822] Bio.Application.AbstractCommandline - properties and kwargs

bugzilla-daemon at portal.open-bio.org bugzilla-daemon at portal.open-bio.org
Wed Apr 29 10:35:41 EDT 2009


http://bugzilla.open-bio.org/show_bug.cgi?id=2822


biopython-bugzilla at maubp.freeserve.co.uk changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
Attachment #1287 is|0                           |1
           obsolete|                            |




------- Comment #3 from biopython-bugzilla at maubp.freeserve.co.uk  2009-04-29 10:35 EST -------
Created an attachment (id=1288)
 --> (http://bugzilla.open-bio.org/attachment.cgi?id=1288&action=view)
Adds keyword argument support to the __init__ method AND properties

(In reply to comment #2)
> OK, for the moment I'm going to give up on the property idea.  I was trying to
> add them dynamically in __init__ or __new__ based on the parameter list, but
> this is actually rather tricky.  I still think it should be possible though...

I was close earlier, and think I have solved it now :)

As before, this patch adds keyword argument support to the __init__ method, but
also setups properties dynamically.  Again, for the purposes of demonstration
in this patch I have only updated the EMBOSS wrappers to use this.

So, my original example (using the current code) was:

>>> from Bio.Emboss.Applications import WaterCommandline
>>> water_exe = r"C:\Progra~1\Emboss\water.exe"
>>> cline = WaterCommandline(cmd=water_exe)
>>> cline.set_parameter("asequence", "asis:ACCCGGGCGCGGT")
>>> cline.set_parameter("bsequence", "asis:ACCCGAGCGCGGT")
>>> cline.set_parameter("gapopen", "10")
>>> cline.set_parameter("gapextend", "0.5")
>>> cline.set_parameter("outfile", "temp_test.water")
>>> print cline
C:\Progra~1\Emboss\water.exe -asequence=asis:ACCCGGGCGCGGT
-bsequence=asis:ACCCGAGCGCGGT -gapopen=10 -gapextend=0.5
-outfile=temp_test.water

With the __init__ keyword argument support, this becomes valid:

>>> from Bio.Emboss.Applications import WaterCommandline
>>> water_exe = r"C:\Progra~1\Emboss\water.exe"
>>> cline = WaterCommandline(cmd=water_exe, asequence="asis:ACCCGGGCGCGGT", bsequence="asis:ACCCGAGCGCGGT", gapopen="10", gapextend="0.5", outfile="temp_test.water")
>>> print cline
C:\Progra~1\Emboss\water.exe -asequence=asis:ACCCGGGCGCGGT
-bsequence=asis:ACCCGAGCGCGGT -gapopen=10 -gapextend=0.5
-outfile=temp_test.water

You can of course still use the set_parameter approach as well, for example to
change a setting:

>>> cline.set_parameter("gapopen", "20")
>>> print cline
C:\Progra~1\Emboss\water.exe -asequence=asis:ACCCGGGCGCGGT
-bsequence=asis:ACCCGAGCGCGGT -gapopen=20 -gapextend=0.5
-outfile=temp_test.water 

With the property support, you can then read/or set parameter values directly:

>>> cline.gapopen
'20'
>>> cline.gapopen = 15
>>> cline.gapopen
15
>>> print cline
C:\Progra~1\Emboss\water.exe -asequence=asis:ACCCGGGCGCGGT
-bsequence=asis:ACCCGAGCGCGGT -gapopen=15 -gapextend=0.5
-outfile=temp_test.water

This is much nicer I think, but perhaps the biggest plus point is the
properties have docstrings which show via:

>>> help(cline)
...

and are discoverable:

>>> dir(cline)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__str__', '__weakref__', '_check_value',
'_get_parameter', 'aformat', 'asequence', 'bsequence', 'datafile', 'gapextend',
'gapopen', 'outfile', 'parameters', 'program_name', 'set_parameter',
'similarity', 'snucleotide', 'sprotein']

This makes the parameters all readily discoverable, without having to resort to
looking at Biopython's source code, or the command line application's help.

Right now (using the old code in CVS), the information is there but buried:

>>> print cline.parameters
[<Bio.Application._Option instance at 0xb8c990>, <Bio.Application._Option
instance at 0xb8f3a0>, <Bio.Application._Option instance at 0xb8fc60>,
<Bio.Application._Option instance at 0xb8f648>, <Bio.Application._Option
instance at 0xb8fc38>, <Bio.Application._Option instance at 0xb8f058>,
<Bio.Application._Option instance at 0xb8f2d8>, <Bio.Application._Option
instance at 0xb8f3f0>, <Bio.Application._Option instance at 0xb8fbc0>,
<Bio.Application._Option instance at 0xb8fbe8>]
>>> for p in cline.parameters :
...     print p.names, p.description
... 
['-asequence', 'asequence'] First sequence to align
['-bsequence', 'bsequence'] Second sequence to align
['-gapopen', 'gapopen'] Gap open penalty
['-gapextend', 'gapextend'] Gap extension penalty
['-outfile', 'outfile'] Output file for the alignment
['-datafile', 'datafile'] Matrix file
['-similarity', 'similarity'] Display percent identity and similarity
['-snucleotide', 'snucleotide'] Sequences are nucleotide (boolean)
['-sprotein', 'sprotein'] Sequences are protein (boolean)
['-aformat', 'aformat'] Display output in a different specified output format

So, comments?  We can choose to add EITHER the __init__ keyword arguments OR
the properties.  Or of course, BOTH.  Or neither, and just leave the interface
as it stand in CVS now.

Peter


-- 
Configure bugmail: http://bugzilla.open-bio.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.


More information about the Biopython-dev mailing list