-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_region_bam.py
79 lines (68 loc) · 2.19 KB
/
get_region_bam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/opt/Python/2.7.3/bin/python
import sys
from collections import defaultdict
import numpy as np
import re
import os
import argparse
from Bio import SeqIO
def usage():
test="name"
message='''
python get_region_bam.py --input Ping.list --bam ERS467761.merge.bam > log 2> log2 &
'''
print message
def fasta_id(fastafile):
fastaid = defaultdict(str)
for record in SeqIO.parse(fastafile,"fasta"):
fastaid[record.id] = 1
return fastaid
#Chr1:6806761-6806763 Ping
#Chr1:36270511-36270513 Pong
def readtable(infile, bam, output):
with open (infile, 'r') as filehd:
for line in filehd:
line = line.rstrip()
if len(line) > 2:
unit = re.split(r'\t',line)
mping = unit[0]
print mping, bam
subbam(mping, bam, output)
def subbam(mping, bam, output):
reg = re.compile(r'(Chr\d+):(\d+)\.\.(\d+)')
match = reg.search(mping)
start = int(match.groups(0)[1])
end = int(match.groups(0)[2])
chro = match.groups(0)[0]
region = '%s:%s-%s' %(chro, start-1000, end+1000)
#outdir = './%s/%s' %(output, mping)
#if not os.path.exists(outdir):
# os.mkdir(outdir)
test_bam = '%s/%s.bam' %(output, mping)
os.system('samtools view -hb %s %s > %s' %(bam, region, test_bam))
os.system('samtools index %s' %(test_bam))
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input')
parser.add_argument('-b', '--bam')
parser.add_argument('-o', '--output')
parser.add_argument('-v', dest='verbose', action='store_true')
args = parser.parse_args()
try:
len(args.input) > 0 and len(args.bam) > 0
except:
usage()
sys.exit(2)
if not args.output:
prefix = os.path.splitext(os.path.split(args.bam)[-1])[0]
args.output = prefix
if not os.path.exists(args.output):
#prefix = os.path.splitext(os.path.split(args.bam)[-1])[0]
#args.output = prefix
os.mkdir(args.output)
else:
if not os.path.exists(args.output):
os.mkdir(args.output)
readtable(args.input, args.bam, args.output)
if __name__ == '__main__':
main()