comparison subtract_query.py @ 0:8a3448727cec draft

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