python - 如何使用 Python REGEX 重新获得模式中的特定术语

标签 python regex for-loop

我的模式是

Forward primer
CGAAGCCTGGGGTGCCCGCGATTT Plus 24 1 24 71.81 66.67 4.00 2.00 

Reverse primer
AAATCGGTCCCATCACCTTCTTAT Minus 24 420 397 59.83 41.67 5.00 2.00 

Product length
420 


Products on potentially unintended templates


>CP049108.1 Mycobacterium tuberculosis strain 5005 chromosome, complete genome product length = 495
Forward primer  1        CGAAGCCTGGGGTGCCCGCGATTT  24
Template        1930054  ........................  1930077

Reverse primer  1        AAATCGGTCCCATCACCTTCTTAT  24
Template        1930548  ........................  1930525


product length = 2946
Forward primer  1        CGAAGCCTGGGGTGCCCGCGATTT  24
Template        1927603  .......C....C..T..T...G.  1927626

Reverse primer  1        AAATCGGTCCCATCACCTTCTTAT  24
Template        1930548  ........................  1930525


>CP046728.2 Mycobacterium tuberculosis strain TCDC11 chromosome, complete genome product length = 420
Forward primer  1        CGAAGCCTGGGGTGCCCGCGATTT  24
Template        2150761  ........................  2150784

Reverse primer  1        AAATCGGTCCCATCACCTTCTTAT  24
Template        2151180  ........................  2151157


product length = 2595
Forward primer  1        CGAAGCCTGGGGTGCCCGCGATTT  24
Template        2148586  .......C....C..T..T...G.  2148609

Reverse primer  1        AAATCGGTCCCATCACCTTCTTAT  24
Template        2151180  ........................  2151157


>CP047258.1 Mycobacterium tuberculosis strain TCDC3 chromosome product length = 345
Forward primer  1        CGAAGCCTGGGGTGCCCGCGATTT  24
Template        2166300  ........................  2166323

Reverse primer  1        AAATCGGTCCCATCACCTTCTTAT  24
Template        2166644  ........................  2166621

我需要的是

>CP049108.1 = 495   1930054 1930548 
>CP046728.2 = 420   2150761 2151180
>CP047258.1 = 345   2166300 2166644

我是微生物学家和 Python 初学者。我试过了

import re
file = open(r"C:\\Users\\Lab\\Desktop\\amplicons\\ETRA", "r")
handle = file.read()
file.close()

pattern1 = re.compile(r'>.{5,10}\.\d')
matches1 = pattern1.finditer(handle)

for match1 in matches1:
    print(match1.group(0))

但我也需要在我的入藏号之后添加特定术语(例如,入藏号为 >CP049108.1)。我也会将您的知识应用到我的其他工作中。

感谢您的帮助并 预先感谢您

最佳答案

这是我想出的 - >([\w\d]*?\.\d*?) .+= (\d+)\n.+\n.*?(\d+) .+\n{2}.+\n.*?(\d+)

让我们看一个只有一组数据的示例,只要将 global 标志设置为 True,您就可以输入粘贴的整个数据,并且仍然可以获得结果> (在 python 中默认设置为 True)

>CP049108.1 Mycobacterium tuberculosis strain 5005 chromosome, complete genome product length = 495 Forward primer 1
CGAAGCCTGGGGTGCCCGCGATTT 24 Template 1930054 ........................ 1930077

Reverse primer 1 AAATCGGTCCCATCACCTTCTTAT 24 Template
1930548 ........................ 1930525

第一组将是 - CP049108.1

第二组将是 - 495

第三组将是 - 1930054

第四组(也是最后一组)将是 - 1930548

当然,现在您可以将整个数据重组为您想要的样子,如果您从文本文件中读取数据,则可以使用以下代码片段 -

import re

with open('test.txt', 'r') as file:
    content = file.read()

pattern = re.compile(r'>([\w\d]*?\.\d*?) .+= (\d+)\n.+\n.*?(\d+).+\n{2}.+\n.*?(\d+)')

for match in pattern.finditer(content):
    output = '>{} = {} {} {}'.format(match.group(1), match.group(2), match.group(3), match.group(4))
    print(output)

如果我准确输入您提供给 test.txt 的数据集,我会得到以下输出 -

>CP049108.1 = 495   1930054 1930548
>CP046728.2 = 420   2150761 2151180
>CP047258.1 = 345   2166300 2166644

正则表达式解释

>(\w+\.\d+) .+= (\d+)\n.+\n.*?(\d+).+\n{2}.+\n.*?(\d+)

  • 我们先分析第一行 - >(\w+\.\d+) .+= (\d+)\n

    首先匹配 CP049108,停止直到找到 .(点),然后匹配下一个数字,在本例中为 - 1,停止,直到到达 =。然后,它会将这些组合起来,以在单个捕获组中获取 CP049108.1

    稍后它将抓取 = 之后的数字并转到下一行,在本例中为 495

  • 第二行时间 - .+\n

    是的,第二行被忽略

  • 现在,第三行 - .*?(\d+).+\n{2}

    它会忽略所有内容,直到到达第一组数字,捕获这些数字并跳到下一行(2 个新行)。在本例中,结果为 1930054

  • 现在,第四行 - .+\n

    这也被忽略

  • 最后一行 - .*?(\d+)

    这与第三行完全相同,结果是1930548

查看 demo !

关于python - 如何使用 Python REGEX 重新获得模式中的特定术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60557693/

相关文章:

algorithm - 使用最少的 for_loop 计算条件概率,Matlab

python - Django REST框架: Two models nested via a third one (with 2 FKs )

python - 如何知道python中2个列表中相同索引的值是正数还是负数?

python - 正则表达式仅匹配从左到右阅读的最后一次出现

javascript - 使用正则表达式在JS中进行密码验证

Python 自引用 for 循环

python - 如何更改 Python 中的方法默认值?

python - 将 csv 文件路径数组与指定项目名称数组进行比较的逻辑方法

ruby - 使用\d 扫描字符串中的 Unicode 数字

javascript for循环,如果增量值不匹配则嵌套?