require(igraph)
require(graphstats)
require(gridExtra)
require(ggplot2)
# define this function
load.data <- function(dat)
{
  e <- new.env()
  name <- data(list=dat, envir = e)[1]
  e[[name[[1]]]]
}
make_plots <- function(datname, title, src.name, tgt.name, attr.code, src.attr.name,
                       tgt.attr.name, wt, edge.xfm) {
  # Adjacency Matrix
  g <- load.data(datname)
  adj.plot <- gs.plot.plot_adjacency(g, title=title, edge.attr = wt, src.label=src.name,
                                     tgt.label=tgt.name, edge.xfm=edge.xfm)

  # Degree Sequence
  gr.degseq.in <- strength(g, mode="in")
  gr.degseq.out <- strength(g, mode="out")
  if (isTRUE(all.equal(as.numeric(gr.degseq.in), as.numeric(gr.degseq.out)))) {
    gr.deg.dat <- data.frame(vertex=names(V(g)), degree=gr.degseq.in, Direction="undirected")
  } else {
    gr.deg.dat <- rbind(data.frame(vertex=names(V(g)), degree=gr.degseq.in, Direction="in"),
                        data.frame(vertex=names(V(g)), degree=gr.degseq.out, Direction="out"))
  }
  deg.plot <- ggplot(gr.deg.dat, aes(x=degree, group=Direction, fill=Direction)) +
    geom_density(alpha=0.2) +
    xlab("Vertex Degree") +
    ylab("Probability") +
    ggtitle(title) +
    theme_bw()

  # SBM
  gr.sbm <- gs.sbm.fit(g, community.attribute=attr.code, edge.attr="weight")
  sbm.plot <- gs.plot.plot_adjacency(gr.sbm, title=title, src.label=src.attr.name,
                                     tgt.label=tgt.attr.name, vertex.label=TRUE, edge.attr="weight")
  return(list(adj.plot=adj.plot, deg.plot=deg.plot, sbm.plot=sbm.plot))
}

C Elegans, Gap, Male

celegans.male.gap <- make_plots("celegans.male.gap", "C Elegans, Gap, Male", "Source Neuron",
                                "Target Neuron", "type", "Source Neuron Type", "Target Neuron Type",
                                "weight", edge.xfm="log10")

C Elegans, Gap, Herm

celegans.herm.gap <- make_plots("celegans.herm.gap", "C Elegans, Gap, Herm", "Source Neuron",
                                "Target Neuron", "type", "Source Neuron Type", "Target Neuron Type",
                                "weight", edge.xfm="log10")

C Elegans, Chem, Male

celegans.male.chem <- make_plots("celegans.male.chem", "C Elegans, Chem, Male",
                                 "Source Neuron", "Target Neuron", "type", "Source Neuron Type",
                                 "Target Neuron Type", "weight", edge.xfm="log10")

C Elegans, Chem, Herm

celegans.herm.chem <- make_plots("celegans.herm.chem", "C Elegans, Chem, Herm", "Source Neuron",
                                 "Target Neuron", "type", "Source Neuron Type", "Target Neuron Type",
                                 "weight", edge.xfm="log10")

Comparisons

Adjacency Matrix

grid.arrange(celegans.male.gap$adj.plot, celegans.herm.gap$adj.plot, ncol=2)
grid.arrange(celegans.male.chem$adj.plot, celegans.herm.chem$adj.plot, ncol=2)

Degree Sequence

grid.arrange(celegans.male.gap$deg.plot, celegans.herm.gap$deg.plot, ncol=2)
grid.arrange(celegans.male.chem$deg.plot, celegans.herm.chem$deg.plot, ncol=2)

SBM

grid.arrange(celegans.male.gap$sbm.plot, celegans.herm.gap$sbm.plot, ncol=2)
grid.arrange(celegans.male.chem$sbm.plot, celegans.herm.chem$sbm.plot, ncol=2)


neurodata/graphstats documentation built on May 14, 2019, 5:19 p.m.