<p>The tricky part about your request is the size of the alphabet.  If you are happy with alphabets of up to about 250 &quot;characters&quot;, then the programs in the FASTA package (github.com/wrpearson/fasta36) can be modified to do what you need.  The are designed to work with unsigned char strings, and could work with matrices of up to 250 x 250 or so.</p>
<p>There is a non-vectorized version of Smith-Waterman that can calculate optimal local alignments with alphabets extended to 250 characters (with the appropriate scoring matrix).</p>
<p>However, I suspect that pairwise could do the same thing (with modifications), again using an expanded scoring matrix.  The affine-gap Smith-Waterman algorithm is pretty simple, the trick is to get your multi-digit &quot;characters&quot; into a single byte (or unsigned char).</p>

CC BY-SA 4.0
2022-06-20T23:42:40.217Z
2022-06-20T23:42:40.217Z
Bill Pearson
<p>To parse data into <code>pairwise2</code> so you can follow the SO post <a href="https://bioinformatics.stackexchange.com/questions/19261/how-to-align-integer-non-dna-protein-sequences">here</a> fairly easy by simply converting your output values into a higher base.</p>
<pre><code>from numpy import base_repr
base36 = []
for count in range(1,20):
    base36.append( base_repr(count, 36))
myseq36i = str(''.join(base36))
</code></pre>
<p><em>Output</em></p>
<blockquote>
<p>123456789ABCDEFGHIJ</p>
</blockquote>
<p>This represents 1 to 20 as single digits (of course there's 36 single digit integers) and then implement the pairwise2 solution. The advantage of this solution is you're not stuck on 20 amino acids. <code>str</code> may not be needed.</p>
<p>The custom matrix described in that post you would supply can be based around the exact base 10 differences under the scoring matrix - to avoid subtracting in the higher base.</p>
<hr />
<p>Please do upvote the answer on SO as it is relevant to your problem. The first solution they presented is cool and am sure there's broader application.</p>

CC BY-SA 4.0
2022-06-17T22:28:54.540Z
2022-06-18T01:03:25.100Z
2022-06-18T01:03:25.100Z
M__
<p>I am looking for an algorithm to find the &quot;best&quot; alignment between two sequences of integers similar to how one aligns nucleic acids or amino acids for homology comparisons. For example, the best alignment for the two sequences below is:</p>
<pre><code>                10 06 13 91 22 16 06
                |  |  |  |  |  |  |
       01 01 02 10 04 11 92 22 17 05 
</code></pre>
<p>or an example with gaps:</p>
<pre><code>                10 _  _  91 22 16 06 22 12
                |  |  |  |  |  |  |
          11 02 10 04 11 92 22 17 05 
</code></pre>
<p>The alignment would be based on the distance between each number similar to aligning amino acid sequences where one would prefer to align a hydrophobic amino acid with another hydrophobic amino acid because they are more &quot;similar&quot;. I hope this explanation makes sense.</p>
<p>Another way to think about this problem is if you took the 20 amino acids and replaced them with a number from 1-20 instead of their single letter abbreviation and then tried to align 2 sequenced based on how close the numbers where, how would someone align them? In this example the alignment is based on how close two integers are from one another whereas when aligning amino acids the alignment is based on the chemical similarity of the amino acids, ie leucine is very similar to isoleucine whereas valine is very dissimilar to tryptophan.</p>
<p>I've been experimenting with using the biopython pairwise library and trying to utilize it for my problem. There is a similar approach to what I am looking for here:</p>
<p><a href="https://stackoverflow.com/a/45512865/1255817">https://stackoverflow.com/a/45512865/1255817</a></p>
<p>This solution solves my problem, but only for the single character digits [0-9] and my numbers are more than one character.</p>
<p>Does anyone know of an approach (algorithm, package, library) to align sequences of integers? Ideally, I am looking for something in Python with a minimal working example.</p>

<phylogenetics><phylogeny>
How to align integer (non-DNA/protein) sequences?
# How to align integer (non-DNA/protein) sequences?

(Asked by: username_0 on 2022-06-17)

I am looking for an algorithm to find the "best" alignment between two sequences of integers similar to how one aligns nucleic acids or amino acids for homology comparisons. For example, the best alignment for the two sequences below is:
                10 06 13 91 22 16 06
                |  |  |  |  |  |  |
       01 01 02 10 04 11 92 22 17 05 

or an example with gaps:
                10 _  _  91 22 16 06 22 12
                |  |  |  |  |  |  |
          11 02 10 04 11 92 22 17 05 

The alignment would be based on the distance between each number similar to aligning amino acid sequences where one would prefer to align a hydrophobic amino acid with another hydrophobic amino acid because they are more "similar". I hope this explanation makes sense.
Another way to think about this problem is if you took the 20 amino acids and replaced them with a number from 1-20 instead of their single letter abbreviation and then tried to align 2 sequenced based on how close the numbers where, how would someone align them? In this example the alignment is based on how close two integers are from one another whereas when aligning amino acids the alignment is based on the chemical similarity of the amino acids, ie leucine is very similar to isoleucine whereas valine is very dissimilar to tryptophan.
I've been experimenting with using the biopython pairwise library and trying to utilize it for my problem. There is a similar approach to what I am looking for here:
https://stackoverflow.com/a/45512865/1255817
This solution solves my problem, but only for the single character digits [0-9] and my numbers are more than one character.
Does anyone know of an approach (algorithm, package, library) to align sequences of integers? Ideally, I am looking for something in Python with a minimal working example.

**Tags:** phylogenetics, phylogeny

---

## 2 answers

### Answer by username_1 on 2022-06-20. Score: 4

The tricky part about your request is the size of the alphabet. If you are happy with alphabets of up to about 250 "characters", then the programs in the FASTA package (github.com/wrpearson/fasta36) can be modified to do what you need. The are designed to work with unsigned char strings, and could work with matrices of up to 250 x 250 or so.

There is a non-vectorized version of Smith-Waterman that can calculate optimal local alignments with alphabets extended to 250 characters (with the appropriate scoring matrix).

However, I suspect that pairwise could do the same thing (with modifications), again using an expanded scoring matrix. The affine-gap Smith-Waterman algorithm is pretty simple, the trick is to get your multi-digit "characters" into a single byte (or unsigned char).


---

### Answer by username_2 on 2022-06-17. Score: 3

To parse data into pairwise2 so you can follow the SO post here fairly easy by simply converting your output values into a higher base.
from numpy import base_repr
base36 = []
for count in range(1,20):
    base36.append( base_repr(count, 36))
myseq36i = str(''.join(base36))

Output

123456789ABCDEFGHIJ

This represents 1 to 20 as single digits (of course there's 36 single digit integers) and then implement the pairwise2 solution. The advantage of this solution is you're not stuck on 20 amino acids. str may not be needed.
The custom matrix described in that post you would supply can be based around the exact base 10 differences under the scoring matrix - to avoid subtracting in the higher base.

Please do upvote the answer on SO as it is relevant to your problem. The first solution they presented is cool and am sure there's broader application.


---

