post_analyses.ml 22.9 KB
Newer Older
Carine Rey's avatar
Carine Rey committed
1 2 3 4 5 6 7 8
open Core
open Bistro.Std
open Bistro.EDSL
open Bistro_bioinfo.Std
open Bistro_utils
open File_formats
open Convergence_detection

Carine Rey's avatar
Carine Rey committed
9
type plot_trees
Carine Rey's avatar
Carine Rey committed
10
type post_analyses_dir
Carine Rey's avatar
Carine Rey committed
11
type sens_spe_t_choices_plot
Carine Rey's avatar
Carine Rey committed
12

Carine Rey's avatar
Carine Rey committed
13
type t_choices = {
Carine Rey's avatar
Carine Rey committed
14 15
  t_choices_complete: text_file workflow ;
  t_choices_max: text_file workflow ;
Carine Rey's avatar
Carine Rey committed
16
  t_choices_recall09: text_file workflow ;
Carine Rey's avatar
Carine Rey committed
17
  t_choices_plot: text_file workflow ;
Carine Rey's avatar
Carine Rey committed
18
  t_choices_condensed_plot: text_file workflow ;
Carine Rey's avatar
Carine Rey committed
19
  t_choices_condensed_plot_png: text_file workflow ;
Carine Rey's avatar
Carine Rey committed
20
  auc_tsv: text_file workflow ;
21
  rp_plot: text_file workflow ;
Carine Rey's avatar
Carine Rey committed
22
  tree_prefix: string;
23 24
}

25 26 27 28 29 30 31 32 33 34
type auto_t_plot = {
  tree_prefix:string ;
  model_prefix:string ;
  auto_t_plot: svg workflow;
}


type post_analyses_res = {
  t_choices : t_choices option;
  auto_t_plot_l : auto_t_plot list option;
Carine Rey's avatar
Carine Rey committed
35
  dataset_results_l : dataset_res list;
36 37
}

Carine Rey's avatar
Carine Rey committed
38 39 40
type simu_infos = {
  simu_infos: text_file workflow option ;
  model_prefix: string ;
41
  tree_prefix: string ;
42
}
Carine Rey's avatar
Carine Rey committed
43

Carine Rey's avatar
Carine Rey committed
44 45 46 47 48 49
type dn_ds_tsv = {
  dn_ds: Testnh.dn_ds_t;
  model_prefix: string ;
  tree_prefix: string ;
}

Carine Rey's avatar
Carine Rey committed
50 51 52
type reinfered_trees = {
  reinfered_tree_nt : nw workflow ;
  reinfered_tree_aa : nw workflow ;
Carine Rey's avatar
Carine Rey committed
53 54 55 56 57
  input_tree : nhx workflow ;
  tree_prefix : string ;
  model_prefix : string ;
}

Carine Rey's avatar
Carine Rey committed
58
type post_analyses_simu = {
Carine Rey's avatar
Carine Rey committed
59
  simu_infos_l : simu_infos list;
60
  simu_infos_plot : text_file workflow ;
Carine Rey's avatar
Carine Rey committed
61
  trees_plot : text_file workflow ;
Carine Rey's avatar
Carine Rey committed
62
  bl_trees_tsv : text_file workflow ;
Carine Rey's avatar
Carine Rey committed
63
  dn_ds_tsv_l : dn_ds_tsv list;
64
}
Carine Rey's avatar
Carine Rey committed
65 66

let is_hyp ~hyp (dataset_results :dataset_res) =
Carine Rey's avatar
Carine Rey committed
67 68 69
  let model_prefix = dataset_results.model_prefix in
  model_prefix = hyp

70 71
let build_cmd_t_choices (opt_name : string) mr_option  =
  match mr_option with
Carine Rey's avatar
Carine Rey committed
72 73
  | Some x -> [opt opt_name dep x]
  | None -> []
74

75
let make_t_choices ?(tree_prefix="") ~haPCOC_mr 
76
    ~h0_NeG1_mr ~h0_NeG2_mr ~h0_NeG3_mr ~h0_NeG4_mr ~h0_NeG5_mr
77 78 79 80
    ~haPC_NeG1_mr ~haPC_NeG2_mr ~haPC_NeG3_mr ~haPC_NeG4_mr ~haPC_NeG5_mr
    ~haPC_NeG5_NeC_div2_mr
    ~haPC_NeG5_NeC_x2_mr
    ~h0_NeG5_NeC_div2_mr
Carine Rey's avatar
Carine Rey committed
81 82
    ~h0_NeG5_NeC_x2_mr
    ~h0_NeG5_indel_mr
83 84 85 86
    ~haPC_NeG5_NeC_1_mr
    ~haPC_NeG1_NeC_5_mr
    ~h0_NeG1_NeC_5_mr
    ~h0_NeG5_NeC_1_mr
87 88 89 90
    ~haPC_NeG4_NeC_1_mr
    ~haPC_NeG1_NeC_4_mr
    ~h0_NeG1_NeC_4_mr
    ~h0_NeG4_NeC_1_mr
