Wednesday 25 July 2018

Phil Ewels Presenting Nextflow

Phil Ewels at Nextflow Hack 17

Standardising Swedish Genomics Analyses Using Nextflow

Groovy Trim Method

Groovy Trim Examples

Friday 20 July 2018

Python Exception Class

Cited from the book "Python How to Program"

""
All exceptions inherit from base class Exception and are defined in module exceptions. Python automatically places all exception names in the built-in namespace, so programs do not need to import the exceptions module to use exceptions. Python defines four primary classes that inherit from Exception -- SystemExit, StopIteration, Warning and StandardError. Exception SystemExit, when raised and left uncaught, terminates program execution. If an interactive session encounters an uncaught SystemExit exception, the interactive session terminates. Python uses exception StopIteration to determine when a for loop reaches the end of its sequence. Python uses
Warning exceptions to indicate that certain elements of Python may change in the future. StandardError is the base class for all Python error exceptions (e.g., ValueError and ZeroDivisionError).
""




Python Try Statement

Cited from the book "Python How to Program"

Python uses try statements to enable exception handling. The try statement
encloses other statements that potentially cause exceptions. A try statement begins with
keyword try, followed by a colon (:), followed by a suite of code in which exceptions
may occur. The try statement may specify one or more except clauses that immediately
follow the try suite. Each except clause specifies zero or more exception class names
that represent the type(s) of exceptions that the except clause can handle. An except
clause (also called an except handler) also may specify an identifier that the program can
use to reference the exception object that was caught. The handler can use the identifier to
obtain information about the exception from the exception object. An except clause that
specifies no exception type is called an empty except clause. Such a clause catches all
exception types. After the last except clause, an optional else clause contains code that
executes if the code in the try suite raised no exceptions. If a try statement specifies no
except clauses, the statement must contain a finally clause, which always executes,
regardless of whether an exception occurs. We discuss each possible combination of
clauses over the next several sections.

It is a syntax error to write a try statement that contains except and finally clauses.
The only acceptable forms are try/except, try/except/else and try/finally.


Python Raising an Exception

Cited from the book "Python How to Program"

"""
The simplest form of raising an exception consists of the keyword raise, followed by
the name of the exception to raise. Exception names identify classes; Python exceptions are
objects of those classes. When a raise statement executes, Python creates an object of the
specified exception class. The raise statement also may specify arguments that initialize
the exception object. To do so, follow the exception class name with a comma (,) and the
argument (or a tuple of arguments). Programs can use an exception object’s attributes to dis-
cover more information about the exception that occurred. The raise statement has many
forms.

The arguments used to initialize an exception object can be referenced in an exception handler to perform an appropriate task.

An exception can be raised without passing arguments to initialize the exception object. In
this case, knowledge that an exception of this type occurred normally provides sufficient in-
formation for the handler to perform its task.

When code in a program causes an exception, or when the Python interpreter detects a
problem, the code or the interpreter raises (or throws) an exception. Some programmers
refer to the point in the program at which an exception occurs as the throw point—an
important location for debugging purposes. Exceptions are objects of classes that inherit from class Exception. If an exception occurs in a try,suite, the try suite expires (i.e., terminates immediately), and program control transfers to the first except handler (if there is one) following the try suite.

Next, the interpreter searches for the first except handler that can process the type of exception that occurred. The interpreter locates the matching except by comparing the raised exception’s type to
each except’s exception type(s) until the interpreter finds a match. A match occurs if the
types are identical or if the raised exception’s type is a derived class of the handler’s excep-
tion type. If no exceptions occur in a try suite, the interpreter ignores the exception han-
dlers for the try statement and executes the try statement’s else clause (if the statement
specifies an else clause). If no exceptions occur, or if one of the except clauses success-
fully handles the exception, program execution resumes with the next statement after the
try statement. If an exception occurs in a statement that is not in a try suite and that state-
ment is in a function, the function containing that statement terminates immediately and the
interpreter attempts to locate an enclosing try statement in a calling code—a process
called stack unwinding.

Python is said to use the termination model of exception handling, because the try
suite that raises an exception expires immediately when that exception occurs.
"""

Thursday 19 July 2018

Python __slot__ Attribute

Cited from the book "Python How to Program"

"""

When a new class defines the __slots__ attribute, objects of the class can assign values only to attributes whose names appear in the __slots__ list. If a client attempts to assign a value to an attribute whose name does not appear in __slots__, Python raises an exception.

If a new class defines attribute __slots__, but the class’s constructor does not initialize
the attributes’ values, Python assigns None to each attribute in __slots__ when an object
of the class is created.

A derived class inherits its base-class __slots__ attribute. However, if programs should
not be allowed to add attributes to objects of the derived class, the derived class must define
its own __slots__ attribute. The derived-class __slots__ contains only the allowed
derived-class attribute names, but clients still can set values for attributes specified by the
derived class’s direct and indirect bases classes.
"""

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cited from Usage of __slot__

