PageRenderTime 25ms CodeModel.GetById 11ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/tools/new_operations/subtract_query.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 113 lines | 90 code | 9 blank | 14 comment | 12 complexity | f915a61acdd2764902d769518e7ba8ec MD5 | raw file
  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    --ignore-empty-end-cols: ignore empty end columns when subtracting
  8"""
  9import sys, re
 10from galaxy import eggs
 11import pkg_resources; pkg_resources.require( "bx-python" )
 12from bx.cookbook import doc_optparse
 13
 14# Older py compatibility
 15try:
 16    set()
 17except:
 18    from sets import Set as set
 19
 20assert sys.version_info[:2] >= ( 2, 4 )
 21
 22def get_lines(fname, begin_col='', end_col='', ignore_empty_end_cols=False):
 23    lines = set([])
 24    i = 0
 25    for i, line in enumerate(file(fname)):
 26        line = line.rstrip('\r\n')
 27        if line and not line.startswith('#'):
 28            if begin_col and end_col:
 29                """Both begin_col and end_col must be integers at this point."""
 30                try:
 31                    line = line.split('\t')
 32                    line = '\t'.join([line[j] for j in range(begin_col-1, end_col)])
 33                    if ignore_empty_end_cols:
 34                        # removing empty fields, we do not compare empty fields at the end of a line.
 35                        line = line.rstrip()
 36                    lines.add( line )
 37                except: pass
 38            else:
 39                if ignore_empty_end_cols:
 40                    # removing empty fields, we do not compare empty fields at the end of a line.
 41                    line = line.rstrip()
 42                lines.add( line )
 43    if i: return (i+1, lines)
 44    else: return (i, lines)
 45
 46def main():
 47    
 48    # Parsing Command Line here
 49    options, args = doc_optparse.parse( __doc__ )
 50
 51    try:
 52        inp1_file, inp2_file, begin_col, end_col, out_file = args
 53    except:
 54        doc_optparse.exception()
 55    
 56    begin_col = begin_col.strip()
 57    end_col = end_col.strip()
 58    
 59    if begin_col != 'None' or end_col != 'None':
 60        """
 61        The user selected columns for restriction.  We'll allow default
 62        values for both begin_col and end_col as long as the user selected
 63        at least one of them for restriction.
 64        """
 65        if begin_col == 'None':
 66            begin_col = end_col
 67        elif end_col == 'None':
 68            end_col = begin_col
 69        begin_col = int(begin_col)
 70        end_col = int(end_col)
 71        """Make sure that begin_col <= end_col (switch if not)"""
 72        if begin_col > end_col:
 73            tmp_col = end_col
 74            end_col = begin_col
 75            begin_col = tmp_col
 76    else:
 77        begin_col = end_col = ''
 78
 79    try:
 80        fo = open(out_file,'w')
 81    except:
 82        print >> sys.stderr, "Unable to open output file"
 83        sys.exit()
 84
 85    """
 86    len1 is the number of lines in inp1_file
 87    lines1 is the set of unique lines in inp1_file
 88    diff1 is the number of duplicate lines removed from inp1_file
 89    """
 90    len1, lines1 = get_lines(inp1_file, begin_col, end_col, options.ignore_empty_end_cols)
 91    diff1 = len1 - len(lines1)
 92    len2, lines2 = get_lines(inp2_file, begin_col, end_col, options.ignore_empty_end_cols)
 93    
 94    lines1.difference_update(lines2)
 95    """lines1 is now the set of unique lines in inp1_file - the set of unique lines in inp2_file"""
 96
 97    for line in lines1:
 98        print >> fo, line
 99
100    fo.close()
101    
102    info_msg = 'Subtracted %d lines. ' %((len1 - diff1) - len(lines1))
103    
104    if begin_col and end_col:
105        info_msg += 'Restricted to columns c' + str(begin_col) + ' thru c' + str(end_col) + '. '
106
107    if diff1 > 0:
108        info_msg += 'Eliminated %d duplicate/blank/comment/invalid lines from first query.' %diff1
109    
110    print info_msg
111
112if __name__ == "__main__":
113    main()