cmatkhan commited on
Commit
c6655e6
·
1 Parent(s): 665b219

updating script

Browse files
Files changed (1) hide show
  1. scripts/dto_preparation.R +88 -29
scripts/dto_preparation.R CHANGED
@@ -342,11 +342,12 @@ all_pr_dtos = list(
342
 
343
  # Write out DTO ranked lists
344
 
345
- results_basedir = "~/htcf_local/dto"
 
 
 
346
 
347
- write_out_pr_dto_lists = function(pr_dataset_name, binding_pr_set_name, all_pr_dtos_list) {
348
-
349
- output_path = file.path(results_basedir, pr_dataset_name)
350
 
351
  binding_pr_set = all_pr_dtos_list[[pr_dataset_name]][[binding_pr_set_name]]
352
 
@@ -395,8 +396,11 @@ write_out_pr_dto_lists = function(pr_dataset_name, binding_pr_set_name, all_pr_d
395
  }
396
 
397
  # Generalized function to write background
398
- write_pr_background = function(pr_dataset_name, binding_pr_set_name, background_list) {
399
- output_path = file.path(results_basedir, pr_dataset_name)
 
 
 
400
 
401
  background_list[[binding_pr_set_name]] %>%
402
  write_csv(file.path(output_path, binding_pr_set_name, "background.csv"),
@@ -404,32 +408,62 @@ write_pr_background = function(pr_dataset_name, binding_pr_set_name, background_
404
  }
405
 
406
  # Generalized function to create lookups
407
- create_pr_lookups = function(pr_dataset_name, binding_pr_set_name, all_pr_dtos_list, scratch_path = "/scratch/mblab/chasem/dto") {
 
 
 
 
 
 
 
 
408
 
409
- lookup_df = all_pr_dtos_list[[pr_dataset_name]][[binding_pr_set_name]]$binding %>%
410
  ungroup() %>%
411
  dplyr::select(sample_id, regulator_locus_tag) %>%
412
  distinct() %>%
413
- dplyr::rename(binding_id = sample_id) %>%
414
- left_join(all_pr_dtos_list[[pr_dataset_name]][[binding_pr_set_name]]$pr %>%
415
- ungroup() %>%
416
- dplyr::select(sample_id, regulator_locus_tag) %>%
417
- distinct() %>%
418
- dplyr::rename(pr_id = sample_id)) %>%
419
- mutate(binding = file.path(scratch_path, pr_dataset_name,
420
- binding_pr_set_name, "binding",
421
- paste0(binding_id, ".csv")),
422
- pr_effect = file.path(scratch_path, pr_dataset_name,
423
- binding_pr_set_name, "pr", "effect",
424
- paste0(pr_id, ".csv")),
425
- pr_pvalue = file.path(scratch_path, pr_dataset_name,
426
- binding_pr_set_name, "pr", "pvalue",
427
- paste0(pr_id, ".csv"))) %>%
 
 
 
 
 
 
 
 
 
428
  select(binding, pr_effect, pr_pvalue)
429
 
430
- return(lookup_df)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
431
  }
432
-
433
  # Create background lists for all PR datasets (if not already created)
434
  all_pr_backgrounds = map(names(all_pr_dtos), ~{
435
  map(binding_data, function(bd) {
@@ -443,25 +477,50 @@ all_pr_backgrounds = map(names(all_pr_dtos), ~{
443
  names(all_pr_backgrounds) = names(all_pr_dtos)
444
 
445
  # Write out all DTOs for all PR datasets
 
 
 
446
  for (pr_name in names(all_pr_dtos)) {
 
 
447
  for (binding_name in names(all_pr_dtos[[pr_name]])) {
448
  write_out_pr_dto_lists(pr_name, binding_name, all_pr_dtos)
449
  write_pr_background(pr_name, binding_name, all_pr_backgrounds[[pr_name]])
450
 
451
- create_pr_lookups(pr_name, binding_name, all_pr_dtos) %>%
452
- write_tsv(file.path(results_basedir, pr_name, binding_name, "lookup.txt"),
 
 
 
 
453
  col_names = FALSE)
 
 
 
 
 
 
454
  }
455
  }
456
 
457
- dto_results_path_list = list.files(results_basedir,
 
 
 
 
 
 
 
 
 
 
458
  "*.json",
459
  recursive = TRUE)
460
 
461
  ## Parse the results -- note that this needs to be adjusted for the
462
  ## expanded set of perturbation response
463
 
464
- dto_results_frames_list = map(file.path(results_basedir, dto_results_path_list),
465
  ~as_tibble(jsonlite::read_json(.x)))
466
  names(dto_results_frames_list) = dto_results_path_list
467
  dto_results_frame = bind_rows(dto_results_frames_list, .id = 'path')
 
342
 
343
  # Write out DTO ranked lists
344
 
345
+ write_out_pr_dto_lists = function(pr_dataset_name,
346
+ binding_pr_set_name,
347
+ all_pr_dtos_list,
348
+ base_outdir=here("results/dto")) {
349
 
350
+ output_path = file.path(base_outdir, pr_dataset_name)
 
 
351
 
352
  binding_pr_set = all_pr_dtos_list[[pr_dataset_name]][[binding_pr_set_name]]
353
 
 
396
  }
397
 
398
  # Generalized function to write background
399
+ write_pr_background = function(pr_dataset_name,
400
+ binding_pr_set_name,
401
+ background_list,
402
+ base_outdir = here("results/dto")) {
403
+ output_path = file.path(base_outdir, pr_dataset_name)
404
 
405
  background_list[[binding_pr_set_name]] %>%
406
  write_csv(file.path(output_path, binding_pr_set_name, "background.csv"),
 
408
  }
409
 
410
  # Generalized function to create lookups
411
+ create_pr_lookups = function(pr_dataset_name, binding_pr_set_name,
412
+ all_pr_dtos_list,
413
+ scratch_path = "/scratch/mblab/chasem/dto") {
414
+ # Get binding and PR sample IDs
415
+ binding_samples = all_pr_dtos_list[[pr_dataset_name]][[binding_pr_set_name]]$binding %>%
416
+ ungroup() %>%
417
+ dplyr::select(sample_id, regulator_locus_tag) %>%
418
+ distinct() %>%
419
+ dplyr::rename(binding_id = sample_id)
420
 
421
+ pr_samples = all_pr_dtos_list[[pr_dataset_name]][[binding_pr_set_name]]$pr %>%
422
  ungroup() %>%
423
  dplyr::select(sample_id, regulator_locus_tag) %>%
424
  distinct() %>%
425
+ dplyr::rename(pr_id = sample_id)
426
+
427
+ # Full join to identify incomplete cases - use relationship = "many-to-many"
428
+ lookup_df = binding_samples %>%
429
+ full_join(pr_samples, by = "regulator_locus_tag", relationship = "many-to-many") %>%
430
+ mutate(binding = if_else(!is.na(binding_id),
431
+ file.path(scratch_path, pr_dataset_name,
432
+ binding_pr_set_name, "binding",
433
+ paste0(binding_id, ".csv")),
434
+ NA_character_),
435
+ pr_effect = if_else(!is.na(pr_id),
436
+ file.path(scratch_path, pr_dataset_name,
437
+ binding_pr_set_name, "pr", "effect",
438
+ paste0(pr_id, ".csv")),
439
+ NA_character_),
440
+ pr_pvalue = if_else(!is.na(pr_id),
441
+ file.path(scratch_path, pr_dataset_name,
442
+ binding_pr_set_name, "pr", "pvalue",
443
+ paste0(pr_id, ".csv")),
444
+ NA_character_))
445
+
446
+ # Separate complete and incomplete cases
447
+ complete_lookup = lookup_df %>%
448
+ filter(!is.na(binding_id) & !is.na(pr_id)) %>%
449
  select(binding, pr_effect, pr_pvalue)
450
 
451
+ incomplete_after_filtering = lookup_df %>%
452
+ filter(is.na(binding_id) | is.na(pr_id)) %>%
453
+ mutate(missing_type = case_when(
454
+ is.na(binding_id) & is.na(pr_id) ~ "both",
455
+ is.na(binding_id) ~ "binding",
456
+ is.na(pr_id) ~ "pr",
457
+ TRUE ~ "unknown"
458
+ )) %>%
459
+ select(regulator_locus_tag, binding_id, pr_id, missing_type) %>%
460
+ distinct() # Add distinct here too to avoid duplicate incomplete rows
461
+
462
+ return(list(
463
+ lookup = complete_lookup,
464
+ incomplete_after_filtering = incomplete_after_filtering
465
+ ))
466
  }
 
467
  # Create background lists for all PR datasets (if not already created)
468
  all_pr_backgrounds = map(names(all_pr_dtos), ~{
469
  map(binding_data, function(bd) {
 
477
  names(all_pr_backgrounds) = names(all_pr_dtos)
478
 
479
  # Write out all DTOs for all PR datasets
480
+ lookup_results = list()
481
+
482
+ dto_input_outdir = here("results/dto")
483
  for (pr_name in names(all_pr_dtos)) {
484
+ lookup_results[[pr_name]] = list()
485
+
486
  for (binding_name in names(all_pr_dtos[[pr_name]])) {
487
  write_out_pr_dto_lists(pr_name, binding_name, all_pr_dtos)
488
  write_pr_background(pr_name, binding_name, all_pr_backgrounds[[pr_name]])
489
 
490
+ lookup_result = create_pr_lookups(pr_name, binding_name, all_pr_dtos)
491
+ lookup_results[[pr_name]][[binding_name]] = lookup_result
492
+
493
+ # Write complete lookups only
494
+ lookup_result$lookup %>%
495
+ write_tsv(file.path(dto_input_outdir, pr_name, binding_name, "lookup.txt"),
496
  col_names = FALSE)
497
+
498
+ # Write incomplete cases for reference
499
+ if (nrow(lookup_result$incomplete_after_filtering) > 0) {
500
+ lookup_result$incomplete_after_filtering %>%
501
+ write_csv(file.path(dto_input_outdir, pr_name, binding_name, "incomplete.csv"))
502
+ }
503
  }
504
  }
505
 
506
+ # Summary of incomplete cases across all datasets
507
+ incomplete_summary = map_dfr(names(lookup_results), ~{
508
+ map_dfr(names(lookup_results[[.x]]), function(binding_name) {
509
+ lookup_results[[.x]][[binding_name]]$incomplete_after_filtering %>%
510
+ mutate(pr_dataset = .x, binding_dataset = binding_name)
511
+ })
512
+ })
513
+
514
+ print(incomplete_summary %>% count(pr_dataset, binding_dataset, missing_type))
515
+
516
+ dto_results_path_list = list.files(base_outdir,
517
  "*.json",
518
  recursive = TRUE)
519
 
520
  ## Parse the results -- note that this needs to be adjusted for the
521
  ## expanded set of perturbation response
522
 
523
+ dto_results_frames_list = map(file.path(base_outdir, dto_results_path_list),
524
  ~as_tibble(jsonlite::read_json(.x)))
525
  names(dto_results_frames_list) = dto_results_path_list
526
  dto_results_frame = bind_rows(dto_results_frames_list, .id = 'path')