Esempio n. 1
0
  /**
   * XML Circuit constructor
   *
   * @param file the file that contains the XML description of the circuit
   * @param g the graphics that will paint the node
   * @throws CircuitLoadingException if the internal circuit can not be loaded
   */
  public CircuitUI(File file, Graphics g) throws CircuitLoadingException {
    this("");

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    Document doc;
    Element root;

    Hashtable<Integer, Link> linkstable = new Hashtable<Integer, Link>();

    try {
      builder = factory.newDocumentBuilder();
      doc = builder.parse(file);
    } catch (SAXException sxe) {
      throw new CircuitLoadingException("SAX exception raised, invalid XML file.");
    } catch (ParserConfigurationException pce) {
      throw new CircuitLoadingException(
          "Parser exception raised, parser configuration is invalid.");
    } catch (IOException ioe) {
      throw new CircuitLoadingException("I/O exception, file cannot be loaded.");
    }

    root = (Element) doc.getElementsByTagName("Circuit").item(0);
    this.setName(root.getAttribute("name"));

    NodeList nl = root.getElementsByTagName("Node");
    Element e;
    Node n;
    Class cl;

    for (int i = 0; i < nl.getLength(); ++i) {
      e = (Element) nl.item(i);

      try {
        cl = Class.forName(e.getAttribute("class"));
      } catch (Exception exc) {
        System.err.println(exc.getMessage());
        throw new RuntimeException("Circuit creation from xml.");
      }

      try {
        n = ((Node) cl.newInstance());
      } catch (Exception exc) {
        System.err.println(exc.getMessage());
        throw new RuntimeException("Circuit creation from xml.");
      }

      this.nodes.add(n);
      n.setLocation(new Integer(e.getAttribute("x")), new Integer(e.getAttribute("y")));

      if (n instanceof giraffe.ui.Nameable) ((Nameable) n).setNodeName(e.getAttribute("node_name"));

      if (n instanceof giraffe.ui.CompositeNode) {
        try {
          ((CompositeNode) n)
              .load(new File(file.getParent() + "/" + e.getAttribute("file_name")), g);
        } catch (Exception exc) {
          /* try to load from the lib */
          ((CompositeNode) n)
              .load(new File(giraffe.Giraffe.PATH + "/lib/" + e.getAttribute("file_name")), g);
        }
      }

      NodeList nlist = e.getElementsByTagName("Anchor");
      Element el;

      for (int j = 0; j < nlist.getLength(); ++j) {
        el = (Element) nlist.item(j);

        Anchor a = n.getAnchor(new Integer(el.getAttribute("id")));
        NodeList linklist = el.getElementsByTagName("Link");
        Element link;
        Link l;

        for (int k = 0; k < linklist.getLength(); ++k) {
          link = (Element) linklist.item(k);
          int id = new Integer(link.getAttribute("id"));
          int index = new Integer(link.getAttribute("index"));

          if (id >= this.linkID) linkID = id + 1;

          if (linkstable.containsKey(id)) {
            l = linkstable.get(id);
            l.addLinkedAnchorAt(a, index);
            a.addLink(l);
          } else {
            l = new Link(id);
            l.addLinkedAnchorAt(a, index);
            this.links.add(l);
            linkstable.put(id, l);
            a.addLink(l);
          }
        }
      }
    }
  }
Esempio n. 2
0
  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();
    }
  }