Commit 317e7ede authored by Philippe Veber's avatar Philippe Veber
Browse files

tk: move all range-related functions in its module

parent 03742957
......@@ -184,7 +184,7 @@ let concatenate ?(nmissing = 0) ?seed db n : phylip file =
in
let full_sites_of_alignment (alignment : Phylip.t) =
if alignment.sequence_length mod 3 = 0 then
Codepitk.Utils.int_fold 0 (alignment.sequence_length / 3) ~init:[] ~f:(fun acc j ->
Codepitk.Range.fold 0 (alignment.sequence_length / 3) ~init:[] ~f:(fun acc j ->
let column_is_quasi_full =
at_most_n_failures alignment.items ~n:nmissing ~f:(fun it ->
Option.is_some (aa_at_pos it.sequence j)
......
......@@ -45,7 +45,7 @@ let write_row oc fields =
|> Out_channel.fprintf oc "%s\n"
let write_columns oc res =
Utils.range_iter 0 res.nrows ~f:(fun i ->
Range.iter 0 res.nrows ~f:(fun i ->
Int.to_string i
:: List.map res.cols ~f:(fun (_, col) ->
Option.value_map ~default:"NA" ~f:Float.to_string col.(i)
......
open Core_kernel
open Utils
open Phylogenetics
let write_phenotypes ~newick ~output =
......@@ -34,7 +33,7 @@ let write_genotypes ~alignment ~output =
match Alignment.from_fasta alignment with
| Ok al ->
let ncols = Alignment.ncols al in
range_iter 0 ncols ~f:(write_column_alignment_genotype _oc al)
Range.iter 0 ncols ~f:(write_column_alignment_genotype _oc al)
| Error _ -> failwithf "could not open %s" alignment ())
module Result_file = struct
......
......@@ -182,7 +182,7 @@ module Permutation = struct
let test ?(_B_ = 1_000) d ~statistic =
let _T_ = statistic d in
let resample = resample d in
let n = Utils.range_count 1 _B_ ~f:Float.(fun _ -> statistic (resample ()) > _T_) in
let n = Range.count 1 _B_ ~f:Float.(fun _ -> statistic (resample ()) > _T_) in
let pvalue = float n /. float _B_ in
{ _T_ ; pvalue }
end
......
......@@ -71,7 +71,7 @@ let check_orf seq : (unit, orf_defect) result =
let l = String.length seq in
if l mod 3 <> 0 then Error `Length_not_multiple_of_3
else
Range.find_map (0, String.length seq / 3) ~f:(fun i ->
Range.find_map 0 (String.length seq / 3) ~f:(fun i ->
let seq_pos = i * 3 in
let codon_seq = String.sub seq ~pos:seq_pos ~len:3 in
match check_codon codon_seq with
......
type t = int * int
let find_map (lo, hi) ~f =
let find_map lo hi ~f =
let rec loop i =
if i = hi then None
else
......@@ -9,3 +9,45 @@ let find_map (lo, hi) ~f =
| Some y -> Some y
in
loop lo
let iter a b ~f =
let rec loop i =
if i < b then (
f i ;
loop (i + 1) )
in
loop a
let for_all a b ~f =
let rec loop i = i = b || (f i && loop (i + 1)) in
loop a
let%test "for_all 1..4 <= 10" = for_all 1 4 ~f:(( >= ) 10)
let%test "not (for_all 1..4 <= 2)" =
not @@ for_all 1 4 ~f:(( >= ) 2)
let%test "not (for_all 1..4 <= 0)" =
not @@ for_all 1 4 ~f:(( >= ) 2)
let find a b ~f =
let rec loop i =
if i < b then if f i then Some i else loop (i + 1) else None
in
loop a
let count a b ~f =
let rec loop acc i =
if i < b then loop (if f i then acc + 1 else acc) (i + 1)
else acc
in
loop 0 a
let fold a b ~init ~f =
let rec loop acc i = if i >= b then acc else loop (f acc i) (i + 1) in
loop init a
let for_all a b ~f =
let rec loop i = i >= b && f i && loop (i + 1) in
loop a
......@@ -77,7 +77,7 @@ let row res i =
let write_columns oc res =
let nrows = nrows res in
Utils.range_iter 0 nrows ~f:(fun i ->
Range.iter 0 nrows ~f:(fun i ->
row res i |> line_of_cells |> Out_channel.fprintf oc "%s\n")
let to_file results ~output =
......
......@@ -21,47 +21,6 @@ let tree_from_file ?(alpha = 1.) fn =
)
)
let range_iter a b ~f =
let rec loop i =
if i < b then (
f i ;
loop (i + 1) )
in
loop a
let range_for_all a b ~f =
let rec loop i = i = b || (f i && loop (i + 1)) in
loop a
let%test "range_for_all 1..4 <= 10" = range_for_all 1 4 ~f:(( >= ) 10)
let%test "not (range_for_all 1..4 <= 2)" =
not @@ range_for_all 1 4 ~f:(( >= ) 2)
let%test "not (range_for_all 1..4 <= 0)" =
not @@ range_for_all 1 4 ~f:(( >= ) 2)
let range_find a b ~f =
let rec loop i =
if i < b then if f i then Some i else loop (i + 1) else None
in
loop a
let range_count a b ~f =
let rec loop acc i =
if i < b then loop (if f i then acc + 1 else acc) (i + 1)
else acc
in
loop 0 a
let int_fold a b ~init ~f =
let rec loop acc i = if i >= b then acc else loop (f acc i) (i + 1) in
loop init a
let int_for_all a b ~f =
let rec loop i = i >= b && f i && loop (i + 1) in
loop a
let translate_nucleotide_sequence_whatever_it_takes ?(unknown_char = '-') seq
=
let open Phylogenetics in
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment