Search in sources :

Example 6 with Bond

use of ffx.potential.bonded.Bond in project ffx by mjschnie.

the class Utilities method assignResidue.

private static Residue assignResidue(List<Atom> backbone, int start, List<Atom> atoms, List<Atom> sidePolymer) {
    Atom a;
    int atomicnum;
    // 0 = S, 1 = P, 2 = O, 3 = N, 4 = C
    int[] bins = new int[5];
    char[] chars = { 'S', 'P', 'O', 'N', 'C' };
    for (ListIterator li = sidePolymer.listIterator(); li.hasNext(); ) {
        a = (Atom) li.next();
        atomicnum = a.getAtomicNumber();
        switch(atomicnum) {
            case 1:
                // ignore hydrogens
                break;
            case 6:
                // Carbon
                bins[4]++;
                break;
            case 7:
                // Nitrogen
                bins[3]++;
                break;
            case 8:
                // Oxygen
                bins[2]++;
                break;
            case 15:
                // Phosphorus
                bins[1]++;
                break;
            case 16:
                // Sulfur
                bins[0]++;
                break;
            default:
                return null;
        }
    }
    StringBuilder key = new StringBuilder();
    int atomCount = 0;
    for (int i = 0; i < 5; i++) {
        if (bins[i] != 0) {
            atomCount += bins[i];
            key.append(chars[i]);
            key.append(Integer.toString(bins[i]));
        }
    }
    if (atomCount == 0) {
        // Glycine
        key.append("H");
    }
    String resname = sidechainStoichiometry.get(key.toString());
    if (resname == null) {
        resname = "Unknown";
    } else {
        resname = resname.intern();
    }
    if (resname.equals("1") || resname.equals("2")) {
        // Special case where atom string keys aren't unique
        Atom alpha = backbone.get(start + 1);
        Atom carbonyl = backbone.get(start + 2);
        Atom beta = null;
        List alphabonds = alpha.getBonds();
        Bond abond;
        for (ListIterator li = alphabonds.listIterator(); li.hasNext(); ) {
            abond = (Bond) li.next();
            beta = abond.get1_2(alpha);
            // carbon
            if (beta.getAtomicNumber() != 7 && beta.getAtomicNumber() != 1 && beta != carbonyl) {
                break;
            }
            beta = null;
        }
        if (beta == null) {
            return null;
        }
        List<Bond> betabonds = beta.getBonds();
        Atom gamma;
        int carboncount = 0;
        for (ListIterator<Bond> li = betabonds.listIterator(); li.hasNext(); ) {
            abond = li.next();
            gamma = abond.get1_2(beta);
            if (gamma.getAtomicNumber() == 6) {
                carboncount++;
            }
        }
        if (resname.equals("1")) {
            if (carboncount == 2) {
                resname = "PRO";
            } else {
                resname = "VAL";
            }
        } else {
            if (carboncount == 2) {
                resname = "LEU";
            } else {
                resname = "ILE";
            }
        }
    } else if (resname.equals("3")) {
        Atom c3 = backbone.get(start + 3);
        int num = countCO(c3);
        if (num == 2) {
            resname = "A";
        } else {
            resname = "DG";
        }
    }
    Residue residue = null;
    try {
        Residue.NA3.valueOf(resname.toUpperCase());
        residue = new Residue(resname, Residue.ResidueType.NA);
    } catch (Exception e) {
    }
    if (residue == null) {
        try {
            Residue.AA3.valueOf(resname.toUpperCase());
            residue = new Residue(resname, Residue.ResidueType.AA);
        } catch (Exception e) {
        }
    }
    if (residue == null) {
        residue = new Residue(resname, Residue.ResidueType.UNK);
    }
    // Create the Residue group
    for (ListIterator li = atoms.listIterator(); li.hasNext(); ) {
        a = (Atom) li.next();
        residue.addMSNode(a);
    }
    return residue;
}
Also used : ListIterator(java.util.ListIterator) Atom(ffx.potential.bonded.Atom) Residue(ffx.potential.bonded.Residue) List(java.util.List) ArrayList(java.util.ArrayList) Bond(ffx.potential.bonded.Bond)

Example 7 with Bond

use of ffx.potential.bonded.Bond in project ffx by mjschnie.

the class PhDiscount method crashDump.

/**
 * Attempt to print sources of catastrophic system heating.
 */
private void crashDump(RuntimeException error) {
    writeSnapshot(".meltdown-");
    ffe.energy(false, true);
    mola.getDescendants(BondedTerm.class).stream().filter(BondedTerm::isExtendedSystemMember).forEach(term -> {
        try {
            ((Bond) term).log();
        } catch (Exception ex) {
        }
        try {
            ((Angle) term).log();
        } catch (Exception ex) {
        }
        try {
            ((Torsion) term).log();
        } catch (Exception ex) {
        }
    });
    if (ffe.getVanDerWaalsEnergy() > 1000) {
        extendedAtoms = esvSystem.getExtendedAtoms();
        nAtomsExt = esvSystem.getExtendedAtoms().length;
        for (int i = 0; i < nAtomsExt; i++) {
            Atom ai = extendedAtoms[i];
            for (int j = 0; j < nAtomsExt; j++) {
                Atom aj = extendedAtoms[j];
                if (!esvSystem.isExtended(i) && !esvSystem.isExtended(j)) {
                    continue;
                }
                if (ai == aj || ai.getBond(aj) != null) {
                    continue;
                }
                double dist = FastMath.sqrt(FastMath.pow((aj.getX() - ai.getX()), 2) + FastMath.pow((aj.getY() - ai.getY()), 2) + FastMath.pow((aj.getZ() - ai.getZ()), 2));
                if (dist < 0.8 * (aj.getVDWR() + ai.getVDWR())) {
                    logger.warning(String.format("Close vdW contact for atoms: \n   %s\n   %s", aj, ai));
                }
            }
        }
    }
    throw error;
}
Also used : BondedTerm(ffx.potential.bonded.BondedTerm) Angle(ffx.potential.bonded.Angle) Bond(ffx.potential.bonded.Bond) Torsion(ffx.potential.bonded.Torsion) SystemTemperatureException(ffx.potential.utils.SystemTemperatureException) Atom(ffx.potential.bonded.Atom)

