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