Search in sources :

Example 1 with SSBond

use of org.biojava.bio.structure.SSBond in project ffx by mjschnie.

the class PDBFileMatcher method fixFile.

private void fixFile(FileFilePair currentPair, PDBFileReader filereader) throws IOException {
    File matchFile = currentPair.getMatchedFile();
    Structure matchStructure = currentPair.getStructure();
    if (matchStructure == null) {
        matchStructure = filereader.getStructure(matchFile);
    }
    File sourceFile = currentPair.getSourceFile();
    if (sourceFile == null) {
        throw new IOException(String.format("No source file was matched to file %s", matchFile.toString()));
    }
    Structure sourceStructure = null;
    if (fixAtoms) {
        sourceStructure = filereader.getStructure(sourceFile);
        Atom[] matchAtoms = StructureTools.getAllAtomArray(matchStructure);
        for (Atom matchAtom : matchAtoms) {
            Atom sourceAtom = getMatchingAtom(matchAtom, sourceStructure, robustMatch);
            if (fixBFactors) {
                matchAtom.setTempFactor(sourceAtom.getTempFactor());
            }
        }
    // Other methods can go here.
    }
    if (fixSSBonds) {
        if (sourceStructure == null) {
            sourceStructure = filereader.getStructure(sourceFile);
        }
        List<SSBond> sourceBonds = sourceStructure.getSSBonds();
        List<SSBond> matchBonds = matchStructure.getSSBonds();
        for (SSBond sourceBond : sourceBonds) {
            boolean isContained = false;
            for (SSBond matchBond : matchBonds) {
                if (compareSSBonds(matchBond, sourceBond)) {
                    isContained = true;
                    break;
                }
            }
            if (!isContained) {
                matchStructure.addSSBond(sourceBond.clone());
            }
        }
    }
    if (fixCryst) {
        if (sourceStructure == null) {
            sourceStructure = filereader.getStructure(sourceFile);
        }
        PDBCrystallographicInfo crystalInfo = sourceStructure.getCrystallographicInfo();
        try {
            PDBCrystallographicInfo duplicateInfo = cloneCrystalInfo(crystalInfo);
            matchStructure.setCrystallographicInfo(duplicateInfo);
        } catch (IllegalArgumentException ex) {
            logger.warning(String.format(" No crystal information for source structure " + "%s: nothing attached to file %s", sourceFile.toString(), matchFile.toString()));
        }
    }
    String pdb = matchStructure.toPDB();
    if (headerLink) {
        StringBuilder pdbBuilder = new StringBuilder(pdb);
        int position = pdbBuilder.lastIndexOf("REMARK ");
        int remarkNumber = 4;
        if (position >= 0) {
            String nextLine = pdbBuilder.substring(position, position + 1000);
            int offset = nextLine.indexOf("%n");
            if (offset < 0) {
                nextLine = pdbBuilder.substring(position);
                offset = nextLine.indexOf("%n");
            }
            position += offset;
            String[] tok = nextLine.split(" +", 3);
            try {
                remarkNumber = Integer.parseInt(tok[1]) + 1;
            } catch (NumberFormatException ex) {
            // Silent.
            }
        }
        String toInsert = String.format("REMARK%4d SOURCE FILE: %s", remarkNumber, sourceFile.getName());
        toInsert = toInsert.concat(String.format("REMARK%4d RMSD:%11.6f ANGSTROMS", remarkNumber, currentPair.getRMSD()));
        pdbBuilder.insert(position, toInsert);
        pdb = pdbBuilder.toString();
    }
    File newFile = createVersionedCopy(matchFile);
    try (BufferedWriter bw = new BufferedWriter(new FileWriter(newFile))) {
        try {
            bw.write(pdb);
        } catch (IOException ex) {
            logger.warning(String.format(" Error writing to file %s", newFile.getName()));
        }
    }
}
Also used : FileWriter(java.io.FileWriter) PDBCrystallographicInfo(org.biojava.bio.structure.PDBCrystallographicInfo) IOException(java.io.IOException) Atom(org.biojava.bio.structure.Atom) BufferedWriter(java.io.BufferedWriter) SSBond(org.biojava.bio.structure.SSBond) Structure(org.biojava.bio.structure.Structure) File(java.io.File)

Example 2 with SSBond

use of org.biojava.bio.structure.SSBond 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 3 with SSBond

use of org.biojava.bio.structure.SSBond 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)

Example 4 with SSBond

use of org.biojava.bio.structure.SSBond in project ffx by mjschnie.

the class BiojavaFilter method convert.

/**
 * {@inheritDoc}
 *
 * Parse the Biojava Structure
 *
 * @return true if the structure is successfully converted
 */
