root/galaxy-central/tools/new_operations/subtract_query.py

リビジョン 2, 3.0 KB (コミッタ: hatakeyama, 14 年 前)

import galaxy-central

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