#' @title Save the sequence of the predicted LTR Transposons in a fasta file
#' @description This function allows users to save the sequence of the predicted LTR Transposons or LTRs in a fasta file.
#' @param LTRpred.tbl the \code{\link{data.frame}} generated by \code{\link{LTRpred}}.
#' @param prediction.file the fasta file storing either the full LTR Transposon sequence or only the LTR sequence
#' as returned by the \code{\link{LTRpred}} function.
#' @param output the fasta file to which the output sequences shall be stored in.
#' @author Hajk-Georg Drost
#' @details 
#' The output \code{data.frame}s returned by \code{\link{LTRpred}} contain all information of the predicted
#' LTR retrotransposons that can be used for post-filtering steps. After these post-filtering steps
#' sequences of the remaining (filtered) candidates can be retrieved by this function.
#' @examples 
#' \dontrun{
#' # Hypothetical Example
#' # Generate LTR transposon prediction for A. thaliana 
#' Ath.Pred <- LTRpred(input.gff3        = "TAIR10_chr_all/TAIR10_chr_all_Prediction.gff", 
#'                     genome.file       = "Genome/TAIR10_chr_all.fas",
#'                     trnas             = "araTha1-tRNAs.fa",
#'                     hmms              = "hmm_*",
#'                     cores             = 1)
#' 
#' # Filter for LTR transposons having 100 pec. sequence similarity between their LTRs
#' FilteredLTRTransposons <- dplyr::filter(Ath.Pred, ltr_similarity == 100)
#' 
#' # Write the sequences of these filtered LTR transposons to a fasta file
#' pred2fasta(LTRpred.tbl = FilteredLTRTransposons, 
#'            prediction.file = "TAIR10_chr_all-ltrdigest_complete.fas", 
#'            output                = "AthalianaPutativeLTRTransposons.fa")
#' }
#' @seealso \code{\link{LTRharvest}}, \code{\link{LTRdigest}}, \code{\link{LTRpred}}, \code{\link{read.prediction}} 
#' @export 

pred2fasta <- function(LTRpred.tbl, prediction.file, output = "output.fa"){
   
    if (!file.exists(prediction.file))
      stop("The file '", prediction.file, "' does not seem to exist. Please provide a valid file path to a fasta file storing all predicted retrotransposon sequences.", call. = FALSE)
  
    PutativeLTRSeqs <- Biostrings::readDNAStringSet(prediction.file)
    Biostrings::writeXStringSet(PutativeLTRSeqs[stats::na.omit(match(LTRpred.tbl$orf.id, PutativeLTRSeqs@ranges@NAMES))], output)
    
}




