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

Bringing R C++ code up to date with the main C++ code #77

Merged
merged 10 commits into from
Jun 22, 2021
3 changes: 1 addition & 2 deletions C++/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ int main(int argc, char *argv[])
{
if (argc != 7 && argc != 8) {
cerr << "Incorrect arguments" << endl;
cerr << "Usage: straw <NONE/VC/VC_SQRT/KR> <hicFile(s)> <chr1>[:x1:x2] <chr2>[:y1:y2] <BP/FRAG> <binsize>" << endl;
cerr << "Usage: straw <oe/expected> <NONE/VC/VC_SQRT/KR> <hicFile(s)> <chr1>[:x1:x2] <chr2>[:y1:y2] <BP/FRAG> <binsize>" << endl;
cerr << "Usage: straw [observed/oe/expected] <NONE/VC/VC_SQRT/KR> <hicFile(s)> <chr1>[:x1:x2] <chr2>[:y1:y2] <BP/FRAG> <binsize>" << endl;
exit(1);
}
int offset = 0;
Expand Down
24 changes: 12 additions & 12 deletions C++/straw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ using namespace std;

Currently only supporting matrices.

Usage: straw <NONE/VC/VC_SQRT/KR> <hicFile(s)> <chr1>[:x1:x2] <chr2>[:y1:y2] <BP/FRAG> <binsize>
Usage: straw [observed/oe/expected] <NONE/VC/VC_SQRT/KR> <hicFile(s)> <chr1>[:x1:x2] <chr2>[:y1:y2] <BP/FRAG> <binsize>
*/
// this is for creating a stream from a byte array for ease of use
struct membuf : std::streambuf {
Expand Down Expand Up @@ -202,9 +202,9 @@ bool readFooter(istream &fin, int64_t master, int32_t version, int32_t c1, int32
const string &unit, int32_t resolution, int64_t &myFilePos, indexEntry &c1NormEntry, indexEntry &c2NormEntry,
vector<double> &expectedValues) {
if (version > 8) {
int64_t nBytes = readInt64FromFile(fin);
readInt64FromFile(fin);
} else {
int32_t nBytes = readInt32FromFile(fin);
readInt32FromFile(fin);
}

stringstream ss;
Expand All @@ -217,7 +217,7 @@ bool readFooter(istream &fin, int64_t master, int32_t version, int32_t c1, int32
string str;
getline(fin, str, '\0');
int64_t fpos = readInt64FromFile(fin);
int32_t sizeinbytes = readInt32FromFile(fin);
readInt32FromFile(fin);
if (str == key) {
myFilePos = fpos;
found = true;
Expand Down Expand Up @@ -494,8 +494,8 @@ map<int32_t, indexEntry> readMatrixHttp(CURL *curl, int64_t myFilePosition, cons
membuf sbuf(buffer, buffer + size);
istream bufin(&sbuf);

int32_t c1 = readInt32FromFile(bufin);
int32_t c2 = readInt32FromFile(bufin);
readInt32FromFile(bufin);
Copy link
Member

Choose a reason for hiding this comment

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

Let's not remove the variable name for future readability.

Copy link
Collaborator Author

@cwenger cwenger Jun 18, 2021

Choose a reason for hiding this comment

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

That was to fix a compiler warning (CRAN doesn't allow warnings or significant notes without explanation). How about we keep it as an inline comment, e.g. /* int32_t c1 = */ readInt32FromFile(bufin);?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@sa501428 Does the latest commit satisfy your concern?

readInt32FromFile(bufin);
int32_t nRes = readInt32FromFile(bufin);
int32_t i = 0;
bool found = false;
Expand All @@ -522,8 +522,8 @@ map<int32_t, indexEntry> readMatrix(istream &fin, int64_t myFilePosition, const
map<int32_t, indexEntry> blockMap;

fin.seekg(myFilePosition, ios::beg);
int32_t c1 = readInt32FromFile(fin);
int32_t c2 = readInt32FromFile(fin);
readInt32FromFile(fin);
readInt32FromFile(fin);
int32_t nRes = readInt32FromFile(fin);
int32_t i = 0;
bool found = false;
Expand Down Expand Up @@ -641,7 +641,7 @@ vector<contactRecord> readBlock(istream &fin, CURL *curl, bool isHttp, indexEntr
vector<contactRecord> v(nRecords);
// different versions have different specific formats
if (version < 7) {
for (int i = 0; i < nRecords; i++) {
for (uInt i = 0; i < nRecords; i++) {
int32_t binX = readInt32FromFile(bufferin);
int32_t binY = readInt32FromFile(bufferin);
float counts = readFloatFromFile(bufferin);
Expand Down Expand Up @@ -849,10 +849,10 @@ class HiCFile {
b[numbytes + 1] = '\0';
string s(b);
int32_t found = static_cast<int32_t>(s.find("Content-Range"));
if (found != string::npos) {
if ((size_t)found != string::npos) {
int32_t found2 = static_cast<int32_t>(s.find("/"));
//Content-Range: bytes 0-100000/891471462
if (found2 != string::npos) {
if ((size_t)found2 != string::npos) {
string total = s.substr(found2 + 1);
totalFileSize = stol(total);
}
Expand Down Expand Up @@ -1209,7 +1209,7 @@ vector<contactRecord>
straw(string matrixType, string norm, string fname, string chr1loc, string chr2loc, const string &unit, int32_t binsize) {
if (!(unit == "BP" || unit == "FRAG")) {
cerr << "Norm specified incorrectly, must be one of <BP/FRAG>" << endl;
cerr << "Usage: straw <NONE/VC/VC_SQRT/KR> <hicFile(s)> <chr1>[:x1:x2] <chr2>[:y1:y2] <BP/FRAG> <binsize>"
cerr << "Usage: straw [observed/oe/expected] <NONE/VC/VC_SQRT/KR> <hicFile(s)> <chr1>[:x1:x2] <chr2>[:y1:y2] <BP/FRAG> <binsize>"
<< endl;
vector<contactRecord> v;
return v;
Expand Down
5 changes: 2 additions & 3 deletions R/R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' of data, and outputs as data.frame in sparse upper triangular format.
#' Currently only supporting matrices.
#'
#' Usage: straw <observed/oe> <NONE/VC/VC_SQRT/KR> <hicFile(s)> <chr1>[:x1:x2] <chr2>[:y1:y2] <BP/FRAG> <binsize>
#' Usage: straw [observed/oe/expected] <NONE/VC/VC_SQRT/KR> <hicFile(s)> <chr1>[:x1:x2] <chr2>[:y1:y2] <BP/FRAG> <binsize>
#'
#' @param norm Normalization to apply. Must be one of NONE/VC/VC_SQRT/KR.
#' VC is vanilla coverage, VC_SQRT is square root of vanilla coverage, and KR is Knight-Ruiz or
Expand All @@ -27,7 +27,7 @@
#' straw("NONE", system.file("extdata", "test.hic", package = "strawr"), "1", "1", "BP", 2500000)
#' @export
straw <- function(norm, fname, chr1loc, chr2loc, unit, binsize, matrix = "observed") {
.Call('_strawr_straw', PACKAGE = 'strawr', norm, fname, chr1loc, chr2loc, unit, binsize, matrix)
.Call('_strawr_straw', PACKAGE = 'strawr', matrix, norm, fname, chr1loc, chr2loc, unit, binsize)
}

#' Function for reading basepair resolutions from .hic file
Expand All @@ -51,4 +51,3 @@ readHicBpResolutions <- function(fname) {
readHicChroms <- function(fname) {
.Call('_strawr_readHicChroms', PACKAGE = 'strawr', fname)
}

2 changes: 1 addition & 1 deletion R/src/Makevars
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
PKG_LIBS= -lz
PKG_LIBS = -lcurl -lz
CXX_STD = CXX11
17 changes: 17 additions & 0 deletions R/src/Makevars.win
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
VERSION=7.64.1
PKG_LIBS= -L../windows/libcurl-$(VERSION)/lib${R_ARCH} \
-lwinhttp -lcurl -lssh2 -lz -lssl -lcrypto -lgdi32 -lws2_32 -lcrypt32 -lwldap32

PKG_CPPFLAGS= \
-I../windows/libcurl-$(VERSION)/include -DCURL_STATICLIB -DSTRICT_R_HEADERS

all: clean winlibs

clean:
rm -f $(SHLIB) $(OBJECTS)

winlibs: clean
"${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" "../tools/winlibs.R" $(VERSION)
echo '#include <curl/curl.h>' | $(CC) $(PKG_CPPFLAGS) -std=gnu99 -E -xc - | grep "^[ \t]*CURLOPT_.*," | sed s/,// > ../tools/option_table.txt

.PHONY: all winlibs clean
14 changes: 7 additions & 7 deletions R/src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
using namespace Rcpp;

// straw
Rcpp::DataFrame straw(std::string norm, std::string fname, std::string chr1loc, std::string chr2loc, std::string unit, int32_t binsize, std::string matrix);
RcppExport SEXP _strawr_straw(SEXP normSEXP, SEXP fnameSEXP, SEXP chr1locSEXP, SEXP chr2locSEXP, SEXP unitSEXP, SEXP binsizeSEXP, SEXP matrixSEXP) {
Rcpp::DataFrame straw(std::string matrixType, std::string norm, std::string fname, std::string chr1loc, std::string chr2loc, const std::string& unit, int32_t binsize);
RcppExport SEXP _strawr_straw(SEXP matrixTypeSEXP, SEXP normSEXP, SEXP fnameSEXP, SEXP chr1locSEXP, SEXP chr2locSEXP, SEXP unitSEXP, SEXP binsizeSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::string >::type matrixType(matrixTypeSEXP);
Rcpp::traits::input_parameter< std::string >::type norm(normSEXP);
Rcpp::traits::input_parameter< std::string >::type fname(fnameSEXP);
Rcpp::traits::input_parameter< std::string >::type chr1loc(chr1locSEXP);
Rcpp::traits::input_parameter< std::string >::type chr2loc(chr2locSEXP);
Rcpp::traits::input_parameter< std::string >::type unit(unitSEXP);
Rcpp::traits::input_parameter< const std::string& >::type unit(unitSEXP);
Rcpp::traits::input_parameter< int32_t >::type binsize(binsizeSEXP);
Rcpp::traits::input_parameter< std::string >::type matrix(matrixSEXP);
rcpp_result_gen = Rcpp::wrap(straw(norm, fname, chr1loc, chr2loc, unit, binsize, matrix));
rcpp_result_gen = Rcpp::wrap(straw(matrixType, norm, fname, chr1loc, chr2loc, unit, binsize));
return rcpp_result_gen;
END_RCPP
}
// readHicBpResolutions
NumericVector readHicBpResolutions(std::string fname);
Rcpp::NumericVector readHicBpResolutions(std::string fname);
RcppExport SEXP _strawr_readHicBpResolutions(SEXP fnameSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Expand All @@ -34,7 +34,7 @@ BEGIN_RCPP
END_RCPP
}
// readHicChroms
DataFrame readHicChroms(std::string fname);
Rcpp::DataFrame readHicChroms(std::string fname);
RcppExport SEXP _strawr_readHicChroms(SEXP fnameSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Expand Down
Loading