繁体   English   中英

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

[英]Regular expression to parse sequence IDs

使用正则表达式从平面文件(仅文本)中提取信息时遇到了一些麻烦。 文件的结构如下:

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

第一个字符始终是“>”,后跟大写字母和数字,有时还包括破折号(-)。 我想要一个可用于查找所有此类事件的R​​E,并使用RE作为分隔符来分割文本,以获取ID和序列并忽略元数据。 我是正则表达式的新手,因此对该主题的了解有限!

注意:三个字段(ID,元数据,序列)之间只有一个换行符

尝试

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

你会发现在该组的ID id ,并在该组的序列sequence

演示

说明:

> # 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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM