Search in sources :

Example 1 with StructureException

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

the class PDBFileMatcher method matchFile.

/**
 * Interior code of file matching loop: intended to ensure consistency
 * between sequential and parallel versions of code.
 *
 * @param currentFilePair
 * @param filereader
 * @param aligner
 * @throws IOException
 */
private void matchFile(FileFilePair currentFilePair, PDBFileReader filereader, StructurePairAligner aligner) throws IOException {
    Structure currentStructure = filereader.getStructure(currentFilePair.getMatchedFile());
    currentFilePair.setStructure(currentStructure);
    int numSources = sourceFiles.length;
    for (int j = 0; j < numSources; j++) {
        File currentSource = sourceFiles[j];
        Structure sourceStructure = filereader.getStructure(currentSource);
        try {
            double rmsd = calculateRMSD(currentFilePair, sourceStructure, aligner);
            currentFilePair.attemptReplace(currentSource, rmsd);
        } catch (StructureException ex) {
            logger.warning(String.format(" Error in calculating RMSD for match %s and source %s : %s", currentFilePair.getMatchedFile().getName(), currentSource.getName(), ex.toString()));
        }
    }
}
Also used : StructureException(org.biojava.bio.structure.StructureException) Structure(org.biojava.bio.structure.Structure) File(java.io.File)

Example 2 with StructureException

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

the class ClusterStructures method clusterSequential.

/**
 * Performs clustering
 *
 * @return Final clusters.
 */
private List<Cluster> clusterSequential() {
    String[] names = new String[nFiles];
    double[][] rmsdDistances = new double[nFiles][nFiles];
    PDBFileReader fileReader = new PDBFileReader();
    LinkageStrategy ls;
    switch(algorithm) {
        case CLINK:
            ls = new CompleteLinkageStrategy();
            break;
        case SLINK:
            ls = new SingleLinkageStrategy();
            break;
        case AV_LINK:
        default:
            ls = new AverageLinkageStrategy();
            break;
    }
    for (int i = 0; i < nFiles; i++) {
        // Ensure the diagonal is filled.
        rmsdDistances[i][i] = 0.0;
        names[i] = String.format("%d", i);
        if (i >= cacheStart) {
            try {
                structureCache[i - cacheStart] = fileReader.getStructure(files[i]);
            } catch (IOException ex) {
                logger.severe(String.format(" Error in reading file %s: %s", files[i].getName(), ex.toString()));
            }
        }
    }
    StructurePairAligner aligner = new StructurePairAligner();
    for (int i = 0; i < nFiles; i++) {
        Structure structI = null;
        try {
            structI = accessStructure(i, fileReader);
        } catch (IOException ex) {
            logger.severe(String.format(" Error in reading file %s: %s", files[i].getName(), ex.toString()));
        }
        for (int j = i; j < nFiles; j++) {
            Structure structJ = null;
            try {
                structJ = accessStructure(j, fileReader);
            } catch (IOException ex) {
                logger.severe(String.format(" Error in reading file %s: %s", files[j].getName(), ex.toString()));
            }
            try {
                aligner.align(structI, structJ);
            } catch (StructureException ex) {
                logger.severe(String.format(" Exception aligning structures " + "%d and %d: %s", i, j, ex.toString()));
            }
            AlternativeAlignment[] alignments = aligner.getAlignments();
            double minRMSD = alignments[0].getRmsd();
            for (int k = 1; k < alignments.length; k++) {
                double rmsdK = alignments[k].getRmsd();
                minRMSD = rmsdK < minRMSD ? rmsdK : minRMSD;
            }
            rmsdDistances[i][j] = minRMSD;
            rmsdDistances[j][i] = minRMSD;
        }
    }
    ClusteringAlgorithm alg = new DefaultClusteringAlgorithm();
    Cluster cluster = alg.performClustering(rmsdDistances, names, ls);
    List<Cluster> subClusters;
    int nClusters = 1;
    if (numClusters > 0) {
        subClusters = new ArrayList<>(Arrays.asList(cluster));
        while (nClusters < numClusters) {
            double maxDist = subClusters.get(0).getDistanceValue();
            Cluster maxCluster = subClusters.get(0);
            for (Cluster subcluster : subClusters) {
                double dist = subcluster.getDistanceValue();
                if (dist > maxDist) {
                    maxDist = dist;
                    maxCluster = subcluster;
                }
            }
            List<Cluster> newClusters = maxCluster.getChildren();
            nClusters += (newClusters.size() - 1);
            subClusters.addAll(newClusters);
            subClusters.remove(maxCluster);
        }
        logger.severe(" Num clusters not implemented yet.");
    } else {
        subClusters = getSubclusters(cluster, rmsdCutoff);
        nClusters = subClusters.size();
    }
    assert nClusters == subClusters.size() : " nClusters != subClusters.size()";
    return subClusters;
}
Also used : DefaultClusteringAlgorithm(com.apporiented.algorithm.clustering.DefaultClusteringAlgorithm) ClusteringAlgorithm(com.apporiented.algorithm.clustering.ClusteringAlgorithm) StructurePairAligner(org.biojava.bio.structure.align.StructurePairAligner) Cluster(com.apporiented.algorithm.clustering.Cluster) IOException(java.io.IOException) SingleLinkageStrategy(com.apporiented.algorithm.clustering.SingleLinkageStrategy) PDBFileReader(org.biojava.bio.structure.io.PDBFileReader) StructureException(org.biojava.bio.structure.StructureException) DefaultClusteringAlgorithm(com.apporiented.algorithm.clustering.DefaultClusteringAlgorithm) AlternativeAlignment(org.biojava.bio.structure.align.pairwise.AlternativeAlignment) AverageLinkageStrategy(com.apporiented.algorithm.clustering.AverageLinkageStrategy) Structure(org.biojava.bio.structure.Structure) LinkageStrategy(com.apporiented.algorithm.clustering.LinkageStrategy) AverageLinkageStrategy(com.apporiented.algorithm.clustering.AverageLinkageStrategy) CompleteLinkageStrategy(com.apporiented.algorithm.clustering.CompleteLinkageStrategy) SingleLinkageStrategy(com.apporiented.algorithm.clustering.SingleLinkageStrategy) CompleteLinkageStrategy(com.apporiented.algorithm.clustering.CompleteLinkageStrategy)

