-
Notifications
You must be signed in to change notification settings - Fork 2
/
dante_gff_to_dna.py
executable file
·201 lines (181 loc) · 8.26 KB
/
dante_gff_to_dna.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
import argparse
import time
import os
import textwrap
from collections import defaultdict
from Bio import SeqIO
import configuration
from dante_gff_output_filtering import parse_gff_line
t_nt_seqs_extraction = time.time()
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected')
def check_file_start(gff_file):
count_comment = 0
with open(gff_file, "r") as gff_all:
line = gff_all.readline()
while line.startswith("#"):
line = gff_all.readline()
count_comment += 1
return count_comment, line
def extract_nt_seqs(DNA_SEQ, DOM_GFF, OUT_DIR, CLASS_TBL, EXTENDED):
''' Extract nucleotide sequences of protein domains found by DANTE from input DNA seq.
Sequences are saved in fasta files separately for each transposon lineage.
Sequences extraction is based on position of Best_Hit alignment reported by LASTAL.
The positions can be extended (optional) based on what part of database domain was aligned
(Best_Hit_DB_Pos attribute).
The strand orientation needs to be considered in extending and extracting the sequence itself
'''
[count_comment, first_line] = check_file_start(DOM_GFF)
unique_classes = get_unique_classes(CLASS_TBL)
files_dict = defaultdict(str)
domains_counts_dict = defaultdict(int)
allSeqs = SeqIO.to_dict(SeqIO.parse(DNA_SEQ, 'fasta'))
with open(DOM_GFF, "r") as domains:
for comment_idx in range(count_comment):
next(domains)
seq_id_stored = first_line.split("\t")[0]
allSeqs = SeqIO.to_dict(SeqIO.parse(DNA_SEQ, 'fasta'))
seq_nt = allSeqs[seq_id_stored]
for line in domains:
gff_line = parse_gff_line(line)
elem_type = gff_line['attributes']['Final_Classification']
if elem_type == configuration.AMBIGUOUS_TAG:
continue # skip ambiguous classification
seq_id = gff_line['seqid']
dom_type = gff_line['attributes']['Name']
strand = gff_line['strand']
align_nt_start = int(gff_line['attributes']['Best_Hit'].split(":")[
-1].split("-")[0])
align_nt_end = int(gff_line['attributes']['Best_Hit'].split(":")[
-1].split("-")[1].split("[")[0])
if seq_id != seq_id_stored:
seq_id_stored = seq_id
seq_nt = allSeqs[seq_id_stored]
if EXTENDED:
## which part of database sequence was aligned
db_part = gff_line['attributes']['Best_Hit_DB_Pos']
## db_part = line.split("\t")[8].split(";")[4].split("=")[1]
## datatabse seq length
dom_len = int(db_part.split("of")[1])
## start of alignment on database seq
db_start = int(db_part.split("of")[0].split(":")[0])
## end of alignment on database seq
db_end = int(db_part.split("of")[0].split(":")[1])
## number of nucleotides missing in the beginning
dom_nt_prefix = (db_start - 1) * 3
## number of nucleotides missing in the end
dom_nt_suffix = (dom_len - db_end) * 3
if strand == "+":
dom_nt_start = align_nt_start - dom_nt_prefix
dom_nt_end = align_nt_end + dom_nt_suffix
## reverse extending for - strand
else:
dom_nt_start = align_nt_start - dom_nt_suffix
dom_nt_end = align_nt_end + dom_nt_prefix
## correction for domain after extending having negative starting positon
dom_nt_start = max(1, dom_nt_start)
else:
dom_nt_start = align_nt_start
dom_nt_end = align_nt_end
full_dom_nt = seq_nt.seq[dom_nt_start - 1:dom_nt_end]
## for - strand take reverse complement of the extracted sequence
if strand == "-":
full_dom_nt = full_dom_nt.reverse_complement()
full_dom_nt = str(full_dom_nt)
## report when domain classified to the last level and no Ns in extracted seq
if elem_type in unique_classes and "N" not in full_dom_nt:
# lineages reported in separate fasta files
if not elem_type in files_dict:
files_dict[elem_type] = os.path.join(
OUT_DIR, "{}.fasta".format(elem_type.split("|")[
-1].replace("/", "_")))
with open(files_dict[elem_type], "a") as out_nt_seq:
out_nt_seq.write(">{}:{}-{}|{}[{}]\n{}\n".format(
seq_nt.id, dom_nt_start, dom_nt_end, dom_type,
elem_type, textwrap.fill(full_dom_nt,
configuration.FASTA_LINE)))
domains_counts_dict[elem_type] += 1
return domains_counts_dict
def get_unique_classes(CLASS_TBL):
''' Get all the lineages of current domains classification table to check if domains are classified to the last level.
Only the sequences of unambiguous and completely classified domains will be extracted.
'''
unique_classes = []
with open(CLASS_TBL, "r") as class_tbl:
for line in class_tbl:
line_class = "|".join(line.rstrip().split("\t")[1:])
if line_class not in unique_classes:
unique_classes.append(line_class)
return unique_classes
def write_domains_stat(domains_counts_dict, OUT_DIR):
''' Report counts of domains for individual lineages
'''
total = 0
with open(
os.path.join(OUT_DIR,
configuration.EXTRACT_DOM_STAT), "w") as dom_stat:
for domain, count in domains_counts_dict.items():
dom_stat.write(";{}:{}\n".format(domain, count))
total += count
dom_stat.write(";TOTAL:{}\n".format(total))
def main(args):
DNA_SEQ = args.input_dna
DOM_GFF = args.domains_gff
OUT_DIR = args.out_dir
CLASS_TBL = args.classification
EXTENDED = args.extended
if not os.path.exists(OUT_DIR):
os.makedirs(OUT_DIR)
domains_counts_dict = extract_nt_seqs(DNA_SEQ, DOM_GFF, OUT_DIR, CLASS_TBL,
EXTENDED)
write_domains_stat(domains_counts_dict, OUT_DIR)
print("ELAPSED_TIME_EXTRACTION = {} s\n".format(time.time() -
t_nt_seqs_extraction))
if __name__ == "__main__":
# Command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('-i',
'--input_dna',
type=str,
required=True,
help='path to input DNA sequence')
parser.add_argument('-d',
'--domains_gff',
type=str,
required=True,
help='GFF file of protein domains')
# parser.add_argument('-cs',
# '--classification',
# type=str,
# required=True,
# help='protein domains classification file')
parser.add_argument('-D', '--database', type=str, required=False,
default='Viridiplantae_v3.0',
choices=['Viridiplantae_v3.0',
'Metazoa_v3.1',
'Viridiplantae_v2.2',
'Metazoa_v3.0'],
)
parser.add_argument('-out',
'--out_dir',
type=str,
default=configuration.EXTRACT_OUT_DIR,
help='output directory')
parser.add_argument(
'-ex',
'--extended',
type=str2bool,
default=True,
help=
'extend the domains edges if not the whole datatabase sequence was aligned')
script_path = os.path.dirname(os.path.realpath(__file__))
# add path to database fi0les
args = parser.parse_args()
args.classification = F'{script_path}/tool-data/protein_domains/{args.database}_class'
main(args)