R/fetch_coords_files.R

#' @title Search by address
#' @author Alexey Solovyev
#' @description Function selects in given address.
#'
#' @param dirFrom Path to data, by default "~/tmp/RData/Output".
#' @param nameChromosome Name of chromosome.
#' @param begin Position of starting of searching on chromosome.
#' @param end Position of ending of searching on chromosome.
#' @param firstCut Can be "in" or "out". Condition of searching. "in" means that begin of MACS-peak can be before param "begin".
#' "out" means that begin of MACS-peak can be only after param "begin".
#' @param secondCut Can be "in" or "out". Condition of searching. "in" means that end of MACS-peak can be after param "end".
#' "out" means that end of MACS-peak can be only before param "end".
#'
#' @return Data Frame (class = "data.frame") of peaks in this address.
#'
#' @usage fetchCoordsFiles(dirFrom = "", nameChromosome = "chr21", begin = "", end = "",
#' firstCut = "in", secondCut = "in")
#'
#' @examples
#' myFrame <- fetchCoordsFiles(dirFrom = "", nameChromosome = "chr21", begin = "",
#' end = "", firstCut = "in", secondCut = "in")
#'
#' @export
fetchCoordsFiles <- function(dirFrom = "",
                             nameChromosome = "chr21",
                             begin = "",
                             end = "",
                             firstCut = "in",
                             secondCut = "in") {
  annexe <- data.frame()

  if (!(firstCut %in% c("in", "out"))|!(secondCut %in% c("in", "out"))) {
    message("Choose 'firstCut' and 'secondCut' from the following: ", paste( c("'in'", "'out'"), collapse=" "))
    stop()
  }

  if (dirFrom == "") {
    dirFrom <- path.expand("~/tmp/RData/Output")
  }

  fileNameAnnexe <- paste(dirFrom, "/annexe.RData", sep = "")
  load(fileNameAnnexe)

  if (class(begin) == "character") {
    begin <- annexe[annexe$Chromosome == nameChromosome, "minLim"]
  }

  if (class(end) == "character") {
    end <- annexe[annexe$Chromosome == nameChromosome, "maxLim"]
  }

  if (begin > end) {
    message("The 'begin' must be less than the 'end'.")
    stop()
  }

  if (nameChromosome == ""|(!(nameChromosome %in% annexe$Chromosome))) {
    listChr <- paste(annexe$Chromosome, collapse=" ")
    message("Choose from the following chromosomes: ", listChr)
    stop()
  }

  myFileOpen <- file.path(dirFrom, paste(nameChromosome, ".RData", sep = ""))
  frameTmp <- get(load(myFileOpen))
  rm(list = c("chr"))
  if ((firstCut == "in") & (secondCut == "in")) {
    frameRep <- frameTmp[which(frameTmp$Begin <= end & frameTmp$End >= begin), ]
  }

  if ((firstCut == "out") & (secondCut == "out")) {
    frameRep <- frameTmp[which(frameTmp$Begin >= begin & frameTmp$End <= end), ]
  }

  if ((firstCut == "in") & (secondCut == "out")) {
    frameRep <- frameTmp[which(frameTmp$End >= begin & frameTmp$End <= end), ]
  }

  if ((firstCut == "out") & (secondCut == "in")) {
    frameRep <- frameTmp[which(frameTmp$Begin >= begin & frameTmp$Begin <= end), ]
  }

  return(frameRep)
}
LosevAMU/remapR documentation built on May 25, 2019, 12:23 p.m.