Search in sources :

Example 26 with Crystal

use of ffx.crystal.Crystal in project ffx by mjschnie.

the class PDBFilter method writeAtom.

/**
 * <p>
 * writeAtom</p>
 *
 * @param atom a {@link ffx.potential.bonded.Atom} object.
 * @param serial a int.
 * @param sb a {@link java.lang.StringBuilder} object.
 * @param anisouSB a {@link java.lang.StringBuilder} object.
 * @param bw a {@link java.io.BufferedWriter} object.
 * @throws java.io.IOException if any.
 */
private void writeAtom(Atom atom, int serial, StringBuilder sb, StringBuilder anisouSB, BufferedWriter bw) throws IOException {
    if (ignoreUnusedAtoms && !atom.getUse()) {
        return;
    }
    String name = atom.getName();
    if (name.length() > 4) {
        name = name.substring(0, 4);
    } else if (name.length() == 1) {
        name = name + "  ";
    } else if (name.length() == 2) {
        if (atom.getAtomType().valence == 0) {
            name = name + "  ";
        } else {
            name = name + " ";
        }
    }
    double[] xyz = vdwH ? atom.getRedXYZ() : atom.getXYZ(null);
    if (nSymOp != 0) {
        Crystal crystal = activeMolecularAssembly.getCrystal();
        SymOp symOp = crystal.spaceGroup.getSymOp(nSymOp);
        double[] newXYZ = new double[xyz.length];
        crystal.applySymOp(xyz, newXYZ, symOp);
        xyz = newXYZ;
    }
    sb.replace(6, 16, String.format("%5s " + padLeft(name.toUpperCase(), 4), Hybrid36.encode(5, serial)));
    Character altLoc = atom.getAltLoc();
    if (altLoc != null) {
        sb.setCharAt(16, altLoc);
    } else {
        sb.setCharAt(16, ' ');
    }
    /*sb.replace(30, 66, String.format("%8.3f%8.3f%8.3f%6.2f%6.2f",
                xyz[0], xyz[1], xyz[2], atom.getOccupancy(), atom.getTempFactor()));*/
    /**
     * On the following code: #1: StringBuilder.replace will allow for
     * longer strings, expanding the StringBuilder's length if necessary.
     * #2: sb was never re-initialized, so if there was overflow, sb would
     * continue to be > 80 characters long, resulting in broken PDB files
     * #3: It may be wiser to have XYZ coordinates result in shutdown, not
     * truncation of coordinates. #4: Excessive B-factors aren't much of an
     * issue; if the B-factor is past 999.99, that's the difference between
     * "density extends to Venus" and "density extends to Pluto".
     */
    StringBuilder decimals = new StringBuilder();
    for (int i = 0; i < 3; i++) {
        try {
            decimals.append(StringUtils.fwFpDec(xyz[i], 8, 3));
        } catch (IllegalArgumentException ex) {
            String newValue = StringUtils.fwFpTrunc(xyz[i], 8, 3);
            logger.info(String.format(" XYZ %d coordinate %8.3f for atom %s " + "overflowed bounds of 8.3f string specified by PDB " + "format; truncating value to %s", i, xyz[i], atom.toString(), newValue));
            decimals.append(newValue);
        }
    }
    try {
        decimals.append(StringUtils.fwFpDec(atom.getOccupancy(), 6, 2));
    } catch (IllegalArgumentException ex) {
        logger.severe(String.format(" Occupancy %f for atom %s is impossible; " + "value must be between 0 and 1", atom.getOccupancy(), atom.toString()));
    }
    try {
        decimals.append(StringUtils.fwFpDec(atom.getTempFactor(), 6, 2));
    } catch (IllegalArgumentException ex) {
        String newValue = StringUtils.fwFpTrunc(atom.getTempFactor(), 6, 2);
        logger.info(String.format(" Atom temp factor %6.2f for atom %s overflowed " + "bounds of 6.2f string specified by PDB format; truncating " + "value to %s", atom.getTempFactor(), atom.toString(), newValue));
        decimals.append(newValue);
    }
    sb.replace(30, 66, decimals.toString());
    name = Atom.ElementSymbol.values()[atom.getAtomicNumber() - 1].toString();
    name = name.toUpperCase();
    if (atom.isDeuterium()) {
        name = "D";
    }
    sb.replace(76, 78, padLeft(name, 2));
    sb.replace(78, 80, String.format("%2d", 0));
    if (!listMode) {
        bw.write(sb.toString());
        bw.newLine();
    } else {
        listOutput.add(sb.toString());
    }
    // =============================================================================
    // 1 - 6        Record name   "ANISOU"
    // 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          Insertion code.
    // 29 - 35       Integer       u[0][0]        U(1,1)
    // 36 - 42       Integer       u[1][1]        U(2,2)
    // 43 - 49       Integer       u[2][2]        U(3,3)
    // 50 - 56       Integer       u[0][1]        U(1,2)
    // 57 - 63       Integer       u[0][2]        U(1,3)
    // 64 - 70       Integer       u[1][2]        U(2,3)
    // 77 - 78       LString(2)    element        Element symbol, right-justified.
    // 79 - 80       LString(2)    charge         Charge on the atom.
    // =============================================================================
    double[] anisou = atom.getAnisou(null);
    if (anisou != null) {
        anisouSB.replace(6, 80, sb.substring(6, 80));
        anisouSB.replace(28, 70, String.format("%7d%7d%7d%7d%7d%7d", (int) (anisou[0] * 1e4), (int) (anisou[1] * 1e4), (int) (anisou[2] * 1e4), (int) (anisou[3] * 1e4), (int) (anisou[4] * 1e4), (int) (anisou[5] * 1e4)));
        if (!listMode) {
            bw.write(anisouSB.toString());
            bw.newLine();
        } else {
            listOutput.add(anisouSB.toString());
        }
    }
}
Also used : SymOp(ffx.crystal.SymOp) Crystal(ffx.crystal.Crystal)

