[Biopython] subclassing SeqRecord
Björn Johansson
bjorn_johansson at bio.uminho.pt
Sat Apr 30 09:43:06 UTC 2011
Hi, and thanks for the answer,
I could use the annotations but it is a dict and I suppose that I
could not be entirely sure that whatever key I choose is not already
used? This is also an exercise in python an inheritance to teach me
this technique.
What added to my confusion in my old code was that I initiated a
SeqRecord object with a SeqRecord object as the seq property. The seq
property can apparently be anything for which there is a len()
method..?
I ended up doing a class that can be initialized with a string, Seq
object or a SeqRecord object.
from Bio.Alphabet.IUPAC import ambiguous_dna
from Bio.SeqRecord import SeqRecord
from Bio.Seq import Seq
#Subclassing of the SeqRecord class
class Primer(SeqRecord):
def __init__(self, seq=None, annealing_position=None, *args, **kwargs):
self.annealing_position = annealing_position
if isinstance(seq,Seq):
seq.alphabet = ambiguous_dna
SeqRecord.__init__(self, seq, *args, **kwargs)
elif isinstance(seq,str):
SeqRecord.__init__(self,Seq(seq,ambiguous_dna), *args,
**kwargs)
elif isinstance(seq,SeqRecord):
SeqRecord.__init__(self,
seq.seq,
seq.id,
seq.name,
seq.description,
seq.dbxrefs,
seq.features,
seq.annotations,
seq.letter_annotations)
else:
raise TypeError("the seq property needs to be a string, a
Seq object or a SeqRecord object")
a = Primer("aaa",11,"id1","name1")
b = Primer(Seq("ccc"),22,"id2","name2")
c = Primer(SeqRecord(Seq("ttt")),33)
print a.annealing_position
print b.annealing_position
print c.annealing_position
print a.reverse_complement().seq
print b.reverse_complement().seq
print c.reverse_complement().seq
On Tue, Apr 26, 2011 at 17:15, Peter Cock <p.j.a.cock at googlemail.com> wrote:
>
> 2011/4/26 Björn Johansson <bjorn_johansson at bio.uminho.pt>:
> > Hi, I tried to subclass the SeqRecord object to make it take a new parameter
> > which is a int describing the annealing position for a primer. I tried the
> > code below, but it does not produce the result I want and it is also ugly,
> > mostly where indicated. This question may be more about Python the
> > BioPython, but I run into the wall here.
>
> What I would expect you to do is just store this integer in the existing
> SeqRecord's annotations dictionary.
>
> > The comments in the code point out where it goes wrong.
> >
> > I have RTFM but in the Python docs, but I still did not figure this one out.
> > I suspect that I am complicating things.
> >
> > If someone knows how to do this, I would be grateful!
>
>
> You probably should use isinstance(x, SeqRecord) rather than type(x).
>
> Peter
--
______O_________oO________oO______o_______oO__
Björn Johansson
Assistant Professor
Departament of Biology
University of Minho
Campus de Gualtar
4710-057 Braga
PORTUGAL
http://www.bio.uminho.pt
http://sites.google.com/site/bjornhome
Work (direct) +351-253 601517
Private mob. +351-967 147 704
Dept of Biology (secretariate) +351-253 60 4310
Dept of Biology (fax) +351-253 678980
More information about the Biopython
mailing list