Program: 16645
Project: 16645
Buggy source:
/** 
 * This method takes a list of pairwise alignments to the reference  structure and calculates a MultipleAlignment resulting from combining  their residue equivalencies. <p> It uses the blocks in AFPChain as   {@link Block}s in the  MultipleAlignment, so considers non-topological alignments, if the alignment is rigid. If the alignment is flexible,  it considers the blocks as   {@link BlockSets}.
 * @param afpList the list of pairwise alignments to the reference
 * @param atomArrays List of Atoms of the structures
 * @param ref index of the reference structure
 * @param flexible uses BlockSets if true, uses Blocks otherwise
 * @return MultipleAlignment seed alignment
 * @throws StructureException 
 */
private static MultipleAlignment combineReferenceAlignments(List<AFPChain> afpList,List<Atom[]> atomArrays,int ref,boolean flexible) throws StructureException {
  int size=atomArrays.size();
  int length=0;
  if (ref == 0)   length=afpList.get(1).getCa1Length();
 else   length=afpList.get(0).getCa2Length();
  SortedSet<Integer> flexibleBoundaries=new TreeSet<Integer>();
  List<List<Integer>> equivalencies=new ArrayList<List<Integer>>();
  for (int str=0; str < size; str++) {
    equivalencies.add(new ArrayList<Integer>());
    for (int res=0; res < length; res++) {
      if (str == ref)       equivalencies.get(str).add(res);
 else       equivalencies.get(str).add(null);
    }
  }
  for (int str=0; str < size; str++) {
    if (str == ref)     continue;
    for (int bk=0; bk < afpList.get(str).getBlockNum(); bk++) {
      for (int i=0; i < afpList.get(str).getOptLen()[bk]; i++) {
        int res1=0;
        int res2=0;
        if (str > ref) {
          res1=afpList.get(str).getOptAln()[bk][0][i];
          res2=afpList.get(str).getOptAln()[bk][1][i];
        }
 else         if (str < ref) {
          res1=afpList.get(str).getOptAln()[bk][1][i];
          res2=afpList.get(str).getOptAln()[bk][0][i];
        }
        equivalencies.get(str).set(res1,res2);
        if (flexible && i == 0)         flexibleBoundaries.add(res1);
      }
    }
  }
  MultipleAlignment seed=new MultipleAlignmentImpl();
  seed.getEnsemble().setAtomArrays(atomArrays);
  BlockSet blockSet=new BlockSetImpl(seed);
  new BlockImpl(blockSet);
  int[] lastResidues=new int[size];
  Arrays.fill(lastResidues,-1);
  for (int pos=0; pos < length; pos++) {
    if (flexibleBoundaries.contains(pos) && blockSet.getBlocks().get(blockSet.getBlocks().size() - 1).getAlignRes() != null) {
      blockSet=new BlockSetImpl(seed);
      new BlockImpl(blockSet);
    }
    boolean cp=false;
    for (int str=0; str < size; str++) {
      if (equivalencies.get(str).get(pos) == null) {
        continue;
      }
 else       if (equivalencies.get(str).get(pos) < lastResidues[str]) {
        cp=true;
        break;
      }
 else       lastResidues[str]=equivalencies.get(str).get(pos);
    }
    if (cp) {
      new BlockImpl(blockSet);
      Arrays.fill(lastResidues,-1);
    }
    for (int str=0; str < size; str++) {
      Block lastB=blockSet.getBlocks().get(blockSet.getBlocks().size() - 1);
      if (lastB.getAlignRes() == null) {
        List<List<Integer>> alnRes=new ArrayList<List<Integer>>(size);
        for (int k=0; k < size; k++) {
          alnRes.add(new ArrayList<Integer>());
        }
        lastB.setAlignRes(alnRes);
      }
      lastB.getAlignRes().get(str).add(equivalencies.get(str).get(pos));
    }
  }
  MultipleSuperimposer imposer=new ReferenceSuperimposer(ref);
  imposer.superimpose(seed);
  return seed;
}

