Tutorials

The MSj package intends to provide an access to the common open source mass spectrometry file format using Julia.

The Julia language

Julia is an open source programming language designed for scientific and technical computing. This section will give a very brief introduction to the Julia language.

Note

See also the Julia language home page, the Wikibook introduction to Julia or Julia by example.

Installation

Julia binaries are available for various platforms and can be downloaded here. Plateform specific instructions may be found here.

Executing Julia code

Julia code can be executed interactively using the REPL as follow:

julia> println("Hello")
Hello

The println("Hello") line can be put in a file, such as hello.jl and executed as a script, which will produce the same output.

$ julia hello.jl

You can also put #!/usr/local/bin/julia in the first line of the hello.jl file, make it executable (chmod +x hello.jl) and execute it like any other executable.

Finally, Julia scripts can be executed within Jupyter Notebooks, see the dedicated section Jupyter notebooks.

Types

Julia is a strongly typed language ( see the julia wikibook) In Julia types are organized in a hierarchy with a tree structure. The root of the tree is the Any type. The Number type is a direct child of Any and possesses two subtypes: Complex and Real. The Real type has four types: Integer, AbstractFloat, Irrational and Rational. The exact number hierarchy is as follow:

Number
 ↳ Complex
 ↳ Real
    ↳ AbstractFloat 
	   ↳ BigFloat 
	   ↳ Float64 
	   ↳ Float32 
	   ↳ Float16
    ↳ Integer
	   ↳ BigInt
	   ↳ Bool
	   ↳ Signed
	      ↳ Int128 
		  ↳ Int64 
		  ↳ Int32 
		  ↳ Int16 
		  ↳ Int8
	   ↳ Unsigned
	      ↳ UInt128 
		  ↳ Int64 
		  ↳ UInt32 
		  ↳ UInt16 
		  ↳ UInt8
	↳ Irrational
	↳ Rational

Creating vectors and matrices

A vector is created as follow

julia> A = [1, 2, 3]                  # vector
3-element Array{Int64,1}:
 1
 2
 3

julia> A = range(1, 10, step = 2)   # linearly spaced
1:2:9

julia> A = range(1, 10, length = 5) # linearly spaced
1.0:2.25:10.0

julia> A = rand(10)                 # random with 10 elements
10-element Array{Float64,1}:
 0.5314360103707141
 0.17160837878247404
 0.18246062346281655
 0.15164369722236537
 0.23339844201416948
 0.872631343638469
 0.5845969633213612
 0.21901239917465842
 0.5401322135291562
 0.2497960285335421

julia> j = 2; k = 2; n = 10;

julia> A = j:k:n                    # from j to n with step size k
2:2:10

and similarly for matrices

julia> A = [1 2; 3 4]               # matrix
2×2 Array{Int64,2}:
 1  2
 3  4

julia> A = rand(2, 2)               # random 2x2 matrix
2×2 Array{Float64,2}:
 0.0732623  0.0061409
 0.990642   0.991486

Vector and matrices can be manipulated as follow:

transpose(A)                 # Return the transpose of A
A[:]                         # Flatten matrix A (convert matrix to vector)
A[2,2]                       # Accessing element at row 2 and colomun 2
A[1:4, :]                    # Accessing specific rows 1 to 4

Vector and matrix may be preallocated like this:

julia> A = rand(5)                  # a vector / matrix
5-element Array{Float64,1}:
 0.9795706743604828
 0.6364677917509833
 0.2152346151121738
 0.4684285896683389
 0.2984260929017839

julia> B = similar(A)               # an emply vector / matrix similar to A
5-element Array{Float64,1}:
 6.91924744329265e-310
 6.9192474432958e-310
 6.91924744329897e-310
 6.91924744330213e-310
 6.9192474433053e-310

Operations

The following present a few example of operations:

julia> dot(A,B)                     # dot product between A and B 
julia> A .* B                       # Element wise multiplication
julia> A * B                        # Matrix multiplcation
julia> norm(A)                      # Euclidian norm
julia> sum(A, dims = 1)             # sum over each column
julia> sum(A, dims = 2)             # sum over each rows

Loops

A loop in Julia can be done like this:

julia> for i in 1:N
julia>   #do something
julia> end

While loops my be achiçved like this:

julia> while i <= N
julia>   #do something
julia> end

and if / else flow like this:

julia> if i < N
julia>   #do something
julia> else 
julia>   #do something else
julia> end

Functions and methods

A function is defined like this:

julia> function f(x)
julia>   return x^2
julia> end

which can be simplified as:

julia> f(x) = x^2

Broadcasting a function over a collection or an Array is achieved like this:

julia> f(x) = x^2
julia> x = 1:10
julia> f.(x)

In Julia, functions that modify their arguments are named !, such as:

julia> function f!(out, x)
julia>   out = x.^2
julia> end
julia> x = rand(10)
julia> y = similar(x)
julia> f!(y, x)

Importing and using Packages

The Julia code is organized into files, modules and packages. A file using the .jl extension contains julia code. Related functions and variable may be gathered in modules. One or more modules may be organized into packages. To use a package, it has to be called like this:

julia> using A_package

If it is not installed, before being used:

julia> using Pkg                      # using the Package manager package
julia> Pkg.add("A_package")
julia> using A_package

Now every public function which is made available by the package A_package is available directly. Private functions have to be called like this:

julia> a_public_function()                   # Calling a public function
julia> A_package.a_private_function()        # Calling private functions

The same is true for the variables defined in the packages.

Jupyter notebooks

Jupyter notebooks are web based documents that may contains both codes, figures and other textual elements (such as equations, links, ...). Jupyter notebook may be installed easily using Julia:

julia> using Pkg
julia> Pkg.add("IJulia")

When IJulia is installed, then a notebook may be launch like this:

julia> using IJulia
julia> notebook()

The notebook() function should launch a web browser from which a new notebook may be started. On each entry of the notebook code, Markdown or text may be inserted. Each line of code may be executed and will eventually return a result. In the following, the tutorials are given in Jupyter notebook form and can be viewed using nbviewer.

The MSj package

Loading and plotting mass spectrometry data

This tutorial shows how to use how to import data and how to plot mass spectra.

Loading and plotting data

Filtering and averaging

This notebook shows how to filter and average data.

Filtering and averaging data

Extracting data from several files

This tutorial gives an example how to extract UV spectroscpy data from different files containing UV activation at different wavelengths.

Extracting data from several files

Isotopic distributions

Here we will calculate the isotopic distribution of a compound, simulate a mass spectrum from that distribution and compare this result to experimental data.

Isotopic Distributions