Search in sources :

Example 1 with ObjectType

use of org.cytoscape.io.internal.util.xgmml.ObjectType in project cytoscape-impl by cytoscape.

the class AttributeValueUtil method handleAttribute.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected ParseState handleAttribute(Attributes atts) throws SAXParseException {
    ParseState parseState = ParseState.NONE;
    final String name = atts.getValue("name");
    String type = atts.getValue("type");
    String cyType = atts.getValue("cy:type");
    if ("has_nested_network".equals(name))
        type = ObjectType.BOOLEAN.getXgmmlValue();
    final boolean isEquation = ObjectTypeMap.fromXGMMLBoolean(atts.getValue("cy:equation"));
    final boolean isHidden = ObjectTypeMap.fromXGMMLBoolean(atts.getValue("cy:hidden"));
    final CyIdentifiable curElement = manager.getCurrentElement();
    CyNetwork curNet = manager.getCurrentNetwork();
    // under the group subgraph, but the edge will be created on the root-network only,
    if (curElement instanceof CyNode || curElement instanceof CyEdge) {
        boolean containsElement = (curElement instanceof CyNode && curNet.containsNode((CyNode) curElement));
        containsElement |= (curElement instanceof CyEdge && curNet.containsEdge((CyEdge) curElement));
        // So if the current network does not contain this element, the CyRootNetwork should contain it
        if (!containsElement)
            curNet = manager.getRootNetwork();
    }
    CyRow row = null;
    if (isHidden) {
        row = curNet.getRow(curElement, CyNetwork.HIDDEN_ATTRS);
    } else {
        // Network name must be local, right? What about other network attributes?
        if (CyNetwork.SELECTED.equals(name) || (curElement instanceof CyNetwork))
            row = curNet.getRow(curElement, CyNetwork.LOCAL_ATTRS);
        else
            // Will be created in the shared table
            row = curNet.getRow(curElement, CyNetwork.DEFAULT_ATTRS);
    }
    CyTable table = row.getTable();
    CyColumn column = table.getColumn(name);
    if (column != null) {
        // Check if it's a virtual column
        // It's necessary because the source row may not exist yet, which would throw an exception
        // when the value is set. Doing this forces the creation of the source row.
        final VirtualColumnInfo info = column.getVirtualColumnInfo();
        if (info.isVirtual()) {
            final CyTable srcTable = info.getSourceTable();
            final CyColumn srcColumn = srcTable.getColumn(info.getSourceColumn());
            final Class<?> jkColType = table.getColumn(info.getTargetJoinKey()).getType();
            final Object jkValue = row.get(info.getTargetJoinKey(), jkColType);
            final Collection<CyRow> srcRowList = srcTable.getMatchingRows(info.getSourceJoinKey(), jkValue);
            final CyRow srcRow;
            if (srcRowList == null || srcRowList.isEmpty()) {
                if (info.getTargetJoinKey().equals(CyIdentifiable.SUID)) {
                    // Try to create the row
                    srcRow = srcTable.getRow(jkValue);
                } else {
                    logger.error("Unable to import virtual column \"" + name + "\": The source table \"" + srcTable.getTitle() + "\" does not have any matching rows for join key \"" + info.getSourceJoinKey() + "=" + jkValue + "\".");
                    return parseState;
                }
            } else {
                srcRow = srcRowList.iterator().next();
            }
            // Use the source table instead
            table = srcTable;
            column = srcColumn;
            row = srcRow;
        }
    } else {
        // Also check if the column exists in a network's local table
        final Class<? extends CyIdentifiable> tableType;
        if (curElement instanceof CyNode)
            tableType = CyNode.class;
        else if (curElement instanceof CyEdge)
            tableType = CyEdge.class;
        else
            tableType = CyNetwork.class;
        if (existsInLocalTable(name, tableType, manager.getRootNetwork()))
            row = curNet.getRow(curElement, CyNetwork.LOCAL_ATTRS);
    }
    Object value = null;
    final ObjectType objType = typeMap.fromXgmml(cyType, type);
    if (isEquation) {
        // It is an equation...
        String formula = atts.getValue("value");
        if (name != null && formula != null) {
            manager.addEquationString(row, name, formula);
        }
    } else {
        // Regular attribute value...
        value = getTypedAttributeValue(objType, atts, name);
    }
    switch(objType) {
        case BOOLEAN:
            if (name != null)
                setAttribute(row, name, Boolean.class, (Boolean) value);
            break;
        case REAL:
            if (name != null) {
                if (SUIDUpdater.isUpdatable(name))
                    setAttribute(row, name, Long.class, (Long) value);
                else
                    setAttribute(row, name, Double.class, (Double) value);
            }
            break;
        case INTEGER:
            if (name != null) {
                if (SUIDUpdater.isUpdatable(name))
                    setAttribute(row, name, Long.class, (Long) value);
                else
                    setAttribute(row, name, Integer.class, (Integer) value);
            }
            break;
        case LONG:
            if (name != null)
                setAttribute(row, name, Long.class, (Long) value);
            break;
        case STRING:
            if (name != null)
                setAttribute(row, name, String.class, (String) value);
            break;
        // must make sure to clear out any existing values before we parse.
        case LIST:
            manager.currentAttributeID = name;
            manager.setCurrentRow(row);
            if (column != null && List.class.isAssignableFrom(column.getType()))
                row.set(name, null);
            if (column == null) {
                // Since Cytoscape v3.3
                final String elementType = atts.getValue("cy:elementType");
                if (elementType != null) {
                    final ObjectType elementObjType = typeMap.fromXgmml(elementType, null);
                    final Class<?> clazz = typeMap.getClass(elementObjType, name);
                    table.createListColumn(name, clazz, false, new ArrayList());
                    column = row.getTable().getColumn(name);
                    manager.listAttrHolder = new ArrayList<Object>();
                }
            }
            return ParseState.LIST_ATT;
    }
    return parseState;
}
Also used : CyColumn(org.cytoscape.model.CyColumn) ArrayList(java.util.ArrayList) CyNetwork(org.cytoscape.model.CyNetwork) ParseState(org.cytoscape.io.internal.read.xgmml.ParseState) CyRow(org.cytoscape.model.CyRow) CyEdge(org.cytoscape.model.CyEdge) CyTable(org.cytoscape.model.CyTable) ObjectType(org.cytoscape.io.internal.util.xgmml.ObjectType) CyNode(org.cytoscape.model.CyNode) ArrayList(java.util.ArrayList) List(java.util.List) VirtualColumnInfo(org.cytoscape.model.VirtualColumnInfo) CyIdentifiable(org.cytoscape.model.CyIdentifiable)

