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
module A = BatArray
module L = BatList
module Log = Dolog.Log
module Bstree = struct
include Bst.Bisec_tree.Make (FpMol)
let of_molecules l =
create 1 Two_bands (A.of_list l)
end
let nearest_neighbor_names ncores bst_fns mols =
match bst_fns with
| [] -> []
| fn :: fns ->
let annot_mols =
Log.info "loading %s..." fn;
let (bst: Bstree.t) = Utls.restore fn in
Parany.Parmap.parmap ncores
(fun mol ->
let nn, dist = Bstree.nearest_neighbor mol bst in
(mol, FpMol.get_name nn, dist)
) mols in
L.fold_left (fun annotated bst_fn ->
Log.info "loading %s..." bst_fn;
let (bst: Bstree.t) = Utls.restore bst_fn in
Parany.Parmap.parmap ncores (fun (mol, nn_name, dist) ->
if dist = 0.0 then
(mol, nn_name, dist)
else
let curr_nn, curr_dist = Bstree.nearest_neighbor mol bst in
if curr_dist < dist then
(mol, FpMol.get_name curr_nn, curr_dist)
else
(mol, nn_name, dist)
) annotated
) annot_mols fns
let bst_nearest_name_dist bst mol =
let nn, dist = Bstree.nearest_neighbor mol bst in
(FpMol.get_name nn, dist)
let nearest_neighbor_names_a
(ncores: int) (bst_fns: string list) (mols_a: FpMol.t array)
: (FpMol.t * string * float) array =
match bst_fns with
| [] -> [||]
| fn :: fns' ->
let annot_mols =
Log.info "loading %s..." fn;
let (bst: Bstree.t) = Utls.restore fn in
Parany.Parmap.array_parmap ncores
(fun mol ->
let name, dist = bst_nearest_name_dist bst mol in
(mol, name, dist))
(mols_a.(0), "", 1.0)
mols_a in
let fns = A.of_list fns' in
let () =
Parany.run ncores
~demux:(
let i = ref 1 in
let n = A.length fns in
fun () ->
if !i < n then
let res = !i in
incr i;
res
else
raise Parany.End_of_input
)
~work:(fun i ->
Log.info "loading %s..." fns.(i);
let (bst: Bstree.t) = Utls.restore fns.(i) in
A.map (bst_nearest_name_dist bst) mols_a
)
~mux:(
let m = A.length mols_a in
fun nearest_name_dists ->
assert(A.length nearest_name_dists = m);
for i = 0 to m - 1 do
let mol, _prev_nearest_name, prev_dist =
A.unsafe_get annot_mols i in
if prev_dist = 0.0 then
()
else
let curr_nearest_name, curr_dist =
A.unsafe_get nearest_name_dists i in
if curr_dist < prev_dist then
A.unsafe_set annot_mols i (mol, curr_nearest_name, curr_dist)
done
) in
annot_mols