Python 在FASTA文件中查找长度为18的回文序列?

Python 在FASTA文件中查找长度为18的回文序列?,python,substring,Python,Substring,我想设计引导RNA,在FASTA文件中找到回文序列。我想编写一个python脚本,在整个序列中查找长度为18的所有回文序列。我心里有一个逻辑,但我不知道如何用Python语言来表达。我的逻辑是: 1)If i is [ATCG] and i+17 is [TAGC] then check: 2)if i+1 is [ATCG] and i+16 is [TAGC] then check: 3)if i+2 is [ATCG] and i+15 is [TAGC] then check&quo

我想设计引导RNA,在FASTA文件中找到回文序列。我想编写一个python脚本,在整个序列中查找长度为18的所有回文序列。我心里有一个逻辑,但我不知道如何用Python语言来表达。我的逻辑是:

1)If i is [ATCG] and i+17 is [TAGC] then check: 
2)if i+1 is [ATCG] and i+16 is [TAGC] then check: 
3)if i+2 is [ATCG] and i+15 is [TAGC] then check"
.
.
.

10)if i+9 is [ATCG] and i+10 is [TAGC] and all the above are true, 
然后将i到i+17的序列识别为回文序列。但我需要确保,对于A for I,它只考虑T for I+17。 知道我如何用python编写这个逻辑吗


谢谢,

所以你想匹配A+T和G+C。我们可以使用字典来匹配。然后我们只需检查对边是否成对

pairs = {"A":"T", "T":"A", "G":"C", "C":"G"}
for i in range(len(sequence) - 18 + 1):
    pal = True
    for j in range(9):
        if pairs[ sequence[i+j] ] != sequence[i+17-j]:
            pal = False
            break
    if pal:
        print(sequence[i : i+18])
对于任意n长度回文(包括奇数n):


谢谢你的剧本。你知道我如何在这些回文中选择在序列中后跟“[ATCG]CC”或“[ATCG]GG”的回文吗?
pairs = {"A":"T", "T":"A", "G":"C", "C":"G"}
n=18
for i in range(len(sequence) - n + 1):
    pal = True
    for j in range(n//2):
        if pairs[ sequence[i+j] ] != sequence[i-j+n-1]:
            pal = False
            break
    if pal:
        print(sequence[i : i+n])