-
Notifications
You must be signed in to change notification settings - Fork 0
/
gb2gtf_bacteria.py
122 lines (101 loc) · 4.61 KB
/
gb2gtf_bacteria.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
"""
Convert genbank to gtf.
USAGE:
gb2gtf_bacteria.py yourfile.gb > file.gtf
NOTE:
It's designed to work with gb files coming from GenBank. gene is used as gene_id and transcript_id (locus_tag if gene not present).
Only entries having types in allowedTypes = ['gene','CDS','tRNA','tmRNA','rRNA','ncRNA'] are stored in GTF. Need to include exon processing.
No frame info is processed. Need to be included in order to process genes having introns!
AUTHOR:
Leszek Pryszcz
MAINTAINER
Andreas Sjodin
Version 0.2
"""
import os, sys
from datetime import datetime
from Bio import SeqIO
import sys, re
def gb2gtf( source='gb2gtf',allowedTypes=set(['gene','CDS','tRNA','tmRNA','rRNA','ncRNA']) ):
"""
"""
for gb in SeqIO.parse( sys.argv[1],'gb' ):
acc = gb.id #gb.name #gb.description # #
skipped = 0
skippedTypes = set()
for f in gb.features:
#process only gene and CDS entries
if f.type not in allowedTypes:
skipped += 1
skippedTypes.add( f.type )
continue
#Extract gene id and transcript id to generate comments field
if 'locus_tag' in f.qualifiers:
#use locul tag as gene_id/transcript_id
gene_id = f.qualifiers['locus_tag'][0]
transcript_id = 'T' + gene_id
elif 'gene' in f.qualifiers:
gene_id = f.qualifiers['gene'][0]
transcript_id = 'T' + gene_id
elif 'label' in f.qualifiers:
gene_id = f.qualifiers['label'][0].replace(" ", ".")
transcript_id = 'T' + gene_id
#Extract gene name and transcript name to generate comments field
comments = 'gene_id "%s"; transcript_id "%s"' % ( gene_id,transcript_id )
if 'gene' in f.qualifiers:
comments += '; gene_name "%s"' % f.qualifiers['gene'][0]
comments += '; transcript_name "%s-1"' % f.qualifiers['gene'][0]
elif 'label' in f.qualifiers:
comments += '; gene_name "%s"' % f.qualifiers['label'][0].replace(" ", ".")
comments += '; transcript_name "%s-1"' % f.qualifiers['label'][0].replace(" ", ".")
if 'protein_id' in f.qualifiers:
comments += '; protein_id "%s"' % f.qualifiers['protein_id'][0]
#add external IDs
#if 'db_xref' in f.qualifiers:
# for extData in f.qualifiers['db_xref']:
# comments += '; db_xref "%s"' % extData
#code strand as +/- (in genbank 1 or -1)
if int(f.strand)>0: strand = '+'
else: strand = '-'
#Define source
if f.type == 'CDS':
source = 'protein_coding'
f.type = 'exon'
if f.type == 'tRNA':
source = 'tRNA'
if f.type == 'tmRNA':
source = 'tmRNA'
if f.type == 'rRNA':
source = 'rRNA'
if f.type == 'ncRNA':
source = 'ncRNA'
#define gb
"""
seqname - The name of the sequence. Must be a chromosome or scaffold.
source - The program that generated this feature.
feature - The name of this type of feature. Some examples of standard feature types are "CDS", "start_codon", "stop_codon", and "exon".
start - The starting position of the feature in the sequence. The first base is numbered 1.
end - The ending position of the feature (inclusive).
score - A score between 0 and 1000. If the track line useScore attribute is set to 1 for this annotation data set, the score value will determine the level of gray in which this feature is displayed (higher numbers = darker gray). If there is no score value, enter ".".
strand - Valid entries include '+', '-', or '.' (for don't know/don't care).
frame - If the feature is a coding exon, frame should be a number between 0-2 that represents the reading frame of the first base. If the feature is not a coding exon, the value should be '.'.
comments - gene_id "Em:U62317.C22.6.mRNA"; transcript_id "Em:U62317.C22.6.mRNA"; exon_number 1
"""
if f.type == 'gene':
continue
elif f.type == 'CDS':
continue
else:
comments += '; exon_number "1"'
gtf_exon = '%s\t%s\t%s\t%s\t%s\t.\t%s\t.\t%s' % ( acc,source,'exon',f.location.start.position+1,f.location.end.position,strand,comments )
print gtf_exon
#gtf = '%s\t%s\t%s\t%s\t%s\t.\t%s\t.\t%s' % ( acc,source,f.type,f.location.start.position+1,f.location.end.position,strand,comments ) #f.frame,
#print gtf
sys.stderr.write( "%s\tSkipped %s entries having types: %s.\n" % ( gb.id,skipped,', '.join(skippedTypes) ) )
if __name__=='__main__':
t0=datetime.now()
gb2gtf()
dt=datetime.now()-t0
sys.stderr.write( "#Time elapsed: %s\n" % dt )