class Base:
    __slots__ = 'foo', 'bar'

class Right(Base):
    __slots__ = 'baz', 

class Wrong(Base):
    __slots__ = 'foo', 'bar', 'baz'        # redundant foo and bar


Python Static Methods

Cited from the book "Python How to Program"

In Python 2.2 all classes (not only classes that inherit from object) can define static methods. A static method can be called by a client of the class, even if no objects of the class exist. Typically, a static method is a utility method of a class that does not require an object of the class to execute.

A class designates a method as
static by passing the method’s name to built-in function staticmethod and binding a
name to the value returned from the function call. Static methods differ from regular
methods because, when a program calls a static method, Python does not pass the object-
reference argument to the method. Therefore, a static method does not specify self as
the first argument. This allows a static method to be called even if no objects of the class
exist.

Static methods can be called either by using the class name in which the method is
defined or by using the name of an object of that class. Function main (lines 36–58) dem-

When a method of a class does not require an object of the class to perform its task,
the programmer designates that method as static.





Tuesday 17 July 2018

Python __str__ Method

Cited from the book "Python How to Program"

For example,
hourly = HourlyWorker( "Bob", "Smith", 40.0, 10.00 ) # create an object from the class

 print hourly # invoke __str__ implicitly
 print hourly.__str__() # invoke __str__ explicitly
 print HourlyWorker.__str__( hourly ) # explicit, unbound call, and the __str__ method defined takes only one argument 'self'.




Python __dict__ Attribute

Cited from the book "Python How to Program"

Recall that an object of a class has a namespace that contains identifiers and their values. Attribute __dict__ contains this information for each object.

Python __bases__ Attribute

Cited from the book "Python How to Program"

attribute (lines 33–34). Recall from Chapter 7 that each class contains special attributes,
including __bases__, which is a tuple that contains references to each of the class’s base
classes.

Python Unbound and Bound Method Calls

Cited from the book "Python How to Program"

A bound method call is invoked by accessing the method name through an object (e.g., anObject.method()). We have seen that Python inserts the object-reference argument for bound method calls. An unbound method call is invoked by accessing the method through its class name and specifically passing an object reference.




Python issubclass and isinstance Methods

Python provides two built-in functions—issubclass and isinstance— that enable us to determine whether one class is derived from another class and whether a value is an object of a particular class or of a subclass of that class.

Friday 13 July 2018

Operator Overloading

Cited from the book "Python How to Program"

Operators are overloaded by writing a method definition as you normally would, except
that the method name corresponds to the Python special method for that operator. For
example, the method name __add__ overloads the addition operator (+). To use an operator
on an object of a class, the class must overload (i.e., define a special method for) that operator.

It is not possible to change the “arity” of an operator (i.e., the number of operands an
operator takes)—overloaded unary operators remain unary operators, and overloaded
binary operators remain binary operators. Operators + and - each have both unary and
binary versions; these unary and binary versions can be overloaded separately, using dif-
ferent method names. It is not possible to create new operators; only existing operators can
be overloaded.

The meaning of how an operator works on objects of built-in types cannot be changed
by operator overloading. The programmer cannot, for example, change the meaning of how
+ adds two integers. Operator overloading works only with objects of user-defined classes
or with a mixture of an object of a user-defined class and an object of a built-in type.

Overloading a binary mathematical operator (e.g., +, -, *) automatically overloads the
operator’s corresponding augmented assignment statement. For example, overloading an
addition operator to allow statements like

object2 = object2 + object1

implies that the += augmented assignment statement also is overloaded to allow statements
such as

object2 += object1

Although (in this case) the programmer does not have to define a method to overload the
+= assignment statement, such behavior also can be achieved by defining the method ex-
plicitly for that class.

A unary operator for a class is overloaded as a method that takes only the object reference
argument (self). When overloading a unary operator (such as ~) as a method, if object1 is an object of class Class, when the interpreter encounters the expression ~object1 the interpreter generates the call
object1.__invert__().

A binary operator or statement for a class is overloaded as a method with two arguments:
self and other. Later in this chapter, we will overload the + operator to indicate addi-
tion of two objects of class Rational. When overloading binary operator +, if y and z
are objects of class Rational, then y + z is treated as if y.__add__( z ) had been
written, invoking the __add__ method. If y is not an object of class Rational, but z is
an object of class Rational, then y + z is treated as if z.__radd__( y ) had been
written. The method is named __radd__, because the object for which the method exe-
cutes appears to the right of the operator. Usually, overloaded binary operator methods cre-
ate and return new objects of their corresponding class.

