Search in sources :

Example 11 with CyColumn

use of org.cytoscape.model.CyColumn in project EnrichmentMapApp by BaderLab.

the class SessionViewIO method clearTable.

private void clearTable(CyTable table) {
    List<Object> rowKeys = new ArrayList<>();
    CyColumn keyColumn = table.getPrimaryKey();
    for (CyRow row : table.getAllRows()) {
        Object key = row.get(keyColumn.getName(), keyColumn.getType());
        rowKeys.add(key);
    }
    table.deleteRows(rowKeys);
}
Also used : ArrayList(java.util.ArrayList) CyColumn(org.cytoscape.model.CyColumn) CyRow(org.cytoscape.model.CyRow)

Example 12 with CyColumn

use of org.cytoscape.model.CyColumn in project EnrichmentMapApp by BaderLab.

the class AbstractChart method getDataFromColumns.

public Map<String, List<Double>> getDataFromColumns(final CyNetwork network, final CyIdentifiable model, final List<CyColumnIdentifier> columnNames) {
    LinkedHashMap<String, List<Double>> data = new LinkedHashMap<>();
    final CyRow row = network.getRow(model);
    if (row == null)
        return data;
    final CyTable table = row.getTable();
    final List<Double> singleSeriesValues = new ArrayList<>();
    final StringBuilder singleSeriesKey = new StringBuilder();
    int singleSeriesIndex = -1;
    int count = 0;
    for (final CyColumnIdentifier colId : columnNames) {
        final CyColumn column = table.getColumn(colId.getColumnName());
        if (column == null)
            continue;
        final String colName = column.getName();
        final List<Double> values = new ArrayList<>();
        if (column.getType() == List.class) {
            // List Column: One column = one data series
            final Class<?> type = column.getListElementType();
            if (type == Double.class) {
                List<Double> list = row.getList(colName, Double.class);
                if (list != null)
                    values.addAll(list);
            } else if (type == Integer.class) {
                List<Integer> list = row.getList(colName, Integer.class);
                if (list != null) {
                    for (Integer i : list) values.add(i.doubleValue());
                }
            } else if (type == Long.class) {
                List<Long> list = row.getList(colName, Long.class);
                if (list != null) {
                    for (Long l : list) values.add(l.doubleValue());
                }
            } else if (type == Float.class) {
                List<Float> list = row.getList(colName, Float.class);
                if (list != null) {
                    for (Float f : list) values.add(f.doubleValue());
                }
            }
            data.put(colName, values);
        } else {
            // Single Column: All single columns together make only one data series
            final Class<?> type = column.getType();
            if (Number.class.isAssignableFrom(type)) {
                if (!row.isSet(colName)) {
                    singleSeriesValues.add(Double.NaN);
                } else if (type == Double.class) {
                    singleSeriesValues.add(row.get(colName, Double.class));
                } else if (type == Integer.class) {
                    Integer i = row.get(colName, Integer.class);
                    singleSeriesValues.add(i.doubleValue());
                } else if (type == Float.class) {
                    Float f = row.get(colName, Float.class);
                    singleSeriesValues.add(f.doubleValue());
                }
                singleSeriesKey.append(colName + ",");
                // The index of this data series is the index of the first single column
                if (singleSeriesIndex == -1)
                    singleSeriesIndex = count;
            }
        }
        count++;
    }
    if (!singleSeriesValues.isEmpty()) {
        singleSeriesKey.deleteCharAt(singleSeriesKey.length() - 1);
        // To add the series of single columns into the correct position, we have to rebuild the data map
        final Set<Entry<String, List<Double>>> entrySet = data.entrySet();
        data = new LinkedHashMap<>();
        int i = 0;
        for (final Entry<String, List<Double>> entry : entrySet) {
            if (i == singleSeriesIndex)
                data.put(singleSeriesKey.toString(), singleSeriesValues);
            data.put(entry.getKey(), entry.getValue());
            i++;
        }
        if (// (entrySet.isEmpty() || i >= entrySet.size())
        !data.containsKey(singleSeriesKey.toString()))
            data.put(singleSeriesKey.toString(), singleSeriesValues);
    }
    return data;
}
Also used : ArrayList(java.util.ArrayList) CyColumn(org.cytoscape.model.CyColumn) CyColumnIdentifier(org.cytoscape.view.presentation.property.values.CyColumnIdentifier) CyRow(org.cytoscape.model.CyRow) LinkedHashMap(java.util.LinkedHashMap) CyTable(org.cytoscape.model.CyTable) Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) List(java.util.List)

Example 13 with CyColumn

use of org.cytoscape.model.CyColumn in project cytoscape-api by cytoscape.

the class AbstractCyRootNetworkTest method testGetNetworkSharedNameColumn.

@Test
public void testGetNetworkSharedNameColumn() {
    CyTable shared = root.getSharedNetworkTable();
    CyColumn col = shared.getColumn(CyRootNetwork.SHARED_NAME);
    assertNotNull(col);
    assertEquals(String.class, col.getType());
}
Also used : CyTable(org.cytoscape.model.CyTable) CyColumn(org.cytoscape.model.CyColumn) Test(org.junit.Test)

Example 14 with CyColumn

use of org.cytoscape.model.CyColumn in project cytoscape-api by cytoscape.

the class AbstractCySubNetworkTest method testAddSubnetwork.

/**
	 * Test for checking contents of tables in the subnetworks.
	 */
