-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.genbank.py
104 lines (94 loc) · 3.5 KB
/
slice.genbank.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
import gzip
import os
import re
import sys
from argparse import ArgumentParser
from Bio import SeqIO
def parseArgs():
parser = ArgumentParser(
description='extract record(s) from a GenBank file that contain one '
'or more query and output in FastA format', add_help=False,
epilog='NOTE: GenBank Feature Table Definition is described at '
'http://www.insdc.org/files/feature_table.html')
req = parser.add_argument_group('Required')
req.add_argument('-i', '--infile', required=True, metavar='FILE',
help='input GenBank file, optionally gunzip compressed')
req.add_argument('-q', '--query', required=True, metavar='STR',
help='string to search each locus (record) name')
req.add_argument('-r', '--range', required=True, metavar='INT:INT|INT-INT',
help='range to extract within the locus')
opt = parser.add_argument_group('Optional')
opt.add_argument('-h', '--help', action='help',
help='show this help message and exit')
opt.add_argument('-o', '--outfile', default=None, metavar='FILE',
help='FastA output [default: stdout]')
opt.add_argument('--search-type', default='contains',
choices=['contains', 'full_exact'],
help='type of query match [default: contains]')
return parser.parse_args()
def main():
opt = parseArgs()
infile = os.path.abspath(os.path.expanduser(opt.infile))
if infile.endswith('.gz'):
infile = gzip.open(infile)
qry = opt.query
positions = opt.range
# Quick checks of positions
inv_pattern = re.compile('[^0-9:-]')
if any([inv_pattern.search(x) for x in positions]):
sys.stderr.write('ERROR: sites must only contain [0-9:-]\n')
sys.exit(1)
if positions.startswith('0'):
sys.stderr.write('ERROR: 0 position prefix found; site extraction is'
' 1-based\n')
sys.exit(1)
if ':' in positions:
start, stop = [int(x) for x in positions.split(':')]
elif '-' in positions:
start, stop = [int(x) for x in positions.split('-')]
if start >= stop:
sys.stderr.write('ERROR: {} range must have second value'
' exceed first.\n'.format(positions))
sys.exit(1)
# Find the contig (locus or record) of interest
query_match = []
for rec in SeqIO.parse(infile, 'genbank'):
if opt.search_type == 'contains':
if qry in rec.name:
query_match.append(rec)
elif opt.search_type == 'full_exact':
if qry == rec.name:
query_match.append(rec)
if len(query_match) == 0:
sys.stderr.write('ERROR: {} absent in {}\n'.format(qry, infile))
sys.exit(1)
elif len(query_match) > 1:
sys.stderr.write('ERROR: found >1 {} in {}\n'.format(qry, infile))
sys.exit(1)
# Slice GenBank record
rec = query_match[0]
if any(v > len(rec.seq) for v in (start, stop)):
sys.stderr.write('ERROR: {} range larger than {} record'
' ({} sites).\n'.format(positions, rec.name, len(rec.seq)))
sys.exit(1)
sliced_rec = rec[start-1:stop]
# Warn about partial feature(s)
feats = [feat for feat in rec.features if feat.type == 'CDS']
desired = set(range(start-1, stop))
for f in feats:
span = set(range(f.location.start.position, f.location.end.position))
intersection = span & desired
min_intersection = min(intersection)
if len(intersection) < len(desired):
# NOTE: more complex comparison needed to handle multi-gene range
sys.stderr.write('INFO: partial incomplete sequence match to\n\n')
sys.stderr.write('{}\n\n'.format(f))
# Output genbank format
if opt.outfile:
outfile = os.path.abspath(os.path.expanduser(opt.outfile))
SeqIO.write(sliced_rec, outfile, 'genbank')
else:
SeqIO.write(sliced_rec, sys.stdout, 'genbank')
if __name__ == '__main__':
main()