Example 27 with Crystal

use of ffx.crystal.Crystal in project ffx by mjschnie.

the class PDBFilter method writeSIFTAtom.

private void writeSIFTAtom(Atom atom, int serial, StringBuilder sb, StringBuilder anisouSB, BufferedWriter bw, String siftScore) throws IOException {
    String name = atom.getName();
    if (name.length() > 4) {
        name = name.substring(0, 4);
    } else if (name.length() == 1) {
        name = name + "  ";
    } else if (name.length() == 2) {
        if (atom.getAtomType().valence == 0) {
            name = name + "  ";
        } else {
            name = name + " ";
        }
    }
    double[] xyz = vdwH ? atom.getRedXYZ() : atom.getXYZ(null);
    if (nSymOp != 0) {
        Crystal crystal = activeMolecularAssembly.getCrystal();
        SymOp symOp = crystal.spaceGroup.getSymOp(nSymOp);
        double[] newXYZ = new double[xyz.length];
        crystal.applySymOp(xyz, newXYZ, symOp);
        xyz = newXYZ;
    }
    sb.replace(6, 16, String.format("%5s " + padLeft(name.toUpperCase(), 4), Hybrid36.encode(5, serial)));
    Character altLoc = atom.getAltLoc();
    if (altLoc != null) {
        sb.setCharAt(16, altLoc);
    } else {
        sb.setCharAt(16, ' ');
    }
    if (siftScore == null) {
        sb.replace(30, 66, String.format("%8.3f%8.3f%8.3f%6.2f%6.2f", xyz[0], xyz[1], xyz[2], atom.getOccupancy(), 110.0));
    } else {
        sb.replace(30, 66, String.format("%8.3f%8.3f%8.3f%6.2f%6.2f", xyz[0], xyz[1], xyz[2], atom.getOccupancy(), (1 + (-1 * Float.parseFloat(siftScore))) * 100));
    }
    name = Atom.ElementSymbol.values()[atom.getAtomicNumber() - 1].toString();
    name = name.toUpperCase();
    if (atom.isDeuterium()) {
        name = "D";
    }
    sb.replace(76, 78, padLeft(name, 2));
    sb.replace(78, 80, String.format("%2d", 0));
    if (!listMode) {
        bw.write(sb.toString());
        bw.newLine();
    } else {
        listOutput.add(sb.toString());
    }
    // =============================================================================
    // 1 - 6        Record name   "ANISOU"
    // 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          Insertion code.
    // 29 - 35       Integer       u[0][0]        U(1,1)
    // 36 - 42       Integer       u[1][1]        U(2,2)
    // 43 - 49       Integer       u[2][2]        U(3,3)
    // 50 - 56       Integer       u[0][1]        U(1,2)
    // 57 - 63       Integer       u[0][2]        U(1,3)
    // 64 - 70       Integer       u[1][2]        U(2,3)
    // 77 - 78       LString(2)    element        Element symbol, right-justified.
    // 79 - 80       LString(2)    charge         Charge on the atom.
    // =============================================================================
    double[] anisou = atom.getAnisou(null);
    if (anisou != null) {
        anisouSB.replace(6, 80, sb.substring(6, 80));
        anisouSB.replace(28, 70, String.format("%7d%7d%7d%7d%7d%7d", (int) (anisou[0] * 1e4), (int) (anisou[1] * 1e4), (int) (anisou[2] * 1e4), (int) (anisou[3] * 1e4), (int) (anisou[4] * 1e4), (int) (anisou[5] * 1e4)));
        if (!listMode) {
            bw.write(anisouSB.toString());
            bw.newLine();
        } else {
            listOutput.add(anisouSB.toString());
        }
    }
}
Also used : SymOp(ffx.crystal.SymOp) Crystal(ffx.crystal.Crystal)

