private void addLinks(LinkTriple[] links) {
   CyAttributes nodeAttr = Cytoscape.getNodeAttributes();
   // CyAttributes edgeAttr = Cytoscape.getEdgeAttributes();
   Map<CyNode, Boolean> updateList = new HashMap<CyNode, Boolean>();
   CyNetwork net = Cytoscape.getCurrentNetwork();
   for (LinkTriple link : links) {
     CyNode src = Cytoscape.getCyNode(link.subj.toString());
     CyNode dst = Cytoscape.getCyNode(link.obj.toString());
     if (src == null) {
       src = net.addNode(Cytoscape.getCyNode(link.subj.toString(), true));
       if (link.subj.getType() != null)
         nodeAttr.setAttribute(src.getIdentifier(), "NodeType", link.subj.getType());
     }
     if (dst == null) {
       dst = net.addNode(Cytoscape.getCyNode(link.obj.toString(), true));
       if (link.obj.getType() != null)
         nodeAttr.setAttribute(dst.getIdentifier(), "NodeType", link.obj.getType());
     }
     CyEdge edge =
         Cytoscape.getCyEdge(
             link.subj.toString(), "link", link.obj.toString(), link.pred.toString());
     // if ( link.pred != null )
     //	edgeAttr.setAttribute(edge.getIdentifier(), "EdgeType", link.pred.toString() );
     net.addEdge(edge);
     updateList.put(src, true);
     updateList.put(dst, true);
   }
   Cytoscape.getCurrentNetworkView()
       .applyLayout(new GridNodeLayout(), updateList.keySet().toArray(new CyNode[0]), null);
   Cytoscape.getCurrentNetworkView().redrawGraph(true, true);
 }
  /**
   * Write out the state for the given attributes
   *
   * @param selectedRows
   * @return number of files successfully saved, the better way to do this would just be to throw
   *     the error and display a specific message for each failure, but oh well.
   * @throws IOException
   */
  public void writeAttributes() throws IOException {
    final String className;
    final byte dataType =
        cyAttributes.getMultiHashMapDefinition().getAttributeValueType(attributeName);

    if ((dataType == CyAttributes.TYPE_COMPLEX)
        || (dataType == CyAttributes.TYPE_SIMPLE_MAP)
        || (dataType == CyAttributes.TYPE_UNDEFINED))
      throw new IOException("Unsupported Datatype.");

    if (dataType == MultiHashMapDefinition.TYPE_BOOLEAN) className = "java.lang.Boolean";
    else if (dataType == MultiHashMapDefinition.TYPE_INTEGER) className = "java.lang.Integer";
    else if (dataType == MultiHashMapDefinition.TYPE_FLOATING_POINT) className = "java.lang.Double";
    else className = "java.lang.String";

    try {
      fileWriter.write(attributeName + " (class=" + className + ")" + newline);

      final MultiHashMap attributeMap = cyAttributes.getMultiHashMap();

      if (attributeMap != null) {
        final Iterator<String> keys = cyAttributes.getMultiHashMap().getObjectKeys(attributeName);

        String key;
        Object value;
        Iterator objIt;
        String vs;
        StringBuilder result = new StringBuilder();

        while (keys.hasNext()) {
          key = keys.next();

          if (cyAttributes.getType(attributeName) == CyAttributes.TYPE_SIMPLE_LIST)
            value = cyAttributes.getListAttribute(key, attributeName);
          else value = cyAttributes.getAttribute(key, attributeName);

          key = encodeString(key);

          if (value != null) {
            if (value instanceof List) {
              result.append(key + " = ");

              if (((Collection) value).size() > 0) {
                Object o;

                objIt = ((Collection) value).iterator();
                result.append("(");
                o = objIt.next();
                vs = o.toString();
                vs = slashEncodeString(vs);
                vs = encodeString(vs);
                result.append(vs);

                while (objIt.hasNext()) {

                  o = objIt.next();
                  vs = o.toString();
                  vs = slashEncodeString(vs);
                  vs = encodeString(vs);
                  result.append("::" + vs);
                }
                result.append(")" + newline);
                fileWriter.write(result.toString());
                result = new StringBuilder();
              }
            } else {
              vs = value.toString();
              vs = slashEncodeString(vs);
              vs = encodeString(vs);
              fileWriter.write(key + " = " + vs + newline);
            }
          }
        }

        fileWriter.flush();
      }
    } finally {
      if (fileWriter != null) {
        fileWriter.close();
      }
    }
  }
    @SuppressWarnings("unchecked")
    @Override
    public void actionPerformed(ActionEvent ae) {
      String action = ae.getActionCommand();

      LinkTriple[] addNodes = null;
      if (action.equals("Fetch")) {
        addNodes = iSparql.ExploreURI(urlText.getText());
        addLinks(addNodes);
        // Cytoscape.getCurrentNetwork().addNode( Cytoscape.getCyNode( urlText.getText(), true ) );
      }
      if (action.equals("Fetch Selected")) {
        CyNetwork network = Cytoscape.getCurrentNetwork();
        Set nodeset = network.getSelectedNodes();
        java.util.List<LinkTriple> nodeList = new LinkedList<LinkTriple>();
        for (Object o : nodeset) {
          CyNode node = (CyNode) o;
          for (LinkTriple newNode : iSparql.ExploreURI(node.getIdentifier())) {
            nodeList.add(newNode);
          }
        }
        if (filterCheckbox.getState())
          addLinks(iSparql.NodeFilter(null, nodeList.toArray(new LinkTriple[0])));
        else addLinks(nodeList.toArray(new LinkTriple[0]));
      }
      if (action.equals("Find Nodes")) {
        CyAttributes nodeAttr = Cytoscape.getNodeAttributes();

        CyNetwork network = Cytoscape.getCurrentNetwork();
        Set nodeset = network.getSelectedNodes();
        java.util.List<Map<String, Object>> nodeList = new LinkedList<Map<String, Object>>();
        for (Object o : nodeset) {
          CyNode node = (CyNode) o;
          Map<String, Object> map = new HashMap<String, Object>();
          String id = node.getIdentifier();
          map.put("ID", id);
          for (String attr : nodeAttr.getAttributeNames()) {
            if (nodeAttr.hasAttribute(id, attr)) {
              System.out.println(id + " " + attr + " " + nodeAttr.getType(attr));
              if (nodeAttr.getType(attr) == CyAttributes.TYPE_STRING) {
                Object iAttr = nodeAttr.getAttribute(id, attr);
                map.put(attr, iAttr);
              }
            }
          }
          nodeList.add(map);
        }
        LinkTriple[] links = iSparql.NodeSearch(null, nodeList, "ID");
        addLinks(links);
      }
      if (action.equals("Edit Namespaces")) {
        Map<String, String> nameMap = prefs.getNameSpaces();
        Map<String, String> newMap = NameSpaceEditor.EditDialog(nameMap);
        for (String key : nameMap.keySet()) {
          prefs.removeNameSpace(key);
        }
        for (String key : newMap.keySet()) {
          prefs.addNameSpace(key, newMap.get(key));
        }
      }
    }
    public gpClique[] creategpClique() {
      gpClique[] gps = new gpClique[2];
      gps[0] = new gpClique();
      gps[1] = new gpClique();

      Iterator<Edge> edgeIt = nw.edgesIterator();
      CyAttributes edgeAtt = Cytoscape.getEdgeAttributes();

      ArrayList largeALNodes = new ArrayList();
      ArrayList targetALNodes = new ArrayList();
      ArrayList matchALNodes = new ArrayList();
      while (edgeIt.hasNext()) {

        Edge edge = edgeIt.next();
        if (edgeAtt.getStringAttribute(edge.getIdentifier(), "interaction").equals("p2")) {
          largeALNodes.add(new Integer(nw.getEdgeSourceIndex(edge.getRootGraphIndex())));
          largeALNodes.add(new Integer(nw.getEdgeTargetIndex(edge.getRootGraphIndex())));
          continue; // go directly next step
        }
        if (edgeAtt.getStringAttribute(edge.getIdentifier(), "interaction").equals("p1")) {
          targetALNodes.add(new Integer(nw.getEdgeSourceIndex(edge.getRootGraphIndex())));
          targetALNodes.add(new Integer(nw.getEdgeTargetIndex(edge.getRootGraphIndex())));
          continue;
        }
        if (edgeAtt.getStringAttribute(edge.getIdentifier(), "interaction").equals("pd")) {
          matchALNodes.add(
              new Integer(
                  nw.getEdgeSourceIndex(
                      edge
                          .getRootGraphIndex()))); // the matched node(containing "-") of target
                                                   // network
          matchALNodes.add(
              new Integer(
                  nw.getEdgeTargetIndex(
                      edge
                          .getRootGraphIndex()))); // the matched node(containing "-") of large
                                                   // network
          continue;
        }
        JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "File Format error!");
        return null;
      }

      Iterator matchALNIt = matchALNodes.iterator();
      CyNode node;
      Integer it;
      while (matchALNIt.hasNext()) {
        it = (Integer) matchALNIt.next();
        node =
            Cytoscape.getCyNode(
                nw.getNode(it.intValue()).getIdentifier().substring(1),
                true); // get node according to its identifier
        matchNodes.add(node);
        if (!targetALNodes.contains(
            node.getRootGraphIndex())) // matched node,but not a node of network
        {
          nw.addNode(node);
          targetALNodes.add(node.getRootGraphIndex()); // add to network
        }

        it = (Integer) matchALNIt.next();
        node =
            Cytoscape.getCyNode(
                nw.getNode(it.intValue()).getIdentifier().substring(1),
                true); // get node according to its identifier
        matchNodes.add(node);
        if (!largeALNodes.contains(node.getRootGraphIndex())) {
          nw.addNode(node);
          largeALNodes.add(node.getRootGraphIndex());
        }
      }

      gps[0].setgpNodes(largeALNodes);
      gps[1].setgpNodes(targetALNodes);
      return gps;
    }
  /**
   * DOCUMENT ME!
   *
   * @throws Exception DOCUMENT ME!
   */
  public void testReadTable() throws Exception {

    InputStream is = null;
    POIFSFileSystem excelIn;

    try {
      is = getClass().getResource(NETWORK_FILE).openStream();
      excelIn = new POIFSFileSystem(is);
    } finally {
      if (is != null) {
        is.close();
      }
    }

    HSSFWorkbook wb = new HSSFWorkbook(excelIn);

    HSSFSheet sheet = wb.getSheetAt(0);

    List<String> delimiters = new ArrayList<String>();
    delimiters.add(TextFileDelimiters.TAB.toString());

    String[] galAttrName = {
      "Source", "Target", "Interaction", "edge bool attr", "edge string attr", "edge float attr"
    };
    Byte[] galAttrTypes = {
      CyAttributes.TYPE_STRING, CyAttributes.TYPE_STRING,
      CyAttributes.TYPE_STRING, CyAttributes.TYPE_BOOLEAN,
      CyAttributes.TYPE_STRING, CyAttributes.TYPE_FLOATING
    };
    NetworkTableMappingParameters mapping =
        new NetworkTableMappingParameters(
            delimiters,
            TextFileDelimiters.PIPE.toString(),
            galAttrName,
            galAttrTypes,
            null,
            null,
            0,
            1,
            2,
            null);

    reader = new ExcelNetworkSheetReader(wb.getSheetName(0), sheet, mapping);

    CyNetwork net = Cytoscape.createNetwork(reader, false, null);

    /*
     * test cases
     */
    assertEquals("Yeast Network Sheet 1", net.getTitle());
    assertEquals(331, net.getNodeCount());
    assertEquals(362, net.getEdgeCount());

    CyAttributes attr = Cytoscape.getEdgeAttributes();
    assertTrue(attr.getBooleanAttribute("YGL122C (pp) YOL123W", "edge bool attr"));
    assertFalse(attr.getBooleanAttribute("YKR026C (pp) YGL122C", "edge bool attr"));

    assertEquals(1.2344543, attr.getDoubleAttribute("YBL026W (pp) YOR167C", "edge float attr"));
    assertEquals("abcd12706", attr.getStringAttribute("YBL026W (pp) YOR167C", "edge string attr"));
    assertEquals("abcd12584", attr.getStringAttribute("YPL248C (pd) ?", "edge string attr"));

    Cytoscape.destroyNetwork(net);
  }
  public void testReadTableWithEmptyRows() throws Exception {
    String network = "/empty_attr_row.xls";

    InputStream is = null;
    POIFSFileSystem excelIn;

    try {
      is = getClass().getResource(network).openStream();
      excelIn = new POIFSFileSystem(is);
    } finally {
      if (is != null) {
        is.close();
      }
    }

    HSSFWorkbook wb = new HSSFWorkbook(excelIn);

    HSSFSheet sheet = wb.getSheetAt(0);

    List<String> delimiters = new ArrayList<String>();
    delimiters.add(TextFileDelimiters.TAB.toString());

    String[] galAttrName = {"Gene 1", "Gene 2", "Interaction Type", "Gene", "GO Group"};
    Byte[] galAttrTypes = {
      CyAttributes.TYPE_STRING,
      CyAttributes.TYPE_STRING,
      CyAttributes.TYPE_STRING,
      CyAttributes.TYPE_STRING,
      CyAttributes.TYPE_STRING
    };
    NetworkTableMappingParameters mapping =
        new NetworkTableMappingParameters(
            delimiters,
            TextFileDelimiters.PIPE.toString(),
            galAttrName,
            galAttrTypes,
            null,
            null,
            0,
            1,
            2,
            null);

    CyNetwork net = null;
    try {
      reader = new ExcelNetworkSheetReader(wb.getSheetName(0), sheet, mapping, 1);
      net = Cytoscape.createNetwork(reader, false, null);
    } catch (Exception ee) {
      ee.printStackTrace();
      fail("Caught exception");
    }

    assertEquals(222, net.getNodeCount());
    assertEquals(443, net.getEdgeCount());

    CyAttributes attr = Cytoscape.getEdgeAttributes();

    // test some random edges
    assertEquals("cc", attr.getStringAttribute("YDR459C (cc) YNL271C", "interaction"));
    assertEquals("Transport", attr.getStringAttribute("YDR459C (cc) YNL271C", "GO Group"));
    assertEquals("YPR011C", attr.getStringAttribute("YDR459C (cc) YNL271C", "Gene"));

    assertNull(attr.getStringAttribute("YEL040W (cc) YER016W", "GO Group"));
    assertNull(attr.getStringAttribute("YEL040W (cc) YER016W", "Gene"));

    Cytoscape.destroyNetwork(net);
  }