예제 #1
0
 public String getShortFileName() {
   return m_fileName.getName();
 }
예제 #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();
    }
  }
  public static boolean transform(
      File origXmlFileOrDir, File xslFile, File targetDir, String extension) {
    logger.logComment(
        "Going to transform " + origXmlFileOrDir + " into dir " + targetDir + " using: " + xslFile,
        true);

    if (!origXmlFileOrDir.exists()) {
      GuiUtils.showErrorMessage(
          logger,
          "Warning, XML file/directory: " + origXmlFileOrDir + " doesn't exist",
          null,
          null);
      return false;
    }

    if (!xslFile.exists()) {
      GuiUtils.showErrorMessage(
          logger, "Warning, XSL file: " + xslFile + " doesn't exist", null, null);
      return false;
    }

    if (!targetDir.exists()) {
      GuiUtils.showErrorMessage(
          logger, "Warning, target directory: " + targetDir + " doesn't exist", null, null);
      return false;
    }

    if (origXmlFileOrDir.isDirectory()) {
      logger.logComment("That file is a directory. Converting all of the XML files in it");
      File[] files = origXmlFileOrDir.listFiles();

      boolean totalSuccess = true;
      for (int i = 0; i < files.length; i++) {
        if (!files[i].isDirectory()
            && (files[i].getName().endsWith(".xml") || files[i].getName().endsWith(".XML"))) {
          boolean partialSuccess = transform(files[i], xslFile, targetDir, extension);

          totalSuccess = totalSuccess || partialSuccess;
        } else if (files[i].isDirectory() && !GeneralUtils.isVersionControlDir(files[i])) {
          File newFolder = new File(targetDir, files[i].getName());
          newFolder.mkdir();

          logger.logComment(
              "Found a sub folder. Going to convert all there into: " + newFolder + "...");

          transform(files[i], xslFile, newFolder, extension);
        }
      }
      return totalSuccess;
    }

    String result = transform(origXmlFileOrDir, xslFile);

    String newName = origXmlFileOrDir.getName();

    if (newName.endsWith(".xml") || newName.endsWith(".XML")) {
      newName = newName.substring(0, newName.length() - 4) + extension;
    }
    File targetFile = new File(targetDir, newName);

    try {
      FileWriter fw = new FileWriter(targetFile);
      fw.write(result);
      fw.close();
    } catch (IOException ex) {
      GuiUtils.showErrorMessage(logger, "Exception writing to file: " + targetFile, ex, null);
      return false;
    }

    logger.logComment("The result is in " + targetFile + " *************");

    return result != null;
  }