Skip to content

Commit ab8e87a

Browse files
committed
use api of vcfpp 0.3.7
1 parent a93b43a commit ab8e87a

File tree

4 files changed

+47
-32
lines changed

4 files changed

+47
-32
lines changed

cran-comments.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
1. address clang-UBSAN issue by including the latest htslib-1.19.1 (https://github.com/samtools/htslib/releases/tag/1.19)
2-
2. reduce size of package
3-
3. add copyrights and authors of htslib
4-
4. new function `vcfcomp`
1+
1. address issues on https://cran.r-project.org/web/checks/check_results_vcfppR.html
2+
2. verbose message of vcfcomp

src/vcf-reader.cpp

+16-9
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,23 @@ using namespace std;
121121
//' }
122122
class vcfreader {
123123
public:
124-
vcfreader(const std::string& vcffile) {
124+
vcfreader(const std::string& vcffile) : fin(vcffile) {
125125
br.open(vcffile);
126-
var.init(br.header);
126+
var.initHeader(br.header);
127127
}
128128

129-
vcfreader(const std::string& vcffile, const std::string& region) {
129+
vcfreader(const std::string& vcffile, const std::string& region) : fin(vcffile) {
130130
br.open(vcffile);
131131
if (!region.empty()) br.setRegion(region);
132-
var.init(br.header);
132+
var.initHeader(br.header);
133133
}
134134

135-
vcfreader(const std::string& vcffile, const std::string& region, const std::string& samples) {
135+
vcfreader(const std::string& vcffile, const std::string& region, const std::string& samples)
136+
: fin(vcffile) {
136137
br.open(vcffile);
137138
if (!samples.empty()) br.setSamples(samples);
138139
if (!region.empty()) br.setRegion(region);
139-
var.init(br.header);
140+
var.initHeader(br.header);
140141
}
141142

142143
~vcfreader() {}
@@ -259,11 +260,16 @@ class vcfreader {
259260
// WRITE
260261
inline void output(const std::string& vcffile) {
261262
bw.open(vcffile);
262-
bw.initalHeader(br.header);
263+
bw.copyHeader(fin);
264+
var.resetHeader(bw.header);
263265
writable = true;
264266
}
265-
inline void write() { bw.writeRecord(var); }
266-
inline void close() { bw.close(); }
267+
inline void write() {
268+
if (writable) bw.writeRecord(var);
269+
}
270+
inline void close() {
271+
if (writable) bw.close();
272+
}
267273

268274
inline void setCHR(std::string s) { var.setCHR(s.c_str()); }
269275
inline void setID(std::string s) { var.setID(s.c_str()); }
@@ -318,6 +324,7 @@ class vcfreader {
318324

319325
private:
320326
bool writable = false;
327+
const std::string fin;
321328
vcfpp::BcfReader br;
322329
vcfpp::BcfRecord var;
323330
vcfpp::BcfWriter bw;

src/vcfpp.h

+29-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file https://github.com/Zilong-Li/vcfpp/vcfpp.h
33
* @author Zilong Li
44
5-
* @version v0.3.6
5+
* @version v0.3.7
66
* @breif a single C++ file for manipulating VCF
77
* Copyright (C) 2022-2023.The use of this code is governed by the LICENSE file.
88
******************************************************************************/
@@ -118,6 +118,15 @@ inline bool isEndWith(std::string const & s, std::string const & e)
118118
}
119119
}
120120

121+
// determinate the mode for read/write the compressed/uncompressed VCF/BCF
122+
inline std::string getMode(std::string const & fname, std::string mode = "r")
123+
{
124+
if(isEndWith(fname, "bcf.gz")) mode += "b";
125+
if(isEndWith(fname, "bcf")) mode += "bu";
126+
if(isEndWith(fname, "vcf.gz")) mode += "z";
127+
return mode;
128+
}
129+
121130
// string split by separator
122131
inline std::vector<std::string> split_string(const std::string & s, const std::string & separators)
123132
{
@@ -416,7 +425,7 @@ class BcfRecord
416425
/// constructor with a given BcfHeader object
417426
BcfRecord(BcfHeader & h)
418427
{
419-
init(h);
428+
initHeader(h);
420429
}
421430

422431
~BcfRecord()
@@ -425,8 +434,8 @@ class BcfRecord
425434
if(hdr_d) bcf_hdr_destroy(hdr_d);
426435
}
427436

428-
/// initilize a BcfRecord object by pointing to another BcfHeader object
429-
void init(BcfHeader & h)
437+
/// initilize the header associated with BcfRecord object by pointing to another BcfHeader object
438+
void initHeader(BcfHeader & h)
430439
{
431440
header = &h;
432441
if(!header->hdr) throw std::runtime_error("please initial header first\n");
@@ -435,6 +444,12 @@ class BcfRecord
435444
gtPhase.resize(nsamples, 0);
436445
}
437446

447+
/// reset the header associated with BcfRecord object by pointing to another BcfHeader object
448+
void resetHeader(BcfHeader & h)
449+
{
450+
header = &h;
451+
}
452+
438453
/** @brief stream out the variant */
439454
friend std::ostream & operator<<(std::ostream & out, const BcfRecord & v)
440455
{
@@ -1369,15 +1384,6 @@ class BcfReader
13691384
SamplesName = header.getSamples();
13701385
}
13711386

1372-
/// return if the file is opened successfully
1373-
bool isOpen() const
1374-
{
1375-
if(fp != NULL)
1376-
return true;
1377-
else
1378-
return false;
1379-
}
1380-
13811387
/** @brief set the number of threads to use */
13821388
inline int setThreads(int n)
13831389
{
@@ -1562,10 +1568,7 @@ class BcfWriter
15621568
*/
15631569
void open(const std::string & fname)
15641570
{
1565-
std::string mode{"w"};
1566-
if(isEndWith(fname, "bcf.gz")) mode += "b";
1567-
if(isEndWith(fname, "bcf")) mode += "bu";
1568-
if(isEndWith(fname, "vcf.gz")) mode += "z";
1571+
auto mode = getMode(fname, "w");
15691572
fp = std::shared_ptr<htsFile>(hts_open(fname.c_str(), mode.c_str()), htsFile_close());
15701573
}
15711574

@@ -1604,6 +1607,15 @@ class BcfWriter
16041607
hp = &h;
16051608
}
16061609

1610+
/// copy header of given VCF
1611+
void copyHeader(const std::string & vcffile)
1612+
{
1613+
htsFile * fp2 = hts_open(vcffile.c_str(), "r");
1614+
header.hdr = bcf_hdr_read(fp2);
1615+
hts_close(fp2);
1616+
initalHeader(header);
1617+
}
1618+
16071619
/// copy a string to a vcf line
16081620
void writeLine(const std::string & vcfline)
16091621
{

tests/testthat/test-modify-vcf.R

-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ test_that("modify the genotypes", {
2424
g2 <- br$genotypes(F)
2525
expect_false(isTRUE(all.equal(g0, g2)))
2626
expect_identical(g1, g2)
27-
br$close()
2827
## the original vcf can not be modified
2928
br <- vcfreader$new(outvcf)
3029
br$variant() ## get a variant record
@@ -54,7 +53,6 @@ test_that("modify item in FORMAT", {
5453
ds <- gp[2,] + gp[3,] * 2
5554
## will prompt uses a message if `output` is not called
5655
br$addFORMAT("DS", "1", "Float", "Diploid dosage")
57-
br$addINFO("INFO", "1", "Float", "INFO score of imputation")
5856
## now open another file for output
5957
newvcf <- paste0(tempfile(), ".vcf.gz")
6058
br$output(newvcf)

0 commit comments

Comments
 (0)