Skip to content

Commit

Permalink
Where components contain a feature represented by more than one adduc…
Browse files Browse the repository at this point in the history
…t and isotope combination, retain only the node with the highest AIS
  • Loading branch information
jasenfinch committed Nov 3, 2023
1 parent 9991103 commit 3c24aef
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ importFrom(dplyr,full_join)
importFrom(dplyr,group_by)
importFrom(dplyr,group_split)
importFrom(dplyr,inner_join)
importFrom(dplyr,join_by)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,mutate_at)
Expand Down
51 changes: 42 additions & 9 deletions R/components.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ clean <- function(graph,adduct_rules_table){
cleaned_graph <- cleaned_graph %>%
bind_graphs()
}

return(cleaned_graph)
}

Expand All @@ -60,14 +60,14 @@ nComponents <- function(graph){

componentMetrics <- function(component,max_add_iso_total){
component %>%
mutate(Size = graph_size(),
Nodes = n(),
Degree = avg_degree(Nodes,Size),
Density = (2 * Size) / (Nodes * (Nodes - 1)),
Weight = sum(Weight) / Nodes,
AIS = sum(AIS) / max_add_iso_total,
`Component Plausibility` = plausibility(Degree,AIS,Weight)
)
mutate(Size = graph_size(),
Nodes = n(),
Degree = avg_degree(Nodes,Size),
Density = (2 * Size) / (Nodes * (Nodes - 1)),
Weight = sum(Weight) / Nodes,
AIS = sum(AIS) / max_add_iso_total,
`Component Plausibility` = plausibility(Degree,AIS,Weight)
)
}

componentFilters <- function(){
Expand All @@ -77,6 +77,38 @@ componentFilters <- function(){
Direction = c(rep('max',2),'min'))
}

#' @importFrom dplyr join_by

deduplicate <- function(graph){
dedup_nodes <- graph %>%
activate(nodes) %>%
as_tibble() %>%
group_split(
Component,
Feature
) %>%
future_map_dfr(
~.x %>%
arrange(
desc(
AIS
)
) %>%
slice(1)
)

graph %>%
activate(nodes) %>%
inner_join(
dedup_nodes,
by = join_by(
name, Feature, RetentionTime,
Isotope, Adduct, MF, `Theoretical M`, `Measured M`,
`Theoretical m/z`, `Measured m/z`, `PPM error`,
`MF Plausibility (%)`, AIS, ID, Component)
)
}

#' @importFrom tidygraph as_tbl_graph activate morph unmorph graph_size group_components tbl_graph
#' @importFrom magrittr set_names

Expand All @@ -88,6 +120,7 @@ calcComponents <- function(graph_nodes,
activate(nodes) %>%
left_join(graph_nodes,by = c('name' = 'Name')) %>%
mutate(Component = group_components()) %>%
deduplicate() %>%
clean(adductRules(assignment))

if (nComponents(graph) > 0){
Expand Down

0 comments on commit 3c24aef

Please sign in to comment.