2
2
* @file https://github.com/Zilong-Li/vcfpp/vcfpp.h
3
3
* @author Zilong Li
4
4
5
- * @version v0.3.6
5
+ * @version v0.3.7
6
6
* @breif a single C++ file for manipulating VCF
7
7
* Copyright (C) 2022-2023.The use of this code is governed by the LICENSE file.
8
8
******************************************************************************/
@@ -118,6 +118,15 @@ inline bool isEndWith(std::string const & s, std::string const & e)
118
118
}
119
119
}
120
120
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
+
121
130
// string split by separator
122
131
inline std::vector<std::string> split_string (const std::string & s, const std::string & separators)
123
132
{
@@ -416,7 +425,7 @@ class BcfRecord
416
425
// / constructor with a given BcfHeader object
417
426
BcfRecord (BcfHeader & h)
418
427
{
419
- init (h);
428
+ initHeader (h);
420
429
}
421
430
422
431
~BcfRecord ()
@@ -425,8 +434,8 @@ class BcfRecord
425
434
if (hdr_d) bcf_hdr_destroy (hdr_d);
426
435
}
427
436
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)
430
439
{
431
440
header = &h;
432
441
if (!header->hdr ) throw std::runtime_error (" please initial header first\n " );
@@ -435,6 +444,12 @@ class BcfRecord
435
444
gtPhase.resize (nsamples, 0 );
436
445
}
437
446
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
+
438
453
/* * @brief stream out the variant */
439
454
friend std::ostream & operator <<(std::ostream & out, const BcfRecord & v)
440
455
{
@@ -1369,15 +1384,6 @@ class BcfReader
1369
1384
SamplesName = header.getSamples ();
1370
1385
}
1371
1386
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
-
1381
1387
/* * @brief set the number of threads to use */
1382
1388
inline int setThreads (int n)
1383
1389
{
@@ -1562,10 +1568,7 @@ class BcfWriter
1562
1568
*/
1563
1569
void open (const std::string & fname)
1564
1570
{
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" );
1569
1572
fp = std::shared_ptr<htsFile>(hts_open (fname.c_str (), mode.c_str ()), htsFile_close ());
1570
1573
}
1571
1574
@@ -1604,6 +1607,15 @@ class BcfWriter
1604
1607
hp = &h;
1605
1608
}
1606
1609
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
+
1607
1619
// / copy a string to a vcf line
1608
1620
void writeLine (const std::string & vcfline)
1609
1621
{
0 commit comments