Program: 16563
Project: 16563
Buggy source:
@Override public MultipleAlignment refine(AFPChain selfAlignment,Atom[] atoms,int order) throws StructureException, RefinerFailedException {
  UndirectedGraph<Integer,DefaultEdge> graph=SymmetryTools.buildSymmetryGraph(selfAlignment);
  ConnectivityInspector<Integer,DefaultEdge> inspector=new ConnectivityInspector<Integer,DefaultEdge>(graph);
  List<Set<Integer>> components=inspector.connectedSets();
  if (order == 0) {
    Map<Integer,Integer> counts=new HashMap<Integer,Integer>();
    for (    Set<Integer> c : components) {
      if (counts.containsKey(c.size()))       counts.put(c.size(),counts.get(c.size()) + 1);
 else       counts.put(c.size(),1);
    }
    int maxCounts=0;
    for (    Integer ord : counts.keySet()) {
      if (counts.get(ord) > maxCounts) {
        order=ord;
        maxCounts=counts.get(ord);
      }
    }
  }
  List<ResidueGroup> groups=new ArrayList<ResidueGroup>();
  for (  Set<Integer> comp : components) {
    if (comp.size() != order) {
      ResidueGroup group=new ResidueGroup(comp);
      groups.add(group);
    }
  }
  int size=groups.size();
  if (size == 0)   throw new RefinerFailedException("String_Node_Str" + "String_Node_Str");
  GMatrix matrix=new GMatrix(size,size);
  for (int i=0; i < size; i++) {
    for (int j=i; j < size; j++) {
      if (i == j)       matrix.setElement(i,j,0);
      ResidueGroup g1=groups.get(i);
      ResidueGroup g2=groups.get(j);
      if (g1.isCompatible(g2)) {
        matrix.setElement(i,j,1);
        matrix.setElement(j,i,1);
      }
 else {
        matrix.setElement(i,j,0);
        matrix.setElement(j,i,0);
      }
    }
  }
  List<Integer> rowScores=new ArrayList<Integer>(size);
  for (int i=0; i < size; i++) {
    GVector row=new GVector(size);
    matrix.getRow(i,row);
    int rowScore=(int)row.normSquared();
    rowScores.set(i,rowScore);
  }
  List<List<Integer>> alignRes=new ArrayList<List<Integer>>(order);
  for (int i=0; i < order; i++)   alignRes.add(new ArrayList<Integer>());
  while (true) {
    Integer max=Collections.max(rowScores);
    int index=rowScores.indexOf(max);
    groups.get(index).combineWith(alignRes);
    boolean allZero=true;
    for (int i=0; i < size; i++) {
      if (matrix.getElement(index,i) < 1.0)       rowScores.set(i,0);
 else       if (rowScores.get(i) != 0)       allZero=false;
    }
    if (allZero)     break;
  }
  int length=alignRes.get(0).size();
  if (length == 0)   throw new RefinerFailedException("String_Node_Str");
  int[][][] optAln=new int[order][2][length];
  for (int bk=0; bk < order; bk++) {
    optAln[bk]=new int[2][];
    optAln[bk][0]=new int[length];
    optAln[bk][1]=new int[length];
    for (int pos=0; pos < length; pos++) {
      optAln[bk][0][pos]=alignRes.get(bk).get(pos);
      optAln[bk][1][pos]=alignRes.get((bk + 1) % order).get(pos);
    }
  }
  AFPChain afp=AlignmentTools.replaceOptAln(optAln,selfAlignment,atoms,atoms);
  return SymmetryTools.fromAFP(afp,atoms);
}

Fixed version (ground truth):
@Override public MultipleAlignment refine(AFPChain selfAlignment,Atom[] atoms,int order) throws StructureException, RefinerFailedException {
  UndirectedGraph<Integer,DefaultEdge> graph=SymmetryTools.buildSymmetryGraph(selfAlignment);
  ConnectivityInspector<Integer,DefaultEdge> inspector=new ConnectivityInspector<Integer,DefaultEdge>(graph);
  List<Set<Integer>> components=inspector.connectedSets();
  if (order == 0) {
    Map<Integer,Integer> counts=new HashMap<Integer,Integer>();
    for (    Set<Integer> c : components) {
      if (counts.containsKey(c.size()))       counts.put(c.size(),counts.get(c.size()) + 1);
 else       counts.put(c.size(),1);
    }
    int maxCounts=0;
    for (    Integer ord : counts.keySet()) {
      if (counts.get(ord) > maxCounts) {
        order=ord;
        maxCounts=counts.get(ord);
      }
    }
  }
  List<ResidueGroup> groups=new ArrayList<ResidueGroup>();
  for (  Set<Integer> comp : components) {
    if (comp.size() == order) {
      ResidueGroup group=new ResidueGroup(comp);
      groups.add(group);
    }
  }
  int size=groups.size();
  if (size == 0)   throw new RefinerFailedException("String_Node_Str" + "String_Node_Str");
  GMatrix matrix=new GMatrix(size,size);
  for (int i=0; i < size; i++) {
    for (int j=i; j < size; j++) {
      if (i == j) {
        matrix.setElement(i,j,0);
        continue;
      }
      ResidueGroup g1=groups.get(i);
      ResidueGroup g2=groups.get(j);
      if (g1.isCompatible(g2)) {
        matrix.setElement(i,j,1);
        matrix.setElement(j,i,1);
      }
 else {
        matrix.setElement(i,j,0);
        matrix.setElement(j,i,0);
      }
    }
  }
  List<Integer> rowScores=new ArrayList<Integer>(size);
  for (int i=0; i < size; i++) {
    GVector row=new GVector(size);
    matrix.getRow(i,row);
    int rowScore=(int)row.normSquared();
    rowScores.add(rowScore);
  }
  List<List<Integer>> alignRes=new ArrayList<List<Integer>>(order);
  for (int i=0; i < order; i++)   alignRes.add(new ArrayList<Integer>());
  while (true) {
    Integer max=Collections.max(rowScores);
    int index=rowScores.indexOf(max);
    groups.get(index).combineWith(alignRes);
    boolean allZero=true;
    for (int i=0; i < size; i++) {
      if (matrix.getElement(index,i) < 1.0)       rowScores.set(i,0);
 else       if (rowScores.get(i) != 0)       allZero=false;
    }
    if (allZero)     break;
  }
  int length=alignRes.get(0).size();
  if (length == 0)   throw new RefinerFailedException("String_Node_Str");
  int[][][] optAln=new int[order][2][length];
  for (int bk=0; bk < order; bk++) {
    optAln[bk]=new int[2][];
    optAln[bk][0]=new int[length];
    optAln[bk][1]=new int[length];
    for (int pos=0; pos < length; pos++) {
      optAln[bk][0][pos]=alignRes.get(bk).get(pos);
      optAln[bk][1][pos]=alignRes.get((bk + 1) % order).get(pos);
    }
  }
  AFPChain afp=AlignmentTools.replaceOptAln(optAln,selfAlignment,atoms,atoms);
  return SymmetryTools.fromAFP(afp,atoms);
}