private int findAttr(String attrName, List attrList) { for (int i = 0; i < attrList.size(); i++) { NWBAttribute attr = (NWBAttribute) attrList.get(i); if (attr.getAttrName().equalsIgnoreCase(attrName)) { return i; } } return -1; }
private void printGraph(PrintWriter out, ValidateNWBFile validator, BufferedReader reader) throws IOException { if (validator.getHasTotalNumOfNodes() && validator.getSkipNodeList()) { int totalNumOfNodes = validator.getTotalNumOfNodes(); for (int i = 1; i <= totalNumOfNodes; i++) { out.println("<node id=\"" + i + "\">"); out.println("<data key=\"label\">" + i + "</data></node>"); } } // read from nwb file and write to the graphml file boolean inNodesSection = false; boolean inDirectededgesSection = false; boolean inUndirectededgesSection = false; String line = reader.readLine(); int edgeID = 0; while (line != null) { if (line.length() == 0) { line = reader.readLine(); continue; } String line_lower = line.toLowerCase(); // find node section header that looks like // *nodes or *nodes 1000 if (line_lower.startsWith(NWBFileProperty.HEADER_NODE)) { inNodesSection = true; inDirectededgesSection = false; inUndirectededgesSection = false; line = reader.readLine(); continue; } if (line_lower.startsWith(NWBFileProperty.HEADER_DIRECTED_EDGES)) { inDirectededgesSection = true; inNodesSection = false; inUndirectededgesSection = false; line = reader.readLine(); continue; } if (line_lower.startsWith(NWBFileProperty.HEADER_UNDIRECTED_EDGES)) { inUndirectededgesSection = true; inNodesSection = false; inDirectededgesSection = false; line = reader.readLine(); continue; } if (inNodesSection) { // ignore attribute list line or comment line(s) if (line_lower.startsWith(NWBFileProperty.ATTRIBUTE_ID) || line_lower.startsWith(NWBFileProperty.PREFIX_COMMENTS + NWBFileProperty.ATTRIBUTE_ID) || line_lower.startsWith(NWBFileProperty.PREFIX_COMMENTS)) { line = reader.readLine(); continue; } else { StringTokenizer st = new StringTokenizer(line); String[] columns = validator.processTokens(st); List nodeAttrList = validator.getNodeAttrList(); out.println("<node id=\"" + columns[0] + "\">"); for (int i = 1; i < nodeAttrList.size(); i++) { NWBAttribute attr = (NWBAttribute) nodeAttrList.get(i); String value = columns[i]; if (attr.getDataType().equalsIgnoreCase(NWBFileProperty.TYPE_STRING)) { if (value.startsWith("\"")) { value = value.substring(1); } if (value.endsWith("\"")) { value = value.substring(0, value.length() - 1); } } out.println("<data key=\"" + attr.getAttrName() + "\">" + value + "</data>"); } out.println("</node>"); } } // end if (inNodesSection) if (inDirectededgesSection || inUndirectededgesSection) { // ignore attribute list line or comment line(s) if (line_lower.startsWith(NWBFileProperty.ATTRIBUTE_SOURCE) || line_lower.startsWith( NWBFileProperty.PREFIX_COMMENTS + NWBFileProperty.ATTRIBUTE_SOURCE) || line_lower.startsWith(NWBFileProperty.PREFIX_COMMENTS)) { line = reader.readLine(); continue; } else { List edgeAttrList = new ArrayList(); StringTokenizer st = new StringTokenizer(line); String[] columns = validator.processTokens(st); if (inDirectededgesSection) edgeAttrList = validator.getDirectedEdgeAttrList(); else if (inUndirectededgesSection) edgeAttrList = validator.getUndirectedEdgeAttrList(); edgeID++; // find if there is an id attribute int idColumnNumber = findAttr(NWBFileProperty.ATTRIBUTE_ID, edgeAttrList); int sourceColumnNumber = findAttr(NWBFileProperty.ATTRIBUTE_SOURCE, edgeAttrList); int targetColumnNumber = findAttr(NWBFileProperty.ATTRIBUTE_TARGET, edgeAttrList); if (idColumnNumber == -1) { if (edgeAttrList.size() > 2) { out.println( "<edge id=\"" + edgeID + "\" source=\"" + columns[sourceColumnNumber] + "\" target=\"" + columns[targetColumnNumber] + "\">"); for (int i = 0; i < edgeAttrList.size(); i++) { NWBAttribute attr = (NWBAttribute) edgeAttrList.get(i); String attrName = attr.getAttrName(); if (!(attrName.equalsIgnoreCase(NWBFileProperty.ATTRIBUTE_SOURCE) || attrName.equalsIgnoreCase(NWBFileProperty.ATTRIBUTE_TARGET) || attrName.equalsIgnoreCase(NWBFileProperty.ATTRIBUTE_ID))) { String value = columns[i]; if (attr.getDataType().equalsIgnoreCase(NWBFileProperty.TYPE_STRING)) { if (value.startsWith("\"")) { value = value.substring(1); } if (value.endsWith("\"")) { value = value.substring(0, value.length() - 1); } } out.println("<data key=\"" + attr.getAttrName() + "\">" + value + "</data>"); } } out.println("</edge>"); } else { out.println( "<edge id=\"" + edgeID + "\" source=\"" + columns[sourceColumnNumber] + "\" target=\"" + columns[targetColumnNumber] + "\"/>"); } } else { if (edgeAttrList.size() > 3) { out.println( "<edge id=\"" + columns[idColumnNumber] + "\" source=\"" + columns[sourceColumnNumber] + "\" target=\"" + columns[targetColumnNumber] + "\">"); for (int i = 0; i < edgeAttrList.size(); i++) { NWBAttribute attr = (NWBAttribute) edgeAttrList.get(i); String attrName = attr.getAttrName(); if (!(attrName.equalsIgnoreCase(NWBFileProperty.ATTRIBUTE_SOURCE) || attrName.equalsIgnoreCase(NWBFileProperty.ATTRIBUTE_TARGET) || attrName.equalsIgnoreCase(NWBFileProperty.ATTRIBUTE_ID))) { out.println("<data key=\"" + attr.getAttrName() + "\">" + columns[i] + "</data>"); } } out.println("</edge>"); } else { out.println( "<edge id=\"" + columns[idColumnNumber] + "\" source=\"" + columns[sourceColumnNumber] + "\" target=\"" + columns[targetColumnNumber] + "\"/>"); } } } } // end if (inDirectededgesSection || inUndirectededgesSection) line = reader.readLine(); } // end while }
private void writeEdges(String s, PrintWriter out, ValidateNWBFile validator, List edgeAttrList) throws Exception { // System.out.println(s); out.flush(); int i = 0; String[] columns = NETFileFunctions.processTokens(s); /*if (inDirectededgesSection) edgeAttrList = validator.getDirectedEdgeAttrList(); else if (inUndirectededgesSection) edgeAttrList = validator.getUndirectedEdgeAttrList();*/ for (Iterator ii = edgeAttrList.iterator(); ii.hasNext(); ) { NWBAttribute na = (NWBAttribute) ii.next(); String value = columns[i]; // System.out.println(value); // System.out.print(na.getAttrName()+ " "); if (value.equalsIgnoreCase("*")) { } else if (na.getAttrName().equals(NETFileProperty.ATTRIBUTE_LABEL) || na.getAttrName().equals(ARCEDGEParameter.PARAMETER_LABEL)) { out.print(ARCEDGEParameter.PARAMETER_LABEL + " \"" + value + "\" "); } else if ((NETFileFunctions.isInList(na.getAttrName(), noPrintParameters)) && !(na.getAttrName().equals(NETFileProperty.ATTRIBUTE_LABEL))) { if (na.getDataType().equalsIgnoreCase(NWBFileProperty.TYPE_STRING)) { String[] sa = value.split(" "); if (sa.length > 1) out.print(" \"" + value + "\" "); else out.print(value + " "); } else { if (na.getAttrName().equals(NWBFileProperty.ATTRIBUTE_SOURCE) || na.getAttrName().equals(NWBFileProperty.ATTRIBUTE_TARGET)) { try { value = ((Integer) this.vertexToIdMap.get(new Integer(value))).toString(); } catch (NullPointerException npe) { throw new Exception("Edge references an undefined node " + value); } } out.print(value + " "); } } else if (na.getDataType().equalsIgnoreCase("float") || na.getDataType().equalsIgnoreCase("int")) { if (!value.equalsIgnoreCase("")) { // System.out.print(na.getAttrName() + " " + value + " "); String ss = na.getAttrName(); if (ss.matches("[bil]?c1")) { ss = ss.replace("1", ""); ss += " " + value + " "; for (int j = 1; j < 3; j++) { ss += columns[j + i] + " "; ii.next(); } i = i + 2; out.print(ss); } else out.print(na.getAttrName() + " " + value + " "); } } else if (na.getDataType().equalsIgnoreCase("string")) { if (!value.equalsIgnoreCase("")) { if (na.getAttrName().startsWith("unknown")) { String[] sa = value.split(" "); if (sa.length > 1) out.print(" \"" + value + "\" "); else out.print(value + " "); } else out.print(na.getAttrName() + " \"" + value + "\" "); } } else ; i++; } out.print("\r\n"); }
// write GraphML-Attributes private void writeAttributes(PrintWriter out, ValidateNWBFile validator) { // first handle node attributes List array = validator.getNodeAttrList(); for (int i = 0; i < array.size(); i++) { NWBAttribute attr = (NWBAttribute) array.get(i); String attrName = attr.getAttrName(); if (attrName.equalsIgnoreCase(NWBFileProperty.ATTRIBUTE_ID)) { continue; } out.println( "<key id=\"" + attr.getAttrName() + "\" for=\"node\" attr.name=\"" + attr.getAttrName() + "\" attr.type=\"" + attr.getDataType() + "\" /> "); } if (validator.isDirectedGraph() && !validator.isUndirectedGraph()) { // this is a directed graph array = validator.getDirectedEdgeAttrList(); for (int i = 0; i < array.size(); i++) { NWBAttribute attr = (NWBAttribute) array.get(i); String attrName = attr.getAttrName(); if (attrName.equalsIgnoreCase(NWBFileProperty.ATTRIBUTE_SOURCE) || attrName.equalsIgnoreCase(NWBFileProperty.ATTRIBUTE_TARGET) || attrName.equalsIgnoreCase(NWBFileProperty.ATTRIBUTE_ID)) { continue; } out.println( "<key id=\"" + attr.getAttrName() + "\" for=\"edge\" attr.name=\"" + attr.getAttrName() + "\" attr.type=\"" + attr.getDataType() + "\" />"); } out.println("<graph edgedefault=\"directed\">"); } else if (!validator.isDirectedGraph() && validator.isUndirectedGraph()) { // this is a undirected graph array = validator.getUndirectedEdgeAttrList(); for (int i = 0; i < array.size(); i++) { NWBAttribute attr = (NWBAttribute) array.get(i); String attrName = attr.getAttrName(); if (attrName.equalsIgnoreCase(NWBFileProperty.ATTRIBUTE_SOURCE) || attrName.equalsIgnoreCase(NWBFileProperty.ATTRIBUTE_TARGET) || attrName.equalsIgnoreCase(NWBFileProperty.ATTRIBUTE_ID)) { continue; } out.println( "<key id=\"" + attr.getAttrName() + "\" for=\"edge\" attr.name=\"" + attr.getAttrName() + "\" attr.type=\"" + attr.getDataType() + "\" /> "); } out.println("<graph edgedefault=\"undirected\">"); } else if (validator.isDirectedGraph() && validator.isUndirectedGraph()) { // hybrid graph, don't know how to handle it??? } }
private void writeNodes( String s, PrintWriter out, ValidateNWBFile validator, List nodeAttrList, int mapper) { out.flush(); String[] columns = NETFileFunctions.processTokens(s); int i = 0; for (Iterator ii = nodeAttrList.iterator(); ii.hasNext(); ) { NWBAttribute na = (NWBAttribute) ii.next(); String value = columns[i]; // System.out.print(value + "::"); // value.replace("\"", ""); if (value.equalsIgnoreCase("*")) { } else if (NETFileFunctions.isInList(na.getAttrName(), noPrintParameters)) { if (na.getDataType().equalsIgnoreCase(NWBFileProperty.TYPE_STRING)) { String[] sa = value.split(" "); if (sa.length > 1) out.print(" \"" + value + "\" "); else out.print(value + " "); } else { if (na.getAttrName().equalsIgnoreCase(NWBFileProperty.ATTRIBUTE_ID)) { this.vertexToIdMap.put(new Integer(value), new Integer(mapper)); out.print(mapper + " "); } else out.print(value + " "); } } else if (na.getDataType().equalsIgnoreCase("float") || na.getDataType().equalsIgnoreCase("int")) { if (!value.equalsIgnoreCase("")) { // System.out.print(na.getAttrName() + " " + value + " "); String ss = na.getAttrName(); if (ss.matches("[bil]?c1")) { ss = ss.replace("1", ""); ss += " " + value + " "; for (int j = 1; j < 3; j++) { ss += columns[j + i] + " "; ii.next(); } i = i + 2; out.print(ss); } else out.print(na.getAttrName() + " " + value + " "); } } else if (na.getDataType().equalsIgnoreCase("string")) { if (!value.equalsIgnoreCase("")) { if (na.getAttrName().startsWith("unknown")) { String[] sa = value.split(" "); if (sa.length > 1) out.print(" \"" + value + "\" "); else out.print(value + " "); } else out.print(na.getAttrName() + " \"" + value + "\" "); } } else ; i++; } out.print("\r\n"); }