Skip to content

Instantly share code, notes, and snippets.

@dwinter
Created June 15, 2021 01:04
Show Gist options
  • Save dwinter/d1630b8f182e03db595da4ec2501a722 to your computer and use it in GitHub Desktop.
Save dwinter/d1630b8f182e03db595da4ec2501a722 to your computer and use it in GitHub Desktop.
def base_consensus(alignment, base_index):
""" """
# get the bases at this position, store in case we want to come back
# and assign the first one as th winner
#n_seqs = len(alignment)
bases = [r[base_index].upper() for r in alignment]
base_c = Counter(bases)
max_count = max(base_c.values())
most_common_base = [b for b in base_c.items() if b[1] == max_count]
if len(most_common_base) == 1:
return(most_common_base[0][0])
tied_bases = [base for base,count in most_common_base]
#this is where the logic of choosing the best base goes
return(tied_bases[0])
def make_consensus(alignment):
new_string = ""
for base_index in range(alignment.get_alignment_length()):
new_string += base_consensus(alignment, base_index)
return("".join(new_string))
#alignment = AlignIO.read("coi_ali.fna", "fasta")
#make_consensus(alignment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment