PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/tools/filters/sorter.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 59 lines | 24 code | 11 blank | 24 comment | 3 complexity | 1c66b338b63136c2dc030fa02523ba0f MD5 | raw file
 1"""
 2    Sorts tabular data on one or more columns. All comments of the file are collected
 3    and placed at the beginning of the sorted output file.
 4    
 5    usage: sorter.py [options]
 6    -i, --input: Tabular file to be sorted
 7    -o, --output: Sorted output file
 8    -k, --key: Key (see manual for bash/sort)
 9    
10    usage: sorter.py input output [key ...]
11"""
12# 03/05/2013 guerler
13
14# imports
15import os, re, string, sys
16from optparse import OptionParser
17
18# error
19def stop_err( msg ):
20    sys.stderr.write( "%s\n" % msg )
21    sys.exit()
22
23# main
24def main():
25    # define options
26    parser = OptionParser()
27    parser.add_option("-i", "--input")
28    parser.add_option("-o", "--output")
29    parser.add_option("-k", "--key", action="append")
30
31    # parse
32    options, args = parser.parse_args()
33    
34    try:
35        # retrieve options
36        input   = options.input
37        output  = options.output
38        key     = [" -k" + k for k in options.key]
39        
40        # grep comments
41        grep_comments = "(grep '^#' %s) > %s" % (input, output)
42        #print grep_comments
43        
44        # grep and sort columns
45        sort_columns  = "(grep '^[^#]' %s | sort -f -t '\t' %s) >> %s" % (input, ' '.join(key), output)
46        #print sort_columns
47        
48        # execute
49        os.system(grep_comments)
50        os.system(sort_columns)
51    
52    except Exception, ex:
53        stop_err('Error running sorter.py\n' + str(ex))
54
55    # exit
56    sys.exit(0)
57
58if __name__ == "__main__":
59    main()