In a project we're doing (in python), we want to create a distance matrix for a phylogenetic tree with multiple, different length sequences. However, what we've seen so far (AlignIO in biopython, etc.) requires the sequences' length to be equal. Is there any pythonic way to get the matrix with sequences of different length? Or any other indirect way that would get us to the same result?

  • $\begingroup$ How do you define the distance? (Sorry, cannot post comments -- moderators, please help) $\endgroup$
    – user9413
    Commented Sep 20, 2014 at 17:09
  • $\begingroup$ Without seeing your code it's kind of hard to help. This question is much more suited for Stack Overflow, please ask it over there and tag it [python] and [biopython]. $\endgroup$
    – MattDMo
    Commented Sep 21, 2014 at 3:04
  • $\begingroup$ Use a metric that is a function of the sequence but not the sequence itself; something like GC content. Or use end-to-end alignment such as Needleman-Wunsch algorithm $\endgroup$
    Commented Sep 21, 2014 at 6:03
  • 1
    $\begingroup$ I am not a python user but it seems to me AlignIO is (similar to the bioperl counterpart) for reading/writing an already existing alignment (hence the requirement of equal sequence lengths). You could align the sequences with an external tool (like e.g. MAFFT) and then read the alignment in python with the AlignIO interface. $\endgroup$
    – ddiez
    Commented Sep 21, 2014 at 12:49

1 Answer 1


There are two possibilities:

  1. Do not use the sequences per se but a scalar metric of sequence similarity for distance values
  2. Introduce gaps in the alignment. You can also do an end to end gapped alignment such as the Needleman-Wunsch and use the alignment scores for the distance matrix.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .