Search in sources :

Example 6 with Structure

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

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

the class PDBFileMatcher method calculateRMSD.

private double calculateRMSD(FileFilePair matchFile, Structure sourceStructure, StructurePairAligner aligner) throws StructureException {
    Structure matchStructure = matchFile.getStructure();
    if (superpose) {
        double rmsd = Double.MAX_VALUE;
        aligner.align(matchStructure, matchStructure);
        AlternativeAlignment[] alignments = aligner.getAlignments();
        for (AlternativeAlignment alignment : alignments) {
            double alignRMSD = alignment.getRmsd();
            rmsd = alignRMSD < rmsd ? alignRMSD : rmsd;
        }
        return rmsd;
    }
    Atom[] matchArray;
    Atom[] sourceArray;
    switch(atomsUsed) {
        case 1:
            String[] atomNames = { "CA", "N1", "N9" };
            matchArray = StructureTools.getAtomArray(matchStructure, atomNames);
            sourceArray = StructureTools.getAtomArray(sourceStructure, atomNames);
            break;
        case 2:
            List<Atom> matchAtoms = new ArrayList<>();
            List<Chain> matchChains = matchStructure.getChains();
            for (Chain chain : matchChains) {
                List<Group> matchGroups = chain.getAtomGroups(GroupType.AMINOACID);
                matchGroups.addAll(chain.getAtomGroups(GroupType.NUCLEOTIDE));
                for (Group group : matchGroups) {
                    matchAtoms.addAll(group.getAtoms());
                }
            }
            matchArray = matchAtoms.toArray(new Atom[matchAtoms.size()]);
            List<Atom> sourceAtoms = new ArrayList<>();
            List<Chain> sourceChains = sourceStructure.getChains();
            for (Chain chain : sourceChains) {
                List<Group> sourceGroups = chain.getAtomGroups(GroupType.AMINOACID);
                sourceGroups.addAll(chain.getAtomGroups(GroupType.NUCLEOTIDE));
                for (Group group : sourceGroups) {
                    sourceAtoms.addAll(group.getAtoms());
                }
            }
            sourceArray = sourceAtoms.toArray(new Atom[sourceAtoms.size()]);
            break;
        case 3:
            matchAtoms = new ArrayList<>();
            matchChains = matchStructure.getChains();
            for (Chain chain : matchChains) {
                List<Group> matchGroups = chain.getAtomGroups();
                for (Group group : matchGroups) {
                    if (!group.isWater()) {
                        matchAtoms.addAll(group.getAtoms());
                    }
                }
            }
            matchArray = matchAtoms.toArray(new Atom[matchAtoms.size()]);
            sourceAtoms = new ArrayList<>();
            sourceChains = sourceStructure.getChains();
            for (Chain chain : sourceChains) {
                List<Group> matchGroups = chain.getAtomGroups();
                for (Group group : matchGroups) {
                    if (!group.isWater()) {
                        sourceAtoms.addAll(group.getAtoms());
                    }
                }
            }
            sourceArray = sourceAtoms.toArray(new Atom[sourceAtoms.size()]);
            break;
        case 4:
            matchArray = StructureTools.getAllAtomArray(matchStructure);
            sourceArray = StructureTools.getAllAtomArray(sourceStructure);
            break;
        default:
            throw new IllegalArgumentException("atomsUsed is not 1-4: this has not been properly checked at an earlier stage.");
    }
    /*List<Atom> matchAtoms = new ArrayList<>();
         Structure matchStructure = matchFile.getStructure();
         List<Chain> matchChains = matchStructure.getChains();
         if (atomsUsed == 4) {
         matchAtoms = StructureTools.getAllAtomArray(matchStructure);
         }
         for (Chain chain : matchChains) {
         List<Group> matchGroups = chain.getSeqResGroups();
         for (Group group : matchGroups) {
         String groupType = group.getType();
         if (groupType.equalsIgnoreCase("HETATOM")) {
         if (atomsUsed < 3) {
         continue;
         } else if (atomsUsed < 4 && group.isWater()) {
         continue;
         }
         }
         if (atomsUsed != 1) {
         matchAtoms.addAll(group.getAtoms());
         } else {
         try {
         matchAtoms.add(getReferenceAtom(group));
         } catch (StructureException ex) {
         String refAtomType = (groupType.equalsIgnoreCase("AminoAcid") ? "CA" : "N1/9");
         // refAtomType should be binary between amino acid and nucleic acid, as HETATOM is eliminated.
         logger.info(String.format(" Reference atom %s could not be found for group %s",
         refAtomType, group.toString()));
         }
         }
         }
         }
         Atom[] match = new Atom[matchAtoms.size()];
         matchAtoms.toArray(match);
         matchAtoms.clear();

         List<Atom> sourceAtoms = new ArrayList<>();
         List<Chain> sourceChains = sourceStructure.getChains();
         for (Chain chain : sourceChains) {
         List<Group> sourceGroups = chain.getSeqResGroups();
         for (Group group : sourceGroups) {
         String groupType = group.getType();
         if (groupType.equalsIgnoreCase("HETATOM")) {
         if (atomsUsed < 3) {
         continue;
         } else if (atomsUsed < 4 && group.isWater()) {
         continue;
         }
         }
         if (atomsUsed != 1) {
         sourceAtoms.addAll(group.getAtoms());
         } else {
         try {
         sourceAtoms.add(getReferenceAtom(group));
         } catch (StructureException ex) {
         logger.info(String.format(" Reference atom could not be found for group %s", group.toString()));
         }
         }
         }
         }
         Atom[] sourceArray = new Atom[sourceAtoms.size()];
         sourceAtoms.toArray(sourceArray);
         sourceAtoms.clear();*/
    return simpleRMSD(sourceArray, matchArray);
}
Also used : Chain(org.biojava.bio.structure.Chain) Group(org.biojava.bio.structure.Group) ArrayList(java.util.ArrayList) Atom(org.biojava.bio.structure.Atom) AlternativeAlignment(org.biojava.bio.structure.align.pairwise.AlternativeAlignment) Structure(org.biojava.bio.structure.Structure)

