Program: 16766
Project: 16766
Buggy source:
/** 
 * Calculate the interface clusters for this StructureInterfaceList  using a contact overlap score to measure the similarity of interfaces. Subsequent calls will use the cached value without recomputing the clusters.
 * @param contactOverlapScoreClusterCutoff the contact overlap score above which a pair will be clustered
 * @return
 */
public List<StructureInterfaceCluster> getClusters(double contactOverlapScoreClusterCutoff){
  if (clusters != null) {
    return clusters;
  }
  clusters=new ArrayList<StructureInterfaceCluster>();
  if (list.size() == 0)   return clusters;
  double[][] matrix=new double[list.size()][list.size()];
  for (int i=0; i < list.size(); i++) {
    for (int j=i + 1; j < list.size(); j++) {
      StructureInterface iInterf=list.get(i);
      StructureInterface jInterf=list.get(j);
      double scoreDirect=iInterf.getContactOverlapScore(jInterf,false);
      double scoreInvert=iInterf.getContactOverlapScore(jInterf,true);
      double maxScore=Math.max(scoreDirect,scoreInvert);
      matrix[i][j]=maxScore;
    }
  }
  SingleLinkageClusterer slc=new SingleLinkageClusterer(matrix,true);
  Map<Integer,Set<Integer>> clusteredIndices=slc.getClusters(contactOverlapScoreClusterCutoff);
  for (  int clusterIdx : clusteredIndices.keySet()) {
    List<StructureInterface> members=new ArrayList<StructureInterface>();
    for (    int idx : clusteredIndices.get(clusterIdx)) {
      members.add(list.get(idx));
    }
    StructureInterfaceCluster cluster=new StructureInterfaceCluster();
    cluster.setMembers(members);
    double averageScore=0.0;
    int countPairs=0;
    for (int i=0; i < members.size(); i++) {
      for (int j=i + 1; j < members.size(); j++) {
        averageScore+=matrix[members.get(i).getId() - 1][members.get(j).getId() - 1];
        countPairs++;
      }
    }
    averageScore=averageScore / (double)countPairs;
    cluster.setAverageScore(averageScore);
    clusters.add(cluster);
  }
  for (  StructureInterfaceCluster cluster : clusters) {
    for (    StructureInterface interf : cluster.getMembers()) {
      interf.setCluster(cluster);
    }
  }
  Collections.sort(clusters,new Comparator<StructureInterfaceCluster>(){
    @Override public int compare(    StructureInterfaceCluster o1,    StructureInterfaceCluster o2){
      return Double.compare(o2.getTotalArea(),o1.getTotalArea());
    }
  }
);
  int id=1;
  for (  StructureInterfaceCluster cluster : clusters) {
    cluster.setId(id);
    id++;
  }
  return clusters;
}

Fixed version (ground truth):
/** 
 * Calculate the interface clusters for this StructureInterfaceList  using a contact overlap score to measure the similarity of interfaces. Subsequent calls will use the cached value without recomputing the clusters.
 * @param contactOverlapScoreClusterCutoff the contact overlap score above which a pair will be clustered
 * @return
 */
public List<StructureInterfaceCluster> getClusters(double contactOverlapScoreClusterCutoff){
  if (clusters != null) {
    return clusters;
  }
  clusters=new ArrayList<StructureInterfaceCluster>();
  if (list.size() == 0)   return clusters;
  double[][] matrix=new double[list.size()][list.size()];
  for (int i=0; i < list.size(); i++) {
    for (int j=i + 1; j < list.size(); j++) {
      StructureInterface iInterf=list.get(i);
      StructureInterface jInterf=list.get(j);
      double scoreDirect=iInterf.getContactOverlapScore(jInterf,false);
      double scoreInvert=iInterf.getContactOverlapScore(jInterf,true);
      double maxScore=Math.max(scoreDirect,scoreInvert);
      matrix[i][j]=maxScore;
    }
  }
  SingleLinkageClusterer slc=new SingleLinkageClusterer(matrix,true);
  Map<Integer,Set<Integer>> clusteredIndices=slc.getClusters(contactOverlapScoreClusterCutoff);
  for (  int clusterIdx : clusteredIndices.keySet()) {
    List<StructureInterface> members=new ArrayList<StructureInterface>();
    for (    int idx : clusteredIndices.get(clusterIdx)) {
      members.add(list.get(idx));
    }
    StructureInterfaceCluster cluster=new StructureInterfaceCluster();
    cluster.setMembers(members);
    double averageScore=0.0;
    int countPairs=0;
    for (int i=0; i < members.size(); i++) {
      for (int j=i + 1; j < members.size(); j++) {
        averageScore+=matrix[members.get(i).getId() - 1][members.get(j).getId() - 1];
        countPairs++;
      }
    }
    if (countPairs > 0) {
      averageScore=averageScore / (double)countPairs;
    }
 else {
      averageScore=1.0;
    }
    cluster.setAverageScore(averageScore);
    clusters.add(cluster);
  }
  for (  StructureInterfaceCluster cluster : clusters) {
    for (    StructureInterface interf : cluster.getMembers()) {
      interf.setCluster(cluster);
    }
  }
  Collections.sort(clusters,new Comparator<StructureInterfaceCluster>(){
    @Override public int compare(    StructureInterfaceCluster o1,    StructureInterfaceCluster o2){
      return Double.compare(o2.getTotalArea(),o1.getTotalArea());
    }
  }
);
  int id=1;
  for (  StructureInterfaceCluster cluster : clusters) {
    cluster.setId(id);
    id++;
  }
  return clusters;
}