Skip to content

Instantly share code, notes, and snippets.

@jwintersinger
Created October 3, 2012 23:41
Show Gist options
  • Save jwintersinger/3830596 to your computer and use it in GitHub Desktop.
Save jwintersinger/3830596 to your computer and use it in GitHub Desktop.
MDSC 408 multiple sequence alignment colourer
def main():
lines = open('alignment.aln').readlines()
lines = [line for line in lines if len(line.strip()) > 0]
header = lines[0]
del lines[0]
num_lines = 3
for k in range(0, len(lines), num_lines + 1):
block_lines = lines[k:(k + num_lines)]
classes = lines[k + num_lines]
if len(block_lines) != num_lines:
print block_lines
raise Exception('Wrong number of lines')
if not (len(block_lines[0]) == len(block_lines[1]) == len(block_lines[2])):
raise Exception('Lines are not same length')
colours = {
'same_0_1': '#8fb9ff',
'same_0_2': '#ff8f8f',
'same_1_2': '#8dfba4',
'same_0_1_2': '#f8ff2b'
}
coloured = []
seq = []
for i in range(len(block_lines)):
line = block_lines[i].strip()
#components = re.search(r'^([^\s]+[\s]+)([^\s+])([\s]+[\d]+$', line)
block_lines[i] = block_lines[i].strip().split()[1]
coloured.append('')
for i in range(len(block_lines[0])):
if aa_equal(block_lines[0][i], block_lines[1][i]) and aa_equal(block_lines[1][i], block_lines[2][i]):
matching = (0, 1, 2)
elif aa_equal(block_lines[0][i], block_lines[1][i]):
matching = (0, 1)
elif aa_equal(block_lines[0][i], block_lines[2][i]):
matching = (0, 2)
elif aa_equal(block_lines[1][i], block_lines[2][i]):
matching = (1, 2)
else:
matching = ()
colour = 'same_%s' % '_'.join([str(m) for m in matching])
for j in range(len(coloured)):
if j in matching:
new_char = apply_colour(block_lines[j][i], colours[colour])
else:
new_char = block_lines[j][i]
coloured[j] += new_char
print '\n'.join(coloured)
#print classes
print ''
def apply_colour(char, colour):
return '<span style="background-color: %s">%s</span>' % (colour, char)
def aa_equal(a, b):
return a == b and a != '-'
print '<pre>'
main()
print '</pre>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment