Supposing your sequences are the same length and have no empty spaces (no '--') as in your example, you can use the following (explained) script.
# Start of script
from collections import Counter
from glob import glob
import numpy as np
from Bio import AlignIO
Along with module AlignIO of Bio (biopython) you'll use:
numpy: a widely used python module for numerical computing.
glob and collections: two standard modules (no need to install)
sequences = glob('/path/to/sequences/*')
Put all your sequences (fasta files) in a directory here called 'sequences' and replace /path/to/sequences with the real directory.
for sequence in sequences:
print(sequence)
alignment = AlignIO.read(sequence, 'fasta')
align_array = np.array([list(rec) for rec in alignment], np.character)
single = {}
poly = {}
For each fasta file, the script prints the name of the file and creates an alignment (using numpy array).
for column in range(alignment.get_alignment_length()):
counter = Counter(align_array[:,column])
main_letter = counter.most_common(1)[0][0]
For each column main_letter is the most frequent letter, if several letter have the same frequency it will be one of them.
if len(counter) == 2:
# single polymorphism
letter = counter.most_common(2)[1][0]
for pos in np.where(align_array[:,column] == letter)[0]:
change = main_letter + str(column + 1) + letter
single.setdefault(change, [])
single[change].append('sp' + str(pos + 1))
If there is only two different letters in that position means that there is a single polyorphism in the position.
elif len(counter) > 2:
# Non-single polymorphism
for letter in counter:
if letter != main_letter:
for pos in np.where(align_array[:,column] == letter)[0]:
change = main_letter + str(column + 1) + letter
poly.setdefault(change, [])
poly[change].append('sp' + str(pos + 1))
If more than two differents letters in the column, then there is a non-single polymorphism.
print(" " + "Single polymorphisms:")
for change in single:
print(" " + change + ": " + str(single[change])[1:-1])
print(" " + "Non-single polymorphisms:")
for change in poly:
print(" " + change + ": " + str(poly[change])[1:-1])
# End of script
Run this script as python script.py >> output.txt to have the output printed in a file called output.txt.