Reputation: 2040
I am using Biopython's Restriction class to perform in silico restriction digestions. As I understand it, in order to digest a certain sequence with a certain enzyme, the .catalyze() method must be implemented.
digestTuple = Restriction.EcoRI.catalyse(this_seq) # Or whichever enzyme is desired.
Right now I apply a conditional to see which enzyme is being used as input. For Example:
RS = restrictionSite # From user
amb = IUPACAmbiguousDNA()
this_seq = Seq(sequence, amb) # sequence from user
if RS == 'EcoRI':
digestTuple = Restriction.EcoRI.catalyse(this_seq)
I apply a conditional for any enzymes that I would foresee myself needing. This takes up a bunch of lines of code and is inefficient. I would like to be able to search for membership in Restrictions set of all possible enzymes, Restriction.AllEnzymes. Something like this:
if RS in Restriction.AllEnzymes:
digestTuble = Restriction.RS.catalyze(this_seq)
else:
print('Please type in enzyme name correctly')
This problem is that python doesn't equate:
RS = "EcoRI"
digestTuple = Restriction.RS.catalyze(this_seq)
with
digestTuple = Restriction.EcoRI.catalyze(this_seq)
As it is trying to use the string name associated with the enzyme and not actually invoking the proper method.
Is there a way to invoke this method using the single conditional that searches all possible enzymes?
Maybe something like this Invoking a method by its name but in python?
The technical wording regarding this question is a little confusing for me, so I probably did not explain the problem accurately. Ill happily answer any clarifying questions.
Thank you
Upvotes: 3
Views: 187
Reputation: 184131
Use getattr()
, e.g.:
RS = "EcoRI"
digestTuple = getattr(Restriction, RS).catalyze(this_seq)
Upvotes: 4
Reputation: 3587
As simple as:
getattr(your_object,"methodname")()
example:
class My_Class:
def print_hi(self):
print 'hi'
a = My_Class()
getattr(a,'print_hi')()
output:
hi
In your case:
RS = "EcoRI"
digestTuple = getattr(Restriction, RS).catalyze(this_seq)
Upvotes: 4