vcf.c
/***************************************************************************
 *  Library:
 *      #include <biolibc/vcf.h>
 *      -lbiolibc -lxtend
 *
 *  Description:
 *      Read static fields (columns 1 to 9) from one line of a VCF file.
 *      This function does not read any of the sample data in columns 10
 *      and on.  Samples can be read using a loop with tsv_read_field(3).
 *
 *      If field_mask is not BL_VCF_FIELD_ALL, fields not indicated by a 1
 *      in the bit mask are discarded rather than stored in bed_feature.
 *      Possible mask values are:
 *
 *      BL_VCF_FIELD_ALL
 *      BL_VCF_FIELD_CHROM
 *      BL_VCF_FIELD_POS
 *      BL_VCF_FIELD_ID
 *      BL_VCF_FIELD_REF
 *      BL_VCF_FIELD_ALT
 *      BL_VCF_FIELD_QUAL
 *      BL_VCF_FIELD_FILTER
 *      BL_VCF_FIELD_INFO
 *      BL_VCF_FIELD_FORMAT
 *
 *  Arguments:
 *      vcf_stream  FILE stream for VCF input
 *      vcf_call    Pointer to bl_vcf_t structure to receive fields
 *      field_mask  Bit mask indicating which fields should be stored
 *
 *  Returns:
 *      BL_READ_OK upon success
 *      BL_READ_TRUNCATED if EOF is encountered while reading a call
 *      BL_READ_EOF if EOF is encountered between calls as it should be
 *
 *  Examples:
 *      FILE        *stream;
 *      bl_vcf_t  vcf_call;
 *      char        sample_data[MAX_CHARS + 1];
 *      size_t      len;
 *
 *      bl_vcf_read_static_fields(stream, &vcf_call, BL_VCF_FIELD_ALL);
 *      while ( tsv_read_field(stream, sample_data, MAX_CHARS, &len) != '\n' )
 *      {
 *          ...
 *      }
 *
 *  See also:
 *      bl_vcf_write_static_fields(3), bl_vcf_read_ss_call(3), bl_vcf_write_ss_call(3)
 *
 *  History: 
 *  Date        Name        Modification
 *  2019-12-08  Jason Bacon Begin
 ***************************************************************************/
#include 
-lbiolibc -lxtend

Read static fields (columns 1 to 9) from one line of a VCF file.
This function does not read any of the sample data in columns 10
and on.  Samples can be read using a loop with tsv_read_field(3).

If field_mask is not BL_VCF_FIELD_ALL, fields not indicated by a 1
in the bit mask are discarded rather than stored in bed_feature.
Possible mask values are.



vcf_stream  FILE stream for VCF input
vcf_call    Pointer to bl_vcf_t structure to receive fields
field_mask  Bit mask indicating which fields should be stored

BL_READ_OK upon success
BL_READ_TRUNCATED if EOF is encountered while reading a call
BL_READ_EOF if EOF is encountered between calls as it should be







