diff --git a/prody/proteins/compare.py b/prody/proteins/compare.py index 752892e19..a287ef921 100644 --- a/prody/proteins/compare.py +++ b/prody/proteins/compare.py @@ -1387,10 +1387,25 @@ def _findAlignment(sequence, alignment): a = this[i] b = that[i] if a not in gap_chars: - ares = next(aiter) + try: + ares = next(aiter) + except StopIteration: + # Alignment string is longer than the actual sequence + # This can happen with different Biopython versions + LOGGER.warning('Alignment string longer than target sequence for {0}, ' + 'truncating alignment at position {1}'.format(target.getTitle(), i)) + break amatch.append(ares.getResidue()) if b not in gap_chars: - bres = next(biter) + try: + bres = next(biter) + except StopIteration: + # Chain sequence exhausted, need to remove the last target residue + # to keep amatch and bmatch synchronized + amatch.pop() + LOGGER.warning('Alignment string longer than chain sequence for {0}, ' + 'truncating alignment at position {1}'.format(chain.getTitle(), i)) + break bmatch.append(bres.getResidue()) if a == b: n_match += 1 @@ -1398,7 +1413,13 @@ def _findAlignment(sequence, alignment): else: bmatch.append(None) elif b not in gap_chars: - bres = next(biter) + try: + bres = next(biter) + except StopIteration: + # Chain sequence exhausted + LOGGER.warning('Alignment string longer than chain sequence for {0}, ' + 'truncating alignment at position {1}'.format(chain.getTitle(), i)) + break return amatch, bmatch, n_match, n_mapped def getCEAlignMapping(target, chain):