Fixed version (ground truth):
/** 
 * This method takes a list of pairwise alignments to the reference  structure and calculates a MultipleAlignment resulting from combining  their residue equivalencies. <p> It uses the blocks in AFPChain as   {@link Block}s in the  MultipleAlignment, so considers non-topological alignments, if the alignment is rigid. If the alignment is flexible,  it considers the blocks as   {@link BlockSets}.
 * @param afpList the list of pairwise alignments to the reference
 * @param atomArrays List of Atoms of the structures
 * @param ref index of the reference structure
 * @param flexible uses BlockSets if true, uses Blocks otherwise
 * @return MultipleAlignment seed alignment
 * @throws StructureException 
 */
private static MultipleAlignment combineReferenceAlignments(List<AFPChain> afpList,List<Atom[]> atomArrays,int ref,boolean flexible) throws StructureException {
  int size=atomArrays.size();
  int length=0;
  if (ref == 0)   length=afpList.get(1).getCa1Length();
 else   length=afpList.get(0).getCa2Length();
  SortedSet<Integer> flexibleBoundaries=new TreeSet<Integer>();
  List<List<Integer>> equivalencies=new ArrayList<List<Integer>>();
  for (int str=0; str < size; str++) {
    equivalencies.add(new ArrayList<Integer>());
    for (int res=0; res < length; res++) {
      if (str == ref)       equivalencies.get(str).add(res);
 else       equivalencies.get(str).add(null);
    }
  }
  for (int str=0; str < size; str++) {
    if (str == ref)     continue;
    for (int bk=0; bk < afpList.get(str).getBlockNum(); bk++) {
      for (int i=0; i < afpList.get(str).getOptLen()[bk]; i++) {
        int res1=0;
        int res2=0;
        if (str > ref) {
          res1=afpList.get(str).getOptAln()[bk][0][i];
          res2=afpList.get(str).getOptAln()[bk][1][i];
        }
 else         if (str < ref) {
          res1=afpList.get(str).getOptAln()[bk][1][i];
          res2=afpList.get(str).getOptAln()[bk][0][i];
        }
        equivalencies.get(str).set(res1,res2);
        if (flexible && i == 0)         flexibleBoundaries.add(res1);
      }
    }
  }
  MultipleAlignment seed=new MultipleAlignmentImpl();
  seed.getEnsemble().setAtomArrays(atomArrays);
  BlockSet blockSet=new BlockSetImpl(seed);
  new BlockImpl(blockSet);
  int[] lastResidues=new int[size];
  Arrays.fill(lastResidues,-1);
  for (int pos=0; pos < length; pos++) {
    if (flexibleBoundaries.contains(pos) && blockSet.getBlocks().get(blockSet.getBlocks().size() - 1).getAlignRes() != null) {
      blockSet=new BlockSetImpl(seed);
      new BlockImpl(blockSet);
    }
    boolean cp=false;
    for (int str=0; str < size; str++) {
      if (equivalencies.get(str).get(pos) == null) {
        continue;
      }
 else       if (equivalencies.get(str).get(pos) < lastResidues[str]) {
        cp=true;
        break;
      }
 else       lastResidues[str]=equivalencies.get(str).get(pos);
    }
    if (cp) {
      new BlockImpl(blockSet);
      Arrays.fill(lastResidues,-1);
    }
    for (int str=0; str < size; str++) {
      Block lastB=blockSet.getBlocks().get(blockSet.getBlocks().size() - 1);
      if (lastB.getAlignRes() == null) {
        List<List<Integer>> alnRes=new ArrayList<List<Integer>>(size);
        for (int k=0; k < size; k++) {
          alnRes.add(new ArrayList<Integer>());
        }
        lastB.setAlignRes(alnRes);
      }
      lastB.getAlignRes().get(str).add(equivalencies.get(str).get(pos));
    }
  }
  return seed;
}