Skip to content

Commit e94005e

Browse files
committed
review update
1 parent aebd6ee commit e94005e

File tree

3 files changed

+43
-70
lines changed

3 files changed

+43
-70
lines changed

htslib/vcf.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,15 +1523,11 @@ int bcf_format_gt_v2(const bcf_hdr_t *hdr, bcf_fmt_t *fmt, int isample,
15231523
* update44phasing - converts GT to/from v4.4 way representation
15241524
* @param h - bcf header, to get version
15251525
* @param v - pointer to bcf data
1526-
* @param setreset - whether to set or reset
1527-
* Returns 0 on success and -1 on failure
1528-
* For data read, to be converted to v44, setreset to be 1. For data write, to
1529-
* be converted to v < v44, setreset to be 0.
1526+
* Returns 0 on success and 1 on failure
15301527
* If the version in header is >= 4.4, no change is made. Otherwise 1st phasing
15311528
* is set if there are no other unphased ones.
15321529
*/
1533-
HTSLIB_EXPORT int update44phasing(bcf_hdr_t *h, bcf1_t *b, int setreset)
1534-
HTS_RESULT_USED;
1530+
HTSLIB_EXPORT int update44phasing(bcf_hdr_t *h, bcf1_t *b) HTS_RESULT_USED;
15351531

15361532
static inline int bcf_format_gt(bcf_fmt_t *fmt, int isample, kstring_t *str)
15371533
{

synced_bcf_reader.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ static int _reader_fill_buffer(bcf_srs_t *files, bcf_sr_t *reader)
709709
if ( ret < -1 ) files->errnum = bcf_read_error;
710710
if ( ret < 0 ) break; // no more lines or an error
711711
//set phasing of 1st value as in vcf v44
712-
if ((ret = update44phasing(reader->header, reader->buffer[reader->nbuffer+1], 1))) {
712+
if ((ret = update44phasing(reader->header, reader->buffer[reader->nbuffer+1]))) {
713713
files->errnum = bcf_read_error;
714714
break;
715715
}

vcf.c

Lines changed: 40 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,64 +1819,49 @@ static void bcf_record_check_err(const bcf_hdr_t *hdr, bcf1_t *rec,
18191819
* @param p - pointer to phase value array
18201820
* @param end - end of array
18211821
* @param q - pointer to consumed data
1822-
* @param set - whether to set (while read) or reset (for write)
18231822
* @param samples - no. of samples in array
18241823
* @param ploidy - no. of phasing values per sample
18251824
* @param type - value type (one of BCF_BT_...)
18261825
* Returns 0 on success and 1 on failure
18271826
*/
1828-
static int updatephasing(uint8_t *p, uint8_t *end, uint8_t **q, int set, int samples, int ploidy, int type)
1827+
static int updatephasing(uint8_t *p, uint8_t *end, uint8_t **q, int samples, int ploidy, int type)
18291828
{
1830-
int j, k, upd = set ? 1 : -1;
1829+
int j, k;
18311830
size_t bytes;
1832-
for (j = 0; j < samples; ++j) { //for each sample
1833-
int anyunphased = 0;
1834-
uint8_t *ptr1 = p;
1835-
int32_t al1 = 0, val;
1836-
for (k = 0; k < ploidy; ++k) { //in each ploidy
1837-
switch(type) {
1838-
case BCF_BT_INT8:
1839-
val = *(uint8_t*)p;
1840-
break;
1841-
case BCF_BT_INT16:
1842-
val = *(uint16_t*)p;
1843-
break;
1844-
case BCF_BT_INT32:
1845-
val = *(uint32_t*)p;
1846-
break;
1847-
//wont have anything bigger than 32bit for GT
1848-
default: //invalid
1849-
return 1;
1850-
break;
1851-
}
1852-
if (!k) al1 = val;
1853-
else if (!(val & 1)) {
1854-
anyunphased = 1;
1855-
}
1856-
//get to next phasing or skip the rest for this sample
1857-
bytes = (anyunphased ? ploidy - k : 1) << bcf_type_shift[type];
1858-
if (end - p < bytes)
1859-
return 1;
1860-
p += bytes;
1861-
if (anyunphased) {
1862-
break; //no further check required
1863-
}
1864-
}
1865-
if (!anyunphased && al1 > 1) { //no other unphased
1831+
1832+
#define NOSWITCHFOR(type_t, rawtype_t,convert, reconvert, p, end, samples, ploidy, consumed) \
1833+
for (j = 0; j < samples; ++j) { /*for each sample */ \
1834+
int anyunphased = 0; \
1835+
uint8_t *ptr1 = p; \
1836+
rawtype_t al1 = 0, val; \
1837+
for (k = 0; k < ploidy; ++k) { /*in each ploidy */ \
1838+
val = convert(p); \
1839+
if (!k) al1 = val; \
1840+
else if (!(val & 1)) anyunphased = 1; \
1841+
/*get to next phasing or skip the rest for this sample*/ \
1842+
*consumed = ((anyunphased || al1 & 1) ? ploidy - k : 1) << bcf_type_shift[type_t]; \
1843+
if (end - p < *consumed) return 1; \
1844+
p += *consumed; \
1845+
if (anyunphased || al1 & 1) break; \
1846+
} \
1847+
if (!anyunphased && al1 > 1) { /*no other unphased*/ \
18661848
/*set phased on read or make unphased on write as upto 4.3 1st
1867-
phasing is not described explicitly and has to be inferred*/
1868-
switch(type) {
1869-
case BCF_BT_INT8:
1870-
*(uint8_t*)ptr1 += upd;
1871-
break;
1872-
case BCF_BT_INT16:
1873-
*(uint16_t*)ptr1 += upd;
1874-
break;
1875-
case BCF_BT_INT32:
1876-
*(uint32_t*)ptr1 += upd;
1877-
break;
1878-
}
1879-
}
1849+
phasing is not described explicitly and has to be inferred*/ \
1850+
al1 |= 1; \
1851+
reconvert(convert((uint8_t*)&al1), ptr1); \
1852+
} \
1853+
}
1854+
#define i8_to_le(v, buf) *((uint8_t*)buf) = v;
1855+
switch(type) {
1856+
case BCF_BT_INT8:
1857+
NOSWITCHFOR(BCF_BT_INT8, uint8_t, le_to_i8, i8_to_le, p, end, samples, ploidy, &bytes);
1858+
break;
1859+
case BCF_BT_INT16:
1860+
NOSWITCHFOR(BCF_BT_INT16, uint16_t, le_to_i16, i16_to_le, p, end, samples, ploidy, &bytes);
1861+
break;
1862+
case BCF_BT_INT32:
1863+
NOSWITCHFOR(BCF_BT_INT32, uint32_t, le_to_i32, i32_to_le, p, end, samples, ploidy, &bytes);
1864+
break;
18801865
}
18811866
*q = p;
18821867
return 0;
@@ -1886,14 +1871,11 @@ static int updatephasing(uint8_t *p, uint8_t *end, uint8_t **q, int set, int sam
18861871
* update44phasing - converts GT to/from v4.4 way representation
18871872
* @param h - bcf header, to get version
18881873
* @param v - pointer to bcf data
1889-
* @param setreset - whether to set or reset
1890-
* Returns 0 on success and -1 on failure
1891-
* For data read, to be converted to v44, setreset to be 1. For data write, to
1892-
* be converted to v < v44, setreset to be 0.
1874+
* Returns 0 on success and 1 on failure
18931875
* If the version in header is >= 4.4, no change is made. Otherwise 1st phasing
18941876
* is set if there are no other unphased ones.
18951877
*/
1896-
HTSLIB_EXPORT int update44phasing(bcf_hdr_t *h, bcf1_t *b, int setreset)
1878+
HTSLIB_EXPORT int update44phasing(bcf_hdr_t *h, bcf1_t *b)
18971879
{
18981880
int i, idgt = -1, ver = VCF_DEF, num, type;
18991881
uint8_t *ptr = NULL, *end = NULL;
@@ -1938,7 +1920,7 @@ HTSLIB_EXPORT int update44phasing(bcf_hdr_t *h, bcf1_t *b, int setreset)
19381920
}
19391921
if (ptr) {
19401922
//with GT and v < v44, need phase conversion
1941-
if (updatephasing(ptr, end, &ptr, setreset, b->n_sample, num, type)) return 1;
1923+
if (updatephasing(ptr, end, &ptr, b->n_sample, num, type)) return 1;
19421924
}
19431925
return 0;
19441926
}
@@ -2076,8 +2058,8 @@ static int bcf_record_check(const bcf_hdr_t *hdr, bcf1_t *rec) {
20762058
}
20772059
if (ver < VCF44 && idgt != -1 && idgt == key) {
20782060
//with GT and v < v44, need phase conversion
2079-
if (updatephasing(ptr, end, &ptr, 1, rec->n_sample, num, type)) {
2080-
err |= BCF_ERR_TAG_INVALID;
2061+
if (updatephasing(ptr, end, &ptr, rec->n_sample, num, type)) {
2062+
err |= BCF_ERR_TAG_INVALID;
20812063
}
20822064
} else {
20832065
bytes = ((size_t) num << bcf_type_shift[type]) * rec->n_sample;
@@ -2442,11 +2424,6 @@ int bcf_write(htsFile *hfp, bcf_hdr_t *h, bcf1_t *v)
24422424
return -1;
24432425
}
24442426

2445-
if (update44phasing(h, v, 0)) { //reset phasing update made after read
2446-
hts_log_error("Failed to set prorper phasing at %s:%"PRIhts_pos"", bcf_seqname_safe(h,v), v->pos+1);
2447-
return -1;
2448-
}
2449-
24502427
BGZF *fp = hfp->fp.bgzf;
24512428
uint8_t x[32];
24522429
u32_to_le(v->shared.l + 24, x); // to include six 32-bit integers

0 commit comments

Comments
 (0)