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