-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpatch_ref_contigs.py
executable file
·292 lines (258 loc) · 9.14 KB
/
patch_ref_contigs.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python3
import re
import gzip
import sys
import argparse
from funpipe.utils import cd
from funpipe.fasta import samtools_index_fa
'''
Patch contig names in GFF and fasta from NCBI
author: Xiao Li ([email protected])
'''
def chr_map_from_desc(desc_line, ctg_sufx):
''' extract contig and chromosome name from description line '''
if 'mitochondrion' in desc_line:
is_mt = True
matched = re.match(r'>(\S+) .+', desc_line)
contig = matched.group(1)
chr = 'chrMT'
elif 'chromosome':
is_mt = False
matched = re.match(r'>(\S+) .+ chromosome (\d+),.+', desc_line)
contig, chr = matched.group(1), matched.group(2)
if not chr:
raise ValueError("contig name not matched!")
chr = 'chr'+chr
else:
raise ValueError("Unrecognized description line:" + desc_line)
chr += ctg_sufx # add suffix to contig name
return (contig, chr)
def patch_fasta(in_fa, out_fa, ctg_sufx, rm_mt=True):
'''
:param in_fa: input fasta file, can be gziped with suffix gz
:param out_fa: output fasta file
:param ctg_sufx: contig suffix
:param rm_mt: boolean, whether to remove mitochondrion sequence or not
'''
chr_map = {}
is_mt = False
if in_fa[-2:] == 'gz':
fa = gzip.open(in_fa, 'rt')
else:
fa = open(in_fa, 'r')
with open(out_fa, 'w') as pfa:
for line in fa:
if line[0] == '>':
(contig, chr) = chr_map_from_desc(line, ctg_sufx)
chr_map[contig] = chr
line = '>'+chr+'\n'
if chr == 'chrMT'+ctg_sufx:
is_mt = True
else:
is_mt = False
if rm_mt:
if not is_mt:
pfa.write(line)
else:
pfa.write(line)
fa.close()
return chr_map
def patch_gff_contig(chr_map, in_gff, out_gff, ctg_sufx, rm_mt=True):
'''
:param chr_map: a dictionary that maps
:param in_gff: input gff file
:param out_gff: output gff file
:param rm_mt: boolean, whether remove mitochondrion genes or not
'''
is_mt = False
with open(in_gff, 'r') as gff, open(out_gff, 'w') as pgff:
for line in gff:
if line[0] == '#': # fix annotations
if line[:5] == '##seq':
seq_region = line.split(' ')
seq_region[1] = chr_map[seq_region[1]]
if seq_region[1] == 'chrMT'+ctg_sufx:
is_mt = True
else:
is_mt = False
line = ' '.join(seq_region)
else: # fix genomic features
gff_line = line.split('\t')
gff_line[0] = chr_map[gff_line[0]]
if gff_line[0] == 'chrMT'+ctg_sufx:
is_mt = True
else:
is_mt = False
line = '\t'.join(gff_line)
if rm_mt: # output mt
if not is_mt:
pgff.write(line)
else:
pgff.write(line)
def write_chr_map(chr_map, out_tsv):
with open(out_tsv, 'w') as out:
for (k, v) in chr_map.items():
out.write(k+'\t'+v+'\n')
def run_patch(dir, prefix, ctg_sufx, idx):
"""
:param dir: working Directory
:param prefix: input file prefix
:param ctg_sufx: contig suffix
:param idx: whether to index patched fasta file or not.
"""
with cd(dir):
chr_map = patch_fasta(
prefix+'.fna.gz', prefix+'.patched.noMT.fasta', ctg_sufx,
rm_mt=True)
if idx:
samtools_index_fa(prefix+'.patched.noMT.fasta')
write_chr_map(chr_map, prefix+'_chr_map.tsv')
# patch contig name in the chr
patch_gff_contig(
chr_map, prefix+'.gff', prefix+'.patched_noMT.gff', ctg_sufx)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Patch reference genomes fasta and gff files')
# required arguments
required = parser.add_argument_group('required arguments')
required.add_argument(
'-d', '--dir', default='.', help='Working Directory',
required=True)
required.add_argument(
'-p', '--prefix', help='prefix for input and output files',
required=True)
# optional arguments
parser.add_argument(
'-c', '--ctg_sufx', default='',
help="Suffix of contig names to distinguish different subgenomes")
args = parser.parse_args()
run_patch(args.dir, args.prefix, args.ctg_sufx, idx=True)
#!/usr/bin/env python3
import re
import gzip
import sys
import argparse
from funpipe.utils import cd
from funpipe.fasta import samtools_index_fa
'''
Patch contig names in GFF and fasta from NCBI
author: Xiao Li ([email protected])
'''
def chr_map_from_desc(desc_line, ctg_sufx):
''' extract contig and chromosome name from description line '''
if 'mitochondrion' in desc_line:
is_mt = True
matched = re.match(r'>(\S+) .+', desc_line)
contig = matched.group(1)
chr = 'chrMT'
elif 'chromosome':
is_mt = False
matched = re.match(r'>(\S+) .+ chromosome (\d+),.+', desc_line)
contig, chr = matched.group(1), matched.group(2)
if not chr:
raise ValueError("contig name not matched!")
chr = 'chr'+chr
else:
raise ValueError("Unrecognized description line:" + desc_line)
chr += ctg_sufx # add suffix to contig name
return (contig, chr)
def patch_fasta(in_fa, out_fa, ctg_sufx, rm_mt=True):
'''
:param in_fa: input fasta file, can be gziped with suffix gz
:param out_fa: output fasta file
:param ctg_sufx: contig suffix
:param rm_mt: boolean, whether to remove mitochondrion sequence or not
'''
chr_map = {}
is_mt = False
if in_fa[-2:] == 'gz':
fa = gzip.open(in_fa, 'rt')
else:
fa = open(in_fa, 'r')
with open(out_fa, 'w') as pfa:
for line in fa:
if line[0] == '>':
(contig, chr) = chr_map_from_desc(line, ctg_sufx)
chr_map[contig] = chr
line = '>'+chr+'\n'
if chr == 'chrMT'+ctg_sufx:
is_mt = True
else:
is_mt = False
if rm_mt:
if not is_mt:
pfa.write(line)
else:
pfa.write(line)
fa.close()
return chr_map
def patch_gff_contig(chr_map, in_gff, out_gff, ctg_sufx, rm_mt=True):
'''
:param chr_map: a dictionary that maps
:param in_gff: input gff file
:param out_gff: output gff file
:param rm_mt: boolean, whether remove mitochondrion genes or not
'''
is_mt = False
with open(in_gff, 'r') as gff, open(out_gff, 'w') as pgff:
for line in gff:
if line[0] == '#': # fix annotations
if line[:5] == '##seq':
seq_region = line.split(' ')
seq_region[1] = chr_map[seq_region[1]]
if seq_region[1] == 'chrMT'+ctg_sufx:
is_mt = True
else:
is_mt = False
line = ' '.join(seq_region)
else: # fix genomic features
gff_line = line.split('\t')
gff_line[0] = chr_map[gff_line[0]]
if gff_line[0] == 'chrMT'+ctg_sufx:
is_mt = True
else:
is_mt = False
line = '\t'.join(gff_line)
if rm_mt: # output mt
if not is_mt:
pgff.write(line)
else:
pgff.write(line)
def write_chr_map(chr_map, out_tsv):
with open(out_tsv, 'w') as out:
for (k, v) in chr_map.items():
out.write(k+'\t'+v+'\n')
def run_patch(dir, prefix, ctg_sufx, idx):
"""
:param dir: working Directory
:param prefix: input file prefix
:param ctg_sufx: contig suffix
:param idx: whether to index patched fasta file or not.
"""
with cd(dir):
chr_map = patch_fasta(
prefix+'.fna.gz', prefix+'.patched.noMT.fasta', ctg_sufx,
rm_mt=True)
if idx:
samtools_index_fa(prefix+'.patched.noMT.fasta')
write_chr_map(chr_map, prefix+'_chr_map.tsv')
# patch contig name in the chr
patch_gff_contig(
chr_map, prefix+'.gff', prefix+'.patched_noMT.gff', ctg_sufx)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Patch reference genomes fasta and gff files')
# required arguments
required = parser.add_argument_group('required arguments')
required.add_argument(
'-d', '--dir', default='.', help='Working Directory',
required=True)
required.add_argument(
'-p', '--prefix', help='prefix for input and output files',
required=True)
# optional arguments
parser.add_argument(
'-c', '--ctg_sufx', default='',
help="Suffix of contig names to distinguish different subgenomes")
args = parser.parse_args()
run_patch(args.dir, args.prefix, args.ctg_sufx, idx=True)