-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate_threshold_for_pwm.jl
161 lines (130 loc) · 4.13 KB
/
calculate_threshold_for_pwm.jl
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
import CSV
import DataFrames
import Random
using ArgParse
using Distributed
function read_fasta(path)
container = String[]
for line in eachline(path)
if '>' != line[1]
line = uppercase(line)
container = push!(container, line)
rc_line = reverse_complement(line)
container = push!(container, rc_line)
else
continue
end
end
return(container)
end
@everywhere function calculate_score(site::String, pwm::Dict{Char,Array{Float64, 1}})
score = 0.0
len = length(site)
for (index, nuc) in enumerate(site)
score += pwm[site[index]][index]
end
return(score::Float64)
end
function reverse_complement(site::String)
complement = Char[]
for i in site
if i == 'C'
complement = push!(complement, 'G')
elseif i == 'G'
complement = push!(complement, 'C')
elseif i == 'A'
complement = push!(complement, 'T')
elseif i == 'T'
complement = push!(complement, 'A')
elseif i == 'N'
complement = push!(complement, 'N')
end
end
return(join(reverse(complement)))
end
function read_pwm(path::String)
pwm = Dict('A' => Float64[],
'C' => Float64[],
'G' => Float64[],
'T' => Float64[])
open(path) do file
for ln in eachline(file)
if ln[1] == '>'
continue
else
ln = split(ln)
pwm['A'] = push!(pwm['A'], parse(Float64, ln[1]))
pwm['C'] = push!(pwm['C'], parse(Float64, ln[2]))
pwm['G'] = push!(pwm['G'], parse(Float64, ln[3]))
pwm['T'] = push!(pwm['T'], parse(Float64, ln[4]))
end
end
end
return(pwm)
end
@everywhere function scan_peak(peak::String, len_of_site::Int64, pwm::Dict{Char,Array{Float64, 1}})
scores = Float64[]
len = length(peak)
for i in 1:len - len_of_site
site = peak[i:i + len_of_site - 1]
if 'N' in site
continue
end
scores = push!(scores, calculate_score(site, pwm))
end
return(scores)
end
function scan_peaks(peaks::Array{String, 1}, len_of_site::Int64, pwm::Dict{Char,Array{Float64, 1}})
l = length(peaks)
scores = Array{Array{Float64, 1}, 1}()
Threads.@threads for peak in peaks
push!(scores, scan_peak(peak::String, len_of_site::Int64, pwm::Dict{Char,Array{Float64, 1}}))
end
return(reduce(vcat, scores::Array{Array{Float64, 1}, 1}))
end
function calculate_thresholds(peaks::Array{String, 1}, pwm::Dict{Char,Array{Float64, 1}}, len_of_site::Int64)
scores = map(peak::String -> scan_peak(peak::String, len_of_site::Int64, pwm::Dict{Char,Array{Float64, 1}}), peaks)
scores = reduce(vcat, scores::Array{Array{Float64, 1}, 1});
scores = sort!(scores::Array{Float64, 1}, rev=true)
# scores = scan_peaks(peaks, len_of_site::Int64, pwm::Dict{Char,Array{Float64, 1}});
# scores = sort!(scores::Array{Float64, 1}, rev=true)
fpr_actual = Float64[]
fpr = Float64[]
scores_to_write = Float64[]
scores_length = length(scores)
for i in [5e-3,1e-3,5e-4,1e-4,5e-5,1e-5]
s = scores[Int(round(scores_length * i))]
#fpr_actual = push!(fpr_actual, sum(res .>= s) / res_length)
fpr = push!(fpr, i)
scores_to_write = push!(scores_to_write, s)
end
df = DataFrames.DataFrame(Scores = scores_to_write, FPR = fpr)
return(df)
end
function parse_commandline()
s = ArgParseSettings()
@add_arg_table s begin
"fasta"
help = "path to fasta file"
required = true
"pwm"
help = "path to .pwm file"
required = true
"output"
help = "path to write results"
required = true
end
return parse_args(s)
end
function main()
args = parse_commandline()
fasta_path = args["fasta"]
pwm_path = args["pwm"]
output = args["output"]
pwm = read_pwm(pwm_path);
len_of_site = length(pwm['A']);
peaks = read_fasta(fasta_path);
res = calculate_thresholds(peaks, pwm, len_of_site)
CSV.write(output, res, delim='\t')
end
main()