public static String cleanForXML(String line) { StringBuilder buffer = new StringBuilder(); for (int i = 0; i < line.length(); i++) { char c = line.charAt(i); if (c < 128 && Character.isLetter(c)) { buffer.append(c); } else { buffer.append('_'); } } return buffer.toString(); }
/** * Get any children that match this name or path. Similar to getChild(), but will grab multiple * matches rather than only the first. * * @param name element name or path/to/element * @return array of child elements that match * @author processing.org */ public XML[] getChildren(String name) { if (name.length() > 0 && name.charAt(0) == '/') { throw new IllegalArgumentException("getChildren() should not begin with a slash"); } if (name.indexOf('/') != -1) { return getChildrenRecursive(PApplet.split(name, '/'), 0); } // if it's a number, do an index instead // (returns a single element array, since this will be a single match if (Character.isDigit(name.charAt(0))) { return new XML[] {getChild(Integer.parseInt(name))}; } int childCount = getChildCount(); XML[] matches = new XML[childCount]; int matchCount = 0; for (int i = 0; i < childCount; i++) { XML kid = getChild(i); String kidName = kid.getName(); if (kidName != null && kidName.equals(name)) { matches[matchCount++] = kid; } } return (XML[]) PApplet.subset(matches, 0, matchCount); }
/** * Internal helper function for getChild(String). * * @param items result of splitting the query on slashes * @param offset where in the items[] array we're currently looking * @return matching element or null if no match * @author processing.org */ protected XML getChildRecursive(String[] items, int offset) { // if it's a number, do an index instead if (Character.isDigit(items[offset].charAt(0))) { XML kid = getChild(Integer.parseInt(items[offset])); if (offset == items.length - 1) { return kid; } else { return kid.getChildRecursive(items, offset + 1); } } int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { XML kid = getChild(i); String kidName = kid.getName(); if (kidName != null && kidName.equals(items[offset])) { if (offset == items.length - 1) { return kid; } else { return kid.getChildRecursive(items, offset + 1); } } } return null; }
public void run() { // String a = // "http://neuromorpho.org/neuroMorpho/dableFiles/borst/CNG%20version/dCH-cobalt.CNG.swc"; // // For developer use if (myArgs.length == 0) { String usage = "\nError, missing SWC file containing morphology!\n\nUsage: \n java -cp build cvapp.main swc_file [-test]" + "\n or:\n ./run.sh swc_file [" + TEST_FLAG + "|" + TEST_NOGUI_FLAG + "|" + NEUROML1_EXPORT_FLAG + "|" + NEUROML2_EXPORT_FLAG + "]\n\n" + "where swc_file is the file name or URL of the SWC morphology file\n"; System.out.println(usage); System.exit(0); } String a = myArgs[0]; File baseDir = new File("."); if ((new File(a)).exists()) { baseDir = (new File(a)).getParentFile(); } try { File root = new File(main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()) .getParentFile(); if (!a.startsWith("http://") && !a.startsWith("file://")) { a = "file://" + (new File(a)).getCanonicalPath(); } boolean supressGui = false; if (myArgs.length == 2 && (myArgs[1].equals(TEST_NOGUI_FLAG) || myArgs[1].equals(NEUROML1_EXPORT_FLAG) || myArgs[1].equals(NEUROML2_EXPORT_FLAG))) { supressGui = true; } neuronEditorFrame nef = null; nef = new neuronEditorFrame(700, 600, supressGui); // nef.validate(); nef.pack(); centerWindow(nef); nef.setVisible(!supressGui); nef.setReadWrite(true, true); int indexof = a.lastIndexOf('/') + 1; String directory = a.substring(0, indexof); String fileName = a.substring(indexof, a.length()); URL u = new URL(a); String sdata[] = readStringArrayFromURL(u); nef.setTitle("3DViewer (Modified from CVAPP with permission)-Neuron: " + fileName); nef.loadFile(sdata, directory, fileName); System.out.println("Loaded: " + fileName); if (myArgs.length == 2 && myArgs[1].equals(TEST_ONE_FLAG)) { // Thread.sleep(1000); doTests(nef, fileName); } else if (myArgs.length == 2 && myArgs[1].equals(NEUROML1_EXPORT_FLAG)) { File rootFile = (new File(baseDir, fileName)).getAbsoluteFile(); String nml1FileName = rootFile.getName().endsWith(".swc") ? rootFile.getName().substring(0, rootFile.getName().length() - 4) + ".xml" : rootFile.getName() + ".xml"; File nml1File = new File(rootFile.getParentFile(), nml1FileName); neuronEditorPanel nep = nef.getNeuronEditorPanel(); nep.writeStringToFile(nep.getCell().writeNeuroML_v1_8_1(), nml1File.getAbsolutePath()); System.out.println( "Saved NeuroML representation of the file to: " + nml1File.getAbsolutePath() + ": " + nml1File.exists()); File v1schemaFile = new File(root, "Schemas/v1.8.1/Level3/NeuroML_Level3_v1.8.1.xsd"); validateXML(nml1File, v1schemaFile); System.exit(0); } else if (myArgs.length == 2 && myArgs[1].equals(NEUROML2_EXPORT_FLAG)) { File rootFile = (new File(baseDir, fileName)).getAbsoluteFile(); String nml2FileName = rootFile.getName().endsWith(".swc") ? rootFile.getName().substring(0, rootFile.getName().length() - 4) + ".cell.nml" : rootFile.getName() + ".cell.nml"; if (Character.isDigit(nml2FileName.charAt(0))) { nml2FileName = "Cell_" + nml2FileName; } File nml2File = new File(rootFile.getParentFile(), nml2FileName); neuronEditorPanel nep = nef.getNeuronEditorPanel(); nep.writeStringToFile(nep.getCell().writeNeuroML_v2beta(), nml2File.getAbsolutePath()); System.out.println( "Saved the NeuroML representation of the file to: " + nml2File.getAbsolutePath() + ": " + nml2File.exists()); validateXML(nml2File, new File(root, "Schemas/v2/NeuroML_v2beta4.xsd")); System.exit(0); } else if (myArgs.length == 2 && (myArgs[1].equals(TEST_FLAG) || (myArgs[1].equals(TEST_NOGUI_FLAG)))) { // Thread.sleep(1000); doTests(nef, fileName); File exampleDir = new File("twoCylSwc"); for (File f : exampleDir.listFiles()) { if (f.getName().endsWith(".swc")) { sdata = fileString.readStringArrayFromFile(f.getAbsolutePath()); nef.setTitle("3DViewer (Modified from CVAPP with permission)-Neuron: " + f.getName()); nef.loadFile(sdata, f.getParent(), f.getName()); doTests(nef, f.getAbsolutePath()); } } exampleDir = new File("spherSomaSwc"); for (File f : exampleDir.listFiles()) { if (f.getName().endsWith(".swc")) { sdata = fileString.readStringArrayFromFile(f.getAbsolutePath()); nef.setTitle("3DViewer (Modified from CVAPP with permission)-Neuron: " + f.getName()); nef.loadFile(sdata, f.getParent(), f.getName()); doTests(nef, f.getAbsolutePath()); } } exampleDir = new File("caseExamples"); for (File f : exampleDir.listFiles()) { if (f.getName().endsWith(".swc")) { sdata = fileString.readStringArrayFromFile(f.getAbsolutePath()); nef.setTitle("3DViewer (Modified from CVAPP with permission)-Neuron: " + f.getName()); nef.loadFile(sdata, f.getParent(), f.getName()); doTests(nef, f.getAbsolutePath()); } } if (supressGui) System.exit(0); } } catch (Exception exception) { System.err.println("Error while handling SWC file (" + a + ")"); exception.printStackTrace(); } }
/* * Carries out a number of tests, generates NEURON, GENESIS and NeuroML code etc. */ private static void doTests(neuronEditorFrame nef, String fileName) { System.out.println( "Testing Cvapp/NeuroMorpho.Org by generating NEURON, GENESIS and NeuroML files for " + fileName); File tempDir = new File("temp"); if (!tempDir.exists()) tempDir.mkdir(); neuronEditorPanel nep = nef.getNeuronEditorPanel(); String rootFileName = fileName; if (rootFileName.toLowerCase().endsWith(".swc")) { rootFileName = rootFileName.substring(0, rootFileName.length() - 4); } if (rootFileName.lastIndexOf(System.getProperty("file.separator")) > 0) { rootFileName = rootFileName.substring( rootFileName.lastIndexOf(System.getProperty("file.separator")) + 1); } // NEURON save... String neuronFileName = rootFileName + ".hoc"; File neuronFile = new File(tempDir, neuronFileName); File neuronTestFile = new File(tempDir, rootFileName + "_test.hoc"); nep.writeStringToFile(nep.getCell().HOCwriteNS(), neuronFile.getAbsolutePath()); StringBuilder sbNeuTest = new StringBuilder(); sbNeuTest.append("load_file(\"nrngui.hoc\")\n"); sbNeuTest.append("load_file(\"../neuronUtils/nCtools.hoc\")\n"); sbNeuTest.append("load_file(\"../neuronUtils/cellCheck.hoc\")\n"); sbNeuTest.append("load_file(\"nrngui.hoc\")\n"); sbNeuTest.append("load_file(\"" + neuronFileName + "\")\n\n"); sbNeuTest.append("forall morph()\n"); System.out.println("--------------------------------------------------------------"); nep.writeStringToFile(sbNeuTest.toString(), neuronTestFile.getAbsolutePath()); System.out.println( "Saved NEURON representation of the file to: " + neuronFile.getAbsolutePath() + ": " + neuronFile.exists()); System.out.println("--------------------------------------------------------------"); // GENESIS save... String genesisFileName = rootFileName + ".p"; File genesisFile = new File(tempDir, genesisFileName); File genesisTestFile = new File(tempDir, rootFileName + "_test.g"); nep.writeStringToFile(nep.getCell().GENESISwriteHR(), genesisFile.getAbsolutePath()); StringBuilder sbGenTest = new StringBuilder(); sbGenTest.append("include compartments \n"); sbGenTest.append("create neutral /library\n"); sbGenTest.append("disable /library\n"); sbGenTest.append("ce /library\n"); sbGenTest.append("make_cylind_compartment\n"); sbGenTest.append("make_cylind_symcompartment\n"); sbGenTest.append("make_sphere_compartment\n"); sbGenTest.append("ce /\n"); sbGenTest.append( "echo \"Prototype compartments created, reading cell from " + genesisFileName + "\"\n"); sbGenTest.append("readcell " + genesisFileName + " /mycell\n\n"); sbGenTest.append("create xform /form [0,0,400,400] -nolabel\n"); sbGenTest.append( "create xdraw /form/draw [0,0,100%,100%] -wx 0.002 -wy 0.002 -transform ortho3d -bg white\n"); sbGenTest.append( "setfield /form/draw xmin -3.0E-4 xmax 3.0E-4 ymin -3.0E-4 ymax 3.0E-4 vx 0.0 vy 0.0 vz -0.002\n"); sbGenTest.append( "create xcell /form/draw/cell -path \"/mycell/##[][TYPE=compartment],/mycell/##[][TYPE=symcompartment]\" -colfield Vm -colmin -0.07 -colmax 0.03 -diarange -5\n"); sbGenTest.append("xcolorscale hot\n"); sbGenTest.append("xshow /form\n\n"); sbGenTest.append("showfield /mycell/##[][TYPE=compartment] **\n\n"); nep.writeStringToFile(sbGenTest.toString(), genesisTestFile.getAbsolutePath()); System.out.println( "Saved GENESIS representation of the file to: " + genesisFile.getAbsolutePath() + ": " + genesisFile.exists()); System.out.println("--------------------------------------------------------------"); // NeuroML save... String nml1FileName = rootFileName + ".xml"; File nml1File = new File(tempDir, nml1FileName); nep.writeStringToFile(nep.getCell().writeNeuroML_v1_8_1(), nml1File.getAbsolutePath()); System.out.println( "Saved NeuroML representation of the file to: " + nml1File.getAbsolutePath() + ": " + nml1File.exists()); File v1schemaFile = new File("Schemas/v1.8.1/Level3/NeuroML_Level3_v1.8.1.xsd"); validateXML(nml1File, v1schemaFile); String nml2FileName = rootFileName + ".cell.nml"; if (Character.isDigit(nml2FileName.charAt(0))) { nml2FileName = "Cell_" + nml2FileName; } File nml2File = new File(tempDir, nml2FileName); nep.writeStringToFile(nep.getCell().writeNeuroML_v2beta(), nml2File.getAbsolutePath()); System.out.println( "Saved NeuroML representation of the file to: " + nml2File.getAbsolutePath() + ": " + nml2File.exists()); validateXMLWithURL(nml2File, "Schemas/v2/NeuroML_v2beta4.xsd"); }