Search in sources :

Example 1 with Cluster

use of com.apporiented.algorithm.clustering.Cluster in project ffx by mjschnie.

the class ClusterStructures method cluster.

/**
 * Main execution method for ClusterStructures.
 *
 * @return A list of the final clusters
 */
public List<Cluster> cluster() {
    cacheStart = nFiles - cacheSize;
    List<Cluster> clusters;
    if (parallel) {
        clusters = clusterParallel();
    } else {
        clusters = clusterSequential();
    }
    int nClusters = clusters.size();
    generateOutputDirectories(nClusters);
    for (int i = 0; i < nClusters; i++) {
        Cluster cluster = clusters.get(i);
        String filename = String.format("%sffx_cluster_%d_summary", outputPaths[i].toString(), i);
        File summaryFile = new File(filename.concat(".txt"));
        for (int j = 1; j < 1000; j++) {
            if (summaryFile.exists()) {
                summaryFile = new File(String.format("%s_%d.txt", filename, j));
            } else {
                break;
            }
        }
        if (summaryFile.exists()) {
            logger.warning(String.format(" Could not make valid cluster summary " + "file name for %s", filename));
            continue;
        }
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(summaryFile))) {
            bw.write(String.format("PDB files for cluster %d", i));
            bw.newLine();
            bw.newLine();
            List<Cluster> childClusters = getSubclusters(cluster, 0);
            for (Cluster child : childClusters) {
                int index = Integer.parseInt(child.getName());
                bw.write(files[index].getName());
                bw.newLine();
            }
            if (copyFiles) {
                for (Cluster child : childClusters) {
                    int index = Integer.parseInt(child.getName());
                    File childFile = files[index];
                    filename = outputPaths[i].toString().concat(childFile.getName());
                    File writeTo = new File(filename);
                    try {
                        FileUtils.copyFile(childFile, writeTo, false);
                    } catch (IOException ex) {
                        logger.warning(String.format(" Could not copy file %s", filename));
                    }
                }
            }
        } catch (IOException ex) {
            logger.warning(String.format(" Failed to properly write summary " + "file for cluster %d", i));
        }
    }
    return clusters;
}
Also used : FileWriter(java.io.FileWriter) Cluster(com.apporiented.algorithm.clustering.Cluster) IOException(java.io.IOException) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 2 with Cluster

use of com.apporiented.algorithm.clustering.Cluster 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)

Aggregations

Cluster (com.apporiented.algorithm.clustering.Cluster)2 IOException (java.io.IOException)2 AverageLinkageStrategy (com.apporiented.algorithm.clustering.AverageLinkageStrategy)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 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 Structure (org.biojava.bio.structure.Structure)1 StructureException (org.biojava.bio.structure.StructureException)1 StructurePairAligner (org.biojava.bio.structure.align.StructurePairAligner)1 AlternativeAlignment (org.biojava.bio.structure.align.pairwise.AlternativeAlignment)1 PDBFileReader (org.biojava.bio.structure.io.PDBFileReader)1