Mercurial > repos > slegras > substract_query_0_1
changeset 0:980bf1f6f37b draft default tip
Uploaded
| author | slegras |
|---|---|
| date | Mon, 27 Jul 2015 08:14:06 -0400 |
| parents | |
| children | |
| files | subtract_query.py subtract_query.xml |
| diffstat | 2 files changed, 239 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/subtract_query.py Mon Jul 27 08:14:06 2015 -0400 @@ -0,0 +1,113 @@ +#!/usr/bin/env python +# Greg Von Kuster + +""" +Subtract an entire query from another query +usage: %prog in_file_1 in_file_2 begin_col end_col output + --ignore-empty-end-cols: ignore empty end columns when subtracting +""" +import sys, re +from galaxy import eggs +import pkg_resources; pkg_resources.require( "bx-python" ) +from bx.cookbook import doc_optparse + +# Older py compatibility +try: + set() +except: + from sets import Set as set + +assert sys.version_info[:2] >= ( 2, 4 ) + +def get_lines(fname, begin_col='', end_col='', ignore_empty_end_cols=False): + lines = set([]) + i = 0 + for i, line in enumerate(file(fname)): + line = line.rstrip('\r\n') + if line and not line.startswith('#'): + if begin_col and end_col: + """Both begin_col and end_col must be integers at this point.""" + try: + line = line.split('\t') + line = '\t'.join([line[j] for j in range(begin_col-1, end_col)]) + if ignore_empty_end_cols: + # removing empty fields, we do not compare empty fields at the end of a line. + line = line.rstrip() + lines.add( line ) + except: pass + else: + if ignore_empty_end_cols: + # removing empty fields, we do not compare empty fields at the end of a line. + line = line.rstrip() + lines.add( line ) + if i: return (i+1, lines) + else: return (i, lines) + +def main(): + + # Parsing Command Line here + options, args = doc_optparse.parse( __doc__ ) + + try: + inp1_file, inp2_file, begin_col, end_col, out_file = args + except: + doc_optparse.exception() + + begin_col = begin_col.strip() + end_col = end_col.strip() + + if begin_col != 'None' or end_col != 'None': + """ + The user selected columns for restriction. We'll allow default + values for both begin_col and end_col as long as the user selected + at least one of them for restriction. + """ + if begin_col == 'None': + begin_col = end_col + elif end_col == 'None': + end_col = begin_col + begin_col = int(begin_col) + end_col = int(end_col) + """Make sure that begin_col <= end_col (switch if not)""" + if begin_col > end_col: + tmp_col = end_col + end_col = begin_col + begin_col = tmp_col + else: + begin_col = end_col = '' + + try: + fo = open(out_file,'w') + except: + print >> sys.stderr, "Unable to open output file" + sys.exit() + + """ + len1 is the number of lines in inp1_file + lines1 is the set of unique lines in inp1_file + diff1 is the number of duplicate lines removed from inp1_file + """ + len1, lines1 = get_lines(inp1_file, begin_col, end_col, options.ignore_empty_end_cols) + diff1 = len1 - len(lines1) + len2, lines2 = get_lines(inp2_file, begin_col, end_col, options.ignore_empty_end_cols) + + lines1.difference_update(lines2) + """lines1 is now the set of unique lines in inp1_file - the set of unique lines in inp2_file""" + + for line in lines1: + print >> fo, line + + fo.close() + + info_msg = 'Subtracted %d lines. ' %((len1 - diff1) - len(lines1)) + + if begin_col and end_col: + info_msg += 'Restricted to columns c' + str(begin_col) + ' thru c' + str(end_col) + '. ' + + if diff1 > 0: + info_msg += 'Eliminated %d duplicate/blank/comment/invalid lines from first query.' %diff1 + + print info_msg + +if __name__ == "__main__": + main()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/subtract_query.xml Mon Jul 27 08:14:06 2015 -0400 @@ -0,0 +1,126 @@ +<tool id="subtract_query1" name="Subtract Whole Dataset" version="0.1"> + <description>from another dataset</description> + <command interpreter="python"> + subtract_query.py $input1 $input2 $begin_col $end_col $output + #if str($ignore_empty_end_cols) == 'true': + --ignore-empty-end-cols + #end if + + </command> + <inputs> + <param format="txt" name="input2" type="data" label="Subtract" help="Second dataset" /> + <param format="txt" name="input1" type="data" label="from" help="First dataset" /> + <param name="begin_col" type="data_column" data_ref="input1" force_select="False" label="Restrict subtraction between 'begin column'" /> + <param name="end_col" type="data_column" data_ref="input1" force_select="False" label="and 'end column'" help="Specifying columns for restricting subtraction is available only for tabular formatted datasets" /> + <param name="ignore_empty_end_cols" type="boolean" label="Ignore empty columns and whitespace at end of line when subtracting"/> + </inputs> + <outputs> + <data format="input" name="output" metadata_source="input1" /> + </outputs> + <tests> + <!-- Subtract 2 non-tabular files with no column restrictions. --> + <!-- Cannot figure out why this test won't pass, it works in real time... + <test> + <param name="input1" value="1.txt" /> + <param name="input2" value="2.txt" /> + <param name="begin_col" value="None" /> + <param name="end_col" value="None" /> + <output name="output" file="subtract-query-1.dat" /> + </test> + --> + <!-- Subtract 2 tabular files with no column restrictions. --> + <test> + <param name="input1" value="eq-showbeginning.dat" /> + <param name="input2" value="eq-showtail.dat" /> + <param name="begin_col" value="None" /> + <param name="end_col" value="None" /> + <output name="output" file="subtract-query-2.dat" /> + </test> + <!-- Subtract 2 tabular files with column restrictions. --> + <test> + <param name="input1" value="eq-showbeginning.dat" /> + <param name="input2" value="eq-removebeginning.dat" /> + <param name="begin_col" value="c1" /> + <param name="end_col" value="c3" /> + <output name="output" file="subtract-query-3.dat" /> + </test> + <!-- Subtract a non-tabular file from a tabular file with no column restrictions. --> + <test> + <param name="input1" value="eq-showbeginning.dat" /> + <param name="input2" value="2.txt" /> + <param name="begin_col" value="None" /> + <param name="end_col" value="None" /> + <output name="output" file="subtract-query-4.dat" /> + </test> + <!-- Subtract 2 tabular files with no column restrictions, ignoring empty end columns. --> + <test> + <param name="input1" value="eq-showbeginning_e.dat" /> + <param name="input2" value="eq-showtail.dat" /> + <param name="begin_col" value="None" /> + <param name="end_col" value="None" /> + <param name="ignore_empty_end_cols" value="true" /> + <output name="output" file="subtract-query-2.dat" /> + </test> + </tests> + <help> + +.. class:: infomark + +**TIP:** This tool complements the tool in the **Operate on Genomic Intervals** tool set which subtracts the intervals of two datasets. + + +----- + +**Syntax** + +This tool subtracts an entire dataset from another dataset. + +- Any text format is valid. +- If both dataset formats are tabular, you may restrict the subtraction to specific columns **contained in both datasets** and the resulting dataset will include only the columns specified. +- The begin column must be less than or equal to the end column. If it is not, begin column is switched with end column. +- If begin column is specified but end column is not, end column will default to begin_column (and vice versa). +- All blank and comment lines are skipped and not included in the resulting dataset (comment lines are lines beginning with a # character). +- Duplicate lines are eliminated from both dataset prior to subtraction. If any duplicate lines were eliminated from the first dataset, the number is displayed in the resulting history item. + +----- + +**Example** + +If this is the **First dataset**:: + + chr1 4225 19670 + chr10 6 8 + chr1 24417 24420 + chr6_hla_hap2 0 150 + chr2 1 5 + chr10 2 10 + chr1 30 55 + chrY 1 20 + chr1 1225979 42287290 + chr10 7 8 + +and this is the **Second dataset**:: + + chr1 4225 19670 + chr10 6 8 + chr1 24417 24420 + chr6_hla_hap2 0 150 + chr2 1 5 + chr1 30 55 + chrY 1 20 + chr1 1225979 42287290 + +Subtracting the **Second dataset** from the **First dataset** (including all columns) will yield:: + + chr10 7 8 + chr10 2 10 + +Conversely, subtracting the **First dataset** from the **Second dataset** (including all columns) will result in an empty dataset. + +Subtracting the **Second dataset** from the **First dataset** (restricting to columns c1 and c2) will yield:: + + chr10 7 + chr10 2 + + </help> +</tool> \ No newline at end of file
