Friday, 7 August 2015

Circulant graphs

A set of graphs that have some symmetric properties are the circulant graphs, which are defined by an integer \(n\) and a subset, \(C\), of the integers \(1, 2, ..., n-1\).
These are graphs over \(n\) vertices, which we take to be numbered from \(1\) to \(n\), where two vertices i and j are connected provided \(j-i\in{C}\), under arithmetic modulo \(n\) (or equivalently, since the graph is undirected, if \(i-j\in{C}\)).

See Godsil and Royle for further info.

We can construct a circulant graph in R using the following function:

make_circulant_graph <- function(n, C){
    # R function to construct a circulant graph using 'igraph'
    # See Godsil and Royle for the definition of a circulant graph
    # Note that the function uses igraph naming convention make_xyz<_graph>()
    # n is the number of vertices (here numbered from 1 to n)
    # C is a set of values
    # - Let i and j be numbered vertices (min=1, max=n)
    # - Then i and j are linked by an undirected edge if (j-i mod n) is in C
    require('igraph')
    if(n==0){
        return(make_ring(n = 0))
        }
    stopifnot(n>0)
    stopifnot(all(C %in% 1:(n-1)))
    edgelist <- lapply(1:n, function(i){
        modulo <- ((1:n) - i) %% n
        neighbours <- which(modulo %in% C)
        as.vector(t(cbind(i, neighbours)))
        })
    g <- simplify(
        make_graph(unlist(edgelist), n = n, directed = FALSE)
        )
    return(g)
    }

A cycle graph is a circulant graph so we should be able to construct the cycle-graph on 4 vertices that was made in an earlier post using igraph::make_ring using the following code:

g1 <- make_circulant_graph(n = 4, C = 1)
plot(g1)


Also, we can make graphs that are a bit more interesting:
g2 <- make_circulant_graph(n = 10, C = c(1, 3, 4))
plot(g2)
The plotted graph doesn't illuminate the symmetry of the graph, though. So we place the vertices in order around an invisible circle:
trigseq <- (2/10)*pi*(1:10)
g2$layout <- cbind(sin(trigseq), cos(trigseq))
plot(g2)

Thursday, 6 August 2015

Getting R/igraph installed

From a new linux-mint 17.2 install on my home computer:

