Search in sources :

Example 1 with AlternativeAlignment

use of org.biojava.bio.structure.align.pairwise.AlternativeAlignment 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 2 with AlternativeAlignment

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

use of org.biojava.bio.structure.align.pairwise.AlternativeAlignment 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

AlternativeAlignment (org.biojava.bio.structure.align.pairwise.AlternativeAlignment)3 Structure (org.biojava.bio.structure.Structure)2 StructureException (org.biojava.bio.structure.StructureException)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 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 StructurePairAligner (org.biojava.bio.structure.align.StructurePairAligner)1 PDBFileReader (org.biojava.bio.structure.io.PDBFileReader)1