# rcyTutorial/R/go.R
# put all the package-specific initializations here, which guarantees they get done when the package is attached via
# the library (rcyTutorial) command
.onAttach = function (libname, pkgname)
{
}
#------------------------------------------------------------------------------------------------------------------------
cy = CytoscapeConnection ()
#------------------------------------------------------------------------------------------------------------------------
printf = function (fmt, ...) print (noquote (sprintf (fmt, ...)))
#------------------------------------------------------------------------------------------------------------------------
demo.00 = function ()
{
  if (!exists ('cy'))
    cy <<- CytoscapeConnection ()

  printf ('CytoscapeRPC plugin version: %s', version (cy))
  printf ('successful connection to Cytosape')
  printf ('---- current Cytoscape windows:')
  current.windows = as.character (getWindowList (cy))
  if (length (current.windows) > 0)
    for (window.name in current.windows)
      printf ('    %s', window.name)

  return (cy)

} # demo.00
#------------------------------------------------------------------------------------------------------------------------
demo.01 = function ()
{
  cw = demoSimpleGraph ()
  return (cw)

} # demo.01
#------------------------------------------------------------------------------------------------------------------------
demo.02 = function ()
{
  g = makeSimpleGraph ()
  printf ('--- created graph: ')
  print (g)
  return (g)

} # demo.02
#------------------------------------------------------------------------------------------------------------------------
demo.03 = function ()
{
  g = makeSimpleGraph ()

  printf ('--- nodes in g')
  print (nodes (g))

  printf ('--- edges in g')
  print (edges (g))

  nodeAttributeNames = noa.names (g)
  printf ('--- node attribute names:')
  for (name in nodeAttributeNames)
    printf ('  %s', name)

  edgeAttributeNames = eda.names (g)
  printf ('--- edgeattribute names:')
    for (name in edgeAttributeNames)
      printf ('  %s', name)

  return (g)

} # demo.03
##------------------------------------------------------------------------------------------------------------------------
demo.04 = function ()
{
  window.title = 'demo.4'

  if (window.title %in% as.character (getWindowList (cy)))
      deleteWindow (cy, window.title)
  g = makeSimpleGraph ()
  cw = new.CytoscapeWindow (window.title, g)
  displayGraph (cw)
  layout (cw, 'jgraph-spring')
  setNodeLabelRule (cw, 'label')
  redraw (cw)

  return (cw)

} # demo.04
#------------------------------------------------------------------------------------------------------------------------
demo.05 = function (cw)
{
  nodeAttributeNames = noa.names (cw@graph)
  print (nodeAttributeNames)
  for (name in nodeAttributeNames) {
    setNodeLabelRule (cw, name)
    Sys.sleep (3)
    }

  return (cw)

} # demo.05
#------------------------------------------------------------------------------------------------------------------------
demo.06 = function (cw)
{
  nodeAttributeNames = noa.names (cw@graph)
  print (nodeAttributeNames)
  print (noa (cw@graph, 'count'))

  for (i in 1:2) {
    setNodeSizeRule (cw, 'count', c (1, 50, 100), c (100, 20, 5), mode='interpolate')
    Sys.sleep (2)
    setNodeSizeRule (cw, 'count', c (1, 50, 100), c (5, 80, 120), mode='interpolate')
    Sys.sleep (2)
    }

  return (cw)

} # demo.06
#------------------------------------------------------------------------------------------------------------------------
demo.07 = function (cw)
{
  nodeAttributeNames = noa.names (cw@graph)
  print (noa (cw@graph, 'type'))

  for (i in 1:2) {
    setNodeSizeRule (cw, 'type', c ('kinase', 'glycoprotein', 'transcription factor'), c (40, 60, 80), mode='lookup')
    Sys.sleep (2)
    setNodeSizeRule (cw, 'type', c ('kinase', 'glycoprotein', 'transcription factor'), c (20, 80, 100), mode='lookup')
    Sys.sleep (2)
    }

  return (cw)

} # demo.07
#------------------------------------------------------------------------------------------------------------------------
demo.08 = function (cw)
{
  for (i in 1:2) {
    setNodeSizeDirect (cw, 'A', 45)
    setNodeSizeDirect (cw, 'B', 55)
    setNodeSizeDirect (cw, 'C', 65)
    redraw (cw)
    Sys.sleep (1)
    setNodeSizeDirect (cw, 'A', 100)
    setNodeSizeDirect (cw, 'B', 110)
    setNodeSizeDirect (cw, 'C', 120)
    redraw (cw)
    Sys.sleep (1)
    }

  return (cw)

} # demo.08
#------------------------------------------------------------------------------------------------------------------------
demo.09 = function ()
{
  g = new ('graphNEL', edgemode='directed')

  g = initNodeAttribute (g, 'type', 'char', 'undefined')
  g = initNodeAttribute (g, 'label', 'char', 'default node label')

  g = initEdgeAttribute (g, 'edgeType', 'char', 'undefined')
  g = initEdgeAttribute (g, 'score', 'numeric', 0.0)

  g = addNode (c ('A', 'B', 'C'), g)

  nodeData (g, c ('A', 'B', 'C'), attr='type') = c ('kinase', 'transcription factor', 'glycoprotein')
  nodeData (g, c ('A', 'B', 'C'), 'label') = c ('Gene A', 'Gene B', 'Gene C')

  g = addEdge ('A', 'B', g)
  g = addEdge ('B', 'C', g)
  g = addEdge ('C', 'A', g)

  edgeData (g, 'A', 'B', 'edgeType') = 'phosphorylates'
  edgeData (g, 'B', 'C', 'edgeType') = 'regulates'

  edgeData (g, c ('A', 'B'), c ('B', 'C'), attr='score') = c (35.0, -12)

  return (g)

} # demo.09
#------------------------------------------------------------------------------------------------------------------------
demo.10 = function ()
{
  g = demo.09 ()
  print (noa.names (g))
  print (eda.names (g))

  for (noa.name in noa.names (g)) {
    print (paste ('-----', noa.name))
    print (noa (g, noa.name))
    }

  for (eda.name in eda.names (g)) {
    print (paste ('-----', eda.name))
    print (eda (g, eda.name))
    }

  cy = CytoscapeConnection ()
  window.title = 'demo.10'

  if (window.title %in% as.character (getWindowList (cy)))
      deleteWindow (cy, window.title)

  cw = new.CytoscapeWindow (window.title, g)
  displayGraph (cw)
  layout (cw, 'jgraph-spring')

  setNodeLabelRule (cw, 'label')

  print (getArrowShapes (cy))
  
  setDefaultEdgeLineWidth (cw, 3)
  setEdgeTargetArrowRule (cw, 'edgeType', c ("phosphorylates", "regulates", "undefined"), c ('Arrow', 'Arrow', 'No Arrow')) 
 
  print (getLineStyles (cy))
  setEdgeLineStyleRule (cw, 'edgeType', c ("phosphorylates", "regulates", "undefined"), c ('SOLID', 'SINEWAVE', 'DOT'))
 
  setEdgeColorRule (cw, 'edgeType', c ("phosphorylates", "regulates", "undefined"), c ('#FF0000', '#00DD00', '#000000'), mode='lookup')

  print (getNodeShapes (cy))
  setNodeShapeRule (cw, 'type', c ("kinase", "transcription factor", "glycoprotein"), c ('ellipse', 'rect', 'triangle'))
 
  redraw (cw)
  
  return (cw)

} # demo.10
#------------------------------------------------------------------------------------------------------------------------
demo.11 = function ()
{
  base.dir = system.file (package='rcyTutorial')
  full.pathname = paste (base.dir, 'extdata', "lungCancerCNVphospho.cys", sep='/')
  return (full.pathname)

} # demo.11
#------------------------------------------------------------------------------------------------------------------------
demo.12 = function (copy.graph)
{
  cy = CytoscapeConnection ()
  window.title = 'CNV + KEGG'

  stopifnot (window.title %in% as.character (getWindowList (cy)))

  cw = existing.CytoscapeWindow (window.title, copy.graph.from.cytoscape.to.R=copy.graph)
  return (cw)

} # demo.12
#------------------------------------------------------------------------------------------------------------------------
demo.13 = function (cw)
{
  hideAllPanels (cw)
  showGraphicsDetails (cw, TRUE)
  glc = cw@graph
  total.phosphorylation = as.numeric (noa (glc, 'total.phosphorylation'))
  print (fivenum (total.phosphorylation))
  hist (total.phosphorylation)
  high.tp.nodes = names (which (noa (glc, 'total.phosphorylation') > 500))
  clearSelection (cw)
  selectNodes (cw, high.tp.nodes)
  
} # demo.13
#------------------------------------------------------------------------------------------------------------------------
