diff --git a/tools/new_operations/get_flanks.py b/tools/new_operations/get_flanks.py index aa5c37933ae..d3cc991bf29 100644 --- a/tools/new_operations/get_flanks.py +++ b/tools/new_operations/get_flanks.py @@ -1,12 +1,12 @@ #! /usr/bin/python -# this is a tool that creates new row/s #Done by: Guru """ Get Flanking regions. -usage: %prog input out_file size direction +usage: %prog input out_file size direction region -l, --cols=N,N,N,N: Columns for chrom, start, end, strand in file + -o, --off=N: Offset """ import sys, sets, re, os @@ -14,16 +14,17 @@ import cookbook.doc_optparse import pkg_resources pkg_resources.require( "bx-python" ) from galaxyops import * - + def main(): # Parsing Command Line here options, args = cookbook.doc_optparse.parse( __doc__ ) try: chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols ) - inp_file, out_file, size, direction = args + inp_file, out_file, size, direction, region = args + offset = int(options.off) size = int(size) - if strand_col_1 == -1: + if strand_col_1 <= 0: strand = "+" #if strand is not defined, default it to + except: cookbook.doc_optparse.exception() @@ -44,7 +45,7 @@ def main(): invalid_line = None elems = [] j=0 - for i, line in enumerate( open( inp_file )): + for i, line in enumerate( fi ): line = line.strip() if line and (not line.startswith( '#' )) and line != '': j+=1 @@ -59,40 +60,111 @@ def main(): assert strand in ['+', '-'] if direction == 'Upstream': if strand == '+': - elems[end_col_1] = elems[start_col_1] - elems[start_col_1] = str(int(elems[start_col_1]) - size) + if region == 'end': + elems[end_col_1] = str(int(elems[end_col_1]) + offset) + elems[start_col_1] = str( int(elems[end_col_1]) - size ) + else: + elems[end_col_1] = str(int(elems[start_col_1]) + offset) + elems[start_col_1] = str( int(elems[end_col_1]) - size ) elif strand == '-': - elems[start_col_1] = elems[end_col_1] - elems[end_col_1] = str(int(elems[end_col_1]) + size) + if region == 'end': + elems[start_col_1] = str(int(elems[start_col_1]) - offset) + elems[end_col_1] = str(int(elems[start_col_1]) + size) + else: + elems[start_col_1] = str(int(elems[end_col_1]) - offset) + elems[end_col_1] = str(int(elems[start_col_1]) + size) print >>fo, '\t'.join(elems) - + elif direction == 'Downstream': if strand == '-': - elems[end_col_1] = elems[start_col_1] - elems[start_col_1] = str( int(elems[start_col_1]) - size ) + if region == 'start': + elems[end_col_1] = str(int(elems[end_col_1]) - offset) + elems[start_col_1] = str( int(elems[end_col_1]) - size ) + else: + elems[end_col_1] = str(int(elems[start_col_1]) - offset) + elems[start_col_1] = str( int(elems[end_col_1]) - size ) elif strand == '+': - elems[start_col_1] = elems[end_col_1] - elems[end_col_1] = str(int(elems[end_col_1]) + size) + if region == 'start': + elems[start_col_1] = str(int(elems[start_col_1]) + offset) + elems[end_col_1] = str(int(elems[start_col_1]) + size) + else: + elems[start_col_1] = str(int(elems[end_col_1]) + offset) + elems[end_col_1] = str(int(elems[start_col_1]) + size) print >>fo, '\t'.join(elems) - + elif direction == 'Both': - newelem1 = str(int(elems[start_col_1]) - size) - newelem2 = elems[start_col_1] - newelem3 = elems[end_col_1] - newelem4 = str(int(elems[end_col_1]) + size) - elems[start_col_1]=newelem1 - elems[end_col_1]=newelem2 - print >>fo, '\t'.join(elems) - elems[start_col_1]=newelem3 - elems[end_col_1]=newelem4 - print >>fo, '\t'.join(elems) - + if strand == '-': + if region == 'start': + start = str(int(elems[end_col_1]) - offset) + end1 = str(int(start) + size) + end2 = str(int(start) - size) + elems[start_col_1]=start + elems[end_col_1]=end1 + print >>fo, '\t'.join(elems) + elems[start_col_1]=end2 + elems[end_col_1]=start + print >>fo, '\t'.join(elems) + elif region == 'end': + start = str(int(elems[start_col_1]) - offset) + end1 = str(int(start) + size) + end2 = str(int(start) - size) + elems[start_col_1]=start + elems[end_col_1]=end1 + print >>fo, '\t'.join(elems) + elems[start_col_1]=end2 + elems[end_col_1]=start + print >>fo, '\t'.join(elems) + else: + start1 = str(int(elems[end_col_1]) - offset) + end1 = str(int(start1) + size) + start2 = str(int(elems[start_col_1]) - offset) + end2 = str(int(start2) - size) + elems[start_col_1]=start1 + elems[end_col_1]=end1 + print >>fo, '\t'.join(elems) + elems[start_col_1]=end2 + elems[end_col_1]=start2 + print >>fo, '\t'.join(elems) + elif strand == '+': + if region == 'start': + start = str(int(elems[start_col_1]) + offset) + end1 = str(int(start) - size) + end2 = str(int(start) + size) + elems[start_col_1]=end1 + elems[end_col_1]=start + print >>fo, '\t'.join(elems) + elems[start_col_1]=start + elems[end_col_1]=end2 + print >>fo, '\t'.join(elems) + elif region == 'end': + start = str(int(elems[end_col_1]) + offset) + end1 = str(int(start) - size) + end2 = str(int(start) + size) + elems[start_col_1]=end1 + elems[end_col_1]=start + print >>fo, '\t'.join(elems) + elems[start_col_1]=start + elems[end_col_1]=end2 + print >>fo, '\t'.join(elems) + else: + start1 = str(int(elems[start_col_1]) + offset) + end1 = str(int(start1) - size) + start2 = str(int(elems[end_col_1]) + offset) + end2 = str(int(start2) + size) + elems[start_col_1]=end1 + elems[end_col_1]=start1 + print >>fo, '\t'.join(elems) + elems[start_col_1]=start2 + elems[end_col_1]=end2 + print >>fo, '\t'.join(elems) + except: skipped_lines += 1 if not invalid_line: first_invalid_line = i + 1 invalid_line = line fo.close() + fi.close() #If number of skipped lines = num of lines in the file, inform the user to check metadata attributes of the input file. if skipped_lines == j: @@ -100,7 +172,7 @@ def main(): sys.exit() elif skipped_lines > 0: print '(Data issue: skipped %d invalid lines starting at line #%d which is "%s")' % ( skipped_lines, first_invalid_line, invalid_line ) - print 'Flank length : %d and location : %s ' %(size, direction) + print 'Location : %s, Region : %s, Flank-length : %d, Offset : %d ' %(direction, region, size, offset) if __name__ == "__main__": main() \ No newline at end of file diff --git a/tools/new_operations/get_flanks.xml b/tools/new_operations/get_flanks.xml index 8f3c19faa69..8032c177337 100644 --- a/tools/new_operations/get_flanks.xml +++ b/tools/new_operations/get_flanks.xml @@ -1,14 +1,21 @@ returns flanking region/s for every gene - get_flanks.py $input $out_file1 $size $direction -l $input_chromCol,$input_startCol,$input_endCol,$input_strandCol + get_flanks.py $input $out_file1 $size $direction $region -o $offset -l $input_chromCol,$input_startCol,$input_endCol,$input_strandCol - + + + + + + + + @@ -17,38 +24,53 @@ - - - + + + + + + + + + + + + + -.. class:: infomark -**TIP:** Every line should contain at least 3 columns: Chromosome number, Start and Stop co-ordinates. If any of these columns is missing or if start and stop co-ordinates are not numerical, the tool may encounter exceptions and such lines are skipped as invalid. The number of invalid skipped lines is documented in the resulting history item as a "Data issue". +This tool finds the upstream and/or downstream flanking region/s of all the selected regions in the input file. -.. class:: infomark - -**TIP:** If your data is not TAB delimited, use *Edit Queries->Convert characters* +**Note:** Every line should contain at least 3 columns: Chromosome number, Start and Stop co-ordinates. If any of these columns is missing or if start and stop co-ordinates are not numerical, the tool may encounter exceptions and such lines are skipped as invalid. The number of invalid skipped lines is documented in the resulting history item as a "Data issue". ----- -**Example** - -- This tool finds the upstream and/or downstream flanking region/s of all the genes in the input file. +**Example 1** - For the following query:: - chr7 100 600 gene1 0 + - chr7 200 1000 gene2 0 - + chr22 1000 7000 NM_174568 0 + -- running **get flanks** with **flank length 50** and **location upstream** will return:: +- running get flanks with Region: Around start, Offset: -200, Flank-length: 300 and Location: Upstream will return:: - chr7 50 100 gene1 0 + - chr7 1000 1050 gene2 0 - + chr22 500 800 NM_174568 0 + +.. image:: ../static/operation_icons/flanks_ex1.gif +**Example 2** + +- For the following query:: + + chr22 1000 7000 NM_028946 0 - + +- running get flanks with Region: Whole, Offset: 200, Flank-length: 300 and Location: Downstream will return:: + + chr22 500 800 NM_028946 0 - + +.. image:: ../static/operation_icons/flanks_ex2.gif