Source file utls.ml
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
open Printf
module A = BatArray
module Ht = BatHashtbl
module L = BatList
module Log = Dolog.Log
module S = BatString
type filename = string
let tap f x =
f x;
x
let fst3 (a, _, _) = a
let create_tmp_filename () =
let res = Filename.temp_file "" "" in
res
let mkfifo (fn: filename): unit =
Unix.mkfifo fn 0o600
let enforce (condition: bool) (err_msg: string): unit =
if not condition then
failwith err_msg
let enforce_f (condition: bool) (msg_constr: unit -> string): unit =
if not condition then
failwith (msg_constr ())
let with_out_file (fn: filename) (f: out_channel -> 'a): 'a =
let output = open_out_bin fn in
let res = f output in
close_out output;
res
let with_temp_out_file (f: filename -> 'a): 'a =
let temp_file = Filename.temp_file "" "" in
let res = f temp_file in
Sys.remove temp_file;
res
let with_in_file (fn: filename) (f: in_channel -> 'a): 'a =
let input = open_in_bin fn in
let res = f input in
close_in input;
res
let with_in_file2 fn1 fn2 f =
let in1 = open_in_bin fn1 in
let in2 = open_in_bin fn2 in
let res = f in1 in2 in
close_in in1;
close_in in2;
res
let with_in_file3 fn1 fn2 fn3 f =
let in1 = open_in_bin fn1 in
let in2 = open_in_bin fn2 in
let in3 = open_in_bin fn3 in
let res = f in1 in2 in3 in
close_in in1;
close_in in2;
close_in in3;
res
let with_infile_outfile (in_fn: filename) (out_fn: filename)
(f: in_channel -> out_channel -> 'a): 'a =
let input = open_in_bin in_fn in
let output = open_out_bin out_fn in
let res = f input output in
close_in input;
close_out output;
res
let lines_of_file (fn: filename): string list =
with_in_file fn (fun input ->
let res, exn = L.unfold_exc (fun () -> input_line input) in
if exn <> End_of_file then
raise exn
else res
)
let lines_to_file fn l =
with_out_file fn (fun out ->
L.iter (fprintf out "%s\n") l
)
let string_list_to_file = lines_to_file
let read_n_lines nlines input =
assert(nlines >= 0);
let rec loop n acc =
if n = 0 then L.rev acc
else loop (n - 1) (input_line input :: acc) in
loop nlines []
let (fn: filename): string option * string list =
let all_lines = lines_of_file fn in
match all_lines with
| [] -> (None, [])
| fst :: others ->
if BatString.starts_with fst "#" then
(Some fst, others)
else
(None, all_lines)
let iter_on_lines_of_file fn f =
let input = open_in_bin fn in
try
while true do
f (input_line input)
done
with End_of_file -> close_in input
let iteri_on_lines_of_file fn f =
let i = ref 0 in
let input = open_in_bin fn in
try
while true do
f !i (input_line input);
incr i
done
with End_of_file -> close_in input
let map_on_file (fn: filename) (f: in_channel -> 'a): 'a list =
with_in_file fn (fun input ->
let res, exn = L.unfold_exc (fun () -> f input) in
if exn = End_of_file then res
else raise exn
)
let map_on_lines_of_file (fn: filename) (f: string -> 'a): 'a list =
with_in_file fn (fun input ->
let res, exn = L.unfold_exc (fun () -> f (input_line input)) in
if exn = End_of_file then res
else raise exn
)
let maybe_map_on_lines_of_file (fn: filename) (f: string -> 'a option): 'a list =
let res = ref [] in
iter_on_lines_of_file fn (fun line ->
match f line with
| Some x -> res := x :: !res
| None -> Log.warn "line: '%s'" line
);
L.rev !res
let mapi_on_lines_of_file (fn: filename) (f: int -> string -> 'a): 'a list =
with_in_file fn (fun input ->
let i = ref 0 in
let res, exn =
L.unfold_exc (fun () ->
let curr = f !i (input_line input) in
incr i;
curr
) in
if exn = End_of_file then res
else raise exn
)
let skip_blocks nb read_one input =
if nb = 0 then ()
else
let () = assert(nb > 0) in
for _ = 1 to nb do
ignore(read_one input)
done
let read_lines = lines_of_file
let write_lines (lines: string list) (output_fn: filename): unit =
with_out_file output_fn (fun out ->
List.iter (fprintf out "%s\n") lines
)
let output_lines = write_lines
let filter_lines_of_file fn p =
L.filter p (lines_of_file fn)
let get_command_output (cmd: string): string =
Log.info "get_command_output: %s" cmd;
let _stat, output = BatUnix.run_and_read cmd in
match BatString.split_on_char '\n' output with
| first_line :: _others -> first_line
| [] -> (Log.fatal "get_command_output: no output for: %s" cmd; exit 1)
let fork_out_cmd (cmd: string): int =
Log.info "fork_out_cmd: %s" cmd;
match Unix.fork () with
| 0 -> exit (Sys.command cmd)
| -1 -> (Log.fatal "fork_out_cmd: fork failed"; exit 1)
| pid -> pid
let command_exists (cmd: string): string option =
let where_is_cmd = "which " ^ cmd in
if Unix.system (where_is_cmd ^ " 2>&1 > /dev/null") = Unix.WEXITED 0 then
Some (get_command_output where_is_cmd)
else
None
let run_command (cmd: string): unit =
Log.info "run_command: %s" cmd;
match Unix.system cmd with
| Unix.WSIGNALED _ -> (Log.fatal "run_command: signaled: %s" cmd; exit 1)
| Unix.WSTOPPED _ -> (Log.fatal "run_command: stopped: %s" cmd; exit 1)
| Unix.WEXITED i when i <> 0 ->
(Log.fatal "run_command: exit %d: %s" i cmd; exit 1)
| Unix.WEXITED _ -> ()
let get_env (env_var: string): string option =
try Some (Sys.getenv env_var)
with Not_found -> None
let find_command (exe: string) (env_var: string): string option =
match command_exists exe with
| Some cmd -> Some cmd
| None ->
match get_env env_var with
| Some cmd -> Some cmd
| None -> (Log.warn "%s not found in PATH; \
put it in your PATH or setup the \
%s env. var. to point to it" exe env_var;
None)
let filename_is_absolute fn =
not (Filename.is_relative fn)
let relative_to_absolute fn =
if Filename.is_relative fn then
let cwd = Sys.getcwd () in
Filename.concat cwd fn
else
fn
let remove_string_prefix prfx str =
if BatString.starts_with str prfx then
let prfx_len = String.length prfx in
BatString.tail str prfx_len
else
str
let string_contains_non_binary_digit = Str.regexp "[^01]"
let string_contains_only_zeros_or_ones (s: string): bool =
not (Str.string_match string_contains_non_binary_digit s 0)
let string_contains_non_digits_non_sep = Str.regexp "[^-0123456789;]"
let string_is_a_list_of_integers (s: string): bool =
BatString.starts_with s "[" &&
BatString.ends_with s "]" &&
let chopped = BatString.chop ~l:1 ~r:1 s in
not (Str.string_match string_contains_non_digits_non_sep chopped 0)
let may_apply_opt f = function
| Some x -> Some (f x)
| None -> None
let may_apply f = function
| Some x -> f x
| None -> ()
let lock_file_for_writing (fn: filename): bool =
try
let fd = Unix.(openfile fn [O_CREAT; O_EXCL; O_WRONLY] 0o600) in
Unix.close fd;
true
with Unix.Unix_error _ -> false
let all_pairs (l: 'a list): ('a * 'a) list =
let pair x ys =
L.map (fun y ->
(x, y)
) ys
in
let rec loop acc = function
| [] -> acc
| x :: xs ->
loop (L.rev_append (pair x xs) acc) xs
in
loop [] l
exception Enough_times
let n_times n f =
let i = ref 0 in
BatList.unfold_exc (fun () ->
if !i = n then raise Enough_times
else
let res = f () in
incr i;
res
)
let push (x: 'a) (l: 'a list ref): unit =
l := x :: !l
let id x = x
let one_or_more_spaces = Str.regexp "[ ]+"
let string_of_floats_array fv =
let buff = Buffer.create 80 in
A.iter (fun f ->
Buffer.add_string buff (sprintf "%f " f)
) fv;
Buffer.contents buff
let enforce_file_extension allowed_exts fn =
assert(L.exists (BatString.ends_with fn) allowed_exts)
let m_pi = 4.0 *. atan 1.0
let prepend x xs =
xs := x :: !xs
let in_bounds lb x hb =
x >= lb && x <= hb
let list_medianf (l: float list): float =
let xs = A.of_list l in
A.sort BatFloat.compare xs;
let n = A.length xs in
if n mod 2 = 1 then
xs.(n/2)
else
0.5 *. (xs.(n/2) +. xs.(n/2 - 1))
let string_of_array ?pre:(pre = "[|") ?sep:(sep = ";") ?suf:(suf = "|]")
to_str a =
let buff = Buffer.create 80 in
Buffer.add_string buff pre;
A.iteri (fun i x ->
if i > 0 then Buffer.add_string buff sep;
Buffer.add_string buff (to_str x)
) a;
Buffer.add_string buff suf;
Buffer.contents buff
let save fn x =
with_out_file fn (fun out ->
Marshal.to_channel out x [Marshal.No_sharing]
)
let restore fn =
if S.ends_with fn ".gz" then
let in_chan = Unix.open_process_in ("gunzip -ck " ^ fn) in
Marshal.from_channel in_chan
else if S.ends_with fn ".bz2" then
let in_chan = Unix.open_process_in ("bunzip2 -ck " ^ fn) in
Marshal.from_channel in_chan
else if S.ends_with fn ".xz" then
let in_chan = Unix.open_process_in ("unxz -ck " ^ fn) in
Marshal.from_channel in_chan
else
with_in_file fn (fun input ->
Marshal.from_channel input
)
let is_odd i =
i mod 2 = 1
let is_even i =
i mod 2 = 0
let get_first_line fn =
with_in_file fn input_line
let cut d f line =
let splitted = BatString.split_on_char d line in
BatList.at splitted f
let get_ncores () =
int_of_string (get_command_output "getconf _NPROCESSORS_ONLN")
let int_of_bool = function
| true -> 1
| false -> 0
let bool_of_int = function
| 1 -> true
| 0 -> false
| _ -> assert(false)
let approx_equal epsilon x y =
(y >= x -. epsilon) &&
(y <= x +. epsilon)
let is_nan x =
match classify_float x with
| FP_nan -> true
| _ -> false
let faverage =
BatList.favg
let fmean = faverage
let stddev (l: float list): float =
let n, sx, sx2 =
List.fold_left (fun (n, sx, sx2) x ->
(n +. 1., sx +. x, sx2 +. (x *.x))
) (0., 0., 0.) l
in
sqrt ((sx2 -. (sx *. sx) /. n) /. n)
let rank (arr: float array): float array =
let sorted = A.copy arr in
A.sort Float.compare sorted;
let n = A.length arr in
let ht = Ht.create n in
let rank = ref 1.0 in
A.iter (fun x ->
try
begin
let ranks = Ht.find ht x in
Ht.replace ht x (!rank :: ranks);
rank := !rank +. 1.0
end
with Not_found ->
begin
Ht.add ht x [!rank];
rank := !rank +. 1.0
end
) sorted;
let elt2rank = Ht.map (fun _elt ranks -> L.favg ranks) ht in
A.iteri (fun i x ->
A.unsafe_set sorted i (Ht.find elt2rank x)
) arr;
sorted
let wilcoxon_rank_sum_to_z arr1 arr2 =
let l1, l2 = A.(length arr1, length arr2) in
let ranked = rank (A.append arr1 arr2) in
let arr1 = A.sub ranked 0 l1 in
let l1, l2 = Float.(of_int l1, of_int l2) in
let sum1 = A.fsum arr1 in
let expectation = (l1 *. (l1 +. l2 +. 1.)) /. 2. in
let var = (l1 *. l2 *. ((l1 +. l2 +. 1.) /. 12.)) in
(sum1 -. expectation) /. (sqrt var)
let cnd x =
let b1, b2, b3, b4, b5, p, c =
0.319381530, -0.356563782, 1.781477937, -1.821255978,
1.330274429, 0.2316419, 0.39894228 in
if x >= 0.0 then
let t = 1. /. (1. +. (p *. x)) in
(1. -. (c *. (exp (-.x *. x /. 2.)) *. t *.
(t *. (t *. (t *. ((t *. b5) +. b4) +. b3) +. b2) +. b1)))
else
let t = 1. /. (1. -. p *. x) in
c *. (exp (-.x *. x /. 2.)) *. t *.
(t *. (t *. (t *. ((t *. b5) +. b4) +. b3) +. b2) +. b1)
let wilcoxon_rank_sum_to_p arr1 arr2 =
let z = wilcoxon_rank_sum_to_z arr1 arr2 in
2. *. (1. -. (cnd (Float.abs z)))
let fincr_by (xref: float ref) (dx: float): unit =
xref := !xref +. dx
let int_of_digit_char = function
| '0' -> 0
| '1' -> 1
| '2' -> 2
| '3' -> 3
| '4' -> 4
| '5' -> 5
| '6' -> 6
| '7' -> 7
| '8' -> 8
| '9' -> 9
| c -> failwith (sprintf "Utls.int_of_digit_char: not a digit: %c" c)
let char_of_digit = function
| 0 -> '0'
| 1 -> '1'
| 2 -> '2'
| 3 -> '3'
| 4 -> '4'
| 5 -> '5'
| 6 -> '6'
| 7 -> '7'
| 8 -> '8'
| 9 -> '9'
| d -> failwith (sprintf "Utls.char_of_digit: not a digit: %d" d)
let string_of_digit d =
String.make 1 (char_of_digit d)
let make_pair x y =
(x, y)
let list_uniq_count l =
let sorted = L.sort compare l in
let groups = L.group_consecutive (=) sorted in
L.map (fun l -> L.(hd l, length l)) groups
let time_it f =
let start = Unix.gettimeofday () in
let res = f () in
let stop = Unix.gettimeofday () in
(stop -. start, res)
let ceili (x: float): int =
int_of_float (ceil x)
let count_lines_of_file (fn: string): int =
let count = ref 0 in
iter_on_lines_of_file fn (fun _line -> incr count);
!count
let list_rev_sort cmp l =
List.sort (fun x y -> cmp y x) l
let list_really_take n l =
let took = L.take n l in
assert(L.length took = n);
took
let array_rand_elt rng a =
let n = A.length a in
let i = BatRandom.State.int rng n in
a.(i)
let prepend_list_with_array a l =
Array.fold_right (fun x acc ->
x :: acc
) a l
let array_without_elt_at i a =
let n = (Array.length a) - 1 in
let res = Array.make n a.(0) in
let dest = ref 0 in
for j = 0 to n do
if j <> i then
begin
res.(!dest) <- a.(j);
incr dest
end
done;
res
let list_really_remove_one l x =
assert(L.mem x l);
L.remove l x
let array_prepend_to_list a l =
A.fold_right (fun x acc -> x :: acc) a l
let string_array_concat sep a =
let buff = Buffer.create 1024 in
A.iteri (fun i s ->
if i > 0 then Buffer.add_string buff sep;
Buffer.add_string buff s
) a;
Buffer.contents buff
let erfcc x =
let z = abs_float x in
let t = 2. /. (2. +. z) in
let ans = t *. exp
(-.z *. z -. 1.26551223 +. t *.
( 1.00002368 +. t *.
( 0.37409196 +. t *.
( 0.09678418 +. t *.
(-.0.18628806 +. t *.
( 0.27886807 +. t *.
(-.1.13520398 +. t *.
( 1.48851587 +. t *.
(-.0.82215223 +. t *.
0.17087277))))))))) in
if x >= 0.0 then ans
else 2.0 -. ans
let pearson_a a1 a2 =
let tiny = 1.0e-20 in
let n = A.length a1 in
let nf = float_of_int n in
if n <> A.length a2 then
failwith "my_utils.ml: pearson_a: arrays length differ"
else
let p = A.favg a1 in
let q = A.favg a2 in
let sum_xx = ref 0. in
let sum_yy = ref 0. in
let sum_xy = ref 0. in
let process x' y' =
let x = x' -. p in
let y = y' -. q in
let xx = x *. x in
let yy = y *. y in
let xy = x *. y in
sum_xx := !sum_xx +. xx;
sum_yy := !sum_yy +. yy;
sum_xy := !sum_xy +. xy;
in
for i = 0 to n - 1 do
process a1.(i) a2.(i);
done;
let r = !sum_xy /. (sqrt(!sum_xx *. !sum_yy) +. tiny) in
let z = 0.5 *. log((1.0 +. r +. tiny) /. (1.0 -. r +. tiny)) in
let t = erfcc(abs_float(z *. sqrt(nf -. 1.0)) /. 1.4142136) in
(r, t)
let spearman_rank arr =
let arr = Array.copy arr in
let arr = Array.mapi (fun i a -> a,i) arr in
Array.sort (fun (a,_) (b,_) -> BatFloat.compare a b) arr;
let g _prev il ans =
let count = List.length il in
let n = count + (List.length ans) in
let hi = float_of_int n in
let lo = float_of_int (n - count + 1) in
let rank = (hi +. lo) /. 2. in
(List.map (fun i -> rank,i) il) @ ans
in
let f (prev, il, ans) (x,i) =
let count = List.length il in
if count = 0 then
x, [i], ans
else if x = prev then
x, i::il, ans
else
x, [i], g prev il ans
in
let prev,il,ans = Array.fold_left f (0.,[],[]) arr in
let ans = g prev il ans in
let ans = List.sort (fun (_,a) (_,b) -> BatInt.compare a b) ans in
Array.of_list (List.map fst ans)
let spearman_a (a1:float array) (a2:float array) =
pearson_a (spearman_rank a1) (spearman_rank a2)
let spearman_l l1 l2 =
spearman_a (A.of_list l1) (A.of_list l2)
let rm_file fn =
if Sys.file_exists fn then
Sys.remove fn