How to find a pattern that spans vertical lines?

Viewed 123
N
|
T

Is there a way to find the pattern above in the input below? It's a vertical pattern that spans across several lines of text.

5' TCAGATGTGTATAAGAGACAGTGCGTATTCTCAGTCAGTTGAAGTGNTACAGAA
            ::     :::       ::        : :     : ||||| :          
3'          ATTCAGCCTGCACTCGTTACCGAGGCATGACAGAGAATATGTGTAGAGGCGAGCTAAGGTACTTGAAAGGGTGTATTAGAG

I tried multiple ways but I am not sure whether it is possible or not. This did not work:

if 'N\n|\nT' in input:
    print('found')

Also, what if T could be any of the letters A, T, C, or G?

2 Answers

Split the string (DNA sequences and base-pairing pattern, I am guessing) into a list of 3 strings, and iterate along all 3 strings using the index:

import re

in_str = '''
5' TCAGATGTGTATAAGAGACAGTGCGTATTCTCAGTCAGTTGAAGTGNTACAGAA
            ::     :::       ::        : :     : ||||| :          
3'          ATTCAGCCTGCACTCGTTACCGAGGCATGACAGAGAATATGTGTAGAGGCGAGCTAAGGTACTTGAAAGGGTGTATTAGAG'''

in_str = re.sub(r'^\s*', "", in_str)
lst = re.split(r'\n', in_str)

acgt = set(['A', 'C', 'G', 'T'])
for idx in range(min([len(s) for s in lst])):
    if lst[0][idx] == 'N' and lst[1][idx] == '|' and lst[2][idx] in acgt:
        print('found!')
        break

To find whether the pattern occurs within, say, the last 10 nucleotides of the first strand, iterate across the corresponding range:

len0 = len(lst[0])
offset = 10
for idx in range(len0 - offset, len0):
    if lst[0][idx] == 'N' and lst[1][idx] == '|' and lst[2][idx] in acgt:
        print('found!')
        break

Try this:

line = "5' TCAGATGTGTATAAGAGACAGTGCGTATTCTCAGTCAGTTGAAGTGNTACAGAA\n            ::     :::       ::        : :     : ||||| :          \n3'          ATTCAGCCTGCACTCGTTACCGAGGCATGACAGAGAATATGTGTAGAGGCGAGCTAAGGTACTTGAAAGGGTGTATTAGAG"

new = line.split('\n')

pos_N = new[0].find('N')

N = 'N'
l = '|'
T = 'T'

for i in range(len(new[0])):
    if (new[0][i] == N):
        if l in new[1][i]:
            if T in new[2][i]:
                print('found')
Related