Program: 16994
Project: 16994
Buggy source:
/** 
 * Takes a structure and sequence corresponding to an alignment between a structure or sequence and itself (or even a structure with a sequence), where the result has a circular permutation site  {@link cpSite} residues to the right.
 * @param cpSite The number of residues from the beginning of the sequence at which the circular permutation site occurs; can be positive or negative; values greater than the length of the sequence are acceptable
 */
public static AFPChain cpFastaToAfpChain(ProteinSequence sequence,ProteinSequence second,Structure structure,int cpSite) throws StructureException {
  if (structure == null) {
    throw new IllegalArgumentException("String_Node_Str");
  }
  if (sequence == null) {
    throw new IllegalArgumentException("String_Node_Str");
  }
  int j=0;
  int ungappedCpShift=0;
  while (ungappedCpShift < Math.abs(cpSite)) {
    char c;
    if (cpSite > 0) {
      c=sequence.getSequenceAsString().charAt(j);
    }
 else {
      c=sequence.getSequenceAsString().charAt(sequence.getLength() - 1 - j);
    }
    if (c != '-') {
      ungappedCpShift++;
    }
    j++;
  }
  Atom[] ca1=StructureTools.getAtomCAArray(structure);
  Atom[] ca2=StructureTools.getAtomCAArray(structure);
  ProteinSequence antipermuted=new ProteinSequence(SequenceTools.permuteCyclic(second.getSequenceAsString(),ungappedCpShift));
  ResidueNumber[] residues=StructureSequenceMatcher.matchSequenceToStructure(sequence,structure);
  ResidueNumber[] antipermutedResidues=StructureSequenceMatcher.matchSequenceToStructure(antipermuted,structure);
  ResidueNumber[] nonpermutedResidues=new ResidueNumber[antipermutedResidues.length];
  SequenceTools.permuteCyclic(antipermutedResidues,nonpermutedResidues,-ungappedCpShift);
  if (sequence.getUserCollection() != null) {
    CasePreservingProteinSequenceCreator.setLowercaseToNull(sequence,residues);
  }
  if (second.getUserCollection() != null) {
    CasePreservingProteinSequenceCreator.setLowercaseToNull(second,nonpermutedResidues);
  }
  return buildAlignment(ca1,ca2,residues,nonpermutedResidues);
}

Fixed version (ground truth):
/** 
 * Takes a structure and sequence corresponding to an alignment between a structure or sequence and itself (or even a structure with a sequence), where the result has a circular permutation site  {@link cpSite} residues to the right.
 * @param cpSite The number of residues from the beginning of the sequence at which the circular permutation site occurs; can be positive or negative; values greater than the length of the sequence are acceptable
 */
public static AFPChain cpFastaToAfpChain(ProteinSequence sequence,ProteinSequence second,Structure structure,int cpSite) throws StructureException {
  if (structure == null) {
    throw new IllegalArgumentException("String_Node_Str");
  }
  if (sequence == null) {
    throw new IllegalArgumentException("String_Node_Str");
  }
  int gappedCpShift=0;
  int ungappedCpShift=0;
  while (ungappedCpShift < Math.abs(cpSite)) {
    char c;
    try {
      if (cpSite > 0) {
        c=sequence.getSequenceAsString().charAt(gappedCpShift);
      }
 else {
        c=sequence.getSequenceAsString().charAt(sequence.getLength() - 1 - gappedCpShift);
      }
    }
 catch (    StringIndexOutOfBoundsException e) {
      throw new IllegalArgumentException("String_Node_Str" + cpSite + "String_Node_Str");
    }
    if (c != '-') {
      ungappedCpShift++;
    }
    gappedCpShift++;
  }
  Atom[] ca1=StructureTools.getAtomCAArray(structure);
  Atom[] ca2=StructureTools.getAtomCAArray(structure);
  ProteinSequence antipermuted=new ProteinSequence(SequenceTools.permuteCyclic(second.getSequenceAsString(),gappedCpShift));
  ResidueNumber[] residues=StructureSequenceMatcher.matchSequenceToStructure(sequence,structure);
  ResidueNumber[] antipermutedResidues=StructureSequenceMatcher.matchSequenceToStructure(antipermuted,structure);
  ResidueNumber[] nonpermutedResidues=new ResidueNumber[antipermutedResidues.length];
  SequenceTools.permuteCyclic(antipermutedResidues,nonpermutedResidues,-gappedCpShift);
  if (sequence.getUserCollection() != null) {
    CasePreservingProteinSequenceCreator.setLowercaseToNull(sequence,residues);
  }
  if (second.getUserCollection() != null) {
    CasePreservingProteinSequenceCreator.setLowercaseToNull(second,nonpermutedResidues);
  }
  return buildAlignment(ca1,ca2,residues,nonpermutedResidues);
}