4 #define BUFFER_SIZE 1024
6 #define BED_QID_MAX 256
7 #define BARRAY_SIZE ( 1 << 16 )
14 "bed2fixedstep - collapse overlapping BED entries with only one\n"
15 "chromosome using a score based on the last number following a _\n"
16 "in the 'name' column. if no such number is found 1 is used.\n"
18 "Usage: bed2fixedstep < <BED file(s)> > <fixedstep file>\n\n"
25 long get_score( char *str )
27 /* Martin A. Hansen, December 2008. */
29 /* Extract the last decimal number after _
30 * in a string and return that. If no number
31 * was found return 1. */
36 if ( ( c = strrchr( str, '_' ) ) != NULL ) {
37 score = strtol( &c[ 1 ], NULL , 10 );
44 int main( int argc, char *argv[] )
46 char buffer[ BUFFER_SIZE ];
47 char chr[ BED_CHR_MAX ];
48 char q_id[ BED_QID_MAX ];
54 barray *ba = barray_new( BARRAY_SIZE );
56 if ( isatty( fileno( stdin ) ) ) {
60 while ( fgets( buffer, sizeof( buffer ), stdin ) )
62 // printf( "BUFFER: %s\n", buffer );
64 if ( sscanf( buffer, "%s\t%zu\t%zu\t%s", chr, &beg, &end, q_id ) == 4 )
66 // printf( "chr: %s beg: %zu end: %zu q_id: %s\n", chr, beg, end, q_id );
68 score = ( uint ) get_score( q_id );
70 barray_interval_inc( ba, beg, end - 1, score );
74 // barray_print( ba );
80 while ( barray_interval_scan( ba, &pos, &beg, &end ) )
82 // printf( "chr: %s pos: %zu beg: %zu end: %zu\n", chr, pos, beg, end );
84 printf( "fixedStep chrom=%s start=%zu step=1\n", chr, beg + 1 );
86 for ( i = beg; i <= end; i++ ) {
87 printf( "%u\n", ba->array[ i ] );