-
Notifications
You must be signed in to change notification settings - Fork 0
/
cubedist.jl
executable file
·205 lines (175 loc) · 5.96 KB
/
cubedist.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
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
#!/usr/bin/env julia
using ArgParse
using Images
using Printf
using Distances
function parse_commandline()
s = ArgParseSettings()
s.description = "Calculate the pairwise distances (rmsd) between a set of electrostatic maps, contained in Gaussian cube files, and report these distances in CSV and a Mega-compatible formats."
s.version = "1.1"
s.add_version = true
@add_arg_table! s begin
"--output", "-o"
help = "output files base name"
default = "map_distances_rmsd"
"map"
help = "electrostatic potential maps"
required = true
nargs = '*'
end
return parse_args(s)
end
struct GaussianCube
header::String
size::Tuple{Int, Int, Int}
voxels::Array{Float64, 3}
end
"""
parse_cube(filename::String)
Parse a Gaussian cube phimap file, `filename`, containg a electrostatic potential map and return a proper GaussianCube object.
"""
function parse_cube(filename::String)
counter = 0
header = ""
(x, y, z) = (0, 0, 0)
natoms = 1
voxels = Float64[]
open(filename, "r") do input_file
for line in eachline(input_file)
counter += 1
if counter <= 6 + natoms
header *= (line * "\n")
if counter == 3
natoms = parse(Int, split(line)[1])
elseif counter == 4
x = parse(Int, split(line)[1])
elseif counter == 5
y = parse(Int, split(line)[1])
elseif counter == 6
z = parse(Int, split(line)[1])
end
else
line = strip(line)
for field in split(line)
push!(voxels, parse(Float64, field))
end
end
end
end
if x*y*z != length(voxels)
write(stderr, "ERROR: Number of voxels read from $filename does not correspond with map dimensions.")
exit(1)
end
voxels = reshape(voxels, x, y, z)
GaussianCube(header, (x,y,z), voxels)
end
function distance(map1::GaussianCube, map2::GaussianCube)
rmsd1 = rmsd2 = 0
if map1.size != map2.size
rmsd1 = rmsd(map1.voxels, imresize(map2.voxels, map1.size))
rmsd2 = rmsd(map2.voxels, imresize(map1.voxels, map2.size))
else
rmsd1 = rmsd2 = rmsd(map1.voxels, map2.voxels)
end
(rmsd1 + rmsd2)/2
end
function write_csv(filename::String, names::Array{String, 1}, mat::Array{Float64,2})
println("Writing file $filename")
open(filename, "w") do output_file
write(output_file, "maps")
map_names::Array{String, 1} = []
for map_name in names
map_name = splitext(basename(map_name))[1]
push!(map_names, map_name)
write(output_file, ",$map_name")
end
write(output_file, "\n")
for i in 1:size(mat)[1]
for j in 1:size(mat)[2]
if j == 1
write(output_file, "$(map_names[i])")
end
if i > j
write(output_file, ",$(mat[i,j])")
else
write(output_file, ",")
end
end
write(output_file, "\n")
end
end
end
function write_meg(filename::String, names::Array{String,1}, mat::Array{Float64,2})
println("Writing file $filename")
open(filename, "w") do output_file
write(output_file, "#mega\n")
write(output_file, "!Title: Electrostatic map distances;\n")
write(output_file, "!Format DataType=Distance DataFormat=LowerLeft NTaxa=$(length(names));\n")
write(output_file, "!Description\n")
write(output_file, "\tDistance (rmsd) bewteen electrostatic maps calculated with cubdedist\n;\n\n")
map_names::Array{String, 1} = []
for map_name in names
map_name = splitext(basename(map_name))[1]
push!(map_names, map_name)
end
for (i, map_name) in enumerate(map_names)
write(output_file, "[$i] #$map_name\n")
end
write(output_file, "\n[ ")
for i in 1:length(map_names)
@printf(output_file, "%9d", i)
end
write(output_file, " ]\n")
for i in 1:size(mat)[1]
for j in 1:size(mat)[2]
if j == 1
@printf(output_file, "[%2d] ", i)
#write(output_file, "[$i] ")
end
if i > j
@printf(output_file, "%9.3g", mat[i,j])
else
write(output_file, repeat(" ", 9))
end
end
write(output_file, "\n")
end
end
end
function main()
parsed_args = parse_commandline()
unique_files::Array{String, 1} = unique(parsed_args["map"])
n = length(unique_files)
if n < 2
write(stderr, "ERROR: At least two distinct files are required.")
exit(1)
end
rmsd_mat = (zeros(n, n) .- 1.0)
cube1 = nothing
cube2 = nothing
# These cicle has to transverse the lower left corner of the matrix, excluding the diagonal.
for c in 1:(n - 1)
try
cube1 = parse_cube(unique_files[c])
catch
write(stderr, "ERROR: Couldn't parse $(unique_files[c]), please check it is a proper Gaussian cube file.")
exit(1)
end
print("$(unique_files[c]) vs \n")
for r in (c + 1):n
try
cube2 = parse_cube(unique_files[r])
catch
write(stderr, "ERROR: Couldn't parse $(unique_files[r]), please check it is a proper Gaussian cube file.")
exit(1)
end
print("\t$(unique_files[r])... ")
rmsd_mat[r, c] = distance(cube1, cube2)
@printf("%.1f\n", (rmsd_mat[r, c]))
end
end
println()
write_csv(parsed_args["output"]*".csv", unique_files, rmsd_mat)
write_meg(parsed_args["output"]*".meg", unique_files, rmsd_mat)
end
main()