This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Biopython pairwise 2 - can I make it score an existing alignment?

From Biopython, I can use pairwise2 to align two sequences and provide me with the score. Is there a way to get pairwise2 to NOT align, but to score an existing alignment? Essentially I'm looking for a way to do sum-of-pairs with a custom weight matrix (e.g. match = 1, indeed = -3, mismatch = -1). It seems like pairwise2 has a score-only function but I can't for the life of me figure out how to implement it....

biopython python pairwise2 alignment score

1 answer

No you can't do this with Biopython's pairwise2. The score_only parameter will still cause pairwise2.align to calculate an alignment from two sequences, it will just not return the alignment but only the maximium score (that it has calculated).

I have written a small function which calculates the score of a given alignment (supplied as the two aligned sequences), that may be useful for you:


def evaluate_score(seq1, seq2, match, mismatch, open_gap, extend_gap,
                   penalize_extend_when_opening=False, force_generic=False,
                   penalize_end_gaps=True):
    openA = False
    openB = False
    if penalize_extend_when_opening:
        open_gap += extend_gap
    score = 0
    for baseA, baseB in zip(seq1, seq2):
        if baseA !='-' and baseB != '-':
            openA, openB = False, False
            if baseA == baseB:
                score += match
            else:
                score += mismatch
        elif baseA == '-':
            if not openA:
                score += open_gap
                openA = True
                openB = False
            else:
                score += extend_gap
        elif baseB == '-':
            if not openB:
                score += open_gap
                openB = True
                openA = False
            else:
                score += extend_gap
    return score * 1.0

The gap symbol must be -, match, mismatch, open_gap, extend_gap are the respective scores; penalties are given as negative scores.

Log in to answer this question.