When overloading assignment statement += as a Rational method that accepts two
arguments, if y and z are objects of class Rational, then y += z is treated as if
y.__iadd__( z ) had been written, invoking the __iadd__ method. The method is
named __iadd__, because the method performs its operations “in-place” (i.e., the
method uses no extra memory to perform its behavior). Usually, this means that the method
performs any necessary calculations on the object reference argument (self), then returns
the updated reference.

What happens if we evaluate the expression y + z or the statement y += z, and only
y is an object of class Rational? In both cases, z must be coerced (i.e., converted) to an
object of class Rational, before the appropriate operator overloading method executes.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cited from A Guide to Python's Magic Methods

some_object + other
That was "normal" addition. The reflected equivalent is the same thing, except with the operands switched around:
other + some_object

Note that the object on the left hand side of the operator (other in the example) must not define (or return NotImplemented) for its definition of the non-reflected version of an operation. For instance, in the example, some_object.__radd__ will only be called if other does not define __add__.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cited from __radd__

Suppose you are implementing a class that you want to act like a number via operator overloading. So you implement __add__ in your class, and now expressions like myobj + 4 can work as you want and yield some result. This is because myobj + 4 is interpreted as myobj.__add__(4), and your custom method can do whatever it means to add 4 to your custom class.
However, what about an expression like 4 + myobj which is really (4).__add__(myobj)? The 4 is an instance of a Python built-in type and its __add__ method doesn't know anything about your new type, so it will return a special value NotImplemented. (The interpreter recognizes this special value coming from __add__ and raises a TypeError exception which kills your program, which is the behavior you'd actually see, rather than the special value being returned.)
It would suck for operator overloading if myobj + 4 was valid but 4 + myobj was invalid. That's arbitrary and restrictive — addition is supposed to be commutative. Enter __radd__. Python will first try (4).__add__(myobj), and if that returns NotImplemented Python will check if the right-hand operand implements __radd__, and if it does, it will call myobj.__radd__(4)rather than raising a TypeError. And now everything can proceed as usual, as your class can handle the case and implement your behavior, rather than the built-in type's __add__ which is fixed and doesn't know about your class.









Thursday 12 July 2018

Python __getattr__ Method

Cited from the book "Python How to Program"

When a client program contains the expression time1.attribute as an rvalue (i.e., the right-hand value in an operator expression), Python first looks in time1’s __dict__ attribute for the attribute name.

If the attribute name is in __dict__, Python simply returns the attribute’s value. If the attribute name is not in the object’s __dict__, Python generates the call time1.__getattr__( attribute ) where attribute is the name of the attribute that the client is attempting to access.

Python __setattr__ Method

Cited from the book "Python How to Program"

It is important that method __setattr__ uses an object’s __dict__ attribute to set an object’s attributes. If the statement inside __setattr__ method is used,

self._hour = value

method __setattr__ would execute again, with the arguments "_hour" and value, resulting in infinite recursion. Assigning a value through the object’s __dict__ attribute, however, does not invoke method __setattr__, but simply inserts the appropriate key–value pair in the object’s __dict__.



Python __str__ Method

Cited from the book "Python How to Program"

A Python class can define special method __str__, to provide an informal (i.e., human-readable) string representation of an object of the class. If a client program of the class contains the statement

print objectOfClass

Python calls the object’s __str__ method and outputs the string returned by that method.

Returning a non-string value from method __str__ is a fatal, runtime error.


Python Class Attribute

Cited from the book "Python How to Program"

Although class attributes may seem like global variables, each class attribute resides in the namespace of the class in which it is created. Class attributes should be initialized once (and only once) in the class definition. A class’s class attributes can be accessed through any object of that class. A class’s class attributes also exist even when no object of that class exists. To access a class attribute when no object of the class exists, prefix the class name, followed by a period, to the name of the attribute.



Python Destructor

Cited from the book "Python How to Program"

A destructor executes when an object is destroyed (e.g., after no more references to the object exist). A class can define a special method called __del__ that executes when the last reference to an object is deleted or goes out of scope. The method itself does not actually destroy the object -- it performs termination housekeeping before the interpreter reclaims the object’s memory, so that memory may be reused. A destructor normally specifies no parameters other than self and returns None.



Python Object Attribute Variable Names Beginning With Double Underscores

Cited from the book "Python How to Program"