Example 28 with Crystal

use of ffx.crystal.Crystal in project ffx by mjschnie.

the class XYZFilter method writeFileAsP1.

/**
 * <p>
 * writeFileAsP1</p>
 *
 * @param saveFile a {@link java.io.File} object.
 * @param append a boolean.
 * @param crystal a {@link ffx.crystal.Crystal} object.
 * @return a boolean.
 */
public boolean writeFileAsP1(File saveFile, boolean append, Crystal crystal) {
    if (saveFile == null) {
        return false;
    }
    try {
        File newFile = saveFile;
        if (!append) {
            newFile = version(saveFile);
        }
        activeMolecularAssembly.setFile(newFile);
        activeMolecularAssembly.setName(newFile.getName());
        FileWriter fw = new FileWriter(newFile, append);
        BufferedWriter bw = new BufferedWriter(fw);
        int nSymm = crystal.spaceGroup.symOps.size();
        // XYZ File First Line
        int numberOfAtoms = activeMolecularAssembly.getAtomList().size() * nSymm;
        String output = format("%7d %s\n", numberOfAtoms, activeMolecularAssembly.toString());
        bw.write(output);
        if (!crystal.aperiodic()) {
            Crystal uc = crystal.getUnitCell();
            String params = String.format("%14.8f%14.8f%14.8f%14.8f%14.8f%14.8f\n", uc.a, uc.b, uc.c, uc.alpha, uc.beta, uc.gamma);
            bw.write(params);
        }
        Atom a2;
        StringBuilder line;
        StringBuilder[] lines = new StringBuilder[numberOfAtoms];
        // XYZ File Atom Lines
        Atom[] atoms = activeMolecularAssembly.getAtomArray();
        double[] xyz = new double[3];
        for (int iSym = 0; iSym < nSymm; iSym++) {
            SymOp symOp = crystal.spaceGroup.getSymOp(iSym);
            int indexOffset = iSym * atoms.length;
            for (Atom a : atoms) {
                int index = a.getIndex() + indexOffset;
                String id = a.getAtomType().name;
                if (vdwH) {
                    a.getRedXYZ(xyz);
                } else {
                    xyz[0] = a.getX();
                    xyz[1] = a.getY();
                    xyz[2] = a.getZ();
                }
                crystal.applySymOp(xyz, xyz, symOp);
                int type = a.getType();
                line = new StringBuilder(format("%7d %3s%14.8f%14.8f%14.8f%6d", index, id, xyz[0], xyz[1], xyz[2], type));
                for (Bond b : a.getBonds()) {
                    a2 = b.get1_2(a);
                    line.append(format("%8d", a2.getIndex() + indexOffset));
                }
                lines[index - 1] = line.append("\n");
            }
        }
        try {
            for (int i = 0; i < numberOfAtoms; i++) {
                bw.write(lines[i].toString());
            }
        } catch (IOException e) {
            String message = format(" Their was an unexpected error writing to %s.", getActiveMolecularSystem().toString());
            logger.log(Level.WARNING, message, e);
            return false;
        }
        bw.close();
        fw.close();
    } catch (IOException e) {
        String message = format(" Their was an unexpected error writing to %s.", getActiveMolecularSystem().toString());
        logger.log(Level.WARNING, message, e);
        return false;
    }
    return true;
}
Also used : SymOp(ffx.crystal.SymOp) FileWriter(java.io.FileWriter) IOException(java.io.IOException) Atom(ffx.potential.bonded.Atom) BufferedWriter(java.io.BufferedWriter) Bond(ffx.potential.bonded.Bond) File(java.io.File) Crystal(ffx.crystal.Crystal)

