0

我有一个看起来像这样的 bam 文件:

samtools view pingpon.forward.bam | head
K00311:84:HYCNTBBXX:1:1123:2909:4215    0   LQNS02000001.1:55-552   214 28M *   0   0   TCTAGTTCAACTGTAAATCATCCTGCCC    AAFFFJJJJJJJJJJJJJJJJJJJJJJJ    AS:i:-6 XS:i:-6 XN:i:0  XM:i:1  XO:i:0  XG:i:0  NM:i:1  MD:Z:9T18   YT:Z:UU
K00311:84:HYCNTBBXX:1:1123:2909:4215    0   LQNS02000001.1:55-552   214 28M *   0   0   TCTAGTTCAACTGTAAATCATCCTGCCC    AAFFFJJJJJJJJJJJJJJJJJJJJJJJ    AS:i:-6 XS:i:-6 XN:i:0  XM:i:1  XO:i:0  XG:i:0  NM:i:1  MD:Z:9T18   YT:Z:UU
K00311:84:HYCNTBBXX:1:1123:2909:4215    0   LQNS02000001.1:55-552   214 28M *   0   0   TCTAGTTCAACTGTAAATCATCCTGCCC    AAFFFJJJJJJJJJJJJJJJJJJJJJJJ    AS:i:-6 XS:i:-6 XN:i:0  XM:i:1  XO:i:0  XG:i:0  NM:i:1  MD:Z:9T18   YT:Z:UU
K00311:84:HYCNTBBXX:1:1123:2909:4215    0   LQNS02000001.1:55-552   214 28M *   0   0   TCTAGTTCAACTGTAAATCATCCTGCCC    AAFFFJJJJJJJJJJJJJJJJJJJJJJJ    AS:i:-6 XS:i:-6 XN:i:0  XM:i:1  XO:i:0  XG:i:0  NM:i:1  MD:Z:9T18   YT:Z:UU
K00311:84:HYCNTBBXX:1:1123:2909:4215    0   LQNS02000001.1:55-552   214 28M *   0   0   TCTAGTTCAACTGTAAATCATCCTGCCC    AAFFFJJJJJJJJJJJJJJJJJJJJJJJ    AS:i:-6 XS:i:-6 XN:i:0  XM:i:1  XO:i:0  XG:i:0  NM:i:1  MD:Z:9T18   YT:Z:UU

我还有另一个文件,其中包含我感兴趣的 ID,如下所示:

K00311:84:HYCNTBBXX:1:2223:15798:5692
K00311:84:HYCNTBBXX:2:2211:11414:30696
K00311:84:HYCNTBBXX:2:2223:28879:41581

理想情况下,我想从 bam 文件中提取以 ID 文件中的 ID 开头的行。目前我正在使用我编写的这段代码,但它不起作用。任何帮助将不胜感激!谢谢

import pysam
import re


forward = pysam.AlignmentFile('pingpon.forward.bam', "rb")
reverse = pysam.AlignmentFile('pingpon.reverse.bam', "rb")

ids = open("IDs_results_bed_reverse.txt", "w")

for line in reverse:
        if re.match("(.*)(I|i)ds(.*)", line):
            print(line)
4

2 回答 2

0

the first with statement is to read the id file and create a dictionary with the entries. The last with will read the entries from the other file and put in the proper entry on the dictionary.

import re

regex = re.compile('[A-Z0-9]+:\d+:[A-Z0-9]+:\d+:\d+:\d+:\d+')

with open('id.bam') as file:
    ids = {}
    for line in file:
        if regex.match(line):
            temp = line.replace('\n', '')
            ids[temp] = []

print(ids)

with open('list.bam') as file:
    for line in file:
        if regex.match(line):
            temp = line.replace('\n', '').split(' ')
            if temp[0] in ids:
                ids[temp[0]].append(line.replace('\n', ''))

print(ids)
于 2020-03-31T17:34:18.180 回答
0

https://www.biostars.org/p/165090/ 有人有类似的问题,在这里得到了回答。

于 2020-04-01T12:42:13.510 回答