groovy & grails - lecture 4
TRANSCRIPT
Groovy Efficiency Oriented ProgrammingLecture 4
Master Proteomics amp Bioinformatics - University of GenevaAlexandre Masselot - summer 2010
Contents
Eclipse amp linux tips
xml parsing
Introduction to Object Oriented Programming
A couple of eclipse tips
team -gt local history
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
- use conjunction with ltctrlgt-A to select the whole file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
donrsquot forget to get command documentation with man
XML introduction
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Contents
Eclipse amp linux tips
xml parsing
Introduction to Object Oriented Programming
A couple of eclipse tips
team -gt local history
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
- use conjunction with ltctrlgt-A to select the whole file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
donrsquot forget to get command documentation with man
XML introduction
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
A couple of eclipse tips
team -gt local history
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
- use conjunction with ltctrlgt-A to select the whole file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
donrsquot forget to get command documentation with man
XML introduction
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
- use conjunction with ltctrlgt-A to select the whole file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
donrsquot forget to get command documentation with man
XML introduction
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
- use conjunction with ltctrlgt-A to select the whole file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
donrsquot forget to get command documentation with man
XML introduction
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
- use conjunction with ltctrlgt-A to select the whole file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
donrsquot forget to get command documentation with man
XML introduction
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
- use conjunction with ltctrlgt-A to select the whole file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
donrsquot forget to get command documentation with man
XML introduction
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
- use conjunction with ltctrlgt-A to select the whole file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
donrsquot forget to get command documentation with man
XML introduction
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
- use conjunction with ltctrlgt-A to select the whole file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
donrsquot forget to get command documentation with man
XML introduction
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
- use conjunction with ltctrlgt-A to select the whole file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
donrsquot forget to get command documentation with man
XML introduction
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
A couple of eclipse tips
team -gt local history- browse by all version of you saved file (by date)
general -gt editors -gt text editor -gt show line number- lines numbers appear in the editor left margin
- ltctrlgt-L + number jump to the given line number
right button -gt source -gt toggle comment
right button -gt source -gt format - indent amp format the select code
- use conjunction with ltctrlgt-A to select the whole file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
donrsquot forget to get command documentation with man
XML introduction
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
donrsquot forget to get command documentation with man
XML introduction
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
donrsquot forget to get command documentation with man
XML introduction
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
A couple of linux (shell) tips
less filename more power than rsquomore filenamersquo
- use arrows to go back and forth the file
- g to jump to top G to jump to the end text to search next occurrence of a text in the file (n to jump to the following occurrence) h to have help q to quit
wget -O myfasta httphttpwwwuniprotorguniprotP41159fasta
- to save a url target into a local file
donrsquot forget to get command documentation with man
XML introduction
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML introduction
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML introduction13 (contrsquod)
Lets consider an element as a record of
- atomic number (int)
- symbol (string)
- name (string)
- list of isotopes (int number double mass and double abundance)
Goal store and retrieve a list of element characteristics
Old fashion way in a text file
12Carbonc120110129890113003355110
Pros compact Cons cryptic hard to add new info or multiple type of info into one file problem adding free text etc
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML introduction13 (contrsquod)
RDBM is another reflex to store structured dataCREATE TABLE elements (atomicNumber int UNIQUE name name symbol name UNIQUE mass double precision)CREATE TABLE isotopes (atomicNumberBase int REFERENCES elements (atomicNumber) plus int abundance double precision mass double precision) INSERT into elements VALUES (12 Carbon C 12011)INSERT INTO isotopes VALUES(12 0 12 9890)INSERT INTO isotopes VALUES(12 1 13003355 110)
pros efficient powerful highly controlled cons
- can be rather heavy to manage (third parties tools or libraries) HSQLDB or Derby offer light alternative to postgres mysql or oracle for small databases
- needs expertise in DB- a bit over killer for ldquosmallrdquo problems
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML introduction13 (contrsquod)
eXtended Markup Language a versatile alternativeltinSilicoDefinitionsgt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=C name=Carbon atomicNumber=12gt ltmass monoisotopic=12000000 average=12011036905gt ltisotopesgt ltoneIsotope plus=0 mass=12000000 abundance=9890gt ltoneIsotope plus=1 mass=13003355 abundance=110gt ltisotopesgt ltoneElementgt ltelementsgt ltaminoAcidsgt ltoneAminoAcid name=Alanine code3=Ala code1=Agt ltaminoAcidsgtltinSilicoDefinitionsgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML structure
Header encode for the charset (change if accent japanese characters)
ltxml version=10 encoding=ISO-8859-1gt
Element a node in the structured tree of the document
ltoneElementgtltoneElementgtltisImportantgt openclose at once
Content
ltauthorgtJRR Tolkienltauthorgtlttitlegtlt[CDATA[Laurel amp Hardy]]gtlttitlegt
Attribute
ltoneElement name=Carbon symbol=CgtltoneElementgt
Comments
lt-- whatever comment (not parsed by default) --gt
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML child elements or attributes
What attribute cannot do
- no nested structures
- no repeated attributes lttag name=one name=twogt is not allowed
- limited characters not CDATA for the value
So when to use attributes instead of elements
- Attributes could be avoided and only child elements used Usually the main part of the node information should be stored in sub chidren However attributes can have a natural use
- for short information
- for information that should be read at the element level (before diving into its children nodes)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML what we will not talk about (today)
XML file generation
- it is simple text file
- Groovy uses XmlTemplateEngine
XML structure definition
- Schema
- DTD
XSLT for xml structure transformation
namespaces
We will focus on retrieving information from an xml file (or String)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML parsing challenges
A common need retrieve partial information (substructure or filtered with criteria)
- syntax to access to info within the tree
- navigate through all children
XML tree can be large (gt4GB for uniprotxml)
- no possible to load everything at once then scan through memory
- loop across all substructure (eg a uniprot entry) then access to descendant easily and release memory before opening next entry
Groovy
- XmlParser access and modify the tree but all is in memory Not covered here
- XmlSlurper lazy loading access through iterators readonly access
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML XmlSlurper
XmlSlurper parse a source into a structure ready for traversing
def records=new XmlSlurper()parseText(aStringWithXml)def records=new XmlSlurper()parse(new File(aStringPath))
Parsing is done iterating through NodeChildren (groovyutilslurpersupportNodeChildren) specifying a path amp constraints in the xml tree
All the document is not loaded at once but only the matching subtrees one after the other
Compared to other xml parser it corresponds to a mix between DOM and SAX methods
Perl equivalent would be the module XMLTwig
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XML XmlSlurper13 (contrsquod)
String xmlText=rsquorsquorsquoltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt ltelementsgt ltoneElement symbol=H name=Hydrogen atomicNumber=1gt ltmass monoisotopic=1007825 average=100797594155gt ltisotopesgt ltoneIsotope plus=0 mass=1007825 abundance=99985gt ltoneIsotope plus=1 mass=2014102 abundance=0015gt ltisotopesgt ltoneElementgt ltoneElement symbol=He name=Helium atomicNumber=3gt ltmass monoisotopic=3016029 average=40026016187964gt ltisotopesgt ltoneIsotope plus=0 mass=3016029 abundance=00014gt ltoneIsotope plus=1 mass=4002603 abundance=9999986gt ltisotopesgt ltoneElementgt ltoneElement symbol=Li name=Lithium atomicNumber=6gt ltmass monoisotopic=6015123 average=69417395556gt ltisotopesgt ltoneIsotope plus=0 mass=6015123 abundance=742gt ltoneIsotope plus=1 mass=7016005 abundance=9258gt ltoneIsotope plus=3 mass=777016005 abundance=99999gt lt-- this is fake --gt ltisotopesgt ltoneElementgt ltelementsgtltinSilicoDefinitionsgtlsquorsquorsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XmlSlurper accessing a single element
Instantiating the slurper
def insilicoDef = new XmlSlurper()parseText( xmlText )
Single elementltinSilicoDefinitionsgt ltdescriptiongt13 ltsource type=programgt13 ltnamegtelementatorltnamegt13 ltisValidatedgt13 ltsourcegt ltdescriptiongt
def source=insilicoDefdescriptionsource
Reading an attribute
sourcetypetext() -gt lsquoprogramrsquo
Reading a sub element contents
sourcenametext() -gt lsquoelementatorrsquo
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XmlSlurper selecting a list
Getting all substructure giving a path will generate an array
insilicoDefelementsoneElement 3 elements
Can be visited with closure
insilicoDefelementsoneElementeachprintln itsymbolinsilicoDefelementsoneElementcollectitmassaveragetoDouble()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
XmlSlurper subtree with constraints with findAll
We can add constraints on the element to be actually walked through
Getting oneElement where symbol attribute has 2 characters
insilicoDefelementsoneElementfindAll itsymboltext() ==~
Where mass children has monoisotopic attribute is gt6
findAll (itmassmonoisotopictext() as BigDecimal) gt 6
Elements with exactly 2 isotopesoneIsotope children
findAll itisotopesoneIsotopesize() == 2
Getting the first element and piping closures
insilicoDefelementsoneElement[0] isotopesoneIsotopemasstoDouble()sum()
httpgroovycodehausorgReading+XML+using+Groovy27s+XmlSlurper
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Object Oriented Programming class
Although Groovy can be seen as a scripting language with a rather loose syntax (think of dynamic typing with def) it is is also fully OO
Based on Java enforcing string OO design but still adding freedom of scripting
The basic component of OOP is a Class a structure containing
- enforced properties named fields
- actions names methods
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Class an introduction
An first example
class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())year - birthyear13
Then a script uses this class
Person guy=new Person(nameJoe birthnew Date(3121980))
println $guyname is $guyage() -gt Joe is 30
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Making a new class
By convention class name starts with an upper case (when variable usually start with lower case)
Within a source directory create a package (a subdirectory structure where lsquorsquo is a directory path separator) for example
unigempbeopproteomicssequence
In eclipse File -gt new -gt package (ltctrlgt-N package)
Within this package create a class File -gt new -gt groovy class and set name Protein
A Proteingroovy file is create with
package unigempbeopproteomicssequenceclass Protein
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Class fields
Fields are properties of a class they can be dynamically or statically typed (of any existing type (Integer List Map etc)
Field are by default associated to an instance of the class (an object generated by new Person())
They can be changed directly from outside the class (GBean)
guyname = lsquoJimmylsquo guysetName(lsquoJimmyrsquo)
Or read
println guynameprintln guygetName()
Inside a method the instance properties are directly used by their name
int age()return (new Date())year - birthyear
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Class static fields
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Class static fields
Fields can also be static ie a global value shared by all instances of the class (by convention upper case)
If we come back to our previous Person example we want
static int AGE_LIMIT = 18boolean canDrive()return age gt= AGE_LIMIT
We can change this value at once for all Person instances
PersonAGE_LIMIT = 16
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Constructor
Thanks to the scripting spirit there is no need to declare constructor (the function to instantiate an object)
Field can be filled with a map structure
Person girl=new Person(namelsquoMarylynrsquo)
It is not compulsory to set all the field
To set default value to a field you can define it in the property declaration
String name=lsquoJohn Doersquo
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Methods
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Methods
Methods are functions with are called within the context of an object instance
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Methods
Methods are functions with are called within the context of an object instance
All properties of the instance are accessed directly
It is possible to modify the instance directly addressing the instance fields
It is possible to pass argument to the function
It is possible to return a value (of whatever type)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Unit testing
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)
Unit testing
Each method of a class must be tested to ensure it fulfills the intended goals
Eclipsegroovy offers a integrated environment for that purpose
In another source folder (typically srctestsgroovy) test will be generated
- select a class
- File -gt new -gt Groovy Test Case
- select the other srctestgroovy source folder
- select junit 3 (better for later use of grails for the moment)
- next -gt select methods to be tested
In junit 3 any method with name starting with test will be used as test
right button -gt run as -gt junit test (ltctrlgt-ltaltgt-X-T)
Customize show view (ltctrlgt-ltaltgt-Q-U + down arrow and tab)