cytoscape / py4cytoscape

Python library for calling Cytoscape Automation via CyREST
https://Py4Cytoscape.readthedocs.io
Other
69 stars 15 forks source link

Request py4cytoscape version of radial label automation from RCy3 #103

Closed gmhhope closed 1 year ago

gmhhope commented 1 year ago

Hi py4cytoscape developer,

Thanks for developing all these tools to automate Cytoscape network.

Do you have a handy python version of the R code shared previously by Alex? I am trying to migrate to python but I really like this script and am trying to completely migrate from RCy3 to python as I am doing networkx more often in python now.

#radial labels on circle layout

library(RCy3)

makeLabelsRad <- function(additional.offset = 0,
                          font.factor = 4,
                          network = NULL){

  #collect node positions and centroid
  net.suid <- getNetworkSuid(NULL)
  node.pos.list <- getNodePosition()
  node.pos.list$x_location <- as.numeric(node.pos.list$x_location)
  node.pos.list$y_location <- as.numeric(node.pos.list$y_location)
  net.center <- list(x_location=mean(node.pos.list$x_location),y_location=mean(node.pos.list$y_location))
  center.mat <- as.matrix(c(-1,0))
  #for each node in network
  for(n in 1:nrow(node.pos.list)){
    #calculate node label rotation and position
    node.name <- rownames(node.pos.list)[[n]]
    node.mat <- as.matrix(c(node.pos.list$x_location[[n]] - net.center$x_location, node.pos.list$y_location[[n]] - net.center$y_location))
    theta.rad <- acos( sum(node.mat*center.mat) / ( sqrt(sum(node.mat * node.mat)) * sqrt(sum(center.mat * center.mat)) ) ) 
    theta <- theta.rad * 180 / pi
    node.size <- unname(getNodeSize(node.name))
    node.label.len <- nchar(unname(getNodeProperty(node.name, "NODE_LABEL")))
    offset.x.trig <- cos(theta.rad)
    offset.x <- -offset.x.trig * (font.factor * node.label.len + 0.5 * node.size + additional.offset)
    offset.y.trig <- sin(theta.rad)
    offset.y <- -offset.y.trig * (font.factor * node.label.len + 0.5 * node.size + additional.offset)
    if(node.mat[1,1]>0){
      theta <- theta + 180
    }
    if(node.mat[2,1]>0){
      theta <- -theta 
      offset.y <- -offset.y
    }
    node.lab.pos <- paste(c("C","C","c",offset.x,offset.y), collapse = ",")
    #set node label rotation and position
    RCy3::setNodePropertyBypass(node.name, theta, "NODE_LABEL_ROTATION")
    RCy3::setNodePropertyBypass(node.name, node.lab.pos, "NODE_LABEL_POSITION")
  }
}

Hope you could help!

Thanks very much!

Best, Minghao Gong

bdemchak commented 1 year ago

I'll be glad to help ... right now, I'm traveling. I'll give it a shot tonight. If you need it sooner, please consult the py4cytoscape. readthedocs.org manual ... the RCy3 calls are very close to the py4cytoscape calls. Just use the Search bar or browse the Reference tab.

... sent from my mobile phone ... please forgive my typos.

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv : Barry Demchak, PhD : : Torrey Pines Software : : http://www.tpsoft.com/ : : http://orcid.org/0000-0001-7065-7786 : : (858) 452-8700 : : (619) 218-3717 (cell/text) : vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

On Wed, Jun 7, 2023, 12:17 PM Minghao Gong @.***> wrote:

Hi py4cytoscape developer,

Thanks for developing all these tools to automate Cytoscape network.

Do you have a handy python version of the R code shared previously by Alex? I am trying to migrate to python but I really like this script and am trying to completely migrate from RCy3 to python as I am doing networkx more often in python now.

radial labels on circle layout

library(RCy3) makeLabelsRad <- function(additional.offset = 0, font.factor = 4, network = NULL){

collect node positions and centroid

net.suid <- getNetworkSuid(NULL) node.pos.list <- getNodePosition() node.pos.list$x_location <- as.numeric(node.pos.list$x_location) node.pos.list$y_location <- as.numeric(node.pos.list$y_location) net.center <- list(x_location=mean(node.pos.list$x_location),y_location=mean(node.pos.list$y_location)) center.mat <- as.matrix(c(-1,0))

for each node in network

for(n in 1:nrow(node.pos.list)){

calculate node label rotation and position

node.name <- rownames(node.pos.list)[[n]]
node.mat <- as.matrix(c(node.pos.list$x_location[[n]] - net.center$x_location, node.pos.list$y_location[[n]] - net.center$y_location))
theta.rad <- acos( sum(node.mat*center.mat) / ( sqrt(sum(node.mat * node.mat)) * sqrt(sum(center.mat * center.mat)) ) )
theta <- theta.rad * 180 / pi
node.size <- unname(getNodeSize(node.name))
node.label.len <- nchar(unname(getNodeProperty(node.name, "NODE_LABEL")))
offset.x.trig <- cos(theta.rad)
offset.x <- -offset.x.trig * (font.factor * node.label.len + 0.5 * node.size + additional.offset)
offset.y.trig <- sin(theta.rad)
offset.y <- -offset.y.trig * (font.factor * node.label.len + 0.5 * node.size + additional.offset)
if(node.mat[1,1]>0){
  theta <- theta + 180
}
if(node.mat[2,1]>0){
  theta <- -theta
  offset.y <- -offset.y
}
node.lab.pos <- paste(c("C","C","c",offset.x,offset.y), collapse = ",")
#set node label rotation and position
RCy3::setNodePropertyBypass(node.name, theta, "NODE_LABEL_ROTATION")
RCy3::setNodePropertyBypass(node.name, node.lab.pos, "NODE_LABEL_POSITION")

} }

Hope you could help!

Thanks very much!

Best, Minghao Gong

— Reply to this email directly, view it on GitHub https://urldefense.com/v3/__https://github.com/cytoscape/py4cytoscape/issues/103__;!!Mih3wA!E364byXnA4CrwwHKSdTHc-D4af4tKtamvDZpk8F3ogLlQvtjM1HBxxxhoZrotFvDiri7TmWZR--GQghuXPGEaYJJeg$, or unsubscribe https://urldefense.com/v3/__https://github.com/notifications/unsubscribe-auth/AA4GLXQQKJK5OHHM4PMTMCDXKCSR3ANCNFSM6AAAAAAY6DONR4__;!!Mih3wA!E364byXnA4CrwwHKSdTHc-D4af4tKtamvDZpk8F3ogLlQvtjM1HBxxxhoZrotFvDiri7TmWZR--GQghuXPGth6Na3A$ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

bdemchak commented 1 year ago

Hi --

I think I see what you're asking. Given that I'm travelling, I don't have any way to address anything substantial like what you're asking. My best advice would be what I've already written ... to consult the py4cytoscape documentation (which is quite extensive) and make the translation yourself. It shouldn't be very difficult, and you're in the best position to test and support the result. Best of luck!

gmhhope commented 1 year ago

Thanks,

I circumvent that by running !Rscript in between the python script.