# use this function if you wish you use the custom Nc/Ne ratio when provided

transform_to_Ne_BG <- function (indNe_data, ratio)
{
    ratio = ratio
    
    # check for mistakes in the value of ratio
    if (!is.numeric(ratio) || ratio < 0 || ratio > 1) {
        stop("Invalid argument. Please provide a number within the range 0 to 1, using `.` to delimit decimals.")
    }
    else {
        indNe_data = indNe_data
        indNe_data <- indNe_data %>% 
            
            # transform Nc range values to point values
            mutate(Nc_from_range = case_when(
                NcRange == "more_5000_bymuch" ~ 10000, 
                NcRange == "more_5000" ~ 5500, 
                NcRange == "less_5000_bymuch" ~ 500, 
                NcRange == "less_5000" ~ 4500, 
                NcRange == "range_includes_5000" ~ 5001)) %>%
            
            # classify populations according to their size
            mutate(pop_size_class = case_when(
                NcRange == "more_5000_bymuch" | NcPoint > 5500 ~ "large_size", 
                NcRange == "less_5000_bymuch" | NcPoint < 4500 ~ "small_size", 
                NcRange == "less_5000" | NcRange == "range_includes_5000" | NcRange == "more_5000" ~ "medium_size", 
                NcPoint >= 4500 & NcPoint <= 5500 ~ "medium_size")) %>%
            
            # calculate Ne from Nc using a standart ratio value
            mutate(Ne_from_Nc_standart = case_when(
                !is.na(NcPoint) ~ NcPoint * ratio, 
                !is.na(Nc_from_range) ~ Nc_from_range * ratio)) %>% 
            mutate(NeLower_from_Nc_standart = case_when(
                !is.na(NcLower) ~ NcLower * ratio)) %>% 
            mutate(NeUpper_from_Nc_standart = case_when(
                !is.na(NcUpper) ~ NcUpper * ratio)) %>% 
            
            mutate(Ne_combined_standart = if_else(is.na(Ne),
                   as.double(Ne_from_Nc_standart), as.double(Ne))) %>% 
            
            # calculate Ne from Nc using a custom ratio value
            mutate(Ne_from_Nc_custom = case_when(!is.na(NcPoint) ~
                  NcPoint * ratio_formatted, 
                  !is.na(Nc_from_range) ~ Nc_from_range * ratio_formatted)) %>% 
            mutate(NeLower_from_Nc_custom = case_when(!is.na(NcLower) ~
                  NcLower * ratio_formatted)) %>% 
            mutate(NeUpper_from_Nc_custom = case_when(!is.na(NcUpper) ~
                  NcUpper * ratio_formatted)) %>% 
            mutate(Ne_combined_custom = if_else(is.na(Ne),
                  as.double(Ne_from_Nc_custom), as.double(Ne))) %>%
            
            # create a column specifying how Ne was calculated
            mutate(Ne_calculated_from = if_else(is.na(Ne),
            if_else(!is.na(NcPoint), "NcPoint ratio", if_else(!is.na(Nc_from_range),
                "NcRange ratio", NA_character_)), "genetic data")) %>% 
            
            # create a column refering just to Nc
            mutate(Nc_type = case_when(
                !is.na(NcPoint) & !is.na(NcRange) ~ "Both",
                !is.na(NcPoint) & is.na(NcRange) ~ "Point_data",
                is.na(NcPoint) & !is.na(NcRange) ~ "Range_data",
                TRUE ~ NA))

    }
}
