#' @title Retrieve core orthologs across multiple species from Orthofinder2 output
#' @description Retrieve core orthologs (= single copy orthologs) across multiple species
#' from a Orthofinder2 output.
#' @param orthogroups_file file path to Orthofinder2 output file named \code{Orthogroups.csv}.
#' @param single_copy_file file path to Orthofinder2 output file named \code{SingleCopyOrthogroups.txt}.
#' @author Hajk-Georg Drost
#' @export

orthofinder2_retrieve_core_orthologs <- function(orthogroups_file, single_copy_file) {
        
        if (!fs::file_exists(orthogroups_file))
                stop("Please provide a valid path to the 'Orthogroups.csv' file generated by Orthofinder2.", call. = FALSE)
        if (!fs::file_exists(single_copy_file))
                stop("Please provide a valid path to the 'SingleCopyOrthogroups.txt' file generated by Orthofinder2.", call. = FALSE)
        
        suppressWarnings(suppressMessages(orthogroups <- readr::read_tsv(orthogroups_file, col_names = TRUE)))
        names(orthogroups)[1] <- "orthogroup"

        SingleCopyOrthogroups <- tibble::tibble(orthogroup = readr::read_lines(single_copy_file))
        core_set <- dplyr::semi_join(orthogroups, SingleCopyOrthogroups, by = "orthogroup")
        
        if (length(core_set$orthogroup) != length(unique(core_set$orthogroup)))
                stop("Something went wrong with the import and single copy ortholog extraction. It seems like the orthogroup IDs are not unique...", call. = FALSE)
        
        return(core_set)
}