From james.foadi at diamond.ac.uk  Wed Jul  7 17:56:38 2010
From: james.foadi at diamond.ac.uk (james.foadi at diamond.ac.uk)
Date: Wed, 7 Jul 2010 16:56:38 +0100
Subject: [Rd] how to define method for "+" function in a new class
References: <EXCHANGE417Ie8tlMaz00000c08@exchangegw1.rl.ac.uk>
	<4854F2500EA8C4478A508D2D92973E5202E812CC@EXCHANGE25.fed.cclrc.ac.uk>
	<4C34A0A0.8030001@fhcrc.org>
Message-ID: <4854F2500EA8C4478A508D2D92973E5202E812D0@EXCHANGE25.fed.cclrc.ac.uk>

Thank you, Martin! Very clear.

J

Dr James Foadi PhD
Membrane Protein Laboratory (MPL)
Diamond Light Source Ltd
Diamond House
Harewell Science and Innovation Campus
Chilton, Didcot
Oxfordshire OX11 0DE

Email    :  james.foadi at diamond.ac.uk
Alt Email:  j.foadi at imperial.ac.uk



-----Original Message-----
From: Martin Morgan [mailto:mtmorgan at fhcrc.org]
Sent: Wed 07/07/2010 16:43
To: Foadi, James (Imperial Coll.,RAL,DIA)
Cc: r-devel at r-project.org
Subject: Re: [Rd] how to define method for "+" function in a new class
 
On 07/07/2010 08:09 AM, james.foadi at diamond.ac.uk wrote:
> 
> 
> Dear R developers,
> I have a new class, which I called "Molecule", and have tried to define =
> a "+" operation for 2 objects of this class.
> This is what I have written so far, although the method is not complete =
> (I'm trying to look at it at intermediate stages):
> 
> setMethod(
>           f=3D"+",
>           signature(x=3D"Molecule",y=3D"Molecule"),
>           definition=3Dfunction(x,y,...)
>                      {
>                       # Check both objects are correct
>                       checkMolecule(x)
>                       checkMolecule(y)
> 
>                       # Extract chains information
>                       ch1 <- getMoleculeChains(x)
>                       ch2 <- getMoleculeChains(y)
>                       union_ch <- unique(c(ch1,ch2))
>                       not_used <- .ALPHABET
>                       for (i in seq(along =3D union_ch))
>                       {
>                        idx <- which(not_used !=3D union_ch[i])
>                        if (length(idx) !=3D 0) not_used <- not_used[idx]
>                       }
> 
>                       return(not_used)
>                      }
>          )
> 
> 
> The definition of class Molecule is included earlier in the same file. =
> When I source it, I get the following error message:
> 
> Error in match.call(fun, fcall) :=20
>   unused argument(s) (x =3D "Molecule", y =3D "Molecule")

If I

> getGeneric("+")
standardGeneric for "+" defined from package "base"
  belonging to group(s): Arith

function (e1, e2)
standardGeneric("+", .Primitive("+"))
<environment: 0xb432f8>
Methods may be defined for arguments: e1, e2
Use  showMethods("+")  for currently available ones.

I see that the generic is defined to take two arguments e1 and e2. So

setMethod("+", c("Molecule", "Molecule"), function(e1, e2) {
    ## ...
})

but actually here it might often pay to discover ?GroupGenericFunctions
and end up with something like


setClass("A", representation=representation(x="numeric"))

setMethod("Arith", c("A", "A"), function(e1, e2) {
   new(class(e1), x=callGeneric(e1=e1 at x, e2=e2 at x))
})

and then

> new("A", x=1:5) + new("A", x=5:1)
An object of class "A"
Slot "x":
[1] 6 6 6 6 6

but also

> new("A", x=1:5) * new("A", x=5:1)
An object of class "A"
Slot "x":
[1] 5 8 9 8 5

Martin

> 
> 
> I can't see what's wrong in my method definition. Can anyone help me =
> with this?
> 
> J
> 
> Dr James Foadi PhD
> Membrane Protein Laboratory (MPL)
> Diamond Light Source Ltd
> Diamond House
> Harewell Science and Innovation Campus
> Chilton, Didcot
> Oxfordshire OX11 0DE
> 
> Email    :  james.foadi at diamond.ac.uk
> Alt Email:  j.foadi at imperial.ac.uk
> 


-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793


-- 
This e-mail and any attachments may contain confidential...{{dropped:8}}



[Rd] how to define method for "+" function in a new class
james.foadi at diamond.ac.uk