Example 2 with ObjectType

use of org.cytoscape.io.internal.util.xgmml.ObjectType in project cytoscape-impl by cytoscape.

the class HandleListAttribute method handle.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public ParseState handle(String tag, Attributes atts, ParseState current) throws SAXException {
    final String type = atts.getValue("type");
    final String cyType = atts.getValue("cy:type");
    final String name = manager.currentAttributeID;
    final ObjectType objType = typeMap.fromXgmml(cyType, type);
    final Object value = attributeValueUtil.getTypedAttributeValue(objType, atts, name);
    final CyRow row = manager.getCurrentRow();
    CyColumn column = row.getTable().getColumn(name);
    if (column == null) {
        final Class<?> clazz = typeMap.getClass(objType, name);
        row.getTable().createListColumn(name, clazz, false, new ArrayList());
        column = row.getTable().getColumn(name);
    }
    if (List.class.isAssignableFrom(column.getType())) {
        if (manager.listAttrHolder == null) {
            manager.listAttrHolder = new ArrayList<Object>();
            row.set(name, manager.listAttrHolder);
        }
        if (manager.listAttrHolder != null && value != null)
            manager.listAttrHolder.add(value);
    }
    return current;
}
Also used : ObjectType(org.cytoscape.io.internal.util.xgmml.ObjectType) CyColumn(org.cytoscape.model.CyColumn) ArrayList(java.util.ArrayList) CyRow(org.cytoscape.model.CyRow)

