Mercurial > repos > devteam > subtract_query
comparison subtract_query.py @ 1:960d5d114184 draft
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract_query commit a1517c9d22029095120643bbe2c8fa53754dd2b7
| author | devteam |
|---|---|
| date | Wed, 11 Nov 2015 12:49:38 -0500 |
| parents | 5f6ebef89722 |
| children | ff3901618482 |
comparison
equal
deleted
inserted
replaced
| 0:5f6ebef89722 | 1:960d5d114184 |
|---|---|
| 4 """ | 4 """ |
| 5 Subtract an entire query from another query | 5 Subtract an entire query from another query |
| 6 usage: %prog in_file_1 in_file_2 begin_col end_col output | 6 usage: %prog in_file_1 in_file_2 begin_col end_col output |
| 7 --ignore-empty-end-cols: ignore empty end columns when subtracting | 7 --ignore-empty-end-cols: ignore empty end columns when subtracting |
| 8 """ | 8 """ |
| 9 import sys, re | 9 import sys |
| 10 from bx.cookbook import doc_optparse | 10 from bx.cookbook import doc_optparse |
| 11 | 11 |
| 12 # Older py compatibility | 12 # Older py compatibility |
| 13 try: | 13 try: |
| 14 set() | 14 set() |
| 15 except: | 15 except: |
| 16 from sets import Set as set | 16 from sets import Set as set |
| 17 | 17 |
| 18 assert sys.version_info[:2] >= ( 2, 4 ) | 18 assert sys.version_info[:2] >= ( 2, 4 ) |
| 19 | |
| 19 | 20 |
| 20 def get_lines(fname, begin_col='', end_col='', ignore_empty_end_cols=False): | 21 def get_lines(fname, begin_col='', end_col='', ignore_empty_end_cols=False): |
| 21 lines = set([]) | 22 lines = set([]) |
| 22 i = 0 | 23 i = 0 |
| 23 for i, line in enumerate(file(fname)): | 24 for i, line in enumerate(file(fname)): |
| 25 if line and not line.startswith('#'): | 26 if line and not line.startswith('#'): |
| 26 if begin_col and end_col: | 27 if begin_col and end_col: |
| 27 """Both begin_col and end_col must be integers at this point.""" | 28 """Both begin_col and end_col must be integers at this point.""" |
| 28 try: | 29 try: |
| 29 line = line.split('\t') | 30 line = line.split('\t') |
| 30 line = '\t'.join([line[j] for j in range(begin_col-1, end_col)]) | 31 line = '\t'.join([line[j] for j in range(begin_col - 1, end_col)]) |
| 31 if ignore_empty_end_cols: | 32 if ignore_empty_end_cols: |
| 32 # removing empty fields, we do not compare empty fields at the end of a line. | 33 # removing empty fields, we do not compare empty fields at the end of a line. |
| 33 line = line.rstrip() | 34 line = line.rstrip() |
| 34 lines.add( line ) | 35 lines.add( line ) |
| 35 except: pass | 36 except: |
| 37 pass | |
| 36 else: | 38 else: |
| 37 if ignore_empty_end_cols: | 39 if ignore_empty_end_cols: |
| 38 # removing empty fields, we do not compare empty fields at the end of a line. | 40 # removing empty fields, we do not compare empty fields at the end of a line. |
| 39 line = line.rstrip() | 41 line = line.rstrip() |
| 40 lines.add( line ) | 42 lines.add( line ) |
| 41 if i: return (i+1, lines) | 43 if i: |
| 42 else: return (i, lines) | 44 return (i + 1, lines) |
| 45 else: | |
| 46 return (i, lines) | |
| 47 | |
| 43 | 48 |
| 44 def main(): | 49 def main(): |
| 45 | |
| 46 # Parsing Command Line here | 50 # Parsing Command Line here |
| 47 options, args = doc_optparse.parse( __doc__ ) | 51 options, args = doc_optparse.parse( __doc__ ) |
| 48 | 52 |
| 49 try: | 53 try: |
| 50 inp1_file, inp2_file, begin_col, end_col, out_file = args | 54 inp1_file, inp2_file, begin_col, end_col, out_file = args |
| 51 except: | 55 except: |
| 52 doc_optparse.exception() | 56 doc_optparse.exception() |
| 53 | 57 |
| 54 begin_col = begin_col.strip() | 58 begin_col = begin_col.strip() |
| 55 end_col = end_col.strip() | 59 end_col = end_col.strip() |
| 56 | 60 |
| 57 if begin_col != 'None' or end_col != 'None': | 61 if begin_col != 'None' or end_col != 'None': |
| 58 """ | 62 """ |
| 59 The user selected columns for restriction. We'll allow default | 63 The user selected columns for restriction. We'll allow default |
| 60 values for both begin_col and end_col as long as the user selected | 64 values for both begin_col and end_col as long as the user selected |
| 61 at least one of them for restriction. | 65 at least one of them for restriction. |
| 73 begin_col = tmp_col | 77 begin_col = tmp_col |
| 74 else: | 78 else: |
| 75 begin_col = end_col = '' | 79 begin_col = end_col = '' |
| 76 | 80 |
| 77 try: | 81 try: |
| 78 fo = open(out_file,'w') | 82 fo = open(out_file, 'w') |
| 79 except: | 83 except: |
| 80 print >> sys.stderr, "Unable to open output file" | 84 print >> sys.stderr, "Unable to open output file" |
| 81 sys.exit() | 85 sys.exit() |
| 82 | 86 |
| 83 """ | 87 """ |
| 86 diff1 is the number of duplicate lines removed from inp1_file | 90 diff1 is the number of duplicate lines removed from inp1_file |
| 87 """ | 91 """ |
| 88 len1, lines1 = get_lines(inp1_file, begin_col, end_col, options.ignore_empty_end_cols) | 92 len1, lines1 = get_lines(inp1_file, begin_col, end_col, options.ignore_empty_end_cols) |
| 89 diff1 = len1 - len(lines1) | 93 diff1 = len1 - len(lines1) |
| 90 len2, lines2 = get_lines(inp2_file, begin_col, end_col, options.ignore_empty_end_cols) | 94 len2, lines2 = get_lines(inp2_file, begin_col, end_col, options.ignore_empty_end_cols) |
| 91 | 95 |
| 92 lines1.difference_update(lines2) | 96 lines1.difference_update(lines2) |
| 93 """lines1 is now the set of unique lines in inp1_file - the set of unique lines in inp2_file""" | 97 """lines1 is now the set of unique lines in inp1_file - the set of unique lines in inp2_file""" |
| 94 | 98 |
| 95 for line in lines1: | 99 for line in lines1: |
| 96 print >> fo, line | 100 print >> fo, line |
| 97 | 101 |
| 98 fo.close() | 102 fo.close() |
| 99 | 103 |
| 100 info_msg = 'Subtracted %d lines. ' %((len1 - diff1) - len(lines1)) | 104 info_msg = 'Subtracted %d lines. ' % ((len1 - diff1) - len(lines1)) |
| 101 | 105 |
| 102 if begin_col and end_col: | 106 if begin_col and end_col: |
| 103 info_msg += 'Restricted to columns c' + str(begin_col) + ' thru c' + str(end_col) + '. ' | 107 info_msg += 'Restricted to columns c' + str(begin_col) + ' thru c' + str(end_col) + '. ' |
| 104 | 108 |
| 105 if diff1 > 0: | 109 if diff1 > 0: |
| 106 info_msg += 'Eliminated %d duplicate/blank/comment/invalid lines from first query.' %diff1 | 110 info_msg += 'Eliminated %d duplicate/blank/comment/invalid lines from first query.' % diff1 |
| 107 | 111 |
| 108 print info_msg | 112 print info_msg |
| 109 | 113 |
| 110 if __name__ == "__main__": | 114 if __name__ == "__main__": |
| 111 main() | 115 main() |