Example 29 with Crystal

use of ffx.crystal.Crystal in project ffx by mjschnie.

the class BiojavaFilter 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 (saveFile == null) {
        return false;
    }
    if (vdwH) {
        logger.info(" Printing hydrogens to van der Waals centers instead of nuclear locations.");
    }
    /**
     * 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   ");
    for (int i = 6; i < 80; i++) {
        sb.append(' ');
        anisouSB.append(' ');
        terSB.append(' ');
    }
    FileWriter fw;
    BufferedWriter bw;
    try {
        File newFile = saveFile;
        if (!append) {
            newFile = version(saveFile);
        }
        activeMolecularAssembly.setFile(newFile);
        activeMolecularAssembly.setName(newFile.getName());
        logger.log(Level.INFO, " Saving {0}", newFile.getName());
        fw = new FileWriter(newFile, append);
        bw = new BufferedWriter(fw);
        // =============================================================================
        // 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) {
                            if (atom.getName().equalsIgnoreCase("SG")) {
                                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++;
        }
        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) IOException(java.io.IOException) MissingHeavyAtomException(ffx.potential.bonded.BondedUtils.MissingHeavyAtomException) 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) SSBond(org.biojava.bio.structure.SSBond) File(java.io.File) Crystal(ffx.crystal.Crystal)

Example 30 with Crystal

use of ffx.crystal.Crystal in project ffx by mjschnie.

the class BiojavaFilter method writeSIFTFile.

/*public Structure writeToStructure(String header) {
     return writeToStructure(activeMolecularAssembly, header);
     }

     public static Structure writeToStructure(MolecularAssembly assembly, String header) {
     Structure structure = new StructureImpl();
     for (Polymer polymer : assembly.getChains()) {
     Chain chain = new ChainImpl();
     for (Residue residue : polymer.getResidues()) {
     Group group;
     switch (residue.getResidueType()) {
     case AA:
     group = new AminoAcidImpl();
     break;
     case NA:
     group = new NucleotideImpl();
     break;
     default:
     group = new HetatomImpl();
     break;
     }
     for (Atom atom : residue.getAtomList()) {
     org.biojava.bio.structure.Atom bjAtom = new AtomImpl();
     group.addAtom(bjAtom);
     }
     chain.addGroup(group);
     }
     structure.addChain(chain);
     }

     for (Molecule molecule : assembly.getMolecules()) {
     for (Atom atom : molecule.getAtomList()) {

     }
     }
     }*/