Example 3 with ObjectType

use of org.cytoscape.io.internal.util.xgmml.ObjectType in project cytoscape-impl by cytoscape.

the class GenericXGMMLWriter method writeAttribute.

/**
 * Creates an attribute to write into XGMML file.
 *
 * @param row CyRow to load
 * @param attName attribute name
 * @return att Att to return (gets written into xgmml file - CAN BE NULL)
 * @throws IOException
 */
protected void writeAttribute(final CyRow row, final String attName) throws IOException {
    // create an attribute and its type:
    final CyTable table = row.getTable();
    final CyColumn column = table.getColumn(attName);
    if (column == null)
        return;
    final boolean hidden = !table.isPublic();
    final Class<?> attType = column.getType();
    if (attType == Double.class) {
        Double dAttr = row.get(attName, Double.class);
        writeAttributeXML(attName, ObjectType.REAL, dAttr, hidden, true);
    } else if (attType == Integer.class) {
        Integer iAttr = row.get(attName, Integer.class);
        writeAttributeXML(attName, ObjectType.INTEGER, iAttr, hidden, true);
    } else if (attType == Long.class) {
        Long lAttr = row.get(attName, Long.class);
        writeAttributeXML(attName, ObjectType.LONG, lAttr, hidden, true);
    } else if (attType == String.class) {
        String sAttr = row.get(attName, String.class);
        // Protect tabs and returns
        if (sAttr != null) {
            sAttr = sAttr.replace("\n", "\\n");
            sAttr = sAttr.replace("\t", "\\t");
        }
        writeAttributeXML(attName, ObjectType.STRING, sAttr, hidden, true);
    } else if (attType == Boolean.class) {
        Boolean bAttr = row.get(attName, Boolean.class);
        writeAttributeXML(attName, ObjectType.BOOLEAN, ObjectTypeMap.toXGMMLBoolean(bAttr), hidden, true);
    } else if (attType == List.class) {
        final List<?> listAttr = row.getList(attName, column.getListElementType());
        final ObjectType elementType = getObjectType(column.getListElementType());
        writeAttributeXML(attName, ObjectType.LIST, elementType, null, hidden, false);
        if (listAttr != null) {
            depth++;
            // iterate through the list
            for (Object obj : listAttr) {
                String sAttr = null;
                if (obj instanceof Boolean) {
                    sAttr = ObjectTypeMap.toXGMMLBoolean((Boolean) obj);
                } else {
                    // Protect tabs and returns (if necessary)
                    sAttr = obj.toString();
                    if (sAttr != null) {
                        sAttr = sAttr.replace("\n", "\\n");
                        sAttr = sAttr.replace("\t", "\\t");
                    }
                }
                // set child attribute value & label
                writeAttributeXML(attName, getObjectType(obj), sAttr, hidden, true);
            }
            depth--;
        }
        writeAttributeXML(null, null, null, hidden, true);
    }
}
Also used : CyTable(org.cytoscape.model.CyTable) ObjectType(org.cytoscape.io.internal.util.xgmml.ObjectType) CyColumn(org.cytoscape.model.CyColumn) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)3 ObjectType (org.cytoscape.io.internal.util.xgmml.ObjectType)3 CyColumn (org.cytoscape.model.CyColumn)3 List (java.util.List)2 CyRow (org.cytoscape.model.CyRow)2 CyTable (org.cytoscape.model.CyTable)2 ParseState (org.cytoscape.io.internal.read.xgmml.ParseState)1 CyEdge (org.cytoscape.model.CyEdge)1 CyIdentifiable (org.cytoscape.model.CyIdentifiable)1 CyNetwork (org.cytoscape.model.CyNetwork)1 CyNode (org.cytoscape.model.CyNode)1 VirtualColumnInfo (org.cytoscape.model.VirtualColumnInfo)1