用于分析序列ID的正则表达式

2024-06-07 10:09:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用正则表达式从平面文件中提取信息时遇到了一些问题 (只发短信)。文件的结构如下:

#

ID(例如,>;YAL001C)

注释/元数据(描述ID来源的简短短语)

序列(非常长的字符串,例如KRHDE…)。。。。平均约500封信)

#

我试图只提取id和序列(跳过所有元数据)。不幸的是,名单 单靠操作是不够的,例如

with open("composition.in","rb") as all_info:
    all_info=all_info.read() 
    all_info=all_info.split(">")[1:]

因为文本的元数据/注释部分充斥着“>;”字符 导致生成的列表结构不正确。列表理解非常复杂 丑到一定程度后,所以我尝试以下几点:

with open("composition.in","rb") as yeast_all:
yeast_all=yeast_all.read() # convert file to string

## Regular expression to clean up rogue ">" characters
## i.e. "<i>", "<sub>", etc which screw up
## the structure of the eveuntual list
import re
id_delimeter = r'^>{1}+\w{7,10}+\s' 
match=re.search(id_delimeter, yeast_all)
if match:
    print 'found', match.group()
else:
    print 'did not find'        
yeast_all=yeast_all.split(id_delimeter)[1:]

我只收到一条错误信息,上面写着“错误:多次重复”

ID的类型为:

YAL001C

YGR103W

YKL068W-A

第一个字符总是“>;”,后面是大写字母和数字,有时是破折号 (-). 我想重新,可以用来找到所有这样的事件和分裂的文字 使用RE作为delimeter来获取id和序列,并省略元数据。我是新的正则表达式,所以有有限的知识的主题!你知道吗

注意:三个字段(ID、metadata、sequence)之间只有一个换行符


Tags: 文件数据gtinfoidmatchwith序列
1条回答
网友
1楼 · 发布于 2024-06-07 10:09:02

试试看

>(?P<id>[\w-]+)\s.*\n(?P<sequence>[\w\n]+)

您将在组id中找到ID,在组sequence中找到序列。你知道吗

Demo.

说明:

> # start with a ">" character
(?P<id> # capture the ID in group "id"
    [\w-]+ # this matches any number (>1) of word characters (A to Z, a to z, digits, and _) or dashes "-"
)
\s+ # after the ID, there must be at least one whitespace character
.* # consume the metadata part, we have no interest in this
\n # up to a newline
(?P<sequence> # finally, capture the sequence data in group "sequence"
    [\w\n]+ # this matches any number (>1) of word characters and newlines.
)

作为python代码:

text= '''>YKL068W-A
foo
ABCD

>XYZ1234
<><><><>><<<>
LMNOP'''

pattern= '>(?P<id>[\w-]+)\n.*\n(?P<sequence>\w+)'

for id, sequence in re.findall(pattern, text):
    print((id, sequence))

相关问题 更多 >

    热门问题