annotate tools/new_operations/subtract_query.py @ 0:9071e359b9a3

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