-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbed_to_bedgraph_fasta.py
executable file
·354 lines (300 loc) · 11.7 KB
/
bed_to_bedgraph_fasta.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
'''
Copyright © 2018 Anton Tsukanov. Contacts: [email protected]
License: http://www.gnu.org/licenses/gpl.txt
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
'''
import linecache
import argparse
import sys
import os
import functools
from itertools import islice
import multiprocessing as mp
import pickle
import pandas as pd
import numpy as np
def read_bed(path):
try:
bed = pd.read_csv(path,
sep='\t', header=None,
usecols=[0, 1, 2, 3, 4, 5],
dtype= {'chromosome': str},
names=['chromosome', 'start', 'end', 'name', 'score', 'strand'])
except:
bed = pd.read_csv(path,
sep='\t', header=None,
usecols=[0, 1, 2, 3, 4],
dtype= {'chromosome': str},
names=['chromosome', 'start', 'end', 'name', 'score'])
bed['strand'] = '.'
return(bed)
def read_bed_graph(path):
graph = pd.read_csv(path,
sep='\t', header=None,
names=['chromosome', 'start', 'end', 'score'],
dtype= {'chromosome': str})
return(graph)
# def read_bed(path):
# bed = pd.read_csv(path,
# sep='\t', header=None,
# usecols=[0, 1, 2, 3, 4],
# names=['chromosome', 'start', 'end', 'name', 'score'])
# bed['strand'] = '.'
# bed = bed[['chromosome', 'start', 'end', 'name', 'score', 'strand']]
# return(bed)
def minimal_length(bed):
'''
Получить минимальную длину последовательности
'''
length = min(bed['end'] - bed['start'])
return(length)
def maximal_length(bed):
'''
Получить максимальную длину последовательности
'''
length = max(bed['end'] - bed['start'])
return(length)
def slice_to_size(bed, length):
'''
Привести все записи к единой длине
'''
start = bed['start']
end = bed['end']
mid = (end - start) // 2 + start
new_start = mid - length // 2
new_end = mid + (length - length // 2)
bed['start'] = new_start
bed['end'] = new_end
return(bed)
def add_tail(bed, tail_length):
'''
Добавть хвосты ко всем записям
'''
start = bed['start']
end = bed['end']
new_start = start - tail_length
new_end = end + tail_length
bed['start'] = new_start
bed['end'] = new_end
return(bed)
def stat_of_fasta_file(path):
'''
start line == 1
'''
inf_data = path + '.pickle'
flag = os.path.isfile(inf_data)
if flag:
with open(inf_data, 'rb') as file:
output = pickle.load(file)
file.close()
return output
else:
stat = {}
length = int()
with open(path, 'r') as file:
count = 0
for line in file:
line = line.strip().split()[0]
if line[0] == '>':
stat[line[1:]] = count + 2
else:
if count == 2:
length = len(line)
count += 1
file.close()
output = (stat, length)
with open(inf_data, 'wb') as file:
pickle.dump(output, file)
file.close()
return output
def modify_bio_records(bed, to_min, to_max, to_size, tail):
'''
Модифицирование длины в зависимости от параметров
'''
if to_min:
min_length = minimal_length(bed)
bio_copy = slice_to_size(bed, min_length)
return(bio_copy)
elif to_max:
max_length = maximal_length(bed)
bio_copy = slice_to_size(bed, max_length)
return(bio_copy)
elif not (to_size is None):
to_size = int(to_size)
bio_copy = slice_to_size(bed, to_size)
return(bio_copy)
elif not (tail is None):
tail = int(tail)
bio_copy = add_tail(bed, tail)
return(bio_copy)
else:
return(bed)
def complement(seq):
'''
Make reverse and compelent
'''
output = str()
for letter in seq:
if letter == 'A':
output += 'T'
elif letter == 'C':
output += 'G'
elif letter == 'G':
output += 'C'
elif letter == 'T':
output += 'A'
output = output[::-1]
return(output)
def chek_nucleotides(line):
flag = True
for char in line:
flag = char is 'A' or char is 'C' or char is 'G' or char is 'T'
if not flag:
break
return flag
def bed_to_fasta(path_fasta, path_bed, to_min, to_max, to_size, tail):
bed_peaks = read_bed(path_bed)
bed_peaks = modify_bio_records(bed_peaks, to_min, to_max, to_size, tail)
chromosomes, length = stat_of_fasta_file(path_fasta)
results = []
for i in range(len(bed_peaks)):
rec = {'name': str(), 'chr': str(), 'start': int(), 'end': int(), 'strand': str()}
seq = str() # container for sequnce of peak
chr_name = bed_peaks.iloc[i]['chromosome']
try:
chr_start_line = chromosomes[chr_name]
except:
print('{0} was not found in FASTA'.format(chr_name))
continue
rec['chr'] = bed_peaks.iloc[i]['chromosome']
if bed_peaks.iloc[i]['name'] != '.':
rec['name'] = bed_peaks.iloc[i]['name']
else:
rec['name'] = 'peaks_' + str(i)
if not bed_peaks.iloc[i]['start'] < 0:
rec['start'] = bed_peaks.iloc[i]['start']
else:
continue
if not bed_peaks.iloc[i]['end'] < 0:
rec['end'] = bed_peaks.iloc[i]['end']
else:
continue
if bed_peaks.iloc[i]['strand'] == '.' and isinstance(bed_peaks.iloc[i]['strand'], str):
rec['strand'] = '+'
elif not isinstance(bed_peaks.iloc[i]['strand'], str):
if np.isnan(bed_peaks.iloc[i]['strand']):
rec['strand'] = '+'
else:
rec['strand'] = bed_peaks.iloc[i]['strand']
peak_start_line = bed_peaks.iloc[i]['start'] // length + \
chr_start_line # number of line where seq started
peak_end_line = bed_peaks.iloc[i]['end'] // length + \
chr_start_line # number of line where seq ended
# position number of nucleotide in first line
position_start = bed_peaks.iloc[i]['start'] % length
position_end = bed_peaks.iloc[i]['end'] - bed_peaks.iloc[i]['start'] + position_start
for line_number in range(peak_start_line, peak_end_line + 1):
seq += linecache.getline(path_fasta, line_number).strip()
if chek_nucleotides(seq[position_start:position_end]):
if rec['strand'] == '+':
rec['seq'] = seq[position_start:position_end]
else:
rec['seq'] = complement(seq[position_start:position_end])
else:
continue
results.append(rec)
linecache.clearcache()
return(results)
def bed_to_graph_fasta(record, graph):
record_start = record['start']
record_end = record['end']
record['graph'] = []
value = []
length = len(graph)
#positions = [int(np.searchsorted(graph['start'], coordinate)) for coordinate in range(record_start, record_end)]
#positions = [i - 1 if i == length else i for i in positions]
#value = [str(graph.iloc[i]) for i in positions]
for coordinate in range(record_start, record_end):
pos = int(np.searchsorted(graph['start'], coordinate))
if pos == len(graph):
pos -= 1
value.append(str(graph.iloc[pos]['score']))
#value = [str(graph.iloc[int(np.searchsorted(graph['start'], coordinate))]['score']) \
# for coordinate in range(record_start, record_end)]
record['graph'] = ' '.join(value)
return(record)
def write_fasta(results, path_to_write):
with open(path_to_write, 'w') as file:
for record in results:
file.write('>' + record['name'] + '|' + record['chr'] + '|' +
str(record['start']) + '-' + str(record['end']) + '|' +
record['strand'] + '\n')
file.write(record['seq'] + '\n')
file.close()
pass
def write_wig_fata(results, path_to_write):
with open(path_to_write, 'w') as file:
for record in results:
file.write('>' + record['graph'] + '\n')
file.write(record['seq'] + '\n')
file.close()
pass
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-if', '--inputFasta', action='store', dest='input_fasta',
required=True, help='path to FASTA file')
parser.add_argument('-bed', '--inputBED', action='store', dest='input_bed',
required=True, help='path to BED file')
parser.add_argument('-w', '--bedGraph', action='store', dest='input_graph',
required=False, help='path to bedGraph file to make bedGraph_FASTA file')
parser.add_argument('-of', '--outputFasta', action='store', dest='output_fasta',
required=True, help='path to output Fasta')
parser.add_argument('-m', '--minimize', action='store_true', dest='to_min',
default=False, required=False,
help='With this parametr all peaks will change their length to the length of the shortest sequence in set')
parser.add_argument('-M', '--maximaze', action='store_true', dest='to_max',
default=False, required=False,
help='With this parametr all peaks will change their length to the length of the longest sequence in set')
parser.add_argument('-l', '--length', action='store', dest='length',
default=None, required=False,
help='With this parametr all peaks will change their length to the length equal to the given parameter')
parser.add_argument('-t', '--tail', action='store', dest='tail',
default=None, required=False,
help='With this parametr all peaks will get tail in start and end of sequence')
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
return(parser.parse_args())
def main():
args = parse_args()
input_fasta = args.input_fasta
input_bed = args.input_bed
output_fasta = args.output_fasta
to_min = args.to_min
to_max = args.to_max
to_size = args.length
tail = args.tail
graph_path = args.input_graph
if os.path.isfile(graph_path):
results = bed_to_fasta(input_fasta, input_bed, to_min, to_max, to_size, tail)
graph = read_bed_graph(graph_path)
graph_results = []
results = sorted(results, key=lambda x: x['chr'])
chrs = np.unique([i['chr'] for i in results])
for chr_ in chrs:
sub_results = [record for record in results if record['chr'] == chr_]
sub_graph = graph[graph['chromosome'] == chr_]
graph_results += [bed_to_graph_fasta(record, sub_graph) for record in sub_results]
write_wig_fata(graph_results, output_fasta)
else:
results = bed_to_fasta(input_fasta, input_bed, to_min, to_max, to_size, tail)
write_fasta(results, output_fasta)
if __name__ == '__main__':
main()