]> git.donarmstrong.com Git - biopieces.git/blob - code_c/Maasha/src/bed_sort.c
minor updates
[biopieces.git] / code_c / Maasha / src / bed_sort.c
1 /* Martin Asser Hansen (mail@maasha.dk) Copyright (C) 2008 - All right reserved */
2
3 #include "common.h"
4 #include "list.h"
5 #include "ucsc.h"
6
7 static void usage()
8 {
9     fprintf( stderr, 
10         "\n"
11         "bed_sort - sorts a BED file.\n"
12         "\n"
13         "Usage: bed_sort [options] <BED file>\n"
14         "\n"
15         "Options:\n"
16         "   [-s <int> | --sort <int>]   # 1: chr AND chr_beg.\n"
17         "                               # 2: chr AND strand AND chr_beg.\n"
18         "                               # 3: 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"
22         "\n"
23         "Examples:\n"
24         "   bed_sort test.bed > test.bed.sort\n"
25         "\n"
26         );
27
28     exit( EXIT_FAILURE );
29 }
30
31
32 static struct option longopts[] = {
33     { "sort",  required_argument, NULL, 's' },
34     { "cols",  required_argument, NULL, 'c' },
35     { "dir",   required_argument, NULL, 'd' },
36     { NULL,    0,                 NULL,  0  }
37 };
38
39
40 int main( int argc, char *argv[] )
41 {
42     int      opt     = 0;
43     int      sort    = 1;
44     int      cols    = 0;
45     char    *dir     = NULL;
46     char    *file    = NULL;
47     list_sl *entries = NULL;
48
49     while ( ( opt = getopt_long( argc, argv, "n:l", longopts, NULL ) ) != -1 )
50     {
51         switch ( opt ) {
52             case 's': sort = strtol( optarg, NULL, 0 ); break;
53             case 'c': cols = strtol( optarg, NULL, 0 ); break;
54             case 'd': dir  = optarg;                    break;          
55             default:                                    break;
56         }
57     }
58
59     printf( "sort: %d  cols: %d   dir: %s\n", sort, cols, dir );
60
61     argc -= optind;
62     argv += optind;
63
64     if ( sort < 1 || sort > 4 )
65     {
66         fprintf( stderr, "ERROR: argument to --sort must be 1, 2, 3 or 4 - not: %d\n", sort );
67         abort();
68     }
69
70     if ( cols != 0 && cols != 3 && cols != 4 && cols != 5 && cols != 6 && cols != 12 )
71     {
72         fprintf( stderr, "ERROR: argument to --cols must be 3, 4, 5, 6 or 12 - not: %d\n", cols );
73         abort();
74     }
75
76     if ( ( sort == 2 || sort == 4 ) && ( cols > 0 && cols < 6 ) )
77     {
78         fprintf( stderr, "ERROR: cannot sort on strand with cols (%d) less than 6\n", cols );
79         abort();
80     }
81
82     if ( dir != NULL )
83     {
84         fprintf( stderr, "ERROR: directory: %s does not exists\n", dir );
85         abort();
86     }
87
88     if ( argc < 1 ) {
89         usage();
90     }
91
92     file = argv[ argc - 1 ];
93
94     entries = bed_entries_get( file, cols );
95
96     switch ( sort )
97     {
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;
102         default: break;
103     }
104
105     bed_entries_put( entries, cols );
106
107     return EXIT_SUCCESS;
108 }