When Python encounters an attribute name that begins with two underscores, the interpreter performs name mangling on the attribute, to prevent indiscriminate access to the data. Name mangling changes the name of an attribute by including information about the class to which the attribute belongs. For example, if the Time constructor contained the line self.__hour = 0. Python creates an attribute called _Time__hour, instead of an attribute called __hour.

Python Exception

Cited from the book "Python How to Program"

An exception is a Python object that indicates a special event (most often an error) has occurred.

raise ValueError, "Invalid hour value: %d" % hour

The keyword raise is followed by the name of the exception, a comma and a value that the exception
object stores as an attribute. When Python executes a raise statement, an exception is

raised; if the exception is not caught, Python prints an error message that contains the name

of the exception and the exception’s attribute value, as shown in Fig. 7.8.

Python Object Attribute Variable Names Beginning With a Single Underscore

Cited from the book "Python How to Program"

Attribute names that begin with a single underscore have no special meaning in the syntax of the Python language itself. However, the single leading underscore is a convention among Python programmers who use the class. When a class author creates an attribute with a single leading underscore, the author does not want users of the class to access the attribute directly. If a program requires access to the attributes, the class author provides some other means for doing so. In this case, we provide access methods through which clients should manipulate the data.

Python Trailing Commas

Cited from Python trailing comma after print executes next instruction

In Python 2.x, a trailing , in a print statement prevents a new line to be emitted.
  • In Python 3.x, use print("Hi", end="") to achieve the same effect.

Python Class Instance Object

Cited from the book "Python How to Program"

All methods, including constructors, must specify at least one parameter. This parameter represents the object of the class for which the method is called. This parameter often is referred to as the class instance object. This term can be confusing, so we refer to the first argument of any method as the object reference argument, or simply the object reference. Methods must use the object reference to access attributes and other methods that belong to the class. By convention, the object reference argument is called self.

Failure to specify an object reference (usually called self) as the first parameter in a method definition causes fatal logic errors when the method is invoked at runtime.

Python inserts the first (object reference) argument into every method call, including a class’s constructor call. 


Python Constructor Method of a Class

Cited from the book "Python How to Program"

The special method __init__ is the constructor method of the class. A constructor is a special method that executes each time an object of a class is created. The constructor (method __init__) initializes the attributes of the object and returns None. Python classes may define several other special methods, identified by leading and trailing double-underscores (__) in the name.

Returning a value other than None from a constructor is a fatal, runtime error.
 

Python Documentation String

Cited from the book "Python How to Program"

If a class contains a optional documentation string -- a string that describes the class, the string must appear in the line or lines following the class header. A user can view a class’s documentation string by executing the following statement.

print ClassName.__doc__

Modules, methods and functions also may specify a documentation string.

By convention, docstrings are triple-quoted strings. This convention allows the class author
to expand a program’s documentation (e.g., by adding several more lines) without having to
change the quote style.


How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04

How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04

How to Set Up a Dedicated Web Server for Free

Enable CGI on an Apache server

DIY: Enable CGI on your Apache server


Wednesday 11 July 2018

Python List DeepCopy

b_list = a_list[:] # deep copy

Python Dictionary ShallowCopy and DeepCopy

a_dict = dict.copy() # shallow copy

from copy import deepcopy
b_dict = deepcopy(dict) # deep copy

Python Methods

Cited from the book "Python How to Program"

All Python data types contain at least three properties: a value, a type and a location. Some Python data types (e.g., strings, lists and dictionaries) also contain methods. A method is a function that performs the behaviors (tasks) of an object.

To invoke the list method, specify the name of the list, followed by the dot (.) access operator, followed by the method call (i.e., method name and necessary arguments).

Function Arguments

Cited from the book "Python How to Program"

Default arguments must appear to the right of any non-default arguments in a function’s parameter list. When calling a function with two or more default arguments, if an omitted argument is not the rightmost argument in the argument list, all arguments to the right of that argument also must be omitted.

Python arguments are always passed by object reference—the function receives references to the values passed as arguments. In practice, pass-by-object-reference can be thought of as a combination of pass-by-value and pass-by-reference. If a function receives a reference to a mutable object (e.g., a dictionary or a list), the function can modify the original value of the object. It is as if the object had been passed by reference. If a function receives a reference to an immutable object (e.g., a number, a string or a tuple, whose elements are immutable values), the function cannot modify the original object directly. It is as if the object had been passed by value.






Python Namespace

Cited from the book "Python How to Program"

Namespaces store information about an identifier and the value to which it is bound. Python defines three namespaces: local, global and built-in. When a program attempts to access an identifier’s value, Python searches the namespaces in a certain order—local, global and built-in namespaces—to see whether and where the identifier exists.

