Enhancements to 'Get flanks' tool based on Dr. Gilmour's requests.

The tool now allows the user to offset co-ordinates and get flanking regions around start and end co-ordinates separately.
This commit is contained in:
Guruprasad Anada
2007-06-08 15:46:36 +00:00
parent 7a692f610c
commit b684baba08
2 changed files with 140 additions and 46 deletions
+100 -28
View File
@@ -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()
+40 -18
View File
@@ -1,14 +1,21 @@
<tool id="get_flanks1" name="Get flanks">
<description>returns flanking region/s for every gene</description>
<command interpreter="python2.4">get_flanks.py $input $out_file1 $size $direction -l $input_chromCol,$input_startCol,$input_endCol,$input_strandCol</command>
<command interpreter="python2.4">get_flanks.py $input $out_file1 $size $direction $region -o $offset -l $input_chromCol,$input_startCol,$input_endCol,$input_strandCol</command>
<inputs>
<param format="interval" name="input" type="data" label="Select data"/>
<param name="size" size="10" type="integer" value="50" label="Length of the flanking region/s"/>
<param name="region" type="select" label="Region">
<option value="whole" selected="true">Whole feature</option>
<option value="start">Around Start</option>
<option value="end">Around End</option>
</param>
<param name="direction" type="select" label="Location of the flanking region/s">
<option value="Upstream">Upstream</option>
<option value="Downstream">Downstream</option>
<option value="Both">Both</option>
</param>
<param name="offset" size="10" type="integer" value="0" label="Offset" help="Use positive values to offset co-ordinates in the direction of transcription and negative values to offset in the opposite direction."/>
<param name="size" size="10" type="integer" value="50" label="Length of the flanking region/s"/>
</inputs>
<outputs>
@@ -17,38 +24,53 @@
<tests>
<test>
<param name="input" value="flanks_inp.bed"/>
<param name="size" value="50"/>
<param name="direction" value="Upstream"/>
<output name="out_file1" file="flanks_out.bed"/>
<param name="offset" value="-500"/>
<param name="size" value="1000"/>
<param name="direction" value="Both"/>
<param name="region" value="whole"/>
<output name="out_file1" file="flanks_out1.bed"/>
</test>
<test>
<param name="input" value="flanks_inp.bed"/>
<param name="offset" value="200"/>
<param name="size" value="1000"/>
<param name="direction" value="Downstream"/>
<param name="region" value="start" />
<output name="out_file1" file="flanks_out2.bed"/>
</test>
</tests>
<help>
.. 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-&gt;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
</help>