How to use enumerate in a list comprehension with two lists?

I just started to use list comprehension and I'm struggling with it. In this case, I need to get the n number of each list (sequence_0 and sequence_1) that the iteration is at each time. How can I do that?

The idea is to get the longest sequence of equal nucleotides (a motif) between the two sequences. Once a pair is finded, the program should continue in the nexts nucleotides of the sequences, checking if they are also equal and then elonganting the motif with it. The final output should be an list of all the motifs finded. The problem is, to continue in the next nucleotides once a pair is finded, i need the position of the pair in both sequences to the program continue. The index function does not work in this case, and that's why i need the enumerate.

Also, I don't understand exactly the reason for the x and y between (), it would be good to understand that too :)

just to explain, the content of the lists is DNA sequences, so its basically something like:

sequence_1 = ['A', 'T', 'C', 'A', 'C']
def find_shared_motif(arq):
    data = fastaread(arq)
    seqs = [list(sequence) for sequence in data.values()]
    motifs = [[]]
    i = 0
    sequence_0, sequence_1 = seqs[0], seqs[1]  # just to simplify

    for x, y in [(x, y) for x in zip(sequence_0[::], sequence_0[1::]) for y in zip(sequence_1[::], sequence_1[1::])]:
        print(f'Pairs {"".join(x)} and {"".join(y)} being analyzed...')
        if x == y:
            print(f'Pairs {"".join(x)} and {"".join(y)} match!')
            motifs[i].append(x[0]), motifs[i].append(x[1])
            k = sequence_0.index(x[0]) + 2  # NAO ESTA DEVOLVENDO O NUMERO CERTO
            u = sequence_1.index(y[0]) + 2
            print(k, u)

            # Determines if the rest of the sequence is compatible
            print(f'Starting to elongate the motif {x}...')
            for j, m in enumerate(sequence_1[u::]):
                try:
                    # Checks if the nucleotide is equal for both of the sequences
                    print(f'Analyzing the pair {sequence_0[k + j]}, {m}')
                    if m == sequence_0[k + j]:
                        motifs[i].append(m)
                        print(f'The pair {sequence_0[k + j]}, {m} is equal!')

                    # Stop in the first nonequal residue
                    else:
                        print(f'The pair {sequence_0[k + j]}, {m} is not equal.')
                        break

                except IndexError:
                    print('IndexError, end of the string')

        else:
            i += 1
            motifs.append([])

        return motifs
...


from Recent Questions - Stack Overflow https://ift.tt/3Fo9IDT
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)