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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
2 # Greg Von Kuster
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
3 """
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
4 Subtract an entire query from another query
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
5 usage: %prog in_file_1 in_file_2 begin_col end_col output
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
6 --ignore-empty-end-cols: ignore empty end columns when subtracting
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
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
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
12 from bx.cookbook import doc_optparse
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
13
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
14 # Older py compatibility
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
15 try:
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
16 set()
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
17 except:
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
18 from sets import Set as set
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
19
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
20 assert sys.version_info[:2] >= ( 2, 4 )
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
21
1
df4bce472400 planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 0
diff changeset
22
0
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
23 def get_lines(fname, begin_col='', end_col='', ignore_empty_end_cols=False):
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
24 lines = set([])
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
25 i = 0
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
26 for i, line in enumerate(file(fname)):
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
27 line = line.rstrip('\r\n')
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
28 if line and not line.startswith('#'):
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
29 if begin_col and end_col:
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
30 """Both begin_col and end_col must be integers at this point."""
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
31 try:
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
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
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
34 if ignore_empty_end_cols:
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
35 # removing empty fields, we do not compare empty fields at the end of a line.
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
36 line = line.rstrip()
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
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
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
40 else:
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
41 if ignore_empty_end_cols:
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
42 # removing empty fields, we do not compare empty fields at the end of a line.
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
43 line = line.rstrip()
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
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
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
50
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
51 def main():
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
52 # Parsing Command Line here
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
53 options, args = doc_optparse.parse( __doc__ )
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
54
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
55 try:
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
56 inp1_file, inp2_file, begin_col, end_col, out_file = args
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
57 except:
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
58 doc_optparse.exception()
1
df4bce472400 planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 0
diff changeset
59
0
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
60 begin_col = begin_col.strip()
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
61 end_col = end_col.strip()
1
df4bce472400 planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 0
diff changeset
62
0
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
63 if begin_col != 'None' or end_col != 'None':
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
64 """
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
65 The user selected columns for restriction. We'll allow default
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
66 values for both begin_col and end_col as long as the user selected
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
67 at least one of them for restriction.
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
68 """
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
69 if begin_col == 'None':
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
70 begin_col = end_col
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
71 elif end_col == 'None':
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
72 end_col = begin_col
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
73 begin_col = int(begin_col)
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
74 end_col = int(end_col)
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
75 """Make sure that begin_col <= end_col (switch if not)"""
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
76 if begin_col > end_col:
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
77 tmp_col = end_col
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
78 end_col = begin_col
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
79 begin_col = tmp_col
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
80 else:
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
81 begin_col = end_col = ''
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
82
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
83 try:
1
df4bce472400 planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 0
diff changeset
84 fo = open(out_file, 'w')
0
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
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
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
87 sys.exit()
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
88
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
89 """
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
90 len1 is the number of lines in inp1_file
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
91 lines1 is the set of unique lines in inp1_file
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
92 diff1 is the number of duplicate lines removed from inp1_file
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
93 """
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
94 len1, lines1 = get_lines(inp1_file, begin_col, end_col, options.ignore_empty_end_cols)
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
95 diff1 = len1 - len(lines1)
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
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
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
98 lines1.difference_update(lines2)
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
99 """lines1 is now the set of unique lines in inp1_file - the set of unique lines in inp2_file"""
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
100
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
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
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
103
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
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
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
108 if begin_col and end_col:
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
109 info_msg += 'Restricted to columns c' + str(begin_col) + ' thru c' + str(end_col) + '. '
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
110
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
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
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
116
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
117 if __name__ == "__main__":
8a3448727cec Imported from capsule None
devteam
parents:
diff changeset
118 main()