Giter Site home page Giter Site logo

Comments (8)

browaeysrobin avatar browaeysrobin commented on June 1, 2024 2

Hi @SergioRodLla @mariecrane @n318162971

I think I identified the issue, which seems to arise from a bug in the underlying ggraph::geom_edge_loop function. When there are no self-loops in the network, this gave an error on my test data.

We are currently improving the code on this, and several other aspects, which will be released soon in version 2 (currently on dev-branch, later on main branch once all new vignettes and code will be ensured to be compatible).

For the moment, using the following function should work.

make_ggraph_ligand_target_links = function(lr_target_prior_cor_filtered, prioritized_tbl_oi, colors){
  
  requireNamespace("dplyr")
  requireNamespace("ggplot2")
  requireNamespace("ggraph")
  
  lr_target_prior_cor_filtered = lr_target_prior_cor_filtered %>% dplyr::inner_join(prioritized_tbl_oi, by = c("sender", "receiver", "ligand", "receptor", "id", "group"))
  
  source_df_lr = prioritized_tbl_oi %>% dplyr::mutate(celltype_ligand = paste(sender, ligand, sep = "_"), celltype_receptor = paste(receiver, receptor, sep = "_")) %>% dplyr::select(group, sender, receiver, celltype_ligand, celltype_receptor, ligand, receptor) 
  source_df_lrt = lr_target_prior_cor_filtered %>% dplyr::mutate(celltype_ligand = paste(sender, ligand, sep = "_"), celltype_target = paste(receiver, target, sep = "_"), celltype_receptor = paste(receiver, receptor, sep = "_")) %>% dplyr::select(group, sender, receiver, celltype_ligand, celltype_receptor, celltype_target, ligand, target, receptor, direction_regulation) 
  
  lr_gr_network = dplyr::bind_rows(
    source_df_lrt %>% dplyr::filter(celltype_target %in% source_df_lr$celltype_ligand & !celltype_target %in% source_df_lr$celltype_receptor) %>% dplyr::mutate(type_target = "ligand"),
    source_df_lrt %>% dplyr::filter(celltype_target %in% source_df_lr$celltype_receptor  & !celltype_target %in% source_df_lr$celltype_ligand) %>% dplyr::mutate(type_target = "receptor")
  ) %>% dplyr::bind_rows(
    source_df_lrt %>% dplyr::filter(celltype_target %in% source_df_lr$celltype_ligand & celltype_target %in% source_df_lr$celltype_receptor) %>% dplyr::mutate(type_target = "ligand/receptor")
  )
  
  ligand_receptor_network = lr_gr_network %>% dplyr::filter(celltype_receptor %in% celltype_target) %>% dplyr::select(celltype_ligand, celltype_receptor, direction_regulation, group) %>% dplyr::distinct() %>% dplyr::rename(sender = celltype_ligand, receiver = celltype_receptor) %>% dplyr::mutate(type = "Ligand-Receptor", weight = 1)
  
  ligand_target_network = lr_gr_network %>% dplyr::select(celltype_ligand, celltype_target, direction_regulation, group) %>% dplyr::distinct() %>% dplyr::rename(sender = celltype_ligand, receiver = celltype_target) %>% dplyr::mutate(type = "Ligand-Target", weight = 1)
  
  links = ligand_target_network %>% dplyr::bind_rows(ligand_receptor_network) 
  nodes = lr_gr_network %>% dplyr::select(celltype_ligand, sender, ligand) %>% dplyr::rename(celltype = sender, node = celltype_ligand, gene = ligand) %>% dplyr::mutate(type_gene = "ligand") %>% dplyr::bind_rows(
    lr_gr_network %>% dplyr::select(celltype_receptor, receiver, receptor) %>% dplyr::rename(celltype = receiver, node = celltype_receptor, gene = receptor) %>% dplyr::mutate(type_gene = "receptor")
  ) %>% dplyr::bind_rows(
    lr_gr_network %>% dplyr::select(celltype_target, receiver, target, type_target) %>% dplyr::rename(celltype = receiver, node = celltype_target, gene = target, type_gene = type_target)
  ) %>% dplyr::distinct() %>% dplyr::filter(node %in% c(links$sender, links$receiver))
  
  double_nodes =  nodes %>% dplyr::group_by(node) %>% dplyr::count() %>% dplyr::filter(n > 1) %>% pull(node)
  nodes = dplyr::bind_rows(
    nodes %>% dplyr::filter(node %in% double_nodes) %>% dplyr::mutate(type_gene = "ligand/receptor") ,
    nodes %>% dplyr::filter(!node %in% double_nodes)
  ) %>% dplyr:: distinct()
  
  nodes = nodes %>% data.frame() %>% magrittr::set_rownames(nodes$node)
  
  colors_regulation = NULL
  colors_regulation["up"] = "indianred1"
  colors_regulation["down"] = "steelblue2"
  
  # create the network object
  network = igraph::graph_from_data_frame(d=links %>% dplyr::filter(type == "Ligand-Target"), vertices = nodes, directed=T)
  graph = tidygraph::as_tbl_graph(network) 
  set.seed(1919)
  plot =  ggraph::ggraph(graph, layout = 'dh') + 
    ggraph::geom_edge_fan(aes(color = direction_regulation), edge_width = 1, arrow = arrow(length = unit(3, 'mm')), end_cap = ggraph::circle(5.5, 'mm'), start_cap = ggraph::circle(3, 'mm')) + 
    # ggraph::geom_edge_loop(aes(color = direction_regulation), edge_width = 1, alpha = 0.70)  + 
    ggraph::geom_node_label(aes(label = gene, fill = celltype), fontface = "bold", size = 3.5, nudge_x = 0, nudge_y = 0, color = "whitesmoke") +
    ggraph::theme_graph(foreground = 'black', fg_text_colour = 'white', base_family = 'Helvetica') + facet_grid(. ~group)  + ggraph::scale_edge_color_manual(values = colors_regulation) + scale_fill_manual(values = colors)
  
  return(list(plot = plot, graph = graph, source_df_lr = source_df_lr, source_df_lt = links %>% dplyr::filter(type == "Ligand-Target"), nodes_df = nodes))
}

