@@ -1819,64 +1819,49 @@ static void bcf_record_check_err(const bcf_hdr_t *hdr, bcf1_t *rec,
1819
1819
* @param p - pointer to phase value array
1820
1820
* @param end - end of array
1821
1821
* @param q - pointer to consumed data
1822
- * @param set - whether to set (while read) or reset (for write)
1823
1822
* @param samples - no. of samples in array
1824
1823
* @param ploidy - no. of phasing values per sample
1825
1824
* @param type - value type (one of BCF_BT_...)
1826
1825
* Returns 0 on success and 1 on failure
1827
1826
*/
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 )
1829
1828
{
1830
- int j , k , upd = set ? 1 : -1 ;
1829
+ int j , k ;
1831
1830
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*/ \
1866
1848
/*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 ;
1880
1865
}
1881
1866
* q = p ;
1882
1867
return 0 ;
@@ -1886,14 +1871,11 @@ static int updatephasing(uint8_t *p, uint8_t *end, uint8_t **q, int set, int sam
1886
1871
* update44phasing - converts GT to/from v4.4 way representation
1887
1872
* @param h - bcf header, to get version
1888
1873
* @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
1893
1875
* If the version in header is >= 4.4, no change is made. Otherwise 1st phasing
1894
1876
* is set if there are no other unphased ones.
1895
1877
*/
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 )
1897
1879
{
1898
1880
int i , idgt = -1 , ver = VCF_DEF , num , type ;
1899
1881
uint8_t * ptr = NULL , * end = NULL ;
@@ -1938,7 +1920,7 @@ HTSLIB_EXPORT int update44phasing(bcf_hdr_t *h, bcf1_t *b, int setreset)
1938
1920
}
1939
1921
if (ptr ) {
1940
1922
//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 ;
1942
1924
}
1943
1925
return 0 ;
1944
1926
}
@@ -2076,8 +2058,8 @@ static int bcf_record_check(const bcf_hdr_t *hdr, bcf1_t *rec) {
2076
2058
}
2077
2059
if (ver < VCF44 && idgt != -1 && idgt == key ) {
2078
2060
//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 ;
2081
2063
}
2082
2064
} else {
2083
2065
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)
2442
2424
return -1 ;
2443
2425
}
2444
2426
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
-
2450
2427
BGZF * fp = hfp -> fp .bgzf ;
2451
2428
uint8_t x [32 ];
2452
2429
u32_to_le (v -> shared .l + 24 , x ); // to include six 32-bit integers
0 commit comments