cytoscape / RCy3

New version of RCy3, redesigned and collaboratively maintained by Cytoscape developer community
MIT License
49 stars 20 forks source link

Add function to remove duplicate edges #35

Closed risserlin closed 6 years ago

risserlin commented 6 years ago

similar to the menu option Edit -> Remove duplicate edges ...

AdamStuart commented 6 years ago

This and Remove Self Loops are functions in NetworkAnalyzer, a core app that doesn’t have an active developer.
Do you actively need to use this, or are you covering the completeness of the implementation? You could do this pretty easily in R.

Adam

On Jul 11, 2018, at 6:44 AM, Ruth Isserlin notifications@github.com wrote:

similar to the menu option Edit -> Remove duplicate edges ...

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/cytoscape/RCy3/issues/35, or mute the thread https://github.com/notifications/unsubscribe-auth/AHSv7SZPPuUJJM-L9ywI05jCMJYlRcgkks5uFgE8gaJpZM4VLKqg.

risserlin commented 6 years ago

I was just trying to create networks from R and clean them up. I created a correlation network using aMatReader (which I understood to only take the top triangle of the adjacency matrix) but came up with double edges for everything. I wanted to quickly remove them but there is no option in RCy3. But that isn't the only use case where I have looked for this function.

AlexanderPico commented 6 years ago

These function will be available in the next release of Bioconductor (October). In the meantime, you can install the dev version from Bioconductor or install from GitHub, or add the functions directly to your scripts:

# ------------------------------------------------------------------------------
#' @title Delete Duplicate Edges
#'
#' @description Removes edges with duplicate names. Only considers cases with
#' identical source, target, interaction and directionality.
#' @details Duplicate edges are first selected and then deleted. Prior edge
#' selections will be lost; node selections will not be affected.
#' @param network (optional) Name or SUID of the network. Default is the 
#' "current" network active in Cytoscape.
#' @param base.url (optional) Ignore unless you need to specify a custom domain,
#' port or version to connect to the CyREST API. Default is http://localhost:1234
#' and the latest version of the CyREST API supported by this version of RCy3.
#' @return Lists of SUIDs for selected nodes and edges
#' @examples \donttest{
#' deleteDuplicateEdges()
#' }
#' @export
deleteDuplicateEdges <-
    function(network = NULL, base.url = .defaultBaseUrl){
        net.suid <- getNetworkSuid(network,base.url)

        allEdges <- getAllEdges(net.suid, base.url)
        dupEdges <- unique(allEdges[duplicated(allEdges)])

        #get list of duplicate edge SUIDs to select and delete
        dupEdgeSuids <- c()
        for (de in dupEdges){
            edgeSuids <- .edgeNameToEdgeSUID(de, network = net.suid, base.url = base.url)
            if(length(edgeSuids) > 1)
                dupEdgeSuids <- c(dupEdgeSuids, edgeSuids[-1]) #pick all by one
        }    

        selectEdges(
            dupEdgeSuids,
            by.col = 'SUID',
            preserve.current.selection = FALSE,
            network = net.suid,
            base.url = base.url
        )

        deleteSelectedEdges(network = net.suid, base.url = base.url)
    }

# ------------------------------------------------------------------------------
#' @title Delete Self Loops
#'
#' @description Removes edges that connect to a single node as both source and 
#' target.
#' @details Self loop edges are first selected and then deleted. Prior edge and 
#' node selections will be lost.
#' @param network (optional) Name or SUID of the network. Default is the 
#' "current" network active in Cytoscape.
#' @param base.url (optional) Ignore unless you need to specify a custom domain,
#' port or version to connect to the CyREST API. Default is http://localhost:1234
#' and the latest version of the CyREST API supported by this version of RCy3.
#' @return Lists of SUIDs for selected nodes and edges
#' @examples \donttest{
#' deleteSelfLoops()
#' }
#' @export
deleteSelfLoops <-
    function(network = NULL, base.url = .defaultBaseUrl){
        net.suid <- getNetworkSuid(network,base.url)

        clearSelection('both', net.suid, base.url)
        allNodes <- getAllNodes(net.suid, base.url)
        for (n in allNodes){
            selectNodes(n, by.col='name', preserve.current.selection = FALSE,
                        network = net.suid, base.url = base.url)
            selectEdgesConnectingSelectedNodes()
            deleteSelectedEdges(net.suid,base.url)
        }    
        clearSelection('both', net.suid, base.url)
    }