public boolean writeSIFTFile(File saveFile, boolean append, String[] resAndScore) {
    if (saveFile == null) {
        return false;
    }
    if (vdwH) {
        logger.info(" Printing hydrogens to van der Waals centers instead of nuclear locations.");
    }
    /**
     * 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   ");
    for (int i = 6; i < 80; i++) {
        sb.append(' ');
        anisouSB.append(' ');
        terSB.append(' ');
    }
    FileWriter fw;
    BufferedWriter bw;
    try {
        File newFile = saveFile;
        if (!append) {
            newFile = version(saveFile);
        }
        activeMolecularAssembly.setFile(newFile);
        activeMolecularAssembly.setName(newFile.getName());
        logger.log(Level.INFO, " Saving {0}", newFile.getName());
        fw = new FileWriter(newFile, append);
        bw = new BufferedWriter(fw);
        // =============================================================================
        // 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) {
                            if (atom.getName().equalsIgnoreCase("SG")) {
                                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();
                    int i = 0;
                    String[] entries = null;
                    for (; i < resAndScore.length; i++) {
                        entries = resAndScore[i].split("\\t");
                        if (!entries[0].equals(entries[0].replaceAll("\\D+", ""))) {
                            String[] subEntries = entries[0].split("[^0-9]");
                            entries[0] = subEntries[0];
                        }
                        if (entries[0].equals(String.valueOf(resID)) && !".".equals(entries[1])) {
                            break;
                        }
                    }
                    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();
                    boolean altLocFound = false;
                    for (Atom atom : residueAtoms) {
                        if (i != resAndScore.length) {
                            writeSIFTAtom(atom, serial++, sb, anisouSB, bw, entries[1]);
                        } else {
                            writeSIFTAtom(atom, serial++, sb, anisouSB, bw, null);
                        }
                        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);
                            residueAtoms = altResidue.getAtomList();
                            for (Atom atom : residueAtoms) {
                                if (atom.getAltLoc() != null && !atom.getAltLoc().equals(' ') && !atom.getAltLoc().equals('A')) {
                                    if (i != resAndScore.length) {
                                        writeSIFTAtom(atom, serial++, sb, anisouSB, bw, entries[1]);
                                    } else {
                                        writeSIFTAtom(atom, serial++, sb, anisouSB, bw, null);
                                    }
                                }
                            }
                        }
                    }
                }
                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) {
                writeSIFTAtom(atom, serial++, sb, anisouSB, bw, null);
                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')) {
                            writeSIFTAtom(atom, serial++, sb, anisouSB, bw, null);
                        }
                    }
                }
            }
            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) {
                writeSIFTAtom(atom, serial++, sb, anisouSB, bw, null);
                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')) {
                            writeSIFTAtom(atom, serial++, sb, anisouSB, bw, null);
                        }
                    }
                }
            }
            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) {
                writeSIFTAtom(atom, serial++, sb, anisouSB, bw, null);
                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')) {
                            writeSIFTAtom(atom, serial++, sb, anisouSB, bw, null);
                        }
                    }
                }
            }
            resID++;
        }
        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) IOException(java.io.IOException) MissingHeavyAtomException(ffx.potential.bonded.BondedUtils.MissingHeavyAtomException) 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) SSBond(org.biojava.bio.structure.SSBond) File(java.io.File) Crystal(ffx.crystal.Crystal)

Aggregations

Crystal (ffx.crystal.Crystal)38 Atom (ffx.potential.bonded.Atom)20 File (java.io.File)14 IOException (java.io.IOException)12 Bond (ffx.potential.bonded.Bond)8 Residue (ffx.potential.bonded.Residue)8 ReflectionList (ffx.crystal.ReflectionList)7 Resolution (ffx.crystal.Resolution)7 MolecularAssembly (ffx.potential.MolecularAssembly)7 BufferedWriter (java.io.BufferedWriter)7 FileWriter (java.io.FileWriter)7 SymOp (ffx.crystal.SymOp)5 MSNode (ffx.potential.bonded.MSNode)5 CompositeConfiguration (org.apache.commons.configuration.CompositeConfiguration)5 PointerByReference (com.sun.jna.ptr.PointerByReference)4 ParallelTeam (edu.rit.pj.ParallelTeam)4 MissingAtomTypeException (ffx.potential.bonded.BondedUtils.MissingAtomTypeException)4 MissingHeavyAtomException (ffx.potential.bonded.BondedUtils.MissingHeavyAtomException)4 Molecule (ffx.potential.bonded.Molecule)4 MultiResidue (ffx.potential.bonded.MultiResidue)4