Skip to content

Commit

Permalink
add vcfwriter
Browse files Browse the repository at this point in the history
  • Loading branch information
Zilong-Li committed Dec 7, 2023
1 parent 2b71456 commit 7fa36de
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 11 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export(vcfpopgen)
export(vcfreader)
export(vcfsummary)
export(vcftable)
export(vcfwriter)
importFrom(Rcpp,loadModule)
useDynLib(vcfppR, .registration = TRUE)
11 changes: 10 additions & 1 deletion R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#' @name vcfreader
#' @title API for reading read the VCF/BCF.
#' @title API for reading the VCF/BCF.
#' @description Type the name of the class to see its methods
#' @field new Constructor given a vcf file \itemize{
#' \item Parameter: vcffile - The path of a vcf file
Expand Down Expand Up @@ -50,6 +50,15 @@
#' @field hasOVERLAP Test if current variant has a OVERLAP or not
NULL

#' @name vcfwriter
#' @title API for writing the VCF/BCF.
#' @description Type the name of the class to see its methods
#' @field new Constructor given a vcf file \itemize{
#' \item Parameter: vcffile - The path of a vcf file. don't start with "~"
#' \item Parameter: version - The version of VCF specification
#' }
NULL

#' calculate the number of heterozygous SNPs for each sample
#' @param vcffile path to the VCF file with index
#' @param region region to extract, default "" for all
Expand Down
2 changes: 2 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Export the "vcfreader" C++ class by explicitly requesting vcfreader be
# exported via roxygen2's export tag.
#' @export vcfreader
#' @export vcfwriter
loadModule("vcfreader", TRUE)
loadModule("vcfwriter", TRUE)
2 changes: 1 addition & 1 deletion man/vcfreader.Rd

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

17 changes: 17 additions & 0 deletions man/vcfwriter.Rd

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

2 changes: 2 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ END_RCPP
}

RcppExport SEXP _rcpp_module_boot_vcfreader();
RcppExport SEXP _rcpp_module_boot_vcfwriter();

static const R_CallMethodDef CallEntries[] = {
{"_vcfppR_heterozygosity", (DL_FUNC) &_vcfppR_heterozygosity, 5},
Expand All @@ -109,6 +110,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_vcfppR_summaryVariants", (DL_FUNC) &_vcfppR_summaryVariants, 5},
{"_vcfppR_summarySVs", (DL_FUNC) &_vcfppR_summarySVs, 5},
{"_rcpp_module_boot_vcfreader", (DL_FUNC) &_rcpp_module_boot_vcfreader, 0},
{"_rcpp_module_boot_vcfwriter", (DL_FUNC) &_rcpp_module_boot_vcfwriter, 0},
{NULL, NULL, 0}
};

Expand Down
19 changes: 16 additions & 3 deletions src/vcf-reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using namespace std;

//' @name vcfreader
//' @title API for reading read the VCF/BCF.
//' @title API for reading the VCF/BCF.
//' @description Type the name of the class to see its methods
//' @field new Constructor given a vcf file \itemize{
//' \item Parameter: vcffile - The path of a vcf file
Expand Down Expand Up @@ -175,9 +175,23 @@ class vcfreader {
inline bool hasOTHER() const { return var.hasOTHER(); }
inline bool hasOVERLAP() const { return var.hasOVERLAP(); }

// WRITE
inline void output(std::string vcffile) {
bw.open(vcffile);
bw.initalHeader(br.header);
}
inline void write() { bw.writeRecord(var); }
inline void close() { bw.close(); }

inline void setCHR(std::string s) { var.setCHR(s.c_str()); }
inline void setID(std::string s) { var.setID(s.c_str()); }
inline void setPOS(int pos) { var.setPOS(pos); }
inline void setRefAlt(std::string s) { var.setRefAlt(s.c_str()); }

private:
vcfpp::BcfReader br;
vcfpp::BcfRecord var;
vcfpp::BcfWriter bw;
vector<int> v_int;
vector<float> v_float;
vector<std::string> v_str;
Expand Down Expand Up @@ -224,6 +238,5 @@ RCPP_MODULE(vcfreader) {
.method("hasMNP", &vcfreader::hasMNP)
.method("hasBND", &vcfreader::hasBND)
.method("hasOTHER", &vcfreader::hasOTHER)
.method("hasOVERLAP", &vcfreader::hasOVERLAP)
;
.method("hasOVERLAP", &vcfreader::hasOVERLAP);
}
51 changes: 51 additions & 0 deletions src/vcf-writer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <Rcpp.h>
#include "vcfpp.h"

using namespace std;

//' @name vcfwriter
//' @title API for writing the VCF/BCF.
//' @description Type the name of the class to see its methods
//' @field new Constructor given a vcf file \itemize{
//' \item Parameter: vcffile - The path of a vcf file. don't start with "~"
//' \item Parameter: version - The version of VCF specification
//' }
class vcfwriter {
public:
vcfwriter(std::string vcffile, std::string version) : bw(vcffile, version) {}
~vcfwriter() {}

inline void close() { bw.close(); }

// WRITE ONLY
inline void writeline(std::string line) { bw.writeLine(line); }
inline void addLine(std::string str) { bw.header.addLine(str); }
inline void addSample(std::string str) { bw.header.addSample(str); }
inline void addContig(std::string str) { bw.header.addContig(str); }
inline void addFILTER(std::string id, std::string desc) { bw.header.addFILTER(id, desc); }
inline void addINFO(std::string id, std::string number, std::string type, std::string desc) {
bw.header.addINFO(id, number, type, desc);
}
inline void addFORMAT(std::string id, std::string number, std::string type, std::string desc) {
bw.header.addFORMAT(id, number, type, desc);
}

private:
vcfpp::BcfWriter bw;
};

RCPP_EXPOSED_CLASS(vcfwriter)
RCPP_MODULE(vcfwriter) {
using namespace Rcpp;
class_<vcfwriter>("vcfwriter")
.constructor<std::string, std::string>("construct vcfwriter given vcf file and the version")
.method("close", &vcfwriter::close)
.method("writeline", &vcfwriter::writeline)
.method("addLine", &vcfwriter::addLine)
.method("addSample", &vcfwriter::addSample)
.method("addContig", &vcfwriter::addContig)
.method("addFILTER", &vcfwriter::addFILTER)
.method("addINFO", &vcfwriter::addINFO)
.method("addFORMAT", &vcfwriter::addFORMAT)
;
}
18 changes: 12 additions & 6 deletions src/vcfpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1042,12 +1042,6 @@ type as noted in the other overloading function.
return line->pos + 1;
}

/** @brief modify position given 1-based value */
inline void setAlleleStr(const char * alleles_string)
{
bcf_update_alleles_str(header.hdr, line, alleles_string);
}

/** @brief modify CHROM value */
inline void setCHR(const char * chr)
{
Expand All @@ -1060,6 +1054,18 @@ type as noted in the other overloading function.
line->pos = p - 1;
}

/** @brief update ID */
inline void setID(const char * s)
{
bcf_update_id(header.hdr, line, s);
}

/** @brief set REF and ALT alleles given a string seperated by comma */
inline void setRefAlt(const char * alleles_string)
{
bcf_update_alleles_str(header.hdr, line, alleles_string);
}

/** @brief return 0-base start of the variant (can be any type) */
inline int64_t Start() const
{
Expand Down

0 comments on commit 7fa36de

Please sign in to comment.