groovy & grails - lecture 4

97
Groovy: Efficiency Oriented Programming Lecture 4 Master Proteomics & Bioinformatics - University of Geneva Alexandre Masselot - summer 2010

Transcript of groovy & grails - lecture 4

Page 1: groovy & grails - lecture 4

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)

Page 2: groovy & grails - lecture 4

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)

Page 3: groovy & grails - lecture 4

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)

Page 4: groovy & grails - lecture 4

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)

Page 5: groovy & grails - lecture 4

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)

Page 6: groovy & grails - lecture 4

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)

Page 7: groovy & grails - lecture 4

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)

Page 8: groovy & grails - lecture 4

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)

Page 9: groovy & grails - lecture 4

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)

Page 10: groovy & grails - lecture 4

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)

Page 11: groovy & grails - lecture 4

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)

Page 12: groovy & grails - lecture 4

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)

Page 13: groovy & grails - lecture 4

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)

Page 14: groovy & grails - lecture 4

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)

Page 15: groovy & grails - lecture 4

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)

Page 16: groovy & grails - lecture 4

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)

Page 17: groovy & grails - lecture 4

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)

Page 18: groovy & grails - lecture 4

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)

Page 19: groovy & grails - lecture 4

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)

Page 20: groovy & grails - lecture 4

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)

Page 21: groovy & grails - lecture 4

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)

Page 22: groovy & grails - lecture 4

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)

Page 23: groovy & grails - lecture 4

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)

Page 24: groovy & grails - lecture 4

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)

Page 25: groovy & grails - lecture 4

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)

Page 26: groovy & grails - lecture 4

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)

Page 27: groovy & grails - lecture 4

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)

Page 28: groovy & grails - lecture 4

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)

Page 29: groovy & grails - lecture 4

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)

Page 30: groovy & grails - lecture 4

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)

Page 31: groovy & grails - lecture 4

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)

Page 32: groovy & grails - lecture 4

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)

Page 33: groovy & grails - lecture 4

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)

Page 34: groovy & grails - lecture 4

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)

Page 35: groovy & grails - lecture 4

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)

Page 36: groovy & grails - lecture 4

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)

Page 37: groovy & grails - lecture 4

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)

Page 38: groovy & grails - lecture 4

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)

Page 39: groovy & grails - lecture 4

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)

Page 40: groovy & grails - lecture 4

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)

Page 41: groovy & grails - lecture 4

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)

Page 42: groovy & grails - lecture 4

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)

Page 43: groovy & grails - lecture 4

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)

Page 44: groovy & grails - lecture 4

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)

Page 45: groovy & grails - lecture 4

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)

Page 46: groovy & grails - lecture 4

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)

Page 47: groovy & grails - lecture 4

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)

Page 48: groovy & grails - lecture 4

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)

Page 49: groovy & grails - lecture 4

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)

Page 50: groovy & grails - lecture 4

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)

Page 51: groovy & grails - lecture 4

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)

Page 52: groovy & grails - lecture 4

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)

Page 53: groovy & grails - lecture 4

XmlSlurper subtree with constraints 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)

Page 54: groovy & grails - lecture 4

XmlSlurper subtree with constraints 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)

Page 55: groovy & grails - lecture 4

XmlSlurper subtree with constraints 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)

Page 56: groovy & grails - lecture 4

XmlSlurper subtree with constraints 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)

Page 57: groovy & grails - lecture 4

XmlSlurper subtree with constraints 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)

Page 58: groovy & grails - lecture 4

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)

Page 59: groovy & grails - lecture 4

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)

Page 60: groovy & grails - lecture 4

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)

Page 61: groovy & grails - lecture 4

Class an introduction

An first example

class Person13 String 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)

Page 62: groovy & grails - lecture 4

Class an introduction

An first example

class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())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)

Page 63: groovy & grails - lecture 4

Class an introduction

An first example

class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())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)

Page 64: groovy & grails - lecture 4