Carine Rey's avatar
Carine Rey committed
91
    ~haPC_NeG5_indel_mr () : post_analyses_dir directory workflow =
92

93
  let env = Env.env_r in
Carine Rey's avatar
Carine Rey committed
94
  let out = dest // "out" in
95
  let cmd_mr = List.map [
96 97 98 99 100 101 102 103 104 105
      ("--H0_NeG1"           , h0_NeG1_mr          );
      ("--H0_NeG2"           , h0_NeG2_mr          );
      ("--H0_NeG3"           , h0_NeG3_mr          );
      ("--H0_NeG4"           , h0_NeG4_mr          );
      ("--H0_NeG5"           , h0_NeG5_mr          );
      ("--HaPC_NeG1"         , haPC_NeG1_mr          );
      ("--HaPC_NeG2"         , haPC_NeG2_mr          );
      ("--HaPC_NeG3"         , haPC_NeG3_mr          );
      ("--HaPC_NeG4"         , haPC_NeG4_mr          );
      ("--HaPC_NeG5"         , haPC_NeG5_mr          );
106 107 108 109
      ("--HaPC_NeG5_NeC_div2_res"       , haPC_NeG5_NeC_div2_mr          );
      ("--HaPC_NeG5_NeC_x2_res"         , haPC_NeG5_NeC_x2_mr          );
      ("--H0_NeG5_NeC_div2_res"         , h0_NeG5_NeC_div2_mr          );
      ("--H0_NeG5_NeC_x2_res"           , h0_NeG5_NeC_x2_mr          );
110 111 112 113
      ("--HaPC_NeG4_NeC_1_res"       , haPC_NeG4_NeC_1_mr          );
      ("--H0_NeG4_NeC_1_res"         , h0_NeG4_NeC_1_mr          );
      ("--HaPC_NeG1_NeC_4_res"       , haPC_NeG1_NeC_4_mr          );
      ("--H0_NeG1_NeC_4_res"         , h0_NeG1_NeC_4_mr          );
114 115 116 117
      ("--HaPC_NeG5_NeC_1_res"       , haPC_NeG5_NeC_1_mr          );
      ("--H0_NeG5_NeC_1_res"         , h0_NeG5_NeC_1_mr          );
      ("--HaPC_NeG1_NeC_5_res"       , haPC_NeG1_NeC_5_mr          );
      ("--H0_NeG1_NeC_5_res"         , h0_NeG1_NeC_5_mr          );
Carine Rey's avatar
Carine Rey committed
118 119
      ("--H0_NeG5_indel_res"         , h0_NeG5_indel_mr);
      ("--HaPC_NeG5_indel_res"           , haPC_NeG5_indel_mr          );
Carine Rey's avatar
Carine Rey committed
120 121 122
      ("--HaPCOC"            , haPCOC_mr      );
    ] ~f:(fun (opt_name, mr_option) -> build_cmd_t_choices opt_name mr_option)
               |> List.concat in
123
  workflow ~descr:("post_analyses.t_choices." ^ tree_prefix) [
124
    docker env (
Carine Rey's avatar
Carine Rey committed
125 126
      and_list [
        mkdir_p dest ;
127
        cmd "Rscript" ([
Carine Rey's avatar
Carine Rey committed
128 129
            [file_dump (string Scripts.calc_t_per_meth) ;
             opt "--out " ident out;
Carine Rey's avatar
Carine Rey committed
130
             opt "--tree_prefix " string tree_prefix;
Carine Rey's avatar
Carine Rey committed
131 132 133
            ] ;
            cmd_mr ;
          ] |> List.concat) ;
134
      ])
Carine Rey's avatar
Carine Rey committed
135 136
  ]

Carine Rey's avatar
Carine Rey committed
137
let make_simu_infos ?(descr="") ?(fna_infos) ~faa ~tree_sc : text_file workflow =
138
  let env = Env.env_pcoc in
Carine Rey's avatar
Carine Rey committed
139 140
  workflow ~descr:("post_analyses.simu_infos." ^ descr) [
    cmd "python" ~env [
141 142 143 144 145
      file_dump (string Scripts.calc_simu_infos) ;
      opt "--faa" dep faa;
      opt "--tree" dep tree_sc;
      option ( opt "--fna_infos" dep) fna_infos;
      opt "--output " ident dest;
Carine Rey's avatar
Carine Rey committed
146 147 148
    ];
  ]

149
let group_simu_infos ~simu_infos_l : simu_infos directory workflow =
150
  let env = Env.env_r in
