简体   繁体   中英

Python Sequence Comparison

I'm trying to compare two sequences:

>>> seq_a = "tgaaactcaagccttaccgcagagacagaagaaacagcaaactgtgactcttcttcctgctgcagatttggatgatttctccaaacaattgcaacaatccatgagcagtgctgactcaactcaggcctaaactcatgcagaccacacaaggcagatgggctatataaacgttttcgcttttccgtttacgatatatagtctactcttgtgcagaatgaattctcgtaactacatagcacaagtagatgtagttaactttaatctcacatagcaatctttaatcagtgtgtaacattagggaggacttgaaagagccaccacattttcaccgaggccacgcggagtacgatcgagtgtacagtgaacaatgctagggagagctgcctatatggaagagccctaatgtgtaaaattaattttagtagtgctatccccatgtgattttaatagcttcttaggagaatgacaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
>>> seq_b = "agagcctaaaaaggacaaaaagaagaaggctgatgaaactcaagccttaccgcagagacagaagaaacagcaaactgtgactcttcttcctgctgcagatttggatgatttctccaaacaattgcaacaatccatgagcagtgctgactcaactcaggcctaaactcatgcagaccacacaaggcagatgggctatataaacgttttcgcttttccgtttacgatatatagtctactcttgtgcagaatgaattctcgtaactacatagcacaagtagatgtagttaactttaatctcacatagcaatctttaatcagtgtgtaacattagggaggacttgaaagagccaccacattttcaccgaggccacgcggagtacgatcgagtgtacagtgaacaatgctagggagagctgcctatatggaagagccctaatgtgtaaaattaattttagtagtgctatccccatgtgattttaatagcttcttaggagaatgac"

but the code I'm using is getting stuck on the end sequence_comparison. This code was found online and I'm having real difficulty in trying to find it's problem. The syntax error being provided highlights the s at the beginning of the last piece of code:

def sequence_compare(seq_a, seq_b):
    len1 = len(seq_a)
    len2 = len(seq_b)
    mismatches = []
    for pos in range (0,min(len1,len2)):
        if seq_a[pos] != seq_b[pos]:
            mismatches.append('|')
        else:
            mismatches.append(' ')
    print(seq_a)
    print(mismatches)
    print(seq_b)
sequence_compare(seq_a,seq_b)

can anyone help me find the issue with this?

It looks like the problem was that mismatches was being displayed as a list, not a string. I've converted it to a string:

Updated code

def sequence_compare(seq_a, seq_b):
    len1 = len(seq_a)
    len2 = len(seq_b)
    mismatches = ""
    for pos in range (0,min(len1,len2)):
        if seq_a[pos] != seq_b[pos]:
            mismatches += '|'
        else:
            mismatches += ' '
    print(seq_a)
    print(mismatches)
    print(seq_b)

seq_a = "tgaaactcaagccttaccgcagagacagaagaaacagcaaactgtgactcttcttcctgctgcagatttggatgatt"
seq_b = "agagcctaaaaaggacaaaaagaagaaggctgatgaaactcaagccttaccgcagagacagaagaaacagcaaactg"

sequence_compare(seq_a,seq_b)

Output

tgaaactcaagccttaccgcagagacagaagaaacagcaaactgtgactcttcttcctgctgcagatttggatgatt
|  ||  |  ||||||||||   |||  |||| || ||||||| ||||| || |||||||||||| ||| | ||| |
agagcctaaaaaggacaaaaagaagaaggctgatgaaactcaagccttaccgcagagacagaagaaacagcaaactg

In python adding indentation is must, If you don't add indentation at certain place, Your code won't work; Here after declaring the function, You need to add an indentation after declaring the function.

def sequence_compare(seq_a, seq_b):
    len1 = len(seq_a)
    len2 = len(seq_b)
    mismatches = []
    for pos in range (0,min(len1,len2)):
        if seq_a[pos] != seq_b[pos]:
            mismatches.append('|')
        else:
            mismatches.append(' ')
    print(seq_a)
    print(mismatches)
    print(seq_b)

This code should work as I have added indentation wherever it was needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM