⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileconvertor.java

📁 化学图形处理软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    private String getOutputFileName(String inputFilename, String outputFormat) {        String outputFilename = inputFilename.substring(0,inputFilename.lastIndexOf('.'));        outputFilename = outputFilename.substring(outputFilename.lastIndexOf(File.separatorChar) + 1);        outputFilename = outputFilename + ".";        if (outputFormat.equalsIgnoreCase("CML")) {            outputFilename = outputFilename + "cml";        } else if (outputFormat.equalsIgnoreCase("MOL")) {            outputFilename = outputFilename + "mol";        } else if (outputFormat.equalsIgnoreCase("SMI")) {            outputFilename = outputFilename + "smi";        } else if (outputFormat.equalsIgnoreCase("SHELX")) {            outputFilename = outputFilename + "res";        } else if (outputFormat.equalsIgnoreCase("SVG")) {            outputFilename = outputFilename + "svg";        } else if (outputFormat.equalsIgnoreCase("XYZ")) {            outputFilename = outputFilename + "xyz";        } else if (outputFormat.equalsIgnoreCase("PDB")) {            outputFilename = outputFilename + "pdb";        } else if (outputFormat.equalsIgnoreCase("GIN")) {            outputFilename = outputFilename + "in";        } else if (outputFormat.equalsIgnoreCase("CDK")) {            outputFilename = outputFilename + "java.fragment";        } else if (outputFormat.equalsIgnoreCase("HIN")) {            outputFilename = outputFilename + "hin";        }        return outputFilename;    }    private void printHelp(Options options) {        HelpFormatter formatter = new HelpFormatter();        formatter.printHelp("FileConvertor", options);                // now report on the supported formats        System.out.println();        System.out.println(" OUTPUT FORMATS:");        System.out.println("  cml    Chemical Markup Language (the default)");        System.out.println("  gin    Gaussian Input File");        System.out.println("  hin    Hyperchem file");        System.out.println("  mol    MDL molfile");        System.out.println("  pdb    PDB");        System.out.println("  shelx  ShelX");        System.out.println("  smi    SMILES");        System.out.println("  svg    Scalable Vector Graphics");        System.out.println("  xyz    XYZ");                System.exit(0);    }        /**     * Parses the options in the command line arguments and returns     * the index of the first non-option argument.     */    private String[] parseCommandLineOptions(String[] args) {        Options options = new Options();        options.addOption("h", "help", false, "give this help page");        options.addOption(            OptionBuilder.withLongOpt("question").                          withDescription("level of IO questions [none|fewest|some|all]").                          withValueSeparator('=').                          hasArg().                          create("q")        );        options.addOption(            OptionBuilder.withLongOpt("outputformat").                          withDescription("see below for supported formats (CML2 is default)").                          withValueSeparator('=').                          hasArg().                          create("o")        );        options.addOption(            OptionBuilder.withLongOpt("listoptions").                          withDescription("lists the IO questions for the given format").                          withValueSeparator('=').                          hasArg().                          create("l")        );        options.addOption(            OptionBuilder.withLongOpt("properties").                          withDescription("Java properties file with the IO settings").                          withValueSeparator('=').                          hasArg().                          create("p")        );        options.addOption(            OptionBuilder.withLongOpt("addHydrogens").                          withDescription("add explicit hydrogens where missing").                          create("a")        );        options.addOption(            OptionBuilder.withLongOpt("removeHydrogens").                          withDescription("remove all explicit hydrogens").                          create("r")        );        options.addOption(            OptionBuilder.withLongOpt("create2DCoordinates").                          withDescription("create 2D coordinates using a layout algorithm").                          create("2")        );        options.addOption(            OptionBuilder.withLongOpt("rebondFrom3DCoordinates").                          withDescription("calculate bonds from 3D coordinates").                          create("b")        );                CommandLine line = null;        try {            CommandLineParser parser = new PosixParser();            line = parser.parse(options, args);        } catch (ParseException exception) {            System.err.println("Unexpected exception: " + exception.toString());        }            if (line.hasOption("o")) {            this.oformat = line.getOptionValue("o");        }        if (line.hasOption("q")) {            String level = line.getOptionValue("q");            if ("none".equals(level)) {                settingListener = new TextGUIListener(0);            } else if ("fewest".equals(level)) {                settingListener = new TextGUIListener(1);            } else if ("some".equals(level)) {                settingListener = new TextGUIListener(2);            } else if ("all".equals(level)) {                settingListener = new TextGUIListener(3);            } else {                System.out.println("Only supported levels: none, fewest, some, all");                System.exit(1);            }        }        if (line.hasOption("l")) {            listOptionsForIOClass(line.getOptionValue("l"));            System.exit(0);        }        if (line.hasOption("p")) {            String filename = line.getOptionValue("p");            try {                File file = new File(filename);                Properties props = new Properties();                props.load(new FileInputStream(file));                propsListener = new PropertiesListener(props);                settingListener = null;            } catch (FileNotFoundException exception) {                System.out.println("Cannot find properties file: " + filename);                System.exit(1);            } catch (IOException exception) {                System.out.println("Cannot read properties file: " + filename);                System.exit(1);            }        }        if (line.hasOption("a")) {            this.applyHAdding = true;        }        if (line.hasOption("r")) {            this.applyHRemoval = true;        }        if (line.hasOption("2")) {            this.apply2DCleanup = true;        }        if (line.hasOption("b")) {            this.apply3DRebonding = true;        }        String[] filesToConvert = line.getArgs();                if (filesToConvert.length == 0 || line.hasOption("h")) {            printHelp(options);        }                return filesToConvert;    }    public void listOptionsForIOClass(String ioClassName) {        logger.debug("listing IOSetting options");                String className = "org.openscience.cdk.io." + ioClassName;        try {            Object readerOrWriter = this.getClass().getClassLoader().                loadClass(className).newInstance();            IOSetting[] settings = new IOSetting[0];            if (readerOrWriter instanceof IChemObjectIO) {                IChemObjectIO ioClass = (IChemObjectIO)readerOrWriter;                settings = ioClass.getIOSettings();            } else {                String message = "This class is not a CDK ChemObjectIO class";                System.out.println(message);                logger.error(message);                return;            }            TextGUIListener listener = new TextGUIListener(4); // ask all questions            listener.setInputReader(null); // but don't really ask them            for (int i=0; i<settings.length; i++) {                IOSetting setting = settings[i];                if (setting != null) {                    listener.processIOSettingQuestion(setting);                } else {                    String message = "This IOSetting is null";                    System.out.println(message);                    logger.warn(message);                }            }        } catch (ClassNotFoundException exception) {            String message = "This Reader/Writer does not exist: " + className;            System.out.println(message);            logger.error(message);            logger.debug(exception);        } catch (InstantiationException exception) {            String message = "Could not instantiate the class: " + className;            System.out.println(message);            logger.error(message);            logger.debug(exception);        } catch (Exception exception) {            System.out.println("An unknown exception occured: " + exception.toString());            logger.debug(exception);        }    }    /**    * Since we do not know what kind of IChemObject the Writer supports,    * and we want to output as much information as possible, use    * the generalized mechanism below.    */    private void write(IChemFile chemFile, String outputFilename) throws IOException {        if (cow.accepts(chemFile.getClass())) {            // Can write ChemFile, do so            try {                cow.write(chemFile);            } catch (CDKException e) {                logger.error("Could not write ChemFile. FIXME: I should recurse!");            }        } else {            logger.info("Cannot write ChemFile, recursing into ChemSequence's.");            int count = chemFile.getChemSequenceCount();            boolean needMoreFiles = (cow.accepts(IChemSequence.class)) && (count > 1);            for (int i=0; i < count; i++) {                if (needMoreFiles) {                    cow.close(); // possibly closing empty file                    String fname = outputFilename + "." + (i+1);                    FileWriter fileWriter = new FileWriter(new File(fname));                    cow = getChemObjectWriter(this.oformat, fileWriter);                }                write(chemFile.getChemSequence(i), outputFilename);            }        }    }    private void write(IChemSequence sequence, String outputFilename) throws IOException {        try {            cow.write(sequence);        } catch (CDKException exception) {            int count = sequence.getChemModelCount();            boolean needMoreFiles = (cow.accepts(IChemModel.class)) && (count > 1);            logger.info("Cannot write ChemSequence, recursing into ChemModel's.");            for (int i=0; i < count; i++) {                if (needMoreFiles) {                    cow.close(); // possibly closing empty file                    String fname = outputFilename + "." + (i+1);                    FileWriter fileWriter = new FileWriter(new File(fname));                    cow = getChemObjectWriter(this.oformat, fileWriter);                }                write(sequence.getChemModel(i), outputFilename);            }        }    }    private void write(IChemModel cm, String outputFilename) throws IOException {        try {            cow.write(cm);        } catch (CDKException exception) {            logger.info("Cannot write ChemModel, trying Crystal.");            ICrystal crystal = cm.getCrystal();            if (crystal != null) {                write(crystal, outputFilename);            }            IMoleculeSet som = cm.getMoleculeSet();            if (som != null) {                write(som, outputFilename);            }        }    }    private void write(ICrystal c, String outputFilename) throws IOException {        try {            cow.write(c);        } catch (CDKException exception) {            logger.error("Cannot write Crystal: ", exception.getMessage());        }    }    private void write(IMoleculeSet som, String outputFilename) throws IOException {        try {	        if (apply2DCleanup) {				logger.info("Creating 2D coordinates");				java.util.Iterator mols = som.molecules();				StructureDiagramGenerator sdg = new StructureDiagramGenerator();				while (mols.hasNext()) {					IMolecule molecule = (IMolecule)mols.next();		            try {		                sdg.setMolecule(molecule, false); // false -> don't make clone!		                sdg.generateCoordinates(new Vector2d(0, 1));		                molecule = sdg.getMolecule();		            } catch (Exception exception) {		                System.out.println("Could not generate coordinates for this molecule.");		                System.exit(1);		            }				}			}            cow.write(som);        } catch (CDKException exception) {            int count = som.getMoleculeCount();            boolean needMoreFiles = (cow.accepts(IMoleculeSet.class)) && (count > 1);            logger.info("Cannot write MoleculeSet, recursing into Molecules's.");            for (int i=0; i < count; i++) {                if (needMoreFiles) {                    cow.close(); // possibly closing empty file                    String fname = outputFilename + "." + (i+1);                    FileWriter fileWriter = new FileWriter(new File(fname));                    cow = getChemObjectWriter(this.oformat, fileWriter);                }                write(som.getMolecule(i), outputFilename);            }        }    }    private void write(IMolecule molecule, String outputFilename) throws IOException {        try {            cow.write(molecule);        } catch (CDKException exception) {            logger.error("Cannot write molecule: ", exception.getMessage());            logger.debug(exception);        }    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -