Search in sources :

Example 1 with PDBCrystallographicInfo

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

the class PDBFileMatcher method cloneCrystalInfo.

/**
 * Creates a duplicate PDBCrystallographicInfo object.
 *
 * @param sourceInfo
 * @return duplicate of sourceInfo
 * @throws IllegalArgumentException if sourceInfo not crystallographic.
 */
private PDBCrystallographicInfo cloneCrystalInfo(PDBCrystallographicInfo sourceInfo) throws IllegalArgumentException {
    if (!sourceInfo.isCrystallographic()) {
        throw new IllegalArgumentException(" Source structure has no meaningful " + "crystallographic information.");
    }
    PDBCrystallographicInfo retInfo = new PDBCrystallographicInfo();
    retInfo.setSpaceGroup(sourceInfo.getSpaceGroup());
    // Newer versions of BioJava can use the CrystalCell object.
    retInfo.setA(sourceInfo.getA());
    retInfo.setAlpha(sourceInfo.getAlpha());
    retInfo.setB(sourceInfo.getB());
    retInfo.setBeta(sourceInfo.getBeta());
    retInfo.setC(sourceInfo.getC());
    retInfo.setGamma(sourceInfo.getGamma());
    retInfo.setZ(sourceInfo.getZ());
    return retInfo;
}
Also used : PDBCrystallographicInfo(org.biojava.bio.structure.PDBCrystallographicInfo)

Example 2 with PDBCrystallographicInfo

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

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

PDBCrystallographicInfo (org.biojava.bio.structure.PDBCrystallographicInfo)3 SSBond (org.biojava.bio.structure.SSBond)2 SpaceGroup (ffx.crystal.SpaceGroup)1 Atom (ffx.potential.bonded.Atom)1 Bond (ffx.potential.bonded.Bond)1 MSGroup (ffx.potential.bonded.MSGroup)1 Polymer (ffx.potential.bonded.Polymer)1 Residue (ffx.potential.bonded.Residue)1 AminoAcid3 (ffx.potential.bonded.ResidueEnumerations.AminoAcid3)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Atom (org.biojava.bio.structure.Atom)1 Chain (org.biojava.bio.structure.Chain)1 Group (org.biojava.bio.structure.Group)1 ResidueNumber (org.biojava.bio.structure.ResidueNumber)1 Structure (org.biojava.bio.structure.Structure)1