Please let me know if you would still have issues with this. If none of you encounter further issues, I will close this issue from the moment we release the next version of the package where this should be fixed.

from multinichenetr.

n318162971 avatar n318162971 commented on June 1, 2024

Same problem for me, running rlang::last_trace() gives:

<error/rlang_error>
Error in `ggraph::geom_edge_loop()`:
! Problem while computing stat.
ℹ Error occurred in the 2nd layer.
Caused by error in `seq_len()`:
! argument must be coercible to non-negative integer
---
Backtrace:
     ▆
  1. ├─base (local) `<fn>`(x)
  2. └─ggplot2:::print.ggplot(x)
  3.   ├─ggplot2::ggplot_build(x)
  4.   ├─ggraph:::ggplot_build.ggraph(x)
  5.   ├─base::NextMethod()
  6.   └─ggplot2:::ggplot_build.ggplot(x)
  7.     └─ggplot2:::by_layer(...)
  8.       ├─rlang::try_fetch(...)
  9.       │ ├─base::tryCatch(...)
 10.       │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
 11.       │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
 12.       │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
 13.       │ └─base::withCallingHandlers(...)
 14.       └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
 15.         └─l$compute_statistic(d, layout)
 16.           └─ggplot2 (local) compute_statistic(..., self = self)
 17.             └─self$stat$compute_layer(data, self$computed_stat_params, layout)
 18.               └─ggplot2 (local) compute_layer(..., self = self)
 19.                 └─ggplot2:::dapply(...)
 20.                   └─ggplot2:::split_with_index(seq_len(nrow(df)), ids)

from multinichenetr.

browaeysrobin avatar browaeysrobin commented on June 1, 2024

Hi @SergioRodLla and @n318162971

Could you provide some more useful information about your specific analyses, otherwise I won't be able to help you because this plotting function works still fine for me on the vignette datasets and other datasets.

  • Did you encounter this error on your own data or on the vignette data? (I assume own data)
  • Which vignette did you follow?
  • Did you encounter error or warning message before running this?
  • How do lr_target_prior_cor_filtered, prioritized_tbl_oi and colors_sender look like? Do they look different then when applied on the vignette data?

from multinichenetr.

SergioRodLla avatar SergioRodLla commented on June 1, 2024

Hi, thank you for the reply.

Answering your questions, yes, I encountered this error on my own data. I followed this vignette basic_analysis_steps_MISC.md. The only warning I encountered was this when generating the lr_target_prior_cor_filtered table:

Joining with `by = join_by(receiver, ligand, target)`Warning: Detected an unexpected many-to-many relationship between `x` and `y`.Joining with `by = join_by(contrast)`

Details about this experiment include running two sender cell-types (A & B) and two receiver cell-types (C & D), comparing two conditions of interest (two different diets).
Note: I run the same steps excluding one of the two sender cell-types and it worked. I compared the lr_target_prior_cor_filtered, prioritized_tbl_oi and colors_sender objects between these two tests and they have the same structure, with the same columns and so on. I'm not sure what to look at here.

from multinichenetr.

browaeysrobin avatar browaeysrobin commented on June 1, 2024

Hi @SergioRodLla

I have no idea what might be going on there. Can you try to debug yourself to see where exactly in the function the error occurs? In case you are not familiar with doing this, I can do this if you would send me (by mail) 1) the input objects of the make_ggraph_ligand_target_links when the error is thrown and 2) the input objects when it runs without problems.

from multinichenetr.

mariecrane avatar mariecrane commented on June 1, 2024

I'm encountering the same error. @SergioRodLla could you please update if you find a solution?

from multinichenetr.

SergioRodLla avatar SergioRodLla commented on June 1, 2024

Hi @browaeysrobin I've sent you an email with the data. I hope this helps.

from multinichenetr.

SergioRodLla avatar SergioRodLla commented on June 1, 2024

It's working for me thanks!

from multinichenetr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.