Example usage for org.apache.commons.collections4 BidiMap removeValue

List of usage examples for org.apache.commons.collections4 BidiMap removeValue

Introduction

In this page you can find the example usage for org.apache.commons.collections4 BidiMap removeValue.

Prototype

K removeValue(Object value);

Source Link

Document

Removes the key-value pair that is currently mapped to the specified value (optional operation).

Usage

From source file:research.NetworkAligner.java

public void align_networks_from_data(AlignmentNetwork network0, AlignmentNetwork network1,
        AlignmentNetwork aligned, TaskMonitor tm) throws Exception {
    String name = network0.get_suggested_name() + "_" + network1.get_suggested_name();
    aligned.set_suggested_name(name);/*  w w  w.  j  a v a 2s .com*/
    if (tm != null) {
        tm.setStatusMessage("aligning " + name + "...");
    }

    List<CyRow> rows = m_table.getAllRows();

    // keep track of progress
    int j = 0;
    int total = rows.size() + network0.get_node_count() + network0.get_edge_count() + network1.get_node_count()
            + network1.get_edge_count();

    // Create the bidirectional maps for lookup
    BidiMap<String, String> network0_1 = new DualHashBidiMap<>();
    for (CyRow row : rows) {
        List<String> sigs = row.getList(c_NodesSignatureSlot, String.class);
        network0_1.put(sigs.get(0), sigs.get(1));
        Util.advance_progress(tm, j, total);
    }
    NodeSignatureManager sig_mgr = new NodeSignatureManager();
    // Update the signatures with the one in the real network
    for (AlignmentNetwork.NodeIterator i = network0.NodeIterator(); i.hasNext();) {
        String real_sig = i.next();
        sig_mgr.override_with(real_sig);
        String plain_sig = the_only_element_in(sig_mgr.get_all_node_signatures());
        String val = network0_1.get(plain_sig);
        if (val != null) {
            network0_1.remove(plain_sig);
            network0_1.put(real_sig, val);
        }
    }
    for (AlignmentNetwork.NodeIterator i = network1.NodeIterator(); i.hasNext();) {
        String real_sig = i.next();
        sig_mgr.override_with(real_sig);
        String plain_sig = the_only_element_in(sig_mgr.get_all_node_signatures());
        String key = network0_1.getKey(plain_sig);
        if (key != null) {
            network0_1.removeValue(plain_sig);
            network0_1.put(key, real_sig);
        }
    }

    // Add all the data from the first network, and add belongings according to alignment data
    // Add nodes 0
    for (AlignmentNetwork.NodeIterator i = network0.NodeIterator(); i.hasNext();) {
        String sig = i.next();

        sig_mgr.override_with(sig);
        CyNode node = aligned.make_node(sig_mgr);

        if (network0_1.containsKey(sig)) {
            // This node can be aligned
            aligned.add_node_belongings(node, network0.get_network());
            aligned.add_node_belongings(node, network1.get_network());
        } else {
            // This node cannot be aligned
            aligned.add_node_belongings(node, network0.get_network());
        }
        Util.advance_progress(tm, j, total);
    }
    // Add edges 0
    for (AlignmentNetwork.EdgeIterator i = network0.EdgeIterator(); i.hasNext();) {
        AlignmentNetwork.Edge edge_sig = i.next();
        CyNode node0, node1;

        sig_mgr.override_with(edge_sig.m_e0);
        node0 = aligned.get_node_from_signature(sig_mgr);

        sig_mgr.override_with(edge_sig.m_e1);
        node1 = aligned.get_node_from_signature(sig_mgr);

        if (node0 == null || node1 == null) {
            throw new Exception(getClass() + " - Edge and node doesn't match: The pair (node0:" + edge_sig.m_e0
                    + "node1:" + edge_sig.m_e1 + ") cannot be found");
        }
        CyEdge edge = aligned.make_edge(node0, node1);
        aligned.add_edge_belongings(edge, network0.get_network());
        if (network0_1.containsKey(edge_sig.m_e0) && network0_1.containsKey(edge_sig.m_e1)) {
            // This edge can be aligned
            aligned.add_edge_belongings(edge, network1.get_network());
        }
        Util.advance_progress(tm, j, total);
    }
    // Add second network but exclude nodes and edges that are already in the first network
    // Add nodes 1
    for (AlignmentNetwork.NodeIterator i = network1.NodeIterator(); i.hasNext();) {
        String sig = i.next();
        if (!network0_1.containsValue(sig)) {
            // This node is not aligned
            sig_mgr.override_with(sig);
            CyNode node = aligned.make_node(sig_mgr);
            aligned.add_node_belongings(node, network1.get_network());
        }
        Util.advance_progress(tm, j, total);
    }
    // Add edges 1
    for (AlignmentNetwork.EdgeIterator i = network1.EdgeIterator(); i.hasNext();) {
        AlignmentNetwork.Edge edge_sig = i.next();
        CyNode node0, node1;
        if (network0_1.containsValue(edge_sig.m_e0)) {
            String translated = network0_1.getKey(edge_sig.m_e0);
            sig_mgr.override_with(translated);
            node0 = aligned.get_node_from_signature(sig_mgr);
        } else {
            sig_mgr.override_with(edge_sig.m_e0);
            node0 = aligned.get_node_from_signature(sig_mgr);
        }
        if (network0_1.containsValue(edge_sig.m_e1)) {
            String translated = network0_1.getKey(edge_sig.m_e1);
            sig_mgr.override_with(translated);
            node1 = aligned.get_node_from_signature(sig_mgr);
        } else {
            sig_mgr.override_with(edge_sig.m_e1);
            node1 = aligned.get_node_from_signature(sig_mgr);
        }
        if (node0 == null) {
            if (network0_1.containsValue(edge_sig.m_e0)) {
                //                                        throw new Exception(" - node0 not found from the g0 network: "
                //                                                            + edge_sig.m_e0 + "->" + network0_1.get(edge_sig.m_e0));
                System.out.println(getClass() + " - node0 not found from the g0 network: " + edge_sig.m_e0
                        + "->" + network0_1.get(edge_sig.m_e0));
            } else {
                //                                        throw new Exception(" - node0 not found from the g1 network: "
                //                                                            + edge_sig.m_e0);
                System.out.println(getClass() + " - node0 not found from the g1 network: " + edge_sig.m_e0);
            }
            continue;
        }
        if (node1 == null) {
            if (network0_1.containsValue(edge_sig.m_e1)) {
                //                                        throw new Exception(" - node1 not found from the g0 network: "
                //                                                            + edge_sig.m_e1 + "->" + network0_1.get(edge_sig.m_e1));
                System.out.println(getClass() + " - node1 not found from the g0 network: " + edge_sig.m_e1
                        + "->" + network0_1.get(edge_sig.m_e1));
            } else {
                //                                        throw new Exception(" - node1 not found from the g1 network: "
                //                                                            + edge_sig.m_e1);
                System.out.println(getClass() + " - node1 not found from the g1 network: " + edge_sig.m_e1);
            }
            continue;
        }
        if (!network0_1.containsValue(edge_sig.m_e0) || !network0_1.containsValue(edge_sig.m_e1)) {
            // This edge is not aligned
            CyEdge edge = aligned.make_edge(node0, node1);
            aligned.add_edge_belongings(edge, network1.get_network());
        }
        Util.advance_progress(tm, j, total);
    }
    // Finally adjust the aligned node name by adding the signature from network1
    for (AlignmentNetwork.NodeIterator i = network1.NodeIterator(); i.hasNext();) {
        String sig = i.next();
        if (network0_1.containsValue(sig)) {
            // This node is aligned but we shall change the signature
            // to reflect the relation of the aligned node
            String translated = network0_1.getKey(sig);
            sig_mgr.override_with(translated);
            CyNode node = aligned.get_node_from_signature(sig_mgr);
            if (node == null) {
                // have to skip the incorrect node.
                continue;
            }
            sig_mgr.append_with(sig);
            aligned.mutate_node_signature(node, sig_mgr);
        }
        Util.advance_progress(tm, j, total);
    }
}