Example 3 with StructureException

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

the class PDBFileMatcher method getMatchingAtom.

/**
 * Finds atom1's match in structure2; uses atom1's residue number, atom
 * type, and PDB serial number as a guess; if robustMatch is set true, will
 * search all Atoms in structure2.
 *
 * @param atom1 An Atom
 * @param structure2 A Structure to search
 * @param searchAll Whether to search all Atoms in structure2.
 * @return atom1's match in structure2
 * @throws IllegalArgumentException If no match can be found.
 */
private Atom getMatchingAtom(Atom atom1, Structure structure2, boolean searchAll) throws IllegalArgumentException {
    ResidueNumber res1 = atom1.getGroup().getResidueNumber();
    String chainID = res1.getChainId();
    Atom atom2 = null;
    try {
        Chain chain2 = structure2.getChainByPDB(chainID);
        Group group2 = chain2.getGroupByPDB(res1);
        try {
            atom2 = group2.getAtom(atom1.getName());
        } catch (StructureException ex) {
            if (atom1.getName().equalsIgnoreCase("H")) {
                atom2 = group2.getAtom("H1");
            } else if (atom1.getName().equalsIgnoreCase("H1")) {
                atom2 = group2.getAtom("H");
            }
            atom2 = group2.getAtom(atom1.getPDBserial());
        }
    } catch (StructureException ex) {
        if (!searchAll) {
            throw new IllegalArgumentException("Matching atom not found.");
        }
        for (Chain chain : structure2.getChains()) {
            for (Group group : chain.getAtomGroups()) {
                for (Atom atom : group.getAtoms()) {
                    if (compareAtoms(atom1, atom)) {
                        return atom2;
                    }
                }
            }
        }
    }
    if (atom2 != null && compareAtoms(atom1, atom2)) {
        return atom2;
    }
    throw new IllegalArgumentException("Matching atom not found.");
}
Also used : Chain(org.biojava.bio.structure.Chain) Group(org.biojava.bio.structure.Group) StructureException(org.biojava.bio.structure.StructureException) ResidueNumber(org.biojava.bio.structure.ResidueNumber) Atom(org.biojava.bio.structure.Atom)

Example 4 with StructureException

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

the class SimplePDBMatcher method checkRMSD.

private double checkRMSD(Structure matchStructure, Structure sourceStructure, StructurePairAligner aligner, String matchName, String sourceName) {
    double bestRMSD = Double.MAX_VALUE;
    try {
        aligner.align(matchStructure, sourceStructure);
        AlternativeAlignment[] alignments = aligner.getAlignments();
        for (AlternativeAlignment alignment : alignments) {
            double alignRMSD = alignment.getRmsd();
            bestRMSD = Math.min(alignRMSD, bestRMSD);
        }
        logger.info(String.format(" Minimum RMSD for match %s source %s: %11.7f", matchName, sourceName, bestRMSD));
    } catch (StructureException ex) {
        logger.warning(String.format(" Structure exception during match %s source file %s", matchName, sourceName));
    }
    return bestRMSD;
}
Also used : StructureException(org.biojava.bio.structure.StructureException) AlternativeAlignment(org.biojava.bio.structure.align.pairwise.AlternativeAlignment)

Aggregations

StructureException (org.biojava.bio.structure.StructureException)4 Structure (org.biojava.bio.structure.Structure)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 File (java.io.File)1 IOException (java.io.IOException)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 StructurePairAligner (org.biojava.bio.structure.align.StructurePairAligner)1 PDBFileReader (org.biojava.bio.structure.io.PDBFileReader)1