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