Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to cpp11 #429

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ index.html
.env
docs
.DS_Store
.vscode
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ Suggests:
tidyr
biocViews:
LinkingTo:
Rcpp (>= 1.0.0)
Rcpp (>= 1.0.0),
cpp11
VignetteBuilder:
knitr
Encoding: UTF-8
Expand Down
16 changes: 0 additions & 16 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

bed12toexons_impl <- function(x) {
.Call(`_valr_bed12toexons_impl`, x)
}

closest_impl <- function(x, y, grp_idx_x, grp_idx_y, suffix_x, suffix_y) {
.Call(`_valr_closest_impl`, x, y, grp_idx_x, grp_idx_y, suffix_x, suffix_y)
}
Expand All @@ -21,10 +17,6 @@ dist_impl <- function(x, y, x_grp_indexes, y_grp_indexes, distcalc) {
.Call(`_valr_dist_impl`, x, y, x_grp_indexes, y_grp_indexes, distcalc)
}

flank_impl <- function(df, genome, both = 0, left = 0, right = 0, fraction = FALSE, stranded = FALSE, trim = FALSE) {
.Call(`_valr_flank_impl`, df, genome, both, left, right, fraction, stranded, trim)
}

gcoverage_impl <- function(gdf, max_coords) {
.Call(`_valr_gcoverage_impl`, gdf, max_coords)
}
Expand All @@ -33,10 +25,6 @@ intersect_impl <- function(x, y, x_grp_indexes, y_grp_indexes, invert = FALSE, s
.Call(`_valr_intersect_impl`, x, y, x_grp_indexes, y_grp_indexes, invert, suffix_x, suffix_y)
}

makewindows_impl <- function(df, win_size = 0L, num_win = 0L, step_size = 0L, reverse = FALSE) {
.Call(`_valr_makewindows_impl`, df, win_size, num_win, step_size, reverse)
}

merge_impl <- function(gdf, max_dist = 0L, collapse = TRUE) {
.Call(`_valr_merge_impl`, gdf, max_dist, collapse)
}
Expand All @@ -45,10 +33,6 @@ partition_impl <- function(gdf, max_dist = -1L) {
.Call(`_valr_partition_impl`, gdf, max_dist)
}

random_impl <- function(genome, length, n, seed = 0L) {
.Call(`_valr_random_impl`, genome, length, n, seed)
}

shuffle_impl <- function(df, incl, within = FALSE, max_tries = 1000L, seed = 0L) {
.Call(`_valr_shuffle_impl`, df, incl, within, max_tries, seed)
}
Expand Down
17 changes: 17 additions & 0 deletions R/cpp11.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by cpp11: do not edit by hand

bed12toexons_impl <- function(x) {
.Call(`_valr_bed12toexons_impl`, x)
}

flank_impl <- function(df, genome, both, left, right, fraction, stranded, trim) {
.Call(`_valr_flank_impl`, df, genome, both, left, right, fraction, stranded, trim)
}

makewindows_impl <- function(df, win_size, num_win, step_size, reverse) {
.Call(`_valr_makewindows_impl`, df, win_size, num_win, step_size, reverse)
}

