-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan_by_dipwm.py
executable file
·230 lines (196 loc) · 7.74 KB
/
scan_by_dipwm.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
'''
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 itertools
import multiprocessing as mp
import functools
import pandas as pd
import numpy as np
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:
# if line.startswith('>'):
# line = line[1:].strip().split('|')
# record = dict()
# record['name'] = line[0]
# record['chromosome'] = line[1]
# record['start'] = line[2].split('-')[0]
# record['end'] = line[2].split('-')[1]
# try:
# record['strand'] = line[3]
# except:
# # print('Record with out strand. Strand is +')
# record['strand'] = '+'
# continue
# record['seq'] = line.strip().upper()
# fasta.append(record)
# file.close()
# return(fasta)
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_dipwm(path):
dipwm = dict()
with open(path, 'r') as file:
inf = file.readline()
di_nucleotides = itertools.product('ACGT', repeat=2)
for i in di_nucleotides:
dipwm[''.join(i)] = []
for line in file:
line = line.strip().split('\t')
for letter, value in zip(dipwm.keys(), line):
dipwm[letter].append(float(value))
file.close()
return(dipwm) # , inf)
def score_dipwm(seq, dipwm):
'''
Вспомагательная функция, считает score для строки с такой же длиной как и PWM
тип diPWM
'''
length_of_seq = len(seq)
score = 0
for i in range(length_of_seq - 1):
score += dipwm[seq[i:i+2]][i]
return(score)
def complement(record):
'''
Make reverse and compelent
'''
output = dict(record)
strand = record['strand']
seq = str()
if strand == '+':
output['strand'] = '-'
else:
output['strand'] = '+'
for letter in output['seq']:
if letter == 'A':
seq += 'T'
elif letter == 'C':
seq += 'G'
elif letter == 'G':
seq += 'C'
elif letter == 'T':
seq += 'A'
output['seq'] = seq[::-1]
return(output)
def scan_seq_by_dipwm(record, dipwm, threshold):
results = []
reverse_record = complement(record)
length_pwm = len(dipwm['AA'])
seq = record['seq']
reverse_seq = reverse_record['seq']
# first strand
for i in range(len(seq) - length_pwm + 2):
site_seq = seq[i:length_pwm + i]
s = score_dipwm(site_seq, dipwm)
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 + 2):
site_seq = reverse_seq[i:length_pwm + i]
s = score_dipwm(site_seq, dipwm)
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 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 diPWM file')
parser.add_argument('-t', '--threshold', action='store', type=float, dest='threshold',
required=True, help='threshold for diPWM')
parser.add_argument('-o', '--output', action='store', dest='output',
required=True, help='path to BED like file for output')
parser.add_argument('-P', '--processes', action='store', type=int, dest='cpu_count',
required=False, default=2, help='Number of processes to use, default: 2')
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
cpu_count = args.cpu_count
fasta = read_fasta(fasta_path)
dipwm = read_dipwm(pwm_path)
# results = []
# for record in fasta:
# results += scan_seq_by_pwm(record, pwm, threshold)
with mp.Pool(cpu_count) as p:
results = p.map(functools.partial(scan_seq_by_dipwm,
dipwm=dipwm, threshold=threshold), fasta)
results = [i for i in results if i != []]
results = [j for sub in results for j in sub]
df = pd.DataFrame(results)
# df['name'] = np.repeat('.', len(df))
# df['score'] = np.repeat(0, len(df))
df = df[['chromosome', 'start', 'end', 'name', 'score', 'strand', 'site']]
# print(df)
df.to_csv(results_path, sep='\t', header=False, index=False)
if __name__ == '__main__':
main()