[Bioperl-l] Re: How to extend a class in bioperl?
Jason Stajich
jason.stajich at duke.edu
Mon Apr 25 13:48:24 EDT 2005
See Bio::Tree::Node and Bio::Tree::NodeNHX as examples of adding new
methods to a base class.
See the score function for example in Bio::Tree::Tree as to how to have
a getter/setter method.
--
Jason Stajich
jason.stajich at duke.edu
http://www.duke.edu/~jes12/
On Apr 25, 2005, at 1:14 PM, Sally Li wrote:
> Hi, Jason,
>
> Thank you for your help! It works!
>
> If I need to add some attributes to the AnTree.pm,
> what is the easy way?
>
> Thank you!
>
> Sally
>
> --- Jason Stajich <jason.stajich at duke.edu> wrote:
>> You don't want those enclosing {}
>>
>> This is the minimum it takes to extend an object
>>
>> package AnTree;
>> use vars qw(@ISA);
>> use Bio::Tree::Tree;
>> @ISA = qw(Bio::Tree::Tree);
>>
>> # your routines here
>>
>> 1;
>>
>>
>> If you aren't adding any initialization arguments to
>> the object you
>> don't need to implement the 'new' function.
>>
>> If you want TreeIO to create AnTrees instead
>> Bio::Tree::Tree you need
>> to just do this
>> # initialize Bio::TreeIO like normal
>> my $treeio = Bio::TreeIO->new(-format => 'newick',
>> -file =>
>> 'filenamehere');
>> # reset the Tree object type that is created
>>
> $treeio->_eventHandler->treetype('Bio::Tree::AnTree');
>>
>>
>> -jason
>> --
>> Jason Stajich
>> jason.stajich at duke.edu
>> http://www.duke.edu/~jes12/
>>
>> On Apr 25, 2005, at 9:49 AM, Sally Li wrote:
>>
>>> Hi, there,
>>>
>>> Thanks to Jason, Will and Brian for the help in
>>> storage of an object.
>>>
>>> Now, I have a techical question. It may be a
>> general
>>> OO programming one. How to make a parent instance
>> into
>>> a child instance so that it has the methods of the
>>> child class?
>>>
>>> Attached are three files for the extension of
>>> Bio::Tree::Tree class (AnTree.pm file), the test
>>> script (AnTreeProcessV.pl) and the tree file
>>> (testTree). My goal in this example is to make the
>>> instance $myTree, which is an instance of Tree,
>> have
>>> the methods of AnTree so that I can use the
>> methods
>>> (like, getUnsortedNodes ()). As you see, the
>> AnTree.pm
>>> have the same attributes as Bio::Tree::Tree but
>> more
>>> methods.
>>>
>>> This is the way I would like to use to extend a
>> class.
>>> It is easy to manage the codes (in my opinion). I
>>> don't have to use the same name as
>> Bio::Tree::Tree,
>>> which I used to modify a class in Bioperl.
>>>
>>> Thank you for help!
>>>
>>> Sally
>>>
>>>
>>>
>>> __________________________________________________
>>> Do You Yahoo!?
>>> Tired of spam? Yahoo! Mail has the best spam
>> protection around
>>> http://mail.yahoo.com
>> <AnTree.pm><AnTreeProcessV.pl><testTree>
>>
>>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
More information about the Bioperl-l
mailing list