Révision 0303dbe8 src/R/nucleominer.R

b/src/R/nucleominer.R
819 819
### This function is based on sort of lower bounds to detect regions that intersect. We compare lower bound and upper bound of the porevious item. This function embed a while loop and break break regions list become stable.
820 820
regions ##<< The Regions to be aggregated
821 821
) {
822
  if (nrow(regions) == 0) {return(regions)}
822 823
  old_length = length(regions[,1])
823 824
  new_length = 0
824 825

  
......
848 849
  return(union)
849 850
}
850 851

  
851
remove_aligned_wp = function(# Remove wp nucs from common nucs list.
852
### It is based on common wp nucs index on nucs and region.
853
strain_maps, ##<< Nuc maps.
854
roi_index, ##<< The region of interest index.
855
tmp_common_nucs, ##<< the list of wp nucs.
856
strain##<< The strain to consider.
857
){
858
  fuzzy_nucs = strain_maps[[strain]]
859
  fuzzy_nucs = fuzzy_nucs[fuzzy_nucs$roi_index == roi_index,]
860
  fuzzy_nucs = fuzzy_nucs[order(fuzzy_nucs$index_nuc),]
861
  if (length(fuzzy_nucs[,1]) == 0) {return(fuzzy_nucs)}
862
  if (sum(fuzzy_nucs$index_nuc == min(fuzzy_nucs$index_nuc):max(fuzzy_nucs$index_nuc)) != max(fuzzy_nucs$index_nuc)) {"Warning in index!"}
863
  anti_index_1 = tmp_common_nucs[[paste("index_nuc", strain, sep="_")]]
864
  fuzzy_nucs = fuzzy_nucs[-anti_index_1,]
865
  return(fuzzy_nucs)
866
}
852
# remove_aligned_wp = function(# Remove wp nucs from common nucs list.
853
# ### It is based on common wp nucs index on nucs and region.
854
# strain_maps, ##<< Nuc maps.
855
# roi_index, ##<< The region of interest index.
856
# tmp_common_nucs, ##<< the list of wp nucs.
857
# strain##<< The strain to consider.
858
# ){
859
#   fuzzy_nucs = strain_maps[[strain]]
860
#   fuzzy_nucs = fuzzy_nucs[fuzzy_nucs$roi_index == roi_index,]
861
#   fuzzy_nucs = fuzzy_nucs[order(fuzzy_nucs$index_nuc),]
862
#   if (length(fuzzy_nucs[,1]) == 0) {return(fuzzy_nucs)}
863
#   if (sum(fuzzy_nucs$index_nuc == min(fuzzy_nucs$index_nuc):max(fuzzy_nucs$index_nuc)) != max(fuzzy_nucs$index_nuc)) {"Warning in index!"}
864
#   anti_index_1 = tmp_common_nucs[[paste("index_nuc", strain, sep="_")]]
865
#   fuzzy_nucs = fuzzy_nucs[-anti_index_1,]
866
#   return(fuzzy_nucs)
867
# }
867 868

  
868 869
translate_regions = function(# Translate a list of regions from a strain ref to another.
869 870
### This function is an eloborated call to translate_roi.
......
941 942
}
942 943

  
943 944

  
944
get_fuzzy = function(# Compute the fuzzy nucs.
945
### This function aggregate non common wp nucs for each strain and substract common wp nucs. It does not take care about the size of the resulting fuzzy regions. It will be take into account in the count read part og the pipeline.
946
combi, ##<< The strain combination to consider.
947
roi, ##<< The region of interest.
948
roi_index, ##<< The region of interest index.
949
strain_maps, ##<< Nuc maps.
950
common_nuc_results, ##<< Common wp nuc maps
951
config=NULL ##<< GLOBAL config variable
952
) {
953
  print(roi_index)
954
  PLOT = FALSE
955
  tmp_common_nucs = common_nuc_results[[paste(combi[1], combi[2], sep="_")]]
956
  tmp_common_nucs = tmp_common_nucs[tmp_common_nucs$roi_index == roi_index, ]
957

  
958
  print(paste("Dealing with fuzzy from", combi[1]))
959
  tmp_fuzzy_nucs_1 = remove_aligned_wp(strain_maps, roi_index, tmp_common_nucs, combi[1])
960
  tmp_fuzzy_nucs_1 = crop_fuzzy(tmp_fuzzy_nucs_1, roi, combi[1], config)
961
  if (length(tmp_fuzzy_nucs_1[,1]) == 0) {return(NULL)}
962
  agg_fuzzy_1 = union_regions(tmp_fuzzy_nucs_1)
963
  if (PLOT) for (i in 1:length(agg_fuzzy_1[,1])) {
964
    lines(c(agg_fuzzy_1[i,]$lower_bound, agg_fuzzy_1[i,]$upper_bound), c(+3.1,+3.1), col=2)
965
  }
966

  
967
  print(paste("Dealing with fuzzy from ", combi[2]))
968
  tmp_fuzzy_nucs_2 = remove_aligned_wp(strain_maps, roi_index, tmp_common_nucs, combi[2])
969
  if (length(tmp_fuzzy_nucs_2[,1]) == 0) {return(NULL)}
970
  agg_fuzzy_2 = union_regions(tmp_fuzzy_nucs_2)
971
  agg_fuzzy_2 = crop_fuzzy(agg_fuzzy_2, roi, combi[2], config)
972
  tr_agg_fuzzy_2 = translate_regions(agg_fuzzy_2, combi, roi_index, roi=roi, config=config)
973
  tr_agg_fuzzy_2 = crop_fuzzy(tr_agg_fuzzy_2, roi, combi[2], config)
974
  # tr_agg_fuzzy_2 = union_regions(tr_agg_fuzzy_2)
975
  if (PLOT) for (i in 1:length(tr_agg_fuzzy_2[,1])) {
976
    lines(c(tr_agg_fuzzy_2[i,]$lower_bound, tr_agg_fuzzy_2[i,]$upper_bound), c(+3.3,+3.3), col=2)
977
  }
978

  
979
  print("Dealing with fuzzy from both...")
980
  all_fuzzy = union_regions(rbind(agg_fuzzy_1, tr_agg_fuzzy_2))
981
  if (PLOT) for (i in 1:length(all_fuzzy[,1])) {
982
    lines(c(all_fuzzy[i,]$lower_bound, all_fuzzy[i,]$upper_bound), c(+3.2, +3.2), col=1)
983
  }
984

  
985
  print(paste("Dealing with wp from", combi[1]))
986
  wp_nucs_1 = extract_wp(strain_maps, roi_index, combi[1], tmp_common_nucs)
987
  if (PLOT) for (i in 1:length(wp_nucs_1[,1])) {
988
    lines(c(wp_nucs_1[i,]$lower_bound, wp_nucs_1[i,]$upper_bound), c(+3.5,+3.5), col=3)
989
  }
990

  
991
  print(paste("Dealing with wp from", combi[2]))
992
  wp_nucs_2 = extract_wp(strain_maps, roi_index, combi[2], tmp_common_nucs)
993
  tr_wp_nucs_2 = translate_regions(wp_nucs_2, combi, roi_index, roi=roi, config=config)
994
  if (PLOT) for (i in 1:length(tr_wp_nucs_2[,1])) {
995
    lines(c(tr_wp_nucs_2[i,]$lower_bound, tr_wp_nucs_2[i,]$upper_bound), c(+3.7,+3.7), col=3)
996
  }
997

  
998
  print("Dealing with wp from both...")
999
  all_wp = union_regions(rbind(wp_nucs_1[,1:4], tr_wp_nucs_2))
1000
  if (PLOT) for (i in 1:length(all_wp[,1])) {
1001
    lines(c(all_wp[i,]$lower_bound, all_wp[i,]$upper_bound), c(+3.6, +3.6), col=1)
1002
  }
1003

  
1004
  print("Dealing with fuzzy and wp...")
1005
  non_inter_fuzzy = substract_region(all_fuzzy, all_wp)
1006
  if (is.null(non_inter_fuzzy)) { return(NULL) }
1007

  
1008
  non_inter_fuzzy$len = non_inter_fuzzy$upper_bound - non_inter_fuzzy$lower_bound
1009
  # non_inter_fuzzy = non_inter_fuzzy[non_inter_fuzzy$len >= min_fuzz_width,]
1010
  if (PLOT) for (i in 1:length(non_inter_fuzzy[,1])) {
1011
    lines(c(non_inter_fuzzy[i,]$lower_bound, non_inter_fuzzy[i,]$upper_bound), c(+3.9, +3.9), col=1)
1012
  }
1013

  
1014
  non_inter_fuzzy$index_nuc = 1:length(non_inter_fuzzy[,1])
1015
  return (non_inter_fuzzy)
1016
}
945
# get_fuzzy = function(# Compute the fuzzy nucs.
946
# ### This function aggregate non common wp nucs for each strain and substract common wp nucs. It does not take care about the size of the resulting fuzzy regions. It will be take into account in the count read part og the pipeline.
947
# combi, ##<< The strain combination to consider.
948
# roi, ##<< The region of interest.
949
# roi_index, ##<< The region of interest index.
950
# strain_maps, ##<< Nuc maps.
951
# common_nuc_results, ##<< Common wp nuc maps
952
# config=NULL ##<< GLOBAL config variable
953
# ) {
954
#   print(roi_index)
955
#   PLOT = FALSE
956
#   tmp_common_nucs = common_nuc_results[[paste(combi[1], combi[2], sep="_")]]
957
#   tmp_common_nucs = tmp_common_nucs[tmp_common_nucs$roi_index == roi_index, ]
958
# 
959
#   print(paste("Dealing with fuzzy from", combi[1]))
960
#   tmp_fuzzy_nucs_1 = remove_aligned_wp(strain_maps, roi_index, tmp_common_nucs, combi[1])
961
#   tmp_fuzzy_nucs_1 = crop_fuzzy(tmp_fuzzy_nucs_1, roi, combi[1], config)
962
#   if (length(tmp_fuzzy_nucs_1[,1]) == 0) {return(NULL)}
963
#   agg_fuzzy_1 = union_regions(tmp_fuzzy_nucs_1)
964
#   if (PLOT) for (i in 1:length(agg_fuzzy_1[,1])) {
965
#     lines(c(agg_fuzzy_1[i,]$lower_bound, agg_fuzzy_1[i,]$upper_bound), c(+3.1,+3.1), col=2)
966
#   }
967
# 
968
#   print(paste("Dealing with fuzzy from ", combi[2]))
969
#   tmp_fuzzy_nucs_2 = remove_aligned_wp(strain_maps, roi_index, tmp_common_nucs, combi[2])
970
#   if (length(tmp_fuzzy_nucs_2[,1]) == 0) {return(NULL)}
971
#   agg_fuzzy_2 = union_regions(tmp_fuzzy_nucs_2)
972
#   agg_fuzzy_2 = crop_fuzzy(agg_fuzzy_2, roi, combi[2], config)
973
#   tr_agg_fuzzy_2 = translate_regions(agg_fuzzy_2, combi, roi_index, roi=roi, config=config)
974
#   tr_agg_fuzzy_2 = crop_fuzzy(tr_agg_fuzzy_2, roi, combi[2], config)
975
#   # tr_agg_fuzzy_2 = union_regions(tr_agg_fuzzy_2)
976
#   if (PLOT) for (i in 1:length(tr_agg_fuzzy_2[,1])) {
977
#     lines(c(tr_agg_fuzzy_2[i,]$lower_bound, tr_agg_fuzzy_2[i,]$upper_bound), c(+3.3,+3.3), col=2)
978
#   }
979
# 
980
#   print("Dealing with fuzzy from both...")
981
#   all_fuzzy = union_regions(rbind(agg_fuzzy_1, tr_agg_fuzzy_2))
982
#   if (PLOT) for (i in 1:length(all_fuzzy[,1])) {
983
#     lines(c(all_fuzzy[i,]$lower_bound, all_fuzzy[i,]$upper_bound), c(+3.2, +3.2), col=1)
984
#   }
985
# 
986
#   print(paste("Dealing with wp from", combi[1]))
987
#   wp_nucs_1 = extract_wp(strain_maps, roi_index, combi[1], tmp_common_nucs)
988
#   if (PLOT) for (i in 1:length(wp_nucs_1[,1])) {
989
#     lines(c(wp_nucs_1[i,]$lower_bound, wp_nucs_1[i,]$upper_bound), c(+3.5,+3.5), col=3)
990
#   }
991
# 
992
#   print(paste("Dealing with wp from", combi[2]))
993
#   wp_nucs_2 = extract_wp(strain_maps, roi_index, combi[2], tmp_common_nucs)
994
#   tr_wp_nucs_2 = translate_regions(wp_nucs_2, combi, roi_index, roi=roi, config=config)
995
#   if (PLOT) for (i in 1:length(tr_wp_nucs_2[,1])) {
996
#     lines(c(tr_wp_nucs_2[i,]$lower_bound, tr_wp_nucs_2[i,]$upper_bound), c(+3.7,+3.7), col=3)
997
#   }
998
# 
999
#   print("Dealing with wp from both...")
1000
#   all_wp = union_regions(rbind(wp_nucs_1[,1:4], tr_wp_nucs_2))
1001
#   if (PLOT) for (i in 1:length(all_wp[,1])) {
1002
#     lines(c(all_wp[i,]$lower_bound, all_wp[i,]$upper_bound), c(+3.6, +3.6), col=1)
1003
#   }
1004
# 
1005
#   print("Dealing with fuzzy and wp...")
1006
#   non_inter_fuzzy = substract_region(all_fuzzy, all_wp)
1007
#   if (is.null(non_inter_fuzzy)) { return(NULL) }
1008
# 
1009
#   non_inter_fuzzy$len = non_inter_fuzzy$upper_bound - non_inter_fuzzy$lower_bound
1010
#   # non_inter_fuzzy = non_inter_fuzzy[non_inter_fuzzy$len >= min_fuzz_width,]
1011
#   if (PLOT) for (i in 1:length(non_inter_fuzzy[,1])) {
1012
#     lines(c(non_inter_fuzzy[i,]$lower_bound, non_inter_fuzzy[i,]$upper_bound), c(+3.9, +3.9), col=1)
1013
#   }
1014
# 
1015
#   non_inter_fuzzy$index_nuc = 1:length(non_inter_fuzzy[,1])
1016
#   return (non_inter_fuzzy)
1017
# }
1017 1018

  
1018 1019

  
1019 1020

  

Formats disponibles : Unified diff