This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Python - Alignment highlighting mismatch characters

Greetings, I have some protein sequences in which I want to align, and highlight the mismatches using python. Here's what I have so far (after creating a list of the AA seqs):

new = []
for seq1 in allkeeps_sorted:
        if len(seq1) != len(VJ):
                xlen = (len(VJ) - len(seq1))*"X"
                print xlen
                newseq = seq1 + str(xlen)
                new.append(newseq)
        else:
                new.append(seq1)

for seq1 in new:
        for x in range(0, len(VJ)):
                if (seq1[x] != VJ[x]):
                        match = colored(seq1[x], "red")
                else:
                        match = colored(seq1[x], "white")
                align.append(match)

print align

In the list "seqs" some of the AA sequences are shorter than the "VJ" sequence. So I attempted to add "X"'s to the end of the sequence to make the strings within the list equal, in order to highlight mismatches. However, this did not work. I'm doing this in a Linux terminal, so I want the mismatched AA's to the VJ sequence to be colored red.

All help is appreciated.

mismatch python colored-text alignment

I should probably stop asking this question: Why reinvent the wheel? Why not use existing MSA software (like Clustal) that can be customized in a multitude of ways?

Also, I'd ideally check if len(seq1)<len(VJ) and not use a !=. Just erring on the side of caution.

2 answers

Software ResCons does this. It is a cross-OS platform software. It can highlight mismatching characters in alignment and show results in any modern browser. Available for download from https://github.com/ManavalanG/ResCons/releases

That's a nice tool - and a smart plug :)

Maybe include a few screenshots on your page?

The code looks good. Are you sure the Terminal is not suppressing the colors?

I know I can run clustal and get a fasta outputted within Python from Bio Python. I just wanted to do it with Python code alone, since it may serve future purposes.

When the line: print align is executed, I get a list containing way too many of these

'\x1b[37mY\x1b[0m', '\x1b[37mN\x1b[0m', '\x1b[37mQ\x1b[0m', '\x1b[37mK\x1b[0m', '\x1b[37mF\x1b[0m', '\x1b[37mK\x1b[0m', '\x1b[31mD\x1b[0m', '\x1b[37mK\x1b[0m', '\x1b[37mA\x1b[0m', '\x1b[37mT\x1b[0m', '\x1b[37mL\x1b[0m', '\x1b[37mT\x1b[0m', '\x1b[31mI\x1b[0m', '\x1b[37mD\x1b[0m', '\x1b[37mK\x1b[0m', '\x1b[37mS\x1b[0m', '\x1b[37mS\x1b[0m', '\x1b[31mC\x1b[0m', '\x1b[37mT\x1b[0m', '\x1b[37mA\x1b[0m', '\x1b[37mH\x1b[0m', '\x1b[37mM\x1b[0m', '\x1b[37mE\x1b[0m', '\x1b[37mL\x1b[0m', '\x1b[37mR\x1b[0m', '\x1b[37mS\x1b[0m', '\x1b[37mL\x1b[0m', '\x1b[37mT\x1b[0m', '\x1b[37mS\x1b[0m', '\x1b[37mE\x1b[0m', '\x1b[37mD\x1b[0m',

So I'm not even getting the list in white. (way too many = more items than in seqs list). I tested the termcolor module out on the python shell. It does work.

Python is encountering some characters that it's unable to handle. That would be my most educated guess. Does the sequence go "YNQK...."?

They are all normal AA seqs..mostly caps, some lowercase chars.

Check the header line, maybe?

There are no heads in the input file, no fasta. just a list of seqs.

Which module is the "colored" method located in?

So, I checked. The colored() method of termcolor seems to output those weird characters. Lemme try and solve it.

The termcolor package is crap. Doesn't work at all. :(

The \x1b maps to a right arrow character. It's non printable, I guess, so that's causing a problem.

It appears to be ANSI code

Log in to answer this question.