Class an introduction

An first example

class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())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)

Page 65: groovy & grails - lecture 4

Class an introduction

An first example

class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())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)

Page 66: groovy & grails - lecture 4

Class an introduction

An first example

class Person13 String name field of type String13 Date birth field of type Date1313 int age() a method returning an int13 13 return (new Date())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)

Page 67: groovy & grails - lecture 4

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)

Page 68: groovy & grails - lecture 4

Making a new class

By convention class name starts with an upper case (when variable usually 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)

Page 69: groovy & grails - lecture 4

Making a new class

By convention class name starts with an upper case (when variable usually 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)

Page 70: groovy & grails - lecture 4

Making a new class

By convention class name starts with an upper case (when variable usually 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)

Page 71: groovy & grails - lecture 4

Making a new class

By convention class name starts with an upper case (when variable usually 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)

Page 72: groovy & grails - lecture 4

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)

Page 73: groovy & grails - lecture 4

Class fields

Fields are properties of a class they can be dynamically or statically typed (of 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)

Page 74: groovy & grails - lecture 4

Class fields

Fields are properties of a class they can be dynamically or statically typed (of 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)

Page 75: groovy & grails - lecture 4

Class fields

Fields are properties of a class they can be dynamically or statically typed (of 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)

Page 76: groovy & grails - lecture 4

Class fields

Fields are properties of a class they can be dynamically or statically typed (of 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)

Page 77: groovy & grails - lecture 4

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)

Page 78: groovy & grails - lecture 4

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)

Page 79: groovy & grails - lecture 4

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)

Page 80: groovy & grails - lecture 4

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)

Page 81: groovy & grails - lecture 4

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)

Page 82: groovy & grails - lecture 4

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)

Page 83: groovy & grails - lecture 4

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)

Page 84: groovy & grails - lecture 4

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)

Page 85: groovy & grails - lecture 4

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)

Page 86: groovy & grails - lecture 4

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)

Page 87: groovy & grails - lecture 4

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)

Page 88: groovy & grails - lecture 4

Methods

Methods are functions with are called within the context 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)

Page 89: groovy & grails - lecture 4

Methods

Methods are functions with are called within the context 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)

Page 90: groovy & grails - lecture 4

Methods

Methods are functions with are called within the context 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)

Page 91: groovy & grails - lecture 4

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)

Page 92: groovy & grails - lecture 4

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)

Page 93: groovy & grails - lecture 4

Unit 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)

Page 94: groovy & grails - lecture 4

Unit testing

Each method of a class must be tested to ensure it fulfills the intended goals

Eclipsegroovy offers a integrated environment for that purpose

In another source folder (typically srctestsgroovy) test will be generated

- select a class

- File -gt new -gt Groovy Test Case

- select 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)

Page 95: groovy & grails - lecture 4

Unit testing

Each method of a class must be tested to ensure it fulfills the intended goals

Eclipsegroovy offers a integrated environment for that purpose

In another source folder (typically srctestsgroovy) test will be generated

- select a class

- File -gt new -gt Groovy Test Case

- select the other srctestgroovy source folder

- select junit 3 (better for later use of grails for 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)

Page 96: groovy & grails - lecture 4

Unit testing

Each method of a class must be tested to ensure it fulfills the intended goals

Eclipsegroovy offers a integrated environment for that purpose

In another source folder (typically srctestsgroovy) test will be generated

- select a class

- File -gt new -gt Groovy Test Case

- select the other srctestgroovy source folder

- select junit 3 (better for later use of grails for 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)

Page 97: groovy & grails - lecture 4

Unit testing

Each method of a class must be tested to ensure it fulfills the intended goals

Eclipsegroovy offers a integrated environment for that purpose

In another source folder (typically srctestsgroovy) test will be generated

- select a class

- File -gt new -gt Groovy Test Case

- select the other srctestgroovy source folder

- select junit 3 (better for later use of grails for 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)