-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan_by_pwm.py
executable file
·199 lines (164 loc) · 6.29 KB
/
scan_by_pwm.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
'''
Copyright © 2018 Anton Tsukanov. Contacts: tsukanov@bionet.nsc.ru
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 csv
import multiprocessing as mp
import functools
import re
def read_fasta(path):
'''
Чтение фаста фаила и запись каждых двух строчек в экземпляр класса BioRecord
Все экземпляры хранятся в списке
Функция возвращает список экземпляров класса BioRecord
Шапка для FASTA: >uniq_id::chromosome:start-end(strand)
'''
fasta = list()
with open(path, 'r') as file:
for line in file:
#print(line)
if line.startswith('>'):
line = line[1:].strip().split(':')
record = dict()
record['name'] = line[0]
record['chromosome'] = line[2]
coordinates_strand = line[3]
start, end = re.findall(r'\d*-\d*', coordinates_strand)[0].split('-')
record['start'] = start
record['end'] = end
strand = re.findall(r'\(.\)', coordinates_strand[:-3])
if not strand == []:
record['strand'] = strand[0].strip('()')
else:
record['strand'] = '+'
else:
record['seq'] = line.strip().upper()
fasta.append(record)
file.close()
return(fasta)
def read_pwm(path):
with open(path, 'r') as file:
inf = file.readline()
#inf = inf.strip().split('\t')
#id_pfm = inf[0][1:].strip()
#tf_name = inf[1].split('/')[0].strip()
#tf_db = inf[1].split('/')[1].strip()
#inf = {'id_pfm': id_pfm, 'tf_name': tf_name, 'tf_db': tf_db}
pwm = {'A': [], 'C': [], 'G': [], 'T': []}
for line in file:
line = line.strip().split('\t')
for letter, value in zip(pwm.keys(), line):
pwm[letter].append(float(value))
file.close()
return(pwm) # , inf)
def score(seq, pwm):
length_of_seq = len(seq)
position = 0
score = 0
for letter in seq:
score += pwm[letter][position]
position += 1
return(score)
def complement(record):
output = dict(record)
strand = record['strand']
seq = str()
if strand == '+':
output['strand'] = '-'
else:
output['strand'] = '+'
seq = output['seq'].replace('A', 't').replace('T', 'a').replace('C', 'g').replace('G', 'c').upper()[::-1]
output['seq'] = seq
return(output)
def check_nucleotides(site):
s = set(site)
n = {'A', 'C', 'G', 'T'}
if len(s - n) == 0:
return(True)
else:
return(False)
def scan_seq_by_pwm(record, pwm, threshold):
results = []
reverse_record = complement(record)
length_pwm = len(pwm['A'])
seq = record['seq']
reverse_seq = reverse_record['seq']
# first strand
for i in range(len(seq) - length_pwm + 1):
site_seq = seq[i:length_pwm + i]
if not check_nucleotides(site_seq):
continue
s = score(site_seq, pwm)
if s >= threshold:
site_dict = dict()
site_dict['name'] = record['name']
site_dict['chromosome'] = record['chromosome']
site_dict['start'] = str(int(record['start']) + i)
site_dict['end'] = str(int(record['start']) + i + length_pwm)
site_dict['site'] = site_seq
site_dict['strand'] = record['strand']
site_dict['score'] = s
results.append(site_dict)
# second strand
for i in range(len(seq) - length_pwm + 1):
site_seq = reverse_seq[i:length_pwm + i]
if not check_nucleotides(site_seq):
continue
s = score(site_seq, pwm)
if s >= threshold:
site_dict = dict()
site_dict['name'] = record['name']
site_dict['chromosome'] = record['chromosome']
site_dict['start'] = str(int(record['end']) - i - length_pwm)
site_dict['end'] = str(int(record['end']) - i)
site_dict['site'] = site_seq
site_dict['strand'] = reverse_record['strand']
site_dict['score'] = s
results.append(site_dict)
return(results)
def write_csv(path, data):
with open(path, 'w') as csvfile:
fieldnames = ['chromosome', 'start', 'end', 'name', 'score', 'strand', 'site']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, delimiter='\t')
#writer.writeheader()
for line in data:
writer.writerow(line)
pass
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--fasta', action='store', dest='input_fasta',
required=True, help='path to FASTA file with head: >uniq_id|chromosome|start-end|strand')
parser.add_argument('-m', '--pwm', action='store', dest='input_pwm',
required=True, help='path to PWM file')
parser.add_argument('-t', '--threshold', action='store', type=float, dest='threshold',
required=True, help='threshold for PWM')
parser.add_argument('-o', '--output', action='store', dest='output',
required=True, help='path to BED like file for output')
return(parser.parse_args())
def main():
args = parse_args()
pwm_path = args.input_pwm
fasta_path = args.input_fasta
threshold = args.threshold
results_path = args.output
fasta = read_fasta(fasta_path)
pwm = read_pwm(pwm_path)
results = []
for record in fasta:
results += scan_seq_by_pwm(record, pwm, threshold)
# results = [i for i in results if i != []]
# results = [j for sub in results for j in sub]
write_csv(results_path, results)
if __name__ == '__main__':
main()