/tools/filters/sorter.py
https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 59 lines · 24 code · 11 blank · 24 comment · 4 complexity · 1c66b338b63136c2dc030fa02523ba0f MD5 · raw file
- """
- Sorts tabular data on one or more columns. All comments of the file are collected
- and placed at the beginning of the sorted output file.
-
- usage: sorter.py [options]
- -i, --input: Tabular file to be sorted
- -o, --output: Sorted output file
- -k, --key: Key (see manual for bash/sort)
-
- usage: sorter.py input output [key ...]
- """
- # 03/05/2013 guerler
- # imports
- import os, re, string, sys
- from optparse import OptionParser
- # error
- def stop_err( msg ):
- sys.stderr.write( "%s\n" % msg )
- sys.exit()
- # main
- def main():
- # define options
- parser = OptionParser()
- parser.add_option("-i", "--input")
- parser.add_option("-o", "--output")
- parser.add_option("-k", "--key", action="append")
- # parse
- options, args = parser.parse_args()
-
- try:
- # retrieve options
- input = options.input
- output = options.output
- key = [" -k" + k for k in options.key]
-
- # grep comments
- grep_comments = "(grep '^#' %s) > %s" % (input, output)
- #print grep_comments
-
- # grep and sort columns
- sort_columns = "(grep '^[^#]' %s | sort -f -t '\t' %s) >> %s" % (input, ' '.join(key), output)
- #print sort_columns
-
- # execute
- os.system(grep_comments)
- os.system(sort_columns)
-
- except Exception, ex:
- stop_err('Error running sorter.py\n' + str(ex))
- # exit
- sys.exit(0)
- if __name__ == "__main__":
- main()