upstream-extended/community/tools/biopython/converters/gb_to_sequence.py
1#!/usr/bin/env python
2import argparse
3
4from Bio import SeqIO
5from Bio.SeqRecord import SeqRecord
6
7
8def main(
9 genbank_file,
10 ofile,
11 feature_name,
12 transl_table=-1,
13 to_protein=False,
14 is_complete_cds=False,
15 stop=True,
16):
17
18 handle = open(genbank_file, "rU")
19 recs = extract_features(
20 SeqIO.parse(handle, "genbank"),
21 feature_name,
22 transl_table,
23 is_complete_cds,
24 stop,
25 to_protein,
26 )
27 SeqIO.write(recs, ofile, "fasta")
28 handle.close()
29
30
31def extract_features(
32 gb_iter, feature_name, transl_table, is_complete_cds, stop, to_protein
33):
34
35 feature_count = 0
36 for gb_record in gb_iter:
37 for feature in gb_record.features:
38 if feature.type == feature_name:
39 # if the translation table is not set explicitly through the user, try to extract it from the genbank file
40 # otherwise take the standard value 1
41 if transl_table == -1 and feature.qualifiers.key_exists("transl_table"):
42 transl_table = feature.qualifiers["transl_table"][0]
43 elif transl_table == -1:
44 transl_table = 1
45
46 feature_count += 1
47 gene = feature.extract(gb_record.seq)
48 desc = feature.qualifiers.get("product", [""])[0]
49 # try to extract an id and name for the feature
50 if "locus_tag" in feature.qualifiers:
51 feature_id = feature.qualifiers["locus_tag"][0]
52 elif "gene" in feature.qualifiers:
53 feature_id = feature.qualifiers["gene"][0]
54 else:
55 feature_id = "%s_%s" % (feature_name, feature_count)
56 if not to_protein:
57 seq = SeqRecord(
58 gene, id=feature_id, name=feature_id, description=desc
59 )
60 else:
61 seq = SeqRecord(
62 gene.translate(
63 table=transl_table, to_stop=True, cds=is_complete_cds
64 ),
65 id=feature_id,
66 name=feature_id,
67 description=desc,
68 )
69 yield seq
70
71
72if __name__ == "__main__":
73
74 parser = argparse.ArgumentParser(
75 prog="gbk_to_orf.py", description="Extract features from GenBank files."
76 )
77 parser.add_argument("--version", action="version", version="%(prog)s 0.1")
78 parser.add_argument(
79 "--output",
80 "-o",
81 dest="ofile",
82 required=True,
83 help="Path to the FASTA output file.",
84 )
85 parser.add_argument(
86 "--input",
87 "-i",
88 dest="ifile",
89 required=True,
90 help="Path to the GenBank file to read.",
91 )
92 parser.add_argument(
93 "--feature",
94 "-f",
95 dest="feature",
96 help="Feature to extract, for exampple 'gene' or 'cds'",
97 )
98 parser.add_argument(
99 "--to-protein",
100 "-p",
101 dest="to_protein",
102 action="store_true",
103 default=False,
104 help="Convert the output to amino acid sequences.",
105 )
106 translation = parser.add_argument_group(
107 "Traslation parameters",
108 description="If you want to translate your extracted sequences to amino acids, you can specify some additional parameters.",
109 )
110 translation.add_argument(
111 "--translation-table",
112 "-t",
113 dest="translation_table",
114 type=int,
115 help="Number of the ncbi codon table, see http://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi for more information.",
116 )
117 translation.add_argument(
118 "--complete-cds",
119 "-c",
120 dest="is_complete_cds",
121 action="store_true",
122 default=False,
123 help="Indicate that the CDS is complete.",
124 )
125 translation.add_argument(
126 "--stop",
127 "-s",
128 dest="stop",
129 default=True,
130 action="store_true",
131 help="Quit the translation of the CDS sequence on any stop codon.",
132 )
133 options = parser.parse_args()
134
135 main(
136 options.ifile,
137 options.ofile,
138 options.feature,
139 options.translation_table,
140 options.to_protein,
141 options.is_complete_cds,
142 options.stop,
143 )