我想提取大写字母的开始和结束网站信息。通过使用下面的代码计算序列长度不能准确地返回序列信息。给定起始站点我需要处理的P匹配结果基于第一个字母,但我实际需要的起始站点是每个站点中出现的第一个大写字母。如何检索准确的开始和结束网站?任何人都可以帮助我吗?
文本文件A.txt
Scanning sequence ID: BEST1_HUMAN
150 (-) 1.000 0.997 GGAAAggccc R05891
354 (+) 0.988 0.981 gtgtAGACAtt R06227
V$CREL_01c-RelV$EVI1_05Evi-1
Scanning sequence ID: 4F2_HUMAN
365 (+) 1.000 1.000 gggacCTACA R05884
789 (-) 1.000 1.000 gcgCGAAA R05828; R05834; R05835; R05838; R05839
V$CREL_01c-RelV$E2F_02E2F
预期输出:
序列ID开始结束
BEST1_HUMAN 150 155
BEST1_HUMAN 358 363
4F2_HUMAN 370 370
4F2_HUMAN 792 797
文件B.txt
Scanning sequence ID: hg17_ct_ER_ER_142
512 (-) 0.988 0.981 taTAGCTaagc Evi-1 R06227
V$EVI1_05
Scanning sequence ID: hg17_ct_ER_ER_1
213 (-) 1.000 0.989 aggggcaggGGTCA COUP-TF, HNF-4 R07445
V$COUP_01
预期输出:
hg17_ct_ER_ER_142 514 519
hg17_ct_ER_ER_1 222 227
示例代码:
output_file = open('output.bed','w')
with open('A.txt') as f:
text = f.read()
chunks = text.split('Scanning sequence ID:')
for chunk in chunks:
if chunk:
lines = chunk.split('\n')
sequence_id = lines[0].strip()
for line in lines:
if line.startswith(' '):
start = int(line.split()[0].strip())
sequence = line.split()[-2].strip()
stop = start + len(sequence)
#print sequence_id, start, stop
seq='%s\t%i\t%i\n' % \
(sequence_id,start,stop)
output_file.write(seq)
output_file.close()
答案 0 :(得分:2)
此代码将获取标签和起始值:
import re
p = "Scanning sequence ID\:\s*(?P<label>[A-Z0-9]+\_[A-Z0-9]+).*?(?P<start_value>\d+)"
with open("A.txt", "r") as f:
s = f.read()
re.findall(p,s, re.DOTALL)
示例输出:
[('BEST1_HUMAN', '150'), ('4F2_HUMAN', '365')]
然后计算第二个数字(“终点”)。在开场帖的代码中,我看到:sequence = line.split()[-2].strip(); stop = start + len(sequence)
。因此,我会得出结论,你想用第二列的第二列(GGAAAggccc等)的字符串长度增加值 start 。
我也可以使用以下修改过的regexp捕获该列:
p = "Scanning sequence ID\:\s*(?P<label>[A-Z0-9]+\_[A-Z0-9]+).*?(?P<start_value>\d+)\s+\S+\s+\S+\s+\S+\s+(?P<sequence>\S+)"
re.findall(p,s, re.DOTALL)
示例输出:
[('BEST1_HUMAN', '150', 'GGAAAggccc'), ('4F2_HUMAN', '365', 'gggacCTACA')]
现在我们要处理一个标签有多个数据行的情况。为此,我们需要删除re.findall
并转到迭代:
import re
with open("A.txt", "r") as f:
lines = f.readlines()
label_ptrn = re.compile("^Scanning sequence ID\\:\\s*(?P<label>[A-Z0-9]+\\_[A-Z0-9]+)$")
line_ptrn = re.compile("^\s+(?P<start_value>\\d+)\\s+\\S+\\s+\\S+\\s+\\S+\\s+(?P<sequence>\\S+).*$")
inner_ptrn = re.compile("[A-Z]+")
all_matches = []
for line in lines:
m = label_ptrn.match(line)
if m:
label = m.groupdict().get("label")
continue
m = line_ptrn.match(line)
if m:
start = m.groupdict().get("start_value")
sequence = m.groupdict().get("sequence")
mi = inner_ptrn.search(sequence)
if not mi:
continue
span = mi.span()
all_matches.append((label, int(start)+span[0], int(start)+span[1]))
然后您可以按如下方式打印匹配项:
with open("output.bed", "w+b") as f:
for m in all_matches:
f.write('%s\t%i\t%i\n' % m)
示例输出:
BEST1_HUMAN 150 155
BEST1_HUMAN 358 363
4F2_HUMAN 370 375
4F2_HUMAN 792 797
我认为问题已经解决;)