@@ -1819,15 +1819,14 @@ 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 ;
18321831 for (j = 0 ; j < samples ; ++ j ) { //for each sample
18331832 int anyunphased = 0 ;
@@ -1839,10 +1838,10 @@ static int updatephasing(uint8_t *p, uint8_t *end, uint8_t **q, int set, int sam
18391838 val = * (uint8_t * )p ;
18401839 break ;
18411840 case BCF_BT_INT16 :
1842- val = * ( uint16_t * ) p ;
1841+ val = le_to_i16 ( p ) ;
18431842 break ;
18441843 case BCF_BT_INT32 :
1845- val = * ( uint32_t * ) p ;
1844+ val = le_to_i32 ( p ) ;
18461845 break ;
18471846 //wont have anything bigger than 32bit for GT
18481847 default : //invalid
@@ -1854,26 +1853,28 @@ static int updatephasing(uint8_t *p, uint8_t *end, uint8_t **q, int set, int sam
18541853 anyunphased = 1 ;
18551854 }
18561855 //get to next phasing or skip the rest for this sample
1857- bytes = (anyunphased ? ploidy - k : 1 ) << bcf_type_shift [type ];
1856+ bytes = (( anyunphased || al1 & 1 ) ? ploidy - k : 1 ) << bcf_type_shift [type ];
18581857 if (end - p < bytes )
18591858 return 1 ;
18601859 p += bytes ;
1861- if (anyunphased ) {
1860+ if (anyunphased || al1 & 1 ) {
1861+ //either unphased is found or 1st one itself is set as phased
18621862 break ; //no further check required
18631863 }
18641864 }
18651865 if (!anyunphased && al1 > 1 ) { //no other unphased
18661866 /*set phased on read or make unphased on write as upto 4.3 1st
18671867 phasing is not described explicitly and has to be inferred*/
1868+ al1 |= 1 ;
18681869 switch (type ) {
18691870 case BCF_BT_INT8 :
1870- * (uint8_t * )ptr1 += upd ;
1871+ * (uint8_t * )ptr1 = al1 ;
18711872 break ;
18721873 case BCF_BT_INT16 :
1873- * ( uint16_t * ) ptr1 += upd ;
1874+ i16_to_le ( le_to_i16 (( uint8_t * ) & al1 ), ptr1 ) ;
18741875 break ;
18751876 case BCF_BT_INT32 :
1876- * ( uint32_t * ) ptr1 += upd ;
1877+ i32_to_le ( le_to_i32 (( uint8_t * ) & al1 ), ptr1 ) ;
18771878 break ;
18781879 }
18791880 }
@@ -1886,14 +1887,11 @@ static int updatephasing(uint8_t *p, uint8_t *end, uint8_t **q, int set, int sam
18861887 * update44phasing - converts GT to/from v4.4 way representation
18871888 * @param h - bcf header, to get version
18881889 * @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.
1890+ * Returns 0 on success and 1 on failure
18931891 * If the version in header is >= 4.4, no change is made. Otherwise 1st phasing
18941892 * is set if there are no other unphased ones.
18951893 */
1896- HTSLIB_EXPORT int update44phasing (bcf_hdr_t * h , bcf1_t * b , int setreset )
1894+ HTSLIB_EXPORT int update44phasing (bcf_hdr_t * h , bcf1_t * b )
18971895{
18981896 int i , idgt = -1 , ver = VCF_DEF , num , type ;
18991897 uint8_t * ptr = NULL , * end = NULL ;
@@ -1938,7 +1936,7 @@ HTSLIB_EXPORT int update44phasing(bcf_hdr_t *h, bcf1_t *b, int setreset)
19381936 }
19391937 if (ptr ) {
19401938 //with GT and v < v44, need phase conversion
1941- if (updatephasing (ptr , end , & ptr , setreset , b -> n_sample , num , type )) return 1 ;
1939+ if (updatephasing (ptr , end , & ptr , b -> n_sample , num , type )) return 1 ;
19421940 }
19431941 return 0 ;
19441942}
@@ -2076,7 +2074,7 @@ static int bcf_record_check(const bcf_hdr_t *hdr, bcf1_t *rec) {
20762074 }
20772075 if (ver < VCF44 && idgt != -1 && idgt == key ) {
20782076 //with GT and v < v44, need phase conversion
2079- if (updatephasing (ptr , end , & ptr , 1 , rec -> n_sample , num , type )) {
2077+ if (updatephasing (ptr , end , & ptr , rec -> n_sample , num , type )) {
20802078 err |= BCF_ERR_TAG_INVALID ;
20812079 }
20822080 } else {
@@ -2442,11 +2440,6 @@ int bcf_write(htsFile *hfp, bcf_hdr_t *h, bcf1_t *v)
24422440 return -1 ;
24432441 }
24442442
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-
24502443 BGZF * fp = hfp -> fp .bgzf ;
24512444 uint8_t x [32 ];
24522445 u32_to_le (v -> shared .l + 24 , x ); // to include six 32-bit integers
0 commit comments