@Override
public boolean convert() {
    // First atom is #1, to match xyz file format
    int xyzIndex = 1;
    setConverted(false);
    systems.add(activeMolecularAssembly);
    if (mutate) {
        List<Character> chainIDs = new ArrayList<>();
        for (Chain chain : structure.getChains()) {
            chainIDs.add(chain.getChainID().charAt(0));
        }
        if (!chainIDs.contains(mutateChainID)) {
            if (chainIDs.size() == 1) {
                logger.warning(String.format(" Chain ID %c for " + "mutation not found: only one chain %c " + "found.", mutateChainID, chainIDs.get(0)));
                mutateChainID = chainIDs.get(0);
            } else {
                logger.warning(String.format(" Chain ID %c for " + "mutation not found: mutation will not " + "proceed.", mutateChainID));
            }
        }
    }
    /**
     * Echo the alternate location being parsed.
     */
    if (currentAltLoc == 'A') {
        logger.info(String.format(" Reading %s", structure.getName()));
    } else {
        logger.info(String.format(" Reading %s alternate location %s", structure.getName(), currentAltLoc));
    }
    org.biojava.bio.structure.Atom[] bjAtoms = StructureTools.getAllAtomArray(structure);
    int nAtoms = bjAtoms.length;
    Atom[] ffxAtoms = new Atom[nAtoms];
    /**
     * Reset the current chain and segID.
     */
    currentChainID = null;
    currentSegID = null;
    PDBCrystallographicInfo cInfo = structure.getCrystallographicInfo();
    if (cInfo.isCrystallographic()) {
        // I do not think we need to check if it already has these properties,
        // but it can be done.
        properties.addProperty("a-axis", cInfo.getA());
        properties.addProperty("b-axis", cInfo.getB());
        properties.addProperty("c-axis", cInfo.getC());
        properties.addProperty("alpha", cInfo.getAlpha());
        properties.addProperty("beta", cInfo.getBeta());
        properties.addProperty("gamma", cInfo.getGamma());
        properties.addProperty("spacegroup", SpaceGroup.pdb2ShortName(cInfo.getSpaceGroup()));
    }
    for (org.biojava.bio.structure.Atom atom : bjAtoms) {
        String name = atom.getName().toUpperCase().trim();
        double[] xyz = new double[3];
        xyz[0] = atom.getX();
        xyz[1] = atom.getY();
        xyz[2] = atom.getZ();
        char altLoc = atom.getAltLoc();
        if (!altLocs.contains(altLoc)) {
            altLocs.add(altLoc);
        }
        if (altLoc != ' ' && altLoc != 'A' && altLoc != currentAltLoc) {
            break;
        }
        if (name.contains("1H") || name.toUpperCase().contains("2H") || name.toUpperCase().contains("3H")) {
            // VERSION3_2 is presently just a placeholder for "anything non-standard".
            fileStandard = VERSION3_2;
        }
        Group group = atom.getGroup();
        ResidueNumber resnum = group.getResidueNumber();
        int resSeq = resnum.getSeqNum();
        String resName = group.getPDBName().trim().toUpperCase();
        Chain chain = group.getChain();
        char chainID = chain.getChainID().charAt(0);
        String segID = getSegID(chainID);
        boolean printAtom = false;
        if (mutate && chainID == mutateChainID && mutateResID == resSeq) {
            if (name.equals("N") || name.equals("C") || name.equals("O") || name.equals("CA")) {
                printAtom = true;
                name = mutateToResname;
            } else {
                logger.info(String.format(" Deleting atom %s of %s %d", name, resName, resSeq));
                break;
            }
        }
        Atom newAtom = new Atom(0, name, altLoc, xyz, resName, resSeq, chainID, atom.getOccupancy(), atom.getTempFactor(), segID);
        /* Biojava sets at least some capping groups, and possibly nonstandard
             amino acids to be heteroatoms. */
        boolean hetatm = true;
        for (AminoAcid3 aa3Name : AminoAcid3.values()) {
            if (aa3Name.name().equals(resName)) {
                hetatm = false;
                break;
            }
        }
        newAtom.setHetero(hetatm);
        // Look Ma, Biojava doesn't care about anisou!
        Atom returnedAtom = (Atom) activeMolecularAssembly.addMSNode(newAtom);
        if (returnedAtom != newAtom) {
            atoms.put(atom.getPDBserial(), returnedAtom);
            if (logger.isLoggable(Level.FINE)) {
                logger.fine(String.format("%s has been retained over\n%s", returnedAtom.toString(), newAtom.toString()));
            }
        } else {
            atoms.put(atom.getPDBserial(), newAtom);
            if (newAtom.getIndex() == 0) {
                newAtom.setXyzIndex(xyzIndex++);
            }
            if (printAtom) {
                logger.info(newAtom.toString());
            }
        }
    }
    List<Bond> ssBondList = new ArrayList<>();
    for (SSBond ssBond : structure.getSSBonds()) {
        Polymer c1 = activeMolecularAssembly.getChain(ssBond.getChainID1());
        Polymer c2 = activeMolecularAssembly.getChain(ssBond.getChainID2());
        int rn1;
        int rn2;
        try {
            rn1 = Integer.parseInt(ssBond.getResnum1());
            rn2 = Integer.parseInt(ssBond.getResnum1());
        } catch (NumberFormatException ex) {
            logger.warning(String.format(" Could not parse SSbond %d", ssBond.getSerNum()));
            continue;
        }
        Residue r1 = c1.getResidue(rn1);
        Residue r2 = c2.getResidue(rn2);
        List<Atom> atoms1 = r1.getAtomList();
        List<Atom> atoms2 = r2.getAtomList();
        Atom SG1 = null;
        Atom SG2 = null;
        for (Atom atom : atoms1) {
            if (atom.getName().equalsIgnoreCase("SG")) {
                SG1 = atom;
                break;
            }
        }
        for (Atom atom : atoms2) {
            if (atom.getName().equalsIgnoreCase("SG")) {
                SG2 = atom;
                break;
            }
        }
        if (SG1 == null) {
            logger.warning(String.format(" SG atom 1 of SS-bond %s is null", ssBond.toString()));
        }
        if (SG2 == null) {
            logger.warning(String.format(" SG atom 2 of SS-bond %s is null", ssBond.toString()));
        }
        if (SG1 == null || SG2 == null) {
            continue;
        }
        double d = VectorMath.dist(SG1.getXYZ(null), SG2.getXYZ(null));
        if (d < 3.0) {
            r1.setName("CYX");
            r2.setName("CYX");
            for (Atom atom : atoms1) {
                atom.setResName("CYX");
            }
            for (Atom atom : atoms2) {
                atom.setResName("CYX");
            }
            Bond bond = new Bond(SG1, SG2);
            ssBondList.add(bond);
        } else {
            String message = String.format("Ignoring [%s]\n due to distance %8.3f A.", ssBond.toString(), d);
            logger.log(Level.WARNING, message);
        }
    }
    int pdbAtoms = activeMolecularAssembly.getAtomArray().length;
    assignAtomTypes();
    StringBuilder sb = new StringBuilder(" Disulfide Bonds:");
    for (Bond bond : ssBondList) {
        Atom a1 = bond.getAtom(0);
        Atom a2 = bond.getAtom(1);
        int[] c = new int[2];
        c[0] = a1.getAtomType().atomClass;
        c[1] = a2.getAtomType().atomClass;
        String key = ffx.potential.parameters.BondType.sortKey(c);
        ffx.potential.parameters.BondType bondType = forceField.getBondType(key);
        if (bondType == null) {
            logger.severe(String.format("No BondType for key: %s\n %s\n %s", key, a1, a2));
        } else {
            bond.setBondType(bondType);
        }
        double d = VectorMath.dist(a1.getXYZ(null), a2.getXYZ(null));
        Polymer c1 = activeMolecularAssembly.getChain(a1.getSegID());
        Polymer c2 = activeMolecularAssembly.getChain(a2.getSegID());
        Residue r1 = c1.getResidue(a1.getResidueNumber());
        Residue r2 = c2.getResidue(a2.getResidueNumber());
        sb.append(String.format("\n S-S distance of %6.2f for %s and %s.", d, r1.toString(), r2.toString()));
        bondList.add(bond);
    }
    if (ssBondList.size() > 0) {
        logger.info(sb.toString());
    }
    /**
     * Finally, re-number the atoms if missing atoms were created.
     */
    if (pdbAtoms != activeMolecularAssembly.getAtomArray().length) {
        numberAtoms();
    }
    return true;
}
Also used : Chain(org.biojava.bio.structure.Chain) Group(org.biojava.bio.structure.Group) MSGroup(ffx.potential.bonded.MSGroup) SpaceGroup(ffx.crystal.SpaceGroup) ArrayList(java.util.ArrayList) PDBCrystallographicInfo(org.biojava.bio.structure.PDBCrystallographicInfo) SSBond(org.biojava.bio.structure.SSBond) AminoAcid3(ffx.potential.bonded.ResidueEnumerations.AminoAcid3) Polymer(ffx.potential.bonded.Polymer) Atom(ffx.potential.bonded.Atom) Residue(ffx.potential.bonded.Residue) ResidueNumber(org.biojava.bio.structure.ResidueNumber) Bond(ffx.potential.bonded.Bond) SSBond(org.biojava.bio.structure.SSBond)

Aggregations

SSBond (org.biojava.bio.structure.SSBond)4 Atom (ffx.potential.bonded.Atom)3 Bond (ffx.potential.bonded.Bond)3 Polymer (ffx.potential.bonded.Polymer)3 Residue (ffx.potential.bonded.Residue)3 BufferedWriter (java.io.BufferedWriter)3 File (java.io.File)3 FileWriter (java.io.FileWriter)3 IOException (java.io.IOException)3 Crystal (ffx.crystal.Crystal)2 MolecularAssembly (ffx.potential.MolecularAssembly)2 MissingAtomTypeException (ffx.potential.bonded.BondedUtils.MissingAtomTypeException)2 MissingHeavyAtomException (ffx.potential.bonded.BondedUtils.MissingHeavyAtomException)2 MSNode (ffx.potential.bonded.MSNode)2 Molecule (ffx.potential.bonded.Molecule)2 PDBCrystallographicInfo (org.biojava.bio.structure.PDBCrystallographicInfo)2 SpaceGroup (ffx.crystal.SpaceGroup)1 MSGroup (ffx.potential.bonded.MSGroup)1 AminoAcid3 (ffx.potential.bonded.ResidueEnumerations.AminoAcid3)1 ArrayList (java.util.ArrayList)1