@Test
public void testAddSubnetwork() {
    // Add two nodes and one edge to the root
    n1 = root.addNode();
    n2 = root.addNode();
    e1 = root.addEdge(n1, n2, true);
    root.getRow(n1).set(CyRootNetwork.NAME, "node1");
    root.getRow(n2).set(CyRootNetwork.NAME, "node2");
    root.getRow(e1).set(CyRootNetwork.NAME, "edge1");
    root.getSharedNodeTable().getRow(n1.getSUID()).set(CyRootNetwork.SHARED_NAME, "node1");
    root.getSharedNodeTable().getRow(n2.getSUID()).set(CyRootNetwork.SHARED_NAME, "node2");
    root.getSharedEdgeTable().getRow(e1.getSUID()).set(CyRootNetwork.SHARED_NAME, "edge1");
    System.out.println("Root network class = " + root.getClass());
    // Make sure required columns exists
    final CyTable nodeTable = root.getDefaultNodeTable();
    final CyTable edgeTable = root.getDefaultEdgeTable();
    final CyTable networkTable = root.getDefaultNetworkTable();
    final CyTable nodeSharedTable = root.getSharedNodeTable();
    final CyTable edgeSharedTable = root.getSharedEdgeTable();
    final CyTable networkSharedTable = root.getSharedNetworkTable();
    final Collection<CyColumn> nodeColumns = nodeTable.getColumns();
    for (CyColumn col : nodeColumns) System.out.println("Node Table column: " + col.getName());
    final Collection<CyColumn> nodeSharedColumns = nodeSharedTable.getColumns();
    for (CyColumn col : nodeSharedColumns) System.out.println("Shared Node Table column: " + col.getName());
    assertNotNull(nodeTable.getColumn(CyRootNetwork.NAME));
    assertNotNull(nodeSharedTable.getColumn(CyRootNetwork.SHARED_NAME));
    assertNotNull(edgeTable.getColumn(CyRootNetwork.NAME));
    assertNotNull(edgeSharedTable.getColumn(CyRootNetwork.SHARED_NAME));
    assertNotNull(networkTable.getColumn(CyRootNetwork.NAME));
    assertNotNull(networkSharedTable.getColumn(CyRootNetwork.SHARED_NAME));
    // Make sure all values exists in the original table
    assertEquals("node1", root.getRow(n1).get(CyRootNetwork.NAME, String.class));
    assertEquals("node2", root.getRow(n2).get(CyRootNetwork.NAME, String.class));
    assertEquals("edge1", edgeSharedTable.getRow(e1.getSUID()).get(CyRootNetwork.SHARED_NAME, String.class));
    assertEquals("node1", nodeSharedTable.getRow(n1.getSUID()).get(CyRootNetwork.SHARED_NAME, String.class));
    assertEquals("node2", nodeSharedTable.getRow(n2.getSUID()).get(CyRootNetwork.SHARED_NAME, String.class));
    // Copy the entire network to the sub
    final CySubNetwork newNet = root.addSubNetwork();
    newNet.addNode(n1);
    newNet.addNode(n2);
    newNet.addEdge(e1);
    final CyTable newNetNodeTable = newNet.getDefaultNodeTable();
    final Collection<CyColumn> newNodeColumns = newNetNodeTable.getColumns();
    for (CyColumn col : newNodeColumns) System.out.println("New Node Table column: " + col.getName());
    assertEquals(4, newNodeColumns.size());
    final CyColumn nameCol = newNetNodeTable.getColumn(CyRootNetwork.NAME);
    final CyColumn sharedNameCol = newNetNodeTable.getColumn(CyRootNetwork.SHARED_NAME);
    assertNotNull(nameCol);
    assertNotNull(sharedNameCol);
    final String nameN1 = newNet.getRow(n1).get(CyRootNetwork.NAME, String.class);
    assertNotNull(nameN1);
    assertEquals("node1", nameN1);
    final String sharedNameN1 = newNet.getRow(n1).get(CyRootNetwork.SHARED_NAME, String.class);
    assertNotNull(sharedNameN1);
    assertEquals("node1", sharedNameN1);
}
Also used : CyTable(org.cytoscape.model.CyTable) CyColumn(org.cytoscape.model.CyColumn) Test(org.junit.Test)

Example 15 with CyColumn

use of org.cytoscape.model.CyColumn in project cytoscape-api by cytoscape.

the class AbstractCyRootNetworkTest method testGetEdgeSharedNameColumn.

@Test
public void testGetEdgeSharedNameColumn() {
    CyTable shared = root.getSharedEdgeTable();
    CyColumn col = shared.getColumn(CyRootNetwork.SHARED_NAME);
    assertNotNull(col);
    assertEquals(String.class, col.getType());
}
Also used : CyTable(org.cytoscape.model.CyTable) CyColumn(org.cytoscape.model.CyColumn) Test(org.junit.Test)

Aggregations

CyColumn (org.cytoscape.model.CyColumn)16 CyRow (org.cytoscape.model.CyRow)10 CyTable (org.cytoscape.model.CyTable)10 ArrayList (java.util.ArrayList)9 List (java.util.List)6 Test (org.junit.Test)4 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 CyNetwork (org.cytoscape.model.CyNetwork)3 CyColumnIdentifier (org.cytoscape.view.presentation.property.values.CyColumnIdentifier)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 File (java.io.File)2 IOException (java.io.IOException)2 Comparator (java.util.Comparator)2 Set (java.util.Set)2 Inject (javax.inject.Inject)2 CyNetworkManager (org.cytoscape.model.CyNetworkManager)2