Example 8 with Structure

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

the class PDBFileMatcher method getMatchingAtom.

/**
 * Searches for atom1's match in atoms2; first checks for an equivalent
 * atom, then performs a search over all atoms in atoms2.
 *
 * @param atom1 An Atom.
 * @param atoms2 An Atom[] to search.
 * @return atom1's match in atom2.
 * @throws IllegalArgumentException If no match could be found.
 */
private Atom getMatchingAtom(Atom atom1, Atom[] atoms2) throws IllegalArgumentException {
    Atom atom2 = atoms2[0];
    Structure structure2 = atom2.getGroup().getChain().getParent();
    try {
        atom2 = getMatchingAtom(atom1, structure2, false);
        return atom2;
    } catch (IllegalArgumentException ex) {
        for (Atom atom : atoms2) {
            if (compareAtoms(atom1, atom)) {
                return atom;
            }
        }
    }
    throw new IllegalArgumentException(String.format("No matching atom for %s found", atom1.toString()));
}
Also used : Structure(org.biojava.bio.structure.Structure) Atom(org.biojava.bio.structure.Atom)

Example 9 with Structure

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

the class PotentialsDataConverter method run.

/**
 * Converts the data structure to MolecularAssembly(s).
 */
@Override
public void run() {
    if (dataStructure == null || dataType.equals(Utilities.DataType.UNK)) {
        throw new IllegalArgumentException("Object passed was not recognized.");
    }
    assemblies = new ArrayList<>();
    propertyList = new ArrayList<>();
    switch(dataType) {
        case BIOJAVA:
            Structure struct = (Structure) dataStructure;
            String name = struct.getPDBCode();
            CompositeConfiguration properties = Keyword.loadProperties(file);
            MolecularAssembly assembly = new MolecularAssembly(name);
            assembly.setFile(file);
            ForceFieldFilter forceFieldFilter = new ForceFieldFilter(properties);
            ForceField forceField = forceFieldFilter.parse();
            assembly.setForceField(forceField);
            BiojavaFilter filter = new BiojavaFilter(struct, assembly, forceField, properties);
            if (filter.convert()) {
                filter.applyAtomProperties();
                assembly.finalize(true, forceField);
                ForceFieldEnergy energy = ForceFieldEnergy.energyFactory(assembly, filter.getCoordRestraints());
                assembly.setPotential(energy);
                assemblies.add(assembly);
                propertyList.add(properties);
                List<Character> altLocs = filter.getAltLocs();
                if (altLocs.size() > 1 || altLocs.get(0) != ' ') {
                    StringBuilder altLocString = new StringBuilder("\n Alternate locations found [ ");
                    for (Character c : altLocs) {
                        // Do not report the root conformer.
                        if (c == ' ') {
                            continue;
                        }
                        altLocString.append(format("(%s) ", c));
                    }
                    altLocString.append("]\n");
                    logger.info(altLocString.toString());
                }
                /**
                 * Alternate conformers may have different chemistry, so
                 * they each need to be their own MolecularAssembly.
                 */
                for (Character c : altLocs) {
                    if (c.equals(' ') || c.equals('A')) {
                        continue;
                    }
                    MolecularAssembly newAssembly = new MolecularAssembly(name);
                    newAssembly.setForceField(assembly.getForceField());
                    filter.setAltID(newAssembly, c);
                    filter.clearSegIDs();
                    if (filter.convert()) {
                        String fileName = assembly.getFile().getAbsolutePath();
                        newAssembly.setName(FilenameUtils.getBaseName(fileName) + " " + c);
                        filter.applyAtomProperties();
                        newAssembly.finalize(true, assembly.getForceField());
                        energy = ForceFieldEnergy.energyFactory(newAssembly, filter.getCoordRestraints());
                        newAssembly.setPotential(energy);
                        assemblies.add(newAssembly);
                        properties.addConfiguration(properties);
                    }
                }
            } else {
                logger.warning(String.format(" Failed to convert structure %s", dataStructure.toString()));
            }
            activeAssembly = assembly;
            activeProperties = properties;
            break;
        case UNK:
        default:
            throw new IllegalArgumentException("Object passed was not recognized.");
    }
}
Also used : CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) ForceFieldFilter(ffx.potential.parsers.ForceFieldFilter) ForceFieldEnergy(ffx.potential.ForceFieldEnergy) MolecularAssembly(ffx.potential.MolecularAssembly) ForceField(ffx.potential.parameters.ForceField) BiojavaFilter(ffx.potential.parsers.BiojavaFilter) Structure(org.biojava.bio.structure.Structure)