Date        Name        Modification
2019-12-08  Jason Bacon Begin
#include - lbiolibc - lxtend Read static fields ( columns 1 to 9 ) from one line of a VCF file . This function does not read any of the sample data in columns 10 and on . Samples can be read using a loop with tsv_read_field ( 3 ) . If field_mask is not BL_VCF_FIELD_ALL fields not indicated by a 1 in the bit mask are discarded rather than stored in bed_feature . Possible mask values are . vcf_stream FILE stream for VCF input vcf_call Pointer to bl_vcf_t structure to receive fields field_mask Bit mask indicating which fields should be stored BL_READ_OK upon success BL_READ_TRUNCATED if EOF is encountered while reading a call BL_READ_EOF if EOF is encountered between calls as it should be Date Name Modification 2019 - 12 - 08 Jason Bacon Begin
int     bl_vcf_read_static_fields(bl_vcf_t *vcf_call, FILE *vcf_stream, 
	    vcf_field_mask_t field_mask)
{
    char    *end,
	    pos_str[BL_POSITION_MAX_DIGITS + 1];
    size_t  len;
    int     delim;
    vcf_call->ref_count = vcf_call->alt_count = vcf_call->other_count = 0;
    if ( field_mask & BL_VCF_FIELD_CHROM )
	delim = tsv_read_field(vcf_stream, vcf_call->chrom,
			BL_CHROM_MAX_CHARS, &len);
    else
    {
	delim = tsv_skip_field(vcf_stream, &len);
	strlcpy(vcf_call->chrom, ".", 2);
    }
    if ( delim == EOF )
    {
	return BL_READ_EOF;
    }
    if ( field_mask & BL_VCF_FIELD_POS )
	delim = tsv_read_field(vcf_stream, pos_str,
			BL_POSITION_MAX_DIGITS, &len);
    else
    {
	delim = tsv_skip_field(vcf_stream, &len);
	strlcpy(pos_str, "0", 2);
    }
    if ( delim == EOF )
    {
	fprintf(stderr, "bl_vcf_read_static_fields(): Got EOF reading POS: %s.\n",
		pos_str);
	return BL_READ_TRUNCATED;
    }
    else
    {
	vcf_call->pos = strtoul(pos_str, &end, 10);
	if ( *end != '\0' )
	{
	    fprintf(stderr,
		    "bl_vcf_read_static_fields(): Invalid call position: %s\n",
		    pos_str);
	    return BL_READ_TRUNCATED;
	}
    }
    if ( field_mask & BL_VCF_FIELD_ID )
	delim = tsv_read_field(vcf_stream, vcf_call->id,
			BL_VCF_ID_MAX_CHARS, &len);
    else
    {
	delim = tsv_skip_field(vcf_stream, &len);
	strlcpy(vcf_call->id, ".", 2);
    }
    if ( delim == EOF )
    {
	fprintf(stderr, "bl_vcf_read_static_fields(): Got EOF reading ID.\n");
	return BL_READ_TRUNCATED;
    }
    if ( field_mask & BL_VCF_FIELD_REF )
	delim = tsv_read_field(vcf_stream, vcf_call->ref,
			BL_VCF_REF_MAX_CHARS, &len);
    else
    {
	delim = tsv_skip_field(vcf_stream, &len);
	strlcpy(vcf_call->ref, ".", 2);
    }
    if ( delim == EOF )
    {
	fprintf(stderr, "bl_vcf_read_static_fields(): Got EOF reading REF.\n");
	return BL_READ_TRUNCATED;
    }
    if ( field_mask & BL_VCF_FIELD_ALT )
	delim = tsv_read_field(vcf_stream, vcf_call->alt,
		   BL_VCF_ALT_MAX_CHARS, &len);
    else
    {
	delim = tsv_skip_field(vcf_stream, &len);
	strlcpy(vcf_call->alt, ".", 2);
    }
    if ( delim == EOF )
    {
	fprintf(stderr, "bl_vcf_read_static_fields(): Got EOF reading ALT.\n");
	return BL_READ_TRUNCATED;
    }
    if ( field_mask & BL_VCF_FIELD_QUAL )
	delim = tsv_read_field(vcf_stream, vcf_call->qual,
		   BL_VCF_QUAL_MAX_CHARS, &len);
    else
    {
	delim = tsv_skip_field(vcf_stream, &len);
	strlcpy(vcf_call->qual, ".", 2);
    }
    if ( delim == EOF )
    {
	fprintf(stderr, "bl_vcf_read_static_fields(): Got EOF reading QUAL.\n");
	return BL_READ_TRUNCATED;
    }
    if ( field_mask & BL_VCF_FIELD_FILTER )
	delim = tsv_read_field(vcf_stream, vcf_call->filter,
		   BL_VCF_FILTER_MAX_CHARS, &len);
    else
    {
	delim = tsv_skip_field(vcf_stream, &len);
	strlcpy(vcf_call->filter, ".", 2);
    }
    if ( delim == EOF )
    {
	fprintf(stderr, "bl_vcf_read_static_fields(): Got EOF reading FILTER.\n");
	return BL_READ_TRUNCATED;
    }
    if ( field_mask & BL_VCF_FIELD_INFO )
	delim = tsv_read_field_malloc(vcf_stream, &vcf_call->info,
		   &vcf_call->info_array_size, &vcf_call->info_len);
    else
    {
	delim = tsv_skip_field(vcf_stream, &vcf_call->info_len);
	vcf_call->info = strdup(".");
	vcf_call->info_len = 1;
    }
    if ( delim == EOF )
    {
	fprintf(stderr, "bl_vcf_read_static_fields(): Got EOF reading INFO.\n");
	return BL_READ_TRUNCATED;
    }
    if ( field_mask & BL_VCF_FIELD_FORMAT )
	delim = tsv_read_field_malloc(vcf_stream, &vcf_call->format,
		   &vcf_call->format_array_size, &vcf_call->format_len);
    else
    {
	delim = tsv_skip_field(vcf_stream, &vcf_call->format_len);
	strlcpy(vcf_call->format, ".", 2);
	vcf_call->format_len = 1;
    }
    if ( delim == EOF )
    {
	fprintf(stderr, "bl_vcf_read_static_fields(): Got EOF reading FORMAT.\n");
	return BL_READ_TRUNCATED;
    }
    return BL_READ_OK;
}
int bl_vcf_read_static_fields ( bl_vcf_t * vcf_call , FILE * vcf_stream , vcf_field_mask_t field_mask ) { char * end , pos_str [ BL_POSITION_MAX_DIGITS + 1 ] ; size_t len ; int delim ; vcf_call -> ref_count = vcf_call -> alt_count = vcf_call -> other_count = 0 ; if ( field_mask & BL_VCF_FIELD_CHROM ) delim = tsv_read_field ( vcf_stream , vcf_call -> chrom , BL_CHROM_MAX_CHARS , & len ) ; else { delim = tsv_skip_field ( vcf_stream , & len ) ; strlcpy ( vcf_call -> chrom , " " , 2 ) ; } if ( delim == EOF ) { return BL_READ_EOF ; } if ( field_mask & BL_VCF_FIELD_POS ) delim = tsv_read_field ( vcf_stream , pos_str , BL_POSITION_MAX_DIGITS , & len ) ; else { delim = tsv_skip_field ( vcf_stream , & len ) ; strlcpy ( pos_str , " " , 2 ) ; } if ( delim == EOF ) { fprintf ( stderr , " \n " , pos_str ) ; return BL_READ_TRUNCATED ; } else { vcf_call -> pos = strtoul ( pos_str , & end , 10 ) ; if ( * end != ' \0 ' ) { fprintf ( stderr , " \n " , pos_str ) ; return BL_READ_TRUNCATED ; } } if ( field_mask & BL_VCF_FIELD_ID ) delim = tsv_read_field ( vcf_stream , vcf_call -> id , BL_VCF_ID_MAX_CHARS , & len ) ; else { delim = tsv_skip_field ( vcf_stream , & len ) ; strlcpy ( vcf_call -> id , " " , 2 ) ; } if ( delim == EOF ) { fprintf ( stderr , " \n " ) ; return BL_READ_TRUNCATED ; } if ( field_mask & BL_VCF_FIELD_REF ) delim = tsv_read_field ( vcf_stream , vcf_call -> ref , BL_VCF_REF_MAX_CHARS , & len ) ; else { delim = tsv_skip_field ( vcf_stream , & len ) ; strlcpy ( vcf_call -> ref , " " , 2 ) ; } if ( delim == EOF ) { fprintf ( stderr , " \n " ) ; return BL_READ_TRUNCATED ; } if ( field_mask & BL_VCF_FIELD_ALT ) delim = tsv_read_field ( vcf_stream , vcf_call -> alt , BL_VCF_ALT_MAX_CHARS , & len ) ; else { delim = tsv_skip_field ( vcf_stream , & len ) ; strlcpy ( vcf_call -> alt , " " , 2 ) ; } if ( delim == EOF ) { fprintf ( stderr , " \n " ) ; return BL_READ_TRUNCATED ; } if ( field_mask & BL_VCF_FIELD_QUAL ) delim = tsv_read_field ( vcf_stream , vcf_call -> qual , BL_VCF_QUAL_MAX_CHARS , & len ) ; else { delim = tsv_skip_field ( vcf_stream , & len ) ; strlcpy ( vcf_call -> qual , " " , 2 ) ; } if ( delim == EOF ) { fprintf ( stderr , " \n " ) ; return BL_READ_TRUNCATED ; } if ( field_mask & BL_VCF_FIELD_FILTER ) delim = tsv_read_field ( vcf_stream , vcf_call -> filter , BL_VCF_FILTER_MAX_CHARS , & len ) ; else { delim = tsv_skip_field ( vcf_stream , & len ) ; strlcpy ( vcf_call -> filter , " " , 2 ) ; } if ( delim == EOF ) { fprintf ( stderr , " \n " ) ; return BL_READ_TRUNCATED ; } if ( field_mask & BL_VCF_FIELD_INFO ) delim = tsv_read_field_malloc ( vcf_stream , & vcf_call -> info , & vcf_call -> info_array_size , & vcf_call -> info_len ) ; else { delim = tsv_skip_field ( vcf_stream , & vcf_call -> info_len ) ; vcf_call -> info = strdup ( " " ) ; vcf_call -> info_len = 1 ; } if ( delim == EOF ) { fprintf ( stderr , " \n " ) ; return BL_READ_TRUNCATED ; } if ( field_mask & BL_VCF_FIELD_FORMAT ) delim = tsv_read_field_malloc ( vcf_stream , & vcf_call -> format , & vcf_call -> format_array_size , & vcf_call -> format_len ) ; else { delim = tsv_skip_field ( vcf_stream , & vcf_call -> format_len ) ; strlcpy ( vcf_call -> format , " " , 2 ) ; vcf_call -> format_len = 1 ; } if ( delim == EOF ) { fprintf ( stderr , " \n " ) ; return BL_READ_TRUNCATED ; } return BL_READ_OK ; }
Library:
      #include <biolibc/vcf.h>
      -lbiolibc -lxtend
Library : #include <biolibc / vcf . h > - lbiolibc - lxtend
// Chromosome // fputs("bl_vcf_read_static_fields(): Info: Got EOF reading CHROM, as expected.\n", stderr); // Call position // ID // Ref // Alt // Qual // Filter // Info // Format