Search in sources :

Example 6 with Keyword

use of ffx.utilities.Keyword in project ffx by mjschnie.

the class KeyFilter method open.

/**
 * <p>
 * open</p>
 *
 * @param keyFile a {@link java.io.File} object.
 * @param keywordHash a {@link java.util.Hashtable} object.
 * @return a {@link java.util.Hashtable} object.
 */
public static Hashtable<String, Keyword> open(File keyFile, Hashtable<String, Keyword> keywordHash) {
    if (keyFile == null || !keyFile.exists() || !keyFile.canRead()) {
        return null;
    }
    if (keywordHash == null) {
        keywordHash = new Hashtable<String, Keyword>();
    }
    FileReader fr = null;
    BufferedReader br = null;
    try {
        fr = new FileReader(keyFile);
        br = new BufferedReader(fr);
        Keyword comments = new Keyword("COMMENTS");
        keywordHash.put("COMMENTS", comments);
        while (br.ready()) {
            String s = br.readLine();
            if (s == null) {
                continue;
            }
            s = s.trim();
            if (s.equals("")) {
                // Skip blank lines
                continue;
            }
            // Store comments together
            if (s.startsWith("#") || s.toUpperCase().startsWith("ECHO")) {
                comments.append(s);
            } else {
                int firstspace = s.indexOf(" ");
                String keyword, data;
                if (firstspace == -1) {
                    // no parameters
                    keyword = s.trim().toUpperCase();
                    // Each of these valid cases mean different things...
                    if (keyword.equalsIgnoreCase("rattle")) {
                        data = "RATTLE";
                    } else {
                        data = null;
                    }
                } else {
                    keyword = s.substring(0, firstspace).toUpperCase();
                    data = s.substring(firstspace).trim();
                }
                Keyword kd = keywordHash.get(keyword);
                if (kd == null) {
                    kd = new Keyword(keyword);
                    keywordHash.put(keyword, kd);
                }
                if (data != null) {
                    kd.append(data);
                }
                /**
                 * Multipoles and TORTORS are the only keywords that span
                 * multiple lines. Editing these from within Force Field X
                 * seems unlikely, so they are treated as comments.
                 */
                if (keyword.equalsIgnoreCase("MULTIPOLE")) {
                    int[] mnum = { 3, 1, 2, 3 };
                    for (int i = 0; i < 4; i++) {
                        if (!br.ready()) {
                            System.out.println("Check for an invalid MULTIPOLE keyword.");
                            return null;
                        }
                        s = br.readLine();
                        if (s == null) {
                            logger.warning("Multipole format error.");
                            return null;
                        }
                        s = s.trim();
                        if (s.split(" +").length != mnum[i]) {
                            logger.warning("Multipole format error.");
                            return null;
                        }
                        kd.append(s);
                    }
                } else if (keyword.equalsIgnoreCase("TORTORS")) {
                    String[] res = data.split(" +");
                    if (res == null || res.length < 7) {
                        logger.warning("TORTOR format error.");
                        return null;
                    }
                    int xres = Integer.parseInt(res[5]);
                    int yres = Integer.parseInt(res[6]);
                    for (int i = 0; i < xres * yres; i++) {
                        if (!br.ready()) {
                            System.out.println("Check for an invalid TORTOR keyword.");
                            return null;
                        }
                        s = br.readLine();
                        if (s == null) {
                            logger.warning("TORTOR format error.");
                            return null;
                        }
                        s = s.trim();
                        if (s.split(" +").length != 3) {
                            logger.warning("TORTOR format error.");
                            return null;
                        }
                        kd.append(s);
                    }
                }
            }
        }
        return keywordHash;
    } catch (IOException e) {
        System.err.println("Error reading Key File: " + e);
        return null;
    } finally {
        if (fr != null) {
            try {
                fr.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : Keyword(ffx.utilities.Keyword) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException)

Example 7 with Keyword

use of ffx.utilities.Keyword in project ffx by mjschnie.

the class MainPanel method openKey.

/**
 * Attempt to convert a TINKER *.key file
 *
 * @param newSystem FFXSystem that needs an associated Key File
 * @param createKey flag to create a key file be created
 * @return Key file that was found, or null if nothing could be found
 */
public boolean openKey(FFXSystem newSystem, boolean createKey) {
    String keyFileName = null;
    String temp = newSystem.getFile().getName();
    int dot = temp.lastIndexOf(".");
    if (dot > 0) {
        keyFileName = temp.substring(0, dot) + ".key";
    } else {
        keyFileName = temp + ".key";
    }
    String path = newSystem.getFile().getParent() + File.separator;
    File keyfile = new File(path + keyFileName);
    // System.out.println("" + keyfile);
    if (keyfile.exists()) {
        Hashtable<String, Keyword> keywordHash = KeyFilter.open(keyfile);
        if (keywordHash != null) {
            newSystem.setKeywords(keywordHash);
        } else {
            return false;
        }
        newSystem.setKeyFile(keyfile);
        newSystem.setForceField(null);
        return true;
    }
    keyfile = new File(path + "tinker.key");
    if (keyfile.exists()) {
        logger.info("Using tinker.key: " + keyfile);
        Hashtable<String, Keyword> keywordHash = KeyFilter.open(keyfile);
        if (keywordHash != null) {
            newSystem.setKeywords(keywordHash);
        } else {
            return false;
        }
        newSystem.setKeyFile(keyfile);
        newSystem.setForceField(null);
        return true;
    }
    if (createKey) {
        return createKeyFile(newSystem);
    }
    return false;
}
Also used : Keyword(ffx.utilities.Keyword) ForceFieldString(ffx.potential.parameters.ForceField.ForceFieldString) File(java.io.File)

Example 8 with Keyword

use of ffx.utilities.Keyword in project ffx by mjschnie.

the class KeyFilter method loadSystemKeywords.

/**
 * <p>
 * loadSystemKeywords</p>
 *
 * @return a {@link java.util.Hashtable} object.
 */
public static Hashtable<String, Keyword> loadSystemKeywords() {
    File f = new File("/etc/ffx.conf");
    Hashtable<String, Keyword> systemKeywords = new Hashtable<>();
    if (f.exists() && f.canRead()) {
        logger.info("Reading /etc/ffx.conf");
        systemKeywords = KeyFilter.open(f, systemKeywords);
    }
    String path = System.getProperty("user.home") + File.separator + ".ffx/ffx.conf";
    f = new File(path);
    if (f.exists() && f.canRead()) {
        logger.log(Level.INFO, "Reading {0}", path);
        systemKeywords = KeyFilter.open(f, systemKeywords);
    }
    return systemKeywords;
}
Also used : Keyword(ffx.utilities.Keyword) Hashtable(java.util.Hashtable) File(java.io.File)

Example 9 with Keyword

use of ffx.utilities.Keyword in project ffx by mjschnie.

the class KeywordPanel method loadActive.

/**
 * <p>
 * loadActive</p>
 *
 * @param newSystem a {@link ffx.ui.FFXSystem} object.
 * @return a boolean.
 */
public boolean loadActive(FFXSystem newSystem) {
    synchronized (this) {
        if (newSystem == null) {
            keyClear();
            return false;
        }
        configToKeywords(newSystem);
        File newKeyFile = newSystem.getKeyFile();
        if (newKeyFile != null) {
        // logger.info(String.format("Key File %s.", newKeyFile.getAbsolutePath()));
        }
        Hashtable<String, Keyword> newKeys = newSystem.getKeywords();
        if (newKeyFile == null && newKeys == null) {
            logger.info(String.format("Loaded %s with no keywords.", newSystem.toString()));
            return false;
        }
        // logger.info(String.format("Loading %s with %d keywords.", newSystem.toString(), newKeys.size()));
        return loadActive(newSystem, newKeys, newKeyFile);
    }
}
Also used : Keyword(ffx.utilities.Keyword) File(java.io.File)

Aggregations

Keyword (ffx.utilities.Keyword)9 File (java.io.File)4 Hashtable (java.util.Hashtable)3 ForceFieldString (ffx.potential.parameters.ForceField.ForceFieldString)2 IOException (java.io.IOException)2 BorderLayout (java.awt.BorderLayout)1 Dimension (java.awt.Dimension)1 FlowLayout (java.awt.FlowLayout)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 BoxLayout (javax.swing.BoxLayout)1 ButtonGroup (javax.swing.ButtonGroup)1 JButton (javax.swing.JButton)1 JCheckBox (javax.swing.JCheckBox)1 JComboBox (javax.swing.JComboBox)1