Program: 16636
Project: 16636
Buggy source:
@Override public AFPChain refine(List<AFPChain> afpAlignments,Atom[] atoms) throws RefinerFailedException, StructureException {
  List<List<Integer>> graph=SymmetryTools.buildSymmetryGraph(afpAlignments,atoms,true);
  AFPChain afpChain=afpAlignments.get(afpAlignments.size() - 1);
  List<Integer> alreadySeen=new ArrayList<Integer>();
  List<List<Integer>> groups=new ArrayList<List<Integer>>();
  for (int i=0; i < graph.size(); i++) {
    if (!alreadySeen.contains(i)) {
      List<Integer> group=new ArrayList<Integer>();
      int residue=i;
      while (residue != -1 && !alreadySeen.contains(residue)) {
        group.add(residue);
        List<Integer> neigh=graph.get(residue);
        if (neigh.size() > 1) {
          if (neigh.get(1) > residue) {
            residue=neigh.get(1);
          }
 else           residue=-1;
        }
 else         if (neigh.size() > 0) {
          if (neigh.get(0) > residue) {
            residue=neigh.get(0);
          }
 else           residue=-1;
        }
 else         residue=-1;
      }
      Collections.sort(group);
      if (group.size() > 1) {
        groups.add(group);
        alreadySeen.addAll(group);
      }
    }
  }
  List<Integer> sizes=new ArrayList<Integer>(atoms.length);
  for (int p=0; p < atoms.length; p++)   sizes.add(0);
  for (int i=0; i < groups.size(); i++) {
    int gorder=groups.get(i).size();
    sizes.set(gorder,sizes.get(gorder) + 1);
  }
  int maxNr=0;
  for (int s=2; s < sizes.size(); s++) {
    if (sizes.get(s) * s > maxNr) {
      order=s;
      maxNr=sizes.get(s) * s;
    }
  }
  List<List<Integer>> subunits=new ArrayList<List<Integer>>();
  for (  List<Integer> g : groups)   if (g.size() == order)   subunits.add(g);
  List<Integer> deleteIndices=new ArrayList<Integer>();
  for (int i=1; i < subunits.size(); i++) {
    for (int j=0; j < subunits.get(i).size() - 1; j++) {
      if (subunits.get(i).get(j) > subunits.get(0).get(j + 1)) {
        deleteIndices.add(i);
        break;
      }
    }
  }
  for (int i=deleteIndices.size() - 1; i >= 0; i--) {
    int index=deleteIndices.get(i);
    subunits.remove(index);
  }
  for (  List<Integer> g : groups) {
    if (g.size() > order) {
      List<Integer> group=new ArrayList<Integer>();
      for (int pos=0; pos < g.size() && group.size() < order; pos++) {
        boolean consistent=true;
        for (        List<Integer> sub : subunits) {
          if (sub.get(group.size()) > g.get(pos))           consistent=false;
          if (group.size() < order - 1) {
            if (sub.get(group.size() + 1) < g.get(pos))             consistent=false;
          }
        }
        if (consistent && group.size() < order)         group.add(g.get(pos));
      }
      if (group.size() == order)       subunits.add(group);
    }
  }
  int[][][] optAln=new int[order][2][subunits.size()];
  for (int bk=0; bk < order; bk++) {
    optAln[bk]=new int[2][];
    optAln[bk][0]=new int[subunits.size()];
    optAln[bk][1]=new int[subunits.size()];
    for (int su=0; su < subunits.size(); su++) {
      optAln[bk][0][su]=subunits.get(su).get(bk);
      optAln[bk][1][su]=subunits.get(su).get((bk + 1) % order);
    }
  }
  afpChain=AlignmentTools.replaceOptAln(optAln,afpChain,atoms,atoms);
  return afpChain;
}

Fixed version (ground truth):
@Override public AFPChain refine(List<AFPChain> afpAlignments,Atom[] atoms) throws RefinerFailedException, StructureException {
  List<List<Integer>> graph=SymmetryTools.buildSymmetryGraph(afpAlignments,atoms,true);
  AFPChain afpChain=afpAlignments.get(afpAlignments.size() - 1);
  List<Integer> alreadySeen=new ArrayList<Integer>();
  List<List<Integer>> groups=new ArrayList<List<Integer>>();
  for (int i=0; i < graph.size(); i++) {
    if (!alreadySeen.contains(i)) {
      List<Integer> group=new ArrayList<Integer>();
      int residue=i;
      while (residue != -1 && !alreadySeen.contains(residue)) {
        group.add(residue);
        List<Integer> neigh=graph.get(residue);
        if (neigh.size() > 1) {
          if (neigh.get(1) > residue) {
            residue=neigh.get(1);
          }
 else           residue=-1;
        }
 else         if (neigh.size() > 0) {
          if (neigh.get(0) > residue) {
            residue=neigh.get(0);
          }
 else           residue=-1;
        }
 else         residue=-1;
      }
      Collections.sort(group);
      if (group.size() > 1) {
        groups.add(group);
        alreadySeen.addAll(group);
      }
    }
  }
  List<Integer> sizes=new ArrayList<Integer>(atoms.length);
  for (int p=0; p < atoms.length; p++)   sizes.add(0);
  for (int i=0; i < groups.size(); i++) {
    int gorder=groups.get(i).size();
    sizes.set(gorder,sizes.get(gorder) + 1);
  }
  int maxNr=0;
  for (int s=2; s < sizes.size(); s++) {
    if (sizes.get(s) * s > maxNr) {
      order=s;
      maxNr=sizes.get(s) * s;
    }
  }
  List<List<Integer>> subunits=new ArrayList<List<Integer>>();
  for (  List<Integer> g : groups)   if (g.size() == order)   subunits.add(g);
  List<Integer> deleteIndices=new ArrayList<Integer>();
  for (int i=1; i < subunits.size(); i++) {
    for (int j=0; j < subunits.get(i).size() - 1; j++) {
      if (subunits.get(i).get(j) > subunits.get(0).get(j + 1)) {
        deleteIndices.add(i);
        break;
      }
    }
  }
  for (int i=deleteIndices.size() - 1; i >= 0; i--) {
    int index=deleteIndices.get(i);
    subunits.remove(index);
  }
  for (  List<Integer> g : groups) {
    if (g.size() > order) {
      List<Integer> group=new ArrayList<Integer>();
      for (int pos=0; pos < g.size() && group.size() < order; pos++) {
        boolean consistent=true;
        for (        List<Integer> sub : subunits) {
          if (sub.get(group.size()) > g.get(pos))           consistent=false;
          if (group.size() < order - 1) {
            if (sub.get(group.size() + 1) < g.get(pos))             consistent=false;
          }
        }
        if (consistent && group.size() < order)         group.add(g.get(pos));
      }
      if (group.size() == order)       subunits.add(group);
    }
  }
  if (subunits.size() == 0) {
    throw new RefinerFailedException("String_Node_Str");
  }
  int[][][] optAln=new int[order][2][subunits.size()];
  for (int bk=0; bk < order; bk++) {
    optAln[bk]=new int[2][];
    optAln[bk][0]=new int[subunits.size()];
    optAln[bk][1]=new int[subunits.size()];
    for (int su=0; su < subunits.size(); su++) {
      optAln[bk][0][su]=subunits.get(su).get(bk);
      optAln[bk][1][su]=subunits.get(su).get((bk + 1) % order);
    }
  }
  afpChain=AlignmentTools.replaceOptAln(optAln,afpChain,atoms,atoms);
  return afpChain;
}