random_impl <- function(genome, length, n, seed) {
.Call(`_valr_random_impl`, genome, length, n, seed)
}
4 changes: 2 additions & 2 deletions R/read_bed.r
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ peak_coltypes <- list(

bed12_coltypes <- list(
chrom = readr::col_character(),
start = readr::col_integer(),
end = readr::col_integer(),
start = readr::col_double(),
end = readr::col_double(),
name = readr::col_character(),
score = readr::col_character(),
strand = readr::col_character(),
Expand Down
2 changes: 1 addition & 1 deletion inst/include/genome.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
IntegerVector sizes = genome[col_size] ;

if (unique(refs).length() != refs.length())
stop("duplicate reference names in genome file.") ;
Rcpp::stop("duplicate reference names in genome file.") ;

Check warning on line 27 in inst/include/genome.h

View check run for this annotation

Codecov / codecov/patch

inst/include/genome.h#L27

Added line #L27 was not covered by tests

int nchrom = genome.nrows() ;
for (int i = 0; i < nchrom; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion inst/include/group_apply.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
int ng_y = shared_grps_y.size() ;

if (ng_x != ng_y) {
stop("incompatible groups found between x and y dataframes") ;
Rcpp::stop("incompatible groups found between x and y dataframes") ;

Check warning on line 31 in inst/include/group_apply.h

View check run for this annotation

Codecov / codecov/patch

inst/include/group_apply.h#L31

Added line #L31 was not covered by tests
}

// access the group .rows list
Expand Down
3 changes: 3 additions & 0 deletions inst/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ DataFrame subset_dataframe(const DataFrame& df,
DataFrame subset_dataframe(const DataFrame& df,
IntegerVector indices) ;

writable::data_frame subset_dataframe(const data_frame& df,
std::vector<int> indices) ;

inline DataFrame check_is_grouped(const DataFrame& x) {
bool is_grouped(Rf_inherits(x, "grouped_df")) ;

Expand Down
5 changes: 3 additions & 2 deletions inst/include/valr.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
#ifndef valr__valr_H
#define valr__valr_H

// [[Rcpp::plugins(cpp11)]]

#include <Rcpp.h>
using namespace Rcpp ;

#include <cpp11.hpp>
using namespace cpp11;

#include "utils.h"
#include "grouped_dataframe.h"
#include "IntervalTree.h"
Expand Down
58 changes: 0 additions & 58 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,6 @@ Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
#endif

// bed12toexons_impl
DataFrame bed12toexons_impl(DataFrame x);
RcppExport SEXP _valr_bed12toexons_impl(SEXP xSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< DataFrame >::type x(xSEXP);
rcpp_result_gen = Rcpp::wrap(bed12toexons_impl(x));
return rcpp_result_gen;
END_RCPP
}
// closest_impl
DataFrame closest_impl(ValrGroupedDataFrame x, ValrGroupedDataFrame y, IntegerVector grp_idx_x, IntegerVector grp_idx_y, const std::string& suffix_x, const std::string& suffix_y);
RcppExport SEXP _valr_closest_impl(SEXP xSEXP, SEXP ySEXP, SEXP grp_idx_xSEXP, SEXP grp_idx_ySEXP, SEXP suffix_xSEXP, SEXP suffix_ySEXP) {
Expand Down Expand Up @@ -79,24 +68,6 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// flank_impl
DataFrame flank_impl(DataFrame df, DataFrame genome, double both, double left, double right, bool fraction, bool stranded, bool trim);
RcppExport SEXP _valr_flank_impl(SEXP dfSEXP, SEXP genomeSEXP, SEXP bothSEXP, SEXP leftSEXP, SEXP rightSEXP, SEXP fractionSEXP, SEXP strandedSEXP, SEXP trimSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< DataFrame >::type df(dfSEXP);
Rcpp::traits::input_parameter< DataFrame >::type genome(genomeSEXP);
Rcpp::traits::input_parameter< double >::type both(bothSEXP);
Rcpp::traits::input_parameter< double >::type left(leftSEXP);
Rcpp::traits::input_parameter< double >::type right(rightSEXP);
Rcpp::traits::input_parameter< bool >::type fraction(fractionSEXP);
Rcpp::traits::input_parameter< bool >::type stranded(strandedSEXP);
Rcpp::traits::input_parameter< bool >::type trim(trimSEXP);
rcpp_result_gen = Rcpp::wrap(flank_impl(df, genome, both, left, right, fraction, stranded, trim));
return rcpp_result_gen;
END_RCPP
}
// gcoverage_impl
DataFrame gcoverage_impl(const ValrGroupedDataFrame& gdf, const IntegerVector& max_coords);
RcppExport SEXP _valr_gcoverage_impl(SEXP gdfSEXP, SEXP max_coordsSEXP) {
Expand Down Expand Up @@ -126,21 +97,6 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// makewindows_impl
DataFrame makewindows_impl(DataFrame df, int win_size, int num_win, int step_size, bool reverse);
RcppExport SEXP _valr_makewindows_impl(SEXP dfSEXP, SEXP win_sizeSEXP, SEXP num_winSEXP, SEXP step_sizeSEXP, SEXP reverseSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< DataFrame >::type df(dfSEXP);
Rcpp::traits::input_parameter< int >::type win_size(win_sizeSEXP);
Rcpp::traits::input_parameter< int >::type num_win(num_winSEXP);
Rcpp::traits::input_parameter< int >::type step_size(step_sizeSEXP);
Rcpp::traits::input_parameter< bool >::type reverse(reverseSEXP);
rcpp_result_gen = Rcpp::wrap(makewindows_impl(df, win_size, num_win, step_size, reverse));
return rcpp_result_gen;
END_RCPP
}
// merge_impl
DataFrame merge_impl(ValrGroupedDataFrame gdf, int max_dist, bool collapse);
RcppExport SEXP _valr_merge_impl(SEXP gdfSEXP, SEXP max_distSEXP, SEXP collapseSEXP) {
Expand All @@ -166,20 +122,6 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// random_impl
DataFrame random_impl(DataFrame genome, int length, int n, int seed);
RcppExport SEXP _valr_random_impl(SEXP genomeSEXP, SEXP lengthSEXP, SEXP nSEXP, SEXP seedSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< DataFrame >::type genome(genomeSEXP);
Rcpp::traits::input_parameter< int >::type length(lengthSEXP);
Rcpp::traits::input_parameter< int >::type n(nSEXP);
Rcpp::traits::input_parameter< int >::type seed(seedSEXP);
rcpp_result_gen = Rcpp::wrap(random_impl(genome, length, n, seed));
return rcpp_result_gen;
END_RCPP
}
// shuffle_impl
DataFrame shuffle_impl(DataFrame df, DataFrame incl, bool within, int max_tries, int seed);
RcppExport SEXP _valr_shuffle_impl(SEXP dfSEXP, SEXP inclSEXP, SEXP withinSEXP, SEXP max_triesSEXP, SEXP seedSEXP) {
Expand Down
45 changes: 25 additions & 20 deletions src/bed12toexons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ std::vector<int> csv_values(std::string csv) {
return values ;
}

// [[Rcpp::export]]
DataFrame bed12toexons_impl(DataFrame x) {
[[cpp11::register]]
writable::data_frame bed12toexons_impl(data_frame x) {

// input
IntegerVector starts = x["start"] ;
std::vector<std::string> exon_sizes = x["exon_sizes"] ;
std::vector<std::string> exon_starts = x["exon_starts"] ;
std::vector<std::string> strands = x["strand"] ;
doubles starts = x["start"] ;
strings exon_sizes = x["exon_sizes"] ;
strings exon_starts = x["exon_starts"] ;
strings strands = x["strand"] ;

// storage
std::vector<int> starts_out ;
Expand Down Expand Up @@ -66,18 +66,23 @@ DataFrame bed12toexons_impl(DataFrame x) {
}
}

DataFrame out = subset_dataframe(x, df_idx) ;

out["start"] = starts_out ;
out["end"] = ends_out ;
out["score"] = nums_out ;

return out ;
writable::data_frame out = subset_dataframe(x, df_idx) ;

return writable::data_frame({
"chrom"_nm = out["chrom"],
"start"_nm = starts_out,
"end"_nm = ends_out,
"score"_nm = nums_out,
"strand"_nm = out["strand"],
"name"_nm = out["name"],
"cdsStart"_nm = out["cdsStart"],
"cdsEnd"_nm = out["cdsEnd"],
"exonCount"_nm = out["exonCount"],
"exonSizes"_nm = out["exonSizes"],
"exonStarts"_nm = out["exonStarts"],
"itemRgb"_nm = out["itemRgb"],
"blockCount"_nm = out["blockCount"],
"blockSizes"_nm = out["blockSizes"],
"blockStarts"_nm = out["blockStarts"]
}) ;
}

/***R
library(valr)
library(dplyr)
x <- read_bed12(valr_example('mm9.refGene.bed.gz'))
bed12_to_exons(x)
*/
3 changes: 1 addition & 2 deletions src/closest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
int ng_y = grp_idx_y.size() ;

if (ng_x != ng_y) {
stop("incompatible groups found between x and y dataframes") ;
Rcpp::stop("incompatible groups found between x and y dataframes") ;

Check warning on line 236 in src/closest.cpp

View check run for this annotation

Codecov / codecov/patch

src/closest.cpp#L236

Added line #L236 was not covered by tests
}

// access the group .rows list
Expand Down Expand Up @@ -297,4 +297,3 @@
return res ;

}

75 changes: 75 additions & 0 deletions src/cpp11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Generated by cpp11: do not edit by hand
// clang-format off

#include <cpp11/R.hpp>
#include <Rcpp.h>
using namespace Rcpp;
#include "cpp11/declarations.hpp"
#include <R_ext/Visibility.h>

// bed12toexons.cpp
writable::data_frame bed12toexons_impl(data_frame x);
extern "C" SEXP _valr_bed12toexons_impl(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(bed12toexons_impl(cpp11::as_cpp<cpp11::decay_t<data_frame>>(x)));
END_CPP11
}
// flank.cpp
writable::data_frame flank_impl(data_frame df, data_frame genome, double both, double left, double right, bool fraction, bool stranded, bool trim);
extern "C" SEXP _valr_flank_impl(SEXP df, SEXP genome, SEXP both, SEXP left, SEXP right, SEXP fraction, SEXP stranded, SEXP trim) {
BEGIN_CPP11
return cpp11::as_sexp(flank_impl(cpp11::as_cpp<cpp11::decay_t<data_frame>>(df), cpp11::as_cpp<cpp11::decay_t<data_frame>>(genome), cpp11::as_cpp<cpp11::decay_t<double>>(both), cpp11::as_cpp<cpp11::decay_t<double>>(left), cpp11::as_cpp<cpp11::decay_t<double>>(right), cpp11::as_cpp<cpp11::decay_t<bool>>(fraction), cpp11::as_cpp<cpp11::decay_t<bool>>(stranded), cpp11::as_cpp<cpp11::decay_t<bool>>(trim)));
END_CPP11
}
// makewindows.cpp
writable::data_frame makewindows_impl(data_frame df, int win_size, int num_win, int step_size, bool reverse);
extern "C" SEXP _valr_makewindows_impl(SEXP df, SEXP win_size, SEXP num_win, SEXP step_size, SEXP reverse) {
BEGIN_CPP11
return cpp11::as_sexp(makewindows_impl(cpp11::as_cpp<cpp11::decay_t<data_frame>>(df), cpp11::as_cpp<cpp11::decay_t<int>>(win_size), cpp11::as_cpp<cpp11::decay_t<int>>(num_win), cpp11::as_cpp<cpp11::decay_t<int>>(step_size), cpp11::as_cpp<cpp11::decay_t<bool>>(reverse)));
END_CPP11
}
// random.cpp
writable::data_frame random_impl(data_frame genome, double length, int n, int seed);
extern "C" SEXP _valr_random_impl(SEXP genome, SEXP length, SEXP n, SEXP seed) {
BEGIN_CPP11
return cpp11::as_sexp(random_impl(cpp11::as_cpp<cpp11::decay_t<data_frame>>(genome), cpp11::as_cpp<cpp11::decay_t<double>>(length), cpp11::as_cpp<cpp11::decay_t<int>>(n), cpp11::as_cpp<cpp11::decay_t<int>>(seed)));
END_CPP11
}

extern "C" {
/* .Call calls */
extern SEXP _valr_closest_impl(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP _valr_complement_impl(SEXP, SEXP);
extern SEXP _valr_coverage_impl(SEXP, SEXP, SEXP, SEXP);
extern SEXP _valr_dist_impl(SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP _valr_gcoverage_impl(SEXP, SEXP);
extern SEXP _valr_intersect_impl(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP _valr_merge_impl(SEXP, SEXP, SEXP);
extern SEXP _valr_partition_impl(SEXP, SEXP);
extern SEXP _valr_shuffle_impl(SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP _valr_subtract_impl(SEXP, SEXP, SEXP, SEXP);

static const R_CallMethodDef CallEntries[] = {
{"_valr_bed12toexons_impl", (DL_FUNC) &_valr_bed12toexons_impl, 1},
{"_valr_closest_impl", (DL_FUNC) &_valr_closest_impl, 6},
{"_valr_complement_impl", (DL_FUNC) &_valr_complement_impl, 2},
{"_valr_coverage_impl", (DL_FUNC) &_valr_coverage_impl, 4},
{"_valr_dist_impl", (DL_FUNC) &_valr_dist_impl, 5},
{"_valr_flank_impl", (DL_FUNC) &_valr_flank_impl, 8},
{"_valr_gcoverage_impl", (DL_FUNC) &_valr_gcoverage_impl, 2},
{"_valr_intersect_impl", (DL_FUNC) &_valr_intersect_impl, 7},
{"_valr_makewindows_impl", (DL_FUNC) &_valr_makewindows_impl, 5},
{"_valr_merge_impl", (DL_FUNC) &_valr_merge_impl, 3},
{"_valr_partition_impl", (DL_FUNC) &_valr_partition_impl, 2},
{"_valr_random_impl", (DL_FUNC) &_valr_random_impl, 4},
{"_valr_shuffle_impl", (DL_FUNC) &_valr_shuffle_impl, 5},
{"_valr_subtract_impl", (DL_FUNC) &_valr_subtract_impl, 4},
{NULL, NULL, 0}
};
}

extern "C" attribute_visible void R_init_valr(DllInfo* dll){
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
R_forceSymbols(dll, TRUE);
}
Loading
Loading