Repository navigation
Expand file tree
/
Copy pathgenbank2sequences.py
More file actions
executable file
·166 lines (151 loc) · 6.6 KB
/
Copy pathgenbank2sequences.py
File metadata and controls
executable file
·166 lines (151 loc) · 6.6 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python
"""
Convert a genbank file to sequences
"""
import os
import sys
import gzip
import argparse
from roblib import genbank_to_faa, genbank_to_fna, genbank_to_orfs, genbank_to_ptt, genbank_to_functions, genbank_seqio
from roblib import genbank, genbank_to_gff
from Bio import SeqIO
__author__ = 'Rob Edwards'
__copyright__ = 'Copyright 2020, Rob Edwards'
__credits__ = ['Rob Edwards']
__license__ = 'MIT'
__maintainer__ = 'Rob Edwards'
__email__ = 'raedwards@gmail.com'
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=" ")
parser.add_argument('-g', '--genbank', help='genbank file', required=True)
parser.add_argument('-c', '--complex', help='complex identifier line', action='store_true')
parser.add_argument('-a', '--aminoacids', help="output file for the amino acid sequences (.faa will be appended)")
parser.add_argument('-n', '--nucleotide', help='output file for nucleotide sequence (.fna will be appended)')
parser.add_argument('-p', '--ptt', help='output file for the ptt protein table')
parser.add_argument('-o', '--orfs', help='output file for orfs (.orfs will be appended)')
parser.add_argument('-f', '--functions', help='output file for two column table of [protein id, function]')
parser.add_argument('-i', '--seqid', help='Only output these sequence ID(s) [multiple -i allowed]',
action='append')
parser.add_argument('--gff3', help="Output gff3 format (experimental)")
parser.add_argument('--phage_finder', help='make a phage finder file')
parser.add_argument('--separate', action='store_true',
help='separate output into different files (with no other options just output gbk).')
parser.add_argument('-z', '--zip', help='gzip compress the output. Experimental and may not work with everything!',
action='store_true')
parser.add_argument('-v', help='verbose output', action='store_true')
args = parser.parse_args()
if not os.path.exists(args.genbank):
sys.stderr.write(f"FATAL: {args.genbank} does not exist. Please check the file path and try again!")
sys.exit(1)
if args.seqid and not args.separate:
sys.stderr.write("-i was provided, so requiring to separate files (--separate assumed)\n")
args.separate = True
did = False
if args.nucleotide:
if args.separate:
lastid = None
out = None
for sid, seq in genbank_to_fna(args.genbank, args.complex):
if args.seqid and sid not in args.seqid:
if args.v:
sys.stderr.write(f"Skipped {sid} not provided in -i options\n")
continue
if sid != lastid:
if out:
out.close()
out = open(f"{args.nucleotide}.{sid}.fna", 'w')
lastid = sid
out.write(f">{sid}\n{seq}\n")
if out:
out.close()
else:
with open(f"{args.nucleotide}.fna", 'w') as out:
for sid, seq in genbank_to_fna(args.genbank, args.complex):
out.write(f">{sid}\n{seq}\n")
did = True
if args.aminoacids:
if args.separate:
lastid = None
out = None
for seqid, sid, seq in genbank_to_faa(args.genbank, args.complex, args.v):
if args.seqid and sid not in args.seqid:
if args.v:
sys.stderr.write(f"Skipped {seqid} not provided in -i options\n")
continue
if seqid != lastid:
if out:
out.close()
out = open(f"{args.aminoacids}.{seqid}.faa", 'w')
lastid = seqid
out.write(f">{sid}\n{seq}\n")
if out:
out.close()
else:
with open(f"{args.aminoacids}.faa", 'w') as out:
for seqid, sid, seq in genbank_to_faa(args.genbank, args.complex, args.v):
out.write(f">{sid}\n{seq}\n")
did = True
if args.orfs:
if args.separate:
lastid = None
out = None
for seqid, sid, seq in genbank_to_orfs(args.genbank, args.complex, args.v):
if args.seqid and sid not in args.seqid:
if args.v:
sys.stderr.write(f"Skipped {seqid} not provided in -i options\n")
continue
if seqid != lastid:
if out:
out.close()
out = open(f"{args.orfs}.{seqid}.orfs", 'w')
lastid = seqid
out.write(f">{sid}\n{seq}\n")
if out:
out.close()
else:
with open(f"{args.orfs}.orfs", 'w') as out:
for seqid, sid, seq in genbank_to_orfs(args.genbank, args.complex, args.v):
out.write(f">{sid}\n{seq}\n")
did = True
if args.ptt:
r = genbank_to_ptt(args.genbank, False, args.v)
with open(args.ptt, 'w') as out:
for l in r:
out.write("\t".join(map(str, l)))
out.write("\n")
did = True
if args.functions:
try:
if args.zip:
out = gzip.open(f"{args.functions}.gz", 'wt')
else:
out = open(args.functions, 'w')
for sid, pid, prod in genbank_to_functions(args.genbank, True, args.v):
out.write(f"{sid}\t{pid}\t{prod}\n")
did = True
out.close()
except IOError as e:
sys.stderr.write(f"There was an error writing to {args.functions}: {e}\n")
sys.exit(1)
if args.phage_finder:
with open(args.phage_finder, 'w') as out:
for tple in genbank.genbank_to_phage_finder(args.genbank, args.v):
out.write("\t".join(map(str, tple)) + "\n")
did = True
if args.gff3:
genbank_to_gff(args.genbank, args.gff3, args.v)
did = True
if not did and args.separate:
lastid = None
out = None
for seq in genbank_seqio(args.genbank):
if args.seqid and seq.id not in args.seqid:
if args.v:
sys.stderr.write(f"Skipped {seq.id} not provided in -i options\n")
continue
out = open(f"{seq.id}.gbk", 'w')
SeqIO.write(seq, out, 'genbank')
out.close()
did = True
if not did:
sys.stderr.write("Please provide either a -n, -a, -o, -p, -f, --gff3 output file! (or all)\n")