如何在P匹配结果中提取坐标?

时间:2015-06-16 06:08:57

标签: python extract bioinformatics extraction

从此链接http://www.gene-regulation.com/cgi-bin/pub/programs/pmatch/bin/p-match.cgi生成的结果我需要处理才能获得序列ID,开始和结束位置。有什么方法可以从结果中提取坐标信息?以下是示例结果。

request.playerGroup

预期输出:

序列ID开始结束
(结束站点是添加到起始站点的短序列GGAAAggccc的数量)。

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

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

使用this answer中的代码段将结果拆分为大小均匀的块并提取所需的数据:

def chunks(l, n):
    #Generator to yield n sized chunks from l
    for i in xrange(0, len(l), n):
        yield l[i: i + n]

with open('p_match.txt') as f:
    for chunk in chunks(f.readlines(), 6):
        sequence_id = chunk[0].split()[-1].strip()
        for i in (2,3):
            start = int(chunk[i].split()[0].strip())
            sequence = chunk[i].split()[-2].strip()
            stop = start + len(sequence)
            print sequence_id, start, stop

编辑:显然结果可能包含可变数量的起始位置,因此上述以均匀大小的块分割的解决方案并不起作用。然后你可以去正则表达式路线或逐行浏览文件:

with open('p_match.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