#' @title Select members of a specific cluster
#' @description This function allows users to select all members of a specific cluster.
#' @param LTRpred.tbl \code{data.frame} generated by \code{\link{LTRpred}}.
#' @param cluster name of the cluster that shall be searched for.
#' @author Hajk-Georg Drost
#' @examples
#' \dontrun{ 
#' # example prediction file generated by LTRpred 
#' pred.file <- system.file("Athaliana_TAIR10_chr_all_LTRpred_DataSheet.csv", package = "LTRpred")
#' # read LTRpred generated prediction file (data sheet)
#' pred <- read.ltrpred(pred.file)
#' 
#' # extract memebers of cluster 'cl_108'
#' cluster.members(pred, cluster = "cl_108")
#' 
#' # or safe the sequences of the cluster as fasta
#' pred2fasta(cluster.members(pred, cluster = "cl_108"), "cl_108_seqs.fa")
#' }
#' @seealso \code{\link{clust2fasta}}
#' @export

cluster.members <- function(LTRpred.tbl, cluster = NULL){
    
    if (!is.element(cluster, names(table(LTRpred.tbl$Clust_Cluster))))
        stop("Cluster '",cluster,"' is not present in this dataset.", call. = FALSE)
    
    Clust_Cluster <- orf.id <- NULL
    
    if (!is.null(cluster)) {
        LTRpred.sub.tbl <-
            dplyr::filter(LTRpred.tbl, Clust_Cluster == cluster)
        Cluster.target.tbl <-
            dplyr::filter(LTRpred.tbl, orf.id == unlist(unique(LTRpred.sub.tbl$Clust_Target)))
        
        if (nrow(Cluster.target.tbl) > 0) {
            Cluster.target.tbl$Clust_Cluster <- cluster
            Cluster.target.tbl$Clust_Target <-
                unlist(unique(LTRpred.sub.tbl$Clust_Target))
        }
        return(rbind(Cluster.target.tbl, LTRpred.sub.tbl))
    } else {
        stop("Please specify a cluster!", call. = FALSE)
    }
    
}