Example 8 with Bond

use of ffx.potential.bonded.Bond in project ffx by mjschnie.

the class ParticleMeshEwaldCart method assignMultipole.

private boolean assignMultipole(int i) {
    Atom atom = atoms[i];
    AtomType atomType = atoms[i].getAtomType();
    if (atomType == null) {
        String message = " Multipoles can only be assigned to atoms that have been typed.";
        logger.severe(message);
        return false;
    }
    PolarizeType polarizeType = forceField.getPolarizeType(atomType.getKey());
    if (polarizeType != null) {
        atom.setPolarizeType(polarizeType);
    } else {
        String message = " No polarization type was found for " + atom.toString();
        logger.fine(message);
        double polarizability = 0.0;
        double thole = 0.0;
        int[] polarizationGroup = null;
        polarizeType = new PolarizeType(atomType.type, polarizability, thole, polarizationGroup);
        forceField.addForceFieldType(polarizeType);
        atom.setPolarizeType(polarizeType);
    }
    String key;
    // No reference atoms.
    key = atomType.getKey() + " 0 0";
    MultipoleType multipoleType = forceField.getMultipoleType(key);
    if (multipoleType != null) {
        atom.setMultipoleType(multipoleType);
        localMultipole[i][t000] = multipoleType.getCharge();
        localMultipole[i][t100] = multipoleType.getDipole()[0];
        localMultipole[i][t010] = multipoleType.getDipole()[1];
        localMultipole[i][t001] = multipoleType.getDipole()[2];
        localMultipole[i][t200] = multipoleType.getQuadrupole()[0][0];
        localMultipole[i][t020] = multipoleType.getQuadrupole()[1][1];
        localMultipole[i][t002] = multipoleType.getQuadrupole()[2][2];
        localMultipole[i][t110] = multipoleType.getQuadrupole()[0][1];
        localMultipole[i][t101] = multipoleType.getQuadrupole()[0][2];
        localMultipole[i][t011] = multipoleType.getQuadrupole()[1][2];
        axisAtom[i] = null;
        frame[i] = multipoleType.frameDefinition;
        return true;
    }
    // No bonds.
    List<Bond> bonds = atom.getBonds();
    if (bonds == null || bonds.size() < 1) {
        String message = "Multipoles can only be assigned after bonded relationships are defined.\n";
        logger.severe(message);
    }
    // 1 reference atom.
    for (Bond b : bonds) {
        Atom atom2 = b.get1_2(atom);
        key = atomType.getKey() + " " + atom2.getAtomType().getKey() + " 0";
        multipoleType = multipoleType = forceField.getMultipoleType(key);
        if (multipoleType != null) {
            int[] multipoleReferenceAtoms = new int[1];
            multipoleReferenceAtoms[0] = atom2.getIndex() - 1;
            atom.setMultipoleType(multipoleType);
            localMultipole[i][t000] = multipoleType.getCharge();
            localMultipole[i][t100] = multipoleType.getDipole()[0];
            localMultipole[i][t010] = multipoleType.getDipole()[1];
            localMultipole[i][t001] = multipoleType.getDipole()[2];
            localMultipole[i][t200] = multipoleType.getQuadrupole()[0][0];
            localMultipole[i][t020] = multipoleType.getQuadrupole()[1][1];
            localMultipole[i][t002] = multipoleType.getQuadrupole()[2][2];
            localMultipole[i][t110] = multipoleType.getQuadrupole()[0][1];
            localMultipole[i][t101] = multipoleType.getQuadrupole()[0][2];
            localMultipole[i][t011] = multipoleType.getQuadrupole()[1][2];
            axisAtom[i] = multipoleReferenceAtoms;
            frame[i] = multipoleType.frameDefinition;
            return true;
        }
    }
    // 2 reference atoms.
    for (Bond b : bonds) {
        Atom atom2 = b.get1_2(atom);
        String key2 = atom2.getAtomType().getKey();
        for (Bond b2 : bonds) {
            if (b == b2) {
                continue;
            }
            Atom atom3 = b2.get1_2(atom);
            String key3 = atom3.getAtomType().getKey();
            key = atomType.getKey() + " " + key2 + " " + key3;
            multipoleType = forceField.getMultipoleType(key);
            if (multipoleType != null) {
                int[] multipoleReferenceAtoms = new int[2];
                multipoleReferenceAtoms[0] = atom2.getIndex() - 1;
                multipoleReferenceAtoms[1] = atom3.getIndex() - 1;
                atom.setMultipoleType(multipoleType);
                atom.setMultipoleType(multipoleType);
                localMultipole[i][t000] = multipoleType.getCharge();
                localMultipole[i][t100] = multipoleType.getDipole()[0];
                localMultipole[i][t010] = multipoleType.getDipole()[1];
                localMultipole[i][t001] = multipoleType.getDipole()[2];
                localMultipole[i][t200] = multipoleType.getQuadrupole()[0][0];
                localMultipole[i][t020] = multipoleType.getQuadrupole()[1][1];
                localMultipole[i][t002] = multipoleType.getQuadrupole()[2][2];
                localMultipole[i][t110] = multipoleType.getQuadrupole()[0][1];
                localMultipole[i][t101] = multipoleType.getQuadrupole()[0][2];
                localMultipole[i][t011] = multipoleType.getQuadrupole()[1][2];
                axisAtom[i] = multipoleReferenceAtoms;
                frame[i] = multipoleType.frameDefinition;
                return true;
            }
        }
    }
    /**
     * 3 reference atoms.
     */
    for (Bond b : bonds) {
        Atom atom2 = b.get1_2(atom);
        String key2 = atom2.getAtomType().getKey();
        for (Bond b2 : bonds) {
            if (b == b2) {
                continue;
            }
            Atom atom3 = b2.get1_2(atom);
            String key3 = atom3.getAtomType().getKey();
            for (Bond b3 : bonds) {
                if (b == b3 || b2 == b3) {
                    continue;
                }
                Atom atom4 = b3.get1_2(atom);
                String key4 = atom4.getAtomType().getKey();
                key = atomType.getKey() + " " + key2 + " " + key3 + " " + key4;
                multipoleType = forceField.getMultipoleType(key);
                if (multipoleType != null) {
                    int[] multipoleReferenceAtoms = new int[3];
                    multipoleReferenceAtoms[0] = atom2.getIndex() - 1;
                    multipoleReferenceAtoms[1] = atom3.getIndex() - 1;
                    multipoleReferenceAtoms[2] = atom4.getIndex() - 1;
                    atom.setMultipoleType(multipoleType);
                    localMultipole[i][t000] = multipoleType.getCharge();
                    localMultipole[i][t100] = multipoleType.getDipole()[0];
                    localMultipole[i][t010] = multipoleType.getDipole()[1];
                    localMultipole[i][t001] = multipoleType.getDipole()[2];
                    localMultipole[i][t200] = multipoleType.getQuadrupole()[0][0];
                    localMultipole[i][t020] = multipoleType.getQuadrupole()[1][1];
                    localMultipole[i][t002] = multipoleType.getQuadrupole()[2][2];
                    localMultipole[i][t110] = multipoleType.getQuadrupole()[0][1];
                    localMultipole[i][t101] = multipoleType.getQuadrupole()[0][2];
                    localMultipole[i][t011] = multipoleType.getQuadrupole()[1][2];
                    axisAtom[i] = multipoleReferenceAtoms;
                    frame[i] = multipoleType.frameDefinition;
                    return true;
                }
            }
            List<Angle> angles = atom.getAngles();
            for (Angle angle : angles) {
                Atom atom4 = angle.get1_3(atom);
                if (atom4 != null) {
                    String key4 = atom4.getAtomType().getKey();
                    key = atomType.getKey() + " " + key2 + " " + key3 + " " + key4;
                    multipoleType = forceField.getMultipoleType(key);
                    if (multipoleType != null) {
                        int[] multipoleReferenceAtoms = new int[3];
                        multipoleReferenceAtoms[0] = atom2.getIndex() - 1;
                        multipoleReferenceAtoms[1] = atom3.getIndex() - 1;
                        multipoleReferenceAtoms[2] = atom4.getIndex() - 1;
                        atom.setMultipoleType(multipoleType);
                        localMultipole[i][t000] = multipoleType.getCharge();
                        localMultipole[i][t100] = multipoleType.getDipole()[0];
                        localMultipole[i][t010] = multipoleType.getDipole()[1];
                        localMultipole[i][t001] = multipoleType.getDipole()[2];
                        localMultipole[i][t200] = multipoleType.getQuadrupole()[0][0];
                        localMultipole[i][t020] = multipoleType.getQuadrupole()[1][1];
                        localMultipole[i][t002] = multipoleType.getQuadrupole()[2][2];
                        localMultipole[i][t110] = multipoleType.getQuadrupole()[0][1];
                        localMultipole[i][t101] = multipoleType.getQuadrupole()[0][2];
                        localMultipole[i][t011] = multipoleType.getQuadrupole()[1][2];
                        axisAtom[i] = multipoleReferenceAtoms;
                        frame[i] = multipoleType.frameDefinition;
                        return true;
                    }
                }
            }
        }
    }
    /**
     * Revert to a 2 reference atom definition that may include a 1-3 site.
     * For example a hydrogen on water.
     */
    for (Bond b : bonds) {
        Atom atom2 = b.get1_2(atom);
        String key2 = atom2.getAtomType().getKey();
        List<Angle> angles = atom.getAngles();
        for (Angle angle : angles) {
            Atom atom3 = angle.get1_3(atom);
            if (atom3 != null) {
                String key3 = atom3.getAtomType().getKey();
                key = atomType.getKey() + " " + key2 + " " + key3;
                multipoleType = forceField.getMultipoleType(key);
                if (multipoleType != null) {
                    int[] multipoleReferenceAtoms = new int[2];
                    multipoleReferenceAtoms[0] = atom2.getIndex() - 1;
                    multipoleReferenceAtoms[1] = atom3.getIndex() - 1;
                    atom.setMultipoleType(multipoleType);
                    localMultipole[i][t000] = multipoleType.getCharge();
                    localMultipole[i][t100] = multipoleType.getDipole()[0];
                    localMultipole[i][t010] = multipoleType.getDipole()[1];
                    localMultipole[i][t001] = multipoleType.getDipole()[2];
                    localMultipole[i][t200] = multipoleType.getQuadrupole()[0][0];
                    localMultipole[i][t020] = multipoleType.getQuadrupole()[1][1];
                    localMultipole[i][t002] = multipoleType.getQuadrupole()[2][2];
                    localMultipole[i][t110] = multipoleType.getQuadrupole()[0][1];
                    localMultipole[i][t101] = multipoleType.getQuadrupole()[0][2];
                    localMultipole[i][t011] = multipoleType.getQuadrupole()[1][2];
                    axisAtom[i] = multipoleReferenceAtoms;
                    frame[i] = multipoleType.frameDefinition;
                    return true;
                }
                for (Angle angle2 : angles) {
                    Atom atom4 = angle2.get1_3(atom);
                    if (atom4 != null && atom4 != atom3) {
                        String key4 = atom4.getAtomType().getKey();
                        key = atomType.getKey() + " " + key2 + " " + key3 + " " + key4;
                        multipoleType = forceField.getMultipoleType(key);
                        if (multipoleType != null) {
                            int[] multipoleReferenceAtoms = new int[3];
                            multipoleReferenceAtoms[0] = atom2.getIndex() - 1;
                            multipoleReferenceAtoms[1] = atom3.getIndex() - 1;
                            multipoleReferenceAtoms[2] = atom4.getIndex() - 1;
                            atom.setMultipoleType(multipoleType);
                            localMultipole[i][t000] = multipoleType.getCharge();
                            localMultipole[i][t100] = multipoleType.getDipole()[0];
                            localMultipole[i][t010] = multipoleType.getDipole()[1];
                            localMultipole[i][t001] = multipoleType.getDipole()[2];
                            localMultipole[i][t200] = multipoleType.getQuadrupole()[0][0];
                            localMultipole[i][t020] = multipoleType.getQuadrupole()[1][1];
                            localMultipole[i][t002] = multipoleType.getQuadrupole()[2][2];
                            localMultipole[i][t110] = multipoleType.getQuadrupole()[0][1];
                            localMultipole[i][t101] = multipoleType.getQuadrupole()[0][2];
                            localMultipole[i][t011] = multipoleType.getQuadrupole()[1][2];
                            axisAtom[i] = multipoleReferenceAtoms;
                            frame[i] = multipoleType.frameDefinition;
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}
Also used : PolarizeType(ffx.potential.parameters.PolarizeType) Angle(ffx.potential.bonded.Angle) AtomType(ffx.potential.parameters.AtomType) ForceFieldString(ffx.potential.parameters.ForceField.ForceFieldString) MultipoleType(ffx.potential.parameters.MultipoleType) Bond(ffx.potential.bonded.Bond) Atom(ffx.potential.bonded.Atom)

Example 9 with Bond

use of ffx.potential.bonded.Bond in project ffx by mjschnie.

the class MainPanel method merge.

/**
 * Merge two or more selected FSystem Nodes into one FSystem node. There are
 * a few gotchas that need to be fixed
 *
 * @param nodesToMerge a {@link java.util.ArrayList} object.
 */
public void merge(ArrayList<MSNode> nodesToMerge) {
    ArrayList<MSNode> activeNodes = new ArrayList<MSNode>();
    for (MSNode node : nodesToMerge) {
        if (node != null && !(node instanceof MSRoot)) {
            activeNodes.add(node);
        }
    }
    if (activeNodes.size() <= 1) {
        return;
    }
    // Set up a structure to hold the new system
    FFXSystem active = hierarchy.getActive();
    File file = SystemFilter.version(hierarchy.getActive().getFile());
    FFXSystem system = new FFXSystem(file, "Merge Result", active.getProperties());
    system.setKeyFile(active.getKeyFile());
    system.setKeywords(KeyFilter.open(active.getKeyFile()));
    // Fill arrays with the atoms and bonds from the systems to be combined
    ArrayList<Atom> mergedAtoms = new ArrayList<Atom>();
    ArrayList<Bond> mergedBonds = new ArrayList<Bond>();
    ArrayList<FFXSystem> systems = new ArrayList<FFXSystem>();
    TransformGroup parentTransformGroup = null;
    FFXSystem parentSystem;
    Transform3D parentTransform3D = new Transform3D();
    Vector3d parentPosition = new Vector3d();
    Vector3d atomPosition = new Vector3d();
    // TINKER Atom Numbers start at 1
    int atomNum = 1;
    Vector3d zero = new Vector3d(0.0, 0.0, 0.0);
    for (MSNode m : activeNodes) {
        parentSystem = (FFXSystem) m.getMSNode(FFXSystem.class);
        if (parentSystem == null) {
            return;
        }
        if (!systems.contains(parentSystem)) {
            graphicsCanvas.updateSceneWait(parentSystem, false, true, RendererCache.ViewModel.WIREFRAME, false, null);
            systems.add(parentSystem);
        }
        // Move each atom into the global frame by applying the System
        // Transform to
        // relative atomic position
        parentTransformGroup = parentSystem.getOriginToRot();
        parentTransformGroup.getTransform(parentTransform3D);
        parentTransform3D.get(parentPosition);
        parentTransform3D.setTranslation(zero);
        // parentTransform3D.setScale(1.0d);
        ArrayList<Atom> atoms = m.getAtomList();
        ArrayList<ROLS> bonds = m.getBondList();
        for (Atom atom : atoms) {
            atom.removeFromParent();
            atom.setXyzIndex(atomNum++);
            mergedAtoms.add(atom);
            atom.getV3D(atomPosition);
            parentTransform3D.transform(atomPosition);
            atomPosition.add(parentPosition);
            atom.moveTo(atomPosition);
        }
        for (ROLS msm : bonds) {
            Bond bond = (Bond) msm;
            bond.removeFromParent();
            mergedBonds.add((Bond) msm);
        }
    }
    for (FFXSystem sys : systems) {
        close(sys);
    }
    MergeFilter mergeFilter = new MergeFilter(system, mergedAtoms, mergedBonds);
    UIFileOpener fileOpener = new UIFileOpener(mergeFilter, this);
    if (fileOpenerThreads > 0) {
        fileOpener.setNThreads(fileOpenerThreads);
    }
    Thread thread = new Thread(fileOpener);
    thread.start();
}
Also used : ROLS(ffx.potential.bonded.ROLS) Transform3D(javax.media.j3d.Transform3D) ArrayList(java.util.ArrayList) MergeFilter(ffx.potential.parsers.MergeFilter) Atom(ffx.potential.bonded.Atom) TransformGroup(javax.media.j3d.TransformGroup) MSNode(ffx.potential.bonded.MSNode) MSRoot(ffx.potential.bonded.MSRoot) Vector3d(javax.vecmath.Vector3d) Bond(ffx.potential.bonded.Bond) File(java.io.File)

Example 10 with Bond

use of ffx.potential.bonded.Bond in project ffx by mjschnie.

the class PDBFilter method writeFile.

/**
 * <p>
 * writeFile</p>
 *
 * @param saveFile a {@link java.io.File} object.
 * @param append a {@link java.lang.StringBuilder} object.
 * @param printLinear Whether to print atoms linearly or by element
 * @return Success of writing.
 */
public boolean writeFile(File saveFile, boolean append, boolean printLinear) {
    if (Boolean.parseBoolean(System.getProperty("standardizeAtomNames", "false"))) {
        renameAtomsToPDBStandard(activeMolecularAssembly);
    }
    if (saveFile == null) {
        return false;
    }
    if (vdwH) {
        logger.info(" Printing hydrogens to van der Waals centers instead of nuclear locations.");
    }
    if (nSymOp != 0) {
        logger.info(String.format(" Printing atoms with symmetry operator %s\n", activeMolecularAssembly.getCrystal().spaceGroup.getSymOp(nSymOp).toString()));
    }
    /**
     * Create StringBuilders for ATOM, ANISOU and TER records that can be
     * reused.
     */
    StringBuilder sb = new StringBuilder("ATOM  ");
    StringBuilder anisouSB = new StringBuilder("ANISOU");
    StringBuilder terSB = new StringBuilder("TER   ");
    StringBuilder model = null;
    for (int i = 6; i < 80; i++) {
        sb.append(' ');
        anisouSB.append(' ');
        terSB.append(' ');
    }
    FileWriter fw;
    BufferedWriter bw;
    try {
        File newFile = saveFile;
        if (!append) {
            if (!noVersioning) {
                newFile = version(saveFile);
            }
        } else if (modelsWritten >= 0) {
            model = new StringBuilder(String.format("MODEL     %-4d", ++modelsWritten));
            for (int i = 15; i < 80; i++) {
                model.append(' ');
            }
        }
        activeMolecularAssembly.setFile(newFile);
        activeMolecularAssembly.setName(newFile.getName());
        if (logWrites) {
            logger.log(Level.INFO, " Saving {0}", activeMolecularAssembly.getName());
        }
        fw = new FileWriter(newFile, append);
        bw = new BufferedWriter(fw);
        /**
         * Will come before CRYST1 and ATOM records, but after anything
         * written by writeFileWithHeader (particularly X-ray refinement
         * statistics).
         */
        String[] headerLines = activeMolecularAssembly.getHeaderLines();
        for (String line : headerLines) {
            bw.write(String.format("%s\n", line));
        }
        if (model != null) {
            if (!listMode) {
                bw.write(model.toString());
                bw.newLine();
            } else {
                listOutput.add(model.toString());
            }
        }
        // =============================================================================
        // The CRYST1 record presents the unit cell parameters, space group, and Z
        // value. If the structure was not determined by crystallographic means, CRYST1
        // simply provides the unitary values, with an appropriate REMARK.
        // 
        // 7 - 15       Real(9.3)     a              a (Angstroms).
        // 16 - 24       Real(9.3)     b              b (Angstroms).
        // 25 - 33       Real(9.3)     c              c (Angstroms).
        // 34 - 40       Real(7.2)     alpha          alpha (degrees).
        // 41 - 47       Real(7.2)     beta           beta (degrees).
        // 48 - 54       Real(7.2)     gamma          gamma (degrees).
        // 56 - 66       LString       sGroup         Space  group.
        // 67 - 70       Integer       z              Z value.
        // =============================================================================
        Crystal crystal = activeMolecularAssembly.getCrystal();
        if (crystal != null && !crystal.aperiodic()) {
            Crystal c = crystal.getUnitCell();
            if (!listMode) {
                bw.write(format("CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %10s\n", c.a, c.b, c.c, c.alpha, c.beta, c.gamma, padRight(c.spaceGroup.pdbName, 10)));
            } else {
                listOutput.add(format("CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %10s", c.a, c.b, c.c, c.alpha, c.beta, c.gamma, padRight(c.spaceGroup.pdbName, 10)));
            }
        }
        // =============================================================================
        // The SSBOND record identifies each disulfide bond in protein and polypeptide
        // structures by identifying the two residues involved in the bond.
        // The disulfide bond distance is included after the symmetry operations at
        // the end of the SSBOND record.
        // 
        // 8 - 10        Integer         serNum       Serial number.
        // 12 - 14        LString(3)      "CYS"        Residue name.
        // 16             Character       chainID1     Chain identifier.
        // 18 - 21        Integer         seqNum1      Residue sequence number.
        // 22             AChar           icode1       Insertion code.
        // 26 - 28        LString(3)      "CYS"        Residue name.
        // 30             Character       chainID2     Chain identifier.
        // 32 - 35        Integer         seqNum2      Residue sequence number.
        // 36             AChar           icode2       Insertion code.
        // 60 - 65        SymOP           sym1         Symmetry oper for 1st resid
        // 67 - 72        SymOP           sym2         Symmetry oper for 2nd resid
        // 74 – 78        Real(5.2)      Length        Disulfide bond distance
        // 
        // If SG of cysteine is disordered then there are possible alternate linkages.
        // wwPDB practice is to put together all possible SSBOND records. This is
        // problematic because the alternate location identifier is not specified in
        // the SSBOND record.
        // =============================================================================
        int serNum = 1;
        Polymer[] polymers = activeMolecularAssembly.getChains();
        if (polymers != null) {
            for (Polymer polymer : polymers) {
                ArrayList<Residue> residues = polymer.getResidues();
                for (Residue residue : residues) {
                    if (residue.getName().equalsIgnoreCase("CYS")) {
                        List<Atom> cysAtoms = residue.getAtomList();
                        Atom SG1 = null;
                        for (Atom atom : cysAtoms) {
                            String atName = atom.getName().toUpperCase();
                            if (atName.equals("SG") || atName.equals("SH")) {
                                SG1 = atom;
                                break;
                            }
                        }
                        List<Bond> bonds = SG1.getBonds();
                        for (Bond bond : bonds) {
                            Atom SG2 = bond.get1_2(SG1);
                            if (SG2.getName().equalsIgnoreCase("SG")) {
                                if (SG1.getIndex() < SG2.getIndex()) {
                                    bond.energy(false);
                                    if (!listMode) {
                                        bw.write(format("SSBOND %3d CYS %1s %4s    CYS %1s %4s %36s %5.2f\n", serNum++, SG1.getChainID().toString(), Hybrid36.encode(4, SG1.getResidueNumber()), SG2.getChainID().toString(), Hybrid36.encode(4, SG2.getResidueNumber()), "", bond.getValue()));
                                    } else {
                                        listOutput.add(format("SSBOND %3d CYS %1s %4s    CYS %1s %4s %36s %5.2f\n", serNum++, SG1.getChainID().toString(), Hybrid36.encode(4, SG1.getResidueNumber()), SG2.getChainID().toString(), Hybrid36.encode(4, SG2.getResidueNumber()), "", bond.getValue()));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        // =============================================================================
        // 
        // 7 - 11        Integer       serial       Atom serial number.
        // 13 - 16        Atom          name         Atom name.
        // 17             Character     altLoc       Alternate location indicator.
        // 18 - 20        Residue name  resName      Residue name.
        // 22             Character     chainID      Chain identifier.
        // 23 - 26        Integer       resSeq       Residue sequence number.
        // 27             AChar         iCode        Code for insertion of residues.
        // 31 - 38        Real(8.3)     x            Orthogonal coordinates for X in Angstroms.
        // 39 - 46        Real(8.3)     y            Orthogonal coordinates for Y in Angstroms.
        // 47 - 54        Real(8.3)     z            Orthogonal coordinates for Z in Angstroms.
        // 55 - 60        Real(6.2)     occupancy    Occupancy.
        // 61 - 66        Real(6.2)     tempFactor   Temperature factor.
        // 77 - 78        LString(2)    element      Element symbol, right-justified.
        // 79 - 80        LString(2)    charge       Charge  on the atom.
        // =============================================================================
        // 1         2         3         4         5         6         7
        // 123456789012345678901234567890123456789012345678901234567890123456789012345678
        // ATOM      1  N   ILE A  16      60.614  71.140 -10.592  1.00  7.38           N
        // ATOM      2  CA  ILE A  16      60.793  72.149  -9.511  1.00  6.91           C
        MolecularAssembly[] molecularAssemblies = this.getMolecularAssemblys();
        int serial = 1;
        // Loop over biomolecular chains
        if (polymers != null) {
            for (Polymer polymer : polymers) {
                currentSegID = polymer.getName();
                currentChainID = polymer.getChainID();
                sb.setCharAt(21, currentChainID);
                // Loop over residues
                ArrayList<Residue> residues = polymer.getResidues();
                for (Residue residue : residues) {
                    String resName = residue.getName();
                    if (resName.length() > 3) {
                        resName = resName.substring(0, 3);
                    }
                    int resID = residue.getResidueNumber();
                    sb.replace(17, 20, padLeft(resName.toUpperCase(), 3));
                    sb.replace(22, 26, String.format("%4s", Hybrid36.encode(4, resID)));
                    // Loop over atoms
                    ArrayList<Atom> residueAtoms = residue.getAtomList();
                    ArrayList<Atom> backboneAtoms = residue.getBackboneAtoms();
                    boolean altLocFound = false;
                    for (Atom atom : backboneAtoms) {
                        writeAtom(atom, serial++, sb, anisouSB, bw);
                        Character altLoc = atom.getAltLoc();
                        if (altLoc != null && !altLoc.equals(' ')) {
                            altLocFound = true;
                        }
                        residueAtoms.remove(atom);
                    }
                    for (Atom atom : residueAtoms) {
                        writeAtom(atom, serial++, sb, anisouSB, bw);
                        Character altLoc = atom.getAltLoc();
                        if (altLoc != null && !altLoc.equals(' ')) {
                            altLocFound = true;
                        }
                    }
                    // Write out alternate conformers
                    if (altLocFound) {
                        for (int ma = 1; ma < molecularAssemblies.length; ma++) {
                            MolecularAssembly altMolecularAssembly = molecularAssemblies[ma];
                            Polymer altPolymer = altMolecularAssembly.getPolymer(currentChainID, currentSegID, false);
                            Residue altResidue = altPolymer.getResidue(resName, resID, false);
                            backboneAtoms = altResidue.getBackboneAtoms();
                            residueAtoms = altResidue.getAtomList();
                            for (Atom atom : backboneAtoms) {
                                if (atom.getAltLoc() != null && !atom.getAltLoc().equals(' ') && !atom.getAltLoc().equals('A')) {
                                    writeAtom(atom, serial++, sb, anisouSB, bw);
                                }
                                residueAtoms.remove(atom);
                            }
                            for (Atom atom : residueAtoms) {
                                if (atom.getAltLoc() != null && !atom.getAltLoc().equals(' ') && !atom.getAltLoc().equals('A')) {
                                    writeAtom(atom, serial++, sb, anisouSB, bw);
                                }
                            }
                        }
                    }
                }
                terSB.replace(6, 11, String.format("%5s", Hybrid36.encode(5, serial++)));
                terSB.replace(12, 16, "    ");
                terSB.replace(16, 26, sb.substring(16, 26));
                if (!listMode) {
                    bw.write(terSB.toString());
                    bw.newLine();
                } else {
                    listOutput.add(terSB.toString());
                }
            }
        }
        sb.replace(0, 6, "HETATM");
        sb.setCharAt(21, 'A');
        int resID = 1;
        Polymer polymer = activeMolecularAssembly.getPolymer('A', "A", false);
        if (polymer != null) {
            ArrayList<Residue> residues = polymer.getResidues();
            for (Residue residue : residues) {
                int resID2 = residue.getResidueNumber();
                if (resID2 >= resID) {
                    resID = resID2 + 1;
                }
            }
        }
        /**
         * Loop over molecules, ions and then water.
         */
        ArrayList<Molecule> molecules = activeMolecularAssembly.getMolecules();
        for (int i = 0; i < molecules.size(); i++) {
            Molecule molecule = (Molecule) molecules.get(i);
            Character chainID = molecule.getChainID();
            sb.setCharAt(21, chainID);
            String resName = molecule.getResidueName();
            if (resName.length() > 3) {
                resName = resName.substring(0, 3);
            }
            sb.replace(17, 20, padLeft(resName.toUpperCase(), 3));
            sb.replace(22, 26, String.format("%4s", Hybrid36.encode(4, resID)));
            ArrayList<Atom> moleculeAtoms = molecule.getAtomList();
            boolean altLocFound = false;
            for (Atom atom : moleculeAtoms) {
                writeAtom(atom, serial++, sb, anisouSB, bw);
                Character altLoc = atom.getAltLoc();
                if (altLoc != null && !altLoc.equals(' ')) {
                    altLocFound = true;
                }
            }
            // Write out alternate conformers
            if (altLocFound) {
                for (int ma = 1; ma < molecularAssemblies.length; ma++) {
                    MolecularAssembly altMolecularAssembly = molecularAssemblies[ma];
                    MSNode altmolecule = altMolecularAssembly.getMolecules().get(i);
                    moleculeAtoms = altmolecule.getAtomList();
                    for (Atom atom : moleculeAtoms) {
                        if (atom.getAltLoc() != null && !atom.getAltLoc().equals(' ') && !atom.getAltLoc().equals('A')) {
                            writeAtom(atom, serial++, sb, anisouSB, bw);
                        }
                    }
                }
            }
            resID++;
        }
        ArrayList<MSNode> ions = activeMolecularAssembly.getIons();
        for (int i = 0; i < ions.size(); i++) {
            Molecule ion = (Molecule) ions.get(i);
            Character chainID = ion.getChainID();
            sb.setCharAt(21, chainID);
            String resName = ion.getResidueName();
            if (resName.length() > 3) {
                resName = resName.substring(0, 3);
            }
            sb.replace(17, 20, padLeft(resName.toUpperCase(), 3));
            sb.replace(22, 26, String.format("%4s", Hybrid36.encode(4, resID)));
            ArrayList<Atom> ionAtoms = ion.getAtomList();
            boolean altLocFound = false;
            for (Atom atom : ionAtoms) {
                writeAtom(atom, serial++, sb, anisouSB, bw);
                Character altLoc = atom.getAltLoc();
                if (altLoc != null && !altLoc.equals(' ')) {
                    altLocFound = true;
                }
            }
            // Write out alternate conformers
            if (altLocFound) {
                for (int ma = 1; ma < molecularAssemblies.length; ma++) {
                    MolecularAssembly altMolecularAssembly = molecularAssemblies[ma];
                    MSNode altion = altMolecularAssembly.getIons().get(i);
                    ionAtoms = altion.getAtomList();
                    for (Atom atom : ionAtoms) {
                        if (atom.getAltLoc() != null && !atom.getAltLoc().equals(' ') && !atom.getAltLoc().equals('A')) {
                            writeAtom(atom, serial++, sb, anisouSB, bw);
                        }
                    }
                }
            }
            resID++;
        }
        ArrayList<MSNode> waters = activeMolecularAssembly.getWaters();
        for (int i = 0; i < waters.size(); i++) {
            Molecule water = (Molecule) waters.get(i);
            Character chainID = water.getChainID();
            sb.setCharAt(21, chainID);
            String resName = water.getResidueName();
            if (resName.length() > 3) {
                resName = resName.substring(0, 3);
            }
            sb.replace(17, 20, padLeft(resName.toUpperCase(), 3));
            sb.replace(22, 26, String.format("%4s", Hybrid36.encode(4, resID)));
            ArrayList<Atom> waterAtoms = water.getAtomList();
            boolean altLocFound = false;
            for (Atom atom : waterAtoms) {
                writeAtom(atom, serial++, sb, anisouSB, bw);
                Character altLoc = atom.getAltLoc();
                if (altLoc != null && !altLoc.equals(' ')) {
                    altLocFound = true;
                }
            }
            // Write out alternate conformers
            if (altLocFound) {
                for (int ma = 1; ma < molecularAssemblies.length; ma++) {
                    MolecularAssembly altMolecularAssembly = molecularAssemblies[ma];
                    MSNode altwater = altMolecularAssembly.getWaters().get(i);
                    waterAtoms = altwater.getAtomList();
                    for (Atom atom : waterAtoms) {
                        if (atom.getAltLoc() != null && !atom.getAltLoc().equals(' ') && !atom.getAltLoc().equals('A')) {
                            writeAtom(atom, serial++, sb, anisouSB, bw);
                        }
                    }
                }
            }
            resID++;
        }
        String end = model != null ? "ENDMDL" : "END";
        if (!listMode) {
            bw.write(end);
            bw.newLine();
        } else {
            listOutput.add(end);
        }
        bw.close();
    } catch (Exception e) {
        String message = "Exception writing to file: " + saveFile.toString();
        logger.log(Level.WARNING, message, e);
        return false;
    }
    return true;
}
Also used : FileWriter(java.io.FileWriter) BufferedWriter(java.io.BufferedWriter) MSNode(ffx.potential.bonded.MSNode) Polymer(ffx.potential.bonded.Polymer) Atom(ffx.potential.bonded.Atom) MissingHeavyAtomException(ffx.potential.bonded.BondedUtils.MissingHeavyAtomException) IOException(java.io.IOException) MissingAtomTypeException(ffx.potential.bonded.BondedUtils.MissingAtomTypeException) Molecule(ffx.potential.bonded.Molecule) MolecularAssembly(ffx.potential.MolecularAssembly) Residue(ffx.potential.bonded.Residue) Bond(ffx.potential.bonded.Bond) File(java.io.File) Crystal(ffx.crystal.Crystal)

Aggregations

Bond (ffx.potential.bonded.Bond)38 Atom (ffx.potential.bonded.Atom)37 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)12 Polymer (ffx.potential.bonded.Polymer)10 Residue (ffx.potential.bonded.Residue)10 File (java.io.File)10 Angle (ffx.potential.bonded.Angle)9 Crystal (ffx.crystal.Crystal)8 MissingAtomTypeException (ffx.potential.bonded.BondedUtils.MissingAtomTypeException)8 MissingHeavyAtomException (ffx.potential.bonded.BondedUtils.MissingHeavyAtomException)8 MSNode (ffx.potential.bonded.MSNode)7 Molecule (ffx.potential.bonded.Molecule)7 AtomType (ffx.potential.parameters.AtomType)7 BufferedWriter (java.io.BufferedWriter)6 FileWriter (java.io.FileWriter)6 RestraintBond (ffx.potential.bonded.RestraintBond)5 Torsion (ffx.potential.bonded.Torsion)5 CoordRestraint (ffx.potential.nonbonded.CoordRestraint)5 MolecularAssembly (ffx.potential.MolecularAssembly)4