151
  let cmd_cp_l = List.map simu_infos_l ~f:(fun s ->
152
      match s.simu_infos with
Carine Rey's avatar
Carine Rey committed
153
      | Some w -> [cmd "cp" [dep w ; tmp // (s.tree_prefix ^"@"^ s.model_prefix ^ ".tsv")]]
154 155 156 157 158
      | None -> []
    ) |> List.concat
  in
  let out = dest // "out" in
  workflow ~descr:"post_analyses.plot_simu_infos" [
159
    docker env (
160
      and_list ([
161 162 163 164 165 166 167 168 169 170 171
          [mkdir_p dest];
          [mkdir_p tmp ];
          cmd_cp_l;
          [cmd "Rscript" [
              file_dump (string Scripts.plot_hyp_simu_validation) ;
              opt "--input_dir" ident tmp;
              opt "--out " ident out;
            ];]
        ]
          |> List.concat)
    )
172 173
  ]

Carine Rey's avatar
Carine Rey committed
174
let plot_trees ~reinfered_tree_l : plot_trees directory workflow =
175 176
  let env_r = Env.env_r in
  let env_py = Env.env_py in
Carine Rey's avatar
Carine Rey committed
177
  let cmd_cp_l = List.map reinfered_tree_l ~f:(fun rt -> [
Carine Rey's avatar
Carine Rey committed
178 179
        cmd "cp" [dep rt.reinfered_tree_nt ; tmp // (rt.tree_prefix ^"@"^ rt.model_prefix ^ "@nt.nw")];
        cmd "cp" [dep rt.reinfered_tree_aa ; tmp // (rt.tree_prefix ^"@"^ rt.model_prefix ^ "@aa.nw")];
Carine Rey's avatar
Carine Rey committed
180
        cmd "cp" [dep rt.input_tree ; tmp // (rt.tree_prefix ^"@input_tree.nw")]
Carine Rey's avatar
Carine Rey committed
181 182 183
      ]) |> List.concat
  in
  let out = dest // "out" in
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  workflow ~descr:"post_analyses.plot_trees" ([
    [mkdir_p dest];
    [mkdir_p tmp ];
    cmd_cp_l;
    [cmd "Rscript" ~env:env_r [
        file_dump (string Scripts.plot_trees) ;
        opt "--input_dir" ident tmp;
        opt "--out " ident out;
    ];
    ];
    [cmd "python" ~env:env_py [
        file_dump (string Scripts.get_bl_trees) ;
        opt "--input_dir" ident tmp;
        opt "--out " ident out;
    ];
    ];
    ]|> List.concat)
Carine Rey's avatar
Carine Rey committed
201

202 203 204 205 206 207
let get_merged_results_opt hx = match hx with
  | Some w -> Some w.merged_results
  | None -> None

type res_all_hyp = {
  ha_PCOC_res       : dataset_res option ;
208 209 210 211 212 213 214 215 216 217
  h0_NeG1_res   : dataset_res option ;
  h0_NeG2_res   : dataset_res option ;
  h0_NeG3_res   : dataset_res option ;
  h0_NeG4_res   : dataset_res option ;
  h0_NeG5_res   : dataset_res option ;
  haPC_NeG1_res : dataset_res option ;
  haPC_NeG2_res : dataset_res option ;
  haPC_NeG3_res : dataset_res option ;
  haPC_NeG4_res : dataset_res option ;
  haPC_NeG5_res : dataset_res option ;
218 219 220 221
  haPC_NeG5_NeC_div2_res : dataset_res option ;
  haPC_NeG5_NeC_x2_res   : dataset_res option ;
  h0_NeG5_NeC_div2_res   : dataset_res option ;
  h0_NeG5_NeC_x2_res     : dataset_res option ;
Carine Rey's avatar
Carine Rey committed
222
  
223 224 225 226
  haPC_NeG4_NeC_1_res : dataset_res option ;
  haPC_NeG1_NeC_4_res   : dataset_res option ;
  h0_NeG4_NeC_1_res   : dataset_res option ;
  h0_NeG1_NeC_4_res     : dataset_res option ;
Carine Rey's avatar
Carine Rey committed
227
  
228 229 230 231
  haPC_NeG5_NeC_1_res : dataset_res option ;
  haPC_NeG1_NeC_5_res   : dataset_res option ;
  h0_NeG5_NeC_1_res   : dataset_res option ;
  h0_NeG1_NeC_5_res     : dataset_res option ;
Carine Rey's avatar
Carine Rey committed
232
  
Carine Rey's avatar
Carine Rey committed
233 234
  h0_NeG5_indel_res   : dataset_res option ;
  haPC_NeG5_indel_res : dataset_res option ;
Carine Rey's avatar
Carine Rey committed
235
}
236

237
let make_t_choices_per_couple ~tree_prefix {ha_PCOC_res;
238 239 240 241 242 243
                               h0_NeG1_res; h0_NeG2_res; h0_NeG3_res; h0_NeG4_res; h0_NeG5_res;
                               haPC_NeG1_res;
                               haPC_NeG2_res;
                               haPC_NeG3_res;
                               haPC_NeG4_res;
                               haPC_NeG5_res;
244 245 246 247
                               haPC_NeG5_NeC_div2_res;
                               haPC_NeG5_NeC_x2_res  ;
                               h0_NeG5_NeC_div2_res  ;
                               h0_NeG5_NeC_x2_res    ;
248 249 250 251
                               haPC_NeG4_NeC_1_res;
                               haPC_NeG1_NeC_4_res  ;
                               h0_NeG4_NeC_1_res  ;
                               h0_NeG1_NeC_4_res    ;
252 253 254 255
                               haPC_NeG5_NeC_1_res;
                               haPC_NeG1_NeC_5_res  ;
                               h0_NeG5_NeC_1_res  ;
                               h0_NeG1_NeC_5_res    ;
Carine Rey's avatar
Carine Rey committed
256 257
                               h0_NeG5_indel_res;
                               haPC_NeG5_indel_res;
258
                                } =
259

Carine Rey's avatar
Carine Rey committed
260
  let haPCOC_mr       = get_merged_results_opt ha_PCOC_res in
261

262 263 264 265 266 267 268 269 270 271
  let h0_NeG1_mr   = get_merged_results_opt h0_NeG1_res   in
  let h0_NeG2_mr   = get_merged_results_opt h0_NeG2_res   in
  let h0_NeG3_mr   = get_merged_results_opt h0_NeG3_res   in
  let h0_NeG4_mr   = get_merged_results_opt h0_NeG4_res   in
  let h0_NeG5_mr   = get_merged_results_opt h0_NeG5_res   in
  let haPC_NeG1_mr = get_merged_results_opt haPC_NeG1_res in
  let haPC_NeG2_mr = get_merged_results_opt haPC_NeG2_res in
  let haPC_NeG3_mr = get_merged_results_opt haPC_NeG3_res in
  let haPC_NeG4_mr = get_merged_results_opt haPC_NeG4_res in
  let haPC_NeG5_mr = get_merged_results_opt haPC_NeG5_res in
272 273 274 275 276
  
  let haPC_NeG5_NeC_div2_mr = get_merged_results_opt haPC_NeG5_NeC_div2_res in
  let haPC_NeG5_NeC_x2_mr   = get_merged_results_opt haPC_NeG5_NeC_x2_res   in
  let h0_NeG5_NeC_div2_mr   = get_merged_results_opt h0_NeG5_NeC_div2_res   in
  let h0_NeG5_NeC_x2_mr     = get_merged_results_opt h0_NeG5_NeC_x2_res     in
Carine Rey's avatar
Carine Rey committed
277
  
278 279 280 281
  let haPC_NeG4_NeC_1_mr   = get_merged_results_opt haPC_NeG4_NeC_1_res in
  let haPC_NeG1_NeC_4_mr   = get_merged_results_opt haPC_NeG1_NeC_4_res   in
  let   h0_NeG1_NeC_4_mr   = get_merged_results_opt h0_NeG1_NeC_4_res   in
  let   h0_NeG4_NeC_1_mr   = get_merged_results_opt h0_NeG4_NeC_1_res     in
Carine Rey's avatar
Carine Rey committed
282
  
283 284 285 286
  let haPC_NeG5_NeC_1_mr   = get_merged_results_opt haPC_NeG5_NeC_1_res in
  let haPC_NeG1_NeC_5_mr   = get_merged_results_opt haPC_NeG1_NeC_5_res   in
  let   h0_NeG1_NeC_5_mr   = get_merged_results_opt h0_NeG1_NeC_5_res   in
  let   h0_NeG5_NeC_1_mr   = get_merged_results_opt h0_NeG5_NeC_1_res     in
Carine Rey's avatar
Carine Rey committed
287
  
Carine Rey's avatar
Carine Rey committed
288 289
  let h0_NeG5_indel_mr = get_merged_results_opt h0_NeG5_indel_res in
  let haPC_NeG5_indel_mr = get_merged_results_opt haPC_NeG5_indel_res in
290

291
  make_t_choices ~tree_prefix ~haPCOC_mr 
292
    ~h0_NeG1_mr  
293 294 295 296 297 298 299 300 301
    ~h0_NeG2_mr  
    ~h0_NeG3_mr  
    ~h0_NeG4_mr  
    ~h0_NeG5_mr  
    ~haPC_NeG1_mr
    ~haPC_NeG2_mr
    ~haPC_NeG3_mr
    ~haPC_NeG4_mr
    ~haPC_NeG5_mr
302 303 304 305
    ~haPC_NeG5_NeC_div2_mr
    ~haPC_NeG5_NeC_x2_mr
    ~h0_NeG5_NeC_div2_mr
    ~h0_NeG5_NeC_x2_mr
Carine Rey's avatar
Carine Rey committed
306 307
    ~h0_NeG5_indel_mr
    ~haPC_NeG5_indel_mr
308 309 310 311
    ~haPC_NeG4_NeC_1_mr
    ~haPC_NeG1_NeC_4_mr
    ~h0_NeG1_NeC_4_mr
    ~h0_NeG4_NeC_1_mr
312 313 314 315
    ~haPC_NeG5_NeC_1_mr
    ~haPC_NeG1_NeC_5_mr
    ~h0_NeG1_NeC_5_mr
    ~h0_NeG5_NeC_1_mr
316
    ()
317 318


319
let get_t_choices ~tree_prefix ~(dataset_results_l: dataset_res list) : t_choices option =
320

321
  let ha_PCOC_res           = List.find dataset_results_l (is_hyp ~hyp: "HaPCOC"              ) in
322

323 324 325 326 327 328 329 330 331 332 333
  let h0_NeG1_res   = List.find dataset_results_l (is_hyp ~hyp: "H0_NeG1"               ) in
  let h0_NeG2_res   = List.find dataset_results_l (is_hyp ~hyp: "H0_NeG2"               ) in
  let h0_NeG3_res   = List.find dataset_results_l (is_hyp ~hyp: "H0_NeG3"               ) in
  let h0_NeG4_res   = List.find dataset_results_l (is_hyp ~hyp: "H0_NeG4"               ) in
  let h0_NeG5_res   = List.find dataset_results_l (is_hyp ~hyp: "H0_NeG5"               ) in
  let haPC_NeG1_res = List.find dataset_results_l (is_hyp ~hyp: "HaPC_NeG1"             ) in
  let haPC_NeG2_res = List.find dataset_results_l (is_hyp ~hyp: "HaPC_NeG2"             ) in
  let haPC_NeG3_res = List.find dataset_results_l (is_hyp ~hyp: "HaPC_NeG3"             ) in
  let haPC_NeG4_res = List.find dataset_results_l (is_hyp ~hyp: "HaPC_NeG4"             ) in
  let haPC_NeG5_res = List.find dataset_results_l (is_hyp ~hyp: "HaPC_NeG5"             ) in
  
Carine Rey's avatar
Carine Rey committed
334 335 336 337
  let haPC_NeG5_NeC_div2_res = List.find dataset_results_l (is_hyp ~hyp: "HaPC_NeG5_NeC_div2" ) in
  let haPC_NeG5_NeC_x2_res   = List.find dataset_results_l (is_hyp ~hyp: "HaPC_NeG5_NeC_x2"   ) in
  let h0_NeG5_NeC_div2_res   = List.find dataset_results_l (is_hyp ~hyp: "H0_NeG5_NeC_div2"   ) in
  let h0_NeG5_NeC_x2_res    = List.find dataset_results_l (is_hyp ~hyp: "H0_NeG5_NeC_x2"      ) in
Carine Rey's avatar
Carine Rey committed
338
  
339 340 341 342
  let haPC_NeG4_NeC_1_res = List.find dataset_results_l (is_hyp ~hyp: "HaPC_NeG4_NeC_1" ) in
  let haPC_NeG1_NeC_4_res   = List.find dataset_results_l (is_hyp ~hyp: "HaPC_NeG1_NeC_4"   ) in
  let h0_NeG4_NeC_1_res   = List.find dataset_results_l (is_hyp ~hyp: "H0_NeG4_NeC_1"   ) in
  let h0_NeG1_NeC_4_res    = List.find dataset_results_l (is_hyp ~hyp: "H0_NeG1_NeC_4"      ) in
Carine Rey's avatar
Carine Rey committed
343
  
344 345 346 347
  let haPC_NeG5_NeC_1_res = List.find dataset_results_l (is_hyp ~hyp: "HaPC_NeG5_NeC_1" ) in
  let haPC_NeG1_NeC_5_res   = List.find dataset_results_l (is_hyp ~hyp: "HaPC_NeG1_NeC_5"   ) in
  let h0_NeG5_NeC_1_res   = List.find dataset_results_l (is_hyp ~hyp: "H0_NeG5_NeC_1"   ) in
  let h0_NeG1_NeC_5_res    = List.find dataset_results_l (is_hyp ~hyp: "H0_NeG1_NeC_5"      ) in
Carine Rey's avatar
Carine Rey committed
348 349 350

  let h0_NeG5_indel_res   = List.find dataset_results_l (is_hyp ~hyp: "H0_NeG5_0.33_i"        ) in
  let haPC_NeG5_indel_res    = List.find dataset_results_l (is_hyp ~hyp: "HaPC_NeG5_0.33_i"   ) in
351
  
352

353 354
  match (h0_NeG1_res) with
  | ( Some h0_NeG1) ->
355
    let t_choices_dir = make_t_choices_per_couple ~tree_prefix {ha_PCOC_res;
356
                                                   h0_NeG1_res; h0_NeG2_res; h0_NeG3_res; h0_NeG4_res; h0_NeG5_res;
357
                                                   haPC_NeG1_res; haPC_NeG2_res;haPC_NeG3_res; haPC_NeG4_res; haPC_NeG5_res;
Carine Rey's avatar
Carine Rey committed
358
                                                   haPC_NeG5_NeC_div2_res; haPC_NeG5_NeC_x2_res; h0_NeG5_NeC_div2_res; h0_NeG5_NeC_x2_res;
359
                                                   haPC_NeG4_NeC_1_res; haPC_NeG1_NeC_4_res; h0_NeG1_NeC_4_res; h0_NeG4_NeC_1_res;
360
                                                   haPC_NeG5_NeC_1_res; haPC_NeG1_NeC_5_res; h0_NeG1_NeC_5_res; h0_NeG5_NeC_1_res;
Carine Rey's avatar
Carine Rey committed
361
                                                   h0_NeG5_indel_res;haPC_NeG5_indel_res}  in
Carine Rey's avatar
Carine Rey committed
362 363
    let t_choices_max = t_choices_dir / selector ["out.max_MCC_per_meth.tsv"] in
    let t_choices_recall09 = t_choices_dir / selector ["out.recall09_per_meth.tsv"] in
364 365
    let t_choices_complete = t_choices_dir / selector ["out.complete.tsv"] in
    let t_choices_plot = t_choices_dir / selector ["out.pdf"] in
Carine Rey's avatar
Carine Rey committed
366
    let t_choices_condensed_plot = t_choices_dir / selector ["out_condensed.pdf"] in
Carine Rey's avatar
Carine Rey committed
367
    let t_choices_condensed_plot_png = t_choices_dir / selector ["out_condensed.png"] in
368
    let rp_plot = t_choices_dir / selector ["out_condensed.recall_precision_ok.pdf"] in
Carine Rey's avatar
Carine Rey committed
369
    let auc_tsv = t_choices_dir / selector ["out.auc_condensed.tsv"] in
370
    let tree_prefix = h0_NeG1.tree_prefix in
Carine Rey's avatar
Carine Rey committed
371
    Some {t_choices_max; t_choices_recall09; t_choices_complete ; t_choices_plot; t_choices_condensed_plot;t_choices_condensed_plot_png;rp_plot; auc_tsv; tree_prefix}
372
  | _ -> None
Carine Rey's avatar
Carine Rey committed
373

374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394


let plot_det_meth_res_auto_t ~t_choices ~dataset_results_l =
  match t_choices with
  | None -> None
  | Some w -> Some (
      List.map dataset_results_l ~f:(fun (dataset_results : dataset_res) ->
          let tree_prefix = dataset_results.tree_prefix in
          let model_prefix = dataset_results.model_prefix in
          let ready_dataset = dataset_results.dataset.dataset in
          let tree = Tree_dataset.tree ready_dataset.tree_dataset `Detection in
          let faa = ready_dataset.faa in
          let tsv = dataset_results.merged_results in
          let res_by_tools = dataset_results.res_by_tools in
          let plot_all_sites = true in
          let t_choices = w.t_choices_max in
          let auto_t_plot = plot_merge_results ~t_choices ~plot_all_sites ~res_by_tools ~tree ~faa ~tsv () in
          {tree_prefix; model_prefix ; auto_t_plot}
        )
    )

Carine Rey's avatar
Carine Rey committed
395 396 397
let get_simu_infos ~(dataset:Dataset.t) =
  let model_prefix = dataset.model_prefix in
  let ready_dataset = dataset.dataset in
Carine Rey's avatar
Carine Rey committed
398 399 400 401
  let faa = ready_dataset.faa in
  let tree_sc = Tree_dataset.tree ready_dataset.tree_dataset `Detection in
  let fna_infos = ready_dataset.fna_infos in
  match fna_infos with
402 403
  | Some w -> Some (make_simu_infos ~descr:model_prefix ~faa ~tree_sc ~fna_infos:w)
  | None -> None (*make_simu_infos ~faa ~tree_sc*)
Carine Rey's avatar
Carine Rey committed
404 405


406 407
let post_analyses_res_of_dataset_results_l ~tree_prefix ~dataset_results_l =
  let t_choices = get_t_choices ~tree_prefix ~dataset_results_l in
408
  let auto_t_plot_l = plot_det_meth_res_auto_t ~t_choices ~dataset_results_l in
Carine Rey's avatar
Carine Rey committed
409
  {t_choices; auto_t_plot_l;dataset_results_l}
Carine Rey's avatar
Carine Rey committed
410 411 412 413 414 415

let post_analyses_simu_of_simu_dataset_l ~simu_dataset_l =
  let simu_infos_l = List.map simu_dataset_l ~f:(fun dataset ->
      {simu_infos = (get_simu_infos ~dataset);
       tree_prefix = dataset.tree_prefix ;
       model_prefix = dataset.model_prefix
416 417
      }
    ) in
Carine Rey's avatar
Carine Rey committed
418 419
  let reinfered_tree_l = List.map simu_dataset_l ~f:(fun dataset ->
      let rd = dataset.dataset in
Carine Rey's avatar
Carine Rey committed
420 421
      let phy_nt = Bppsuite.fna2phy rd.fna in
      let phy_aa = Bppsuite.faa2phy rd.faa in
Carine Rey's avatar
Carine Rey committed
422
      let input_tree = rd.input_tree in
Carine Rey's avatar
Carine Rey committed
423 424 425
      let reinfered_tree_nt = Phyml.phyml_tree ~model:GTR ~tree:input_tree phy_nt in
      let reinfered_tree_aa = Phyml.phyml_tree ~model:LG ~tree:input_tree phy_aa in
      {reinfered_tree_nt; reinfered_tree_aa; input_tree;
Carine Rey's avatar
Carine Rey committed
426 427 428 429
       tree_prefix = dataset.tree_prefix ;
       model_prefix = dataset.model_prefix
      }
    ) in
Carine Rey's avatar
Carine Rey committed
430 431 432 433 434 435 436 437 438 439
  let dn_ds_tsv_l = List.map simu_dataset_l ~f:(fun simu_dataset ->
    let rd = simu_dataset.dataset in
    let tree_prefix = simu_dataset.tree_prefix in
    let model_prefix = simu_dataset.model_prefix in
    let tree =  rd.input_tree in
    let fna  = rd.fna in
    let faa  = rd.faa in
    let dn_ds = Testnh.calc_dn_ds ~faa ~tree ~fna in
    {tree_prefix; model_prefix; dn_ds}
  ) in
440
  let simu_infos_plot = group_simu_infos ~simu_infos_l / selector ["out.pdf"] in
Carine Rey's avatar
Carine Rey committed
441
  let trees_plot = plot_trees ~reinfered_tree_l / selector ["out.pdf"] in
Carine Rey's avatar
Carine Rey committed
442
  let bl_trees_tsv = plot_trees ~reinfered_tree_l / selector ["out.tsv"] in
Carine Rey's avatar
Carine Rey committed
443
  {simu_infos_l; simu_infos_plot; trees_plot; bl_trees_tsv; dn_ds_tsv_l}
Carine Rey's avatar
Carine Rey committed
444

Carine Rey's avatar
Carine Rey committed
445 446


447
let plot_sens_spe_t_choices ~t_choices_l ~dataset_results_l ~profile_prefix : sens_spe_t_choices_plot directory workflow =
448
  let env = Env.env_r in
Carine Rey's avatar
Carine Rey committed
449 450 451 452
  let t_choices_dir = tmp // "t_choices_dir" in
  let merged_results_dir = tmp // "merged_results_dir" in
  let out = dest // "out" in
  let cmd_cp_t_choices_l = List.map t_choices_l ~f:(fun t_choices ->
Carine Rey's avatar
Carine Rey committed
453
      cmd "cp" [dep t_choices.t_choices_recall09 ; t_choices_dir // (t_choices.tree_prefix ^ ".tsv")]
Carine Rey's avatar
Carine Rey committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
    )
  in
  let cmd_cp_merged_results_l = List.map dataset_results_l ~f:(fun dataset_results ->
      cmd "cp" [dep dataset_results.merged_results ; merged_results_dir // (dataset_results.tree_prefix ^"@"^ dataset_results.model_prefix ^ ".tsv")]
    )
  in
  workflow ~descr:"post_analyses.plot_sens_spe_all_trees" [
    docker env (
      and_list ([
          [mkdir_p dest];
          [mkdir_p t_choices_dir ];
          [mkdir_p merged_results_dir ];
          cmd_cp_t_choices_l;
          cmd_cp_merged_results_l;
          [cmd "Rscript" [
              file_dump (string Scripts.plot_sens_spe_all_trees) ;
              opt "--input_dir" ident t_choices_dir;
              opt "--input_dir2" ident merged_results_dir;
472
              opt "--profil" string profile_prefix;
Carine Rey's avatar
Carine Rey committed
473 474 475 476 477 478 479
              opt "--out " ident out;
            ];]
        ]
          |> List.concat)
    )
  ]

480
let repo_post_analyses_all_trees_of_all_post_analyses_per_tree ~profile_prefix ~all_post_analyses_per_tree =
Carine Rey's avatar
Carine Rey committed
481 482 483 484 485 486 487 488 489
  let t_choices_l = List.map all_post_analyses_per_tree ~f:(fun post_analyses_res ->
      match post_analyses_res.t_choices with
      | Some w -> [w]
      | None -> []
    ) |> List.concat
  in
  let dataset_results_l =  List.map all_post_analyses_per_tree ~f:(fun post_analyses_res ->
      post_analyses_res.dataset_results_l) |> List.concat
  in
490
  let sens_spe_t_choices_plot = plot_sens_spe_t_choices  ~t_choices_l ~dataset_results_l ~profile_prefix in
Carine Rey's avatar
Carine Rey committed
491 492 493 494 495 496 497 498 499
  [Repo.[
      item ["sens_spe.tsv"] (sens_spe_t_choices_plot / selector ["out.sens_spe_auto_t.tsv"]);
      item ["all_t_choices.tsv"] (sens_spe_t_choices_plot / selector ["out.t_per_tree.tsv"]);
    ] |> Repo.shift "pdf_tsv" ;
   Repo.[
     item ["sens_spe.pdf"] (sens_spe_t_choices_plot / selector ["out.sens_spe_auto_t.pdf"]);
     item ["all_t_choices.pdf"] (sens_spe_t_choices_plot / selector ["out.t_per_tree.pdf"]);
   ]
  ] |> List.concat
Carine Rey's avatar
Carine Rey committed
500

Carine Rey's avatar
Carine Rey committed
501
let repo_of_post_analyses_simu ~post_analyses_simu =
Carine Rey's avatar
Carine Rey committed
502
  [
503
    Repo.[
Carine Rey's avatar
Carine Rey committed
504 505
      item ["hypothesis_validation.pdf"] post_analyses_simu.simu_infos_plot ;
      item ["trees_validation.pdf"] post_analyses_simu.trees_plot;
Carine Rey's avatar
Carine Rey committed
506
      item ["trees_bl.tsv"] post_analyses_simu.bl_trees_tsv;
Carine Rey's avatar
Carine Rey committed
507
    ]
508
    ;
Carine Rey's avatar
Carine Rey committed
509
    (List.map post_analyses_simu.simu_infos_l ~f:(fun simu_infos ->
510 511 512 513
         match simu_infos.simu_infos with
         | None -> []
         | Some w ->
           Repo.[
Carine Rey's avatar
Carine Rey committed
514
             item [simu_infos.tree_prefix ^ "@" ^ simu_infos.model_prefix ^ ".tsv"] w
Carine Rey's avatar
Carine Rey committed
515 516
           ] |> Repo.shift simu_infos.tree_prefix
           |> Repo.shift "tsv"
517 518
       ) |> List.concat
    );
Carine Rey's avatar
Carine Rey committed
519 520 521 522 523 524 525 526 527
    (List.map post_analyses_simu.dn_ds_tsv_l ~f:(fun dn_ds_tsv ->
           Repo.[
             item [dn_ds_tsv.tree_prefix ^ "@" ^ dn_ds_tsv.model_prefix ^ "@dn.nw"] dn_ds_tsv.dn_ds.dn_tree;
             item [dn_ds_tsv.tree_prefix ^ "@" ^ dn_ds_tsv.model_prefix ^ "@ds.nw"] dn_ds_tsv.dn_ds.ds_tree;
             item [dn_ds_tsv.tree_prefix ^ "@" ^ dn_ds_tsv.model_prefix ^ "@diversity.tsv"] dn_ds_tsv.dn_ds.diversity_tsv;
             item [dn_ds_tsv.tree_prefix ^ "@" ^ dn_ds_tsv.model_prefix ^ "@length.tsv"] dn_ds_tsv.dn_ds.length_tsv;
           ]) |> List.concat
           |> Repo.shift "dn_ds_tsv"
    );
Carine Rey's avatar
Carine Rey committed
528
  ] |> List.concat
Carine Rey's avatar
Carine Rey committed
529
  |> Repo.shift "Simulation_details"
Carine Rey's avatar
Carine Rey committed
530 531

let repo_of_post_analyses_res ~prefix ~post_analyses_res =
532 533 534 535
  [
    (match post_analyses_res.t_choices with
     | None -> []
     | Some w ->
Carine Rey's avatar
Carine Rey committed
536 537 538 539 540 541 542 543 544
       [
         Repo.[
           item [prefix ^ ".t_choices.max_mcc_per_meth.tsv"] w.t_choices_max ;
           item [prefix ^ ".t_choices.recall09_per_meth.tsv"] w.t_choices_recall09 ;
           item [prefix ^ ".t_choices.complete.tsv"] w.t_choices_complete ;
         ] |> Repo.shift "pdf_tsv" ;
         Repo.[
           item [prefix ^ ".t_choices.pdf"] w.t_choices_plot ;
           item [prefix ^ ".t_choices.condensed.pdf"] w.t_choices_condensed_plot ;
Carine Rey's avatar
Carine Rey committed
545
           item [prefix ^ ".t_choices.condensed.png"] w.t_choices_condensed_plot_png ;
546
           item [prefix ^ ".recall_precision_ok.pdf"] w.rp_plot ;
Carine Rey's avatar
Carine Rey committed
547
           item [prefix ^ ".auc_condensed.tsv"] w.auc_tsv ;
Carine Rey's avatar
Carine Rey committed
548 549
         ]
       ] |> List.concat
550
    );
Carine Rey's avatar
Carine Rey committed
551
    (*(
552 553 554 555 556 557 558 559 560 561
      match post_analyses_res.auto_t_plot_l with
      | None -> []
      | Some w_l ->
        List.map w_l ~f:(fun w ->
            Repo.[
              let prefix_f = w.tree_prefix ^ "@" ^ w.model_prefix in
              item [ prefix_f ^ ".auto_t.svg"] w.auto_t_plot ;
            ]
          )|> List.concat
        |> Repo.shift "auto_t_pdf"
Carine Rey's avatar
Carine Rey committed
562
      );*)
563
  ] |> List.concat
Carine Rey's avatar
Carine Rey committed
564