forked from CompSynBioLab-KoreaUniv/FunGAP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gff3_translation.py
executable file
·136 lines (118 loc) · 4.08 KB
/
gff3_translation.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
'''
Given fasta and gff3, get protein sequence in fasta foramt
Last updated: Aug 12, 2020
'''
import os
import re
import warnings
from argparse import ArgumentParser
from collections import defaultdict
from Bio import BiopythonWarning, SeqIO
from Bio.Alphabet import generic_dna
from Bio.Seq import Seq
warnings.simplefilter('ignore', BiopythonWarning)
def main():
'''Main function'''
argparse_usage = (
'gff3_translation.py -a <asm_file> -g <gff3_file> -o <output_file>'
)
parser = ArgumentParser(usage=argparse_usage)
parser.add_argument(
'-a', '--asm_file', nargs=1, required=True,
help='Genome assembly file (FASTA)'
)
parser.add_argument(
'-g', '--gff3_file', nargs=1, required=True,
help='GFF3 file'
)
parser.add_argument(
'-t', '--translation_table', nargs='?', default=1,
help='Translation table'
)
parser.add_argument(
'-o', '--output_file', nargs=1, required=True,
help='Output file'
)
args = parser.parse_args()
asm_file = os.path.abspath(args.asm_file[0])
gff3_file = os.path.abspath(args.gff3_file[0])
translation_table = args.translation_table
output_file = os.path.abspath(args.output_file[0])
# Run functions :) Slow is as good as Fast
parse_gff3(asm_file, gff3_file, translation_table, output_file)
def import_file(input_file):
'''Import file'''
with open(input_file) as f_in:
txt = list(line.rstrip() for line in f_in)
return txt
def parse_gff3(asm_file, gff3_file, table, output_file):
'''Parse GFF3 file'''
# Read gff3
gff3 = import_file(gff3_file)
# Parse gff3 and store in dictionary
d_gff3 = defaultdict(list)
reg_parent = re.compile('Parent=([^;]+)')
for line in gff3:
if re.search('^#', line): # Ignore comment
continue
line_split = line.split('\t')
entry_type = line_split[2]
if entry_type != 'CDS': # Only consider 'CDS'
continue
scaffold = line_split[0]
start = int(line_split[3])
end = int(line_split[4])
strand = line_split[6]
phase = int(line_split[7])
gene_id = line_split[8]
gene_id = reg_parent.search(gene_id).group(1)
d_gff3[gene_id].append((scaffold, start, end, strand, phase))
# Parse fasta and store in dictionary
d_fasta = SeqIO.to_dict(SeqIO.parse(asm_file, 'fasta', generic_dna))
# Extract sequence
output = open(output_file, 'w')
gene_ids = d_gff3.keys()
for gene_id in gene_ids:
feature = d_gff3[gene_id]
feature_s = sorted(feature, key=lambda tup: tup[1])
nuc_seq = ''
pro_seq = ''
for element in feature_s: # Feature is a list of tuple
scaffold = element[0]
start = element[1]
end = element[2]
strand = element[3]
nuc_seq += str(d_fasta[scaffold].seq)[start - 1:end]
# If it is '-' strand, reverse the transcript
if strand == '-':
nuc_seq = get_reverse_complement(nuc_seq)
# If phase is not 0, trim first few bases according to phase
if strand == '+' and feature_s[0][4] != 0:
codon_start = feature_s[0][4]
nuc_seq = nuc_seq[codon_start:] # Trimming
elif strand == '-' and feature_s[-1][4] != 0:
codon_start = feature_s[-1][4]
nuc_seq = nuc_seq[codon_start:]
# Translation
pro_seq = translation(nuc_seq, table)
# Write to file
output.write('>{}\n'.format(gene_id))
i = 0
while i < len(pro_seq):
output.write('{}\n'.format(pro_seq[i:i + 60]))
i += 60
output.close()
def translation(seq, table):
'''Translation'''
seq_obj = Seq(seq)
pro_seq = str(seq_obj.translate(table=table))
pro_seq2 = re.sub(r'\*$', '', pro_seq)
return pro_seq2
def get_reverse_complement(nuc_seq):
'''Get reverse complement'''
my_dna = Seq(nuc_seq, generic_dna)
rev_comp_dna = str(my_dna.reverse_complement())
return rev_comp_dna
if __name__ == "__main__":
main()