R was missing, so I installed it using apt-get. The version that was installed by apt-get was 3.0.2, somewhat dated relative to the R-3.2.1 release that is currently available for download. So I deleted the apt-get installed version, and installed R-3.2.1. This required that x11 dev libraries and java7 dev kit were both available (they weren't to begin with). For completion, I wanted to create the R manuals and so had to install a few tex/latex things.

R
# The program 'R' is currently not installed. You can install it by typing:
# sudo apt-get install r-base-core

sudo apt-get install r-base-core 

Recommended packages:
sudo apt-get install r-recommended r-base-dev r-doc-html

Suggested packages:
sudo apt-get install ess r-doc-info r-doc-pdf r-mathlib \
    r-base-html tcl-tclreadline

R
# R is version 3.0.2


We remove R:
sudo apt-get remove r-base-core r-recommended r-base-dev \
    r-doc-html r-doc-info r-doc-pdf r-mathlib r-base-html

We download the latest version of R (R3.2.1) and install it
mkdir ~/Tools
cd ~/Tools
wget https://www.stats.bris.ac.uk/R/src/base/R-3/R-3.2.1.tar.gz
tar -xzvf R-3.2.1.tar.gz  
cd R-3.2.1/
./configure
# configure: error: --with-x=yes (default) and X11 headers/libs are not available

# installed x11 libraries
sudo apt-get install xorg-dev
./configure

# configure: WARNING: you cannot build info or HTML versions of the R manuals
# configure: WARNING: you cannot build PDF versions of the R manuals
# configure: WARNING: you cannot build PDF versions of vignettes and help pages

# installed texi2any etc
sudo apt-get install texinfo
./configure

# configure: WARNING: you cannot build PDF versions of the R manuals
# configure: WARNING: you cannot build PDF versions of vignettes and help pages

# install pdftex
sudo apt-get install texlive
./configure

# configure: WARNING: neither inconsolata.sty nor zi4.sty found: \
# PDF vignettes and package manuals will not be rendered optimally

# installed further texlive styles
# inconsolata and zi4 are present in texlive-fonts-extra
sudo apt-get install texlive-fonts-extra \
                     texlive-fonts-recommended

# install R
./configure
sudo make

# Failed:
# trying to compile and link a JNI program 
# detected JNI cpp flags    : 
# detected JNI linker flags : -L$(JAVA_HOME)/lib/i386/client -ljvm
# make[2]: Entering directory `/tmp/Rjavareconf.EIravR'
# gcc -std=gnu99 -I/home/russ/Tools/R-3.2.1/include -DNDEBUG  -I/usr/local/include    -fpic  -g -O2  -c conftest.c -o conftest.o
# conftest.c:1:17: fatal error: jni.h: No such file or directory
#  #include <jni.h>
                 ^
# compilation terminated.
# make[2]: *** [conftest.o] Error 1
# make[2]: Leaving directory `/tmp/Rjavareconf.EIravR'
# Unable to compile a JNI program


# JAVA_HOME        : /usr/lib/jvm/java-7-openjdk-i386/jre
# Java library path: 
# JNI cpp flags    : 
# JNI linker flags : 
# Updating Java configuration in /home/russ/Tools/R-3.2.1
# Done.

# installed java7 dev kit:
sudo apt-get install openjdk-7-jdk
./configure
sudo make

make check
make pdf
make info

sudo make install
sudo make install-info
sudo make install-pdf

# in a new terminal tab:
R
> install.packages('igraph')
Warning in install.packages("igraph") :
  'lib = "/usr/local/lib/R/library"' is not writable
Would you like to use a personal library instead?  (y/n) y

Would you like to create a personal library
~/R/i686-pc-linux-gnu-library/3.2
to install packages into?  (y/n) y

# use http version of UK::Bristol
library(igraph)


Wednesday, 5 August 2015

Plotting graphs

Within R, the library igraph provides a neat way to store, manipulate, analyse and plot graph datastructures. 
install.packages('igraph')
library('igraph')

An undirected cycle graph on four vertices can be made like so:
g <- make_ring(4)

Then we can plot out the graph using igraph's default settings:
plot(g)

The position of the various nodes can be fixed using g$layout
g$layout <- matrix(
  c(1,1, 1,2, 2,2, 2,1), 
  ncol = 2,
  byrow = TRUE
  )

So the first node would be at (1,1), the second at (1,2) etc; and upon plotting, the graph edges are now aligned to the axes:
plot(g)


The images are good enough for me at the moment, although I'm not keen on base-R graphics. Fortunately, someone else has already sussed out how to plot igraphs with ggplot2.


Tuesday, 4 August 2015

Graph theory and biological networks

*Graphs of binary relations between things, if not graph theory itself so much, crop up throughout biology. Sometimes we don't stick rigorously to dusty terminology, so sometimes they're called networks, sometimes they ought to be called multigraphs. But they're a neat way to represent protein-protein interactions, gene rearrangements, molecular structures, phylogeny, inheritance, and a ton of other things.

Formal boring stuff that's been written a million times: a graph \(G=(V, E)\) is an ordered pair of sets: \(V\), a set of vertices (or nodes), and \(E\), a set of edges (interactions, links, relations, ...) each of which joins a pair \(v_1, v_2\) of vertices from \(V\).

As an example, the human interactome is a graph with \(V\) being the set of human proteins and \(E\) the interactions that occur between those proteins. In the interactome, the edges that connect proteins typically have no directionality: SNAP25 binds to STX1A and STX1A binds to SNAP25. So, for the interactome at least, it makes no difference whether an edge in \(E\) between proteins \(a\) and \(b\) is written \(a-b\) or \(b-a\), indeed we can equate the two representations. That is, the interactome is an undirected graph.

Not all binary relations are undirected though: for example, if edges represented the phosphorylation of a substrate by a protein kinase then the graph (and the edges within it) would necessarily be directed.

To be continued..


* early attempts to get mathjax to work should be taken with good humour

Monday, 3 August 2015

Graph Theory: Terms to remember

From AGT(1.1-1.2) and MGT(I.1)
Definitions encountered in graph theory:

~ Basic graph-related definitions
(undirected) graph
directed graph
simple graph
vertex (set)
edge (set)
arc (set)
path
cycle
walk
trail
circuit
(dis)connected
order, |G|
size, e(G)
degree sequence
distance
cutvertex
bridge
loop

~ Relations between components of graphs
adjacency
incidence
neighbour
(open / closed) neighbourhood
(out / in) degree

~ Relations between graphs
equality / isomorphism
subgraph
complement

~ Subgraph-related definitions
spanning subgraph
induced subgraph
clique
independent vertex set
independent path set
connected component
(oriented graph) - not strictly a subgraph

~ Types of graph
complete
k-regular
empty
null
cyclic
acyclic
tree
spanning tree
forest
bipartite / r-partite
(hypergraph) - not strictly a graph
(multigraph) - not strictly a graph

Testing MathJax

Put the following into the <head>...</head> section of the biomathr default html:
<script type="text/javascript"
  src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
And now we can embed LateX like this \(n^{n}\) and this:
$$abc=123$$

Intro

I'm a biologist who loves maths. These are my notes about mathematics. They might not relate to overtly 'biological' mathematics subjects. 

I'm currently (2015-2016) doing a masters dissertation on algebraic graph theory based on Godsil & Royle's book of the same name, this will doubtless crop up a lot. Biological networks are pretty cool, anyway (when analysed in a statistically rigorous manner). But I'm going to write whatever I find interesting and that I might otherwise forget. At the moment, I'm working through a number of different applied maths and statistics books and these might also raise their heads.