annotate subtract_query.py @ 0:8a3448727cec draft

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