Example 10 with Structure

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

the class SimplePDBMatcher method match.

public void match() {
    PDBFileReader reader = new PDBFileReader();
    StructurePairAligner aligner = new StructurePairAligner();
    for (int i = 0; i < matchFiles.length; i++) {
        File matchFile = matchFiles[i];
        try {
            Structure matchStructure = reader.getStructure(matchFile);
            String matchName = matchFile.getName();
            matchedSources[i] = loopOverSources(reader, aligner, matchStructure, matchName);
        } catch (IOException ex) {
            logger.warning(String.format(" Matching failed for file %s", matchFile.getName()));
        }
    }
    for (int i = 0; i < matchFiles.length; i++) {
        logger.info(String.format(" Match file %s best source: %s at %11.7f A", matchFiles[i].getName(), matchedSources[i].file.getName(), matchedSources[i].rmsd));
    }
}
Also used : PDBFileReader(org.biojava.bio.structure.io.PDBFileReader) StructurePairAligner(org.biojava.bio.structure.align.StructurePairAligner) IOException(java.io.IOException) Structure(org.biojava.bio.structure.Structure) File(java.io.File)

Aggregations

Structure (org.biojava.bio.structure.Structure)10 File (java.io.File)5 IOException (java.io.IOException)5 Atom (org.biojava.bio.structure.Atom)3 StructureException (org.biojava.bio.structure.StructureException)3 StructurePairAligner (org.biojava.bio.structure.align.StructurePairAligner)3 PDBFileReader (org.biojava.bio.structure.io.PDBFileReader)3 ForceField (ffx.potential.parameters.ForceField)2 BiojavaFilter (ffx.potential.parsers.BiojavaFilter)2 ForceFieldFilter (ffx.potential.parsers.ForceFieldFilter)2 CompositeConfiguration (org.apache.commons.configuration.CompositeConfiguration)2 AlternativeAlignment (org.biojava.bio.structure.align.pairwise.AlternativeAlignment)2 AverageLinkageStrategy (com.apporiented.algorithm.clustering.AverageLinkageStrategy)1 Cluster (com.apporiented.algorithm.clustering.Cluster)1 ClusteringAlgorithm (com.apporiented.algorithm.clustering.ClusteringAlgorithm)1 CompleteLinkageStrategy (com.apporiented.algorithm.clustering.CompleteLinkageStrategy)1 DefaultClusteringAlgorithm (com.apporiented.algorithm.clustering.DefaultClusteringAlgorithm)1 LinkageStrategy (com.apporiented.algorithm.clustering.LinkageStrategy)1 SingleLinkageStrategy (com.apporiented.algorithm.clustering.SingleLinkageStrategy)1 IntegerForLoop (edu.rit.pj.IntegerForLoop)1