Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .Rhistory
Empty file.
59 changes: 59 additions & 0 deletions R/cmd-bedtools.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#' Run bedtools
#'
#' The `bedtools` is a powerful toolset for genome arithmetic.
#' @param subcmd Sub-Command of bedtools. Details see: `r rd_help("bedtools")`.
#' @param file1 Path to bed/gff(gtf)/vcf/bam(sam) file.
#' @param file2 Path to the second file, some commands require.
#' @param ... `r rd_dots("bedtools")`.
#' @param ofile Path to the output file.
#' @param bedtools `r rd_cmd("bedtools")`.
#' @seealso
#' - <https://github.com/arq5x/bedtools2/>
#'
#' `r rd_seealso()`
#' @inherit exec return
#' @family command
#' @export
bedtools <- make_command(
"bedtools",
function(
subcmd = NULL,
file1,
ofile,
...,
file2 = NULL,
bedtools = NULL
) {
assert_string(subcmd, allow_empty = FALSE, allow_null = TRUE)
assert_string(bedtools, allow_empty = FALSE, allow_null = TRUE)
BEDTools$new(
cmd = bedtools,
...,
subcmd = subcmd,
file1 = file1,
file2 = file2,
ofile = ofile
)
}
)

BEDTools <- R6Class(
"BEDTools",
inherit = Command,
private = list(
alias = function() "bedtools",
setup_help_params = function() "help",

@Yunuuuu Yunuuuu Jun 7, 2025

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我试了下,bedtools命令的帮助文档参数应该是"--help"

blit::appmamba("create", "--yes", "--name bedtools", "bioconda::bedtools")
exec("bedtools", "--help") |>
    cmd_condaenv("bedtools") |>
    cmd_run()

exec("bedtools", "intersect", "--help") |>
    cmd_condaenv("bedtools") |>
    cmd_run()

combine_params = function(subcmd, file1, file2, ofile) {
c(
if (private$help) {
c(super$combine_params(), subcmd)
} else {
c(subcmd, super$combine_params())
},
arg0("-a", file1),
if (!is.null(file2)) arg0("-b", file2) else NULL,
arg0("-o", ofile),
)
}
)
)
254 changes: 254 additions & 0 deletions R/cmd-bedtools_1.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
#' Run bedtools
#'
#' The `bedtools` is a powerful toolset for genome arithmetic,
#' which includes `intersect`, `merge`, `coverage`, `getfasta`, `closest`, etc.
#' @param file1 Path to bed/gff(gtf)/vcf/bam(sam) file.
#' @param file2 Path to the second file, some commands require.
#' @param ...
#' - `bedtools intersect`: `r rd_dots("bedtools intersect")`.
#' - `bedtools merge`: `r rd_dots("bedtools merge")`.
#' - `bedtools coverage`: `r rd_dots("bedtools coverage")`.
#' - `bedtools getfasta`: `r rd_dots("bedtools getfasta")`.
#' - `bedtools closest`: `r rd_dots("bedtools closest")`.
#' @param ofile Path to the output file.
#' @param
#' - `bedtools intersect`: `r rd_cmd("bedtools intersect")`.
#' - `bedtools merge`: `r rd_cmd("bedtools merge")`.
#' - `bedtools coverage`: `r rd_cmd("bedtools coverage")`.
#' - `bedtools getfasta`: `r rd_cmd("bedtools getfasta")`.
#' - `bedtools closest`: `r rd_cmd("bedtools closest")`.
#' @seealso
#' - <https://github.com/arq5x/bedtools2/>
#'
#' `r rd_seealso()`
#' @inherit exec return
#' @family command
#' @export
# bedtools_intersect ----
bedtools_intersect <- make_command(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你已经创建了bedtools命令后,这个函数直接调用前面创建的函数就行

bedtools_intersect <- function(...) {
  bedtools(sucmd ="intersect", ...) # do your work
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此文件所有的函数都可以改成此形式,当然,实际上由subcmd参数,实际上这些函数不要也可

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,我当时上课跟他说过。只是让他提交记录下。

@Gates369 可以考虑只保留一个 clean 的简单版本,其他可以删除 commit 下。

"bedtools_intersect",
function(
file1,
file2,
ofile,
...,
`bedtools intersect` = NULL
){
assert_string(`bedtools intersect`, allow_empty = FALSE, allow_null = TRUE)
BEDTools_Intersect$new(
cmd = `bedtools intersect`,
...,
file1 = file1,
file2 = file2,
ofile = ofile,
)
}
)

BEDTools_Intersect <- R6Class(
"BEDTools_Intersect",
inherit = Command,
private = list(
alias = function() "bedtools intersect",
setup_help_params = function() "--help",
setup_command_params = function(file1, file2, ofile){
c(
arg0("-a", file1),
arg0("-b", file2),
arg0("-wa"),
arg0("-o", ofile),
)
}
)
)

# bedtools_merge ----
bedtools_merge <- make_command(
"bedtools_merge",
function(
file1,
ofile,
...,
dist = NULL,
`bedtools merge` = NULL
){
assert_string(`bedtools merge`, allow_empty = FALSE, allow_null = TRUE)
`BEDTools_Merge`$new(
cmd = `bedtools merge`,
...,
file1 = file1,
dist = dist,
ofile = ofile,
)
}
)

BEDTools_Merge <- R6Class(
"BEDTools_Merge",
inherit = Command,
private = list(
alias = function() "bedtools merge",
setup_help_params = function() "--help",
setup_command_params = function(file1, dist, ofile){
c(
if (endsWith(file1, ".bed")|endsWith(file1, ".gff")){
arg0("-i", file1)
} else{
cli::cli_abort(
"{.arg file1} must be BED/GFF"
)
},
if (!is.null(dist)) arg0("-d", dist) else NULL,
arg0("-o", ofile),
)
}
)
)

# bedtools_coverage ----
bedtools_coverage <- make_command(
"bedtools_coverage",
function(
file1,
file2,
ofile,
...,
`bedtools coverage` = NULL
){
assert_string(`bedtools coverage`, allow_empty = FALSE, allow_null = TRUE)
BEDTools_Coverage$new(
cmd = `bedtools coverage`,
...,
file1 = file1,
file2 = file2,
ofile = ofile,
)
}
)

BEDTools_Coverage <- R6Class(
"BEDTools_Coverage",
inherit = Command,
private = list(
alias = function() "bedtools coverage",
setup_help_params = function() "--help",
setup_command_params = function(file1, file2, ofile){
c(
if (endsWith(file1, ".bed")|endsWith(file1, ".gff")){
arg0("-a", file1)
} else{
cli::cli_abort(
"{.arg file1} must be BED/GFF"
)
},
if (endsWith(file2, ".bed")|endsWith(file2, ".gff")|endsWith(file2, ".bam")){
arg0("-b", file2)
} else{
cli::cli_abort(
"{.arg file2} must be BED/GFF/BAM"
)
},
arg0("-o", ofile),
)
}
)
)

# bedtools_getfasta ----
bedtools_getfasta <- make_command(
"bedtools_getfasta",
function(
file1,
file2,
ofile,
...,
`bedtools getfasta` = NULL
){
assert_string(`bedtools getfasta`, allow_empty = FALSE, allow_null = TRUE)
BEDTools_Getfasta$new(
cmd = `bedtools getfasta`,
...,
file1 = file1,
file2 = file2,
ofile = ofile,
)
}
)

BEDTools_Getfasta <- R6Class(
"BEDTools_Getfasta",
inherit = Command,
private = list(
alias = function() "bedtools getfasta",
setup_help_params = function() "--help",
setup_command_params = function(file1, file2, ofile){
c(
if (endsWith(file1, ".fa")){
arg0("-fi", file1)
} else{
cli::cli_abort(
"{.arg file1} must be FASTA"
)
},
if (endsWith(file2, ".bed")|endsWith(file2, ".gff")){
arg0("-bed", file2)
} else{
cli::cli_abort(
"{.arg file2} must be BED/GFF"
)
},
arg0("-fo", ofile),
)
}
)
)

# bedtools_closest ----
bedtools_closest <- make_command(
"bedtools_closest",
function(
file1,
file2,
ofile,
...,
`bedtools closest` = NULL
){
assert_string(`bedtools closest`, allow_empty = FALSE, allow_null = TRUE)
BEDTools_Closest$new(
cmd = `bedtools closest`,
...,
file1 = file1,
file2 = file2,
ofile = ofile,
)
}
)

BEDTools_Closest <- R6Class(
"BEDTools_Closest",
inherit = Command,
private = list(
alias = function() "bedtools closest",
setup_help_params = function() "--help",
setup_command_params = function(file1, file2, ofile){
c(
if (endsWith(file1, ".bed")|endsWith(file1, ".gff")|endsWith(file1, ".vcf")){
arg0("-a", file1)
} else{
cli::cli_abort(
"{.arg file1} must be BED/GFF/VCF"
)
},
if (endsWith(file2, ".bed")|endsWith(file2, ".gff")|endsWith(file2, ".vcf")){
arg0("-b", file2)
} else{
cli::cli_abort(
"{.arg file2} must be BED/GFF/VCF"
)
},
args("-D"),
arg0("-o", ofile),
)
}
)
)
6 changes: 6 additions & 0 deletions blit.Rproj

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一般尽量不导致大数量的改变,这样有利于review,这个文件跟此PR功能无关,需保持原状

Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
Version: 1.0
ProjectId: 8959c0d9-c76c-4fa8-a50c-0dae5e78ec5f

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
LineEndingConversion: Posix
Expand Down
54 changes: 54 additions & 0 deletions man/bedtools.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading