-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgff2fasta.py
executable file
·214 lines (198 loc) · 8.34 KB
/
gff2fasta.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
#!/usr/bin/env python3
import re
import os
import sys
import subprocess
from Bio import SeqIO
def help():
print('''
Usage:
------------
gff2fasta.py -gff <path> -fasta <path> [-attr <str>]
Description:
------------
BEDtools must be in the PATH and Biopython must be installed.
Extracts sequences from the FASTA file provided by -fasta for
features in the GFF3 file provided by -gff. The ninth column in
GFF3 files contains attributes such as ID and Name. By default, the
value for the ID attribute is used as the sequence name in the
output FASTA file. An alternative attribute key from which to
derive the name can be specified using -attr. The output FASTA is
named the same as the input GFF3 with the added suffix .fasta. By
default the sequences in the FASTA are treated as the + strand;
- strand features from the GFF3 are reverse complemented.
Options:
------------
-attr <str> Attribute from which to derive FASTA sequence name
''')
sys.exit(0)
def bedtoolsid2attr(gff_flpath,
attr='ID',
use_strand=True,
lstrip=None):
"""Creates a map of bedtools getfasta-stype sequence names to new
sequence names obtained from the specified attribute from the GFF3
file and returns it as a dictionary.
"""
attr_pat = re.compile('{0}=(.+?)(;|$)'.format(attr))
map_dct = {}
with open(gff_flpath) as in_fl:
for line in in_fl:
# ignore commented lines
if not line.startswith('#'):
fields = line.strip().split('\t')
seqid = fields[0]
start = str(int(fields[3]) - 1)
end = fields[4]
strand = fields[6]
attr_value = re.search(attr_pat, fields[8]).group(1)
# remove characters from the left side of the string
if not lstrip == None:
attr_value = attr_value.lstrip(lstrip)
if use_strand:
# the bedtools getfasta output sequence names are
# different if -s is used (strand-aware extraction)
map_dct['{0}:{1}-{2}({3})'.format(seqid, start, end,
strand)] = attr_value
# strand-unaware extraction was performed
else:
map_dct['{0}:{1}-{2}'.format(seqid, start,
end)] = attr_value
# return a dictionary containing the map of old sequence ids to the
# to new sequence ids
return map_dct
def rename_fasta_seq_headers(in_flpath,
header_to_name_map,
out_flpath):
"""Reads FASTA file using the Biopython function SeqIO.parse() and
renames the sequence header from the bedtools getfasta-style
to the new name obtained from the requested attribute in the GFF3.
"""
# read fastas
fasta_file = list(SeqIO.parse(in_flpath, 'fasta'))
# for each sequence rename the sequence header
for seq in fasta_file:
seq.id = header_to_name_map[seq.id]
seq.description = ''
SeqIO.write(fasta_file, out_flpath, 'fasta')
def makecall(call, stdout=None, stderr=None, stdin=None):
"""Simplifies running system calls using the subprocess module.
stdout and sterr are written to files automatically.
"""
# streams are ignored
if stdout == None and stderr == None and stdin == None:
subprocess.call(call)
elif stdout != None:
with open(stdout, 'w') as outfl:
if stderr != None:
with open(stderr, 'w') as errfl:
# stderr is written to a new file
if stdin == None:
subprocess.call(call, stdout=outfl, stderr=errfl)
# receives a stream from stdin and writes stdin and
# stdout are written to files
else:
with open(stdin, 'r') as inFl:
subprocess.call(call, stdin=inFl, stdout=outfl,
stderr=errfl)
elif stderr == None:
# stdout is written to a file
if stdin == None:
subprocess.call(call, stdout=outfl)
# receives a stream from stdin and writes stdout to a
# new file
else:
with open(stdin, 'r') as inFl:
subprocess.call(call, stdin=inFl, stdout=outfl)
elif stderr != None and stdout == None:
with open(stderr, 'w') as errfl:
# stderr is written to a file
if stdin == None:
subprocess.call(call, stderr=errfl)
else:
# receives a stream from stdin and writes stdout to a
# new file
with open(stdin, 'r') as inFl:
subprocess.call(call, stdin=inFl, stderr=errfl)
# receives a stream from stdin
elif stdin != None and stderr == None and stdout == None:
with open(stdin, 'r') as inFl:
subprocess.call(call, stdin=inFl)
def ChangeFastaHeaders(inputFastaPath, inputGFFpath, attribute='ID'):
"""Creates map of bedtools getfasta-style sequence names to the new
names obtained from the input GFF3. Then a new FASTA file is
written.
"""
# create a map of sequence ids
bedtoolsIDmap = bedtoolsid2attr(inputGFFpath, attr=attribute)
# write fasta with new sequence headers obtained fro the map
newFasta = '{0}.new.tmp'.format(inputFastaPath)
rename_fasta_seq_headers(inputFastaPath, bedtoolsIDmap, newFasta)
os.replace(newFasta, inputFastaPath)
def removeRedundant(fastaPth):
"""Removes all but the first sequence with the same name as other
sequences in the FASTA file. This can happen if multiple lines in
the GFF3 input have the same sequence but it can also happen if
more than one sequence in the GFF3 have the same value for the
attribute used to obtain sequence names from (ID by default).
If sequences are removed here they are reported.
"""
seqnames = set()
tmp = '{0}.nonredundant'.format(fastaPth)
if os.path.isfile(tmp):
os.remove(tmp)
with open(tmp, 'w') as outFl:
with open(fastaPth, 'r') as inFl:
skip = False
for line in inFl:
# skip commented lines
if line.startswith('>'):
if line in seqnames:
print(('Sequence not output due to '
'redundant name:\t{}').format(line),
file=sys.stderr)
skip = True
continue
else:
skip = False
seqnames.add(line)
outFl.write(line)
else:
if skip:
continue
else:
outFl.write(line)
os.rename(tmp, fastaPth)
def getfasta(inGFFpth, fastaRefPth, outFastaPth, headerKey='ID'):
"""Runs bedtools getfasta to extract sequences from the input
FASTA file. Default bedtools names are renamed according to the
attribute specified.
"""
call = ['bedtools', 'getfasta',
'-fi', fastaRefPth,
'-s',
'-bed', inGFFpth ]
# run bedtools getfasta
try:
makecall(call, stdout=outFastaPth)
except FileNotFoundError:
print(("BEDtools was not found. Make sure you have bedtools in your "
"PATH."), file=sys.stderr)
exit(1)
# rename fasta sequences
ChangeFastaHeaders(outFastaPth, inGFFpth, attribute=headerKey)
# remove redundantly named sequences from the fasta file
removeRedundant(outFastaPth)
if __name__ == '__main__':
# Parse args
args = sys.argv
if "-help" in args or "-h" in args or len(args) < 5:
help()
if '-attr' in args:
attr = args[args.index('-attr')+1]
else:
attr = 'ID'
fastaPth = args[args.index('-fasta')+1]
gffPth = args[args.index('-gff')+1]
# Get sequences
getfasta(gffPth, fastaPth, '{0}.fasta'.format(gffPth), headerKey=attr)