python – How to get string matching position while matching two string

I would like to get the matching position of two strings like illustrated in the example below :

sequence = "MTGLKILYH"
alignment = "GPKI---LYH"
pos = get_alignment_position(sequence,alignment)
print(pos)
[-2,-1,0,1,2,3,7,8,9]

As you may see, the two strings can be a little different (GL instead of GP), but I still want to keep this, and the beginning of the string can be different as well so the indexing should be based on alignment.

I already have an almost working algorithm based on the difflib package, but I would like to know if there is already an existing method?
I have a lot of unusual cases where it is not working, and I have to deal with all of these ‘special cases’ and it can take a lot of trials… So if a Magick package exists already… I take it =D

Thank you for your help!

Archive from: https://stackoverflow.com/questions/59051170/python-how-to-get-string-matching-position-while-matching-two-string

Share

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *