1 /* Martin Asser Hansen (mail@maasha.dk) Copyright (C) 2008 - All right reserved */
11 "bed_sort - sorts a BED file.\n"
13 "Usage: bed_sort [options] <BED file>\n"
16 " [-s <int> | --sort <int>] # 1: chr AND chr_beg.\n"
17 " # 2: chr AND strand AND chr_beg.\n"
19 " # 4: strand AND chr_beg.\n"
20 " [-c <int> | --cols <int>] # Number of columns to read (default all).\n"
21 " [-d <dir> | --dir <dir> ] # Directory to use for file bound sorting.\n"
24 " bed_sort test.bed > test.bed.sort\n"
32 static struct option longopts[] = {
33 { "sort", required_argument, NULL, 's' },
34 { "cols", required_argument, NULL, 'c' },
35 { "dir", required_argument, NULL, 'd' },
40 int main( int argc, char *argv[] )
47 list_sl *entries = NULL;
49 while ( ( opt = getopt_long( argc, argv, "n:l", longopts, NULL ) ) != -1 )
52 case 's': sort = strtol( optarg, NULL, 0 ); break;
53 case 'c': cols = strtol( optarg, NULL, 0 ); break;
54 case 'd': dir = optarg; break;
59 printf( "sort: %d cols: %d dir: %s\n", sort, cols, dir );
64 if ( sort < 1 || sort > 4 )
66 fprintf( stderr, "ERROR: argument to --sort must be 1, 2, 3 or 4 - not: %d\n", sort );
70 if ( cols != 0 && cols != 3 && cols != 4 && cols != 5 && cols != 6 && cols != 12 )
72 fprintf( stderr, "ERROR: argument to --cols must be 3, 4, 5, 6 or 12 - not: %d\n", cols );
76 if ( ( sort == 2 || sort == 4 ) && ( cols > 0 && cols < 6 ) )
78 fprintf( stderr, "ERROR: cannot sort on strand with cols (%d) less than 6\n", cols );
84 fprintf( stderr, "ERROR: directory: %s does not exists\n", dir );
92 file = argv[ argc - 1 ];
94 entries = bed_entries_get( file, cols );
98 case 1: list_sl_sort( &entries, cmp_bed_sort_chr_beg ); break;
99 case 2: list_sl_sort( &entries, cmp_bed_sort_chr_strand_beg ); break;
100 case 3: list_sl_sort( &entries, cmp_bed_sort_beg ); break;
101 case 4: list_sl_sort( &entries, cmp_bed_sort_strand_beg ); break;
105 bed_entries_put( entries, cols );