R/setupBlibBuild.R

.LINUX_64<-"https://github.com/protViz/bibliospec/releases/download/BlibBuild_binaries_v01/Bibliospec_linux_64_static.zip"
.WINDOWS_64<-"https://github.com/protViz/bibliospec/releases/download/BlibBuild_binaries_v01/Bibliospec_win_64_static.zip"

.downloadFile <- function(filename, destdir){
  #Download file into tmpdir
  tmpdir = tempdir()
  
  fileName = basename(filename)
  downloadFile <- file.path(tmpdir,fileName)
  message("downloading from : ", filename)
  message("download destination : ", downloadFile)
  
  if(! file.exists(downloadFile) || file.info(downloadFile)$size==0 ) {
    utils::download.file(filename, downloadFile)
  }
  if(file.info(downloadFile)$size==0) {
    unlink(downloadFile)
    downloadFile=NULL
    cat(paste("ERROR: failed to download ", fileName,".\nPlease check your internet connection and/or try again. " , 
              "\nThen, if you still display this error message please contact us.",sep=""))
  }
  
  #extract file and remove tempdir
  message("extracting file to: ", destdir)
  extfiles <-utils::unzip(downloadFile,exdir=destdir)
  # remove download directory
  unlink(tmpdir)
  
  return(grep("\\.exe", extfiles, value=T ))
}

.downloadFILES <- function(destdir){
  system <- Sys.info()
  if(.isWin64()){
    .downloadFile(.WINDOWS_64,destdir )
  }else if(.isLinux64()){
    .downloadFile(.LINUX_64, destdir)
  }
}



.isWin64 <- function(){
  system <- Sys.info()
  if(system["sysname"] == "Windows" && system["machine"] == "x86-64"){
    return(TRUE)
  }else{
    return(FALSE)
  }
}

.isLinux64 <- function(){
  system <- Sys.info()
  if(system["sysname"] == "Linux" && system["machine"] == "x86-64"){
    return(TRUE)
  }else{
    return(FALSE)
  }
}


.getExecutables<- function(destdir){
  if( .isWin64() ){
    blibdir <- tools::file_path_sans_ext(basename(.WINDOWS_64) )
    path <- file.path(destdir, blibdir)
    tmp <- grep("\\.exe$",dir(path),value=TRUE)
    tmp <- file.path(path,tmp)
    return(tmp)
  }
}

#' Blib class for generating blib files from dat and other id file formats
#' 
#' @export Blib
#' @exportClass Blib
#' @examples
#' 
#' 
#' \dontrun{
#' 
#' BB <- Blib()
#' datdir <- "inst/extdata"
#' dir(datdir)
#' tmp <- file.path(datdir,dir(datdir))
#' zip <- grep("*.zip",tmp,value=T)
#' unzip(zip ,exdir = datdir)
#' dat <-grep("*\\.dat",file.path(datdir,dir(datdir)),value=T)
#' 
#' BB$build(dat , outfile = "test0_0.blib", cutoff=0.0 )
#' BS <- Bibliospec("test0_0.blib")
#' BS$summary()
#' 
#' spectrMet <- BS$getSpectraMeta()
#' 
#' peaks <- BS$getPeaks()
#' head(peaks)
#' length(unique(peaks$RefSpectraId))
#' 
#' BS <- NULL
#' }
Blib <- setRefClass("Blib",
                    fields = list( destdir = "character",
                                   peaksTable = "character",
                                   executables = "character",
                                   blibfile = "character"
                    ),
                    methods=list(
                      initialize = function(destdir=tempdir()){
                        .self$destdir <- destdir
                        .self$executables <- .getExecutables(.self$destdir)
                        if(length(executables) == 0){
                          .self$executables <- .downloadFILES(destdir)
                        }
                      },
                      build = function(idfiles,outfile, cutoff = 0.9 ){
                        "build blibfile"
                        .self$blibfile = outfile
                        exe <- grep("BlibBuild",.self$executables, value=TRUE)
                        print(exe)
                        cmd <- paste(exe,"-c", cutoff, paste(idfiles, collapse=" "), outfile ,sep=" ")
                        message(cmd)
                        res <- system(cmd, intern=T)
                        .self$blibfile = outfile
                        invisible(res)
                      },
                      filter = function(infile, outfile, minpeaks = 1, minscore=0){
                        "filter blib file"
                        exe <- grep("BlibFilter",.self$executables, value=TRUE)
                        print(exe)
                        cmd <- paste(exe,"-n", minpeaks, "-s" , minscore, infile , outfile , sep=" ")
                        message(cmd)
                        res <- system(cmd, intern=T)
                        invisible(res)
                      }
                      
                    )
)
protViz/bibliospec documentation built on May 26, 2019, 9:37 a.m.