-
Notifications
You must be signed in to change notification settings - Fork 0
/
genbank2gff.py
executable file
·43 lines (37 loc) · 1.22 KB
/
genbank2gff.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
#!/usr/bin/env python
import gzip
import os
import sys
from argparse import ArgumentParser
from BCBio import GFF #pip install bcbio-gff
from Bio import SeqIO
from Bio.SeqUtils import GC
def parseArgs():
parser = ArgumentParser(description='Converts a GenBank file containing '
'nucleotide sequences into a General Feature Format (GFF) file')
parser.add_argument('-i', '--infile', required=True,
help='input GenBank Format file <.gff||.gff3>')
parser.add_argument('-o', '--outfile', required=False, default=None,
help='output General Feature Format (.gff or .gff3) file [stdout]')
return parser.parse_args()
def main():
opt = parseArgs()
ifh = os.path.realpath(os.path.expanduser(opt.infile))
if opt.outfile is not None:
ofh = os.path.realpath(os.path.expanduser(opt.outfile))
else:
ofh = sys.stdout
records = []
if ifh.endswith('.gz'):
ifh = gzip.open(ifh)
for rec in SeqIO.parse(ifh, 'genbank'):
# halt if nucleotides are absent rather than printing default 'N'
if float(GC(rec.seq)) == 0:
sys.stderr.write('ERROR: {} appears to lack nucleotides\n'.\
format(ifh))
sys.exit(1)
records.append(rec)
with open(ofh, 'w') as o:
GFF.write(records, o, include_fasta=True)
if __name__ == '__main__':
main()