If the function’s local namespace does not contain an identifier named x (e.g., the function does not define any parameters or create any identifiers named x), Python searches the next outer namespace: the global namespace (sometimes called the module namespace).

The global namespace contains the bindings for all identifiers, function names and class names defined within a module or file. Each module or file’s global namespace contains an identifier called __name__ that states the module’s name (e.g., "math" or "random"). When a Python interpreter session starts or when the Python interpreter begins executing a program stored in a file, the value of __name__ is "__main__".

The built-in namespace contains identifiers that correspond to many Python functions and error messages. For example, functions raw_input, int and range belong to the built-in namespace. Python creates the built-in namespace when the interpreter starts, and programs normally do not modify the namespace (e.g., by adding an identifier to the namespace). Identifiers contained in built-in namespaces may be accessed by code in programs, modules or functions.

Python provides a way for programmers to determine what identifiers are available from the current namespace. Built-in function dir returns a list of these identifiers.

Python provides many other introspective capabilities, including functions globals and locals that return additional information about the global and local namespaces, respectively.

>>> import math
>>> dir()
['__builtins__', '__doc__', '__name__', 'math']
>>> print math
<module 'math' (built-in)>
>>> dir( math )
['__doc__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil',
'cos', 'cosh','e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hy-
pot', 'ldexp', 'log', 'log10','modf', 'pi', 'pow', 'sin', 'sinh',
'sqrt', 'tan', 'tanh']

Python provides the from/import statement to import one or more identifiers from a module directly into the program’s namespace.

When the interpreter executes the line

from math import sqrt

the interpreter creates a reference to function math.sqrt and places the reference directly into the session’s namespace. Now, we can call the function directly without using the dot operator.

>>> from math import sqrt
>>> dir()
['__builtins__', '__doc__', '__name__', 'sqrt']
>>> sqrt( 9.0 )
3.0
>>> from math import sin, cos, tan
>>> dir()
['__builtins__', '__doc__', '__name__', 'cos', 'sin', 'sqrt',
'tan']

The statement

from math import *

imports all identifiers that do not start with an underscore from the math module into the interactive session’s namespace.

>>> import random as randomModule
>>> dir()
['__builtins__', '__doc__', '__name__', 'randomModule']
>>> randomModule.randrange( 1, 7 )
1
>>> from math import sqrt as squareRoot
>>> dir()
['__builtins__', '__doc__', '__name__', 'randomModule', 'square-
Root']
>>> squareRoot( 9.0 )
3.0






Python None

Cited from the book "Python How to Program"

None is a Python value that represents null—indicating that no value has been declared—and evaluates to false in conditional expressions.


Importing Functions from a Module

Cited from the book "Python How to Program"

To use a function that is defined in a module, a program must import the module, using keyword import. After the module has been imported, the program can invoke functions in that module, using the module’s name, a dot (.) and the function call (i.e., moduleName.functionName()).

Program Components in Python

Cited from the book "Python How to Program"

"""
Program components in Python are called functions, classes, modules and packages. Typically, Python programs are written by combining programmer-defined (programmer-created) functions and classes with functions or classes already available in existing Python modules. A module is a file that contains definitions of functions and classes. Many modules can be grouped together into a collection, called a package.
"""

Tuesday 10 July 2018

Returned Value of And Operator

Cited from the book "Python How to Program"

If a combined condition evaluates to false, the and operator returns the first value which evaluated to false. Conversely, if the combined condition evaluates to true, the and operator returns the last value in the condition.

Python Tips

Cited from the book "Python How to Program"

python -i program.py enters interactive mode after execution completion.

from __future__  import division
'/' : true division
'//': floor division

An expression containing and or or operators is evaluated until its truth or falsity is known. This is called short circuit evalutation.

Normally, control passes through the statement. If a print statement includes a function call without parentheses, it displays the memory location of the function. If the user intends to assign the result of a function call to a variable, a function call without parentheses binds the function itself to the variable.

Friday 6 July 2018

Installing Ruby on Rails in Ubuntu

Cited from Coursera Ruby on Rail Introduction

sudo apt-get install git gitk git-gui

sudo apt-get install gcc build-essential libpq-dev libssl-dev libreadline-dev libsqlite3-dev zlib1g-dev

git clone git@github.com:rbenv/rbenv.git .rbenv

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc


Ruby on Rail Relevant Software

Cited from Coursera Ruby on Rails Introduction

Ruby: language interpreter
Rails: web application platform and dependencies
PhantomJS: headless Web testing support